Tom Stellard | 75aadc2 | 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" |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 16 | #include "AMDGPU.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 17 | #include "AMDIL.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 18 | #include "AMDILIntrinsicInfo.h" |
| 19 | #include "SIInstrInfo.h" |
| 20 | #include "SIMachineFunctionInfo.h" |
| 21 | #include "SIRegisterInfo.h" |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/CallingConvLower.h" |
Tom Stellard | 75aadc2 | 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" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 27 | |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 28 | const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL; |
| 29 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | SITargetLowering::SITargetLowering(TargetMachine &TM) : |
| 33 | AMDGPUTargetLowering(TM), |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 34 | TII(static_cast<const SIInstrInfo*>(TM.getInstrInfo())), |
| 35 | TRI(TM.getRegisterInfo()) { |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 36 | |
Christian Konig | a881179 | 2013-02-16 11:28:30 +0000 | [diff] [blame] | 37 | addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 38 | addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); |
| 39 | |
| 40 | addRegisterClass(MVT::v16i8, &AMDGPU::SReg_128RegClass); |
| 41 | addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass); |
| 42 | addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass); |
| 43 | |
| 44 | addRegisterClass(MVT::i32, &AMDGPU::VReg_32RegClass); |
| 45 | addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 46 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 47 | addRegisterClass(MVT::v1i32, &AMDGPU::VReg_32RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 48 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 49 | addRegisterClass(MVT::v2i32, &AMDGPU::VReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 50 | addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); |
| 51 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 52 | addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 53 | addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 54 | addRegisterClass(MVT::i128, &AMDGPU::SReg_128RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 55 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 56 | addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 57 | addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); |
| 58 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 59 | addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 60 | addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 61 | |
| 62 | computeRegisterProperties(); |
| 63 | |
Christian Konig | 2989ffc | 2013-03-18 11:34:16 +0000 | [diff] [blame] | 64 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); |
| 65 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); |
| 66 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); |
| 67 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); |
| 68 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 69 | setOperationAction(ISD::ADD, MVT::i64, Legal); |
| 70 | setOperationAction(ISD::ADD, MVT::i32, Legal); |
| 71 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 72 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 73 | setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); |
| 74 | |
| 75 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 76 | |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame^] | 77 | setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom); |
| 78 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 79 | setTargetDAGCombine(ISD::SELECT_CC); |
| 80 | |
| 81 | setTargetDAGCombine(ISD::SETCC); |
Michel Danzer | f52a672 | 2013-03-08 10:58:01 +0000 | [diff] [blame] | 82 | |
Christian Konig | eecebd0 | 2013-03-26 14:04:02 +0000 | [diff] [blame] | 83 | setSchedulingPreference(Sched::RegPressure); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 86 | SDValue SITargetLowering::LowerFormalArguments( |
| 87 | SDValue Chain, |
| 88 | CallingConv::ID CallConv, |
| 89 | bool isVarArg, |
| 90 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 91 | SDLoc DL, SelectionDAG &DAG, |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 92 | SmallVectorImpl<SDValue> &InVals) const { |
| 93 | |
| 94 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 95 | |
| 96 | MachineFunction &MF = DAG.getMachineFunction(); |
| 97 | FunctionType *FType = MF.getFunction()->getFunctionType(); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 98 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 99 | |
| 100 | assert(CallConv == CallingConv::C); |
| 101 | |
| 102 | SmallVector<ISD::InputArg, 16> Splits; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 103 | uint32_t Skipped = 0; |
| 104 | |
| 105 | for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 106 | const ISD::InputArg &Arg = Ins[i]; |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 107 | |
| 108 | // First check if it's a PS input addr |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 109 | if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg()) { |
| 110 | |
| 111 | assert((PSInputNum <= 15) && "Too many PS inputs!"); |
| 112 | |
| 113 | if (!Arg.Used) { |
| 114 | // We can savely skip PS inputs |
| 115 | Skipped |= 1 << i; |
| 116 | ++PSInputNum; |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | Info->PSInputAddr |= 1 << PSInputNum++; |
| 121 | } |
| 122 | |
| 123 | // Second split vertices into their elements |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 124 | if (Arg.VT.isVector()) { |
| 125 | ISD::InputArg NewArg = Arg; |
| 126 | NewArg.Flags.setSplit(); |
| 127 | NewArg.VT = Arg.VT.getVectorElementType(); |
| 128 | |
| 129 | // We REALLY want the ORIGINAL number of vertex elements here, e.g. a |
| 130 | // three or five element vertex only needs three or five registers, |
| 131 | // NOT four or eigth. |
| 132 | Type *ParamType = FType->getParamType(Arg.OrigArgIndex); |
| 133 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 134 | |
| 135 | for (unsigned j = 0; j != NumElements; ++j) { |
| 136 | Splits.push_back(NewArg); |
| 137 | NewArg.PartOffset += NewArg.VT.getStoreSize(); |
| 138 | } |
| 139 | |
| 140 | } else { |
| 141 | Splits.push_back(Arg); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | SmallVector<CCValAssign, 16> ArgLocs; |
| 146 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| 147 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
| 148 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 149 | // At least one interpolation mode must be enabled or else the GPU will hang. |
| 150 | if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) { |
| 151 | Info->PSInputAddr |= 1; |
| 152 | CCInfo.AllocateReg(AMDGPU::VGPR0); |
| 153 | CCInfo.AllocateReg(AMDGPU::VGPR1); |
| 154 | } |
| 155 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 156 | AnalyzeFormalArguments(CCInfo, Splits); |
| 157 | |
| 158 | for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { |
| 159 | |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 160 | const ISD::InputArg &Arg = Ins[i]; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 161 | if (Skipped & (1 << i)) { |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 162 | InVals.push_back(DAG.getUNDEF(Arg.VT)); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 166 | CCValAssign &VA = ArgLocs[ArgIdx++]; |
| 167 | assert(VA.isRegLoc() && "Parameter must be in a register!"); |
| 168 | |
| 169 | unsigned Reg = VA.getLocReg(); |
| 170 | MVT VT = VA.getLocVT(); |
| 171 | |
| 172 | if (VT == MVT::i64) { |
| 173 | // For now assume it is a pointer |
| 174 | Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, |
| 175 | &AMDGPU::SReg_64RegClass); |
| 176 | Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); |
| 177 | InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); |
| 182 | |
| 183 | Reg = MF.addLiveIn(Reg, RC); |
| 184 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 185 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 186 | if (Arg.VT.isVector()) { |
| 187 | |
| 188 | // Build a vector from the registers |
| 189 | Type *ParamType = FType->getParamType(Arg.OrigArgIndex); |
| 190 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 191 | |
| 192 | SmallVector<SDValue, 4> Regs; |
| 193 | Regs.push_back(Val); |
| 194 | for (unsigned j = 1; j != NumElements; ++j) { |
| 195 | Reg = ArgLocs[ArgIdx++].getLocReg(); |
| 196 | Reg = MF.addLiveIn(Reg, RC); |
| 197 | Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 198 | } |
| 199 | |
| 200 | // Fill up the missing vector elements |
| 201 | NumElements = Arg.VT.getVectorNumElements() - NumElements; |
| 202 | for (unsigned j = 0; j != NumElements; ++j) |
| 203 | Regs.push_back(DAG.getUNDEF(VT)); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 204 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 205 | InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, |
| 206 | Regs.data(), Regs.size())); |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | InVals.push_back(Val); |
| 211 | } |
| 212 | return Chain; |
| 213 | } |
| 214 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 215 | MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( |
| 216 | MachineInstr * MI, MachineBasicBlock * BB) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 217 | |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 218 | MachineBasicBlock::iterator I = *MI; |
| 219 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 220 | switch (MI->getOpcode()) { |
| 221 | default: |
| 222 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
| 223 | case AMDGPU::BRANCH: return BB; |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 224 | case AMDGPU::SI_ADDR64_RSRC: { |
| 225 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
| 226 | unsigned SuperReg = MI->getOperand(0).getReg(); |
| 227 | unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 228 | unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 229 | unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); |
| 230 | unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); |
| 231 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo) |
| 232 | .addOperand(MI->getOperand(1)); |
| 233 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo) |
| 234 | .addImm(0); |
| 235 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi) |
| 236 | .addImm(RSRC_DATA_FORMAT >> 32); |
| 237 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi) |
| 238 | .addReg(SubRegHiLo) |
| 239 | .addImm(AMDGPU::sub0) |
| 240 | .addReg(SubRegHiHi) |
| 241 | .addImm(AMDGPU::sub1); |
| 242 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg) |
| 243 | .addReg(SubRegLo) |
| 244 | .addImm(AMDGPU::sub0_sub1) |
| 245 | .addReg(SubRegHi) |
| 246 | .addImm(AMDGPU::sub2_sub3); |
| 247 | MI->eraseFromParent(); |
| 248 | break; |
| 249 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 250 | } |
| 251 | return BB; |
| 252 | } |
| 253 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 254 | EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 255 | return MVT::i1; |
| 256 | } |
| 257 | |
Christian Konig | 082a14a | 2013-03-18 11:34:05 +0000 | [diff] [blame] | 258 | MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const { |
| 259 | return MVT::i32; |
| 260 | } |
| 261 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 262 | //===----------------------------------------------------------------------===// |
| 263 | // Custom DAG Lowering Operations |
| 264 | //===----------------------------------------------------------------------===// |
| 265 | |
| 266 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 267 | switch (Op.getOpcode()) { |
| 268 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 269 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 270 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame^] | 271 | case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 272 | } |
| 273 | return SDValue(); |
| 274 | } |
| 275 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 276 | /// \brief Helper function for LowerBRCOND |
| 277 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 278 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 279 | SDNode *Parent = Value.getNode(); |
| 280 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 281 | I != E; ++I) { |
| 282 | |
| 283 | if (I.getUse().get() != Value) |
| 284 | continue; |
| 285 | |
| 286 | if (I->getOpcode() == Opcode) |
| 287 | return *I; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /// This transforms the control flow intrinsics to get the branch destination as |
| 293 | /// last parameter, also switches branch target with BR if the need arise |
| 294 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 295 | SelectionDAG &DAG) const { |
| 296 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 297 | SDLoc DL(BRCOND); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 298 | |
| 299 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 300 | SDValue Target = BRCOND.getOperand(2); |
| 301 | SDNode *BR = 0; |
| 302 | |
| 303 | if (Intr->getOpcode() == ISD::SETCC) { |
| 304 | // As long as we negate the condition everything is fine |
| 305 | SDNode *SetCC = Intr; |
| 306 | assert(SetCC->getConstantOperandVal(1) == 1); |
NAKAMURA Takumi | 458a827 | 2013-01-07 11:14:44 +0000 | [diff] [blame] | 307 | assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 308 | ISD::SETNE); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 309 | Intr = SetCC->getOperand(0).getNode(); |
| 310 | |
| 311 | } else { |
| 312 | // Get the target from BR if we don't negate the condition |
| 313 | BR = findUser(BRCOND, ISD::BR); |
| 314 | Target = BR->getOperand(1); |
| 315 | } |
| 316 | |
| 317 | assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); |
| 318 | |
| 319 | // Build the result and |
| 320 | SmallVector<EVT, 4> Res; |
| 321 | for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i) |
| 322 | Res.push_back(Intr->getValueType(i)); |
| 323 | |
| 324 | // operands of the new intrinsic call |
| 325 | SmallVector<SDValue, 4> Ops; |
| 326 | Ops.push_back(BRCOND.getOperand(0)); |
| 327 | for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i) |
| 328 | Ops.push_back(Intr->getOperand(i)); |
| 329 | Ops.push_back(Target); |
| 330 | |
| 331 | // build the new intrinsic call |
| 332 | SDNode *Result = DAG.getNode( |
| 333 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
| 334 | DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode(); |
| 335 | |
| 336 | if (BR) { |
| 337 | // Give the branch instruction our target |
| 338 | SDValue Ops[] = { |
| 339 | BR->getOperand(0), |
| 340 | BRCOND.getOperand(2) |
| 341 | }; |
| 342 | DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2); |
| 343 | } |
| 344 | |
| 345 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 346 | |
| 347 | // Copy the intrinsic results to registers |
| 348 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 349 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 350 | if (!CopyToReg) |
| 351 | continue; |
| 352 | |
| 353 | Chain = DAG.getCopyToReg( |
| 354 | Chain, DL, |
| 355 | CopyToReg->getOperand(1), |
| 356 | SDValue(Result, i - 1), |
| 357 | SDValue()); |
| 358 | |
| 359 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 360 | } |
| 361 | |
| 362 | // Remove the old intrinsic from the chain |
| 363 | DAG.ReplaceAllUsesOfValueWith( |
| 364 | SDValue(Intr, Intr->getNumValues() - 1), |
| 365 | Intr->getOperand(0)); |
| 366 | |
| 367 | return Chain; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 370 | SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { |
| 371 | SDValue LHS = Op.getOperand(0); |
| 372 | SDValue RHS = Op.getOperand(1); |
| 373 | SDValue True = Op.getOperand(2); |
| 374 | SDValue False = Op.getOperand(3); |
| 375 | SDValue CC = Op.getOperand(4); |
| 376 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 377 | SDLoc DL(Op); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 378 | |
| 379 | // Possible Min/Max pattern |
| 380 | SDValue MinMax = LowerMinMax(Op, DAG); |
| 381 | if (MinMax.getNode()) { |
| 382 | return MinMax; |
| 383 | } |
| 384 | |
| 385 | SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC); |
| 386 | return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False); |
| 387 | } |
| 388 | |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame^] | 389 | SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op, |
| 390 | SelectionDAG &DAG) const { |
| 391 | EVT VT = Op.getValueType(); |
| 392 | SDLoc DL(Op); |
| 393 | |
| 394 | if (VT != MVT::i64) { |
| 395 | return SDValue(); |
| 396 | } |
| 397 | |
| 398 | SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0), |
| 399 | DAG.getConstant(31, MVT::i32)); |
| 400 | |
| 401 | return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi); |
| 402 | } |
| 403 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 404 | //===----------------------------------------------------------------------===// |
| 405 | // Custom DAG optimizations |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | |
| 408 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 409 | DAGCombinerInfo &DCI) const { |
| 410 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 411 | SDLoc DL(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 412 | EVT VT = N->getValueType(0); |
| 413 | |
| 414 | switch (N->getOpcode()) { |
| 415 | default: break; |
| 416 | case ISD::SELECT_CC: { |
| 417 | N->dump(); |
| 418 | ConstantSDNode *True, *False; |
| 419 | // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc) |
| 420 | if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 421 | && (False = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 422 | && True->isAllOnesValue() |
| 423 | && False->isNullValue() |
| 424 | && VT == MVT::i1) { |
| 425 | return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0), |
| 426 | N->getOperand(1), N->getOperand(4)); |
| 427 | |
| 428 | } |
| 429 | break; |
| 430 | } |
| 431 | case ISD::SETCC: { |
| 432 | SDValue Arg0 = N->getOperand(0); |
| 433 | SDValue Arg1 = N->getOperand(1); |
| 434 | SDValue CC = N->getOperand(2); |
| 435 | ConstantSDNode * C = NULL; |
| 436 | ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get(); |
| 437 | |
| 438 | // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne) |
| 439 | if (VT == MVT::i1 |
| 440 | && Arg0.getOpcode() == ISD::SIGN_EXTEND |
| 441 | && Arg0.getOperand(0).getValueType() == MVT::i1 |
| 442 | && (C = dyn_cast<ConstantSDNode>(Arg1)) |
| 443 | && C->isNullValue() |
| 444 | && CCOp == ISD::SETNE) { |
| 445 | return SimplifySetCC(VT, Arg0.getOperand(0), |
| 446 | DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL); |
| 447 | } |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | return SDValue(); |
| 452 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 453 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 454 | /// \brief Test if RegClass is one of the VSrc classes |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 455 | static bool isVSrc(unsigned RegClass) { |
| 456 | return AMDGPU::VSrc_32RegClassID == RegClass || |
| 457 | AMDGPU::VSrc_64RegClassID == RegClass; |
| 458 | } |
| 459 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 460 | /// \brief Test if RegClass is one of the SSrc classes |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 461 | static bool isSSrc(unsigned RegClass) { |
| 462 | return AMDGPU::SSrc_32RegClassID == RegClass || |
| 463 | AMDGPU::SSrc_64RegClassID == RegClass; |
| 464 | } |
| 465 | |
| 466 | /// \brief Analyze the possible immediate value Op |
| 467 | /// |
| 468 | /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate |
| 469 | /// and the immediate value if it's a literal immediate |
| 470 | int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { |
| 471 | |
| 472 | union { |
| 473 | int32_t I; |
| 474 | float F; |
| 475 | } Imm; |
| 476 | |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 477 | if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) { |
| 478 | if (Node->getZExtValue() >> 32) { |
| 479 | return -1; |
| 480 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 481 | Imm.I = Node->getSExtValue(); |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 482 | } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 483 | Imm.F = Node->getValueAPF().convertToFloat(); |
| 484 | else |
| 485 | return -1; // It isn't an immediate |
| 486 | |
| 487 | if ((Imm.I >= -16 && Imm.I <= 64) || |
| 488 | Imm.F == 0.5f || Imm.F == -0.5f || |
| 489 | Imm.F == 1.0f || Imm.F == -1.0f || |
| 490 | Imm.F == 2.0f || Imm.F == -2.0f || |
| 491 | Imm.F == 4.0f || Imm.F == -4.0f) |
| 492 | return 0; // It's an inline immediate |
| 493 | |
| 494 | return Imm.I; // It's a literal immediate |
| 495 | } |
| 496 | |
| 497 | /// \brief Try to fold an immediate directly into an instruction |
| 498 | bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate, |
| 499 | bool &ScalarSlotUsed) const { |
| 500 | |
| 501 | MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand); |
| 502 | if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode())) |
| 503 | return false; |
| 504 | |
| 505 | const SDValue &Op = Mov->getOperand(0); |
| 506 | int32_t Value = analyzeImmediate(Op.getNode()); |
| 507 | if (Value == -1) { |
| 508 | // Not an immediate at all |
| 509 | return false; |
| 510 | |
| 511 | } else if (Value == 0) { |
| 512 | // Inline immediates can always be fold |
| 513 | Operand = Op; |
| 514 | return true; |
| 515 | |
| 516 | } else if (Value == Immediate) { |
| 517 | // Already fold literal immediate |
| 518 | Operand = Op; |
| 519 | return true; |
| 520 | |
| 521 | } else if (!ScalarSlotUsed && !Immediate) { |
| 522 | // Fold this literal immediate |
| 523 | ScalarSlotUsed = true; |
| 524 | Immediate = Value; |
| 525 | Operand = Op; |
| 526 | return true; |
| 527 | |
| 528 | } |
| 529 | |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | /// \brief Does "Op" fit into register class "RegClass" ? |
Tom Stellard | b35efba | 2013-05-20 15:02:01 +0000 | [diff] [blame] | 534 | bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op, |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 535 | unsigned RegClass) const { |
| 536 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 537 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 538 | SDNode *Node = Op.getNode(); |
| 539 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 540 | const TargetRegisterClass *OpClass; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 541 | if (MachineSDNode *MN = dyn_cast<MachineSDNode>(Node)) { |
| 542 | const MCInstrDesc &Desc = TII->get(MN->getMachineOpcode()); |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 543 | int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass; |
Tom Stellard | bad1f59 | 2013-06-03 17:39:54 +0000 | [diff] [blame] | 544 | if (OpClassID == -1) { |
| 545 | switch (MN->getMachineOpcode()) { |
| 546 | case AMDGPU::REG_SEQUENCE: |
| 547 | // Operand 0 is the register class id for REG_SEQUENCE instructions. |
| 548 | OpClass = TRI->getRegClass( |
| 549 | cast<ConstantSDNode>(MN->getOperand(0))->getZExtValue()); |
| 550 | break; |
| 551 | default: |
| 552 | OpClass = getRegClassFor(Op.getSimpleValueType()); |
| 553 | break; |
| 554 | } |
| 555 | } else { |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 556 | OpClass = TRI->getRegClass(OpClassID); |
Tom Stellard | bad1f59 | 2013-06-03 17:39:54 +0000 | [diff] [blame] | 557 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 558 | |
| 559 | } else if (Node->getOpcode() == ISD::CopyFromReg) { |
| 560 | RegisterSDNode *Reg = cast<RegisterSDNode>(Node->getOperand(1).getNode()); |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 561 | OpClass = MRI.getRegClass(Reg->getReg()); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 562 | |
| 563 | } else |
| 564 | return false; |
| 565 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 566 | return TRI->getRegClass(RegClass)->hasSubClassEq(OpClass); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | /// \brief Make sure that we don't exeed the number of allowed scalars |
| 570 | void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand, |
| 571 | unsigned RegClass, |
| 572 | bool &ScalarSlotUsed) const { |
| 573 | |
| 574 | // First map the operands register class to a destination class |
| 575 | if (RegClass == AMDGPU::VSrc_32RegClassID) |
| 576 | RegClass = AMDGPU::VReg_32RegClassID; |
| 577 | else if (RegClass == AMDGPU::VSrc_64RegClassID) |
| 578 | RegClass = AMDGPU::VReg_64RegClassID; |
| 579 | else |
| 580 | return; |
| 581 | |
| 582 | // Nothing todo if they fit naturaly |
| 583 | if (fitsRegClass(DAG, Operand, RegClass)) |
| 584 | return; |
| 585 | |
| 586 | // If the scalar slot isn't used yet use it now |
| 587 | if (!ScalarSlotUsed) { |
| 588 | ScalarSlotUsed = true; |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | // This is a conservative aproach, it is possible that we can't determine |
| 593 | // the correct register class and copy too often, but better save than sorry. |
| 594 | SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 595 | SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(), |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 596 | Operand.getValueType(), Operand, RC); |
| 597 | Operand = SDValue(Node, 0); |
| 598 | } |
| 599 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 600 | /// \brief Try to fold the Nodes operands into the Node |
| 601 | SDNode *SITargetLowering::foldOperands(MachineSDNode *Node, |
| 602 | SelectionDAG &DAG) const { |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 603 | |
| 604 | // Original encoding (either e32 or e64) |
| 605 | int Opcode = Node->getMachineOpcode(); |
| 606 | const MCInstrDesc *Desc = &TII->get(Opcode); |
| 607 | |
| 608 | unsigned NumDefs = Desc->getNumDefs(); |
| 609 | unsigned NumOps = Desc->getNumOperands(); |
| 610 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 611 | // Commuted opcode if available |
| 612 | int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1; |
| 613 | const MCInstrDesc *DescRev = OpcodeRev == -1 ? 0 : &TII->get(OpcodeRev); |
| 614 | |
| 615 | assert(!DescRev || DescRev->getNumDefs() == NumDefs); |
| 616 | assert(!DescRev || DescRev->getNumOperands() == NumOps); |
| 617 | |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 618 | // e64 version if available, -1 otherwise |
| 619 | int OpcodeE64 = AMDGPU::getVOPe64(Opcode); |
| 620 | const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64); |
| 621 | |
| 622 | assert(!DescE64 || DescE64->getNumDefs() == NumDefs); |
| 623 | assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4)); |
| 624 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 625 | int32_t Immediate = Desc->getSize() == 4 ? 0 : -1; |
| 626 | bool HaveVSrc = false, HaveSSrc = false; |
| 627 | |
| 628 | // First figure out what we alread have in this instruction |
| 629 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 630 | i != e && Op < NumOps; ++i, ++Op) { |
| 631 | |
| 632 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
| 633 | if (isVSrc(RegClass)) |
| 634 | HaveVSrc = true; |
| 635 | else if (isSSrc(RegClass)) |
| 636 | HaveSSrc = true; |
| 637 | else |
| 638 | continue; |
| 639 | |
| 640 | int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode()); |
| 641 | if (Imm != -1 && Imm != 0) { |
| 642 | // Literal immediate |
| 643 | Immediate = Imm; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // If we neither have VSrc nor SSrc it makes no sense to continue |
| 648 | if (!HaveVSrc && !HaveSSrc) |
| 649 | return Node; |
| 650 | |
| 651 | // No scalar allowed when we have both VSrc and SSrc |
| 652 | bool ScalarSlotUsed = HaveVSrc && HaveSSrc; |
| 653 | |
| 654 | // Second go over the operands and try to fold them |
| 655 | std::vector<SDValue> Ops; |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 656 | bool Promote2e64 = false; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 657 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 658 | i != e && Op < NumOps; ++i, ++Op) { |
| 659 | |
| 660 | const SDValue &Operand = Node->getOperand(i); |
| 661 | Ops.push_back(Operand); |
| 662 | |
| 663 | // Already folded immediate ? |
| 664 | if (isa<ConstantSDNode>(Operand.getNode()) || |
| 665 | isa<ConstantFPSDNode>(Operand.getNode())) |
| 666 | continue; |
| 667 | |
| 668 | // Is this a VSrc or SSrc operand ? |
| 669 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 670 | if (isVSrc(RegClass) || isSSrc(RegClass)) { |
| 671 | // Try to fold the immediates |
| 672 | if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) { |
| 673 | // Folding didn't worked, make sure we don't hit the SReg limit |
| 674 | ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed); |
| 675 | } |
| 676 | continue; |
| 677 | } |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 678 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 679 | if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) { |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 680 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 681 | unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass; |
| 682 | assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass)); |
| 683 | |
| 684 | // Test if it makes sense to swap operands |
| 685 | if (foldImm(Ops[1], Immediate, ScalarSlotUsed) || |
| 686 | (!fitsRegClass(DAG, Ops[1], RegClass) && |
| 687 | fitsRegClass(DAG, Ops[1], OtherRegClass))) { |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 688 | |
| 689 | // Swap commutable operands |
| 690 | SDValue Tmp = Ops[1]; |
| 691 | Ops[1] = Ops[0]; |
| 692 | Ops[0] = Tmp; |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 693 | |
| 694 | Desc = DescRev; |
| 695 | DescRev = 0; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 696 | continue; |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 697 | } |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 698 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 699 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 700 | if (DescE64 && !Immediate) { |
| 701 | |
| 702 | // Test if it makes sense to switch to e64 encoding |
| 703 | unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass; |
| 704 | if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass)) |
| 705 | continue; |
| 706 | |
| 707 | int32_t TmpImm = -1; |
| 708 | if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) || |
| 709 | (!fitsRegClass(DAG, Ops[i], RegClass) && |
| 710 | fitsRegClass(DAG, Ops[1], OtherRegClass))) { |
| 711 | |
| 712 | // Switch to e64 encoding |
| 713 | Immediate = -1; |
| 714 | Promote2e64 = true; |
| 715 | Desc = DescE64; |
| 716 | DescE64 = 0; |
| 717 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 721 | if (Promote2e64) { |
| 722 | // Add the modifier flags while promoting |
| 723 | for (unsigned i = 0; i < 4; ++i) |
| 724 | Ops.push_back(DAG.getTargetConstant(0, MVT::i32)); |
| 725 | } |
| 726 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 727 | // Add optional chain and glue |
| 728 | for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i) |
| 729 | Ops.push_back(Node->getOperand(i)); |
| 730 | |
Tom Stellard | b5a9700 | 2013-06-03 17:39:50 +0000 | [diff] [blame] | 731 | // Nodes that have a glue result are not CSE'd by getMachineNode(), so in |
| 732 | // this case a brand new node is always be created, even if the operands |
| 733 | // are the same as before. So, manually check if anything has been changed. |
| 734 | if (Desc->Opcode == Opcode) { |
| 735 | bool Changed = false; |
| 736 | for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) { |
| 737 | if (Ops[i].getNode() != Node->getOperand(i).getNode()) { |
| 738 | Changed = true; |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | if (!Changed) { |
| 743 | return Node; |
| 744 | } |
| 745 | } |
| 746 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 747 | // Create a complete new instruction |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 748 | return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops); |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 749 | } |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 750 | |
| 751 | /// \brief Helper function for adjustWritemask |
Benjamin Kramer | 635e368 | 2013-05-23 15:43:05 +0000 | [diff] [blame] | 752 | static unsigned SubIdx2Lane(unsigned Idx) { |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 753 | switch (Idx) { |
| 754 | default: return 0; |
| 755 | case AMDGPU::sub0: return 0; |
| 756 | case AMDGPU::sub1: return 1; |
| 757 | case AMDGPU::sub2: return 2; |
| 758 | case AMDGPU::sub3: return 3; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /// \brief Adjust the writemask of MIMG instructions |
| 763 | void SITargetLowering::adjustWritemask(MachineSDNode *&Node, |
| 764 | SelectionDAG &DAG) const { |
| 765 | SDNode *Users[4] = { }; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 766 | unsigned Writemask = 0, Lane = 0; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 767 | |
| 768 | // Try to figure out the used register components |
| 769 | for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); |
| 770 | I != E; ++I) { |
| 771 | |
| 772 | // Abort if we can't understand the usage |
| 773 | if (!I->isMachineOpcode() || |
| 774 | I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) |
| 775 | return; |
| 776 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 777 | Lane = SubIdx2Lane(I->getConstantOperandVal(1)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 778 | |
| 779 | // Abort if we have more than one user per component |
| 780 | if (Users[Lane]) |
| 781 | return; |
| 782 | |
| 783 | Users[Lane] = *I; |
| 784 | Writemask |= 1 << Lane; |
| 785 | } |
| 786 | |
| 787 | // Abort if all components are used |
| 788 | if (Writemask == 0xf) |
| 789 | return; |
| 790 | |
| 791 | // Adjust the writemask in the node |
| 792 | std::vector<SDValue> Ops; |
| 793 | Ops.push_back(DAG.getTargetConstant(Writemask, MVT::i32)); |
| 794 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 795 | Ops.push_back(Node->getOperand(i)); |
| 796 | Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size()); |
| 797 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 798 | // If we only got one lane, replace it with a copy |
| 799 | if (Writemask == (1U << Lane)) { |
| 800 | SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32); |
| 801 | SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 802 | SDLoc(), Users[Lane]->getValueType(0), |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 803 | SDValue(Node, 0), RC); |
| 804 | DAG.ReplaceAllUsesWith(Users[Lane], Copy); |
| 805 | return; |
| 806 | } |
| 807 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 808 | // Update the users of the node with the new indices |
| 809 | for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { |
| 810 | |
| 811 | SDNode *User = Users[i]; |
| 812 | if (!User) |
| 813 | continue; |
| 814 | |
| 815 | SDValue Op = DAG.getTargetConstant(Idx, MVT::i32); |
| 816 | DAG.UpdateNodeOperands(User, User->getOperand(0), Op); |
| 817 | |
| 818 | switch (Idx) { |
| 819 | default: break; |
| 820 | case AMDGPU::sub0: Idx = AMDGPU::sub1; break; |
| 821 | case AMDGPU::sub1: Idx = AMDGPU::sub2; break; |
| 822 | case AMDGPU::sub2: Idx = AMDGPU::sub3; break; |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /// \brief Fold the instructions after slecting them |
| 828 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 829 | SelectionDAG &DAG) const { |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 830 | Node = AdjustRegClass(Node, DAG); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 831 | |
| 832 | if (AMDGPU::isMIMG(Node->getMachineOpcode()) != -1) |
| 833 | adjustWritemask(Node, DAG); |
| 834 | |
| 835 | return foldOperands(Node, DAG); |
| 836 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 837 | |
| 838 | /// \brief Assign the register class depending on the number of |
| 839 | /// bits set in the writemask |
| 840 | void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, |
| 841 | SDNode *Node) const { |
| 842 | if (AMDGPU::isMIMG(MI->getOpcode()) == -1) |
| 843 | return; |
| 844 | |
| 845 | unsigned VReg = MI->getOperand(0).getReg(); |
| 846 | unsigned Writemask = MI->getOperand(1).getImm(); |
| 847 | unsigned BitsSet = 0; |
| 848 | for (unsigned i = 0; i < 4; ++i) |
| 849 | BitsSet += Writemask & (1 << i) ? 1 : 0; |
| 850 | |
| 851 | const TargetRegisterClass *RC; |
| 852 | switch (BitsSet) { |
| 853 | default: return; |
| 854 | case 1: RC = &AMDGPU::VReg_32RegClass; break; |
| 855 | case 2: RC = &AMDGPU::VReg_64RegClass; break; |
| 856 | case 3: RC = &AMDGPU::VReg_96RegClass; break; |
| 857 | } |
| 858 | |
| 859 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 860 | MRI.setRegClass(VReg, RC); |
| 861 | } |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 862 | |
| 863 | MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N, |
| 864 | SelectionDAG &DAG) const { |
| 865 | |
| 866 | SDLoc DL(N); |
| 867 | unsigned NewOpcode = N->getMachineOpcode(); |
| 868 | |
| 869 | switch (N->getMachineOpcode()) { |
| 870 | default: return N; |
| 871 | case AMDGPU::REG_SEQUENCE: { |
| 872 | // MVT::i128 only use SGPRs, so i128 REG_SEQUENCEs don't need to be |
| 873 | // rewritten. |
| 874 | if (N->getValueType(0) == MVT::i128) { |
| 875 | return N; |
| 876 | } |
| 877 | const SDValue Ops[] = { |
| 878 | DAG.getTargetConstant(AMDGPU::VReg_64RegClassID, MVT::i32), |
| 879 | N->getOperand(1) , N->getOperand(2), |
| 880 | N->getOperand(3), N->getOperand(4) |
| 881 | }; |
| 882 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::i64, Ops); |
| 883 | } |
| 884 | |
| 885 | case AMDGPU::S_LOAD_DWORD_IMM: |
| 886 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64; |
| 887 | // Fall-through |
| 888 | case AMDGPU::S_LOAD_DWORDX2_SGPR: |
| 889 | if (NewOpcode == N->getMachineOpcode()) { |
| 890 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64; |
| 891 | } |
| 892 | // Fall-through |
| 893 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
| 894 | case AMDGPU::S_LOAD_DWORDX4_SGPR: { |
| 895 | if (NewOpcode == N->getMachineOpcode()) { |
| 896 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64; |
| 897 | } |
| 898 | if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) { |
| 899 | return N; |
| 900 | } |
| 901 | ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1)); |
| 902 | SDValue Ops[] = { |
| 903 | SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128, |
| 904 | DAG.getConstant(0, MVT::i64)), 0), |
| 905 | N->getOperand(0), |
| 906 | DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32) |
| 907 | }; |
| 908 | return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops); |
| 909 | } |
| 910 | } |
| 911 | } |