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