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 | d48da14 | 2015-11-02 23:23:02 +0000 | [diff] [blame] | 23 | #include "AMDGPUDiagnosticInfoUnsupported.h" |
Matt Arsenault | c791f39 | 2014-06-23 18:00:31 +0000 | [diff] [blame] | 24 | #include "AMDGPUIntrinsicInfo.h" |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 25 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 26 | #include "SIInstrInfo.h" |
| 27 | #include "SIMachineFunctionInfo.h" |
| 28 | #include "SIRegisterInfo.h" |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/BitVector.h" |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/CallingConvLower.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 33 | #include "llvm/CodeGen/SelectionDAG.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Function.h" |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallString.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace llvm; |
| 38 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 39 | SITargetLowering::SITargetLowering(TargetMachine &TM, |
| 40 | const AMDGPUSubtarget &STI) |
| 41 | : AMDGPUTargetLowering(TM, STI) { |
Tom Stellard | 1bd8072 | 2014-04-30 15:31:33 +0000 | [diff] [blame] | 42 | addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 43 | addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 44 | |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 45 | addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass); |
| 46 | addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass); |
| 47 | |
Tom Stellard | 334b29c | 2014-04-17 21:00:09 +0000 | [diff] [blame] | 48 | addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass); |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 49 | addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 50 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 51 | addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); |
| 52 | addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); |
| 53 | addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 54 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 55 | addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass); |
| 56 | addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass); |
| 57 | |
Tom Stellard | 436780b | 2014-05-15 14:41:57 +0000 | [diff] [blame] | 58 | addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); |
| 59 | addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 60 | |
Tom Stellard | f0a2107 | 2014-11-18 20:39:39 +0000 | [diff] [blame] | 61 | addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 62 | addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); |
| 63 | |
Tom Stellard | f0a2107 | 2014-11-18 20:39:39 +0000 | [diff] [blame] | 64 | addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); |
Christian Konig | 2214f14 | 2013-03-07 09:03:38 +0000 | [diff] [blame] | 65 | addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 66 | |
Eric Christopher | 23a3a7c | 2015-02-26 00:00:24 +0000 | [diff] [blame] | 67 | computeRegisterProperties(STI.getRegisterInfo()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 68 | |
Christian Konig | 2989ffc | 2013-03-18 11:34:16 +0000 | [diff] [blame] | 69 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); |
| 70 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); |
| 71 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); |
| 72 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); |
| 73 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 74 | setOperationAction(ISD::ADD, MVT::i32, Legal); |
Matt Arsenault | e8d2146 | 2013-11-18 20:09:40 +0000 | [diff] [blame] | 75 | setOperationAction(ISD::ADDC, MVT::i32, Legal); |
| 76 | setOperationAction(ISD::ADDE, MVT::i32, Legal); |
Matt Arsenault | b8b5153 | 2014-06-23 18:00:38 +0000 | [diff] [blame] | 77 | setOperationAction(ISD::SUBC, MVT::i32, Legal); |
| 78 | setOperationAction(ISD::SUBE, MVT::i32, Legal); |
Aaron Watry | daabb20 | 2013-06-25 13:55:52 +0000 | [diff] [blame] | 79 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 80 | setOperationAction(ISD::FSIN, MVT::f32, Custom); |
| 81 | setOperationAction(ISD::FCOS, MVT::f32, Custom); |
| 82 | |
Matt Arsenault | 7c93690 | 2014-10-21 23:01:01 +0000 | [diff] [blame] | 83 | setOperationAction(ISD::FMINNUM, MVT::f64, Legal); |
| 84 | setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); |
| 85 | |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 86 | // We need to custom lower vector stores from local memory |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 87 | setOperationAction(ISD::LOAD, MVT::v4i32, Custom); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 88 | setOperationAction(ISD::LOAD, MVT::v8i32, Custom); |
| 89 | setOperationAction(ISD::LOAD, MVT::v16i32, Custom); |
| 90 | |
| 91 | setOperationAction(ISD::STORE, MVT::v8i32, Custom); |
| 92 | setOperationAction(ISD::STORE, MVT::v16i32, Custom); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 93 | |
Tom Stellard | 1c8788e | 2014-03-07 20:12:33 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::STORE, MVT::i1, Custom); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 95 | setOperationAction(ISD::STORE, MVT::v4i32, Custom); |
| 96 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 97 | setOperationAction(ISD::SELECT, MVT::i64, Custom); |
Tom Stellard | da99c6e | 2014-03-24 16:07:30 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::SELECT, MVT::f64, Promote); |
| 99 | AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 100 | |
Tom Stellard | 3ca1bfc | 2014-06-10 16:01:22 +0000 | [diff] [blame] | 101 | setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); |
| 102 | setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); |
| 103 | setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); |
| 104 | setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); |
Tom Stellard | 754f80f | 2013-04-05 23:31:51 +0000 | [diff] [blame] | 105 | |
Tom Stellard | d1efda8 | 2016-01-20 21:48:24 +0000 | [diff] [blame] | 106 | setOperationAction(ISD::SETCC, MVT::i1, Promote); |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 107 | setOperationAction(ISD::SETCC, MVT::v2i1, Expand); |
| 108 | setOperationAction(ISD::SETCC, MVT::v4i1, Expand); |
| 109 | |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 110 | setOperationAction(ISD::BSWAP, MVT::i32, Legal); |
Matt Arsenault | d079285 | 2015-12-14 17:25:38 +0000 | [diff] [blame] | 111 | setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 112 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 113 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 114 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); |
| 115 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); |
| 116 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 117 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); |
| 119 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); |
| 120 | |
Matt Arsenault | 5dbd5db | 2014-04-22 03:49:30 +0000 | [diff] [blame] | 121 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 122 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); |
| 123 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); |
| 124 | |
Matt Arsenault | 9481221 | 2014-11-14 18:18:16 +0000 | [diff] [blame] | 125 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
Matt Arsenault | 4e46665 | 2014-04-16 01:41:30 +0000 | [diff] [blame] | 126 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); |
| 127 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 128 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 129 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); |
| 130 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom); |
| 131 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 132 | |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 133 | setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); |
Matt Arsenault | e54e1c3 | 2014-06-23 18:00:44 +0000 | [diff] [blame] | 134 | setOperationAction(ISD::BRCOND, MVT::Other, Custom); |
Tom Stellard | afcf12f | 2013-09-12 02:55:14 +0000 | [diff] [blame] | 135 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 136 | for (MVT VT : MVT::integer_valuetypes()) { |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 137 | if (VT == MVT::i64) |
| 138 | continue; |
| 139 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 140 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 141 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal); |
| 142 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 143 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand); |
Tom Stellard | 31209cc | 2013-07-15 19:00:09 +0000 | [diff] [blame] | 144 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 145 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 146 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal); |
| 147 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 148 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand); |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 149 | |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 150 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); |
Matt Arsenault | bd22342 | 2015-01-14 01:35:17 +0000 | [diff] [blame] | 151 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal); |
| 152 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 153 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand); |
| 154 | } |
| 155 | |
| 156 | for (MVT VT : MVT::integer_vector_valuetypes()) { |
| 157 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand); |
| 158 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand); |
| 159 | } |
| 160 | |
| 161 | for (MVT VT : MVT::fp_valuetypes()) |
| 162 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand); |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 163 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 164 | setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand); |
| 165 | setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand); |
| 166 | |
Matt Arsenault | 6f24379 | 2013-09-05 19:41:10 +0000 | [diff] [blame] | 167 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 168 | setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); |
Matt Arsenault | e1ce344 | 2015-07-31 04:12:04 +0000 | [diff] [blame] | 169 | setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand); |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 170 | setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); |
Niels Ole Salscheider | 719fbc9 | 2013-08-08 16:06:15 +0000 | [diff] [blame] | 171 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 172 | |
| 173 | setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand); |
| 174 | |
| 175 | setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand); |
| 176 | setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand); |
| 177 | |
Matt Arsenault | 470acd8 | 2014-04-15 22:28:39 +0000 | [diff] [blame] | 178 | setOperationAction(ISD::LOAD, MVT::i1, Custom); |
| 179 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 180 | setOperationAction(ISD::LOAD, MVT::v2i64, Promote); |
| 181 | AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32); |
| 182 | |
| 183 | setOperationAction(ISD::STORE, MVT::v2i64, Promote); |
| 184 | AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32); |
| 185 | |
| 186 | setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand); |
| 187 | |
Tom Stellard | fd15582 | 2013-08-26 15:05:36 +0000 | [diff] [blame] | 188 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Tom Stellard | 04c0e98 | 2014-01-22 19:24:21 +0000 | [diff] [blame] | 189 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Matt Arsenault | a98cd6a | 2013-12-19 05:32:55 +0000 | [diff] [blame] | 190 | setOperationAction(ISD::FrameIndex, MVT::i32, Custom); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 191 | |
Tom Stellard | 5f33788 | 2014-04-29 23:12:43 +0000 | [diff] [blame] | 192 | // These should use UDIVREM, so set them to expand |
| 193 | setOperationAction(ISD::UDIV, MVT::i64, Expand); |
| 194 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
| 195 | |
Matt Arsenault | 0d89e84 | 2014-07-15 21:44:37 +0000 | [diff] [blame] | 196 | setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); |
| 197 | setOperationAction(ISD::SELECT, MVT::i1, Promote); |
| 198 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 199 | setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); |
| 200 | |
| 201 | |
| 202 | setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); |
| 203 | |
Benjamin Kramer | 867bfc5 | 2015-03-07 17:41:00 +0000 | [diff] [blame] | 204 | // We only support LOAD/STORE and vector manipulation ops for vectors |
| 205 | // with > 4 elements. |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 206 | for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) { |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 207 | for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { |
| 208 | switch(Op) { |
| 209 | case ISD::LOAD: |
| 210 | case ISD::STORE: |
| 211 | case ISD::BUILD_VECTOR: |
| 212 | case ISD::BITCAST: |
| 213 | case ISD::EXTRACT_VECTOR_ELT: |
| 214 | case ISD::INSERT_VECTOR_ELT: |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 215 | case ISD::INSERT_SUBVECTOR: |
| 216 | case ISD::EXTRACT_SUBVECTOR: |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 217 | case ISD::SCALAR_TO_VECTOR: |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 218 | break; |
Tom Stellard | c0503db | 2014-08-09 01:06:56 +0000 | [diff] [blame] | 219 | case ISD::CONCAT_VECTORS: |
| 220 | setOperationAction(Op, VT, Custom); |
| 221 | break; |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 222 | default: |
Matt Arsenault | d504a74 | 2014-05-15 21:44:05 +0000 | [diff] [blame] | 223 | setOperationAction(Op, VT, Expand); |
Tom Stellard | 967bf58 | 2014-02-13 23:34:15 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Matt Arsenault | 61001bb | 2015-11-25 19:58:34 +0000 | [diff] [blame] | 229 | // Most operations are naturally 32-bit vector operations. We only support |
| 230 | // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. |
| 231 | for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { |
| 232 | setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); |
| 233 | AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); |
| 234 | |
| 235 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); |
| 236 | AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); |
| 237 | |
| 238 | setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); |
| 239 | AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); |
| 240 | |
| 241 | setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); |
| 242 | AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); |
| 243 | } |
| 244 | |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 245 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) { |
| 246 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
| 247 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
Matt Arsenault | a90d22f | 2014-04-17 17:06:37 +0000 | [diff] [blame] | 248 | setOperationAction(ISD::FRINT, MVT::f64, Legal); |
Matt Arsenault | 41e2f2b | 2014-02-24 21:01:28 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Marek Olsak | 7d77728 | 2015-03-24 13:40:15 +0000 | [diff] [blame] | 251 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 252 | setOperationAction(ISD::FDIV, MVT::f32, Custom); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 253 | setOperationAction(ISD::FDIV, MVT::f64, Custom); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 254 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 255 | setTargetDAGCombine(ISD::FADD); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 256 | setTargetDAGCombine(ISD::FSUB); |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 257 | setTargetDAGCombine(ISD::FMINNUM); |
| 258 | setTargetDAGCombine(ISD::FMAXNUM); |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 259 | setTargetDAGCombine(ISD::SMIN); |
| 260 | setTargetDAGCombine(ISD::SMAX); |
| 261 | setTargetDAGCombine(ISD::UMIN); |
| 262 | setTargetDAGCombine(ISD::UMAX); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 263 | setTargetDAGCombine(ISD::SETCC); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 264 | setTargetDAGCombine(ISD::AND); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 265 | setTargetDAGCombine(ISD::OR); |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 266 | setTargetDAGCombine(ISD::UINT_TO_FP); |
| 267 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 268 | // All memory operations. Some folding on the pointer operand is done to help |
| 269 | // matching the constant offsets in the addressing modes. |
| 270 | setTargetDAGCombine(ISD::LOAD); |
| 271 | setTargetDAGCombine(ISD::STORE); |
| 272 | setTargetDAGCombine(ISD::ATOMIC_LOAD); |
| 273 | setTargetDAGCombine(ISD::ATOMIC_STORE); |
| 274 | setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); |
| 275 | setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); |
| 276 | setTargetDAGCombine(ISD::ATOMIC_SWAP); |
| 277 | setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); |
| 278 | setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); |
| 279 | setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); |
| 280 | setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); |
| 281 | setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); |
| 282 | setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); |
| 283 | setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); |
| 284 | setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); |
| 285 | setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); |
| 286 | setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); |
| 287 | |
Christian Konig | eecebd0 | 2013-03-26 14:04:02 +0000 | [diff] [blame] | 288 | setSchedulingPreference(Sched::RegPressure); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 291 | //===----------------------------------------------------------------------===// |
| 292 | // TargetLowering queries |
| 293 | //===----------------------------------------------------------------------===// |
| 294 | |
Matt Arsenault | e306a32 | 2014-10-21 16:25:08 +0000 | [diff] [blame] | 295 | bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &, |
| 296 | EVT) const { |
| 297 | // SI has some legal vector types, but no legal vector operations. Say no |
| 298 | // shuffles are legal in order to prefer scalarizing some vector operations. |
| 299 | return false; |
| 300 | } |
| 301 | |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 302 | bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { |
| 303 | // Flat instructions do not have offsets, and only have the register |
| 304 | // address. |
| 305 | return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1); |
| 306 | } |
| 307 | |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 308 | bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { |
| 309 | // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and |
| 310 | // additionally can do r + r + i with addr64. 32-bit has more addressing |
| 311 | // mode options. Depending on the resource constant, it can also do |
| 312 | // (i64 r0) + (i32 r1) * (i14 i). |
| 313 | // |
| 314 | // Private arrays end up using a scratch buffer most of the time, so also |
| 315 | // assume those use MUBUF instructions. Scratch loads / stores are currently |
| 316 | // implemented as mubuf instructions with offen bit set, so slightly |
| 317 | // different than the normal addr64. |
| 318 | if (!isUInt<12>(AM.BaseOffs)) |
| 319 | return false; |
| 320 | |
| 321 | // FIXME: Since we can split immediate into soffset and immediate offset, |
| 322 | // would it make sense to allow any immediate? |
| 323 | |
| 324 | switch (AM.Scale) { |
| 325 | case 0: // r + i or just i, depending on HasBaseReg. |
| 326 | return true; |
| 327 | case 1: |
| 328 | return true; // We have r + r or r + i. |
| 329 | case 2: |
| 330 | if (AM.HasBaseReg) { |
| 331 | // Reject 2 * r + r. |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | // Allow 2 * r as r + r |
| 336 | // Or 2 * r + i is allowed as r + r + i. |
| 337 | return true; |
| 338 | default: // Don't allow n * r |
| 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 343 | bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, |
| 344 | const AddrMode &AM, Type *Ty, |
| 345 | unsigned AS) const { |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 346 | // No global is ever allowed as a base. |
| 347 | if (AM.BaseGV) |
| 348 | return false; |
| 349 | |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 350 | switch (AS) { |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 351 | case AMDGPUAS::GLOBAL_ADDRESS: { |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 352 | if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { |
| 353 | // Assume the we will use FLAT for all global memory accesses |
| 354 | // on VI. |
| 355 | // FIXME: This assumption is currently wrong. On VI we still use |
| 356 | // MUBUF instructions for the r + i addressing mode. As currently |
| 357 | // implemented, the MUBUF instructions only work on buffer < 4GB. |
| 358 | // It may be possible to support > 4GB buffers with MUBUF instructions, |
| 359 | // by setting the stride value in the resource descriptor which would |
| 360 | // increase the size limit to (stride * 4GB). However, this is risky, |
| 361 | // because it has never been validated. |
| 362 | return isLegalFlatAddressingMode(AM); |
| 363 | } |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 364 | |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 365 | return isLegalMUBUFAddressingMode(AM); |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 366 | } |
Matt Arsenault | 711b390 | 2015-08-07 20:18:34 +0000 | [diff] [blame] | 367 | case AMDGPUAS::CONSTANT_ADDRESS: { |
| 368 | // If the offset isn't a multiple of 4, it probably isn't going to be |
| 369 | // correctly aligned. |
| 370 | if (AM.BaseOffs % 4 != 0) |
| 371 | return isLegalMUBUFAddressingMode(AM); |
| 372 | |
| 373 | // There are no SMRD extloads, so if we have to do a small type access we |
| 374 | // will use a MUBUF load. |
| 375 | // FIXME?: We also need to do this if unaligned, but we don't know the |
| 376 | // alignment here. |
| 377 | if (DL.getTypeStoreSize(Ty) < 4) |
| 378 | return isLegalMUBUFAddressingMode(AM); |
| 379 | |
| 380 | if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
| 381 | // SMRD instructions have an 8-bit, dword offset on SI. |
| 382 | if (!isUInt<8>(AM.BaseOffs / 4)) |
| 383 | return false; |
| 384 | } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) { |
| 385 | // On CI+, this can also be a 32-bit literal constant offset. If it fits |
| 386 | // in 8-bits, it can use a smaller encoding. |
| 387 | if (!isUInt<32>(AM.BaseOffs / 4)) |
| 388 | return false; |
| 389 | } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) { |
| 390 | // On VI, these use the SMEM format and the offset is 20-bit in bytes. |
| 391 | if (!isUInt<20>(AM.BaseOffs)) |
| 392 | return false; |
| 393 | } else |
| 394 | llvm_unreachable("unhandled generation"); |
| 395 | |
| 396 | if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. |
| 397 | return true; |
| 398 | |
| 399 | if (AM.Scale == 1 && AM.HasBaseReg) |
| 400 | return true; |
| 401 | |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | case AMDGPUAS::PRIVATE_ADDRESS: |
| 406 | case AMDGPUAS::UNKNOWN_ADDRESS_SPACE: |
| 407 | return isLegalMUBUFAddressingMode(AM); |
| 408 | |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 409 | case AMDGPUAS::LOCAL_ADDRESS: |
| 410 | case AMDGPUAS::REGION_ADDRESS: { |
| 411 | // Basic, single offset DS instructions allow a 16-bit unsigned immediate |
| 412 | // field. |
| 413 | // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have |
| 414 | // an 8-bit dword offset but we don't know the alignment here. |
| 415 | if (!isUInt<16>(AM.BaseOffs)) |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 416 | return false; |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 417 | |
| 418 | if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. |
| 419 | return true; |
| 420 | |
| 421 | if (AM.Scale == 1 && AM.HasBaseReg) |
| 422 | return true; |
| 423 | |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 424 | return false; |
| 425 | } |
Tom Stellard | 70580f8 | 2015-07-20 14:28:41 +0000 | [diff] [blame] | 426 | case AMDGPUAS::FLAT_ADDRESS: |
| 427 | return isLegalFlatAddressingMode(AM); |
| 428 | |
Matt Arsenault | 73e06fa | 2015-06-04 16:17:42 +0000 | [diff] [blame] | 429 | default: |
| 430 | llvm_unreachable("unhandled address space"); |
| 431 | } |
Matt Arsenault | 5015a89 | 2014-08-15 17:17:07 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 434 | bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 435 | unsigned AddrSpace, |
| 436 | unsigned Align, |
| 437 | bool *IsFast) const { |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 438 | if (IsFast) |
| 439 | *IsFast = false; |
| 440 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 441 | // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, |
| 442 | // which isn't a simple VT. |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 443 | if (!VT.isSimple() || VT == MVT::Other) |
| 444 | return false; |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 445 | |
Tom Stellard | c6b299c | 2015-02-02 18:02:28 +0000 | [diff] [blame] | 446 | // TODO - CI+ supports unaligned memory accesses, but this requires driver |
| 447 | // support. |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 448 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 449 | // XXX - The only mention I see of this in the ISA manual is for LDS direct |
| 450 | // reads the "byte address and must be dword aligned". Is it also true for the |
| 451 | // normal loads and stores? |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 452 | if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) { |
| 453 | // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte |
| 454 | // aligned, 8 byte access in a single operation using ds_read2/write2_b32 |
| 455 | // with adjacent offsets. |
Sanjay Patel | ce74db9 | 2015-09-03 15:03:19 +0000 | [diff] [blame] | 456 | bool AlignedBy4 = (Align % 4 == 0); |
| 457 | if (IsFast) |
| 458 | *IsFast = AlignedBy4; |
| 459 | return AlignedBy4; |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 460 | } |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 461 | |
Tom Stellard | 33e64c6 | 2015-02-04 20:49:52 +0000 | [diff] [blame] | 462 | // Smaller than dword value must be aligned. |
| 463 | // FIXME: This should be allowed on CI+ |
| 464 | if (VT.bitsLT(MVT::i32)) |
| 465 | return false; |
| 466 | |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 467 | // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the |
| 468 | // byte-address are ignored, thus forcing Dword alignment. |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 469 | // This applies to private, global, and constant memory. |
Matt Arsenault | 1018c89 | 2014-04-24 17:08:26 +0000 | [diff] [blame] | 470 | if (IsFast) |
| 471 | *IsFast = true; |
Tom Stellard | c6b299c | 2015-02-02 18:02:28 +0000 | [diff] [blame] | 472 | |
| 473 | return VT.bitsGT(MVT::i32) && Align % 4 == 0; |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Matt Arsenault | 46645fa | 2014-07-28 17:49:26 +0000 | [diff] [blame] | 476 | EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, |
| 477 | unsigned SrcAlign, bool IsMemset, |
| 478 | bool ZeroMemset, |
| 479 | bool MemcpyStrSrc, |
| 480 | MachineFunction &MF) const { |
| 481 | // FIXME: Should account for address space here. |
| 482 | |
| 483 | // The default fallback uses the private pointer size as a guess for a type to |
| 484 | // use. Make sure we switch these to 64-bit accesses. |
| 485 | |
| 486 | if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global |
| 487 | return MVT::v4i32; |
| 488 | |
| 489 | if (Size >= 8 && DstAlign >= 4) |
| 490 | return MVT::v2i32; |
| 491 | |
| 492 | // Use the default. |
| 493 | return MVT::Other; |
| 494 | } |
| 495 | |
Matt Arsenault | f9bfeaf | 2015-12-01 23:04:00 +0000 | [diff] [blame] | 496 | static bool isFlatGlobalAddrSpace(unsigned AS) { |
| 497 | return AS == AMDGPUAS::GLOBAL_ADDRESS || |
| 498 | AS == AMDGPUAS::FLAT_ADDRESS || |
| 499 | AS == AMDGPUAS::CONSTANT_ADDRESS; |
| 500 | } |
| 501 | |
| 502 | bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, |
| 503 | unsigned DestAS) const { |
| 504 | return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS); |
| 505 | } |
| 506 | |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 507 | |
| 508 | bool SITargetLowering::isMemOpUniform(const SDNode *N) const { |
| 509 | const MemSDNode *MemNode = cast<MemSDNode>(N); |
| 510 | const Value *Ptr = MemNode->getMemOperand()->getValue(); |
| 511 | |
| 512 | // UndefValue means this is a load of a kernel input. These are uniform. |
| 513 | // Sometimes LDS instructions have constant pointers |
| 514 | if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) || |
| 515 | isa<GlobalValue>(Ptr)) |
| 516 | return true; |
| 517 | |
| 518 | const Instruction *I = dyn_cast_or_null<Instruction>(Ptr); |
| 519 | return I && I->getMetadata("amdgpu.uniform"); |
| 520 | } |
| 521 | |
Chandler Carruth | 9d010ff | 2014-07-03 00:23:43 +0000 | [diff] [blame] | 522 | TargetLoweringBase::LegalizeTypeAction |
| 523 | SITargetLowering::getPreferredVectorAction(EVT VT) const { |
| 524 | if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) |
| 525 | return TypeSplitVector; |
| 526 | |
| 527 | return TargetLoweringBase::getPreferredVectorAction(VT); |
Tom Stellard | d86003e | 2013-08-14 23:25:00 +0000 | [diff] [blame] | 528 | } |
Tom Stellard | 0125f2a | 2013-06-25 02:39:35 +0000 | [diff] [blame] | 529 | |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 530 | bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, |
| 531 | Type *Ty) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 532 | const SIInstrInfo *TII = |
| 533 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Matt Arsenault | d7bdcc4 | 2014-03-31 19:54:27 +0000 | [diff] [blame] | 534 | return TII->isInlineConstant(Imm); |
| 535 | } |
| 536 | |
Tom Stellard | 2e045bb | 2016-01-20 00:13:22 +0000 | [diff] [blame] | 537 | bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { |
| 538 | |
| 539 | // SimplifySetCC uses this function to determine whether or not it should |
| 540 | // create setcc with i1 operands. We don't have instructions for i1 setcc. |
| 541 | if (VT == MVT::i1 && Op == ISD::SETCC) |
| 542 | return false; |
| 543 | |
| 544 | return TargetLowering::isTypeDesirableForOp(Op, VT); |
| 545 | } |
| 546 | |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 547 | SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 548 | SDLoc SL, SDValue Chain, |
Matt Arsenault | e1f030c | 2014-04-11 20:59:54 +0000 | [diff] [blame] | 549 | unsigned Offset, bool Signed) const { |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 550 | const DataLayout &DL = DAG.getDataLayout(); |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 551 | MachineFunction &MF = DAG.getMachineFunction(); |
| 552 | const SIRegisterInfo *TRI = |
| 553 | static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo()); |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 554 | unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 555 | |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 556 | Type *Ty = VT.getTypeForEVT(*DAG.getContext()); |
| 557 | |
| 558 | MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 559 | MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 560 | PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); |
Matt Arsenault | a0269b6 | 2015-06-01 21:58:24 +0000 | [diff] [blame] | 561 | SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, |
| 562 | MRI.getLiveInVirtReg(InputPtrReg), PtrVT); |
| 563 | SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, |
| 564 | DAG.getConstant(Offset, SL, PtrVT)); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 565 | SDValue PtrOffset = DAG.getUNDEF(PtrVT); |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 566 | MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); |
| 567 | |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 568 | unsigned Align = DL.getABITypeAlignment(Ty); |
Matt Arsenault | 81c7ae2 | 2015-06-04 16:00:27 +0000 | [diff] [blame] | 569 | |
Matt Arsenault | 81c7ae2 | 2015-06-04 16:00:27 +0000 | [diff] [blame] | 570 | ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD; |
Matt Arsenault | acd68b5 | 2015-09-09 01:12:27 +0000 | [diff] [blame] | 571 | if (MemVT.isFloatingPoint()) |
| 572 | ExtTy = ISD::EXTLOAD; |
| 573 | |
Matt Arsenault | 81c7ae2 | 2015-06-04 16:00:27 +0000 | [diff] [blame] | 574 | return DAG.getLoad(ISD::UNINDEXED, ExtTy, |
Matt Arsenault | 86033ca | 2014-07-28 17:31:39 +0000 | [diff] [blame] | 575 | VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT, |
| 576 | false, // isVolatile |
| 577 | true, // isNonTemporal |
| 578 | true, // isInvariant |
Matt Arsenault | 81c7ae2 | 2015-06-04 16:00:27 +0000 | [diff] [blame] | 579 | Align); // Alignment |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 582 | SDValue SITargetLowering::LowerFormalArguments( |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 583 | SDValue Chain, CallingConv::ID CallConv, bool isVarArg, |
| 584 | const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG, |
| 585 | SmallVectorImpl<SDValue> &InVals) const { |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 586 | const SIRegisterInfo *TRI = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 587 | static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 588 | |
| 589 | MachineFunction &MF = DAG.getMachineFunction(); |
| 590 | FunctionType *FType = MF.getFunction()->getFunctionType(); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 591 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 592 | const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 593 | |
Matt Arsenault | d48da14 | 2015-11-02 23:23:02 +0000 | [diff] [blame] | 594 | if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) { |
| 595 | const Function *Fn = MF.getFunction(); |
| 596 | DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA"); |
| 597 | DAG.getContext()->diagnose(NoGraphicsHSA); |
| 598 | return SDValue(); |
| 599 | } |
| 600 | |
Tom Stellard | 0fbf899 | 2015-10-06 21:16:34 +0000 | [diff] [blame] | 601 | // FIXME: We currently assume all calling conventions are kernels. |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 602 | |
| 603 | SmallVector<ISD::InputArg, 16> Splits; |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 604 | BitVector Skipped(Ins.size()); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 605 | |
| 606 | for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 607 | const ISD::InputArg &Arg = Ins[i]; |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 608 | |
| 609 | // First check if it's a PS input addr |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 610 | if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() && |
Marek Olsak | b6c8c3d | 2016-01-13 11:46:10 +0000 | [diff] [blame] | 611 | !Arg.Flags.isByVal() && PSInputNum <= 15) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 612 | |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 613 | if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) { |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 614 | // We can safely skip PS inputs |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 615 | Skipped.set(i); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 616 | ++PSInputNum; |
| 617 | continue; |
| 618 | } |
| 619 | |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 620 | Info->markPSInputAllocated(PSInputNum); |
| 621 | if (Arg.Used) |
| 622 | Info->PSInputEna |= 1 << PSInputNum; |
| 623 | |
| 624 | ++PSInputNum; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Second split vertices into their elements |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 628 | if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 629 | ISD::InputArg NewArg = Arg; |
| 630 | NewArg.Flags.setSplit(); |
| 631 | NewArg.VT = Arg.VT.getVectorElementType(); |
| 632 | |
| 633 | // We REALLY want the ORIGINAL number of vertex elements here, e.g. a |
| 634 | // three or five element vertex only needs three or five registers, |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 635 | // NOT four or eight. |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 636 | Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 637 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 638 | |
| 639 | for (unsigned j = 0; j != NumElements; ++j) { |
| 640 | Splits.push_back(NewArg); |
| 641 | NewArg.PartOffset += NewArg.VT.getStoreSize(); |
| 642 | } |
| 643 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 644 | } else if (Info->getShaderType() != ShaderType::COMPUTE) { |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 645 | Splits.push_back(Arg); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 650 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, |
| 651 | *DAG.getContext()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 652 | |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 653 | // At least one interpolation mode must be enabled or else the GPU will hang. |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 654 | // |
| 655 | // Check PSInputAddr instead of PSInputEna. The idea is that if the user set |
| 656 | // PSInputAddr, the user wants to enable some bits after the compilation |
| 657 | // based on run-time states. Since we can't know what the final PSInputEna |
| 658 | // will look like, so we shouldn't do anything here and the user should take |
| 659 | // responsibility for the correct programming. |
Marek Olsak | 46dadbf | 2016-01-13 17:23:20 +0000 | [diff] [blame] | 660 | // |
| 661 | // Otherwise, the following restrictions apply: |
| 662 | // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. |
| 663 | // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be |
| 664 | // enabled too. |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 665 | if (Info->getShaderType() == ShaderType::PIXEL && |
Marek Olsak | 46dadbf | 2016-01-13 17:23:20 +0000 | [diff] [blame] | 666 | ((Info->getPSInputAddr() & 0x7F) == 0 || |
| 667 | ((Info->getPSInputAddr() & 0xF) == 0 && |
| 668 | Info->isPSInputAllocated(11)))) { |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 669 | CCInfo.AllocateReg(AMDGPU::VGPR0); |
| 670 | CCInfo.AllocateReg(AMDGPU::VGPR1); |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 671 | Info->markPSInputAllocated(0); |
| 672 | Info->PSInputEna |= 1; |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame] | 675 | if (Info->getShaderType() == ShaderType::COMPUTE) { |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 676 | getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins, |
| 677 | Splits); |
| 678 | } |
| 679 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 680 | // FIXME: How should these inputs interact with inreg / custom SGPR inputs? |
| 681 | if (Info->hasPrivateSegmentBuffer()) { |
| 682 | unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI); |
| 683 | MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass); |
| 684 | CCInfo.AllocateReg(PrivateSegmentBufferReg); |
| 685 | } |
| 686 | |
| 687 | if (Info->hasDispatchPtr()) { |
| 688 | unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI); |
| 689 | MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass); |
| 690 | CCInfo.AllocateReg(DispatchPtrReg); |
| 691 | } |
| 692 | |
| 693 | if (Info->hasKernargSegmentPtr()) { |
| 694 | unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI); |
| 695 | MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass); |
| 696 | CCInfo.AllocateReg(InputPtrReg); |
| 697 | } |
| 698 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 699 | AnalyzeFormalArguments(CCInfo, Splits); |
| 700 | |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 701 | SmallVector<SDValue, 16> Chains; |
| 702 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 703 | for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { |
| 704 | |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 705 | const ISD::InputArg &Arg = Ins[i]; |
Alexey Samsonov | a253bf9 | 2014-08-27 19:36:53 +0000 | [diff] [blame] | 706 | if (Skipped[i]) { |
Christian Konig | b7be72d | 2013-05-17 09:46:48 +0000 | [diff] [blame] | 707 | InVals.push_back(DAG.getUNDEF(Arg.VT)); |
Christian Konig | 99ee0f4 | 2013-03-07 09:04:14 +0000 | [diff] [blame] | 708 | continue; |
| 709 | } |
| 710 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 711 | CCValAssign &VA = ArgLocs[ArgIdx++]; |
Craig Topper | 7f416c8 | 2014-11-16 21:17:18 +0000 | [diff] [blame] | 712 | MVT VT = VA.getLocVT(); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 713 | |
| 714 | if (VA.isMemLoc()) { |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 715 | VT = Ins[i].VT; |
| 716 | EVT MemVT = Splits[i].VT; |
Tom Stellard | b5798b0 | 2015-06-26 21:15:03 +0000 | [diff] [blame] | 717 | const unsigned Offset = Subtarget->getExplicitKernelArgOffset() + |
| 718 | VA.getLocMemOffset(); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 719 | // The first 36 bytes of the input buffer contains information about |
| 720 | // thread group and global sizes. |
Matt Arsenault | 0d51973 | 2015-07-10 22:28:41 +0000 | [diff] [blame] | 721 | SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain, |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 722 | Offset, Ins[i].Flags.isSExt()); |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 723 | Chains.push_back(Arg.getValue(1)); |
Tom Stellard | ca7ecf3 | 2014-08-22 18:49:31 +0000 | [diff] [blame] | 724 | |
Craig Topper | e3dcce9 | 2015-08-01 22:20:21 +0000 | [diff] [blame] | 725 | auto *ParamTy = |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 726 | dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); |
Tom Stellard | ca7ecf3 | 2014-08-22 18:49:31 +0000 | [diff] [blame] | 727 | if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && |
| 728 | ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { |
| 729 | // On SI local pointers are just offsets into LDS, so they are always |
| 730 | // less than 16-bits. On CI and newer they could potentially be |
| 731 | // real pointers, so we can't guarantee their size. |
| 732 | Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, |
| 733 | DAG.getValueType(MVT::i16)); |
| 734 | } |
| 735 | |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 736 | InVals.push_back(Arg); |
Jan Vesely | e5121f3 | 2014-10-14 20:05:26 +0000 | [diff] [blame] | 737 | Info->ABIArgOffset = Offset + MemVT.getStoreSize(); |
Tom Stellard | ed882c2 | 2013-06-03 17:40:11 +0000 | [diff] [blame] | 738 | continue; |
| 739 | } |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 740 | assert(VA.isRegLoc() && "Parameter must be in a register!"); |
| 741 | |
| 742 | unsigned Reg = VA.getLocReg(); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 743 | |
| 744 | if (VT == MVT::i64) { |
| 745 | // For now assume it is a pointer |
| 746 | Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, |
| 747 | &AMDGPU::SReg_64RegClass); |
| 748 | Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass); |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 749 | SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 750 | InVals.push_back(Copy); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 751 | continue; |
| 752 | } |
| 753 | |
| 754 | const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); |
| 755 | |
| 756 | Reg = MF.addLiveIn(Reg, RC); |
| 757 | SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 758 | |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 759 | if (Arg.VT.isVector()) { |
| 760 | |
| 761 | // Build a vector from the registers |
Andrew Trick | 05938a5 | 2015-02-16 18:10:47 +0000 | [diff] [blame] | 762 | Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 763 | unsigned NumElements = ParamType->getVectorNumElements(); |
| 764 | |
| 765 | SmallVector<SDValue, 4> Regs; |
| 766 | Regs.push_back(Val); |
| 767 | for (unsigned j = 1; j != NumElements; ++j) { |
| 768 | Reg = ArgLocs[ArgIdx++].getLocReg(); |
| 769 | Reg = MF.addLiveIn(Reg, RC); |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 770 | |
| 771 | SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); |
| 772 | Regs.push_back(Copy); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // Fill up the missing vector elements |
| 776 | NumElements = Arg.VT.getVectorNumElements() - NumElements; |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 777 | Regs.append(NumElements, DAG.getUNDEF(VT)); |
Matt Arsenault | 75865923 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 778 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 779 | InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs)); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 780 | continue; |
| 781 | } |
| 782 | |
| 783 | InVals.push_back(Val); |
| 784 | } |
Tom Stellard | e99fb65 | 2015-01-20 19:33:04 +0000 | [diff] [blame] | 785 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 786 | // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read |
| 787 | // these from the dispatch pointer. |
| 788 | |
| 789 | // Start adding system SGPRs. |
| 790 | if (Info->hasWorkGroupIDX()) { |
| 791 | unsigned Reg = Info->addWorkGroupIDX(); |
| 792 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 793 | CCInfo.AllocateReg(Reg); |
| 794 | } else |
| 795 | llvm_unreachable("work group id x is always enabled"); |
| 796 | |
| 797 | if (Info->hasWorkGroupIDY()) { |
| 798 | unsigned Reg = Info->addWorkGroupIDY(); |
| 799 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 800 | CCInfo.AllocateReg(Reg); |
Tom Stellard | e99fb65 | 2015-01-20 19:33:04 +0000 | [diff] [blame] | 801 | } |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 802 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 803 | if (Info->hasWorkGroupIDZ()) { |
| 804 | unsigned Reg = Info->addWorkGroupIDZ(); |
| 805 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 806 | CCInfo.AllocateReg(Reg); |
| 807 | } |
| 808 | |
| 809 | if (Info->hasWorkGroupInfo()) { |
| 810 | unsigned Reg = Info->addWorkGroupInfo(); |
| 811 | MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass); |
| 812 | CCInfo.AllocateReg(Reg); |
| 813 | } |
| 814 | |
| 815 | if (Info->hasPrivateSegmentWaveByteOffset()) { |
| 816 | // Scratch wave offset passed in system SGPR. |
| 817 | unsigned PrivateSegmentWaveByteOffsetReg |
| 818 | = Info->addPrivateSegmentWaveByteOffset(); |
| 819 | |
| 820 | MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); |
| 821 | CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); |
| 822 | } |
| 823 | |
| 824 | // Now that we've figured out where the scratch register inputs are, see if |
| 825 | // should reserve the arguments and use them directly. |
| 826 | |
| 827 | bool HasStackObjects = MF.getFrameInfo()->hasStackObjects(); |
| 828 | |
| 829 | if (ST.isAmdHsaOS()) { |
| 830 | // TODO: Assume we will spill without optimizations. |
| 831 | if (HasStackObjects) { |
| 832 | // If we have stack objects, we unquestionably need the private buffer |
| 833 | // resource. For the HSA ABI, this will be the first 4 user SGPR |
| 834 | // inputs. We can reserve those and use them directly. |
| 835 | |
| 836 | unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue( |
| 837 | MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); |
| 838 | Info->setScratchRSrcReg(PrivateSegmentBufferReg); |
| 839 | |
| 840 | unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue( |
| 841 | MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
| 842 | Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); |
| 843 | } else { |
| 844 | unsigned ReservedBufferReg |
| 845 | = TRI->reservedPrivateSegmentBufferReg(MF); |
| 846 | unsigned ReservedOffsetReg |
| 847 | = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
| 848 | |
| 849 | // We tentatively reserve the last registers (skipping the last two |
| 850 | // which may contain VCC). After register allocation, we'll replace |
| 851 | // these with the ones immediately after those which were really |
| 852 | // allocated. In the prologue copies will be inserted from the argument |
| 853 | // to these reserved registers. |
| 854 | Info->setScratchRSrcReg(ReservedBufferReg); |
| 855 | Info->setScratchWaveOffsetReg(ReservedOffsetReg); |
| 856 | } |
| 857 | } else { |
| 858 | unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF); |
| 859 | |
| 860 | // Without HSA, relocations are used for the scratch pointer and the |
| 861 | // buffer resource setup is always inserted in the prologue. Scratch wave |
| 862 | // offset is still in an input SGPR. |
| 863 | Info->setScratchRSrcReg(ReservedBufferReg); |
| 864 | |
| 865 | if (HasStackObjects) { |
| 866 | unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue( |
| 867 | MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); |
| 868 | Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg); |
| 869 | } else { |
| 870 | unsigned ReservedOffsetReg |
| 871 | = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); |
| 872 | Info->setScratchWaveOffsetReg(ReservedOffsetReg); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | if (Info->hasWorkItemIDX()) { |
| 877 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X); |
| 878 | MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); |
| 879 | CCInfo.AllocateReg(Reg); |
| 880 | } else |
| 881 | llvm_unreachable("workitem id x should always be enabled"); |
| 882 | |
| 883 | if (Info->hasWorkItemIDY()) { |
| 884 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y); |
| 885 | MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); |
| 886 | CCInfo.AllocateReg(Reg); |
| 887 | } |
| 888 | |
| 889 | if (Info->hasWorkItemIDZ()) { |
| 890 | unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z); |
| 891 | MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); |
| 892 | CCInfo.AllocateReg(Reg); |
| 893 | } |
Matt Arsenault | 0e3d389 | 2015-11-30 21:15:53 +0000 | [diff] [blame] | 894 | |
Matt Arsenault | cf13d18 | 2015-07-10 22:51:36 +0000 | [diff] [blame] | 895 | if (Chains.empty()) |
| 896 | return Chain; |
| 897 | |
| 898 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); |
Christian Konig | 2c8f6d5 | 2013-03-07 09:03:52 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Marek Olsak | 8a0f335 | 2016-01-13 17:23:04 +0000 | [diff] [blame] | 901 | SDValue SITargetLowering::LowerReturn(SDValue Chain, |
| 902 | CallingConv::ID CallConv, |
| 903 | bool isVarArg, |
| 904 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 905 | const SmallVectorImpl<SDValue> &OutVals, |
| 906 | SDLoc DL, SelectionDAG &DAG) const { |
| 907 | MachineFunction &MF = DAG.getMachineFunction(); |
| 908 | SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); |
| 909 | |
| 910 | if (Info->getShaderType() == ShaderType::COMPUTE) |
| 911 | return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, |
| 912 | OutVals, DL, DAG); |
| 913 | |
Marek Olsak | 8e9cc63 | 2016-01-13 17:23:09 +0000 | [diff] [blame] | 914 | Info->setIfReturnsVoid(Outs.size() == 0); |
| 915 | |
Marek Olsak | 8a0f335 | 2016-01-13 17:23:04 +0000 | [diff] [blame] | 916 | SmallVector<ISD::OutputArg, 48> Splits; |
| 917 | SmallVector<SDValue, 48> SplitVals; |
| 918 | |
| 919 | // Split vectors into their elements. |
| 920 | for (unsigned i = 0, e = Outs.size(); i != e; ++i) { |
| 921 | const ISD::OutputArg &Out = Outs[i]; |
| 922 | |
| 923 | if (Out.VT.isVector()) { |
| 924 | MVT VT = Out.VT.getVectorElementType(); |
| 925 | ISD::OutputArg NewOut = Out; |
| 926 | NewOut.Flags.setSplit(); |
| 927 | NewOut.VT = VT; |
| 928 | |
| 929 | // We want the original number of vector elements here, e.g. |
| 930 | // three or five, not four or eight. |
| 931 | unsigned NumElements = Out.ArgVT.getVectorNumElements(); |
| 932 | |
| 933 | for (unsigned j = 0; j != NumElements; ++j) { |
| 934 | SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i], |
| 935 | DAG.getConstant(j, DL, MVT::i32)); |
| 936 | SplitVals.push_back(Elem); |
| 937 | Splits.push_back(NewOut); |
| 938 | NewOut.PartOffset += NewOut.VT.getStoreSize(); |
| 939 | } |
| 940 | } else { |
| 941 | SplitVals.push_back(OutVals[i]); |
| 942 | Splits.push_back(Out); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | // CCValAssign - represent the assignment of the return value to a location. |
| 947 | SmallVector<CCValAssign, 48> RVLocs; |
| 948 | |
| 949 | // CCState - Info about the registers and stack slots. |
| 950 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, |
| 951 | *DAG.getContext()); |
| 952 | |
| 953 | // Analyze outgoing return values. |
| 954 | AnalyzeReturn(CCInfo, Splits); |
| 955 | |
| 956 | SDValue Flag; |
| 957 | SmallVector<SDValue, 48> RetOps; |
| 958 | RetOps.push_back(Chain); // Operand #0 = Chain (updated below) |
| 959 | |
| 960 | // Copy the result values into the output registers. |
| 961 | for (unsigned i = 0, realRVLocIdx = 0; |
| 962 | i != RVLocs.size(); |
| 963 | ++i, ++realRVLocIdx) { |
| 964 | CCValAssign &VA = RVLocs[i]; |
| 965 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 966 | |
| 967 | SDValue Arg = SplitVals[realRVLocIdx]; |
| 968 | |
| 969 | // Copied from other backends. |
| 970 | switch (VA.getLocInfo()) { |
| 971 | default: llvm_unreachable("Unknown loc info!"); |
| 972 | case CCValAssign::Full: |
| 973 | break; |
| 974 | case CCValAssign::BCvt: |
| 975 | Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); |
| 976 | break; |
| 977 | } |
| 978 | |
| 979 | Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); |
| 980 | Flag = Chain.getValue(1); |
| 981 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
| 982 | } |
| 983 | |
| 984 | // Update chain and glue. |
| 985 | RetOps[0] = Chain; |
| 986 | if (Flag.getNode()) |
| 987 | RetOps.push_back(Flag); |
| 988 | |
| 989 | return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps); |
| 990 | } |
| 991 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 992 | MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter( |
| 993 | MachineInstr * MI, MachineBasicBlock * BB) const { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 994 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 995 | switch (MI->getOpcode()) { |
| 996 | default: |
| 997 | return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); |
Matt Arsenault | 20711b7 | 2015-02-20 22:10:45 +0000 | [diff] [blame] | 998 | case AMDGPU::BRANCH: |
| 999 | return BB; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1000 | } |
| 1001 | return BB; |
| 1002 | } |
| 1003 | |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 1004 | bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { |
| 1005 | // This currently forces unfolding various combinations of fsub into fma with |
| 1006 | // free fneg'd operands. As long as we have fast FMA (controlled by |
| 1007 | // isFMAFasterThanFMulAndFAdd), we should perform these. |
| 1008 | |
| 1009 | // When fma is quarter rate, for f64 where add / sub are at best half rate, |
| 1010 | // most of these combines appear to be cycle neutral but save on instruction |
| 1011 | // count / code size. |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1015 | EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, |
| 1016 | EVT VT) const { |
Tom Stellard | 8374720 | 2013-07-18 21:43:53 +0000 | [diff] [blame] | 1017 | if (!VT.isVector()) { |
| 1018 | return MVT::i1; |
| 1019 | } |
Matt Arsenault | 8596f71 | 2014-11-28 22:51:38 +0000 | [diff] [blame] | 1020 | return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Mehdi Amini | eaabc51 | 2015-07-09 15:12:23 +0000 | [diff] [blame] | 1023 | MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const { |
Christian Konig | 082a14a | 2013-03-18 11:34:05 +0000 | [diff] [blame] | 1024 | return MVT::i32; |
| 1025 | } |
| 1026 | |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 1027 | // Answering this is somewhat tricky and depends on the specific device which |
| 1028 | // have different rates for fma or all f64 operations. |
| 1029 | // |
| 1030 | // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other |
| 1031 | // regardless of which device (although the number of cycles differs between |
| 1032 | // devices), so it is always profitable for f64. |
| 1033 | // |
| 1034 | // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable |
| 1035 | // only on full rate devices. Normally, we should prefer selecting v_mad_f32 |
| 1036 | // which we can always do even without fused FP ops since it returns the same |
| 1037 | // result as the separate operations and since it is always full |
| 1038 | // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 |
| 1039 | // however does not support denormals, so we do report fma as faster if we have |
| 1040 | // a fast fma device and require denormals. |
| 1041 | // |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 1042 | bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 1043 | VT = VT.getScalarType(); |
| 1044 | |
| 1045 | if (!VT.isSimple()) |
| 1046 | return false; |
| 1047 | |
| 1048 | switch (VT.getSimpleVT().SimpleTy) { |
| 1049 | case MVT::f32: |
Matt Arsenault | 423bf3f | 2015-01-29 19:34:32 +0000 | [diff] [blame] | 1050 | // This is as fast on some subtargets. However, we always have full rate f32 |
| 1051 | // mad available which returns the same result as the separate operations |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 1052 | // which we should prefer over fma. We can't use this if we want to support |
| 1053 | // denormals, so only report this in these cases. |
| 1054 | return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); |
Niels Ole Salscheider | d3a039f | 2013-08-10 10:38:54 +0000 | [diff] [blame] | 1055 | case MVT::f64: |
| 1056 | return true; |
| 1057 | default: |
| 1058 | break; |
| 1059 | } |
| 1060 | |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1064 | //===----------------------------------------------------------------------===// |
| 1065 | // Custom DAG Lowering Operations |
| 1066 | //===----------------------------------------------------------------------===// |
| 1067 | |
| 1068 | SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| 1069 | switch (Op.getOpcode()) { |
| 1070 | default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1071 | case ISD::FrameIndex: return LowerFrameIndex(Op, DAG); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1072 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 1073 | case ISD::LOAD: { |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1074 | SDValue Result = LowerLOAD(Op, DAG); |
| 1075 | assert((!Result.getNode() || |
| 1076 | Result.getNode()->getNumValues() == 2) && |
| 1077 | "Load should return a value and a chain"); |
| 1078 | return Result; |
Tom Stellard | 35bb18c | 2013-08-26 15:06:04 +0000 | [diff] [blame] | 1079 | } |
Tom Stellard | af77543 | 2013-10-23 00:44:32 +0000 | [diff] [blame] | 1080 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1081 | case ISD::FSIN: |
| 1082 | case ISD::FCOS: |
| 1083 | return LowerTrig(Op, DAG); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1084 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1085 | case ISD::FDIV: return LowerFDIV(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1086 | case ISD::STORE: return LowerSTORE(Op, DAG); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1087 | case ISD::GlobalAddress: { |
| 1088 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1089 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 1090 | return LowerGlobalAddress(MFI, Op, DAG); |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 1091 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1092 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 1093 | case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1094 | } |
| 1095 | return SDValue(); |
| 1096 | } |
| 1097 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1098 | /// \brief Helper function for LowerBRCOND |
| 1099 | static SDNode *findUser(SDValue Value, unsigned Opcode) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1100 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1101 | SDNode *Parent = Value.getNode(); |
| 1102 | for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); |
| 1103 | I != E; ++I) { |
| 1104 | |
| 1105 | if (I.getUse().get() != Value) |
| 1106 | continue; |
| 1107 | |
| 1108 | if (I->getOpcode() == Opcode) |
| 1109 | return *I; |
| 1110 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1111 | return nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1114 | SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const { |
| 1115 | |
Tom Stellard | c98ee20 | 2015-07-16 19:40:07 +0000 | [diff] [blame] | 1116 | SDLoc SL(Op); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1117 | FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op); |
| 1118 | unsigned FrameIndex = FINode->getIndex(); |
| 1119 | |
Tom Stellard | c98ee20 | 2015-07-16 19:40:07 +0000 | [diff] [blame] | 1120 | // A FrameIndex node represents a 32-bit offset into scratch memory. If |
| 1121 | // the high bit of a frame index offset were to be set, this would mean |
| 1122 | // that it represented an offset of ~2GB * 64 = ~128GB from the start of the |
| 1123 | // scratch buffer, with 64 being the number of threads per wave. |
| 1124 | // |
| 1125 | // If we know the machine uses less than 128GB of scratch, then we can |
| 1126 | // amrk the high bit of the FrameIndex node as known zero, |
| 1127 | // which is important, because it means in most situations we can |
| 1128 | // prove that values derived from FrameIndex nodes are non-negative. |
| 1129 | // This enables us to take advantage of more addressing modes when |
| 1130 | // accessing scratch buffers, since for scratch reads/writes, the register |
| 1131 | // offset must always be positive. |
| 1132 | |
| 1133 | SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32); |
| 1134 | if (Subtarget->enableHugeScratchBuffer()) |
| 1135 | return TFI; |
| 1136 | |
| 1137 | return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI, |
| 1138 | DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31))); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1141 | /// This transforms the control flow intrinsics to get the branch destination as |
| 1142 | /// last parameter, also switches branch target with BR if the need arise |
| 1143 | SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, |
| 1144 | SelectionDAG &DAG) const { |
| 1145 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1146 | SDLoc DL(BRCOND); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1147 | |
| 1148 | SDNode *Intr = BRCOND.getOperand(1).getNode(); |
| 1149 | SDValue Target = BRCOND.getOperand(2); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1150 | SDNode *BR = nullptr; |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1151 | |
| 1152 | if (Intr->getOpcode() == ISD::SETCC) { |
| 1153 | // As long as we negate the condition everything is fine |
| 1154 | SDNode *SetCC = Intr; |
| 1155 | assert(SetCC->getConstantOperandVal(1) == 1); |
NAKAMURA Takumi | 458a827 | 2013-01-07 11:14:44 +0000 | [diff] [blame] | 1156 | assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == |
| 1157 | ISD::SETNE); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1158 | Intr = SetCC->getOperand(0).getNode(); |
| 1159 | |
| 1160 | } else { |
| 1161 | // Get the target from BR if we don't negate the condition |
| 1162 | BR = findUser(BRCOND, ISD::BR); |
| 1163 | Target = BR->getOperand(1); |
| 1164 | } |
| 1165 | |
| 1166 | assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN); |
| 1167 | |
| 1168 | // Build the result and |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 1169 | ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1170 | |
| 1171 | // operands of the new intrinsic call |
| 1172 | SmallVector<SDValue, 4> Ops; |
| 1173 | Ops.push_back(BRCOND.getOperand(0)); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 1174 | Ops.append(Intr->op_begin() + 1, Intr->op_end()); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1175 | Ops.push_back(Target); |
| 1176 | |
| 1177 | // build the new intrinsic call |
| 1178 | SDNode *Result = DAG.getNode( |
| 1179 | Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1180 | DAG.getVTList(Res), Ops).getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (BR) { |
| 1183 | // Give the branch instruction our target |
| 1184 | SDValue Ops[] = { |
| 1185 | BR->getOperand(0), |
| 1186 | BRCOND.getOperand(2) |
| 1187 | }; |
Chandler Carruth | 356665a | 2014-08-01 22:09:43 +0000 | [diff] [blame] | 1188 | SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); |
| 1189 | DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); |
| 1190 | BR = NewBR.getNode(); |
Tom Stellard | f879435 | 2012-12-19 22:10:31 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | SDValue Chain = SDValue(Result, Result->getNumValues() - 1); |
| 1194 | |
| 1195 | // Copy the intrinsic results to registers |
| 1196 | for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { |
| 1197 | SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); |
| 1198 | if (!CopyToReg) |
| 1199 | continue; |
| 1200 | |
| 1201 | Chain = DAG.getCopyToReg( |
| 1202 | Chain, DL, |
| 1203 | CopyToReg->getOperand(1), |
| 1204 | SDValue(Result, i - 1), |
| 1205 | SDValue()); |
| 1206 | |
| 1207 | DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); |
| 1208 | } |
| 1209 | |
| 1210 | // Remove the old intrinsic from the chain |
| 1211 | DAG.ReplaceAllUsesOfValueWith( |
| 1212 | SDValue(Intr, Intr->getNumValues() - 1), |
| 1213 | Intr->getOperand(0)); |
| 1214 | |
| 1215 | return Chain; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 1218 | SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, |
| 1219 | SDValue Op, |
| 1220 | SelectionDAG &DAG) const { |
| 1221 | GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); |
| 1222 | |
| 1223 | if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS) |
| 1224 | return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); |
| 1225 | |
| 1226 | SDLoc DL(GSD); |
| 1227 | const GlobalValue *GV = GSD->getGlobal(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1228 | MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace()); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 1229 | |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 1230 | SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32); |
Tom Stellard | c93fc11 | 2015-12-10 02:13:01 +0000 | [diff] [blame] | 1231 | return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA); |
Tom Stellard | 067c815 | 2014-07-21 14:01:14 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 1234 | SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL, |
| 1235 | SDValue V) const { |
| 1236 | // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, |
| 1237 | // so we will end up with redundant moves to m0. |
| 1238 | // |
| 1239 | // We can't use S_MOV_B32, because there is no way to specify m0 as the |
| 1240 | // destination register. |
| 1241 | // |
| 1242 | // We have to use them both. Machine cse will combine all the S_MOV_B32 |
| 1243 | // instructions and the register coalescer eliminate the extra copies. |
| 1244 | SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V); |
| 1245 | return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32), |
| 1246 | SDValue(M0, 0), SDValue()); // Glue |
| 1247 | // A Null SDValue creates |
| 1248 | // a glue result. |
| 1249 | } |
| 1250 | |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 1251 | SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, |
| 1252 | SDValue Op, |
| 1253 | MVT VT, |
| 1254 | unsigned Offset) const { |
| 1255 | SDLoc SL(Op); |
| 1256 | SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL, |
| 1257 | DAG.getEntryNode(), Offset, false); |
| 1258 | // The local size values will have the hi 16-bits as zero. |
| 1259 | return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, |
| 1260 | DAG.getValueType(VT)); |
| 1261 | } |
| 1262 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1263 | SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
| 1264 | SelectionDAG &DAG) const { |
| 1265 | MachineFunction &MF = DAG.getMachineFunction(); |
Tom Stellard | dcb9f09 | 2015-07-09 21:20:37 +0000 | [diff] [blame] | 1266 | auto MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1267 | const SIRegisterInfo *TRI = |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 1268 | static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1269 | |
| 1270 | EVT VT = Op.getValueType(); |
| 1271 | SDLoc DL(Op); |
| 1272 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 1273 | |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 1274 | // TODO: Should this propagate fast-math-flags? |
| 1275 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1276 | switch (IntrinsicID) { |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 1277 | case Intrinsic::amdgcn_dispatch_ptr: |
Matt Arsenault | 800fecf | 2016-01-11 21:18:33 +0000 | [diff] [blame] | 1278 | if (!Subtarget->isAmdHsaOS()) { |
| 1279 | DiagnosticInfoUnsupported BadIntrin(*MF.getFunction(), |
| 1280 | "hsa intrinsic without hsa target"); |
| 1281 | DAG.getContext()->diagnose(BadIntrin); |
| 1282 | return DAG.getUNDEF(VT); |
| 1283 | } |
| 1284 | |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 1285 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, |
| 1286 | TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT); |
| 1287 | |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1288 | case Intrinsic::r600_read_ngroups_x: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1289 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 1290 | SI::KernelInputOffsets::NGROUPS_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1291 | case Intrinsic::r600_read_ngroups_y: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1292 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 1293 | SI::KernelInputOffsets::NGROUPS_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1294 | case Intrinsic::r600_read_ngroups_z: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1295 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 1296 | SI::KernelInputOffsets::NGROUPS_Z, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1297 | case Intrinsic::r600_read_global_size_x: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1298 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 1299 | SI::KernelInputOffsets::GLOBAL_SIZE_X, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1300 | case Intrinsic::r600_read_global_size_y: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1301 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 1302 | SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1303 | case Intrinsic::r600_read_global_size_z: |
Tom Stellard | ec2e43c | 2014-09-22 15:35:29 +0000 | [diff] [blame] | 1304 | return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), |
| 1305 | SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1306 | case Intrinsic::r600_read_local_size_x: |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 1307 | return lowerImplicitZextParam(DAG, Op, MVT::i16, |
| 1308 | SI::KernelInputOffsets::LOCAL_SIZE_X); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1309 | case Intrinsic::r600_read_local_size_y: |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 1310 | return lowerImplicitZextParam(DAG, Op, MVT::i16, |
| 1311 | SI::KernelInputOffsets::LOCAL_SIZE_Y); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1312 | case Intrinsic::r600_read_local_size_z: |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 1313 | return lowerImplicitZextParam(DAG, Op, MVT::i16, |
| 1314 | SI::KernelInputOffsets::LOCAL_SIZE_Z); |
Matt Arsenault | bef34e2 | 2016-01-22 21:30:34 +0000 | [diff] [blame^] | 1315 | case Intrinsic::amdgcn_read_workdim: |
| 1316 | case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name. |
Matt Arsenault | ff6da2f | 2015-11-30 21:15:45 +0000 | [diff] [blame] | 1317 | // Really only 2 bits. |
| 1318 | return lowerImplicitZextParam(DAG, Op, MVT::i8, |
| 1319 | getImplicitParameterOffset(MFI, GRID_DIM)); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1320 | case Intrinsic::r600_read_tgid_x: |
| 1321 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 1322 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1323 | case Intrinsic::r600_read_tgid_y: |
| 1324 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 1325 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1326 | case Intrinsic::r600_read_tgid_z: |
| 1327 | return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 1328 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1329 | case Intrinsic::r600_read_tidig_x: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 1330 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 1331 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1332 | case Intrinsic::r600_read_tidig_y: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 1333 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 1334 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1335 | case Intrinsic::r600_read_tidig_z: |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 1336 | return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, |
Matt Arsenault | ac234b6 | 2015-11-30 21:15:57 +0000 | [diff] [blame] | 1337 | TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1338 | case AMDGPUIntrinsic::SI_load_const: { |
| 1339 | SDValue Ops[] = { |
| 1340 | Op.getOperand(1), |
| 1341 | Op.getOperand(2) |
| 1342 | }; |
| 1343 | |
| 1344 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 1345 | MachinePointerInfo(), |
| 1346 | MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant, |
| 1347 | VT.getStoreSize(), 4); |
| 1348 | return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, |
| 1349 | Op->getVTList(), Ops, VT, MMO); |
| 1350 | } |
| 1351 | case AMDGPUIntrinsic::SI_sample: |
| 1352 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG); |
| 1353 | case AMDGPUIntrinsic::SI_sampleb: |
| 1354 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG); |
| 1355 | case AMDGPUIntrinsic::SI_sampled: |
| 1356 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG); |
| 1357 | case AMDGPUIntrinsic::SI_samplel: |
| 1358 | return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG); |
| 1359 | case AMDGPUIntrinsic::SI_vs_load_input: |
| 1360 | return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, |
| 1361 | Op.getOperand(1), |
| 1362 | Op.getOperand(2), |
| 1363 | Op.getOperand(3)); |
Marek Olsak | 43650e4 | 2015-03-24 13:40:08 +0000 | [diff] [blame] | 1364 | |
Tom Stellard | 2a9d947 | 2015-05-12 15:00:46 +0000 | [diff] [blame] | 1365 | case AMDGPUIntrinsic::SI_fs_constant: { |
| 1366 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); |
| 1367 | SDValue Glue = M0.getValue(1); |
| 1368 | return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, |
| 1369 | DAG.getConstant(2, DL, MVT::i32), // P0 |
| 1370 | Op.getOperand(1), Op.getOperand(2), Glue); |
| 1371 | } |
Marek Olsak | 6f6d318 | 2015-10-29 15:29:09 +0000 | [diff] [blame] | 1372 | case AMDGPUIntrinsic::SI_packf16: |
| 1373 | if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef()) |
| 1374 | return DAG.getUNDEF(MVT::i32); |
| 1375 | return Op; |
Tom Stellard | 2a9d947 | 2015-05-12 15:00:46 +0000 | [diff] [blame] | 1376 | case AMDGPUIntrinsic::SI_fs_interp: { |
| 1377 | SDValue IJ = Op.getOperand(4); |
| 1378 | SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, |
| 1379 | DAG.getConstant(0, DL, MVT::i32)); |
| 1380 | SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ, |
| 1381 | DAG.getConstant(1, DL, MVT::i32)); |
| 1382 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3)); |
| 1383 | SDValue Glue = M0.getValue(1); |
| 1384 | SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL, |
| 1385 | DAG.getVTList(MVT::f32, MVT::Glue), |
| 1386 | I, Op.getOperand(1), Op.getOperand(2), Glue); |
| 1387 | Glue = SDValue(P1.getNode(), 1); |
| 1388 | return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J, |
| 1389 | Op.getOperand(1), Op.getOperand(2), Glue); |
| 1390 | } |
Tom Stellard | ad7d03d | 2015-12-15 17:02:49 +0000 | [diff] [blame] | 1391 | case Intrinsic::amdgcn_interp_p1: { |
| 1392 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); |
| 1393 | SDValue Glue = M0.getValue(1); |
| 1394 | return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), |
| 1395 | Op.getOperand(2), Op.getOperand(3), Glue); |
| 1396 | } |
| 1397 | case Intrinsic::amdgcn_interp_p2: { |
| 1398 | SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); |
| 1399 | SDValue Glue = SDValue(M0.getNode(), 1); |
| 1400 | return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), |
| 1401 | Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), |
| 1402 | Glue); |
| 1403 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1404 | default: |
| 1405 | return AMDGPUTargetLowering::LowerOperation(Op, DAG); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, |
| 1410 | SelectionDAG &DAG) const { |
| 1411 | MachineFunction &MF = DAG.getMachineFunction(); |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 1412 | SDLoc DL(Op); |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1413 | SDValue Chain = Op.getOperand(0); |
| 1414 | unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 1415 | |
| 1416 | switch (IntrinsicID) { |
Tom Stellard | fc92e77 | 2015-05-12 14:18:14 +0000 | [diff] [blame] | 1417 | case AMDGPUIntrinsic::SI_sendmsg: { |
| 1418 | Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); |
| 1419 | SDValue Glue = Chain.getValue(1); |
| 1420 | return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain, |
| 1421 | Op.getOperand(2), Glue); |
| 1422 | } |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1423 | case AMDGPUIntrinsic::SI_tbuffer_store: { |
Matt Arsenault | a5789bb | 2014-07-26 06:23:37 +0000 | [diff] [blame] | 1424 | SDValue Ops[] = { |
| 1425 | Chain, |
| 1426 | Op.getOperand(2), |
| 1427 | Op.getOperand(3), |
| 1428 | Op.getOperand(4), |
| 1429 | Op.getOperand(5), |
| 1430 | Op.getOperand(6), |
| 1431 | Op.getOperand(7), |
| 1432 | Op.getOperand(8), |
| 1433 | Op.getOperand(9), |
| 1434 | Op.getOperand(10), |
| 1435 | Op.getOperand(11), |
| 1436 | Op.getOperand(12), |
| 1437 | Op.getOperand(13), |
| 1438 | Op.getOperand(14) |
| 1439 | }; |
| 1440 | |
| 1441 | EVT VT = Op.getOperand(3).getValueType(); |
| 1442 | |
| 1443 | MachineMemOperand *MMO = MF.getMachineMemOperand( |
| 1444 | MachinePointerInfo(), |
| 1445 | MachineMemOperand::MOStore, |
| 1446 | VT.getStoreSize(), 4); |
| 1447 | return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, |
| 1448 | Op->getVTList(), Ops, VT, MMO); |
| 1449 | } |
| 1450 | default: |
| 1451 | return SDValue(); |
| 1452 | } |
| 1453 | } |
| 1454 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1455 | SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { |
| 1456 | SDLoc DL(Op); |
| 1457 | LoadSDNode *Load = cast<LoadSDNode>(Op); |
| 1458 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1459 | if (Op.getValueType().isVector()) { |
| 1460 | assert(Op.getValueType().getVectorElementType() == MVT::i32 && |
| 1461 | "Custom lowering for non-i32 vectors hasn't been implemented."); |
| 1462 | unsigned NumElements = Op.getValueType().getVectorNumElements(); |
| 1463 | assert(NumElements != 2 && "v2 loads are supported for all address spaces."); |
Matt Arsenault | 4d801cd | 2015-11-24 12:05:03 +0000 | [diff] [blame] | 1464 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1465 | switch (Load->getAddressSpace()) { |
| 1466 | default: break; |
Tom Stellard | a6f24c6 | 2015-12-15 20:55:55 +0000 | [diff] [blame] | 1467 | case AMDGPUAS::CONSTANT_ADDRESS: |
| 1468 | if (isMemOpUniform(Load)) |
| 1469 | break; |
| 1470 | // Non-uniform loads will be selected to MUBUF instructions, so they |
| 1471 | // have the same legalization requires ments as global and private |
| 1472 | // loads. |
| 1473 | // |
| 1474 | // Fall-through |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1475 | case AMDGPUAS::GLOBAL_ADDRESS: |
| 1476 | case AMDGPUAS::PRIVATE_ADDRESS: |
Matt Arsenault | 4d801cd | 2015-11-24 12:05:03 +0000 | [diff] [blame] | 1477 | if (NumElements >= 8) |
| 1478 | return SplitVectorLoad(Op, DAG); |
| 1479 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1480 | // v4 loads are supported for private and global memory. |
| 1481 | if (NumElements <= 4) |
| 1482 | break; |
| 1483 | // fall-through |
| 1484 | case AMDGPUAS::LOCAL_ADDRESS: |
Matt Arsenault | ff05da8 | 2015-11-24 12:18:54 +0000 | [diff] [blame] | 1485 | // If properly aligned, if we split we might be able to use ds_read_b64. |
| 1486 | return SplitVectorLoad(Op, DAG); |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1487 | } |
Tom Stellard | e937360 | 2014-01-22 19:24:14 +0000 | [diff] [blame] | 1488 | } |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1489 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1490 | return AMDGPUTargetLowering::LowerLOAD(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 1493 | SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode, |
| 1494 | const SDValue &Op, |
| 1495 | SelectionDAG &DAG) const { |
| 1496 | return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1), |
| 1497 | Op.getOperand(2), |
Tom Stellard | 868fd92 | 2014-04-17 21:00:11 +0000 | [diff] [blame] | 1498 | Op.getOperand(3), |
Tom Stellard | 9fa1791 | 2013-08-14 23:24:45 +0000 | [diff] [blame] | 1499 | Op.getOperand(4)); |
| 1500 | } |
| 1501 | |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1502 | SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { |
| 1503 | if (Op.getValueType() != MVT::i64) |
| 1504 | return SDValue(); |
| 1505 | |
| 1506 | SDLoc DL(Op); |
| 1507 | SDValue Cond = Op.getOperand(0); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1508 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1509 | SDValue Zero = DAG.getConstant(0, DL, MVT::i32); |
| 1510 | SDValue One = DAG.getConstant(1, DL, MVT::i32); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1511 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 1512 | SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); |
| 1513 | SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); |
| 1514 | |
| 1515 | SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); |
| 1516 | 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] | 1517 | |
| 1518 | SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); |
| 1519 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 1520 | SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); |
| 1521 | 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] | 1522 | |
| 1523 | SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); |
| 1524 | |
Tom Stellard | 7ea3d6d | 2014-03-31 14:01:55 +0000 | [diff] [blame] | 1525 | SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi); |
| 1526 | return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); |
Tom Stellard | 0ec134f | 2014-02-04 17:18:40 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1529 | // Catch division cases where we can use shortcuts with rcp and rsq |
| 1530 | // instructions. |
| 1531 | SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1532 | SDLoc SL(Op); |
| 1533 | SDValue LHS = Op.getOperand(0); |
| 1534 | SDValue RHS = Op.getOperand(1); |
| 1535 | EVT VT = Op.getValueType(); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1536 | bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1537 | |
| 1538 | if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1539 | if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) && |
| 1540 | CLHS->isExactlyValue(1.0)) { |
| 1541 | // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to |
| 1542 | // the CI documentation has a worst case error of 1 ulp. |
| 1543 | // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to |
| 1544 | // use it as long as we aren't trying to use denormals. |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1545 | |
| 1546 | // 1.0 / sqrt(x) -> rsq(x) |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1547 | // |
| 1548 | // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP |
| 1549 | // error seems really high at 2^29 ULP. |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1550 | if (RHS.getOpcode() == ISD::FSQRT) |
| 1551 | return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); |
| 1552 | |
| 1553 | // 1.0 / x -> rcp(x) |
| 1554 | return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); |
| 1555 | } |
| 1556 | } |
| 1557 | |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1558 | if (Unsafe) { |
| 1559 | // Turn into multiply by the reciprocal. |
| 1560 | // x / y -> x * (1.0 / y) |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 1561 | SDNodeFlags Flags; |
| 1562 | Flags.setUnsafeAlgebra(true); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1563 | SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 1564 | return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags); |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | return SDValue(); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | 22ca3f8 | 2014-07-15 23:50:10 +0000 | [diff] [blame] | 1571 | SDValue FastLowered = LowerFastFDIV(Op, DAG); |
| 1572 | if (FastLowered.getNode()) |
| 1573 | return FastLowered; |
| 1574 | |
| 1575 | // This uses v_rcp_f32 which does not handle denormals. Let this hit a |
| 1576 | // selection error for now rather than do something incorrect. |
| 1577 | if (Subtarget->hasFP32Denormals()) |
| 1578 | return SDValue(); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1579 | |
| 1580 | SDLoc SL(Op); |
| 1581 | SDValue LHS = Op.getOperand(0); |
| 1582 | SDValue RHS = Op.getOperand(1); |
| 1583 | |
| 1584 | SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); |
| 1585 | |
| 1586 | const APFloat K0Val(BitsToFloat(0x6f800000)); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1587 | const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1588 | |
| 1589 | const APFloat K1Val(BitsToFloat(0x2f800000)); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1590 | const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1591 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1592 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1593 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1594 | EVT SetCCVT = |
| 1595 | getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1596 | |
| 1597 | SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); |
| 1598 | |
| 1599 | SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); |
| 1600 | |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 1601 | // TODO: Should this propagate fast-math-flags? |
| 1602 | |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1603 | r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); |
| 1604 | |
| 1605 | SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); |
| 1606 | |
| 1607 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); |
| 1608 | |
| 1609 | return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); |
| 1610 | } |
| 1611 | |
| 1612 | SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 1613 | if (DAG.getTarget().Options.UnsafeFPMath) |
| 1614 | return LowerFastFDIV(Op, DAG); |
| 1615 | |
| 1616 | SDLoc SL(Op); |
| 1617 | SDValue X = Op.getOperand(0); |
| 1618 | SDValue Y = Op.getOperand(1); |
| 1619 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1620 | const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 1621 | |
| 1622 | SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); |
| 1623 | |
| 1624 | SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); |
| 1625 | |
| 1626 | SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); |
| 1627 | |
| 1628 | SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); |
| 1629 | |
| 1630 | SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); |
| 1631 | |
| 1632 | SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); |
| 1633 | |
| 1634 | SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); |
| 1635 | |
| 1636 | SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); |
| 1637 | |
| 1638 | SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); |
| 1639 | SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); |
| 1640 | |
| 1641 | SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, |
| 1642 | NegDivScale0, Mul, DivScale1); |
| 1643 | |
| 1644 | SDValue Scale; |
| 1645 | |
| 1646 | if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
| 1647 | // Workaround a hardware bug on SI where the condition output from div_scale |
| 1648 | // is not usable. |
| 1649 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1650 | const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); |
Matt Arsenault | 0bbcd8b | 2015-02-14 04:30:08 +0000 | [diff] [blame] | 1651 | |
| 1652 | // Figure out if the scale to use for div_fmas. |
| 1653 | SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); |
| 1654 | SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); |
| 1655 | SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); |
| 1656 | SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); |
| 1657 | |
| 1658 | SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); |
| 1659 | SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); |
| 1660 | |
| 1661 | SDValue Scale0Hi |
| 1662 | = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); |
| 1663 | SDValue Scale1Hi |
| 1664 | = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); |
| 1665 | |
| 1666 | SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); |
| 1667 | SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); |
| 1668 | Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); |
| 1669 | } else { |
| 1670 | Scale = DivScale1.getValue(1); |
| 1671 | } |
| 1672 | |
| 1673 | SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, |
| 1674 | Fma4, Fma3, Mul, Scale); |
| 1675 | |
| 1676 | return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); |
Matt Arsenault | e9fa3b8 | 2014-07-15 20:18:31 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { |
| 1680 | EVT VT = Op.getValueType(); |
| 1681 | |
| 1682 | if (VT == MVT::f32) |
| 1683 | return LowerFDIV32(Op, DAG); |
| 1684 | |
| 1685 | if (VT == MVT::f64) |
| 1686 | return LowerFDIV64(Op, DAG); |
| 1687 | |
| 1688 | llvm_unreachable("Unexpected type for fdiv"); |
| 1689 | } |
| 1690 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1691 | SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { |
| 1692 | SDLoc DL(Op); |
| 1693 | StoreSDNode *Store = cast<StoreSDNode>(Op); |
| 1694 | EVT VT = Store->getMemoryVT(); |
| 1695 | |
Tom Stellard | 9b3816b | 2014-06-24 23:33:04 +0000 | [diff] [blame] | 1696 | // These stores are legal. |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1697 | if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { |
| 1698 | if (VT.isVector() && VT.getVectorNumElements() > 4) |
Matt Arsenault | 83e6058 | 2014-07-24 17:10:35 +0000 | [diff] [blame] | 1699 | return ScalarizeVectorStore(Op, DAG); |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 1700 | return SDValue(); |
| 1701 | } |
| 1702 | |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1703 | SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG); |
| 1704 | if (Ret.getNode()) |
| 1705 | return Ret; |
| 1706 | |
| 1707 | if (VT.isVector() && VT.getVectorNumElements() >= 8) |
Matt Arsenault | 4d801cd | 2015-11-24 12:05:03 +0000 | [diff] [blame] | 1708 | return SplitVectorStore(Op, DAG); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1709 | |
Tom Stellard | 1c8788e | 2014-03-07 20:12:33 +0000 | [diff] [blame] | 1710 | if (VT == MVT::i1) |
| 1711 | return DAG.getTruncStore(Store->getChain(), DL, |
| 1712 | DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), |
| 1713 | Store->getBasePtr(), MVT::i1, Store->getMemOperand()); |
| 1714 | |
Tom Stellard | e812f2f | 2014-07-21 15:45:06 +0000 | [diff] [blame] | 1715 | return SDValue(); |
Tom Stellard | 81d871d | 2013-11-13 23:36:50 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1718 | SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1719 | SDLoc DL(Op); |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1720 | EVT VT = Op.getValueType(); |
| 1721 | SDValue Arg = Op.getOperand(0); |
Sanjay Patel | a260701 | 2015-09-16 16:31:21 +0000 | [diff] [blame] | 1722 | // TODO: Should this propagate fast-math-flags? |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1723 | SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, |
| 1724 | DAG.getNode(ISD::FMUL, DL, VT, Arg, |
| 1725 | DAG.getConstantFP(0.5/M_PI, DL, |
| 1726 | VT))); |
Matt Arsenault | ad14ce8 | 2014-07-19 18:44:39 +0000 | [diff] [blame] | 1727 | |
| 1728 | switch (Op.getOpcode()) { |
| 1729 | case ISD::FCOS: |
| 1730 | return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); |
| 1731 | case ISD::FSIN: |
| 1732 | return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); |
| 1733 | default: |
| 1734 | llvm_unreachable("Wrong trig opcode"); |
| 1735 | } |
| 1736 | } |
| 1737 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1738 | //===----------------------------------------------------------------------===// |
| 1739 | // Custom DAG optimizations |
| 1740 | //===----------------------------------------------------------------------===// |
| 1741 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1742 | SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 1743 | DAGCombinerInfo &DCI) const { |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1744 | EVT VT = N->getValueType(0); |
| 1745 | EVT ScalarVT = VT.getScalarType(); |
| 1746 | if (ScalarVT != MVT::f32) |
| 1747 | return SDValue(); |
| 1748 | |
| 1749 | SelectionDAG &DAG = DCI.DAG; |
| 1750 | SDLoc DL(N); |
| 1751 | |
| 1752 | SDValue Src = N->getOperand(0); |
| 1753 | EVT SrcVT = Src.getValueType(); |
| 1754 | |
| 1755 | // TODO: We could try to match extracting the higher bytes, which would be |
| 1756 | // easier if i8 vectors weren't promoted to i32 vectors, particularly after |
| 1757 | // types are legalized. v4i8 -> v4f32 is probably the only case to worry |
| 1758 | // about in practice. |
| 1759 | if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { |
| 1760 | if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { |
| 1761 | SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); |
| 1762 | DCI.AddToWorklist(Cvt.getNode()); |
| 1763 | return Cvt; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | // We are primarily trying to catch operations on illegal vector types |
| 1768 | // before they are expanded. |
| 1769 | // For scalars, we can use the more flexible method of checking masked bits |
| 1770 | // after legalization. |
| 1771 | if (!DCI.isBeforeLegalize() || |
| 1772 | !SrcVT.isVector() || |
| 1773 | SrcVT.getVectorElementType() != MVT::i8) { |
| 1774 | return SDValue(); |
| 1775 | } |
| 1776 | |
| 1777 | assert(DCI.isBeforeLegalize() && "Unexpected legal type"); |
| 1778 | |
| 1779 | // Weird sized vectors are a pain to handle, but we know 3 is really the same |
| 1780 | // size as 4. |
| 1781 | unsigned NElts = SrcVT.getVectorNumElements(); |
| 1782 | if (!SrcVT.isSimple() && NElts != 3) |
| 1783 | return SDValue(); |
| 1784 | |
| 1785 | // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to |
| 1786 | // prevent a mess from expanding to v4i32 and repacking. |
| 1787 | if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) { |
| 1788 | EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT); |
| 1789 | EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT); |
| 1790 | EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts); |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1791 | LoadSDNode *Load = cast<LoadSDNode>(Src); |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 1792 | |
| 1793 | unsigned AS = Load->getAddressSpace(); |
| 1794 | unsigned Align = Load->getAlignment(); |
| 1795 | Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext()); |
Mehdi Amini | a749f2a | 2015-07-09 02:09:52 +0000 | [diff] [blame] | 1796 | unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty); |
Matt Arsenault | e698663 | 2015-01-14 01:35:22 +0000 | [diff] [blame] | 1797 | |
| 1798 | // Don't try to replace the load if we have to expand it due to alignment |
| 1799 | // problems. Otherwise we will end up scalarizing the load, and trying to |
| 1800 | // repack into the vector for no real reason. |
| 1801 | if (Align < ABIAlignment && |
| 1802 | !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) { |
| 1803 | return SDValue(); |
| 1804 | } |
| 1805 | |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 1806 | SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT, |
| 1807 | Load->getChain(), |
| 1808 | Load->getBasePtr(), |
| 1809 | LoadVT, |
| 1810 | Load->getMemOperand()); |
| 1811 | |
| 1812 | // Make sure successors of the original load stay after it by updating |
| 1813 | // them to use the new Chain. |
| 1814 | DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1)); |
| 1815 | |
| 1816 | SmallVector<SDValue, 4> Elts; |
| 1817 | if (RegVT.isVector()) |
| 1818 | DAG.ExtractVectorElements(NewLoad, Elts); |
| 1819 | else |
| 1820 | Elts.push_back(NewLoad); |
| 1821 | |
| 1822 | SmallVector<SDValue, 4> Ops; |
| 1823 | |
| 1824 | unsigned EltIdx = 0; |
| 1825 | for (SDValue Elt : Elts) { |
| 1826 | unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx); |
| 1827 | for (unsigned I = 0; I < ComponentsInElt; ++I) { |
| 1828 | unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I; |
| 1829 | SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt); |
| 1830 | DCI.AddToWorklist(Cvt.getNode()); |
| 1831 | Ops.push_back(Cvt); |
| 1832 | } |
| 1833 | |
| 1834 | ++EltIdx; |
| 1835 | } |
| 1836 | |
| 1837 | assert(Ops.size() == NElts); |
| 1838 | |
| 1839 | return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops); |
| 1840 | } |
| 1841 | |
| 1842 | return SDValue(); |
| 1843 | } |
| 1844 | |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 1845 | /// \brief Return true if the given offset Size in bytes can be folded into |
| 1846 | /// the immediate offsets of a memory instruction for the given address space. |
| 1847 | static bool canFoldOffset(unsigned OffsetSize, unsigned AS, |
| 1848 | const AMDGPUSubtarget &STI) { |
| 1849 | switch (AS) { |
| 1850 | case AMDGPUAS::GLOBAL_ADDRESS: { |
| 1851 | // MUBUF instructions a 12-bit offset in bytes. |
| 1852 | return isUInt<12>(OffsetSize); |
| 1853 | } |
| 1854 | case AMDGPUAS::CONSTANT_ADDRESS: { |
| 1855 | // SMRD instructions have an 8-bit offset in dwords on SI and |
| 1856 | // a 20-bit offset in bytes on VI. |
| 1857 | if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) |
| 1858 | return isUInt<20>(OffsetSize); |
| 1859 | else |
| 1860 | return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); |
| 1861 | } |
| 1862 | case AMDGPUAS::LOCAL_ADDRESS: |
| 1863 | case AMDGPUAS::REGION_ADDRESS: { |
| 1864 | // The single offset versions have a 16-bit offset in bytes. |
| 1865 | return isUInt<16>(OffsetSize); |
| 1866 | } |
| 1867 | case AMDGPUAS::PRIVATE_ADDRESS: |
| 1868 | // Indirect register addressing does not use any offsets. |
| 1869 | default: |
| 1870 | return 0; |
| 1871 | } |
| 1872 | } |
| 1873 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1874 | // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) |
| 1875 | |
| 1876 | // This is a variant of |
| 1877 | // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), |
| 1878 | // |
| 1879 | // The normal DAG combiner will do this, but only if the add has one use since |
| 1880 | // that would increase the number of instructions. |
| 1881 | // |
| 1882 | // This prevents us from seeing a constant offset that can be folded into a |
| 1883 | // memory instruction's addressing mode. If we know the resulting add offset of |
| 1884 | // a pointer can be folded into an addressing offset, we can replace the pointer |
| 1885 | // operand with the add of new constant offset. This eliminates one of the uses, |
| 1886 | // and may allow the remaining use to also be simplified. |
| 1887 | // |
| 1888 | SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, |
| 1889 | unsigned AddrSpace, |
| 1890 | DAGCombinerInfo &DCI) const { |
| 1891 | SDValue N0 = N->getOperand(0); |
| 1892 | SDValue N1 = N->getOperand(1); |
| 1893 | |
| 1894 | if (N0.getOpcode() != ISD::ADD) |
| 1895 | return SDValue(); |
| 1896 | |
| 1897 | const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); |
| 1898 | if (!CN1) |
| 1899 | return SDValue(); |
| 1900 | |
| 1901 | const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 1902 | if (!CAdd) |
| 1903 | return SDValue(); |
| 1904 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1905 | // If the resulting offset is too large, we can't fold it into the addressing |
| 1906 | // mode offset. |
| 1907 | APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); |
Eric Christopher | 6c5b511 | 2015-03-11 18:43:21 +0000 | [diff] [blame] | 1908 | if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget)) |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1909 | return SDValue(); |
| 1910 | |
| 1911 | SelectionDAG &DAG = DCI.DAG; |
| 1912 | SDLoc SL(N); |
| 1913 | EVT VT = N->getValueType(0); |
| 1914 | |
| 1915 | SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1916 | SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 1917 | |
| 1918 | return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); |
| 1919 | } |
| 1920 | |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 1921 | SDValue SITargetLowering::performAndCombine(SDNode *N, |
| 1922 | DAGCombinerInfo &DCI) const { |
| 1923 | if (DCI.isBeforeLegalize()) |
| 1924 | return SDValue(); |
| 1925 | |
Matt Arsenault | 6e3a451 | 2016-01-18 22:01:13 +0000 | [diff] [blame] | 1926 | if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI)) |
| 1927 | return Base; |
| 1928 | |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 1929 | SelectionDAG &DAG = DCI.DAG; |
| 1930 | |
| 1931 | // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> |
| 1932 | // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) |
| 1933 | SDValue LHS = N->getOperand(0); |
| 1934 | SDValue RHS = N->getOperand(1); |
| 1935 | |
| 1936 | if (LHS.getOpcode() == ISD::SETCC && |
| 1937 | RHS.getOpcode() == ISD::SETCC) { |
| 1938 | ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); |
| 1939 | ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); |
| 1940 | |
| 1941 | SDValue X = LHS.getOperand(0); |
| 1942 | SDValue Y = RHS.getOperand(0); |
| 1943 | if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) |
| 1944 | return SDValue(); |
| 1945 | |
| 1946 | if (LCC == ISD::SETO) { |
| 1947 | if (X != LHS.getOperand(1)) |
| 1948 | return SDValue(); |
| 1949 | |
| 1950 | if (RCC == ISD::SETUNE) { |
| 1951 | const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); |
| 1952 | if (!C1 || !C1->isInfinity() || C1->isNegative()) |
| 1953 | return SDValue(); |
| 1954 | |
| 1955 | const uint32_t Mask = SIInstrFlags::N_NORMAL | |
| 1956 | SIInstrFlags::N_SUBNORMAL | |
| 1957 | SIInstrFlags::N_ZERO | |
| 1958 | SIInstrFlags::P_ZERO | |
| 1959 | SIInstrFlags::P_SUBNORMAL | |
| 1960 | SIInstrFlags::P_NORMAL; |
| 1961 | |
| 1962 | static_assert(((~(SIInstrFlags::S_NAN | |
| 1963 | SIInstrFlags::Q_NAN | |
| 1964 | SIInstrFlags::N_INFINITY | |
| 1965 | SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, |
| 1966 | "mask not equal"); |
| 1967 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1968 | SDLoc DL(N); |
| 1969 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, |
| 1970 | X, DAG.getConstant(Mask, DL, MVT::i32)); |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 1971 | } |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | return SDValue(); |
| 1976 | } |
| 1977 | |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 1978 | SDValue SITargetLowering::performOrCombine(SDNode *N, |
| 1979 | DAGCombinerInfo &DCI) const { |
| 1980 | SelectionDAG &DAG = DCI.DAG; |
| 1981 | SDValue LHS = N->getOperand(0); |
| 1982 | SDValue RHS = N->getOperand(1); |
| 1983 | |
| 1984 | // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) |
| 1985 | if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && |
| 1986 | RHS.getOpcode() == AMDGPUISD::FP_CLASS) { |
| 1987 | SDValue Src = LHS.getOperand(0); |
| 1988 | if (Src != RHS.getOperand(0)) |
| 1989 | return SDValue(); |
| 1990 | |
| 1991 | const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); |
| 1992 | const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); |
| 1993 | if (!CLHS || !CRHS) |
| 1994 | return SDValue(); |
| 1995 | |
| 1996 | // Only 10 bits are used. |
| 1997 | static const uint32_t MaxMask = 0x3ff; |
| 1998 | |
| 1999 | uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2000 | SDLoc DL(N); |
| 2001 | return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, |
| 2002 | Src, DAG.getConstant(NewMask, DL, MVT::i32)); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | return SDValue(); |
| 2006 | } |
| 2007 | |
| 2008 | SDValue SITargetLowering::performClassCombine(SDNode *N, |
| 2009 | DAGCombinerInfo &DCI) const { |
| 2010 | SelectionDAG &DAG = DCI.DAG; |
| 2011 | SDValue Mask = N->getOperand(1); |
| 2012 | |
| 2013 | // fp_class x, 0 -> false |
| 2014 | if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { |
| 2015 | if (CMask->isNullValue()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2016 | return DAG.getConstant(0, SDLoc(N), MVT::i1); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | return SDValue(); |
| 2020 | } |
| 2021 | |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2022 | static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { |
| 2023 | switch (Opc) { |
| 2024 | case ISD::FMAXNUM: |
| 2025 | return AMDGPUISD::FMAX3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 2026 | case ISD::SMAX: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2027 | return AMDGPUISD::SMAX3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 2028 | case ISD::UMAX: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2029 | return AMDGPUISD::UMAX3; |
| 2030 | case ISD::FMINNUM: |
| 2031 | return AMDGPUISD::FMIN3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 2032 | case ISD::SMIN: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2033 | return AMDGPUISD::SMIN3; |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 2034 | case ISD::UMIN: |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2035 | return AMDGPUISD::UMIN3; |
| 2036 | default: |
| 2037 | llvm_unreachable("Not a min/max opcode"); |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | SDValue SITargetLowering::performMin3Max3Combine(SDNode *N, |
| 2042 | DAGCombinerInfo &DCI) const { |
| 2043 | SelectionDAG &DAG = DCI.DAG; |
| 2044 | |
| 2045 | unsigned Opc = N->getOpcode(); |
| 2046 | SDValue Op0 = N->getOperand(0); |
| 2047 | SDValue Op1 = N->getOperand(1); |
| 2048 | |
| 2049 | // Only do this if the inner op has one use since this will just increases |
| 2050 | // register pressure for no benefit. |
| 2051 | |
| 2052 | // max(max(a, b), c) |
| 2053 | if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { |
| 2054 | SDLoc DL(N); |
| 2055 | return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), |
| 2056 | DL, |
| 2057 | N->getValueType(0), |
| 2058 | Op0.getOperand(0), |
| 2059 | Op0.getOperand(1), |
| 2060 | Op1); |
| 2061 | } |
| 2062 | |
| 2063 | // max(a, max(b, c)) |
| 2064 | if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { |
| 2065 | SDLoc DL(N); |
| 2066 | return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), |
| 2067 | DL, |
| 2068 | N->getValueType(0), |
| 2069 | Op0, |
| 2070 | Op1.getOperand(0), |
| 2071 | Op1.getOperand(1)); |
| 2072 | } |
| 2073 | |
| 2074 | return SDValue(); |
| 2075 | } |
| 2076 | |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 2077 | SDValue SITargetLowering::performSetCCCombine(SDNode *N, |
| 2078 | DAGCombinerInfo &DCI) const { |
| 2079 | SelectionDAG &DAG = DCI.DAG; |
| 2080 | SDLoc SL(N); |
| 2081 | |
| 2082 | SDValue LHS = N->getOperand(0); |
| 2083 | SDValue RHS = N->getOperand(1); |
| 2084 | EVT VT = LHS.getValueType(); |
| 2085 | |
| 2086 | if (VT != MVT::f32 && VT != MVT::f64) |
| 2087 | return SDValue(); |
| 2088 | |
| 2089 | // Match isinf pattern |
| 2090 | // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) |
| 2091 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 2092 | if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { |
| 2093 | const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); |
| 2094 | if (!CRHS) |
| 2095 | return SDValue(); |
| 2096 | |
| 2097 | const APFloat &APF = CRHS->getValueAPF(); |
| 2098 | if (APF.isInfinity() && !APF.isNegative()) { |
| 2099 | unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2100 | return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), |
| 2101 | DAG.getConstant(Mask, SL, MVT::i32)); |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | return SDValue(); |
| 2106 | } |
| 2107 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 2108 | SDValue SITargetLowering::PerformDAGCombine(SDNode *N, |
| 2109 | DAGCombinerInfo &DCI) const { |
| 2110 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2111 | SDLoc DL(N); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 2112 | |
| 2113 | switch (N->getOpcode()) { |
Matt Arsenault | 22b4c25 | 2014-12-21 16:48:42 +0000 | [diff] [blame] | 2114 | default: |
| 2115 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Matt Arsenault | 6f6233d | 2015-01-06 23:00:41 +0000 | [diff] [blame] | 2116 | case ISD::SETCC: |
| 2117 | return performSetCCCombine(N, DCI); |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2118 | case ISD::FMAXNUM: // TODO: What about fmax_legacy? |
| 2119 | case ISD::FMINNUM: |
Matt Arsenault | 5881f4e | 2015-06-09 00:52:37 +0000 | [diff] [blame] | 2120 | case ISD::SMAX: |
| 2121 | case ISD::SMIN: |
| 2122 | case ISD::UMAX: |
| 2123 | case ISD::UMIN: { |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2124 | if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && |
Tom Stellard | 7c840bc | 2015-03-16 15:53:55 +0000 | [diff] [blame] | 2125 | N->getValueType(0) != MVT::f64 && |
Matt Arsenault | cc3c2b3 | 2014-11-14 20:08:52 +0000 | [diff] [blame] | 2126 | getTargetMachine().getOptLevel() > CodeGenOpt::None) |
| 2127 | return performMin3Max3Combine(N, DCI); |
| 2128 | break; |
| 2129 | } |
Matt Arsenault | 364a674 | 2014-06-11 17:50:44 +0000 | [diff] [blame] | 2130 | |
| 2131 | case AMDGPUISD::CVT_F32_UBYTE0: |
| 2132 | case AMDGPUISD::CVT_F32_UBYTE1: |
| 2133 | case AMDGPUISD::CVT_F32_UBYTE2: |
| 2134 | case AMDGPUISD::CVT_F32_UBYTE3: { |
| 2135 | unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; |
| 2136 | |
| 2137 | SDValue Src = N->getOperand(0); |
| 2138 | APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); |
| 2139 | |
| 2140 | APInt KnownZero, KnownOne; |
| 2141 | TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), |
| 2142 | !DCI.isBeforeLegalizeOps()); |
| 2143 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 2144 | if (TLO.ShrinkDemandedConstant(Src, Demanded) || |
| 2145 | TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { |
| 2146 | DCI.CommitTargetLoweringOpt(TLO); |
| 2147 | } |
| 2148 | |
| 2149 | break; |
| 2150 | } |
| 2151 | |
| 2152 | case ISD::UINT_TO_FP: { |
| 2153 | return performUCharToFloatCombine(N, DCI); |
Matt Arsenault | de5fbe9 | 2016-01-11 17:02:00 +0000 | [diff] [blame] | 2154 | } |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 2155 | case ISD::FADD: { |
| 2156 | if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) |
| 2157 | break; |
| 2158 | |
| 2159 | EVT VT = N->getValueType(0); |
| 2160 | if (VT != MVT::f32) |
| 2161 | break; |
| 2162 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2163 | // Only do this if we are not trying to support denormals. v_mad_f32 does |
| 2164 | // not support denormals ever. |
| 2165 | if (Subtarget->hasFP32Denormals()) |
| 2166 | break; |
| 2167 | |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 2168 | SDValue LHS = N->getOperand(0); |
| 2169 | SDValue RHS = N->getOperand(1); |
| 2170 | |
| 2171 | // These should really be instruction patterns, but writing patterns with |
| 2172 | // source modiifiers is a pain. |
| 2173 | |
| 2174 | // fadd (fadd (a, a), b) -> mad 2.0, a, b |
| 2175 | if (LHS.getOpcode() == ISD::FADD) { |
| 2176 | SDValue A = LHS.getOperand(0); |
| 2177 | if (A == LHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2178 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2179 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | // fadd (b, fadd (a, a)) -> mad 2.0, a, b |
| 2184 | if (RHS.getOpcode() == ISD::FADD) { |
| 2185 | SDValue A = RHS.getOperand(0); |
| 2186 | if (A == RHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2187 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2188 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2192 | return SDValue(); |
Matt Arsenault | 02cb0ff | 2014-09-29 14:59:34 +0000 | [diff] [blame] | 2193 | } |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 2194 | case ISD::FSUB: { |
| 2195 | if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) |
| 2196 | break; |
| 2197 | |
| 2198 | EVT VT = N->getValueType(0); |
| 2199 | |
| 2200 | // Try to get the fneg to fold into the source modifier. This undoes generic |
| 2201 | // DAG combines and folds them into the mad. |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2202 | // |
| 2203 | // Only do this if we are not trying to support denormals. v_mad_f32 does |
| 2204 | // not support denormals ever. |
| 2205 | if (VT == MVT::f32 && |
| 2206 | !Subtarget->hasFP32Denormals()) { |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 2207 | SDValue LHS = N->getOperand(0); |
| 2208 | SDValue RHS = N->getOperand(1); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 2209 | if (LHS.getOpcode() == ISD::FADD) { |
| 2210 | // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) |
| 2211 | |
| 2212 | SDValue A = LHS.getOperand(0); |
| 2213 | if (A == LHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2214 | const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 2215 | SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS); |
| 2216 | |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2217 | return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (RHS.getOpcode() == ISD::FADD) { |
| 2222 | // (fsub c, (fadd a, a)) -> mad -2.0, a, c |
| 2223 | |
| 2224 | SDValue A = RHS.getOperand(0); |
| 2225 | if (A == RHS.getOperand(1)) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2226 | const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32); |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2227 | return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS); |
Matt Arsenault | 3d4233f | 2014-09-29 14:59:38 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
Matt Arsenault | 8d63003 | 2015-02-20 22:10:41 +0000 | [diff] [blame] | 2230 | |
| 2231 | return SDValue(); |
Matt Arsenault | 8675db1 | 2014-08-29 16:01:14 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | break; |
| 2235 | } |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 2236 | case ISD::LOAD: |
| 2237 | case ISD::STORE: |
| 2238 | case ISD::ATOMIC_LOAD: |
| 2239 | case ISD::ATOMIC_STORE: |
| 2240 | case ISD::ATOMIC_CMP_SWAP: |
| 2241 | case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: |
| 2242 | case ISD::ATOMIC_SWAP: |
| 2243 | case ISD::ATOMIC_LOAD_ADD: |
| 2244 | case ISD::ATOMIC_LOAD_SUB: |
| 2245 | case ISD::ATOMIC_LOAD_AND: |
| 2246 | case ISD::ATOMIC_LOAD_OR: |
| 2247 | case ISD::ATOMIC_LOAD_XOR: |
| 2248 | case ISD::ATOMIC_LOAD_NAND: |
| 2249 | case ISD::ATOMIC_LOAD_MIN: |
| 2250 | case ISD::ATOMIC_LOAD_MAX: |
| 2251 | case ISD::ATOMIC_LOAD_UMIN: |
| 2252 | case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics. |
| 2253 | if (DCI.isBeforeLegalize()) |
| 2254 | break; |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 2255 | |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 2256 | MemSDNode *MemNode = cast<MemSDNode>(N); |
| 2257 | SDValue Ptr = MemNode->getBasePtr(); |
| 2258 | |
| 2259 | // TODO: We could also do this for multiplies. |
| 2260 | unsigned AS = MemNode->getAddressSpace(); |
| 2261 | if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { |
| 2262 | SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); |
| 2263 | if (NewPtr) { |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 2264 | SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end()); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 2265 | |
| 2266 | NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; |
| 2267 | return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0); |
| 2268 | } |
| 2269 | } |
| 2270 | break; |
| 2271 | } |
Matt Arsenault | d0101a2 | 2015-01-06 23:00:46 +0000 | [diff] [blame] | 2272 | case ISD::AND: |
| 2273 | return performAndCombine(N, DCI); |
Matt Arsenault | f229033 | 2015-01-06 23:00:39 +0000 | [diff] [blame] | 2274 | case ISD::OR: |
| 2275 | return performOrCombine(N, DCI); |
| 2276 | case AMDGPUISD::FP_CLASS: |
| 2277 | return performClassCombine(N, DCI); |
Matt Arsenault | b2baffa | 2014-08-15 17:49:05 +0000 | [diff] [blame] | 2278 | } |
Matt Arsenault | 5565f65e | 2014-05-22 18:09:07 +0000 | [diff] [blame] | 2279 | return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 2280 | } |
Christian Konig | d910b7d | 2013-02-26 17:52:16 +0000 | [diff] [blame] | 2281 | |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 2282 | /// \brief Analyze the possible immediate value Op |
| 2283 | /// |
| 2284 | /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate |
| 2285 | /// and the immediate value if it's a literal immediate |
| 2286 | int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const { |
| 2287 | |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 2288 | const SIInstrInfo *TII = |
| 2289 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 2290 | |
Tom Stellard | edbf1eb | 2013-04-05 23:31:20 +0000 | [diff] [blame] | 2291 | if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) { |
Matt Arsenault | 303011a | 2014-12-17 21:04:08 +0000 | [diff] [blame] | 2292 | if (TII->isInlineConstant(Node->getAPIntValue())) |
| 2293 | return 0; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 2294 | |
Matt Arsenault | 11a4d67 | 2015-02-13 19:05:03 +0000 | [diff] [blame] | 2295 | uint64_t Val = Node->getZExtValue(); |
| 2296 | return isUInt<32>(Val) ? Val : -1; |
Matt Arsenault | 303011a | 2014-12-17 21:04:08 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) { |
| 2300 | if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt())) |
| 2301 | return 0; |
| 2302 | |
| 2303 | if (Node->getValueType(0) == MVT::f32) |
| 2304 | return FloatToBits(Node->getValueAPF().convertToFloat()); |
| 2305 | |
| 2306 | return -1; |
| 2307 | } |
| 2308 | |
| 2309 | return -1; |
Christian Konig | f82901a | 2013-02-26 17:52:23 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2312 | /// \brief Helper function for adjustWritemask |
Benjamin Kramer | 635e368 | 2013-05-23 15:43:05 +0000 | [diff] [blame] | 2313 | static unsigned SubIdx2Lane(unsigned Idx) { |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2314 | switch (Idx) { |
| 2315 | default: return 0; |
| 2316 | case AMDGPU::sub0: return 0; |
| 2317 | case AMDGPU::sub1: return 1; |
| 2318 | case AMDGPU::sub2: return 2; |
| 2319 | case AMDGPU::sub3: return 3; |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | /// \brief Adjust the writemask of MIMG instructions |
| 2324 | void SITargetLowering::adjustWritemask(MachineSDNode *&Node, |
| 2325 | SelectionDAG &DAG) const { |
| 2326 | SDNode *Users[4] = { }; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2327 | unsigned Lane = 0; |
| 2328 | unsigned OldDmask = Node->getConstantOperandVal(0); |
| 2329 | unsigned NewDmask = 0; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2330 | |
| 2331 | // Try to figure out the used register components |
| 2332 | for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); |
| 2333 | I != E; ++I) { |
| 2334 | |
| 2335 | // Abort if we can't understand the usage |
| 2336 | if (!I->isMachineOpcode() || |
| 2337 | I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) |
| 2338 | return; |
| 2339 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2340 | // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. |
| 2341 | // Note that subregs are packed, i.e. Lane==0 is the first bit set |
| 2342 | // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit |
| 2343 | // set, etc. |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2344 | Lane = SubIdx2Lane(I->getConstantOperandVal(1)); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2345 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2346 | // Set which texture component corresponds to the lane. |
| 2347 | unsigned Comp; |
| 2348 | for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { |
| 2349 | assert(Dmask); |
Tom Stellard | 03a5c08 | 2013-10-23 03:50:25 +0000 | [diff] [blame] | 2350 | Comp = countTrailingZeros(Dmask); |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2351 | Dmask &= ~(1 << Comp); |
| 2352 | } |
| 2353 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2354 | // Abort if we have more than one user per component |
| 2355 | if (Users[Lane]) |
| 2356 | return; |
| 2357 | |
| 2358 | Users[Lane] = *I; |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2359 | NewDmask |= 1 << Comp; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2362 | // Abort if there's no change |
| 2363 | if (NewDmask == OldDmask) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2364 | return; |
| 2365 | |
| 2366 | // Adjust the writemask in the node |
| 2367 | std::vector<SDValue> Ops; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2368 | Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 2369 | Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end()); |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 2370 | Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2371 | |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2372 | // If we only got one lane, replace it with a copy |
Tom Stellard | 54774e5 | 2013-10-23 02:53:47 +0000 | [diff] [blame] | 2373 | // (if NewDmask has only one bit set...) |
| 2374 | if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2375 | SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), |
| 2376 | MVT::i32); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2377 | SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2378 | SDLoc(), Users[Lane]->getValueType(0), |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2379 | SDValue(Node, 0), RC); |
| 2380 | DAG.ReplaceAllUsesWith(Users[Lane], Copy); |
| 2381 | return; |
| 2382 | } |
| 2383 | |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2384 | // Update the users of the node with the new indices |
| 2385 | for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { |
| 2386 | |
| 2387 | SDNode *User = Users[i]; |
| 2388 | if (!User) |
| 2389 | continue; |
| 2390 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2391 | SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2392 | DAG.UpdateNodeOperands(User, User->getOperand(0), Op); |
| 2393 | |
| 2394 | switch (Idx) { |
| 2395 | default: break; |
| 2396 | case AMDGPU::sub0: Idx = AMDGPU::sub1; break; |
| 2397 | case AMDGPU::sub1: Idx = AMDGPU::sub2; break; |
| 2398 | case AMDGPU::sub2: Idx = AMDGPU::sub3; break; |
| 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | |
Tom Stellard | c98ee20 | 2015-07-16 19:40:07 +0000 | [diff] [blame] | 2403 | static bool isFrameIndexOp(SDValue Op) { |
| 2404 | if (Op.getOpcode() == ISD::AssertZext) |
| 2405 | Op = Op.getOperand(0); |
| 2406 | |
| 2407 | return isa<FrameIndexSDNode>(Op); |
| 2408 | } |
| 2409 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 2410 | /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) |
| 2411 | /// with frame index operands. |
| 2412 | /// LLVM assumes that inputs are to these instructions are registers. |
| 2413 | void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, |
| 2414 | SelectionDAG &DAG) const { |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 2415 | |
| 2416 | SmallVector<SDValue, 8> Ops; |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 2417 | for (unsigned i = 0; i < Node->getNumOperands(); ++i) { |
Tom Stellard | c98ee20 | 2015-07-16 19:40:07 +0000 | [diff] [blame] | 2418 | if (!isFrameIndexOp(Node->getOperand(i))) { |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 2419 | Ops.push_back(Node->getOperand(i)); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 2420 | continue; |
| 2421 | } |
| 2422 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 2423 | SDLoc DL(Node); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 2424 | Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 2425 | Node->getOperand(i).getValueType(), |
| 2426 | Node->getOperand(i)), 0)); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Tom Stellard | 3457a84 | 2014-10-09 19:06:00 +0000 | [diff] [blame] | 2429 | DAG.UpdateNodeOperands(Node, Ops); |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Matt Arsenault | 08d8494 | 2014-06-03 23:06:13 +0000 | [diff] [blame] | 2432 | /// \brief Fold the instructions after selecting them. |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2433 | SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, |
| 2434 | SelectionDAG &DAG) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 2435 | const SIInstrInfo *TII = |
| 2436 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2437 | |
Tom Stellard | 16a9a20 | 2013-08-14 23:24:17 +0000 | [diff] [blame] | 2438 | if (TII->isMIMG(Node->getMachineOpcode())) |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2439 | adjustWritemask(Node, DAG); |
| 2440 | |
Matt Arsenault | 7d858d8 | 2014-11-02 23:46:54 +0000 | [diff] [blame] | 2441 | if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG || |
| 2442 | Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) { |
Tom Stellard | 8dd392e | 2014-10-09 18:09:15 +0000 | [diff] [blame] | 2443 | legalizeTargetIndependentNode(Node, DAG); |
| 2444 | return Node; |
| 2445 | } |
Tom Stellard | 654d669 | 2015-01-08 15:08:17 +0000 | [diff] [blame] | 2446 | return Node; |
Christian Konig | 8e06e2a | 2013-04-10 08:39:08 +0000 | [diff] [blame] | 2447 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2448 | |
| 2449 | /// \brief Assign the register class depending on the number of |
| 2450 | /// bits set in the writemask |
| 2451 | void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI, |
| 2452 | SDNode *Node) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 2453 | const SIInstrInfo *TII = |
| 2454 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 2455 | |
Tom Stellard | a99ada5 | 2014-11-21 22:31:44 +0000 | [diff] [blame] | 2456 | MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); |
Matt Arsenault | 6005fcb | 2015-10-21 21:51:02 +0000 | [diff] [blame] | 2457 | |
| 2458 | if (TII->isVOP3(MI->getOpcode())) { |
| 2459 | // Make sure constant bus requirements are respected. |
| 2460 | TII->legalizeOperandsVOP3(MRI, MI); |
| 2461 | return; |
| 2462 | } |
Matt Arsenault | cb0ac3d | 2014-09-26 17:54:59 +0000 | [diff] [blame] | 2463 | |
Matt Arsenault | 3add643 | 2015-10-20 04:35:43 +0000 | [diff] [blame] | 2464 | if (TII->isMIMG(*MI)) { |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 2465 | unsigned VReg = MI->getOperand(0).getReg(); |
| 2466 | unsigned Writemask = MI->getOperand(1).getImm(); |
| 2467 | unsigned BitsSet = 0; |
| 2468 | for (unsigned i = 0; i < 4; ++i) |
| 2469 | BitsSet += Writemask & (1 << i) ? 1 : 0; |
| 2470 | |
| 2471 | const TargetRegisterClass *RC; |
| 2472 | switch (BitsSet) { |
| 2473 | default: return; |
Tom Stellard | 45c0b3a | 2015-01-07 20:59:25 +0000 | [diff] [blame] | 2474 | case 1: RC = &AMDGPU::VGPR_32RegClass; break; |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 2475 | case 2: RC = &AMDGPU::VReg_64RegClass; break; |
| 2476 | case 3: RC = &AMDGPU::VReg_96RegClass; break; |
| 2477 | } |
| 2478 | |
| 2479 | unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet); |
| 2480 | MI->setDesc(TII->get(NewOpcode)); |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 2481 | MRI.setRegClass(VReg, RC); |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2482 | return; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Matt Arsenault | 7ac9c4a | 2014-09-08 15:07:31 +0000 | [diff] [blame] | 2485 | // Replace unused atomics with the no return version. |
| 2486 | int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode()); |
| 2487 | if (NoRetAtomicOp != -1) { |
| 2488 | if (!Node->hasAnyUseOfValue(0)) { |
| 2489 | MI->setDesc(TII->get(NoRetAtomicOp)); |
| 2490 | MI->RemoveOperand(0); |
| 2491 | } |
| 2492 | |
| 2493 | return; |
| 2494 | } |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 2495 | } |
Tom Stellard | 0518ff8 | 2013-06-03 17:39:58 +0000 | [diff] [blame] | 2496 | |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2497 | static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2498 | SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2499 | return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); |
| 2500 | } |
| 2501 | |
| 2502 | MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, |
| 2503 | SDLoc DL, |
| 2504 | SDValue Ptr) const { |
Eric Christopher | 7792e32 | 2015-01-30 23:24:40 +0000 | [diff] [blame] | 2505 | const SIInstrInfo *TII = |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 2506 | static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2507 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 2508 | // Build the half of the subregister with the constants before building the |
| 2509 | // full 128-bit register. If we are building multiple resource descriptors, |
| 2510 | // this will allow CSEing of the 2-component register. |
| 2511 | const SDValue Ops0[] = { |
| 2512 | DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), |
| 2513 | buildSMovImm32(DAG, DL, 0), |
| 2514 | DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
| 2515 | buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), |
| 2516 | DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) |
| 2517 | }; |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2518 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 2519 | SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, |
| 2520 | MVT::v2i32, Ops0), 0); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2521 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 2522 | // Combine the constants and the pointer. |
| 2523 | const SDValue Ops1[] = { |
| 2524 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), |
| 2525 | Ptr, |
| 2526 | DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), |
| 2527 | SubRegHi, |
| 2528 | DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) |
| 2529 | }; |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2530 | |
Matt Arsenault | 2d6fdb8 | 2015-09-25 17:08:42 +0000 | [diff] [blame] | 2531 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); |
Matt Arsenault | 485defe | 2014-11-05 19:01:17 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2534 | /// \brief Return a resource descriptor with the 'Add TID' bit enabled |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 2535 | /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] |
| 2536 | /// of the resource descriptor) to create an offset, which is added to |
| 2537 | /// the resource pointer. |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2538 | MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, |
| 2539 | SDLoc DL, |
| 2540 | SDValue Ptr, |
| 2541 | uint32_t RsrcDword1, |
| 2542 | uint64_t RsrcDword2And3) const { |
| 2543 | SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); |
| 2544 | SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); |
| 2545 | if (RsrcDword1) { |
| 2546 | PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2547 | DAG.getConstant(RsrcDword1, DL, MVT::i32)), |
| 2548 | 0); |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | SDValue DataLo = buildSMovImm32(DAG, DL, |
| 2552 | RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); |
| 2553 | SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); |
| 2554 | |
| 2555 | const SDValue Ops[] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2556 | DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2557 | PtrLo, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2558 | DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2559 | PtrHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2560 | DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2561 | DataLo, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2562 | DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2563 | DataHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2564 | DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) |
Matt Arsenault | f3cd451 | 2014-11-05 19:01:19 +0000 | [diff] [blame] | 2565 | }; |
| 2566 | |
| 2567 | return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); |
| 2568 | } |
| 2569 | |
Tom Stellard | 94593ee | 2013-06-03 17:40:18 +0000 | [diff] [blame] | 2570 | SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, |
| 2571 | const TargetRegisterClass *RC, |
| 2572 | unsigned Reg, EVT VT) const { |
| 2573 | SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); |
| 2574 | |
| 2575 | return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), |
| 2576 | cast<RegisterSDNode>(VReg)->getReg(), VT); |
| 2577 | } |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 2578 | |
| 2579 | //===----------------------------------------------------------------------===// |
| 2580 | // SI Inline Assembly Support |
| 2581 | //===----------------------------------------------------------------------===// |
| 2582 | |
| 2583 | std::pair<unsigned, const TargetRegisterClass *> |
| 2584 | SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 2585 | StringRef Constraint, |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 2586 | MVT VT) const { |
Tom Stellard | b3c3bda | 2015-12-10 02:12:53 +0000 | [diff] [blame] | 2587 | |
| 2588 | if (Constraint.size() == 1) { |
| 2589 | switch (Constraint[0]) { |
| 2590 | case 's': |
| 2591 | case 'r': |
| 2592 | switch (VT.getSizeInBits()) { |
| 2593 | default: |
| 2594 | return std::make_pair(0U, nullptr); |
| 2595 | case 32: |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 2596 | return std::make_pair(0U, &AMDGPU::SGPR_32RegClass); |
Tom Stellard | b3c3bda | 2015-12-10 02:12:53 +0000 | [diff] [blame] | 2597 | case 64: |
| 2598 | return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); |
| 2599 | case 128: |
| 2600 | return std::make_pair(0U, &AMDGPU::SReg_128RegClass); |
| 2601 | case 256: |
| 2602 | return std::make_pair(0U, &AMDGPU::SReg_256RegClass); |
| 2603 | } |
| 2604 | |
| 2605 | case 'v': |
| 2606 | switch (VT.getSizeInBits()) { |
| 2607 | default: |
| 2608 | return std::make_pair(0U, nullptr); |
| 2609 | case 32: |
| 2610 | return std::make_pair(0U, &AMDGPU::VGPR_32RegClass); |
| 2611 | case 64: |
| 2612 | return std::make_pair(0U, &AMDGPU::VReg_64RegClass); |
| 2613 | case 96: |
| 2614 | return std::make_pair(0U, &AMDGPU::VReg_96RegClass); |
| 2615 | case 128: |
| 2616 | return std::make_pair(0U, &AMDGPU::VReg_128RegClass); |
| 2617 | case 256: |
| 2618 | return std::make_pair(0U, &AMDGPU::VReg_256RegClass); |
| 2619 | case 512: |
| 2620 | return std::make_pair(0U, &AMDGPU::VReg_512RegClass); |
| 2621 | } |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | if (Constraint.size() > 1) { |
| 2626 | const TargetRegisterClass *RC = nullptr; |
| 2627 | if (Constraint[1] == 'v') { |
| 2628 | RC = &AMDGPU::VGPR_32RegClass; |
| 2629 | } else if (Constraint[1] == 's') { |
| 2630 | RC = &AMDGPU::SGPR_32RegClass; |
| 2631 | } |
| 2632 | |
| 2633 | if (RC) { |
Matt Arsenault | 0b554ed | 2015-06-23 02:05:55 +0000 | [diff] [blame] | 2634 | uint32_t Idx; |
| 2635 | bool Failed = Constraint.substr(2).getAsInteger(10, Idx); |
| 2636 | if (!Failed && Idx < RC->getNumRegs()) |
Tom Stellard | d7e6f13 | 2015-04-08 01:09:26 +0000 | [diff] [blame] | 2637 | return std::make_pair(RC->getRegister(Idx), RC); |
| 2638 | } |
| 2639 | } |
| 2640 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
| 2641 | } |
Tom Stellard | b3c3bda | 2015-12-10 02:12:53 +0000 | [diff] [blame] | 2642 | |
| 2643 | SITargetLowering::ConstraintType |
| 2644 | SITargetLowering::getConstraintType(StringRef Constraint) const { |
| 2645 | if (Constraint.size() == 1) { |
| 2646 | switch (Constraint[0]) { |
| 2647 | default: break; |
| 2648 | case 's': |
| 2649 | case 'v': |
| 2650 | return C_RegisterClass; |
| 2651 | } |
| 2652 | } |
| 2653 | return TargetLowering::getConstraintType(Constraint); |
| 2654 | } |