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