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" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 17 | #include "AMDILIntrinsicInfo.h" |
| 18 | #include "SIInstrInfo.h" |
| 19 | #include "SIMachineFunctionInfo.h" |
| 20 | #include "SIRegisterInfo.h" |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/CallingConvLower.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 24 | #include "llvm/CodeGen/SelectionDAG.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Function.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 26 | |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 27 | const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL; |
| 28 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | SITargetLowering::SITargetLowering(TargetMachine &TM) : |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 32 | AMDGPUTargetLowering(TM) { |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 33 | |
Christian Konig | a881179 | 2013-02-16 11:28:30 +0000 | [diff] [blame] | 34 | addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass); |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 35 | addRegisterClass(MVT::i64, &AMDGPU::VSrc_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 36 | |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 37 | addRegisterClass(MVT::v2i1, &AMDGPU::VReg_64RegClass); |
| 38 | addRegisterClass(MVT::v4i1, &AMDGPU::VReg_128RegClass); |
| 39 | |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 40 | addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass); |
| 41 | addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass); |
| 42 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 43 | addRegisterClass(MVT::i32, &AMDGPU::VSrc_32RegClass); |
| 44 | addRegisterClass(MVT::f32, &AMDGPU::VSrc_32RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 45 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 46 | addRegisterClass(MVT::v1i32, &AMDGPU::VSrc_32RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 47 | |
Tom Stellard | 2f7cdda | 2013-08-06 23:08:28 +0000 | [diff] [blame] | 48 | addRegisterClass(MVT::f64, &AMDGPU::VSrc_64RegClass); |
| 49 | addRegisterClass(MVT::v2i32, &AMDGPU::VSrc_64RegClass); |
| 50 | addRegisterClass(MVT::v2f32, &AMDGPU::VSrc_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 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); |
Aaron Watry | daabb20 | 2013-06-25 13:55:52 +0000 | [diff] [blame] | 71 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame^] | 72 | setOperationAction(ISD::BITCAST, MVT::i128, Legal); |
| 73 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 74 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 75 | setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); |
| 76 | |
| 77 | setOperationAction(ISD::SELECT_CC, MVT::Other, Expand); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 78 | |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 79 | setOperationAction(ISD::SETCC, MVT::v2i1, Expand); |
| 80 | setOperationAction(ISD::SETCC, MVT::v4i1, Expand); |
| 81 | |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame] | 82 | setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom); |
Tom Stellard | 98f675a | 2013-08-01 15:23:26 +0000 | [diff] [blame] | 83 | setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom); |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame] | 84 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 85 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame^] | 86 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); |
| 87 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom); |
| 88 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 89 | |
Tom Stellard | 31209cc | 2013-07-15 19:00:09 +0000 | [diff] [blame] | 90 | setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand); |
| 91 | |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 92 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 93 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 94 | |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 95 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
| 96 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 97 | setTargetDAGCombine(ISD::SELECT_CC); |
| 98 | |
| 99 | setTargetDAGCombine(ISD::SETCC); |
Michel Danzer | f52a672 | 2013-03-08 10:58:01 +0000 | [diff] [blame] | 100 | |
Christian Konig | eecebd0 | 2013-03-26 14:04:02 +0000 | [diff] [blame] | 101 | setSchedulingPreference(Sched::RegPressure); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 104 | //===----------------------------------------------------------------------===// |
| 105 | // TargetLowering queries |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | |
| 108 | bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT, |
| 109 | bool *IsFast) const { |
| 110 | // XXX: This depends on the address space and also we may want to revist |
| 111 | // the alignment values we specify in the DataLayout. |
| 112 | return VT.bitsGT(MVT::i32); |
| 113 | } |
| 114 | |
| 115 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 116 | SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, |
| 117 | SDLoc DL, SDValue Chain, |
| 118 | unsigned Offset) const { |
| 119 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 120 | PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()), |
| 121 | AMDGPUAS::CONSTANT_ADDRESS); |
| 122 | EVT ArgVT = MVT::getIntegerVT(VT.getSizeInBits()); |
| 123 | SDValue BasePtr = DAG.getCopyFromReg(Chain, DL, |
| 124 | MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64); |
| 125 | SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr, |
| 126 | DAG.getConstant(Offset, MVT::i64)); |
Tom Stellard | 9f95033 | 2013-07-23 01:48:35 +0000 | [diff] [blame] | 127 | return DAG.getLoad(VT, DL, Chain, Ptr, |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 128 | MachinePointerInfo(UndefValue::get(PtrTy)), |
Tom Stellard | 9f95033 | 2013-07-23 01:48:35 +0000 | [diff] [blame] | 129 | false, false, false, ArgVT.getSizeInBits() >> 3); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 130 | |
| 131 | } |
| 132 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 133 | SDValue SITargetLowering::LowerFormalArguments( |
| 134 | SDValue Chain, |
| 135 | CallingConv::ID CallConv, |
| 136 | bool isVarArg, |
| 137 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 138 | SDLoc DL, SelectionDAG &DAG, |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 139 | SmallVectorImpl<SDValue> &InVals) const { |
| 140 | |
| 141 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 142 | |
| 143 | MachineFunction &MF = DAG.getMachineFunction(); |
| 144 | FunctionType *FType = MF.getFunction()->getFunctionType(); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 145 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 146 | |
| 147 | assert(CallConv == CallingConv::C); |
| 148 | |
| 149 | SmallVector<ISD::InputArg, 16> Splits; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 150 | uint32_t Skipped = 0; |
| 151 | |
| 152 | for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 153 | const ISD::InputArg &Arg = Ins[i]; |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 154 | |
| 155 | // First check if it's a PS input addr |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 156 | if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg()) { |
| 157 | |
| 158 | assert((PSInputNum <= 15) && "Too many PS inputs!"); |
| 159 | |
| 160 | if (!Arg.Used) { |
| 161 | // We can savely skip PS inputs |
| 162 | Skipped |= 1 << i; |
| 163 | ++PSInputNum; |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | Info->PSInputAddr |= 1 << PSInputNum++; |
| 168 | } |
| 169 | |
| 170 | // Second split vertices into their elements |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 171 | if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 172 | ISD::InputArg NewArg = Arg; |
| 173 | NewArg.Flags.setSplit(); |
| 174 | NewArg.VT = Arg.VT.getVectorElementType(); |
| 175 | |
| 176 | // We REALLY want the ORIGINAL number of vertex elements here, e.g. a |
| 177 | // three or five element vertex only needs three or five registers, |
| 178 | // NOT four or eigth. |
| 179 | Type *ParamType = FType->getParamType(Arg.OrigArgIndex); |
| 180 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 181 | |
| 182 | for (unsigned j = 0; j != NumElements; ++j) { |
| 183 | Splits.push_back(NewArg); |
| 184 | NewArg.PartOffset += NewArg.VT.getStoreSize(); |
| 185 | } |
| 186 | |
| 187 | } else { |
| 188 | Splits.push_back(Arg); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | SmallVector<CCValAssign, 16> ArgLocs; |
| 193 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| 194 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
| 195 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 196 | // At least one interpolation mode must be enabled or else the GPU will hang. |
| 197 | if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) { |
| 198 | Info->PSInputAddr |= 1; |
| 199 | CCInfo.AllocateReg(AMDGPU::VGPR0); |
| 200 | CCInfo.AllocateReg(AMDGPU::VGPR1); |
| 201 | } |
| 202 | |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 203 | // The pointer to the list of arguments is stored in SGPR0, SGPR1 |
| 204 | if (Info->ShaderType == ShaderType::COMPUTE) { |
| 205 | CCInfo.AllocateReg(AMDGPU::SGPR0); |
| 206 | CCInfo.AllocateReg(AMDGPU::SGPR1); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 207 | MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 210 | AnalyzeFormalArguments(CCInfo, Splits); |
| 211 | |
| 212 | for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { |
| 213 | |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 214 | const ISD::InputArg &Arg = Ins[i]; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 215 | if (Skipped & (1 << i)) { |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 216 | InVals.push_back(DAG.getUNDEF(Arg.VT)); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 217 | continue; |
| 218 | } |
| 219 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 220 | CCValAssign &VA = ArgLocs[ArgIdx++]; |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 221 | EVT VT = VA.getLocVT(); |
| 222 | |
| 223 | if (VA.isMemLoc()) { |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 224 | // The first 36 bytes of the input buffer contains information about |
| 225 | // thread group and global sizes. |
| 226 | SDValue Arg = LowerParameter(DAG, VT, DL, DAG.getRoot(), |
| 227 | 36 + VA.getLocMemOffset()); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 228 | InVals.push_back(Arg); |
| 229 | continue; |
| 230 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 231 | assert(VA.isRegLoc() && "Parameter must be in a register!"); |
| 232 | |
| 233 | unsigned Reg = VA.getLocReg(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 234 | |
| 235 | if (VT == MVT::i64) { |
| 236 | // For now assume it is a pointer |
| 237 | Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, |
| 238 | &AMDGPU::SReg_64RegClass); |
| 239 | Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); |
| 240 | InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); |
| 245 | |
| 246 | Reg = MF.addLiveIn(Reg, RC); |
| 247 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 248 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 249 | if (Arg.VT.isVector()) { |
| 250 | |
| 251 | // Build a vector from the registers |
| 252 | Type *ParamType = FType->getParamType(Arg.OrigArgIndex); |
| 253 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 254 | |
| 255 | SmallVector<SDValue, 4> Regs; |
| 256 | Regs.push_back(Val); |
| 257 | for (unsigned j = 1; j != NumElements; ++j) { |
| 258 | Reg = ArgLocs[ArgIdx++].getLocReg(); |
| 259 | Reg = MF.addLiveIn(Reg, RC); |
| 260 | Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 261 | } |
| 262 | |
| 263 | // Fill up the missing vector elements |
| 264 | NumElements = Arg.VT.getVectorNumElements() - NumElements; |
| 265 | for (unsigned j = 0; j != NumElements; ++j) |
| 266 | Regs.push_back(DAG.getUNDEF(VT)); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 267 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 268 | InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, |
| 269 | Regs.data(), Regs.size())); |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | InVals.push_back(Val); |
| 274 | } |
| 275 | return Chain; |
| 276 | } |
| 277 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 278 | MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( |
| 279 | MachineInstr * MI, MachineBasicBlock * BB) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 280 | |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 281 | MachineBasicBlock::iterator I = *MI; |
| 282 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 283 | switch (MI->getOpcode()) { |
| 284 | default: |
| 285 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
| 286 | case AMDGPU::BRANCH: return BB; |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 287 | case AMDGPU::SI_ADDR64_RSRC: { |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 288 | const SIInstrInfo *TII = |
| 289 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 290 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
| 291 | unsigned SuperReg = MI->getOperand(0).getReg(); |
| 292 | unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 293 | unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 294 | unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); |
| 295 | unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); |
| 296 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo) |
| 297 | .addOperand(MI->getOperand(1)); |
| 298 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo) |
| 299 | .addImm(0); |
| 300 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi) |
| 301 | .addImm(RSRC_DATA_FORMAT >> 32); |
| 302 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi) |
| 303 | .addReg(SubRegHiLo) |
| 304 | .addImm(AMDGPU::sub0) |
| 305 | .addReg(SubRegHiHi) |
| 306 | .addImm(AMDGPU::sub1); |
| 307 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg) |
| 308 | .addReg(SubRegLo) |
| 309 | .addImm(AMDGPU::sub0_sub1) |
| 310 | .addReg(SubRegHi) |
| 311 | .addImm(AMDGPU::sub2_sub3); |
| 312 | MI->eraseFromParent(); |
| 313 | break; |
| 314 | } |
Tom Stellard | 2a6a6105 | 2013-07-12 18:15:08 +0000 | [diff] [blame] | 315 | case AMDGPU::V_SUB_F64: { |
| 316 | const SIInstrInfo *TII = |
| 317 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 318 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), |
| 319 | MI->getOperand(0).getReg()) |
| 320 | .addReg(MI->getOperand(1).getReg()) |
| 321 | .addReg(MI->getOperand(2).getReg()) |
| 322 | .addImm(0) /* src2 */ |
| 323 | .addImm(0) /* ABS */ |
| 324 | .addImm(0) /* CLAMP */ |
| 325 | .addImm(0) /* OMOD */ |
| 326 | .addImm(2); /* NEG */ |
| 327 | MI->eraseFromParent(); |
| 328 | break; |
| 329 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 330 | } |
| 331 | return BB; |
| 332 | } |
| 333 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 334 | EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 335 | if (!VT.isVector()) { |
| 336 | return MVT::i1; |
| 337 | } |
| 338 | return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Christian Konig | 082a14a | 2013-03-18 11:34:05 +0000 | [diff] [blame] | 341 | MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const { |
| 342 | return MVT::i32; |
| 343 | } |
| 344 | |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 345 | bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 346 | VT = VT.getScalarType(); |
| 347 | |
| 348 | if (!VT.isSimple()) |
| 349 | return false; |
| 350 | |
| 351 | switch (VT.getSimpleVT().SimpleTy) { |
| 352 | case MVT::f32: |
| 353 | return false; /* There is V_MAD_F32 for f32 */ |
| 354 | case MVT::f64: |
| 355 | return true; |
| 356 | default: |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | return false; |
| 361 | } |
| 362 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 363 | //===----------------------------------------------------------------------===// |
| 364 | // Custom DAG Lowering Operations |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
| 367 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 368 | MachineFunction &MF = DAG.getMachineFunction(); |
| 369 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 370 | switch (Op.getOpcode()) { |
| 371 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 372 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 373 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame] | 374 | case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG); |
Tom Stellard | 98f675a | 2013-08-01 15:23:26 +0000 | [diff] [blame] | 375 | case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 376 | case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 377 | case ISD::INTRINSIC_WO_CHAIN: { |
| 378 | unsigned IntrinsicID = |
| 379 | cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 380 | EVT VT = Op.getValueType(); |
| 381 | SDLoc DL(Op); |
| 382 | //XXX: Hardcoded we only use two to store the pointer to the parameters. |
| 383 | unsigned NumUserSGPRs = 2; |
| 384 | switch (IntrinsicID) { |
| 385 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
| 386 | case Intrinsic::r600_read_ngroups_x: |
| 387 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 0); |
| 388 | case Intrinsic::r600_read_ngroups_y: |
| 389 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 4); |
| 390 | case Intrinsic::r600_read_ngroups_z: |
| 391 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 8); |
| 392 | case Intrinsic::r600_read_global_size_x: |
| 393 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 12); |
| 394 | case Intrinsic::r600_read_global_size_y: |
| 395 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 16); |
| 396 | case Intrinsic::r600_read_global_size_z: |
| 397 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 20); |
| 398 | case Intrinsic::r600_read_local_size_x: |
| 399 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 24); |
| 400 | case Intrinsic::r600_read_local_size_y: |
| 401 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 28); |
| 402 | case Intrinsic::r600_read_local_size_z: |
| 403 | return LowerParameter(DAG, VT, DL, DAG.getEntryNode(), 32); |
| 404 | case Intrinsic::r600_read_tgid_x: |
| 405 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
| 406 | AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT); |
| 407 | case Intrinsic::r600_read_tgid_y: |
| 408 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
| 409 | AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT); |
| 410 | case Intrinsic::r600_read_tgid_z: |
| 411 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
| 412 | AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT); |
| 413 | case Intrinsic::r600_read_tidig_x: |
| 414 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 415 | AMDGPU::VGPR0, VT); |
| 416 | case Intrinsic::r600_read_tidig_y: |
| 417 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 418 | AMDGPU::VGPR1, VT); |
| 419 | case Intrinsic::r600_read_tidig_z: |
| 420 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 421 | AMDGPU::VGPR2, VT); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame^] | 422 | case AMDGPUIntrinsic::SI_load_const: { |
| 423 | SDValue Ops [] = { |
| 424 | ResourceDescriptorToi128(Op.getOperand(1), DAG), |
| 425 | Op.getOperand(2) |
| 426 | }; |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 427 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame^] | 428 | MachineMemOperand *MMO = new MachineMemOperand(MachinePointerInfo(), |
| 429 | MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, |
| 430 | VT.getSizeInBits() / 8, 4); |
| 431 | return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, |
| 432 | Op->getVTList(), Ops, 2, VT, MMO); |
| 433 | } |
| 434 | case AMDGPUIntrinsic::SI_sample: |
| 435 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG); |
| 436 | case AMDGPUIntrinsic::SI_sampleb: |
| 437 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG); |
| 438 | case AMDGPUIntrinsic::SI_sampled: |
| 439 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG); |
| 440 | case AMDGPUIntrinsic::SI_samplel: |
| 441 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG); |
| 442 | case AMDGPUIntrinsic::SI_vs_load_input: |
| 443 | return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, |
| 444 | ResourceDescriptorToi128(Op.getOperand(1), DAG), |
| 445 | Op.getOperand(2), |
| 446 | Op.getOperand(3)); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 449 | } |
| 450 | return SDValue(); |
| 451 | } |
| 452 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 453 | /// \brief Helper function for LowerBRCOND |
| 454 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 455 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 456 | SDNode *Parent = Value.getNode(); |
| 457 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 458 | I != E; ++I) { |
| 459 | |
| 460 | if (I.getUse().get() != Value) |
| 461 | continue; |
| 462 | |
| 463 | if (I->getOpcode() == Opcode) |
| 464 | return *I; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /// This transforms the control flow intrinsics to get the branch destination as |
| 470 | /// last parameter, also switches branch target with BR if the need arise |
| 471 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 472 | SelectionDAG &DAG) const { |
| 473 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 474 | SDLoc DL(BRCOND); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 475 | |
| 476 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 477 | SDValue Target = BRCOND.getOperand(2); |
| 478 | SDNode *BR = 0; |
| 479 | |
| 480 | if (Intr->getOpcode() == ISD::SETCC) { |
| 481 | // As long as we negate the condition everything is fine |
| 482 | SDNode *SetCC = Intr; |
| 483 | assert(SetCC->getConstantOperandVal(1) == 1); |
NAKAMURA Takumi | 458a827 | 2013-01-07 11:14:44 +0000 | [diff] [blame] | 484 | assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 485 | ISD::SETNE); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 486 | Intr = SetCC->getOperand(0).getNode(); |
| 487 | |
| 488 | } else { |
| 489 | // Get the target from BR if we don't negate the condition |
| 490 | BR = findUser(BRCOND, ISD::BR); |
| 491 | Target = BR->getOperand(1); |
| 492 | } |
| 493 | |
| 494 | assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); |
| 495 | |
| 496 | // Build the result and |
| 497 | SmallVector<EVT, 4> Res; |
| 498 | for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i) |
| 499 | Res.push_back(Intr->getValueType(i)); |
| 500 | |
| 501 | // operands of the new intrinsic call |
| 502 | SmallVector<SDValue, 4> Ops; |
| 503 | Ops.push_back(BRCOND.getOperand(0)); |
| 504 | for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i) |
| 505 | Ops.push_back(Intr->getOperand(i)); |
| 506 | Ops.push_back(Target); |
| 507 | |
| 508 | // build the new intrinsic call |
| 509 | SDNode *Result = DAG.getNode( |
| 510 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
| 511 | DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode(); |
| 512 | |
| 513 | if (BR) { |
| 514 | // Give the branch instruction our target |
| 515 | SDValue Ops[] = { |
| 516 | BR->getOperand(0), |
| 517 | BRCOND.getOperand(2) |
| 518 | }; |
| 519 | DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2); |
| 520 | } |
| 521 | |
| 522 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 523 | |
| 524 | // Copy the intrinsic results to registers |
| 525 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 526 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 527 | if (!CopyToReg) |
| 528 | continue; |
| 529 | |
| 530 | Chain = DAG.getCopyToReg( |
| 531 | Chain, DL, |
| 532 | CopyToReg->getOperand(1), |
| 533 | SDValue(Result, i - 1), |
| 534 | SDValue()); |
| 535 | |
| 536 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 537 | } |
| 538 | |
| 539 | // Remove the old intrinsic from the chain |
| 540 | DAG.ReplaceAllUsesOfValueWith( |
| 541 | SDValue(Intr, Intr->getNumValues() - 1), |
| 542 | Intr->getOperand(0)); |
| 543 | |
| 544 | return Chain; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame^] | 547 | SDValue SITargetLowering::ResourceDescriptorToi128(SDValue Op, |
| 548 | SelectionDAG &DAG) const { |
| 549 | |
| 550 | if (Op.getValueType() == MVT::i128) { |
| 551 | return Op; |
| 552 | } |
| 553 | |
| 554 | assert(Op.getOpcode() == ISD::UNDEF); |
| 555 | |
| 556 | return DAG.getNode(ISD::BUILD_PAIR, SDLoc(Op), MVT::i128, |
| 557 | DAG.getConstant(0, MVT::i64), |
| 558 | DAG.getConstant(0, MVT::i64)); |
| 559 | } |
| 560 | |
| 561 | SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode, |
| 562 | const SDValue &Op, |
| 563 | SelectionDAG &DAG) const { |
| 564 | return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1), |
| 565 | Op.getOperand(2), |
| 566 | ResourceDescriptorToi128(Op.getOperand(3), DAG), |
| 567 | Op.getOperand(4)); |
| 568 | } |
| 569 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 570 | SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { |
| 571 | SDValue LHS = Op.getOperand(0); |
| 572 | SDValue RHS = Op.getOperand(1); |
| 573 | SDValue True = Op.getOperand(2); |
| 574 | SDValue False = Op.getOperand(3); |
| 575 | SDValue CC = Op.getOperand(4); |
| 576 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 577 | SDLoc DL(Op); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 578 | |
| 579 | // Possible Min/Max pattern |
| 580 | SDValue MinMax = LowerMinMax(Op, DAG); |
| 581 | if (MinMax.getNode()) { |
| 582 | return MinMax; |
| 583 | } |
| 584 | |
| 585 | SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC); |
| 586 | return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False); |
| 587 | } |
| 588 | |
Tom Stellard | 046039e | 2013-06-03 17:40:03 +0000 | [diff] [blame] | 589 | SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op, |
| 590 | SelectionDAG &DAG) const { |
| 591 | EVT VT = Op.getValueType(); |
| 592 | SDLoc DL(Op); |
| 593 | |
| 594 | if (VT != MVT::i64) { |
| 595 | return SDValue(); |
| 596 | } |
| 597 | |
| 598 | SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0), |
| 599 | DAG.getConstant(31, MVT::i32)); |
| 600 | |
| 601 | return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi); |
| 602 | } |
| 603 | |
Tom Stellard | 98f675a | 2013-08-01 15:23:26 +0000 | [diff] [blame] | 604 | SDValue SITargetLowering::LowerZERO_EXTEND(SDValue Op, |
| 605 | SelectionDAG &DAG) const { |
| 606 | EVT VT = Op.getValueType(); |
| 607 | SDLoc DL(Op); |
| 608 | |
| 609 | if (VT != MVT::i64) { |
| 610 | return SDValue(); |
| 611 | } |
| 612 | |
| 613 | return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), |
| 614 | DAG.getConstant(0, MVT::i32)); |
| 615 | } |
| 616 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 617 | //===----------------------------------------------------------------------===// |
| 618 | // Custom DAG optimizations |
| 619 | //===----------------------------------------------------------------------===// |
| 620 | |
| 621 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 622 | DAGCombinerInfo &DCI) const { |
| 623 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 624 | SDLoc DL(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 625 | EVT VT = N->getValueType(0); |
| 626 | |
| 627 | switch (N->getOpcode()) { |
| 628 | default: break; |
| 629 | case ISD::SELECT_CC: { |
| 630 | N->dump(); |
| 631 | ConstantSDNode *True, *False; |
| 632 | // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc) |
| 633 | if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 634 | && (False = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 635 | && True->isAllOnesValue() |
| 636 | && False->isNullValue() |
| 637 | && VT == MVT::i1) { |
| 638 | return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0), |
| 639 | N->getOperand(1), N->getOperand(4)); |
| 640 | |
| 641 | } |
| 642 | break; |
| 643 | } |
| 644 | case ISD::SETCC: { |
| 645 | SDValue Arg0 = N->getOperand(0); |
| 646 | SDValue Arg1 = N->getOperand(1); |
| 647 | SDValue CC = N->getOperand(2); |
| 648 | ConstantSDNode * C = NULL; |
| 649 | ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get(); |
| 650 | |
| 651 | // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne) |
| 652 | if (VT == MVT::i1 |
| 653 | && Arg0.getOpcode() == ISD::SIGN_EXTEND |
| 654 | && Arg0.getOperand(0).getValueType() == MVT::i1 |
| 655 | && (C = dyn_cast<ConstantSDNode>(Arg1)) |
| 656 | && C->isNullValue() |
| 657 | && CCOp == ISD::SETNE) { |
| 658 | return SimplifySetCC(VT, Arg0.getOperand(0), |
| 659 | DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL); |
| 660 | } |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | return SDValue(); |
| 665 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 666 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 667 | /// \brief Test if RegClass is one of the VSrc classes |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 668 | static bool isVSrc(unsigned RegClass) { |
| 669 | return AMDGPU::VSrc_32RegClassID == RegClass || |
| 670 | AMDGPU::VSrc_64RegClassID == RegClass; |
| 671 | } |
| 672 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 673 | /// \brief Test if RegClass is one of the SSrc classes |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 674 | static bool isSSrc(unsigned RegClass) { |
| 675 | return AMDGPU::SSrc_32RegClassID == RegClass || |
| 676 | AMDGPU::SSrc_64RegClassID == RegClass; |
| 677 | } |
| 678 | |
| 679 | /// \brief Analyze the possible immediate value Op |
| 680 | /// |
| 681 | /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate |
| 682 | /// and the immediate value if it's a literal immediate |
| 683 | int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { |
| 684 | |
| 685 | union { |
| 686 | int32_t I; |
| 687 | float F; |
| 688 | } Imm; |
| 689 | |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 690 | if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) { |
| 691 | if (Node->getZExtValue() >> 32) { |
| 692 | return -1; |
| 693 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 694 | Imm.I = Node->getSExtValue(); |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 695 | } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 696 | Imm.F = Node->getValueAPF().convertToFloat(); |
| 697 | else |
| 698 | return -1; // It isn't an immediate |
| 699 | |
| 700 | if ((Imm.I >= -16 && Imm.I <= 64) || |
| 701 | Imm.F == 0.5f || Imm.F == -0.5f || |
| 702 | Imm.F == 1.0f || Imm.F == -1.0f || |
| 703 | Imm.F == 2.0f || Imm.F == -2.0f || |
| 704 | Imm.F == 4.0f || Imm.F == -4.0f) |
| 705 | return 0; // It's an inline immediate |
| 706 | |
| 707 | return Imm.I; // It's a literal immediate |
| 708 | } |
| 709 | |
| 710 | /// \brief Try to fold an immediate directly into an instruction |
| 711 | bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate, |
| 712 | bool &ScalarSlotUsed) const { |
| 713 | |
| 714 | MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand); |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 715 | const SIInstrInfo *TII = |
| 716 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 717 | if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode())) |
| 718 | return false; |
| 719 | |
| 720 | const SDValue &Op = Mov->getOperand(0); |
| 721 | int32_t Value = analyzeImmediate(Op.getNode()); |
| 722 | if (Value == -1) { |
| 723 | // Not an immediate at all |
| 724 | return false; |
| 725 | |
| 726 | } else if (Value == 0) { |
| 727 | // Inline immediates can always be fold |
| 728 | Operand = Op; |
| 729 | return true; |
| 730 | |
| 731 | } else if (Value == Immediate) { |
| 732 | // Already fold literal immediate |
| 733 | Operand = Op; |
| 734 | return true; |
| 735 | |
| 736 | } else if (!ScalarSlotUsed && !Immediate) { |
| 737 | // Fold this literal immediate |
| 738 | ScalarSlotUsed = true; |
| 739 | Immediate = Value; |
| 740 | Operand = Op; |
| 741 | return true; |
| 742 | |
| 743 | } |
| 744 | |
| 745 | return false; |
| 746 | } |
| 747 | |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 748 | const TargetRegisterClass *SITargetLowering::getRegClassForNode( |
| 749 | SelectionDAG &DAG, const SDValue &Op) const { |
| 750 | const SIInstrInfo *TII = |
| 751 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 752 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 753 | |
| 754 | if (!Op->isMachineOpcode()) { |
| 755 | switch(Op->getOpcode()) { |
| 756 | case ISD::CopyFromReg: { |
| 757 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 758 | unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg(); |
| 759 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 760 | return MRI.getRegClass(Reg); |
| 761 | } |
| 762 | return TRI.getPhysRegClass(Reg); |
| 763 | } |
| 764 | default: return NULL; |
| 765 | } |
| 766 | } |
| 767 | const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode()); |
| 768 | int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass; |
| 769 | if (OpClassID != -1) { |
| 770 | return TRI.getRegClass(OpClassID); |
| 771 | } |
| 772 | switch(Op.getMachineOpcode()) { |
| 773 | case AMDGPU::COPY_TO_REGCLASS: |
| 774 | // Operand 1 is the register class id for COPY_TO_REGCLASS instructions. |
| 775 | OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue(); |
| 776 | |
| 777 | // If the COPY_TO_REGCLASS instruction is copying to a VSrc register |
| 778 | // class, then the register class for the value could be either a |
| 779 | // VReg or and SReg. In order to get a more accurate |
| 780 | if (OpClassID == AMDGPU::VSrc_32RegClassID || |
| 781 | OpClassID == AMDGPU::VSrc_64RegClassID) { |
| 782 | return getRegClassForNode(DAG, Op.getOperand(0)); |
| 783 | } |
| 784 | return TRI.getRegClass(OpClassID); |
| 785 | case AMDGPU::EXTRACT_SUBREG: { |
| 786 | int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 787 | const TargetRegisterClass *SuperClass = |
| 788 | getRegClassForNode(DAG, Op.getOperand(0)); |
| 789 | return TRI.getSubClassWithSubReg(SuperClass, SubIdx); |
| 790 | } |
| 791 | case AMDGPU::REG_SEQUENCE: |
| 792 | // Operand 0 is the register class id for REG_SEQUENCE instructions. |
| 793 | return TRI.getRegClass( |
| 794 | cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()); |
| 795 | default: |
| 796 | return getRegClassFor(Op.getSimpleValueType()); |
| 797 | } |
| 798 | } |
| 799 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 800 | /// \brief Does "Op" fit into register class "RegClass" ? |
Tom Stellard | b35efba | 2013-05-20 15:02:01 +0000 | [diff] [blame] | 801 | bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op, |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 802 | unsigned RegClass) const { |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 803 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 804 | const TargetRegisterClass *RC = getRegClassForNode(DAG, Op); |
| 805 | if (!RC) { |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 806 | return false; |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 807 | } |
| 808 | return TRI->getRegClass(RegClass)->hasSubClassEq(RC); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /// \brief Make sure that we don't exeed the number of allowed scalars |
| 812 | void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand, |
| 813 | unsigned RegClass, |
| 814 | bool &ScalarSlotUsed) const { |
| 815 | |
| 816 | // First map the operands register class to a destination class |
| 817 | if (RegClass == AMDGPU::VSrc_32RegClassID) |
| 818 | RegClass = AMDGPU::VReg_32RegClassID; |
| 819 | else if (RegClass == AMDGPU::VSrc_64RegClassID) |
| 820 | RegClass = AMDGPU::VReg_64RegClassID; |
| 821 | else |
| 822 | return; |
| 823 | |
| 824 | // Nothing todo if they fit naturaly |
| 825 | if (fitsRegClass(DAG, Operand, RegClass)) |
| 826 | return; |
| 827 | |
| 828 | // If the scalar slot isn't used yet use it now |
| 829 | if (!ScalarSlotUsed) { |
| 830 | ScalarSlotUsed = true; |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | // This is a conservative aproach, it is possible that we can't determine |
| 835 | // the correct register class and copy too often, but better save than sorry. |
| 836 | SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 837 | SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(), |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 838 | Operand.getValueType(), Operand, RC); |
| 839 | Operand = SDValue(Node, 0); |
| 840 | } |
| 841 | |
Tom Stellard | acec99c | 2013-06-05 23:39:50 +0000 | [diff] [blame] | 842 | /// \returns true if \p Node's operands are different from the SDValue list |
| 843 | /// \p Ops |
| 844 | static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) { |
| 845 | for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) { |
| 846 | if (Ops[i].getNode() != Node->getOperand(i).getNode()) { |
| 847 | return true; |
| 848 | } |
| 849 | } |
| 850 | return false; |
| 851 | } |
| 852 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 853 | /// \brief Try to fold the Nodes operands into the Node |
| 854 | SDNode *SITargetLowering::foldOperands(MachineSDNode *Node, |
| 855 | SelectionDAG &DAG) const { |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 856 | |
| 857 | // Original encoding (either e32 or e64) |
| 858 | int Opcode = Node->getMachineOpcode(); |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 859 | const SIInstrInfo *TII = |
| 860 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 861 | const MCInstrDesc *Desc = &TII->get(Opcode); |
| 862 | |
| 863 | unsigned NumDefs = Desc->getNumDefs(); |
| 864 | unsigned NumOps = Desc->getNumOperands(); |
| 865 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 866 | // Commuted opcode if available |
| 867 | int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1; |
| 868 | const MCInstrDesc *DescRev = OpcodeRev == -1 ? 0 : &TII->get(OpcodeRev); |
| 869 | |
| 870 | assert(!DescRev || DescRev->getNumDefs() == NumDefs); |
| 871 | assert(!DescRev || DescRev->getNumOperands() == NumOps); |
| 872 | |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 873 | // e64 version if available, -1 otherwise |
| 874 | int OpcodeE64 = AMDGPU::getVOPe64(Opcode); |
| 875 | const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64); |
| 876 | |
| 877 | assert(!DescE64 || DescE64->getNumDefs() == NumDefs); |
| 878 | assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4)); |
| 879 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 880 | int32_t Immediate = Desc->getSize() == 4 ? 0 : -1; |
| 881 | bool HaveVSrc = false, HaveSSrc = false; |
| 882 | |
| 883 | // First figure out what we alread have in this instruction |
| 884 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 885 | i != e && Op < NumOps; ++i, ++Op) { |
| 886 | |
| 887 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
| 888 | if (isVSrc(RegClass)) |
| 889 | HaveVSrc = true; |
| 890 | else if (isSSrc(RegClass)) |
| 891 | HaveSSrc = true; |
| 892 | else |
| 893 | continue; |
| 894 | |
| 895 | int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode()); |
| 896 | if (Imm != -1 && Imm != 0) { |
| 897 | // Literal immediate |
| 898 | Immediate = Imm; |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | // If we neither have VSrc nor SSrc it makes no sense to continue |
| 903 | if (!HaveVSrc && !HaveSSrc) |
| 904 | return Node; |
| 905 | |
| 906 | // No scalar allowed when we have both VSrc and SSrc |
| 907 | bool ScalarSlotUsed = HaveVSrc && HaveSSrc; |
| 908 | |
| 909 | // Second go over the operands and try to fold them |
| 910 | std::vector<SDValue> Ops; |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 911 | bool Promote2e64 = false; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 912 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 913 | i != e && Op < NumOps; ++i, ++Op) { |
| 914 | |
| 915 | const SDValue &Operand = Node->getOperand(i); |
| 916 | Ops.push_back(Operand); |
| 917 | |
| 918 | // Already folded immediate ? |
| 919 | if (isa<ConstantSDNode>(Operand.getNode()) || |
| 920 | isa<ConstantFPSDNode>(Operand.getNode())) |
| 921 | continue; |
| 922 | |
| 923 | // Is this a VSrc or SSrc operand ? |
| 924 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 925 | if (isVSrc(RegClass) || isSSrc(RegClass)) { |
| 926 | // Try to fold the immediates |
| 927 | if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) { |
| 928 | // Folding didn't worked, make sure we don't hit the SReg limit |
| 929 | ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed); |
| 930 | } |
| 931 | continue; |
| 932 | } |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 933 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 934 | if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) { |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 935 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 936 | unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass; |
| 937 | assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass)); |
| 938 | |
| 939 | // Test if it makes sense to swap operands |
| 940 | if (foldImm(Ops[1], Immediate, ScalarSlotUsed) || |
| 941 | (!fitsRegClass(DAG, Ops[1], RegClass) && |
| 942 | fitsRegClass(DAG, Ops[1], OtherRegClass))) { |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 943 | |
| 944 | // Swap commutable operands |
| 945 | SDValue Tmp = Ops[1]; |
| 946 | Ops[1] = Ops[0]; |
| 947 | Ops[0] = Tmp; |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 948 | |
| 949 | Desc = DescRev; |
| 950 | DescRev = 0; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 951 | continue; |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 952 | } |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 953 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 954 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 955 | if (DescE64 && !Immediate) { |
| 956 | |
| 957 | // Test if it makes sense to switch to e64 encoding |
| 958 | unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass; |
| 959 | if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass)) |
| 960 | continue; |
| 961 | |
| 962 | int32_t TmpImm = -1; |
| 963 | if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) || |
| 964 | (!fitsRegClass(DAG, Ops[i], RegClass) && |
| 965 | fitsRegClass(DAG, Ops[1], OtherRegClass))) { |
| 966 | |
| 967 | // Switch to e64 encoding |
| 968 | Immediate = -1; |
| 969 | Promote2e64 = true; |
| 970 | Desc = DescE64; |
| 971 | DescE64 = 0; |
| 972 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 976 | if (Promote2e64) { |
| 977 | // Add the modifier flags while promoting |
| 978 | for (unsigned i = 0; i < 4; ++i) |
| 979 | Ops.push_back(DAG.getTargetConstant(0, MVT::i32)); |
| 980 | } |
| 981 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 982 | // Add optional chain and glue |
| 983 | for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i) |
| 984 | Ops.push_back(Node->getOperand(i)); |
| 985 | |
Tom Stellard | b5a9700 | 2013-06-03 17:39:50 +0000 | [diff] [blame] | 986 | // Nodes that have a glue result are not CSE'd by getMachineNode(), so in |
| 987 | // this case a brand new node is always be created, even if the operands |
| 988 | // are the same as before. So, manually check if anything has been changed. |
Tom Stellard | acec99c | 2013-06-05 23:39:50 +0000 | [diff] [blame] | 989 | if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) { |
| 990 | return Node; |
Tom Stellard | b5a9700 | 2013-06-03 17:39:50 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 993 | // Create a complete new instruction |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 994 | return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops); |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 995 | } |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 996 | |
| 997 | /// \brief Helper function for adjustWritemask |
Benjamin Kramer | 635e368 | 2013-05-23 15:43:05 +0000 | [diff] [blame] | 998 | static unsigned SubIdx2Lane(unsigned Idx) { |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 999 | switch (Idx) { |
| 1000 | default: return 0; |
| 1001 | case AMDGPU::sub0: return 0; |
| 1002 | case AMDGPU::sub1: return 1; |
| 1003 | case AMDGPU::sub2: return 2; |
| 1004 | case AMDGPU::sub3: return 3; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /// \brief Adjust the writemask of MIMG instructions |
| 1009 | void SITargetLowering::adjustWritemask(MachineSDNode *&Node, |
| 1010 | SelectionDAG &DAG) const { |
| 1011 | SDNode *Users[4] = { }; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1012 | unsigned Writemask = 0, Lane = 0; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Try to figure out the used register components |
| 1015 | for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); |
| 1016 | I != E; ++I) { |
| 1017 | |
| 1018 | // Abort if we can't understand the usage |
| 1019 | if (!I->isMachineOpcode() || |
| 1020 | I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) |
| 1021 | return; |
| 1022 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1023 | Lane = SubIdx2Lane(I->getConstantOperandVal(1)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Abort if we have more than one user per component |
| 1026 | if (Users[Lane]) |
| 1027 | return; |
| 1028 | |
| 1029 | Users[Lane] = *I; |
| 1030 | Writemask |= 1 << Lane; |
| 1031 | } |
| 1032 | |
| 1033 | // Abort if all components are used |
| 1034 | if (Writemask == 0xf) |
| 1035 | return; |
| 1036 | |
| 1037 | // Adjust the writemask in the node |
| 1038 | std::vector<SDValue> Ops; |
| 1039 | Ops.push_back(DAG.getTargetConstant(Writemask, MVT::i32)); |
| 1040 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 1041 | Ops.push_back(Node->getOperand(i)); |
| 1042 | Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size()); |
| 1043 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1044 | // If we only got one lane, replace it with a copy |
| 1045 | if (Writemask == (1U << Lane)) { |
| 1046 | SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32); |
| 1047 | SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1048 | SDLoc(), Users[Lane]->getValueType(0), |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1049 | SDValue(Node, 0), RC); |
| 1050 | DAG.ReplaceAllUsesWith(Users[Lane], Copy); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1054 | // Update the users of the node with the new indices |
| 1055 | for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { |
| 1056 | |
| 1057 | SDNode *User = Users[i]; |
| 1058 | if (!User) |
| 1059 | continue; |
| 1060 | |
| 1061 | SDValue Op = DAG.getTargetConstant(Idx, MVT::i32); |
| 1062 | DAG.UpdateNodeOperands(User, User->getOperand(0), Op); |
| 1063 | |
| 1064 | switch (Idx) { |
| 1065 | default: break; |
| 1066 | case AMDGPU::sub0: Idx = AMDGPU::sub1; break; |
| 1067 | case AMDGPU::sub1: Idx = AMDGPU::sub2; break; |
| 1068 | case AMDGPU::sub2: Idx = AMDGPU::sub3; break; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /// \brief Fold the instructions after slecting them |
| 1074 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 1075 | SelectionDAG &DAG) const { |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1076 | const SIInstrInfo *TII = |
| 1077 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 1078 | Node = AdjustRegClass(Node, DAG); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1079 | |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1080 | if (TII->isMIMG(Node->getMachineOpcode())) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1081 | adjustWritemask(Node, DAG); |
| 1082 | |
| 1083 | return foldOperands(Node, DAG); |
| 1084 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1085 | |
| 1086 | /// \brief Assign the register class depending on the number of |
| 1087 | /// bits set in the writemask |
| 1088 | void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, |
| 1089 | SDNode *Node) const { |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1090 | const SIInstrInfo *TII = |
| 1091 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 1092 | if (!TII->isMIMG(MI->getOpcode())) |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1093 | return; |
| 1094 | |
| 1095 | unsigned VReg = MI->getOperand(0).getReg(); |
| 1096 | unsigned Writemask = MI->getOperand(1).getImm(); |
| 1097 | unsigned BitsSet = 0; |
| 1098 | for (unsigned i = 0; i < 4; ++i) |
| 1099 | BitsSet += Writemask & (1 << i) ? 1 : 0; |
| 1100 | |
| 1101 | const TargetRegisterClass *RC; |
| 1102 | switch (BitsSet) { |
| 1103 | default: return; |
| 1104 | case 1: RC = &AMDGPU::VReg_32RegClass; break; |
| 1105 | case 2: RC = &AMDGPU::VReg_64RegClass; break; |
| 1106 | case 3: RC = &AMDGPU::VReg_96RegClass; break; |
| 1107 | } |
| 1108 | |
| 1109 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 1110 | MRI.setRegClass(VReg, RC); |
| 1111 | } |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 1112 | |
| 1113 | MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N, |
| 1114 | SelectionDAG &DAG) const { |
| 1115 | |
| 1116 | SDLoc DL(N); |
| 1117 | unsigned NewOpcode = N->getMachineOpcode(); |
| 1118 | |
| 1119 | switch (N->getMachineOpcode()) { |
| 1120 | default: return N; |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 1121 | case AMDGPU::S_LOAD_DWORD_IMM: |
| 1122 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64; |
| 1123 | // Fall-through |
| 1124 | case AMDGPU::S_LOAD_DWORDX2_SGPR: |
| 1125 | if (NewOpcode == N->getMachineOpcode()) { |
| 1126 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64; |
| 1127 | } |
| 1128 | // Fall-through |
| 1129 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
| 1130 | case AMDGPU::S_LOAD_DWORDX4_SGPR: { |
| 1131 | if (NewOpcode == N->getMachineOpcode()) { |
| 1132 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64; |
| 1133 | } |
| 1134 | if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) { |
| 1135 | return N; |
| 1136 | } |
| 1137 | ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1)); |
| 1138 | SDValue Ops[] = { |
| 1139 | SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128, |
| 1140 | DAG.getConstant(0, MVT::i64)), 0), |
| 1141 | N->getOperand(0), |
| 1142 | DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32) |
| 1143 | }; |
| 1144 | return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 1148 | |
| 1149 | SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, |
| 1150 | const TargetRegisterClass *RC, |
| 1151 | unsigned Reg, EVT VT) const { |
| 1152 | SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); |
| 1153 | |
| 1154 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), |
| 1155 | cast<RegisterSDNode>(VReg)->getReg(), VT); |
| 1156 | } |