Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===// |
| 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 | /// \file |
| 11 | /// \brief Custom DAG lowering for SI |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SIISelLowering.h" |
| 16 | #include "AMDIL.h" |
| 17 | #include "AMDILIntrinsicInfo.h" |
| 18 | #include "SIInstrInfo.h" |
| 19 | #include "SIMachineFunctionInfo.h" |
| 20 | #include "SIRegisterInfo.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 23 | #include "llvm/CodeGen/SelectionDAG.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | SITargetLowering::SITargetLowering(TargetMachine &TM) : |
| 28 | AMDGPUTargetLowering(TM), |
Christian Konig | d3b5509 | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 29 | TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())), |
| 30 | TRI(TM.getRegisterInfo()) { |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 31 | addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); |
| 32 | addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass); |
| 33 | addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass); |
| 34 | addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); |
Christian Konig | e9ba183 | 2013-02-16 11:28:30 +0000 | [diff] [blame] | 35 | addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass); |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 36 | |
Tom Stellard | 36ba909 | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 37 | addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass); |
| 38 | addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass); |
| 39 | addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass); |
| 40 | addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass); |
| 41 | addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass); |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 42 | |
| 43 | computeRegisterProperties(); |
| 44 | |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 45 | setOperationAction(ISD::ADD, MVT::i64, Legal); |
| 46 | setOperationAction(ISD::ADD, MVT::i32, Legal); |
| 47 | |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 48 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| 49 | |
| 50 | // We need to custom lower loads from the USER_SGPR address space, so we can |
| 51 | // add the SGPRs as livein registers. |
| 52 | setOperationAction(ISD::LOAD, MVT::i32, Custom); |
| 53 | setOperationAction(ISD::LOAD, MVT::i64, Custom); |
| 54 | |
| 55 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 56 | setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); |
| 57 | |
| 58 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
| 59 | setTargetDAGCombine(ISD::SELECT_CC); |
| 60 | |
| 61 | setTargetDAGCombine(ISD::SETCC); |
| 62 | } |
| 63 | |
| 64 | MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( |
| 65 | MachineInstr * MI, MachineBasicBlock * BB) const { |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 66 | MachineRegisterInfo & MRI = BB->getParent()->getRegInfo(); |
| 67 | MachineBasicBlock::iterator I = MI; |
| 68 | |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 69 | switch (MI->getOpcode()) { |
| 70 | default: |
| 71 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
| 72 | case AMDGPU::BRANCH: return BB; |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 73 | case AMDGPU::SHADER_TYPE: |
| 74 | BB->getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType = |
| 75 | MI->getOperand(0).getImm(); |
| 76 | MI->eraseFromParent(); |
| 77 | break; |
| 78 | |
| 79 | case AMDGPU::SI_INTERP: |
| 80 | LowerSI_INTERP(MI, *BB, I, MRI); |
| 81 | break; |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 82 | case AMDGPU::SI_WQM: |
| 83 | LowerSI_WQM(MI, *BB, I, MRI); |
| 84 | break; |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 85 | } |
| 86 | return BB; |
| 87 | } |
| 88 | |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 89 | void SITargetLowering::LowerSI_WQM(MachineInstr *MI, MachineBasicBlock &BB, |
| 90 | MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const { |
| 91 | BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC) |
| 92 | .addReg(AMDGPU::EXEC); |
| 93 | |
| 94 | MI->eraseFromParent(); |
| 95 | } |
| 96 | |
| 97 | void SITargetLowering::LowerSI_INTERP(MachineInstr *MI, MachineBasicBlock &BB, |
| 98 | MachineBasicBlock::iterator I, MachineRegisterInfo & MRI) const { |
| 99 | unsigned tmp = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 100 | unsigned M0 = MRI.createVirtualRegister(&AMDGPU::M0RegRegClass); |
| 101 | MachineOperand dst = MI->getOperand(0); |
| 102 | MachineOperand iReg = MI->getOperand(1); |
| 103 | MachineOperand jReg = MI->getOperand(2); |
| 104 | MachineOperand attr_chan = MI->getOperand(3); |
| 105 | MachineOperand attr = MI->getOperand(4); |
| 106 | MachineOperand params = MI->getOperand(5); |
| 107 | |
| 108 | BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::S_MOV_B32), M0) |
| 109 | .addOperand(params); |
| 110 | |
| 111 | BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P1_F32), tmp) |
| 112 | .addOperand(iReg) |
| 113 | .addOperand(attr_chan) |
| 114 | .addOperand(attr) |
| 115 | .addReg(M0); |
| 116 | |
| 117 | BuildMI(BB, I, BB.findDebugLoc(I), TII->get(AMDGPU::V_INTERP_P2_F32)) |
| 118 | .addOperand(dst) |
| 119 | .addReg(tmp) |
| 120 | .addOperand(jReg) |
| 121 | .addOperand(attr_chan) |
| 122 | .addOperand(attr) |
| 123 | .addReg(M0); |
| 124 | |
| 125 | MI->eraseFromParent(); |
| 126 | } |
| 127 | |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 128 | EVT SITargetLowering::getSetCCResultType(EVT VT) const { |
| 129 | return MVT::i1; |
| 130 | } |
| 131 | |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | // Custom DAG Lowering Operations |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | |
| 136 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 137 | switch (Op.getOpcode()) { |
| 138 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 139 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 140 | case ISD::LOAD: return LowerLOAD(Op, DAG); |
| 141 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 142 | case ISD::INTRINSIC_WO_CHAIN: { |
| 143 | unsigned IntrinsicID = |
| 144 | cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 145 | EVT VT = Op.getValueType(); |
| 146 | switch (IntrinsicID) { |
| 147 | case AMDGPUIntrinsic::SI_vs_load_buffer_index: |
| 148 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 149 | AMDGPU::VGPR0, VT); |
| 150 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
| 151 | } |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | return SDValue(); |
| 156 | } |
| 157 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 158 | /// \brief Helper function for LowerBRCOND |
| 159 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 160 | |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 161 | SDNode *Parent = Value.getNode(); |
| 162 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 163 | I != E; ++I) { |
| 164 | |
| 165 | if (I.getUse().get() != Value) |
| 166 | continue; |
| 167 | |
| 168 | if (I->getOpcode() == Opcode) |
| 169 | return *I; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /// This transforms the control flow intrinsics to get the branch destination as |
| 175 | /// last parameter, also switches branch target with BR if the need arise |
| 176 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 177 | SelectionDAG &DAG) const { |
| 178 | |
| 179 | DebugLoc DL = BRCOND.getDebugLoc(); |
| 180 | |
| 181 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 182 | SDValue Target = BRCOND.getOperand(2); |
| 183 | SDNode *BR = 0; |
| 184 | |
| 185 | if (Intr->getOpcode() == ISD::SETCC) { |
| 186 | // As long as we negate the condition everything is fine |
| 187 | SDNode *SetCC = Intr; |
| 188 | assert(SetCC->getConstantOperandVal(1) == 1); |
NAKAMURA Takumi | e13a2a3 | 2013-01-07 11:14:44 +0000 | [diff] [blame] | 189 | assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 190 | ISD::SETNE); |
Tom Stellard | 6b7d99d | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 191 | Intr = SetCC->getOperand(0).getNode(); |
| 192 | |
| 193 | } else { |
| 194 | // Get the target from BR if we don't negate the condition |
| 195 | BR = findUser(BRCOND, ISD::BR); |
| 196 | Target = BR->getOperand(1); |
| 197 | } |
| 198 | |
| 199 | assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); |
| 200 | |
| 201 | // Build the result and |
| 202 | SmallVector<EVT, 4> Res; |
| 203 | for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i) |
| 204 | Res.push_back(Intr->getValueType(i)); |
| 205 | |
| 206 | // operands of the new intrinsic call |
| 207 | SmallVector<SDValue, 4> Ops; |
| 208 | Ops.push_back(BRCOND.getOperand(0)); |
| 209 | for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i) |
| 210 | Ops.push_back(Intr->getOperand(i)); |
| 211 | Ops.push_back(Target); |
| 212 | |
| 213 | // build the new intrinsic call |
| 214 | SDNode *Result = DAG.getNode( |
| 215 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
| 216 | DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode(); |
| 217 | |
| 218 | if (BR) { |
| 219 | // Give the branch instruction our target |
| 220 | SDValue Ops[] = { |
| 221 | BR->getOperand(0), |
| 222 | BRCOND.getOperand(2) |
| 223 | }; |
| 224 | DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2); |
| 225 | } |
| 226 | |
| 227 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 228 | |
| 229 | // Copy the intrinsic results to registers |
| 230 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 231 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 232 | if (!CopyToReg) |
| 233 | continue; |
| 234 | |
| 235 | Chain = DAG.getCopyToReg( |
| 236 | Chain, DL, |
| 237 | CopyToReg->getOperand(1), |
| 238 | SDValue(Result, i - 1), |
| 239 | SDValue()); |
| 240 | |
| 241 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 242 | } |
| 243 | |
| 244 | // Remove the old intrinsic from the chain |
| 245 | DAG.ReplaceAllUsesOfValueWith( |
| 246 | SDValue(Intr, Intr->getNumValues() - 1), |
| 247 | Intr->getOperand(0)); |
| 248 | |
| 249 | return Chain; |
Tom Stellard | f98f2ce | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
| 253 | EVT VT = Op.getValueType(); |
| 254 | LoadSDNode *Ptr = dyn_cast<LoadSDNode>(Op); |
| 255 | |
| 256 | assert(Ptr); |
| 257 | |
| 258 | unsigned AddrSpace = Ptr->getPointerInfo().getAddrSpace(); |
| 259 | |
| 260 | // We only need to lower USER_SGPR address space loads |
| 261 | if (AddrSpace != AMDGPUAS::USER_SGPR_ADDRESS) { |
| 262 | return SDValue(); |
| 263 | } |
| 264 | |
| 265 | // Loads from the USER_SGPR address space can only have constant value |
| 266 | // pointers. |
| 267 | ConstantSDNode *BasePtr = dyn_cast<ConstantSDNode>(Ptr->getBasePtr()); |
| 268 | assert(BasePtr); |
| 269 | |
| 270 | unsigned TypeDwordWidth = VT.getSizeInBits() / 32; |
| 271 | const TargetRegisterClass * dstClass; |
| 272 | switch (TypeDwordWidth) { |
| 273 | default: |
| 274 | assert(!"USER_SGPR value size not implemented"); |
| 275 | return SDValue(); |
| 276 | case 1: |
| 277 | dstClass = &AMDGPU::SReg_32RegClass; |
| 278 | break; |
| 279 | case 2: |
| 280 | dstClass = &AMDGPU::SReg_64RegClass; |
| 281 | break; |
| 282 | } |
| 283 | uint64_t Index = BasePtr->getZExtValue(); |
| 284 | assert(Index % TypeDwordWidth == 0 && "USER_SGPR not properly aligned"); |
| 285 | unsigned SGPRIndex = Index / TypeDwordWidth; |
| 286 | unsigned Reg = dstClass->getRegister(SGPRIndex); |
| 287 | |
| 288 | DAG.ReplaceAllUsesOfValueWith(Op, CreateLiveInRegister(DAG, dstClass, Reg, |
| 289 | VT)); |
| 290 | return SDValue(); |
| 291 | } |
| 292 | |
| 293 | SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { |
| 294 | SDValue LHS = Op.getOperand(0); |
| 295 | SDValue RHS = Op.getOperand(1); |
| 296 | SDValue True = Op.getOperand(2); |
| 297 | SDValue False = Op.getOperand(3); |
| 298 | SDValue CC = Op.getOperand(4); |
| 299 | EVT VT = Op.getValueType(); |
| 300 | DebugLoc DL = Op.getDebugLoc(); |
| 301 | |
| 302 | // Possible Min/Max pattern |
| 303 | SDValue MinMax = LowerMinMax(Op, DAG); |
| 304 | if (MinMax.getNode()) { |
| 305 | return MinMax; |
| 306 | } |
| 307 | |
| 308 | SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC); |
| 309 | return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False); |
| 310 | } |
| 311 | |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | // Custom DAG optimizations |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | |
| 316 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 317 | DAGCombinerInfo &DCI) const { |
| 318 | SelectionDAG &DAG = DCI.DAG; |
| 319 | DebugLoc DL = N->getDebugLoc(); |
| 320 | EVT VT = N->getValueType(0); |
| 321 | |
| 322 | switch (N->getOpcode()) { |
| 323 | default: break; |
| 324 | case ISD::SELECT_CC: { |
| 325 | N->dump(); |
| 326 | ConstantSDNode *True, *False; |
| 327 | // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc) |
| 328 | if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 329 | && (False = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 330 | && True->isAllOnesValue() |
| 331 | && False->isNullValue() |
| 332 | && VT == MVT::i1) { |
| 333 | return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0), |
| 334 | N->getOperand(1), N->getOperand(4)); |
| 335 | |
| 336 | } |
| 337 | break; |
| 338 | } |
| 339 | case ISD::SETCC: { |
| 340 | SDValue Arg0 = N->getOperand(0); |
| 341 | SDValue Arg1 = N->getOperand(1); |
| 342 | SDValue CC = N->getOperand(2); |
| 343 | ConstantSDNode * C = NULL; |
| 344 | ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get(); |
| 345 | |
| 346 | // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne) |
| 347 | if (VT == MVT::i1 |
| 348 | && Arg0.getOpcode() == ISD::SIGN_EXTEND |
| 349 | && Arg0.getOperand(0).getValueType() == MVT::i1 |
| 350 | && (C = dyn_cast<ConstantSDNode>(Arg1)) |
| 351 | && C->isNullValue() |
| 352 | && CCOp == ISD::SETNE) { |
| 353 | return SimplifySetCC(VT, Arg0.getOperand(0), |
| 354 | DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL); |
| 355 | } |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | return SDValue(); |
| 360 | } |
Christian Konig | c018eca | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 361 | |
Christian Konig | d3b5509 | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 362 | /// \brief Test if RegClass is one of the VSrc classes |
| 363 | static bool isVSrc(unsigned RegClass) { |
| 364 | return AMDGPU::VSrc_32RegClassID == RegClass || |
| 365 | AMDGPU::VSrc_64RegClassID == RegClass; |
| 366 | } |
| 367 | |
| 368 | /// \brief Test if RegClass is one of the SSrc classes |
| 369 | static bool isSSrc(unsigned RegClass) { |
| 370 | return AMDGPU::SSrc_32RegClassID == RegClass || |
| 371 | AMDGPU::SSrc_64RegClassID == RegClass; |
| 372 | } |
| 373 | |
| 374 | /// \brief Analyze the possible immediate value Op |
| 375 | /// |
| 376 | /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate |
| 377 | /// and the immediate value if it's a literal immediate |
| 378 | int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { |
| 379 | |
| 380 | union { |
| 381 | int32_t I; |
| 382 | float F; |
| 383 | } Imm; |
| 384 | |
| 385 | if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) |
| 386 | Imm.I = Node->getSExtValue(); |
| 387 | else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) |
| 388 | Imm.F = Node->getValueAPF().convertToFloat(); |
| 389 | else |
| 390 | return -1; // It isn't an immediate |
| 391 | |
| 392 | if ((Imm.I >= -16 && Imm.I <= 64) || |
| 393 | Imm.F == 0.5f || Imm.F == -0.5f || |
| 394 | Imm.F == 1.0f || Imm.F == -1.0f || |
| 395 | Imm.F == 2.0f || Imm.F == -2.0f || |
| 396 | Imm.F == 4.0f || Imm.F == -4.0f) |
| 397 | return 0; // It's an inline immediate |
| 398 | |
| 399 | return Imm.I; // It's a literal immediate |
| 400 | } |
| 401 | |
| 402 | /// \brief Try to fold an immediate directly into an instruction |
| 403 | bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate, |
| 404 | bool &ScalarSlotUsed) const { |
| 405 | |
| 406 | MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand); |
| 407 | if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode())) |
| 408 | return false; |
| 409 | |
| 410 | const SDValue &Op = Mov->getOperand(0); |
| 411 | int32_t Value = analyzeImmediate(Op.getNode()); |
| 412 | if (Value == -1) { |
| 413 | // Not an immediate at all |
| 414 | return false; |
| 415 | |
| 416 | } else if (Value == 0) { |
| 417 | // Inline immediates can always be fold |
| 418 | Operand = Op; |
| 419 | return true; |
| 420 | |
| 421 | } else if (Value == Immediate) { |
| 422 | // Already fold literal immediate |
| 423 | Operand = Op; |
| 424 | return true; |
| 425 | |
| 426 | } else if (!ScalarSlotUsed && !Immediate) { |
| 427 | // Fold this literal immediate |
| 428 | ScalarSlotUsed = true; |
| 429 | Immediate = Value; |
| 430 | Operand = Op; |
| 431 | return true; |
| 432 | |
| 433 | } |
| 434 | |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | /// \brief Does "Op" fit into register class "RegClass" ? |
| 439 | bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, SDValue &Op, |
| 440 | unsigned RegClass) const { |
| 441 | |
| 442 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 443 | SDNode *Node = Op.getNode(); |
| 444 | |
| 445 | int OpClass; |
| 446 | if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) { |
| 447 | const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode()); |
| 448 | OpClass = Desc.OpInfo[Op.getResNo()].RegClass; |
| 449 | |
| 450 | } else if (Node->getOpcode() == ISD::CopyFromReg) { |
| 451 | RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode()); |
| 452 | OpClass = MRI.getRegClass(Reg->getReg())->getID(); |
| 453 | |
| 454 | } else |
| 455 | return false; |
| 456 | |
| 457 | if (OpClass == -1) |
| 458 | return false; |
| 459 | |
| 460 | return TRI->getRegClass(RegClass)->hasSubClassEq(TRI->getRegClass(OpClass)); |
| 461 | } |
| 462 | |
| 463 | /// \brief Make sure that we don't exeed the number of allowed scalars |
| 464 | void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand, |
| 465 | unsigned RegClass, |
| 466 | bool &ScalarSlotUsed) const { |
| 467 | |
| 468 | // First map the operands register class to a destination class |
| 469 | if (RegClass == AMDGPU::VSrc_32RegClassID) |
| 470 | RegClass = AMDGPU::VReg_32RegClassID; |
| 471 | else if (RegClass == AMDGPU::VSrc_64RegClassID) |
| 472 | RegClass = AMDGPU::VReg_64RegClassID; |
| 473 | else |
| 474 | return; |
| 475 | |
| 476 | // Nothing todo if they fit naturaly |
| 477 | if (fitsRegClass(DAG, Operand, RegClass)) |
| 478 | return; |
| 479 | |
| 480 | // If the scalar slot isn't used yet use it now |
| 481 | if (!ScalarSlotUsed) { |
| 482 | ScalarSlotUsed = true; |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | // This is a conservative aproach, it is possible that we can't determine |
| 487 | // the correct register class and copy too often, but better save than sorry. |
| 488 | SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32); |
| 489 | SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DebugLoc(), |
| 490 | Operand.getValueType(), Operand, RC); |
| 491 | Operand = SDValue(Node, 0); |
| 492 | } |
| 493 | |
Christian Konig | c018eca | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 494 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 495 | SelectionDAG &DAG) const { |
Christian Konig | d3b5509 | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 496 | |
| 497 | // Original encoding (either e32 or e64) |
| 498 | int Opcode = Node->getMachineOpcode(); |
| 499 | const MCInstrDesc *Desc = &TII->get(Opcode); |
| 500 | |
| 501 | unsigned NumDefs = Desc->getNumDefs(); |
| 502 | unsigned NumOps = Desc->getNumOperands(); |
| 503 | |
| 504 | int32_t Immediate = Desc->getSize() == 4 ? 0 : -1; |
| 505 | bool HaveVSrc = false, HaveSSrc = false; |
| 506 | |
| 507 | // First figure out what we alread have in this instruction |
| 508 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 509 | i != e && Op < NumOps; ++i, ++Op) { |
| 510 | |
| 511 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
| 512 | if (isVSrc(RegClass)) |
| 513 | HaveVSrc = true; |
| 514 | else if (isSSrc(RegClass)) |
| 515 | HaveSSrc = true; |
| 516 | else |
| 517 | continue; |
| 518 | |
| 519 | int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode()); |
| 520 | if (Imm != -1 && Imm != 0) { |
| 521 | // Literal immediate |
| 522 | Immediate = Imm; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // If we neither have VSrc nor SSrc it makes no sense to continue |
| 527 | if (!HaveVSrc && !HaveSSrc) |
| 528 | return Node; |
| 529 | |
| 530 | // No scalar allowed when we have both VSrc and SSrc |
| 531 | bool ScalarSlotUsed = HaveVSrc && HaveSSrc; |
| 532 | |
| 533 | // Second go over the operands and try to fold them |
| 534 | std::vector<SDValue> Ops; |
| 535 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 536 | i != e && Op < NumOps; ++i, ++Op) { |
| 537 | |
| 538 | const SDValue &Operand = Node->getOperand(i); |
| 539 | Ops.push_back(Operand); |
| 540 | |
| 541 | // Already folded immediate ? |
| 542 | if (isa<ConstantSDNode>(Operand.getNode()) || |
| 543 | isa<ConstantFPSDNode>(Operand.getNode())) |
| 544 | continue; |
| 545 | |
| 546 | // Is this a VSrc or SSrc operand ? |
| 547 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
Christian Konig | b37afdc | 2013-02-26 17:52:36 +0000 | [diff] [blame^] | 548 | if (!isVSrc(RegClass) && !isSSrc(RegClass)) { |
| 549 | |
| 550 | if (i == 1 && Desc->isCommutable() && |
| 551 | fitsRegClass(DAG, Ops[0], RegClass) && |
| 552 | foldImm(Ops[1], Immediate, ScalarSlotUsed)) { |
| 553 | |
| 554 | assert(isVSrc(Desc->OpInfo[NumDefs].RegClass) || |
| 555 | isSSrc(Desc->OpInfo[NumDefs].RegClass)); |
| 556 | |
| 557 | // Swap commutable operands |
| 558 | SDValue Tmp = Ops[1]; |
| 559 | Ops[1] = Ops[0]; |
| 560 | Ops[0] = Tmp; |
| 561 | } |
Christian Konig | d3b5509 | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 562 | continue; |
Christian Konig | b37afdc | 2013-02-26 17:52:36 +0000 | [diff] [blame^] | 563 | } |
Christian Konig | d3b5509 | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 564 | |
| 565 | // Try to fold the immediates |
| 566 | if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) { |
| 567 | // Folding didn't worked, make sure we don't hit the SReg limit |
| 568 | ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // Add optional chain and glue |
| 573 | for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i) |
| 574 | Ops.push_back(Node->getOperand(i)); |
| 575 | |
| 576 | // Update the instruction parameters |
| 577 | return DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size()); |
Christian Konig | c018eca | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 578 | } |