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