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" |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/BitVector.h" |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/CallingConvLower.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 32 | #include "llvm/CodeGen/SelectionDAG.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Function.h" |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallString.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 38 | SITargetLowering::SITargetLowering(TargetMachine &TM, |
| 39 | const AMDGPUSubtarget &STI) |
| 40 | : AMDGPUTargetLowering(TM, STI) { |
Tom Stellard | 1bd8072 | 2014-04-30 15:31:33 +0000 | [diff] [blame] | 41 | addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 42 | addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 43 | |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 44 | addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass); |
| 45 | addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass); |
| 46 | |
Tom Stellard | 334b29c | 2014-04-17 21:00:09 +0000 | [diff] [blame] | 47 | addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 48 | addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 49 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 50 | addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); |
| 51 | addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); |
| 52 | addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 53 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 54 | addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); |
| 55 | addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 56 | |
Tom Stellard | f0a2107 | 2014-11-18 20:39:39 +0000 | [diff] [blame] | 57 | addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 58 | addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); |
| 59 | |
Tom Stellard | f0a2107 | 2014-11-18 20:39:39 +0000 | [diff] [blame] | 60 | addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 61 | addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 62 | |
Eric Christopher | 23a3a7c | 2015-02-26 00:00:24 +0000 | [diff] [blame] | 63 | computeRegisterProperties(STI.getRegisterInfo()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 64 | |
Christian Konig | 2989ffc | 2013-03-18 11:34:16 +0000 | [diff] [blame] | 65 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); |
| 66 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); |
| 67 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); |
| 68 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); |
| 69 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 70 | setOperationAction(ISD::ADD, MVT::i32, Legal); |
Matt Arsenault | e8d2146 | 2013-11-18 20:09:40 +0000 | [diff] [blame] | 71 | setOperationAction(ISD::ADDC, MVT::i32, Legal); |
| 72 | setOperationAction(ISD::ADDE, MVT::i32, Legal); |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 73 | setOperationAction(ISD::SUBC, MVT::i32, Legal); |
| 74 | setOperationAction(ISD::SUBE, MVT::i32, Legal); |
Aaron Watry | daabb20 | 2013-06-25 13:55:52 +0000 | [diff] [blame] | 75 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 76 | setOperationAction(ISD::FSIN, MVT::f32, Custom); |
| 77 | setOperationAction(ISD::FCOS, MVT::f32, Custom); |
| 78 | |
Matt Arsenault | 7c93690 | 2014-10-21 23:01:01 +0000 | [diff] [blame] | 79 | setOperationAction(ISD::FMINNUM, MVT::f64, Legal); |
| 80 | setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); |
| 81 | |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 82 | // We need to custom lower vector stores from local memory |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 83 | setOperationAction(ISD::LOAD, MVT::v4i32, Custom); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 84 | setOperationAction(ISD::LOAD, MVT::v8i32, Custom); |
| 85 | setOperationAction(ISD::LOAD, MVT::v16i32, Custom); |
| 86 | |
| 87 | setOperationAction(ISD::STORE, MVT::v8i32, Custom); |
| 88 | setOperationAction(ISD::STORE, MVT::v16i32, Custom); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 89 | |
Tom Stellard | 1c8788e | 2014-03-07 20:12:33 +0000 | [diff] [blame] | 90 | setOperationAction(ISD::STORE, MVT::i1, Custom); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 91 | setOperationAction(ISD::STORE, MVT::v4i32, Custom); |
| 92 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 93 | setOperationAction(ISD::SELECT, MVT::i64, Custom); |
Tom Stellard | da99c6e | 2014-03-24 16:07:30 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::SELECT, MVT::f64, Promote); |
| 95 | AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 96 | |
Tom Stellard | 3ca1bfc | 2014-06-10 16:01:22 +0000 | [diff] [blame] | 97 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 98 | setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); |
| 99 | setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); |
| 100 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 101 | |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 102 | setOperationAction(ISD::SETCC, MVT::v2i1, Expand); |
| 103 | setOperationAction(ISD::SETCC, MVT::v4i1, Expand); |
| 104 | |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 105 | setOperationAction(ISD::BSWAP, MVT::i32, Legal); |
| 106 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 107 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 108 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); |
| 109 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); |
| 110 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 111 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 112 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); |
| 113 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); |
| 114 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 115 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 116 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); |
| 117 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); |
| 118 | |
Matt Arsenault | 9481221 | 2014-11-14 18:18:16 +0000 | [diff] [blame] | 119 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 120 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); |
| 121 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 122 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 123 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); |
| 124 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom); |
| 125 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 126 | |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 127 | setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); |
Matt Arsenault | e54e1c3 | 2014-06-23 18:00:44 +0000 | [diff] [blame] | 128 | setOperationAction(ISD::BRCOND, MVT::Other, Custom); |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 129 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 130 | for (MVT VT : MVT::integer_valuetypes()) { |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 131 | if (VT == MVT::i64) |
| 132 | continue; |
| 133 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 134 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 135 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal); |
| 136 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 137 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand); |
Tom Stellard | 31209cc | 2013-07-15 19:00:09 +0000 | [diff] [blame] | 138 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 139 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 140 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal); |
| 141 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 142 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand); |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 143 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 144 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 145 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal); |
| 146 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 147 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand); |
| 148 | } |
| 149 | |
| 150 | for (MVT VT : MVT::integer_vector_valuetypes()) { |
| 151 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand); |
| 152 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand); |
| 153 | } |
| 154 | |
| 155 | for (MVT VT : MVT::fp_valuetypes()) |
| 156 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 157 | |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 158 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Matt Arsenault | 6f24379 | 2013-09-05 19:41:10 +0000 | [diff] [blame] | 159 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 160 | setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); |
| 161 | setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 162 | |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 163 | setOperationAction(ISD::LOAD, MVT::i1, Custom); |
| 164 | |
Tom Stellard | fd15582 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 165 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Tom Stellard | 04c0e98 | 2014-01-22 19:24:21 +0000 | [diff] [blame] | 166 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Matt Arsenault | a98cd6a | 2013-12-19 05:32:55 +0000 | [diff] [blame] | 167 | setOperationAction(ISD::FrameIndex, MVT::i32, Custom); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 168 | |
Tom Stellard | 5f33788 | 2014-04-29 23:12:43 +0000 | [diff] [blame] | 169 | // These should use UDIVREM, so set them to expand |
| 170 | setOperationAction(ISD::UDIV, MVT::i64, Expand); |
| 171 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
| 172 | |
Matt Arsenault | 0d89e84 | 2014-07-15 21:44:37 +0000 | [diff] [blame] | 173 | setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); |
| 174 | setOperationAction(ISD::SELECT, MVT::i1, Promote); |
| 175 | |
Benjamin Kramer | 867bfc5 | 2015-03-07 17:41:00 +0000 | [diff] [blame] | 176 | // We only support LOAD/STORE and vector manipulation ops for vectors |
| 177 | // with > 4 elements. |
| 178 | for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32}) { |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 179 | for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { |
| 180 | switch(Op) { |
| 181 | case ISD::LOAD: |
| 182 | case ISD::STORE: |
| 183 | case ISD::BUILD_VECTOR: |
| 184 | case ISD::BITCAST: |
| 185 | case ISD::EXTRACT_VECTOR_ELT: |
| 186 | case ISD::INSERT_VECTOR_ELT: |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 187 | case ISD::INSERT_SUBVECTOR: |
| 188 | case ISD::EXTRACT_SUBVECTOR: |
| 189 | break; |
Tom Stellard | c0503db | 2014-08-09 01:06:56 +0000 | [diff] [blame] | 190 | case ISD::CONCAT_VECTORS: |
| 191 | setOperationAction(Op, VT, Custom); |
| 192 | break; |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 193 | default: |
Matt Arsenault | d504a74 | 2014-05-15 21:44:05 +0000 | [diff] [blame] | 194 | setOperationAction(Op, VT, Expand); |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 200 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) { |
| 201 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
| 202 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
Matt Arsenault | a90d22f | 2014-04-17 17:06:37 +0000 | [diff] [blame] | 203 | setOperationAction(ISD::FRINT, MVT::f64, Legal); |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Marek Olsak | 7d77728 | 2015-03-24 13:40:15 +0000 | [diff] [blame] | 206 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 207 | setOperationAction(ISD::FDIV, MVT::f32, Custom); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 208 | setOperationAction(ISD::FDIV, MVT::f64, Custom); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 209 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 210 | setTargetDAGCombine(ISD::FADD); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 211 | setTargetDAGCombine(ISD::FSUB); |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 212 | setTargetDAGCombine(ISD::FMINNUM); |
| 213 | setTargetDAGCombine(ISD::FMAXNUM); |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 214 | setTargetDAGCombine(ISD::SELECT_CC); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 215 | setTargetDAGCombine(ISD::SETCC); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 216 | setTargetDAGCombine(ISD::AND); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 217 | setTargetDAGCombine(ISD::OR); |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 218 | setTargetDAGCombine(ISD::UINT_TO_FP); |
| 219 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 220 | // All memory operations. Some folding on the pointer operand is done to help |
| 221 | // matching the constant offsets in the addressing modes. |
| 222 | setTargetDAGCombine(ISD::LOAD); |
| 223 | setTargetDAGCombine(ISD::STORE); |
| 224 | setTargetDAGCombine(ISD::ATOMIC_LOAD); |
| 225 | setTargetDAGCombine(ISD::ATOMIC_STORE); |
| 226 | setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); |
| 227 | setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); |
| 228 | setTargetDAGCombine(ISD::ATOMIC_SWAP); |
| 229 | setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); |
| 230 | setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); |
| 231 | setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); |
| 232 | setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); |
| 233 | setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); |
| 234 | setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); |
| 235 | setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); |
| 236 | setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); |
| 237 | setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); |
| 238 | setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); |
| 239 | |
Christian Konig | eecebd0 | 2013-03-26 14:04:02 +0000 | [diff] [blame] | 240 | setSchedulingPreference(Sched::RegPressure); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 243 | //===----------------------------------------------------------------------===// |
| 244 | // TargetLowering queries |
| 245 | //===----------------------------------------------------------------------===// |
| 246 | |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 247 | bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &, |
| 248 | EVT) const { |
| 249 | // SI has some legal vector types, but no legal vector operations. Say no |
| 250 | // shuffles are legal in order to prefer scalarizing some vector operations. |
| 251 | return false; |
| 252 | } |
| 253 | |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 254 | // FIXME: This really needs an address space argument. The immediate offset |
| 255 | // size is different for different sets of memory instruction sets. |
| 256 | |
| 257 | // The single offset DS instructions have a 16-bit unsigned byte offset. |
| 258 | // |
| 259 | // MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r + |
| 260 | // r + i with addr64. 32-bit has more addressing mode options. Depending on the |
| 261 | // resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i). |
| 262 | // |
| 263 | // SMRD instructions have an 8-bit, dword offset. |
| 264 | // |
| 265 | bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| 266 | Type *Ty) const { |
| 267 | // No global is ever allowed as a base. |
| 268 | if (AM.BaseGV) |
| 269 | return false; |
| 270 | |
| 271 | // Allow a 16-bit unsigned immediate field, since this is what DS instructions |
| 272 | // use. |
| 273 | if (!isUInt<16>(AM.BaseOffs)) |
| 274 | return false; |
| 275 | |
| 276 | // Only support r+r, |
| 277 | switch (AM.Scale) { |
| 278 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 279 | break; |
| 280 | case 1: |
| 281 | if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. |
| 282 | return false; |
| 283 | // Otherwise we have r+r or r+i. |
| 284 | break; |
| 285 | case 2: |
| 286 | if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. |
| 287 | return false; |
| 288 | // Allow 2*r as r+r. |
| 289 | break; |
| 290 | default: // Don't allow n * r |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | return true; |
| 295 | } |
| 296 | |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 297 | bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 298 | unsigned AddrSpace, |
| 299 | unsigned Align, |
| 300 | bool *IsFast) const { |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 301 | if (IsFast) |
| 302 | *IsFast = false; |
| 303 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 304 | // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, |
| 305 | // which isn't a simple VT. |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 306 | if (!VT.isSimple() || VT == MVT::Other) |
| 307 | return false; |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 308 | |
Tom Stellard | c6b299c | 2015-02-02 18:02:28 +0000 | [diff] [blame] | 309 | // TODO - CI+ supports unaligned memory accesses, but this requires driver |
| 310 | // support. |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 311 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 312 | // XXX - The only mention I see of this in the ISA manual is for LDS direct |
| 313 | // reads the "byte address and must be dword aligned". Is it also true for the |
| 314 | // normal loads and stores? |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 315 | if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) { |
| 316 | // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte |
| 317 | // aligned, 8 byte access in a single operation using ds_read2/write2_b32 |
| 318 | // with adjacent offsets. |
| 319 | return Align % 4 == 0; |
| 320 | } |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 321 | |
Tom Stellard | 33e64c6 | 2015-02-04 20:49:52 +0000 | [diff] [blame] | 322 | // Smaller than dword value must be aligned. |
| 323 | // FIXME: This should be allowed on CI+ |
| 324 | if (VT.bitsLT(MVT::i32)) |
| 325 | return false; |
| 326 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 327 | // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the |
| 328 | // byte-address are ignored, thus forcing Dword alignment. |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 329 | // This applies to private, global, and constant memory. |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 330 | if (IsFast) |
| 331 | *IsFast = true; |
Tom Stellard | c6b299c | 2015-02-02 18:02:28 +0000 | [diff] [blame] | 332 | |
| 333 | return VT.bitsGT(MVT::i32) && Align % 4 == 0; |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Matt Arsenault | 46645fa | 2014-07-28 17:49:26 +0000 | [diff] [blame] | 336 | EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, |
| 337 | unsigned SrcAlign, bool IsMemset, |
| 338 | bool ZeroMemset, |
| 339 | bool MemcpyStrSrc, |
| 340 | MachineFunction &MF) const { |
| 341 | // FIXME: Should account for address space here. |
| 342 | |
| 343 | // The default fallback uses the private pointer size as a guess for a type to |
| 344 | // use. Make sure we switch these to 64-bit accesses. |
| 345 | |
| 346 | if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global |
| 347 | return MVT::v4i32; |
| 348 | |
| 349 | if (Size >= 8 && DstAlign >= 4) |
| 350 | return MVT::v2i32; |
| 351 | |
| 352 | // Use the default. |
| 353 | return MVT::Other; |
| 354 | } |
| 355 | |
Chandler Carruth | 9d010ff | 2014-07-03 00:23:43 +0000 | [diff] [blame] | 356 | TargetLoweringBase::LegalizeTypeAction |
| 357 | SITargetLowering::getPreferredVectorAction(EVT VT) const { |
| 358 | if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) |
| 359 | return TypeSplitVector; |
| 360 | |
| 361 | return TargetLoweringBase::getPreferredVectorAction(VT); |
Tom Stellard | d86003e | 2013-08-14 23:25:00 +0000 | [diff] [blame] | 362 | } |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 363 | |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 364 | bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, |
| 365 | Type *Ty) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 366 | const SIInstrInfo *TII = |
| 367 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 368 | return TII->isInlineConstant(Imm); |
| 369 | } |
| 370 | |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 371 | SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 372 | SDLoc SL, SDValue Chain, |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 373 | unsigned Offset, bool Signed) const { |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 374 | const DataLayout *DL = getDataLayout(); |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 375 | MachineFunction &MF = DAG.getMachineFunction(); |
| 376 | const SIRegisterInfo *TRI = |
| 377 | static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo()); |
| 378 | unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 379 | |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 380 | Type *Ty = VT.getTypeForEVT(*DAG.getContext()); |
| 381 | |
| 382 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
| 383 | PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); |
| 384 | SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 385 | MRI.getLiveInVirtReg(InputPtrReg), MVT::i64); |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 386 | SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 387 | DAG.getConstant(Offset, SL, MVT::i64)); |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 388 | SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS)); |
| 389 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 390 | |
| 391 | return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, |
| 392 | VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT, |
| 393 | false, // isVolatile |
| 394 | true, // isNonTemporal |
| 395 | true, // isInvariant |
| 396 | DL->getABITypeAlignment(Ty)); // Alignment |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 399 | SDValue SITargetLowering::LowerFormalArguments( |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 400 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 401 | const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, |
| 402 | SmallVectorImpl<SDValue> &InVals) const { |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 403 | const SIRegisterInfo *TRI = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 404 | static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 405 | |
| 406 | MachineFunction &MF = DAG.getMachineFunction(); |
| 407 | FunctionType *FType = MF.getFunction()->getFunctionType(); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 408 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 409 | |
| 410 | assert(CallConv == CallingConv::C); |
| 411 | |
| 412 | SmallVector<ISD::InputArg, 16> Splits; |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 413 | BitVector Skipped(Ins.size()); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 414 | |
| 415 | for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 416 | const ISD::InputArg &Arg = Ins[i]; |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 417 | |
| 418 | // First check if it's a PS input addr |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 419 | if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() && |
Vincent Lejeune | d623644 | 2013-10-13 17:56:16 +0000 | [diff] [blame] | 420 | !Arg.Flags.isByVal()) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 421 | |
| 422 | assert((PSInputNum <= 15) && "Too many PS inputs!"); |
| 423 | |
| 424 | if (!Arg.Used) { |
| 425 | // We can savely skip PS inputs |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 426 | Skipped.set(i); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 427 | ++PSInputNum; |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | Info->PSInputAddr |= 1 << PSInputNum++; |
| 432 | } |
| 433 | |
| 434 | // Second split vertices into their elements |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 435 | if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 436 | ISD::InputArg NewArg = Arg; |
| 437 | NewArg.Flags.setSplit(); |
| 438 | NewArg.VT = Arg.VT.getVectorElementType(); |
| 439 | |
| 440 | // We REALLY want the ORIGINAL number of vertex elements here, e.g. a |
| 441 | // three or five element vertex only needs three or five registers, |
| 442 | // NOT four or eigth. |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 443 | Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 444 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 445 | |
| 446 | for (unsigned j = 0; j != NumElements; ++j) { |
| 447 | Splits.push_back(NewArg); |
| 448 | NewArg.PartOffset += NewArg.VT.getStoreSize(); |
| 449 | } |
| 450 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 451 | } else if (Info->getShaderType() != ShaderType::COMPUTE) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 452 | Splits.push_back(Arg); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 457 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, |
| 458 | *DAG.getContext()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 459 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 460 | // 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] | 461 | if (Info->getShaderType() == ShaderType::PIXEL && |
| 462 | (Info->PSInputAddr & 0x7F) == 0) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 463 | Info->PSInputAddr |= 1; |
| 464 | CCInfo.AllocateReg(AMDGPU::VGPR0); |
| 465 | CCInfo.AllocateReg(AMDGPU::VGPR1); |
| 466 | } |
| 467 | |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 468 | // The pointer to the list of arguments is stored in SGPR0, SGPR1 |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 469 | // The pointer to the scratch buffer is stored in SGPR2, SGPR3 |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 470 | if (Info->getShaderType() == ShaderType::COMPUTE) { |
Tom Stellard | feab91c | 2014-12-02 17:41:43 +0000 | [diff] [blame] | 471 | if (Subtarget->isAmdHsaOS()) |
| 472 | Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers. |
| 473 | else |
| 474 | Info->NumUserSGPRs = 4; |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 475 | |
| 476 | unsigned InputPtrReg = |
| 477 | TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR); |
| 478 | unsigned InputPtrRegLo = |
| 479 | TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0); |
| 480 | unsigned InputPtrRegHi = |
| 481 | TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1); |
| 482 | |
| 483 | unsigned ScratchPtrReg = |
| 484 | TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR); |
| 485 | unsigned ScratchPtrRegLo = |
| 486 | TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0); |
| 487 | unsigned ScratchPtrRegHi = |
| 488 | TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1); |
| 489 | |
| 490 | CCInfo.AllocateReg(InputPtrRegLo); |
| 491 | CCInfo.AllocateReg(InputPtrRegHi); |
| 492 | CCInfo.AllocateReg(ScratchPtrRegLo); |
| 493 | CCInfo.AllocateReg(ScratchPtrRegHi); |
| 494 | MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass); |
| 495 | MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 498 | if (Info->getShaderType() == ShaderType::COMPUTE) { |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 499 | getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins, |
| 500 | Splits); |
| 501 | } |
| 502 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 503 | AnalyzeFormalArguments(CCInfo, Splits); |
| 504 | |
| 505 | for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { |
| 506 | |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 507 | const ISD::InputArg &Arg = Ins[i]; |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 508 | if (Skipped[i]) { |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 509 | InVals.push_back(DAG.getUNDEF(Arg.VT)); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 510 | continue; |
| 511 | } |
| 512 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 513 | CCValAssign &VA = ArgLocs[ArgIdx++]; |
Craig Topper | 7f416c8 | 2014-11-16 21:17:18 +0000 | [diff] [blame] | 514 | MVT VT = VA.getLocVT(); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 515 | |
| 516 | if (VA.isMemLoc()) { |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 517 | VT = Ins[i].VT; |
| 518 | EVT MemVT = Splits[i].VT; |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 519 | const unsigned Offset = 36 + VA.getLocMemOffset(); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 520 | // The first 36 bytes of the input buffer contains information about |
| 521 | // thread group and global sizes. |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 522 | SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(), |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 523 | Offset, Ins[i].Flags.isSExt()); |
Tom Stellard | ca7ecf3 | 2014-08-22 18:49:31 +0000 | [diff] [blame] | 524 | |
| 525 | const PointerType *ParamTy = |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 526 | dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); |
Tom Stellard | ca7ecf3 | 2014-08-22 18:49:31 +0000 | [diff] [blame] | 527 | if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && |
| 528 | ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { |
| 529 | // On SI local pointers are just offsets into LDS, so they are always |
| 530 | // less than 16-bits. On CI and newer they could potentially be |
| 531 | // real pointers, so we can't guarantee their size. |
| 532 | Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, |
| 533 | DAG.getValueType(MVT::i16)); |
| 534 | } |
| 535 | |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 536 | InVals.push_back(Arg); |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 537 | Info->ABIArgOffset = Offset + MemVT.getStoreSize(); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 538 | continue; |
| 539 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 540 | assert(VA.isRegLoc() && "Parameter must be in a register!"); |
| 541 | |
| 542 | unsigned Reg = VA.getLocReg(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 543 | |
| 544 | if (VT == MVT::i64) { |
| 545 | // For now assume it is a pointer |
| 546 | Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, |
| 547 | &AMDGPU::SReg_64RegClass); |
| 548 | Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); |
| 549 | InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); |
| 554 | |
| 555 | Reg = MF.addLiveIn(Reg, RC); |
| 556 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 557 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 558 | if (Arg.VT.isVector()) { |
| 559 | |
| 560 | // Build a vector from the registers |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 561 | Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 562 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 563 | |
| 564 | SmallVector<SDValue, 4> Regs; |
| 565 | Regs.push_back(Val); |
| 566 | for (unsigned j = 1; j != NumElements; ++j) { |
| 567 | Reg = ArgLocs[ArgIdx++].getLocReg(); |
| 568 | Reg = MF.addLiveIn(Reg, RC); |
| 569 | Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT)); |
| 570 | } |
| 571 | |
| 572 | // Fill up the missing vector elements |
| 573 | NumElements = Arg.VT.getVectorNumElements() - NumElements; |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 574 | Regs.append(NumElements, DAG.getUNDEF(VT)); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 575 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 576 | InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs)); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 577 | continue; |
| 578 | } |
| 579 | |
| 580 | InVals.push_back(Val); |
| 581 | } |
Tom Stellard | e99fb65 | 2015-01-20 19:33:04 +0000 | [diff] [blame] | 582 | |
| 583 | if (Info->getShaderType() != ShaderType::COMPUTE) { |
Tim Northover | 3b6b7ca | 2015-02-21 02:11:17 +0000 | [diff] [blame] | 584 | unsigned ScratchIdx = CCInfo.getFirstUnallocated(ArrayRef<MCPhysReg>( |
| 585 | AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs())); |
Tom Stellard | e99fb65 | 2015-01-20 19:33:04 +0000 | [diff] [blame] | 586 | Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx); |
| 587 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 588 | return Chain; |
| 589 | } |
| 590 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 591 | MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( |
| 592 | MachineInstr * MI, MachineBasicBlock * BB) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 593 | |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 594 | MachineBasicBlock::iterator I = *MI; |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 595 | const SIInstrInfo *TII = |
| 596 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Tom Stellard | 556d9aa | 2013-06-03 17:39:37 +0000 | [diff] [blame] | 597 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 598 | switch (MI->getOpcode()) { |
| 599 | default: |
| 600 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
Matt Arsenault | 20711b7 | 2015-02-20 22:10:45 +0000 | [diff] [blame] | 601 | case AMDGPU::BRANCH: |
| 602 | return BB; |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 603 | case AMDGPU::SI_RegisterStorePseudo: { |
| 604 | MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 605 | unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); |
| 606 | MachineInstrBuilder MIB = |
| 607 | BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore), |
| 608 | Reg); |
| 609 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) |
| 610 | MIB.addOperand(MI->getOperand(i)); |
| 611 | |
| 612 | MI->eraseFromParent(); |
Vincent Lejeune | 79a5834 | 2014-05-10 19:18:25 +0000 | [diff] [blame] | 613 | break; |
| 614 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 615 | } |
| 616 | return BB; |
| 617 | } |
| 618 | |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 619 | bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { |
| 620 | // This currently forces unfolding various combinations of fsub into fma with |
| 621 | // free fneg'd operands. As long as we have fast FMA (controlled by |
| 622 | // isFMAFasterThanFMulAndFAdd), we should perform these. |
| 623 | |
| 624 | // When fma is quarter rate, for f64 where add / sub are at best half rate, |
| 625 | // most of these combines appear to be cycle neutral but save on instruction |
| 626 | // count / code size. |
| 627 | return true; |
| 628 | } |
| 629 | |
Matt Arsenault | 8596f71 | 2014-11-28 22:51:38 +0000 | [diff] [blame] | 630 | EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const { |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 631 | if (!VT.isVector()) { |
| 632 | return MVT::i1; |
| 633 | } |
Matt Arsenault | 8596f71 | 2014-11-28 22:51:38 +0000 | [diff] [blame] | 634 | return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Christian Konig | 082a14a | 2013-03-18 11:34:05 +0000 | [diff] [blame] | 637 | MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const { |
| 638 | return MVT::i32; |
| 639 | } |
| 640 | |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 641 | // Answering this is somewhat tricky and depends on the specific device which |
| 642 | // have different rates for fma or all f64 operations. |
| 643 | // |
| 644 | // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other |
| 645 | // regardless of which device (although the number of cycles differs between |
| 646 | // devices), so it is always profitable for f64. |
| 647 | // |
| 648 | // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable |
| 649 | // only on full rate devices. Normally, we should prefer selecting v_mad_f32 |
| 650 | // which we can always do even without fused FP ops since it returns the same |
| 651 | // result as the separate operations and since it is always full |
| 652 | // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 |
| 653 | // however does not support denormals, so we do report fma as faster if we have |
| 654 | // a fast fma device and require denormals. |
| 655 | // |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 656 | bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 657 | VT = VT.getScalarType(); |
| 658 | |
| 659 | if (!VT.isSimple()) |
| 660 | return false; |
| 661 | |
| 662 | switch (VT.getSimpleVT().SimpleTy) { |
| 663 | case MVT::f32: |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 664 | // This is as fast on some subtargets. However, we always have full rate f32 |
| 665 | // mad available which returns the same result as the separate operations |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 666 | // which we should prefer over fma. We can't use this if we want to support |
| 667 | // denormals, so only report this in these cases. |
| 668 | return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 669 | case MVT::f64: |
| 670 | return true; |
| 671 | default: |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | return false; |
| 676 | } |
| 677 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 678 | //===----------------------------------------------------------------------===// |
| 679 | // Custom DAG Lowering Operations |
| 680 | //===----------------------------------------------------------------------===// |
| 681 | |
| 682 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 683 | switch (Op.getOpcode()) { |
| 684 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 685 | case ISD::FrameIndex: return LowerFrameIndex(Op, DAG); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 686 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 687 | case ISD::LOAD: { |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 688 | SDValue Result = LowerLOAD(Op, DAG); |
| 689 | assert((!Result.getNode() || |
| 690 | Result.getNode()->getNumValues() == 2) && |
| 691 | "Load should return a value and a chain"); |
| 692 | return Result; |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 693 | } |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 694 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 695 | case ISD::FSIN: |
| 696 | case ISD::FCOS: |
| 697 | return LowerTrig(Op, DAG); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 698 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 699 | case ISD::FDIV: return LowerFDIV(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 700 | case ISD::STORE: return LowerSTORE(Op, DAG); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 701 | case ISD::GlobalAddress: { |
| 702 | MachineFunction &MF = DAG.getMachineFunction(); |
| 703 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 704 | return LowerGlobalAddress(MFI, Op, DAG); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 705 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 706 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 707 | case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 708 | } |
| 709 | return SDValue(); |
| 710 | } |
| 711 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 712 | /// \brief Helper function for LowerBRCOND |
| 713 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 714 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 715 | SDNode *Parent = Value.getNode(); |
| 716 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 717 | I != E; ++I) { |
| 718 | |
| 719 | if (I.getUse().get() != Value) |
| 720 | continue; |
| 721 | |
| 722 | if (I->getOpcode() == Opcode) |
| 723 | return *I; |
| 724 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 725 | return nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 728 | SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const { |
| 729 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 730 | FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op); |
| 731 | unsigned FrameIndex = FINode->getIndex(); |
| 732 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 733 | return DAG.getTargetFrameIndex(FrameIndex, MVT::i32); |
| 734 | } |
| 735 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 736 | /// This transforms the control flow intrinsics to get the branch destination as |
| 737 | /// last parameter, also switches branch target with BR if the need arise |
| 738 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 739 | SelectionDAG &DAG) const { |
| 740 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 741 | SDLoc DL(BRCOND); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 742 | |
| 743 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 744 | SDValue Target = BRCOND.getOperand(2); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 745 | SDNode *BR = nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 746 | |
| 747 | if (Intr->getOpcode() == ISD::SETCC) { |
| 748 | // As long as we negate the condition everything is fine |
| 749 | SDNode *SetCC = Intr; |
| 750 | assert(SetCC->getConstantOperandVal(1) == 1); |
NAKAMURA Takumi | 458a827 | 2013-01-07 11:14:44 +0000 | [diff] [blame] | 751 | assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 752 | ISD::SETNE); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 753 | Intr = SetCC->getOperand(0).getNode(); |
| 754 | |
| 755 | } else { |
| 756 | // Get the target from BR if we don't negate the condition |
| 757 | BR = findUser(BRCOND, ISD::BR); |
| 758 | Target = BR->getOperand(1); |
| 759 | } |
| 760 | |
| 761 | assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); |
| 762 | |
| 763 | // Build the result and |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 764 | ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 765 | |
| 766 | // operands of the new intrinsic call |
| 767 | SmallVector<SDValue, 4> Ops; |
| 768 | Ops.push_back(BRCOND.getOperand(0)); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 769 | Ops.append(Intr->op_begin() + 1, Intr->op_end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 770 | Ops.push_back(Target); |
| 771 | |
| 772 | // build the new intrinsic call |
| 773 | SDNode *Result = DAG.getNode( |
| 774 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 775 | DAG.getVTList(Res), Ops).getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 776 | |
| 777 | if (BR) { |
| 778 | // Give the branch instruction our target |
| 779 | SDValue Ops[] = { |
| 780 | BR->getOperand(0), |
| 781 | BRCOND.getOperand(2) |
| 782 | }; |
Chandler Carruth | 356665a | 2014-08-01 22:09:43 +0000 | [diff] [blame] | 783 | SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); |
| 784 | DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); |
| 785 | BR = NewBR.getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 789 | |
| 790 | // Copy the intrinsic results to registers |
| 791 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 792 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 793 | if (!CopyToReg) |
| 794 | continue; |
| 795 | |
| 796 | Chain = DAG.getCopyToReg( |
| 797 | Chain, DL, |
| 798 | CopyToReg->getOperand(1), |
| 799 | SDValue(Result, i - 1), |
| 800 | SDValue()); |
| 801 | |
| 802 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 803 | } |
| 804 | |
| 805 | // Remove the old intrinsic from the chain |
| 806 | DAG.ReplaceAllUsesOfValueWith( |
| 807 | SDValue(Intr, Intr->getNumValues() - 1), |
| 808 | Intr->getOperand(0)); |
| 809 | |
| 810 | return Chain; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 813 | SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, |
| 814 | SDValue Op, |
| 815 | SelectionDAG &DAG) const { |
| 816 | GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); |
| 817 | |
| 818 | if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) |
| 819 | return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); |
| 820 | |
| 821 | SDLoc DL(GSD); |
| 822 | const GlobalValue *GV = GSD->getGlobal(); |
| 823 | MVT PtrVT = getPointerTy(GSD->getAddressSpace()); |
| 824 | |
| 825 | SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT); |
| 826 | SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32); |
| 827 | |
| 828 | SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 829 | DAG.getConstant(0, DL, MVT::i32)); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 830 | SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 831 | DAG.getConstant(1, DL, MVT::i32)); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 832 | |
| 833 | SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue), |
| 834 | PtrLo, GA); |
| 835 | SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 836 | PtrHi, DAG.getConstant(0, DL, MVT::i32), |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 837 | SDValue(Lo.getNode(), 1)); |
| 838 | return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi); |
| 839 | } |
| 840 | |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame^] | 841 | SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL, |
| 842 | SDValue V) const { |
| 843 | // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, |
| 844 | // so we will end up with redundant moves to m0. |
| 845 | // |
| 846 | // We can't use S_MOV_B32, because there is no way to specify m0 as the |
| 847 | // destination register. |
| 848 | // |
| 849 | // We have to use them both. Machine cse will combine all the S_MOV_B32 |
| 850 | // instructions and the register coalescer eliminate the extra copies. |
| 851 | SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V); |
| 852 | return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32), |
| 853 | SDValue(M0, 0), SDValue()); // Glue |
| 854 | // A Null SDValue creates |
| 855 | // a glue result. |
| 856 | } |
| 857 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 858 | SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
| 859 | SelectionDAG &DAG) const { |
| 860 | MachineFunction &MF = DAG.getMachineFunction(); |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 861 | const SIRegisterInfo *TRI = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 862 | static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 863 | |
| 864 | EVT VT = Op.getValueType(); |
| 865 | SDLoc DL(Op); |
| 866 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 867 | |
| 868 | switch (IntrinsicID) { |
| 869 | case Intrinsic::r600_read_ngroups_x: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 870 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 871 | SI::KernelInputOffsets::NGROUPS_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 872 | case Intrinsic::r600_read_ngroups_y: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 873 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 874 | SI::KernelInputOffsets::NGROUPS_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 875 | case Intrinsic::r600_read_ngroups_z: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 876 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 877 | SI::KernelInputOffsets::NGROUPS_Z, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 878 | case Intrinsic::r600_read_global_size_x: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 879 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 880 | SI::KernelInputOffsets::GLOBAL_SIZE_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 881 | case Intrinsic::r600_read_global_size_y: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 882 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 883 | SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 884 | case Intrinsic::r600_read_global_size_z: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 885 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 886 | SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 887 | case Intrinsic::r600_read_local_size_x: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 888 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 889 | SI::KernelInputOffsets::LOCAL_SIZE_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 890 | case Intrinsic::r600_read_local_size_y: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 891 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 892 | SI::KernelInputOffsets::LOCAL_SIZE_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 893 | case Intrinsic::r600_read_local_size_z: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 894 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 895 | SI::KernelInputOffsets::LOCAL_SIZE_Z, false); |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 896 | |
| 897 | case Intrinsic::AMDGPU_read_workdim: |
| 898 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 899 | MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset, |
| 900 | false); |
| 901 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 902 | case Intrinsic::r600_read_tgid_x: |
| 903 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 904 | TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 905 | case Intrinsic::r600_read_tgid_y: |
| 906 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 907 | TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 908 | case Intrinsic::r600_read_tgid_z: |
| 909 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 910 | TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 911 | case Intrinsic::r600_read_tidig_x: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 912 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 913 | TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 914 | case Intrinsic::r600_read_tidig_y: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 915 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 916 | TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 917 | case Intrinsic::r600_read_tidig_z: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 918 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 919 | TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 920 | case AMDGPUIntrinsic::SI_load_const: { |
| 921 | SDValue Ops[] = { |
| 922 | Op.getOperand(1), |
| 923 | Op.getOperand(2) |
| 924 | }; |
| 925 | |
| 926 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 927 | MachinePointerInfo(), |
| 928 | MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, |
| 929 | VT.getStoreSize(), 4); |
| 930 | return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, |
| 931 | Op->getVTList(), Ops, VT, MMO); |
| 932 | } |
| 933 | case AMDGPUIntrinsic::SI_sample: |
| 934 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG); |
| 935 | case AMDGPUIntrinsic::SI_sampleb: |
| 936 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG); |
| 937 | case AMDGPUIntrinsic::SI_sampled: |
| 938 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG); |
| 939 | case AMDGPUIntrinsic::SI_samplel: |
| 940 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG); |
| 941 | case AMDGPUIntrinsic::SI_vs_load_input: |
| 942 | return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, |
| 943 | Op.getOperand(1), |
| 944 | Op.getOperand(2), |
| 945 | Op.getOperand(3)); |
Marek Olsak | 43650e4 | 2015-03-24 13:40:08 +0000 | [diff] [blame] | 946 | |
| 947 | case AMDGPUIntrinsic::AMDGPU_fract: |
| 948 | case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name. |
| 949 | return DAG.getNode(ISD::FSUB, DL, VT, Op.getOperand(1), |
| 950 | DAG.getNode(ISD::FFLOOR, DL, VT, Op.getOperand(1))); |
| 951 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 952 | default: |
| 953 | return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, |
| 958 | SelectionDAG &DAG) const { |
| 959 | MachineFunction &MF = DAG.getMachineFunction(); |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame^] | 960 | SDLoc DL(Op); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 961 | SDValue Chain = Op.getOperand(0); |
| 962 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 963 | |
| 964 | switch (IntrinsicID) { |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame^] | 965 | case AMDGPUIntrinsic::SI_sendmsg: { |
| 966 | Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); |
| 967 | SDValue Glue = Chain.getValue(1); |
| 968 | return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain, |
| 969 | Op.getOperand(2), Glue); |
| 970 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 971 | case AMDGPUIntrinsic::SI_tbuffer_store: { |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 972 | SDValue Ops[] = { |
| 973 | Chain, |
| 974 | Op.getOperand(2), |
| 975 | Op.getOperand(3), |
| 976 | Op.getOperand(4), |
| 977 | Op.getOperand(5), |
| 978 | Op.getOperand(6), |
| 979 | Op.getOperand(7), |
| 980 | Op.getOperand(8), |
| 981 | Op.getOperand(9), |
| 982 | Op.getOperand(10), |
| 983 | Op.getOperand(11), |
| 984 | Op.getOperand(12), |
| 985 | Op.getOperand(13), |
| 986 | Op.getOperand(14) |
| 987 | }; |
| 988 | |
| 989 | EVT VT = Op.getOperand(3).getValueType(); |
| 990 | |
| 991 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 992 | MachinePointerInfo(), |
| 993 | MachineMemOperand::MOStore, |
| 994 | VT.getStoreSize(), 4); |
| 995 | return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, |
| 996 | Op->getVTList(), Ops, VT, MMO); |
| 997 | } |
| 998 | default: |
| 999 | return SDValue(); |
| 1000 | } |
| 1001 | } |
| 1002 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1003 | SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
| 1004 | SDLoc DL(Op); |
| 1005 | LoadSDNode *Load = cast<LoadSDNode>(Op); |
| 1006 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1007 | if (Op.getValueType().isVector()) { |
| 1008 | assert(Op.getValueType().getVectorElementType() == MVT::i32 && |
| 1009 | "Custom lowering for non-i32 vectors hasn't been implemented."); |
| 1010 | unsigned NumElements = Op.getValueType().getVectorNumElements(); |
| 1011 | assert(NumElements != 2 && "v2 loads are supported for all address spaces."); |
| 1012 | switch (Load->getAddressSpace()) { |
| 1013 | default: break; |
| 1014 | case AMDGPUAS::GLOBAL_ADDRESS: |
| 1015 | case AMDGPUAS::PRIVATE_ADDRESS: |
| 1016 | // v4 loads are supported for private and global memory. |
| 1017 | if (NumElements <= 4) |
| 1018 | break; |
| 1019 | // fall-through |
| 1020 | case AMDGPUAS::LOCAL_ADDRESS: |
Matt Arsenault | 83e6058 | 2014-07-24 17:10:35 +0000 | [diff] [blame] | 1021 | return ScalarizeVectorLoad(Op, DAG); |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1022 | } |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 1023 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1024 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1025 | return AMDGPUTargetLowering::LowerLOAD(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 1028 | SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode, |
| 1029 | const SDValue &Op, |
| 1030 | SelectionDAG &DAG) const { |
| 1031 | return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1), |
| 1032 | Op.getOperand(2), |
Tom Stellard | 868fd92 | 2014-04-17 21:00:11 +0000 | [diff] [blame] | 1033 | Op.getOperand(3), |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 1034 | Op.getOperand(4)); |
| 1035 | } |
| 1036 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1037 | SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { |
| 1038 | if (Op.getValueType() != MVT::i64) |
| 1039 | return SDValue(); |
| 1040 | |
| 1041 | SDLoc DL(Op); |
| 1042 | SDValue Cond = Op.getOperand(0); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1043 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1044 | SDValue Zero = DAG.getConstant(0, DL, MVT::i32); |
| 1045 | SDValue One = DAG.getConstant(1, DL, MVT::i32); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1046 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 1047 | SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); |
| 1048 | SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); |
| 1049 | |
| 1050 | SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); |
| 1051 | 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] | 1052 | |
| 1053 | SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); |
| 1054 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 1055 | SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); |
| 1056 | 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] | 1057 | |
| 1058 | SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); |
| 1059 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 1060 | SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi); |
| 1061 | return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1064 | // Catch division cases where we can use shortcuts with rcp and rsq |
| 1065 | // instructions. |
| 1066 | SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1067 | SDLoc SL(Op); |
| 1068 | SDValue LHS = Op.getOperand(0); |
| 1069 | SDValue RHS = Op.getOperand(1); |
| 1070 | EVT VT = Op.getValueType(); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1071 | bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1072 | |
| 1073 | if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1074 | if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) && |
| 1075 | CLHS->isExactlyValue(1.0)) { |
| 1076 | // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to |
| 1077 | // the CI documentation has a worst case error of 1 ulp. |
| 1078 | // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to |
| 1079 | // use it as long as we aren't trying to use denormals. |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1080 | |
| 1081 | // 1.0 / sqrt(x) -> rsq(x) |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1082 | // |
| 1083 | // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP |
| 1084 | // error seems really high at 2^29 ULP. |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1085 | if (RHS.getOpcode() == ISD::FSQRT) |
| 1086 | return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); |
| 1087 | |
| 1088 | // 1.0 / x -> rcp(x) |
| 1089 | return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); |
| 1090 | } |
| 1091 | } |
| 1092 | |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1093 | if (Unsafe) { |
| 1094 | // Turn into multiply by the reciprocal. |
| 1095 | // x / y -> x * (1.0 / y) |
| 1096 | SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); |
| 1097 | return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip); |
| 1098 | } |
| 1099 | |
| 1100 | return SDValue(); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1104 | SDValue FastLowered = LowerFastFDIV(Op, DAG); |
| 1105 | if (FastLowered.getNode()) |
| 1106 | return FastLowered; |
| 1107 | |
| 1108 | // This uses v_rcp_f32 which does not handle denormals. Let this hit a |
| 1109 | // selection error for now rather than do something incorrect. |
| 1110 | if (Subtarget->hasFP32Denormals()) |
| 1111 | return SDValue(); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1112 | |
| 1113 | SDLoc SL(Op); |
| 1114 | SDValue LHS = Op.getOperand(0); |
| 1115 | SDValue RHS = Op.getOperand(1); |
| 1116 | |
| 1117 | SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); |
| 1118 | |
| 1119 | const APFloat K0Val(BitsToFloat(0x6f800000)); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1120 | const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1121 | |
| 1122 | const APFloat K1Val(BitsToFloat(0x2f800000)); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1123 | const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1124 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1125 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1126 | |
| 1127 | EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32); |
| 1128 | |
| 1129 | SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); |
| 1130 | |
| 1131 | SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); |
| 1132 | |
| 1133 | r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); |
| 1134 | |
| 1135 | SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); |
| 1136 | |
| 1137 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); |
| 1138 | |
| 1139 | return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); |
| 1140 | } |
| 1141 | |
| 1142 | SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 1143 | if (DAG.getTarget().Options.UnsafeFPMath) |
| 1144 | return LowerFastFDIV(Op, DAG); |
| 1145 | |
| 1146 | SDLoc SL(Op); |
| 1147 | SDValue X = Op.getOperand(0); |
| 1148 | SDValue Y = Op.getOperand(1); |
| 1149 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1150 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 1151 | |
| 1152 | SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); |
| 1153 | |
| 1154 | SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); |
| 1155 | |
| 1156 | SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); |
| 1157 | |
| 1158 | SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); |
| 1159 | |
| 1160 | SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); |
| 1161 | |
| 1162 | SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); |
| 1163 | |
| 1164 | SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); |
| 1165 | |
| 1166 | SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); |
| 1167 | |
| 1168 | SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); |
| 1169 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); |
| 1170 | |
| 1171 | SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, |
| 1172 | NegDivScale0, Mul, DivScale1); |
| 1173 | |
| 1174 | SDValue Scale; |
| 1175 | |
| 1176 | if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
| 1177 | // Workaround a hardware bug on SI where the condition output from div_scale |
| 1178 | // is not usable. |
| 1179 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1180 | const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Figure out if the scale to use for div_fmas. |
| 1183 | SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); |
| 1184 | SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); |
| 1185 | SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); |
| 1186 | SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); |
| 1187 | |
| 1188 | SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); |
| 1189 | SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); |
| 1190 | |
| 1191 | SDValue Scale0Hi |
| 1192 | = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); |
| 1193 | SDValue Scale1Hi |
| 1194 | = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); |
| 1195 | |
| 1196 | SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); |
| 1197 | SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); |
| 1198 | Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); |
| 1199 | } else { |
| 1200 | Scale = DivScale1.getValue(1); |
| 1201 | } |
| 1202 | |
| 1203 | SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, |
| 1204 | Fma4, Fma3, Mul, Scale); |
| 1205 | |
| 1206 | return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { |
| 1210 | EVT VT = Op.getValueType(); |
| 1211 | |
| 1212 | if (VT == MVT::f32) |
| 1213 | return LowerFDIV32(Op, DAG); |
| 1214 | |
| 1215 | if (VT == MVT::f64) |
| 1216 | return LowerFDIV64(Op, DAG); |
| 1217 | |
| 1218 | llvm_unreachable("Unexpected type for fdiv"); |
| 1219 | } |
| 1220 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1221 | SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { |
| 1222 | SDLoc DL(Op); |
| 1223 | StoreSDNode *Store = cast<StoreSDNode>(Op); |
| 1224 | EVT VT = Store->getMemoryVT(); |
| 1225 | |
Tom Stellard | 9b3816b | 2014-06-24 23:33:04 +0000 | [diff] [blame] | 1226 | // These stores are legal. |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1227 | if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { |
| 1228 | if (VT.isVector() && VT.getVectorNumElements() > 4) |
Matt Arsenault | 83e6058 | 2014-07-24 17:10:35 +0000 | [diff] [blame] | 1229 | return ScalarizeVectorStore(Op, DAG); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1230 | return SDValue(); |
| 1231 | } |
| 1232 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1233 | SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG); |
| 1234 | if (Ret.getNode()) |
| 1235 | return Ret; |
| 1236 | |
| 1237 | if (VT.isVector() && VT.getVectorNumElements() >= 8) |
Matt Arsenault | 83e6058 | 2014-07-24 17:10:35 +0000 | [diff] [blame] | 1238 | return ScalarizeVectorStore(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1239 | |
Tom Stellard | 1c8788e | 2014-03-07 20:12:33 +0000 | [diff] [blame] | 1240 | if (VT == MVT::i1) |
| 1241 | return DAG.getTruncStore(Store->getChain(), DL, |
| 1242 | DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), |
| 1243 | Store->getBasePtr(), MVT::i1, Store->getMemOperand()); |
| 1244 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1245 | return SDValue(); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1248 | SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1249 | SDLoc DL(Op); |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1250 | EVT VT = Op.getValueType(); |
| 1251 | SDValue Arg = Op.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1252 | SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, |
| 1253 | DAG.getNode(ISD::FMUL, DL, VT, Arg, |
| 1254 | DAG.getConstantFP(0.5/M_PI, DL, |
| 1255 | VT))); |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1256 | |
| 1257 | switch (Op.getOpcode()) { |
| 1258 | case ISD::FCOS: |
| 1259 | return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); |
| 1260 | case ISD::FSIN: |
| 1261 | return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); |
| 1262 | default: |
| 1263 | llvm_unreachable("Wrong trig opcode"); |
| 1264 | } |
| 1265 | } |
| 1266 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1267 | //===----------------------------------------------------------------------===// |
| 1268 | // Custom DAG optimizations |
| 1269 | //===----------------------------------------------------------------------===// |
| 1270 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1271 | SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 1272 | DAGCombinerInfo &DCI) const { |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1273 | EVT VT = N->getValueType(0); |
| 1274 | EVT ScalarVT = VT.getScalarType(); |
| 1275 | if (ScalarVT != MVT::f32) |
| 1276 | return SDValue(); |
| 1277 | |
| 1278 | SelectionDAG &DAG = DCI.DAG; |
| 1279 | SDLoc DL(N); |
| 1280 | |
| 1281 | SDValue Src = N->getOperand(0); |
| 1282 | EVT SrcVT = Src.getValueType(); |
| 1283 | |
| 1284 | // TODO: We could try to match extracting the higher bytes, which would be |
| 1285 | // easier if i8 vectors weren't promoted to i32 vectors, particularly after |
| 1286 | // types are legalized. v4i8 -> v4f32 is probably the only case to worry |
| 1287 | // about in practice. |
| 1288 | if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { |
| 1289 | if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { |
| 1290 | SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); |
| 1291 | DCI.AddToWorklist(Cvt.getNode()); |
| 1292 | return Cvt; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | // We are primarily trying to catch operations on illegal vector types |
| 1297 | // before they are expanded. |
| 1298 | // For scalars, we can use the more flexible method of checking masked bits |
| 1299 | // after legalization. |
| 1300 | if (!DCI.isBeforeLegalize() || |
| 1301 | !SrcVT.isVector() || |
| 1302 | SrcVT.getVectorElementType() != MVT::i8) { |
| 1303 | return SDValue(); |
| 1304 | } |
| 1305 | |
| 1306 | assert(DCI.isBeforeLegalize() && "Unexpected legal type"); |
| 1307 | |
| 1308 | // Weird sized vectors are a pain to handle, but we know 3 is really the same |
| 1309 | // size as 4. |
| 1310 | unsigned NElts = SrcVT.getVectorNumElements(); |
| 1311 | if (!SrcVT.isSimple() && NElts != 3) |
| 1312 | return SDValue(); |
| 1313 | |
| 1314 | // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to |
| 1315 | // prevent a mess from expanding to v4i32 and repacking. |
| 1316 | if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) { |
| 1317 | EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT); |
| 1318 | EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT); |
| 1319 | EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts); |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1320 | LoadSDNode *Load = cast<LoadSDNode>(Src); |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 1321 | |
| 1322 | unsigned AS = Load->getAddressSpace(); |
| 1323 | unsigned Align = Load->getAlignment(); |
| 1324 | Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext()); |
| 1325 | unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty); |
| 1326 | |
| 1327 | // Don't try to replace the load if we have to expand it due to alignment |
| 1328 | // problems. Otherwise we will end up scalarizing the load, and trying to |
| 1329 | // repack into the vector for no real reason. |
| 1330 | if (Align < ABIAlignment && |
| 1331 | !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) { |
| 1332 | return SDValue(); |
| 1333 | } |
| 1334 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1335 | SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT, |
| 1336 | Load->getChain(), |
| 1337 | Load->getBasePtr(), |
| 1338 | LoadVT, |
| 1339 | Load->getMemOperand()); |
| 1340 | |
| 1341 | // Make sure successors of the original load stay after it by updating |
| 1342 | // them to use the new Chain. |
| 1343 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1)); |
| 1344 | |
| 1345 | SmallVector<SDValue, 4> Elts; |
| 1346 | if (RegVT.isVector()) |
| 1347 | DAG.ExtractVectorElements(NewLoad, Elts); |
| 1348 | else |
| 1349 | Elts.push_back(NewLoad); |
| 1350 | |
| 1351 | SmallVector<SDValue, 4> Ops; |
| 1352 | |
| 1353 | unsigned EltIdx = 0; |
| 1354 | for (SDValue Elt : Elts) { |
| 1355 | unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx); |
| 1356 | for (unsigned I = 0; I < ComponentsInElt; ++I) { |
| 1357 | unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I; |
| 1358 | SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt); |
| 1359 | DCI.AddToWorklist(Cvt.getNode()); |
| 1360 | Ops.push_back(Cvt); |
| 1361 | } |
| 1362 | |
| 1363 | ++EltIdx; |
| 1364 | } |
| 1365 | |
| 1366 | assert(Ops.size() == NElts); |
| 1367 | |
| 1368 | return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops); |
| 1369 | } |
| 1370 | |
| 1371 | return SDValue(); |
| 1372 | } |
| 1373 | |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 1374 | /// \brief Return true if the given offset Size in bytes can be folded into |
| 1375 | /// the immediate offsets of a memory instruction for the given address space. |
| 1376 | static bool canFoldOffset(unsigned OffsetSize, unsigned AS, |
| 1377 | const AMDGPUSubtarget &STI) { |
| 1378 | switch (AS) { |
| 1379 | case AMDGPUAS::GLOBAL_ADDRESS: { |
| 1380 | // MUBUF instructions a 12-bit offset in bytes. |
| 1381 | return isUInt<12>(OffsetSize); |
| 1382 | } |
| 1383 | case AMDGPUAS::CONSTANT_ADDRESS: { |
| 1384 | // SMRD instructions have an 8-bit offset in dwords on SI and |
| 1385 | // a 20-bit offset in bytes on VI. |
| 1386 | if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 1387 | return isUInt<20>(OffsetSize); |
| 1388 | else |
| 1389 | return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); |
| 1390 | } |
| 1391 | case AMDGPUAS::LOCAL_ADDRESS: |
| 1392 | case AMDGPUAS::REGION_ADDRESS: { |
| 1393 | // The single offset versions have a 16-bit offset in bytes. |
| 1394 | return isUInt<16>(OffsetSize); |
| 1395 | } |
| 1396 | case AMDGPUAS::PRIVATE_ADDRESS: |
| 1397 | // Indirect register addressing does not use any offsets. |
| 1398 | default: |
| 1399 | return 0; |
| 1400 | } |
| 1401 | } |
| 1402 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1403 | // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) |
| 1404 | |
| 1405 | // This is a variant of |
| 1406 | // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), |
| 1407 | // |
| 1408 | // The normal DAG combiner will do this, but only if the add has one use since |
| 1409 | // that would increase the number of instructions. |
| 1410 | // |
| 1411 | // This prevents us from seeing a constant offset that can be folded into a |
| 1412 | // memory instruction's addressing mode. If we know the resulting add offset of |
| 1413 | // a pointer can be folded into an addressing offset, we can replace the pointer |
| 1414 | // operand with the add of new constant offset. This eliminates one of the uses, |
| 1415 | // and may allow the remaining use to also be simplified. |
| 1416 | // |
| 1417 | SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, |
| 1418 | unsigned AddrSpace, |
| 1419 | DAGCombinerInfo &DCI) const { |
| 1420 | SDValue N0 = N->getOperand(0); |
| 1421 | SDValue N1 = N->getOperand(1); |
| 1422 | |
| 1423 | if (N0.getOpcode() != ISD::ADD) |
| 1424 | return SDValue(); |
| 1425 | |
| 1426 | const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); |
| 1427 | if (!CN1) |
| 1428 | return SDValue(); |
| 1429 | |
| 1430 | const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 1431 | if (!CAdd) |
| 1432 | return SDValue(); |
| 1433 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1434 | // If the resulting offset is too large, we can't fold it into the addressing |
| 1435 | // mode offset. |
| 1436 | APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 1437 | if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget)) |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1438 | return SDValue(); |
| 1439 | |
| 1440 | SelectionDAG &DAG = DCI.DAG; |
| 1441 | SDLoc SL(N); |
| 1442 | EVT VT = N->getValueType(0); |
| 1443 | |
| 1444 | SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1445 | SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1446 | |
| 1447 | return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); |
| 1448 | } |
| 1449 | |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 1450 | SDValue SITargetLowering::performAndCombine(SDNode *N, |
| 1451 | DAGCombinerInfo &DCI) const { |
| 1452 | if (DCI.isBeforeLegalize()) |
| 1453 | return SDValue(); |
| 1454 | |
| 1455 | SelectionDAG &DAG = DCI.DAG; |
| 1456 | |
| 1457 | // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> |
| 1458 | // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) |
| 1459 | SDValue LHS = N->getOperand(0); |
| 1460 | SDValue RHS = N->getOperand(1); |
| 1461 | |
| 1462 | if (LHS.getOpcode() == ISD::SETCC && |
| 1463 | RHS.getOpcode() == ISD::SETCC) { |
| 1464 | ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); |
| 1465 | ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); |
| 1466 | |
| 1467 | SDValue X = LHS.getOperand(0); |
| 1468 | SDValue Y = RHS.getOperand(0); |
| 1469 | if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) |
| 1470 | return SDValue(); |
| 1471 | |
| 1472 | if (LCC == ISD::SETO) { |
| 1473 | if (X != LHS.getOperand(1)) |
| 1474 | return SDValue(); |
| 1475 | |
| 1476 | if (RCC == ISD::SETUNE) { |
| 1477 | const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); |
| 1478 | if (!C1 || !C1->isInfinity() || C1->isNegative()) |
| 1479 | return SDValue(); |
| 1480 | |
| 1481 | const uint32_t Mask = SIInstrFlags::N_NORMAL | |
| 1482 | SIInstrFlags::N_SUBNORMAL | |
| 1483 | SIInstrFlags::N_ZERO | |
| 1484 | SIInstrFlags::P_ZERO | |
| 1485 | SIInstrFlags::P_SUBNORMAL | |
| 1486 | SIInstrFlags::P_NORMAL; |
| 1487 | |
| 1488 | static_assert(((~(SIInstrFlags::S_NAN | |
| 1489 | SIInstrFlags::Q_NAN | |
| 1490 | SIInstrFlags::N_INFINITY | |
| 1491 | SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, |
| 1492 | "mask not equal"); |
| 1493 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1494 | SDLoc DL(N); |
| 1495 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, |
| 1496 | X, DAG.getConstant(Mask, DL, MVT::i32)); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | return SDValue(); |
| 1502 | } |
| 1503 | |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 1504 | SDValue SITargetLowering::performOrCombine(SDNode *N, |
| 1505 | DAGCombinerInfo &DCI) const { |
| 1506 | SelectionDAG &DAG = DCI.DAG; |
| 1507 | SDValue LHS = N->getOperand(0); |
| 1508 | SDValue RHS = N->getOperand(1); |
| 1509 | |
| 1510 | // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) |
| 1511 | if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && |
| 1512 | RHS.getOpcode() == AMDGPUISD::FP_CLASS) { |
| 1513 | SDValue Src = LHS.getOperand(0); |
| 1514 | if (Src != RHS.getOperand(0)) |
| 1515 | return SDValue(); |
| 1516 | |
| 1517 | const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); |
| 1518 | const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); |
| 1519 | if (!CLHS || !CRHS) |
| 1520 | return SDValue(); |
| 1521 | |
| 1522 | // Only 10 bits are used. |
| 1523 | static const uint32_t MaxMask = 0x3ff; |
| 1524 | |
| 1525 | uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1526 | SDLoc DL(N); |
| 1527 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, |
| 1528 | Src, DAG.getConstant(NewMask, DL, MVT::i32)); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | return SDValue(); |
| 1532 | } |
| 1533 | |
| 1534 | SDValue SITargetLowering::performClassCombine(SDNode *N, |
| 1535 | DAGCombinerInfo &DCI) const { |
| 1536 | SelectionDAG &DAG = DCI.DAG; |
| 1537 | SDValue Mask = N->getOperand(1); |
| 1538 | |
| 1539 | // fp_class x, 0 -> false |
| 1540 | if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { |
| 1541 | if (CMask->isNullValue()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1542 | return DAG.getConstant(0, SDLoc(N), MVT::i1); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | return SDValue(); |
| 1546 | } |
| 1547 | |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 1548 | static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { |
| 1549 | switch (Opc) { |
| 1550 | case ISD::FMAXNUM: |
| 1551 | return AMDGPUISD::FMAX3; |
| 1552 | case AMDGPUISD::SMAX: |
| 1553 | return AMDGPUISD::SMAX3; |
| 1554 | case AMDGPUISD::UMAX: |
| 1555 | return AMDGPUISD::UMAX3; |
| 1556 | case ISD::FMINNUM: |
| 1557 | return AMDGPUISD::FMIN3; |
| 1558 | case AMDGPUISD::SMIN: |
| 1559 | return AMDGPUISD::SMIN3; |
| 1560 | case AMDGPUISD::UMIN: |
| 1561 | return AMDGPUISD::UMIN3; |
| 1562 | default: |
| 1563 | llvm_unreachable("Not a min/max opcode"); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | SDValue SITargetLowering::performMin3Max3Combine(SDNode *N, |
| 1568 | DAGCombinerInfo &DCI) const { |
| 1569 | SelectionDAG &DAG = DCI.DAG; |
| 1570 | |
| 1571 | unsigned Opc = N->getOpcode(); |
| 1572 | SDValue Op0 = N->getOperand(0); |
| 1573 | SDValue Op1 = N->getOperand(1); |
| 1574 | |
| 1575 | // Only do this if the inner op has one use since this will just increases |
| 1576 | // register pressure for no benefit. |
| 1577 | |
| 1578 | // max(max(a, b), c) |
| 1579 | if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { |
| 1580 | SDLoc DL(N); |
| 1581 | return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), |
| 1582 | DL, |
| 1583 | N->getValueType(0), |
| 1584 | Op0.getOperand(0), |
| 1585 | Op0.getOperand(1), |
| 1586 | Op1); |
| 1587 | } |
| 1588 | |
| 1589 | // max(a, max(b, c)) |
| 1590 | if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { |
| 1591 | SDLoc DL(N); |
| 1592 | return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), |
| 1593 | DL, |
| 1594 | N->getValueType(0), |
| 1595 | Op0, |
| 1596 | Op1.getOperand(0), |
| 1597 | Op1.getOperand(1)); |
| 1598 | } |
| 1599 | |
| 1600 | return SDValue(); |
| 1601 | } |
| 1602 | |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 1603 | SDValue SITargetLowering::performSetCCCombine(SDNode *N, |
| 1604 | DAGCombinerInfo &DCI) const { |
| 1605 | SelectionDAG &DAG = DCI.DAG; |
| 1606 | SDLoc SL(N); |
| 1607 | |
| 1608 | SDValue LHS = N->getOperand(0); |
| 1609 | SDValue RHS = N->getOperand(1); |
| 1610 | EVT VT = LHS.getValueType(); |
| 1611 | |
| 1612 | if (VT != MVT::f32 && VT != MVT::f64) |
| 1613 | return SDValue(); |
| 1614 | |
| 1615 | // Match isinf pattern |
| 1616 | // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) |
| 1617 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 1618 | if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { |
| 1619 | const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); |
| 1620 | if (!CRHS) |
| 1621 | return SDValue(); |
| 1622 | |
| 1623 | const APFloat &APF = CRHS->getValueAPF(); |
| 1624 | if (APF.isInfinity() && !APF.isNegative()) { |
| 1625 | unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1626 | return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), |
| 1627 | DAG.getConstant(Mask, SL, MVT::i32)); |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | return SDValue(); |
| 1632 | } |
| 1633 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1634 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 1635 | DAGCombinerInfo &DCI) const { |
| 1636 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1637 | SDLoc DL(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1638 | |
| 1639 | switch (N->getOpcode()) { |
Matt Arsenault | 22b4c25 | 2014-12-21 16:48:42 +0000 | [diff] [blame] | 1640 | default: |
| 1641 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 1642 | case ISD::SETCC: |
| 1643 | return performSetCCCombine(N, DCI); |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 1644 | case ISD::FMAXNUM: // TODO: What about fmax_legacy? |
| 1645 | case ISD::FMINNUM: |
| 1646 | case AMDGPUISD::SMAX: |
| 1647 | case AMDGPUISD::SMIN: |
| 1648 | case AMDGPUISD::UMAX: |
| 1649 | case AMDGPUISD::UMIN: { |
| 1650 | if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && |
Tom Stellard | 7c840bc | 2015-03-16 15:53:55 +0000 | [diff] [blame] | 1651 | N->getValueType(0) != MVT::f64 && |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 1652 | getTargetMachine().getOptLevel() > CodeGenOpt::None) |
| 1653 | return performMin3Max3Combine(N, DCI); |
| 1654 | break; |
| 1655 | } |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1656 | |
| 1657 | case AMDGPUISD::CVT_F32_UBYTE0: |
| 1658 | case AMDGPUISD::CVT_F32_UBYTE1: |
| 1659 | case AMDGPUISD::CVT_F32_UBYTE2: |
| 1660 | case AMDGPUISD::CVT_F32_UBYTE3: { |
| 1661 | unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; |
| 1662 | |
| 1663 | SDValue Src = N->getOperand(0); |
| 1664 | APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); |
| 1665 | |
| 1666 | APInt KnownZero, KnownOne; |
| 1667 | TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), |
| 1668 | !DCI.isBeforeLegalizeOps()); |
| 1669 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 1670 | if (TLO.ShrinkDemandedConstant(Src, Demanded) || |
| 1671 | TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { |
| 1672 | DCI.CommitTargetLoweringOpt(TLO); |
| 1673 | } |
| 1674 | |
| 1675 | break; |
| 1676 | } |
| 1677 | |
| 1678 | case ISD::UINT_TO_FP: { |
| 1679 | return performUCharToFloatCombine(N, DCI); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 1680 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 1681 | case ISD::FADD: { |
| 1682 | if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) |
| 1683 | break; |
| 1684 | |
| 1685 | EVT VT = N->getValueType(0); |
| 1686 | if (VT != MVT::f32) |
| 1687 | break; |
| 1688 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1689 | // Only do this if we are not trying to support denormals. v_mad_f32 does |
| 1690 | // not support denormals ever. |
| 1691 | if (Subtarget->hasFP32Denormals()) |
| 1692 | break; |
| 1693 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 1694 | SDValue LHS = N->getOperand(0); |
| 1695 | SDValue RHS = N->getOperand(1); |
| 1696 | |
| 1697 | // These should really be instruction patterns, but writing patterns with |
| 1698 | // source modiifiers is a pain. |
| 1699 | |
| 1700 | // fadd (fadd (a, a), b) -> mad 2.0, a, b |
| 1701 | if (LHS.getOpcode() == ISD::FADD) { |
| 1702 | SDValue A = LHS.getOperand(0); |
| 1703 | if (A == LHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1704 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1705 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | // fadd (b, fadd (a, a)) -> mad 2.0, a, b |
| 1710 | if (RHS.getOpcode() == ISD::FADD) { |
| 1711 | SDValue A = RHS.getOperand(0); |
| 1712 | if (A == RHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1713 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1714 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 1715 | } |
| 1716 | } |
| 1717 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1718 | return SDValue(); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 1719 | } |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 1720 | case ISD::FSUB: { |
| 1721 | if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) |
| 1722 | break; |
| 1723 | |
| 1724 | EVT VT = N->getValueType(0); |
| 1725 | |
| 1726 | // Try to get the fneg to fold into the source modifier. This undoes generic |
| 1727 | // DAG combines and folds them into the mad. |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1728 | // |
| 1729 | // Only do this if we are not trying to support denormals. v_mad_f32 does |
| 1730 | // not support denormals ever. |
| 1731 | if (VT == MVT::f32 && |
| 1732 | !Subtarget->hasFP32Denormals()) { |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 1733 | SDValue LHS = N->getOperand(0); |
| 1734 | SDValue RHS = N->getOperand(1); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 1735 | if (LHS.getOpcode() == ISD::FADD) { |
| 1736 | // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) |
| 1737 | |
| 1738 | SDValue A = LHS.getOperand(0); |
| 1739 | if (A == LHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1740 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 1741 | SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS); |
| 1742 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1743 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | if (RHS.getOpcode() == ISD::FADD) { |
| 1748 | // (fsub c, (fadd a, a)) -> mad -2.0, a, c |
| 1749 | |
| 1750 | SDValue A = RHS.getOperand(0); |
| 1751 | if (A == RHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1752 | const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1753 | return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 1754 | } |
| 1755 | } |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1756 | |
| 1757 | return SDValue(); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | break; |
| 1761 | } |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1762 | } |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1763 | case ISD::LOAD: |
| 1764 | case ISD::STORE: |
| 1765 | case ISD::ATOMIC_LOAD: |
| 1766 | case ISD::ATOMIC_STORE: |
| 1767 | case ISD::ATOMIC_CMP_SWAP: |
| 1768 | case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: |
| 1769 | case ISD::ATOMIC_SWAP: |
| 1770 | case ISD::ATOMIC_LOAD_ADD: |
| 1771 | case ISD::ATOMIC_LOAD_SUB: |
| 1772 | case ISD::ATOMIC_LOAD_AND: |
| 1773 | case ISD::ATOMIC_LOAD_OR: |
| 1774 | case ISD::ATOMIC_LOAD_XOR: |
| 1775 | case ISD::ATOMIC_LOAD_NAND: |
| 1776 | case ISD::ATOMIC_LOAD_MIN: |
| 1777 | case ISD::ATOMIC_LOAD_MAX: |
| 1778 | case ISD::ATOMIC_LOAD_UMIN: |
| 1779 | case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics. |
| 1780 | if (DCI.isBeforeLegalize()) |
| 1781 | break; |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 1782 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1783 | MemSDNode *MemNode = cast<MemSDNode>(N); |
| 1784 | SDValue Ptr = MemNode->getBasePtr(); |
| 1785 | |
| 1786 | // TODO: We could also do this for multiplies. |
| 1787 | unsigned AS = MemNode->getAddressSpace(); |
| 1788 | if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { |
| 1789 | SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); |
| 1790 | if (NewPtr) { |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 1791 | SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end()); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1792 | |
| 1793 | NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; |
| 1794 | return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0); |
| 1795 | } |
| 1796 | } |
| 1797 | break; |
| 1798 | } |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 1799 | case ISD::AND: |
| 1800 | return performAndCombine(N, DCI); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 1801 | case ISD::OR: |
| 1802 | return performOrCombine(N, DCI); |
| 1803 | case AMDGPUISD::FP_CLASS: |
| 1804 | return performClassCombine(N, DCI); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1805 | } |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 1806 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1807 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 1808 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1809 | /// \brief Analyze the possible immediate value Op |
| 1810 | /// |
| 1811 | /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate |
| 1812 | /// and the immediate value if it's a literal immediate |
| 1813 | int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { |
| 1814 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1815 | const SIInstrInfo *TII = |
| 1816 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1817 | |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 1818 | if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) { |
Matt Arsenault | 303011a | 2014-12-17 21:04:08 +0000 | [diff] [blame] | 1819 | if (TII->isInlineConstant(Node->getAPIntValue())) |
| 1820 | return 0; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1821 | |
Matt Arsenault | 11a4d67 | 2015-02-13 19:05:03 +0000 | [diff] [blame] | 1822 | uint64_t Val = Node->getZExtValue(); |
| 1823 | return isUInt<32>(Val) ? Val : -1; |
Matt Arsenault | 303011a | 2014-12-17 21:04:08 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) { |
| 1827 | if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt())) |
| 1828 | return 0; |
| 1829 | |
| 1830 | if (Node->getValueType(0) == MVT::f32) |
| 1831 | return FloatToBits(Node->getValueAPF().convertToFloat()); |
| 1832 | |
| 1833 | return -1; |
| 1834 | } |
| 1835 | |
| 1836 | return -1; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1839 | /// \brief Helper function for adjustWritemask |
Benjamin Kramer | 635e368 | 2013-05-23 15:43:05 +0000 | [diff] [blame] | 1840 | static unsigned SubIdx2Lane(unsigned Idx) { |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1841 | switch (Idx) { |
| 1842 | default: return 0; |
| 1843 | case AMDGPU::sub0: return 0; |
| 1844 | case AMDGPU::sub1: return 1; |
| 1845 | case AMDGPU::sub2: return 2; |
| 1846 | case AMDGPU::sub3: return 3; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | /// \brief Adjust the writemask of MIMG instructions |
| 1851 | void SITargetLowering::adjustWritemask(MachineSDNode *&Node, |
| 1852 | SelectionDAG &DAG) const { |
| 1853 | SDNode *Users[4] = { }; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1854 | unsigned Lane = 0; |
| 1855 | unsigned OldDmask = Node->getConstantOperandVal(0); |
| 1856 | unsigned NewDmask = 0; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Try to figure out the used register components |
| 1859 | for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); |
| 1860 | I != E; ++I) { |
| 1861 | |
| 1862 | // Abort if we can't understand the usage |
| 1863 | if (!I->isMachineOpcode() || |
| 1864 | I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) |
| 1865 | return; |
| 1866 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1867 | // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. |
| 1868 | // Note that subregs are packed, i.e. Lane==0 is the first bit set |
| 1869 | // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit |
| 1870 | // set, etc. |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1871 | Lane = SubIdx2Lane(I->getConstantOperandVal(1)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1872 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1873 | // Set which texture component corresponds to the lane. |
| 1874 | unsigned Comp; |
| 1875 | for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { |
| 1876 | assert(Dmask); |
Tom Stellard | 03a5c08 | 2013-10-23 03:50:25 +0000 | [diff] [blame] | 1877 | Comp = countTrailingZeros(Dmask); |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1878 | Dmask &= ~(1 << Comp); |
| 1879 | } |
| 1880 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1881 | // Abort if we have more than one user per component |
| 1882 | if (Users[Lane]) |
| 1883 | return; |
| 1884 | |
| 1885 | Users[Lane] = *I; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1886 | NewDmask |= 1 << Comp; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1889 | // Abort if there's no change |
| 1890 | if (NewDmask == OldDmask) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1891 | return; |
| 1892 | |
| 1893 | // Adjust the writemask in the node |
| 1894 | std::vector<SDValue> Ops; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1895 | Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 1896 | Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end()); |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 1897 | Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1898 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1899 | // If we only got one lane, replace it with a copy |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 1900 | // (if NewDmask has only one bit set...) |
| 1901 | if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1902 | SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), |
| 1903 | MVT::i32); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1904 | SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1905 | SDLoc(), Users[Lane]->getValueType(0), |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1906 | SDValue(Node, 0), RC); |
| 1907 | DAG.ReplaceAllUsesWith(Users[Lane], Copy); |
| 1908 | return; |
| 1909 | } |
| 1910 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1911 | // Update the users of the node with the new indices |
| 1912 | for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { |
| 1913 | |
| 1914 | SDNode *User = Users[i]; |
| 1915 | if (!User) |
| 1916 | continue; |
| 1917 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1918 | SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1919 | DAG.UpdateNodeOperands(User, User->getOperand(0), Op); |
| 1920 | |
| 1921 | switch (Idx) { |
| 1922 | default: break; |
| 1923 | case AMDGPU::sub0: Idx = AMDGPU::sub1; break; |
| 1924 | case AMDGPU::sub1: Idx = AMDGPU::sub2; break; |
| 1925 | case AMDGPU::sub2: Idx = AMDGPU::sub3; break; |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 1930 | /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) |
| 1931 | /// with frame index operands. |
| 1932 | /// LLVM assumes that inputs are to these instructions are registers. |
| 1933 | void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, |
| 1934 | SelectionDAG &DAG) const { |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 1935 | |
| 1936 | SmallVector<SDValue, 8> Ops; |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 1937 | for (unsigned i = 0; i < Node->getNumOperands(); ++i) { |
| 1938 | if (!isa<FrameIndexSDNode>(Node->getOperand(i))) { |
| 1939 | Ops.push_back(Node->getOperand(i)); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 1940 | continue; |
| 1941 | } |
| 1942 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 1943 | SDLoc DL(Node); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 1944 | Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 1945 | Node->getOperand(i).getValueType(), |
| 1946 | Node->getOperand(i)), 0)); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 1949 | DAG.UpdateNodeOperands(Node, Ops); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 1952 | /// \brief Fold the instructions after selecting them. |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1953 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 1954 | SelectionDAG &DAG) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1955 | const SIInstrInfo *TII = |
| 1956 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1957 | |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 1958 | if (TII->isMIMG(Node->getMachineOpcode())) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1959 | adjustWritemask(Node, DAG); |
| 1960 | |
Matt Arsenault | 7d858d8 | 2014-11-02 23:46:54 +0000 | [diff] [blame] | 1961 | if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG || |
| 1962 | Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) { |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 1963 | legalizeTargetIndependentNode(Node, DAG); |
| 1964 | return Node; |
| 1965 | } |
Tom Stellard | 654d669 | 2015-01-08 15:08:17 +0000 | [diff] [blame] | 1966 | return Node; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 1967 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1968 | |
| 1969 | /// \brief Assign the register class depending on the number of |
| 1970 | /// bits set in the writemask |
| 1971 | void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, |
| 1972 | SDNode *Node) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1973 | const SIInstrInfo *TII = |
| 1974 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 1975 | |
Tom Stellard | a99ada5 | 2014-11-21 22:31:44 +0000 | [diff] [blame] | 1976 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
Matt Arsenault | cb0ac3d | 2014-09-26 17:54:59 +0000 | [diff] [blame] | 1977 | TII->legalizeOperands(MI); |
| 1978 | |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 1979 | if (TII->isMIMG(MI->getOpcode())) { |
| 1980 | unsigned VReg = MI->getOperand(0).getReg(); |
| 1981 | unsigned Writemask = MI->getOperand(1).getImm(); |
| 1982 | unsigned BitsSet = 0; |
| 1983 | for (unsigned i = 0; i < 4; ++i) |
| 1984 | BitsSet += Writemask & (1 << i) ? 1 : 0; |
| 1985 | |
| 1986 | const TargetRegisterClass *RC; |
| 1987 | switch (BitsSet) { |
| 1988 | default: return; |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 1989 | case 1: RC = &AMDGPU::VGPR_32RegClass; break; |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 1990 | case 2: RC = &AMDGPU::VReg_64RegClass; break; |
| 1991 | case 3: RC = &AMDGPU::VReg_96RegClass; break; |
| 1992 | } |
| 1993 | |
| 1994 | unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet); |
| 1995 | MI->setDesc(TII->get(NewOpcode)); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 1996 | MRI.setRegClass(VReg, RC); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1997 | return; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 2000 | // Replace unused atomics with the no return version. |
| 2001 | int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode()); |
| 2002 | if (NoRetAtomicOp != -1) { |
| 2003 | if (!Node->hasAnyUseOfValue(0)) { |
| 2004 | MI->setDesc(TII->get(NoRetAtomicOp)); |
| 2005 | MI->RemoveOperand(0); |
| 2006 | } |
| 2007 | |
| 2008 | return; |
| 2009 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2010 | } |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 2011 | |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2012 | static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2013 | SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2014 | return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); |
| 2015 | } |
| 2016 | |
| 2017 | MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, |
| 2018 | SDLoc DL, |
| 2019 | SDValue Ptr) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 2020 | const SIInstrInfo *TII = |
| 2021 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2022 | #if 1 |
| 2023 | // XXX - Workaround for moveToVALU not handling different register class |
| 2024 | // inserts for REG_SEQUENCE. |
| 2025 | |
| 2026 | // Build the half of the subregister with the constants. |
| 2027 | const SDValue Ops0[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2028 | DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2029 | buildSMovImm32(DAG, DL, 0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2030 | DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 2031 | buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2032 | DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2033 | }; |
| 2034 | |
| 2035 | SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, |
| 2036 | MVT::v2i32, Ops0), 0); |
| 2037 | |
| 2038 | // Combine the constants and the pointer. |
| 2039 | const SDValue Ops1[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2040 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2041 | Ptr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2042 | DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2043 | SubRegHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2044 | DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2045 | }; |
| 2046 | |
| 2047 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); |
| 2048 | #else |
| 2049 | const SDValue Ops[] = { |
| 2050 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32), |
| 2051 | Ptr, |
| 2052 | DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32), |
| 2053 | buildSMovImm32(DAG, DL, 0), |
| 2054 | DAG.getTargetConstant(AMDGPU::sub2, MVT::i32), |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 2055 | buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32), |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2056 | DAG.getTargetConstant(AMDGPU::sub3, MVT::i32) |
| 2057 | }; |
| 2058 | |
| 2059 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); |
| 2060 | |
| 2061 | #endif |
| 2062 | } |
| 2063 | |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2064 | /// \brief Return a resource descriptor with the 'Add TID' bit enabled |
| 2065 | /// The TID (Thread ID) is multipled by the stride value (bits [61:48] |
| 2066 | /// of the resource descriptor) to create an offset, which is added to the |
| 2067 | /// resource ponter. |
| 2068 | MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, |
| 2069 | SDLoc DL, |
| 2070 | SDValue Ptr, |
| 2071 | uint32_t RsrcDword1, |
| 2072 | uint64_t RsrcDword2And3) const { |
| 2073 | SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); |
| 2074 | SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); |
| 2075 | if (RsrcDword1) { |
| 2076 | PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2077 | DAG.getConstant(RsrcDword1, DL, MVT::i32)), |
| 2078 | 0); |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | SDValue DataLo = buildSMovImm32(DAG, DL, |
| 2082 | RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); |
| 2083 | SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); |
| 2084 | |
| 2085 | const SDValue Ops[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2086 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2087 | PtrLo, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2088 | DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2089 | PtrHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2090 | DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2091 | DataLo, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2092 | DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2093 | DataHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2094 | DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2095 | }; |
| 2096 | |
| 2097 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); |
| 2098 | } |
| 2099 | |
| 2100 | MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG, |
| 2101 | SDLoc DL, |
| 2102 | SDValue Ptr) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 2103 | const SIInstrInfo *TII = |
| 2104 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Tom Stellard | 794c8c0 | 2014-12-02 17:05:41 +0000 | [diff] [blame] | 2105 | uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE | |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2106 | 0xffffffff; // Size |
| 2107 | |
| 2108 | return buildRSRC(DAG, DL, Ptr, 0, Rsrc); |
| 2109 | } |
| 2110 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 2111 | SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, |
| 2112 | const TargetRegisterClass *RC, |
| 2113 | unsigned Reg, EVT VT) const { |
| 2114 | SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); |
| 2115 | |
| 2116 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), |
| 2117 | cast<RegisterSDNode>(VReg)->getReg(), VT); |
| 2118 | } |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 2119 | |
| 2120 | //===----------------------------------------------------------------------===// |
| 2121 | // SI Inline Assembly Support |
| 2122 | //===----------------------------------------------------------------------===// |
| 2123 | |
| 2124 | std::pair<unsigned, const TargetRegisterClass *> |
| 2125 | SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, |
| 2126 | const std::string &Constraint, |
| 2127 | MVT VT) const { |
| 2128 | if (Constraint == "r") { |
| 2129 | switch(VT.SimpleTy) { |
| 2130 | default: llvm_unreachable("Unhandled type for 'r' inline asm constraint"); |
| 2131 | case MVT::i64: |
| 2132 | return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); |
| 2133 | case MVT::i32: |
| 2134 | return std::make_pair(0U, &AMDGPU::SGPR_32RegClass); |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | if (Constraint.size() > 1) { |
| 2139 | const TargetRegisterClass *RC = nullptr; |
| 2140 | if (Constraint[1] == 'v') { |
| 2141 | RC = &AMDGPU::VGPR_32RegClass; |
| 2142 | } else if (Constraint[1] == 's') { |
| 2143 | RC = &AMDGPU::SGPR_32RegClass; |
| 2144 | } |
| 2145 | |
| 2146 | if (RC) { |
| 2147 | unsigned Idx = std::atoi(Constraint.substr(2).c_str()); |
| 2148 | if (Idx < RC->getNumRegs()) |
| 2149 | return std::make_pair(RC->getRegister(Idx), RC); |
| 2150 | } |
| 2151 | } |
| 2152 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
| 2153 | } |