Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 1 | //===- AArch64LegalizerInfo.cpp ----------------------------------*- C++ -*-==// |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 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 | /// \file |
| 10 | /// This file implements the targeting of the Machinelegalizer class for |
| 11 | /// AArch64. |
| 12 | /// \todo This should be generated by TableGen. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 15 | #include "AArch64LegalizerInfo.h" |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 16 | #include "AArch64Subtarget.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstr.h" |
| 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetOpcodes.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/ValueTypes.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Type.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 26 | using namespace LegalizeActions; |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 27 | using namespace LegalityPredicates; |
Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 28 | |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 29 | AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST) { |
Ahmed Bougacha | ad30db3 | 2016-08-02 15:10:28 +0000 | [diff] [blame] | 30 | using namespace TargetOpcode; |
Tim Northover | 5ae8350 | 2016-09-15 09:20:34 +0000 | [diff] [blame] | 31 | const LLT p0 = LLT::pointer(0, 64); |
Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 32 | const LLT s1 = LLT::scalar(1); |
Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 33 | const LLT s8 = LLT::scalar(8); |
| 34 | const LLT s16 = LLT::scalar(16); |
Ahmed Bougacha | ad30db3 | 2016-08-02 15:10:28 +0000 | [diff] [blame] | 35 | const LLT s32 = LLT::scalar(32); |
| 36 | const LLT s64 = LLT::scalar(64); |
Quentin Colombet | 7c114d3 | 2017-10-16 22:28:27 +0000 | [diff] [blame] | 37 | const LLT s128 = LLT::scalar(128); |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 38 | const LLT s256 = LLT::scalar(256); |
| 39 | const LLT s512 = LLT::scalar(512); |
| 40 | const LLT v16s8 = LLT::vector(16, 8); |
| 41 | const LLT v8s8 = LLT::vector(8, 8); |
| 42 | const LLT v4s8 = LLT::vector(4, 8); |
| 43 | const LLT v8s16 = LLT::vector(8, 16); |
| 44 | const LLT v4s16 = LLT::vector(4, 16); |
| 45 | const LLT v2s16 = LLT::vector(2, 16); |
Ahmed Bougacha | ad30db3 | 2016-08-02 15:10:28 +0000 | [diff] [blame] | 46 | const LLT v2s32 = LLT::vector(2, 32); |
| 47 | const LLT v4s32 = LLT::vector(4, 32); |
| 48 | const LLT v2s64 = LLT::vector(2, 64); |
| 49 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 50 | getActionDefinitionsBuilder(G_IMPLICIT_DEF) |
| 51 | .legalFor({p0, s1, s8, s16, s32, s64}) |
| 52 | .clampScalar(0, s1, s64) |
| 53 | .widenScalarToNextPow2(0, 8); |
Tim Northover | ff5e7e1 | 2017-06-30 20:27:36 +0000 | [diff] [blame] | 54 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 55 | getActionDefinitionsBuilder(G_PHI) |
| 56 | .legalFor({p0, s16, s32, s64}) |
| 57 | .clampScalar(0, s16, s64) |
| 58 | .widenScalarToNextPow2(0); |
Aditya Nandakumar | efd8a84 | 2017-08-23 20:45:48 +0000 | [diff] [blame] | 59 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 60 | getActionDefinitionsBuilder(G_BSWAP) |
| 61 | .legalFor({s32, s64}) |
| 62 | .clampScalar(0, s16, s64) |
| 63 | .widenScalarToNextPow2(0); |
Aditya Nandakumar | 892979e | 2017-08-25 04:57:27 +0000 | [diff] [blame] | 64 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 65 | getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SHL}) |
| 66 | .legalFor({s32, s64, v2s32, v4s32, v2s64}) |
| 67 | .clampScalar(0, s32, s64) |
| 68 | .widenScalarToNextPow2(0) |
| 69 | .clampNumElements(0, v2s32, v4s32) |
| 70 | .clampNumElements(0, v2s64, v2s64) |
| 71 | .moreElementsToNextPow2(0); |
Daniel Sanders | 83e23d1 | 2017-09-19 14:25:15 +0000 | [diff] [blame] | 72 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 73 | getActionDefinitionsBuilder(G_GEP) |
| 74 | .legalFor({{p0, s64}}) |
| 75 | .clampScalar(1, s64, s64); |
Ahmed Bougacha | cfb384d | 2017-01-23 21:10:05 +0000 | [diff] [blame] | 76 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 77 | getActionDefinitionsBuilder(G_PTR_MASK).legalFor({p0}); |
Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 78 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 79 | getActionDefinitionsBuilder({G_LSHR, G_ASHR, G_SDIV, G_UDIV}) |
| 80 | .legalFor({s32, s64}) |
| 81 | .clampScalar(0, s32, s64) |
| 82 | .widenScalarToNextPow2(0); |
Tim Northover | 7a753d9 | 2016-08-26 17:46:06 +0000 | [diff] [blame] | 83 | |
Quentin Colombet | 24203cf | 2017-01-27 01:13:30 +0000 | [diff] [blame] | 84 | for (unsigned BinOp : {G_SREM, G_UREM}) |
Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 85 | for (auto Ty : { s1, s8, s16, s32, s64 }) |
Quentin Colombet | e15e460 | 2017-01-27 01:13:25 +0000 | [diff] [blame] | 86 | setAction({BinOp, Ty}, Lower); |
Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 87 | |
Kristof Beyls | af9814a | 2017-11-07 10:34:34 +0000 | [diff] [blame] | 88 | for (unsigned Op : {G_SMULO, G_UMULO}) { |
| 89 | setAction({Op, 0, s64}, Lower); |
| 90 | setAction({Op, 1, s1}, Legal); |
| 91 | } |
Tim Northover | 0a9b279 | 2017-02-08 21:22:15 +0000 | [diff] [blame] | 92 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 93 | getActionDefinitionsBuilder({G_SMULH, G_UMULH}).legalFor({s32, s64}); |
Tim Northover | 438c77c | 2016-08-25 17:37:32 +0000 | [diff] [blame] | 94 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 95 | getActionDefinitionsBuilder({G_UADDE, G_USUBE, G_SADDO, G_SSUBO}) |
| 96 | .legalFor({{s32, s1}, {s64, s1}}); |
Tim Northover | d8a6d7c | 2016-08-25 17:37:41 +0000 | [diff] [blame] | 97 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 98 | getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMA, G_FMUL, G_FDIV}) |
| 99 | .legalFor({s32, s64}); |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 100 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 101 | getActionDefinitionsBuilder({G_FREM, G_FPOW}).libcallFor({s32, s64}); |
Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 102 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 103 | getActionDefinitionsBuilder(G_INSERT) |
| 104 | .unsupportedIf([=](const LegalityQuery &Query) { |
| 105 | return Query.Types[0].getSizeInBits() <= Query.Types[1].getSizeInBits(); |
| 106 | }) |
| 107 | .legalIf([=](const LegalityQuery &Query) { |
| 108 | const LLT &Ty0 = Query.Types[0]; |
| 109 | const LLT &Ty1 = Query.Types[1]; |
| 110 | if (Ty0 != s32 && Ty0 != s64 && Ty0 != p0) |
| 111 | return false; |
| 112 | return isPowerOf2_32(Ty1.getSizeInBits()) && |
| 113 | (Ty1.getSizeInBits() == 1 || Ty1.getSizeInBits() >= 8); |
| 114 | }) |
| 115 | .clampScalar(0, s32, s64) |
| 116 | .widenScalarToNextPow2(0) |
| 117 | .maxScalarIf(typeInSet(0, {s32}), 1, s16) |
| 118 | .maxScalarIf(typeInSet(0, {s64}), 1, s32) |
| 119 | .widenScalarToNextPow2(1); |
Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 120 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 121 | getActionDefinitionsBuilder(G_EXTRACT) |
| 122 | .unsupportedIf([=](const LegalityQuery &Query) { |
| 123 | return Query.Types[0].getSizeInBits() >= Query.Types[1].getSizeInBits(); |
| 124 | }) |
| 125 | .legalIf([=](const LegalityQuery &Query) { |
| 126 | const LLT &Ty0 = Query.Types[0]; |
| 127 | const LLT &Ty1 = Query.Types[1]; |
| 128 | if (Ty1 != s32 && Ty1 != s64) |
| 129 | return false; |
| 130 | if (Ty1 == p0) |
| 131 | return true; |
| 132 | return isPowerOf2_32(Ty0.getSizeInBits()) && |
| 133 | (Ty0.getSizeInBits() == 1 || Ty0.getSizeInBits() >= 8); |
| 134 | }) |
| 135 | .clampScalar(1, s32, s64) |
| 136 | .widenScalarToNextPow2(1) |
| 137 | .maxScalarIf(typeInSet(1, {s32}), 0, s16) |
| 138 | .maxScalarIf(typeInSet(1, {s64}), 0, s32) |
| 139 | .widenScalarToNextPow2(0); |
Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 140 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 141 | getActionDefinitionsBuilder({G_LOAD, G_STORE}) |
| 142 | .legalFor( |
| 143 | {{s8, p0}, {s16, p0}, {s32, p0}, {s64, p0}, {p0, p0}, {v2s32, p0}}) |
| 144 | .clampScalar(0, s8, s64) |
| 145 | .widenScalarToNextPow2(0) |
| 146 | .clampNumElements(0, v2s32, v2s32); |
Tim Northover | 3c73e36 | 2016-08-23 18:20:09 +0000 | [diff] [blame] | 147 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 148 | // Constants |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 149 | getActionDefinitionsBuilder(G_CONSTANT) |
| 150 | .legalFor({p0, s32, s64}) |
| 151 | .clampScalar(0, s32, s64) |
| 152 | .widenScalarToNextPow2(0); |
| 153 | getActionDefinitionsBuilder(G_FCONSTANT) |
| 154 | .legalFor({s32, s64}) |
| 155 | .clampScalar(0, s32, s64); |
Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 156 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 157 | getActionDefinitionsBuilder(G_ICMP) |
| 158 | .legalFor({{s32, s32}, {s32, s64}, {s32, p0}}) |
| 159 | .clampScalar(0, s32, s32) |
| 160 | .clampScalar(1, s32, s64) |
| 161 | .widenScalarToNextPow2(1); |
Tim Northover | 7a1ec01 | 2016-08-25 17:37:35 +0000 | [diff] [blame] | 162 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 163 | getActionDefinitionsBuilder(G_FCMP) |
| 164 | .legalFor({{s32, s32}, {s32, s64}}) |
| 165 | .clampScalar(0, s32, s32) |
| 166 | .clampScalar(1, s32, s64) |
| 167 | .widenScalarToNextPow2(1); |
Tim Northover | 30bd36e | 2016-08-26 17:46:19 +0000 | [diff] [blame] | 168 | |
Tim Northover | 2c4a838 | 2016-08-25 17:37:25 +0000 | [diff] [blame] | 169 | // Extensions |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 170 | getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT}) |
| 171 | .legalFor({s1, s8, s16, s32, s64}) |
| 172 | .maxScalar(0, s64) |
| 173 | .widenScalarToNextPow2(0); |
Tim Northover | 2c4a838 | 2016-08-25 17:37:25 +0000 | [diff] [blame] | 174 | |
Ahmed Bougacha | 106dd03 | 2017-09-12 21:04:11 +0000 | [diff] [blame] | 175 | // FP conversions |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 176 | getActionDefinitionsBuilder(G_FPTRUNC).legalFor( |
| 177 | {{s16, s32}, {s16, s64}, {s32, s64}}); |
| 178 | getActionDefinitionsBuilder(G_FPEXT).legalFor( |
| 179 | {{s32, s16}, {s64, s16}, {s64, s32}}); |
Tim Northover | 438c77c | 2016-08-25 17:37:32 +0000 | [diff] [blame] | 180 | |
Tim Northover | 5d0eaa4 | 2016-08-26 17:45:58 +0000 | [diff] [blame] | 181 | // Conversions |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 182 | getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI}) |
| 183 | .legalForCartesianProduct({s32, s64}) |
| 184 | .clampScalar(0, s32, s64) |
| 185 | .widenScalarToNextPow2(0) |
| 186 | .clampScalar(1, s32, s64) |
| 187 | .widenScalarToNextPow2(1); |
Tim Northover | 5d0eaa4 | 2016-08-26 17:45:58 +0000 | [diff] [blame] | 188 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 189 | getActionDefinitionsBuilder({G_SITOFP, G_UITOFP}) |
| 190 | .legalForCartesianProduct({s32, s64}) |
| 191 | .clampScalar(1, s32, s64) |
| 192 | .widenScalarToNextPow2(1) |
| 193 | .clampScalar(0, s32, s64) |
| 194 | .widenScalarToNextPow2(0); |
Tim Northover | 438c77c | 2016-08-25 17:37:32 +0000 | [diff] [blame] | 195 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 196 | // Control-flow |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 197 | getActionDefinitionsBuilder(G_BRCOND).legalFor({s1, s8, s16, s32}); |
| 198 | getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0}); |
Ahmed Bougacha | ad30db3 | 2016-08-02 15:10:28 +0000 | [diff] [blame] | 199 | |
Tim Northover | 1d18a99 | 2016-08-26 17:46:03 +0000 | [diff] [blame] | 200 | // Select |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 201 | getActionDefinitionsBuilder(G_SELECT) |
| 202 | .legalFor({{s32, s1}, {s64, s1}, {p0, s1}}) |
| 203 | .clampScalar(0, s32, s64) |
| 204 | .widenScalarToNextPow2(0); |
Tim Northover | 1d18a99 | 2016-08-26 17:46:03 +0000 | [diff] [blame] | 205 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 206 | // Pointer-handling |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 207 | getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0}); |
| 208 | getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0}); |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 209 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 210 | getActionDefinitionsBuilder(G_PTRTOINT) |
| 211 | .legalForCartesianProduct({s1, s8, s16, s32, s64}, {p0}) |
| 212 | .maxScalar(0, s64) |
| 213 | .widenScalarToNextPow2(0, /*Min*/ 8); |
Tim Northover | 037af52c | 2016-10-31 18:31:09 +0000 | [diff] [blame] | 214 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 215 | getActionDefinitionsBuilder(G_INTTOPTR) |
| 216 | .unsupportedIf([&](const LegalityQuery &Query) { |
| 217 | return Query.Types[0].getSizeInBits() != Query.Types[1].getSizeInBits(); |
| 218 | }) |
| 219 | .legalFor({s64, p0}); |
Tim Northover | 456a3c0 | 2016-08-23 19:30:38 +0000 | [diff] [blame] | 220 | |
Quentin Colombet | 404e435 | 2016-10-12 03:57:43 +0000 | [diff] [blame] | 221 | // Casts for 32 and 64-bit width type are just copies. |
Quentin Colombet | 7c114d3 | 2017-10-16 22:28:27 +0000 | [diff] [blame] | 222 | // Same for 128-bit width type, except they are on the FPR bank. |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 223 | getActionDefinitionsBuilder(G_BITCAST) |
| 224 | // FIXME: This is wrong since G_BITCAST is not allowed to change the |
| 225 | // number of bits but it's what the previous code described and fixing |
| 226 | // it breaks tests. |
| 227 | .legalForCartesianProduct({s1, s8, s16, s32, s64, s128, v16s8, v8s8, v4s8, |
| 228 | v8s16, v4s16, v2s16, v4s32, v2s32, v2s64}); |
Tim Northover | c1d8c2b | 2016-10-11 22:29:23 +0000 | [diff] [blame] | 229 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 230 | getActionDefinitionsBuilder(G_VASTART).legalFor({p0}); |
Tim Northover | e9600d8 | 2017-02-08 17:57:27 +0000 | [diff] [blame] | 231 | |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 232 | // va_list must be a pointer, but most sized types are pretty easy to handle |
| 233 | // as the destination. |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 234 | getActionDefinitionsBuilder(G_VAARG) |
| 235 | .customForCartesianProduct({s8, s16, s32, s64, p0}, {p0}) |
| 236 | .clampScalar(0, s8, s64) |
| 237 | .widenScalarToNextPow2(0, /*Min*/ 8); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 238 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 239 | if (ST.hasLSE()) { |
| 240 | getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG) |
| 241 | .legalForCartesianProduct({s8, s16, s32, s64}, {p0}); |
| 242 | } |
| 243 | getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 244 | |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 245 | if (ST.hasLSE()) { |
Daniel Sanders | aef1dfc | 2017-11-30 20:11:42 +0000 | [diff] [blame] | 246 | for (auto Ty : {s8, s16, s32, s64}) { |
| 247 | setAction({G_ATOMIC_CMPXCHG_WITH_SUCCESS, Ty}, Lower); |
Daniel Sanders | aef1dfc | 2017-11-30 20:11:42 +0000 | [diff] [blame] | 248 | } |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 249 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 250 | getActionDefinitionsBuilder( |
| 251 | {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, G_ATOMICRMW_AND, |
| 252 | G_ATOMICRMW_OR, G_ATOMICRMW_XOR, G_ATOMICRMW_MIN, G_ATOMICRMW_MAX, |
| 253 | G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX}) |
| 254 | .legalForCartesianProduct({s8, s16, s32, s64}, {p0}); |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Volkan Keles | a32ff00 | 2017-12-01 08:19:10 +0000 | [diff] [blame] | 257 | // Merge/Unmerge |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 258 | for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) { |
| 259 | unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1; |
| 260 | unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0; |
| 261 | |
| 262 | auto notValidElt = [](const LegalityQuery &Query, unsigned TypeIdx) { |
| 263 | const LLT &Ty = Query.Types[TypeIdx]; |
| 264 | if (Ty.isVector()) { |
| 265 | const LLT &EltTy = Ty.getElementType(); |
| 266 | if (EltTy.getSizeInBits() < 8 || EltTy.getSizeInBits() > 64) |
| 267 | return true; |
| 268 | if (!isPowerOf2_32(EltTy.getSizeInBits())) |
| 269 | return true; |
Volkan Keles | a32ff00 | 2017-12-01 08:19:10 +0000 | [diff] [blame] | 270 | } |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame^] | 271 | return false; |
| 272 | }; |
| 273 | auto scalarize = |
| 274 | [](const LegalityQuery &Query, unsigned TypeIdx) { |
| 275 | const LLT &Ty = Query.Types[TypeIdx]; |
| 276 | return std::make_pair(TypeIdx, Ty.getElementType()); |
| 277 | }; |
| 278 | |
| 279 | // FIXME: This rule is horrible, but specifies the same as what we had |
| 280 | // before with the particularly strange definitions removed (e.g. |
| 281 | // s8 = G_MERGE_VALUES s32, s32). |
| 282 | // Part of the complexity comes from these ops being extremely flexible. For |
| 283 | // example, you can build/decompose vectors with it, concatenate vectors, |
| 284 | // etc. and in addition to this you can also bitcast with it at the same |
| 285 | // time. We've been considering breaking it up into multiple ops to make it |
| 286 | // more manageable throughout the backend. |
| 287 | getActionDefinitionsBuilder(Op) |
| 288 | // Break up vectors with weird elements into scalars |
| 289 | .fewerElementsIf( |
| 290 | [=](const LegalityQuery &Query) { return notValidElt(Query, 0); }, |
| 291 | [=](const LegalityQuery &Query) { return scalarize(Query, 0); }) |
| 292 | .fewerElementsIf( |
| 293 | [=](const LegalityQuery &Query) { return notValidElt(Query, 1); }, |
| 294 | [=](const LegalityQuery &Query) { return scalarize(Query, 1); }) |
| 295 | // Clamp the big scalar to s8-s512 and make it either a power of 2, 192, |
| 296 | // or 384. |
| 297 | .clampScalar(BigTyIdx, s8, s512) |
| 298 | .widenScalarIf( |
| 299 | [=](const LegalityQuery &Query) { |
| 300 | const LLT &Ty = Query.Types[BigTyIdx]; |
| 301 | return !isPowerOf2_32(Ty.getSizeInBits()) && |
| 302 | Ty.getSizeInBits() % 64 != 0; |
| 303 | }, |
| 304 | [=](const LegalityQuery &Query) { |
| 305 | // Pick the next power of 2, or a multiple of 64 over 128. |
| 306 | // Whichever is smaller. |
| 307 | const LLT &Ty = Query.Types[BigTyIdx]; |
| 308 | unsigned NewSizeInBits = 1 |
| 309 | << Log2_32_Ceil(Ty.getSizeInBits() + 1); |
| 310 | if (NewSizeInBits >= 256) { |
| 311 | unsigned RoundedTo = alignTo<64>(Ty.getSizeInBits() + 1); |
| 312 | if (RoundedTo < NewSizeInBits) |
| 313 | NewSizeInBits = RoundedTo; |
| 314 | } |
| 315 | return std::make_pair(BigTyIdx, LLT::scalar(NewSizeInBits)); |
| 316 | }) |
| 317 | // Clamp the little scalar to s8-s256 and make it a power of 2. It's not |
| 318 | // worth considering the multiples of 64 since 2*192 and 2*384 are not |
| 319 | // valid. |
| 320 | .clampScalar(LitTyIdx, s8, s256) |
| 321 | .widenScalarToNextPow2(LitTyIdx, /*Min*/ 8) |
| 322 | // So at this point, we have s8, s16, s32, s64, s128, s192, s256, s384, |
| 323 | // s512, <X x s8>, <X x s16>, <X x s32>, or <X x s64>. |
| 324 | // At this point it's simple enough to accept the legal types. |
| 325 | .legalIf([=](const LegalityQuery &Query) { |
| 326 | const LLT &BigTy = Query.Types[BigTyIdx]; |
| 327 | const LLT &LitTy = Query.Types[LitTyIdx]; |
| 328 | if (BigTy.isVector() && BigTy.getSizeInBits() < 32) |
| 329 | return false; |
| 330 | if (LitTy.isVector() && LitTy.getSizeInBits() < 32) |
| 331 | return false; |
| 332 | return BigTy.getSizeInBits() % LitTy.getSizeInBits() == 0; |
| 333 | }) |
| 334 | // Any vectors left are the wrong size. Scalarize them. |
| 335 | .fewerElementsIf([](const LegalityQuery &Query) { return true; }, |
| 336 | [](const LegalityQuery &Query) { |
| 337 | return std::make_pair( |
| 338 | 0, Query.Types[0].getElementType()); |
| 339 | }) |
| 340 | .fewerElementsIf([](const LegalityQuery &Query) { return true; }, |
| 341 | [](const LegalityQuery &Query) { |
| 342 | return std::make_pair( |
| 343 | 1, Query.Types[1].getElementType()); |
| 344 | }); |
| 345 | } |
Volkan Keles | a32ff00 | 2017-12-01 08:19:10 +0000 | [diff] [blame] | 346 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 347 | computeTables(); |
| 348 | } |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 349 | |
| 350 | bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI, |
| 351 | MachineRegisterInfo &MRI, |
| 352 | MachineIRBuilder &MIRBuilder) const { |
| 353 | switch (MI.getOpcode()) { |
| 354 | default: |
| 355 | // No idea what to do. |
| 356 | return false; |
| 357 | case TargetOpcode::G_VAARG: |
| 358 | return legalizeVaArg(MI, MRI, MIRBuilder); |
| 359 | } |
| 360 | |
| 361 | llvm_unreachable("expected switch to return"); |
| 362 | } |
| 363 | |
| 364 | bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI, |
| 365 | MachineRegisterInfo &MRI, |
| 366 | MachineIRBuilder &MIRBuilder) const { |
| 367 | MIRBuilder.setInstr(MI); |
| 368 | MachineFunction &MF = MIRBuilder.getMF(); |
| 369 | unsigned Align = MI.getOperand(2).getImm(); |
| 370 | unsigned Dst = MI.getOperand(0).getReg(); |
| 371 | unsigned ListPtr = MI.getOperand(1).getReg(); |
| 372 | |
| 373 | LLT PtrTy = MRI.getType(ListPtr); |
| 374 | LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits()); |
| 375 | |
| 376 | const unsigned PtrSize = PtrTy.getSizeInBits() / 8; |
| 377 | unsigned List = MRI.createGenericVirtualRegister(PtrTy); |
| 378 | MIRBuilder.buildLoad( |
| 379 | List, ListPtr, |
| 380 | *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad, |
| 381 | PtrSize, /* Align = */ PtrSize)); |
| 382 | |
| 383 | unsigned DstPtr; |
| 384 | if (Align > PtrSize) { |
| 385 | // Realign the list to the actual required alignment. |
Aditya Nandakumar | 1745121 | 2017-07-06 19:40:07 +0000 | [diff] [blame] | 386 | auto AlignMinus1 = MIRBuilder.buildConstant(IntPtrTy, Align - 1); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 387 | |
| 388 | unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy); |
Aditya Nandakumar | 1745121 | 2017-07-06 19:40:07 +0000 | [diff] [blame] | 389 | MIRBuilder.buildGEP(ListTmp, List, AlignMinus1->getOperand(0).getReg()); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 390 | |
| 391 | DstPtr = MRI.createGenericVirtualRegister(PtrTy); |
| 392 | MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align)); |
| 393 | } else |
| 394 | DstPtr = List; |
| 395 | |
| 396 | uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8; |
| 397 | MIRBuilder.buildLoad( |
| 398 | Dst, DstPtr, |
| 399 | *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad, |
| 400 | ValSize, std::max(Align, PtrSize))); |
| 401 | |
| 402 | unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy); |
| 403 | MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize)); |
| 404 | |
| 405 | unsigned NewList = MRI.createGenericVirtualRegister(PtrTy); |
| 406 | MIRBuilder.buildGEP(NewList, DstPtr, SizeReg); |
| 407 | |
| 408 | MIRBuilder.buildStore( |
| 409 | NewList, ListPtr, |
| 410 | *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore, |
| 411 | PtrSize, /* Align = */ PtrSize)); |
| 412 | |
| 413 | MI.eraseFromParent(); |
| 414 | return true; |
| 415 | } |