Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 1 | //===-- MipsSEISelDAGToDAG.cpp - A Dag to Dag Inst Selector for MipsSE ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Subclass of MipsDAGToDAGISel specialized for mips32/64. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 14 | #include "MipsSEISelDAGToDAG.h" |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/MipsBaseInfo.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "Mips.h" |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 17 | #include "MipsAnalyzeImmediate.h" |
| 18 | #include "MipsMachineFunction.h" |
| 19 | #include "MipsRegisterInfo.h" |
| 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CFG.h" |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GlobalValue.h" |
| 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/Intrinsics.h" |
| 30 | #include "llvm/IR/Type.h" |
Nitesh Jain | b0bc573 | 2017-01-04 09:34:37 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Dominators.h" |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | using namespace llvm; |
| 37 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "mips-isel" |
| 39 | |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 40 | bool MipsSEDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 41 | Subtarget = &static_cast<const MipsSubtarget &>(MF.getSubtarget()); |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 42 | if (Subtarget->inMips16Mode()) |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 43 | return false; |
| 44 | return MipsDAGToDAGISel::runOnMachineFunction(MF); |
| 45 | } |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 46 | |
Nitesh Jain | b0bc573 | 2017-01-04 09:34:37 +0000 | [diff] [blame] | 47 | void MipsSEDAGToDAGISel::getAnalysisUsage(AnalysisUsage &AU) const { |
| 48 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 49 | SelectionDAGISel::getAnalysisUsage(AU); |
| 50 | } |
| 51 | |
Akira Hatanaka | e86bd4f | 2013-05-03 18:37:49 +0000 | [diff] [blame] | 52 | void MipsSEDAGToDAGISel::addDSPCtrlRegOperands(bool IsDef, MachineInstr &MI, |
| 53 | MachineFunction &MF) { |
| 54 | MachineInstrBuilder MIB(MF, &MI); |
| 55 | unsigned Mask = MI.getOperand(1).getImm(); |
Daniel Sanders | 435a653 | 2016-06-14 09:29:46 +0000 | [diff] [blame] | 56 | unsigned Flag = |
| 57 | IsDef ? RegState::ImplicitDefine : RegState::Implicit | RegState::Undef; |
Akira Hatanaka | e86bd4f | 2013-05-03 18:37:49 +0000 | [diff] [blame] | 58 | |
| 59 | if (Mask & 1) |
| 60 | MIB.addReg(Mips::DSPPos, Flag); |
| 61 | |
| 62 | if (Mask & 2) |
| 63 | MIB.addReg(Mips::DSPSCount, Flag); |
| 64 | |
| 65 | if (Mask & 4) |
| 66 | MIB.addReg(Mips::DSPCarry, Flag); |
| 67 | |
| 68 | if (Mask & 8) |
| 69 | MIB.addReg(Mips::DSPOutFlag, Flag); |
| 70 | |
| 71 | if (Mask & 16) |
| 72 | MIB.addReg(Mips::DSPCCond, Flag); |
| 73 | |
| 74 | if (Mask & 32) |
| 75 | MIB.addReg(Mips::DSPEFI, Flag); |
| 76 | } |
| 77 | |
Daniel Sanders | f9aa1d1 | 2013-08-28 10:26:24 +0000 | [diff] [blame] | 78 | unsigned MipsSEDAGToDAGISel::getMSACtrlReg(const SDValue RegIdx) const { |
| 79 | switch (cast<ConstantSDNode>(RegIdx)->getZExtValue()) { |
| 80 | default: |
| 81 | llvm_unreachable("Could not map int to register"); |
| 82 | case 0: return Mips::MSAIR; |
| 83 | case 1: return Mips::MSACSR; |
| 84 | case 2: return Mips::MSAAccess; |
| 85 | case 3: return Mips::MSASave; |
| 86 | case 4: return Mips::MSAModify; |
| 87 | case 5: return Mips::MSARequest; |
| 88 | case 6: return Mips::MSAMap; |
| 89 | case 7: return Mips::MSAUnmap; |
| 90 | } |
| 91 | } |
| 92 | |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 93 | bool MipsSEDAGToDAGISel::replaceUsesWithZeroReg(MachineRegisterInfo *MRI, |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 94 | const MachineInstr& MI) { |
| 95 | unsigned DstReg = 0, ZeroReg = 0; |
| 96 | |
| 97 | // Check if MI is "addiu $dst, $zero, 0" or "daddiu $dst, $zero, 0". |
| 98 | if ((MI.getOpcode() == Mips::ADDiu) && |
| 99 | (MI.getOperand(1).getReg() == Mips::ZERO) && |
Simon Dardis | ca74dd7 | 2017-01-27 11:36:52 +0000 | [diff] [blame] | 100 | (MI.getOperand(2).isImm()) && |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 101 | (MI.getOperand(2).getImm() == 0)) { |
| 102 | DstReg = MI.getOperand(0).getReg(); |
| 103 | ZeroReg = Mips::ZERO; |
| 104 | } else if ((MI.getOpcode() == Mips::DADDiu) && |
| 105 | (MI.getOperand(1).getReg() == Mips::ZERO_64) && |
Simon Dardis | ca74dd7 | 2017-01-27 11:36:52 +0000 | [diff] [blame] | 106 | (MI.getOperand(2).isImm()) && |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 107 | (MI.getOperand(2).getImm() == 0)) { |
| 108 | DstReg = MI.getOperand(0).getReg(); |
| 109 | ZeroReg = Mips::ZERO_64; |
| 110 | } |
| 111 | |
| 112 | if (!DstReg) |
| 113 | return false; |
| 114 | |
| 115 | // Replace uses with ZeroReg. |
| 116 | for (MachineRegisterInfo::use_iterator U = MRI->use_begin(DstReg), |
| 117 | E = MRI->use_end(); U != E;) { |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 118 | MachineOperand &MO = *U; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 119 | unsigned OpNo = U.getOperandNo(); |
| 120 | MachineInstr *MI = MO.getParent(); |
| 121 | ++U; |
| 122 | |
| 123 | // Do not replace if it is a phi's operand or is tied to def operand. |
| 124 | if (MI->isPHI() || MI->isRegTiedToDefOperand(OpNo) || MI->isPseudo()) |
| 125 | continue; |
| 126 | |
Vasileios Kalintiris | 2f41268 | 2015-10-29 10:17:16 +0000 | [diff] [blame] | 127 | // Also, we have to check that the register class of the operand |
| 128 | // contains the zero register. |
| 129 | if (!MRI->getRegClass(MO.getReg())->contains(ZeroReg)) |
| 130 | continue; |
| 131 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 132 | MO.setReg(ZeroReg); |
| 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 138 | void MipsSEDAGToDAGISel::initGlobalBaseReg(MachineFunction &MF) { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 139 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 140 | |
| 141 | if (!MipsFI->globalBaseRegSet()) |
| 142 | return; |
| 143 | |
| 144 | MachineBasicBlock &MBB = MF.front(); |
| 145 | MachineBasicBlock::iterator I = MBB.begin(); |
| 146 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 147 | const TargetInstrInfo &TII = *Subtarget->getInstrInfo(); |
Petar Jovanovic | 28e2b71 | 2015-08-28 17:53:26 +0000 | [diff] [blame] | 148 | DebugLoc DL; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 149 | unsigned V0, V1, GlobalBaseReg = MipsFI->getGlobalBaseReg(); |
| 150 | const TargetRegisterClass *RC; |
Eric Christopher | d86af63 | 2015-01-29 23:27:45 +0000 | [diff] [blame] | 151 | const MipsABIInfo &ABI = static_cast<const MipsTargetMachine &>(TM).getABI(); |
| 152 | RC = (ABI.IsN64()) ? &Mips::GPR64RegClass : &Mips::GPR32RegClass; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 153 | |
| 154 | V0 = RegInfo.createVirtualRegister(RC); |
| 155 | V1 = RegInfo.createVirtualRegister(RC); |
| 156 | |
Eric Christopher | d86af63 | 2015-01-29 23:27:45 +0000 | [diff] [blame] | 157 | if (ABI.IsN64()) { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 158 | MF.getRegInfo().addLiveIn(Mips::T9_64); |
| 159 | MBB.addLiveIn(Mips::T9_64); |
| 160 | |
| 161 | // lui $v0, %hi(%neg(%gp_rel(fname))) |
| 162 | // daddu $v1, $v0, $t9 |
| 163 | // daddiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname))) |
| 164 | const GlobalValue *FName = MF.getFunction(); |
| 165 | BuildMI(MBB, I, DL, TII.get(Mips::LUi64), V0) |
| 166 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI); |
| 167 | BuildMI(MBB, I, DL, TII.get(Mips::DADDu), V1).addReg(V0) |
| 168 | .addReg(Mips::T9_64); |
| 169 | BuildMI(MBB, I, DL, TII.get(Mips::DADDiu), GlobalBaseReg).addReg(V1) |
| 170 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO); |
| 171 | return; |
| 172 | } |
| 173 | |
Rafael Espindola | b30e66b | 2016-06-28 14:33:28 +0000 | [diff] [blame] | 174 | if (!MF.getTarget().isPositionIndependent()) { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 175 | // Set global register to __gnu_local_gp. |
| 176 | // |
| 177 | // lui $v0, %hi(__gnu_local_gp) |
| 178 | // addiu $globalbasereg, $v0, %lo(__gnu_local_gp) |
| 179 | BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0) |
| 180 | .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_HI); |
| 181 | BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V0) |
| 182 | .addExternalSymbol("__gnu_local_gp", MipsII::MO_ABS_LO); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | MF.getRegInfo().addLiveIn(Mips::T9); |
| 187 | MBB.addLiveIn(Mips::T9); |
| 188 | |
Eric Christopher | d86af63 | 2015-01-29 23:27:45 +0000 | [diff] [blame] | 189 | if (ABI.IsN32()) { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 190 | // lui $v0, %hi(%neg(%gp_rel(fname))) |
| 191 | // addu $v1, $v0, $t9 |
| 192 | // addiu $globalbasereg, $v1, %lo(%neg(%gp_rel(fname))) |
| 193 | const GlobalValue *FName = MF.getFunction(); |
| 194 | BuildMI(MBB, I, DL, TII.get(Mips::LUi), V0) |
| 195 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_HI); |
| 196 | BuildMI(MBB, I, DL, TII.get(Mips::ADDu), V1).addReg(V0).addReg(Mips::T9); |
| 197 | BuildMI(MBB, I, DL, TII.get(Mips::ADDiu), GlobalBaseReg).addReg(V1) |
| 198 | .addGlobalAddress(FName, 0, MipsII::MO_GPOFF_LO); |
| 199 | return; |
| 200 | } |
| 201 | |
Eric Christopher | d86af63 | 2015-01-29 23:27:45 +0000 | [diff] [blame] | 202 | assert(ABI.IsO32()); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 203 | |
| 204 | // For O32 ABI, the following instruction sequence is emitted to initialize |
| 205 | // the global base register: |
| 206 | // |
| 207 | // 0. lui $2, %hi(_gp_disp) |
| 208 | // 1. addiu $2, $2, %lo(_gp_disp) |
| 209 | // 2. addu $globalbasereg, $2, $t9 |
| 210 | // |
| 211 | // We emit only the last instruction here. |
| 212 | // |
| 213 | // GNU linker requires that the first two instructions appear at the beginning |
| 214 | // of a function and no instructions be inserted before or between them. |
| 215 | // The two instructions are emitted during lowering to MC layer in order to |
| 216 | // avoid any reordering. |
| 217 | // |
| 218 | // Register $2 (Mips::V0) is added to the list of live-in registers to ensure |
| 219 | // the value instruction 1 (addiu) defines is valid when instruction 2 (addu) |
| 220 | // reads it. |
| 221 | MF.getRegInfo().addLiveIn(Mips::V0); |
| 222 | MBB.addLiveIn(Mips::V0); |
| 223 | BuildMI(MBB, I, DL, TII.get(Mips::ADDu), GlobalBaseReg) |
| 224 | .addReg(Mips::V0).addReg(Mips::T9); |
| 225 | } |
| 226 | |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 227 | void MipsSEDAGToDAGISel::processFunctionAfterISel(MachineFunction &MF) { |
| 228 | initGlobalBaseReg(MF); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 229 | |
| 230 | MachineRegisterInfo *MRI = &MF.getRegInfo(); |
| 231 | |
Vasileios Kalintiris | 3631139 | 2016-04-15 20:18:48 +0000 | [diff] [blame] | 232 | for (auto &MBB: MF) { |
| 233 | for (auto &MI: MBB) { |
| 234 | switch (MI.getOpcode()) { |
| 235 | case Mips::RDDSP: |
| 236 | addDSPCtrlRegOperands(false, MI, MF); |
| 237 | break; |
| 238 | case Mips::WRDSP: |
| 239 | addDSPCtrlRegOperands(true, MI, MF); |
| 240 | break; |
| 241 | default: |
| 242 | replaceUsesWithZeroReg(MRI, MI); |
| 243 | } |
Akira Hatanaka | e86bd4f | 2013-05-03 18:37:49 +0000 | [diff] [blame] | 244 | } |
Vasileios Kalintiris | 3631139 | 2016-04-15 20:18:48 +0000 | [diff] [blame] | 245 | } |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 248 | void MipsSEDAGToDAGISel::selectAddESubE(unsigned MOp, SDValue InFlag, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 249 | SDValue CmpLHS, const SDLoc &DL, |
| 250 | SDNode *Node) const { |
Akira Hatanaka | b8835b8 | 2013-03-14 18:39:25 +0000 | [diff] [blame] | 251 | unsigned Opc = InFlag.getOpcode(); (void)Opc; |
| 252 | |
| 253 | assert(((Opc == ISD::ADDC || Opc == ISD::ADDE) || |
| 254 | (Opc == ISD::SUBC || Opc == ISD::SUBE)) && |
| 255 | "(ADD|SUB)E flag operand must come from (ADD|SUB)C/E insn"); |
| 256 | |
Vasileios Kalintiris | ef96a8e | 2015-01-26 12:33:22 +0000 | [diff] [blame] | 257 | unsigned SLTuOp = Mips::SLTu, ADDuOp = Mips::ADDu; |
| 258 | if (Subtarget->isGP64bit()) { |
| 259 | SLTuOp = Mips::SLTu64; |
| 260 | ADDuOp = Mips::DADDu; |
| 261 | } |
| 262 | |
Akira Hatanaka | b8835b8 | 2013-03-14 18:39:25 +0000 | [diff] [blame] | 263 | SDValue Ops[] = { CmpLHS, InFlag.getOperand(1) }; |
| 264 | SDValue LHS = Node->getOperand(0), RHS = Node->getOperand(1); |
| 265 | EVT VT = LHS.getValueType(); |
| 266 | |
Vasileios Kalintiris | 36901dd | 2016-03-01 20:25:43 +0000 | [diff] [blame] | 267 | SDNode *Carry = CurDAG->getMachineNode(SLTuOp, DL, VT, Ops); |
| 268 | |
| 269 | if (Subtarget->isGP64bit()) { |
| 270 | // On 64-bit targets, sltu produces an i64 but our backend currently says |
| 271 | // that SLTu64 produces an i32. We need to fix this in the long run but for |
| 272 | // now, just make the DAG type-correct by asserting the upper bits are zero. |
| 273 | Carry = CurDAG->getMachineNode(Mips::SUBREG_TO_REG, DL, VT, |
| 274 | CurDAG->getTargetConstant(0, DL, VT), |
| 275 | SDValue(Carry, 0), |
| 276 | CurDAG->getTargetConstant(Mips::sub_32, DL, |
| 277 | VT)); |
| 278 | } |
| 279 | |
Vasileios Kalintiris | 18581f1 | 2015-02-27 09:01:39 +0000 | [diff] [blame] | 280 | // Generate a second addition only if we know that RHS is not a |
| 281 | // constant-zero node. |
Vasileios Kalintiris | 36901dd | 2016-03-01 20:25:43 +0000 | [diff] [blame] | 282 | SDNode *AddCarry = Carry; |
Vasileios Kalintiris | 18581f1 | 2015-02-27 09:01:39 +0000 | [diff] [blame] | 283 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS); |
| 284 | if (!C || C->getZExtValue()) |
Vasileios Kalintiris | 36901dd | 2016-03-01 20:25:43 +0000 | [diff] [blame] | 285 | AddCarry = CurDAG->getMachineNode(ADDuOp, DL, VT, SDValue(Carry, 0), RHS); |
Vasileios Kalintiris | ef96a8e | 2015-01-26 12:33:22 +0000 | [diff] [blame] | 286 | |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 287 | CurDAG->SelectNodeTo(Node, MOp, VT, MVT::Glue, LHS, SDValue(AddCarry, 0)); |
Akira Hatanaka | b8835b8 | 2013-03-14 18:39:25 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 290 | /// Match frameindex |
| 291 | bool MipsSEDAGToDAGISel::selectAddrFrameIndex(SDValue Addr, SDValue &Base, |
| 292 | SDValue &Offset) const { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 293 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 294 | EVT ValTy = Addr.getValueType(); |
| 295 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 296 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 297 | Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), ValTy); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 298 | return true; |
| 299 | } |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
| 303 | /// Match frameindex+offset and frameindex|offset |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 304 | bool MipsSEDAGToDAGISel::selectAddrFrameIndexOffset( |
| 305 | SDValue Addr, SDValue &Base, SDValue &Offset, unsigned OffsetBits, |
| 306 | unsigned ShiftAmount = 0) const { |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 307 | if (CurDAG->isBaseWithConstantOffset(Addr)) { |
| 308 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)); |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 309 | if (isIntN(OffsetBits + ShiftAmount, CN->getSExtValue())) { |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 310 | EVT ValTy = Addr.getValueType(); |
| 311 | |
| 312 | // If the first operand is a FI, get the TargetFI Node |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 313 | if (FrameIndexSDNode *FIN = |
| 314 | dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 315 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy); |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 316 | else { |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 317 | Base = Addr.getOperand(0); |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 318 | // If base is a FI, additional offset calculation is done in |
| 319 | // eliminateFrameIndex, otherwise we need to check the alignment |
Simon Pilgrim | 2ddeee1 | 2016-08-01 09:40:38 +0000 | [diff] [blame] | 320 | if (OffsetToAlignment(CN->getZExtValue(), 1ull << ShiftAmount) != 0) |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 321 | return false; |
| 322 | } |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 323 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 324 | Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr), |
| 325 | ValTy); |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 326 | return true; |
| 327 | } |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | /// ComplexPattern used on MipsInstrInfo |
| 333 | /// Used on Mips Load/Store instructions |
| 334 | bool MipsSEDAGToDAGISel::selectAddrRegImm(SDValue Addr, SDValue &Base, |
| 335 | SDValue &Offset) const { |
| 336 | // if Address is FI, get the TargetFrameIndex. |
| 337 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 338 | return true; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 339 | |
| 340 | // on PIC code Load GA |
| 341 | if (Addr.getOpcode() == MipsISD::Wrapper) { |
| 342 | Base = Addr.getOperand(0); |
| 343 | Offset = Addr.getOperand(1); |
| 344 | return true; |
| 345 | } |
| 346 | |
Rafael Espindola | b30e66b | 2016-06-28 14:33:28 +0000 | [diff] [blame] | 347 | if (!TM.isPositionIndependent()) { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 348 | if ((Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 349 | Addr.getOpcode() == ISD::TargetGlobalAddress)) |
| 350 | return false; |
| 351 | } |
| 352 | |
| 353 | // Addresses of the form FI+const or FI|const |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 354 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16)) |
| 355 | return true; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 356 | |
| 357 | // Operand is a result from an ADD. |
| 358 | if (Addr.getOpcode() == ISD::ADD) { |
| 359 | // When loading from constant pools, load the lower address part in |
| 360 | // the instruction itself. Example, instead of: |
| 361 | // lui $2, %hi($CPI1_0) |
| 362 | // addiu $2, $2, %lo($CPI1_0) |
| 363 | // lwc1 $f0, 0($2) |
| 364 | // Generate: |
| 365 | // lui $2, %hi($CPI1_0) |
| 366 | // lwc1 $f0, %lo($CPI1_0)($2) |
| 367 | if (Addr.getOperand(1).getOpcode() == MipsISD::Lo || |
| 368 | Addr.getOperand(1).getOpcode() == MipsISD::GPRel) { |
| 369 | SDValue Opnd0 = Addr.getOperand(1).getOperand(0); |
| 370 | if (isa<ConstantPoolSDNode>(Opnd0) || isa<GlobalAddressSDNode>(Opnd0) || |
| 371 | isa<JumpTableSDNode>(Opnd0)) { |
| 372 | Base = Addr.getOperand(0); |
| 373 | Offset = Opnd0; |
| 374 | return true; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return false; |
| 380 | } |
| 381 | |
Daniel Sanders | e6ed5b7 | 2013-08-28 12:04:29 +0000 | [diff] [blame] | 382 | /// ComplexPattern used on MipsInstrInfo |
| 383 | /// Used on Mips Load/Store instructions |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 384 | bool MipsSEDAGToDAGISel::selectAddrDefault(SDValue Addr, SDValue &Base, |
| 385 | SDValue &Offset) const { |
| 386 | Base = Addr; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 387 | Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Addr.getValueType()); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 388 | return true; |
| 389 | } |
| 390 | |
| 391 | bool MipsSEDAGToDAGISel::selectIntAddr(SDValue Addr, SDValue &Base, |
| 392 | SDValue &Offset) const { |
| 393 | return selectAddrRegImm(Addr, Base, Offset) || |
| 394 | selectAddrDefault(Addr, Base, Offset); |
| 395 | } |
| 396 | |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 397 | bool MipsSEDAGToDAGISel::selectAddrRegImm9(SDValue Addr, SDValue &Base, |
| 398 | SDValue &Offset) const { |
| 399 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 400 | return true; |
| 401 | |
| 402 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 9)) |
| 403 | return true; |
| 404 | |
| 405 | return false; |
| 406 | } |
| 407 | |
Zlatko Buljan | cba9f80 | 2016-07-11 07:41:56 +0000 | [diff] [blame] | 408 | /// Used on microMIPS LWC2, LDC2, SWC2 and SDC2 instructions (11-bit offset) |
| 409 | bool MipsSEDAGToDAGISel::selectAddrRegImm11(SDValue Addr, SDValue &Base, |
| 410 | SDValue &Offset) const { |
| 411 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 412 | return true; |
| 413 | |
| 414 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 11)) |
| 415 | return true; |
| 416 | |
| 417 | return false; |
| 418 | } |
| 419 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 420 | /// Used on microMIPS Load/Store unaligned instructions (12-bit offset) |
| 421 | bool MipsSEDAGToDAGISel::selectAddrRegImm12(SDValue Addr, SDValue &Base, |
| 422 | SDValue &Offset) const { |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 423 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 424 | return true; |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 425 | |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 426 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 12)) |
| 427 | return true; |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 428 | |
| 429 | return false; |
| 430 | } |
| 431 | |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 432 | bool MipsSEDAGToDAGISel::selectAddrRegImm16(SDValue Addr, SDValue &Base, |
| 433 | SDValue &Offset) const { |
| 434 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 435 | return true; |
| 436 | |
| 437 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 16)) |
| 438 | return true; |
| 439 | |
| 440 | return false; |
| 441 | } |
| 442 | |
Zlatko Buljan | cba9f80 | 2016-07-11 07:41:56 +0000 | [diff] [blame] | 443 | bool MipsSEDAGToDAGISel::selectIntAddr11MM(SDValue Addr, SDValue &Base, |
| 444 | SDValue &Offset) const { |
| 445 | return selectAddrRegImm11(Addr, Base, Offset) || |
| 446 | selectAddrDefault(Addr, Base, Offset); |
| 447 | } |
| 448 | |
| 449 | bool MipsSEDAGToDAGISel::selectIntAddr12MM(SDValue Addr, SDValue &Base, |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 450 | SDValue &Offset) const { |
| 451 | return selectAddrRegImm12(Addr, Base, Offset) || |
| 452 | selectAddrDefault(Addr, Base, Offset); |
| 453 | } |
| 454 | |
Zlatko Buljan | cba9f80 | 2016-07-11 07:41:56 +0000 | [diff] [blame] | 455 | bool MipsSEDAGToDAGISel::selectIntAddr16MM(SDValue Addr, SDValue &Base, |
| 456 | SDValue &Offset) const { |
| 457 | return selectAddrRegImm16(Addr, Base, Offset) || |
| 458 | selectAddrDefault(Addr, Base, Offset); |
| 459 | } |
| 460 | |
Zoran Jovanovic | 5a1a780 | 2015-02-04 15:43:17 +0000 | [diff] [blame] | 461 | bool MipsSEDAGToDAGISel::selectIntAddrLSL2MM(SDValue Addr, SDValue &Base, |
| 462 | SDValue &Offset) const { |
| 463 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 7)) { |
Vasileios Kalintiris | 99eeb8a | 2015-02-13 19:14:22 +0000 | [diff] [blame] | 464 | if (isa<FrameIndexSDNode>(Base)) |
Zoran Jovanovic | 5a1a780 | 2015-02-04 15:43:17 +0000 | [diff] [blame] | 465 | return false; |
Zoran Jovanovic | 5a1a780 | 2015-02-04 15:43:17 +0000 | [diff] [blame] | 466 | |
Vasileios Kalintiris | 99eeb8a | 2015-02-13 19:14:22 +0000 | [diff] [blame] | 467 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Offset)) { |
| 468 | unsigned CnstOff = CN->getZExtValue(); |
| 469 | return (CnstOff == (CnstOff & 0x3c)); |
Zoran Jovanovic | 5a1a780 | 2015-02-04 15:43:17 +0000 | [diff] [blame] | 470 | } |
Vasileios Kalintiris | 99eeb8a | 2015-02-13 19:14:22 +0000 | [diff] [blame] | 471 | |
| 472 | return false; |
Zoran Jovanovic | 5a1a780 | 2015-02-04 15:43:17 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // For all other cases where "lw" would be selected, don't select "lw16" |
| 476 | // because it would result in additional instructions to prepare operands. |
| 477 | if (selectAddrRegImm(Addr, Base, Offset)) |
| 478 | return false; |
| 479 | |
| 480 | return selectAddrDefault(Addr, Base, Offset); |
| 481 | } |
| 482 | |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 483 | bool MipsSEDAGToDAGISel::selectIntAddrSImm10(SDValue Addr, SDValue &Base, |
| 484 | SDValue &Offset) const { |
| 485 | |
| 486 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 487 | return true; |
| 488 | |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 489 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10)) |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 490 | return true; |
| 491 | |
Hrvoje Varga | 00d96ee | 2016-08-01 06:46:20 +0000 | [diff] [blame] | 492 | return selectAddrDefault(Addr, Base, Offset); |
| 493 | } |
| 494 | |
| 495 | bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl1(SDValue Addr, SDValue &Base, |
| 496 | SDValue &Offset) const { |
| 497 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 498 | return true; |
| 499 | |
| 500 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 1)) |
| 501 | return true; |
| 502 | |
| 503 | return selectAddrDefault(Addr, Base, Offset); |
| 504 | } |
| 505 | |
| 506 | bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl2(SDValue Addr, SDValue &Base, |
| 507 | SDValue &Offset) const { |
| 508 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 509 | return true; |
| 510 | |
| 511 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 2)) |
| 512 | return true; |
| 513 | |
| 514 | return selectAddrDefault(Addr, Base, Offset); |
| 515 | } |
| 516 | |
| 517 | bool MipsSEDAGToDAGISel::selectIntAddrSImm10Lsl3(SDValue Addr, SDValue &Base, |
| 518 | SDValue &Offset) const { |
| 519 | if (selectAddrFrameIndex(Addr, Base, Offset)) |
| 520 | return true; |
| 521 | |
| 522 | if (selectAddrFrameIndexOffset(Addr, Base, Offset, 10, 3)) |
| 523 | return true; |
| 524 | |
| 525 | return selectAddrDefault(Addr, Base, Offset); |
Daniel Sanders | fa961d7 | 2014-03-03 14:31:21 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 528 | // Select constant vector splats. |
| 529 | // |
| 530 | // Returns true and sets Imm if: |
| 531 | // * MSA is enabled |
| 532 | // * N is a ISD::BUILD_VECTOR representing a constant splat |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 533 | bool MipsSEDAGToDAGISel::selectVSplat(SDNode *N, APInt &Imm, |
| 534 | unsigned MinSizeInBits) const { |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 535 | if (!Subtarget->hasMSA()) |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 536 | return false; |
| 537 | |
| 538 | BuildVectorSDNode *Node = dyn_cast<BuildVectorSDNode>(N); |
| 539 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 540 | if (!Node) |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 541 | return false; |
| 542 | |
| 543 | APInt SplatValue, SplatUndef; |
| 544 | unsigned SplatBitSize; |
| 545 | bool HasAnyUndefs; |
| 546 | |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 547 | if (!Node->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, HasAnyUndefs, |
| 548 | MinSizeInBits, !Subtarget->isLittle())) |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 549 | return false; |
| 550 | |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 551 | Imm = SplatValue; |
| 552 | |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | // Select constant vector splats. |
| 557 | // |
| 558 | // In addition to the requirements of selectVSplat(), this function returns |
| 559 | // true and sets Imm if: |
| 560 | // * The splat value is the same width as the elements of the vector |
| 561 | // * The splat value fits in an integer with the specified signed-ness and |
| 562 | // width. |
| 563 | // |
| 564 | // This function looks through ISD::BITCAST nodes. |
| 565 | // TODO: This might not be appropriate for big-endian MSA since BITCAST is |
| 566 | // sometimes a shuffle in big-endian mode. |
| 567 | // |
| 568 | // It's worth noting that this function is not used as part of the selection |
| 569 | // of ldi.[bhwd] since it does not permit using the wrong-typed ldi.[bhwd] |
| 570 | // instruction to achieve the desired bit pattern. ldi.[bhwd] is selected in |
| 571 | // MipsSEDAGToDAGISel::selectNode. |
| 572 | bool MipsSEDAGToDAGISel:: |
| 573 | selectVSplatCommon(SDValue N, SDValue &Imm, bool Signed, |
| 574 | unsigned ImmBitSize) const { |
| 575 | APInt ImmValue; |
| 576 | EVT EltTy = N->getValueType(0).getVectorElementType(); |
| 577 | |
| 578 | if (N->getOpcode() == ISD::BITCAST) |
| 579 | N = N->getOperand(0); |
| 580 | |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 581 | if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 582 | ImmValue.getBitWidth() == EltTy.getSizeInBits()) { |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 583 | |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 584 | if (( Signed && ImmValue.isSignedIntN(ImmBitSize)) || |
| 585 | (!Signed && ImmValue.isIntN(ImmBitSize))) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 586 | Imm = CurDAG->getTargetConstant(ImmValue, SDLoc(N), EltTy); |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | // Select constant vector splats. |
| 595 | bool MipsSEDAGToDAGISel:: |
Daniel Sanders | 7e51fe1 | 2013-09-27 11:48:57 +0000 | [diff] [blame] | 596 | selectVSplatUimm1(SDValue N, SDValue &Imm) const { |
| 597 | return selectVSplatCommon(N, Imm, false, 1); |
| 598 | } |
| 599 | |
| 600 | bool MipsSEDAGToDAGISel:: |
| 601 | selectVSplatUimm2(SDValue N, SDValue &Imm) const { |
| 602 | return selectVSplatCommon(N, Imm, false, 2); |
| 603 | } |
| 604 | |
| 605 | bool MipsSEDAGToDAGISel:: |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 606 | selectVSplatUimm3(SDValue N, SDValue &Imm) const { |
| 607 | return selectVSplatCommon(N, Imm, false, 3); |
| 608 | } |
| 609 | |
| 610 | // Select constant vector splats. |
| 611 | bool MipsSEDAGToDAGISel:: |
| 612 | selectVSplatUimm4(SDValue N, SDValue &Imm) const { |
| 613 | return selectVSplatCommon(N, Imm, false, 4); |
| 614 | } |
| 615 | |
| 616 | // Select constant vector splats. |
| 617 | bool MipsSEDAGToDAGISel:: |
| 618 | selectVSplatUimm5(SDValue N, SDValue &Imm) const { |
| 619 | return selectVSplatCommon(N, Imm, false, 5); |
| 620 | } |
| 621 | |
| 622 | // Select constant vector splats. |
| 623 | bool MipsSEDAGToDAGISel:: |
| 624 | selectVSplatUimm6(SDValue N, SDValue &Imm) const { |
| 625 | return selectVSplatCommon(N, Imm, false, 6); |
| 626 | } |
| 627 | |
| 628 | // Select constant vector splats. |
| 629 | bool MipsSEDAGToDAGISel:: |
| 630 | selectVSplatUimm8(SDValue N, SDValue &Imm) const { |
| 631 | return selectVSplatCommon(N, Imm, false, 8); |
| 632 | } |
| 633 | |
| 634 | // Select constant vector splats. |
| 635 | bool MipsSEDAGToDAGISel:: |
| 636 | selectVSplatSimm5(SDValue N, SDValue &Imm) const { |
| 637 | return selectVSplatCommon(N, Imm, true, 5); |
| 638 | } |
| 639 | |
| 640 | // Select constant vector splats whose value is a power of 2. |
| 641 | // |
| 642 | // In addition to the requirements of selectVSplat(), this function returns |
| 643 | // true and sets Imm if: |
| 644 | // * The splat value is the same width as the elements of the vector |
| 645 | // * The splat value is a power of two. |
| 646 | // |
| 647 | // This function looks through ISD::BITCAST nodes. |
| 648 | // TODO: This might not be appropriate for big-endian MSA since BITCAST is |
| 649 | // sometimes a shuffle in big-endian mode. |
| 650 | bool MipsSEDAGToDAGISel::selectVSplatUimmPow2(SDValue N, SDValue &Imm) const { |
| 651 | APInt ImmValue; |
| 652 | EVT EltTy = N->getValueType(0).getVectorElementType(); |
| 653 | |
| 654 | if (N->getOpcode() == ISD::BITCAST) |
| 655 | N = N->getOperand(0); |
| 656 | |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 657 | if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 658 | ImmValue.getBitWidth() == EltTy.getSizeInBits()) { |
| 659 | int32_t Log2 = ImmValue.exactLogBase2(); |
| 660 | |
| 661 | if (Log2 != -1) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 662 | Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy); |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 663 | return true; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return false; |
| 668 | } |
| 669 | |
Daniel Sanders | d74b130 | 2013-10-30 14:45:14 +0000 | [diff] [blame] | 670 | // Select constant vector splats whose value only has a consecutive sequence |
| 671 | // of left-most bits set (e.g. 0b11...1100...00). |
| 672 | // |
| 673 | // In addition to the requirements of selectVSplat(), this function returns |
| 674 | // true and sets Imm if: |
| 675 | // * The splat value is the same width as the elements of the vector |
| 676 | // * The splat value is a consecutive sequence of left-most bits. |
| 677 | // |
| 678 | // This function looks through ISD::BITCAST nodes. |
| 679 | // TODO: This might not be appropriate for big-endian MSA since BITCAST is |
| 680 | // sometimes a shuffle in big-endian mode. |
| 681 | bool MipsSEDAGToDAGISel::selectVSplatMaskL(SDValue N, SDValue &Imm) const { |
| 682 | APInt ImmValue; |
| 683 | EVT EltTy = N->getValueType(0).getVectorElementType(); |
| 684 | |
| 685 | if (N->getOpcode() == ISD::BITCAST) |
| 686 | N = N->getOperand(0); |
| 687 | |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 688 | if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && |
Daniel Sanders | d74b130 | 2013-10-30 14:45:14 +0000 | [diff] [blame] | 689 | ImmValue.getBitWidth() == EltTy.getSizeInBits()) { |
| 690 | // Extract the run of set bits starting with bit zero from the bitwise |
| 691 | // inverse of ImmValue, and test that the inverse of this is the same |
| 692 | // as the original value. |
| 693 | if (ImmValue == ~(~ImmValue & ~(~ImmValue + 1))) { |
| 694 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 695 | Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N), |
| 696 | EltTy); |
Daniel Sanders | d74b130 | 2013-10-30 14:45:14 +0000 | [diff] [blame] | 697 | return true; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return false; |
| 702 | } |
| 703 | |
| 704 | // Select constant vector splats whose value only has a consecutive sequence |
| 705 | // of right-most bits set (e.g. 0b00...0011...11). |
| 706 | // |
| 707 | // In addition to the requirements of selectVSplat(), this function returns |
| 708 | // true and sets Imm if: |
| 709 | // * The splat value is the same width as the elements of the vector |
| 710 | // * The splat value is a consecutive sequence of right-most bits. |
| 711 | // |
| 712 | // This function looks through ISD::BITCAST nodes. |
| 713 | // TODO: This might not be appropriate for big-endian MSA since BITCAST is |
| 714 | // sometimes a shuffle in big-endian mode. |
| 715 | bool MipsSEDAGToDAGISel::selectVSplatMaskR(SDValue N, SDValue &Imm) const { |
| 716 | APInt ImmValue; |
| 717 | EVT EltTy = N->getValueType(0).getVectorElementType(); |
| 718 | |
| 719 | if (N->getOpcode() == ISD::BITCAST) |
| 720 | N = N->getOperand(0); |
| 721 | |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 722 | if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && |
Daniel Sanders | d74b130 | 2013-10-30 14:45:14 +0000 | [diff] [blame] | 723 | ImmValue.getBitWidth() == EltTy.getSizeInBits()) { |
| 724 | // Extract the run of set bits starting with bit zero, and test that the |
| 725 | // result is the same as the original value |
| 726 | if (ImmValue == (ImmValue & ~(ImmValue + 1))) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 727 | Imm = CurDAG->getTargetConstant(ImmValue.countPopulation(), SDLoc(N), |
| 728 | EltTy); |
Daniel Sanders | d74b130 | 2013-10-30 14:45:14 +0000 | [diff] [blame] | 729 | return true; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | return false; |
| 734 | } |
| 735 | |
Daniel Sanders | 3f6eb54 | 2013-11-12 10:45:18 +0000 | [diff] [blame] | 736 | bool MipsSEDAGToDAGISel::selectVSplatUimmInvPow2(SDValue N, |
| 737 | SDValue &Imm) const { |
| 738 | APInt ImmValue; |
| 739 | EVT EltTy = N->getValueType(0).getVectorElementType(); |
| 740 | |
| 741 | if (N->getOpcode() == ISD::BITCAST) |
| 742 | N = N->getOperand(0); |
| 743 | |
Daniel Sanders | c8cd58f | 2015-05-19 12:24:52 +0000 | [diff] [blame] | 744 | if (selectVSplat(N.getNode(), ImmValue, EltTy.getSizeInBits()) && |
Daniel Sanders | 3f6eb54 | 2013-11-12 10:45:18 +0000 | [diff] [blame] | 745 | ImmValue.getBitWidth() == EltTy.getSizeInBits()) { |
| 746 | int32_t Log2 = (~ImmValue).exactLogBase2(); |
| 747 | |
| 748 | if (Log2 != -1) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 749 | Imm = CurDAG->getTargetConstant(Log2, SDLoc(N), EltTy); |
Daniel Sanders | 3f6eb54 | 2013-11-12 10:45:18 +0000 | [diff] [blame] | 750 | return true; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return false; |
| 755 | } |
| 756 | |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 757 | bool MipsSEDAGToDAGISel::trySelect(SDNode *Node) { |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 758 | unsigned Opcode = Node->getOpcode(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 759 | SDLoc DL(Node); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 760 | |
| 761 | /// |
| 762 | // Instruction Selection not handled by the auto-generated |
| 763 | // tablegen selection should be handled here. |
| 764 | /// |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 765 | switch(Opcode) { |
| 766 | default: break; |
| 767 | |
Akira Hatanaka | b8835b8 | 2013-03-14 18:39:25 +0000 | [diff] [blame] | 768 | case ISD::SUBE: { |
| 769 | SDValue InFlag = Node->getOperand(2); |
Vasileios Kalintiris | ef96a8e | 2015-01-26 12:33:22 +0000 | [diff] [blame] | 770 | unsigned Opc = Subtarget->isGP64bit() ? Mips::DSUBu : Mips::SUBu; |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 771 | selectAddESubE(Opc, InFlag, InFlag.getOperand(0), DL, Node); |
| 772 | return true; |
Akira Hatanaka | b8835b8 | 2013-03-14 18:39:25 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 775 | case ISD::ADDE: { |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 776 | if (Subtarget->hasDSP()) // Select DSP instructions, ADDSC and ADDWC. |
Akira Hatanaka | 2f08822 | 2013-04-13 00:55:41 +0000 | [diff] [blame] | 777 | break; |
Akira Hatanaka | b8835b8 | 2013-03-14 18:39:25 +0000 | [diff] [blame] | 778 | SDValue InFlag = Node->getOperand(2); |
Vasileios Kalintiris | ef96a8e | 2015-01-26 12:33:22 +0000 | [diff] [blame] | 779 | unsigned Opc = Subtarget->isGP64bit() ? Mips::DADDu : Mips::ADDu; |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 780 | selectAddESubE(Opc, InFlag, InFlag.getValue(0), DL, Node); |
| 781 | return true; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 784 | case ISD::ConstantFP: { |
| 785 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(Node); |
| 786 | if (Node->getValueType(0) == MVT::f64 && CN->isExactlyValue(+0.0)) { |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 787 | if (Subtarget->isGP64bit()) { |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 788 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 789 | Mips::ZERO_64, MVT::i64); |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 790 | ReplaceNode(Node, |
| 791 | CurDAG->getMachineNode(Mips::DMTC1, DL, MVT::f64, Zero)); |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 792 | } else if (Subtarget->isFP64bit()) { |
Daniel Sanders | 08d3cd1 | 2013-11-18 13:12:43 +0000 | [diff] [blame] | 793 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, |
| 794 | Mips::ZERO, MVT::i32); |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 795 | ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64_64, DL, |
| 796 | MVT::f64, Zero, Zero)); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 797 | } else { |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 798 | SDValue Zero = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 799 | Mips::ZERO, MVT::i32); |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 800 | ReplaceNode(Node, CurDAG->getMachineNode(Mips::BuildPairF64, DL, |
| 801 | MVT::f64, Zero, Zero)); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 802 | } |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 803 | return true; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 804 | } |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | case ISD::Constant: { |
| 809 | const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Node); |
Simon Dardis | 6189752 | 2016-07-25 09:57:28 +0000 | [diff] [blame] | 810 | int64_t Imm = CN->getSExtValue(); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 811 | unsigned Size = CN->getValueSizeInBits(0); |
| 812 | |
Simon Dardis | 6189752 | 2016-07-25 09:57:28 +0000 | [diff] [blame] | 813 | if (isInt<32>(Imm)) |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 814 | break; |
| 815 | |
| 816 | MipsAnalyzeImmediate AnalyzeImm; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 817 | |
| 818 | const MipsAnalyzeImmediate::InstSeq &Seq = |
| 819 | AnalyzeImm.Analyze(Imm, Size, false); |
| 820 | |
| 821 | MipsAnalyzeImmediate::InstSeq::const_iterator Inst = Seq.begin(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 822 | SDLoc DL(CN); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 823 | SDNode *RegOpnd; |
| 824 | SDValue ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 825 | DL, MVT::i64); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 826 | |
| 827 | // The first instruction can be a LUi which is different from other |
| 828 | // instructions (ADDiu, ORI and SLL) in that it does not have a register |
| 829 | // operand. |
| 830 | if (Inst->Opc == Mips::LUi64) |
| 831 | RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, ImmOpnd); |
| 832 | else |
| 833 | RegOpnd = |
| 834 | CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, |
| 835 | CurDAG->getRegister(Mips::ZERO_64, MVT::i64), |
| 836 | ImmOpnd); |
| 837 | |
| 838 | // The remaining instructions in the sequence are handled here. |
| 839 | for (++Inst; Inst != Seq.end(); ++Inst) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 840 | ImmOpnd = CurDAG->getTargetConstant(SignExtend64<16>(Inst->ImmOpnd), DL, |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 841 | MVT::i64); |
| 842 | RegOpnd = CurDAG->getMachineNode(Inst->Opc, DL, MVT::i64, |
| 843 | SDValue(RegOpnd, 0), ImmOpnd); |
| 844 | } |
| 845 | |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 846 | ReplaceNode(Node, RegOpnd); |
| 847 | return true; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Daniel Sanders | f9aa1d1 | 2013-08-28 10:26:24 +0000 | [diff] [blame] | 850 | case ISD::INTRINSIC_W_CHAIN: { |
| 851 | switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { |
| 852 | default: |
| 853 | break; |
| 854 | |
| 855 | case Intrinsic::mips_cfcmsa: { |
| 856 | SDValue ChainIn = Node->getOperand(0); |
| 857 | SDValue RegIdx = Node->getOperand(2); |
| 858 | SDValue Reg = CurDAG->getCopyFromReg(ChainIn, DL, |
| 859 | getMSACtrlReg(RegIdx), MVT::i32); |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 860 | ReplaceNode(Node, Reg.getNode()); |
| 861 | return true; |
Daniel Sanders | f9aa1d1 | 2013-08-28 10:26:24 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | break; |
| 865 | } |
| 866 | |
Daniel Sanders | ba9c850 | 2013-08-28 10:44:47 +0000 | [diff] [blame] | 867 | case ISD::INTRINSIC_WO_CHAIN: { |
| 868 | switch (cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue()) { |
| 869 | default: |
| 870 | break; |
| 871 | |
| 872 | case Intrinsic::mips_move_v: |
| 873 | // Like an assignment but will always produce a move.v even if |
| 874 | // unnecessary. |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 875 | ReplaceNode(Node, CurDAG->getMachineNode(Mips::MOVE_V, DL, |
| 876 | Node->getValueType(0), |
| 877 | Node->getOperand(1))); |
| 878 | return true; |
Daniel Sanders | ba9c850 | 2013-08-28 10:44:47 +0000 | [diff] [blame] | 879 | } |
| 880 | break; |
| 881 | } |
| 882 | |
Daniel Sanders | f9aa1d1 | 2013-08-28 10:26:24 +0000 | [diff] [blame] | 883 | case ISD::INTRINSIC_VOID: { |
| 884 | switch (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { |
| 885 | default: |
| 886 | break; |
| 887 | |
| 888 | case Intrinsic::mips_ctcmsa: { |
| 889 | SDValue ChainIn = Node->getOperand(0); |
| 890 | SDValue RegIdx = Node->getOperand(2); |
| 891 | SDValue Value = Node->getOperand(3); |
| 892 | SDValue ChainOut = CurDAG->getCopyToReg(ChainIn, DL, |
| 893 | getMSACtrlReg(RegIdx), Value); |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 894 | ReplaceNode(Node, ChainOut.getNode()); |
| 895 | return true; |
Daniel Sanders | f9aa1d1 | 2013-08-28 10:26:24 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | break; |
| 899 | } |
| 900 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 901 | case MipsISD::ThreadPointer: { |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 902 | EVT PtrVT = getTargetLowering()->getPointerTy(CurDAG->getDataLayout()); |
Akira Hatanaka | 85ccf23 | 2013-08-08 21:37:32 +0000 | [diff] [blame] | 903 | unsigned RdhwrOpc, DestReg; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 904 | |
| 905 | if (PtrVT == MVT::i32) { |
| 906 | RdhwrOpc = Mips::RDHWR; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 907 | DestReg = Mips::V1; |
| 908 | } else { |
| 909 | RdhwrOpc = Mips::RDHWR64; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 910 | DestReg = Mips::V1_64; |
| 911 | } |
| 912 | |
| 913 | SDNode *Rdhwr = |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 914 | CurDAG->getMachineNode(RdhwrOpc, DL, |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 915 | Node->getValueType(0), |
Akira Hatanaka | 85ccf23 | 2013-08-08 21:37:32 +0000 | [diff] [blame] | 916 | CurDAG->getRegister(Mips::HWR29, MVT::i32)); |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 917 | SDValue Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, DestReg, |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 918 | SDValue(Rdhwr, 0)); |
Akira Hatanaka | 040d225 | 2013-03-14 18:33:23 +0000 | [diff] [blame] | 919 | SDValue ResNode = CurDAG->getCopyFromReg(Chain, DL, DestReg, PtrVT); |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 920 | ReplaceNode(Node, ResNode.getNode()); |
| 921 | return true; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 922 | } |
Akira Hatanaka | be8612f | 2013-03-30 01:36:35 +0000 | [diff] [blame] | 923 | |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 924 | case ISD::BUILD_VECTOR: { |
| 925 | // Select appropriate ldi.[bhwd] instructions for constant splats of |
| 926 | // 128-bit when MSA is enabled. Fixup any register class mismatches that |
| 927 | // occur as a result. |
| 928 | // |
| 929 | // This allows the compiler to use a wider range of immediates than would |
| 930 | // otherwise be allowed. If, for example, v4i32 could only use ldi.h then |
| 931 | // it would not be possible to load { 0x01010101, 0x01010101, 0x01010101, |
| 932 | // 0x01010101 } without using a constant pool. This would be sub-optimal |
| 933 | // when // 'ldi.b wd, 1' is capable of producing that bit-pattern in the |
| 934 | // same set/ of registers. Similarly, ldi.h isn't capable of producing { |
| 935 | // 0x00000000, 0x00000001, 0x00000000, 0x00000001 } but 'ldi.d wd, 1' can. |
| 936 | |
Simon Dardis | 7090d14 | 2017-03-10 13:27:14 +0000 | [diff] [blame] | 937 | const MipsABIInfo &ABI = |
| 938 | static_cast<const MipsTargetMachine &>(TM).getABI(); |
| 939 | |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 940 | BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Node); |
| 941 | APInt SplatValue, SplatUndef; |
| 942 | unsigned SplatBitSize; |
| 943 | bool HasAnyUndefs; |
| 944 | unsigned LdiOp; |
| 945 | EVT ResVecTy = BVN->getValueType(0); |
| 946 | EVT ViaVecTy; |
| 947 | |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 948 | if (!Subtarget->hasMSA() || !BVN->getValueType(0).is128BitVector()) |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 949 | return false; |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 950 | |
| 951 | if (!BVN->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, |
| 952 | HasAnyUndefs, 8, |
Eric Christopher | 22405e4 | 2014-07-10 17:26:51 +0000 | [diff] [blame] | 953 | !Subtarget->isLittle())) |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 954 | return false; |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 955 | |
| 956 | switch (SplatBitSize) { |
| 957 | default: |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 958 | return false; |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 959 | case 8: |
| 960 | LdiOp = Mips::LDI_B; |
| 961 | ViaVecTy = MVT::v16i8; |
| 962 | break; |
| 963 | case 16: |
| 964 | LdiOp = Mips::LDI_H; |
| 965 | ViaVecTy = MVT::v8i16; |
| 966 | break; |
| 967 | case 32: |
| 968 | LdiOp = Mips::LDI_W; |
| 969 | ViaVecTy = MVT::v4i32; |
| 970 | break; |
| 971 | case 64: |
| 972 | LdiOp = Mips::LDI_D; |
| 973 | ViaVecTy = MVT::v2i64; |
| 974 | break; |
| 975 | } |
| 976 | |
Simon Dardis | 7090d14 | 2017-03-10 13:27:14 +0000 | [diff] [blame] | 977 | SDNode *Res; |
| 978 | |
| 979 | // If we have a signed 10 bit integer, we can splat it directly. |
| 980 | // |
| 981 | // If we have something bigger we can synthesize the value into a GPR and |
| 982 | // splat from there. |
| 983 | if (SplatValue.isSignedIntN(10)) { |
| 984 | SDValue Imm = CurDAG->getTargetConstant(SplatValue, DL, |
| 985 | ViaVecTy.getVectorElementType()); |
| 986 | |
| 987 | Res = CurDAG->getMachineNode(LdiOp, DL, ViaVecTy, Imm); |
| 988 | } else if (SplatValue.isSignedIntN(16) && |
| 989 | ((ABI.IsO32() && SplatBitSize < 64) || |
| 990 | (ABI.IsN32() || ABI.IsN64()))) { |
| 991 | // Only handle signed 16 bit values when the element size is GPR width. |
| 992 | // MIPS64 can handle all the cases but MIPS32 would need to handle |
| 993 | // negative cases specifically here. Instead, handle those cases as |
| 994 | // 64bit values. |
| 995 | |
| 996 | bool Is32BitSplat = ABI.IsO32() || SplatBitSize < 64; |
| 997 | const unsigned ADDiuOp = Is32BitSplat ? Mips::ADDiu : Mips::DADDiu; |
| 998 | const MVT SplatMVT = Is32BitSplat ? MVT::i32 : MVT::i64; |
| 999 | SDValue ZeroVal = CurDAG->getRegister( |
| 1000 | Is32BitSplat ? Mips::ZERO : Mips::ZERO_64, SplatMVT); |
| 1001 | |
| 1002 | const unsigned FILLOp = |
| 1003 | SplatBitSize == 16 |
| 1004 | ? Mips::FILL_H |
| 1005 | : (SplatBitSize == 32 ? Mips::FILL_W |
| 1006 | : (SplatBitSize == 64 ? Mips::FILL_D : 0)); |
| 1007 | |
| 1008 | assert(FILLOp != 0 && "Unknown FILL Op for splat synthesis!"); |
Simon Pilgrim | cee3fc6 | 2017-03-15 23:27:43 +0000 | [diff] [blame^] | 1009 | assert((!ABI.IsO32() || (FILLOp != Mips::FILL_D)) && |
Simon Dardis | 7090d14 | 2017-03-10 13:27:14 +0000 | [diff] [blame] | 1010 | "Attempting to use fill.d on MIPS32!"); |
| 1011 | |
| 1012 | const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); |
| 1013 | SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, SplatMVT); |
| 1014 | |
| 1015 | Res = CurDAG->getMachineNode(ADDiuOp, DL, SplatMVT, ZeroVal, LoVal); |
| 1016 | Res = CurDAG->getMachineNode(FILLOp, DL, ViaVecTy, SDValue(Res, 0)); |
| 1017 | |
| 1018 | } else if (SplatValue.isSignedIntN(32) && SplatBitSize == 32) { |
| 1019 | // Only handle the cases where the splat size agrees with the size |
| 1020 | // of the SplatValue here. |
| 1021 | const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); |
| 1022 | const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue(); |
| 1023 | SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32); |
| 1024 | |
| 1025 | SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32); |
| 1026 | SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32); |
| 1027 | |
| 1028 | if (Hi) |
| 1029 | Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal); |
| 1030 | |
| 1031 | if (Lo) |
| 1032 | Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, |
| 1033 | Hi ? SDValue(Res, 0) : ZeroVal, LoVal); |
| 1034 | |
| 1035 | assert((Hi || Lo) && "Zero case reached 32 bit case splat synthesis!"); |
| 1036 | Res = CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32, SDValue(Res, 0)); |
| 1037 | |
| 1038 | } else if (SplatValue.isSignedIntN(32) && SplatBitSize == 64 && |
| 1039 | (ABI.IsN32() || ABI.IsN64())) { |
| 1040 | // N32 and N64 can perform some tricks that O32 can't for signed 32 bit |
| 1041 | // integers due to having 64bit registers. lui will cause the necessary |
| 1042 | // zero/sign extension. |
| 1043 | const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); |
| 1044 | const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue(); |
| 1045 | SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32); |
| 1046 | |
| 1047 | SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32); |
| 1048 | SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32); |
| 1049 | |
| 1050 | if (Hi) |
| 1051 | Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal); |
| 1052 | |
| 1053 | if (Lo) |
| 1054 | Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, |
| 1055 | Hi ? SDValue(Res, 0) : ZeroVal, LoVal); |
| 1056 | |
| 1057 | Res = CurDAG->getMachineNode( |
| 1058 | Mips::SUBREG_TO_REG, DL, MVT::i64, |
| 1059 | CurDAG->getTargetConstant(((Hi >> 15) & 0x1), DL, MVT::i64), |
| 1060 | SDValue(Res, 0), |
| 1061 | CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64)); |
| 1062 | |
| 1063 | Res = |
| 1064 | CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64, SDValue(Res, 0)); |
| 1065 | |
| 1066 | } else if (SplatValue.isSignedIntN(64)) { |
| 1067 | // If we have a 64 bit Splat value, we perform a similar sequence to the |
| 1068 | // above: |
| 1069 | // |
| 1070 | // MIPS32: MIPS64: |
| 1071 | // lui $res, %highest(val) lui $res, %highest(val) |
| 1072 | // ori $res, $res, %higher(val) ori $res, $res, %higher(val) |
| 1073 | // lui $res2, %hi(val) lui $res2, %hi(val) |
| 1074 | // ori $res2, %res2, %lo(val) ori $res2, %res2, %lo(val) |
| 1075 | // $res3 = fill $res2 dinsu $res, $res2, 0, 32 |
| 1076 | // $res4 = insert.w $res3[1], $res fill.d $res |
| 1077 | // splat.d $res4, 0 |
| 1078 | // |
| 1079 | // The ability to use dinsu is guaranteed as MSA requires MIPSR5. This saves |
| 1080 | // having to materialize the value by shifts and ors. |
| 1081 | // |
| 1082 | // FIXME: Implement the preferred sequence for MIPS64R6: |
| 1083 | // |
| 1084 | // MIPS64R6: |
| 1085 | // ori $res, $zero, %lo(val) |
| 1086 | // daui $res, $res, %hi(val) |
| 1087 | // dahi $res, $res, %higher(val) |
| 1088 | // dati $res, $res, %highest(cal) |
| 1089 | // fill.d $res |
| 1090 | // |
| 1091 | |
| 1092 | const unsigned Lo = SplatValue.getLoBits(16).getZExtValue(); |
| 1093 | const unsigned Hi = SplatValue.lshr(16).getLoBits(16).getZExtValue(); |
| 1094 | const unsigned Higher = SplatValue.lshr(32).getLoBits(16).getZExtValue(); |
| 1095 | const unsigned Highest = SplatValue.lshr(48).getLoBits(16).getZExtValue(); |
| 1096 | |
| 1097 | SDValue LoVal = CurDAG->getTargetConstant(Lo, DL, MVT::i32); |
| 1098 | SDValue HiVal = CurDAG->getTargetConstant(Hi, DL, MVT::i32); |
| 1099 | SDValue HigherVal = CurDAG->getTargetConstant(Higher, DL, MVT::i32); |
| 1100 | SDValue HighestVal = CurDAG->getTargetConstant(Highest, DL, MVT::i32); |
| 1101 | SDValue ZeroVal = CurDAG->getRegister(Mips::ZERO, MVT::i32); |
| 1102 | |
| 1103 | // Independent of whether we're targeting MIPS64 or not, the basic |
| 1104 | // operations are the same. Also, directly use the $zero register if |
| 1105 | // the 16 bit chunk is zero. |
| 1106 | // |
| 1107 | // For optimization purposes we always synthesize the splat value as |
| 1108 | // an i32 value, then if we're targetting MIPS64, use SUBREG_TO_REG |
| 1109 | // just before combining the values with dinsu to produce an i64. This |
| 1110 | // enables SelectionDAG to aggressively share components of splat values |
| 1111 | // where possible. |
| 1112 | // |
| 1113 | // FIXME: This is the general constant synthesis problem. This code |
| 1114 | // should be factored out into a class shared between all the |
| 1115 | // classes that need it. Specifically, for a splat size of 64 |
| 1116 | // bits that's a negative number we can do better than LUi/ORi |
| 1117 | // for the upper 32bits. |
| 1118 | |
| 1119 | if (Hi) |
| 1120 | Res = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HiVal); |
| 1121 | |
| 1122 | if (Lo) |
| 1123 | Res = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, |
| 1124 | Hi ? SDValue(Res, 0) : ZeroVal, LoVal); |
| 1125 | |
| 1126 | SDNode *HiRes; |
| 1127 | if (Highest) |
| 1128 | HiRes = CurDAG->getMachineNode(Mips::LUi, DL, MVT::i32, HighestVal); |
| 1129 | |
| 1130 | if (Higher) |
| 1131 | HiRes = CurDAG->getMachineNode(Mips::ORi, DL, MVT::i32, |
| 1132 | Highest ? SDValue(HiRes, 0) : ZeroVal, |
| 1133 | HigherVal); |
| 1134 | |
| 1135 | |
| 1136 | if (ABI.IsO32()) { |
| 1137 | Res = CurDAG->getMachineNode(Mips::FILL_W, DL, MVT::v4i32, |
| 1138 | (Hi || Lo) ? SDValue(Res, 0) : ZeroVal); |
| 1139 | |
| 1140 | Res = CurDAG->getMachineNode( |
| 1141 | Mips::INSERT_W, DL, MVT::v4i32, SDValue(Res, 0), |
| 1142 | (Highest || Higher) ? SDValue(HiRes, 0) : ZeroVal, |
| 1143 | CurDAG->getTargetConstant(1, DL, MVT::i32)); |
| 1144 | |
| 1145 | const TargetLowering *TLI = getTargetLowering(); |
| 1146 | const TargetRegisterClass *RC = |
| 1147 | TLI->getRegClassFor(ViaVecTy.getSimpleVT()); |
| 1148 | |
| 1149 | Res = CurDAG->getMachineNode( |
| 1150 | Mips::COPY_TO_REGCLASS, DL, ViaVecTy, SDValue(Res, 0), |
| 1151 | CurDAG->getTargetConstant(RC->getID(), DL, MVT::i32)); |
| 1152 | |
| 1153 | Res = CurDAG->getMachineNode( |
| 1154 | Mips::SPLATI_D, DL, MVT::v2i64, SDValue(Res, 0), |
| 1155 | CurDAG->getTargetConstant(0, DL, MVT::i32)); |
| 1156 | } else if (ABI.IsN64() || ABI.IsN32()) { |
| 1157 | |
| 1158 | SDValue Zero64Val = CurDAG->getRegister(Mips::ZERO_64, MVT::i64); |
| 1159 | const bool HiResNonZero = Highest || Higher; |
| 1160 | const bool ResNonZero = Hi || Lo; |
| 1161 | |
| 1162 | if (HiResNonZero) |
| 1163 | HiRes = CurDAG->getMachineNode( |
| 1164 | Mips::SUBREG_TO_REG, DL, MVT::i64, |
| 1165 | CurDAG->getTargetConstant(((Highest >> 15) & 0x1), DL, MVT::i64), |
| 1166 | SDValue(HiRes, 0), |
| 1167 | CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64)); |
| 1168 | |
| 1169 | if (ResNonZero) |
| 1170 | Res = CurDAG->getMachineNode( |
| 1171 | Mips::SUBREG_TO_REG, DL, MVT::i64, |
| 1172 | CurDAG->getTargetConstant(((Hi >> 15) & 0x1), DL, MVT::i64), |
| 1173 | SDValue(Res, 0), |
| 1174 | CurDAG->getTargetConstant(Mips::sub_32, DL, MVT::i64)); |
| 1175 | |
| 1176 | // We have 3 cases: |
| 1177 | // The HiRes is nonzero but Res is $zero => dsll32 HiRes, 0 |
| 1178 | // The Res is nonzero but HiRes is $zero => dinsu Res, $zero, 32, 32 |
| 1179 | // Both are non zero => dinsu Res, HiRes, 32, 32 |
| 1180 | // |
| 1181 | // The obvious "missing" case is when both are zero, but that case is |
| 1182 | // handled by the ldi case. |
| 1183 | if (ResNonZero) { |
| 1184 | SDValue Ops[4] = {HiResNonZero ? SDValue(HiRes, 0) : Zero64Val, |
| 1185 | CurDAG->getTargetConstant(64, DL, MVT::i32), |
| 1186 | CurDAG->getTargetConstant(32, DL, MVT::i32), |
| 1187 | SDValue(Res, 0)}; |
| 1188 | |
| 1189 | Res = CurDAG->getMachineNode(Mips::DINSU, DL, MVT::i64, Ops); |
| 1190 | } else if (HiResNonZero) { |
| 1191 | Res = CurDAG->getMachineNode( |
| 1192 | Mips::DSLL32, DL, MVT::i64, SDValue(HiRes, 0), |
| 1193 | CurDAG->getTargetConstant(0, DL, MVT::i32)); |
| 1194 | } else |
| 1195 | llvm_unreachable( |
| 1196 | "Zero splat value handled by non-zero 64bit splat synthesis!"); |
| 1197 | |
| 1198 | Res = CurDAG->getMachineNode(Mips::FILL_D, DL, MVT::v2i64, SDValue(Res, 0)); |
| 1199 | } else |
| 1200 | llvm_unreachable("Unknown ABI in MipsISelDAGToDAG!"); |
| 1201 | |
| 1202 | } else |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 1203 | return false; |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 1204 | |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 1205 | if (ResVecTy != ViaVecTy) { |
| 1206 | // If LdiOp is writing to a different register class to ResVecTy, then |
| 1207 | // fix it up here. This COPY_TO_REGCLASS should never cause a move.v |
| 1208 | // since the source and destination register sets contain the same |
| 1209 | // registers. |
| 1210 | const TargetLowering *TLI = getTargetLowering(); |
| 1211 | MVT ResVecTySimple = ResVecTy.getSimpleVT(); |
| 1212 | const TargetRegisterClass *RC = TLI->getRegClassFor(ResVecTySimple); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1213 | Res = CurDAG->getMachineNode(Mips::COPY_TO_REGCLASS, DL, |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 1214 | ResVecTy, SDValue(Res, 0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1215 | CurDAG->getTargetConstant(RC->getID(), DL, |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 1216 | MVT::i32)); |
| 1217 | } |
| 1218 | |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 1219 | ReplaceNode(Node, Res); |
| 1220 | return true; |
Daniel Sanders | f49dd82 | 2013-09-24 13:33:07 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Justin Bogner | eeae751 | 2016-05-13 23:55:59 +0000 | [diff] [blame] | 1225 | return false; |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 1228 | bool MipsSEDAGToDAGISel:: |
| 1229 | SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, |
| 1230 | std::vector<SDValue> &OutOps) { |
| 1231 | SDValue Base, Offset; |
| 1232 | |
| 1233 | switch(ConstraintID) { |
| 1234 | default: |
| 1235 | llvm_unreachable("Unexpected asm memory constraint"); |
| 1236 | // All memory constraints can at least accept raw pointers. |
| 1237 | case InlineAsm::Constraint_i: |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 1238 | OutOps.push_back(Op); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1239 | OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 1240 | return false; |
Daniel Sanders | c676f2a | 2015-03-24 15:19:14 +0000 | [diff] [blame] | 1241 | case InlineAsm::Constraint_m: |
| 1242 | if (selectAddrRegImm16(Op, Base, Offset)) { |
| 1243 | OutOps.push_back(Base); |
| 1244 | OutOps.push_back(Offset); |
| 1245 | return false; |
| 1246 | } |
| 1247 | OutOps.push_back(Op); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1248 | OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); |
Daniel Sanders | c676f2a | 2015-03-24 15:19:14 +0000 | [diff] [blame] | 1249 | return false; |
Daniel Sanders | 82df616 | 2015-03-30 13:27:25 +0000 | [diff] [blame] | 1250 | case InlineAsm::Constraint_R: |
| 1251 | // The 'R' constraint is supposed to be much more complicated than this. |
| 1252 | // However, it's becoming less useful due to architectural changes and |
| 1253 | // ought to be replaced by other constraints such as 'ZC'. |
| 1254 | // For now, support 9-bit signed offsets which is supportable by all |
| 1255 | // subtargets for all instructions. |
| 1256 | if (selectAddrRegImm9(Op, Base, Offset)) { |
| 1257 | OutOps.push_back(Base); |
| 1258 | OutOps.push_back(Offset); |
| 1259 | return false; |
| 1260 | } |
| 1261 | OutOps.push_back(Op); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1262 | OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); |
Daniel Sanders | 82df616 | 2015-03-30 13:27:25 +0000 | [diff] [blame] | 1263 | return false; |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 1264 | case InlineAsm::Constraint_ZC: |
| 1265 | // ZC matches whatever the pref, ll, and sc instructions can handle for the |
| 1266 | // given subtarget. |
| 1267 | if (Subtarget->inMicroMipsMode()) { |
| 1268 | // On microMIPS, they can handle 12-bit offsets. |
| 1269 | if (selectAddrRegImm12(Op, Base, Offset)) { |
| 1270 | OutOps.push_back(Base); |
| 1271 | OutOps.push_back(Offset); |
| 1272 | return false; |
| 1273 | } |
| 1274 | } else if (Subtarget->hasMips32r6()) { |
| 1275 | // On MIPS32r6/MIPS64r6, they can only handle 9-bit offsets. |
| 1276 | if (selectAddrRegImm9(Op, Base, Offset)) { |
| 1277 | OutOps.push_back(Base); |
| 1278 | OutOps.push_back(Offset); |
| 1279 | return false; |
| 1280 | } |
| 1281 | } else if (selectAddrRegImm16(Op, Base, Offset)) { |
| 1282 | // Prior to MIPS32r6/MIPS64r6, they can handle 16-bit offsets. |
| 1283 | OutOps.push_back(Base); |
| 1284 | OutOps.push_back(Offset); |
| 1285 | return false; |
| 1286 | } |
| 1287 | // In all cases, 0-bit offsets are acceptable. |
| 1288 | OutOps.push_back(Op); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1289 | OutOps.push_back(CurDAG->getTargetConstant(0, SDLoc(Op), MVT::i32)); |
Daniel Sanders | a73d8fe | 2015-03-24 11:26:34 +0000 | [diff] [blame] | 1290 | return false; |
| 1291 | } |
| 1292 | return true; |
| 1293 | } |
| 1294 | |
Daniel Sanders | 46fe655 | 2016-07-14 13:25:22 +0000 | [diff] [blame] | 1295 | FunctionPass *llvm::createMipsSEISelDag(MipsTargetMachine &TM, |
| 1296 | CodeGenOpt::Level OptLevel) { |
| 1297 | return new MipsSEDAGToDAGISel(TM, OptLevel); |
Akira Hatanaka | 30a8478 | 2013-03-14 18:27:31 +0000 | [diff] [blame] | 1298 | } |