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 | |
NAKAMURA Takumi | 45e0a83 | 2014-07-20 11:15:07 +0000 | [diff] [blame] | 15 | #ifdef _MSC_VER |
| 16 | // Provide M_PI. |
| 17 | #define _USE_MATH_DEFINES |
| 18 | #include <cmath> |
| 19 | #endif |
| 20 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 21 | #include "AMDGPU.h" |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 22 | #include "AMDGPUIntrinsicInfo.h" |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 23 | #include "AMDGPUSubtarget.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 24 | #include "SIISelLowering.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 25 | #include "SIInstrInfo.h" |
| 26 | #include "SIMachineFunctionInfo.h" |
| 27 | #include "SIRegisterInfo.h" |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/BitVector.h" |
Matt Arsenault | 9a10cea | 2016-01-26 04:29:24 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringSwitch.h" |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/CallingConvLower.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 33 | #include "llvm/CodeGen/SelectionDAG.h" |
Wei Ding | 07e0371 | 2016-07-28 16:42:13 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/Analysis.h" |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 35 | #include "llvm/IR/DiagnosticInfo.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Function.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
| 39 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 40 | static cl::opt<bool> EnableVGPRIndexMode( |
| 41 | "amdgpu-vgpr-index-mode", |
| 42 | cl::desc("Use GPR indexing mode instead of movrel for vector indexing"), |
| 43 | cl::init(false)); |
| 44 | |
| 45 | |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 46 | static unsigned findFirstFreeSGPR(CCState &CCInfo) { |
| 47 | unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs(); |
| 48 | for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) { |
| 49 | if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) { |
| 50 | return AMDGPU::SGPR0 + Reg; |
| 51 | } |
| 52 | } |
| 53 | llvm_unreachable("Cannot allocate sgpr"); |
| 54 | } |
| 55 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 56 | SITargetLowering::SITargetLowering(const TargetMachine &TM, |
| 57 | const SISubtarget &STI) |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 58 | : AMDGPUTargetLowering(TM, STI) { |
Tom Stellard | 1bd8072 | 2014-04-30 15:31:33 +0000 | [diff] [blame] | 59 | addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 60 | addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 61 | |
Tom Stellard | 334b29c | 2014-04-17 21:00:09 +0000 | [diff] [blame] | 62 | addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 63 | addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 64 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 65 | addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); |
| 66 | addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); |
| 67 | addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 68 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 69 | addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass); |
| 70 | addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass); |
| 71 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 72 | addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); |
| 73 | addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 74 | |
Tom Stellard | f0a2107 | 2014-11-18 20:39:39 +0000 | [diff] [blame] | 75 | addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 76 | addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); |
| 77 | |
Tom Stellard | f0a2107 | 2014-11-18 20:39:39 +0000 | [diff] [blame] | 78 | addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 79 | addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 80 | |
Eric Christopher | 23a3a7c | 2015-02-26 00:00:24 +0000 | [diff] [blame] | 81 | computeRegisterProperties(STI.getRegisterInfo()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 82 | |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 83 | // We need to custom lower vector stores from local memory |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 84 | setOperationAction(ISD::LOAD, MVT::v2i32, Custom); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 85 | setOperationAction(ISD::LOAD, MVT::v4i32, Custom); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 86 | setOperationAction(ISD::LOAD, MVT::v8i32, Custom); |
| 87 | setOperationAction(ISD::LOAD, MVT::v16i32, Custom); |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 88 | setOperationAction(ISD::LOAD, MVT::i1, Custom); |
Matt Arsenault | 2b957b5 | 2016-05-02 20:07:26 +0000 | [diff] [blame] | 89 | |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 90 | setOperationAction(ISD::STORE, MVT::v2i32, Custom); |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 91 | setOperationAction(ISD::STORE, MVT::v4i32, Custom); |
| 92 | setOperationAction(ISD::STORE, MVT::v8i32, Custom); |
| 93 | setOperationAction(ISD::STORE, MVT::v16i32, Custom); |
| 94 | setOperationAction(ISD::STORE, MVT::i1, Custom); |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 95 | |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 96 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 97 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand); |
| 99 | |
| 100 | setOperationAction(ISD::SELECT, MVT::i1, Promote); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 101 | setOperationAction(ISD::SELECT, MVT::i64, Custom); |
Tom Stellard | da99c6e | 2014-03-24 16:07:30 +0000 | [diff] [blame] | 102 | setOperationAction(ISD::SELECT, MVT::f64, Promote); |
| 103 | AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 104 | |
Tom Stellard | 3ca1bfc | 2014-06-10 16:01:22 +0000 | [diff] [blame] | 105 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 106 | setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); |
| 107 | setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); |
| 108 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 109 | setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 110 | |
Tom Stellard | d1efda8 | 2016-01-20 21:48:24 +0000 | [diff] [blame] | 111 | setOperationAction(ISD::SETCC, MVT::i1, Promote); |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 112 | setOperationAction(ISD::SETCC, MVT::v2i1, Expand); |
| 113 | setOperationAction(ISD::SETCC, MVT::v4i1, Expand); |
| 114 | |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 115 | setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); |
| 116 | setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 117 | |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); |
| 119 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 120 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); |
| 121 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 122 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); |
| 123 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 124 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); |
| 125 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 126 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 127 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); |
Matt Arsenault | a9dbdca | 2016-04-12 14:05:04 +0000 | [diff] [blame] | 128 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); |
| 129 | |
Matt Arsenault | e54e1c3 | 2014-06-23 18:00:44 +0000 | [diff] [blame] | 130 | setOperationAction(ISD::BRCOND, MVT::Other, Custom); |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 131 | setOperationAction(ISD::BR_CC, MVT::i1, Expand); |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 132 | setOperationAction(ISD::BR_CC, MVT::i32, Expand); |
| 133 | setOperationAction(ISD::BR_CC, MVT::i64, Expand); |
| 134 | setOperationAction(ISD::BR_CC, MVT::f32, Expand); |
| 135 | setOperationAction(ISD::BR_CC, MVT::f64, Expand); |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 136 | |
Benjamin Kramer | 867bfc5 | 2015-03-07 17:41:00 +0000 | [diff] [blame] | 137 | // We only support LOAD/STORE and vector manipulation ops for vectors |
| 138 | // with > 4 elements. |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 139 | for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) { |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 140 | for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 141 | switch (Op) { |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 142 | case ISD::LOAD: |
| 143 | case ISD::STORE: |
| 144 | case ISD::BUILD_VECTOR: |
| 145 | case ISD::BITCAST: |
| 146 | case ISD::EXTRACT_VECTOR_ELT: |
| 147 | case ISD::INSERT_VECTOR_ELT: |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 148 | case ISD::INSERT_SUBVECTOR: |
| 149 | case ISD::EXTRACT_SUBVECTOR: |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 150 | case ISD::SCALAR_TO_VECTOR: |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 151 | break; |
Tom Stellard | c0503db | 2014-08-09 01:06:56 +0000 | [diff] [blame] | 152 | case ISD::CONCAT_VECTORS: |
| 153 | setOperationAction(Op, VT, Custom); |
| 154 | break; |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 155 | default: |
Matt Arsenault | d504a74 | 2014-05-15 21:44:05 +0000 | [diff] [blame] | 156 | setOperationAction(Op, VT, Expand); |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 162 | // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that |
| 163 | // is expanded to avoid having two separate loops in case the index is a VGPR. |
| 164 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 165 | // Most operations are naturally 32-bit vector operations. We only support |
| 166 | // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. |
| 167 | for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { |
| 168 | setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); |
| 169 | AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); |
| 170 | |
| 171 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); |
| 172 | AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); |
| 173 | |
| 174 | setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); |
| 175 | AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); |
| 176 | |
| 177 | setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); |
| 178 | AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); |
| 179 | } |
| 180 | |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 181 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); |
| 182 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); |
| 183 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); |
| 184 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 185 | |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 186 | // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling, |
| 187 | // and output demarshalling |
| 188 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); |
| 189 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); |
| 190 | |
| 191 | // We can't return success/failure, only the old value, |
| 192 | // let LLVM add the comparison |
| 193 | setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand); |
| 194 | setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand); |
| 195 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 196 | if (getSubtarget()->hasFlatAddressSpace()) { |
Matt Arsenault | 99c1452 | 2016-04-25 19:27:24 +0000 | [diff] [blame] | 197 | setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom); |
| 198 | setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom); |
| 199 | } |
| 200 | |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 201 | setOperationAction(ISD::BSWAP, MVT::i32, Legal); |
| 202 | setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); |
| 203 | |
| 204 | // On SI this is s_memtime and s_memrealtime on VI. |
| 205 | setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); |
Matt Arsenault | 0bb294b | 2016-06-17 22:27:03 +0000 | [diff] [blame] | 206 | setOperationAction(ISD::TRAP, MVT::Other, Custom); |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 207 | |
| 208 | setOperationAction(ISD::FMINNUM, MVT::f64, Legal); |
| 209 | setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); |
| 210 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 211 | if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) { |
Matt Arsenault | 71e6676 | 2016-05-21 02:27:49 +0000 | [diff] [blame] | 212 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
| 213 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
| 214 | setOperationAction(ISD::FRINT, MVT::f64, Legal); |
| 215 | } |
| 216 | |
| 217 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
| 218 | |
| 219 | setOperationAction(ISD::FSIN, MVT::f32, Custom); |
| 220 | setOperationAction(ISD::FCOS, MVT::f32, Custom); |
| 221 | setOperationAction(ISD::FDIV, MVT::f32, Custom); |
| 222 | setOperationAction(ISD::FDIV, MVT::f64, Custom); |
| 223 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 224 | setTargetDAGCombine(ISD::FADD); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 225 | setTargetDAGCombine(ISD::FSUB); |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 226 | setTargetDAGCombine(ISD::FMINNUM); |
| 227 | setTargetDAGCombine(ISD::FMAXNUM); |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 228 | setTargetDAGCombine(ISD::SMIN); |
| 229 | setTargetDAGCombine(ISD::SMAX); |
| 230 | setTargetDAGCombine(ISD::UMIN); |
| 231 | setTargetDAGCombine(ISD::UMAX); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 232 | setTargetDAGCombine(ISD::SETCC); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 233 | setTargetDAGCombine(ISD::AND); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 234 | setTargetDAGCombine(ISD::OR); |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 235 | setTargetDAGCombine(ISD::XOR); |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 236 | setTargetDAGCombine(ISD::UINT_TO_FP); |
Matt Arsenault | 9cd9071 | 2016-04-14 01:42:16 +0000 | [diff] [blame] | 237 | setTargetDAGCombine(ISD::FCANONICALIZE); |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 238 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 239 | // All memory operations. Some folding on the pointer operand is done to help |
| 240 | // matching the constant offsets in the addressing modes. |
| 241 | setTargetDAGCombine(ISD::LOAD); |
| 242 | setTargetDAGCombine(ISD::STORE); |
| 243 | setTargetDAGCombine(ISD::ATOMIC_LOAD); |
| 244 | setTargetDAGCombine(ISD::ATOMIC_STORE); |
| 245 | setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); |
| 246 | setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); |
| 247 | setTargetDAGCombine(ISD::ATOMIC_SWAP); |
| 248 | setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); |
| 249 | setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); |
| 250 | setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); |
| 251 | setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); |
| 252 | setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); |
| 253 | setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); |
| 254 | setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); |
| 255 | setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); |
| 256 | setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); |
| 257 | setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); |
| 258 | |
Christian Konig | eecebd0 | 2013-03-26 14:04:02 +0000 | [diff] [blame] | 259 | setSchedulingPreference(Sched::RegPressure); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 262 | const SISubtarget *SITargetLowering::getSubtarget() const { |
| 263 | return static_cast<const SISubtarget *>(Subtarget); |
| 264 | } |
| 265 | |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 266 | //===----------------------------------------------------------------------===// |
| 267 | // TargetLowering queries |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | |
Matt Arsenault | a9dbdca | 2016-04-12 14:05:04 +0000 | [diff] [blame] | 270 | bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, |
| 271 | const CallInst &CI, |
| 272 | unsigned IntrID) const { |
| 273 | switch (IntrID) { |
| 274 | case Intrinsic::amdgcn_atomic_inc: |
| 275 | case Intrinsic::amdgcn_atomic_dec: |
| 276 | Info.opc = ISD::INTRINSIC_W_CHAIN; |
| 277 | Info.memVT = MVT::getVT(CI.getType()); |
| 278 | Info.ptrVal = CI.getOperand(0); |
| 279 | Info.align = 0; |
| 280 | Info.vol = false; |
| 281 | Info.readMem = true; |
| 282 | Info.writeMem = true; |
| 283 | return true; |
| 284 | default: |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 289 | bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &, |
| 290 | EVT) const { |
| 291 | // SI has some legal vector types, but no legal vector operations. Say no |
| 292 | // shuffles are legal in order to prefer scalarizing some vector operations. |
| 293 | return false; |
| 294 | } |
| 295 | |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 296 | bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { |
| 297 | // Flat instructions do not have offsets, and only have the register |
| 298 | // address. |
| 299 | return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1); |
| 300 | } |
| 301 | |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 302 | bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { |
| 303 | // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and |
| 304 | // additionally can do r + r + i with addr64. 32-bit has more addressing |
| 305 | // mode options. Depending on the resource constant, it can also do |
| 306 | // (i64 r0) + (i32 r1) * (i14 i). |
| 307 | // |
| 308 | // Private arrays end up using a scratch buffer most of the time, so also |
| 309 | // assume those use MUBUF instructions. Scratch loads / stores are currently |
| 310 | // implemented as mubuf instructions with offen bit set, so slightly |
| 311 | // different than the normal addr64. |
| 312 | if (!isUInt<12>(AM.BaseOffs)) |
| 313 | return false; |
| 314 | |
| 315 | // FIXME: Since we can split immediate into soffset and immediate offset, |
| 316 | // would it make sense to allow any immediate? |
| 317 | |
| 318 | switch (AM.Scale) { |
| 319 | case 0: // r + i or just i, depending on HasBaseReg. |
| 320 | return true; |
| 321 | case 1: |
| 322 | return true; // We have r + r or r + i. |
| 323 | case 2: |
| 324 | if (AM.HasBaseReg) { |
| 325 | // Reject 2 * r + r. |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | // Allow 2 * r as r + r |
| 330 | // Or 2 * r + i is allowed as r + r + i. |
| 331 | return true; |
| 332 | default: // Don't allow n * r |
| 333 | return false; |
| 334 | } |
| 335 | } |
| 336 | |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 337 | bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, |
| 338 | const AddrMode &AM, Type *Ty, |
| 339 | unsigned AS) const { |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 340 | // No global is ever allowed as a base. |
| 341 | if (AM.BaseGV) |
| 342 | return false; |
| 343 | |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 344 | switch (AS) { |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 345 | case AMDGPUAS::GLOBAL_ADDRESS: { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 346 | if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 347 | // Assume the we will use FLAT for all global memory accesses |
| 348 | // on VI. |
| 349 | // FIXME: This assumption is currently wrong. On VI we still use |
| 350 | // MUBUF instructions for the r + i addressing mode. As currently |
| 351 | // implemented, the MUBUF instructions only work on buffer < 4GB. |
| 352 | // It may be possible to support > 4GB buffers with MUBUF instructions, |
| 353 | // by setting the stride value in the resource descriptor which would |
| 354 | // increase the size limit to (stride * 4GB). However, this is risky, |
| 355 | // because it has never been validated. |
| 356 | return isLegalFlatAddressingMode(AM); |
| 357 | } |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 358 | |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 359 | return isLegalMUBUFAddressingMode(AM); |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 360 | } |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 361 | case AMDGPUAS::CONSTANT_ADDRESS: { |
| 362 | // If the offset isn't a multiple of 4, it probably isn't going to be |
| 363 | // correctly aligned. |
Matt Arsenault | 3cc1e00 | 2016-08-13 01:43:51 +0000 | [diff] [blame] | 364 | // FIXME: Can we get the real alignment here? |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 365 | if (AM.BaseOffs % 4 != 0) |
| 366 | return isLegalMUBUFAddressingMode(AM); |
| 367 | |
| 368 | // There are no SMRD extloads, so if we have to do a small type access we |
| 369 | // will use a MUBUF load. |
| 370 | // FIXME?: We also need to do this if unaligned, but we don't know the |
| 371 | // alignment here. |
| 372 | if (DL.getTypeStoreSize(Ty) < 4) |
| 373 | return isLegalMUBUFAddressingMode(AM); |
| 374 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 375 | if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 376 | // SMRD instructions have an 8-bit, dword offset on SI. |
| 377 | if (!isUInt<8>(AM.BaseOffs / 4)) |
| 378 | return false; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 379 | } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) { |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 380 | // On CI+, this can also be a 32-bit literal constant offset. If it fits |
| 381 | // in 8-bits, it can use a smaller encoding. |
| 382 | if (!isUInt<32>(AM.BaseOffs / 4)) |
| 383 | return false; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 384 | } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) { |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 385 | // On VI, these use the SMEM format and the offset is 20-bit in bytes. |
| 386 | if (!isUInt<20>(AM.BaseOffs)) |
| 387 | return false; |
| 388 | } else |
| 389 | llvm_unreachable("unhandled generation"); |
| 390 | |
| 391 | if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. |
| 392 | return true; |
| 393 | |
| 394 | if (AM.Scale == 1 && AM.HasBaseReg) |
| 395 | return true; |
| 396 | |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | case AMDGPUAS::PRIVATE_ADDRESS: |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 401 | return isLegalMUBUFAddressingMode(AM); |
| 402 | |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 403 | case AMDGPUAS::LOCAL_ADDRESS: |
| 404 | case AMDGPUAS::REGION_ADDRESS: { |
| 405 | // Basic, single offset DS instructions allow a 16-bit unsigned immediate |
| 406 | // field. |
| 407 | // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have |
| 408 | // an 8-bit dword offset but we don't know the alignment here. |
| 409 | if (!isUInt<16>(AM.BaseOffs)) |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 410 | return false; |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 411 | |
| 412 | if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. |
| 413 | return true; |
| 414 | |
| 415 | if (AM.Scale == 1 && AM.HasBaseReg) |
| 416 | return true; |
| 417 | |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 418 | return false; |
| 419 | } |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 420 | case AMDGPUAS::FLAT_ADDRESS: |
Matt Arsenault | 7d1b6c8 | 2016-04-29 06:25:10 +0000 | [diff] [blame] | 421 | case AMDGPUAS::UNKNOWN_ADDRESS_SPACE: |
| 422 | // For an unknown address space, this usually means that this is for some |
| 423 | // reason being used for pure arithmetic, and not based on some addressing |
| 424 | // computation. We don't have instructions that compute pointers with any |
| 425 | // addressing modes, so treat them as having no offset like flat |
| 426 | // instructions. |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 427 | return isLegalFlatAddressingMode(AM); |
| 428 | |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 429 | default: |
| 430 | llvm_unreachable("unhandled address space"); |
| 431 | } |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 434 | bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 435 | unsigned AddrSpace, |
| 436 | unsigned Align, |
| 437 | bool *IsFast) const { |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 438 | if (IsFast) |
| 439 | *IsFast = false; |
| 440 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 441 | // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, |
| 442 | // which isn't a simple VT. |
Alina Sbirlea | 6f937b1 | 2016-08-04 16:38:44 +0000 | [diff] [blame] | 443 | // Until MVT is extended to handle this, simply check for the size and |
| 444 | // rely on the condition below: allow accesses if the size is a multiple of 4. |
| 445 | if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 && |
| 446 | VT.getStoreSize() > 16)) { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 447 | return false; |
Alina Sbirlea | 6f937b1 | 2016-08-04 16:38:44 +0000 | [diff] [blame] | 448 | } |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 449 | |
Matt Arsenault | 7f681ac | 2016-07-01 23:03:44 +0000 | [diff] [blame] | 450 | if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS || |
| 451 | AddrSpace == AMDGPUAS::REGION_ADDRESS) { |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 452 | // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte |
| 453 | // aligned, 8 byte access in a single operation using ds_read2/write2_b32 |
| 454 | // with adjacent offsets. |
Sanjay Patel | ce74db9 | 2015-09-03 15:03:19 +0000 | [diff] [blame] | 455 | bool AlignedBy4 = (Align % 4 == 0); |
| 456 | if (IsFast) |
| 457 | *IsFast = AlignedBy4; |
Matt Arsenault | 7f681ac | 2016-07-01 23:03:44 +0000 | [diff] [blame] | 458 | |
Sanjay Patel | ce74db9 | 2015-09-03 15:03:19 +0000 | [diff] [blame] | 459 | return AlignedBy4; |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 460 | } |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 461 | |
Matt Arsenault | 7f681ac | 2016-07-01 23:03:44 +0000 | [diff] [blame] | 462 | if (Subtarget->hasUnalignedBufferAccess()) { |
| 463 | // If we have an uniform constant load, it still requires using a slow |
| 464 | // buffer instruction if unaligned. |
| 465 | if (IsFast) { |
| 466 | *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ? |
| 467 | (Align % 4 == 0) : true; |
| 468 | } |
| 469 | |
| 470 | return true; |
| 471 | } |
| 472 | |
Tom Stellard | 33e64c6 | 2015-02-04 20:49:52 +0000 | [diff] [blame] | 473 | // Smaller than dword value must be aligned. |
Tom Stellard | 33e64c6 | 2015-02-04 20:49:52 +0000 | [diff] [blame] | 474 | if (VT.bitsLT(MVT::i32)) |
| 475 | return false; |
| 476 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 477 | // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the |
| 478 | // byte-address are ignored, thus forcing Dword alignment. |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 479 | // This applies to private, global, and constant memory. |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 480 | if (IsFast) |
| 481 | *IsFast = true; |
Tom Stellard | c6b299c | 2015-02-02 18:02:28 +0000 | [diff] [blame] | 482 | |
| 483 | return VT.bitsGT(MVT::i32) && Align % 4 == 0; |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Matt Arsenault | 46645fa | 2014-07-28 17:49:26 +0000 | [diff] [blame] | 486 | EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, |
| 487 | unsigned SrcAlign, bool IsMemset, |
| 488 | bool ZeroMemset, |
| 489 | bool MemcpyStrSrc, |
| 490 | MachineFunction &MF) const { |
| 491 | // FIXME: Should account for address space here. |
| 492 | |
| 493 | // The default fallback uses the private pointer size as a guess for a type to |
| 494 | // use. Make sure we switch these to 64-bit accesses. |
| 495 | |
| 496 | if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global |
| 497 | return MVT::v4i32; |
| 498 | |
| 499 | if (Size >= 8 && DstAlign >= 4) |
| 500 | return MVT::v2i32; |
| 501 | |
| 502 | // Use the default. |
| 503 | return MVT::Other; |
| 504 | } |
| 505 | |
Matt Arsenault | f9bfeaf | 2015-12-01 23:04:00 +0000 | [diff] [blame] | 506 | static bool isFlatGlobalAddrSpace(unsigned AS) { |
| 507 | return AS == AMDGPUAS::GLOBAL_ADDRESS || |
| 508 | AS == AMDGPUAS::FLAT_ADDRESS || |
| 509 | AS == AMDGPUAS::CONSTANT_ADDRESS; |
| 510 | } |
| 511 | |
| 512 | bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, |
| 513 | unsigned DestAS) const { |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 514 | return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS); |
Matt Arsenault | f9bfeaf | 2015-12-01 23:04:00 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 517 | bool SITargetLowering::isMemOpUniform(const SDNode *N) const { |
| 518 | const MemSDNode *MemNode = cast<MemSDNode>(N); |
| 519 | const Value *Ptr = MemNode->getMemOperand()->getValue(); |
| 520 | |
| 521 | // UndefValue means this is a load of a kernel input. These are uniform. |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 522 | // Sometimes LDS instructions have constant pointers. |
| 523 | // If Ptr is null, then that means this mem operand contains a |
| 524 | // PseudoSourceValue like GOT. |
| 525 | if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || |
| 526 | isa<Constant>(Ptr) || isa<GlobalValue>(Ptr)) |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 527 | return true; |
| 528 | |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 529 | const Instruction *I = dyn_cast<Instruction>(Ptr); |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 530 | return I && I->getMetadata("amdgpu.uniform"); |
| 531 | } |
| 532 | |
Chandler Carruth | 9d010ff | 2014-07-03 00:23:43 +0000 | [diff] [blame] | 533 | TargetLoweringBase::LegalizeTypeAction |
| 534 | SITargetLowering::getPreferredVectorAction(EVT VT) const { |
| 535 | if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) |
| 536 | return TypeSplitVector; |
| 537 | |
| 538 | return TargetLoweringBase::getPreferredVectorAction(VT); |
Tom Stellard | d86003e | 2013-08-14 23:25:00 +0000 | [diff] [blame] | 539 | } |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 540 | |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 541 | bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, |
| 542 | Type *Ty) const { |
Matt Arsenault | 749035b | 2016-07-30 01:40:36 +0000 | [diff] [blame] | 543 | // FIXME: Could be smarter if called for vector constants. |
| 544 | return true; |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Tom Stellard | 2e045bb | 2016-01-20 00:13:22 +0000 | [diff] [blame] | 547 | bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { |
| 548 | |
Konstantin Zhuravlyov | e14df4b | 2016-09-28 20:05:39 +0000 | [diff] [blame] | 549 | // i16 is not desirable unless it is a load or a store. |
| 550 | if (VT == MVT::i16 && Op != ISD::LOAD && Op != ISD::STORE) |
| 551 | return false; |
| 552 | |
Tom Stellard | 2e045bb | 2016-01-20 00:13:22 +0000 | [diff] [blame] | 553 | // SimplifySetCC uses this function to determine whether or not it should |
| 554 | // create setcc with i1 operands. We don't have instructions for i1 setcc. |
| 555 | if (VT == MVT::i1 && Op == ISD::SETCC) |
| 556 | return false; |
| 557 | |
| 558 | return TargetLowering::isTypeDesirableForOp(Op, VT); |
| 559 | } |
| 560 | |
Jan Vesely | fea814d | 2016-06-21 20:46:20 +0000 | [diff] [blame] | 561 | SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG, |
| 562 | const SDLoc &SL, SDValue Chain, |
| 563 | unsigned Offset) const { |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 564 | const DataLayout &DL = DAG.getDataLayout(); |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 565 | MachineFunction &MF = DAG.getMachineFunction(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 566 | const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 567 | unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 568 | |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 569 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 570 | MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); |
Matt Arsenault | a0269b6 | 2015-06-01 21:58:24 +0000 | [diff] [blame] | 571 | SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, |
| 572 | MRI.getLiveInVirtReg(InputPtrReg), PtrVT); |
Jan Vesely | fea814d | 2016-06-21 20:46:20 +0000 | [diff] [blame] | 573 | return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, |
| 574 | DAG.getConstant(Offset, SL, PtrVT)); |
| 575 | } |
| 576 | SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, |
| 577 | const SDLoc &SL, SDValue Chain, |
| 578 | unsigned Offset, bool Signed) const { |
| 579 | const DataLayout &DL = DAG.getDataLayout(); |
| 580 | Type *Ty = VT.getTypeForEVT(*DAG.getContext()); |
| 581 | MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); |
| 582 | PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 583 | SDValue PtrOffset = DAG.getUNDEF(PtrVT); |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 584 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 585 | |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 586 | unsigned Align = DL.getABITypeAlignment(Ty); |
Matt Arsenault | 81c7ae2 | 2015-06-04 16:00:27 +0000 | [diff] [blame] | 587 | |
Matt Arsenault | 81c7ae2 | 2015-06-04 16:00:27 +0000 | [diff] [blame] | 588 | ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD; |
Matt Arsenault | acd68b5 | 2015-09-09 01:12:27 +0000 | [diff] [blame] | 589 | if (MemVT.isFloatingPoint()) |
| 590 | ExtTy = ISD::EXTLOAD; |
| 591 | |
Jan Vesely | fea814d | 2016-06-21 20:46:20 +0000 | [diff] [blame] | 592 | SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset); |
Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 593 | return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset, |
Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 594 | PtrInfo, MemVT, Align, |
| 595 | MachineMemOperand::MONonTemporal | |
| 596 | MachineMemOperand::MODereferenceable | |
| 597 | MachineMemOperand::MOInvariant); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 600 | SDValue SITargetLowering::LowerFormalArguments( |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 601 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 602 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, |
| 603 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 604 | const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 605 | |
| 606 | MachineFunction &MF = DAG.getMachineFunction(); |
| 607 | FunctionType *FType = MF.getFunction()->getFunctionType(); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 608 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 609 | const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 610 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 611 | if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) { |
Matt Arsenault | d48da14 | 2015-11-02 23:23:02 +0000 | [diff] [blame] | 612 | const Function *Fn = MF.getFunction(); |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 613 | DiagnosticInfoUnsupported NoGraphicsHSA( |
| 614 | *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()); |
Matt Arsenault | d48da14 | 2015-11-02 23:23:02 +0000 | [diff] [blame] | 615 | DAG.getContext()->diagnose(NoGraphicsHSA); |
Diana Picus | 81bc317 | 2016-05-26 15:24:55 +0000 | [diff] [blame] | 616 | return DAG.getEntryNode(); |
Matt Arsenault | d48da14 | 2015-11-02 23:23:02 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 619 | // Create stack objects that are used for emitting debugger prologue if |
| 620 | // "amdgpu-debugger-emit-prologue" attribute was specified. |
| 621 | if (ST.debuggerEmitPrologue()) |
| 622 | createDebuggerPrologueStackObjects(MF); |
| 623 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 624 | SmallVector<ISD::InputArg, 16> Splits; |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 625 | BitVector Skipped(Ins.size()); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 626 | |
| 627 | for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 628 | const ISD::InputArg &Arg = Ins[i]; |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 629 | |
| 630 | // First check if it's a PS input addr |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 631 | if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() && |
Marek Olsak | b6c8c3d | 2016-01-13 11:46:10 +0000 | [diff] [blame] | 632 | !Arg.Flags.isByVal() && PSInputNum <= 15) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 633 | |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 634 | if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 635 | // We can safely skip PS inputs |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 636 | Skipped.set(i); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 637 | ++PSInputNum; |
| 638 | continue; |
| 639 | } |
| 640 | |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 641 | Info->markPSInputAllocated(PSInputNum); |
| 642 | if (Arg.Used) |
| 643 | Info->PSInputEna |= 1 << PSInputNum; |
| 644 | |
| 645 | ++PSInputNum; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Matt Arsenault | 539ca88 | 2016-05-05 20:27:02 +0000 | [diff] [blame] | 648 | if (AMDGPU::isShader(CallConv)) { |
| 649 | // Second split vertices into their elements |
| 650 | if (Arg.VT.isVector()) { |
| 651 | ISD::InputArg NewArg = Arg; |
| 652 | NewArg.Flags.setSplit(); |
| 653 | NewArg.VT = Arg.VT.getVectorElementType(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 654 | |
Matt Arsenault | 539ca88 | 2016-05-05 20:27:02 +0000 | [diff] [blame] | 655 | // We REALLY want the ORIGINAL number of vertex elements here, e.g. a |
| 656 | // three or five element vertex only needs three or five registers, |
| 657 | // NOT four or eight. |
| 658 | Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); |
| 659 | unsigned NumElements = ParamType->getVectorNumElements(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 660 | |
Matt Arsenault | 539ca88 | 2016-05-05 20:27:02 +0000 | [diff] [blame] | 661 | for (unsigned j = 0; j != NumElements; ++j) { |
| 662 | Splits.push_back(NewArg); |
| 663 | NewArg.PartOffset += NewArg.VT.getStoreSize(); |
| 664 | } |
| 665 | } else { |
| 666 | Splits.push_back(Arg); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 667 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
| 671 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 672 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, |
| 673 | *DAG.getContext()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 674 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 675 | // At least one interpolation mode must be enabled or else the GPU will hang. |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 676 | // |
| 677 | // Check PSInputAddr instead of PSInputEna. The idea is that if the user set |
| 678 | // PSInputAddr, the user wants to enable some bits after the compilation |
| 679 | // based on run-time states. Since we can't know what the final PSInputEna |
| 680 | // will look like, so we shouldn't do anything here and the user should take |
| 681 | // responsibility for the correct programming. |
Marek Olsak | 46dadbf | 2016-01-13 17:23:20 +0000 | [diff] [blame] | 682 | // |
| 683 | // Otherwise, the following restrictions apply: |
| 684 | // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. |
| 685 | // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be |
| 686 | // enabled too. |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 687 | if (CallConv == CallingConv::AMDGPU_PS && |
Marek Olsak | 46dadbf | 2016-01-13 17:23:20 +0000 | [diff] [blame] | 688 | ((Info->getPSInputAddr() & 0x7F) == 0 || |
NAKAMURA Takumi | fe1202c | 2016-06-20 00:37:41 +0000 | [diff] [blame] | 689 | ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 690 | CCInfo.AllocateReg(AMDGPU::VGPR0); |
| 691 | CCInfo.AllocateReg(AMDGPU::VGPR1); |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 692 | Info->markPSInputAllocated(0); |
| 693 | Info->PSInputEna |= 1; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 696 | if (!AMDGPU::isShader(CallConv)) { |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 697 | assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX()); |
| 698 | } else { |
| 699 | assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() && |
| 700 | !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() && |
| 701 | !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() && |
| 702 | !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() && |
| 703 | !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() && |
| 704 | !Info->hasWorkItemIDZ()); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 707 | // FIXME: How should these inputs interact with inreg / custom SGPR inputs? |
| 708 | if (Info->hasPrivateSegmentBuffer()) { |
| 709 | unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI); |
| 710 | MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass); |
| 711 | CCInfo.AllocateReg(PrivateSegmentBufferReg); |
| 712 | } |
| 713 | |
| 714 | if (Info->hasDispatchPtr()) { |
| 715 | unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI); |
| 716 | MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass); |
| 717 | CCInfo.AllocateReg(DispatchPtrReg); |
| 718 | } |
| 719 | |
Matt Arsenault | 48ab526 | 2016-04-25 19:27:18 +0000 | [diff] [blame] | 720 | if (Info->hasQueuePtr()) { |
| 721 | unsigned QueuePtrReg = Info->addQueuePtr(*TRI); |
| 722 | MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass); |
| 723 | CCInfo.AllocateReg(QueuePtrReg); |
| 724 | } |
| 725 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 726 | if (Info->hasKernargSegmentPtr()) { |
| 727 | unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI); |
| 728 | MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass); |
| 729 | CCInfo.AllocateReg(InputPtrReg); |
| 730 | } |
| 731 | |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 732 | if (Info->hasDispatchID()) { |
| 733 | unsigned DispatchIDReg = Info->addDispatchID(*TRI); |
| 734 | MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass); |
| 735 | CCInfo.AllocateReg(DispatchIDReg); |
| 736 | } |
| 737 | |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 738 | if (Info->hasFlatScratchInit()) { |
| 739 | unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI); |
| 740 | MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass); |
| 741 | CCInfo.AllocateReg(FlatScratchInitReg); |
| 742 | } |
| 743 | |
Tom Stellard | bbeb45a | 2016-09-16 21:53:00 +0000 | [diff] [blame] | 744 | if (!AMDGPU::isShader(CallConv)) |
| 745 | analyzeFormalArgumentsCompute(CCInfo, Ins); |
| 746 | else |
| 747 | AnalyzeFormalArguments(CCInfo, Splits); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 748 | |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 749 | SmallVector<SDValue, 16> Chains; |
| 750 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 751 | for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { |
| 752 | |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 753 | const ISD::InputArg &Arg = Ins[i]; |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 754 | if (Skipped[i]) { |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 755 | InVals.push_back(DAG.getUNDEF(Arg.VT)); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 756 | continue; |
| 757 | } |
| 758 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 759 | CCValAssign &VA = ArgLocs[ArgIdx++]; |
Craig Topper | 7f416c8 | 2014-11-16 21:17:18 +0000 | [diff] [blame] | 760 | MVT VT = VA.getLocVT(); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 761 | |
| 762 | if (VA.isMemLoc()) { |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 763 | VT = Ins[i].VT; |
Tom Stellard | bbeb45a | 2016-09-16 21:53:00 +0000 | [diff] [blame] | 764 | EVT MemVT = VA.getLocVT(); |
Tom Stellard | b5798b0 | 2015-06-26 21:15:03 +0000 | [diff] [blame] | 765 | const unsigned Offset = Subtarget->getExplicitKernelArgOffset() + |
| 766 | VA.getLocMemOffset(); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 767 | // The first 36 bytes of the input buffer contains information about |
| 768 | // thread group and global sizes. |
Matt Arsenault | 0d51973 | 2015-07-10 22:28:41 +0000 | [diff] [blame] | 769 | SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain, |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 770 | Offset, Ins[i].Flags.isSExt()); |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 771 | Chains.push_back(Arg.getValue(1)); |
Tom Stellard | ca7ecf3 | 2014-08-22 18:49:31 +0000 | [diff] [blame] | 772 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 773 | auto *ParamTy = |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 774 | dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 775 | if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && |
Tom Stellard | ca7ecf3 | 2014-08-22 18:49:31 +0000 | [diff] [blame] | 776 | ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { |
| 777 | // On SI local pointers are just offsets into LDS, so they are always |
| 778 | // less than 16-bits. On CI and newer they could potentially be |
| 779 | // real pointers, so we can't guarantee their size. |
| 780 | Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, |
| 781 | DAG.getValueType(MVT::i16)); |
| 782 | } |
| 783 | |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 784 | InVals.push_back(Arg); |
Matt Arsenault | 52ef401 | 2016-07-26 16:45:58 +0000 | [diff] [blame] | 785 | Info->setABIArgOffset(Offset + MemVT.getStoreSize()); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 786 | continue; |
| 787 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 788 | assert(VA.isRegLoc() && "Parameter must be in a register!"); |
| 789 | |
| 790 | unsigned Reg = VA.getLocReg(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 791 | |
| 792 | if (VT == MVT::i64) { |
| 793 | // For now assume it is a pointer |
| 794 | Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, |
| 795 | &AMDGPU::SReg_64RegClass); |
| 796 | Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 797 | SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 798 | InVals.push_back(Copy); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 799 | continue; |
| 800 | } |
| 801 | |
| 802 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); |
| 803 | |
| 804 | Reg = MF.addLiveIn(Reg, RC); |
| 805 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 806 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 807 | if (Arg.VT.isVector()) { |
| 808 | |
| 809 | // Build a vector from the registers |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 810 | Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 811 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 812 | |
| 813 | SmallVector<SDValue, 4> Regs; |
| 814 | Regs.push_back(Val); |
| 815 | for (unsigned j = 1; j != NumElements; ++j) { |
| 816 | Reg = ArgLocs[ArgIdx++].getLocReg(); |
| 817 | Reg = MF.addLiveIn(Reg, RC); |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 818 | |
| 819 | SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 820 | Regs.push_back(Copy); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // Fill up the missing vector elements |
| 824 | NumElements = Arg.VT.getVectorNumElements() - NumElements; |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 825 | Regs.append(NumElements, DAG.getUNDEF(VT)); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 826 | |
Ahmed Bougacha | 128f873 | 2016-04-26 21:15:30 +0000 | [diff] [blame] | 827 | InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs)); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 828 | continue; |
| 829 | } |
| 830 | |
| 831 | InVals.push_back(Val); |
| 832 | } |
Tom Stellard | e99fb65 | 2015-01-20 19:33:04 +0000 | [diff] [blame] | 833 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 834 | // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read |
| 835 | // these from the dispatch pointer. |
| 836 | |
| 837 | // Start adding system SGPRs. |
| 838 | if (Info->hasWorkGroupIDX()) { |
| 839 | unsigned Reg = Info->addWorkGroupIDX(); |
| 840 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 841 | CCInfo.AllocateReg(Reg); |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 842 | } |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 843 | |
| 844 | if (Info->hasWorkGroupIDY()) { |
| 845 | unsigned Reg = Info->addWorkGroupIDY(); |
| 846 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 847 | CCInfo.AllocateReg(Reg); |
Tom Stellard | e99fb65 | 2015-01-20 19:33:04 +0000 | [diff] [blame] | 848 | } |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 849 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 850 | if (Info->hasWorkGroupIDZ()) { |
| 851 | unsigned Reg = Info->addWorkGroupIDZ(); |
| 852 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 853 | CCInfo.AllocateReg(Reg); |
| 854 | } |
| 855 | |
| 856 | if (Info->hasWorkGroupInfo()) { |
| 857 | unsigned Reg = Info->addWorkGroupInfo(); |
| 858 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 859 | CCInfo.AllocateReg(Reg); |
| 860 | } |
| 861 | |
| 862 | if (Info->hasPrivateSegmentWaveByteOffset()) { |
| 863 | // Scratch wave offset passed in system SGPR. |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 864 | unsigned PrivateSegmentWaveByteOffsetReg; |
| 865 | |
| 866 | if (AMDGPU::isShader(CallConv)) { |
| 867 | PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo); |
| 868 | Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg); |
| 869 | } else |
| 870 | PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset(); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 871 | |
| 872 | MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); |
| 873 | CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); |
| 874 | } |
| 875 | |
| 876 | // Now that we've figured out where the scratch register inputs are, see if |
| 877 | // should reserve the arguments and use them directly. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 878 | bool HasStackObjects = MF.getFrameInfo().hasStackObjects(); |
Matt Arsenault | 296b849 | 2016-02-12 06:31:30 +0000 | [diff] [blame] | 879 | // Record that we know we have non-spill stack objects so we don't need to |
| 880 | // check all stack objects later. |
| 881 | if (HasStackObjects) |
| 882 | Info->setHasNonSpillStackObjects(true); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 883 | |
Matt Arsenault | 253640e | 2016-10-13 13:10:00 +0000 | [diff] [blame] | 884 | // Everything live out of a block is spilled with fast regalloc, so it's |
| 885 | // almost certain that spilling will be required. |
| 886 | if (getTargetMachine().getOptLevel() == CodeGenOpt::None) |
| 887 | HasStackObjects = true; |
| 888 | |
Tom Stellard | 0b76fc4c | 2016-09-16 21:34:26 +0000 | [diff] [blame] | 889 | if (ST.isAmdCodeObjectV2()) { |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 890 | if (HasStackObjects) { |
| 891 | // If we have stack objects, we unquestionably need the private buffer |
Tom Stellard | 0b76fc4c | 2016-09-16 21:34:26 +0000 | [diff] [blame] | 892 | // resource. For the Code Object V2 ABI, this will be the first 4 user |
| 893 | // SGPR inputs. We can reserve those and use them directly. |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 894 | |
| 895 | unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue( |
| 896 | MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); |
| 897 | Info->setScratchRSrcReg(PrivateSegmentBufferReg); |
| 898 | |
| 899 | unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue( |
| 900 | MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
| 901 | Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); |
| 902 | } else { |
| 903 | unsigned ReservedBufferReg |
| 904 | = TRI->reservedPrivateSegmentBufferReg(MF); |
| 905 | unsigned ReservedOffsetReg |
| 906 | = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
| 907 | |
| 908 | // We tentatively reserve the last registers (skipping the last two |
| 909 | // which may contain VCC). After register allocation, we'll replace |
| 910 | // these with the ones immediately after those which were really |
| 911 | // allocated. In the prologue copies will be inserted from the argument |
| 912 | // to these reserved registers. |
| 913 | Info->setScratchRSrcReg(ReservedBufferReg); |
| 914 | Info->setScratchWaveOffsetReg(ReservedOffsetReg); |
| 915 | } |
| 916 | } else { |
| 917 | unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF); |
| 918 | |
| 919 | // Without HSA, relocations are used for the scratch pointer and the |
| 920 | // buffer resource setup is always inserted in the prologue. Scratch wave |
| 921 | // offset is still in an input SGPR. |
| 922 | Info->setScratchRSrcReg(ReservedBufferReg); |
| 923 | |
| 924 | if (HasStackObjects) { |
| 925 | unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue( |
| 926 | MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
| 927 | Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg); |
| 928 | } else { |
| 929 | unsigned ReservedOffsetReg |
| 930 | = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
| 931 | Info->setScratchWaveOffsetReg(ReservedOffsetReg); |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | if (Info->hasWorkItemIDX()) { |
| 936 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X); |
| 937 | MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); |
| 938 | CCInfo.AllocateReg(Reg); |
Tom Stellard | f110f8f | 2016-04-14 16:27:03 +0000 | [diff] [blame] | 939 | } |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 940 | |
| 941 | if (Info->hasWorkItemIDY()) { |
| 942 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y); |
| 943 | MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); |
| 944 | CCInfo.AllocateReg(Reg); |
| 945 | } |
| 946 | |
| 947 | if (Info->hasWorkItemIDZ()) { |
| 948 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z); |
| 949 | MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); |
| 950 | CCInfo.AllocateReg(Reg); |
| 951 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 952 | |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 953 | if (Chains.empty()) |
| 954 | return Chain; |
| 955 | |
| 956 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 959 | SDValue |
| 960 | SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, |
| 961 | bool isVarArg, |
| 962 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 963 | const SmallVectorImpl<SDValue> &OutVals, |
| 964 | const SDLoc &DL, SelectionDAG &DAG) const { |
Marek Olsak | 8a0f335 | 2016-01-13 17:23:04 +0000 | [diff] [blame] | 965 | MachineFunction &MF = DAG.getMachineFunction(); |
| 966 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
| 967 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 968 | if (!AMDGPU::isShader(CallConv)) |
Marek Olsak | 8a0f335 | 2016-01-13 17:23:04 +0000 | [diff] [blame] | 969 | return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, |
| 970 | OutVals, DL, DAG); |
| 971 | |
Marek Olsak | 8e9cc63 | 2016-01-13 17:23:09 +0000 | [diff] [blame] | 972 | Info->setIfReturnsVoid(Outs.size() == 0); |
| 973 | |
Marek Olsak | 8a0f335 | 2016-01-13 17:23:04 +0000 | [diff] [blame] | 974 | SmallVector<ISD::OutputArg, 48> Splits; |
| 975 | SmallVector<SDValue, 48> SplitVals; |
| 976 | |
| 977 | // Split vectors into their elements. |
| 978 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
| 979 | const ISD::OutputArg &Out = Outs[i]; |
| 980 | |
| 981 | if (Out.VT.isVector()) { |
| 982 | MVT VT = Out.VT.getVectorElementType(); |
| 983 | ISD::OutputArg NewOut = Out; |
| 984 | NewOut.Flags.setSplit(); |
| 985 | NewOut.VT = VT; |
| 986 | |
| 987 | // We want the original number of vector elements here, e.g. |
| 988 | // three or five, not four or eight. |
| 989 | unsigned NumElements = Out.ArgVT.getVectorNumElements(); |
| 990 | |
| 991 | for (unsigned j = 0; j != NumElements; ++j) { |
| 992 | SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i], |
| 993 | DAG.getConstant(j, DL, MVT::i32)); |
| 994 | SplitVals.push_back(Elem); |
| 995 | Splits.push_back(NewOut); |
| 996 | NewOut.PartOffset += NewOut.VT.getStoreSize(); |
| 997 | } |
| 998 | } else { |
| 999 | SplitVals.push_back(OutVals[i]); |
| 1000 | Splits.push_back(Out); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | // CCValAssign - represent the assignment of the return value to a location. |
| 1005 | SmallVector<CCValAssign, 48> RVLocs; |
| 1006 | |
| 1007 | // CCState - Info about the registers and stack slots. |
| 1008 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, |
| 1009 | *DAG.getContext()); |
| 1010 | |
| 1011 | // Analyze outgoing return values. |
| 1012 | AnalyzeReturn(CCInfo, Splits); |
| 1013 | |
| 1014 | SDValue Flag; |
| 1015 | SmallVector<SDValue, 48> RetOps; |
| 1016 | RetOps.push_back(Chain); // Operand #0 = Chain (updated below) |
| 1017 | |
| 1018 | // Copy the result values into the output registers. |
| 1019 | for (unsigned i = 0, realRVLocIdx = 0; |
| 1020 | i != RVLocs.size(); |
| 1021 | ++i, ++realRVLocIdx) { |
| 1022 | CCValAssign &VA = RVLocs[i]; |
| 1023 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 1024 | |
| 1025 | SDValue Arg = SplitVals[realRVLocIdx]; |
| 1026 | |
| 1027 | // Copied from other backends. |
| 1028 | switch (VA.getLocInfo()) { |
| 1029 | default: llvm_unreachable("Unknown loc info!"); |
| 1030 | case CCValAssign::Full: |
| 1031 | break; |
| 1032 | case CCValAssign::BCvt: |
| 1033 | Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); |
| 1038 | Flag = Chain.getValue(1); |
| 1039 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
| 1040 | } |
| 1041 | |
| 1042 | // Update chain and glue. |
| 1043 | RetOps[0] = Chain; |
| 1044 | if (Flag.getNode()) |
| 1045 | RetOps.push_back(Flag); |
| 1046 | |
Matt Arsenault | 9babdf4 | 2016-06-22 20:15:28 +0000 | [diff] [blame] | 1047 | unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN; |
| 1048 | return DAG.getNode(Opc, DL, MVT::Other, RetOps); |
Marek Olsak | 8a0f335 | 2016-01-13 17:23:04 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Matt Arsenault | 9a10cea | 2016-01-26 04:29:24 +0000 | [diff] [blame] | 1051 | unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT, |
| 1052 | SelectionDAG &DAG) const { |
| 1053 | unsigned Reg = StringSwitch<unsigned>(RegName) |
| 1054 | .Case("m0", AMDGPU::M0) |
| 1055 | .Case("exec", AMDGPU::EXEC) |
| 1056 | .Case("exec_lo", AMDGPU::EXEC_LO) |
| 1057 | .Case("exec_hi", AMDGPU::EXEC_HI) |
| 1058 | .Case("flat_scratch", AMDGPU::FLAT_SCR) |
| 1059 | .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) |
| 1060 | .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) |
| 1061 | .Default(AMDGPU::NoRegister); |
| 1062 | |
| 1063 | if (Reg == AMDGPU::NoRegister) { |
| 1064 | report_fatal_error(Twine("invalid register name \"" |
| 1065 | + StringRef(RegName) + "\".")); |
| 1066 | |
| 1067 | } |
| 1068 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 1069 | if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && |
Matt Arsenault | 9a10cea | 2016-01-26 04:29:24 +0000 | [diff] [blame] | 1070 | Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { |
| 1071 | report_fatal_error(Twine("invalid register \"" |
| 1072 | + StringRef(RegName) + "\" for subtarget.")); |
| 1073 | } |
| 1074 | |
| 1075 | switch (Reg) { |
| 1076 | case AMDGPU::M0: |
| 1077 | case AMDGPU::EXEC_LO: |
| 1078 | case AMDGPU::EXEC_HI: |
| 1079 | case AMDGPU::FLAT_SCR_LO: |
| 1080 | case AMDGPU::FLAT_SCR_HI: |
| 1081 | if (VT.getSizeInBits() == 32) |
| 1082 | return Reg; |
| 1083 | break; |
| 1084 | case AMDGPU::EXEC: |
| 1085 | case AMDGPU::FLAT_SCR: |
| 1086 | if (VT.getSizeInBits() == 64) |
| 1087 | return Reg; |
| 1088 | break; |
| 1089 | default: |
| 1090 | llvm_unreachable("missing register type checking"); |
| 1091 | } |
| 1092 | |
| 1093 | report_fatal_error(Twine("invalid type for register \"" |
| 1094 | + StringRef(RegName) + "\".")); |
| 1095 | } |
| 1096 | |
Matt Arsenault | 786724a | 2016-07-12 21:41:32 +0000 | [diff] [blame] | 1097 | // If kill is not the last instruction, split the block so kill is always a |
| 1098 | // proper terminator. |
| 1099 | MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, |
| 1100 | MachineBasicBlock *BB) const { |
| 1101 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
| 1102 | |
| 1103 | MachineBasicBlock::iterator SplitPoint(&MI); |
| 1104 | ++SplitPoint; |
| 1105 | |
| 1106 | if (SplitPoint == BB->end()) { |
| 1107 | // Don't bother with a new block. |
| 1108 | MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); |
| 1109 | return BB; |
| 1110 | } |
| 1111 | |
| 1112 | MachineFunction *MF = BB->getParent(); |
| 1113 | MachineBasicBlock *SplitBB |
| 1114 | = MF->CreateMachineBasicBlock(BB->getBasicBlock()); |
| 1115 | |
Matt Arsenault | 786724a | 2016-07-12 21:41:32 +0000 | [diff] [blame] | 1116 | MF->insert(++MachineFunction::iterator(BB), SplitBB); |
| 1117 | SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); |
| 1118 | |
Matt Arsenault | d40ded6 | 2016-07-22 17:01:15 +0000 | [diff] [blame] | 1119 | SplitBB->transferSuccessorsAndUpdatePHIs(BB); |
Matt Arsenault | 786724a | 2016-07-12 21:41:32 +0000 | [diff] [blame] | 1120 | BB->addSuccessor(SplitBB); |
| 1121 | |
| 1122 | MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); |
| 1123 | return SplitBB; |
| 1124 | } |
| 1125 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1126 | // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the |
| 1127 | // wavefront. If the value is uniform and just happens to be in a VGPR, this |
| 1128 | // will only do one iteration. In the worst case, this will loop 64 times. |
| 1129 | // |
| 1130 | // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value. |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1131 | static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop( |
| 1132 | const SIInstrInfo *TII, |
| 1133 | MachineRegisterInfo &MRI, |
| 1134 | MachineBasicBlock &OrigBB, |
| 1135 | MachineBasicBlock &LoopBB, |
| 1136 | const DebugLoc &DL, |
| 1137 | const MachineOperand &IdxReg, |
| 1138 | unsigned InitReg, |
| 1139 | unsigned ResultReg, |
| 1140 | unsigned PhiReg, |
| 1141 | unsigned InitSaveExecReg, |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1142 | int Offset, |
| 1143 | bool UseGPRIdxMode) { |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1144 | MachineBasicBlock::iterator I = LoopBB.begin(); |
| 1145 | |
| 1146 | unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 1147 | unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 1148 | unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1149 | unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 1150 | |
| 1151 | BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg) |
| 1152 | .addReg(InitReg) |
| 1153 | .addMBB(&OrigBB) |
| 1154 | .addReg(ResultReg) |
| 1155 | .addMBB(&LoopBB); |
| 1156 | |
| 1157 | BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec) |
| 1158 | .addReg(InitSaveExecReg) |
| 1159 | .addMBB(&OrigBB) |
| 1160 | .addReg(NewExec) |
| 1161 | .addMBB(&LoopBB); |
| 1162 | |
| 1163 | // Read the next variant <- also loop target. |
| 1164 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg) |
| 1165 | .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef())); |
| 1166 | |
| 1167 | // Compare the just read M0 value to all possible Idx values. |
| 1168 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg) |
| 1169 | .addReg(CurrentIdxReg) |
Matt Arsenault | f0ba86a | 2016-07-21 09:40:57 +0000 | [diff] [blame] | 1170 | .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg()); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1171 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1172 | if (UseGPRIdxMode) { |
| 1173 | unsigned IdxReg; |
| 1174 | if (Offset == 0) { |
| 1175 | IdxReg = CurrentIdxReg; |
| 1176 | } else { |
| 1177 | IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 1178 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg) |
| 1179 | .addReg(CurrentIdxReg, RegState::Kill) |
| 1180 | .addImm(Offset); |
| 1181 | } |
| 1182 | |
| 1183 | MachineInstr *SetIdx = |
| 1184 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX)) |
| 1185 | .addReg(IdxReg, RegState::Kill); |
Matt Arsenault | dac31db | 2016-10-13 12:45:16 +0000 | [diff] [blame] | 1186 | SetIdx->getOperand(2).setIsUndef(); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1187 | } else { |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1188 | // Move index from VCC into M0 |
| 1189 | if (Offset == 0) { |
| 1190 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) |
| 1191 | .addReg(CurrentIdxReg, RegState::Kill); |
| 1192 | } else { |
| 1193 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) |
| 1194 | .addReg(CurrentIdxReg, RegState::Kill) |
| 1195 | .addImm(Offset); |
| 1196 | } |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | // Update EXEC, save the original EXEC value to VCC. |
| 1200 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec) |
| 1201 | .addReg(CondReg, RegState::Kill); |
| 1202 | |
| 1203 | MRI.setSimpleHint(NewExec, CondReg); |
| 1204 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1205 | // Update EXEC, switch all done bits to 0 and all todo bits to 1. |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1206 | MachineInstr *InsertPt = |
| 1207 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC) |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1208 | .addReg(AMDGPU::EXEC) |
| 1209 | .addReg(NewExec); |
| 1210 | |
| 1211 | // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use |
| 1212 | // s_cbranch_scc0? |
| 1213 | |
| 1214 | // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover. |
| 1215 | BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) |
| 1216 | .addMBB(&LoopBB); |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1217 | |
| 1218 | return InsertPt->getIterator(); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | // This has slightly sub-optimal regalloc when the source vector is killed by |
| 1222 | // the read. The register allocator does not understand that the kill is |
| 1223 | // per-workitem, so is kept alive for the whole loop so we end up not re-using a |
| 1224 | // subregister from it, using 1 more VGPR than necessary. This was saved when |
| 1225 | // this was expanded after register allocation. |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1226 | static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, |
| 1227 | MachineBasicBlock &MBB, |
| 1228 | MachineInstr &MI, |
| 1229 | unsigned InitResultReg, |
| 1230 | unsigned PhiReg, |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1231 | int Offset, |
| 1232 | bool UseGPRIdxMode) { |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1233 | MachineFunction *MF = MBB.getParent(); |
| 1234 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 1235 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1236 | MachineBasicBlock::iterator I(&MI); |
| 1237 | |
| 1238 | unsigned DstReg = MI.getOperand(0).getReg(); |
| 1239 | unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 1240 | unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 1241 | |
| 1242 | BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec); |
| 1243 | |
| 1244 | // Save the EXEC mask |
| 1245 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec) |
| 1246 | .addReg(AMDGPU::EXEC); |
| 1247 | |
| 1248 | // To insert the loop we need to split the block. Move everything after this |
| 1249 | // point to a new block, and insert a new empty block between the two. |
| 1250 | MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock(); |
| 1251 | MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock(); |
| 1252 | MachineFunction::iterator MBBI(MBB); |
| 1253 | ++MBBI; |
| 1254 | |
| 1255 | MF->insert(MBBI, LoopBB); |
| 1256 | MF->insert(MBBI, RemainderBB); |
| 1257 | |
| 1258 | LoopBB->addSuccessor(LoopBB); |
| 1259 | LoopBB->addSuccessor(RemainderBB); |
| 1260 | |
| 1261 | // Move the rest of the block into a new block. |
Matt Arsenault | d40ded6 | 2016-07-22 17:01:15 +0000 | [diff] [blame] | 1262 | RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1263 | RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); |
| 1264 | |
| 1265 | MBB.addSuccessor(LoopBB); |
| 1266 | |
| 1267 | const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); |
| 1268 | |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1269 | auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx, |
| 1270 | InitResultReg, DstReg, PhiReg, TmpExec, |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1271 | Offset, UseGPRIdxMode); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1272 | |
| 1273 | MachineBasicBlock::iterator First = RemainderBB->begin(); |
| 1274 | BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC) |
| 1275 | .addReg(SaveExec); |
| 1276 | |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1277 | return InsPt; |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | // Returns subreg index, offset |
| 1281 | static std::pair<unsigned, int> |
| 1282 | computeIndirectRegAndOffset(const SIRegisterInfo &TRI, |
| 1283 | const TargetRegisterClass *SuperRC, |
| 1284 | unsigned VecReg, |
| 1285 | int Offset) { |
| 1286 | int NumElts = SuperRC->getSize() / 4; |
| 1287 | |
| 1288 | // Skip out of bounds offsets, or else we would end up using an undefined |
| 1289 | // register. |
| 1290 | if (Offset >= NumElts || Offset < 0) |
| 1291 | return std::make_pair(AMDGPU::sub0, Offset); |
| 1292 | |
| 1293 | return std::make_pair(AMDGPU::sub0 + Offset, 0); |
| 1294 | } |
| 1295 | |
| 1296 | // Return true if the index is an SGPR and was set. |
| 1297 | static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, |
| 1298 | MachineRegisterInfo &MRI, |
| 1299 | MachineInstr &MI, |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1300 | int Offset, |
| 1301 | bool UseGPRIdxMode, |
| 1302 | bool IsIndirectSrc) { |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1303 | MachineBasicBlock *MBB = MI.getParent(); |
| 1304 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1305 | MachineBasicBlock::iterator I(&MI); |
| 1306 | |
| 1307 | const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); |
| 1308 | const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg()); |
| 1309 | |
| 1310 | assert(Idx->getReg() != AMDGPU::NoRegister); |
| 1311 | |
| 1312 | if (!TII->getRegisterInfo().isSGPRClass(IdxRC)) |
| 1313 | return false; |
| 1314 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1315 | if (UseGPRIdxMode) { |
| 1316 | unsigned IdxMode = IsIndirectSrc ? |
| 1317 | VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE; |
| 1318 | if (Offset == 0) { |
| 1319 | MachineInstr *SetOn = |
| 1320 | BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) |
| 1321 | .addOperand(*Idx) |
| 1322 | .addImm(IdxMode); |
| 1323 | |
Matt Arsenault | dac31db | 2016-10-13 12:45:16 +0000 | [diff] [blame] | 1324 | SetOn->getOperand(3).setIsUndef(); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1325 | } else { |
| 1326 | unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); |
| 1327 | BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp) |
| 1328 | .addOperand(*Idx) |
| 1329 | .addImm(Offset); |
| 1330 | MachineInstr *SetOn = |
| 1331 | BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) |
| 1332 | .addReg(Tmp, RegState::Kill) |
| 1333 | .addImm(IdxMode); |
| 1334 | |
Matt Arsenault | dac31db | 2016-10-13 12:45:16 +0000 | [diff] [blame] | 1335 | SetOn->getOperand(3).setIsUndef(); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | return true; |
| 1339 | } |
| 1340 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1341 | if (Offset == 0) { |
| 1342 | BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) |
| 1343 | .addOperand(*Idx); |
| 1344 | } else { |
| 1345 | BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) |
| 1346 | .addOperand(*Idx) |
| 1347 | .addImm(Offset); |
| 1348 | } |
| 1349 | |
| 1350 | return true; |
| 1351 | } |
| 1352 | |
| 1353 | // Control flow needs to be inserted if indexing with a VGPR. |
| 1354 | static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI, |
| 1355 | MachineBasicBlock &MBB, |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1356 | const SISubtarget &ST) { |
| 1357 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1358 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 1359 | MachineFunction *MF = MBB.getParent(); |
| 1360 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 1361 | |
| 1362 | unsigned Dst = MI.getOperand(0).getReg(); |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1363 | unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg(); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1364 | int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); |
| 1365 | |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1366 | const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1367 | |
| 1368 | unsigned SubReg; |
| 1369 | std::tie(SubReg, Offset) |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1370 | = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1371 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1372 | bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; |
| 1373 | |
| 1374 | if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) { |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1375 | MachineBasicBlock::iterator I(&MI); |
| 1376 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1377 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1378 | if (UseGPRIdxMode) { |
| 1379 | // TODO: Look at the uses to avoid the copy. This may require rescheduling |
| 1380 | // to avoid interfering with other uses, so probably requires a new |
| 1381 | // optimization pass. |
| 1382 | BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1383 | .addReg(SrcReg, RegState::Undef, SubReg) |
| 1384 | .addReg(SrcReg, RegState::Implicit) |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1385 | .addReg(AMDGPU::M0, RegState::Implicit); |
| 1386 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); |
| 1387 | } else { |
| 1388 | BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1389 | .addReg(SrcReg, RegState::Undef, SubReg) |
| 1390 | .addReg(SrcReg, RegState::Implicit); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1393 | MI.eraseFromParent(); |
| 1394 | |
| 1395 | return &MBB; |
| 1396 | } |
| 1397 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1398 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1399 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1400 | MachineBasicBlock::iterator I(&MI); |
| 1401 | |
| 1402 | unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1403 | unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1404 | |
| 1405 | BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg); |
| 1406 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1407 | if (UseGPRIdxMode) { |
| 1408 | MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) |
| 1409 | .addImm(0) // Reset inside loop. |
| 1410 | .addImm(VGPRIndexMode::SRC0_ENABLE); |
Matt Arsenault | dac31db | 2016-10-13 12:45:16 +0000 | [diff] [blame] | 1411 | SetOn->getOperand(3).setIsUndef(); |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1412 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1413 | // Disable again after the loop. |
| 1414 | BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); |
| 1415 | } |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1416 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1417 | auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode); |
| 1418 | MachineBasicBlock *LoopBB = InsPt->getParent(); |
| 1419 | |
| 1420 | if (UseGPRIdxMode) { |
| 1421 | BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1422 | .addReg(SrcReg, RegState::Undef, SubReg) |
| 1423 | .addReg(SrcReg, RegState::Implicit) |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1424 | .addReg(AMDGPU::M0, RegState::Implicit); |
| 1425 | } else { |
| 1426 | BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1427 | .addReg(SrcReg, RegState::Undef, SubReg) |
| 1428 | .addReg(SrcReg, RegState::Implicit); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1431 | MI.eraseFromParent(); |
| 1432 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1433 | return LoopBB; |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | static MachineBasicBlock *emitIndirectDst(MachineInstr &MI, |
| 1437 | MachineBasicBlock &MBB, |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1438 | const SISubtarget &ST) { |
| 1439 | const SIInstrInfo *TII = ST.getInstrInfo(); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1440 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 1441 | MachineFunction *MF = MBB.getParent(); |
| 1442 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 1443 | |
| 1444 | unsigned Dst = MI.getOperand(0).getReg(); |
| 1445 | const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); |
| 1446 | const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); |
| 1447 | const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val); |
| 1448 | int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); |
| 1449 | const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); |
| 1450 | |
| 1451 | // This can be an immediate, but will be folded later. |
| 1452 | assert(Val->getReg()); |
| 1453 | |
| 1454 | unsigned SubReg; |
| 1455 | std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC, |
| 1456 | SrcVec->getReg(), |
| 1457 | Offset); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1458 | bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; |
| 1459 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1460 | if (Idx->getReg() == AMDGPU::NoRegister) { |
| 1461 | MachineBasicBlock::iterator I(&MI); |
| 1462 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1463 | |
| 1464 | assert(Offset == 0); |
| 1465 | |
| 1466 | BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst) |
| 1467 | .addOperand(*SrcVec) |
| 1468 | .addOperand(*Val) |
| 1469 | .addImm(SubReg); |
| 1470 | |
| 1471 | MI.eraseFromParent(); |
| 1472 | return &MBB; |
| 1473 | } |
| 1474 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1475 | if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) { |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1476 | MachineBasicBlock::iterator I(&MI); |
| 1477 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1478 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1479 | if (UseGPRIdxMode) { |
| 1480 | BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) |
| 1481 | .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst |
| 1482 | .addOperand(*Val) |
| 1483 | .addReg(Dst, RegState::ImplicitDefine) |
| 1484 | .addReg(SrcVec->getReg(), RegState::Implicit) |
| 1485 | .addReg(AMDGPU::M0, RegState::Implicit); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1486 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1487 | BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); |
| 1488 | } else { |
| 1489 | const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1490 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1491 | MachineInstr *MovRel = |
| 1492 | BuildMI(MBB, I, DL, MovRelDesc) |
| 1493 | .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst |
| 1494 | .addOperand(*Val) |
| 1495 | .addReg(Dst, RegState::ImplicitDefine) |
| 1496 | .addReg(SrcVec->getReg(), RegState::Implicit); |
| 1497 | |
| 1498 | const int ImpDefIdx = MovRelDesc.getNumOperands() + |
| 1499 | MovRelDesc.getNumImplicitUses(); |
| 1500 | const int ImpUseIdx = ImpDefIdx + 1; |
| 1501 | |
| 1502 | MovRel->tieOperands(ImpDefIdx, ImpUseIdx); |
| 1503 | } |
| 1504 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1505 | MI.eraseFromParent(); |
| 1506 | return &MBB; |
| 1507 | } |
| 1508 | |
| 1509 | if (Val->isReg()) |
| 1510 | MRI.clearKillFlags(Val->getReg()); |
| 1511 | |
| 1512 | const DebugLoc &DL = MI.getDebugLoc(); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1513 | |
| 1514 | if (UseGPRIdxMode) { |
| 1515 | MachineBasicBlock::iterator I(&MI); |
| 1516 | |
| 1517 | MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) |
| 1518 | .addImm(0) // Reset inside loop. |
| 1519 | .addImm(VGPRIndexMode::DST_ENABLE); |
Matt Arsenault | dac31db | 2016-10-13 12:45:16 +0000 | [diff] [blame] | 1520 | SetOn->getOperand(3).setIsUndef(); |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1521 | |
| 1522 | // Disable again after the loop. |
| 1523 | BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); |
| 1524 | } |
| 1525 | |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1526 | unsigned PhiReg = MRI.createVirtualRegister(VecRC); |
| 1527 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1528 | auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, |
| 1529 | Offset, UseGPRIdxMode); |
| 1530 | MachineBasicBlock *LoopBB = InsPt->getParent(); |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1531 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1532 | if (UseGPRIdxMode) { |
| 1533 | BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) |
| 1534 | .addReg(PhiReg, RegState::Undef, SubReg) // vdst |
| 1535 | .addOperand(*Val) // src0 |
| 1536 | .addReg(Dst, RegState::ImplicitDefine) |
| 1537 | .addReg(PhiReg, RegState::Implicit) |
| 1538 | .addReg(AMDGPU::M0, RegState::Implicit); |
| 1539 | } else { |
| 1540 | const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32); |
| 1541 | // vdst is not actually read and just provides the base register index. |
| 1542 | MachineInstr *MovRel = |
| 1543 | BuildMI(*LoopBB, InsPt, DL, MovRelDesc) |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1544 | .addReg(PhiReg, RegState::Undef, SubReg) // vdst |
| 1545 | .addOperand(*Val) |
| 1546 | .addReg(Dst, RegState::ImplicitDefine) |
| 1547 | .addReg(PhiReg, RegState::Implicit); |
| 1548 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1549 | const int ImpDefIdx = MovRelDesc.getNumOperands() + |
| 1550 | MovRelDesc.getNumImplicitUses(); |
| 1551 | const int ImpUseIdx = ImpDefIdx + 1; |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1552 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1553 | MovRel->tieOperands(ImpDefIdx, ImpUseIdx); |
| 1554 | } |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1555 | |
Nicolai Haehnle | bd15c32 | 2016-10-14 09:03:04 +0000 | [diff] [blame] | 1556 | MI.eraseFromParent(); |
| 1557 | |
Matt Arsenault | d486d3f | 2016-10-12 18:49:05 +0000 | [diff] [blame] | 1558 | return LoopBB; |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Matt Arsenault | 786724a | 2016-07-12 21:41:32 +0000 | [diff] [blame] | 1561 | MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter( |
| 1562 | MachineInstr &MI, MachineBasicBlock *BB) const { |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 1563 | switch (MI.getOpcode()) { |
Matt Arsenault | 4ac341c | 2016-04-14 21:58:15 +0000 | [diff] [blame] | 1564 | case AMDGPU::SI_INIT_M0: { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 1565 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 1566 | BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(), |
Matt Arsenault | 4ac341c | 2016-04-14 21:58:15 +0000 | [diff] [blame] | 1567 | TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1568 | .addOperand(MI.getOperand(0)); |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 1569 | MI.eraseFromParent(); |
Matt Arsenault | 20711b7 | 2015-02-20 22:10:45 +0000 | [diff] [blame] | 1570 | return BB; |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1571 | } |
Changpeng Fang | 01f6062 | 2016-03-15 17:28:44 +0000 | [diff] [blame] | 1572 | case AMDGPU::GET_GROUPSTATICSIZE: { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 1573 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
| 1574 | |
Changpeng Fang | 01f6062 | 2016-03-15 17:28:44 +0000 | [diff] [blame] | 1575 | MachineFunction *MF = BB->getParent(); |
| 1576 | SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 1577 | DebugLoc DL = MI.getDebugLoc(); |
Matt Arsenault | 3c07c81 | 2016-07-22 17:01:33 +0000 | [diff] [blame] | 1578 | BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32)) |
| 1579 | .addOperand(MI.getOperand(0)) |
Matt Arsenault | 52ef401 | 2016-07-26 16:45:58 +0000 | [diff] [blame] | 1580 | .addImm(MFI->getLDSSize()); |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 1581 | MI.eraseFromParent(); |
Changpeng Fang | 01f6062 | 2016-03-15 17:28:44 +0000 | [diff] [blame] | 1582 | return BB; |
| 1583 | } |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1584 | case AMDGPU::SI_INDIRECT_SRC_V1: |
| 1585 | case AMDGPU::SI_INDIRECT_SRC_V2: |
| 1586 | case AMDGPU::SI_INDIRECT_SRC_V4: |
| 1587 | case AMDGPU::SI_INDIRECT_SRC_V8: |
| 1588 | case AMDGPU::SI_INDIRECT_SRC_V16: |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1589 | return emitIndirectSrc(MI, *BB, *getSubtarget()); |
Matt Arsenault | cb540bc | 2016-07-19 00:35:03 +0000 | [diff] [blame] | 1590 | case AMDGPU::SI_INDIRECT_DST_V1: |
| 1591 | case AMDGPU::SI_INDIRECT_DST_V2: |
| 1592 | case AMDGPU::SI_INDIRECT_DST_V4: |
| 1593 | case AMDGPU::SI_INDIRECT_DST_V8: |
| 1594 | case AMDGPU::SI_INDIRECT_DST_V16: |
Matt Arsenault | dcf0cfc | 2016-10-04 01:41:05 +0000 | [diff] [blame] | 1595 | return emitIndirectDst(MI, *BB, *getSubtarget()); |
Matt Arsenault | 786724a | 2016-07-12 21:41:32 +0000 | [diff] [blame] | 1596 | case AMDGPU::SI_KILL: |
| 1597 | return splitKillBlock(MI, BB); |
Matt Arsenault | 22e4179 | 2016-08-27 01:00:37 +0000 | [diff] [blame] | 1598 | case AMDGPU::V_CNDMASK_B64_PSEUDO: { |
| 1599 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
| 1600 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
| 1601 | |
| 1602 | unsigned Dst = MI.getOperand(0).getReg(); |
| 1603 | unsigned Src0 = MI.getOperand(1).getReg(); |
| 1604 | unsigned Src1 = MI.getOperand(2).getReg(); |
| 1605 | const DebugLoc &DL = MI.getDebugLoc(); |
| 1606 | unsigned SrcCond = MI.getOperand(3).getReg(); |
| 1607 | |
| 1608 | unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1609 | unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); |
| 1610 | |
| 1611 | BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo) |
| 1612 | .addReg(Src0, 0, AMDGPU::sub0) |
| 1613 | .addReg(Src1, 0, AMDGPU::sub0) |
| 1614 | .addReg(SrcCond); |
| 1615 | BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi) |
| 1616 | .addReg(Src0, 0, AMDGPU::sub1) |
| 1617 | .addReg(Src1, 0, AMDGPU::sub1) |
| 1618 | .addReg(SrcCond); |
| 1619 | |
| 1620 | BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst) |
| 1621 | .addReg(DstLo) |
| 1622 | .addImm(AMDGPU::sub0) |
| 1623 | .addReg(DstHi) |
| 1624 | .addImm(AMDGPU::sub1); |
| 1625 | MI.eraseFromParent(); |
| 1626 | return BB; |
| 1627 | } |
Changpeng Fang | 01f6062 | 2016-03-15 17:28:44 +0000 | [diff] [blame] | 1628 | default: |
| 1629 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1630 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 1633 | bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { |
| 1634 | // This currently forces unfolding various combinations of fsub into fma with |
| 1635 | // free fneg'd operands. As long as we have fast FMA (controlled by |
| 1636 | // isFMAFasterThanFMulAndFAdd), we should perform these. |
| 1637 | |
| 1638 | // When fma is quarter rate, for f64 where add / sub are at best half rate, |
| 1639 | // most of these combines appear to be cycle neutral but save on instruction |
| 1640 | // count / code size. |
| 1641 | return true; |
| 1642 | } |
| 1643 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1644 | EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, |
| 1645 | EVT VT) const { |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 1646 | if (!VT.isVector()) { |
| 1647 | return MVT::i1; |
| 1648 | } |
Matt Arsenault | 8596f71 | 2014-11-28 22:51:38 +0000 | [diff] [blame] | 1649 | return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Mehdi Amini | eaabc51 | 2015-07-09 15:12:23 +0000 | [diff] [blame] | 1652 | MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const { |
Christian Konig | 082a14a | 2013-03-18 11:34:05 +0000 | [diff] [blame] | 1653 | return MVT::i32; |
| 1654 | } |
| 1655 | |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 1656 | // Answering this is somewhat tricky and depends on the specific device which |
| 1657 | // have different rates for fma or all f64 operations. |
| 1658 | // |
| 1659 | // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other |
| 1660 | // regardless of which device (although the number of cycles differs between |
| 1661 | // devices), so it is always profitable for f64. |
| 1662 | // |
| 1663 | // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable |
| 1664 | // only on full rate devices. Normally, we should prefer selecting v_mad_f32 |
| 1665 | // which we can always do even without fused FP ops since it returns the same |
| 1666 | // result as the separate operations and since it is always full |
| 1667 | // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 |
| 1668 | // however does not support denormals, so we do report fma as faster if we have |
| 1669 | // a fast fma device and require denormals. |
| 1670 | // |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 1671 | bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 1672 | VT = VT.getScalarType(); |
| 1673 | |
| 1674 | if (!VT.isSimple()) |
| 1675 | return false; |
| 1676 | |
| 1677 | switch (VT.getSimpleVT().SimpleTy) { |
| 1678 | case MVT::f32: |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 1679 | // This is as fast on some subtargets. However, we always have full rate f32 |
| 1680 | // mad available which returns the same result as the separate operations |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1681 | // which we should prefer over fma. We can't use this if we want to support |
| 1682 | // denormals, so only report this in these cases. |
| 1683 | return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 1684 | case MVT::f64: |
| 1685 | return true; |
| 1686 | default: |
| 1687 | break; |
| 1688 | } |
| 1689 | |
| 1690 | return false; |
| 1691 | } |
| 1692 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1693 | //===----------------------------------------------------------------------===// |
| 1694 | // Custom DAG Lowering Operations |
| 1695 | //===----------------------------------------------------------------------===// |
| 1696 | |
| 1697 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 1698 | switch (Op.getOpcode()) { |
| 1699 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1700 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 1701 | case ISD::LOAD: { |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1702 | SDValue Result = LowerLOAD(Op, DAG); |
| 1703 | assert((!Result.getNode() || |
| 1704 | Result.getNode()->getNumValues() == 2) && |
| 1705 | "Load should return a value and a chain"); |
| 1706 | return Result; |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 1707 | } |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 1708 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1709 | case ISD::FSIN: |
| 1710 | case ISD::FCOS: |
| 1711 | return LowerTrig(Op, DAG); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1712 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1713 | case ISD::FDIV: return LowerFDIV(Op, DAG); |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 1714 | case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1715 | case ISD::STORE: return LowerSTORE(Op, DAG); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1716 | case ISD::GlobalAddress: { |
| 1717 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1718 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 1719 | return LowerGlobalAddress(MFI, Op, DAG); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 1720 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1721 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
Matt Arsenault | a9dbdca | 2016-04-12 14:05:04 +0000 | [diff] [blame] | 1722 | case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1723 | case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); |
Matt Arsenault | 99c1452 | 2016-04-25 19:27:24 +0000 | [diff] [blame] | 1724 | case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG); |
Matt Arsenault | 0bb294b | 2016-06-17 22:27:03 +0000 | [diff] [blame] | 1725 | case ISD::TRAP: return lowerTRAP(Op, DAG); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1726 | } |
| 1727 | return SDValue(); |
| 1728 | } |
| 1729 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1730 | /// \brief Helper function for LowerBRCOND |
| 1731 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1732 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1733 | SDNode *Parent = Value.getNode(); |
| 1734 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 1735 | I != E; ++I) { |
| 1736 | |
| 1737 | if (I.getUse().get() != Value) |
| 1738 | continue; |
| 1739 | |
| 1740 | if (I->getOpcode() == Opcode) |
| 1741 | return *I; |
| 1742 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1743 | return nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1746 | bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const { |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1747 | if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) { |
| 1748 | switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) { |
| 1749 | case AMDGPUIntrinsic::amdgcn_if: |
| 1750 | case AMDGPUIntrinsic::amdgcn_else: |
| 1751 | case AMDGPUIntrinsic::amdgcn_end_cf: |
| 1752 | case AMDGPUIntrinsic::amdgcn_loop: |
| 1753 | return true; |
| 1754 | default: |
| 1755 | return false; |
| 1756 | } |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1757 | } |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1758 | |
| 1759 | if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) { |
| 1760 | switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) { |
| 1761 | case AMDGPUIntrinsic::amdgcn_break: |
| 1762 | case AMDGPUIntrinsic::amdgcn_if_break: |
| 1763 | case AMDGPUIntrinsic::amdgcn_else_break: |
| 1764 | return true; |
| 1765 | default: |
| 1766 | return false; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | return false; |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 1773 | void SITargetLowering::createDebuggerPrologueStackObjects( |
| 1774 | MachineFunction &MF) const { |
| 1775 | // Create stack objects that are used for emitting debugger prologue. |
| 1776 | // |
| 1777 | // Debugger prologue writes work group IDs and work item IDs to scratch memory |
| 1778 | // at fixed location in the following format: |
| 1779 | // offset 0: work group ID x |
| 1780 | // offset 4: work group ID y |
| 1781 | // offset 8: work group ID z |
| 1782 | // offset 16: work item ID x |
| 1783 | // offset 20: work item ID y |
| 1784 | // offset 24: work item ID z |
| 1785 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
| 1786 | int ObjectIdx = 0; |
| 1787 | |
| 1788 | // For each dimension: |
| 1789 | for (unsigned i = 0; i < 3; ++i) { |
| 1790 | // Create fixed stack object for work group ID. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1791 | ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true); |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 1792 | Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx); |
| 1793 | // Create fixed stack object for work item ID. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1794 | ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true); |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 1795 | Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx); |
| 1796 | } |
| 1797 | } |
| 1798 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1799 | /// This transforms the control flow intrinsics to get the branch destination as |
| 1800 | /// last parameter, also switches branch target with BR if the need arise |
| 1801 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 1802 | SelectionDAG &DAG) const { |
| 1803 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1804 | SDLoc DL(BRCOND); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1805 | |
| 1806 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 1807 | SDValue Target = BRCOND.getOperand(2); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1808 | SDNode *BR = nullptr; |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1809 | SDNode *SetCC = nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (Intr->getOpcode() == ISD::SETCC) { |
| 1812 | // As long as we negate the condition everything is fine |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1813 | SetCC = Intr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1814 | Intr = SetCC->getOperand(0).getNode(); |
| 1815 | |
| 1816 | } else { |
| 1817 | // Get the target from BR if we don't negate the condition |
| 1818 | BR = findUser(BRCOND, ISD::BR); |
| 1819 | Target = BR->getOperand(1); |
| 1820 | } |
| 1821 | |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1822 | // FIXME: This changes the types of the intrinsics instead of introducing new |
| 1823 | // nodes with the correct types. |
| 1824 | // e.g. llvm.amdgcn.loop |
| 1825 | |
| 1826 | // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3 |
| 1827 | // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088> |
| 1828 | |
Nicolai Haehnle | ffbd56a | 2016-05-05 17:36:36 +0000 | [diff] [blame] | 1829 | if (!isCFIntrinsic(Intr)) { |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1830 | // This is a uniform branch so we don't need to legalize. |
| 1831 | return BRCOND; |
| 1832 | } |
| 1833 | |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1834 | bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID || |
| 1835 | Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN; |
| 1836 | |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1837 | assert(!SetCC || |
| 1838 | (SetCC->getConstantOperandVal(1) == 1 && |
Tom Stellard | bc4497b | 2016-02-12 23:45:29 +0000 | [diff] [blame] | 1839 | cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 1840 | ISD::SETNE)); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1841 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1842 | // operands of the new intrinsic call |
| 1843 | SmallVector<SDValue, 4> Ops; |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1844 | if (HaveChain) |
| 1845 | Ops.push_back(BRCOND.getOperand(0)); |
| 1846 | |
| 1847 | Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1848 | Ops.push_back(Target); |
| 1849 | |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1850 | ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); |
| 1851 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1852 | // build the new intrinsic call |
| 1853 | SDNode *Result = DAG.getNode( |
| 1854 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1855 | DAG.getVTList(Res), Ops).getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1856 | |
Matt Arsenault | 6408c91 | 2016-09-16 22:11:18 +0000 | [diff] [blame] | 1857 | if (!HaveChain) { |
| 1858 | SDValue Ops[] = { |
| 1859 | SDValue(Result, 0), |
| 1860 | BRCOND.getOperand(0) |
| 1861 | }; |
| 1862 | |
| 1863 | Result = DAG.getMergeValues(Ops, DL).getNode(); |
| 1864 | } |
| 1865 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1866 | if (BR) { |
| 1867 | // Give the branch instruction our target |
| 1868 | SDValue Ops[] = { |
| 1869 | BR->getOperand(0), |
| 1870 | BRCOND.getOperand(2) |
| 1871 | }; |
Chandler Carruth | 356665a | 2014-08-01 22:09:43 +0000 | [diff] [blame] | 1872 | SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); |
| 1873 | DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); |
| 1874 | BR = NewBR.getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 1878 | |
| 1879 | // Copy the intrinsic results to registers |
| 1880 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 1881 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 1882 | if (!CopyToReg) |
| 1883 | continue; |
| 1884 | |
| 1885 | Chain = DAG.getCopyToReg( |
| 1886 | Chain, DL, |
| 1887 | CopyToReg->getOperand(1), |
| 1888 | SDValue(Result, i - 1), |
| 1889 | SDValue()); |
| 1890 | |
| 1891 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 1892 | } |
| 1893 | |
| 1894 | // Remove the old intrinsic from the chain |
| 1895 | DAG.ReplaceAllUsesOfValueWith( |
| 1896 | SDValue(Intr, Intr->getNumValues() - 1), |
| 1897 | Intr->getOperand(0)); |
| 1898 | |
| 1899 | return Chain; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Matt Arsenault | 99c1452 | 2016-04-25 19:27:24 +0000 | [diff] [blame] | 1902 | SDValue SITargetLowering::getSegmentAperture(unsigned AS, |
| 1903 | SelectionDAG &DAG) const { |
| 1904 | SDLoc SL; |
| 1905 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1906 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 3b2e2a5 | 2016-06-06 20:03:31 +0000 | [diff] [blame] | 1907 | unsigned UserSGPR = Info->getQueuePtrUserSGPR(); |
| 1908 | assert(UserSGPR != AMDGPU::NoRegister); |
| 1909 | |
Matt Arsenault | 99c1452 | 2016-04-25 19:27:24 +0000 | [diff] [blame] | 1910 | SDValue QueuePtr = CreateLiveInRegister( |
Matt Arsenault | 3b2e2a5 | 2016-06-06 20:03:31 +0000 | [diff] [blame] | 1911 | DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); |
Matt Arsenault | 99c1452 | 2016-04-25 19:27:24 +0000 | [diff] [blame] | 1912 | |
| 1913 | // Offset into amd_queue_t for group_segment_aperture_base_hi / |
| 1914 | // private_segment_aperture_base_hi. |
| 1915 | uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44; |
| 1916 | |
| 1917 | SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr, |
| 1918 | DAG.getConstant(StructOffset, SL, MVT::i64)); |
| 1919 | |
| 1920 | // TODO: Use custom target PseudoSourceValue. |
| 1921 | // TODO: We should use the value from the IR intrinsic call, but it might not |
| 1922 | // be available and how do we get it? |
| 1923 | Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()), |
| 1924 | AMDGPUAS::CONSTANT_ADDRESS)); |
| 1925 | |
| 1926 | MachinePointerInfo PtrInfo(V, StructOffset); |
Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 1927 | return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo, |
| 1928 | MinAlign(64, StructOffset), |
Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 1929 | MachineMemOperand::MODereferenceable | |
| 1930 | MachineMemOperand::MOInvariant); |
Matt Arsenault | 99c1452 | 2016-04-25 19:27:24 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
| 1933 | SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op, |
| 1934 | SelectionDAG &DAG) const { |
| 1935 | SDLoc SL(Op); |
| 1936 | const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op); |
| 1937 | |
| 1938 | SDValue Src = ASC->getOperand(0); |
| 1939 | |
| 1940 | // FIXME: Really support non-0 null pointers. |
| 1941 | SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32); |
| 1942 | SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64); |
| 1943 | |
| 1944 | // flat -> local/private |
| 1945 | if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { |
| 1946 | if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || |
| 1947 | ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { |
| 1948 | SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE); |
| 1949 | SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src); |
| 1950 | |
| 1951 | return DAG.getNode(ISD::SELECT, SL, MVT::i32, |
| 1952 | NonNull, Ptr, SegmentNullPtr); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | // local/private -> flat |
| 1957 | if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { |
| 1958 | if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || |
| 1959 | ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { |
| 1960 | SDValue NonNull |
| 1961 | = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE); |
| 1962 | |
| 1963 | SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG); |
| 1964 | SDValue CvtPtr |
| 1965 | = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture); |
| 1966 | |
| 1967 | return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, |
| 1968 | DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr), |
| 1969 | FlatNullPtr); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | // global <-> flat are no-ops and never emitted. |
| 1974 | |
| 1975 | const MachineFunction &MF = DAG.getMachineFunction(); |
| 1976 | DiagnosticInfoUnsupported InvalidAddrSpaceCast( |
| 1977 | *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc()); |
| 1978 | DAG.getContext()->diagnose(InvalidAddrSpaceCast); |
| 1979 | |
| 1980 | return DAG.getUNDEF(ASC->getValueType(0)); |
| 1981 | } |
| 1982 | |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 1983 | static bool shouldEmitFixup(const GlobalValue *GV, |
| 1984 | const TargetMachine &TM) { |
| 1985 | // FIXME: We need to emit global variables in constant address space in a |
| 1986 | // separate section, and use relocations. |
| 1987 | return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS; |
| 1988 | } |
| 1989 | |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 1990 | static bool shouldEmitGOTReloc(const GlobalValue *GV, |
| 1991 | const TargetMachine &TM) { |
| 1992 | return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && |
| 1993 | !TM.shouldAssumeDSOLocal(*GV->getParent(), GV); |
Tom Stellard | b164a98 | 2016-06-25 01:59:16 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 1996 | static bool shouldEmitPCReloc(const GlobalValue *GV, |
| 1997 | const TargetMachine &TM) { |
| 1998 | return !shouldEmitFixup(GV, TM) && !shouldEmitGOTReloc(GV, TM); |
| 1999 | } |
| 2000 | |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2001 | bool |
| 2002 | SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 2003 | // We can fold offsets for anything that doesn't require a GOT relocation. |
| 2004 | return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && |
| 2005 | !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine()); |
| 2006 | } |
Tom Stellard | bf3e6e5 | 2016-06-14 20:29:59 +0000 | [diff] [blame] | 2007 | |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2008 | static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, |
| 2009 | SDLoc DL, unsigned Offset, EVT PtrVT, |
| 2010 | unsigned GAFlags = SIInstrInfo::MO_NONE) { |
Tom Stellard | bf3e6e5 | 2016-06-14 20:29:59 +0000 | [diff] [blame] | 2011 | // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is |
| 2012 | // lowered to the following code sequence: |
Tom Stellard | bf3e6e5 | 2016-06-14 20:29:59 +0000 | [diff] [blame] | 2013 | // |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 2014 | // For constant address space: |
| 2015 | // s_getpc_b64 s[0:1] |
| 2016 | // s_add_u32 s0, s0, $symbol |
| 2017 | // s_addc_u32 s1, s1, 0 |
| 2018 | // |
| 2019 | // s_getpc_b64 returns the address of the s_add_u32 instruction and then |
| 2020 | // a fixup or relocation is emitted to replace $symbol with a literal |
| 2021 | // constant, which is a pc-relative offset from the encoding of the $symbol |
| 2022 | // operand to the global variable. |
| 2023 | // |
| 2024 | // For global address space: |
| 2025 | // s_getpc_b64 s[0:1] |
| 2026 | // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo |
| 2027 | // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi |
| 2028 | // |
| 2029 | // s_getpc_b64 returns the address of the s_add_u32 instruction and then |
| 2030 | // fixups or relocations are emitted to replace $symbol@*@lo and |
| 2031 | // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant, |
| 2032 | // which is a 64-bit pc-relative offset from the encoding of the $symbol |
| 2033 | // operand to the global variable. |
Tom Stellard | bf3e6e5 | 2016-06-14 20:29:59 +0000 | [diff] [blame] | 2034 | // |
| 2035 | // What we want here is an offset from the value returned by s_getpc |
| 2036 | // (which is the address of the s_add_u32 instruction) to the global |
| 2037 | // variable, but since the encoding of $symbol starts 4 bytes after the start |
| 2038 | // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too |
| 2039 | // small. This requires us to add 4 to the global variable offset in order to |
| 2040 | // compute the correct address. |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 2041 | SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, |
| 2042 | GAFlags); |
| 2043 | SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, |
| 2044 | GAFlags == SIInstrInfo::MO_NONE ? |
| 2045 | GAFlags : GAFlags + 1); |
| 2046 | return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi); |
Tom Stellard | bf3e6e5 | 2016-06-14 20:29:59 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2049 | SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, |
| 2050 | SDValue Op, |
| 2051 | SelectionDAG &DAG) const { |
| 2052 | GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); |
| 2053 | |
| 2054 | if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS && |
| 2055 | GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS) |
| 2056 | return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); |
| 2057 | |
| 2058 | SDLoc DL(GSD); |
| 2059 | const GlobalValue *GV = GSD->getGlobal(); |
| 2060 | EVT PtrVT = Op.getValueType(); |
| 2061 | |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 2062 | if (shouldEmitFixup(GV, getTargetMachine())) |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2063 | return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT); |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 2064 | else if (shouldEmitPCReloc(GV, getTargetMachine())) |
| 2065 | return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT, |
| 2066 | SIInstrInfo::MO_REL32); |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2067 | |
| 2068 | SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT, |
Konstantin Zhuravlyov | c96b5d7 | 2016-10-14 04:37:34 +0000 | [diff] [blame] | 2069 | SIInstrInfo::MO_GOTPCREL32); |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2070 | |
| 2071 | Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext()); |
| 2072 | PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); |
| 2073 | const DataLayout &DataLayout = DAG.getDataLayout(); |
| 2074 | unsigned Align = DataLayout.getABITypeAlignment(PtrTy); |
| 2075 | // FIXME: Use a PseudoSourceValue once those can be assigned an address space. |
| 2076 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 2077 | |
Justin Lebar | 9c37581 | 2016-07-15 18:27:10 +0000 | [diff] [blame] | 2078 | return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align, |
Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 2079 | MachineMemOperand::MODereferenceable | |
| 2080 | MachineMemOperand::MOInvariant); |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Matt Arsenault | 0bb294b | 2016-06-17 22:27:03 +0000 | [diff] [blame] | 2083 | SDValue SITargetLowering::lowerTRAP(SDValue Op, |
| 2084 | SelectionDAG &DAG) const { |
| 2085 | const MachineFunction &MF = DAG.getMachineFunction(); |
| 2086 | DiagnosticInfoUnsupported NoTrap(*MF.getFunction(), |
| 2087 | "trap handler not supported", |
| 2088 | Op.getDebugLoc(), |
| 2089 | DS_Warning); |
| 2090 | DAG.getContext()->diagnose(NoTrap); |
| 2091 | |
| 2092 | // Emit s_endpgm. |
| 2093 | |
| 2094 | // FIXME: This should really be selected to s_trap, but that requires |
| 2095 | // setting up the trap handler for it o do anything. |
Matt Arsenault | 9babdf4 | 2016-06-22 20:15:28 +0000 | [diff] [blame] | 2096 | return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other, |
| 2097 | Op.getOperand(0)); |
Matt Arsenault | 0bb294b | 2016-06-17 22:27:03 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 2100 | SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, |
| 2101 | const SDLoc &DL, SDValue V) const { |
Matt Arsenault | 4ac341c | 2016-04-14 21:58:15 +0000 | [diff] [blame] | 2102 | // We can't use S_MOV_B32 directly, because there is no way to specify m0 as |
| 2103 | // the destination register. |
| 2104 | // |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 2105 | // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, |
| 2106 | // so we will end up with redundant moves to m0. |
| 2107 | // |
Matt Arsenault | 4ac341c | 2016-04-14 21:58:15 +0000 | [diff] [blame] | 2108 | // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result. |
| 2109 | |
| 2110 | // A Null SDValue creates a glue result. |
| 2111 | SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue, |
| 2112 | V, Chain); |
| 2113 | return SDValue(M0, 0); |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 2116 | SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, |
| 2117 | SDValue Op, |
| 2118 | MVT VT, |
| 2119 | unsigned Offset) const { |
| 2120 | SDLoc SL(Op); |
| 2121 | SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL, |
| 2122 | DAG.getEntryNode(), Offset, false); |
| 2123 | // The local size values will have the hi 16-bits as zero. |
| 2124 | return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, |
| 2125 | DAG.getValueType(VT)); |
| 2126 | } |
| 2127 | |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2128 | static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) { |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2129 | DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2130 | "non-hsa intrinsic with hsa target", |
| 2131 | DL.getDebugLoc()); |
| 2132 | DAG.getContext()->diagnose(BadIntrin); |
| 2133 | return DAG.getUNDEF(VT); |
| 2134 | } |
| 2135 | |
| 2136 | static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) { |
| 2137 | DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), |
| 2138 | "intrinsic not supported on subtarget", |
| 2139 | DL.getDebugLoc()); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2140 | DAG.getContext()->diagnose(BadIntrin); |
| 2141 | return DAG.getUNDEF(VT); |
| 2142 | } |
| 2143 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2144 | SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
| 2145 | SelectionDAG &DAG) const { |
| 2146 | MachineFunction &MF = DAG.getMachineFunction(); |
Tom Stellard | dcb9f09 | 2015-07-09 21:20:37 +0000 | [diff] [blame] | 2147 | auto MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 2148 | const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2149 | |
| 2150 | EVT VT = Op.getValueType(); |
| 2151 | SDLoc DL(Op); |
| 2152 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 2153 | |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 2154 | // TODO: Should this propagate fast-math-flags? |
| 2155 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2156 | switch (IntrinsicID) { |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 2157 | case Intrinsic::amdgcn_dispatch_ptr: |
Matt Arsenault | 48ab526 | 2016-04-25 19:27:18 +0000 | [diff] [blame] | 2158 | case Intrinsic::amdgcn_queue_ptr: { |
Tom Stellard | 0b76fc4c | 2016-09-16 21:34:26 +0000 | [diff] [blame] | 2159 | if (!Subtarget->isAmdCodeObjectV2()) { |
Oliver Stannard | 7e7d983 | 2016-02-02 13:52:43 +0000 | [diff] [blame] | 2160 | DiagnosticInfoUnsupported BadIntrin( |
| 2161 | *MF.getFunction(), "unsupported hsa intrinsic without hsa target", |
| 2162 | DL.getDebugLoc()); |
Matt Arsenault | 800fecf | 2016-01-11 21:18:33 +0000 | [diff] [blame] | 2163 | DAG.getContext()->diagnose(BadIntrin); |
| 2164 | return DAG.getUNDEF(VT); |
| 2165 | } |
| 2166 | |
Matt Arsenault | 48ab526 | 2016-04-25 19:27:18 +0000 | [diff] [blame] | 2167 | auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ? |
| 2168 | SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR; |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 2169 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, |
Matt Arsenault | 48ab526 | 2016-04-25 19:27:18 +0000 | [diff] [blame] | 2170 | TRI->getPreloadedValue(MF, Reg), VT); |
| 2171 | } |
Jan Vesely | fea814d | 2016-06-21 20:46:20 +0000 | [diff] [blame] | 2172 | case Intrinsic::amdgcn_implicitarg_ptr: { |
| 2173 | unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT); |
| 2174 | return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset); |
| 2175 | } |
Matt Arsenault | dc4ebad | 2016-04-29 21:16:52 +0000 | [diff] [blame] | 2176 | case Intrinsic::amdgcn_kernarg_segment_ptr: { |
| 2177 | unsigned Reg |
| 2178 | = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); |
| 2179 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); |
| 2180 | } |
Matt Arsenault | 8d718dc | 2016-07-22 17:01:30 +0000 | [diff] [blame] | 2181 | case Intrinsic::amdgcn_dispatch_id: { |
| 2182 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID); |
| 2183 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); |
| 2184 | } |
Matt Arsenault | f75257a | 2016-01-23 05:32:20 +0000 | [diff] [blame] | 2185 | case Intrinsic::amdgcn_rcp: |
| 2186 | return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); |
| 2187 | case Intrinsic::amdgcn_rsq: |
Matt Arsenault | 0c3e233 | 2016-01-26 04:14:16 +0000 | [diff] [blame] | 2188 | case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name |
Matt Arsenault | f75257a | 2016-01-23 05:32:20 +0000 | [diff] [blame] | 2189 | return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2190 | case Intrinsic::amdgcn_rsq_legacy: { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 2191 | if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2192 | return emitRemovedIntrinsicError(DAG, DL, VT); |
| 2193 | |
| 2194 | return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1)); |
| 2195 | } |
Matt Arsenault | 32fc527 | 2016-07-26 16:45:45 +0000 | [diff] [blame] | 2196 | case Intrinsic::amdgcn_rcp_legacy: { |
| 2197 | if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) |
| 2198 | return emitRemovedIntrinsicError(DAG, DL, VT); |
| 2199 | return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1)); |
| 2200 | } |
Matt Arsenault | 09b2c4a | 2016-07-15 21:26:52 +0000 | [diff] [blame] | 2201 | case Intrinsic::amdgcn_rsq_clamp: { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 2202 | if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) |
Matt Arsenault | 79963e8 | 2016-02-13 01:03:00 +0000 | [diff] [blame] | 2203 | return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 2204 | |
Matt Arsenault | f75257a | 2016-01-23 05:32:20 +0000 | [diff] [blame] | 2205 | Type *Type = VT.getTypeForEVT(*DAG.getContext()); |
| 2206 | APFloat Max = APFloat::getLargest(Type->getFltSemantics()); |
| 2207 | APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); |
| 2208 | |
| 2209 | SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); |
| 2210 | SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, |
| 2211 | DAG.getConstantFP(Max, DL, VT)); |
| 2212 | return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, |
| 2213 | DAG.getConstantFP(Min, DL, VT)); |
| 2214 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2215 | case Intrinsic::r600_read_ngroups_x: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2216 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2217 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2218 | |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 2219 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 2220 | SI::KernelInputOffsets::NGROUPS_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2221 | case Intrinsic::r600_read_ngroups_y: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2222 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2223 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2224 | |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 2225 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 2226 | SI::KernelInputOffsets::NGROUPS_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2227 | case Intrinsic::r600_read_ngroups_z: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2228 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2229 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2230 | |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 2231 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 2232 | SI::KernelInputOffsets::NGROUPS_Z, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2233 | case Intrinsic::r600_read_global_size_x: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2234 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2235 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2236 | |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 2237 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 2238 | SI::KernelInputOffsets::GLOBAL_SIZE_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2239 | case Intrinsic::r600_read_global_size_y: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2240 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2241 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2242 | |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 2243 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 2244 | SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2245 | case Intrinsic::r600_read_global_size_z: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2246 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2247 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2248 | |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 2249 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 2250 | SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2251 | case Intrinsic::r600_read_local_size_x: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2252 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2253 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2254 | |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 2255 | return lowerImplicitZextParam(DAG, Op, MVT::i16, |
| 2256 | SI::KernelInputOffsets::LOCAL_SIZE_X); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2257 | case Intrinsic::r600_read_local_size_y: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2258 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2259 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2260 | |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 2261 | return lowerImplicitZextParam(DAG, Op, MVT::i16, |
| 2262 | SI::KernelInputOffsets::LOCAL_SIZE_Y); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2263 | case Intrinsic::r600_read_local_size_z: |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2264 | if (Subtarget->isAmdHsaOS()) |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 2265 | return emitNonHSAIntrinsicError(DAG, DL, VT); |
Matt Arsenault | e013246 | 2016-01-30 05:19:45 +0000 | [diff] [blame] | 2266 | |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 2267 | return lowerImplicitZextParam(DAG, Op, MVT::i16, |
| 2268 | SI::KernelInputOffsets::LOCAL_SIZE_Z); |
Matt Arsenault | 43976df | 2016-01-30 04:25:19 +0000 | [diff] [blame] | 2269 | case Intrinsic::amdgcn_workgroup_id_x: |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2270 | case Intrinsic::r600_read_tgid_x: |
| 2271 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 2272 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT); |
Matt Arsenault | 43976df | 2016-01-30 04:25:19 +0000 | [diff] [blame] | 2273 | case Intrinsic::amdgcn_workgroup_id_y: |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2274 | case Intrinsic::r600_read_tgid_y: |
| 2275 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 2276 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT); |
Matt Arsenault | 43976df | 2016-01-30 04:25:19 +0000 | [diff] [blame] | 2277 | case Intrinsic::amdgcn_workgroup_id_z: |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2278 | case Intrinsic::r600_read_tgid_z: |
| 2279 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 2280 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT); |
Matt Arsenault | 43976df | 2016-01-30 04:25:19 +0000 | [diff] [blame] | 2281 | case Intrinsic::amdgcn_workitem_id_x: |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2282 | case Intrinsic::r600_read_tidig_x: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 2283 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 2284 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT); |
Matt Arsenault | 43976df | 2016-01-30 04:25:19 +0000 | [diff] [blame] | 2285 | case Intrinsic::amdgcn_workitem_id_y: |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2286 | case Intrinsic::r600_read_tidig_y: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 2287 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 2288 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT); |
Matt Arsenault | 43976df | 2016-01-30 04:25:19 +0000 | [diff] [blame] | 2289 | case Intrinsic::amdgcn_workitem_id_z: |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2290 | case Intrinsic::r600_read_tidig_z: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 2291 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 2292 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2293 | case AMDGPUIntrinsic::SI_load_const: { |
| 2294 | SDValue Ops[] = { |
| 2295 | Op.getOperand(1), |
| 2296 | Op.getOperand(2) |
| 2297 | }; |
| 2298 | |
| 2299 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 2300 | MachinePointerInfo(), |
| 2301 | MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | |
| 2302 | MachineMemOperand::MOInvariant, |
| 2303 | VT.getStoreSize(), 4); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2304 | return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, |
| 2305 | Op->getVTList(), Ops, VT, MMO); |
| 2306 | } |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 2307 | case AMDGPUIntrinsic::amdgcn_fdiv_fast: { |
| 2308 | return lowerFDIV_FAST(Op, DAG); |
| 2309 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2310 | case AMDGPUIntrinsic::SI_vs_load_input: |
| 2311 | return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, |
| 2312 | Op.getOperand(1), |
| 2313 | Op.getOperand(2), |
| 2314 | Op.getOperand(3)); |
Marek Olsak | 43650e4 | 2015-03-24 13:40:08 +0000 | [diff] [blame] | 2315 | |
Tom Stellard | 2a9d947 | 2015-05-12 15:00:46 +0000 | [diff] [blame] | 2316 | case AMDGPUIntrinsic::SI_fs_constant: { |
| 2317 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); |
| 2318 | SDValue Glue = M0.getValue(1); |
| 2319 | return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, |
| 2320 | DAG.getConstant(2, DL, MVT::i32), // P0 |
| 2321 | Op.getOperand(1), Op.getOperand(2), Glue); |
| 2322 | } |
Marek Olsak | 6f6d318 | 2015-10-29 15:29:09 +0000 | [diff] [blame] | 2323 | case AMDGPUIntrinsic::SI_packf16: |
| 2324 | if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef()) |
| 2325 | return DAG.getUNDEF(MVT::i32); |
| 2326 | return Op; |
Tom Stellard | 2a9d947 | 2015-05-12 15:00:46 +0000 | [diff] [blame] | 2327 | case AMDGPUIntrinsic::SI_fs_interp: { |
| 2328 | SDValue IJ = Op.getOperand(4); |
| 2329 | SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, |
| 2330 | DAG.getConstant(0, DL, MVT::i32)); |
| 2331 | SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, |
| 2332 | DAG.getConstant(1, DL, MVT::i32)); |
| 2333 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); |
| 2334 | SDValue Glue = M0.getValue(1); |
| 2335 | SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL, |
| 2336 | DAG.getVTList(MVT::f32, MVT::Glue), |
| 2337 | I, Op.getOperand(1), Op.getOperand(2), Glue); |
| 2338 | Glue = SDValue(P1.getNode(), 1); |
| 2339 | return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J, |
| 2340 | Op.getOperand(1), Op.getOperand(2), Glue); |
| 2341 | } |
Tom Stellard | ad7d03d | 2015-12-15 17:02:49 +0000 | [diff] [blame] | 2342 | case Intrinsic::amdgcn_interp_p1: { |
| 2343 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); |
| 2344 | SDValue Glue = M0.getValue(1); |
| 2345 | return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), |
| 2346 | Op.getOperand(2), Op.getOperand(3), Glue); |
| 2347 | } |
| 2348 | case Intrinsic::amdgcn_interp_p2: { |
| 2349 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); |
| 2350 | SDValue Glue = SDValue(M0.getNode(), 1); |
| 2351 | return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), |
| 2352 | Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), |
| 2353 | Glue); |
| 2354 | } |
Matt Arsenault | ce56a0e | 2016-02-13 01:19:56 +0000 | [diff] [blame] | 2355 | case Intrinsic::amdgcn_sin: |
| 2356 | return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1)); |
| 2357 | |
| 2358 | case Intrinsic::amdgcn_cos: |
| 2359 | return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1)); |
| 2360 | |
| 2361 | case Intrinsic::amdgcn_log_clamp: { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 2362 | if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) |
Matt Arsenault | ce56a0e | 2016-02-13 01:19:56 +0000 | [diff] [blame] | 2363 | return SDValue(); |
| 2364 | |
| 2365 | DiagnosticInfoUnsupported BadIntrin( |
| 2366 | *MF.getFunction(), "intrinsic not supported on subtarget", |
| 2367 | DL.getDebugLoc()); |
| 2368 | DAG.getContext()->diagnose(BadIntrin); |
| 2369 | return DAG.getUNDEF(VT); |
| 2370 | } |
Matt Arsenault | f75257a | 2016-01-23 05:32:20 +0000 | [diff] [blame] | 2371 | case Intrinsic::amdgcn_ldexp: |
| 2372 | return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, |
| 2373 | Op.getOperand(1), Op.getOperand(2)); |
Matt Arsenault | 7401516 | 2016-05-28 00:19:52 +0000 | [diff] [blame] | 2374 | |
| 2375 | case Intrinsic::amdgcn_fract: |
| 2376 | return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1)); |
| 2377 | |
Matt Arsenault | f75257a | 2016-01-23 05:32:20 +0000 | [diff] [blame] | 2378 | case Intrinsic::amdgcn_class: |
| 2379 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, |
| 2380 | Op.getOperand(1), Op.getOperand(2)); |
| 2381 | case Intrinsic::amdgcn_div_fmas: |
| 2382 | return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, |
| 2383 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), |
| 2384 | Op.getOperand(4)); |
| 2385 | |
| 2386 | case Intrinsic::amdgcn_div_fixup: |
| 2387 | return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, |
| 2388 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); |
| 2389 | |
| 2390 | case Intrinsic::amdgcn_trig_preop: |
| 2391 | return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, |
| 2392 | Op.getOperand(1), Op.getOperand(2)); |
| 2393 | case Intrinsic::amdgcn_div_scale: { |
| 2394 | // 3rd parameter required to be a constant. |
| 2395 | const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 2396 | if (!Param) |
| 2397 | return DAG.getUNDEF(VT); |
| 2398 | |
| 2399 | // Translate to the operands expected by the machine instruction. The |
| 2400 | // first parameter must be the same as the first instruction. |
| 2401 | SDValue Numerator = Op.getOperand(1); |
| 2402 | SDValue Denominator = Op.getOperand(2); |
| 2403 | |
| 2404 | // Note this order is opposite of the machine instruction's operations, |
| 2405 | // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The |
| 2406 | // intrinsic has the numerator as the first operand to match a normal |
| 2407 | // division operation. |
| 2408 | |
| 2409 | SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; |
| 2410 | |
| 2411 | return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, |
| 2412 | Denominator, Numerator); |
| 2413 | } |
Wei Ding | 07e0371 | 2016-07-28 16:42:13 +0000 | [diff] [blame] | 2414 | case Intrinsic::amdgcn_icmp: { |
| 2415 | const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 2416 | int CondCode = CD->getSExtValue(); |
| 2417 | |
| 2418 | if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 2419 | CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE) |
Wei Ding | 07e0371 | 2016-07-28 16:42:13 +0000 | [diff] [blame] | 2420 | return DAG.getUNDEF(VT); |
| 2421 | |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 2422 | ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode); |
Wei Ding | 07e0371 | 2016-07-28 16:42:13 +0000 | [diff] [blame] | 2423 | ISD::CondCode CCOpcode = getICmpCondCode(IcInput); |
| 2424 | return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), |
| 2425 | Op.getOperand(2), DAG.getCondCode(CCOpcode)); |
| 2426 | } |
| 2427 | case Intrinsic::amdgcn_fcmp: { |
| 2428 | const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); |
| 2429 | int CondCode = CD->getSExtValue(); |
| 2430 | |
| 2431 | if (CondCode <= FCmpInst::Predicate::FCMP_FALSE || |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 2432 | CondCode >= FCmpInst::Predicate::FCMP_TRUE) |
Wei Ding | 07e0371 | 2016-07-28 16:42:13 +0000 | [diff] [blame] | 2433 | return DAG.getUNDEF(VT); |
| 2434 | |
NAKAMURA Takumi | 59a2064 | 2016-08-22 00:58:04 +0000 | [diff] [blame] | 2435 | FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode); |
Wei Ding | 07e0371 | 2016-07-28 16:42:13 +0000 | [diff] [blame] | 2436 | ISD::CondCode CCOpcode = getFCmpCondCode(IcInput); |
| 2437 | return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), |
| 2438 | Op.getOperand(2), DAG.getCondCode(CCOpcode)); |
| 2439 | } |
Matt Arsenault | 32fc527 | 2016-07-26 16:45:45 +0000 | [diff] [blame] | 2440 | case Intrinsic::amdgcn_fmul_legacy: |
| 2441 | return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, |
| 2442 | Op.getOperand(1), Op.getOperand(2)); |
Matt Arsenault | c96e1de | 2016-07-18 18:35:05 +0000 | [diff] [blame] | 2443 | case Intrinsic::amdgcn_sffbh: |
| 2444 | case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name. |
| 2445 | return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1)); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2446 | default: |
| 2447 | return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
| 2448 | } |
| 2449 | } |
| 2450 | |
Matt Arsenault | a9dbdca | 2016-04-12 14:05:04 +0000 | [diff] [blame] | 2451 | SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, |
| 2452 | SelectionDAG &DAG) const { |
| 2453 | unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 2454 | switch (IntrID) { |
| 2455 | case Intrinsic::amdgcn_atomic_inc: |
| 2456 | case Intrinsic::amdgcn_atomic_dec: { |
| 2457 | MemSDNode *M = cast<MemSDNode>(Op); |
| 2458 | unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ? |
| 2459 | AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC; |
| 2460 | SDValue Ops[] = { |
| 2461 | M->getOperand(0), // Chain |
| 2462 | M->getOperand(2), // Ptr |
| 2463 | M->getOperand(3) // Value |
| 2464 | }; |
| 2465 | |
| 2466 | return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops, |
| 2467 | M->getMemoryVT(), M->getMemOperand()); |
| 2468 | } |
| 2469 | default: |
| 2470 | return SDValue(); |
| 2471 | } |
| 2472 | } |
| 2473 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2474 | SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, |
| 2475 | SelectionDAG &DAG) const { |
| 2476 | MachineFunction &MF = DAG.getMachineFunction(); |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 2477 | SDLoc DL(Op); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2478 | SDValue Chain = Op.getOperand(0); |
| 2479 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 2480 | |
| 2481 | switch (IntrinsicID) { |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 2482 | case AMDGPUIntrinsic::SI_sendmsg: { |
| 2483 | Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); |
| 2484 | SDValue Glue = Chain.getValue(1); |
| 2485 | return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain, |
| 2486 | Op.getOperand(2), Glue); |
| 2487 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2488 | case AMDGPUIntrinsic::SI_tbuffer_store: { |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2489 | SDValue Ops[] = { |
| 2490 | Chain, |
| 2491 | Op.getOperand(2), |
| 2492 | Op.getOperand(3), |
| 2493 | Op.getOperand(4), |
| 2494 | Op.getOperand(5), |
| 2495 | Op.getOperand(6), |
| 2496 | Op.getOperand(7), |
| 2497 | Op.getOperand(8), |
| 2498 | Op.getOperand(9), |
| 2499 | Op.getOperand(10), |
| 2500 | Op.getOperand(11), |
| 2501 | Op.getOperand(12), |
| 2502 | Op.getOperand(13), |
| 2503 | Op.getOperand(14) |
| 2504 | }; |
| 2505 | |
| 2506 | EVT VT = Op.getOperand(3).getValueType(); |
| 2507 | |
| 2508 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 2509 | MachinePointerInfo(), |
| 2510 | MachineMemOperand::MOStore, |
| 2511 | VT.getStoreSize(), 4); |
| 2512 | return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, |
| 2513 | Op->getVTList(), Ops, VT, MMO); |
| 2514 | } |
Matt Arsenault | 0056868 | 2016-07-13 06:04:22 +0000 | [diff] [blame] | 2515 | case AMDGPUIntrinsic::AMDGPU_kill: { |
Matt Arsenault | 03006fd | 2016-07-19 16:27:56 +0000 | [diff] [blame] | 2516 | SDValue Src = Op.getOperand(2); |
| 2517 | if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) { |
Matt Arsenault | 0056868 | 2016-07-13 06:04:22 +0000 | [diff] [blame] | 2518 | if (!K->isNegative()) |
| 2519 | return Chain; |
Matt Arsenault | 03006fd | 2016-07-19 16:27:56 +0000 | [diff] [blame] | 2520 | |
| 2521 | SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32); |
| 2522 | return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne); |
Matt Arsenault | 0056868 | 2016-07-13 06:04:22 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Matt Arsenault | 03006fd | 2016-07-19 16:27:56 +0000 | [diff] [blame] | 2525 | SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src); |
| 2526 | return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast); |
Matt Arsenault | 0056868 | 2016-07-13 06:04:22 +0000 | [diff] [blame] | 2527 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 2528 | default: |
| 2529 | return SDValue(); |
| 2530 | } |
| 2531 | } |
| 2532 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 2533 | SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
| 2534 | SDLoc DL(Op); |
| 2535 | LoadSDNode *Load = cast<LoadSDNode>(Op); |
Matt Arsenault | 6dfda96 | 2016-02-10 18:21:39 +0000 | [diff] [blame] | 2536 | ISD::LoadExtType ExtType = Load->getExtensionType(); |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2537 | EVT MemVT = Load->getMemoryVT(); |
Matt Arsenault | 6dfda96 | 2016-02-10 18:21:39 +0000 | [diff] [blame] | 2538 | |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2539 | if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) { |
| 2540 | assert(MemVT == MVT::i1 && "Only i1 non-extloads expected"); |
Matt Arsenault | 6dfda96 | 2016-02-10 18:21:39 +0000 | [diff] [blame] | 2541 | // FIXME: Copied from PPC |
| 2542 | // First, load into 32 bits, then truncate to 1 bit. |
| 2543 | |
| 2544 | SDValue Chain = Load->getChain(); |
| 2545 | SDValue BasePtr = Load->getBasePtr(); |
| 2546 | MachineMemOperand *MMO = Load->getMemOperand(); |
| 2547 | |
| 2548 | SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, |
| 2549 | BasePtr, MVT::i8, MMO); |
| 2550 | |
| 2551 | SDValue Ops[] = { |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2552 | DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD), |
Matt Arsenault | 6dfda96 | 2016-02-10 18:21:39 +0000 | [diff] [blame] | 2553 | NewLD.getValue(1) |
| 2554 | }; |
| 2555 | |
| 2556 | return DAG.getMergeValues(Ops, DL); |
| 2557 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 2558 | |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2559 | if (!MemVT.isVector()) |
| 2560 | return SDValue(); |
Matt Arsenault | 4d801cd | 2015-11-24 12:05:03 +0000 | [diff] [blame] | 2561 | |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2562 | assert(Op.getValueType().getVectorElementType() == MVT::i32 && |
| 2563 | "Custom lowering for non-i32 vectors hasn't been implemented."); |
Matt Arsenault | 4d801cd | 2015-11-24 12:05:03 +0000 | [diff] [blame] | 2564 | |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2565 | unsigned AS = Load->getAddressSpace(); |
| 2566 | if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, |
| 2567 | AS, Load->getAlignment())) { |
| 2568 | SDValue Ops[2]; |
| 2569 | std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG); |
| 2570 | return DAG.getMergeValues(Ops, DL); |
| 2571 | } |
| 2572 | |
| 2573 | unsigned NumElements = MemVT.getVectorNumElements(); |
| 2574 | switch (AS) { |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2575 | case AMDGPUAS::CONSTANT_ADDRESS: |
| 2576 | if (isMemOpUniform(Load)) |
| 2577 | return SDValue(); |
| 2578 | // Non-uniform loads will be selected to MUBUF instructions, so they |
| 2579 | // have the same legalization requires ments as global and private |
| 2580 | // loads. |
| 2581 | // |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 2582 | LLVM_FALLTHROUGH; |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2583 | case AMDGPUAS::GLOBAL_ADDRESS: |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2584 | case AMDGPUAS::FLAT_ADDRESS: |
| 2585 | if (NumElements > 4) |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2586 | return SplitVectorLoad(Op, DAG); |
| 2587 | // v4 loads are supported for private and global memory. |
| 2588 | return SDValue(); |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2589 | case AMDGPUAS::PRIVATE_ADDRESS: { |
| 2590 | // Depending on the setting of the private_element_size field in the |
| 2591 | // resource descriptor, we can only make private accesses up to a certain |
| 2592 | // size. |
| 2593 | switch (Subtarget->getMaxPrivateElementSize()) { |
| 2594 | case 4: |
Matt Arsenault | 9c499c3 | 2016-04-14 23:31:26 +0000 | [diff] [blame] | 2595 | return scalarizeVectorLoad(Load, DAG); |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2596 | case 8: |
| 2597 | if (NumElements > 2) |
| 2598 | return SplitVectorLoad(Op, DAG); |
| 2599 | return SDValue(); |
| 2600 | case 16: |
| 2601 | // Same as global/flat |
| 2602 | if (NumElements > 4) |
| 2603 | return SplitVectorLoad(Op, DAG); |
| 2604 | return SDValue(); |
| 2605 | default: |
| 2606 | llvm_unreachable("unsupported private_element_size"); |
| 2607 | } |
| 2608 | } |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2609 | case AMDGPUAS::LOCAL_ADDRESS: { |
| 2610 | if (NumElements > 2) |
| 2611 | return SplitVectorLoad(Op, DAG); |
| 2612 | |
| 2613 | if (NumElements == 2) |
| 2614 | return SDValue(); |
| 2615 | |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2616 | // If properly aligned, if we split we might be able to use ds_read_b64. |
| 2617 | return SplitVectorLoad(Op, DAG); |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2618 | } |
Matt Arsenault | a143641 | 2016-02-10 18:21:45 +0000 | [diff] [blame] | 2619 | default: |
| 2620 | return SDValue(); |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 2621 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 2624 | SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { |
| 2625 | if (Op.getValueType() != MVT::i64) |
| 2626 | return SDValue(); |
| 2627 | |
| 2628 | SDLoc DL(Op); |
| 2629 | SDValue Cond = Op.getOperand(0); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 2630 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2631 | SDValue Zero = DAG.getConstant(0, DL, MVT::i32); |
| 2632 | SDValue One = DAG.getConstant(1, DL, MVT::i32); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 2633 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 2634 | SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); |
| 2635 | SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); |
| 2636 | |
| 2637 | SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); |
| 2638 | SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 2639 | |
| 2640 | SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); |
| 2641 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 2642 | SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); |
| 2643 | SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 2644 | |
| 2645 | SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); |
| 2646 | |
Ahmed Bougacha | 128f873 | 2016-04-26 21:15:30 +0000 | [diff] [blame] | 2647 | SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi}); |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 2648 | return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 2651 | // Catch division cases where we can use shortcuts with rcp and rsq |
| 2652 | // instructions. |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 2653 | SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op, |
| 2654 | SelectionDAG &DAG) const { |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2655 | SDLoc SL(Op); |
| 2656 | SDValue LHS = Op.getOperand(0); |
| 2657 | SDValue RHS = Op.getOperand(1); |
| 2658 | EVT VT = Op.getValueType(); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 2659 | bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2660 | |
| 2661 | if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { |
Matt Arsenault | 979902b | 2016-08-02 22:25:04 +0000 | [diff] [blame] | 2662 | if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) { |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2663 | |
Matt Arsenault | 979902b | 2016-08-02 22:25:04 +0000 | [diff] [blame] | 2664 | if (CLHS->isExactlyValue(1.0)) { |
| 2665 | // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to |
| 2666 | // the CI documentation has a worst case error of 1 ulp. |
| 2667 | // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to |
| 2668 | // use it as long as we aren't trying to use denormals. |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2669 | |
Matt Arsenault | 979902b | 2016-08-02 22:25:04 +0000 | [diff] [blame] | 2670 | // 1.0 / sqrt(x) -> rsq(x) |
| 2671 | // |
| 2672 | // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP |
| 2673 | // error seems really high at 2^29 ULP. |
| 2674 | if (RHS.getOpcode() == ISD::FSQRT) |
| 2675 | return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); |
| 2676 | |
| 2677 | // 1.0 / x -> rcp(x) |
| 2678 | return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); |
| 2679 | } |
| 2680 | |
| 2681 | // Same as for 1.0, but expand the sign out of the constant. |
| 2682 | if (CLHS->isExactlyValue(-1.0)) { |
| 2683 | // -1.0 / x -> rcp (fneg x) |
| 2684 | SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); |
| 2685 | return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS); |
| 2686 | } |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2687 | } |
| 2688 | } |
| 2689 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2690 | const SDNodeFlags *Flags = Op->getFlags(); |
| 2691 | |
| 2692 | if (Unsafe || Flags->hasAllowReciprocal()) { |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 2693 | // Turn into multiply by the reciprocal. |
| 2694 | // x / y -> x * (1.0 / y) |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 2695 | SDNodeFlags Flags; |
| 2696 | Flags.setUnsafeAlgebra(true); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 2697 | SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 2698 | return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | return SDValue(); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 2704 | // Faster 2.5 ULP division that does not support denormals. |
| 2705 | SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const { |
| 2706 | SDLoc SL(Op); |
| 2707 | SDValue LHS = Op.getOperand(1); |
| 2708 | SDValue RHS = Op.getOperand(2); |
| 2709 | |
| 2710 | SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); |
| 2711 | |
| 2712 | const APFloat K0Val(BitsToFloat(0x6f800000)); |
| 2713 | const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); |
| 2714 | |
| 2715 | const APFloat K1Val(BitsToFloat(0x2f800000)); |
| 2716 | const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); |
| 2717 | |
| 2718 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); |
| 2719 | |
| 2720 | EVT SetCCVT = |
| 2721 | getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); |
| 2722 | |
| 2723 | SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); |
| 2724 | |
| 2725 | SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); |
| 2726 | |
| 2727 | // TODO: Should this propagate fast-math-flags? |
| 2728 | r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); |
| 2729 | |
| 2730 | // rcp does not support denormals. |
| 2731 | SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); |
| 2732 | |
| 2733 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); |
| 2734 | |
| 2735 | return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); |
| 2736 | } |
| 2737 | |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2738 | SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 2739 | if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) |
Eric Christopher | 538d09d0 | 2016-06-07 20:27:12 +0000 | [diff] [blame] | 2740 | return FastLowered; |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 2741 | |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2742 | SDLoc SL(Op); |
| 2743 | SDValue LHS = Op.getOperand(0); |
| 2744 | SDValue RHS = Op.getOperand(1); |
| 2745 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2746 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2747 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2748 | SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2749 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2750 | SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS); |
| 2751 | SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2752 | |
Matt Arsenault | dfec5ce | 2016-07-09 07:48:11 +0000 | [diff] [blame] | 2753 | // Denominator is scaled to not be denormal, so using rcp is ok. |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2754 | SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2755 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2756 | SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2757 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2758 | SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One); |
| 2759 | SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2760 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2761 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2762 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2763 | SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled); |
| 2764 | SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul); |
| 2765 | SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2766 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2767 | SDValue Scale = NumeratorScaled.getValue(1); |
| 2768 | SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale); |
Matt Arsenault | 37fefd6 | 2016-06-10 02:18:02 +0000 | [diff] [blame] | 2769 | |
Wei Ding | ed0f97f | 2016-06-09 19:17:15 +0000 | [diff] [blame] | 2770 | return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 2774 | if (DAG.getTarget().Options.UnsafeFPMath) |
Matt Arsenault | a1fe17c | 2016-07-19 23:16:53 +0000 | [diff] [blame] | 2775 | return lowerFastUnsafeFDIV(Op, DAG); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 2776 | |
| 2777 | SDLoc SL(Op); |
| 2778 | SDValue X = Op.getOperand(0); |
| 2779 | SDValue Y = Op.getOperand(1); |
| 2780 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2781 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 2782 | |
| 2783 | SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); |
| 2784 | |
| 2785 | SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); |
| 2786 | |
| 2787 | SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); |
| 2788 | |
| 2789 | SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); |
| 2790 | |
| 2791 | SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); |
| 2792 | |
| 2793 | SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); |
| 2794 | |
| 2795 | SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); |
| 2796 | |
| 2797 | SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); |
| 2798 | |
| 2799 | SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); |
| 2800 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); |
| 2801 | |
| 2802 | SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, |
| 2803 | NegDivScale0, Mul, DivScale1); |
| 2804 | |
| 2805 | SDValue Scale; |
| 2806 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 2807 | if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 2808 | // Workaround a hardware bug on SI where the condition output from div_scale |
| 2809 | // is not usable. |
| 2810 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2811 | const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 2812 | |
| 2813 | // Figure out if the scale to use for div_fmas. |
| 2814 | SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); |
| 2815 | SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); |
| 2816 | SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); |
| 2817 | SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); |
| 2818 | |
| 2819 | SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); |
| 2820 | SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); |
| 2821 | |
| 2822 | SDValue Scale0Hi |
| 2823 | = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); |
| 2824 | SDValue Scale1Hi |
| 2825 | = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); |
| 2826 | |
| 2827 | SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); |
| 2828 | SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); |
| 2829 | Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); |
| 2830 | } else { |
| 2831 | Scale = DivScale1.getValue(1); |
| 2832 | } |
| 2833 | |
| 2834 | SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, |
| 2835 | Fma4, Fma3, Mul, Scale); |
| 2836 | |
| 2837 | return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { |
| 2841 | EVT VT = Op.getValueType(); |
| 2842 | |
| 2843 | if (VT == MVT::f32) |
| 2844 | return LowerFDIV32(Op, DAG); |
| 2845 | |
| 2846 | if (VT == MVT::f64) |
| 2847 | return LowerFDIV64(Op, DAG); |
| 2848 | |
| 2849 | llvm_unreachable("Unexpected type for fdiv"); |
| 2850 | } |
| 2851 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 2852 | SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { |
| 2853 | SDLoc DL(Op); |
| 2854 | StoreSDNode *Store = cast<StoreSDNode>(Op); |
| 2855 | EVT VT = Store->getMemoryVT(); |
| 2856 | |
Matt Arsenault | 9524566 | 2016-02-11 05:32:46 +0000 | [diff] [blame] | 2857 | if (VT == MVT::i1) { |
| 2858 | return DAG.getTruncStore(Store->getChain(), DL, |
| 2859 | DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), |
| 2860 | Store->getBasePtr(), MVT::i1, Store->getMemOperand()); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2863 | assert(VT.isVector() && |
| 2864 | Store->getValue().getValueType().getScalarType() == MVT::i32); |
| 2865 | |
| 2866 | unsigned AS = Store->getAddressSpace(); |
| 2867 | if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, |
| 2868 | AS, Store->getAlignment())) { |
| 2869 | return expandUnalignedStore(Store, DAG); |
| 2870 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 2871 | |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2872 | unsigned NumElements = VT.getVectorNumElements(); |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2873 | switch (AS) { |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2874 | case AMDGPUAS::GLOBAL_ADDRESS: |
| 2875 | case AMDGPUAS::FLAT_ADDRESS: |
| 2876 | if (NumElements > 4) |
| 2877 | return SplitVectorStore(Op, DAG); |
| 2878 | return SDValue(); |
| 2879 | case AMDGPUAS::PRIVATE_ADDRESS: { |
| 2880 | switch (Subtarget->getMaxPrivateElementSize()) { |
| 2881 | case 4: |
Matt Arsenault | 9c499c3 | 2016-04-14 23:31:26 +0000 | [diff] [blame] | 2882 | return scalarizeVectorStore(Store, DAG); |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2883 | case 8: |
| 2884 | if (NumElements > 2) |
| 2885 | return SplitVectorStore(Op, DAG); |
| 2886 | return SDValue(); |
| 2887 | case 16: |
| 2888 | if (NumElements > 4) |
| 2889 | return SplitVectorStore(Op, DAG); |
| 2890 | return SDValue(); |
| 2891 | default: |
| 2892 | llvm_unreachable("unsupported private_element_size"); |
| 2893 | } |
| 2894 | } |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2895 | case AMDGPUAS::LOCAL_ADDRESS: { |
| 2896 | if (NumElements > 2) |
| 2897 | return SplitVectorStore(Op, DAG); |
| 2898 | |
| 2899 | if (NumElements == 2) |
| 2900 | return Op; |
| 2901 | |
Matt Arsenault | 9524566 | 2016-02-11 05:32:46 +0000 | [diff] [blame] | 2902 | // If properly aligned, if we split we might be able to use ds_write_b64. |
| 2903 | return SplitVectorStore(Op, DAG); |
Matt Arsenault | bcdfee7 | 2016-05-02 20:13:51 +0000 | [diff] [blame] | 2904 | } |
Matt Arsenault | f2ddbf0 | 2016-02-13 04:18:53 +0000 | [diff] [blame] | 2905 | default: |
| 2906 | llvm_unreachable("unhandled address space"); |
Matt Arsenault | 9524566 | 2016-02-11 05:32:46 +0000 | [diff] [blame] | 2907 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 2910 | SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2911 | SDLoc DL(Op); |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 2912 | EVT VT = Op.getValueType(); |
| 2913 | SDValue Arg = Op.getOperand(0); |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 2914 | // TODO: Should this propagate fast-math-flags? |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2915 | SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, |
| 2916 | DAG.getNode(ISD::FMUL, DL, VT, Arg, |
| 2917 | DAG.getConstantFP(0.5/M_PI, DL, |
| 2918 | VT))); |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 2919 | |
| 2920 | switch (Op.getOpcode()) { |
| 2921 | case ISD::FCOS: |
| 2922 | return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); |
| 2923 | case ISD::FSIN: |
| 2924 | return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); |
| 2925 | default: |
| 2926 | llvm_unreachable("Wrong trig opcode"); |
| 2927 | } |
| 2928 | } |
| 2929 | |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 2930 | SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const { |
| 2931 | AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op); |
| 2932 | assert(AtomicNode->isCompareAndSwap()); |
| 2933 | unsigned AS = AtomicNode->getAddressSpace(); |
| 2934 | |
| 2935 | // No custom lowering required for local address space |
| 2936 | if (!isFlatGlobalAddrSpace(AS)) |
| 2937 | return Op; |
| 2938 | |
| 2939 | // Non-local address space requires custom lowering for atomic compare |
| 2940 | // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2 |
| 2941 | SDLoc DL(Op); |
| 2942 | SDValue ChainIn = Op.getOperand(0); |
| 2943 | SDValue Addr = Op.getOperand(1); |
| 2944 | SDValue Old = Op.getOperand(2); |
| 2945 | SDValue New = Op.getOperand(3); |
| 2946 | EVT VT = Op.getValueType(); |
| 2947 | MVT SimpleVT = VT.getSimpleVT(); |
| 2948 | MVT VecType = MVT::getVectorVT(SimpleVT, 2); |
| 2949 | |
Ahmed Bougacha | 128f873 | 2016-04-26 21:15:30 +0000 | [diff] [blame] | 2950 | SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old}); |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 2951 | SDValue Ops[] = { ChainIn, Addr, NewOld }; |
Matt Arsenault | 8870181 | 2016-06-09 23:42:48 +0000 | [diff] [blame] | 2952 | |
| 2953 | return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(), |
| 2954 | Ops, VT, AtomicNode->getMemOperand()); |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 2957 | //===----------------------------------------------------------------------===// |
| 2958 | // Custom DAG optimizations |
| 2959 | //===----------------------------------------------------------------------===// |
| 2960 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 2961 | SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 2962 | DAGCombinerInfo &DCI) const { |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 2963 | EVT VT = N->getValueType(0); |
| 2964 | EVT ScalarVT = VT.getScalarType(); |
| 2965 | if (ScalarVT != MVT::f32) |
| 2966 | return SDValue(); |
| 2967 | |
| 2968 | SelectionDAG &DAG = DCI.DAG; |
| 2969 | SDLoc DL(N); |
| 2970 | |
| 2971 | SDValue Src = N->getOperand(0); |
| 2972 | EVT SrcVT = Src.getValueType(); |
| 2973 | |
| 2974 | // TODO: We could try to match extracting the higher bytes, which would be |
| 2975 | // easier if i8 vectors weren't promoted to i32 vectors, particularly after |
| 2976 | // types are legalized. v4i8 -> v4f32 is probably the only case to worry |
| 2977 | // about in practice. |
| 2978 | if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { |
| 2979 | if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { |
| 2980 | SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); |
| 2981 | DCI.AddToWorklist(Cvt.getNode()); |
| 2982 | return Cvt; |
| 2983 | } |
| 2984 | } |
| 2985 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 2986 | return SDValue(); |
| 2987 | } |
| 2988 | |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 2989 | /// \brief Return true if the given offset Size in bytes can be folded into |
| 2990 | /// the immediate offsets of a memory instruction for the given address space. |
| 2991 | static bool canFoldOffset(unsigned OffsetSize, unsigned AS, |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 2992 | const SISubtarget &STI) { |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 2993 | switch (AS) { |
| 2994 | case AMDGPUAS::GLOBAL_ADDRESS: { |
| 2995 | // MUBUF instructions a 12-bit offset in bytes. |
| 2996 | return isUInt<12>(OffsetSize); |
| 2997 | } |
| 2998 | case AMDGPUAS::CONSTANT_ADDRESS: { |
| 2999 | // SMRD instructions have an 8-bit offset in dwords on SI and |
| 3000 | // a 20-bit offset in bytes on VI. |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 3001 | if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 3002 | return isUInt<20>(OffsetSize); |
| 3003 | else |
| 3004 | return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); |
| 3005 | } |
| 3006 | case AMDGPUAS::LOCAL_ADDRESS: |
| 3007 | case AMDGPUAS::REGION_ADDRESS: { |
| 3008 | // The single offset versions have a 16-bit offset in bytes. |
| 3009 | return isUInt<16>(OffsetSize); |
| 3010 | } |
| 3011 | case AMDGPUAS::PRIVATE_ADDRESS: |
| 3012 | // Indirect register addressing does not use any offsets. |
| 3013 | default: |
| 3014 | return 0; |
| 3015 | } |
| 3016 | } |
| 3017 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3018 | // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) |
| 3019 | |
| 3020 | // This is a variant of |
| 3021 | // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), |
| 3022 | // |
| 3023 | // The normal DAG combiner will do this, but only if the add has one use since |
| 3024 | // that would increase the number of instructions. |
| 3025 | // |
| 3026 | // This prevents us from seeing a constant offset that can be folded into a |
| 3027 | // memory instruction's addressing mode. If we know the resulting add offset of |
| 3028 | // a pointer can be folded into an addressing offset, we can replace the pointer |
| 3029 | // operand with the add of new constant offset. This eliminates one of the uses, |
| 3030 | // and may allow the remaining use to also be simplified. |
| 3031 | // |
| 3032 | SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, |
| 3033 | unsigned AddrSpace, |
| 3034 | DAGCombinerInfo &DCI) const { |
| 3035 | SDValue N0 = N->getOperand(0); |
| 3036 | SDValue N1 = N->getOperand(1); |
| 3037 | |
| 3038 | if (N0.getOpcode() != ISD::ADD) |
| 3039 | return SDValue(); |
| 3040 | |
| 3041 | const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); |
| 3042 | if (!CN1) |
| 3043 | return SDValue(); |
| 3044 | |
| 3045 | const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 3046 | if (!CAdd) |
| 3047 | return SDValue(); |
| 3048 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3049 | // If the resulting offset is too large, we can't fold it into the addressing |
| 3050 | // mode offset. |
| 3051 | APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 3052 | if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget())) |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3053 | return SDValue(); |
| 3054 | |
| 3055 | SelectionDAG &DAG = DCI.DAG; |
| 3056 | SDLoc SL(N); |
| 3057 | EVT VT = N->getValueType(0); |
| 3058 | |
| 3059 | SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3060 | SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3061 | |
| 3062 | return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); |
| 3063 | } |
| 3064 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3065 | static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) { |
| 3066 | return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) || |
| 3067 | (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) || |
| 3068 | (Opc == ISD::XOR && Val == 0); |
| 3069 | } |
| 3070 | |
| 3071 | // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This |
| 3072 | // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit |
| 3073 | // integer combine opportunities since most 64-bit operations are decomposed |
| 3074 | // this way. TODO: We won't want this for SALU especially if it is an inline |
| 3075 | // immediate. |
| 3076 | SDValue SITargetLowering::splitBinaryBitConstantOp( |
| 3077 | DAGCombinerInfo &DCI, |
| 3078 | const SDLoc &SL, |
| 3079 | unsigned Opc, SDValue LHS, |
| 3080 | const ConstantSDNode *CRHS) const { |
| 3081 | uint64_t Val = CRHS->getZExtValue(); |
| 3082 | uint32_t ValLo = Lo_32(Val); |
| 3083 | uint32_t ValHi = Hi_32(Val); |
| 3084 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
| 3085 | |
| 3086 | if ((bitOpWithConstantIsReducible(Opc, ValLo) || |
| 3087 | bitOpWithConstantIsReducible(Opc, ValHi)) || |
| 3088 | (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) { |
| 3089 | // If we need to materialize a 64-bit immediate, it will be split up later |
| 3090 | // anyway. Avoid creating the harder to understand 64-bit immediate |
| 3091 | // materialization. |
| 3092 | return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi); |
| 3093 | } |
| 3094 | |
| 3095 | return SDValue(); |
| 3096 | } |
| 3097 | |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 3098 | SDValue SITargetLowering::performAndCombine(SDNode *N, |
| 3099 | DAGCombinerInfo &DCI) const { |
| 3100 | if (DCI.isBeforeLegalize()) |
| 3101 | return SDValue(); |
| 3102 | |
| 3103 | SelectionDAG &DAG = DCI.DAG; |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3104 | EVT VT = N->getValueType(0); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 3105 | SDValue LHS = N->getOperand(0); |
| 3106 | SDValue RHS = N->getOperand(1); |
| 3107 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3108 | |
| 3109 | if (VT == MVT::i64) { |
| 3110 | const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); |
| 3111 | if (CRHS) { |
| 3112 | if (SDValue Split |
| 3113 | = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS)) |
| 3114 | return Split; |
| 3115 | } |
| 3116 | } |
| 3117 | |
| 3118 | // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> |
| 3119 | // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) |
| 3120 | if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) { |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 3121 | ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); |
| 3122 | ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); |
| 3123 | |
| 3124 | SDValue X = LHS.getOperand(0); |
| 3125 | SDValue Y = RHS.getOperand(0); |
| 3126 | if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) |
| 3127 | return SDValue(); |
| 3128 | |
| 3129 | if (LCC == ISD::SETO) { |
| 3130 | if (X != LHS.getOperand(1)) |
| 3131 | return SDValue(); |
| 3132 | |
| 3133 | if (RCC == ISD::SETUNE) { |
| 3134 | const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); |
| 3135 | if (!C1 || !C1->isInfinity() || C1->isNegative()) |
| 3136 | return SDValue(); |
| 3137 | |
| 3138 | const uint32_t Mask = SIInstrFlags::N_NORMAL | |
| 3139 | SIInstrFlags::N_SUBNORMAL | |
| 3140 | SIInstrFlags::N_ZERO | |
| 3141 | SIInstrFlags::P_ZERO | |
| 3142 | SIInstrFlags::P_SUBNORMAL | |
| 3143 | SIInstrFlags::P_NORMAL; |
| 3144 | |
| 3145 | static_assert(((~(SIInstrFlags::S_NAN | |
| 3146 | SIInstrFlags::Q_NAN | |
| 3147 | SIInstrFlags::N_INFINITY | |
| 3148 | SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, |
| 3149 | "mask not equal"); |
| 3150 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3151 | SDLoc DL(N); |
| 3152 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, |
| 3153 | X, DAG.getConstant(Mask, DL, MVT::i32)); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 3154 | } |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | return SDValue(); |
| 3159 | } |
| 3160 | |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3161 | SDValue SITargetLowering::performOrCombine(SDNode *N, |
| 3162 | DAGCombinerInfo &DCI) const { |
| 3163 | SelectionDAG &DAG = DCI.DAG; |
| 3164 | SDValue LHS = N->getOperand(0); |
| 3165 | SDValue RHS = N->getOperand(1); |
| 3166 | |
Matt Arsenault | 3b08238 | 2016-04-12 18:24:38 +0000 | [diff] [blame] | 3167 | EVT VT = N->getValueType(0); |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3168 | if (VT == MVT::i1) { |
| 3169 | // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) |
| 3170 | if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && |
| 3171 | RHS.getOpcode() == AMDGPUISD::FP_CLASS) { |
| 3172 | SDValue Src = LHS.getOperand(0); |
| 3173 | if (Src != RHS.getOperand(0)) |
| 3174 | return SDValue(); |
Matt Arsenault | 3b08238 | 2016-04-12 18:24:38 +0000 | [diff] [blame] | 3175 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3176 | const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); |
| 3177 | const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); |
| 3178 | if (!CLHS || !CRHS) |
| 3179 | return SDValue(); |
Matt Arsenault | 3b08238 | 2016-04-12 18:24:38 +0000 | [diff] [blame] | 3180 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3181 | // Only 10 bits are used. |
| 3182 | static const uint32_t MaxMask = 0x3ff; |
Matt Arsenault | 3b08238 | 2016-04-12 18:24:38 +0000 | [diff] [blame] | 3183 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3184 | uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; |
| 3185 | SDLoc DL(N); |
| 3186 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, |
| 3187 | Src, DAG.getConstant(NewMask, DL, MVT::i32)); |
| 3188 | } |
Matt Arsenault | 3b08238 | 2016-04-12 18:24:38 +0000 | [diff] [blame] | 3189 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3190 | return SDValue(); |
| 3191 | } |
| 3192 | |
| 3193 | if (VT != MVT::i64) |
| 3194 | return SDValue(); |
| 3195 | |
| 3196 | // TODO: This could be a generic combine with a predicate for extracting the |
| 3197 | // high half of an integer being free. |
| 3198 | |
| 3199 | // (or i64:x, (zero_extend i32:y)) -> |
| 3200 | // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x))) |
| 3201 | if (LHS.getOpcode() == ISD::ZERO_EXTEND && |
| 3202 | RHS.getOpcode() != ISD::ZERO_EXTEND) |
| 3203 | std::swap(LHS, RHS); |
| 3204 | |
| 3205 | if (RHS.getOpcode() == ISD::ZERO_EXTEND) { |
| 3206 | SDValue ExtSrc = RHS.getOperand(0); |
| 3207 | EVT SrcVT = ExtSrc.getValueType(); |
| 3208 | if (SrcVT == MVT::i32) { |
| 3209 | SDLoc SL(N); |
| 3210 | SDValue LowLHS, HiBits; |
| 3211 | std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG); |
| 3212 | SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc); |
| 3213 | |
| 3214 | DCI.AddToWorklist(LowOr.getNode()); |
| 3215 | DCI.AddToWorklist(HiBits.getNode()); |
| 3216 | |
| 3217 | SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, |
| 3218 | LowOr, HiBits); |
| 3219 | return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); |
Matt Arsenault | 3b08238 | 2016-04-12 18:24:38 +0000 | [diff] [blame] | 3220 | } |
| 3221 | } |
| 3222 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3223 | const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 3224 | if (CRHS) { |
| 3225 | if (SDValue Split |
| 3226 | = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS)) |
| 3227 | return Split; |
| 3228 | } |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3229 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3230 | return SDValue(); |
| 3231 | } |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3232 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3233 | SDValue SITargetLowering::performXorCombine(SDNode *N, |
| 3234 | DAGCombinerInfo &DCI) const { |
| 3235 | EVT VT = N->getValueType(0); |
| 3236 | if (VT != MVT::i64) |
| 3237 | return SDValue(); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3238 | |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3239 | SDValue LHS = N->getOperand(0); |
| 3240 | SDValue RHS = N->getOperand(1); |
| 3241 | |
| 3242 | const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); |
| 3243 | if (CRHS) { |
| 3244 | if (SDValue Split |
| 3245 | = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS)) |
| 3246 | return Split; |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | return SDValue(); |
| 3250 | } |
| 3251 | |
| 3252 | SDValue SITargetLowering::performClassCombine(SDNode *N, |
| 3253 | DAGCombinerInfo &DCI) const { |
| 3254 | SelectionDAG &DAG = DCI.DAG; |
| 3255 | SDValue Mask = N->getOperand(1); |
| 3256 | |
| 3257 | // fp_class x, 0 -> false |
| 3258 | if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { |
| 3259 | if (CMask->isNullValue()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3260 | return DAG.getConstant(0, SDLoc(N), MVT::i1); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 3263 | if (N->getOperand(0).isUndef()) |
| 3264 | return DAG.getUNDEF(MVT::i1); |
| 3265 | |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3266 | return SDValue(); |
| 3267 | } |
| 3268 | |
Matt Arsenault | 9cd9071 | 2016-04-14 01:42:16 +0000 | [diff] [blame] | 3269 | // Constant fold canonicalize. |
| 3270 | SDValue SITargetLowering::performFCanonicalizeCombine( |
| 3271 | SDNode *N, |
| 3272 | DAGCombinerInfo &DCI) const { |
| 3273 | ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); |
| 3274 | if (!CFP) |
| 3275 | return SDValue(); |
| 3276 | |
| 3277 | SelectionDAG &DAG = DCI.DAG; |
| 3278 | const APFloat &C = CFP->getValueAPF(); |
| 3279 | |
| 3280 | // Flush denormals to 0 if not enabled. |
| 3281 | if (C.isDenormal()) { |
| 3282 | EVT VT = N->getValueType(0); |
| 3283 | if (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) |
| 3284 | return DAG.getConstantFP(0.0, SDLoc(N), VT); |
| 3285 | |
| 3286 | if (VT == MVT::f64 && !Subtarget->hasFP64Denormals()) |
| 3287 | return DAG.getConstantFP(0.0, SDLoc(N), VT); |
| 3288 | } |
| 3289 | |
| 3290 | if (C.isNaN()) { |
| 3291 | EVT VT = N->getValueType(0); |
| 3292 | APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics()); |
| 3293 | if (C.isSignaling()) { |
| 3294 | // Quiet a signaling NaN. |
| 3295 | return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); |
| 3296 | } |
| 3297 | |
| 3298 | // Make sure it is the canonical NaN bitpattern. |
| 3299 | // |
| 3300 | // TODO: Can we use -1 as the canonical NaN value since it's an inline |
| 3301 | // immediate? |
| 3302 | if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt()) |
| 3303 | return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); |
| 3304 | } |
| 3305 | |
| 3306 | return SDValue(CFP, 0); |
| 3307 | } |
| 3308 | |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3309 | static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { |
| 3310 | switch (Opc) { |
| 3311 | case ISD::FMAXNUM: |
| 3312 | return AMDGPUISD::FMAX3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 3313 | case ISD::SMAX: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3314 | return AMDGPUISD::SMAX3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 3315 | case ISD::UMAX: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3316 | return AMDGPUISD::UMAX3; |
| 3317 | case ISD::FMINNUM: |
| 3318 | return AMDGPUISD::FMIN3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 3319 | case ISD::SMIN: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3320 | return AMDGPUISD::SMIN3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 3321 | case ISD::UMIN: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3322 | return AMDGPUISD::UMIN3; |
| 3323 | default: |
| 3324 | llvm_unreachable("Not a min/max opcode"); |
| 3325 | } |
| 3326 | } |
| 3327 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3328 | static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, |
| 3329 | SDValue Op0, SDValue Op1, bool Signed) { |
Matt Arsenault | f639c32 | 2016-01-28 20:53:42 +0000 | [diff] [blame] | 3330 | ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1); |
| 3331 | if (!K1) |
| 3332 | return SDValue(); |
| 3333 | |
| 3334 | ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1)); |
| 3335 | if (!K0) |
| 3336 | return SDValue(); |
| 3337 | |
Matt Arsenault | f639c32 | 2016-01-28 20:53:42 +0000 | [diff] [blame] | 3338 | if (Signed) { |
| 3339 | if (K0->getAPIntValue().sge(K1->getAPIntValue())) |
| 3340 | return SDValue(); |
| 3341 | } else { |
| 3342 | if (K0->getAPIntValue().uge(K1->getAPIntValue())) |
| 3343 | return SDValue(); |
| 3344 | } |
| 3345 | |
| 3346 | EVT VT = K0->getValueType(0); |
| 3347 | return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT, |
| 3348 | Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0)); |
| 3349 | } |
| 3350 | |
| 3351 | static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) { |
| 3352 | if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions()) |
| 3353 | return true; |
| 3354 | |
| 3355 | return DAG.isKnownNeverNaN(Op); |
| 3356 | } |
| 3357 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3358 | static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, |
| 3359 | SDValue Op0, SDValue Op1) { |
Matt Arsenault | f639c32 | 2016-01-28 20:53:42 +0000 | [diff] [blame] | 3360 | ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1); |
| 3361 | if (!K1) |
| 3362 | return SDValue(); |
| 3363 | |
| 3364 | ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1)); |
| 3365 | if (!K0) |
| 3366 | return SDValue(); |
| 3367 | |
| 3368 | // Ordered >= (although NaN inputs should have folded away by now). |
| 3369 | APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF()); |
| 3370 | if (Cmp == APFloat::cmpGreaterThan) |
| 3371 | return SDValue(); |
| 3372 | |
| 3373 | // This isn't safe with signaling NaNs because in IEEE mode, min/max on a |
| 3374 | // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then |
| 3375 | // give the other result, which is different from med3 with a NaN input. |
| 3376 | SDValue Var = Op0.getOperand(0); |
| 3377 | if (!isKnownNeverSNan(DAG, Var)) |
| 3378 | return SDValue(); |
| 3379 | |
| 3380 | return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), |
| 3381 | Var, SDValue(K0, 0), SDValue(K1, 0)); |
| 3382 | } |
| 3383 | |
| 3384 | SDValue SITargetLowering::performMinMaxCombine(SDNode *N, |
| 3385 | DAGCombinerInfo &DCI) const { |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3386 | SelectionDAG &DAG = DCI.DAG; |
| 3387 | |
| 3388 | unsigned Opc = N->getOpcode(); |
| 3389 | SDValue Op0 = N->getOperand(0); |
| 3390 | SDValue Op1 = N->getOperand(1); |
| 3391 | |
| 3392 | // Only do this if the inner op has one use since this will just increases |
| 3393 | // register pressure for no benefit. |
| 3394 | |
Matt Arsenault | 5b39b34 | 2016-01-28 20:53:48 +0000 | [diff] [blame] | 3395 | if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) { |
| 3396 | // max(max(a, b), c) -> max3(a, b, c) |
| 3397 | // min(min(a, b), c) -> min3(a, b, c) |
| 3398 | if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { |
| 3399 | SDLoc DL(N); |
| 3400 | return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), |
| 3401 | DL, |
| 3402 | N->getValueType(0), |
| 3403 | Op0.getOperand(0), |
| 3404 | Op0.getOperand(1), |
| 3405 | Op1); |
| 3406 | } |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3407 | |
Matt Arsenault | 5b39b34 | 2016-01-28 20:53:48 +0000 | [diff] [blame] | 3408 | // Try commuted. |
| 3409 | // max(a, max(b, c)) -> max3(a, b, c) |
| 3410 | // min(a, min(b, c)) -> min3(a, b, c) |
| 3411 | if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { |
| 3412 | SDLoc DL(N); |
| 3413 | return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), |
| 3414 | DL, |
| 3415 | N->getValueType(0), |
| 3416 | Op0, |
| 3417 | Op1.getOperand(0), |
| 3418 | Op1.getOperand(1)); |
| 3419 | } |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Matt Arsenault | f639c32 | 2016-01-28 20:53:42 +0000 | [diff] [blame] | 3422 | // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1) |
| 3423 | if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) { |
| 3424 | if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true)) |
| 3425 | return Med3; |
| 3426 | } |
| 3427 | |
| 3428 | if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) { |
| 3429 | if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false)) |
| 3430 | return Med3; |
| 3431 | } |
| 3432 | |
| 3433 | // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1) |
Matt Arsenault | 5b39b34 | 2016-01-28 20:53:48 +0000 | [diff] [blame] | 3434 | if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) || |
| 3435 | (Opc == AMDGPUISD::FMIN_LEGACY && |
| 3436 | Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) && |
Matt Arsenault | f639c32 | 2016-01-28 20:53:42 +0000 | [diff] [blame] | 3437 | N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) { |
| 3438 | if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1)) |
| 3439 | return Res; |
| 3440 | } |
| 3441 | |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3442 | return SDValue(); |
| 3443 | } |
| 3444 | |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 3445 | SDValue SITargetLowering::performSetCCCombine(SDNode *N, |
| 3446 | DAGCombinerInfo &DCI) const { |
| 3447 | SelectionDAG &DAG = DCI.DAG; |
| 3448 | SDLoc SL(N); |
| 3449 | |
| 3450 | SDValue LHS = N->getOperand(0); |
| 3451 | SDValue RHS = N->getOperand(1); |
| 3452 | EVT VT = LHS.getValueType(); |
| 3453 | |
| 3454 | if (VT != MVT::f32 && VT != MVT::f64) |
| 3455 | return SDValue(); |
| 3456 | |
| 3457 | // Match isinf pattern |
| 3458 | // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) |
| 3459 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 3460 | if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { |
| 3461 | const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); |
| 3462 | if (!CRHS) |
| 3463 | return SDValue(); |
| 3464 | |
| 3465 | const APFloat &APF = CRHS->getValueAPF(); |
| 3466 | if (APF.isInfinity() && !APF.isNegative()) { |
| 3467 | unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3468 | return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), |
| 3469 | DAG.getConstant(Mask, SL, MVT::i32)); |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | return SDValue(); |
| 3474 | } |
| 3475 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 3476 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 3477 | DAGCombinerInfo &DCI) const { |
| 3478 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3479 | SDLoc DL(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 3480 | |
| 3481 | switch (N->getOpcode()) { |
Matt Arsenault | 22b4c25 | 2014-12-21 16:48:42 +0000 | [diff] [blame] | 3482 | default: |
| 3483 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 3484 | case ISD::SETCC: |
| 3485 | return performSetCCCombine(N, DCI); |
Matt Arsenault | 5b39b34 | 2016-01-28 20:53:48 +0000 | [diff] [blame] | 3486 | case ISD::FMAXNUM: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3487 | case ISD::FMINNUM: |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 3488 | case ISD::SMAX: |
| 3489 | case ISD::SMIN: |
| 3490 | case ISD::UMAX: |
Matt Arsenault | 5b39b34 | 2016-01-28 20:53:48 +0000 | [diff] [blame] | 3491 | case ISD::UMIN: |
| 3492 | case AMDGPUISD::FMIN_LEGACY: |
| 3493 | case AMDGPUISD::FMAX_LEGACY: { |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3494 | if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && |
Tom Stellard | 7c840bc | 2015-03-16 15:53:55 +0000 | [diff] [blame] | 3495 | N->getValueType(0) != MVT::f64 && |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3496 | getTargetMachine().getOptLevel() > CodeGenOpt::None) |
Matt Arsenault | f639c32 | 2016-01-28 20:53:42 +0000 | [diff] [blame] | 3497 | return performMinMaxCombine(N, DCI); |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 3498 | break; |
| 3499 | } |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 3500 | |
| 3501 | case AMDGPUISD::CVT_F32_UBYTE0: |
| 3502 | case AMDGPUISD::CVT_F32_UBYTE1: |
| 3503 | case AMDGPUISD::CVT_F32_UBYTE2: |
| 3504 | case AMDGPUISD::CVT_F32_UBYTE3: { |
| 3505 | unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 3506 | SDValue Src = N->getOperand(0); |
Matt Arsenault | a949dc6 | 2016-05-09 16:29:50 +0000 | [diff] [blame] | 3507 | |
Matt Arsenault | 327bb5a | 2016-07-01 22:47:50 +0000 | [diff] [blame] | 3508 | // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero. |
Matt Arsenault | a949dc6 | 2016-05-09 16:29:50 +0000 | [diff] [blame] | 3509 | if (Src.getOpcode() == ISD::SRL) { |
| 3510 | // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x |
| 3511 | // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x |
| 3512 | // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x |
| 3513 | |
| 3514 | if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) { |
| 3515 | unsigned SrcOffset = C->getZExtValue() + 8 * Offset; |
| 3516 | if (SrcOffset < 32 && SrcOffset % 8 == 0) { |
| 3517 | return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL, |
| 3518 | MVT::f32, Src.getOperand(0)); |
| 3519 | } |
| 3520 | } |
| 3521 | } |
| 3522 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 3523 | APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); |
| 3524 | |
| 3525 | APInt KnownZero, KnownOne; |
| 3526 | TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), |
| 3527 | !DCI.isBeforeLegalizeOps()); |
| 3528 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 3529 | if (TLO.ShrinkDemandedConstant(Src, Demanded) || |
| 3530 | TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { |
| 3531 | DCI.CommitTargetLoweringOpt(TLO); |
| 3532 | } |
| 3533 | |
| 3534 | break; |
| 3535 | } |
| 3536 | |
| 3537 | case ISD::UINT_TO_FP: { |
| 3538 | return performUCharToFloatCombine(N, DCI); |
Matt Arsenault | de5fbe9 | 2016-01-11 17:02:00 +0000 | [diff] [blame] | 3539 | } |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 3540 | case ISD::FADD: { |
| 3541 | if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) |
| 3542 | break; |
| 3543 | |
| 3544 | EVT VT = N->getValueType(0); |
| 3545 | if (VT != MVT::f32) |
| 3546 | break; |
| 3547 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3548 | // Only do this if we are not trying to support denormals. v_mad_f32 does |
| 3549 | // not support denormals ever. |
| 3550 | if (Subtarget->hasFP32Denormals()) |
| 3551 | break; |
| 3552 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 3553 | SDValue LHS = N->getOperand(0); |
| 3554 | SDValue RHS = N->getOperand(1); |
| 3555 | |
| 3556 | // These should really be instruction patterns, but writing patterns with |
| 3557 | // source modiifiers is a pain. |
| 3558 | |
| 3559 | // fadd (fadd (a, a), b) -> mad 2.0, a, b |
| 3560 | if (LHS.getOpcode() == ISD::FADD) { |
| 3561 | SDValue A = LHS.getOperand(0); |
| 3562 | if (A == LHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3563 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3564 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | // fadd (b, fadd (a, a)) -> mad 2.0, a, b |
| 3569 | if (RHS.getOpcode() == ISD::FADD) { |
| 3570 | SDValue A = RHS.getOperand(0); |
| 3571 | if (A == RHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3572 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3573 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 3574 | } |
| 3575 | } |
| 3576 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3577 | return SDValue(); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 3578 | } |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 3579 | case ISD::FSUB: { |
| 3580 | if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) |
| 3581 | break; |
| 3582 | |
| 3583 | EVT VT = N->getValueType(0); |
| 3584 | |
| 3585 | // Try to get the fneg to fold into the source modifier. This undoes generic |
| 3586 | // DAG combines and folds them into the mad. |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3587 | // |
| 3588 | // Only do this if we are not trying to support denormals. v_mad_f32 does |
| 3589 | // not support denormals ever. |
| 3590 | if (VT == MVT::f32 && |
| 3591 | !Subtarget->hasFP32Denormals()) { |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 3592 | SDValue LHS = N->getOperand(0); |
| 3593 | SDValue RHS = N->getOperand(1); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 3594 | if (LHS.getOpcode() == ISD::FADD) { |
| 3595 | // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) |
| 3596 | |
| 3597 | SDValue A = LHS.getOperand(0); |
| 3598 | if (A == LHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3599 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 3600 | SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS); |
| 3601 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3602 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | if (RHS.getOpcode() == ISD::FADD) { |
| 3607 | // (fsub c, (fadd a, a)) -> mad -2.0, a, c |
| 3608 | |
| 3609 | SDValue A = RHS.getOperand(0); |
| 3610 | if (A == RHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3611 | const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3612 | return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 3613 | } |
| 3614 | } |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 3615 | |
| 3616 | return SDValue(); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | break; |
| 3620 | } |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3621 | case ISD::LOAD: |
| 3622 | case ISD::STORE: |
| 3623 | case ISD::ATOMIC_LOAD: |
| 3624 | case ISD::ATOMIC_STORE: |
| 3625 | case ISD::ATOMIC_CMP_SWAP: |
| 3626 | case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: |
| 3627 | case ISD::ATOMIC_SWAP: |
| 3628 | case ISD::ATOMIC_LOAD_ADD: |
| 3629 | case ISD::ATOMIC_LOAD_SUB: |
| 3630 | case ISD::ATOMIC_LOAD_AND: |
| 3631 | case ISD::ATOMIC_LOAD_OR: |
| 3632 | case ISD::ATOMIC_LOAD_XOR: |
| 3633 | case ISD::ATOMIC_LOAD_NAND: |
| 3634 | case ISD::ATOMIC_LOAD_MIN: |
| 3635 | case ISD::ATOMIC_LOAD_MAX: |
| 3636 | case ISD::ATOMIC_LOAD_UMIN: |
Matt Arsenault | a9dbdca | 2016-04-12 14:05:04 +0000 | [diff] [blame] | 3637 | case ISD::ATOMIC_LOAD_UMAX: |
| 3638 | case AMDGPUISD::ATOMIC_INC: |
| 3639 | case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics. |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3640 | if (DCI.isBeforeLegalize()) |
| 3641 | break; |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 3642 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3643 | MemSDNode *MemNode = cast<MemSDNode>(N); |
| 3644 | SDValue Ptr = MemNode->getBasePtr(); |
| 3645 | |
| 3646 | // TODO: We could also do this for multiplies. |
| 3647 | unsigned AS = MemNode->getAddressSpace(); |
| 3648 | if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { |
| 3649 | SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); |
| 3650 | if (NewPtr) { |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 3651 | SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end()); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3652 | |
| 3653 | NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; |
| 3654 | return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0); |
| 3655 | } |
| 3656 | } |
| 3657 | break; |
| 3658 | } |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 3659 | case ISD::AND: |
| 3660 | return performAndCombine(N, DCI); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3661 | case ISD::OR: |
| 3662 | return performOrCombine(N, DCI); |
Matt Arsenault | fa5f767 | 2016-09-14 15:19:03 +0000 | [diff] [blame] | 3663 | case ISD::XOR: |
| 3664 | return performXorCombine(N, DCI); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 3665 | case AMDGPUISD::FP_CLASS: |
| 3666 | return performClassCombine(N, DCI); |
Matt Arsenault | 9cd9071 | 2016-04-14 01:42:16 +0000 | [diff] [blame] | 3667 | case ISD::FCANONICALIZE: |
| 3668 | return performFCanonicalizeCombine(N, DCI); |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 3669 | case AMDGPUISD::FRACT: |
| 3670 | case AMDGPUISD::RCP: |
| 3671 | case AMDGPUISD::RSQ: |
Matt Arsenault | 32fc527 | 2016-07-26 16:45:45 +0000 | [diff] [blame] | 3672 | case AMDGPUISD::RCP_LEGACY: |
Matt Arsenault | b6d8c37 | 2016-06-20 18:33:56 +0000 | [diff] [blame] | 3673 | case AMDGPUISD::RSQ_LEGACY: |
| 3674 | case AMDGPUISD::RSQ_CLAMP: |
| 3675 | case AMDGPUISD::LDEXP: { |
| 3676 | SDValue Src = N->getOperand(0); |
| 3677 | if (Src.isUndef()) |
| 3678 | return Src; |
| 3679 | break; |
| 3680 | } |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 3681 | } |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 3682 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 3683 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 3684 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3685 | /// \brief Helper function for adjustWritemask |
Benjamin Kramer | 635e368 | 2013-05-23 15:43:05 +0000 | [diff] [blame] | 3686 | static unsigned SubIdx2Lane(unsigned Idx) { |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3687 | switch (Idx) { |
| 3688 | default: return 0; |
| 3689 | case AMDGPU::sub0: return 0; |
| 3690 | case AMDGPU::sub1: return 1; |
| 3691 | case AMDGPU::sub2: return 2; |
| 3692 | case AMDGPU::sub3: return 3; |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | /// \brief Adjust the writemask of MIMG instructions |
| 3697 | void SITargetLowering::adjustWritemask(MachineSDNode *&Node, |
| 3698 | SelectionDAG &DAG) const { |
| 3699 | SDNode *Users[4] = { }; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3700 | unsigned Lane = 0; |
Nikolay Haustov | 2f684f1 | 2016-02-26 09:51:05 +0000 | [diff] [blame] | 3701 | unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3; |
| 3702 | unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx); |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3703 | unsigned NewDmask = 0; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3704 | |
| 3705 | // Try to figure out the used register components |
| 3706 | for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); |
| 3707 | I != E; ++I) { |
| 3708 | |
| 3709 | // Abort if we can't understand the usage |
| 3710 | if (!I->isMachineOpcode() || |
| 3711 | I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) |
| 3712 | return; |
| 3713 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3714 | // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. |
| 3715 | // Note that subregs are packed, i.e. Lane==0 is the first bit set |
| 3716 | // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit |
| 3717 | // set, etc. |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3718 | Lane = SubIdx2Lane(I->getConstantOperandVal(1)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3719 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3720 | // Set which texture component corresponds to the lane. |
| 3721 | unsigned Comp; |
| 3722 | for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { |
| 3723 | assert(Dmask); |
Tom Stellard | 03a5c08 | 2013-10-23 03:50:25 +0000 | [diff] [blame] | 3724 | Comp = countTrailingZeros(Dmask); |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3725 | Dmask &= ~(1 << Comp); |
| 3726 | } |
| 3727 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3728 | // Abort if we have more than one user per component |
| 3729 | if (Users[Lane]) |
| 3730 | return; |
| 3731 | |
| 3732 | Users[Lane] = *I; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3733 | NewDmask |= 1 << Comp; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3736 | // Abort if there's no change |
| 3737 | if (NewDmask == OldDmask) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3738 | return; |
| 3739 | |
| 3740 | // Adjust the writemask in the node |
| 3741 | std::vector<SDValue> Ops; |
Nikolay Haustov | 2f684f1 | 2016-02-26 09:51:05 +0000 | [diff] [blame] | 3742 | Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3743 | Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); |
Nikolay Haustov | 2f684f1 | 2016-02-26 09:51:05 +0000 | [diff] [blame] | 3744 | Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end()); |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 3745 | Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3746 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3747 | // If we only got one lane, replace it with a copy |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 3748 | // (if NewDmask has only one bit set...) |
| 3749 | if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3750 | SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), |
| 3751 | MVT::i32); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3752 | SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3753 | SDLoc(), Users[Lane]->getValueType(0), |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3754 | SDValue(Node, 0), RC); |
| 3755 | DAG.ReplaceAllUsesWith(Users[Lane], Copy); |
| 3756 | return; |
| 3757 | } |
| 3758 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3759 | // Update the users of the node with the new indices |
| 3760 | for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { |
| 3761 | |
| 3762 | SDNode *User = Users[i]; |
| 3763 | if (!User) |
| 3764 | continue; |
| 3765 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3766 | SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3767 | DAG.UpdateNodeOperands(User, User->getOperand(0), Op); |
| 3768 | |
| 3769 | switch (Idx) { |
| 3770 | default: break; |
| 3771 | case AMDGPU::sub0: Idx = AMDGPU::sub1; break; |
| 3772 | case AMDGPU::sub1: Idx = AMDGPU::sub2; break; |
| 3773 | case AMDGPU::sub2: Idx = AMDGPU::sub3; break; |
| 3774 | } |
| 3775 | } |
| 3776 | } |
| 3777 | |
Tom Stellard | c98ee20 | 2015-07-16 19:40:07 +0000 | [diff] [blame] | 3778 | static bool isFrameIndexOp(SDValue Op) { |
| 3779 | if (Op.getOpcode() == ISD::AssertZext) |
| 3780 | Op = Op.getOperand(0); |
| 3781 | |
| 3782 | return isa<FrameIndexSDNode>(Op); |
| 3783 | } |
| 3784 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 3785 | /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) |
| 3786 | /// with frame index operands. |
| 3787 | /// LLVM assumes that inputs are to these instructions are registers. |
| 3788 | void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, |
| 3789 | SelectionDAG &DAG) const { |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 3790 | |
| 3791 | SmallVector<SDValue, 8> Ops; |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 3792 | for (unsigned i = 0; i < Node->getNumOperands(); ++i) { |
Tom Stellard | c98ee20 | 2015-07-16 19:40:07 +0000 | [diff] [blame] | 3793 | if (!isFrameIndexOp(Node->getOperand(i))) { |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 3794 | Ops.push_back(Node->getOperand(i)); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 3795 | continue; |
| 3796 | } |
| 3797 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 3798 | SDLoc DL(Node); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 3799 | Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 3800 | Node->getOperand(i).getValueType(), |
| 3801 | Node->getOperand(i)), 0)); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 3804 | DAG.UpdateNodeOperands(Node, Ops); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 3807 | /// \brief Fold the instructions after selecting them. |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3808 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 3809 | SelectionDAG &DAG) const { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 3810 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
Nicolai Haehnle | f2c64db | 2016-02-18 16:44:18 +0000 | [diff] [blame] | 3811 | unsigned Opcode = Node->getMachineOpcode(); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3812 | |
Nicolai Haehnle | c06bfa1 | 2016-07-11 21:59:43 +0000 | [diff] [blame] | 3813 | if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() && |
| 3814 | !TII->isGather4(Opcode)) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3815 | adjustWritemask(Node, DAG); |
| 3816 | |
Nicolai Haehnle | f2c64db | 2016-02-18 16:44:18 +0000 | [diff] [blame] | 3817 | if (Opcode == AMDGPU::INSERT_SUBREG || |
| 3818 | Opcode == AMDGPU::REG_SEQUENCE) { |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 3819 | legalizeTargetIndependentNode(Node, DAG); |
| 3820 | return Node; |
| 3821 | } |
Tom Stellard | 654d669 | 2015-01-08 15:08:17 +0000 | [diff] [blame] | 3822 | return Node; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 3823 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3824 | |
| 3825 | /// \brief Assign the register class depending on the number of |
| 3826 | /// bits set in the writemask |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3827 | void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3828 | SDNode *Node) const { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 3829 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3830 | |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3831 | MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); |
Matt Arsenault | 6005fcb | 2015-10-21 21:51:02 +0000 | [diff] [blame] | 3832 | |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3833 | if (TII->isVOP3(MI.getOpcode())) { |
Matt Arsenault | 6005fcb | 2015-10-21 21:51:02 +0000 | [diff] [blame] | 3834 | // Make sure constant bus requirements are respected. |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3835 | TII->legalizeOperandsVOP3(MRI, MI); |
Matt Arsenault | 6005fcb | 2015-10-21 21:51:02 +0000 | [diff] [blame] | 3836 | return; |
| 3837 | } |
Matt Arsenault | cb0ac3d | 2014-09-26 17:54:59 +0000 | [diff] [blame] | 3838 | |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3839 | if (TII->isMIMG(MI)) { |
| 3840 | unsigned VReg = MI.getOperand(0).getReg(); |
| 3841 | unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4; |
| 3842 | unsigned Writemask = MI.getOperand(DmaskIdx).getImm(); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3843 | unsigned BitsSet = 0; |
| 3844 | for (unsigned i = 0; i < 4; ++i) |
| 3845 | BitsSet += Writemask & (1 << i) ? 1 : 0; |
| 3846 | |
| 3847 | const TargetRegisterClass *RC; |
| 3848 | switch (BitsSet) { |
| 3849 | default: return; |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 3850 | case 1: RC = &AMDGPU::VGPR_32RegClass; break; |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3851 | case 2: RC = &AMDGPU::VReg_64RegClass; break; |
| 3852 | case 3: RC = &AMDGPU::VReg_96RegClass; break; |
| 3853 | } |
| 3854 | |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3855 | unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet); |
| 3856 | MI.setDesc(TII->get(NewOpcode)); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3857 | MRI.setRegClass(VReg, RC); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3858 | return; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3859 | } |
| 3860 | |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3861 | // Replace unused atomics with the no return version. |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3862 | int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode()); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3863 | if (NoRetAtomicOp != -1) { |
| 3864 | if (!Node->hasAnyUseOfValue(0)) { |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3865 | MI.setDesc(TII->get(NoRetAtomicOp)); |
| 3866 | MI.RemoveOperand(0); |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 3867 | return; |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 3870 | // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg |
| 3871 | // instruction, because the return type of these instructions is a vec2 of |
| 3872 | // the memory type, so it can be tied to the input operand. |
| 3873 | // This means these instructions always have a use, so we need to add a |
| 3874 | // special case to check if the atomic has only one extract_subreg use, |
| 3875 | // which itself has no uses. |
| 3876 | if ((Node->hasNUsesOfValue(1, 0) && |
Nicolai Haehnle | 750082d | 2016-04-15 14:42:36 +0000 | [diff] [blame] | 3877 | Node->use_begin()->isMachineOpcode() && |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 3878 | Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG && |
| 3879 | !Node->use_begin()->hasAnyUseOfValue(0))) { |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3880 | unsigned Def = MI.getOperand(0).getReg(); |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 3881 | |
| 3882 | // Change this into a noret atomic. |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3883 | MI.setDesc(TII->get(NoRetAtomicOp)); |
| 3884 | MI.RemoveOperand(0); |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 3885 | |
| 3886 | // If we only remove the def operand from the atomic instruction, the |
| 3887 | // extract_subreg will be left with a use of a vreg without a def. |
| 3888 | // So we need to insert an implicit_def to avoid machine verifier |
| 3889 | // errors. |
Duncan P. N. Exon Smith | e4f5e4f | 2016-06-30 22:52:52 +0000 | [diff] [blame] | 3890 | BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), |
Tom Stellard | 354a43c | 2016-04-01 18:27:37 +0000 | [diff] [blame] | 3891 | TII->get(AMDGPU::IMPLICIT_DEF), Def); |
| 3892 | } |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 3893 | return; |
| 3894 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 3895 | } |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 3896 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3897 | static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, |
| 3898 | uint64_t Val) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3899 | SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3900 | return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); |
| 3901 | } |
| 3902 | |
| 3903 | MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3904 | const SDLoc &DL, |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3905 | SDValue Ptr) const { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 3906 | const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3907 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 3908 | // Build the half of the subregister with the constants before building the |
| 3909 | // full 128-bit register. If we are building multiple resource descriptors, |
| 3910 | // this will allow CSEing of the 2-component register. |
| 3911 | const SDValue Ops0[] = { |
| 3912 | DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), |
| 3913 | buildSMovImm32(DAG, DL, 0), |
| 3914 | DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
| 3915 | buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), |
| 3916 | DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) |
| 3917 | }; |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3918 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 3919 | SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, |
| 3920 | MVT::v2i32, Ops0), 0); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3921 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 3922 | // Combine the constants and the pointer. |
| 3923 | const SDValue Ops1[] = { |
| 3924 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), |
| 3925 | Ptr, |
| 3926 | DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), |
| 3927 | SubRegHi, |
| 3928 | DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) |
| 3929 | }; |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3930 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 3931 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3934 | /// \brief Return a resource descriptor with the 'Add TID' bit enabled |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 3935 | /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] |
| 3936 | /// of the resource descriptor) to create an offset, which is added to |
| 3937 | /// the resource pointer. |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 3938 | MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL, |
| 3939 | SDValue Ptr, uint32_t RsrcDword1, |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3940 | uint64_t RsrcDword2And3) const { |
| 3941 | SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); |
| 3942 | SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); |
| 3943 | if (RsrcDword1) { |
| 3944 | PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3945 | DAG.getConstant(RsrcDword1, DL, MVT::i32)), |
| 3946 | 0); |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
| 3949 | SDValue DataLo = buildSMovImm32(DAG, DL, |
| 3950 | RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); |
| 3951 | SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); |
| 3952 | |
| 3953 | const SDValue Ops[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3954 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3955 | PtrLo, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3956 | DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3957 | PtrHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3958 | DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3959 | DataLo, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3960 | DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3961 | DataHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3962 | DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 3963 | }; |
| 3964 | |
| 3965 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); |
| 3966 | } |
| 3967 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 3968 | SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, |
| 3969 | const TargetRegisterClass *RC, |
| 3970 | unsigned Reg, EVT VT) const { |
| 3971 | SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); |
| 3972 | |
| 3973 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), |
| 3974 | cast<RegisterSDNode>(VReg)->getReg(), VT); |
| 3975 | } |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 3976 | |
| 3977 | //===----------------------------------------------------------------------===// |
| 3978 | // SI Inline Assembly Support |
| 3979 | //===----------------------------------------------------------------------===// |
| 3980 | |
| 3981 | std::pair<unsigned, const TargetRegisterClass *> |
| 3982 | SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 3983 | StringRef Constraint, |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 3984 | MVT VT) const { |
Tom Stellard | b3c3bda | 2015-12-10 02:12:53 +0000 | [diff] [blame] | 3985 | |
| 3986 | if (Constraint.size() == 1) { |
| 3987 | switch (Constraint[0]) { |
| 3988 | case 's': |
| 3989 | case 'r': |
| 3990 | switch (VT.getSizeInBits()) { |
| 3991 | default: |
| 3992 | return std::make_pair(0U, nullptr); |
| 3993 | case 32: |
Matt Arsenault | a609e2d | 2016-08-30 20:50:08 +0000 | [diff] [blame] | 3994 | return std::make_pair(0U, &AMDGPU::SReg_32RegClass); |
Tom Stellard | b3c3bda | 2015-12-10 02:12:53 +0000 | [diff] [blame] | 3995 | case 64: |
| 3996 | return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); |
| 3997 | case 128: |
| 3998 | return std::make_pair(0U, &AMDGPU::SReg_128RegClass); |
| 3999 | case 256: |
| 4000 | return std::make_pair(0U, &AMDGPU::SReg_256RegClass); |
| 4001 | } |
| 4002 | |
| 4003 | case 'v': |
| 4004 | switch (VT.getSizeInBits()) { |
| 4005 | default: |
| 4006 | return std::make_pair(0U, nullptr); |
| 4007 | case 32: |
| 4008 | return std::make_pair(0U, &AMDGPU::VGPR_32RegClass); |
| 4009 | case 64: |
| 4010 | return std::make_pair(0U, &AMDGPU::VReg_64RegClass); |
| 4011 | case 96: |
| 4012 | return std::make_pair(0U, &AMDGPU::VReg_96RegClass); |
| 4013 | case 128: |
| 4014 | return std::make_pair(0U, &AMDGPU::VReg_128RegClass); |
| 4015 | case 256: |
| 4016 | return std::make_pair(0U, &AMDGPU::VReg_256RegClass); |
| 4017 | case 512: |
| 4018 | return std::make_pair(0U, &AMDGPU::VReg_512RegClass); |
| 4019 | } |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | if (Constraint.size() > 1) { |
| 4024 | const TargetRegisterClass *RC = nullptr; |
| 4025 | if (Constraint[1] == 'v') { |
| 4026 | RC = &AMDGPU::VGPR_32RegClass; |
| 4027 | } else if (Constraint[1] == 's') { |
| 4028 | RC = &AMDGPU::SGPR_32RegClass; |
| 4029 | } |
| 4030 | |
| 4031 | if (RC) { |
Matt Arsenault | 0b554ed | 2015-06-23 02:05:55 +0000 | [diff] [blame] | 4032 | uint32_t Idx; |
| 4033 | bool Failed = Constraint.substr(2).getAsInteger(10, Idx); |
| 4034 | if (!Failed && Idx < RC->getNumRegs()) |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 4035 | return std::make_pair(RC->getRegister(Idx), RC); |
| 4036 | } |
| 4037 | } |
| 4038 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
| 4039 | } |
Tom Stellard | b3c3bda | 2015-12-10 02:12:53 +0000 | [diff] [blame] | 4040 | |
| 4041 | SITargetLowering::ConstraintType |
| 4042 | SITargetLowering::getConstraintType(StringRef Constraint) const { |
| 4043 | if (Constraint.size() == 1) { |
| 4044 | switch (Constraint[0]) { |
| 4045 | default: break; |
| 4046 | case 's': |
| 4047 | case 'v': |
| 4048 | return C_RegisterClass; |
| 4049 | } |
| 4050 | } |
| 4051 | return TargetLowering::getConstraintType(Constraint); |
| 4052 | } |