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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// This file implements the targeting of the Machinelegalizer class for |
| 10 | /// AArch64. |
| 11 | /// \todo This should be generated by TableGen. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 14 | #include "AArch64LegalizerInfo.h" |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 15 | #include "AArch64Subtarget.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h" |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstr.h" |
| 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/TargetOpcodes.h" |
Craig Topper | 2fa1436 | 2018-03-29 17:21:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/ValueTypes.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Type.h" |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
Daniel Sanders | 9ade559 | 2018-01-29 17:37:29 +0000 | [diff] [blame] | 25 | using namespace LegalizeActions; |
Matt Arsenault | 990f507 | 2019-01-25 00:51:00 +0000 | [diff] [blame] | 26 | using namespace LegalizeMutations; |
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) |
Matt Arsenault | 3dddb16 | 2019-01-09 07:51:52 +0000 | [diff] [blame] | 51 | .legalFor({p0, s1, s8, s16, s32, s64, v2s64}) |
| 52 | .clampScalar(0, s1, s64) |
| 53 | .widenScalarToNextPow2(0, 8) |
| 54 | .fewerElementsIf( |
| 55 | [=](const LegalityQuery &Query) { |
| 56 | return Query.Types[0].isVector() && |
| 57 | (Query.Types[0].getElementType() != s64 || |
| 58 | Query.Types[0].getNumElements() != 2); |
| 59 | }, |
| 60 | [=](const LegalityQuery &Query) { |
| 61 | LLT EltTy = Query.Types[0].getElementType(); |
| 62 | if (EltTy == s64) |
| 63 | return std::make_pair(0, LLT::vector(2, 64)); |
| 64 | return std::make_pair(0, EltTy); |
| 65 | }); |
Tim Northover | ff5e7e1 | 2017-06-30 20:27:36 +0000 | [diff] [blame] | 66 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 67 | getActionDefinitionsBuilder(G_PHI) |
| 68 | .legalFor({p0, s16, s32, s64}) |
| 69 | .clampScalar(0, s16, s64) |
| 70 | .widenScalarToNextPow2(0); |
Aditya Nandakumar | efd8a84 | 2017-08-23 20:45:48 +0000 | [diff] [blame] | 71 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 72 | getActionDefinitionsBuilder(G_BSWAP) |
| 73 | .legalFor({s32, s64}) |
| 74 | .clampScalar(0, s16, s64) |
| 75 | .widenScalarToNextPow2(0); |
Aditya Nandakumar | 892979e | 2017-08-25 04:57:27 +0000 | [diff] [blame] | 76 | |
Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 77 | getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR}) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 78 | .legalFor({s32, s64, v2s32, v4s32, v2s64}) |
| 79 | .clampScalar(0, s32, s64) |
| 80 | .widenScalarToNextPow2(0) |
| 81 | .clampNumElements(0, v2s32, v4s32) |
| 82 | .clampNumElements(0, v2s64, v2s64) |
| 83 | .moreElementsToNextPow2(0); |
Daniel Sanders | 83e23d1 | 2017-09-19 14:25:15 +0000 | [diff] [blame] | 84 | |
Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 85 | getActionDefinitionsBuilder(G_SHL) |
| 86 | .legalFor({{s32, s32}, {s64, s64}, |
| 87 | {v2s32, v2s32}, {v4s32, v4s32}, {v2s64, v2s64}}) |
| 88 | .clampScalar(0, s32, s64) |
| 89 | .widenScalarToNextPow2(0) |
| 90 | .clampNumElements(0, v2s32, v4s32) |
| 91 | .clampNumElements(0, v2s64, v2s64) |
| 92 | .moreElementsToNextPow2(0) |
| 93 | .minScalarSameAs(1, 0); |
| 94 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 95 | getActionDefinitionsBuilder(G_GEP) |
| 96 | .legalFor({{p0, s64}}) |
| 97 | .clampScalar(1, s64, s64); |
Ahmed Bougacha | cfb384d | 2017-01-23 21:10:05 +0000 | [diff] [blame] | 98 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 99 | getActionDefinitionsBuilder(G_PTR_MASK).legalFor({p0}); |
Tim Northover | 9656f14 | 2016-08-04 20:54:13 +0000 | [diff] [blame] | 100 | |
Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 101 | getActionDefinitionsBuilder({G_SDIV, G_UDIV}) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 102 | .legalFor({s32, s64}) |
| 103 | .clampScalar(0, s32, s64) |
| 104 | .widenScalarToNextPow2(0); |
Tim Northover | 7a753d9 | 2016-08-26 17:46:06 +0000 | [diff] [blame] | 105 | |
Matt Arsenault | 30989e4 | 2019-01-22 21:42:11 +0000 | [diff] [blame] | 106 | getActionDefinitionsBuilder({G_LSHR, G_ASHR}) |
| 107 | .legalFor({{s32, s32}, {s64, s64}}) |
| 108 | .clampScalar(0, s32, s64) |
| 109 | .minScalarSameAs(1, 0); |
| 110 | |
Daniel Sanders | 5281b02 | 2018-04-09 21:10:09 +0000 | [diff] [blame] | 111 | getActionDefinitionsBuilder({G_SREM, G_UREM}) |
| 112 | .lowerFor({s1, s8, s16, s32, s64}); |
Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 113 | |
Daniel Sanders | 5281b02 | 2018-04-09 21:10:09 +0000 | [diff] [blame] | 114 | getActionDefinitionsBuilder({G_SMULO, G_UMULO}) |
| 115 | .lowerFor({{s64, s1}}); |
Tim Northover | 0a9b279 | 2017-02-08 21:22:15 +0000 | [diff] [blame] | 116 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 117 | getActionDefinitionsBuilder({G_SMULH, G_UMULH}).legalFor({s32, s64}); |
Tim Northover | 438c77c | 2016-08-25 17:37:32 +0000 | [diff] [blame] | 118 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 119 | getActionDefinitionsBuilder({G_UADDE, G_USUBE, G_SADDO, G_SSUBO}) |
| 120 | .legalFor({{s32, s1}, {s64, s1}}); |
Tim Northover | d8a6d7c | 2016-08-25 17:37:41 +0000 | [diff] [blame] | 121 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 122 | getActionDefinitionsBuilder({G_FADD, G_FSUB, G_FMA, G_FMUL, G_FDIV}) |
| 123 | .legalFor({s32, s64}); |
Ahmed Bougacha | 33e19fe | 2016-08-18 16:05:11 +0000 | [diff] [blame] | 124 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 125 | getActionDefinitionsBuilder({G_FREM, G_FPOW}).libcallFor({s32, s64}); |
Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 126 | |
Jessica Paquette | 3560e93 | 2018-12-19 19:01:36 +0000 | [diff] [blame] | 127 | getActionDefinitionsBuilder(G_FCEIL) |
Jessica Paquette | 245047d | 2019-01-24 22:00:41 +0000 | [diff] [blame] | 128 | // If we don't have full FP16 support, then scalarize the elements of |
| 129 | // vectors containing fp16 types. |
| 130 | .fewerElementsIf( |
| 131 | [=, &ST](const LegalityQuery &Query) { |
| 132 | const auto &Ty = Query.Types[0]; |
| 133 | return Ty.isVector() && Ty.getElementType() == s16 && |
| 134 | !ST.hasFullFP16(); |
| 135 | }, |
| 136 | [=](const LegalityQuery &Query) { return std::make_pair(0, s16); }) |
Jessica Paquette | 453ab1d | 2018-12-21 17:05:26 +0000 | [diff] [blame] | 137 | // If we don't have full FP16 support, then widen s16 to s32 if we |
| 138 | // encounter it. |
| 139 | .widenScalarIf( |
| 140 | [=, &ST](const LegalityQuery &Query) { |
| 141 | return Query.Types[0] == s16 && !ST.hasFullFP16(); |
| 142 | }, |
| 143 | [=](const LegalityQuery &Query) { return std::make_pair(0, s32); }) |
Jessica Paquette | 245047d | 2019-01-24 22:00:41 +0000 | [diff] [blame] | 144 | .legalFor({s16, s32, s64, v2s32, v4s32, v2s64, v2s16, v4s16, v8s16}); |
Jessica Paquette | 3560e93 | 2018-12-19 19:01:36 +0000 | [diff] [blame] | 145 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 146 | getActionDefinitionsBuilder(G_INSERT) |
| 147 | .unsupportedIf([=](const LegalityQuery &Query) { |
| 148 | return Query.Types[0].getSizeInBits() <= Query.Types[1].getSizeInBits(); |
| 149 | }) |
| 150 | .legalIf([=](const LegalityQuery &Query) { |
| 151 | const LLT &Ty0 = Query.Types[0]; |
| 152 | const LLT &Ty1 = Query.Types[1]; |
| 153 | if (Ty0 != s32 && Ty0 != s64 && Ty0 != p0) |
| 154 | return false; |
| 155 | return isPowerOf2_32(Ty1.getSizeInBits()) && |
| 156 | (Ty1.getSizeInBits() == 1 || Ty1.getSizeInBits() >= 8); |
| 157 | }) |
| 158 | .clampScalar(0, s32, s64) |
| 159 | .widenScalarToNextPow2(0) |
| 160 | .maxScalarIf(typeInSet(0, {s32}), 1, s16) |
| 161 | .maxScalarIf(typeInSet(0, {s64}), 1, s32) |
| 162 | .widenScalarToNextPow2(1); |
Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 163 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 164 | getActionDefinitionsBuilder(G_EXTRACT) |
| 165 | .unsupportedIf([=](const LegalityQuery &Query) { |
| 166 | return Query.Types[0].getSizeInBits() >= Query.Types[1].getSizeInBits(); |
| 167 | }) |
| 168 | .legalIf([=](const LegalityQuery &Query) { |
| 169 | const LLT &Ty0 = Query.Types[0]; |
| 170 | const LLT &Ty1 = Query.Types[1]; |
| 171 | if (Ty1 != s32 && Ty1 != s64) |
| 172 | return false; |
| 173 | if (Ty1 == p0) |
| 174 | return true; |
| 175 | return isPowerOf2_32(Ty0.getSizeInBits()) && |
| 176 | (Ty0.getSizeInBits() == 1 || Ty0.getSizeInBits() >= 8); |
| 177 | }) |
| 178 | .clampScalar(1, s32, s64) |
| 179 | .widenScalarToNextPow2(1) |
| 180 | .maxScalarIf(typeInSet(1, {s32}), 0, s16) |
| 181 | .maxScalarIf(typeInSet(1, {s64}), 0, s32) |
| 182 | .widenScalarToNextPow2(0); |
Tim Northover | c2d5e6d | 2017-06-26 20:34:13 +0000 | [diff] [blame] | 183 | |
Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 184 | getActionDefinitionsBuilder({G_SEXTLOAD, G_ZEXTLOAD}) |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 185 | .legalForTypesWithMemSize({{s32, p0, 8}, |
| 186 | {s32, p0, 16}, |
| 187 | {s32, p0, 32}, |
| 188 | {s64, p0, 64}, |
| 189 | {p0, p0, 64}, |
| 190 | {v2s32, p0, 64}}) |
| 191 | .clampScalar(0, s32, s64) |
| 192 | .widenScalarToNextPow2(0) |
| 193 | // TODO: We could support sum-of-pow2's but the lowering code doesn't know |
| 194 | // how to do that yet. |
| 195 | .unsupportedIfMemSizeNotPow2() |
| 196 | // Lower anything left over into G_*EXT and G_LOAD |
Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 197 | .lower(); |
| 198 | |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 199 | getActionDefinitionsBuilder(G_LOAD) |
Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 200 | .legalForTypesWithMemSize({{s8, p0, 8}, |
| 201 | {s16, p0, 16}, |
| 202 | {s32, p0, 32}, |
| 203 | {s64, p0, 64}, |
| 204 | {p0, p0, 64}, |
| 205 | {v2s32, p0, 64}}) |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 206 | // These extends are also legal |
| 207 | .legalForTypesWithMemSize({{s32, p0, 8}, |
| 208 | {s32, p0, 16}}) |
| 209 | .clampScalar(0, s8, s64) |
| 210 | .widenScalarToNextPow2(0) |
Daniel Sanders | 27fe8a5 | 2018-04-27 19:48:53 +0000 | [diff] [blame] | 211 | // TODO: We could support sum-of-pow2's but the lowering code doesn't know |
| 212 | // how to do that yet. |
| 213 | .unsupportedIfMemSizeNotPow2() |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 214 | // Lower any any-extending loads left into G_ANYEXT and G_LOAD |
| 215 | .lowerIf([=](const LegalityQuery &Query) { |
Volkan Keles | f87473f | 2018-10-25 17:37:07 +0000 | [diff] [blame] | 216 | return Query.Types[0].getSizeInBits() != Query.MMODescrs[0].SizeInBits; |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 217 | }) |
Volkan Keles | 0a8dc9e | 2018-11-01 19:01:53 +0000 | [diff] [blame] | 218 | .clampNumElements(0, v2s32, v2s32) |
| 219 | .clampMaxNumElements(0, s64, 1); |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 220 | |
| 221 | getActionDefinitionsBuilder(G_STORE) |
| 222 | .legalForTypesWithMemSize({{s8, p0, 8}, |
| 223 | {s16, p0, 16}, |
| 224 | {s32, p0, 32}, |
| 225 | {s64, p0, 64}, |
| 226 | {p0, p0, 64}, |
| 227 | {v2s32, p0, 64}}) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 228 | .clampScalar(0, s8, s64) |
| 229 | .widenScalarToNextPow2(0) |
Daniel Sanders | f84bc37 | 2018-05-05 20:53:24 +0000 | [diff] [blame] | 230 | // TODO: We could support sum-of-pow2's but the lowering code doesn't know |
| 231 | // how to do that yet. |
| 232 | .unsupportedIfMemSizeNotPow2() |
Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 233 | .lowerIf([=](const LegalityQuery &Query) { |
Daniel Sanders | 2de9d4a | 2018-04-30 17:20:01 +0000 | [diff] [blame] | 234 | return Query.Types[0].isScalar() && |
Volkan Keles | f87473f | 2018-10-25 17:37:07 +0000 | [diff] [blame] | 235 | Query.Types[0].getSizeInBits() != Query.MMODescrs[0].SizeInBits; |
Daniel Sanders | 5eb9f58 | 2018-04-28 18:14:50 +0000 | [diff] [blame] | 236 | }) |
Volkan Keles | 0a8dc9e | 2018-11-01 19:01:53 +0000 | [diff] [blame] | 237 | .clampNumElements(0, v2s32, v2s32) |
| 238 | .clampMaxNumElements(0, s64, 1); |
Tim Northover | 3c73e36 | 2016-08-23 18:20:09 +0000 | [diff] [blame] | 239 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 240 | // Constants |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 241 | getActionDefinitionsBuilder(G_CONSTANT) |
| 242 | .legalFor({p0, s32, s64}) |
| 243 | .clampScalar(0, s32, s64) |
| 244 | .widenScalarToNextPow2(0); |
| 245 | getActionDefinitionsBuilder(G_FCONSTANT) |
| 246 | .legalFor({s32, s64}) |
| 247 | .clampScalar(0, s32, s64); |
Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 248 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 249 | getActionDefinitionsBuilder(G_ICMP) |
| 250 | .legalFor({{s32, s32}, {s32, s64}, {s32, p0}}) |
| 251 | .clampScalar(0, s32, s32) |
| 252 | .clampScalar(1, s32, s64) |
| 253 | .widenScalarToNextPow2(1); |
Tim Northover | 7a1ec01 | 2016-08-25 17:37:35 +0000 | [diff] [blame] | 254 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 255 | getActionDefinitionsBuilder(G_FCMP) |
| 256 | .legalFor({{s32, s32}, {s32, s64}}) |
| 257 | .clampScalar(0, s32, s32) |
| 258 | .clampScalar(1, s32, s64) |
| 259 | .widenScalarToNextPow2(1); |
Tim Northover | 30bd36e | 2016-08-26 17:46:19 +0000 | [diff] [blame] | 260 | |
Tim Northover | 2c4a838 | 2016-08-25 17:37:25 +0000 | [diff] [blame] | 261 | // Extensions |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 262 | getActionDefinitionsBuilder({G_ZEXT, G_SEXT, G_ANYEXT}) |
Roman Tereshin | 5a65eb7 | 2018-05-31 01:56:05 +0000 | [diff] [blame] | 263 | .legalForCartesianProduct({s8, s16, s32, s64}, {s1, s8, s16, s32}); |
Tim Northover | 2c4a838 | 2016-08-25 17:37:25 +0000 | [diff] [blame] | 264 | |
Ahmed Bougacha | 106dd03 | 2017-09-12 21:04:11 +0000 | [diff] [blame] | 265 | // FP conversions |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 266 | getActionDefinitionsBuilder(G_FPTRUNC).legalFor( |
| 267 | {{s16, s32}, {s16, s64}, {s32, s64}}); |
| 268 | getActionDefinitionsBuilder(G_FPEXT).legalFor( |
| 269 | {{s32, s16}, {s64, s16}, {s64, s32}}); |
Tim Northover | 438c77c | 2016-08-25 17:37:32 +0000 | [diff] [blame] | 270 | |
Tim Northover | 5d0eaa4 | 2016-08-26 17:45:58 +0000 | [diff] [blame] | 271 | // Conversions |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 272 | getActionDefinitionsBuilder({G_FPTOSI, G_FPTOUI}) |
Amara Emerson | 92ffb30 | 2019-01-28 02:27:59 +0000 | [diff] [blame^] | 273 | .legalForCartesianProduct({s32, s64, v2s64, v4s32, v2s32}) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 274 | .clampScalar(0, s32, s64) |
| 275 | .widenScalarToNextPow2(0) |
| 276 | .clampScalar(1, s32, s64) |
| 277 | .widenScalarToNextPow2(1); |
Tim Northover | 5d0eaa4 | 2016-08-26 17:45:58 +0000 | [diff] [blame] | 278 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 279 | getActionDefinitionsBuilder({G_SITOFP, G_UITOFP}) |
Amara Emerson | 92ffb30 | 2019-01-28 02:27:59 +0000 | [diff] [blame^] | 280 | .legalForCartesianProduct({s32, s64, v2s64, v4s32, v2s32}) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 281 | .clampScalar(1, s32, s64) |
| 282 | .widenScalarToNextPow2(1) |
| 283 | .clampScalar(0, s32, s64) |
| 284 | .widenScalarToNextPow2(0); |
Tim Northover | 438c77c | 2016-08-25 17:37:32 +0000 | [diff] [blame] | 285 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 286 | // Control-flow |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 287 | getActionDefinitionsBuilder(G_BRCOND).legalFor({s1, s8, s16, s32}); |
| 288 | getActionDefinitionsBuilder(G_BRINDIRECT).legalFor({p0}); |
Ahmed Bougacha | ad30db3 | 2016-08-02 15:10:28 +0000 | [diff] [blame] | 289 | |
Tim Northover | 1d18a99 | 2016-08-26 17:46:03 +0000 | [diff] [blame] | 290 | // Select |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 291 | getActionDefinitionsBuilder(G_SELECT) |
| 292 | .legalFor({{s32, s1}, {s64, s1}, {p0, s1}}) |
| 293 | .clampScalar(0, s32, s64) |
| 294 | .widenScalarToNextPow2(0); |
Tim Northover | 1d18a99 | 2016-08-26 17:46:03 +0000 | [diff] [blame] | 295 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 296 | // Pointer-handling |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 297 | getActionDefinitionsBuilder(G_FRAME_INDEX).legalFor({p0}); |
| 298 | getActionDefinitionsBuilder(G_GLOBAL_VALUE).legalFor({p0}); |
Ahmed Bougacha | 0306b5e | 2016-08-16 14:02:42 +0000 | [diff] [blame] | 299 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 300 | getActionDefinitionsBuilder(G_PTRTOINT) |
| 301 | .legalForCartesianProduct({s1, s8, s16, s32, s64}, {p0}) |
| 302 | .maxScalar(0, s64) |
| 303 | .widenScalarToNextPow2(0, /*Min*/ 8); |
Tim Northover | 037af52c | 2016-10-31 18:31:09 +0000 | [diff] [blame] | 304 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 305 | getActionDefinitionsBuilder(G_INTTOPTR) |
| 306 | .unsupportedIf([&](const LegalityQuery &Query) { |
| 307 | return Query.Types[0].getSizeInBits() != Query.Types[1].getSizeInBits(); |
| 308 | }) |
Roman Tereshin | 5a65eb7 | 2018-05-31 01:56:05 +0000 | [diff] [blame] | 309 | .legalFor({{p0, s64}}); |
Tim Northover | 456a3c0 | 2016-08-23 19:30:38 +0000 | [diff] [blame] | 310 | |
Quentin Colombet | 404e435 | 2016-10-12 03:57:43 +0000 | [diff] [blame] | 311 | // Casts for 32 and 64-bit width type are just copies. |
Quentin Colombet | 7c114d3 | 2017-10-16 22:28:27 +0000 | [diff] [blame] | 312 | // 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] | 313 | getActionDefinitionsBuilder(G_BITCAST) |
| 314 | // FIXME: This is wrong since G_BITCAST is not allowed to change the |
| 315 | // number of bits but it's what the previous code described and fixing |
| 316 | // it breaks tests. |
| 317 | .legalForCartesianProduct({s1, s8, s16, s32, s64, s128, v16s8, v8s8, v4s8, |
| 318 | v8s16, v4s16, v2s16, v4s32, v2s32, v2s64}); |
Tim Northover | c1d8c2b | 2016-10-11 22:29:23 +0000 | [diff] [blame] | 319 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 320 | getActionDefinitionsBuilder(G_VASTART).legalFor({p0}); |
Tim Northover | e9600d8 | 2017-02-08 17:57:27 +0000 | [diff] [blame] | 321 | |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 322 | // va_list must be a pointer, but most sized types are pretty easy to handle |
| 323 | // as the destination. |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 324 | getActionDefinitionsBuilder(G_VAARG) |
| 325 | .customForCartesianProduct({s8, s16, s32, s64, p0}, {p0}) |
| 326 | .clampScalar(0, s8, s64) |
| 327 | .widenScalarToNextPow2(0, /*Min*/ 8); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 328 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 329 | if (ST.hasLSE()) { |
Daniel Sanders | 5281b02 | 2018-04-09 21:10:09 +0000 | [diff] [blame] | 330 | getActionDefinitionsBuilder(G_ATOMIC_CMPXCHG_WITH_SUCCESS) |
Daniel Sanders | bdeb880 | 2018-06-27 19:03:21 +0000 | [diff] [blame] | 331 | .lowerIf(all( |
| 332 | typeInSet(0, {s8, s16, s32, s64}), typeIs(1, s1), typeIs(2, p0), |
| 333 | atomicOrderingAtLeastOrStrongerThan(0, AtomicOrdering::Monotonic))); |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 334 | |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 335 | getActionDefinitionsBuilder( |
| 336 | {G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, G_ATOMICRMW_AND, |
| 337 | G_ATOMICRMW_OR, G_ATOMICRMW_XOR, G_ATOMICRMW_MIN, G_ATOMICRMW_MAX, |
Roman Tereshin | 5a65eb7 | 2018-05-31 01:56:05 +0000 | [diff] [blame] | 338 | G_ATOMICRMW_UMIN, G_ATOMICRMW_UMAX, G_ATOMIC_CMPXCHG}) |
Daniel Sanders | bdeb880 | 2018-06-27 19:03:21 +0000 | [diff] [blame] | 339 | .legalIf(all( |
| 340 | typeInSet(0, {s8, s16, s32, s64}), typeIs(1, p0), |
| 341 | atomicOrderingAtLeastOrStrongerThan(0, AtomicOrdering::Monotonic))); |
Daniel Sanders | 7fe7acc | 2017-11-28 20:21:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Amara Emerson | 0e86c07 | 2018-07-31 00:08:56 +0000 | [diff] [blame] | 344 | getActionDefinitionsBuilder(G_BLOCK_ADDR).legalFor({p0}); |
| 345 | |
Volkan Keles | a32ff00 | 2017-12-01 08:19:10 +0000 | [diff] [blame] | 346 | // Merge/Unmerge |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 347 | for (unsigned Op : {G_MERGE_VALUES, G_UNMERGE_VALUES}) { |
| 348 | unsigned BigTyIdx = Op == G_MERGE_VALUES ? 0 : 1; |
| 349 | unsigned LitTyIdx = Op == G_MERGE_VALUES ? 1 : 0; |
| 350 | |
| 351 | auto notValidElt = [](const LegalityQuery &Query, unsigned TypeIdx) { |
| 352 | const LLT &Ty = Query.Types[TypeIdx]; |
| 353 | if (Ty.isVector()) { |
| 354 | const LLT &EltTy = Ty.getElementType(); |
| 355 | if (EltTy.getSizeInBits() < 8 || EltTy.getSizeInBits() > 64) |
| 356 | return true; |
| 357 | if (!isPowerOf2_32(EltTy.getSizeInBits())) |
| 358 | return true; |
Volkan Keles | a32ff00 | 2017-12-01 08:19:10 +0000 | [diff] [blame] | 359 | } |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 360 | return false; |
| 361 | }; |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 362 | |
| 363 | // FIXME: This rule is horrible, but specifies the same as what we had |
| 364 | // before with the particularly strange definitions removed (e.g. |
| 365 | // s8 = G_MERGE_VALUES s32, s32). |
| 366 | // Part of the complexity comes from these ops being extremely flexible. For |
| 367 | // example, you can build/decompose vectors with it, concatenate vectors, |
| 368 | // etc. and in addition to this you can also bitcast with it at the same |
| 369 | // time. We've been considering breaking it up into multiple ops to make it |
| 370 | // more manageable throughout the backend. |
| 371 | getActionDefinitionsBuilder(Op) |
| 372 | // Break up vectors with weird elements into scalars |
| 373 | .fewerElementsIf( |
| 374 | [=](const LegalityQuery &Query) { return notValidElt(Query, 0); }, |
Matt Arsenault | 990f507 | 2019-01-25 00:51:00 +0000 | [diff] [blame] | 375 | scalarize(0)) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 376 | .fewerElementsIf( |
| 377 | [=](const LegalityQuery &Query) { return notValidElt(Query, 1); }, |
Matt Arsenault | 990f507 | 2019-01-25 00:51:00 +0000 | [diff] [blame] | 378 | scalarize(1)) |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 379 | // Clamp the big scalar to s8-s512 and make it either a power of 2, 192, |
| 380 | // or 384. |
| 381 | .clampScalar(BigTyIdx, s8, s512) |
| 382 | .widenScalarIf( |
| 383 | [=](const LegalityQuery &Query) { |
| 384 | const LLT &Ty = Query.Types[BigTyIdx]; |
| 385 | return !isPowerOf2_32(Ty.getSizeInBits()) && |
| 386 | Ty.getSizeInBits() % 64 != 0; |
| 387 | }, |
| 388 | [=](const LegalityQuery &Query) { |
| 389 | // Pick the next power of 2, or a multiple of 64 over 128. |
| 390 | // Whichever is smaller. |
| 391 | const LLT &Ty = Query.Types[BigTyIdx]; |
| 392 | unsigned NewSizeInBits = 1 |
| 393 | << Log2_32_Ceil(Ty.getSizeInBits() + 1); |
| 394 | if (NewSizeInBits >= 256) { |
| 395 | unsigned RoundedTo = alignTo<64>(Ty.getSizeInBits() + 1); |
| 396 | if (RoundedTo < NewSizeInBits) |
| 397 | NewSizeInBits = RoundedTo; |
| 398 | } |
| 399 | return std::make_pair(BigTyIdx, LLT::scalar(NewSizeInBits)); |
| 400 | }) |
| 401 | // Clamp the little scalar to s8-s256 and make it a power of 2. It's not |
| 402 | // worth considering the multiples of 64 since 2*192 and 2*384 are not |
| 403 | // valid. |
| 404 | .clampScalar(LitTyIdx, s8, s256) |
| 405 | .widenScalarToNextPow2(LitTyIdx, /*Min*/ 8) |
| 406 | // So at this point, we have s8, s16, s32, s64, s128, s192, s256, s384, |
| 407 | // s512, <X x s8>, <X x s16>, <X x s32>, or <X x s64>. |
| 408 | // At this point it's simple enough to accept the legal types. |
| 409 | .legalIf([=](const LegalityQuery &Query) { |
| 410 | const LLT &BigTy = Query.Types[BigTyIdx]; |
| 411 | const LLT &LitTy = Query.Types[LitTyIdx]; |
| 412 | if (BigTy.isVector() && BigTy.getSizeInBits() < 32) |
| 413 | return false; |
| 414 | if (LitTy.isVector() && LitTy.getSizeInBits() < 32) |
| 415 | return false; |
| 416 | return BigTy.getSizeInBits() % LitTy.getSizeInBits() == 0; |
| 417 | }) |
| 418 | // Any vectors left are the wrong size. Scalarize them. |
Matt Arsenault | 990f507 | 2019-01-25 00:51:00 +0000 | [diff] [blame] | 419 | .scalarize(0) |
| 420 | .scalarize(1); |
Daniel Sanders | 79cb839 | 2018-01-29 19:54:49 +0000 | [diff] [blame] | 421 | } |
Volkan Keles | a32ff00 | 2017-12-01 08:19:10 +0000 | [diff] [blame] | 422 | |
Amara Emerson | cbd86d8 | 2018-10-25 14:04:54 +0000 | [diff] [blame] | 423 | getActionDefinitionsBuilder(G_EXTRACT_VECTOR_ELT) |
| 424 | .unsupportedIf([=](const LegalityQuery &Query) { |
| 425 | const LLT &EltTy = Query.Types[1].getElementType(); |
| 426 | return Query.Types[0] != EltTy; |
| 427 | }) |
| 428 | .minScalar(2, s64) |
| 429 | .legalIf([=](const LegalityQuery &Query) { |
| 430 | const LLT &VecTy = Query.Types[1]; |
| 431 | return VecTy == v4s32 || VecTy == v2s64; |
| 432 | }); |
| 433 | |
Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 434 | getActionDefinitionsBuilder(G_BUILD_VECTOR) |
Jessica Paquette | 245047d | 2019-01-24 22:00:41 +0000 | [diff] [blame] | 435 | .legalFor({{v4s16, s16}, {v8s16, s16}, {v4s32, s32}, {v2s64, s64}}) |
Amara Emerson | 5ec1460 | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 436 | .clampNumElements(0, v4s32, v4s32) |
| 437 | .clampNumElements(0, v2s64, v2s64) |
| 438 | |
| 439 | // Deal with larger scalar types, which will be implicitly truncated. |
| 440 | .legalIf([=](const LegalityQuery &Query) { |
| 441 | return Query.Types[0].getScalarSizeInBits() < |
| 442 | Query.Types[1].getSizeInBits(); |
| 443 | }) |
| 444 | .minScalarSameAs(1, 0); |
| 445 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 446 | computeTables(); |
Roman Tereshin | 8f1753e | 2018-05-30 22:10:04 +0000 | [diff] [blame] | 447 | verify(*ST.getInstrInfo()); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 448 | } |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 449 | |
| 450 | bool AArch64LegalizerInfo::legalizeCustom(MachineInstr &MI, |
| 451 | MachineRegisterInfo &MRI, |
Aditya Nandakumar | f75d4f3 | 2018-12-05 20:14:52 +0000 | [diff] [blame] | 452 | MachineIRBuilder &MIRBuilder, |
| 453 | GISelChangeObserver &Observer) const { |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 454 | switch (MI.getOpcode()) { |
| 455 | default: |
| 456 | // No idea what to do. |
| 457 | return false; |
| 458 | case TargetOpcode::G_VAARG: |
| 459 | return legalizeVaArg(MI, MRI, MIRBuilder); |
| 460 | } |
| 461 | |
| 462 | llvm_unreachable("expected switch to return"); |
| 463 | } |
| 464 | |
| 465 | bool AArch64LegalizerInfo::legalizeVaArg(MachineInstr &MI, |
| 466 | MachineRegisterInfo &MRI, |
| 467 | MachineIRBuilder &MIRBuilder) const { |
| 468 | MIRBuilder.setInstr(MI); |
| 469 | MachineFunction &MF = MIRBuilder.getMF(); |
| 470 | unsigned Align = MI.getOperand(2).getImm(); |
| 471 | unsigned Dst = MI.getOperand(0).getReg(); |
| 472 | unsigned ListPtr = MI.getOperand(1).getReg(); |
| 473 | |
| 474 | LLT PtrTy = MRI.getType(ListPtr); |
| 475 | LLT IntPtrTy = LLT::scalar(PtrTy.getSizeInBits()); |
| 476 | |
| 477 | const unsigned PtrSize = PtrTy.getSizeInBits() / 8; |
| 478 | unsigned List = MRI.createGenericVirtualRegister(PtrTy); |
| 479 | MIRBuilder.buildLoad( |
| 480 | List, ListPtr, |
| 481 | *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad, |
| 482 | PtrSize, /* Align = */ PtrSize)); |
| 483 | |
| 484 | unsigned DstPtr; |
| 485 | if (Align > PtrSize) { |
| 486 | // Realign the list to the actual required alignment. |
Aditya Nandakumar | 1745121 | 2017-07-06 19:40:07 +0000 | [diff] [blame] | 487 | auto AlignMinus1 = MIRBuilder.buildConstant(IntPtrTy, Align - 1); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 488 | |
| 489 | unsigned ListTmp = MRI.createGenericVirtualRegister(PtrTy); |
Aditya Nandakumar | 1745121 | 2017-07-06 19:40:07 +0000 | [diff] [blame] | 490 | MIRBuilder.buildGEP(ListTmp, List, AlignMinus1->getOperand(0).getReg()); |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 491 | |
| 492 | DstPtr = MRI.createGenericVirtualRegister(PtrTy); |
| 493 | MIRBuilder.buildPtrMask(DstPtr, ListTmp, Log2_64(Align)); |
| 494 | } else |
| 495 | DstPtr = List; |
| 496 | |
| 497 | uint64_t ValSize = MRI.getType(Dst).getSizeInBits() / 8; |
| 498 | MIRBuilder.buildLoad( |
| 499 | Dst, DstPtr, |
| 500 | *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOLoad, |
| 501 | ValSize, std::max(Align, PtrSize))); |
| 502 | |
| 503 | unsigned SizeReg = MRI.createGenericVirtualRegister(IntPtrTy); |
| 504 | MIRBuilder.buildConstant(SizeReg, alignTo(ValSize, PtrSize)); |
| 505 | |
| 506 | unsigned NewList = MRI.createGenericVirtualRegister(PtrTy); |
| 507 | MIRBuilder.buildGEP(NewList, DstPtr, SizeReg); |
| 508 | |
| 509 | MIRBuilder.buildStore( |
| 510 | NewList, ListPtr, |
| 511 | *MF.getMachineMemOperand(MachinePointerInfo(), MachineMemOperand::MOStore, |
| 512 | PtrSize, /* Align = */ PtrSize)); |
| 513 | |
| 514 | MI.eraseFromParent(); |
| 515 | return true; |
| 516 | } |