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