Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief Custom DAG lowering for SI |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SIISelLowering.h" |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 16 | #include "AMDGPU.h" |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 17 | #include "AMDGPUIntrinsicInfo.h" |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 18 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 19 | #include "SIInstrInfo.h" |
| 20 | #include "SIMachineFunctionInfo.h" |
| 21 | #include "SIRegisterInfo.h" |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/CallingConvLower.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | SITargetLowering::SITargetLowering(TargetMachine &TM) : |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 32 | AMDGPUTargetLowering(TM) { |
Tom Stellard | 1bd8072 | 2014-04-30 15:31:33 +0000 | [diff] [blame] | 33 | addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 34 | addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 35 | |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 36 | addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass); |
| 37 | addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass); |
| 38 | |
Tom Stellard | 334b29c | 2014-04-17 21:00:09 +0000 | [diff] [blame] | 39 | addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 40 | addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 41 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 42 | addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); |
| 43 | addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); |
| 44 | addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 45 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 46 | addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); |
| 47 | addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 48 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 49 | addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 50 | addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); |
| 51 | |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 52 | addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 53 | addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 54 | |
| 55 | computeRegisterProperties(); |
| 56 | |
Tom Stellard | c084533 | 2013-11-22 23:07:58 +0000 | [diff] [blame] | 57 | // Condition Codes |
| 58 | setCondCodeAction(ISD::SETONE, MVT::f32, Expand); |
| 59 | setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); |
| 60 | setCondCodeAction(ISD::SETUGE, MVT::f32, Expand); |
| 61 | setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); |
| 62 | setCondCodeAction(ISD::SETULE, MVT::f32, Expand); |
| 63 | setCondCodeAction(ISD::SETULT, MVT::f32, Expand); |
| 64 | |
| 65 | setCondCodeAction(ISD::SETONE, MVT::f64, Expand); |
| 66 | setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand); |
| 67 | setCondCodeAction(ISD::SETUGE, MVT::f64, Expand); |
| 68 | setCondCodeAction(ISD::SETUGT, MVT::f64, Expand); |
| 69 | setCondCodeAction(ISD::SETULE, MVT::f64, Expand); |
| 70 | setCondCodeAction(ISD::SETULT, MVT::f64, Expand); |
| 71 | |
Christian Konig | 2989ffc | 2013-03-18 11:34:16 +0000 | [diff] [blame] | 72 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); |
| 73 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); |
| 74 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); |
| 75 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); |
| 76 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 77 | setOperationAction(ISD::ADD, MVT::i32, Legal); |
Matt Arsenault | e8d2146 | 2013-11-18 20:09:40 +0000 | [diff] [blame] | 78 | setOperationAction(ISD::ADDC, MVT::i32, Legal); |
| 79 | setOperationAction(ISD::ADDE, MVT::i32, Legal); |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 80 | setOperationAction(ISD::SUBC, MVT::i32, Legal); |
| 81 | setOperationAction(ISD::SUBE, MVT::i32, Legal); |
Aaron Watry | daabb20 | 2013-06-25 13:55:52 +0000 | [diff] [blame] | 82 | |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 83 | // We need to custom lower vector stores from local memory |
| 84 | setOperationAction(ISD::LOAD, MVT::v2i32, Custom); |
| 85 | setOperationAction(ISD::LOAD, MVT::v4i32, Custom); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 86 | setOperationAction(ISD::LOAD, MVT::v8i32, Custom); |
| 87 | setOperationAction(ISD::LOAD, MVT::v16i32, Custom); |
| 88 | |
| 89 | setOperationAction(ISD::STORE, MVT::v8i32, Custom); |
| 90 | setOperationAction(ISD::STORE, MVT::v16i32, Custom); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 91 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 92 | // We need to custom lower loads/stores from private memory |
| 93 | setOperationAction(ISD::LOAD, MVT::i32, Custom); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::LOAD, MVT::v2i32, Custom); |
| 95 | setOperationAction(ISD::LOAD, MVT::v4i32, Custom); |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 96 | setOperationAction(ISD::LOAD, MVT::v8i32, Custom); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 97 | |
Tom Stellard | 1c8788e | 2014-03-07 20:12:33 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::STORE, MVT::i1, Custom); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 99 | setOperationAction(ISD::STORE, MVT::i32, Custom); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 100 | setOperationAction(ISD::STORE, MVT::v2i32, Custom); |
| 101 | setOperationAction(ISD::STORE, MVT::v4i32, Custom); |
| 102 | |
Tom Stellard | f719ee9 | 2014-05-16 20:56:41 +0000 | [diff] [blame] | 103 | setOperationAction(ISD::SELECT, MVT::f32, Promote); |
| 104 | AddPromotedToType(ISD::SELECT, MVT::f32, MVT::i32); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 105 | setOperationAction(ISD::SELECT, MVT::i64, Custom); |
Tom Stellard | da99c6e | 2014-03-24 16:07:30 +0000 | [diff] [blame] | 106 | setOperationAction(ISD::SELECT, MVT::f64, Promote); |
| 107 | AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 108 | |
Tom Stellard | 3ca1bfc | 2014-06-10 16:01:22 +0000 | [diff] [blame] | 109 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 110 | setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); |
| 111 | setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); |
| 112 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 113 | |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 114 | setOperationAction(ISD::SETCC, MVT::v2i1, Expand); |
| 115 | setOperationAction(ISD::SETCC, MVT::v4i1, Expand); |
| 116 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 117 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); |
| 119 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); |
| 120 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 121 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 122 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); |
| 123 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); |
| 124 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 125 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 126 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); |
| 127 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); |
| 128 | |
| 129 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom); |
| 130 | |
| 131 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); |
| 132 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 133 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 134 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); |
| 135 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom); |
| 136 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 137 | |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 138 | setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); |
Matt Arsenault | e54e1c3 | 2014-06-23 18:00:44 +0000 | [diff] [blame] | 139 | setOperationAction(ISD::BRCOND, MVT::Other, Custom); |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 140 | |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 141 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 142 | setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom); |
| 143 | setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom); |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 144 | setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 145 | setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand); |
| 146 | setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand); |
Tom Stellard | 31209cc | 2013-07-15 19:00:09 +0000 | [diff] [blame] | 147 | |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 148 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); |
| 149 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom); |
| 150 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom); |
| 151 | setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand); |
| 152 | |
| 153 | setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 154 | setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom); |
| 155 | setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom); |
| 156 | setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand); |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 157 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 158 | |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 159 | setTruncStoreAction(MVT::i32, MVT::i8, Custom); |
| 160 | setTruncStoreAction(MVT::i32, MVT::i16, Custom); |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 161 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Matt Arsenault | 6f24379 | 2013-09-05 19:41:10 +0000 | [diff] [blame] | 162 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 163 | setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); |
| 164 | setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 165 | |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 166 | setOperationAction(ISD::LOAD, MVT::i1, Custom); |
| 167 | |
Tom Stellard | fd15582 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 168 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Tom Stellard | 04c0e98 | 2014-01-22 19:24:21 +0000 | [diff] [blame] | 169 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Matt Arsenault | a98cd6a | 2013-12-19 05:32:55 +0000 | [diff] [blame] | 170 | setOperationAction(ISD::FrameIndex, MVT::i32, Custom); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 171 | |
Tom Stellard | 5f33788 | 2014-04-29 23:12:43 +0000 | [diff] [blame] | 172 | // These should use UDIVREM, so set them to expand |
| 173 | setOperationAction(ISD::UDIV, MVT::i64, Expand); |
| 174 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
| 175 | |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 176 | // We only support LOAD/STORE and vector manipulation ops for vectors |
| 177 | // with > 4 elements. |
| 178 | MVT VecTypes[] = { |
Tom Stellard | d61a1c3 | 2014-02-28 21:36:37 +0000 | [diff] [blame] | 179 | MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32 |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Matt Arsenault | d504a74 | 2014-05-15 21:44:05 +0000 | [diff] [blame] | 182 | for (MVT VT : VecTypes) { |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 183 | for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { |
| 184 | switch(Op) { |
| 185 | case ISD::LOAD: |
| 186 | case ISD::STORE: |
| 187 | case ISD::BUILD_VECTOR: |
| 188 | case ISD::BITCAST: |
| 189 | case ISD::EXTRACT_VECTOR_ELT: |
| 190 | case ISD::INSERT_VECTOR_ELT: |
| 191 | case ISD::CONCAT_VECTORS: |
| 192 | case ISD::INSERT_SUBVECTOR: |
| 193 | case ISD::EXTRACT_SUBVECTOR: |
| 194 | break; |
| 195 | default: |
Matt Arsenault | d504a74 | 2014-05-15 21:44:05 +0000 | [diff] [blame] | 196 | setOperationAction(Op, VT, Expand); |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 202 | for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) { |
| 203 | MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I); |
Matt Arsenault | a81aee8 | 2014-02-24 21:16:50 +0000 | [diff] [blame] | 204 | setOperationAction(ISD::FTRUNC, VT, Expand); |
| 205 | setOperationAction(ISD::FCEIL, VT, Expand); |
| 206 | setOperationAction(ISD::FFLOOR, VT, Expand); |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 207 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 208 | |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 209 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) { |
| 210 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
| 211 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
| 212 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
Matt Arsenault | a90d22f | 2014-04-17 17:06:37 +0000 | [diff] [blame] | 213 | setOperationAction(ISD::FRINT, MVT::f64, Legal); |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Matt Arsenault | e54e1c3 | 2014-06-23 18:00:44 +0000 | [diff] [blame] | 216 | // FIXME: These should be removed and handled the same was as f32 fneg. Source |
Matt Arsenault | 7aeb813 | 2014-06-18 17:05:22 +0000 | [diff] [blame] | 217 | // modifiers also work for the double instructions. |
| 218 | setOperationAction(ISD::FNEG, MVT::f64, Expand); |
Matt Arsenault | e54e1c3 | 2014-06-23 18:00:44 +0000 | [diff] [blame] | 219 | setOperationAction(ISD::FABS, MVT::f64, Expand); |
Matt Arsenault | 7aeb813 | 2014-06-18 17:05:22 +0000 | [diff] [blame] | 220 | |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 221 | setTargetDAGCombine(ISD::SELECT_CC); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 222 | setTargetDAGCombine(ISD::SETCC); |
Michel Danzer | f52a672 | 2013-03-08 10:58:01 +0000 | [diff] [blame] | 223 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 224 | setTargetDAGCombine(ISD::UINT_TO_FP); |
| 225 | |
Christian Konig | eecebd0 | 2013-03-26 14:04:02 +0000 | [diff] [blame] | 226 | setSchedulingPreference(Sched::RegPressure); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
| 230 | // TargetLowering queries |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | |
| 233 | bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT, |
Matt Arsenault | 25793a3 | 2014-02-05 23:15:53 +0000 | [diff] [blame] | 234 | unsigned AddrSpace, |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 235 | bool *IsFast) const { |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 236 | if (IsFast) |
| 237 | *IsFast = false; |
| 238 | |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 239 | // XXX: This depends on the address space and also we may want to revist |
| 240 | // the alignment values we specify in the DataLayout. |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 241 | |
| 242 | // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, |
| 243 | // which isn't a simple VT. |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 244 | if (!VT.isSimple() || VT == MVT::Other) |
| 245 | return false; |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 246 | |
| 247 | // XXX - CI changes say "Support for unaligned memory accesses" but I don't |
| 248 | // see what for specifically. The wording everywhere else seems to be the |
| 249 | // same. |
| 250 | |
| 251 | // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have |
| 252 | // no alignment restrictions. |
| 253 | if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) { |
| 254 | // Using any pair of GPRs should be the same as any other pair. |
| 255 | if (IsFast) |
| 256 | *IsFast = true; |
| 257 | return VT.bitsGE(MVT::i64); |
| 258 | } |
| 259 | |
| 260 | // XXX - The only mention I see of this in the ISA manual is for LDS direct |
| 261 | // reads the "byte address and must be dword aligned". Is it also true for the |
| 262 | // normal loads and stores? |
| 263 | if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) |
| 264 | return false; |
| 265 | |
| 266 | // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the |
| 267 | // byte-address are ignored, thus forcing Dword alignment. |
| 268 | if (IsFast) |
| 269 | *IsFast = true; |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 270 | return VT.bitsGT(MVT::i32); |
| 271 | } |
| 272 | |
Matt Arsenault | f751d62 | 2014-03-31 20:54:58 +0000 | [diff] [blame] | 273 | bool SITargetLowering::shouldSplitVectorType(EVT VT) const { |
| 274 | return VT.getScalarType().bitsLE(MVT::i16); |
Tom Stellard | d86003e | 2013-08-14 23:25:00 +0000 | [diff] [blame] | 275 | } |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 276 | |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 277 | bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, |
| 278 | Type *Ty) const { |
| 279 | const SIInstrInfo *TII = |
| 280 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 281 | return TII->isInlineConstant(Imm); |
| 282 | } |
| 283 | |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 284 | SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 285 | SDLoc DL, SDValue Chain, |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 286 | unsigned Offset, bool Signed) const { |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 287 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 288 | PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()), |
| 289 | AMDGPUAS::CONSTANT_ADDRESS); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 290 | SDValue BasePtr = DAG.getCopyFromReg(Chain, DL, |
| 291 | MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64); |
| 292 | SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr, |
| 293 | DAG.getConstant(Offset, MVT::i64)); |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 294 | return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr, |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 295 | MachinePointerInfo(UndefValue::get(PtrTy)), MemVT, |
| 296 | false, false, MemVT.getSizeInBits() >> 3); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 297 | |
| 298 | } |
| 299 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 300 | SDValue SITargetLowering::LowerFormalArguments( |
| 301 | SDValue Chain, |
| 302 | CallingConv::ID CallConv, |
| 303 | bool isVarArg, |
| 304 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 305 | SDLoc DL, SelectionDAG &DAG, |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 306 | SmallVectorImpl<SDValue> &InVals) const { |
| 307 | |
| 308 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 309 | |
| 310 | MachineFunction &MF = DAG.getMachineFunction(); |
| 311 | FunctionType *FType = MF.getFunction()->getFunctionType(); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 312 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 313 | |
| 314 | assert(CallConv == CallingConv::C); |
| 315 | |
| 316 | SmallVector<ISD::InputArg, 16> Splits; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 317 | uint32_t Skipped = 0; |
| 318 | |
| 319 | for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 320 | const ISD::InputArg &Arg = Ins[i]; |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 321 | |
| 322 | // First check if it's a PS input addr |
Vincent Lejeune | d623644 | 2013-10-13 17:56:16 +0000 | [diff] [blame] | 323 | if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() && |
| 324 | !Arg.Flags.isByVal()) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 325 | |
| 326 | assert((PSInputNum <= 15) && "Too many PS inputs!"); |
| 327 | |
| 328 | if (!Arg.Used) { |
| 329 | // We can savely skip PS inputs |
| 330 | Skipped |= 1 << i; |
| 331 | ++PSInputNum; |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | Info->PSInputAddr |= 1 << PSInputNum++; |
| 336 | } |
| 337 | |
| 338 | // Second split vertices into their elements |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 339 | if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 340 | ISD::InputArg NewArg = Arg; |
| 341 | NewArg.Flags.setSplit(); |
| 342 | NewArg.VT = Arg.VT.getVectorElementType(); |
| 343 | |
| 344 | // We REALLY want the ORIGINAL number of vertex elements here, e.g. a |
| 345 | // three or five element vertex only needs three or five registers, |
| 346 | // NOT four or eigth. |
| 347 | Type *ParamType = FType->getParamType(Arg.OrigArgIndex); |
| 348 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 349 | |
| 350 | for (unsigned j = 0; j != NumElements; ++j) { |
| 351 | Splits.push_back(NewArg); |
| 352 | NewArg.PartOffset += NewArg.VT.getStoreSize(); |
| 353 | } |
| 354 | |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 355 | } else if (Info->ShaderType != ShaderType::COMPUTE) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 356 | Splits.push_back(Arg); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | SmallVector<CCValAssign, 16> ArgLocs; |
| 361 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| 362 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
| 363 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 364 | // At least one interpolation mode must be enabled or else the GPU will hang. |
| 365 | if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) { |
| 366 | Info->PSInputAddr |= 1; |
| 367 | CCInfo.AllocateReg(AMDGPU::VGPR0); |
| 368 | CCInfo.AllocateReg(AMDGPU::VGPR1); |
| 369 | } |
| 370 | |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 371 | // The pointer to the list of arguments is stored in SGPR0, SGPR1 |
| 372 | if (Info->ShaderType == ShaderType::COMPUTE) { |
| 373 | CCInfo.AllocateReg(AMDGPU::SGPR0); |
| 374 | CCInfo.AllocateReg(AMDGPU::SGPR1); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 375 | MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 378 | if (Info->ShaderType == ShaderType::COMPUTE) { |
| 379 | getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins, |
| 380 | Splits); |
| 381 | } |
| 382 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 383 | AnalyzeFormalArguments(CCInfo, Splits); |
| 384 | |
| 385 | for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { |
| 386 | |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 387 | const ISD::InputArg &Arg = Ins[i]; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 388 | if (Skipped & (1 << i)) { |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 389 | InVals.push_back(DAG.getUNDEF(Arg.VT)); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 390 | continue; |
| 391 | } |
| 392 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 393 | CCValAssign &VA = ArgLocs[ArgIdx++]; |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 394 | EVT VT = VA.getLocVT(); |
| 395 | |
| 396 | if (VA.isMemLoc()) { |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 397 | VT = Ins[i].VT; |
| 398 | EVT MemVT = Splits[i].VT; |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 399 | // The first 36 bytes of the input buffer contains information about |
| 400 | // thread group and global sizes. |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 401 | SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(), |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 402 | 36 + VA.getLocMemOffset(), |
| 403 | Ins[i].Flags.isSExt()); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 404 | InVals.push_back(Arg); |
| 405 | continue; |
| 406 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 407 | assert(VA.isRegLoc() && "Parameter must be in a register!"); |
| 408 | |
| 409 | unsigned Reg = VA.getLocReg(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 410 | |
| 411 | if (VT == MVT::i64) { |
| 412 | // For now assume it is a pointer |
| 413 | Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, |
| 414 | &AMDGPU::SReg_64RegClass); |
| 415 | Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); |
| 416 | InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); |
| 421 | |
| 422 | Reg = MF.addLiveIn(Reg, RC); |
| 423 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 424 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 425 | if (Arg.VT.isVector()) { |
| 426 | |
| 427 | // Build a vector from the registers |
| 428 | Type *ParamType = FType->getParamType(Arg.OrigArgIndex); |
| 429 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 430 | |
| 431 | SmallVector<SDValue, 4> Regs; |
| 432 | Regs.push_back(Val); |
| 433 | for (unsigned j = 1; j != NumElements; ++j) { |
| 434 | Reg = ArgLocs[ArgIdx++].getLocReg(); |
| 435 | Reg = MF.addLiveIn(Reg, RC); |
| 436 | Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 437 | } |
| 438 | |
| 439 | // Fill up the missing vector elements |
| 440 | NumElements = Arg.VT.getVectorNumElements() - NumElements; |
| 441 | for (unsigned j = 0; j != NumElements; ++j) |
| 442 | Regs.push_back(DAG.getUNDEF(VT)); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 443 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 444 | InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs)); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 445 | continue; |
| 446 | } |
| 447 | |
| 448 | InVals.push_back(Val); |
| 449 | } |
| 450 | return Chain; |
| 451 | } |
| 452 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 453 | MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( |
| 454 | MachineInstr * MI, MachineBasicBlock * BB) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 455 | |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 456 | MachineBasicBlock::iterator I = *MI; |
Tom Stellard | 919bb6b | 2014-04-29 23:12:53 +0000 | [diff] [blame] | 457 | const SIInstrInfo *TII = |
| 458 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 459 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 460 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 461 | switch (MI->getOpcode()) { |
| 462 | default: |
| 463 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
| 464 | case AMDGPU::BRANCH: return BB; |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 465 | case AMDGPU::SI_ADDR64_RSRC: { |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 466 | unsigned SuperReg = MI->getOperand(0).getReg(); |
Tom Stellard | def38c5 | 2014-03-21 15:51:53 +0000 | [diff] [blame] | 467 | unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass); |
| 468 | unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass); |
| 469 | unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
| 470 | unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 471 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo) |
| 472 | .addOperand(MI->getOperand(1)); |
| 473 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo) |
| 474 | .addImm(0); |
| 475 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi) |
Tom Stellard | 1583409 | 2014-03-21 15:51:57 +0000 | [diff] [blame] | 476 | .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32); |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 477 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi) |
| 478 | .addReg(SubRegHiLo) |
| 479 | .addImm(AMDGPU::sub0) |
| 480 | .addReg(SubRegHiHi) |
| 481 | .addImm(AMDGPU::sub1); |
| 482 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg) |
| 483 | .addReg(SubRegLo) |
| 484 | .addImm(AMDGPU::sub0_sub1) |
| 485 | .addReg(SubRegHi) |
| 486 | .addImm(AMDGPU::sub2_sub3); |
| 487 | MI->eraseFromParent(); |
| 488 | break; |
| 489 | } |
Matt Arsenault | dbc9aae | 2014-06-18 17:13:51 +0000 | [diff] [blame] | 490 | case AMDGPU::V_SUB_F64: { |
| 491 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 492 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg) |
| 493 | .addImm(0) // SRC0 modifiers |
| 494 | .addReg(MI->getOperand(1).getReg()) |
| 495 | .addImm(1) // SRC1 modifiers |
| 496 | .addReg(MI->getOperand(2).getReg()) |
| 497 | .addImm(0) // SRC2 modifiers |
| 498 | .addImm(0) // src2 |
| 499 | .addImm(0) // CLAMP |
| 500 | .addImm(0); // OMOD |
Tom Stellard | 2a6a6105 | 2013-07-12 18:15:08 +0000 | [diff] [blame] | 501 | MI->eraseFromParent(); |
| 502 | break; |
Matt Arsenault | dbc9aae | 2014-06-18 17:13:51 +0000 | [diff] [blame] | 503 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 504 | case AMDGPU::SI_RegisterStorePseudo: { |
| 505 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 506 | unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 507 | MachineInstrBuilder MIB = |
| 508 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore), |
| 509 | Reg); |
| 510 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) |
| 511 | MIB.addOperand(MI->getOperand(i)); |
| 512 | |
| 513 | MI->eraseFromParent(); |
Vincent Lejeune | 79a5834 | 2014-05-10 19:18:25 +0000 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | case AMDGPU::FABS_SI: { |
| 517 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
| 518 | const SIInstrInfo *TII = |
| 519 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 520 | unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 521 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32), |
| 522 | Reg) |
| 523 | .addImm(0x7fffffff); |
| 524 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32), |
| 525 | MI->getOperand(0).getReg()) |
| 526 | .addReg(MI->getOperand(1).getReg()) |
| 527 | .addReg(Reg); |
| 528 | MI->eraseFromParent(); |
| 529 | break; |
| 530 | } |
| 531 | case AMDGPU::FNEG_SI: { |
| 532 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
| 533 | const SIInstrInfo *TII = |
| 534 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 535 | unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
| 536 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32), |
| 537 | Reg) |
| 538 | .addImm(0x80000000); |
| 539 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32), |
| 540 | MI->getOperand(0).getReg()) |
| 541 | .addReg(MI->getOperand(1).getReg()) |
| 542 | .addReg(Reg); |
| 543 | MI->eraseFromParent(); |
| 544 | break; |
| 545 | } |
| 546 | case AMDGPU::FCLAMP_SI: { |
| 547 | const SIInstrInfo *TII = |
| 548 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 549 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64), |
| 550 | MI->getOperand(0).getReg()) |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 551 | .addImm(0) // SRC0 modifiers |
Vincent Lejeune | 79a5834 | 2014-05-10 19:18:25 +0000 | [diff] [blame] | 552 | .addOperand(MI->getOperand(1)) |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 553 | .addImm(0) // SRC1 modifiers |
Vincent Lejeune | 79a5834 | 2014-05-10 19:18:25 +0000 | [diff] [blame] | 554 | .addImm(0) // SRC1 |
Vincent Lejeune | 79a5834 | 2014-05-10 19:18:25 +0000 | [diff] [blame] | 555 | .addImm(1) // CLAMP |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 556 | .addImm(0); // OMOD |
Vincent Lejeune | 79a5834 | 2014-05-10 19:18:25 +0000 | [diff] [blame] | 557 | MI->eraseFromParent(); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 558 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 559 | } |
| 560 | return BB; |
| 561 | } |
| 562 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 563 | EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 564 | if (!VT.isVector()) { |
| 565 | return MVT::i1; |
| 566 | } |
| 567 | return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Christian Konig | 082a14a | 2013-03-18 11:34:05 +0000 | [diff] [blame] | 570 | MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const { |
| 571 | return MVT::i32; |
| 572 | } |
| 573 | |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 574 | bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 575 | VT = VT.getScalarType(); |
| 576 | |
| 577 | if (!VT.isSimple()) |
| 578 | return false; |
| 579 | |
| 580 | switch (VT.getSimpleVT().SimpleTy) { |
| 581 | case MVT::f32: |
| 582 | return false; /* There is V_MAD_F32 for f32 */ |
| 583 | case MVT::f64: |
| 584 | return true; |
| 585 | default: |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | return false; |
| 590 | } |
| 591 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 592 | //===----------------------------------------------------------------------===// |
| 593 | // Custom DAG Lowering Operations |
| 594 | //===----------------------------------------------------------------------===// |
| 595 | |
| 596 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 597 | MachineFunction &MF = DAG.getMachineFunction(); |
| 598 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 599 | switch (Op.getOpcode()) { |
| 600 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 601 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 602 | case ISD::LOAD: { |
| 603 | LoadSDNode *Load = dyn_cast<LoadSDNode>(Op); |
Tom Stellard | 10ae6a0 | 2014-07-02 20:53:54 +0000 | [diff] [blame^] | 604 | EVT VT = Op.getValueType(); |
| 605 | |
| 606 | // These loads are legal. |
| 607 | if (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && |
| 608 | VT.isVector() && VT.getVectorNumElements() == 2 && |
| 609 | VT.getVectorElementType() == MVT::i32) |
| 610 | return SDValue(); |
| 611 | |
Tom Stellard | 80be965 | 2014-02-13 23:34:10 +0000 | [diff] [blame] | 612 | if (Op.getValueType().isVector() && |
| 613 | (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || |
| 614 | Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS || |
| 615 | (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && |
| 616 | Op.getValueType().getVectorNumElements() > 4))) { |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 617 | SDValue MergedValues[2] = { |
| 618 | SplitVectorLoad(Op, DAG), |
| 619 | Load->getChain() |
| 620 | }; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 621 | return DAG.getMergeValues(MergedValues, SDLoc(Op)); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 622 | } else { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 623 | return LowerLOAD(Op, DAG); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 626 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 627 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 628 | case ISD::STORE: return LowerSTORE(Op, DAG); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 629 | case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 630 | case ISD::INTRINSIC_WO_CHAIN: { |
| 631 | unsigned IntrinsicID = |
| 632 | cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 633 | EVT VT = Op.getValueType(); |
| 634 | SDLoc DL(Op); |
| 635 | //XXX: Hardcoded we only use two to store the pointer to the parameters. |
| 636 | unsigned NumUserSGPRs = 2; |
| 637 | switch (IntrinsicID) { |
| 638 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
| 639 | case Intrinsic::r600_read_ngroups_x: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 640 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 641 | case Intrinsic::r600_read_ngroups_y: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 642 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 643 | case Intrinsic::r600_read_ngroups_z: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 644 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 645 | case Intrinsic::r600_read_global_size_x: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 646 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 647 | case Intrinsic::r600_read_global_size_y: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 648 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 649 | case Intrinsic::r600_read_global_size_z: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 650 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 651 | case Intrinsic::r600_read_local_size_x: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 652 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 653 | case Intrinsic::r600_read_local_size_y: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 654 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 655 | case Intrinsic::r600_read_local_size_z: |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 656 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 657 | case Intrinsic::r600_read_tgid_x: |
| 658 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
| 659 | AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT); |
| 660 | case Intrinsic::r600_read_tgid_y: |
| 661 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
| 662 | AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT); |
| 663 | case Intrinsic::r600_read_tgid_z: |
| 664 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
| 665 | AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT); |
| 666 | case Intrinsic::r600_read_tidig_x: |
| 667 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 668 | AMDGPU::VGPR0, VT); |
| 669 | case Intrinsic::r600_read_tidig_y: |
| 670 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 671 | AMDGPU::VGPR1, VT); |
| 672 | case Intrinsic::r600_read_tidig_z: |
| 673 | return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass, |
| 674 | AMDGPU::VGPR2, VT); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 675 | case AMDGPUIntrinsic::SI_load_const: { |
| 676 | SDValue Ops [] = { |
Tom Stellard | 868fd92 | 2014-04-17 21:00:11 +0000 | [diff] [blame] | 677 | Op.getOperand(1), |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 678 | Op.getOperand(2) |
| 679 | }; |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 680 | |
Benjamin Kramer | a8eecee | 2013-08-16 14:48:09 +0000 | [diff] [blame] | 681 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 682 | MachinePointerInfo(), |
| 683 | MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, |
| 684 | VT.getSizeInBits() / 8, 4); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 685 | return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, |
Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 686 | Op->getVTList(), Ops, VT, MMO); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 687 | } |
| 688 | case AMDGPUIntrinsic::SI_sample: |
| 689 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG); |
| 690 | case AMDGPUIntrinsic::SI_sampleb: |
| 691 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG); |
| 692 | case AMDGPUIntrinsic::SI_sampled: |
| 693 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG); |
| 694 | case AMDGPUIntrinsic::SI_samplel: |
| 695 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG); |
| 696 | case AMDGPUIntrinsic::SI_vs_load_input: |
| 697 | return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, |
Tom Stellard | 868fd92 | 2014-04-17 21:00:11 +0000 | [diff] [blame] | 698 | Op.getOperand(1), |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 699 | Op.getOperand(2), |
| 700 | Op.getOperand(3)); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 703 | |
| 704 | case ISD::INTRINSIC_VOID: |
| 705 | SDValue Chain = Op.getOperand(0); |
| 706 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 707 | |
| 708 | switch (IntrinsicID) { |
| 709 | case AMDGPUIntrinsic::SI_tbuffer_store: { |
| 710 | SDLoc DL(Op); |
| 711 | SDValue Ops [] = { |
| 712 | Chain, |
Tom Stellard | 868fd92 | 2014-04-17 21:00:11 +0000 | [diff] [blame] | 713 | Op.getOperand(2), |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 714 | Op.getOperand(3), |
| 715 | Op.getOperand(4), |
| 716 | Op.getOperand(5), |
| 717 | Op.getOperand(6), |
| 718 | Op.getOperand(7), |
| 719 | Op.getOperand(8), |
| 720 | Op.getOperand(9), |
| 721 | Op.getOperand(10), |
| 722 | Op.getOperand(11), |
| 723 | Op.getOperand(12), |
| 724 | Op.getOperand(13), |
| 725 | Op.getOperand(14) |
| 726 | }; |
| 727 | EVT VT = Op.getOperand(3).getValueType(); |
| 728 | |
| 729 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 730 | MachinePointerInfo(), |
| 731 | MachineMemOperand::MOStore, |
| 732 | VT.getSizeInBits() / 8, 4); |
| 733 | return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, |
Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 734 | Op->getVTList(), Ops, VT, MMO); |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 735 | } |
| 736 | default: |
| 737 | break; |
| 738 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 739 | } |
| 740 | return SDValue(); |
| 741 | } |
| 742 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 743 | /// \brief Helper function for LowerBRCOND |
| 744 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 745 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 746 | SDNode *Parent = Value.getNode(); |
| 747 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 748 | I != E; ++I) { |
| 749 | |
| 750 | if (I.getUse().get() != Value) |
| 751 | continue; |
| 752 | |
| 753 | if (I->getOpcode() == Opcode) |
| 754 | return *I; |
| 755 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 756 | return nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /// This transforms the control flow intrinsics to get the branch destination as |
| 760 | /// last parameter, also switches branch target with BR if the need arise |
| 761 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 762 | SelectionDAG &DAG) const { |
| 763 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 764 | SDLoc DL(BRCOND); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 765 | |
| 766 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 767 | SDValue Target = BRCOND.getOperand(2); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 768 | SDNode *BR = nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 769 | |
| 770 | if (Intr->getOpcode() == ISD::SETCC) { |
| 771 | // As long as we negate the condition everything is fine |
| 772 | SDNode *SetCC = Intr; |
| 773 | assert(SetCC->getConstantOperandVal(1) == 1); |
NAKAMURA Takumi | 458a827 | 2013-01-07 11:14:44 +0000 | [diff] [blame] | 774 | assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 775 | ISD::SETNE); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 776 | Intr = SetCC->getOperand(0).getNode(); |
| 777 | |
| 778 | } else { |
| 779 | // Get the target from BR if we don't negate the condition |
| 780 | BR = findUser(BRCOND, ISD::BR); |
| 781 | Target = BR->getOperand(1); |
| 782 | } |
| 783 | |
| 784 | assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); |
| 785 | |
| 786 | // Build the result and |
| 787 | SmallVector<EVT, 4> Res; |
| 788 | for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i) |
| 789 | Res.push_back(Intr->getValueType(i)); |
| 790 | |
| 791 | // operands of the new intrinsic call |
| 792 | SmallVector<SDValue, 4> Ops; |
| 793 | Ops.push_back(BRCOND.getOperand(0)); |
| 794 | for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i) |
| 795 | Ops.push_back(Intr->getOperand(i)); |
| 796 | Ops.push_back(Target); |
| 797 | |
| 798 | // build the new intrinsic call |
| 799 | SDNode *Result = DAG.getNode( |
| 800 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 801 | DAG.getVTList(Res), Ops).getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 802 | |
| 803 | if (BR) { |
| 804 | // Give the branch instruction our target |
| 805 | SDValue Ops[] = { |
| 806 | BR->getOperand(0), |
| 807 | BRCOND.getOperand(2) |
| 808 | }; |
Craig Topper | 131de82 | 2014-04-27 19:21:16 +0000 | [diff] [blame] | 809 | DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 813 | |
| 814 | // Copy the intrinsic results to registers |
| 815 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 816 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 817 | if (!CopyToReg) |
| 818 | continue; |
| 819 | |
| 820 | Chain = DAG.getCopyToReg( |
| 821 | Chain, DL, |
| 822 | CopyToReg->getOperand(1), |
| 823 | SDValue(Result, i - 1), |
| 824 | SDValue()); |
| 825 | |
| 826 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 827 | } |
| 828 | |
| 829 | // Remove the old intrinsic from the chain |
| 830 | DAG.ReplaceAllUsesOfValueWith( |
| 831 | SDValue(Intr, Intr->getNumValues() - 1), |
| 832 | Intr->getOperand(0)); |
| 833 | |
| 834 | return Chain; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 837 | SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
| 838 | SDLoc DL(Op); |
| 839 | LoadSDNode *Load = cast<LoadSDNode>(Op); |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 840 | SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG); |
| 841 | SDValue MergedValues[2]; |
| 842 | MergedValues[1] = Load->getChain(); |
| 843 | if (Ret.getNode()) { |
| 844 | MergedValues[0] = Ret; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 845 | return DAG.getMergeValues(MergedValues, DL); |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 846 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 847 | |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 848 | if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 849 | return SDValue(); |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 850 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 851 | |
Matt Arsenault | ad41d7b | 2014-03-24 17:50:46 +0000 | [diff] [blame] | 852 | EVT MemVT = Load->getMemoryVT(); |
| 853 | |
| 854 | assert(!MemVT.isVector() && "Private loads should be scalarized"); |
| 855 | assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int"); |
| 856 | |
Matt Arsenault | a98cd6a | 2013-12-19 05:32:55 +0000 | [diff] [blame] | 857 | SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(), |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 858 | DAG.getConstant(2, MVT::i32)); |
Matt Arsenault | ad41d7b | 2014-03-24 17:50:46 +0000 | [diff] [blame] | 859 | Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32, |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 860 | Load->getChain(), Ptr, |
| 861 | DAG.getTargetConstant(0, MVT::i32), |
| 862 | Op.getOperand(2)); |
Matt Arsenault | ad41d7b | 2014-03-24 17:50:46 +0000 | [diff] [blame] | 863 | if (MemVT.getSizeInBits() == 64) { |
| 864 | SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr, |
| 865 | DAG.getConstant(1, MVT::i32)); |
| 866 | |
| 867 | SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32, |
| 868 | Load->getChain(), IncPtr, |
| 869 | DAG.getTargetConstant(0, MVT::i32), |
| 870 | Op.getOperand(2)); |
| 871 | |
| 872 | Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper); |
| 873 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 874 | |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 875 | MergedValues[0] = Ret; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 876 | return DAG.getMergeValues(MergedValues, DL); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 877 | |
| 878 | } |
| 879 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 880 | SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode, |
| 881 | const SDValue &Op, |
| 882 | SelectionDAG &DAG) const { |
| 883 | return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1), |
| 884 | Op.getOperand(2), |
Tom Stellard | 868fd92 | 2014-04-17 21:00:11 +0000 | [diff] [blame] | 885 | Op.getOperand(3), |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 886 | Op.getOperand(4)); |
| 887 | } |
| 888 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 889 | SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { |
| 890 | if (Op.getValueType() != MVT::i64) |
| 891 | return SDValue(); |
| 892 | |
| 893 | SDLoc DL(Op); |
| 894 | SDValue Cond = Op.getOperand(0); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 895 | |
| 896 | SDValue Zero = DAG.getConstant(0, MVT::i32); |
| 897 | SDValue One = DAG.getConstant(1, MVT::i32); |
| 898 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 899 | SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); |
| 900 | SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); |
| 901 | |
| 902 | SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); |
| 903 | 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] | 904 | |
| 905 | SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); |
| 906 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 907 | SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); |
| 908 | 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] | 909 | |
| 910 | SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); |
| 911 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 912 | SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi); |
| 913 | return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 916 | SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { |
| 917 | SDLoc DL(Op); |
| 918 | StoreSDNode *Store = cast<StoreSDNode>(Op); |
| 919 | EVT VT = Store->getMemoryVT(); |
| 920 | |
Tom Stellard | 9b3816b | 2014-06-24 23:33:04 +0000 | [diff] [blame] | 921 | // These stores are legal. |
| 922 | if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && |
| 923 | VT.isVector() && VT.getVectorNumElements() == 2 && |
| 924 | VT.getVectorElementType() == MVT::i32) |
| 925 | return SDValue(); |
| 926 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 927 | SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG); |
| 928 | if (Ret.getNode()) |
| 929 | return Ret; |
| 930 | |
| 931 | if (VT.isVector() && VT.getVectorNumElements() >= 8) |
| 932 | return SplitVectorStore(Op, DAG); |
| 933 | |
Tom Stellard | 1c8788e | 2014-03-07 20:12:33 +0000 | [diff] [blame] | 934 | if (VT == MVT::i1) |
| 935 | return DAG.getTruncStore(Store->getChain(), DL, |
| 936 | DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), |
| 937 | Store->getBasePtr(), MVT::i1, Store->getMemOperand()); |
| 938 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 939 | if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) |
| 940 | return SDValue(); |
| 941 | |
Matt Arsenault | a98cd6a | 2013-12-19 05:32:55 +0000 | [diff] [blame] | 942 | SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(), |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 943 | DAG.getConstant(2, MVT::i32)); |
| 944 | SDValue Chain = Store->getChain(); |
| 945 | SmallVector<SDValue, 8> Values; |
| 946 | |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 947 | if (Store->isTruncatingStore()) { |
| 948 | unsigned Mask = 0; |
| 949 | if (Store->getMemoryVT() == MVT::i8) { |
| 950 | Mask = 0xff; |
| 951 | } else if (Store->getMemoryVT() == MVT::i16) { |
| 952 | Mask = 0xffff; |
| 953 | } |
| 954 | SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32, |
| 955 | Chain, Store->getBasePtr(), |
| 956 | DAG.getConstant(0, MVT::i32)); |
| 957 | SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(), |
| 958 | DAG.getConstant(0x3, MVT::i32)); |
| 959 | SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx, |
| 960 | DAG.getConstant(3, MVT::i32)); |
| 961 | SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(), |
| 962 | DAG.getConstant(Mask, MVT::i32)); |
| 963 | SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32, |
| 964 | MaskedValue, ShiftAmt); |
| 965 | SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32, |
| 966 | DAG.getConstant(32, MVT::i32), ShiftAmt); |
| 967 | SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32, |
| 968 | DAG.getConstant(Mask, MVT::i32), |
| 969 | RotrAmt); |
| 970 | Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask); |
| 971 | Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue); |
| 972 | |
| 973 | Values.push_back(Dst); |
| 974 | } else if (VT == MVT::i64) { |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 975 | for (unsigned i = 0; i < 2; ++i) { |
| 976 | Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, |
| 977 | Store->getValue(), DAG.getConstant(i, MVT::i32))); |
| 978 | } |
| 979 | } else if (VT == MVT::i128) { |
| 980 | for (unsigned i = 0; i < 2; ++i) { |
| 981 | for (unsigned j = 0; j < 2; ++j) { |
| 982 | Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, |
| 983 | DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, |
| 984 | Store->getValue(), DAG.getConstant(i, MVT::i32)), |
| 985 | DAG.getConstant(j, MVT::i32))); |
| 986 | } |
| 987 | } |
| 988 | } else { |
| 989 | Values.push_back(Store->getValue()); |
| 990 | } |
| 991 | |
| 992 | for (unsigned i = 0; i < Values.size(); ++i) { |
| 993 | SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, |
| 994 | Ptr, DAG.getConstant(i, MVT::i32)); |
| 995 | Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, |
| 996 | Chain, Values[i], PartPtr, |
| 997 | DAG.getTargetConstant(0, MVT::i32)); |
| 998 | } |
| 999 | return Chain; |
| 1000 | } |
| 1001 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1002 | //===----------------------------------------------------------------------===// |
| 1003 | // Custom DAG optimizations |
| 1004 | //===----------------------------------------------------------------------===// |
| 1005 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1006 | SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, |
| 1007 | DAGCombinerInfo &DCI) { |
| 1008 | EVT VT = N->getValueType(0); |
| 1009 | EVT ScalarVT = VT.getScalarType(); |
| 1010 | if (ScalarVT != MVT::f32) |
| 1011 | return SDValue(); |
| 1012 | |
| 1013 | SelectionDAG &DAG = DCI.DAG; |
| 1014 | SDLoc DL(N); |
| 1015 | |
| 1016 | SDValue Src = N->getOperand(0); |
| 1017 | EVT SrcVT = Src.getValueType(); |
| 1018 | |
| 1019 | // TODO: We could try to match extracting the higher bytes, which would be |
| 1020 | // easier if i8 vectors weren't promoted to i32 vectors, particularly after |
| 1021 | // types are legalized. v4i8 -> v4f32 is probably the only case to worry |
| 1022 | // about in practice. |
| 1023 | if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { |
| 1024 | if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { |
| 1025 | SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); |
| 1026 | DCI.AddToWorklist(Cvt.getNode()); |
| 1027 | return Cvt; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | // We are primarily trying to catch operations on illegal vector types |
| 1032 | // before they are expanded. |
| 1033 | // For scalars, we can use the more flexible method of checking masked bits |
| 1034 | // after legalization. |
| 1035 | if (!DCI.isBeforeLegalize() || |
| 1036 | !SrcVT.isVector() || |
| 1037 | SrcVT.getVectorElementType() != MVT::i8) { |
| 1038 | return SDValue(); |
| 1039 | } |
| 1040 | |
| 1041 | assert(DCI.isBeforeLegalize() && "Unexpected legal type"); |
| 1042 | |
| 1043 | // Weird sized vectors are a pain to handle, but we know 3 is really the same |
| 1044 | // size as 4. |
| 1045 | unsigned NElts = SrcVT.getVectorNumElements(); |
| 1046 | if (!SrcVT.isSimple() && NElts != 3) |
| 1047 | return SDValue(); |
| 1048 | |
| 1049 | // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to |
| 1050 | // prevent a mess from expanding to v4i32 and repacking. |
| 1051 | if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) { |
| 1052 | EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT); |
| 1053 | EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT); |
| 1054 | EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts); |
| 1055 | |
| 1056 | LoadSDNode *Load = cast<LoadSDNode>(Src); |
| 1057 | SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT, |
| 1058 | Load->getChain(), |
| 1059 | Load->getBasePtr(), |
| 1060 | LoadVT, |
| 1061 | Load->getMemOperand()); |
| 1062 | |
| 1063 | // Make sure successors of the original load stay after it by updating |
| 1064 | // them to use the new Chain. |
| 1065 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1)); |
| 1066 | |
| 1067 | SmallVector<SDValue, 4> Elts; |
| 1068 | if (RegVT.isVector()) |
| 1069 | DAG.ExtractVectorElements(NewLoad, Elts); |
| 1070 | else |
| 1071 | Elts.push_back(NewLoad); |
| 1072 | |
| 1073 | SmallVector<SDValue, 4> Ops; |
| 1074 | |
| 1075 | unsigned EltIdx = 0; |
| 1076 | for (SDValue Elt : Elts) { |
| 1077 | unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx); |
| 1078 | for (unsigned I = 0; I < ComponentsInElt; ++I) { |
| 1079 | unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I; |
| 1080 | SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt); |
| 1081 | DCI.AddToWorklist(Cvt.getNode()); |
| 1082 | Ops.push_back(Cvt); |
| 1083 | } |
| 1084 | |
| 1085 | ++EltIdx; |
| 1086 | } |
| 1087 | |
| 1088 | assert(Ops.size() == NElts); |
| 1089 | |
| 1090 | return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops); |
| 1091 | } |
| 1092 | |
| 1093 | return SDValue(); |
| 1094 | } |
| 1095 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1096 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 1097 | DAGCombinerInfo &DCI) const { |
| 1098 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1099 | SDLoc DL(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1100 | EVT VT = N->getValueType(0); |
| 1101 | |
| 1102 | switch (N->getOpcode()) { |
Tom Stellard | 50122a5 | 2014-04-07 19:45:41 +0000 | [diff] [blame] | 1103 | default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1104 | case ISD::SELECT_CC: { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1105 | ConstantSDNode *True, *False; |
| 1106 | // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc) |
| 1107 | if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 1108 | && (False = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 1109 | && True->isAllOnesValue() |
| 1110 | && False->isNullValue() |
| 1111 | && VT == MVT::i1) { |
| 1112 | return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0), |
| 1113 | N->getOperand(1), N->getOperand(4)); |
| 1114 | |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | case ISD::SETCC: { |
| 1119 | SDValue Arg0 = N->getOperand(0); |
| 1120 | SDValue Arg1 = N->getOperand(1); |
| 1121 | SDValue CC = N->getOperand(2); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1122 | ConstantSDNode * C = nullptr; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1123 | ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get(); |
| 1124 | |
| 1125 | // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne) |
| 1126 | if (VT == MVT::i1 |
| 1127 | && Arg0.getOpcode() == ISD::SIGN_EXTEND |
| 1128 | && Arg0.getOperand(0).getValueType() == MVT::i1 |
| 1129 | && (C = dyn_cast<ConstantSDNode>(Arg1)) |
| 1130 | && C->isNullValue() |
| 1131 | && CCOp == ISD::SETNE) { |
| 1132 | return SimplifySetCC(VT, Arg0.getOperand(0), |
| 1133 | DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL); |
| 1134 | } |
| 1135 | break; |
| 1136 | } |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1137 | |
| 1138 | case AMDGPUISD::CVT_F32_UBYTE0: |
| 1139 | case AMDGPUISD::CVT_F32_UBYTE1: |
| 1140 | case AMDGPUISD::CVT_F32_UBYTE2: |
| 1141 | case AMDGPUISD::CVT_F32_UBYTE3: { |
| 1142 | unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; |
| 1143 | |
| 1144 | SDValue Src = N->getOperand(0); |
| 1145 | APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); |
| 1146 | |
| 1147 | APInt KnownZero, KnownOne; |
| 1148 | TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), |
| 1149 | !DCI.isBeforeLegalizeOps()); |
| 1150 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 1151 | if (TLO.ShrinkDemandedConstant(Src, Demanded) || |
| 1152 | TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { |
| 1153 | DCI.CommitTargetLoweringOpt(TLO); |
| 1154 | } |
| 1155 | |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | case ISD::UINT_TO_FP: { |
| 1160 | return performUCharToFloatCombine(N, DCI); |
| 1161 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1162 | } |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 1163 | |
| 1164 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1165 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1166 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1167 | /// \brief Test if RegClass is one of the VSrc classes |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1168 | static bool isVSrc(unsigned RegClass) { |
| 1169 | return AMDGPU::VSrc_32RegClassID == RegClass || |
| 1170 | AMDGPU::VSrc_64RegClassID == RegClass; |
| 1171 | } |
| 1172 | |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1173 | /// \brief Test if RegClass is one of the SSrc classes |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1174 | static bool isSSrc(unsigned RegClass) { |
| 1175 | return AMDGPU::SSrc_32RegClassID == RegClass || |
| 1176 | AMDGPU::SSrc_64RegClassID == RegClass; |
| 1177 | } |
| 1178 | |
| 1179 | /// \brief Analyze the possible immediate value Op |
| 1180 | /// |
| 1181 | /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate |
| 1182 | /// and the immediate value if it's a literal immediate |
| 1183 | int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { |
| 1184 | |
| 1185 | union { |
| 1186 | int32_t I; |
| 1187 | float F; |
| 1188 | } Imm; |
| 1189 | |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 1190 | if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) { |
| 1191 | if (Node->getZExtValue() >> 32) { |
| 1192 | return -1; |
| 1193 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1194 | Imm.I = Node->getSExtValue(); |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 1195 | } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) { |
| 1196 | if (N->getValueType(0) != MVT::f32) |
| 1197 | return -1; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1198 | Imm.F = Node->getValueAPF().convertToFloat(); |
Tom Stellard | 7ed0b52 | 2014-04-03 20:19:27 +0000 | [diff] [blame] | 1199 | } else |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1200 | return -1; // It isn't an immediate |
| 1201 | |
| 1202 | if ((Imm.I >= -16 && Imm.I <= 64) || |
| 1203 | Imm.F == 0.5f || Imm.F == -0.5f || |
| 1204 | Imm.F == 1.0f || Imm.F == -1.0f || |
| 1205 | Imm.F == 2.0f || Imm.F == -2.0f || |
| 1206 | Imm.F == 4.0f || Imm.F == -4.0f) |
| 1207 | return 0; // It's an inline immediate |
| 1208 | |
| 1209 | return Imm.I; // It's a literal immediate |
| 1210 | } |
| 1211 | |
| 1212 | /// \brief Try to fold an immediate directly into an instruction |
| 1213 | bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate, |
| 1214 | bool &ScalarSlotUsed) const { |
| 1215 | |
| 1216 | MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand); |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 1217 | const SIInstrInfo *TII = |
| 1218 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1219 | if (!Mov || !TII->isMov(Mov->getMachineOpcode())) |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1220 | return false; |
| 1221 | |
| 1222 | const SDValue &Op = Mov->getOperand(0); |
| 1223 | int32_t Value = analyzeImmediate(Op.getNode()); |
| 1224 | if (Value == -1) { |
| 1225 | // Not an immediate at all |
| 1226 | return false; |
| 1227 | |
| 1228 | } else if (Value == 0) { |
| 1229 | // Inline immediates can always be fold |
| 1230 | Operand = Op; |
| 1231 | return true; |
| 1232 | |
| 1233 | } else if (Value == Immediate) { |
| 1234 | // Already fold literal immediate |
| 1235 | Operand = Op; |
| 1236 | return true; |
| 1237 | |
| 1238 | } else if (!ScalarSlotUsed && !Immediate) { |
| 1239 | // Fold this literal immediate |
| 1240 | ScalarSlotUsed = true; |
| 1241 | Immediate = Value; |
| 1242 | Operand = Op; |
| 1243 | return true; |
| 1244 | |
| 1245 | } |
| 1246 | |
| 1247 | return false; |
| 1248 | } |
| 1249 | |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 1250 | const TargetRegisterClass *SITargetLowering::getRegClassForNode( |
| 1251 | SelectionDAG &DAG, const SDValue &Op) const { |
| 1252 | const SIInstrInfo *TII = |
| 1253 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 1254 | const SIRegisterInfo &TRI = TII->getRegisterInfo(); |
| 1255 | |
| 1256 | if (!Op->isMachineOpcode()) { |
| 1257 | switch(Op->getOpcode()) { |
| 1258 | case ISD::CopyFromReg: { |
| 1259 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 1260 | unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg(); |
| 1261 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 1262 | return MRI.getRegClass(Reg); |
| 1263 | } |
| 1264 | return TRI.getPhysRegClass(Reg); |
| 1265 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1266 | default: return nullptr; |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode()); |
| 1270 | int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass; |
| 1271 | if (OpClassID != -1) { |
| 1272 | return TRI.getRegClass(OpClassID); |
| 1273 | } |
| 1274 | switch(Op.getMachineOpcode()) { |
| 1275 | case AMDGPU::COPY_TO_REGCLASS: |
| 1276 | // Operand 1 is the register class id for COPY_TO_REGCLASS instructions. |
| 1277 | OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue(); |
| 1278 | |
| 1279 | // If the COPY_TO_REGCLASS instruction is copying to a VSrc register |
| 1280 | // class, then the register class for the value could be either a |
| 1281 | // VReg or and SReg. In order to get a more accurate |
| 1282 | if (OpClassID == AMDGPU::VSrc_32RegClassID || |
| 1283 | OpClassID == AMDGPU::VSrc_64RegClassID) { |
| 1284 | return getRegClassForNode(DAG, Op.getOperand(0)); |
| 1285 | } |
| 1286 | return TRI.getRegClass(OpClassID); |
| 1287 | case AMDGPU::EXTRACT_SUBREG: { |
| 1288 | int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 1289 | const TargetRegisterClass *SuperClass = |
| 1290 | getRegClassForNode(DAG, Op.getOperand(0)); |
| 1291 | return TRI.getSubClassWithSubReg(SuperClass, SubIdx); |
| 1292 | } |
| 1293 | case AMDGPU::REG_SEQUENCE: |
| 1294 | // Operand 0 is the register class id for REG_SEQUENCE instructions. |
| 1295 | return TRI.getRegClass( |
| 1296 | cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()); |
| 1297 | default: |
| 1298 | return getRegClassFor(Op.getSimpleValueType()); |
| 1299 | } |
| 1300 | } |
| 1301 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1302 | /// \brief Does "Op" fit into register class "RegClass" ? |
Tom Stellard | b35efba | 2013-05-20 15:02:01 +0000 | [diff] [blame] | 1303 | bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op, |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1304 | unsigned RegClass) const { |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 1305 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 1306 | const TargetRegisterClass *RC = getRegClassForNode(DAG, Op); |
| 1307 | if (!RC) { |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1308 | return false; |
Tom Stellard | 4c0ffcc | 2013-08-06 23:08:18 +0000 | [diff] [blame] | 1309 | } |
| 1310 | return TRI->getRegClass(RegClass)->hasSubClassEq(RC); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /// \brief Make sure that we don't exeed the number of allowed scalars |
| 1314 | void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand, |
| 1315 | unsigned RegClass, |
| 1316 | bool &ScalarSlotUsed) const { |
| 1317 | |
| 1318 | // First map the operands register class to a destination class |
| 1319 | if (RegClass == AMDGPU::VSrc_32RegClassID) |
| 1320 | RegClass = AMDGPU::VReg_32RegClassID; |
| 1321 | else if (RegClass == AMDGPU::VSrc_64RegClassID) |
| 1322 | RegClass = AMDGPU::VReg_64RegClassID; |
| 1323 | else |
| 1324 | return; |
| 1325 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1326 | // Nothing to do if they fit naturally |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1327 | if (fitsRegClass(DAG, Operand, RegClass)) |
| 1328 | return; |
| 1329 | |
| 1330 | // If the scalar slot isn't used yet use it now |
| 1331 | if (!ScalarSlotUsed) { |
| 1332 | ScalarSlotUsed = true; |
| 1333 | return; |
| 1334 | } |
| 1335 | |
Matt Arsenault | 1408b60 | 2013-10-10 23:05:37 +0000 | [diff] [blame] | 1336 | // This is a conservative aproach. It is possible that we can't determine the |
| 1337 | // correct register class and copy too often, but better safe than sorry. |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1338 | SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1339 | SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(), |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1340 | Operand.getValueType(), Operand, RC); |
| 1341 | Operand = SDValue(Node, 0); |
| 1342 | } |
| 1343 | |
Tom Stellard | acec99c | 2013-06-05 23:39:50 +0000 | [diff] [blame] | 1344 | /// \returns true if \p Node's operands are different from the SDValue list |
| 1345 | /// \p Ops |
| 1346 | static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) { |
| 1347 | for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) { |
| 1348 | if (Ops[i].getNode() != Node->getOperand(i).getNode()) { |
| 1349 | return true; |
| 1350 | } |
| 1351 | } |
| 1352 | return false; |
| 1353 | } |
| 1354 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1355 | /// \brief Try to fold the Nodes operands into the Node |
| 1356 | SDNode *SITargetLowering::foldOperands(MachineSDNode *Node, |
| 1357 | SelectionDAG &DAG) const { |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1358 | |
| 1359 | // Original encoding (either e32 or e64) |
| 1360 | int Opcode = Node->getMachineOpcode(); |
Bill Wendling | 37e9adb | 2013-06-07 20:28:55 +0000 | [diff] [blame] | 1361 | const SIInstrInfo *TII = |
| 1362 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1363 | const MCInstrDesc *Desc = &TII->get(Opcode); |
| 1364 | |
| 1365 | unsigned NumDefs = Desc->getNumDefs(); |
| 1366 | unsigned NumOps = Desc->getNumOperands(); |
| 1367 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 1368 | // Commuted opcode if available |
| 1369 | int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1370 | const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev); |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 1371 | |
| 1372 | assert(!DescRev || DescRev->getNumDefs() == NumDefs); |
| 1373 | assert(!DescRev || DescRev->getNumOperands() == NumOps); |
| 1374 | |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1375 | // e64 version if available, -1 otherwise |
| 1376 | int OpcodeE64 = AMDGPU::getVOPe64(Opcode); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1377 | const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64); |
Vincent Lejeune | 29c0c21 | 2014-05-10 19:18:39 +0000 | [diff] [blame] | 1378 | int InputModifiers[3] = {0}; |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1379 | |
| 1380 | assert(!DescE64 || DescE64->getNumDefs() == NumDefs); |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1381 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1382 | int32_t Immediate = Desc->getSize() == 4 ? 0 : -1; |
| 1383 | bool HaveVSrc = false, HaveSSrc = false; |
| 1384 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1385 | // First figure out what we already have in this instruction. |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1386 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 1387 | i != e && Op < NumOps; ++i, ++Op) { |
| 1388 | |
| 1389 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
| 1390 | if (isVSrc(RegClass)) |
| 1391 | HaveVSrc = true; |
| 1392 | else if (isSSrc(RegClass)) |
| 1393 | HaveSSrc = true; |
| 1394 | else |
| 1395 | continue; |
| 1396 | |
| 1397 | int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode()); |
| 1398 | if (Imm != -1 && Imm != 0) { |
| 1399 | // Literal immediate |
| 1400 | Immediate = Imm; |
| 1401 | } |
| 1402 | } |
| 1403 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1404 | // If we neither have VSrc nor SSrc, it makes no sense to continue. |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1405 | if (!HaveVSrc && !HaveSSrc) |
| 1406 | return Node; |
| 1407 | |
| 1408 | // No scalar allowed when we have both VSrc and SSrc |
| 1409 | bool ScalarSlotUsed = HaveVSrc && HaveSSrc; |
| 1410 | |
| 1411 | // Second go over the operands and try to fold them |
| 1412 | std::vector<SDValue> Ops; |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1413 | bool Promote2e64 = false; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1414 | for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs; |
| 1415 | i != e && Op < NumOps; ++i, ++Op) { |
| 1416 | |
| 1417 | const SDValue &Operand = Node->getOperand(i); |
| 1418 | Ops.push_back(Operand); |
| 1419 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1420 | // Already folded immediate? |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1421 | if (isa<ConstantSDNode>(Operand.getNode()) || |
| 1422 | isa<ConstantFPSDNode>(Operand.getNode())) |
| 1423 | continue; |
| 1424 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1425 | // Is this a VSrc or SSrc operand? |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1426 | unsigned RegClass = Desc->OpInfo[Op].RegClass; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 1427 | if (isVSrc(RegClass) || isSSrc(RegClass)) { |
| 1428 | // Try to fold the immediates |
| 1429 | if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) { |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1430 | // Folding didn't work, make sure we don't hit the SReg limit. |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 1431 | ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed); |
| 1432 | } |
| 1433 | continue; |
| 1434 | } |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 1435 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 1436 | if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) { |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 1437 | |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 1438 | unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass; |
| 1439 | assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass)); |
| 1440 | |
| 1441 | // Test if it makes sense to swap operands |
| 1442 | if (foldImm(Ops[1], Immediate, ScalarSlotUsed) || |
| 1443 | (!fitsRegClass(DAG, Ops[1], RegClass) && |
| 1444 | fitsRegClass(DAG, Ops[1], OtherRegClass))) { |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Swap commutable operands |
Matt Arsenault | 4be76e9 | 2014-04-07 16:44:26 +0000 | [diff] [blame] | 1447 | std::swap(Ops[0], Ops[1]); |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 1448 | |
| 1449 | Desc = DescRev; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1450 | DescRev = nullptr; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 1451 | continue; |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 1452 | } |
Christian Konig | 6612ac3 | 2013-02-26 17:52:36 +0000 | [diff] [blame] | 1453 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1454 | |
Vincent Lejeune | 29c0c21 | 2014-05-10 19:18:39 +0000 | [diff] [blame] | 1455 | if (Immediate) |
| 1456 | continue; |
| 1457 | |
| 1458 | if (DescE64) { |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 1459 | // Test if it makes sense to switch to e64 encoding |
| 1460 | unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass; |
| 1461 | if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass)) |
| 1462 | continue; |
| 1463 | |
| 1464 | int32_t TmpImm = -1; |
| 1465 | if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) || |
| 1466 | (!fitsRegClass(DAG, Ops[i], RegClass) && |
| 1467 | fitsRegClass(DAG, Ops[1], OtherRegClass))) { |
| 1468 | |
| 1469 | // Switch to e64 encoding |
| 1470 | Immediate = -1; |
| 1471 | Promote2e64 = true; |
| 1472 | Desc = DescE64; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1473 | DescE64 = nullptr; |
Christian Konig | 8370dbb | 2013-03-26 14:04:17 +0000 | [diff] [blame] | 1474 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1475 | } |
Vincent Lejeune | 29c0c21 | 2014-05-10 19:18:39 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (!DescE64 && !Promote2e64) |
| 1478 | continue; |
| 1479 | if (!Operand.isMachineOpcode()) |
| 1480 | continue; |
| 1481 | if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) { |
| 1482 | Ops.pop_back(); |
| 1483 | Ops.push_back(Operand.getOperand(0)); |
| 1484 | InputModifiers[i] = 1; |
| 1485 | Promote2e64 = true; |
| 1486 | if (!DescE64) |
| 1487 | continue; |
| 1488 | Desc = DescE64; |
Matt Arsenault | c6f338d | 2014-06-05 00:01:12 +0000 | [diff] [blame] | 1489 | DescE64 = nullptr; |
Vincent Lejeune | 29c0c21 | 2014-05-10 19:18:39 +0000 | [diff] [blame] | 1490 | } |
| 1491 | else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) { |
| 1492 | Ops.pop_back(); |
| 1493 | Ops.push_back(Operand.getOperand(0)); |
| 1494 | InputModifiers[i] = 2; |
| 1495 | Promote2e64 = true; |
| 1496 | if (!DescE64) |
| 1497 | continue; |
| 1498 | Desc = DescE64; |
Matt Arsenault | c6f338d | 2014-06-05 00:01:12 +0000 | [diff] [blame] | 1499 | DescE64 = nullptr; |
Vincent Lejeune | 29c0c21 | 2014-05-10 19:18:39 +0000 | [diff] [blame] | 1500 | } |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1503 | if (Promote2e64) { |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1504 | std::vector<SDValue> OldOps(Ops); |
| 1505 | Ops.clear(); |
| 1506 | for (unsigned i = 0; i < OldOps.size(); ++i) { |
| 1507 | // src_modifier |
Vincent Lejeune | 29c0c21 | 2014-05-10 19:18:39 +0000 | [diff] [blame] | 1508 | Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32)); |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1509 | Ops.push_back(OldOps[i]); |
| 1510 | } |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1511 | // Add the modifier flags while promoting |
Vincent Lejeune | 94af31f | 2014-05-10 19:18:33 +0000 | [diff] [blame] | 1512 | for (unsigned i = 0; i < 2; ++i) |
Christian Konig | e500e44 | 2013-02-26 17:52:47 +0000 | [diff] [blame] | 1513 | Ops.push_back(DAG.getTargetConstant(0, MVT::i32)); |
| 1514 | } |
| 1515 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1516 | // Add optional chain and glue |
| 1517 | for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i) |
| 1518 | Ops.push_back(Node->getOperand(i)); |
| 1519 | |
Tom Stellard | b5a9700 | 2013-06-03 17:39:50 +0000 | [diff] [blame] | 1520 | // Nodes that have a glue result are not CSE'd by getMachineNode(), so in |
| 1521 | // this case a brand new node is always be created, even if the operands |
| 1522 | // are the same as before. So, manually check if anything has been changed. |
Tom Stellard | acec99c | 2013-06-05 23:39:50 +0000 | [diff] [blame] | 1523 | if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) { |
| 1524 | return Node; |
Tom Stellard | b5a9700 | 2013-06-03 17:39:50 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Christian Konig | 3c14580 | 2013-03-27 09:12:59 +0000 | [diff] [blame] | 1527 | // Create a complete new instruction |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1528 | return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops); |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1529 | } |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1530 | |
| 1531 | /// \brief Helper function for adjustWritemask |
Benjamin Kramer | 635e368 | 2013-05-23 15:43:05 +0000 | [diff] [blame] | 1532 | static unsigned SubIdx2Lane(unsigned Idx) { |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1533 | switch (Idx) { |
| 1534 | default: return 0; |
| 1535 | case AMDGPU::sub0: return 0; |
| 1536 | case AMDGPU::sub1: return 1; |
| 1537 | case AMDGPU::sub2: return 2; |
| 1538 | case AMDGPU::sub3: return 3; |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /// \brief Adjust the writemask of MIMG instructions |
| 1543 | void SITargetLowering::adjustWritemask(MachineSDNode *&Node, |
| 1544 | SelectionDAG &DAG) const { |
| 1545 | SDNode *Users[4] = { }; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1546 | unsigned Lane = 0; |
| 1547 | unsigned OldDmask = Node->getConstantOperandVal(0); |
| 1548 | unsigned NewDmask = 0; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1549 | |
| 1550 | // Try to figure out the used register components |
| 1551 | for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); |
| 1552 | I != E; ++I) { |
| 1553 | |
| 1554 | // Abort if we can't understand the usage |
| 1555 | if (!I->isMachineOpcode() || |
| 1556 | I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) |
| 1557 | return; |
| 1558 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1559 | // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. |
| 1560 | // Note that subregs are packed, i.e. Lane==0 is the first bit set |
| 1561 | // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit |
| 1562 | // set, etc. |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1563 | Lane = SubIdx2Lane(I->getConstantOperandVal(1)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1564 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1565 | // Set which texture component corresponds to the lane. |
| 1566 | unsigned Comp; |
| 1567 | for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { |
| 1568 | assert(Dmask); |
Tom Stellard | 03a5c08 | 2013-10-23 03:50:25 +0000 | [diff] [blame] | 1569 | Comp = countTrailingZeros(Dmask); |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1570 | Dmask &= ~(1 << Comp); |
| 1571 | } |
| 1572 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1573 | // Abort if we have more than one user per component |
| 1574 | if (Users[Lane]) |
| 1575 | return; |
| 1576 | |
| 1577 | Users[Lane] = *I; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1578 | NewDmask |= 1 << Comp; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1581 | // Abort if there's no change |
| 1582 | if (NewDmask == OldDmask) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1583 | return; |
| 1584 | |
| 1585 | // Adjust the writemask in the node |
| 1586 | std::vector<SDValue> Ops; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1587 | Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1588 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) |
| 1589 | Ops.push_back(Node->getOperand(i)); |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 1590 | Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1591 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1592 | // If we only got one lane, replace it with a copy |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1593 | // (if NewDmask has only one bit set...) |
| 1594 | if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1595 | SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32); |
| 1596 | SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1597 | SDLoc(), Users[Lane]->getValueType(0), |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1598 | SDValue(Node, 0), RC); |
| 1599 | DAG.ReplaceAllUsesWith(Users[Lane], Copy); |
| 1600 | return; |
| 1601 | } |
| 1602 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1603 | // Update the users of the node with the new indices |
| 1604 | for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { |
| 1605 | |
| 1606 | SDNode *User = Users[i]; |
| 1607 | if (!User) |
| 1608 | continue; |
| 1609 | |
| 1610 | SDValue Op = DAG.getTargetConstant(Idx, MVT::i32); |
| 1611 | DAG.UpdateNodeOperands(User, User->getOperand(0), Op); |
| 1612 | |
| 1613 | switch (Idx) { |
| 1614 | default: break; |
| 1615 | case AMDGPU::sub0: Idx = AMDGPU::sub1; break; |
| 1616 | case AMDGPU::sub1: Idx = AMDGPU::sub2; break; |
| 1617 | case AMDGPU::sub2: Idx = AMDGPU::sub3; break; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1622 | /// \brief Fold the instructions after selecting them. |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1623 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 1624 | SelectionDAG &DAG) const { |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1625 | const SIInstrInfo *TII = |
| 1626 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 1627 | Node = AdjustRegClass(Node, DAG); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1628 | |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1629 | if (TII->isMIMG(Node->getMachineOpcode())) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1630 | adjustWritemask(Node, DAG); |
| 1631 | |
| 1632 | return foldOperands(Node, DAG); |
| 1633 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1634 | |
| 1635 | /// \brief Assign the register class depending on the number of |
| 1636 | /// bits set in the writemask |
| 1637 | void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, |
| 1638 | SDNode *Node) const { |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1639 | const SIInstrInfo *TII = |
| 1640 | static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo()); |
| 1641 | if (!TII->isMIMG(MI->getOpcode())) |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1642 | return; |
| 1643 | |
| 1644 | unsigned VReg = MI->getOperand(0).getReg(); |
| 1645 | unsigned Writemask = MI->getOperand(1).getImm(); |
| 1646 | unsigned BitsSet = 0; |
| 1647 | for (unsigned i = 0; i < 4; ++i) |
| 1648 | BitsSet += Writemask & (1 << i) ? 1 : 0; |
| 1649 | |
| 1650 | const TargetRegisterClass *RC; |
| 1651 | switch (BitsSet) { |
| 1652 | default: return; |
| 1653 | case 1: RC = &AMDGPU::VReg_32RegClass; break; |
| 1654 | case 2: RC = &AMDGPU::VReg_64RegClass; break; |
| 1655 | case 3: RC = &AMDGPU::VReg_96RegClass; break; |
| 1656 | } |
| 1657 | |
Tom Stellard | 682bfbc | 2013-10-10 17:11:24 +0000 | [diff] [blame] | 1658 | unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet); |
| 1659 | MI->setDesc(TII->get(NewOpcode)); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1660 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
| 1661 | MRI.setRegClass(VReg, RC); |
| 1662 | } |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 1663 | |
| 1664 | MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N, |
| 1665 | SelectionDAG &DAG) const { |
| 1666 | |
| 1667 | SDLoc DL(N); |
| 1668 | unsigned NewOpcode = N->getMachineOpcode(); |
| 1669 | |
| 1670 | switch (N->getMachineOpcode()) { |
| 1671 | default: return N; |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 1672 | case AMDGPU::S_LOAD_DWORD_IMM: |
| 1673 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64; |
| 1674 | // Fall-through |
| 1675 | case AMDGPU::S_LOAD_DWORDX2_SGPR: |
| 1676 | if (NewOpcode == N->getMachineOpcode()) { |
| 1677 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64; |
| 1678 | } |
| 1679 | // Fall-through |
| 1680 | case AMDGPU::S_LOAD_DWORDX4_IMM: |
| 1681 | case AMDGPU::S_LOAD_DWORDX4_SGPR: { |
| 1682 | if (NewOpcode == N->getMachineOpcode()) { |
| 1683 | NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64; |
| 1684 | } |
| 1685 | if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) { |
| 1686 | return N; |
| 1687 | } |
| 1688 | ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1)); |
| 1689 | SDValue Ops[] = { |
| 1690 | SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128, |
| 1691 | DAG.getConstant(0, MVT::i64)), 0), |
| 1692 | N->getOperand(0), |
| 1693 | DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32) |
| 1694 | }; |
| 1695 | return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops); |
| 1696 | } |
| 1697 | } |
| 1698 | } |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 1699 | |
| 1700 | SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, |
| 1701 | const TargetRegisterClass *RC, |
| 1702 | unsigned Reg, EVT VT) const { |
| 1703 | SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); |
| 1704 | |
| 1705 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), |
| 1706 | cast<RegisterSDNode>(VReg)->getReg(), VT); |
| 1707 | } |