Bug Summary

File:build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
Warning:line 1126, column 10
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name LegalizeIntegerTypes.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm -resource-dir /usr/lib/llvm-15/lib/clang/15.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/CodeGen/SelectionDAG -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/lib/CodeGen/SelectionDAG -I include -I /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-15/lib/clang/15.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-04-20-140412-16051-1 -x c++ /build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

1//===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements integer type expansion and promotion for LegalizeTypes.
10// Promotion is the act of changing a computation in an illegal type into a
11// computation in a larger type. For example, implementing i8 arithmetic in an
12// i32 register (often needed on powerpc).
13// Expansion is the act of changing a computation in an illegal type into a
14// computation in two identical registers of a smaller type. For example,
15// implementing i64 arithmetic in two i32 registers (often needed on 32-bit
16// targets).
17//
18//===----------------------------------------------------------------------===//
19
20#include "LegalizeTypes.h"
21#include "llvm/Analysis/TargetLibraryInfo.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/KnownBits.h"
25#include "llvm/Support/raw_ostream.h"
26#include <algorithm>
27using namespace llvm;
28
29#define DEBUG_TYPE"legalize-types" "legalize-types"
30
31//===----------------------------------------------------------------------===//
32// Integer Result Promotion
33//===----------------------------------------------------------------------===//
34
35/// PromoteIntegerResult - This method is called when a result of a node is
36/// found to be in need of promotion to a larger type. At this point, the node
37/// may also have invalid operands or may have other results that need
38/// expansion, we just know that (at least) one result needs promotion.
39void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
40 LLVM_DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Promote integer result: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
41 dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Promote integer result: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
;
42 SDValue Res = SDValue();
43
44 // See if the target wants to custom expand this node.
45 if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
46 LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Node has been custom expanded, done\n"
; } } while (false)
;
47 return;
48 }
49
50 switch (N->getOpcode()) {
51 default:
52#ifndef NDEBUG
53 dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
54 N->dump(&DAG); dbgs() << "\n";
55#endif
56 llvm_unreachable("Do not know how to promote this operator!")::llvm::llvm_unreachable_internal("Do not know how to promote this operator!"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 56
)
;
57 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
58 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
59 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
60 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
61 case ISD::BITREVERSE: Res = PromoteIntRes_BITREVERSE(N); break;
62 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
63 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
64 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
65 case ISD::CTLZ_ZERO_UNDEF:
66 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
67 case ISD::PARITY:
68 case ISD::CTPOP: Res = PromoteIntRes_CTPOP_PARITY(N); break;
69 case ISD::CTTZ_ZERO_UNDEF:
70 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
71 case ISD::EXTRACT_VECTOR_ELT:
72 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
73 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
74 case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
75 break;
76 case ISD::MGATHER: Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
77 break;
78 case ISD::SELECT:
79 case ISD::VSELECT:
80 case ISD::VP_SELECT:
81 Res = PromoteIntRes_Select(N);
82 break;
83 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
84 case ISD::STRICT_FSETCC:
85 case ISD::STRICT_FSETCCS:
86 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
87 case ISD::SMIN:
88 case ISD::SMAX: Res = PromoteIntRes_SExtIntBinOp(N); break;
89 case ISD::UMIN:
90 case ISD::UMAX: Res = PromoteIntRes_UMINUMAX(N); break;
91
92 case ISD::SHL:
93 case ISD::VP_SHL: Res = PromoteIntRes_SHL(N); break;
94 case ISD::SIGN_EXTEND_INREG:
95 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
96 case ISD::SRA:
97 case ISD::VP_ASHR: Res = PromoteIntRes_SRA(N); break;
98 case ISD::SRL:
99 case ISD::VP_LSHR: Res = PromoteIntRes_SRL(N); break;
100 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
101 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
102 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
103 case ISD::VSCALE: Res = PromoteIntRes_VSCALE(N); break;
104
105 case ISD::EXTRACT_SUBVECTOR:
106 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
107 case ISD::INSERT_SUBVECTOR:
108 Res = PromoteIntRes_INSERT_SUBVECTOR(N); break;
109 case ISD::VECTOR_REVERSE:
110 Res = PromoteIntRes_VECTOR_REVERSE(N); break;
111 case ISD::VECTOR_SHUFFLE:
112 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
113 case ISD::VECTOR_SPLICE:
114 Res = PromoteIntRes_VECTOR_SPLICE(N); break;
115 case ISD::INSERT_VECTOR_ELT:
116 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
117 case ISD::BUILD_VECTOR:
118 Res = PromoteIntRes_BUILD_VECTOR(N); break;
119 case ISD::SCALAR_TO_VECTOR:
120 Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
121 case ISD::SPLAT_VECTOR:
122 Res = PromoteIntRes_SPLAT_VECTOR(N); break;
123 case ISD::STEP_VECTOR: Res = PromoteIntRes_STEP_VECTOR(N); break;
124 case ISD::CONCAT_VECTORS:
125 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
126
127 case ISD::ANY_EXTEND_VECTOR_INREG:
128 case ISD::SIGN_EXTEND_VECTOR_INREG:
129 case ISD::ZERO_EXTEND_VECTOR_INREG:
130 Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
131
132 case ISD::SIGN_EXTEND:
133 case ISD::ZERO_EXTEND:
134 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
135
136 case ISD::STRICT_FP_TO_SINT:
137 case ISD::STRICT_FP_TO_UINT:
138 case ISD::FP_TO_SINT:
139 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
140
141 case ISD::FP_TO_SINT_SAT:
142 case ISD::FP_TO_UINT_SAT:
143 Res = PromoteIntRes_FP_TO_XINT_SAT(N); break;
144
145 case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break;
146
147 case ISD::FLT_ROUNDS_: Res = PromoteIntRes_FLT_ROUNDS(N); break;
148
149 case ISD::AND:
150 case ISD::OR:
151 case ISD::XOR:
152 case ISD::ADD:
153 case ISD::SUB:
154 case ISD::MUL:
155 case ISD::VP_AND:
156 case ISD::VP_OR:
157 case ISD::VP_XOR:
158 case ISD::VP_ADD:
159 case ISD::VP_SUB:
160 case ISD::VP_MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
161
162 case ISD::SDIV:
163 case ISD::SREM:
164 case ISD::VP_SDIV:
165 case ISD::VP_SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
166
167 case ISD::UDIV:
168 case ISD::UREM:
169 case ISD::VP_UDIV:
170 case ISD::VP_UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
171
172 case ISD::SADDO:
173 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
174 case ISD::UADDO:
175 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
176 case ISD::SMULO:
177 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
178
179 case ISD::ADDE:
180 case ISD::SUBE:
181 case ISD::ADDCARRY:
182 case ISD::SUBCARRY: Res = PromoteIntRes_ADDSUBCARRY(N, ResNo); break;
183
184 case ISD::SADDO_CARRY:
185 case ISD::SSUBO_CARRY: Res = PromoteIntRes_SADDSUBO_CARRY(N, ResNo); break;
186
187 case ISD::SADDSAT:
188 case ISD::UADDSAT:
189 case ISD::SSUBSAT:
190 case ISD::USUBSAT:
191 case ISD::SSHLSAT:
192 case ISD::USHLSAT: Res = PromoteIntRes_ADDSUBSHLSAT(N); break;
193
194 case ISD::SMULFIX:
195 case ISD::SMULFIXSAT:
196 case ISD::UMULFIX:
197 case ISD::UMULFIXSAT: Res = PromoteIntRes_MULFIX(N); break;
198
199 case ISD::SDIVFIX:
200 case ISD::SDIVFIXSAT:
201 case ISD::UDIVFIX:
202 case ISD::UDIVFIXSAT: Res = PromoteIntRes_DIVFIX(N); break;
203
204 case ISD::ABS: Res = PromoteIntRes_ABS(N); break;
205
206 case ISD::ATOMIC_LOAD:
207 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
208
209 case ISD::ATOMIC_LOAD_ADD:
210 case ISD::ATOMIC_LOAD_SUB:
211 case ISD::ATOMIC_LOAD_AND:
212 case ISD::ATOMIC_LOAD_CLR:
213 case ISD::ATOMIC_LOAD_OR:
214 case ISD::ATOMIC_LOAD_XOR:
215 case ISD::ATOMIC_LOAD_NAND:
216 case ISD::ATOMIC_LOAD_MIN:
217 case ISD::ATOMIC_LOAD_MAX:
218 case ISD::ATOMIC_LOAD_UMIN:
219 case ISD::ATOMIC_LOAD_UMAX:
220 case ISD::ATOMIC_SWAP:
221 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
222
223 case ISD::ATOMIC_CMP_SWAP:
224 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
225 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
226 break;
227
228 case ISD::VECREDUCE_ADD:
229 case ISD::VECREDUCE_MUL:
230 case ISD::VECREDUCE_AND:
231 case ISD::VECREDUCE_OR:
232 case ISD::VECREDUCE_XOR:
233 case ISD::VECREDUCE_SMAX:
234 case ISD::VECREDUCE_SMIN:
235 case ISD::VECREDUCE_UMAX:
236 case ISD::VECREDUCE_UMIN:
237 Res = PromoteIntRes_VECREDUCE(N);
238 break;
239
240 case ISD::VP_REDUCE_ADD:
241 case ISD::VP_REDUCE_MUL:
242 case ISD::VP_REDUCE_AND:
243 case ISD::VP_REDUCE_OR:
244 case ISD::VP_REDUCE_XOR:
245 case ISD::VP_REDUCE_SMAX:
246 case ISD::VP_REDUCE_SMIN:
247 case ISD::VP_REDUCE_UMAX:
248 case ISD::VP_REDUCE_UMIN:
249 Res = PromoteIntRes_VP_REDUCE(N);
250 break;
251
252 case ISD::FREEZE:
253 Res = PromoteIntRes_FREEZE(N);
254 break;
255
256 case ISD::ROTL:
257 case ISD::ROTR:
258 Res = PromoteIntRes_Rotate(N);
259 break;
260
261 case ISD::FSHL:
262 case ISD::FSHR:
263 Res = PromoteIntRes_FunnelShift(N);
264 break;
265 }
266
267 // If the result is null then the sub-method took care of registering it.
268 if (Res.getNode())
269 SetPromotedInteger(SDValue(N, ResNo), Res);
270}
271
272SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
273 unsigned ResNo) {
274 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
275 return GetPromotedInteger(Op);
276}
277
278SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
279 // Sign-extend the new bits, and continue the assertion.
280 SDValue Op = SExtPromotedInteger(N->getOperand(0));
281 return DAG.getNode(ISD::AssertSext, SDLoc(N),
282 Op.getValueType(), Op, N->getOperand(1));
283}
284
285SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
286 // Zero the new bits, and continue the assertion.
287 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
288 return DAG.getNode(ISD::AssertZext, SDLoc(N),
289 Op.getValueType(), Op, N->getOperand(1));
290}
291
292SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
293 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
294 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
295 N->getMemoryVT(), ResVT,
296 N->getChain(), N->getBasePtr(),
297 N->getMemOperand());
298 // Legalize the chain result - switch anything that used the old chain to
299 // use the new one.
300 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
301 return Res;
302}
303
304SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
305 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
306 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
307 N->getMemoryVT(),
308 N->getChain(), N->getBasePtr(),
309 Op2, N->getMemOperand());
310 // Legalize the chain result - switch anything that used the old chain to
311 // use the new one.
312 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
313 return Res;
314}
315
316SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
317 unsigned ResNo) {
318 if (ResNo == 1) {
319 assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS)(static_cast <bool> (N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS
) ? void (0) : __assert_fail ("N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 319
, __extension__ __PRETTY_FUNCTION__))
;
320 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
321 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
322
323 // Only use the result of getSetCCResultType if it is legal,
324 // otherwise just use the promoted result type (NVT).
325 if (!TLI.isTypeLegal(SVT))
326 SVT = NVT;
327
328 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
329 SDValue Res = DAG.getAtomicCmpSwap(
330 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
331 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
332 N->getMemOperand());
333 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
334 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
335 return Res.getValue(1);
336 }
337
338 // Op2 is used for the comparison and thus must be extended according to the
339 // target's atomic operations. Op3 is merely stored and so can be left alone.
340 SDValue Op2 = N->getOperand(2);
341 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
342 switch (TLI.getExtendForAtomicCmpSwapArg()) {
343 case ISD::SIGN_EXTEND:
344 Op2 = SExtPromotedInteger(Op2);
345 break;
346 case ISD::ZERO_EXTEND:
347 Op2 = ZExtPromotedInteger(Op2);
348 break;
349 case ISD::ANY_EXTEND:
350 Op2 = GetPromotedInteger(Op2);
351 break;
352 default:
353 llvm_unreachable("Invalid atomic op extension")::llvm::llvm_unreachable_internal("Invalid atomic op extension"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 353
)
;
354 }
355
356 SDVTList VTs =
357 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
358 SDValue Res = DAG.getAtomicCmpSwap(
359 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
360 N->getBasePtr(), Op2, Op3, N->getMemOperand());
361 // Update the use to N with the newly created Res.
362 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
363 ReplaceValueWith(SDValue(N, i), Res.getValue(i));
364 return Res;
365}
366
367SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
368 SDValue InOp = N->getOperand(0);
369 EVT InVT = InOp.getValueType();
370 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
371 EVT OutVT = N->getValueType(0);
372 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
373 SDLoc dl(N);
374
375 switch (getTypeAction(InVT)) {
376 case TargetLowering::TypeLegal:
377 break;
378 case TargetLowering::TypePromoteInteger:
379 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
380 // The input promotes to the same size. Convert the promoted value.
381 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
382 break;
383 case TargetLowering::TypeSoftenFloat:
384 // Promote the integer operand by hand.
385 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
386 case TargetLowering::TypeSoftPromoteHalf:
387 // Promote the integer operand by hand.
388 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftPromotedHalf(InOp));
389 case TargetLowering::TypePromoteFloat: {
390 // Convert the promoted float by hand.
391 if (!NOutVT.isVector())
392 return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, GetPromotedFloat(InOp));
393 break;
394 }
395 case TargetLowering::TypeExpandInteger:
396 case TargetLowering::TypeExpandFloat:
397 break;
398 case TargetLowering::TypeScalarizeVector:
399 // Convert the element to an integer and promote it by hand.
400 if (!NOutVT.isVector())
401 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
402 BitConvertToInteger(GetScalarizedVector(InOp)));
403 break;
404 case TargetLowering::TypeScalarizeScalableVector:
405 report_fatal_error("Scalarization of scalable vectors is not supported.");
406 case TargetLowering::TypeSplitVector: {
407 if (!NOutVT.isVector()) {
408 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
409 // pieces of the input into integers and reassemble in the final type.
410 SDValue Lo, Hi;
411 GetSplitVector(N->getOperand(0), Lo, Hi);
412 Lo = BitConvertToInteger(Lo);
413 Hi = BitConvertToInteger(Hi);
414
415 if (DAG.getDataLayout().isBigEndian())
416 std::swap(Lo, Hi);
417
418 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
419 EVT::getIntegerVT(*DAG.getContext(),
420 NOutVT.getSizeInBits()),
421 JoinIntegers(Lo, Hi));
422 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
423 }
424 break;
425 }
426 case TargetLowering::TypeWidenVector:
427 // The input is widened to the same size. Convert to the widened value.
428 // Make sure that the outgoing value is not a vector, because this would
429 // make us bitcast between two vectors which are legalized in different ways.
430 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector()) {
431 SDValue Res =
432 DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
433
434 // For big endian targets we need to shift the casted value or the
435 // interesting bits will end up at the wrong place.
436 if (DAG.getDataLayout().isBigEndian()) {
437 unsigned ShiftAmt = NInVT.getSizeInBits() - InVT.getSizeInBits();
438 EVT ShiftAmtTy = TLI.getShiftAmountTy(NOutVT, DAG.getDataLayout());
439 assert(ShiftAmt < NOutVT.getSizeInBits() && "Too large shift amount!")(static_cast <bool> (ShiftAmt < NOutVT.getSizeInBits
() && "Too large shift amount!") ? void (0) : __assert_fail
("ShiftAmt < NOutVT.getSizeInBits() && \"Too large shift amount!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 439
, __extension__ __PRETTY_FUNCTION__))
;
440 Res = DAG.getNode(ISD::SRL, dl, NOutVT, Res,
441 DAG.getConstant(ShiftAmt, dl, ShiftAmtTy));
442 }
443 return Res;
444 }
445 // If the output type is also a vector and widening it to the same size
446 // as the widened input type would be a legal type, we can widen the bitcast
447 // and handle the promotion after.
448 if (NOutVT.isVector()) {
449 unsigned WidenInSize = NInVT.getSizeInBits();
450 unsigned OutSize = OutVT.getSizeInBits();
451 if (WidenInSize % OutSize == 0) {
452 unsigned Scale = WidenInSize / OutSize;
453 EVT WideOutVT = EVT::getVectorVT(*DAG.getContext(),
454 OutVT.getVectorElementType(),
455 OutVT.getVectorNumElements() * Scale);
456 if (isTypeLegal(WideOutVT)) {
457 InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
458 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
459 DAG.getVectorIdxConstant(0, dl));
460 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
461 }
462 }
463 }
464 }
465
466 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
467 CreateStackStoreLoad(InOp, OutVT));
468}
469
470SDValue DAGTypeLegalizer::PromoteIntRes_FREEZE(SDNode *N) {
471 SDValue V = GetPromotedInteger(N->getOperand(0));
472 return DAG.getNode(ISD::FREEZE, SDLoc(N),
473 V.getValueType(), V);
474}
475
476SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
477 SDValue Op = GetPromotedInteger(N->getOperand(0));
478 EVT OVT = N->getValueType(0);
479 EVT NVT = Op.getValueType();
480 SDLoc dl(N);
481
482 // If the larger BSWAP isn't supported by the target, try to expand now.
483 // If we expand later we'll end up with more operations since we lost the
484 // original type. We only do this for scalars since we have a shuffle
485 // based lowering for vectors in LegalizeVectorOps.
486 if (!OVT.isVector() &&
487 !TLI.isOperationLegalOrCustomOrPromote(ISD::BSWAP, NVT)) {
488 if (SDValue Res = TLI.expandBSWAP(N, DAG))
489 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
490 }
491
492 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
493 EVT ShiftVT = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
494 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
495 DAG.getConstant(DiffBits, dl, ShiftVT));
496}
497
498SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
499 SDValue Op = GetPromotedInteger(N->getOperand(0));
500 EVT OVT = N->getValueType(0);
501 EVT NVT = Op.getValueType();
502 SDLoc dl(N);
503
504 // If the larger BITREVERSE isn't supported by the target, try to expand now.
505 // If we expand later we'll end up with more operations since we lost the
506 // original type. We only do this for scalars since we have a shuffle
507 // based lowering for vectors in LegalizeVectorOps.
508 if (!OVT.isVector() && OVT.isSimple() &&
509 !TLI.isOperationLegalOrCustomOrPromote(ISD::BITREVERSE, NVT)) {
510 if (SDValue Res = TLI.expandBITREVERSE(N, DAG))
511 return DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Res);
512 }
513
514 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
515 EVT ShiftVT = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
516 return DAG.getNode(ISD::SRL, dl, NVT,
517 DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
518 DAG.getConstant(DiffBits, dl, ShiftVT));
519}
520
521SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
522 // The pair element type may be legal, or may not promote to the same type as
523 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
524 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
525 TLI.getTypeToTransformTo(*DAG.getContext(),
526 N->getValueType(0)), JoinIntegers(N->getOperand(0),
527 N->getOperand(1)));
528}
529
530SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
531 EVT VT = N->getValueType(0);
532 // FIXME there is no actual debug info here
533 SDLoc dl(N);
534 // Zero extend things like i1, sign extend everything else. It shouldn't
535 // matter in theory which one we pick, but this tends to give better code?
536 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
537 SDValue Result = DAG.getNode(Opc, dl,
538 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
539 SDValue(N, 0));
540 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?")(static_cast <bool> (isa<ConstantSDNode>(Result) &&
"Didn't constant fold ext?") ? void (0) : __assert_fail ("isa<ConstantSDNode>(Result) && \"Didn't constant fold ext?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 540
, __extension__ __PRETTY_FUNCTION__))
;
541 return Result;
542}
543
544SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
545 EVT OVT = N->getValueType(0);
546 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
547 SDLoc dl(N);
548
549 // If the larger CTLZ isn't supported by the target, try to expand now.
550 // If we expand later we'll end up with more operations since we lost the
551 // original type.
552 if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
553 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTLZ, NVT) &&
554 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTLZ_ZERO_UNDEF, NVT)) {
555 if (SDValue Result = TLI.expandCTLZ(N, DAG)) {
556 Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
557 return Result;
558 }
559 }
560
561 // Zero extend to the promoted type and do the count there.
562 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
563 Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
564 // Subtract off the extra leading bits in the bigger type.
565 return DAG.getNode(
566 ISD::SUB, dl, NVT, Op,
567 DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
568 NVT));
569}
570
571SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP_PARITY(SDNode *N) {
572 EVT OVT = N->getValueType(0);
573 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
574
575 // If the larger CTPOP isn't supported by the target, try to expand now.
576 // If we expand later we'll end up with more operations since we lost the
577 // original type.
578 // TODO: Expand ISD::PARITY. Need to move ExpandPARITY from LegalizeDAG to
579 // TargetLowering.
580 if (N->getOpcode() == ISD::CTPOP && !OVT.isVector() && TLI.isTypeLegal(NVT) &&
581 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTPOP, NVT)) {
582 if (SDValue Result = TLI.expandCTPOP(N, DAG)) {
583 Result = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Result);
584 return Result;
585 }
586 }
587
588 // Zero extend to the promoted type and do the count or parity there.
589 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
590 return DAG.getNode(N->getOpcode(), SDLoc(N), Op.getValueType(), Op);
591}
592
593SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
594 SDValue Op = GetPromotedInteger(N->getOperand(0));
595 EVT OVT = N->getValueType(0);
596 EVT NVT = Op.getValueType();
597 SDLoc dl(N);
598
599 // If the larger CTTZ isn't supported by the target, try to expand now.
600 // If we expand later we'll end up with more operations since we lost the
601 // original type. Don't expand if we can use CTPOP or CTLZ expansion on the
602 // larger type.
603 if (!OVT.isVector() && TLI.isTypeLegal(NVT) &&
604 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ, NVT) &&
605 !TLI.isOperationLegalOrCustomOrPromote(ISD::CTTZ_ZERO_UNDEF, NVT) &&
606 !TLI.isOperationLegal(ISD::CTPOP, NVT) &&
607 !TLI.isOperationLegal(ISD::CTLZ, NVT)) {
608 if (SDValue Result = TLI.expandCTTZ(N, DAG)) {
609 Result = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Result);
610 return Result;
611 }
612 }
613
614 if (N->getOpcode() == ISD::CTTZ) {
615 // The count is the same in the promoted type except if the original
616 // value was zero. This can be handled by setting the bit just off
617 // the top of the original type.
618 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
619 OVT.getScalarSizeInBits());
620 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
621 }
622 return DAG.getNode(N->getOpcode(), dl, NVT, Op);
623}
624
625SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
626 SDLoc dl(N);
627 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
628
629 SDValue Op0 = N->getOperand(0);
630 SDValue Op1 = N->getOperand(1);
631
632 // If the input also needs to be promoted, do that first so we can get a
633 // get a good idea for the output type.
634 if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
635 == TargetLowering::TypePromoteInteger) {
636 SDValue In = GetPromotedInteger(Op0);
637
638 // If the new type is larger than NVT, use it. We probably won't need to
639 // promote it again.
640 EVT SVT = In.getValueType().getScalarType();
641 if (SVT.bitsGE(NVT)) {
642 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
643 return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
644 }
645 }
646
647 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
648}
649
650SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
651 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
652 unsigned NewOpc = N->getOpcode();
653 SDLoc dl(N);
654
655 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
656 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
657 // and SINT conversions are Custom, there is no way to tell which is
658 // preferable. We choose SINT because that's the right thing on PPC.)
659 if (N->getOpcode() == ISD::FP_TO_UINT &&
660 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
661 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
662 NewOpc = ISD::FP_TO_SINT;
663
664 if (N->getOpcode() == ISD::STRICT_FP_TO_UINT &&
665 !TLI.isOperationLegal(ISD::STRICT_FP_TO_UINT, NVT) &&
666 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
667 NewOpc = ISD::STRICT_FP_TO_SINT;
668
669 SDValue Res;
670 if (N->isStrictFPOpcode()) {
671 Res = DAG.getNode(NewOpc, dl, {NVT, MVT::Other},
672 {N->getOperand(0), N->getOperand(1)});
673 // Legalize the chain result - switch anything that used the old chain to
674 // use the new one.
675 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
676 } else
677 Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
678
679 // Assert that the converted value fits in the original type. If it doesn't
680 // (eg: because the value being converted is too big), then the result of the
681 // original operation was undefined anyway, so the assert is still correct.
682 //
683 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
684 // before legalization: fp-to-uint16, 65534. -> 0xfffe
685 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
686 return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
687 N->getOpcode() == ISD::STRICT_FP_TO_UINT) ?
688 ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
689 DAG.getValueType(N->getValueType(0).getScalarType()));
690}
691
692SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT_SAT(SDNode *N) {
693 // Promote the result type, while keeping the original width in Op1.
694 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
695 SDLoc dl(N);
696 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0),
697 N->getOperand(1));
698}
699
700SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
701 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
702 SDLoc dl(N);
703
704 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
705}
706
707SDValue DAGTypeLegalizer::PromoteIntRes_FLT_ROUNDS(SDNode *N) {
708 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
709 SDLoc dl(N);
710
711 SDValue Res =
712 DAG.getNode(N->getOpcode(), dl, {NVT, MVT::Other}, N->getOperand(0));
713
714 // Legalize the chain result - switch anything that used the old chain to
715 // use the new one.
716 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
717 return Res;
718}
719
720SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
721 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
722 SDLoc dl(N);
723
724 if (getTypeAction(N->getOperand(0).getValueType())
725 == TargetLowering::TypePromoteInteger) {
726 SDValue Res = GetPromotedInteger(N->getOperand(0));
727 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!")(static_cast <bool> (Res.getValueType().bitsLE(NVT) &&
"Extension doesn't make sense!") ? void (0) : __assert_fail (
"Res.getValueType().bitsLE(NVT) && \"Extension doesn't make sense!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 727
, __extension__ __PRETTY_FUNCTION__))
;
728
729 // If the result and operand types are the same after promotion, simplify
730 // to an in-register extension.
731 if (NVT == Res.getValueType()) {
732 // The high bits are not guaranteed to be anything. Insert an extend.
733 if (N->getOpcode() == ISD::SIGN_EXTEND)
734 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
735 DAG.getValueType(N->getOperand(0).getValueType()));
736 if (N->getOpcode() == ISD::ZERO_EXTEND)
737 return DAG.getZeroExtendInReg(Res, dl, N->getOperand(0).getValueType());
738 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!")(static_cast <bool> (N->getOpcode() == ISD::ANY_EXTEND
&& "Unknown integer extension!") ? void (0) : __assert_fail
("N->getOpcode() == ISD::ANY_EXTEND && \"Unknown integer extension!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 738
, __extension__ __PRETTY_FUNCTION__))
;
739 return Res;
740 }
741 }
742
743 // Otherwise, just extend the original operand all the way to the larger type.
744 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
745}
746
747SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
748 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!")(static_cast <bool> (ISD::isUNINDEXEDLoad(N) &&
"Indexed load during type legalization!") ? void (0) : __assert_fail
("ISD::isUNINDEXEDLoad(N) && \"Indexed load during type legalization!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 748
, __extension__ __PRETTY_FUNCTION__))
;
749 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
750 ISD::LoadExtType ExtType =
751 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
752 SDLoc dl(N);
753 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
754 N->getMemoryVT(), N->getMemOperand());
755
756 // Legalize the chain result - switch anything that used the old chain to
757 // use the new one.
758 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
759 return Res;
760}
761
762SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
763 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
764 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
765
766 ISD::LoadExtType ExtType = N->getExtensionType();
767 if (ExtType == ISD::NON_EXTLOAD)
768 ExtType = ISD::EXTLOAD;
769
770 SDLoc dl(N);
771 SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
772 N->getOffset(), N->getMask(), ExtPassThru,
773 N->getMemoryVT(), N->getMemOperand(),
774 N->getAddressingMode(), ExtType,
775 N->isExpandingLoad());
776 // Legalize the chain result - switch anything that used the old chain to
777 // use the new one.
778 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
779 return Res;
780}
781
782SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
783 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
784 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
785 assert(NVT == ExtPassThru.getValueType() &&(static_cast <bool> (NVT == ExtPassThru.getValueType() &&
"Gather result type and the passThru argument type should be the same"
) ? void (0) : __assert_fail ("NVT == ExtPassThru.getValueType() && \"Gather result type and the passThru argument type should be the same\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 786
, __extension__ __PRETTY_FUNCTION__))
786 "Gather result type and the passThru argument type should be the same")(static_cast <bool> (NVT == ExtPassThru.getValueType() &&
"Gather result type and the passThru argument type should be the same"
) ? void (0) : __assert_fail ("NVT == ExtPassThru.getValueType() && \"Gather result type and the passThru argument type should be the same\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 786
, __extension__ __PRETTY_FUNCTION__))
;
787
788 ISD::LoadExtType ExtType = N->getExtensionType();
789 if (ExtType == ISD::NON_EXTLOAD)
790 ExtType = ISD::EXTLOAD;
791
792 SDLoc dl(N);
793 SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
794 N->getIndex(), N->getScale() };
795 SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
796 N->getMemoryVT(), dl, Ops,
797 N->getMemOperand(), N->getIndexType(),
798 ExtType);
799 // Legalize the chain result - switch anything that used the old chain to
800 // use the new one.
801 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
802 return Res;
803}
804
805/// Promote the overflow flag of an overflowing arithmetic node.
806SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
807 // Change the return type of the boolean result while obeying
808 // getSetCCResultType.
809 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
810 EVT VT = N->getValueType(0);
811 EVT SVT = getSetCCResultType(VT);
812 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
813 unsigned NumOps = N->getNumOperands();
814 assert(NumOps <= 3 && "Too many operands")(static_cast <bool> (NumOps <= 3 && "Too many operands"
) ? void (0) : __assert_fail ("NumOps <= 3 && \"Too many operands\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 814
, __extension__ __PRETTY_FUNCTION__))
;
815 if (NumOps == 3)
816 Ops[2] = N->getOperand(2);
817
818 SDLoc dl(N);
819 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
820 makeArrayRef(Ops, NumOps));
821
822 // Modified the sum result - switch anything that used the old sum to use
823 // the new one.
824 ReplaceValueWith(SDValue(N, 0), Res);
825
826 // Convert to the expected type.
827 return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
828}
829
830SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSHLSAT(SDNode *N) {
831 // If the promoted type is legal, we can convert this to:
832 // 1. ANY_EXTEND iN to iM
833 // 2. SHL by M-N
834 // 3. [US][ADD|SUB|SHL]SAT
835 // 4. L/ASHR by M-N
836 // Else it is more efficient to convert this to a min and a max
837 // operation in the higher precision arithmetic.
838 SDLoc dl(N);
839 SDValue Op1 = N->getOperand(0);
840 SDValue Op2 = N->getOperand(1);
841 unsigned OldBits = Op1.getScalarValueSizeInBits();
842
843 unsigned Opcode = N->getOpcode();
844 bool IsShift = Opcode == ISD::USHLSAT || Opcode == ISD::SSHLSAT;
845
846 SDValue Op1Promoted, Op2Promoted;
847 if (IsShift) {
848 Op1Promoted = GetPromotedInteger(Op1);
849 Op2Promoted = ZExtPromotedInteger(Op2);
850 } else if (Opcode == ISD::UADDSAT || Opcode == ISD::USUBSAT) {
851 Op1Promoted = ZExtPromotedInteger(Op1);
852 Op2Promoted = ZExtPromotedInteger(Op2);
853 } else {
854 Op1Promoted = SExtPromotedInteger(Op1);
855 Op2Promoted = SExtPromotedInteger(Op2);
856 }
857 EVT PromotedType = Op1Promoted.getValueType();
858 unsigned NewBits = PromotedType.getScalarSizeInBits();
859
860 if (Opcode == ISD::UADDSAT) {
861 APInt MaxVal = APInt::getAllOnes(OldBits).zext(NewBits);
862 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
863 SDValue Add =
864 DAG.getNode(ISD::ADD, dl, PromotedType, Op1Promoted, Op2Promoted);
865 return DAG.getNode(ISD::UMIN, dl, PromotedType, Add, SatMax);
866 }
867
868 // USUBSAT can always be promoted as long as we have zero-extended the args.
869 if (Opcode == ISD::USUBSAT)
870 return DAG.getNode(ISD::USUBSAT, dl, PromotedType, Op1Promoted,
871 Op2Promoted);
872
873 // Shift cannot use a min/max expansion, we can't detect overflow if all of
874 // the bits have been shifted out.
875 if (IsShift || TLI.isOperationLegal(Opcode, PromotedType)) {
876 unsigned ShiftOp;
877 switch (Opcode) {
878 case ISD::SADDSAT:
879 case ISD::SSUBSAT:
880 case ISD::SSHLSAT:
881 ShiftOp = ISD::SRA;
882 break;
883 case ISD::USHLSAT:
884 ShiftOp = ISD::SRL;
885 break;
886 default:
887 llvm_unreachable("Expected opcode to be signed or unsigned saturation "::llvm::llvm_unreachable_internal("Expected opcode to be signed or unsigned saturation "
"addition, subtraction or left shift", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 888)
888 "addition, subtraction or left shift")::llvm::llvm_unreachable_internal("Expected opcode to be signed or unsigned saturation "
"addition, subtraction or left shift", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 888)
;
889 }
890
891 unsigned SHLAmount = NewBits - OldBits;
892 EVT SHVT = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
893 SDValue ShiftAmount = DAG.getConstant(SHLAmount, dl, SHVT);
894 Op1Promoted =
895 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted, ShiftAmount);
896 if (!IsShift)
897 Op2Promoted =
898 DAG.getNode(ISD::SHL, dl, PromotedType, Op2Promoted, ShiftAmount);
899
900 SDValue Result =
901 DAG.getNode(Opcode, dl, PromotedType, Op1Promoted, Op2Promoted);
902 return DAG.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
903 }
904
905 unsigned AddOp = Opcode == ISD::SADDSAT ? ISD::ADD : ISD::SUB;
906 APInt MinVal = APInt::getSignedMinValue(OldBits).sext(NewBits);
907 APInt MaxVal = APInt::getSignedMaxValue(OldBits).sext(NewBits);
908 SDValue SatMin = DAG.getConstant(MinVal, dl, PromotedType);
909 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
910 SDValue Result =
911 DAG.getNode(AddOp, dl, PromotedType, Op1Promoted, Op2Promoted);
912 Result = DAG.getNode(ISD::SMIN, dl, PromotedType, Result, SatMax);
913 Result = DAG.getNode(ISD::SMAX, dl, PromotedType, Result, SatMin);
914 return Result;
915}
916
917SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
918 // Can just promote the operands then continue with operation.
919 SDLoc dl(N);
920 SDValue Op1Promoted, Op2Promoted;
921 bool Signed =
922 N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
923 bool Saturating =
924 N->getOpcode() == ISD::SMULFIXSAT || N->getOpcode() == ISD::UMULFIXSAT;
925 if (Signed) {
926 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
927 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
928 } else {
929 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
930 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
931 }
932 EVT OldType = N->getOperand(0).getValueType();
933 EVT PromotedType = Op1Promoted.getValueType();
934 unsigned DiffSize =
935 PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
936
937 if (Saturating) {
938 // Promoting the operand and result values changes the saturation width,
939 // which is extends the values that we clamp to on saturation. This could be
940 // resolved by shifting one of the operands the same amount, which would
941 // also shift the result we compare against, then shifting back.
942 EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
943 Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
944 DAG.getConstant(DiffSize, dl, ShiftTy));
945 SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
946 Op2Promoted, N->getOperand(2));
947 unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
948 return DAG.getNode(ShiftOp, dl, PromotedType, Result,
949 DAG.getConstant(DiffSize, dl, ShiftTy));
950 }
951 return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
952 N->getOperand(2));
953}
954
955static SDValue SaturateWidenedDIVFIX(SDValue V, SDLoc &dl,
956 unsigned SatW, bool Signed,
957 const TargetLowering &TLI,
958 SelectionDAG &DAG) {
959 EVT VT = V.getValueType();
960 unsigned VTW = VT.getScalarSizeInBits();
961
962 if (!Signed) {
963 // Saturate to the unsigned maximum by getting the minimum of V and the
964 // maximum.
965 return DAG.getNode(ISD::UMIN, dl, VT, V,
966 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW),
967 dl, VT));
968 }
969
970 // Saturate to the signed maximum (the low SatW - 1 bits) by taking the
971 // signed minimum of it and V.
972 V = DAG.getNode(ISD::SMIN, dl, VT, V,
973 DAG.getConstant(APInt::getLowBitsSet(VTW, SatW - 1),
974 dl, VT));
975 // Saturate to the signed minimum (the high SatW + 1 bits) by taking the
976 // signed maximum of it and V.
977 V = DAG.getNode(ISD::SMAX, dl, VT, V,
978 DAG.getConstant(APInt::getHighBitsSet(VTW, VTW - SatW + 1),
979 dl, VT));
980 return V;
981}
982
983static SDValue earlyExpandDIVFIX(SDNode *N, SDValue LHS, SDValue RHS,
984 unsigned Scale, const TargetLowering &TLI,
985 SelectionDAG &DAG, unsigned SatW = 0) {
986 EVT VT = LHS.getValueType();
987 unsigned VTSize = VT.getScalarSizeInBits();
988 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
989 N->getOpcode() == ISD::SDIVFIXSAT;
990 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
991 N->getOpcode() == ISD::UDIVFIXSAT;
992
993 SDLoc dl(N);
994 // Widen the types by a factor of two. This is guaranteed to expand, since it
995 // will always have enough high bits in the LHS to shift into.
996 EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VTSize * 2);
997 if (VT.isVector())
998 WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT,
999 VT.getVectorElementCount());
1000 if (Signed) {
1001 LHS = DAG.getSExtOrTrunc(LHS, dl, WideVT);
1002 RHS = DAG.getSExtOrTrunc(RHS, dl, WideVT);
1003 } else {
1004 LHS = DAG.getZExtOrTrunc(LHS, dl, WideVT);
1005 RHS = DAG.getZExtOrTrunc(RHS, dl, WideVT);
1006 }
1007
1008 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, LHS, RHS, Scale,
1009 DAG);
1010 assert(Res && "Expanding DIVFIX with wide type failed?")(static_cast <bool> (Res && "Expanding DIVFIX with wide type failed?"
) ? void (0) : __assert_fail ("Res && \"Expanding DIVFIX with wide type failed?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1010
, __extension__ __PRETTY_FUNCTION__))
;
1011 if (Saturating) {
1012 // If the caller has told us to saturate at something less, use that width
1013 // instead of the type before doubling. However, it cannot be more than
1014 // what we just widened!
1015 assert(SatW <= VTSize &&(static_cast <bool> (SatW <= VTSize && "Tried to saturate to more than the original type?"
) ? void (0) : __assert_fail ("SatW <= VTSize && \"Tried to saturate to more than the original type?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1016
, __extension__ __PRETTY_FUNCTION__))
1016 "Tried to saturate to more than the original type?")(static_cast <bool> (SatW <= VTSize && "Tried to saturate to more than the original type?"
) ? void (0) : __assert_fail ("SatW <= VTSize && \"Tried to saturate to more than the original type?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1016
, __extension__ __PRETTY_FUNCTION__))
;
1017 Res = SaturateWidenedDIVFIX(Res, dl, SatW == 0 ? VTSize : SatW, Signed,
1018 TLI, DAG);
1019 }
1020 return DAG.getZExtOrTrunc(Res, dl, VT);
1021}
1022
1023SDValue DAGTypeLegalizer::PromoteIntRes_DIVFIX(SDNode *N) {
1024 SDLoc dl(N);
1025 SDValue Op1Promoted, Op2Promoted;
1026 bool Signed = N->getOpcode() == ISD::SDIVFIX ||
1027 N->getOpcode() == ISD::SDIVFIXSAT;
1028 bool Saturating = N->getOpcode() == ISD::SDIVFIXSAT ||
1029 N->getOpcode() == ISD::UDIVFIXSAT;
1030 if (Signed) {
1031 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
1032 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
1033 } else {
1034 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
1035 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
1036 }
1037 EVT PromotedType = Op1Promoted.getValueType();
1038 unsigned Scale = N->getConstantOperandVal(2);
1039
1040 // If the type is already legal and the operation is legal in that type, we
1041 // should not early expand.
1042 if (TLI.isTypeLegal(PromotedType)) {
1043 TargetLowering::LegalizeAction Action =
1044 TLI.getFixedPointOperationAction(N->getOpcode(), PromotedType, Scale);
1045 if (Action == TargetLowering::Legal || Action == TargetLowering::Custom) {
1046 EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
1047 unsigned Diff = PromotedType.getScalarSizeInBits() -
1048 N->getValueType(0).getScalarSizeInBits();
1049 if (Saturating)
1050 Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
1051 DAG.getConstant(Diff, dl, ShiftTy));
1052 SDValue Res = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
1053 Op2Promoted, N->getOperand(2));
1054 if (Saturating)
1055 Res = DAG.getNode(Signed ? ISD::SRA : ISD::SRL, dl, PromotedType, Res,
1056 DAG.getConstant(Diff, dl, ShiftTy));
1057 return Res;
1058 }
1059 }
1060
1061 // See if we can perform the division in this type without expanding.
1062 if (SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, Op1Promoted,
1063 Op2Promoted, Scale, DAG)) {
1064 if (Saturating)
1065 Res = SaturateWidenedDIVFIX(Res, dl,
1066 N->getValueType(0).getScalarSizeInBits(),
1067 Signed, TLI, DAG);
1068 return Res;
1069 }
1070 // If we cannot, expand it to twice the type width. If we are saturating, give
1071 // it the original width as a saturating width so we don't need to emit
1072 // two saturations.
1073 return earlyExpandDIVFIX(N, Op1Promoted, Op2Promoted, Scale, TLI, DAG,
1074 N->getValueType(0).getScalarSizeInBits());
1075}
1076
1077SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
1078 if (ResNo == 1)
1079 return PromoteIntRes_Overflow(N);
1080
1081 // The operation overflowed iff the result in the larger type is not the
1082 // sign extension of its truncation to the original type.
1083 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1084 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1085 EVT OVT = N->getOperand(0).getValueType();
1086 EVT NVT = LHS.getValueType();
1087 SDLoc dl(N);
1088
1089 // Do the arithmetic in the larger type.
1090 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
1091 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1092
1093 // Calculate the overflow flag: sign extend the arithmetic result from
1094 // the original type.
1095 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
1096 DAG.getValueType(OVT));
1097 // Overflowed if and only if this is not equal to Res.
1098 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1099
1100 // Use the calculated overflow everywhere.
1101 ReplaceValueWith(SDValue(N, 1), Ofl);
1102
1103 return Res;
1104}
1105
1106SDValue DAGTypeLegalizer::PromoteIntRes_Select(SDNode *N) {
1107 SDValue Mask = N->getOperand(0);
1108
1109 SDValue LHS = GetPromotedInteger(N->getOperand(1));
1110 SDValue RHS = GetPromotedInteger(N->getOperand(2));
1111
1112 unsigned Opcode = N->getOpcode();
1113 return Opcode == ISD::VP_SELECT
1114 ? DAG.getNode(Opcode, SDLoc(N), LHS.getValueType(), Mask, LHS, RHS,
1115 N->getOperand(3))
1116 : DAG.getNode(Opcode, SDLoc(N), LHS.getValueType(), Mask, LHS,
1117 RHS);
1118}
1119
1120SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
1121 SDValue LHS = GetPromotedInteger(N->getOperand(2));
1122 SDValue RHS = GetPromotedInteger(N->getOperand(3));
1123 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
1124 LHS.getValueType(), N->getOperand(0),
1125 N->getOperand(1), LHS, RHS, N->getOperand(4));
1126}
1127
1128SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
1129 unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
1130 EVT InVT = N->getOperand(OpNo).getValueType();
1131 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1132
1133 EVT SVT = getSetCCResultType(InVT);
1134
1135 // If we got back a type that needs to be promoted, this likely means the
1136 // the input type also needs to be promoted. So get the promoted type for
1137 // the input and try the query again.
1138 if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
1139 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
1140 InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
1141 SVT = getSetCCResultType(InVT);
1142 } else {
1143 // Input type isn't promoted, just use the default promoted type.
1144 SVT = NVT;
1145 }
1146 }
1147
1148 SDLoc dl(N);
1149 assert(SVT.isVector() == N->getOperand(OpNo).getValueType().isVector() &&(static_cast <bool> (SVT.isVector() == N->getOperand
(OpNo).getValueType().isVector() && "Vector compare must return a vector result!"
) ? void (0) : __assert_fail ("SVT.isVector() == N->getOperand(OpNo).getValueType().isVector() && \"Vector compare must return a vector result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1150
, __extension__ __PRETTY_FUNCTION__))
1150 "Vector compare must return a vector result!")(static_cast <bool> (SVT.isVector() == N->getOperand
(OpNo).getValueType().isVector() && "Vector compare must return a vector result!"
) ? void (0) : __assert_fail ("SVT.isVector() == N->getOperand(OpNo).getValueType().isVector() && \"Vector compare must return a vector result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1150
, __extension__ __PRETTY_FUNCTION__))
;
1151
1152 // Get the SETCC result using the canonical SETCC type.
1153 SDValue SetCC;
1154 if (N->isStrictFPOpcode()) {
1155 EVT VTs[] = {SVT, MVT::Other};
1156 SDValue Opers[] = {N->getOperand(0), N->getOperand(1),
1157 N->getOperand(2), N->getOperand(3)};
1158 SetCC = DAG.getNode(N->getOpcode(), dl, VTs, Opers);
1159 // Legalize the chain result - switch anything that used the old chain to
1160 // use the new one.
1161 ReplaceValueWith(SDValue(N, 1), SetCC.getValue(1));
1162 } else
1163 SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
1164 N->getOperand(1), N->getOperand(2));
1165
1166 // Convert to the expected type.
1167 return DAG.getSExtOrTrunc(SetCC, dl, NVT);
1168}
1169
1170SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
1171 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1172 SDValue RHS = N->getOperand(1);
1173 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1174 RHS = ZExtPromotedInteger(RHS);
1175 if (N->getOpcode() != ISD::VP_SHL)
1176 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1177 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1178 N->getOperand(2), N->getOperand(3));
1179}
1180
1181SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
1182 SDValue Op = GetPromotedInteger(N->getOperand(0));
1183 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
1184 Op.getValueType(), Op, N->getOperand(1));
1185}
1186
1187SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
1188 // The input may have strange things in the top bits of the registers, but
1189 // these operations don't care. They may have weird bits going out, but
1190 // that too is okay if they are integer operations.
1191 SDValue LHS = GetPromotedInteger(N->getOperand(0));
1192 SDValue RHS = GetPromotedInteger(N->getOperand(1));
1193 if (N->getNumOperands() == 2)
1194 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1195 assert(N->getNumOperands() == 4 && "Unexpected number of operands!")(static_cast <bool> (N->getNumOperands() == 4 &&
"Unexpected number of operands!") ? void (0) : __assert_fail
("N->getNumOperands() == 4 && \"Unexpected number of operands!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1195
, __extension__ __PRETTY_FUNCTION__))
;
1196 assert(N->isVPOpcode() && "Expected VP opcode")(static_cast <bool> (N->isVPOpcode() && "Expected VP opcode"
) ? void (0) : __assert_fail ("N->isVPOpcode() && \"Expected VP opcode\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1196
, __extension__ __PRETTY_FUNCTION__))
;
1197 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1198 N->getOperand(2), N->getOperand(3));
1199}
1200
1201SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
1202 // Sign extend the input.
1203 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1204 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1205 if (N->getNumOperands() == 2)
1206 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1207 assert(N->getNumOperands() == 4 && "Unexpected number of operands!")(static_cast <bool> (N->getNumOperands() == 4 &&
"Unexpected number of operands!") ? void (0) : __assert_fail
("N->getNumOperands() == 4 && \"Unexpected number of operands!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1207
, __extension__ __PRETTY_FUNCTION__))
;
1208 assert(N->isVPOpcode() && "Expected VP opcode")(static_cast <bool> (N->isVPOpcode() && "Expected VP opcode"
) ? void (0) : __assert_fail ("N->isVPOpcode() && \"Expected VP opcode\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1208
, __extension__ __PRETTY_FUNCTION__))
;
1209 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1210 N->getOperand(2), N->getOperand(3));
1211}
1212
1213SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
1214 // Zero extend the input.
1215 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1216 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1217 if (N->getNumOperands() == 2)
1218 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1219 assert(N->getNumOperands() == 4 && "Unexpected number of operands!")(static_cast <bool> (N->getNumOperands() == 4 &&
"Unexpected number of operands!") ? void (0) : __assert_fail
("N->getNumOperands() == 4 && \"Unexpected number of operands!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1219
, __extension__ __PRETTY_FUNCTION__))
;
1220 assert(N->isVPOpcode() && "Expected VP opcode")(static_cast <bool> (N->isVPOpcode() && "Expected VP opcode"
) ? void (0) : __assert_fail ("N->isVPOpcode() && \"Expected VP opcode\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1220
, __extension__ __PRETTY_FUNCTION__))
;
1221 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1222 N->getOperand(2), N->getOperand(3));
1223}
1224
1225SDValue DAGTypeLegalizer::PromoteIntRes_UMINUMAX(SDNode *N) {
1226 // It doesn't matter if we sign extend or zero extend in the inputs. So do
1227 // whatever is best for the target.
1228 SDValue LHS = SExtOrZExtPromotedInteger(N->getOperand(0));
1229 SDValue RHS = SExtOrZExtPromotedInteger(N->getOperand(1));
1230 return DAG.getNode(N->getOpcode(), SDLoc(N),
1231 LHS.getValueType(), LHS, RHS);
1232}
1233
1234SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
1235 // The input value must be properly sign extended.
1236 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1237 SDValue RHS = N->getOperand(1);
1238 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1239 RHS = ZExtPromotedInteger(RHS);
1240 if (N->getOpcode() != ISD::VP_ASHR)
1241 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1242 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1243 N->getOperand(2), N->getOperand(3));
1244}
1245
1246SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
1247 // The input value must be properly zero extended.
1248 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1249 SDValue RHS = N->getOperand(1);
1250 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
1251 RHS = ZExtPromotedInteger(RHS);
1252 if (N->getOpcode() != ISD::VP_LSHR)
1253 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS);
1254 return DAG.getNode(N->getOpcode(), SDLoc(N), LHS.getValueType(), LHS, RHS,
1255 N->getOperand(2), N->getOperand(3));
1256}
1257
1258SDValue DAGTypeLegalizer::PromoteIntRes_Rotate(SDNode *N) {
1259 // Lower the rotate to shifts and ORs which can be promoted.
1260 SDValue Res = TLI.expandROT(N, true /*AllowVectorOps*/, DAG);
1261 ReplaceValueWith(SDValue(N, 0), Res);
1262 return SDValue();
1263}
1264
1265SDValue DAGTypeLegalizer::PromoteIntRes_FunnelShift(SDNode *N) {
1266 SDValue Hi = GetPromotedInteger(N->getOperand(0));
1267 SDValue Lo = GetPromotedInteger(N->getOperand(1));
1268 SDValue Amt = GetPromotedInteger(N->getOperand(2));
1269
1270 SDLoc DL(N);
1271 EVT OldVT = N->getOperand(0).getValueType();
1272 EVT VT = Lo.getValueType();
1273 unsigned Opcode = N->getOpcode();
1274 bool IsFSHR = Opcode == ISD::FSHR;
1275 unsigned OldBits = OldVT.getScalarSizeInBits();
1276 unsigned NewBits = VT.getScalarSizeInBits();
1277
1278 // Amount has to be interpreted modulo the old bit width.
1279 Amt = DAG.getNode(ISD::UREM, DL, VT, Amt, DAG.getConstant(OldBits, DL, VT));
1280
1281 // If the promoted type is twice the size (or more), then we use the
1282 // traditional funnel 'double' shift codegen. This isn't necessary if the
1283 // shift amount is constant.
1284 // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z % bw)) >> bw.
1285 // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z % bw)).
1286 if (NewBits >= (2 * OldBits) && !isa<ConstantSDNode>(Amt) &&
1287 !TLI.isOperationLegalOrCustom(Opcode, VT)) {
1288 SDValue HiShift = DAG.getConstant(OldBits, DL, VT);
1289 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, HiShift);
1290 Lo = DAG.getZeroExtendInReg(Lo, DL, OldVT);
1291 SDValue Res = DAG.getNode(ISD::OR, DL, VT, Hi, Lo);
1292 Res = DAG.getNode(IsFSHR ? ISD::SRL : ISD::SHL, DL, VT, Res, Amt);
1293 if (!IsFSHR)
1294 Res = DAG.getNode(ISD::SRL, DL, VT, Res, HiShift);
1295 return Res;
1296 }
1297
1298 // Shift Lo up to occupy the upper bits of the promoted type.
1299 SDValue ShiftOffset = DAG.getConstant(NewBits - OldBits, DL, VT);
1300 Lo = DAG.getNode(ISD::SHL, DL, VT, Lo, ShiftOffset);
1301
1302 // Increase Amount to shift the result into the lower bits of the promoted
1303 // type.
1304 if (IsFSHR)
1305 Amt = DAG.getNode(ISD::ADD, DL, VT, Amt, ShiftOffset);
1306
1307 return DAG.getNode(Opcode, DL, VT, Hi, Lo, Amt);
1308}
1309
1310SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
1311 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1312 SDValue Res;
1313 SDValue InOp = N->getOperand(0);
1314 SDLoc dl(N);
1315
1316 switch (getTypeAction(InOp.getValueType())) {
1317 default: llvm_unreachable("Unknown type action!")::llvm::llvm_unreachable_internal("Unknown type action!", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 1317)
;
1318 case TargetLowering::TypeLegal:
1319 case TargetLowering::TypeExpandInteger:
1320 Res = InOp;
1321 break;
1322 case TargetLowering::TypePromoteInteger:
1323 Res = GetPromotedInteger(InOp);
1324 break;
1325 case TargetLowering::TypeSplitVector: {
1326 EVT InVT = InOp.getValueType();
1327 assert(InVT.isVector() && "Cannot split scalar types")(static_cast <bool> (InVT.isVector() && "Cannot split scalar types"
) ? void (0) : __assert_fail ("InVT.isVector() && \"Cannot split scalar types\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1327
, __extension__ __PRETTY_FUNCTION__))
;
1328 ElementCount NumElts = InVT.getVectorElementCount();
1329 assert(NumElts == NVT.getVectorElementCount() &&(static_cast <bool> (NumElts == NVT.getVectorElementCount
() && "Dst and Src must have the same number of elements"
) ? void (0) : __assert_fail ("NumElts == NVT.getVectorElementCount() && \"Dst and Src must have the same number of elements\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1330
, __extension__ __PRETTY_FUNCTION__))
1330 "Dst and Src must have the same number of elements")(static_cast <bool> (NumElts == NVT.getVectorElementCount
() && "Dst and Src must have the same number of elements"
) ? void (0) : __assert_fail ("NumElts == NVT.getVectorElementCount() && \"Dst and Src must have the same number of elements\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1330
, __extension__ __PRETTY_FUNCTION__))
;
1331 assert(isPowerOf2_32(NumElts.getKnownMinValue()) &&(static_cast <bool> (isPowerOf2_32(NumElts.getKnownMinValue
()) && "Promoted vector type must be a power of two")
? void (0) : __assert_fail ("isPowerOf2_32(NumElts.getKnownMinValue()) && \"Promoted vector type must be a power of two\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1332
, __extension__ __PRETTY_FUNCTION__))
1332 "Promoted vector type must be a power of two")(static_cast <bool> (isPowerOf2_32(NumElts.getKnownMinValue
()) && "Promoted vector type must be a power of two")
? void (0) : __assert_fail ("isPowerOf2_32(NumElts.getKnownMinValue()) && \"Promoted vector type must be a power of two\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1332
, __extension__ __PRETTY_FUNCTION__))
;
1333
1334 SDValue EOp1, EOp2;
1335 GetSplitVector(InOp, EOp1, EOp2);
1336
1337 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
1338 NumElts.divideCoefficientBy(2));
1339 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
1340 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
1341
1342 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
1343 }
1344 case TargetLowering::TypeWidenVector: {
1345 SDValue WideInOp = GetWidenedVector(InOp);
1346
1347 // Truncate widened InOp.
1348 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
1349 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
1350 N->getValueType(0).getScalarType(), NumElem);
1351 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
1352
1353 // Zero extend so that the elements are of same type as those of NVT
1354 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
1355 NumElem);
1356 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
1357
1358 // Extract the low NVT subvector.
1359 SDValue ZeroIdx = DAG.getVectorIdxConstant(0, dl);
1360 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
1361 }
1362 }
1363
1364 // Truncate to NVT instead of VT
1365 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
1366}
1367
1368SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
1369 if (ResNo == 1)
1370 return PromoteIntRes_Overflow(N);
1371
1372 // The operation overflowed iff the result in the larger type is not the
1373 // zero extension of its truncation to the original type.
1374 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
1375 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
1376 EVT OVT = N->getOperand(0).getValueType();
1377 EVT NVT = LHS.getValueType();
1378 SDLoc dl(N);
1379
1380 // Do the arithmetic in the larger type.
1381 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
1382 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
1383
1384 // Calculate the overflow flag: zero extend the arithmetic result from
1385 // the original type.
1386 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
1387 // Overflowed if and only if this is not equal to Res.
1388 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
1389
1390 // Use the calculated overflow everywhere.
1391 ReplaceValueWith(SDValue(N, 1), Ofl);
1392
1393 return Res;
1394}
1395
1396// Handle promotion for the ADDE/SUBE/ADDCARRY/SUBCARRY nodes. Notice that
1397// the third operand of ADDE/SUBE nodes is carry flag, which differs from
1398// the ADDCARRY/SUBCARRY nodes in that the third operand is carry Boolean.
1399SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBCARRY(SDNode *N, unsigned ResNo) {
1400 if (ResNo == 1)
1401 return PromoteIntRes_Overflow(N);
1402
1403 // We need to sign-extend the operands so the carry value computed by the
1404 // wide operation will be equivalent to the carry value computed by the
1405 // narrow operation.
1406 // An ADDCARRY can generate carry only if any of the operands has its
1407 // most significant bit set. Sign extension propagates the most significant
1408 // bit into the higher bits which means the extra bit that the narrow
1409 // addition would need (i.e. the carry) will be propagated through the higher
1410 // bits of the wide addition.
1411 // A SUBCARRY can generate borrow only if LHS < RHS and this property will be
1412 // preserved by sign extension.
1413 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1414 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1415
1416 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
1417
1418 // Do the arithmetic in the wide type.
1419 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
1420 LHS, RHS, N->getOperand(2));
1421
1422 // Update the users of the original carry/borrow value.
1423 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1424
1425 return SDValue(Res.getNode(), 0);
1426}
1427
1428SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO_CARRY(SDNode *N,
1429 unsigned ResNo) {
1430 assert(ResNo == 1 && "Don't know how to promote other results yet.")(static_cast <bool> (ResNo == 1 && "Don't know how to promote other results yet."
) ? void (0) : __assert_fail ("ResNo == 1 && \"Don't know how to promote other results yet.\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1430
, __extension__ __PRETTY_FUNCTION__))
;
1431 return PromoteIntRes_Overflow(N);
1432}
1433
1434SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
1435 EVT OVT = N->getValueType(0);
1436 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), OVT);
1437
1438 // If a larger ABS or SMAX isn't supported by the target, try to expand now.
1439 // If we expand later we'll end up sign extending more than just the sra input
1440 // in sra+xor+sub expansion.
1441 if (!OVT.isVector() &&
1442 !TLI.isOperationLegalOrCustomOrPromote(ISD::ABS, NVT) &&
1443 !TLI.isOperationLegal(ISD::SMAX, NVT)) {
1444 if (SDValue Res = TLI.expandABS(N, DAG))
1445 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), NVT, Res);
1446 }
1447
1448 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
1449 return DAG.getNode(ISD::ABS, SDLoc(N), Op0.getValueType(), Op0);
1450}
1451
1452SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
1453 // Promote the overflow bit trivially.
1454 if (ResNo == 1)
1455 return PromoteIntRes_Overflow(N);
1456
1457 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
1458 SDLoc DL(N);
1459 EVT SmallVT = LHS.getValueType();
1460
1461 // To determine if the result overflowed in a larger type, we extend the
1462 // input to the larger type, do the multiply (checking if it overflows),
1463 // then also check the high bits of the result to see if overflow happened
1464 // there.
1465 if (N->getOpcode() == ISD::SMULO) {
1466 LHS = SExtPromotedInteger(LHS);
1467 RHS = SExtPromotedInteger(RHS);
1468 } else {
1469 LHS = ZExtPromotedInteger(LHS);
1470 RHS = ZExtPromotedInteger(RHS);
1471 }
1472 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
1473 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
1474
1475 // Overflow occurred if it occurred in the larger type, or if the high part
1476 // of the result does not zero/sign-extend the low part. Check this second
1477 // possibility first.
1478 SDValue Overflow;
1479 if (N->getOpcode() == ISD::UMULO) {
1480 // Unsigned overflow occurred if the high part is non-zero.
1481 unsigned Shift = SmallVT.getScalarSizeInBits();
1482 EVT ShiftTy = TLI.getShiftAmountTy(Mul.getValueType(), DAG.getDataLayout());
1483 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
1484 DAG.getConstant(Shift, DL, ShiftTy));
1485 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
1486 DAG.getConstant(0, DL, Hi.getValueType()),
1487 ISD::SETNE);
1488 } else {
1489 // Signed overflow occurred if the high part does not sign extend the low.
1490 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
1491 Mul, DAG.getValueType(SmallVT));
1492 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
1493 }
1494
1495 // The only other way for overflow to occur is if the multiplication in the
1496 // larger type itself overflowed.
1497 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
1498 SDValue(Mul.getNode(), 1));
1499
1500 // Use the calculated overflow everywhere.
1501 ReplaceValueWith(SDValue(N, 1), Overflow);
1502 return Mul;
1503}
1504
1505SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
1506 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1507 N->getValueType(0)));
1508}
1509
1510SDValue DAGTypeLegalizer::PromoteIntRes_VSCALE(SDNode *N) {
1511 EVT VT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1512
1513 APInt MulImm = cast<ConstantSDNode>(N->getOperand(0))->getAPIntValue();
1514 return DAG.getVScale(SDLoc(N), VT, MulImm.sextOrSelf(VT.getSizeInBits()));
1515}
1516
1517SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
1518 SDValue Chain = N->getOperand(0); // Get the chain.
1519 SDValue Ptr = N->getOperand(1); // Get the pointer.
1520 EVT VT = N->getValueType(0);
1521 SDLoc dl(N);
1522
1523 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
1524 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
1525 // The argument is passed as NumRegs registers of type RegVT.
1526
1527 SmallVector<SDValue, 8> Parts(NumRegs);
1528 for (unsigned i = 0; i < NumRegs; ++i) {
1529 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
1530 N->getConstantOperandVal(3));
1531 Chain = Parts[i].getValue(1);
1532 }
1533
1534 // Handle endianness of the load.
1535 if (DAG.getDataLayout().isBigEndian())
1536 std::reverse(Parts.begin(), Parts.end());
1537
1538 // Assemble the parts in the promoted type.
1539 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1540 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
1541 for (unsigned i = 1; i < NumRegs; ++i) {
1542 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
1543 // Shift it to the right position and "or" it in.
1544 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
1545 DAG.getConstant(i * RegVT.getSizeInBits(), dl,
1546 TLI.getPointerTy(DAG.getDataLayout())));
1547 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
1548 }
1549
1550 // Modified the chain result - switch anything that used the old chain to
1551 // use the new one.
1552 ReplaceValueWith(SDValue(N, 1), Chain);
1553
1554 return Res;
1555}
1556
1557//===----------------------------------------------------------------------===//
1558// Integer Operand Promotion
1559//===----------------------------------------------------------------------===//
1560
1561/// PromoteIntegerOperand - This method is called when the specified operand of
1562/// the specified node is found to need promotion. At this point, all of the
1563/// result types of the node are known to be legal, but other operands of the
1564/// node may need promotion or expansion as well as the specified one.
1565bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
1566 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Promote integer operand: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
1567 dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Promote integer operand: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
;
1568 SDValue Res = SDValue();
1569 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
1570 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Node has been custom lowered, done\n"
; } } while (false)
;
1571 return false;
1572 }
1573
1574 switch (N->getOpcode()) {
1575 default:
1576 #ifndef NDEBUG
1577 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
1578 N->dump(&DAG); dbgs() << "\n";
1579 #endif
1580 llvm_unreachable("Do not know how to promote this operator's operand!")::llvm::llvm_unreachable_internal("Do not know how to promote this operator's operand!"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1580
)
;
1581
1582 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
1583 case ISD::ATOMIC_STORE:
1584 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
1585 break;
1586 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
1587 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
1588 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
1589 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
1590 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
1591 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
1592 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
1593 case ISD::INSERT_VECTOR_ELT:
1594 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
1595 case ISD::SCALAR_TO_VECTOR:
1596 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
1597 case ISD::SPLAT_VECTOR:
1598 Res = PromoteIntOp_SPLAT_VECTOR(N); break;
1599 case ISD::VSELECT:
1600 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
1601 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
1602 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
1603 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
1604 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
1605 case ISD::STRICT_SINT_TO_FP: Res = PromoteIntOp_STRICT_SINT_TO_FP(N); break;
1606 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
1607 OpNo); break;
1608 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
1609 OpNo); break;
1610 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
1611 OpNo); break;
1612 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
1613 OpNo); break;
1614 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
1615 OpNo); break;
1616 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
1617 case ISD::FP16_TO_FP:
1618 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
1619 case ISD::STRICT_UINT_TO_FP: Res = PromoteIntOp_STRICT_UINT_TO_FP(N); break;
1620 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
1621 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
1622 case ISD::INSERT_SUBVECTOR: Res = PromoteIntOp_INSERT_SUBVECTOR(N); break;
1623
1624 case ISD::SHL:
1625 case ISD::SRA:
1626 case ISD::SRL:
1627 case ISD::ROTL:
1628 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
1629
1630 case ISD::SADDO_CARRY:
1631 case ISD::SSUBO_CARRY:
1632 case ISD::ADDCARRY:
1633 case ISD::SUBCARRY: Res = PromoteIntOp_ADDSUBCARRY(N, OpNo); break;
1634
1635 case ISD::FRAMEADDR:
1636 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
1637
1638 case ISD::PREFETCH: Res = PromoteIntOp_PREFETCH(N, OpNo); break;
1639
1640 case ISD::SMULFIX:
1641 case ISD::SMULFIXSAT:
1642 case ISD::UMULFIX:
1643 case ISD::UMULFIXSAT:
1644 case ISD::SDIVFIX:
1645 case ISD::SDIVFIXSAT:
1646 case ISD::UDIVFIX:
1647 case ISD::UDIVFIXSAT: Res = PromoteIntOp_FIX(N); break;
1648
1649 case ISD::FPOWI:
1650 case ISD::STRICT_FPOWI: Res = PromoteIntOp_FPOWI(N); break;
1651
1652 case ISD::VECREDUCE_ADD:
1653 case ISD::VECREDUCE_MUL:
1654 case ISD::VECREDUCE_AND:
1655 case ISD::VECREDUCE_OR:
1656 case ISD::VECREDUCE_XOR:
1657 case ISD::VECREDUCE_SMAX:
1658 case ISD::VECREDUCE_SMIN:
1659 case ISD::VECREDUCE_UMAX:
1660 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
1661 case ISD::VP_REDUCE_ADD:
1662 case ISD::VP_REDUCE_MUL:
1663 case ISD::VP_REDUCE_AND:
1664 case ISD::VP_REDUCE_OR:
1665 case ISD::VP_REDUCE_XOR:
1666 case ISD::VP_REDUCE_SMAX:
1667 case ISD::VP_REDUCE_SMIN:
1668 case ISD::VP_REDUCE_UMAX:
1669 case ISD::VP_REDUCE_UMIN:
1670 Res = PromoteIntOp_VP_REDUCE(N, OpNo);
1671 break;
1672
1673 case ISD::SET_ROUNDING: Res = PromoteIntOp_SET_ROUNDING(N); break;
1674 }
1675
1676 // If the result is null, the sub-method took care of registering results etc.
1677 if (!Res.getNode()) return false;
1678
1679 // If the result is N, the sub-method updated N in place. Tell the legalizer
1680 // core about this.
1681 if (Res.getNode() == N)
1682 return true;
1683
1684 const bool IsStrictFp = N->isStrictFPOpcode();
1685 assert(Res.getValueType() == N->getValueType(0) &&(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && N->getNumValues() == (IsStrictFp ? 2 : 1) &&
"Invalid operand expansion") ? void (0) : __assert_fail ("Res.getValueType() == N->getValueType(0) && N->getNumValues() == (IsStrictFp ? 2 : 1) && \"Invalid operand expansion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1687
, __extension__ __PRETTY_FUNCTION__))
1686 N->getNumValues() == (IsStrictFp ? 2 : 1) &&(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && N->getNumValues() == (IsStrictFp ? 2 : 1) &&
"Invalid operand expansion") ? void (0) : __assert_fail ("Res.getValueType() == N->getValueType(0) && N->getNumValues() == (IsStrictFp ? 2 : 1) && \"Invalid operand expansion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1687
, __extension__ __PRETTY_FUNCTION__))
1687 "Invalid operand expansion")(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && N->getNumValues() == (IsStrictFp ? 2 : 1) &&
"Invalid operand expansion") ? void (0) : __assert_fail ("Res.getValueType() == N->getValueType(0) && N->getNumValues() == (IsStrictFp ? 2 : 1) && \"Invalid operand expansion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1687
, __extension__ __PRETTY_FUNCTION__))
;
1688 LLVM_DEBUG(dbgs() << "Replacing: "; N->dump(&DAG); dbgs() << " with: ";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Replacing: "; N->dump
(&DAG); dbgs() << " with: "; Res.dump(); } } while
(false)
1689 Res.dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Replacing: "; N->dump
(&DAG); dbgs() << " with: "; Res.dump(); } } while
(false)
;
1690
1691 ReplaceValueWith(SDValue(N, 0), Res);
1692 if (IsStrictFp)
1693 ReplaceValueWith(SDValue(N, 1), SDValue(Res.getNode(), 1));
1694
1695 return false;
1696}
1697
1698/// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1699/// shared among BR_CC, SELECT_CC, and SETCC handlers.
1700void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &LHS, SDValue &RHS,
1701 ISD::CondCode CCCode) {
1702 // We have to insert explicit sign or zero extends. Note that we could
1703 // insert sign extends for ALL conditions. For those operations where either
1704 // zero or sign extension would be valid, we ask the target which extension
1705 // it would prefer.
1706
1707 // Signed comparisons always require sign extension.
1708 if (ISD::isSignedIntSetCC(CCCode)) {
1709 LHS = SExtPromotedInteger(LHS);
1710 RHS = SExtPromotedInteger(RHS);
1711 return;
1712 }
1713
1714 assert((ISD::isUnsignedIntSetCC(CCCode) || ISD::isIntEqualitySetCC(CCCode)) &&(static_cast <bool> ((ISD::isUnsignedIntSetCC(CCCode) ||
ISD::isIntEqualitySetCC(CCCode)) && "Unknown integer comparison!"
) ? void (0) : __assert_fail ("(ISD::isUnsignedIntSetCC(CCCode) || ISD::isIntEqualitySetCC(CCCode)) && \"Unknown integer comparison!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1715
, __extension__ __PRETTY_FUNCTION__))
1715 "Unknown integer comparison!")(static_cast <bool> ((ISD::isUnsignedIntSetCC(CCCode) ||
ISD::isIntEqualitySetCC(CCCode)) && "Unknown integer comparison!"
) ? void (0) : __assert_fail ("(ISD::isUnsignedIntSetCC(CCCode) || ISD::isIntEqualitySetCC(CCCode)) && \"Unknown integer comparison!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1715
, __extension__ __PRETTY_FUNCTION__))
;
1716
1717 SDValue OpL = GetPromotedInteger(LHS);
1718 SDValue OpR = GetPromotedInteger(RHS);
1719
1720 if (TLI.isSExtCheaperThanZExt(LHS.getValueType(), OpL.getValueType())) {
1721 // The target would prefer to promote the comparison operand with sign
1722 // extension. Honor that unless the promoted values are already zero
1723 // extended.
1724 unsigned OpLEffectiveBits =
1725 DAG.computeKnownBits(OpL).countMaxActiveBits();
1726 unsigned OpREffectiveBits =
1727 DAG.computeKnownBits(OpR).countMaxActiveBits();
1728 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
1729 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
1730 LHS = OpL;
1731 RHS = OpR;
1732 return;
1733 }
1734
1735 // The promoted values aren't zero extended, use a sext_inreg.
1736 LHS = SExtPromotedInteger(LHS);
1737 RHS = SExtPromotedInteger(RHS);
1738 return;
1739 }
1740
1741 // Prefer to promote the comparison operand with zero extension.
1742
1743 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
1744 // than the width of LHS/RHS, we can avoid/ inserting a zext_inreg operation
1745 // that we might not be able to remove.
1746 unsigned OpLEffectiveBits = DAG.ComputeMaxSignificantBits(OpL);
1747 unsigned OpREffectiveBits = DAG.ComputeMaxSignificantBits(OpR);
1748 if (OpLEffectiveBits <= LHS.getScalarValueSizeInBits() &&
1749 OpREffectiveBits <= RHS.getScalarValueSizeInBits()) {
1750 LHS = OpL;
1751 RHS = OpR;
1752 return;
1753 }
1754
1755 // Otherwise, use zext_inreg.
1756 LHS = ZExtPromotedInteger(LHS);
1757 RHS = ZExtPromotedInteger(RHS);
1758}
1759
1760SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
1761 SDValue Op = GetPromotedInteger(N->getOperand(0));
1762 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
1763}
1764
1765SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
1766 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
1767 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
1768 N->getChain(), N->getBasePtr(), Op2, N->getMemOperand());
1769}
1770
1771SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
1772 // This should only occur in unusual situations like bitcasting to an
1773 // x86_fp80, so just turn it into a store+load
1774 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1775}
1776
1777SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
1778 assert(OpNo == 2 && "Don't know how to promote this operand!")(static_cast <bool> (OpNo == 2 && "Don't know how to promote this operand!"
) ? void (0) : __assert_fail ("OpNo == 2 && \"Don't know how to promote this operand!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1778
, __extension__ __PRETTY_FUNCTION__))
;
1779
1780 SDValue LHS = N->getOperand(2);
1781 SDValue RHS = N->getOperand(3);
1782 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1783
1784 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1785 // legal types.
1786 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1787 N->getOperand(1), LHS, RHS, N->getOperand(4)),
1788 0);
1789}
1790
1791SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
1792 assert(OpNo == 1 && "only know how to promote condition")(static_cast <bool> (OpNo == 1 && "only know how to promote condition"
) ? void (0) : __assert_fail ("OpNo == 1 && \"only know how to promote condition\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1792
, __extension__ __PRETTY_FUNCTION__))
;
1793
1794 // Promote all the way up to the canonical SetCC type.
1795 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
1796
1797 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1798 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
1799 N->getOperand(2)), 0);
1800}
1801
1802SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
1803 // Since the result type is legal, the operands must promote to it.
1804 EVT OVT = N->getOperand(0).getValueType();
1805 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
1806 SDValue Hi = GetPromotedInteger(N->getOperand(1));
1807 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?")(static_cast <bool> (Lo.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Lo.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1807
, __extension__ __PRETTY_FUNCTION__))
;
1808 SDLoc dl(N);
1809
1810 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
1811 DAG.getConstant(OVT.getSizeInBits(), dl,
1812 TLI.getPointerTy(DAG.getDataLayout())));
1813 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
1814}
1815
1816SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
1817 // The vector type is legal but the element type is not. This implies
1818 // that the vector is a power-of-two in length and that the element
1819 // type does not have a strange size (eg: it is not i1).
1820 EVT VecVT = N->getValueType(0);
1821 unsigned NumElts = VecVT.getVectorNumElements();
1822 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&(static_cast <bool> (!((NumElts & 1) && (!TLI
.isTypeLegal(VecVT))) && "Legal vector of one illegal element?"
) ? void (0) : __assert_fail ("!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) && \"Legal vector of one illegal element?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1823
, __extension__ __PRETTY_FUNCTION__))
1823 "Legal vector of one illegal element?")(static_cast <bool> (!((NumElts & 1) && (!TLI
.isTypeLegal(VecVT))) && "Legal vector of one illegal element?"
) ? void (0) : __assert_fail ("!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) && \"Legal vector of one illegal element?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1823
, __extension__ __PRETTY_FUNCTION__))
;
1824
1825 // Promote the inserted value. The type does not need to match the
1826 // vector element type. Check that any extra bits introduced will be
1827 // truncated away.
1828 assert(N->getOperand(0).getValueSizeInBits() >=(static_cast <bool> (N->getOperand(0).getValueSizeInBits
() >= N->getValueType(0).getScalarSizeInBits() &&
"Type of inserted value narrower than vector element type!")
? void (0) : __assert_fail ("N->getOperand(0).getValueSizeInBits() >= N->getValueType(0).getScalarSizeInBits() && \"Type of inserted value narrower than vector element type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1830
, __extension__ __PRETTY_FUNCTION__))
1829 N->getValueType(0).getScalarSizeInBits() &&(static_cast <bool> (N->getOperand(0).getValueSizeInBits
() >= N->getValueType(0).getScalarSizeInBits() &&
"Type of inserted value narrower than vector element type!")
? void (0) : __assert_fail ("N->getOperand(0).getValueSizeInBits() >= N->getValueType(0).getScalarSizeInBits() && \"Type of inserted value narrower than vector element type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1830
, __extension__ __PRETTY_FUNCTION__))
1830 "Type of inserted value narrower than vector element type!")(static_cast <bool> (N->getOperand(0).getValueSizeInBits
() >= N->getValueType(0).getScalarSizeInBits() &&
"Type of inserted value narrower than vector element type!")
? void (0) : __assert_fail ("N->getOperand(0).getValueSizeInBits() >= N->getValueType(0).getScalarSizeInBits() && \"Type of inserted value narrower than vector element type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1830
, __extension__ __PRETTY_FUNCTION__))
;
1831
1832 SmallVector<SDValue, 16> NewOps;
1833 for (unsigned i = 0; i < NumElts; ++i)
1834 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
1835
1836 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1837}
1838
1839SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
1840 unsigned OpNo) {
1841 if (OpNo == 1) {
1842 // Promote the inserted value. This is valid because the type does not
1843 // have to match the vector element type.
1844
1845 // Check that any extra bits introduced will be truncated away.
1846 assert(N->getOperand(1).getValueSizeInBits() >=(static_cast <bool> (N->getOperand(1).getValueSizeInBits
() >= N->getValueType(0).getScalarSizeInBits() &&
"Type of inserted value narrower than vector element type!")
? void (0) : __assert_fail ("N->getOperand(1).getValueSizeInBits() >= N->getValueType(0).getScalarSizeInBits() && \"Type of inserted value narrower than vector element type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1848
, __extension__ __PRETTY_FUNCTION__))
1847 N->getValueType(0).getScalarSizeInBits() &&(static_cast <bool> (N->getOperand(1).getValueSizeInBits
() >= N->getValueType(0).getScalarSizeInBits() &&
"Type of inserted value narrower than vector element type!")
? void (0) : __assert_fail ("N->getOperand(1).getValueSizeInBits() >= N->getValueType(0).getScalarSizeInBits() && \"Type of inserted value narrower than vector element type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1848
, __extension__ __PRETTY_FUNCTION__))
1848 "Type of inserted value narrower than vector element type!")(static_cast <bool> (N->getOperand(1).getValueSizeInBits
() >= N->getValueType(0).getScalarSizeInBits() &&
"Type of inserted value narrower than vector element type!")
? void (0) : __assert_fail ("N->getOperand(1).getValueSizeInBits() >= N->getValueType(0).getScalarSizeInBits() && \"Type of inserted value narrower than vector element type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1848
, __extension__ __PRETTY_FUNCTION__))
;
1849 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1850 GetPromotedInteger(N->getOperand(1)),
1851 N->getOperand(2)),
1852 0);
1853 }
1854
1855 assert(OpNo == 2 && "Different operand and result vector types?")(static_cast <bool> (OpNo == 2 && "Different operand and result vector types?"
) ? void (0) : __assert_fail ("OpNo == 2 && \"Different operand and result vector types?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1855
, __extension__ __PRETTY_FUNCTION__))
;
1856
1857 // Promote the index.
1858 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
1859 TLI.getVectorIdxTy(DAG.getDataLayout()));
1860 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1861 N->getOperand(1), Idx), 0);
1862}
1863
1864SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
1865 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
1866 // the operand in place.
1867 return SDValue(DAG.UpdateNodeOperands(N,
1868 GetPromotedInteger(N->getOperand(0))), 0);
1869}
1870
1871SDValue DAGTypeLegalizer::PromoteIntOp_SPLAT_VECTOR(SDNode *N) {
1872 // Integer SPLAT_VECTOR operands are implicitly truncated, so just promote the
1873 // operand in place.
1874 return SDValue(
1875 DAG.UpdateNodeOperands(N, GetPromotedInteger(N->getOperand(0))), 0);
1876}
1877
1878SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
1879 assert(OpNo == 0 && "Only know how to promote the condition!")(static_cast <bool> (OpNo == 0 && "Only know how to promote the condition!"
) ? void (0) : __assert_fail ("OpNo == 0 && \"Only know how to promote the condition!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1879
, __extension__ __PRETTY_FUNCTION__))
;
1880 SDValue Cond = N->getOperand(0);
1881 EVT OpTy = N->getOperand(1).getValueType();
1882
1883 if (N->getOpcode() == ISD::VSELECT)
1884 if (SDValue Res = WidenVSELECTMask(N))
1885 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1886 Res, N->getOperand(1), N->getOperand(2));
1887
1888 // Promote all the way up to the canonical SetCC type.
1889 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
1890 Cond = PromoteTargetBoolean(Cond, OpVT);
1891
1892 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
1893 N->getOperand(2)), 0);
1894}
1895
1896SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1897 assert(OpNo == 0 && "Don't know how to promote this operand!")(static_cast <bool> (OpNo == 0 && "Don't know how to promote this operand!"
) ? void (0) : __assert_fail ("OpNo == 0 && \"Don't know how to promote this operand!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1897
, __extension__ __PRETTY_FUNCTION__))
;
1898
1899 SDValue LHS = N->getOperand(0);
1900 SDValue RHS = N->getOperand(1);
1901 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
1902
1903 // The CC (#4) and the possible return values (#2 and #3) have legal types.
1904 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
1905 N->getOperand(3), N->getOperand(4)), 0);
1906}
1907
1908SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
1909 assert(OpNo == 0 && "Don't know how to promote this operand!")(static_cast <bool> (OpNo == 0 && "Don't know how to promote this operand!"
) ? void (0) : __assert_fail ("OpNo == 0 && \"Don't know how to promote this operand!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1909
, __extension__ __PRETTY_FUNCTION__))
;
1910
1911 SDValue LHS = N->getOperand(0);
1912 SDValue RHS = N->getOperand(1);
1913 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
1914
1915 // The CC (#2) is always legal.
1916 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
1917}
1918
1919SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
1920 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1921 ZExtPromotedInteger(N->getOperand(1))), 0);
1922}
1923
1924SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
1925 SDValue Op = GetPromotedInteger(N->getOperand(0));
1926 SDLoc dl(N);
1927 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1928 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
1929 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1930}
1931
1932SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
1933 return SDValue(DAG.UpdateNodeOperands(N,
1934 SExtPromotedInteger(N->getOperand(0))), 0);
1935}
1936
1937SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_SINT_TO_FP(SDNode *N) {
1938 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1939 SExtPromotedInteger(N->getOperand(1))), 0);
1940}
1941
1942SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
1943 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!")(static_cast <bool> (ISD::isUNINDEXEDStore(N) &&
"Indexed store during type legalization!") ? void (0) : __assert_fail
("ISD::isUNINDEXEDStore(N) && \"Indexed store during type legalization!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1943
, __extension__ __PRETTY_FUNCTION__))
;
1944 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
1945 SDLoc dl(N);
1946
1947 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
1948
1949 // Truncate the value and store the result.
1950 return DAG.getTruncStore(Ch, dl, Val, Ptr,
1951 N->getMemoryVT(), N->getMemOperand());
1952}
1953
1954SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
1955 unsigned OpNo) {
1956 SDValue DataOp = N->getValue();
1957 SDValue Mask = N->getMask();
1958
1959 if (OpNo == 4) {
1960 // The Mask. Update in place.
1961 EVT DataVT = DataOp.getValueType();
1962 Mask = PromoteTargetBoolean(Mask, DataVT);
1963 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1964 NewOps[4] = Mask;
1965 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1966 }
1967
1968 assert(OpNo == 1 && "Unexpected operand for promotion")(static_cast <bool> (OpNo == 1 && "Unexpected operand for promotion"
) ? void (0) : __assert_fail ("OpNo == 1 && \"Unexpected operand for promotion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1968
, __extension__ __PRETTY_FUNCTION__))
;
1969 DataOp = GetPromotedInteger(DataOp);
1970
1971 return DAG.getMaskedStore(N->getChain(), SDLoc(N), DataOp, N->getBasePtr(),
1972 N->getOffset(), Mask, N->getMemoryVT(),
1973 N->getMemOperand(), N->getAddressingMode(),
1974 /*IsTruncating*/ true, N->isCompressingStore());
1975}
1976
1977SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
1978 unsigned OpNo) {
1979 assert(OpNo == 3 && "Only know how to promote the mask!")(static_cast <bool> (OpNo == 3 && "Only know how to promote the mask!"
) ? void (0) : __assert_fail ("OpNo == 3 && \"Only know how to promote the mask!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 1979
, __extension__ __PRETTY_FUNCTION__))
;
1980 EVT DataVT = N->getValueType(0);
1981 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1982 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1983 NewOps[OpNo] = Mask;
1984 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
1985 if (Res == N)
1986 return SDValue(Res, 0);
1987
1988 // Update triggered CSE, do our own replacement since caller can't.
1989 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
1990 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
1991 return SDValue();
1992}
1993
1994SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
1995 unsigned OpNo) {
1996 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1997
1998 if (OpNo == 2) {
1999 // The Mask
2000 EVT DataVT = N->getValueType(0);
2001 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2002 } else if (OpNo == 4) {
2003 // The Index
2004 if (N->isIndexSigned())
2005 // Need to sign extend the index since the bits will likely be used.
2006 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2007 else
2008 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2009 } else
2010 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2011
2012 SDNode *Res = DAG.UpdateNodeOperands(N, NewOps);
2013 if (Res == N)
2014 return SDValue(Res, 0);
2015
2016 // Update triggered CSE, do our own replacement since caller can't.
2017 ReplaceValueWith(SDValue(N, 0), SDValue(Res, 0));
2018 ReplaceValueWith(SDValue(N, 1), SDValue(Res, 1));
2019 return SDValue();
2020}
2021
2022SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
2023 unsigned OpNo) {
2024 bool TruncateStore = N->isTruncatingStore();
2025 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
2026
2027 if (OpNo == 2) {
2028 // The Mask
2029 EVT DataVT = N->getValue().getValueType();
2030 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
2031 } else if (OpNo == 4) {
2032 // The Index
2033 if (N->isIndexSigned())
2034 // Need to sign extend the index since the bits will likely be used.
2035 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
2036 else
2037 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
2038 } else {
2039 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
2040 TruncateStore = true;
2041 }
2042
2043 return DAG.getMaskedScatter(DAG.getVTList(MVT::Other), N->getMemoryVT(),
2044 SDLoc(N), NewOps, N->getMemOperand(),
2045 N->getIndexType(), TruncateStore);
2046}
2047
2048SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
2049 SDValue Op = GetPromotedInteger(N->getOperand(0));
2050 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
2051}
2052
2053SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
2054 return SDValue(DAG.UpdateNodeOperands(N,
2055 ZExtPromotedInteger(N->getOperand(0))), 0);
2056}
2057
2058SDValue DAGTypeLegalizer::PromoteIntOp_STRICT_UINT_TO_FP(SDNode *N) {
2059 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
2060 ZExtPromotedInteger(N->getOperand(1))), 0);
2061}
2062
2063SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
2064 SDLoc dl(N);
2065 SDValue Op = GetPromotedInteger(N->getOperand(0));
2066 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
2067 return DAG.getZeroExtendInReg(Op, dl, N->getOperand(0).getValueType());
2068}
2069
2070SDValue DAGTypeLegalizer::PromoteIntOp_ADDSUBCARRY(SDNode *N, unsigned OpNo) {
2071 assert(OpNo == 2 && "Don't know how to promote this operand!")(static_cast <bool> (OpNo == 2 && "Don't know how to promote this operand!"
) ? void (0) : __assert_fail ("OpNo == 2 && \"Don't know how to promote this operand!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2071
, __extension__ __PRETTY_FUNCTION__))
;
2072
2073 SDValue LHS = N->getOperand(0);
2074 SDValue RHS = N->getOperand(1);
2075 SDValue Carry = N->getOperand(2);
2076 SDLoc DL(N);
2077
2078 Carry = PromoteTargetBoolean(Carry, LHS.getValueType());
2079
2080 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, Carry), 0);
2081}
2082
2083SDValue DAGTypeLegalizer::PromoteIntOp_FIX(SDNode *N) {
2084 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
2085 return SDValue(
2086 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
2087}
2088
2089SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
2090 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
2091 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
2092 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
2093}
2094
2095SDValue DAGTypeLegalizer::PromoteIntOp_PREFETCH(SDNode *N, unsigned OpNo) {
2096 assert(OpNo > 1 && "Don't know how to promote this operand!")(static_cast <bool> (OpNo > 1 && "Don't know how to promote this operand!"
) ? void (0) : __assert_fail ("OpNo > 1 && \"Don't know how to promote this operand!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2096
, __extension__ __PRETTY_FUNCTION__))
;
2097 // Promote the rw, locality, and cache type arguments to a supported integer
2098 // width.
2099 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
2100 SDValue Op3 = ZExtPromotedInteger(N->getOperand(3));
2101 SDValue Op4 = ZExtPromotedInteger(N->getOperand(4));
2102 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
2103 Op2, Op3, Op4),
2104 0);
2105}
2106
2107SDValue DAGTypeLegalizer::PromoteIntOp_FPOWI(SDNode *N) {
2108 bool IsStrict = N->isStrictFPOpcode();
2109 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
2110
2111 // The integer operand is the last operand in FPOWI (so the result and
2112 // floating point operand is already type legalized).
2113
2114 // We can't just promote the exponent type in FPOWI, since we want to lower
2115 // the node to a libcall and we if we promote to a type larger than
2116 // sizeof(int) the libcall might not be according to the targets ABI. Instead
2117 // we rewrite to a libcall here directly, letting makeLibCall handle promotion
2118 // if the target accepts it according to shouldSignExtendTypeInLibCall.
2119 RTLIB::Libcall LC = RTLIB::getPOWI(N->getValueType(0));
2120 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected fpowi.") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected fpowi.\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2120
, __extension__ __PRETTY_FUNCTION__))
;
2121 if (!TLI.getLibcallName(LC)) {
2122 // Some targets don't have a powi libcall; use pow instead.
2123 // FIXME: Implement this if some target needs it.
2124 DAG.getContext()->emitError("Don't know how to promote fpowi to fpow");
2125 return DAG.getUNDEF(N->getValueType(0));
2126 }
2127 unsigned OpOffset = IsStrict ? 1 : 0;
2128 // The exponent should fit in a sizeof(int) type for the libcall to be valid.
2129 assert(DAG.getLibInfo().getIntSize() ==(static_cast <bool> (DAG.getLibInfo().getIntSize() == N
->getOperand(1 + OpOffset).getValueType().getSizeInBits() &&
"POWI exponent should match with sizeof(int) when doing the libcall."
) ? void (0) : __assert_fail ("DAG.getLibInfo().getIntSize() == N->getOperand(1 + OpOffset).getValueType().getSizeInBits() && \"POWI exponent should match with sizeof(int) when doing the libcall.\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2131
, __extension__ __PRETTY_FUNCTION__))
2130 N->getOperand(1 + OpOffset).getValueType().getSizeInBits() &&(static_cast <bool> (DAG.getLibInfo().getIntSize() == N
->getOperand(1 + OpOffset).getValueType().getSizeInBits() &&
"POWI exponent should match with sizeof(int) when doing the libcall."
) ? void (0) : __assert_fail ("DAG.getLibInfo().getIntSize() == N->getOperand(1 + OpOffset).getValueType().getSizeInBits() && \"POWI exponent should match with sizeof(int) when doing the libcall.\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2131
, __extension__ __PRETTY_FUNCTION__))
2131 "POWI exponent should match with sizeof(int) when doing the libcall.")(static_cast <bool> (DAG.getLibInfo().getIntSize() == N
->getOperand(1 + OpOffset).getValueType().getSizeInBits() &&
"POWI exponent should match with sizeof(int) when doing the libcall."
) ? void (0) : __assert_fail ("DAG.getLibInfo().getIntSize() == N->getOperand(1 + OpOffset).getValueType().getSizeInBits() && \"POWI exponent should match with sizeof(int) when doing the libcall.\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2131
, __extension__ __PRETTY_FUNCTION__))
;
2132 TargetLowering::MakeLibCallOptions CallOptions;
2133 CallOptions.setSExt(true);
2134 SDValue Ops[2] = {N->getOperand(0 + OpOffset), N->getOperand(1 + OpOffset)};
2135 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
2136 DAG, LC, N->getValueType(0), Ops, CallOptions, SDLoc(N), Chain);
2137 ReplaceValueWith(SDValue(N, 0), Tmp.first);
2138 if (IsStrict)
2139 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2140 return SDValue();
2141}
2142
2143static unsigned getExtendForIntVecReduction(SDNode *N) {
2144 switch (N->getOpcode()) {
2145 default:
2146 llvm_unreachable("Expected integer vector reduction")::llvm::llvm_unreachable_internal("Expected integer vector reduction"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2146
)
;
2147 case ISD::VECREDUCE_ADD:
2148 case ISD::VECREDUCE_MUL:
2149 case ISD::VECREDUCE_AND:
2150 case ISD::VECREDUCE_OR:
2151 case ISD::VECREDUCE_XOR:
2152 case ISD::VP_REDUCE_ADD:
2153 case ISD::VP_REDUCE_MUL:
2154 case ISD::VP_REDUCE_AND:
2155 case ISD::VP_REDUCE_OR:
2156 case ISD::VP_REDUCE_XOR:
2157 return ISD::ANY_EXTEND;
2158 case ISD::VECREDUCE_SMAX:
2159 case ISD::VECREDUCE_SMIN:
2160 case ISD::VP_REDUCE_SMAX:
2161 case ISD::VP_REDUCE_SMIN:
2162 return ISD::SIGN_EXTEND;
2163 case ISD::VECREDUCE_UMAX:
2164 case ISD::VECREDUCE_UMIN:
2165 case ISD::VP_REDUCE_UMAX:
2166 case ISD::VP_REDUCE_UMIN:
2167 return ISD::ZERO_EXTEND;
2168 }
2169}
2170
2171SDValue DAGTypeLegalizer::PromoteIntOpVectorReduction(SDNode *N, SDValue V) {
2172 switch (getExtendForIntVecReduction(N)) {
2173 default:
2174 llvm_unreachable("Impossible extension kind for integer reduction")::llvm::llvm_unreachable_internal("Impossible extension kind for integer reduction"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2174
)
;
2175 case ISD::ANY_EXTEND:
2176 return GetPromotedInteger(V);
2177 case ISD::SIGN_EXTEND:
2178 return SExtPromotedInteger(V);
2179 case ISD::ZERO_EXTEND:
2180 return ZExtPromotedInteger(V);
2181 }
2182}
2183
2184SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
2185 SDLoc dl(N);
2186 SDValue Op = PromoteIntOpVectorReduction(N, N->getOperand(0));
2187
2188 EVT EltVT = Op.getValueType().getVectorElementType();
2189 EVT VT = N->getValueType(0);
2190
2191 if (VT.bitsGE(EltVT))
2192 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, Op);
2193
2194 // Result size must be >= element size. If this is not the case after
2195 // promotion, also promote the result type and then truncate.
2196 SDValue Reduce = DAG.getNode(N->getOpcode(), dl, EltVT, Op);
2197 return DAG.getNode(ISD::TRUNCATE, dl, VT, Reduce);
2198}
2199
2200SDValue DAGTypeLegalizer::PromoteIntOp_VP_REDUCE(SDNode *N, unsigned OpNo) {
2201 SDLoc DL(N);
2202 SDValue Op = N->getOperand(OpNo);
2203 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
2204
2205 if (OpNo == 2) { // Mask
2206 // Update in place.
2207 NewOps[2] = PromoteTargetBoolean(Op, N->getOperand(1).getValueType());
2208 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
2209 }
2210
2211 assert(OpNo == 1 && "Unexpected operand for promotion")(static_cast <bool> (OpNo == 1 && "Unexpected operand for promotion"
) ? void (0) : __assert_fail ("OpNo == 1 && \"Unexpected operand for promotion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2211
, __extension__ __PRETTY_FUNCTION__))
;
2212
2213 Op = PromoteIntOpVectorReduction(N, Op);
2214
2215 NewOps[OpNo] = Op;
2216
2217 EVT VT = N->getValueType(0);
2218 EVT EltVT = Op.getValueType().getScalarType();
2219
2220 if (VT.bitsGE(EltVT))
2221 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, NewOps);
2222
2223 // Result size must be >= element/start-value size. If this is not the case
2224 // after promotion, also promote both the start value and result type and
2225 // then truncate.
2226 NewOps[0] =
2227 DAG.getNode(getExtendForIntVecReduction(N), DL, EltVT, N->getOperand(0));
2228 SDValue Reduce = DAG.getNode(N->getOpcode(), DL, EltVT, NewOps);
2229 return DAG.getNode(ISD::TRUNCATE, DL, VT, Reduce);
2230}
2231
2232SDValue DAGTypeLegalizer::PromoteIntOp_SET_ROUNDING(SDNode *N) {
2233 SDValue Op = ZExtPromotedInteger(N->getOperand(1));
2234 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
2235}
2236
2237//===----------------------------------------------------------------------===//
2238// Integer Result Expansion
2239//===----------------------------------------------------------------------===//
2240
2241/// ExpandIntegerResult - This method is called when the specified result of the
2242/// specified node is found to need expansion. At this point, the node may also
2243/// have invalid operands or may have other results that need promotion, we just
2244/// know that (at least) one result needs expansion.
2245void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
2246 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Expand integer result: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
1
Assuming 'DebugFlag' is false
2
Loop condition is false. Exiting loop
2247 dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Expand integer result: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
;
2248 SDValue Lo, Hi;
2249 Lo = Hi = SDValue();
2250
2251 // See if the target wants to custom expand this node.
2252 if (CustomLowerNode(N, N->getValueType(ResNo), true))
3
Assuming the condition is false
4
Taking false branch
2253 return;
2254
2255 switch (N->getOpcode()) {
5
Control jumps to 'case SHL:' at line 2376
2256 default:
2257#ifndef NDEBUG
2258 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
2259 N->dump(&DAG); dbgs() << "\n";
2260#endif
2261 report_fatal_error("Do not know how to expand the result of this "
2262 "operator!");
2263
2264 case ISD::ARITH_FENCE: SplitRes_ARITH_FENCE(N, Lo, Hi); break;
2265 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
2266 case ISD::SELECT: SplitRes_Select(N, Lo, Hi); break;
2267 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
2268 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
2269 case ISD::FREEZE: SplitRes_FREEZE(N, Lo, Hi); break;
2270
2271 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
2272 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
2273 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
2274 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
2275 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
2276
2277 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
2278 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
2279 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
2280 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
2281 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
2282 case ISD::PARITY: ExpandIntRes_PARITY(N, Lo, Hi); break;
2283 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
2284 case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
2285 case ISD::CTLZ_ZERO_UNDEF:
2286 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
2287 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
2288 case ISD::CTTZ_ZERO_UNDEF:
2289 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
2290 case ISD::FLT_ROUNDS_: ExpandIntRes_FLT_ROUNDS(N, Lo, Hi); break;
2291 case ISD::STRICT_FP_TO_SINT:
2292 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
2293 case ISD::STRICT_FP_TO_UINT:
2294 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
2295 case ISD::FP_TO_SINT_SAT:
2296 case ISD::FP_TO_UINT_SAT: ExpandIntRes_FP_TO_XINT_SAT(N, Lo, Hi); break;
2297 case ISD::STRICT_LLROUND:
2298 case ISD::STRICT_LLRINT:
2299 case ISD::LLROUND:
2300 case ISD::LLRINT: ExpandIntRes_LLROUND_LLRINT(N, Lo, Hi); break;
2301 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
2302 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
2303 case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
2304 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
2305 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
2306 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
2307 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
2308 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
2309 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
2310 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
2311 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
2312 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
2313
2314 case ISD::ATOMIC_LOAD_ADD:
2315 case ISD::ATOMIC_LOAD_SUB:
2316 case ISD::ATOMIC_LOAD_AND:
2317 case ISD::ATOMIC_LOAD_CLR:
2318 case ISD::ATOMIC_LOAD_OR:
2319 case ISD::ATOMIC_LOAD_XOR:
2320 case ISD::ATOMIC_LOAD_NAND:
2321 case ISD::ATOMIC_LOAD_MIN:
2322 case ISD::ATOMIC_LOAD_MAX:
2323 case ISD::ATOMIC_LOAD_UMIN:
2324 case ISD::ATOMIC_LOAD_UMAX:
2325 case ISD::ATOMIC_SWAP:
2326 case ISD::ATOMIC_CMP_SWAP: {
2327 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
2328 SplitInteger(Tmp.first, Lo, Hi);
2329 ReplaceValueWith(SDValue(N, 1), Tmp.second);
2330 break;
2331 }
2332 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
2333 AtomicSDNode *AN = cast<AtomicSDNode>(N);
2334 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
2335 SDValue Tmp = DAG.getAtomicCmpSwap(
2336 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
2337 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
2338 AN->getMemOperand());
2339
2340 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
2341 // success simply by comparing the loaded value against the ingoing
2342 // comparison.
2343 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
2344 N->getOperand(2), ISD::SETEQ);
2345
2346 SplitInteger(Tmp, Lo, Hi);
2347 ReplaceValueWith(SDValue(N, 1), Success);
2348 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
2349 break;
2350 }
2351
2352 case ISD::AND:
2353 case ISD::OR:
2354 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
2355
2356 case ISD::UMAX:
2357 case ISD::SMAX:
2358 case ISD::UMIN:
2359 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
2360
2361 case ISD::ADD:
2362 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
2363
2364 case ISD::ADDC:
2365 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
2366
2367 case ISD::ADDE:
2368 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
2369
2370 case ISD::ADDCARRY:
2371 case ISD::SUBCARRY: ExpandIntRes_ADDSUBCARRY(N, Lo, Hi); break;
2372
2373 case ISD::SADDO_CARRY:
2374 case ISD::SSUBO_CARRY: ExpandIntRes_SADDSUBO_CARRY(N, Lo, Hi); break;
2375
2376 case ISD::SHL:
2377 case ISD::SRA:
2378 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
6
Calling 'DAGTypeLegalizer::ExpandIntRes_Shift'
2379
2380 case ISD::SADDO:
2381 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
2382 case ISD::UADDO:
2383 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
2384 case ISD::UMULO:
2385 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
2386
2387 case ISD::SADDSAT:
2388 case ISD::UADDSAT:
2389 case ISD::SSUBSAT:
2390 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
2391
2392 case ISD::SSHLSAT:
2393 case ISD::USHLSAT: ExpandIntRes_SHLSAT(N, Lo, Hi); break;
2394
2395 case ISD::SMULFIX:
2396 case ISD::SMULFIXSAT:
2397 case ISD::UMULFIX:
2398 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
2399
2400 case ISD::SDIVFIX:
2401 case ISD::SDIVFIXSAT:
2402 case ISD::UDIVFIX:
2403 case ISD::UDIVFIXSAT: ExpandIntRes_DIVFIX(N, Lo, Hi); break;
2404
2405 case ISD::VECREDUCE_ADD:
2406 case ISD::VECREDUCE_MUL:
2407 case ISD::VECREDUCE_AND:
2408 case ISD::VECREDUCE_OR:
2409 case ISD::VECREDUCE_XOR:
2410 case ISD::VECREDUCE_SMAX:
2411 case ISD::VECREDUCE_SMIN:
2412 case ISD::VECREDUCE_UMAX:
2413 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
2414
2415 case ISD::ROTL:
2416 case ISD::ROTR:
2417 ExpandIntRes_Rotate(N, Lo, Hi);
2418 break;
2419
2420 case ISD::FSHL:
2421 case ISD::FSHR:
2422 ExpandIntRes_FunnelShift(N, Lo, Hi);
2423 break;
2424
2425 case ISD::VSCALE:
2426 ExpandIntRes_VSCALE(N, Lo, Hi);
2427 break;
2428 }
2429
2430 // If Lo/Hi is null, the sub-method took care of registering results etc.
2431 if (Lo.getNode())
2432 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
2433}
2434
2435/// Lower an atomic node to the appropriate builtin call.
2436std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
2437 unsigned Opc = Node->getOpcode();
2438 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
2439 AtomicOrdering order = cast<AtomicSDNode>(Node)->getMergedOrdering();
2440 // Lower to outline atomic libcall if outline atomics enabled,
2441 // or to sync libcall otherwise
2442 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, order, VT);
2443 EVT RetVT = Node->getValueType(0);
2444 TargetLowering::MakeLibCallOptions CallOptions;
2445 SmallVector<SDValue, 4> Ops;
2446 if (TLI.getLibcallName(LC)) {
2447 Ops.append(Node->op_begin() + 2, Node->op_end());
2448 Ops.push_back(Node->getOperand(1));
2449 } else {
2450 LC = RTLIB::getSYNC(Opc, VT);
2451 assert(LC != RTLIB::UNKNOWN_LIBCALL &&(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected atomic op or value type!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected atomic op or value type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2452
, __extension__ __PRETTY_FUNCTION__))
2452 "Unexpected atomic op or value type!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected atomic op or value type!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected atomic op or value type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2452
, __extension__ __PRETTY_FUNCTION__))
;
2453 Ops.append(Node->op_begin() + 1, Node->op_end());
2454 }
2455 return TLI.makeLibCall(DAG, LC, RetVT, Ops, CallOptions, SDLoc(Node),
2456 Node->getOperand(0));
2457}
2458
2459/// N is a shift by a value that needs to be expanded,
2460/// and the shift amount is a constant 'Amt'. Expand the operation.
2461void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
2462 SDValue &Lo, SDValue &Hi) {
2463 SDLoc DL(N);
2464 // Expand the incoming operand to be shifted, so that we have its parts
2465 SDValue InL, InH;
2466 GetExpandedInteger(N->getOperand(0), InL, InH);
2467
2468 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
2469 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
2470 if (!Amt) {
2471 Lo = InL;
2472 Hi = InH;
2473 return;
2474 }
2475
2476 EVT NVT = InL.getValueType();
2477 unsigned VTBits = N->getValueType(0).getSizeInBits();
2478 unsigned NVTBits = NVT.getSizeInBits();
2479 EVT ShTy = N->getOperand(1).getValueType();
2480
2481 if (N->getOpcode() == ISD::SHL) {
2482 if (Amt.uge(VTBits)) {
2483 Lo = Hi = DAG.getConstant(0, DL, NVT);
2484 } else if (Amt.ugt(NVTBits)) {
2485 Lo = DAG.getConstant(0, DL, NVT);
2486 Hi = DAG.getNode(ISD::SHL, DL,
2487 NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
2488 } else if (Amt == NVTBits) {
2489 Lo = DAG.getConstant(0, DL, NVT);
2490 Hi = InL;
2491 } else {
2492 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
2493 Hi = DAG.getNode(ISD::OR, DL, NVT,
2494 DAG.getNode(ISD::SHL, DL, NVT, InH,
2495 DAG.getConstant(Amt, DL, ShTy)),
2496 DAG.getNode(ISD::SRL, DL, NVT, InL,
2497 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2498 }
2499 return;
2500 }
2501
2502 if (N->getOpcode() == ISD::SRL) {
2503 if (Amt.uge(VTBits)) {
2504 Lo = Hi = DAG.getConstant(0, DL, NVT);
2505 } else if (Amt.ugt(NVTBits)) {
2506 Lo = DAG.getNode(ISD::SRL, DL,
2507 NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
2508 Hi = DAG.getConstant(0, DL, NVT);
2509 } else if (Amt == NVTBits) {
2510 Lo = InH;
2511 Hi = DAG.getConstant(0, DL, NVT);
2512 } else {
2513 Lo = DAG.getNode(ISD::OR, DL, NVT,
2514 DAG.getNode(ISD::SRL, DL, NVT, InL,
2515 DAG.getConstant(Amt, DL, ShTy)),
2516 DAG.getNode(ISD::SHL, DL, NVT, InH,
2517 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2518 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
2519 }
2520 return;
2521 }
2522
2523 assert(N->getOpcode() == ISD::SRA && "Unknown shift!")(static_cast <bool> (N->getOpcode() == ISD::SRA &&
"Unknown shift!") ? void (0) : __assert_fail ("N->getOpcode() == ISD::SRA && \"Unknown shift!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2523
, __extension__ __PRETTY_FUNCTION__))
;
2524 if (Amt.uge(VTBits)) {
2525 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
2526 DAG.getConstant(NVTBits - 1, DL, ShTy));
2527 } else if (Amt.ugt(NVTBits)) {
2528 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
2529 DAG.getConstant(Amt - NVTBits, DL, ShTy));
2530 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
2531 DAG.getConstant(NVTBits - 1, DL, ShTy));
2532 } else if (Amt == NVTBits) {
2533 Lo = InH;
2534 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
2535 DAG.getConstant(NVTBits - 1, DL, ShTy));
2536 } else {
2537 Lo = DAG.getNode(ISD::OR, DL, NVT,
2538 DAG.getNode(ISD::SRL, DL, NVT, InL,
2539 DAG.getConstant(Amt, DL, ShTy)),
2540 DAG.getNode(ISD::SHL, DL, NVT, InH,
2541 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
2542 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
2543 }
2544}
2545
2546/// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
2547/// this shift based on knowledge of the high bit of the shift amount. If we
2548/// can tell this, we know that it is >= 32 or < 32, without knowing the actual
2549/// shift amount.
2550bool DAGTypeLegalizer::
2551ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2552 SDValue Amt = N->getOperand(1);
10
Value assigned to 'Amt.Node'
2553 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2554 EVT ShTy = Amt.getValueType();
11
Calling 'SDValue::getValueType'
2555 unsigned ShBits = ShTy.getScalarSizeInBits();
2556 unsigned NVTBits = NVT.getScalarSizeInBits();
2557 assert(isPowerOf2_32(NVTBits) &&(static_cast <bool> (isPowerOf2_32(NVTBits) && "Expanded integer type size not a power of two!"
) ? void (0) : __assert_fail ("isPowerOf2_32(NVTBits) && \"Expanded integer type size not a power of two!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2558
, __extension__ __PRETTY_FUNCTION__))
2558 "Expanded integer type size not a power of two!")(static_cast <bool> (isPowerOf2_32(NVTBits) && "Expanded integer type size not a power of two!"
) ? void (0) : __assert_fail ("isPowerOf2_32(NVTBits) && \"Expanded integer type size not a power of two!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2558
, __extension__ __PRETTY_FUNCTION__))
;
2559 SDLoc dl(N);
2560
2561 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
2562 KnownBits Known = DAG.computeKnownBits(N->getOperand(1));
2563
2564 // If we don't know anything about the high bits, exit.
2565 if (((Known.Zero|Known.One) & HighBitMask) == 0)
2566 return false;
2567
2568 // Get the incoming operand to be shifted.
2569 SDValue InL, InH;
2570 GetExpandedInteger(N->getOperand(0), InL, InH);
2571
2572 // If we know that any of the high bits of the shift amount are one, then we
2573 // can do this as a couple of simple shifts.
2574 if (Known.One.intersects(HighBitMask)) {
2575 // Mask out the high bit, which we know is set.
2576 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
2577 DAG.getConstant(~HighBitMask, dl, ShTy));
2578
2579 switch (N->getOpcode()) {
2580 default: llvm_unreachable("Unknown shift")::llvm::llvm_unreachable_internal("Unknown shift", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 2580)
;
2581 case ISD::SHL:
2582 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
2583 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
2584 return true;
2585 case ISD::SRL:
2586 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2587 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
2588 return true;
2589 case ISD::SRA:
2590 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
2591 DAG.getConstant(NVTBits - 1, dl, ShTy));
2592 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
2593 return true;
2594 }
2595 }
2596
2597 // If we know that all of the high bits of the shift amount are zero, then we
2598 // can do this as a couple of simple shifts.
2599 if (HighBitMask.isSubsetOf(Known.Zero)) {
2600 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
2601 // shift if x is zero. We can use XOR here because x is known to be smaller
2602 // than 32.
2603 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
2604 DAG.getConstant(NVTBits - 1, dl, ShTy));
2605
2606 unsigned Op1, Op2;
2607 switch (N->getOpcode()) {
2608 default: llvm_unreachable("Unknown shift")::llvm::llvm_unreachable_internal("Unknown shift", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 2608)
;
2609 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
2610 case ISD::SRL:
2611 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
2612 }
2613
2614 // When shifting right the arithmetic for Lo and Hi is swapped.
2615 if (N->getOpcode() != ISD::SHL)
2616 std::swap(InL, InH);
2617
2618 // Use a little trick to get the bits that move from Lo to Hi. First
2619 // shift by one bit.
2620 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
2621 // Then compute the remaining shift with amount-1.
2622 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
2623
2624 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
2625 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
2626
2627 if (N->getOpcode() != ISD::SHL)
2628 std::swap(Hi, Lo);
2629 return true;
2630 }
2631
2632 return false;
2633}
2634
2635/// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
2636/// of any size.
2637bool DAGTypeLegalizer::
2638ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2639 SDValue Amt = N->getOperand(1);
2640 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2641 EVT ShTy = Amt.getValueType();
2642 unsigned NVTBits = NVT.getSizeInBits();
2643 assert(isPowerOf2_32(NVTBits) &&(static_cast <bool> (isPowerOf2_32(NVTBits) && "Expanded integer type size not a power of two!"
) ? void (0) : __assert_fail ("isPowerOf2_32(NVTBits) && \"Expanded integer type size not a power of two!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2644
, __extension__ __PRETTY_FUNCTION__))
2644 "Expanded integer type size not a power of two!")(static_cast <bool> (isPowerOf2_32(NVTBits) && "Expanded integer type size not a power of two!"
) ? void (0) : __assert_fail ("isPowerOf2_32(NVTBits) && \"Expanded integer type size not a power of two!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2644
, __extension__ __PRETTY_FUNCTION__))
;
2645 SDLoc dl(N);
2646
2647 // Get the incoming operand to be shifted.
2648 SDValue InL, InH;
2649 GetExpandedInteger(N->getOperand(0), InL, InH);
2650
2651 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
2652 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
2653 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
2654 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2655 Amt, NVBitsNode, ISD::SETULT);
2656 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2657 Amt, DAG.getConstant(0, dl, ShTy),
2658 ISD::SETEQ);
2659
2660 SDValue LoS, HiS, LoL, HiL;
2661 switch (N->getOpcode()) {
2662 default: llvm_unreachable("Unknown shift")::llvm::llvm_unreachable_internal("Unknown shift", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 2662)
;
2663 case ISD::SHL:
2664 // Short: ShAmt < NVTBits
2665 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
2666 HiS = DAG.getNode(ISD::OR, dl, NVT,
2667 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
2668 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
2669
2670 // Long: ShAmt >= NVTBits
2671 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
2672 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
2673
2674 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
2675 Hi = DAG.getSelect(dl, NVT, isZero, InH,
2676 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
2677 return true;
2678 case ISD::SRL:
2679 // Short: ShAmt < NVTBits
2680 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
2681 LoS = DAG.getNode(ISD::OR, dl, NVT,
2682 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2683 // FIXME: If Amt is zero, the following shift generates an undefined result
2684 // on some architectures.
2685 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2686
2687 // Long: ShAmt >= NVTBits
2688 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2689 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2690
2691 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2692 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2693 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2694 return true;
2695 case ISD::SRA:
2696 // Short: ShAmt < NVTBits
2697 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
2698 LoS = DAG.getNode(ISD::OR, dl, NVT,
2699 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2700 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2701
2702 // Long: ShAmt >= NVTBits
2703 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
2704 DAG.getConstant(NVTBits - 1, dl, ShTy));
2705 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2706
2707 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2708 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2709 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2710 return true;
2711 }
2712}
2713
2714static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
2715
2716 switch (Op) {
2717 default: llvm_unreachable("invalid min/max opcode")::llvm::llvm_unreachable_internal("invalid min/max opcode", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 2717)
;
2718 case ISD::SMAX:
2719 return std::make_pair(ISD::SETGT, ISD::UMAX);
2720 case ISD::UMAX:
2721 return std::make_pair(ISD::SETUGT, ISD::UMAX);
2722 case ISD::SMIN:
2723 return std::make_pair(ISD::SETLT, ISD::UMIN);
2724 case ISD::UMIN:
2725 return std::make_pair(ISD::SETULT, ISD::UMIN);
2726 }
2727}
2728
2729void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
2730 SDValue &Lo, SDValue &Hi) {
2731 SDLoc DL(N);
2732 ISD::NodeType LoOpc;
2733 ISD::CondCode CondC;
2734 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
2735
2736 // Expand the subcomponents.
2737 SDValue LHSL, LHSH, RHSL, RHSH;
2738 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2739 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2740
2741 // Value types
2742 EVT NVT = LHSL.getValueType();
2743 EVT CCT = getSetCCResultType(NVT);
2744
2745 // Hi part is always the same op
2746 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
2747
2748 // We need to know whether to select Lo part that corresponds to 'winning'
2749 // Hi part or if Hi parts are equal.
2750 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
2751 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
2752
2753 // Lo part corresponding to the 'winning' Hi part
2754 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
2755
2756 // Recursed Lo part if Hi parts are equal, this uses unsigned version
2757 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
2758
2759 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
2760}
2761
2762void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
2763 SDValue &Lo, SDValue &Hi) {
2764 SDLoc dl(N);
2765 // Expand the subcomponents.
2766 SDValue LHSL, LHSH, RHSL, RHSH;
2767 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2768 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2769
2770 EVT NVT = LHSL.getValueType();
2771 SDValue LoOps[2] = { LHSL, RHSL };
2772 SDValue HiOps[3] = { LHSH, RHSH };
2773
2774 bool HasOpCarry = TLI.isOperationLegalOrCustom(
2775 N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
2776 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2777 if (HasOpCarry) {
2778 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
2779 if (N->getOpcode() == ISD::ADD) {
2780 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2781 HiOps[2] = Lo.getValue(1);
2782 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, HiOps);
2783 } else {
2784 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2785 HiOps[2] = Lo.getValue(1);
2786 Hi = DAG.getNode(ISD::SUBCARRY, dl, VTList, HiOps);
2787 }
2788 return;
2789 }
2790
2791 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
2792 // them. TODO: Teach operation legalization how to expand unsupported
2793 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
2794 // a carry of type MVT::Glue, but there doesn't seem to be any way to
2795 // generate a value of this type in the expanded code sequence.
2796 bool hasCarry =
2797 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2798 ISD::ADDC : ISD::SUBC,
2799 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2800
2801 if (hasCarry) {
2802 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
2803 if (N->getOpcode() == ISD::ADD) {
2804 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2805 HiOps[2] = Lo.getValue(1);
2806 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2807 } else {
2808 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2809 HiOps[2] = Lo.getValue(1);
2810 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2811 }
2812 return;
2813 }
2814
2815 bool hasOVF =
2816 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2817 ISD::UADDO : ISD::USUBO,
2818 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2819 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
2820
2821 if (hasOVF) {
2822 EVT OvfVT = getSetCCResultType(NVT);
2823 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
2824 int RevOpc;
2825 if (N->getOpcode() == ISD::ADD) {
2826 RevOpc = ISD::SUB;
2827 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2828 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2829 } else {
2830 RevOpc = ISD::ADD;
2831 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2832 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2833 }
2834 SDValue OVF = Lo.getValue(1);
2835
2836 switch (BoolType) {
2837 case TargetLoweringBase::UndefinedBooleanContent:
2838 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
2839 LLVM_FALLTHROUGH[[gnu::fallthrough]];
2840 case TargetLoweringBase::ZeroOrOneBooleanContent:
2841 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
2842 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
2843 break;
2844 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
2845 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
2846 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
2847 }
2848 return;
2849 }
2850
2851 if (N->getOpcode() == ISD::ADD) {
2852 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
2853 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2854 SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
2855 ISD::SETULT);
2856
2857 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent) {
2858 SDValue Carry = DAG.getZExtOrTrunc(Cmp1, dl, NVT);
2859 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
2860 return;
2861 }
2862
2863 SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
2864 DAG.getConstant(1, dl, NVT),
2865 DAG.getConstant(0, dl, NVT));
2866 SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
2867 ISD::SETULT);
2868 SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
2869 DAG.getConstant(1, dl, NVT), Carry1);
2870 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
2871 } else {
2872 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
2873 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2874 SDValue Cmp =
2875 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
2876 LoOps[0], LoOps[1], ISD::SETULT);
2877
2878 SDValue Borrow;
2879 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent)
2880 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
2881 else
2882 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
2883 DAG.getConstant(0, dl, NVT));
2884
2885 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
2886 }
2887}
2888
2889void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
2890 SDValue &Lo, SDValue &Hi) {
2891 // Expand the subcomponents.
2892 SDValue LHSL, LHSH, RHSL, RHSH;
2893 SDLoc dl(N);
2894 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2895 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2896 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2897 SDValue LoOps[2] = { LHSL, RHSL };
2898 SDValue HiOps[3] = { LHSH, RHSH };
2899
2900 if (N->getOpcode() == ISD::ADDC) {
2901 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2902 HiOps[2] = Lo.getValue(1);
2903 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2904 } else {
2905 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2906 HiOps[2] = Lo.getValue(1);
2907 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2908 }
2909
2910 // Legalized the flag result - switch anything that used the old flag to
2911 // use the new one.
2912 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2913}
2914
2915void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
2916 SDValue &Lo, SDValue &Hi) {
2917 // Expand the subcomponents.
2918 SDValue LHSL, LHSH, RHSL, RHSH;
2919 SDLoc dl(N);
2920 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2921 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2922 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2923 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2924 SDValue HiOps[3] = { LHSH, RHSH };
2925
2926 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2927 HiOps[2] = Lo.getValue(1);
2928 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2929
2930 // Legalized the flag result - switch anything that used the old flag to
2931 // use the new one.
2932 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2933}
2934
2935void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
2936 SDValue &Lo, SDValue &Hi) {
2937 SDValue LHS = N->getOperand(0);
2938 SDValue RHS = N->getOperand(1);
2939 SDLoc dl(N);
2940
2941 SDValue Ovf;
2942
2943 unsigned CarryOp, NoCarryOp;
2944 ISD::CondCode Cond;
2945 switch(N->getOpcode()) {
2946 case ISD::UADDO:
2947 CarryOp = ISD::ADDCARRY;
2948 NoCarryOp = ISD::ADD;
2949 Cond = ISD::SETULT;
2950 break;
2951 case ISD::USUBO:
2952 CarryOp = ISD::SUBCARRY;
2953 NoCarryOp = ISD::SUB;
2954 Cond = ISD::SETUGT;
2955 break;
2956 default:
2957 llvm_unreachable("Node has unexpected Opcode")::llvm::llvm_unreachable_internal("Node has unexpected Opcode"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 2957
)
;
2958 }
2959
2960 bool HasCarryOp = TLI.isOperationLegalOrCustom(
2961 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
2962
2963 if (HasCarryOp) {
2964 // Expand the subcomponents.
2965 SDValue LHSL, LHSH, RHSL, RHSH;
2966 GetExpandedInteger(LHS, LHSL, LHSH);
2967 GetExpandedInteger(RHS, RHSL, RHSH);
2968 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2969 SDValue LoOps[2] = { LHSL, RHSL };
2970 SDValue HiOps[3] = { LHSH, RHSH };
2971
2972 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2973 HiOps[2] = Lo.getValue(1);
2974 Hi = DAG.getNode(CarryOp, dl, VTList, HiOps);
2975
2976 Ovf = Hi.getValue(1);
2977 } else {
2978 // Expand the result by simply replacing it with the equivalent
2979 // non-overflow-checking operation.
2980 SDValue Sum = DAG.getNode(NoCarryOp, dl, LHS.getValueType(), LHS, RHS);
2981 SplitInteger(Sum, Lo, Hi);
2982
2983 // Calculate the overflow: addition overflows iff a + b < a, and subtraction
2984 // overflows iff a - b > a.
2985 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
2986 }
2987
2988 // Legalized the flag result - switch anything that used the old flag to
2989 // use the new one.
2990 ReplaceValueWith(SDValue(N, 1), Ovf);
2991}
2992
2993void DAGTypeLegalizer::ExpandIntRes_ADDSUBCARRY(SDNode *N,
2994 SDValue &Lo, SDValue &Hi) {
2995 // Expand the subcomponents.
2996 SDValue LHSL, LHSH, RHSL, RHSH;
2997 SDLoc dl(N);
2998 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2999 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3000 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3001 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
3002 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
3003
3004 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
3005 HiOps[2] = Lo.getValue(1);
3006 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
3007
3008 // Legalized the flag result - switch anything that used the old flag to
3009 // use the new one.
3010 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3011}
3012
3013void DAGTypeLegalizer::ExpandIntRes_SADDSUBO_CARRY(SDNode *N,
3014 SDValue &Lo, SDValue &Hi) {
3015 // Expand the subcomponents.
3016 SDValue LHSL, LHSH, RHSL, RHSH;
3017 SDLoc dl(N);
3018 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3019 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
3020 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
3021
3022 // We need to use an unsigned carry op for the lo part.
3023 unsigned CarryOp = N->getOpcode() == ISD::SADDO_CARRY ? ISD::ADDCARRY
3024 : ISD::SUBCARRY;
3025 Lo = DAG.getNode(CarryOp, dl, VTList, { LHSL, RHSL, N->getOperand(2) });
3026 Hi = DAG.getNode(N->getOpcode(), dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
3027
3028 // Legalized the flag result - switch anything that used the old flag to
3029 // use the new one.
3030 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
3031}
3032
3033void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
3034 SDValue &Lo, SDValue &Hi) {
3035 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3036 SDLoc dl(N);
3037 SDValue Op = N->getOperand(0);
3038 if (Op.getValueType().bitsLE(NVT)) {
3039 // The low part is any extension of the input (which degenerates to a copy).
3040 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
3041 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
3042 } else {
3043 // For example, extension of an i48 to an i64. The operand type necessarily
3044 // promotes to the result type, so will end up being expanded too.
3045 assert(getTypeAction(Op.getValueType()) ==(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3047
, __extension__ __PRETTY_FUNCTION__))
3046 TargetLowering::TypePromoteInteger &&(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3047
, __extension__ __PRETTY_FUNCTION__))
3047 "Only know how to promote this result!")(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3047
, __extension__ __PRETTY_FUNCTION__))
;
3048 SDValue Res = GetPromotedInteger(Op);
3049 assert(Res.getValueType() == N->getValueType(0) &&(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Res.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3050
, __extension__ __PRETTY_FUNCTION__))
3050 "Operand over promoted?")(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Res.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3050
, __extension__ __PRETTY_FUNCTION__))
;
3051 // Split the promoted operand. This will simplify when it is expanded.
3052 SplitInteger(Res, Lo, Hi);
3053 }
3054}
3055
3056void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
3057 SDValue &Lo, SDValue &Hi) {
3058 SDLoc dl(N);
3059 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3060 EVT NVT = Lo.getValueType();
3061 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3062 unsigned NVTBits = NVT.getSizeInBits();
3063 unsigned EVTBits = EVT.getSizeInBits();
3064
3065 if (NVTBits < EVTBits) {
3066 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
3067 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3068 EVTBits - NVTBits)));
3069 } else {
3070 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
3071 // The high part replicates the sign bit of Lo, make it explicit.
3072 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3073 DAG.getConstant(NVTBits - 1, dl,
3074 TLI.getPointerTy(DAG.getDataLayout())));
3075 }
3076}
3077
3078void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
3079 SDValue &Lo, SDValue &Hi) {
3080 SDLoc dl(N);
3081 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3082 EVT NVT = Lo.getValueType();
3083 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3084 unsigned NVTBits = NVT.getSizeInBits();
3085 unsigned EVTBits = EVT.getSizeInBits();
3086
3087 if (NVTBits < EVTBits) {
3088 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
3089 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3090 EVTBits - NVTBits)));
3091 } else {
3092 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
3093 // The high part must be zero, make it explicit.
3094 Hi = DAG.getConstant(0, dl, NVT);
3095 }
3096}
3097
3098void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
3099 SDValue &Lo, SDValue &Hi) {
3100 SDLoc dl(N);
3101 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
3102 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
3103 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
3104}
3105
3106void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
3107 SDValue &Lo, SDValue &Hi) {
3108 SDLoc dl(N);
3109 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
3110 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
3111 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
3112}
3113
3114void DAGTypeLegalizer::ExpandIntRes_PARITY(SDNode *N, SDValue &Lo,
3115 SDValue &Hi) {
3116 SDLoc dl(N);
3117 // parity(HiLo) -> parity(Lo^Hi)
3118 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3119 EVT NVT = Lo.getValueType();
3120 Lo =
3121 DAG.getNode(ISD::PARITY, dl, NVT, DAG.getNode(ISD::XOR, dl, NVT, Lo, Hi));
3122 Hi = DAG.getConstant(0, dl, NVT);
3123}
3124
3125void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
3126 SDValue &Lo, SDValue &Hi) {
3127 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3128 unsigned NBitWidth = NVT.getSizeInBits();
3129 auto Constant = cast<ConstantSDNode>(N);
3130 const APInt &Cst = Constant->getAPIntValue();
3131 bool IsTarget = Constant->isTargetOpcode();
3132 bool IsOpaque = Constant->isOpaque();
3133 SDLoc dl(N);
3134 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
3135 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
3136 IsOpaque);
3137}
3138
3139void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
3140 SDLoc dl(N);
3141
3142 SDValue N0 = N->getOperand(0);
3143 GetExpandedInteger(N0, Lo, Hi);
3144 EVT NVT = Lo.getValueType();
3145
3146 // If we have SUBCARRY, use the expanded form of the sra+xor+sub sequence we
3147 // use in LegalizeDAG. The SUB part of the expansion is based on
3148 // ExpandIntRes_ADDSUB which also uses SUBCARRY/USUBO after checking that
3149 // SUBCARRY is LegalOrCustom. Each of the pieces here can be further expanded
3150 // if needed. Shift expansion has a special case for filling with sign bits
3151 // so that we will only end up with one SRA.
3152 bool HasSubCarry = TLI.isOperationLegalOrCustom(
3153 ISD::SUBCARRY, TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
3154 if (HasSubCarry) {
3155 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3156 SDValue Sign =
3157 DAG.getNode(ISD::SRA, dl, NVT, Hi,
3158 DAG.getConstant(NVT.getSizeInBits() - 1, dl, ShiftAmtTy));
3159 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
3160 Lo = DAG.getNode(ISD::XOR, dl, NVT, Lo, Sign);
3161 Hi = DAG.getNode(ISD::XOR, dl, NVT, Hi, Sign);
3162 Lo = DAG.getNode(ISD::USUBO, dl, VTList, Lo, Sign);
3163 Hi = DAG.getNode(ISD::SUBCARRY, dl, VTList, Hi, Sign, Lo.getValue(1));
3164 return;
3165 }
3166
3167 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
3168 EVT VT = N->getValueType(0);
3169 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
3170 DAG.getConstant(0, dl, VT), N0);
3171 SDValue NegLo, NegHi;
3172 SplitInteger(Neg, NegLo, NegHi);
3173
3174 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
3175 DAG.getConstant(0, dl, NVT), ISD::SETLT);
3176 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
3177 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
3178}
3179
3180void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
3181 SDValue &Lo, SDValue &Hi) {
3182 SDLoc dl(N);
3183 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
3184 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3185 EVT NVT = Lo.getValueType();
3186
3187 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
3188 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3189
3190 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
3191 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
3192
3193 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
3194 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
3195 DAG.getConstant(NVT.getSizeInBits(), dl,
3196 NVT)));
3197 Hi = DAG.getConstant(0, dl, NVT);
3198}
3199
3200void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
3201 SDValue &Lo, SDValue &Hi) {
3202 SDLoc dl(N);
3203 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
3204 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3205 EVT NVT = Lo.getValueType();
3206 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
3207 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
3208 Hi = DAG.getConstant(0, dl, NVT);
3209}
3210
3211void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
3212 SDValue &Lo, SDValue &Hi) {
3213 SDLoc dl(N);
3214 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
3215 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3216 EVT NVT = Lo.getValueType();
3217
3218 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
3219 DAG.getConstant(0, dl, NVT), ISD::SETNE);
3220
3221 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
3222 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
3223
3224 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
3225 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
3226 DAG.getConstant(NVT.getSizeInBits(), dl,
3227 NVT)));
3228 Hi = DAG.getConstant(0, dl, NVT);
3229}
3230
3231void DAGTypeLegalizer::ExpandIntRes_FLT_ROUNDS(SDNode *N, SDValue &Lo,
3232 SDValue &Hi) {
3233 SDLoc dl(N);
3234 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3235 unsigned NBitWidth = NVT.getSizeInBits();
3236
3237 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3238 Lo = DAG.getNode(ISD::FLT_ROUNDS_, dl, {NVT, MVT::Other}, N->getOperand(0));
3239 SDValue Chain = Lo.getValue(1);
3240 // The high part is the sign of Lo, as -1 is a valid value for FLT_ROUNDS
3241 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3242 DAG.getConstant(NBitWidth - 1, dl, ShiftAmtTy));
3243
3244 // Legalize the chain result - switch anything that used the old chain to
3245 // use the new one.
3246 ReplaceValueWith(SDValue(N, 1), Chain);
3247}
3248
3249void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
3250 SDValue &Hi) {
3251 SDLoc dl(N);
3252 EVT VT = N->getValueType(0);
3253
3254 bool IsStrict = N->isStrictFPOpcode();
3255 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
3256 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
3257 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
3258 Op = GetPromotedFloat(Op);
3259
3260 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf) {
3261 EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType());
3262 Op = GetSoftPromotedHalf(Op);
3263 Op = DAG.getNode(ISD::FP16_TO_FP, dl, NFPVT, Op);
3264 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
3265 SplitInteger(Op, Lo, Hi);
3266 return;
3267 }
3268
3269 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
3270 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected fp-to-sint conversion!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected fp-to-sint conversion!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3270
, __extension__ __PRETTY_FUNCTION__))
;
3271 TargetLowering::MakeLibCallOptions CallOptions;
3272 CallOptions.setSExt(true);
3273 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
3274 CallOptions, dl, Chain);
3275 SplitInteger(Tmp.first, Lo, Hi);
3276
3277 if (IsStrict)
3278 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3279}
3280
3281void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
3282 SDValue &Hi) {
3283 SDLoc dl(N);
3284 EVT VT = N->getValueType(0);
3285
3286 bool IsStrict = N->isStrictFPOpcode();
3287 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
3288 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
3289 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
3290 Op = GetPromotedFloat(Op);
3291
3292 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftPromoteHalf) {
3293 EVT NFPVT = TLI.getTypeToTransformTo(*DAG.getContext(), Op.getValueType());
3294 Op = GetSoftPromotedHalf(Op);
3295 Op = DAG.getNode(ISD::FP16_TO_FP, dl, NFPVT, Op);
3296 Op = DAG.getNode(ISD::FP_TO_UINT, dl, VT, Op);
3297 SplitInteger(Op, Lo, Hi);
3298 return;
3299 }
3300
3301 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
3302 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected fp-to-uint conversion!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected fp-to-uint conversion!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3302
, __extension__ __PRETTY_FUNCTION__))
;
3303 TargetLowering::MakeLibCallOptions CallOptions;
3304 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, VT, Op,
3305 CallOptions, dl, Chain);
3306 SplitInteger(Tmp.first, Lo, Hi);
3307
3308 if (IsStrict)
3309 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3310}
3311
3312void DAGTypeLegalizer::ExpandIntRes_FP_TO_XINT_SAT(SDNode *N, SDValue &Lo,
3313 SDValue &Hi) {
3314 SDValue Res = TLI.expandFP_TO_INT_SAT(N, DAG);
3315 SplitInteger(Res, Lo, Hi);
3316}
3317
3318void DAGTypeLegalizer::ExpandIntRes_LLROUND_LLRINT(SDNode *N, SDValue &Lo,
3319 SDValue &Hi) {
3320 SDValue Op = N->getOperand(N->isStrictFPOpcode() ? 1 : 0);
3321
3322 assert(getTypeAction(Op.getValueType()) != TargetLowering::TypePromoteFloat &&(static_cast <bool> (getTypeAction(Op.getValueType()) !=
TargetLowering::TypePromoteFloat && "Input type needs to be promoted!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) != TargetLowering::TypePromoteFloat && \"Input type needs to be promoted!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3323
, __extension__ __PRETTY_FUNCTION__))
3323 "Input type needs to be promoted!")(static_cast <bool> (getTypeAction(Op.getValueType()) !=
TargetLowering::TypePromoteFloat && "Input type needs to be promoted!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) != TargetLowering::TypePromoteFloat && \"Input type needs to be promoted!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3323
, __extension__ __PRETTY_FUNCTION__))
;
3324
3325 EVT VT = Op.getValueType();
3326
3327 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3328 if (N->getOpcode() == ISD::LLROUND ||
3329 N->getOpcode() == ISD::STRICT_LLROUND) {
3330 if (VT == MVT::f32)
3331 LC = RTLIB::LLROUND_F32;
3332 else if (VT == MVT::f64)
3333 LC = RTLIB::LLROUND_F64;
3334 else if (VT == MVT::f80)
3335 LC = RTLIB::LLROUND_F80;
3336 else if (VT == MVT::f128)
3337 LC = RTLIB::LLROUND_F128;
3338 else if (VT == MVT::ppcf128)
3339 LC = RTLIB::LLROUND_PPCF128;
3340 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected llround input type!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected llround input type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3340
, __extension__ __PRETTY_FUNCTION__))
;
3341 } else if (N->getOpcode() == ISD::LLRINT ||
3342 N->getOpcode() == ISD::STRICT_LLRINT) {
3343 if (VT == MVT::f32)
3344 LC = RTLIB::LLRINT_F32;
3345 else if (VT == MVT::f64)
3346 LC = RTLIB::LLRINT_F64;
3347 else if (VT == MVT::f80)
3348 LC = RTLIB::LLRINT_F80;
3349 else if (VT == MVT::f128)
3350 LC = RTLIB::LLRINT_F128;
3351 else if (VT == MVT::ppcf128)
3352 LC = RTLIB::LLRINT_PPCF128;
3353 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unexpected llrint input type!") ? void (0) : __assert_fail (
"LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected llrint input type!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3353
, __extension__ __PRETTY_FUNCTION__))
;
3354 } else
3355 llvm_unreachable("Unexpected opcode!")::llvm::llvm_unreachable_internal("Unexpected opcode!", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 3355)
;
3356
3357 SDLoc dl(N);
3358 EVT RetVT = N->getValueType(0);
3359 SDValue Chain = N->isStrictFPOpcode() ? N->getOperand(0) : SDValue();
3360
3361 TargetLowering::MakeLibCallOptions CallOptions;
3362 CallOptions.setSExt(true);
3363 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
3364 Op, CallOptions, dl,
3365 Chain);
3366 SplitInteger(Tmp.first, Lo, Hi);
3367
3368 if (N->isStrictFPOpcode())
3369 ReplaceValueWith(SDValue(N, 1), Tmp.second);
3370}
3371
3372void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
3373 SDValue &Lo, SDValue &Hi) {
3374 if (N->isAtomic()) {
3375 // It's typical to have larger CAS than atomic load instructions.
3376 SDLoc dl(N);
3377 EVT VT = N->getMemoryVT();
3378 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
3379 SDValue Zero = DAG.getConstant(0, dl, VT);
3380 SDValue Swap = DAG.getAtomicCmpSwap(
3381 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
3382 VT, VTs, N->getOperand(0),
3383 N->getOperand(1), Zero, Zero, N->getMemOperand());
3384 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
3385 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
3386 return;
3387 }
3388
3389 if (ISD::isNormalLoad(N)) {
3390 ExpandRes_NormalLoad(N, Lo, Hi);
3391 return;
3392 }
3393
3394 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!")(static_cast <bool> (ISD::isUNINDEXEDLoad(N) &&
"Indexed load during type legalization!") ? void (0) : __assert_fail
("ISD::isUNINDEXEDLoad(N) && \"Indexed load during type legalization!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3394
, __extension__ __PRETTY_FUNCTION__))
;
3395
3396 EVT VT = N->getValueType(0);
3397 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3398 SDValue Ch = N->getChain();
3399 SDValue Ptr = N->getBasePtr();
3400 ISD::LoadExtType ExtType = N->getExtensionType();
3401 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
3402 AAMDNodes AAInfo = N->getAAInfo();
3403 SDLoc dl(N);
3404
3405 assert(NVT.isByteSized() && "Expanded type not byte sized!")(static_cast <bool> (NVT.isByteSized() && "Expanded type not byte sized!"
) ? void (0) : __assert_fail ("NVT.isByteSized() && \"Expanded type not byte sized!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3405
, __extension__ __PRETTY_FUNCTION__))
;
3406
3407 if (N->getMemoryVT().bitsLE(NVT)) {
3408 EVT MemVT = N->getMemoryVT();
3409
3410 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
3411 N->getOriginalAlign(), MMOFlags, AAInfo);
3412
3413 // Remember the chain.
3414 Ch = Lo.getValue(1);
3415
3416 if (ExtType == ISD::SEXTLOAD) {
3417 // The high part is obtained by SRA'ing all but one of the bits of the
3418 // lo part.
3419 unsigned LoSize = Lo.getValueSizeInBits();
3420 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
3421 DAG.getConstant(LoSize - 1, dl,
3422 TLI.getPointerTy(DAG.getDataLayout())));
3423 } else if (ExtType == ISD::ZEXTLOAD) {
3424 // The high part is just a zero.
3425 Hi = DAG.getConstant(0, dl, NVT);
3426 } else {
3427 assert(ExtType == ISD::EXTLOAD && "Unknown extload!")(static_cast <bool> (ExtType == ISD::EXTLOAD &&
"Unknown extload!") ? void (0) : __assert_fail ("ExtType == ISD::EXTLOAD && \"Unknown extload!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3427
, __extension__ __PRETTY_FUNCTION__))
;
3428 // The high part is undefined.
3429 Hi = DAG.getUNDEF(NVT);
3430 }
3431 } else if (DAG.getDataLayout().isLittleEndian()) {
3432 // Little-endian - low bits are at low addresses.
3433 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(),
3434 N->getOriginalAlign(), MMOFlags, AAInfo);
3435
3436 unsigned ExcessBits =
3437 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
3438 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
3439
3440 // Increment the pointer to the other half.
3441 unsigned IncrementSize = NVT.getSizeInBits()/8;
3442 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
3443 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
3444 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
3445 N->getOriginalAlign(), MMOFlags, AAInfo);
3446
3447 // Build a factor node to remember that this load is independent of the
3448 // other one.
3449 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
3450 Hi.getValue(1));
3451 } else {
3452 // Big-endian - high bits are at low addresses. Favor aligned loads at
3453 // the cost of some bit-fiddling.
3454 EVT MemVT = N->getMemoryVT();
3455 unsigned EBytes = MemVT.getStoreSize();
3456 unsigned IncrementSize = NVT.getSizeInBits()/8;
3457 unsigned ExcessBits = (EBytes - IncrementSize)*8;
3458
3459 // Load both the high bits and maybe some of the low bits.
3460 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
3461 EVT::getIntegerVT(*DAG.getContext(),
3462 MemVT.getSizeInBits() - ExcessBits),
3463 N->getOriginalAlign(), MMOFlags, AAInfo);
3464
3465 // Increment the pointer to the other half.
3466 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
3467 // Load the rest of the low bits.
3468 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
3469 N->getPointerInfo().getWithOffset(IncrementSize),
3470 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
3471 N->getOriginalAlign(), MMOFlags, AAInfo);
3472
3473 // Build a factor node to remember that this load is independent of the
3474 // other one.
3475 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
3476 Hi.getValue(1));
3477
3478 if (ExcessBits < NVT.getSizeInBits()) {
3479 // Transfer low bits from the bottom of Hi to the top of Lo.
3480 Lo = DAG.getNode(
3481 ISD::OR, dl, NVT, Lo,
3482 DAG.getNode(ISD::SHL, dl, NVT, Hi,
3483 DAG.getConstant(ExcessBits, dl,
3484 TLI.getPointerTy(DAG.getDataLayout()))));
3485 // Move high bits to the right position in Hi.
3486 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
3487 Hi,
3488 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
3489 TLI.getPointerTy(DAG.getDataLayout())));
3490 }
3491 }
3492
3493 // Legalize the chain result - switch anything that used the old chain to
3494 // use the new one.
3495 ReplaceValueWith(SDValue(N, 1), Ch);
3496}
3497
3498void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
3499 SDValue &Lo, SDValue &Hi) {
3500 SDLoc dl(N);
3501 SDValue LL, LH, RL, RH;
3502 GetExpandedInteger(N->getOperand(0), LL, LH);
3503 GetExpandedInteger(N->getOperand(1), RL, RH);
3504 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
3505 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
3506}
3507
3508void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
3509 SDValue &Lo, SDValue &Hi) {
3510 EVT VT = N->getValueType(0);
3511 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3512 SDLoc dl(N);
3513
3514 SDValue LL, LH, RL, RH;
3515 GetExpandedInteger(N->getOperand(0), LL, LH);
3516 GetExpandedInteger(N->getOperand(1), RL, RH);
3517
3518 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
3519 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
3520 LL, LH, RL, RH))
3521 return;
3522
3523 // If nothing else, we can make a libcall.
3524 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3525 if (VT == MVT::i16)
3526 LC = RTLIB::MUL_I16;
3527 else if (VT == MVT::i32)
3528 LC = RTLIB::MUL_I32;
3529 else if (VT == MVT::i64)
3530 LC = RTLIB::MUL_I64;
3531 else if (VT == MVT::i128)
3532 LC = RTLIB::MUL_I128;
3533
3534 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
3535 // We'll expand the multiplication by brute force because we have no other
3536 // options. This is a trivially-generalized version of the code from
3537 // Hacker's Delight (itself derived from Knuth's Algorithm M from section
3538 // 4.3.1).
3539 unsigned Bits = NVT.getSizeInBits();
3540 unsigned HalfBits = Bits >> 1;
3541 SDValue Mask = DAG.getConstant(APInt::getLowBitsSet(Bits, HalfBits), dl,
3542 NVT);
3543 SDValue LLL = DAG.getNode(ISD::AND, dl, NVT, LL, Mask);
3544 SDValue RLL = DAG.getNode(ISD::AND, dl, NVT, RL, Mask);
3545
3546 SDValue T = DAG.getNode(ISD::MUL, dl, NVT, LLL, RLL);
3547 SDValue TL = DAG.getNode(ISD::AND, dl, NVT, T, Mask);
3548
3549 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3550 SDValue Shift = DAG.getConstant(HalfBits, dl, ShiftAmtTy);
3551 SDValue TH = DAG.getNode(ISD::SRL, dl, NVT, T, Shift);
3552 SDValue LLH = DAG.getNode(ISD::SRL, dl, NVT, LL, Shift);
3553 SDValue RLH = DAG.getNode(ISD::SRL, dl, NVT, RL, Shift);
3554
3555 SDValue U = DAG.getNode(ISD::ADD, dl, NVT,
3556 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLL), TH);
3557 SDValue UL = DAG.getNode(ISD::AND, dl, NVT, U, Mask);
3558 SDValue UH = DAG.getNode(ISD::SRL, dl, NVT, U, Shift);
3559
3560 SDValue V = DAG.getNode(ISD::ADD, dl, NVT,
3561 DAG.getNode(ISD::MUL, dl, NVT, LLL, RLH), UL);
3562 SDValue VH = DAG.getNode(ISD::SRL, dl, NVT, V, Shift);
3563
3564 SDValue W = DAG.getNode(ISD::ADD, dl, NVT,
3565 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLH),
3566 DAG.getNode(ISD::ADD, dl, NVT, UH, VH));
3567 Lo = DAG.getNode(ISD::ADD, dl, NVT, TL,
3568 DAG.getNode(ISD::SHL, dl, NVT, V, Shift));
3569
3570 Hi = DAG.getNode(ISD::ADD, dl, NVT, W,
3571 DAG.getNode(ISD::ADD, dl, NVT,
3572 DAG.getNode(ISD::MUL, dl, NVT, RH, LL),
3573 DAG.getNode(ISD::MUL, dl, NVT, RL, LH)));
3574 return;
3575 }
3576
3577 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3578 TargetLowering::MakeLibCallOptions CallOptions;
3579 CallOptions.setSExt(true);
3580 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first,
3581 Lo, Hi);
3582}
3583
3584void DAGTypeLegalizer::ExpandIntRes_READCYCLECOUNTER(SDNode *N, SDValue &Lo,
3585 SDValue &Hi) {
3586 SDLoc DL(N);
3587 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3588 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
3589 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
3590 Lo = R.getValue(0);
3591 Hi = R.getValue(1);
3592 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
3593}
3594
3595void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
3596 SDValue &Hi) {
3597 SDValue Result = TLI.expandAddSubSat(N, DAG);
3598 SplitInteger(Result, Lo, Hi);
3599}
3600
3601void DAGTypeLegalizer::ExpandIntRes_SHLSAT(SDNode *N, SDValue &Lo,
3602 SDValue &Hi) {
3603 SDValue Result = TLI.expandShlSat(N, DAG);
3604 SplitInteger(Result, Lo, Hi);
3605}
3606
3607/// This performs an expansion of the integer result for a fixed point
3608/// multiplication. The default expansion performs rounding down towards
3609/// negative infinity, though targets that do care about rounding should specify
3610/// a target hook for rounding and provide their own expansion or lowering of
3611/// fixed point multiplication to be consistent with rounding.
3612void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
3613 SDValue &Hi) {
3614 SDLoc dl(N);
3615 EVT VT = N->getValueType(0);
3616 unsigned VTSize = VT.getScalarSizeInBits();
3617 SDValue LHS = N->getOperand(0);
3618 SDValue RHS = N->getOperand(1);
3619 uint64_t Scale = N->getConstantOperandVal(2);
3620 bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
3621 N->getOpcode() == ISD::UMULFIXSAT);
3622 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
3623 N->getOpcode() == ISD::SMULFIXSAT);
3624
3625 // Handle special case when scale is equal to zero.
3626 if (!Scale) {
3627 SDValue Result;
3628 if (!Saturating) {
3629 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
3630 } else {
3631 EVT BoolVT = getSetCCResultType(VT);
3632 unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
3633 Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
3634 SDValue Product = Result.getValue(0);
3635 SDValue Overflow = Result.getValue(1);
3636 if (Signed) {
3637 APInt MinVal = APInt::getSignedMinValue(VTSize);
3638 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
3639 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
3640 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
3641 SDValue Zero = DAG.getConstant(0, dl, VT);
3642 // Xor the inputs, if resulting sign bit is 0 the product will be
3643 // positive, else negative.
3644 SDValue Xor = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
3645 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Xor, Zero, ISD::SETLT);
3646 Result = DAG.getSelect(dl, VT, ProdNeg, SatMin, SatMax);
3647 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
3648 } else {
3649 // For unsigned multiplication, we only need to check the max since we
3650 // can't really overflow towards zero.
3651 APInt MaxVal = APInt::getMaxValue(VTSize);
3652 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
3653 Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
3654 }
3655 }
3656 SplitInteger(Result, Lo, Hi);
3657 return;
3658 }
3659
3660 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
3661 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
3662 assert(Scale <= VTSize && "Scale can't be larger than the value type size.")(static_cast <bool> (Scale <= VTSize && "Scale can't be larger than the value type size."
) ? void (0) : __assert_fail ("Scale <= VTSize && \"Scale can't be larger than the value type size.\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3662
, __extension__ __PRETTY_FUNCTION__))
;
3663
3664 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3665 SDValue LL, LH, RL, RH;
3666 GetExpandedInteger(LHS, LL, LH);
3667 GetExpandedInteger(RHS, RL, RH);
3668 SmallVector<SDValue, 4> Result;
3669
3670 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
3671 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
3672 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
3673 LL, LH, RL, RH)) {
3674 report_fatal_error("Unable to expand MUL_FIX using MUL_LOHI.");
3675 return;
3676 }
3677
3678 unsigned NVTSize = NVT.getScalarSizeInBits();
3679 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "(static_cast <bool> ((VTSize == NVTSize * 2) &&
"Expected the new value type to be half " "the size of the current value type"
) ? void (0) : __assert_fail ("(VTSize == NVTSize * 2) && \"Expected the new value type to be half \" \"the size of the current value type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3680
, __extension__ __PRETTY_FUNCTION__))
3680 "the size of the current value type")(static_cast <bool> ((VTSize == NVTSize * 2) &&
"Expected the new value type to be half " "the size of the current value type"
) ? void (0) : __assert_fail ("(VTSize == NVTSize * 2) && \"Expected the new value type to be half \" \"the size of the current value type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3680
, __extension__ __PRETTY_FUNCTION__))
;
3681 EVT ShiftTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
3682
3683 // After getting the multiplication result in 4 parts, we need to perform a
3684 // shift right by the amount of the scale to get the result in that scale.
3685 //
3686 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
3687 // 128 bits that are cut into 4 32-bit parts:
3688 //
3689 // HH HL LH LL
3690 // |---32---|---32---|---32---|---32---|
3691 // 128 96 64 32 0
3692 //
3693 // |------VTSize-----|
3694 //
3695 // |NVTSize-|
3696 //
3697 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
3698 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
3699 // amount and get Lo and Hi using two funnel shifts. Or for the special case
3700 // when Scale is a multiple of NVTSize we can just pick the result without
3701 // shifting.
3702 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
3703 if (Scale % NVTSize) {
3704 SDValue ShiftAmount = DAG.getConstant(Scale % NVTSize, dl, ShiftTy);
3705 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
3706 ShiftAmount);
3707 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
3708 ShiftAmount);
3709 } else {
3710 Lo = Result[Part0];
3711 Hi = Result[Part0 + 1];
3712 }
3713
3714 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
3715 if (!Saturating)
3716 return;
3717
3718 // Can not overflow when there is no integer part.
3719 if (Scale == VTSize)
3720 return;
3721
3722 // To handle saturation we must check for overflow in the multiplication.
3723 //
3724 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
3725 // aren't all zeroes.
3726 //
3727 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
3728 // aren't all ones or all zeroes.
3729 //
3730 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
3731 // highest bit of HH determines saturation direction in the event of signed
3732 // saturation.
3733
3734 SDValue ResultHL = Result[2];
3735 SDValue ResultHH = Result[3];
3736
3737 SDValue SatMax, SatMin;
3738 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
3739 SDValue NVTNeg1 = DAG.getConstant(-1, dl, NVT);
3740 EVT BoolNVT = getSetCCResultType(NVT);
3741
3742 if (!Signed) {
3743 if (Scale < NVTSize) {
3744 // Overflow happened if ((HH | (HL >> Scale)) != 0).
3745 SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
3746 DAG.getConstant(Scale, dl, ShiftTy));
3747 SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
3748 SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
3749 } else if (Scale == NVTSize) {
3750 // Overflow happened if (HH != 0).
3751 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
3752 } else if (Scale < VTSize) {
3753 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
3754 SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
3755 DAG.getConstant(Scale - NVTSize, dl,
3756 ShiftTy));
3757 SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
3758 } else
3759 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"::llvm::llvm_unreachable_internal("Scale must be less or equal to VTSize for UMULFIXSAT"
"(and saturation can't happen with Scale==VTSize).", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 3760)
3760 "(and saturation can't happen with Scale==VTSize).")::llvm::llvm_unreachable_internal("Scale must be less or equal to VTSize for UMULFIXSAT"
"(and saturation can't happen with Scale==VTSize).", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 3760)
;
3761
3762 Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
3763 Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
3764 return;
3765 }
3766
3767 if (Scale < NVTSize) {
3768 // The number of overflow bits we can check are VTSize - Scale + 1 (we
3769 // include the sign bit). If these top bits are > 0, then we overflowed past
3770 // the max value. If these top bits are < -1, then we overflowed past the
3771 // min value. Otherwise, we did not overflow.
3772 unsigned OverflowBits = VTSize - Scale + 1;
3773 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&(static_cast <bool> (OverflowBits <= VTSize &&
OverflowBits > NVTSize && "Extent of overflow bits must start within HL"
) ? void (0) : __assert_fail ("OverflowBits <= VTSize && OverflowBits > NVTSize && \"Extent of overflow bits must start within HL\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3774
, __extension__ __PRETTY_FUNCTION__))
3774 "Extent of overflow bits must start within HL")(static_cast <bool> (OverflowBits <= VTSize &&
OverflowBits > NVTSize && "Extent of overflow bits must start within HL"
) ? void (0) : __assert_fail ("OverflowBits <= VTSize && OverflowBits > NVTSize && \"Extent of overflow bits must start within HL\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3774
, __extension__ __PRETTY_FUNCTION__))
;
3775 SDValue HLHiMask = DAG.getConstant(
3776 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
3777 SDValue HLLoMask = DAG.getConstant(
3778 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
3779 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
3780 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
3781 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
3782 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
3783 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
3784 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
3785 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
3786 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
3787 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
3788 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
3789 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
3790 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
3791 } else if (Scale == NVTSize) {
3792 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
3793 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
3794 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
3795 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
3796 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
3797 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
3798 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
3799 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
3800 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
3801 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
3802 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
3803 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
3804 } else if (Scale < VTSize) {
3805 // This is similar to the case when we saturate if Scale < NVTSize, but we
3806 // only need to check HH.
3807 unsigned OverflowBits = VTSize - Scale + 1;
3808 SDValue HHHiMask = DAG.getConstant(
3809 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
3810 SDValue HHLoMask = DAG.getConstant(
3811 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
3812 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
3813 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
3814 } else
3815 llvm_unreachable("Illegal scale for signed fixed point mul.")::llvm::llvm_unreachable_internal("Illegal scale for signed fixed point mul."
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3815
)
;
3816
3817 // Saturate to signed maximum.
3818 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
3819 APInt MaxLo = APInt::getAllOnes(NVTSize);
3820 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
3821 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
3822 // Saturate to signed minimum.
3823 APInt MinHi = APInt::getSignedMinValue(NVTSize);
3824 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
3825 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
3826}
3827
3828void DAGTypeLegalizer::ExpandIntRes_DIVFIX(SDNode *N, SDValue &Lo,
3829 SDValue &Hi) {
3830 SDLoc dl(N);
3831 // Try expanding in the existing type first.
3832 SDValue Res = TLI.expandFixedPointDiv(N->getOpcode(), dl, N->getOperand(0),
3833 N->getOperand(1),
3834 N->getConstantOperandVal(2), DAG);
3835
3836 if (!Res)
3837 Res = earlyExpandDIVFIX(N, N->getOperand(0), N->getOperand(1),
3838 N->getConstantOperandVal(2), TLI, DAG);
3839 SplitInteger(Res, Lo, Hi);
3840}
3841
3842void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
3843 SDValue &Lo, SDValue &Hi) {
3844 assert((Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) &&(static_cast <bool> ((Node->getOpcode() == ISD::SADDO
|| Node->getOpcode() == ISD::SSUBO) && "Node has unexpected Opcode"
) ? void (0) : __assert_fail ("(Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) && \"Node has unexpected Opcode\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3845
, __extension__ __PRETTY_FUNCTION__))
3845 "Node has unexpected Opcode")(static_cast <bool> ((Node->getOpcode() == ISD::SADDO
|| Node->getOpcode() == ISD::SSUBO) && "Node has unexpected Opcode"
) ? void (0) : __assert_fail ("(Node->getOpcode() == ISD::SADDO || Node->getOpcode() == ISD::SSUBO) && \"Node has unexpected Opcode\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3845
, __extension__ __PRETTY_FUNCTION__))
;
3846 SDValue LHS = Node->getOperand(0);
3847 SDValue RHS = Node->getOperand(1);
3848 SDLoc dl(Node);
3849
3850 SDValue Ovf;
3851
3852 bool IsAdd = Node->getOpcode() == ISD::SADDO;
3853 unsigned CarryOp = IsAdd ? ISD::SADDO_CARRY : ISD::SSUBO_CARRY;
3854
3855 bool HasCarryOp = TLI.isOperationLegalOrCustom(
3856 CarryOp, TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
3857
3858 if (HasCarryOp) {
3859 // Expand the subcomponents.
3860 SDValue LHSL, LHSH, RHSL, RHSH;
3861 GetExpandedInteger(LHS, LHSL, LHSH);
3862 GetExpandedInteger(RHS, RHSL, RHSH);
3863 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), Node->getValueType(1));
3864
3865 Lo = DAG.getNode(IsAdd ? ISD::UADDO : ISD::USUBO, dl, VTList, {LHSL, RHSL});
3866 Hi = DAG.getNode(CarryOp, dl, VTList, { LHSH, RHSH, Lo.getValue(1) });
3867
3868 Ovf = Hi.getValue(1);
3869 } else {
3870 // Expand the result by simply replacing it with the equivalent
3871 // non-overflow-checking operation.
3872 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3873 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3874 LHS, RHS);
3875 SplitInteger(Sum, Lo, Hi);
3876
3877 // Compute the overflow.
3878 //
3879 // LHSSign -> LHS < 0
3880 // RHSSign -> RHS < 0
3881 // SumSign -> Sum < 0
3882 //
3883 // Add:
3884 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3885 // Sub:
3886 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3887 //
3888 // To get better codegen we can rewrite this by doing bitwise math on
3889 // the integers and extract the final sign bit at the end. So the
3890 // above becomes:
3891 //
3892 // Add:
3893 // Overflow -> (~(LHS ^ RHS) & (LHS ^ Sum)) < 0
3894 // Sub:
3895 // Overflow -> ((LHS ^ RHS) & (LHS ^ Sum)) < 0
3896 //
3897 // NOTE: This is different than the expansion we do in expandSADDSUBO
3898 // because it is more costly to determine the RHS is > 0 for SSUBO with the
3899 // integers split.
3900 EVT VT = LHS.getValueType();
3901 SDValue SignsMatch = DAG.getNode(ISD::XOR, dl, VT, LHS, RHS);
3902 if (IsAdd)
3903 SignsMatch = DAG.getNOT(dl, SignsMatch, VT);
3904
3905 SDValue SumSignNE = DAG.getNode(ISD::XOR, dl, VT, LHS, Sum);
3906 Ovf = DAG.getNode(ISD::AND, dl, VT, SignsMatch, SumSignNE);
3907 EVT OType = Node->getValueType(1);
3908 Ovf = DAG.getSetCC(dl, OType, Ovf, DAG.getConstant(0, dl, VT), ISD::SETLT);
3909 }
3910
3911 // Use the calculated overflow everywhere.
3912 ReplaceValueWith(SDValue(Node, 1), Ovf);
3913}
3914
3915// Emit a call to __udivei4 and friends which require
3916// the arguments be based on the stack
3917// and extra argument that contains the number of bits of the operands.
3918// Returns the result of the call operation.
3919static SDValue ExpandExtIntRes_DIVREM(const TargetLowering &TLI,
3920 const RTLIB::Libcall &LC,
3921 SelectionDAG &DAG, SDNode *N,
3922 const SDLoc &DL, const EVT &VT) {
3923
3924 SDValue InChain = DAG.getEntryNode();
3925
3926 TargetLowering::ArgListTy Args;
3927 TargetLowering::ArgListEntry Entry;
3928
3929 // The signature of __udivei4 is
3930 // void __udivei4(unsigned int *quo, unsigned int *a, unsigned int *b,
3931 // unsigned int bits)
3932 EVT ArgVT = N->op_begin()->getValueType();
3933 assert(ArgVT.isInteger() && ArgVT.getSizeInBits() > 128 &&(static_cast <bool> (ArgVT.isInteger() && ArgVT
.getSizeInBits() > 128 && "Unexpected argument type for lowering"
) ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgVT.getSizeInBits() > 128 && \"Unexpected argument type for lowering\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3934
, __extension__ __PRETTY_FUNCTION__))
3934 "Unexpected argument type for lowering")(static_cast <bool> (ArgVT.isInteger() && ArgVT
.getSizeInBits() > 128 && "Unexpected argument type for lowering"
) ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgVT.getSizeInBits() > 128 && \"Unexpected argument type for lowering\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 3934
, __extension__ __PRETTY_FUNCTION__))
;
3935 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
3936
3937 SDValue Output = DAG.CreateStackTemporary(ArgVT);
3938 Entry.Node = Output;
3939 Entry.Ty = ArgTy->getPointerTo();
3940 Entry.IsSExt = false;
3941 Entry.IsZExt = false;
3942 Args.push_back(Entry);
3943
3944 for (const llvm::SDUse &Op : N->ops()) {
3945 SDValue StackPtr = DAG.CreateStackTemporary(ArgVT);
3946 InChain = DAG.getStore(InChain, DL, Op, StackPtr, MachinePointerInfo());
3947 Entry.Node = StackPtr;
3948 Entry.Ty = ArgTy->getPointerTo();
3949 Entry.IsSExt = false;
3950 Entry.IsZExt = false;
3951 Args.push_back(Entry);
3952 }
3953
3954 int Bits = N->getOperand(0)
3955 .getValueType()
3956 .getTypeForEVT(*DAG.getContext())
3957 ->getIntegerBitWidth();
3958 Entry.Node = DAG.getConstant(Bits, DL, TLI.getPointerTy(DAG.getDataLayout()));
3959 Entry.Ty = Type::getInt32Ty(*DAG.getContext());
3960 Entry.IsSExt = false;
3961 Entry.IsZExt = true;
3962 Args.push_back(Entry);
3963
3964 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
3965 TLI.getPointerTy(DAG.getDataLayout()));
3966
3967 TargetLowering::CallLoweringInfo CLI(DAG);
3968 CLI.setDebugLoc(DL)
3969 .setChain(InChain)
3970 .setLibCallee(TLI.getLibcallCallingConv(LC),
3971 Type::getVoidTy(*DAG.getContext()), Callee, std::move(Args))
3972 .setDiscardResult();
3973
3974 SDValue Chain = TLI.LowerCallTo(CLI).second;
3975
3976 return DAG.getLoad(ArgVT, DL, Chain, Output, MachinePointerInfo());
3977}
3978
3979void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
3980 SDValue &Lo, SDValue &Hi) {
3981 EVT VT = N->getValueType(0);
3982 SDLoc dl(N);
3983 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3984
3985 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3986 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3987 SplitInteger(Res.getValue(0), Lo, Hi);
3988 return;
3989 }
3990
3991 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3992 if (VT == MVT::i16)
3993 LC = RTLIB::SDIV_I16;
3994 else if (VT == MVT::i32)
3995 LC = RTLIB::SDIV_I32;
3996 else if (VT == MVT::i64)
3997 LC = RTLIB::SDIV_I64;
3998 else if (VT == MVT::i128)
3999 LC = RTLIB::SDIV_I128;
4000
4001 else {
4002 SDValue Result =
4003 ExpandExtIntRes_DIVREM(TLI, RTLIB::SDIV_IEXT, DAG, N, dl, VT);
4004 SplitInteger(Result, Lo, Hi);
4005 return;
4006 }
4007
4008 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unsupported SDIV!") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unsupported SDIV!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4008
, __extension__ __PRETTY_FUNCTION__))
;
4009
4010 TargetLowering::MakeLibCallOptions CallOptions;
4011 CallOptions.setSExt(true);
4012 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4013}
4014
4015void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
4016 SDValue &Lo, SDValue &Hi) {
4017 EVT VT = N->getValueType(0);
4018 SDLoc dl(N);
4019
4020 // If we can emit an efficient shift operation, do so now. Check to see if
4021 // the RHS is a constant.
4022 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
7
Assuming 'CN' is null
8
Taking false branch
4023 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
4024
4025 // If we can determine that the high bit of the shift is zero or one, even if
4026 // the low bits are variable, emit this shift in an optimized form.
4027 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
9
Calling 'DAGTypeLegalizer::ExpandShiftWithKnownAmountBit'
4028 return;
4029
4030 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
4031 unsigned PartsOpc;
4032 if (N->getOpcode() == ISD::SHL) {
4033 PartsOpc = ISD::SHL_PARTS;
4034 } else if (N->getOpcode() == ISD::SRL) {
4035 PartsOpc = ISD::SRL_PARTS;
4036 } else {
4037 assert(N->getOpcode() == ISD::SRA && "Unknown shift!")(static_cast <bool> (N->getOpcode() == ISD::SRA &&
"Unknown shift!") ? void (0) : __assert_fail ("N->getOpcode() == ISD::SRA && \"Unknown shift!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4037
, __extension__ __PRETTY_FUNCTION__))
;
4038 PartsOpc = ISD::SRA_PARTS;
4039 }
4040
4041 // Next check to see if the target supports this SHL_PARTS operation or if it
4042 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
4043 // size, but create a libcall instead.
4044 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4045 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
4046 const bool LegalOrCustom =
4047 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
4048 Action == TargetLowering::Custom;
4049
4050 if (LegalOrCustom && TLI.shouldExpandShift(DAG, N)) {
4051 // Expand the subcomponents.
4052 SDValue LHSL, LHSH;
4053 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
4054 EVT VT = LHSL.getValueType();
4055
4056 // If the shift amount operand is coming from a vector legalization it may
4057 // have an illegal type. Fix that first by casting the operand, otherwise
4058 // the new SHL_PARTS operation would need further legalization.
4059 SDValue ShiftOp = N->getOperand(1);
4060 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
4061 if (ShiftOp.getValueType() != ShiftTy)
4062 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
4063
4064 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
4065 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
4066 Hi = Lo.getValue(1);
4067 return;
4068 }
4069
4070 // Otherwise, emit a libcall.
4071 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4072 bool isSigned;
4073 if (N->getOpcode() == ISD::SHL) {
4074 isSigned = false; /*sign irrelevant*/
4075 if (VT == MVT::i16)
4076 LC = RTLIB::SHL_I16;
4077 else if (VT == MVT::i32)
4078 LC = RTLIB::SHL_I32;
4079 else if (VT == MVT::i64)
4080 LC = RTLIB::SHL_I64;
4081 else if (VT == MVT::i128)
4082 LC = RTLIB::SHL_I128;
4083 } else if (N->getOpcode() == ISD::SRL) {
4084 isSigned = false;
4085 if (VT == MVT::i16)
4086 LC = RTLIB::SRL_I16;
4087 else if (VT == MVT::i32)
4088 LC = RTLIB::SRL_I32;
4089 else if (VT == MVT::i64)
4090 LC = RTLIB::SRL_I64;
4091 else if (VT == MVT::i128)
4092 LC = RTLIB::SRL_I128;
4093 } else {
4094 assert(N->getOpcode() == ISD::SRA && "Unknown shift!")(static_cast <bool> (N->getOpcode() == ISD::SRA &&
"Unknown shift!") ? void (0) : __assert_fail ("N->getOpcode() == ISD::SRA && \"Unknown shift!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4094
, __extension__ __PRETTY_FUNCTION__))
;
4095 isSigned = true;
4096 if (VT == MVT::i16)
4097 LC = RTLIB::SRA_I16;
4098 else if (VT == MVT::i32)
4099 LC = RTLIB::SRA_I32;
4100 else if (VT == MVT::i64)
4101 LC = RTLIB::SRA_I64;
4102 else if (VT == MVT::i128)
4103 LC = RTLIB::SRA_I128;
4104 }
4105
4106 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
4107 EVT ShAmtTy =
4108 EVT::getIntegerVT(*DAG.getContext(), DAG.getLibInfo().getIntSize());
4109 SDValue ShAmt = DAG.getZExtOrTrunc(N->getOperand(1), dl, ShAmtTy);
4110 SDValue Ops[2] = {N->getOperand(0), ShAmt};
4111 TargetLowering::MakeLibCallOptions CallOptions;
4112 CallOptions.setSExt(isSigned);
4113 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4114 return;
4115 }
4116
4117 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
4118 llvm_unreachable("Unsupported shift!")::llvm::llvm_unreachable_internal("Unsupported shift!", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 4118)
;
4119}
4120
4121void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
4122 SDValue &Lo, SDValue &Hi) {
4123 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4124 SDLoc dl(N);
4125 SDValue Op = N->getOperand(0);
4126 if (Op.getValueType().bitsLE(NVT)) {
4127 // The low part is sign extension of the input (degenerates to a copy).
4128 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
4129 // The high part is obtained by SRA'ing all but one of the bits of low part.
4130 unsigned LoSize = NVT.getSizeInBits();
4131 Hi = DAG.getNode(
4132 ISD::SRA, dl, NVT, Lo,
4133 DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
4134 } else {
4135 // For example, extension of an i48 to an i64. The operand type necessarily
4136 // promotes to the result type, so will end up being expanded too.
4137 assert(getTypeAction(Op.getValueType()) ==(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4139
, __extension__ __PRETTY_FUNCTION__))
4138 TargetLowering::TypePromoteInteger &&(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4139
, __extension__ __PRETTY_FUNCTION__))
4139 "Only know how to promote this result!")(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4139
, __extension__ __PRETTY_FUNCTION__))
;
4140 SDValue Res = GetPromotedInteger(Op);
4141 assert(Res.getValueType() == N->getValueType(0) &&(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Res.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4142
, __extension__ __PRETTY_FUNCTION__))
4142 "Operand over promoted?")(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Res.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4142
, __extension__ __PRETTY_FUNCTION__))
;
4143 // Split the promoted operand. This will simplify when it is expanded.
4144 SplitInteger(Res, Lo, Hi);
4145 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
4146 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
4147 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
4148 ExcessBits)));
4149 }
4150}
4151
4152void DAGTypeLegalizer::
4153ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
4154 SDLoc dl(N);
4155 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4156 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
4157
4158 if (EVT.bitsLE(Lo.getValueType())) {
4159 // sext_inreg the low part if needed.
4160 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
4161 N->getOperand(1));
4162
4163 // The high part gets the sign extension from the lo-part. This handles
4164 // things like sextinreg V:i64 from i8.
4165 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
4166 DAG.getConstant(Hi.getValueSizeInBits() - 1, dl,
4167 TLI.getPointerTy(DAG.getDataLayout())));
4168 } else {
4169 // For example, extension of an i48 to an i64. Leave the low part alone,
4170 // sext_inreg the high part.
4171 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
4172 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
4173 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
4174 ExcessBits)));
4175 }
4176}
4177
4178void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
4179 SDValue &Lo, SDValue &Hi) {
4180 EVT VT = N->getValueType(0);
4181 SDLoc dl(N);
4182 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4183
4184 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
4185 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
4186 SplitInteger(Res.getValue(1), Lo, Hi);
4187 return;
4188 }
4189
4190 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4191 if (VT == MVT::i16)
4192 LC = RTLIB::SREM_I16;
4193 else if (VT == MVT::i32)
4194 LC = RTLIB::SREM_I32;
4195 else if (VT == MVT::i64)
4196 LC = RTLIB::SREM_I64;
4197 else if (VT == MVT::i128)
4198 LC = RTLIB::SREM_I128;
4199
4200 else {
4201 SDValue Result =
4202 ExpandExtIntRes_DIVREM(TLI, RTLIB::SREM_IEXT, DAG, N, dl, VT);
4203 SplitInteger(Result, Lo, Hi);
4204 return;
4205 }
4206
4207 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unsupported SREM!") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unsupported SREM!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4207
, __extension__ __PRETTY_FUNCTION__))
;
4208
4209 TargetLowering::MakeLibCallOptions CallOptions;
4210 CallOptions.setSExt(true);
4211 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4212}
4213
4214void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
4215 SDValue &Lo, SDValue &Hi) {
4216 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4217 SDLoc dl(N);
4218 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
4219 Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
4220 N->getOperand(0),
4221 DAG.getConstant(NVT.getSizeInBits(), dl,
4222 TLI.getPointerTy(DAG.getDataLayout())));
4223 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
4224}
4225
4226void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
4227 SDValue &Lo, SDValue &Hi) {
4228 EVT VT = N->getValueType(0);
4229 SDLoc dl(N);
4230
4231 if (N->getOpcode() == ISD::UMULO) {
4232 // This section expands the operation into the following sequence of
4233 // instructions. `iNh` here refers to a type which has half the bit width of
4234 // the type the original operation operated on.
4235 //
4236 // %0 = %LHS.HI != 0 && %RHS.HI != 0
4237 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
4238 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
4239 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
4240 // %4 = add iNh %1.0, %2.0 as iN
4241 // %5 = { iNh, i1 } @uadd.with.overflow.iNh(iNh %4, iNh %3.HIGH)
4242 //
4243 // %lo = %3.LO
4244 // %hi = %5.0
4245 // %ovf = %0 || %1.1 || %2.1 || %5.1
4246 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
4247 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
4248 GetExpandedInteger(LHS, LHSLow, LHSHigh);
4249 GetExpandedInteger(RHS, RHSLow, RHSHigh);
4250 EVT HalfVT = LHSLow.getValueType();
4251 EVT BitVT = N->getValueType(1);
4252 SDVTList VTHalfWithO = DAG.getVTList(HalfVT, BitVT);
4253
4254 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
4255 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
4256 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
4257 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
4258
4259 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, LHSHigh, RHSLow);
4260 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
4261
4262 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfWithO, RHSHigh, LHSLow);
4263 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
4264
4265 SDValue HighSum = DAG.getNode(ISD::ADD, dl, HalfVT, One, Two);
4266
4267 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
4268 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
4269 // operation recursively legalized?).
4270 //
4271 // Many backends understand this pattern and will convert into LOHI
4272 // themselves, if applicable.
4273 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
4274 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
4275 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
4276 SplitInteger(Three, Lo, Hi);
4277
4278 Hi = DAG.getNode(ISD::UADDO, dl, VTHalfWithO, Hi, HighSum);
4279 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Hi.getValue(1));
4280 ReplaceValueWith(SDValue(N, 1), Overflow);
4281 return;
4282 }
4283
4284 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
4285 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
4286 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
4287
4288 // Replace this with a libcall that will check overflow.
4289 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4290 if (VT == MVT::i32)
4291 LC = RTLIB::MULO_I32;
4292 else if (VT == MVT::i64)
4293 LC = RTLIB::MULO_I64;
4294 else if (VT == MVT::i128)
4295 LC = RTLIB::MULO_I128;
4296
4297 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
4298 // FIXME: This is not an optimal expansion, but better than crashing.
4299 EVT WideVT =
4300 EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2);
4301 SDValue LHS = DAG.getNode(ISD::SIGN_EXTEND, dl, WideVT, N->getOperand(0));
4302 SDValue RHS = DAG.getNode(ISD::SIGN_EXTEND, dl, WideVT, N->getOperand(1));
4303 SDValue Mul = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS);
4304 SDValue MulLo, MulHi;
4305 SplitInteger(Mul, MulLo, MulHi);
4306 SDValue SRA =
4307 DAG.getNode(ISD::SRA, dl, VT, MulLo,
4308 DAG.getConstant(VT.getScalarSizeInBits() - 1, dl, VT));
4309 SDValue Overflow =
4310 DAG.getSetCC(dl, N->getValueType(1), MulHi, SRA, ISD::SETNE);
4311 SplitInteger(MulLo, Lo, Hi);
4312 ReplaceValueWith(SDValue(N, 1), Overflow);
4313 return;
4314 }
4315
4316 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
4317 // Temporary for the overflow value, default it to zero.
4318 SDValue Chain =
4319 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
4320 MachinePointerInfo());
4321
4322 TargetLowering::ArgListTy Args;
4323 TargetLowering::ArgListEntry Entry;
4324 for (const SDValue &Op : N->op_values()) {
4325 EVT ArgVT = Op.getValueType();
4326 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
4327 Entry.Node = Op;
4328 Entry.Ty = ArgTy;
4329 Entry.IsSExt = true;
4330 Entry.IsZExt = false;
4331 Args.push_back(Entry);
4332 }
4333
4334 // Also pass the address of the overflow check.
4335 Entry.Node = Temp;
4336 Entry.Ty = PtrTy->getPointerTo();
4337 Entry.IsSExt = true;
4338 Entry.IsZExt = false;
4339 Args.push_back(Entry);
4340
4341 SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
4342
4343 TargetLowering::CallLoweringInfo CLI(DAG);
4344 CLI.setDebugLoc(dl)
4345 .setChain(Chain)
4346 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args))
4347 .setSExtResult();
4348
4349 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
4350
4351 SplitInteger(CallInfo.first, Lo, Hi);
4352 SDValue Temp2 =
4353 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
4354 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
4355 DAG.getConstant(0, dl, PtrVT),
4356 ISD::SETNE);
4357 // Use the overflow from the libcall everywhere.
4358 ReplaceValueWith(SDValue(N, 1), Ofl);
4359}
4360
4361void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
4362 SDValue &Lo, SDValue &Hi) {
4363 EVT VT = N->getValueType(0);
4364 SDLoc dl(N);
4365 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4366
4367 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
4368 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
4369 SplitInteger(Res.getValue(0), Lo, Hi);
4370 return;
4371 }
4372
4373 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4374 if (VT == MVT::i16)
4375 LC = RTLIB::UDIV_I16;
4376 else if (VT == MVT::i32)
4377 LC = RTLIB::UDIV_I32;
4378 else if (VT == MVT::i64)
4379 LC = RTLIB::UDIV_I64;
4380 else if (VT == MVT::i128)
4381 LC = RTLIB::UDIV_I128;
4382
4383 else {
4384 SDValue Result =
4385 ExpandExtIntRes_DIVREM(TLI, RTLIB::UDIV_IEXT, DAG, N, dl, VT);
4386 SplitInteger(Result, Lo, Hi);
4387 return;
4388 }
4389
4390 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unsupported UDIV!") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unsupported UDIV!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4390
, __extension__ __PRETTY_FUNCTION__))
;
4391
4392 TargetLowering::MakeLibCallOptions CallOptions;
4393 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4394}
4395
4396void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
4397 SDValue &Lo, SDValue &Hi) {
4398 EVT VT = N->getValueType(0);
4399 SDLoc dl(N);
4400 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
4401
4402 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
4403 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
4404 SplitInteger(Res.getValue(1), Lo, Hi);
4405 return;
4406 }
4407
4408 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4409 if (VT == MVT::i16)
4410 LC = RTLIB::UREM_I16;
4411 else if (VT == MVT::i32)
4412 LC = RTLIB::UREM_I32;
4413 else if (VT == MVT::i64)
4414 LC = RTLIB::UREM_I64;
4415 else if (VT == MVT::i128)
4416 LC = RTLIB::UREM_I128;
4417
4418 else {
4419 SDValue Result =
4420 ExpandExtIntRes_DIVREM(TLI, RTLIB::UREM_IEXT, DAG, N, dl, VT);
4421 SplitInteger(Result, Lo, Hi);
4422 return;
4423 }
4424
4425 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Unsupported UREM!") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unsupported UREM!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4425
, __extension__ __PRETTY_FUNCTION__))
;
4426
4427 TargetLowering::MakeLibCallOptions CallOptions;
4428 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
4429}
4430
4431void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
4432 SDValue &Lo, SDValue &Hi) {
4433 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4434 SDLoc dl(N);
4435 SDValue Op = N->getOperand(0);
4436 if (Op.getValueType().bitsLE(NVT)) {
4437 // The low part is zero extension of the input (degenerates to a copy).
4438 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
4439 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
4440 } else {
4441 // For example, extension of an i48 to an i64. The operand type necessarily
4442 // promotes to the result type, so will end up being expanded too.
4443 assert(getTypeAction(Op.getValueType()) ==(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4445
, __extension__ __PRETTY_FUNCTION__))
4444 TargetLowering::TypePromoteInteger &&(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4445
, __extension__ __PRETTY_FUNCTION__))
4445 "Only know how to promote this result!")(static_cast <bool> (getTypeAction(Op.getValueType()) ==
TargetLowering::TypePromoteInteger && "Only know how to promote this result!"
) ? void (0) : __assert_fail ("getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger && \"Only know how to promote this result!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4445
, __extension__ __PRETTY_FUNCTION__))
;
4446 SDValue Res = GetPromotedInteger(Op);
4447 assert(Res.getValueType() == N->getValueType(0) &&(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Res.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4448
, __extension__ __PRETTY_FUNCTION__))
4448 "Operand over promoted?")(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && "Operand over promoted?") ? void (0) : __assert_fail
("Res.getValueType() == N->getValueType(0) && \"Operand over promoted?\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4448
, __extension__ __PRETTY_FUNCTION__))
;
4449 // Split the promoted operand. This will simplify when it is expanded.
4450 SplitInteger(Res, Lo, Hi);
4451 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
4452 Hi = DAG.getZeroExtendInReg(Hi, dl,
4453 EVT::getIntegerVT(*DAG.getContext(),
4454 ExcessBits));
4455 }
4456}
4457
4458void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
4459 SDValue &Lo, SDValue &Hi) {
4460 SDLoc dl(N);
4461 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
4462 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
4463 SDValue Zero = DAG.getConstant(0, dl, VT);
4464 SDValue Swap = DAG.getAtomicCmpSwap(
4465 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
4466 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
4467 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
4468
4469 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
4470 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
4471}
4472
4473void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
4474 SDValue &Lo, SDValue &Hi) {
4475 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
4476 // both halves independently.
4477 SDValue Res = TLI.expandVecReduce(N, DAG);
4478 SplitInteger(Res, Lo, Hi);
4479}
4480
4481void DAGTypeLegalizer::ExpandIntRes_Rotate(SDNode *N,
4482 SDValue &Lo, SDValue &Hi) {
4483 // Delegate to funnel-shift expansion.
4484 SDLoc DL(N);
4485 unsigned Opcode = N->getOpcode() == ISD::ROTL ? ISD::FSHL : ISD::FSHR;
4486 SDValue Res = DAG.getNode(Opcode, DL, N->getValueType(0), N->getOperand(0),
4487 N->getOperand(0), N->getOperand(1));
4488 SplitInteger(Res, Lo, Hi);
4489}
4490
4491void DAGTypeLegalizer::ExpandIntRes_FunnelShift(SDNode *N, SDValue &Lo,
4492 SDValue &Hi) {
4493 // Values numbered from least significant to most significant.
4494 SDValue In1, In2, In3, In4;
4495 GetExpandedInteger(N->getOperand(0), In3, In4);
4496 GetExpandedInteger(N->getOperand(1), In1, In2);
4497 EVT HalfVT = In1.getValueType();
4498
4499 SDLoc DL(N);
4500 unsigned Opc = N->getOpcode();
4501 SDValue ShAmt = N->getOperand(2);
4502 EVT ShAmtVT = ShAmt.getValueType();
4503 EVT ShAmtCCVT = getSetCCResultType(ShAmtVT);
4504
4505 // If the shift amount is at least half the bitwidth, swap the inputs.
4506 unsigned HalfVTBits = HalfVT.getScalarSizeInBits();
4507 SDValue AndNode = DAG.getNode(ISD::AND, DL, ShAmtVT, ShAmt,
4508 DAG.getConstant(HalfVTBits, DL, ShAmtVT));
4509 SDValue Cond =
4510 DAG.getSetCC(DL, ShAmtCCVT, AndNode, DAG.getConstant(0, DL, ShAmtVT),
4511 Opc == ISD::FSHL ? ISD::SETNE : ISD::SETEQ);
4512
4513 // Expand to a pair of funnel shifts.
4514 EVT NewShAmtVT = TLI.getShiftAmountTy(HalfVT, DAG.getDataLayout());
4515 SDValue NewShAmt = DAG.getAnyExtOrTrunc(ShAmt, DL, NewShAmtVT);
4516
4517 SDValue Select1 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In1, In2);
4518 SDValue Select2 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In2, In3);
4519 SDValue Select3 = DAG.getNode(ISD::SELECT, DL, HalfVT, Cond, In3, In4);
4520 Lo = DAG.getNode(Opc, DL, HalfVT, Select2, Select1, NewShAmt);
4521 Hi = DAG.getNode(Opc, DL, HalfVT, Select3, Select2, NewShAmt);
4522}
4523
4524void DAGTypeLegalizer::ExpandIntRes_VSCALE(SDNode *N, SDValue &Lo,
4525 SDValue &Hi) {
4526 EVT VT = N->getValueType(0);
4527 EVT HalfVT =
4528 EVT::getIntegerVT(*DAG.getContext(), N->getValueSizeInBits(0) / 2);
4529 SDLoc dl(N);
4530
4531 // We assume VSCALE(1) fits into a legal integer.
4532 APInt One(HalfVT.getSizeInBits(), 1);
4533 SDValue VScaleBase = DAG.getVScale(dl, HalfVT, One);
4534 VScaleBase = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, VScaleBase);
4535 SDValue Res = DAG.getNode(ISD::MUL, dl, VT, VScaleBase, N->getOperand(0));
4536 SplitInteger(Res, Lo, Hi);
4537}
4538
4539//===----------------------------------------------------------------------===//
4540// Integer Operand Expansion
4541//===----------------------------------------------------------------------===//
4542
4543/// ExpandIntegerOperand - This method is called when the specified operand of
4544/// the specified node is found to need expansion. At this point, all of the
4545/// result types of the node are known to be legal, but other operands of the
4546/// node may need promotion or expansion as well as the specified one.
4547bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
4548 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG);do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Expand integer operand: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
4549 dbgs() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("legalize-types")) { dbgs() << "Expand integer operand: "
; N->dump(&DAG); dbgs() << "\n"; } } while (false
)
;
4550 SDValue Res = SDValue();
4551
4552 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
4553 return false;
4554
4555 switch (N->getOpcode()) {
4556 default:
4557 #ifndef NDEBUG
4558 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
4559 N->dump(&DAG); dbgs() << "\n";
4560 #endif
4561 report_fatal_error("Do not know how to expand this operator's operand!");
4562
4563 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
4564 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
4565 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
4566 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
4567 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
4568 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
4569 case ISD::SPLAT_VECTOR: Res = ExpandIntOp_SPLAT_VECTOR(N); break;
4570 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
4571 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
4572 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
4573 case ISD::STRICT_SINT_TO_FP:
4574 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
4575 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
4576 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
4577 case ISD::STRICT_UINT_TO_FP:
4578 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
4579
4580 case ISD::SHL:
4581 case ISD::SRA:
4582 case ISD::SRL:
4583 case ISD::ROTL:
4584 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
4585 case ISD::RETURNADDR:
4586 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
4587
4588 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
4589 }
4590
4591 // If the result is null, the sub-method took care of registering results etc.
4592 if (!Res.getNode()) return false;
4593
4594 // If the result is N, the sub-method updated N in place. Tell the legalizer
4595 // core about this.
4596 if (Res.getNode() == N)
4597 return true;
4598
4599 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && N->getNumValues() == 1 && "Invalid operand expansion"
) ? void (0) : __assert_fail ("Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 && \"Invalid operand expansion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4600
, __extension__ __PRETTY_FUNCTION__))
4600 "Invalid operand expansion")(static_cast <bool> (Res.getValueType() == N->getValueType
(0) && N->getNumValues() == 1 && "Invalid operand expansion"
) ? void (0) : __assert_fail ("Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 && \"Invalid operand expansion\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4600
, __extension__ __PRETTY_FUNCTION__))
;
4601
4602 ReplaceValueWith(SDValue(N, 0), Res);
4603 return false;
4604}
4605
4606/// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
4607/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
4608void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
4609 SDValue &NewRHS,
4610 ISD::CondCode &CCCode,
4611 const SDLoc &dl) {
4612 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
4613 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
4614 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
4615
4616 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
4617 if (RHSLo == RHSHi) {
4618 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
4619 if (RHSCST->isAllOnes()) {
4620 // Equality comparison to -1.
4621 NewLHS = DAG.getNode(ISD::AND, dl,
4622 LHSLo.getValueType(), LHSLo, LHSHi);
4623 NewRHS = RHSLo;
4624 return;
4625 }
4626 }
4627 }
4628
4629 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
4630 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
4631 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
4632 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
4633 return;
4634 }
4635
4636 // If this is a comparison of the sign bit, just look at the top part.
4637 // X > -1, x < 0
4638 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
4639 if ((CCCode == ISD::SETLT && CST->isZero()) || // X < 0
4640 (CCCode == ISD::SETGT && CST->isAllOnes())) { // X > -1
4641 NewLHS = LHSHi;
4642 NewRHS = RHSHi;
4643 return;
4644 }
4645
4646 // FIXME: This generated code sucks.
4647 ISD::CondCode LowCC;
4648 switch (CCCode) {
4649 default: llvm_unreachable("Unknown integer setcc!")::llvm::llvm_unreachable_internal("Unknown integer setcc!", "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp"
, 4649)
;
4650 case ISD::SETLT:
4651 case ISD::SETULT: LowCC = ISD::SETULT; break;
4652 case ISD::SETGT:
4653 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4654 case ISD::SETLE:
4655 case ISD::SETULE: LowCC = ISD::SETULE; break;
4656 case ISD::SETGE:
4657 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4658 }
4659
4660 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
4661 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
4662 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
4663
4664 // NOTE: on targets without efficient SELECT of bools, we can always use
4665 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
4666 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
4667 nullptr);
4668 SDValue LoCmp, HiCmp;
4669 if (TLI.isTypeLegal(LHSLo.getValueType()) &&
4670 TLI.isTypeLegal(RHSLo.getValueType()))
4671 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
4672 RHSLo, LowCC, false, DagCombineInfo, dl);
4673 if (!LoCmp.getNode())
4674 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
4675 RHSLo, LowCC);
4676 if (TLI.isTypeLegal(LHSHi.getValueType()) &&
4677 TLI.isTypeLegal(RHSHi.getValueType()))
4678 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
4679 RHSHi, CCCode, false, DagCombineInfo, dl);
4680 if (!HiCmp.getNode())
4681 HiCmp =
4682 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
4683 LHSHi, RHSHi, DAG.getCondCode(CCCode));
4684
4685 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
4686 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
4687
4688 bool EqAllowed = (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4689 CCCode == ISD::SETUGE || CCCode == ISD::SETULE);
4690
4691 // FIXME: Is the HiCmpC->isOne() here correct for
4692 // ZeroOrNegativeOneBooleanContent.
4693 if ((EqAllowed && (HiCmpC && HiCmpC->isZero())) ||
4694 (!EqAllowed &&
4695 ((HiCmpC && HiCmpC->isOne()) || (LoCmpC && LoCmpC->isZero())))) {
4696 // For LE / GE, if high part is known false, ignore the low part.
4697 // For LT / GT: if low part is known false, return the high part.
4698 // if high part is known true, ignore the low part.
4699 NewLHS = HiCmp;
4700 NewRHS = SDValue();
4701 return;
4702 }
4703
4704 if (LHSHi == RHSHi) {
4705 // Comparing the low bits is enough.
4706 NewLHS = LoCmp;
4707 NewRHS = SDValue();
4708 return;
4709 }
4710
4711 // Lower with SETCCCARRY if the target supports it.
4712 EVT HiVT = LHSHi.getValueType();
4713 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
4714 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
4715
4716 // FIXME: Make all targets support this, then remove the other lowering.
4717 if (HasSETCCCARRY) {
4718 // SETCCCARRY can detect < and >= directly. For > and <=, flip
4719 // operands and condition code.
4720 bool FlipOperands = false;
4721 switch (CCCode) {
4722 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
4723 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
4724 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
4725 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
4726 default: break;
4727 }
4728 if (FlipOperands) {
4729 std::swap(LHSLo, RHSLo);
4730 std::swap(LHSHi, RHSHi);
4731 }
4732 // Perform a wide subtraction, feeding the carry from the low part into
4733 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
4734 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
4735 // zero or positive iff LHS >= RHS.
4736 EVT LoVT = LHSLo.getValueType();
4737 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
4738 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
4739 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
4740 LHSHi, RHSHi, LowCmp.getValue(1),
4741 DAG.getCondCode(CCCode));
4742 NewLHS = Res;
4743 NewRHS = SDValue();
4744 return;
4745 }
4746
4747 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
4748 false, DagCombineInfo, dl);
4749 if (!NewLHS.getNode())
4750 NewLHS =
4751 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
4752 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
4753 NewRHS = SDValue();
4754}
4755
4756SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
4757 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
4758 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
4759 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
4760
4761 // If ExpandSetCCOperands returned a scalar, we need to compare the result
4762 // against zero to select between true and false values.
4763 if (!NewRHS.getNode()) {
4764 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
4765 CCCode = ISD::SETNE;
4766 }
4767
4768 // Update N to have the operands specified.
4769 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
4770 DAG.getCondCode(CCCode), NewLHS, NewRHS,
4771 N->getOperand(4)), 0);
4772}
4773
4774SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
4775 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
4776 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
4777 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
4778
4779 // If ExpandSetCCOperands returned a scalar, we need to compare the result
4780 // against zero to select between true and false values.
4781 if (!NewRHS.getNode()) {
4782 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
4783 CCCode = ISD::SETNE;
4784 }
4785
4786 // Update N to have the operands specified.
4787 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
4788 N->getOperand(2), N->getOperand(3),
4789 DAG.getCondCode(CCCode)), 0);
4790}
4791
4792SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
4793 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
4794 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
4795 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
4796
4797 // If ExpandSetCCOperands returned a scalar, use it.
4798 if (!NewRHS.getNode()) {
4799 assert(NewLHS.getValueType() == N->getValueType(0) &&(static_cast <bool> (NewLHS.getValueType() == N->getValueType
(0) && "Unexpected setcc expansion!") ? void (0) : __assert_fail
("NewLHS.getValueType() == N->getValueType(0) && \"Unexpected setcc expansion!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4800
, __extension__ __PRETTY_FUNCTION__))
4800 "Unexpected setcc expansion!")(static_cast <bool> (NewLHS.getValueType() == N->getValueType
(0) && "Unexpected setcc expansion!") ? void (0) : __assert_fail
("NewLHS.getValueType() == N->getValueType(0) && \"Unexpected setcc expansion!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4800
, __extension__ __PRETTY_FUNCTION__))
;
4801 return NewLHS;
4802 }
4803
4804 // Otherwise, update N to have the operands specified.
4805 return SDValue(
4806 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
4807}
4808
4809SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
4810 SDValue LHS = N->getOperand(0);
4811 SDValue RHS = N->getOperand(1);
4812 SDValue Carry = N->getOperand(2);
4813 SDValue Cond = N->getOperand(3);
4814 SDLoc dl = SDLoc(N);
4815
4816 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
4817 GetExpandedInteger(LHS, LHSLo, LHSHi);
4818 GetExpandedInteger(RHS, RHSLo, RHSHi);
4819
4820 // Expand to a SUBE for the low part and a smaller SETCCCARRY for the high.
4821 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
4822 SDValue LowCmp = DAG.getNode(ISD::SUBCARRY, dl, VTList, LHSLo, RHSLo, Carry);
4823 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
4824 LowCmp.getValue(1), Cond);
4825}
4826
4827SDValue DAGTypeLegalizer::ExpandIntOp_SPLAT_VECTOR(SDNode *N) {
4828 // Split the operand and replace with SPLAT_VECTOR_PARTS.
4829 SDValue Lo, Hi;
4830 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4831 return DAG.getNode(ISD::SPLAT_VECTOR_PARTS, SDLoc(N), N->getValueType(0), Lo,
4832 Hi);
4833}
4834
4835SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
4836 // The value being shifted is legal, but the shift amount is too big.
4837 // It follows that either the result of the shift is undefined, or the
4838 // upper half of the shift amount is zero. Just use the lower half.
4839 SDValue Lo, Hi;
4840 GetExpandedInteger(N->getOperand(1), Lo, Hi);
4841 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
4842}
4843
4844SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
4845 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
4846 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
4847 // constant to valid type.
4848 SDValue Lo, Hi;
4849 GetExpandedInteger(N->getOperand(0), Lo, Hi);
4850 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
4851}
4852
4853SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
4854 bool IsStrict = N->isStrictFPOpcode();
4855 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4856 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4857 EVT DstVT = N->getValueType(0);
4858 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
4859 assert(LC != RTLIB::UNKNOWN_LIBCALL &&(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Don't know how to expand this SINT_TO_FP!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Don't know how to expand this SINT_TO_FP!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4860
, __extension__ __PRETTY_FUNCTION__))
4860 "Don't know how to expand this SINT_TO_FP!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Don't know how to expand this SINT_TO_FP!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Don't know how to expand this SINT_TO_FP!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4860
, __extension__ __PRETTY_FUNCTION__))
;
4861 TargetLowering::MakeLibCallOptions CallOptions;
4862 CallOptions.setSExt(true);
4863 std::pair<SDValue, SDValue> Tmp =
4864 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
4865
4866 if (!IsStrict)
4867 return Tmp.first;
4868
4869 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4870 ReplaceValueWith(SDValue(N, 0), Tmp.first);
4871 return SDValue();
4872}
4873
4874SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
4875 if (N->isAtomic()) {
4876 // It's typical to have larger CAS than atomic store instructions.
4877 SDLoc dl(N);
4878 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
4879 N->getMemoryVT(),
4880 N->getOperand(0), N->getOperand(2),
4881 N->getOperand(1),
4882 N->getMemOperand());
4883 return Swap.getValue(1);
4884 }
4885 if (ISD::isNormalStore(N))
4886 return ExpandOp_NormalStore(N, OpNo);
4887
4888 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!")(static_cast <bool> (ISD::isUNINDEXEDStore(N) &&
"Indexed store during type legalization!") ? void (0) : __assert_fail
("ISD::isUNINDEXEDStore(N) && \"Indexed store during type legalization!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4888
, __extension__ __PRETTY_FUNCTION__))
;
4889 assert(OpNo == 1 && "Can only expand the stored value so far")(static_cast <bool> (OpNo == 1 && "Can only expand the stored value so far"
) ? void (0) : __assert_fail ("OpNo == 1 && \"Can only expand the stored value so far\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4889
, __extension__ __PRETTY_FUNCTION__))
;
4890
4891 EVT VT = N->getOperand(1).getValueType();
4892 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4893 SDValue Ch = N->getChain();
4894 SDValue Ptr = N->getBasePtr();
4895 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
4896 AAMDNodes AAInfo = N->getAAInfo();
4897 SDLoc dl(N);
4898 SDValue Lo, Hi;
4899
4900 assert(NVT.isByteSized() && "Expanded type not byte sized!")(static_cast <bool> (NVT.isByteSized() && "Expanded type not byte sized!"
) ? void (0) : __assert_fail ("NVT.isByteSized() && \"Expanded type not byte sized!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4900
, __extension__ __PRETTY_FUNCTION__))
;
4901
4902 if (N->getMemoryVT().bitsLE(NVT)) {
4903 GetExpandedInteger(N->getValue(), Lo, Hi);
4904 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
4905 N->getMemoryVT(), N->getOriginalAlign(), MMOFlags,
4906 AAInfo);
4907 }
4908
4909 if (DAG.getDataLayout().isLittleEndian()) {
4910 // Little-endian - low bits are at low addresses.
4911 GetExpandedInteger(N->getValue(), Lo, Hi);
4912
4913 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
4914 N->getOriginalAlign(), MMOFlags, AAInfo);
4915
4916 unsigned ExcessBits =
4917 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
4918 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
4919
4920 // Increment the pointer to the other half.
4921 unsigned IncrementSize = NVT.getSizeInBits()/8;
4922 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
4923 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
4924 N->getPointerInfo().getWithOffset(IncrementSize),
4925 NEVT, N->getOriginalAlign(), MMOFlags, AAInfo);
4926 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4927 }
4928
4929 // Big-endian - high bits are at low addresses. Favor aligned stores at
4930 // the cost of some bit-fiddling.
4931 GetExpandedInteger(N->getValue(), Lo, Hi);
4932
4933 EVT ExtVT = N->getMemoryVT();
4934 unsigned EBytes = ExtVT.getStoreSize();
4935 unsigned IncrementSize = NVT.getSizeInBits()/8;
4936 unsigned ExcessBits = (EBytes - IncrementSize)*8;
4937 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
4938 ExtVT.getSizeInBits() - ExcessBits);
4939
4940 if (ExcessBits < NVT.getSizeInBits()) {
4941 // Transfer high bits from the top of Lo to the bottom of Hi.
4942 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
4943 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
4944 TLI.getPointerTy(DAG.getDataLayout())));
4945 Hi = DAG.getNode(
4946 ISD::OR, dl, NVT, Hi,
4947 DAG.getNode(ISD::SRL, dl, NVT, Lo,
4948 DAG.getConstant(ExcessBits, dl,
4949 TLI.getPointerTy(DAG.getDataLayout()))));
4950 }
4951
4952 // Store both the high bits and maybe some of the low bits.
4953 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT,
4954 N->getOriginalAlign(), MMOFlags, AAInfo);
4955
4956 // Increment the pointer to the other half.
4957 Ptr = DAG.getObjectPtrOffset(dl, Ptr, TypeSize::Fixed(IncrementSize));
4958 // Store the lowest ExcessBits bits in the second half.
4959 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
4960 N->getPointerInfo().getWithOffset(IncrementSize),
4961 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
4962 N->getOriginalAlign(), MMOFlags, AAInfo);
4963 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4964}
4965
4966SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
4967 SDValue InL, InH;
4968 GetExpandedInteger(N->getOperand(0), InL, InH);
4969 // Just truncate the low part of the source.
4970 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
4971}
4972
4973SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
4974 bool IsStrict = N->isStrictFPOpcode();
4975 SDValue Chain = IsStrict ? N->getOperand(0) : SDValue();
4976 SDValue Op = N->getOperand(IsStrict ? 1 : 0);
4977 EVT DstVT = N->getValueType(0);
4978 RTLIB::Libcall LC = RTLIB::getUINTTOFP(Op.getValueType(), DstVT);
4979 assert(LC != RTLIB::UNKNOWN_LIBCALL &&(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Don't know how to expand this UINT_TO_FP!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Don't know how to expand this UINT_TO_FP!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4980
, __extension__ __PRETTY_FUNCTION__))
4980 "Don't know how to expand this UINT_TO_FP!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL &&
"Don't know how to expand this UINT_TO_FP!") ? void (0) : __assert_fail
("LC != RTLIB::UNKNOWN_LIBCALL && \"Don't know how to expand this UINT_TO_FP!\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 4980
, __extension__ __PRETTY_FUNCTION__))
;
4981 TargetLowering::MakeLibCallOptions CallOptions;
4982 CallOptions.setSExt(true);
4983 std::pair<SDValue, SDValue> Tmp =
4984 TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N), Chain);
4985
4986 if (!IsStrict)
4987 return Tmp.first;
4988
4989 ReplaceValueWith(SDValue(N, 1), Tmp.second);
4990 ReplaceValueWith(SDValue(N, 0), Tmp.first);
4991 return SDValue();
4992}
4993
4994SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
4995 SDLoc dl(N);
4996 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
4997 cast<AtomicSDNode>(N)->getMemoryVT(),
4998 N->getOperand(0),
4999 N->getOperand(1), N->getOperand(2),
5000 cast<AtomicSDNode>(N)->getMemOperand());
5001 return Swap.getValue(1);
5002}
5003
5004SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SPLICE(SDNode *N) {
5005 SDLoc dl(N);
5006
5007 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5008 SDValue V1 = GetPromotedInteger(N->getOperand(1));
5009 EVT OutVT = V0.getValueType();
5010
5011 return DAG.getNode(ISD::VECTOR_SPLICE, dl, OutVT, V0, V1, N->getOperand(2));
5012}
5013
5014SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
5015
5016 EVT OutVT = N->getValueType(0);
5017 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5018 assert(NOutVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5018
, __extension__ __PRETTY_FUNCTION__))
;
5019 EVT NOutVTElem = NOutVT.getVectorElementType();
5020
5021 SDLoc dl(N);
5022 SDValue BaseIdx = N->getOperand(1);
5023
5024 // TODO: We may be able to use this for types other than scalable
5025 // vectors and fix those tests that expect BUILD_VECTOR to be used
5026 if (OutVT.isScalableVector()) {
5027 SDValue InOp0 = N->getOperand(0);
5028 EVT InVT = InOp0.getValueType();
5029
5030 // Try and extract from a smaller type so that it eventually falls
5031 // into the promotion code below.
5032 if (getTypeAction(InVT) == TargetLowering::TypeSplitVector ||
5033 getTypeAction(InVT) == TargetLowering::TypeLegal) {
5034 EVT NInVT = InVT.getHalfNumVectorElementsVT(*DAG.getContext());
5035 unsigned NElts = NInVT.getVectorMinNumElements();
5036 uint64_t IdxVal = cast<ConstantSDNode>(BaseIdx)->getZExtValue();
5037
5038 SDValue Step1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NInVT, InOp0,
5039 DAG.getConstant(alignDown(IdxVal, NElts), dl,
5040 BaseIdx.getValueType()));
5041 SDValue Step2 = DAG.getNode(
5042 ISD::EXTRACT_SUBVECTOR, dl, OutVT, Step1,
5043 DAG.getConstant(IdxVal % NElts, dl, BaseIdx.getValueType()));
5044 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Step2);
5045 }
5046
5047 // Try and extract from a widened type.
5048 if (getTypeAction(InVT) == TargetLowering::TypeWidenVector) {
5049 SDValue Ops[] = {GetWidenedVector(InOp0), BaseIdx};
5050 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), OutVT, Ops);
5051 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
5052 }
5053
5054 // Promote operands and see if this is handled by target lowering,
5055 // Otherwise, use the BUILD_VECTOR approach below
5056 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
5057 // Collect the (promoted) operands
5058 SDValue Ops[] = { GetPromotedInteger(InOp0), BaseIdx };
5059
5060 EVT PromEltVT = Ops[0].getValueType().getVectorElementType();
5061 assert(PromEltVT.bitsLE(NOutVTElem) &&(static_cast <bool> (PromEltVT.bitsLE(NOutVTElem) &&
"Promoted operand has an element type greater than result") ?
void (0) : __assert_fail ("PromEltVT.bitsLE(NOutVTElem) && \"Promoted operand has an element type greater than result\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5062
, __extension__ __PRETTY_FUNCTION__))
5062 "Promoted operand has an element type greater than result")(static_cast <bool> (PromEltVT.bitsLE(NOutVTElem) &&
"Promoted operand has an element type greater than result") ?
void (0) : __assert_fail ("PromEltVT.bitsLE(NOutVTElem) && \"Promoted operand has an element type greater than result\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5062
, __extension__ __PRETTY_FUNCTION__))
;
5063
5064 EVT ExtVT = NOutVT.changeVectorElementType(PromEltVT);
5065 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), ExtVT, Ops);
5066 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, Ext);
5067 }
5068 }
5069
5070 if (OutVT.isScalableVector())
5071 report_fatal_error("Unable to promote scalable types using BUILD_VECTOR");
5072
5073 SDValue InOp0 = N->getOperand(0);
5074 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
5075 InOp0 = GetPromotedInteger(N->getOperand(0));
5076
5077 EVT InVT = InOp0.getValueType();
5078
5079 unsigned OutNumElems = OutVT.getVectorNumElements();
5080 SmallVector<SDValue, 8> Ops;
5081 Ops.reserve(OutNumElems);
5082 for (unsigned i = 0; i != OutNumElems; ++i) {
5083
5084 // Extract the element from the original vector.
5085 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
5086 BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
5087 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5088 InVT.getVectorElementType(), N->getOperand(0), Index);
5089
5090 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
5091 // Insert the converted element to the new vector.
5092 Ops.push_back(Op);
5093 }
5094
5095 return DAG.getBuildVector(NOutVT, dl, Ops);
5096}
5097
5098SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_SUBVECTOR(SDNode *N) {
5099 EVT OutVT = N->getValueType(0);
5100 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5101 assert(NOutVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5101
, __extension__ __PRETTY_FUNCTION__))
;
5102
5103 SDLoc dl(N);
5104 SDValue Vec = N->getOperand(0);
5105 SDValue SubVec = N->getOperand(1);
5106 SDValue Idx = N->getOperand(2);
5107
5108 EVT SubVecVT = SubVec.getValueType();
5109 EVT NSubVT =
5110 EVT::getVectorVT(*DAG.getContext(), NOutVT.getVectorElementType(),
5111 SubVecVT.getVectorElementCount());
5112
5113 Vec = GetPromotedInteger(Vec);
5114 SubVec = DAG.getNode(ISD::ANY_EXTEND, dl, NSubVT, SubVec);
5115
5116 return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NOutVT, Vec, SubVec, Idx);
5117}
5118
5119SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_REVERSE(SDNode *N) {
5120 SDLoc dl(N);
5121
5122 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5123 EVT OutVT = V0.getValueType();
5124
5125 return DAG.getNode(ISD::VECTOR_REVERSE, dl, OutVT, V0);
5126}
5127
5128SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
5129 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
5130 EVT VT = N->getValueType(0);
5131 SDLoc dl(N);
5132
5133 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
5134
5135 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5136 SDValue V1 = GetPromotedInteger(N->getOperand(1));
5137 EVT OutVT = V0.getValueType();
5138
5139 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
5140}
5141
5142
5143SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
5144 EVT OutVT = N->getValueType(0);
5145 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5146 assert(NOutVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5146
, __extension__ __PRETTY_FUNCTION__))
;
5147 unsigned NumElems = N->getNumOperands();
5148 EVT NOutVTElem = NOutVT.getVectorElementType();
5149
5150 SDLoc dl(N);
5151
5152 SmallVector<SDValue, 8> Ops;
5153 Ops.reserve(NumElems);
5154 for (unsigned i = 0; i != NumElems; ++i) {
5155 SDValue Op;
5156 // BUILD_VECTOR integer operand types are allowed to be larger than the
5157 // result's element type. This may still be true after the promotion. For
5158 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
5159 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
5160 if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
5161 Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
5162 else
5163 Op = N->getOperand(i);
5164 Ops.push_back(Op);
5165 }
5166
5167 return DAG.getBuildVector(NOutVT, dl, Ops);
5168}
5169
5170SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
5171
5172 SDLoc dl(N);
5173
5174 assert(!N->getOperand(0).getValueType().isVector() &&(static_cast <bool> (!N->getOperand(0).getValueType(
).isVector() && "Input must be a scalar") ? void (0) :
__assert_fail ("!N->getOperand(0).getValueType().isVector() && \"Input must be a scalar\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5175
, __extension__ __PRETTY_FUNCTION__))
5175 "Input must be a scalar")(static_cast <bool> (!N->getOperand(0).getValueType(
).isVector() && "Input must be a scalar") ? void (0) :
__assert_fail ("!N->getOperand(0).getValueType().isVector() && \"Input must be a scalar\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5175
, __extension__ __PRETTY_FUNCTION__))
;
5176
5177 EVT OutVT = N->getValueType(0);
5178 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5179 assert(NOutVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5179
, __extension__ __PRETTY_FUNCTION__))
;
5180 EVT NOutVTElem = NOutVT.getVectorElementType();
5181
5182 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
5183
5184 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
5185}
5186
5187SDValue DAGTypeLegalizer::PromoteIntRes_SPLAT_VECTOR(SDNode *N) {
5188 SDLoc dl(N);
5189
5190 SDValue SplatVal = N->getOperand(0);
5191
5192 assert(!SplatVal.getValueType().isVector() && "Input must be a scalar")(static_cast <bool> (!SplatVal.getValueType().isVector(
) && "Input must be a scalar") ? void (0) : __assert_fail
("!SplatVal.getValueType().isVector() && \"Input must be a scalar\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5192
, __extension__ __PRETTY_FUNCTION__))
;
5193
5194 EVT OutVT = N->getValueType(0);
5195 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5196 assert(NOutVT.isVector() && "Type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "Type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"Type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5196
, __extension__ __PRETTY_FUNCTION__))
;
5197 EVT NOutElemVT = NOutVT.getVectorElementType();
5198
5199 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutElemVT, SplatVal);
5200
5201 return DAG.getNode(ISD::SPLAT_VECTOR, dl, NOutVT, Op);
5202}
5203
5204SDValue DAGTypeLegalizer::PromoteIntRes_STEP_VECTOR(SDNode *N) {
5205 SDLoc dl(N);
5206 EVT OutVT = N->getValueType(0);
5207 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5208 assert(NOutVT.isVector() && "Type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "Type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"Type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5208
, __extension__ __PRETTY_FUNCTION__))
;
5209 APInt StepVal = cast<ConstantSDNode>(N->getOperand(0))->getAPIntValue();
5210 return DAG.getStepVector(dl, NOutVT,
5211 StepVal.sext(NOutVT.getScalarSizeInBits()));
5212}
5213
5214SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
5215 SDLoc dl(N);
5216
5217 EVT OutVT = N->getValueType(0);
5218 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5219 assert(NOutVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5219
, __extension__ __PRETTY_FUNCTION__))
;
5220
5221 unsigned NumOperands = N->getNumOperands();
5222 unsigned NumOutElem = NOutVT.getVectorMinNumElements();
5223 EVT OutElemTy = NOutVT.getVectorElementType();
5224 if (OutVT.isScalableVector()) {
5225 // Find the largest promoted element type for each of the operands.
5226 SDUse *MaxSizedValue = std::max_element(
5227 N->op_begin(), N->op_end(), [](const SDValue &A, const SDValue &B) {
5228 EVT AVT = A.getValueType().getVectorElementType();
5229 EVT BVT = B.getValueType().getVectorElementType();
5230 return AVT.getScalarSizeInBits() < BVT.getScalarSizeInBits();
5231 });
5232 EVT MaxElementVT = MaxSizedValue->getValueType().getVectorElementType();
5233
5234 // Then promote all vectors to the largest element type.
5235 SmallVector<SDValue, 8> Ops;
5236 for (unsigned I = 0; I < NumOperands; ++I) {
5237 SDValue Op = N->getOperand(I);
5238 EVT OpVT = Op.getValueType();
5239 if (getTypeAction(OpVT) == TargetLowering::TypePromoteInteger)
5240 Op = GetPromotedInteger(Op);
5241 else
5242 assert(getTypeAction(OpVT) == TargetLowering::TypeLegal &&(static_cast <bool> (getTypeAction(OpVT) == TargetLowering
::TypeLegal && "Unhandled legalization type") ? void (
0) : __assert_fail ("getTypeAction(OpVT) == TargetLowering::TypeLegal && \"Unhandled legalization type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5243
, __extension__ __PRETTY_FUNCTION__))
5243 "Unhandled legalization type")(static_cast <bool> (getTypeAction(OpVT) == TargetLowering
::TypeLegal && "Unhandled legalization type") ? void (
0) : __assert_fail ("getTypeAction(OpVT) == TargetLowering::TypeLegal && \"Unhandled legalization type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5243
, __extension__ __PRETTY_FUNCTION__))
;
5244
5245 if (OpVT.getVectorElementType().getScalarSizeInBits() <
5246 MaxElementVT.getScalarSizeInBits())
5247 Op = DAG.getAnyExtOrTrunc(Op, dl,
5248 OpVT.changeVectorElementType(MaxElementVT));
5249 Ops.push_back(Op);
5250 }
5251
5252 // Do the CONCAT on the promoted type and finally truncate to (the promoted)
5253 // NOutVT.
5254 return DAG.getAnyExtOrTrunc(
5255 DAG.getNode(ISD::CONCAT_VECTORS, dl,
5256 OutVT.changeVectorElementType(MaxElementVT), Ops),
5257 dl, NOutVT);
5258 }
5259
5260 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
5261 assert(NumElem * NumOperands == NumOutElem &&(static_cast <bool> (NumElem * NumOperands == NumOutElem
&& "Unexpected number of elements") ? void (0) : __assert_fail
("NumElem * NumOperands == NumOutElem && \"Unexpected number of elements\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5262
, __extension__ __PRETTY_FUNCTION__))
5262 "Unexpected number of elements")(static_cast <bool> (NumElem * NumOperands == NumOutElem
&& "Unexpected number of elements") ? void (0) : __assert_fail
("NumElem * NumOperands == NumOutElem && \"Unexpected number of elements\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5262
, __extension__ __PRETTY_FUNCTION__))
;
5263
5264 // Take the elements from the first vector.
5265 SmallVector<SDValue, 8> Ops(NumOutElem);
5266 for (unsigned i = 0; i < NumOperands; ++i) {
5267 SDValue Op = N->getOperand(i);
5268 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
5269 Op = GetPromotedInteger(Op);
5270 EVT SclrTy = Op.getValueType().getVectorElementType();
5271 assert(NumElem == Op.getValueType().getVectorNumElements() &&(static_cast <bool> (NumElem == Op.getValueType().getVectorNumElements
() && "Unexpected number of elements") ? void (0) : __assert_fail
("NumElem == Op.getValueType().getVectorNumElements() && \"Unexpected number of elements\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5272
, __extension__ __PRETTY_FUNCTION__))
5272 "Unexpected number of elements")(static_cast <bool> (NumElem == Op.getValueType().getVectorNumElements
() && "Unexpected number of elements") ? void (0) : __assert_fail
("NumElem == Op.getValueType().getVectorNumElements() && \"Unexpected number of elements\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5272
, __extension__ __PRETTY_FUNCTION__))
;
5273
5274 for (unsigned j = 0; j < NumElem; ++j) {
5275 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
5276 DAG.getVectorIdxConstant(j, dl));
5277 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
5278 }
5279 }
5280
5281 return DAG.getBuildVector(NOutVT, dl, Ops);
5282}
5283
5284SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
5285 EVT VT = N->getValueType(0);
5286 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
5287 assert(NVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5287
, __extension__ __PRETTY_FUNCTION__))
;
5288
5289 SDLoc dl(N);
5290
5291 // For operands whose TypeAction is to promote, extend the promoted node
5292 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
5293 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
5294 // type..
5295 if (getTypeAction(N->getOperand(0).getValueType())
5296 == TargetLowering::TypePromoteInteger) {
5297 SDValue Promoted;
5298
5299 switch(N->getOpcode()) {
5300 case ISD::SIGN_EXTEND_VECTOR_INREG:
5301 Promoted = SExtPromotedInteger(N->getOperand(0));
5302 break;
5303 case ISD::ZERO_EXTEND_VECTOR_INREG:
5304 Promoted = ZExtPromotedInteger(N->getOperand(0));
5305 break;
5306 case ISD::ANY_EXTEND_VECTOR_INREG:
5307 Promoted = GetPromotedInteger(N->getOperand(0));
5308 break;
5309 default:
5310 llvm_unreachable("Node has unexpected Opcode")::llvm::llvm_unreachable_internal("Node has unexpected Opcode"
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5310
)
;
5311 }
5312 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
5313 }
5314
5315 // Directly extend to the appropriate transform-to type.
5316 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
5317}
5318
5319SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
5320 EVT OutVT = N->getValueType(0);
5321 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
5322 assert(NOutVT.isVector() && "This type must be promoted to a vector type")(static_cast <bool> (NOutVT.isVector() && "This type must be promoted to a vector type"
) ? void (0) : __assert_fail ("NOutVT.isVector() && \"This type must be promoted to a vector type\""
, "llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp", 5322
, __extension__ __PRETTY_FUNCTION__))
;
5323
5324 EVT NOutVTElem = NOutVT.getVectorElementType();
5325
5326 SDLoc dl(N);
5327 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5328
5329 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
5330 NOutVTElem, N->getOperand(1));
5331 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
5332 V0, ConvElem, N->getOperand(2));
5333}
5334
5335SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
5336 // The VECREDUCE result size may be larger than the element size, so
5337 // we can simply change the result type.
5338 SDLoc dl(N);
5339 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
5340 return DAG.getNode(N->getOpcode(), dl, NVT, N->ops());
5341}
5342
5343SDValue DAGTypeLegalizer::PromoteIntRes_VP_REDUCE(SDNode *N) {
5344 // The VP_REDUCE result size may be larger than the element size, so we can
5345 // simply change the result type. However the start value and result must be
5346 // the same.
5347 SDLoc DL(N);
5348 SDValue Start = PromoteIntOpVectorReduction(N, N->getOperand(0));
5349 return DAG.getNode(N->getOpcode(), DL, Start.getValueType(), Start,
5350 N->getOperand(1), N->getOperand(2), N->getOperand(3));
5351}
5352
5353SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
5354 SDLoc dl(N);
5355 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5356 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
5357 TLI.getVectorIdxTy(DAG.getDataLayout()));
5358 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
5359 V0->getValueType(0).getScalarType(), V0, V1);
5360
5361 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
5362 // element types. If this is the case then we need to expand the outgoing
5363 // value and not truncate it.
5364 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
5365}
5366
5367SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_SUBVECTOR(SDNode *N) {
5368 SDLoc dl(N);
5369 // The result type is equal to the first input operand's type, so the
5370 // type that needs promoting must be the second source vector.
5371 SDValue V0 = N->getOperand(0);
5372 SDValue V1 = GetPromotedInteger(N->getOperand(1));
5373 SDValue Idx = N->getOperand(2);
5374 EVT PromVT = EVT::getVectorVT(*DAG.getContext(),
5375 V1.getValueType().getVectorElementType(),
5376 V0.getValueType().getVectorElementCount());
5377 V0 = DAG.getAnyExtOrTrunc(V0, dl, PromVT);
5378 SDValue Ext = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, PromVT, V0, V1, Idx);
5379 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
5380}
5381
5382SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
5383 SDLoc dl(N);
5384 SDValue V0 = GetPromotedInteger(N->getOperand(0));
5385 MVT InVT = V0.getValueType().getSimpleVT();
5386 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
5387 N->getValueType(0).getVectorNumElements());
5388 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
5389 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
5390}
5391
5392SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
5393 SDLoc dl(N);
5394
5395 EVT ResVT = N->getValueType(0);
5396 unsigned NumElems = N->getNumOperands();
5397
5398 if (ResVT.isScalableVector()) {
5399 SDValue ResVec = DAG.getUNDEF(ResVT);
5400
5401 for (unsigned OpIdx = 0; OpIdx < NumElems; ++OpIdx) {
5402 SDValue Op = N->getOperand(OpIdx);
5403 unsigned OpNumElts = Op.getValueType().getVectorMinNumElements();
5404 ResVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ResVec, Op,
5405 DAG.getIntPtrConstant(OpIdx * OpNumElts, dl));
5406 }
5407
5408 return ResVec;
5409 }
5410
5411 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
5412
5413 SmallVector<SDValue, 8> NewOps;
5414 NewOps.reserve(NumElems);
5415
5416 // For each incoming vector
5417 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
5418 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
5419 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
5420 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
5421
5422 for (unsigned i=0; i<NumElem; ++i) {
5423 // Extract element from incoming vector
5424 SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
5425 DAG.getVectorIdxConstant(i, dl));
5426 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
5427 NewOps.push_back(Tr);
5428 }
5429 }
5430
5431 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);
5432}

/build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include/llvm/CodeGen/SelectionDAGNodes.h

1//===- llvm/CodeGen/SelectionDAGNodes.h - SelectionDAG Nodes ----*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the SDNode class and derived classes, which are used to
10// represent the nodes and operations present in a SelectionDAG. These nodes
11// and operations are machine code level operations, with some similarities to
12// the GCC RTL representation.
13//
14// Clients should include the SelectionDAG.h file instead of this file directly.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CODEGEN_SELECTIONDAGNODES_H
19#define LLVM_CODEGEN_SELECTIONDAGNODES_H
20
21#include "llvm/ADT/APFloat.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/FoldingSet.h"
25#include "llvm/ADT/GraphTraits.h"
26#include "llvm/ADT/SmallPtrSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/ilist_node.h"
29#include "llvm/ADT/iterator.h"
30#include "llvm/ADT/iterator_range.h"
31#include "llvm/CodeGen/ISDOpcodes.h"
32#include "llvm/CodeGen/MachineMemOperand.h"
33#include "llvm/CodeGen/Register.h"
34#include "llvm/CodeGen/ValueTypes.h"
35#include "llvm/IR/Constants.h"
36#include "llvm/IR/DebugLoc.h"
37#include "llvm/IR/Instruction.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/Metadata.h"
40#include "llvm/IR/Operator.h"
41#include "llvm/Support/AlignOf.h"
42#include "llvm/Support/AtomicOrdering.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/MachineValueType.h"
46#include "llvm/Support/TypeSize.h"
47#include <algorithm>
48#include <cassert>
49#include <climits>
50#include <cstddef>
51#include <cstdint>
52#include <cstring>
53#include <iterator>
54#include <string>
55#include <tuple>
56
57namespace llvm {
58
59class APInt;
60class Constant;
61class GlobalValue;
62class MachineBasicBlock;
63class MachineConstantPoolValue;
64class MCSymbol;
65class raw_ostream;
66class SDNode;
67class SelectionDAG;
68class Type;
69class Value;
70
71void checkForCycles(const SDNode *N, const SelectionDAG *DAG = nullptr,
72 bool force = false);
73
74/// This represents a list of ValueType's that has been intern'd by
75/// a SelectionDAG. Instances of this simple value class are returned by
76/// SelectionDAG::getVTList(...).
77///
78struct SDVTList {
79 const EVT *VTs;
80 unsigned int NumVTs;
81};
82
83namespace ISD {
84
85 /// Node predicates
86
87/// If N is a BUILD_VECTOR or SPLAT_VECTOR node whose elements are all the
88/// same constant or undefined, return true and return the constant value in
89/// \p SplatValue.
90bool isConstantSplatVector(const SDNode *N, APInt &SplatValue);
91
92/// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where
93/// all of the elements are ~0 or undef. If \p BuildVectorOnly is set to
94/// true, it only checks BUILD_VECTOR.
95bool isConstantSplatVectorAllOnes(const SDNode *N,
96 bool BuildVectorOnly = false);
97
98/// Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where
99/// all of the elements are 0 or undef. If \p BuildVectorOnly is set to true, it
100/// only checks BUILD_VECTOR.
101bool isConstantSplatVectorAllZeros(const SDNode *N,
102 bool BuildVectorOnly = false);
103
104/// Return true if the specified node is a BUILD_VECTOR where all of the
105/// elements are ~0 or undef.
106bool isBuildVectorAllOnes(const SDNode *N);
107
108/// Return true if the specified node is a BUILD_VECTOR where all of the
109/// elements are 0 or undef.
110bool isBuildVectorAllZeros(const SDNode *N);
111
112/// Return true if the specified node is a BUILD_VECTOR node of all
113/// ConstantSDNode or undef.
114bool isBuildVectorOfConstantSDNodes(const SDNode *N);
115
116/// Return true if the specified node is a BUILD_VECTOR node of all
117/// ConstantFPSDNode or undef.
118bool isBuildVectorOfConstantFPSDNodes(const SDNode *N);
119
120/// Return true if the node has at least one operand and all operands of the
121/// specified node are ISD::UNDEF.
122bool allOperandsUndef(const SDNode *N);
123
124} // end namespace ISD
125
126//===----------------------------------------------------------------------===//
127/// Unlike LLVM values, Selection DAG nodes may return multiple
128/// values as the result of a computation. Many nodes return multiple values,
129/// from loads (which define a token and a return value) to ADDC (which returns
130/// a result and a carry value), to calls (which may return an arbitrary number
131/// of values).
132///
133/// As such, each use of a SelectionDAG computation must indicate the node that
134/// computes it as well as which return value to use from that node. This pair
135/// of information is represented with the SDValue value type.
136///
137class SDValue {
138 friend struct DenseMapInfo<SDValue>;
139
140 SDNode *Node = nullptr; // The node defining the value we are using.
141 unsigned ResNo = 0; // Which return value of the node we are using.
142
143public:
144 SDValue() = default;
145 SDValue(SDNode *node, unsigned resno);
146
147 /// get the index which selects a specific result in the SDNode
148 unsigned getResNo() const { return ResNo; }
149
150 /// get the SDNode which holds the desired result
151 SDNode *getNode() const { return Node; }
152
153 /// set the SDNode
154 void setNode(SDNode *N) { Node = N; }
155
156 inline SDNode *operator->() const { return Node; }
157
158 bool operator==(const SDValue &O) const {
159 return Node == O.Node && ResNo == O.ResNo;
160 }
161 bool operator!=(const SDValue &O) const {
162 return !operator==(O);
163 }
164 bool operator<(const SDValue &O) const {
165 return std::tie(Node, ResNo) < std::tie(O.Node, O.ResNo);
166 }
167 explicit operator bool() const {
168 return Node != nullptr;
169 }
170
171 SDValue getValue(unsigned R) const {
172 return SDValue(Node, R);
173 }
174
175 /// Return true if this node is an operand of N.
176 bool isOperandOf(const SDNode *N) const;
177
178 /// Return the ValueType of the referenced return value.
179 inline EVT getValueType() const;
180
181 /// Return the simple ValueType of the referenced return value.
182 MVT getSimpleValueType() const {
183 return getValueType().getSimpleVT();
184 }
185
186 /// Returns the size of the value in bits.
187 ///
188 /// If the value type is a scalable vector type, the scalable property will
189 /// be set and the runtime size will be a positive integer multiple of the
190 /// base size.
191 TypeSize getValueSizeInBits() const {
192 return getValueType().getSizeInBits();
193 }
194
195 uint64_t getScalarValueSizeInBits() const {
196 return getValueType().getScalarType().getFixedSizeInBits();
197 }
198
199 // Forwarding methods - These forward to the corresponding methods in SDNode.
200 inline unsigned getOpcode() const;
201 inline unsigned getNumOperands() const;
202 inline const SDValue &getOperand(unsigned i) const;
203 inline uint64_t getConstantOperandVal(unsigned i) const;
204 inline const APInt &getConstantOperandAPInt(unsigned i) const;
205 inline bool isTargetMemoryOpcode() const;
206 inline bool isTargetOpcode() const;
207 inline bool isMachineOpcode() const;
208 inline bool isUndef() const;
209 inline unsigned getMachineOpcode() const;
210 inline const DebugLoc &getDebugLoc() const;
211 inline void dump() const;
212 inline void dump(const SelectionDAG *G) const;
213 inline void dumpr() const;
214 inline void dumpr(const SelectionDAG *G) const;
215
216 /// Return true if this operand (which must be a chain) reaches the
217 /// specified operand without crossing any side-effecting instructions.
218 /// In practice, this looks through token factors and non-volatile loads.
219 /// In order to remain efficient, this only
220 /// looks a couple of nodes in, it does not do an exhaustive search.
221 bool reachesChainWithoutSideEffects(SDValue Dest,
222 unsigned Depth = 2) const;
223
224 /// Return true if there are no nodes using value ResNo of Node.
225 inline bool use_empty() const;
226
227 /// Return true if there is exactly one node using value ResNo of Node.
228 inline bool hasOneUse() const;
229};
230
231template<> struct DenseMapInfo<SDValue> {
232 static inline SDValue getEmptyKey() {
233 SDValue V;
234 V.ResNo = -1U;
235 return V;
236 }
237
238 static inline SDValue getTombstoneKey() {
239 SDValue V;
240 V.ResNo = -2U;
241 return V;
242 }
243
244 static unsigned getHashValue(const SDValue &Val) {
245 return ((unsigned)((uintptr_t)Val.getNode() >> 4) ^
246 (unsigned)((uintptr_t)Val.getNode() >> 9)) + Val.getResNo();
247 }
248
249 static bool isEqual(const SDValue &LHS, const SDValue &RHS) {
250 return LHS == RHS;
251 }
252};
253
254/// Allow casting operators to work directly on
255/// SDValues as if they were SDNode*'s.
256template<> struct simplify_type<SDValue> {
257 using SimpleType = SDNode *;
258
259 static SimpleType getSimplifiedValue(SDValue &Val) {
260 return Val.getNode();
261 }
262};
263template<> struct simplify_type<const SDValue> {
264 using SimpleType = /*const*/ SDNode *;
265
266 static SimpleType getSimplifiedValue(const SDValue &Val) {
267 return Val.getNode();
268 }
269};
270
271/// Represents a use of a SDNode. This class holds an SDValue,
272/// which records the SDNode being used and the result number, a
273/// pointer to the SDNode using the value, and Next and Prev pointers,
274/// which link together all the uses of an SDNode.
275///
276class SDUse {
277 /// Val - The value being used.
278 SDValue Val;
279 /// User - The user of this value.
280 SDNode *User = nullptr;
281 /// Prev, Next - Pointers to the uses list of the SDNode referred by
282 /// this operand.
283 SDUse **Prev = nullptr;
284 SDUse *Next = nullptr;
285
286public:
287 SDUse() = default;
288 SDUse(const SDUse &U) = delete;
289 SDUse &operator=(const SDUse &) = delete;
290
291 /// Normally SDUse will just implicitly convert to an SDValue that it holds.
292 operator const SDValue&() const { return Val; }
293
294 /// If implicit conversion to SDValue doesn't work, the get() method returns
295 /// the SDValue.
296 const SDValue &get() const { return Val; }
297
298 /// This returns the SDNode that contains this Use.
299 SDNode *getUser() { return User; }
300
301 /// Get the next SDUse in the use list.
302 SDUse *getNext() const { return Next; }
303
304 /// Convenience function for get().getNode().
305 SDNode *getNode() const { return Val.getNode(); }
306 /// Convenience function for get().getResNo().
307 unsigned getResNo() const { return Val.getResNo(); }
308 /// Convenience function for get().getValueType().
309 EVT getValueType() const { return Val.getValueType(); }
310
311 /// Convenience function for get().operator==
312 bool operator==(const SDValue &V) const {
313 return Val == V;
314 }
315
316 /// Convenience function for get().operator!=
317 bool operator!=(const SDValue &V) const {
318 return Val != V;
319 }
320
321 /// Convenience function for get().operator<
322 bool operator<(const SDValue &V) const {
323 return Val < V;
324 }
325
326private:
327 friend class SelectionDAG;
328 friend class SDNode;
329 // TODO: unfriend HandleSDNode once we fix its operand handling.
330 friend class HandleSDNode;
331
332 void setUser(SDNode *p) { User = p; }
333
334 /// Remove this use from its existing use list, assign it the
335 /// given value, and add it to the new value's node's use list.
336 inline void set(const SDValue &V);
337 /// Like set, but only supports initializing a newly-allocated
338 /// SDUse with a non-null value.
339 inline void setInitial(const SDValue &V);
340 /// Like set, but only sets the Node portion of the value,
341 /// leaving the ResNo portion unmodified.
342 inline void setNode(SDNode *N);
343
344 void addToList(SDUse **List) {
345 Next = *List;
346 if (Next) Next->Prev = &Next;
347 Prev = List;
348 *List = this;
349 }
350
351 void removeFromList() {
352 *Prev = Next;
353 if (Next) Next->Prev = Prev;
354 }
355};
356
357/// simplify_type specializations - Allow casting operators to work directly on
358/// SDValues as if they were SDNode*'s.
359template<> struct simplify_type<SDUse> {
360 using SimpleType = SDNode *;
361
362 static SimpleType getSimplifiedValue(SDUse &Val) {
363 return Val.getNode();
364 }
365};
366
367/// These are IR-level optimization flags that may be propagated to SDNodes.
368/// TODO: This data structure should be shared by the IR optimizer and the
369/// the backend.
370struct SDNodeFlags {
371private:
372 bool NoUnsignedWrap : 1;
373 bool NoSignedWrap : 1;
374 bool Exact : 1;
375 bool NoNaNs : 1;
376 bool NoInfs : 1;
377 bool NoSignedZeros : 1;
378 bool AllowReciprocal : 1;
379 bool AllowContract : 1;
380 bool ApproximateFuncs : 1;
381 bool AllowReassociation : 1;
382
383 // We assume instructions do not raise floating-point exceptions by default,
384 // and only those marked explicitly may do so. We could choose to represent
385 // this via a positive "FPExcept" flags like on the MI level, but having a
386 // negative "NoFPExcept" flag here (that defaults to true) makes the flag
387 // intersection logic more straightforward.
388 bool NoFPExcept : 1;
389
390public:
391 /// Default constructor turns off all optimization flags.
392 SDNodeFlags()
393 : NoUnsignedWrap(false), NoSignedWrap(false), Exact(false), NoNaNs(false),
394 NoInfs(false), NoSignedZeros(false), AllowReciprocal(false),
395 AllowContract(false), ApproximateFuncs(false),
396 AllowReassociation(false), NoFPExcept(false) {}
397
398 /// Propagate the fast-math-flags from an IR FPMathOperator.
399 void copyFMF(const FPMathOperator &FPMO) {
400 setNoNaNs(FPMO.hasNoNaNs());
401 setNoInfs(FPMO.hasNoInfs());
402 setNoSignedZeros(FPMO.hasNoSignedZeros());
403 setAllowReciprocal(FPMO.hasAllowReciprocal());
404 setAllowContract(FPMO.hasAllowContract());
405 setApproximateFuncs(FPMO.hasApproxFunc());
406 setAllowReassociation(FPMO.hasAllowReassoc());
407 }
408
409 // These are mutators for each flag.
410 void setNoUnsignedWrap(bool b) { NoUnsignedWrap = b; }
411 void setNoSignedWrap(bool b) { NoSignedWrap = b; }
412 void setExact(bool b) { Exact = b; }
413 void setNoNaNs(bool b) { NoNaNs = b; }
414 void setNoInfs(bool b) { NoInfs = b; }
415 void setNoSignedZeros(bool b) { NoSignedZeros = b; }
416 void setAllowReciprocal(bool b) { AllowReciprocal = b; }
417 void setAllowContract(bool b) { AllowContract = b; }
418 void setApproximateFuncs(bool b) { ApproximateFuncs = b; }
419 void setAllowReassociation(bool b) { AllowReassociation = b; }
420 void setNoFPExcept(bool b) { NoFPExcept = b; }
421
422 // These are accessors for each flag.
423 bool hasNoUnsignedWrap() const { return NoUnsignedWrap; }
424 bool hasNoSignedWrap() const { return NoSignedWrap; }
425 bool hasExact() const { return Exact; }
426 bool hasNoNaNs() const { return NoNaNs; }
427 bool hasNoInfs() const { return NoInfs; }
428 bool hasNoSignedZeros() const { return NoSignedZeros; }
429 bool hasAllowReciprocal() const { return AllowReciprocal; }
430 bool hasAllowContract() const { return AllowContract; }
431 bool hasApproximateFuncs() const { return ApproximateFuncs; }
432 bool hasAllowReassociation() const { return AllowReassociation; }
433 bool hasNoFPExcept() const { return NoFPExcept; }
434
435 /// Clear any flags in this flag set that aren't also set in Flags. All
436 /// flags will be cleared if Flags are undefined.
437 void intersectWith(const SDNodeFlags Flags) {
438 NoUnsignedWrap &= Flags.NoUnsignedWrap;
439 NoSignedWrap &= Flags.NoSignedWrap;
440 Exact &= Flags.Exact;
441 NoNaNs &= Flags.NoNaNs;
442 NoInfs &= Flags.NoInfs;
443 NoSignedZeros &= Flags.NoSignedZeros;
444 AllowReciprocal &= Flags.AllowReciprocal;
445 AllowContract &= Flags.AllowContract;
446 ApproximateFuncs &= Flags.ApproximateFuncs;
447 AllowReassociation &= Flags.AllowReassociation;
448 NoFPExcept &= Flags.NoFPExcept;
449 }
450};
451
452/// Represents one node in the SelectionDAG.
453///
454class SDNode : public FoldingSetNode, public ilist_node<SDNode> {
455private:
456 /// The operation that this node performs.
457 int16_t NodeType;
458
459protected:
460 // We define a set of mini-helper classes to help us interpret the bits in our
461 // SubclassData. These are designed to fit within a uint16_t so they pack
462 // with NodeType.
463
464#if defined(_AIX) && (!defined(__GNUC__4) || defined(__clang__1))
465// Except for GCC; by default, AIX compilers store bit-fields in 4-byte words
466// and give the `pack` pragma push semantics.
467#define BEGIN_TWO_BYTE_PACK() _Pragma("pack(2)")pack(2)
468#define END_TWO_BYTE_PACK() _Pragma("pack(pop)")pack(pop)
469#else
470#define BEGIN_TWO_BYTE_PACK()
471#define END_TWO_BYTE_PACK()
472#endif
473
474BEGIN_TWO_BYTE_PACK()
475 class SDNodeBitfields {
476 friend class SDNode;
477 friend class MemIntrinsicSDNode;
478 friend class MemSDNode;
479 friend class SelectionDAG;
480
481 uint16_t HasDebugValue : 1;
482 uint16_t IsMemIntrinsic : 1;
483 uint16_t IsDivergent : 1;
484 };
485 enum { NumSDNodeBits = 3 };
486
487 class ConstantSDNodeBitfields {
488 friend class ConstantSDNode;
489
490 uint16_t : NumSDNodeBits;
491
492 uint16_t IsOpaque : 1;
493 };
494
495 class MemSDNodeBitfields {
496 friend class MemSDNode;
497 friend class MemIntrinsicSDNode;
498 friend class AtomicSDNode;
499
500 uint16_t : NumSDNodeBits;
501
502 uint16_t IsVolatile : 1;
503 uint16_t IsNonTemporal : 1;
504 uint16_t IsDereferenceable : 1;
505 uint16_t IsInvariant : 1;
506 };
507 enum { NumMemSDNodeBits = NumSDNodeBits + 4 };
508
509 class LSBaseSDNodeBitfields {
510 friend class LSBaseSDNode;
511 friend class VPBaseLoadStoreSDNode;
512 friend class MaskedLoadStoreSDNode;
513 friend class MaskedGatherScatterSDNode;
514 friend class VPGatherScatterSDNode;
515
516 uint16_t : NumMemSDNodeBits;
517
518 // This storage is shared between disparate class hierarchies to hold an
519 // enumeration specific to the class hierarchy in use.
520 // LSBaseSDNode => enum ISD::MemIndexedMode
521 // VPLoadStoreBaseSDNode => enum ISD::MemIndexedMode
522 // MaskedLoadStoreBaseSDNode => enum ISD::MemIndexedMode
523 // VPGatherScatterSDNode => enum ISD::MemIndexType
524 // MaskedGatherScatterSDNode => enum ISD::MemIndexType
525 uint16_t AddressingMode : 3;
526 };
527 enum { NumLSBaseSDNodeBits = NumMemSDNodeBits + 3 };
528
529 class LoadSDNodeBitfields {
530 friend class LoadSDNode;
531 friend class VPLoadSDNode;
532 friend class VPStridedLoadSDNode;
533 friend class MaskedLoadSDNode;
534 friend class MaskedGatherSDNode;
535 friend class VPGatherSDNode;
536
537 uint16_t : NumLSBaseSDNodeBits;
538
539 uint16_t ExtTy : 2; // enum ISD::LoadExtType
540 uint16_t IsExpanding : 1;
541 };
542
543 class StoreSDNodeBitfields {
544 friend class StoreSDNode;
545 friend class VPStoreSDNode;
546 friend class VPStridedStoreSDNode;
547 friend class MaskedStoreSDNode;
548 friend class MaskedScatterSDNode;
549 friend class VPScatterSDNode;
550
551 uint16_t : NumLSBaseSDNodeBits;
552
553 uint16_t IsTruncating : 1;
554 uint16_t IsCompressing : 1;
555 };
556
557 union {
558 char RawSDNodeBits[sizeof(uint16_t)];
559 SDNodeBitfields SDNodeBits;
560 ConstantSDNodeBitfields ConstantSDNodeBits;
561 MemSDNodeBitfields MemSDNodeBits;
562 LSBaseSDNodeBitfields LSBaseSDNodeBits;
563 LoadSDNodeBitfields LoadSDNodeBits;
564 StoreSDNodeBitfields StoreSDNodeBits;
565 };
566END_TWO_BYTE_PACK()
567#undef BEGIN_TWO_BYTE_PACK
568#undef END_TWO_BYTE_PACK
569
570 // RawSDNodeBits must cover the entirety of the union. This means that all of
571 // the union's members must have size <= RawSDNodeBits. We write the RHS as
572 // "2" instead of sizeof(RawSDNodeBits) because MSVC can't handle the latter.
573 static_assert(sizeof(SDNodeBitfields) <= 2, "field too wide");
574 static_assert(sizeof(ConstantSDNodeBitfields) <= 2, "field too wide");
575 static_assert(sizeof(MemSDNodeBitfields) <= 2, "field too wide");
576 static_assert(sizeof(LSBaseSDNodeBitfields) <= 2, "field too wide");
577 static_assert(sizeof(LoadSDNodeBitfields) <= 2, "field too wide");
578 static_assert(sizeof(StoreSDNodeBitfields) <= 2, "field too wide");
579
580private:
581 friend class SelectionDAG;
582 // TODO: unfriend HandleSDNode once we fix its operand handling.
583 friend class HandleSDNode;
584
585 /// Unique id per SDNode in the DAG.
586 int NodeId = -1;
587
588 /// The values that are used by this operation.
589 SDUse *OperandList = nullptr;
590
591 /// The types of the values this node defines. SDNode's may
592 /// define multiple values simultaneously.
593 const EVT *ValueList;
594
595 /// List of uses for this SDNode.
596 SDUse *UseList = nullptr;
597
598 /// The number of entries in the Operand/Value list.
599 unsigned short NumOperands = 0;
600 unsigned short NumValues;
601
602 // The ordering of the SDNodes. It roughly corresponds to the ordering of the
603 // original LLVM instructions.
604 // This is used for turning off scheduling, because we'll forgo
605 // the normal scheduling algorithms and output the instructions according to
606 // this ordering.
607 unsigned IROrder;
608
609 /// Source line information.
610 DebugLoc debugLoc;
611
612 /// Return a pointer to the specified value type.
613 static const EVT *getValueTypeList(EVT VT);
614
615 SDNodeFlags Flags;
616
617public:
618 /// Unique and persistent id per SDNode in the DAG. Used for debug printing.
619 /// We do not place that under `#if LLVM_ENABLE_ABI_BREAKING_CHECKS`
620 /// intentionally because it adds unneeded complexity without noticeable
621 /// benefits (see discussion with @thakis in D120714).
622 uint16_t PersistentId;
623
624 //===--------------------------------------------------------------------===//
625 // Accessors
626 //
627
628 /// Return the SelectionDAG opcode value for this node. For
629 /// pre-isel nodes (those for which isMachineOpcode returns false), these
630 /// are the opcode values in the ISD and <target>ISD namespaces. For
631 /// post-isel opcodes, see getMachineOpcode.
632 unsigned getOpcode() const { return (unsigned short)NodeType; }
633
634 /// Test if this node has a target-specific opcode (in the
635 /// \<target\>ISD namespace).
636 bool isTargetOpcode() const { return NodeType >= ISD::BUILTIN_OP_END; }
637
638 /// Test if this node has a target-specific opcode that may raise
639 /// FP exceptions (in the \<target\>ISD namespace and greater than
640 /// FIRST_TARGET_STRICTFP_OPCODE). Note that all target memory
641 /// opcode are currently automatically considered to possibly raise
642 /// FP exceptions as well.
643 bool isTargetStrictFPOpcode() const {
644 return NodeType >= ISD::FIRST_TARGET_STRICTFP_OPCODE;
645 }
646
647 /// Test if this node has a target-specific
648 /// memory-referencing opcode (in the \<target\>ISD namespace and
649 /// greater than FIRST_TARGET_MEMORY_OPCODE).
650 bool isTargetMemoryOpcode() const {
651 return NodeType >= ISD::FIRST_TARGET_MEMORY_OPCODE;
652 }
653
654 /// Return true if the type of the node type undefined.
655 bool isUndef() const { return NodeType == ISD::UNDEF; }
656
657 /// Test if this node is a memory intrinsic (with valid pointer information).
658 /// INTRINSIC_W_CHAIN and INTRINSIC_VOID nodes are sometimes created for
659 /// non-memory intrinsics (with chains) that are not really instances of
660 /// MemSDNode. For such nodes, we need some extra state to determine the
661 /// proper classof relationship.
662 bool isMemIntrinsic() const {
663 return (NodeType == ISD::INTRINSIC_W_CHAIN ||
664 NodeType == ISD::INTRINSIC_VOID) &&
665 SDNodeBits.IsMemIntrinsic;
666 }
667
668 /// Test if this node is a strict floating point pseudo-op.
669 bool isStrictFPOpcode() {
670 switch (NodeType) {
671 default:
672 return false;
673 case ISD::STRICT_FP16_TO_FP:
674 case ISD::STRICT_FP_TO_FP16:
675#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
676 case ISD::STRICT_##DAGN:
677#include "llvm/IR/ConstrainedOps.def"
678 return true;
679 }
680 }
681
682 /// Test if this node is a vector predication operation.
683 bool isVPOpcode() const { return ISD::isVPOpcode(getOpcode()); }
684
685 /// Test if this node has a post-isel opcode, directly
686 /// corresponding to a MachineInstr opcode.
687 bool isMachineOpcode() const { return NodeType < 0; }
688
689 /// This may only be called if isMachineOpcode returns
690 /// true. It returns the MachineInstr opcode value that the node's opcode
691 /// corresponds to.
692 unsigned getMachineOpcode() const {
693 assert(isMachineOpcode() && "Not a MachineInstr opcode!")(static_cast <bool> (isMachineOpcode() && "Not a MachineInstr opcode!"
) ? void (0) : __assert_fail ("isMachineOpcode() && \"Not a MachineInstr opcode!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 693, __extension__
__PRETTY_FUNCTION__))
;
694 return ~NodeType;
695 }
696
697 bool getHasDebugValue() const { return SDNodeBits.HasDebugValue; }
698 void setHasDebugValue(bool b) { SDNodeBits.HasDebugValue = b; }
699
700 bool isDivergent() const { return SDNodeBits.IsDivergent; }
701
702 /// Return true if there are no uses of this node.
703 bool use_empty() const { return UseList == nullptr; }
704
705 /// Return true if there is exactly one use of this node.
706 bool hasOneUse() const { return hasSingleElement(uses()); }
707
708 /// Return the number of uses of this node. This method takes
709 /// time proportional to the number of uses.
710 size_t use_size() const { return std::distance(use_begin(), use_end()); }
711
712 /// Return the unique node id.
713 int getNodeId() const { return NodeId; }
714
715 /// Set unique node id.
716 void setNodeId(int Id) { NodeId = Id; }
717
718 /// Return the node ordering.
719 unsigned getIROrder() const { return IROrder; }
720
721 /// Set the node ordering.
722 void setIROrder(unsigned Order) { IROrder = Order; }
723
724 /// Return the source location info.
725 const DebugLoc &getDebugLoc() const { return debugLoc; }
726
727 /// Set source location info. Try to avoid this, putting
728 /// it in the constructor is preferable.
729 void setDebugLoc(DebugLoc dl) { debugLoc = std::move(dl); }
730
731 /// This class provides iterator support for SDUse
732 /// operands that use a specific SDNode.
733 class use_iterator {
734 friend class SDNode;
735
736 SDUse *Op = nullptr;
737
738 explicit use_iterator(SDUse *op) : Op(op) {}
739
740 public:
741 using iterator_category = std::forward_iterator_tag;
742 using value_type = SDUse;
743 using difference_type = std::ptrdiff_t;
744 using pointer = value_type *;
745 using reference = value_type &;
746
747 use_iterator() = default;
748 use_iterator(const use_iterator &I) = default;
749
750 bool operator==(const use_iterator &x) const { return Op == x.Op; }
751 bool operator!=(const use_iterator &x) const {
752 return !operator==(x);
753 }
754
755 /// Return true if this iterator is at the end of uses list.
756 bool atEnd() const { return Op == nullptr; }
757
758 // Iterator traversal: forward iteration only.
759 use_iterator &operator++() { // Preincrement
760 assert(Op && "Cannot increment end iterator!")(static_cast <bool> (Op && "Cannot increment end iterator!"
) ? void (0) : __assert_fail ("Op && \"Cannot increment end iterator!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 760, __extension__
__PRETTY_FUNCTION__))
;
761 Op = Op->getNext();
762 return *this;
763 }
764
765 use_iterator operator++(int) { // Postincrement
766 use_iterator tmp = *this; ++*this; return tmp;
767 }
768
769 /// Retrieve a pointer to the current user node.
770 SDNode *operator*() const {
771 assert(Op && "Cannot dereference end iterator!")(static_cast <bool> (Op && "Cannot dereference end iterator!"
) ? void (0) : __assert_fail ("Op && \"Cannot dereference end iterator!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 771, __extension__
__PRETTY_FUNCTION__))
;
772 return Op->getUser();
773 }
774
775 SDNode *operator->() const { return operator*(); }
776
777 SDUse &getUse() const { return *Op; }
778
779 /// Retrieve the operand # of this use in its user.
780 unsigned getOperandNo() const {
781 assert(Op && "Cannot dereference end iterator!")(static_cast <bool> (Op && "Cannot dereference end iterator!"
) ? void (0) : __assert_fail ("Op && \"Cannot dereference end iterator!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 781, __extension__
__PRETTY_FUNCTION__))
;
782 return (unsigned)(Op - Op->getUser()->OperandList);
783 }
784 };
785
786 /// Provide iteration support to walk over all uses of an SDNode.
787 use_iterator use_begin() const {
788 return use_iterator(UseList);
789 }
790
791 static use_iterator use_end() { return use_iterator(nullptr); }
792
793 inline iterator_range<use_iterator> uses() {
794 return make_range(use_begin(), use_end());
795 }
796 inline iterator_range<use_iterator> uses() const {
797 return make_range(use_begin(), use_end());
798 }
799
800 /// Return true if there are exactly NUSES uses of the indicated value.
801 /// This method ignores uses of other values defined by this operation.
802 bool hasNUsesOfValue(unsigned NUses, unsigned Value) const;
803
804 /// Return true if there are any use of the indicated value.
805 /// This method ignores uses of other values defined by this operation.
806 bool hasAnyUseOfValue(unsigned Value) const;
807
808 /// Return true if this node is the only use of N.
809 bool isOnlyUserOf(const SDNode *N) const;
810
811 /// Return true if this node is an operand of N.
812 bool isOperandOf(const SDNode *N) const;
813
814 /// Return true if this node is a predecessor of N.
815 /// NOTE: Implemented on top of hasPredecessor and every bit as
816 /// expensive. Use carefully.
817 bool isPredecessorOf(const SDNode *N) const {
818 return N->hasPredecessor(this);
819 }
820
821 /// Return true if N is a predecessor of this node.
822 /// N is either an operand of this node, or can be reached by recursively
823 /// traversing up the operands.
824 /// NOTE: This is an expensive method. Use it carefully.
825 bool hasPredecessor(const SDNode *N) const;
826
827 /// Returns true if N is a predecessor of any node in Worklist. This
828 /// helper keeps Visited and Worklist sets externally to allow unions
829 /// searches to be performed in parallel, caching of results across
830 /// queries and incremental addition to Worklist. Stops early if N is
831 /// found but will resume. Remember to clear Visited and Worklists
832 /// if DAG changes. MaxSteps gives a maximum number of nodes to visit before
833 /// giving up. The TopologicalPrune flag signals that positive NodeIds are
834 /// topologically ordered (Operands have strictly smaller node id) and search
835 /// can be pruned leveraging this.
836 static bool hasPredecessorHelper(const SDNode *N,
837 SmallPtrSetImpl<const SDNode *> &Visited,
838 SmallVectorImpl<const SDNode *> &Worklist,
839 unsigned int MaxSteps = 0,
840 bool TopologicalPrune = false) {
841 SmallVector<const SDNode *, 8> DeferredNodes;
842 if (Visited.count(N))
843 return true;
844
845 // Node Id's are assigned in three places: As a topological
846 // ordering (> 0), during legalization (results in values set to
847 // 0), new nodes (set to -1). If N has a topolgical id then we
848 // know that all nodes with ids smaller than it cannot be
849 // successors and we need not check them. Filter out all node
850 // that can't be matches. We add them to the worklist before exit
851 // in case of multiple calls. Note that during selection the topological id
852 // may be violated if a node's predecessor is selected before it. We mark
853 // this at selection negating the id of unselected successors and
854 // restricting topological pruning to positive ids.
855
856 int NId = N->getNodeId();
857 // If we Invalidated the Id, reconstruct original NId.
858 if (NId < -1)
859 NId = -(NId + 1);
860
861 bool Found = false;
862 while (!Worklist.empty()) {
863 const SDNode *M = Worklist.pop_back_val();
864 int MId = M->getNodeId();
865 if (TopologicalPrune && M->getOpcode() != ISD::TokenFactor && (NId > 0) &&
866 (MId > 0) && (MId < NId)) {
867 DeferredNodes.push_back(M);
868 continue;
869 }
870 for (const SDValue &OpV : M->op_values()) {
871 SDNode *Op = OpV.getNode();
872 if (Visited.insert(Op).second)
873 Worklist.push_back(Op);
874 if (Op == N)
875 Found = true;
876 }
877 if (Found)
878 break;
879 if (MaxSteps != 0 && Visited.size() >= MaxSteps)
880 break;
881 }
882 // Push deferred nodes back on worklist.
883 Worklist.append(DeferredNodes.begin(), DeferredNodes.end());
884 // If we bailed early, conservatively return found.
885 if (MaxSteps != 0 && Visited.size() >= MaxSteps)
886 return true;
887 return Found;
888 }
889
890 /// Return true if all the users of N are contained in Nodes.
891 /// NOTE: Requires at least one match, but doesn't require them all.
892 static bool areOnlyUsersOf(ArrayRef<const SDNode *> Nodes, const SDNode *N);
893
894 /// Return the number of values used by this operation.
895 unsigned getNumOperands() const { return NumOperands; }
896
897 /// Return the maximum number of operands that a SDNode can hold.
898 static constexpr size_t getMaxNumOperands() {
899 return std::numeric_limits<decltype(SDNode::NumOperands)>::max();
900 }
901
902 /// Helper method returns the integer value of a ConstantSDNode operand.
903 inline uint64_t getConstantOperandVal(unsigned Num) const;
904
905 /// Helper method returns the APInt of a ConstantSDNode operand.
906 inline const APInt &getConstantOperandAPInt(unsigned Num) const;
907
908 const SDValue &getOperand(unsigned Num) const {
909 assert(Num < NumOperands && "Invalid child # of SDNode!")(static_cast <bool> (Num < NumOperands && "Invalid child # of SDNode!"
) ? void (0) : __assert_fail ("Num < NumOperands && \"Invalid child # of SDNode!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 909, __extension__
__PRETTY_FUNCTION__))
;
910 return OperandList[Num];
911 }
912
913 using op_iterator = SDUse *;
914
915 op_iterator op_begin() const { return OperandList; }
916 op_iterator op_end() const { return OperandList+NumOperands; }
917 ArrayRef<SDUse> ops() const { return makeArrayRef(op_begin(), op_end()); }
918
919 /// Iterator for directly iterating over the operand SDValue's.
920 struct value_op_iterator
921 : iterator_adaptor_base<value_op_iterator, op_iterator,
922 std::random_access_iterator_tag, SDValue,
923 ptrdiff_t, value_op_iterator *,
924 value_op_iterator *> {
925 explicit value_op_iterator(SDUse *U = nullptr)
926 : iterator_adaptor_base(U) {}
927
928 const SDValue &operator*() const { return I->get(); }
929 };
930
931 iterator_range<value_op_iterator> op_values() const {
932 return make_range(value_op_iterator(op_begin()),
933 value_op_iterator(op_end()));
934 }
935
936 SDVTList getVTList() const {
937 SDVTList X = { ValueList, NumValues };
938 return X;
939 }
940
941 /// If this node has a glue operand, return the node
942 /// to which the glue operand points. Otherwise return NULL.
943 SDNode *getGluedNode() const {
944 if (getNumOperands() != 0 &&
945 getOperand(getNumOperands()-1).getValueType() == MVT::Glue)
946 return getOperand(getNumOperands()-1).getNode();
947 return nullptr;
948 }
949
950 /// If this node has a glue value with a user, return
951 /// the user (there is at most one). Otherwise return NULL.
952 SDNode *getGluedUser() const {
953 for (use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI)
954 if (UI.getUse().get().getValueType() == MVT::Glue)
955 return *UI;
956 return nullptr;
957 }
958
959 SDNodeFlags getFlags() const { return Flags; }
960 void setFlags(SDNodeFlags NewFlags) { Flags = NewFlags; }
961
962 /// Clear any flags in this node that aren't also set in Flags.
963 /// If Flags is not in a defined state then this has no effect.
964 void intersectFlagsWith(const SDNodeFlags Flags);
965
966 /// Return the number of values defined/returned by this operator.
967 unsigned getNumValues() const { return NumValues; }
968
969 /// Return the type of a specified result.
970 EVT getValueType(unsigned ResNo) const {
971 assert(ResNo < NumValues && "Illegal result number!")(static_cast <bool> (ResNo < NumValues && "Illegal result number!"
) ? void (0) : __assert_fail ("ResNo < NumValues && \"Illegal result number!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 971, __extension__
__PRETTY_FUNCTION__))
;
972 return ValueList[ResNo];
973 }
974
975 /// Return the type of a specified result as a simple type.
976 MVT getSimpleValueType(unsigned ResNo) const {
977 return getValueType(ResNo).getSimpleVT();
978 }
979
980 /// Returns MVT::getSizeInBits(getValueType(ResNo)).
981 ///
982 /// If the value type is a scalable vector type, the scalable property will
983 /// be set and the runtime size will be a positive integer multiple of the
984 /// base size.
985 TypeSize getValueSizeInBits(unsigned ResNo) const {
986 return getValueType(ResNo).getSizeInBits();
987 }
988
989 using value_iterator = const EVT *;
990
991 value_iterator value_begin() const { return ValueList; }
992 value_iterator value_end() const { return ValueList+NumValues; }
993 iterator_range<value_iterator> values() const {
994 return llvm::make_range(value_begin(), value_end());
995 }
996
997 /// Return the opcode of this operation for printing.
998 std::string getOperationName(const SelectionDAG *G = nullptr) const;
999 static const char* getIndexedModeName(ISD::MemIndexedMode AM);
1000 void print_types(raw_ostream &OS, const SelectionDAG *G) const;
1001 void print_details(raw_ostream &OS, const SelectionDAG *G) const;
1002 void print(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
1003 void printr(raw_ostream &OS, const SelectionDAG *G = nullptr) const;
1004
1005 /// Print a SelectionDAG node and all children down to
1006 /// the leaves. The given SelectionDAG allows target-specific nodes
1007 /// to be printed in human-readable form. Unlike printr, this will
1008 /// print the whole DAG, including children that appear multiple
1009 /// times.
1010 ///
1011 void printrFull(raw_ostream &O, const SelectionDAG *G = nullptr) const;
1012
1013 /// Print a SelectionDAG node and children up to
1014 /// depth "depth." The given SelectionDAG allows target-specific
1015 /// nodes to be printed in human-readable form. Unlike printr, this
1016 /// will print children that appear multiple times wherever they are
1017 /// used.
1018 ///
1019 void printrWithDepth(raw_ostream &O, const SelectionDAG *G = nullptr,
1020 unsigned depth = 100) const;
1021
1022 /// Dump this node, for debugging.
1023 void dump() const;
1024
1025 /// Dump (recursively) this node and its use-def subgraph.
1026 void dumpr() const;
1027
1028 /// Dump this node, for debugging.
1029 /// The given SelectionDAG allows target-specific nodes to be printed
1030 /// in human-readable form.
1031 void dump(const SelectionDAG *G) const;
1032
1033 /// Dump (recursively) this node and its use-def subgraph.
1034 /// The given SelectionDAG allows target-specific nodes to be printed
1035 /// in human-readable form.
1036 void dumpr(const SelectionDAG *G) const;
1037
1038 /// printrFull to dbgs(). The given SelectionDAG allows
1039 /// target-specific nodes to be printed in human-readable form.
1040 /// Unlike dumpr, this will print the whole DAG, including children
1041 /// that appear multiple times.
1042 void dumprFull(const SelectionDAG *G = nullptr) const;
1043
1044 /// printrWithDepth to dbgs(). The given
1045 /// SelectionDAG allows target-specific nodes to be printed in
1046 /// human-readable form. Unlike dumpr, this will print children
1047 /// that appear multiple times wherever they are used.
1048 ///
1049 void dumprWithDepth(const SelectionDAG *G = nullptr,
1050 unsigned depth = 100) const;
1051
1052 /// Gather unique data for the node.
1053 void Profile(FoldingSetNodeID &ID) const;
1054
1055 /// This method should only be used by the SDUse class.
1056 void addUse(SDUse &U) { U.addToList(&UseList); }
1057
1058protected:
1059 static SDVTList getSDVTList(EVT VT) {
1060 SDVTList Ret = { getValueTypeList(VT), 1 };
1061 return Ret;
1062 }
1063
1064 /// Create an SDNode.
1065 ///
1066 /// SDNodes are created without any operands, and never own the operand
1067 /// storage. To add operands, see SelectionDAG::createOperands.
1068 SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
1069 : NodeType(Opc), ValueList(VTs.VTs), NumValues(VTs.NumVTs),
1070 IROrder(Order), debugLoc(std::move(dl)) {
1071 memset(&RawSDNodeBits, 0, sizeof(RawSDNodeBits));
1072 assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor")(static_cast <bool> (debugLoc.hasTrivialDestructor() &&
"Expected trivial destructor") ? void (0) : __assert_fail ("debugLoc.hasTrivialDestructor() && \"Expected trivial destructor\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1072, __extension__
__PRETTY_FUNCTION__))
;
1073 assert(NumValues == VTs.NumVTs &&(static_cast <bool> (NumValues == VTs.NumVTs &&
"NumValues wasn't wide enough for its operands!") ? void (0)
: __assert_fail ("NumValues == VTs.NumVTs && \"NumValues wasn't wide enough for its operands!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1074, __extension__
__PRETTY_FUNCTION__))
1074 "NumValues wasn't wide enough for its operands!")(static_cast <bool> (NumValues == VTs.NumVTs &&
"NumValues wasn't wide enough for its operands!") ? void (0)
: __assert_fail ("NumValues == VTs.NumVTs && \"NumValues wasn't wide enough for its operands!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1074, __extension__
__PRETTY_FUNCTION__))
;
1075 }
1076
1077 /// Release the operands and set this node to have zero operands.
1078 void DropOperands();
1079};
1080
1081/// Wrapper class for IR location info (IR ordering and DebugLoc) to be passed
1082/// into SDNode creation functions.
1083/// When an SDNode is created from the DAGBuilder, the DebugLoc is extracted
1084/// from the original Instruction, and IROrder is the ordinal position of
1085/// the instruction.
1086/// When an SDNode is created after the DAG is being built, both DebugLoc and
1087/// the IROrder are propagated from the original SDNode.
1088/// So SDLoc class provides two constructors besides the default one, one to
1089/// be used by the DAGBuilder, the other to be used by others.
1090class SDLoc {
1091private:
1092 DebugLoc DL;
1093 int IROrder = 0;
1094
1095public:
1096 SDLoc() = default;
1097 SDLoc(const SDNode *N) : DL(N->getDebugLoc()), IROrder(N->getIROrder()) {}
1098 SDLoc(const SDValue V) : SDLoc(V.getNode()) {}
1099 SDLoc(const Instruction *I, int Order) : IROrder(Order) {
1100 assert(Order >= 0 && "bad IROrder")(static_cast <bool> (Order >= 0 && "bad IROrder"
) ? void (0) : __assert_fail ("Order >= 0 && \"bad IROrder\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1100, __extension__
__PRETTY_FUNCTION__))
;
1101 if (I)
1102 DL = I->getDebugLoc();
1103 }
1104
1105 unsigned getIROrder() const { return IROrder; }
1106 const DebugLoc &getDebugLoc() const { return DL; }
1107};
1108
1109// Define inline functions from the SDValue class.
1110
1111inline SDValue::SDValue(SDNode *node, unsigned resno)
1112 : Node(node), ResNo(resno) {
1113 // Explicitly check for !ResNo to avoid use-after-free, because there are
1114 // callers that use SDValue(N, 0) with a deleted N to indicate successful
1115 // combines.
1116 assert((!Node || !ResNo || ResNo < Node->getNumValues()) &&(static_cast <bool> ((!Node || !ResNo || ResNo < Node
->getNumValues()) && "Invalid result number for the given node!"
) ? void (0) : __assert_fail ("(!Node || !ResNo || ResNo < Node->getNumValues()) && \"Invalid result number for the given node!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1117, __extension__
__PRETTY_FUNCTION__))
1117 "Invalid result number for the given node!")(static_cast <bool> ((!Node || !ResNo || ResNo < Node
->getNumValues()) && "Invalid result number for the given node!"
) ? void (0) : __assert_fail ("(!Node || !ResNo || ResNo < Node->getNumValues()) && \"Invalid result number for the given node!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1117, __extension__
__PRETTY_FUNCTION__))
;
1118 assert(ResNo < -2U && "Cannot use result numbers reserved for DenseMaps.")(static_cast <bool> (ResNo < -2U && "Cannot use result numbers reserved for DenseMaps."
) ? void (0) : __assert_fail ("ResNo < -2U && \"Cannot use result numbers reserved for DenseMaps.\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1118, __extension__
__PRETTY_FUNCTION__))
;
1119}
1120
1121inline unsigned SDValue::getOpcode() const {
1122 return Node->getOpcode();
1123}
1124
1125inline EVT SDValue::getValueType() const {
1126 return Node->getValueType(ResNo);
12
Called C++ object pointer is null
1127}
1128
1129inline unsigned SDValue::getNumOperands() const {
1130 return Node->getNumOperands();
1131}
1132
1133inline const SDValue &SDValue::getOperand(unsigned i) const {
1134 return Node->getOperand(i);
1135}
1136
1137inline uint64_t SDValue::getConstantOperandVal(unsigned i) const {
1138 return Node->getConstantOperandVal(i);
1139}
1140
1141inline const APInt &SDValue::getConstantOperandAPInt(unsigned i) const {
1142 return Node->getConstantOperandAPInt(i);
1143}
1144
1145inline bool SDValue::isTargetOpcode() const {
1146 return Node->isTargetOpcode();
1147}
1148
1149inline bool SDValue::isTargetMemoryOpcode() const {
1150 return Node->isTargetMemoryOpcode();
1151}
1152
1153inline bool SDValue::isMachineOpcode() const {
1154 return Node->isMachineOpcode();
1155}
1156
1157inline unsigned SDValue::getMachineOpcode() const {
1158 return Node->getMachineOpcode();
1159}
1160
1161inline bool SDValue::isUndef() const {
1162 return Node->isUndef();
1163}
1164
1165inline bool SDValue::use_empty() const {
1166 return !Node->hasAnyUseOfValue(ResNo);
1167}
1168
1169inline bool SDValue::hasOneUse() const {
1170 return Node->hasNUsesOfValue(1, ResNo);
1171}
1172
1173inline const DebugLoc &SDValue::getDebugLoc() const {
1174 return Node->getDebugLoc();
1175}
1176
1177inline void SDValue::dump() const {
1178 return Node->dump();
1179}
1180
1181inline void SDValue::dump(const SelectionDAG *G) const {
1182 return Node->dump(G);
1183}
1184
1185inline void SDValue::dumpr() const {
1186 return Node->dumpr();
1187}
1188
1189inline void SDValue::dumpr(const SelectionDAG *G) const {
1190 return Node->dumpr(G);
1191}
1192
1193// Define inline functions from the SDUse class.
1194
1195inline void SDUse::set(const SDValue &V) {
1196 if (Val.getNode()) removeFromList();
1197 Val = V;
1198 if (V.getNode())
1199 V->addUse(*this);
1200}
1201
1202inline void SDUse::setInitial(const SDValue &V) {
1203 Val = V;
1204 V->addUse(*this);
1205}
1206
1207inline void SDUse::setNode(SDNode *N) {
1208 if (Val.getNode()) removeFromList();
1209 Val.setNode(N);
1210 if (N) N->addUse(*this);
1211}
1212
1213/// This class is used to form a handle around another node that
1214/// is persistent and is updated across invocations of replaceAllUsesWith on its
1215/// operand. This node should be directly created by end-users and not added to
1216/// the AllNodes list.
1217class HandleSDNode : public SDNode {
1218 SDUse Op;
1219
1220public:
1221 explicit HandleSDNode(SDValue X)
1222 : SDNode(ISD::HANDLENODE, 0, DebugLoc(), getSDVTList(MVT::Other)) {
1223 // HandleSDNodes are never inserted into the DAG, so they won't be
1224 // auto-numbered. Use ID 65535 as a sentinel.
1225 PersistentId = 0xffff;
1226
1227 // Manually set up the operand list. This node type is special in that it's
1228 // always stack allocated and SelectionDAG does not manage its operands.
1229 // TODO: This should either (a) not be in the SDNode hierarchy, or (b) not
1230 // be so special.
1231 Op.setUser(this);
1232 Op.setInitial(X);
1233 NumOperands = 1;
1234 OperandList = &Op;
1235 }
1236 ~HandleSDNode();
1237
1238 const SDValue &getValue() const { return Op; }
1239};
1240
1241class AddrSpaceCastSDNode : public SDNode {
1242private:
1243 unsigned SrcAddrSpace;
1244 unsigned DestAddrSpace;
1245
1246public:
1247 AddrSpaceCastSDNode(unsigned Order, const DebugLoc &dl, EVT VT,
1248 unsigned SrcAS, unsigned DestAS);
1249
1250 unsigned getSrcAddressSpace() const { return SrcAddrSpace; }
1251 unsigned getDestAddressSpace() const { return DestAddrSpace; }
1252
1253 static bool classof(const SDNode *N) {
1254 return N->getOpcode() == ISD::ADDRSPACECAST;
1255 }
1256};
1257
1258/// This is an abstract virtual class for memory operations.
1259class MemSDNode : public SDNode {
1260private:
1261 // VT of in-memory value.
1262 EVT MemoryVT;
1263
1264protected:
1265 /// Memory reference information.
1266 MachineMemOperand *MMO;
1267
1268public:
1269 MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs,
1270 EVT memvt, MachineMemOperand *MMO);
1271
1272 bool readMem() const { return MMO->isLoad(); }
1273 bool writeMem() const { return MMO->isStore(); }
1274
1275 /// Returns alignment and volatility of the memory access
1276 Align getOriginalAlign() const { return MMO->getBaseAlign(); }
1277 Align getAlign() const { return MMO->getAlign(); }
1278 // FIXME: Remove once transition to getAlign is over.
1279 unsigned getAlignment() const { return MMO->getAlign().value(); }
1280
1281 /// Return the SubclassData value, without HasDebugValue. This contains an
1282 /// encoding of the volatile flag, as well as bits used by subclasses. This
1283 /// function should only be used to compute a FoldingSetNodeID value.
1284 /// The HasDebugValue bit is masked out because CSE map needs to match
1285 /// nodes with debug info with nodes without debug info. Same is about
1286 /// isDivergent bit.
1287 unsigned getRawSubclassData() const {
1288 uint16_t Data;
1289 union {
1290 char RawSDNodeBits[sizeof(uint16_t)];
1291 SDNodeBitfields SDNodeBits;
1292 };
1293 memcpy(&RawSDNodeBits, &this->RawSDNodeBits, sizeof(this->RawSDNodeBits));
1294 SDNodeBits.HasDebugValue = 0;
1295 SDNodeBits.IsDivergent = false;
1296 memcpy(&Data, &RawSDNodeBits, sizeof(RawSDNodeBits));
1297 return Data;
1298 }
1299
1300 bool isVolatile() const { return MemSDNodeBits.IsVolatile; }
1301 bool isNonTemporal() const { return MemSDNodeBits.IsNonTemporal; }
1302 bool isDereferenceable() const { return MemSDNodeBits.IsDereferenceable; }
1303 bool isInvariant() const { return MemSDNodeBits.IsInvariant; }
1304
1305 // Returns the offset from the location of the access.
1306 int64_t getSrcValueOffset() const { return MMO->getOffset(); }
1307
1308 /// Returns the AA info that describes the dereference.
1309 AAMDNodes getAAInfo() const { return MMO->getAAInfo(); }
1310
1311 /// Returns the Ranges that describes the dereference.
1312 const MDNode *getRanges() const { return MMO->getRanges(); }
1313
1314 /// Returns the synchronization scope ID for this memory operation.
1315 SyncScope::ID getSyncScopeID() const { return MMO->getSyncScopeID(); }
1316
1317 /// Return the atomic ordering requirements for this memory operation. For
1318 /// cmpxchg atomic operations, return the atomic ordering requirements when
1319 /// store occurs.
1320 AtomicOrdering getSuccessOrdering() const {
1321 return MMO->getSuccessOrdering();
1322 }
1323
1324 /// Return a single atomic ordering that is at least as strong as both the
1325 /// success and failure orderings for an atomic operation. (For operations
1326 /// other than cmpxchg, this is equivalent to getSuccessOrdering().)
1327 AtomicOrdering getMergedOrdering() const { return MMO->getMergedOrdering(); }
1328
1329 /// Return true if the memory operation ordering is Unordered or higher.
1330 bool isAtomic() const { return MMO->isAtomic(); }
1331
1332 /// Returns true if the memory operation doesn't imply any ordering
1333 /// constraints on surrounding memory operations beyond the normal memory
1334 /// aliasing rules.
1335 bool isUnordered() const { return MMO->isUnordered(); }
1336
1337 /// Returns true if the memory operation is neither atomic or volatile.
1338 bool isSimple() const { return !isAtomic() && !isVolatile(); }
1339
1340 /// Return the type of the in-memory value.
1341 EVT getMemoryVT() const { return MemoryVT; }
1342
1343 /// Return a MachineMemOperand object describing the memory
1344 /// reference performed by operation.
1345 MachineMemOperand *getMemOperand() const { return MMO; }
1346
1347 const MachinePointerInfo &getPointerInfo() const {
1348 return MMO->getPointerInfo();
1349 }
1350
1351 /// Return the address space for the associated pointer
1352 unsigned getAddressSpace() const {
1353 return getPointerInfo().getAddrSpace();
1354 }
1355
1356 /// Update this MemSDNode's MachineMemOperand information
1357 /// to reflect the alignment of NewMMO, if it has a greater alignment.
1358 /// This must only be used when the new alignment applies to all users of
1359 /// this MachineMemOperand.
1360 void refineAlignment(const MachineMemOperand *NewMMO) {
1361 MMO->refineAlignment(NewMMO);
1362 }
1363
1364 const SDValue &getChain() const { return getOperand(0); }
1365
1366 const SDValue &getBasePtr() const {
1367 switch (getOpcode()) {
1368 case ISD::STORE:
1369 case ISD::VP_STORE:
1370 case ISD::MSTORE:
1371 case ISD::VP_SCATTER:
1372 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
1373 return getOperand(2);
1374 case ISD::MGATHER:
1375 case ISD::MSCATTER:
1376 return getOperand(3);
1377 default:
1378 return getOperand(1);
1379 }
1380 }
1381
1382 // Methods to support isa and dyn_cast
1383 static bool classof(const SDNode *N) {
1384 // For some targets, we lower some target intrinsics to a MemIntrinsicNode
1385 // with either an intrinsic or a target opcode.
1386 switch (N->getOpcode()) {
1387 case ISD::LOAD:
1388 case ISD::STORE:
1389 case ISD::PREFETCH:
1390 case ISD::ATOMIC_CMP_SWAP:
1391 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1392 case ISD::ATOMIC_SWAP:
1393 case ISD::ATOMIC_LOAD_ADD:
1394 case ISD::ATOMIC_LOAD_SUB:
1395 case ISD::ATOMIC_LOAD_AND:
1396 case ISD::ATOMIC_LOAD_CLR:
1397 case ISD::ATOMIC_LOAD_OR:
1398 case ISD::ATOMIC_LOAD_XOR:
1399 case ISD::ATOMIC_LOAD_NAND:
1400 case ISD::ATOMIC_LOAD_MIN:
1401 case ISD::ATOMIC_LOAD_MAX:
1402 case ISD::ATOMIC_LOAD_UMIN:
1403 case ISD::ATOMIC_LOAD_UMAX:
1404 case ISD::ATOMIC_LOAD_FADD:
1405 case ISD::ATOMIC_LOAD_FSUB:
1406 case ISD::ATOMIC_LOAD:
1407 case ISD::ATOMIC_STORE:
1408 case ISD::MLOAD:
1409 case ISD::MSTORE:
1410 case ISD::MGATHER:
1411 case ISD::MSCATTER:
1412 case ISD::VP_LOAD:
1413 case ISD::VP_STORE:
1414 case ISD::VP_GATHER:
1415 case ISD::VP_SCATTER:
1416 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
1417 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
1418 return true;
1419 default:
1420 return N->isMemIntrinsic() || N->isTargetMemoryOpcode();
1421 }
1422 }
1423};
1424
1425/// This is an SDNode representing atomic operations.
1426class AtomicSDNode : public MemSDNode {
1427public:
1428 AtomicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTL,
1429 EVT MemVT, MachineMemOperand *MMO)
1430 : MemSDNode(Opc, Order, dl, VTL, MemVT, MMO) {
1431 assert(((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) ||(static_cast <bool> (((Opc != ISD::ATOMIC_LOAD &&
Opc != ISD::ATOMIC_STORE) || MMO->isAtomic()) && "then why are we using an AtomicSDNode?"
) ? void (0) : __assert_fail ("((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) || MMO->isAtomic()) && \"then why are we using an AtomicSDNode?\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1432, __extension__
__PRETTY_FUNCTION__))
1432 MMO->isAtomic()) && "then why are we using an AtomicSDNode?")(static_cast <bool> (((Opc != ISD::ATOMIC_LOAD &&
Opc != ISD::ATOMIC_STORE) || MMO->isAtomic()) && "then why are we using an AtomicSDNode?"
) ? void (0) : __assert_fail ("((Opc != ISD::ATOMIC_LOAD && Opc != ISD::ATOMIC_STORE) || MMO->isAtomic()) && \"then why are we using an AtomicSDNode?\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1432, __extension__
__PRETTY_FUNCTION__))
;
1433 }
1434
1435 const SDValue &getBasePtr() const { return getOperand(1); }
1436 const SDValue &getVal() const { return getOperand(2); }
1437
1438 /// Returns true if this SDNode represents cmpxchg atomic operation, false
1439 /// otherwise.
1440 bool isCompareAndSwap() const {
1441 unsigned Op = getOpcode();
1442 return Op == ISD::ATOMIC_CMP_SWAP ||
1443 Op == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS;
1444 }
1445
1446 /// For cmpxchg atomic operations, return the atomic ordering requirements
1447 /// when store does not occur.
1448 AtomicOrdering getFailureOrdering() const {
1449 assert(isCompareAndSwap() && "Must be cmpxchg operation")(static_cast <bool> (isCompareAndSwap() && "Must be cmpxchg operation"
) ? void (0) : __assert_fail ("isCompareAndSwap() && \"Must be cmpxchg operation\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1449, __extension__
__PRETTY_FUNCTION__))
;
1450 return MMO->getFailureOrdering();
1451 }
1452
1453 // Methods to support isa and dyn_cast
1454 static bool classof(const SDNode *N) {
1455 return N->getOpcode() == ISD::ATOMIC_CMP_SWAP ||
1456 N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS ||
1457 N->getOpcode() == ISD::ATOMIC_SWAP ||
1458 N->getOpcode() == ISD::ATOMIC_LOAD_ADD ||
1459 N->getOpcode() == ISD::ATOMIC_LOAD_SUB ||
1460 N->getOpcode() == ISD::ATOMIC_LOAD_AND ||
1461 N->getOpcode() == ISD::ATOMIC_LOAD_CLR ||
1462 N->getOpcode() == ISD::ATOMIC_LOAD_OR ||
1463 N->getOpcode() == ISD::ATOMIC_LOAD_XOR ||
1464 N->getOpcode() == ISD::ATOMIC_LOAD_NAND ||
1465 N->getOpcode() == ISD::ATOMIC_LOAD_MIN ||
1466 N->getOpcode() == ISD::ATOMIC_LOAD_MAX ||
1467 N->getOpcode() == ISD::ATOMIC_LOAD_UMIN ||
1468 N->getOpcode() == ISD::ATOMIC_LOAD_UMAX ||
1469 N->getOpcode() == ISD::ATOMIC_LOAD_FADD ||
1470 N->getOpcode() == ISD::ATOMIC_LOAD_FSUB ||
1471 N->getOpcode() == ISD::ATOMIC_LOAD ||
1472 N->getOpcode() == ISD::ATOMIC_STORE;
1473 }
1474};
1475
1476/// This SDNode is used for target intrinsics that touch
1477/// memory and need an associated MachineMemOperand. Its opcode may be
1478/// INTRINSIC_VOID, INTRINSIC_W_CHAIN, PREFETCH, or a target-specific opcode
1479/// with a value not less than FIRST_TARGET_MEMORY_OPCODE.
1480class MemIntrinsicSDNode : public MemSDNode {
1481public:
1482 MemIntrinsicSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
1483 SDVTList VTs, EVT MemoryVT, MachineMemOperand *MMO)
1484 : MemSDNode(Opc, Order, dl, VTs, MemoryVT, MMO) {
1485 SDNodeBits.IsMemIntrinsic = true;
1486 }
1487
1488 // Methods to support isa and dyn_cast
1489 static bool classof(const SDNode *N) {
1490 // We lower some target intrinsics to their target opcode
1491 // early a node with a target opcode can be of this class
1492 return N->isMemIntrinsic() ||
1493 N->getOpcode() == ISD::PREFETCH ||
1494 N->isTargetMemoryOpcode();
1495 }
1496};
1497
1498/// This SDNode is used to implement the code generator
1499/// support for the llvm IR shufflevector instruction. It combines elements
1500/// from two input vectors into a new input vector, with the selection and
1501/// ordering of elements determined by an array of integers, referred to as
1502/// the shuffle mask. For input vectors of width N, mask indices of 0..N-1
1503/// refer to elements from the LHS input, and indices from N to 2N-1 the RHS.
1504/// An index of -1 is treated as undef, such that the code generator may put
1505/// any value in the corresponding element of the result.
1506class ShuffleVectorSDNode : public SDNode {
1507 // The memory for Mask is owned by the SelectionDAG's OperandAllocator, and
1508 // is freed when the SelectionDAG object is destroyed.
1509 const int *Mask;
1510
1511protected:
1512 friend class SelectionDAG;
1513
1514 ShuffleVectorSDNode(EVT VT, unsigned Order, const DebugLoc &dl, const int *M)
1515 : SDNode(ISD::VECTOR_SHUFFLE, Order, dl, getSDVTList(VT)), Mask(M) {}
1516
1517public:
1518 ArrayRef<int> getMask() const {
1519 EVT VT = getValueType(0);
1520 return makeArrayRef(Mask, VT.getVectorNumElements());
1521 }
1522
1523 int getMaskElt(unsigned Idx) const {
1524 assert(Idx < getValueType(0).getVectorNumElements() && "Idx out of range!")(static_cast <bool> (Idx < getValueType(0).getVectorNumElements
() && "Idx out of range!") ? void (0) : __assert_fail
("Idx < getValueType(0).getVectorNumElements() && \"Idx out of range!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1524, __extension__
__PRETTY_FUNCTION__))
;
1525 return Mask[Idx];
1526 }
1527
1528 bool isSplat() const { return isSplatMask(Mask, getValueType(0)); }
1529
1530 int getSplatIndex() const {
1531 assert(isSplat() && "Cannot get splat index for non-splat!")(static_cast <bool> (isSplat() && "Cannot get splat index for non-splat!"
) ? void (0) : __assert_fail ("isSplat() && \"Cannot get splat index for non-splat!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1531, __extension__
__PRETTY_FUNCTION__))
;
1532 EVT VT = getValueType(0);
1533 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1534 if (Mask[i] >= 0)
1535 return Mask[i];
1536
1537 // We can choose any index value here and be correct because all elements
1538 // are undefined. Return 0 for better potential for callers to simplify.
1539 return 0;
1540 }
1541
1542 static bool isSplatMask(const int *Mask, EVT VT);
1543
1544 /// Change values in a shuffle permute mask assuming
1545 /// the two vector operands have swapped position.
1546 static void commuteMask(MutableArrayRef<int> Mask) {
1547 unsigned NumElems = Mask.size();
1548 for (unsigned i = 0; i != NumElems; ++i) {
1549 int idx = Mask[i];
1550 if (idx < 0)
1551 continue;
1552 else if (idx < (int)NumElems)
1553 Mask[i] = idx + NumElems;
1554 else
1555 Mask[i] = idx - NumElems;
1556 }
1557 }
1558
1559 static bool classof(const SDNode *N) {
1560 return N->getOpcode() == ISD::VECTOR_SHUFFLE;
1561 }
1562};
1563
1564class ConstantSDNode : public SDNode {
1565 friend class SelectionDAG;
1566
1567 const ConstantInt *Value;
1568
1569 ConstantSDNode(bool isTarget, bool isOpaque, const ConstantInt *val, EVT VT)
1570 : SDNode(isTarget ? ISD::TargetConstant : ISD::Constant, 0, DebugLoc(),
1571 getSDVTList(VT)),
1572 Value(val) {
1573 ConstantSDNodeBits.IsOpaque = isOpaque;
1574 }
1575
1576public:
1577 const ConstantInt *getConstantIntValue() const { return Value; }
1578 const APInt &getAPIntValue() const { return Value->getValue(); }
1579 uint64_t getZExtValue() const { return Value->getZExtValue(); }
1580 int64_t getSExtValue() const { return Value->getSExtValue(); }
1581 uint64_t getLimitedValue(uint64_t Limit = UINT64_MAX(18446744073709551615UL)) {
1582 return Value->getLimitedValue(Limit);
1583 }
1584 MaybeAlign getMaybeAlignValue() const { return Value->getMaybeAlignValue(); }
1585 Align getAlignValue() const { return Value->getAlignValue(); }
1586
1587 bool isOne() const { return Value->isOne(); }
1588 bool isZero() const { return Value->isZero(); }
1589 // NOTE: This is soft-deprecated. Please use `isZero()` instead.
1590 bool isNullValue() const { return isZero(); }
1591 bool isAllOnes() const { return Value->isMinusOne(); }
1592 // NOTE: This is soft-deprecated. Please use `isAllOnes()` instead.
1593 bool isAllOnesValue() const { return isAllOnes(); }
1594 bool isMaxSignedValue() const { return Value->isMaxValue(true); }
1595 bool isMinSignedValue() const { return Value->isMinValue(true); }
1596
1597 bool isOpaque() const { return ConstantSDNodeBits.IsOpaque; }
1598
1599 static bool classof(const SDNode *N) {
1600 return N->getOpcode() == ISD::Constant ||
1601 N->getOpcode() == ISD::TargetConstant;
1602 }
1603};
1604
1605uint64_t SDNode::getConstantOperandVal(unsigned Num) const {
1606 return cast<ConstantSDNode>(getOperand(Num))->getZExtValue();
1607}
1608
1609const APInt &SDNode::getConstantOperandAPInt(unsigned Num) const {
1610 return cast<ConstantSDNode>(getOperand(Num))->getAPIntValue();
1611}
1612
1613class ConstantFPSDNode : public SDNode {
1614 friend class SelectionDAG;
1615
1616 const ConstantFP *Value;
1617
1618 ConstantFPSDNode(bool isTarget, const ConstantFP *val, EVT VT)
1619 : SDNode(isTarget ? ISD::TargetConstantFP : ISD::ConstantFP, 0,
1620 DebugLoc(), getSDVTList(VT)),
1621 Value(val) {}
1622
1623public:
1624 const APFloat& getValueAPF() const { return Value->getValueAPF(); }
1625 const ConstantFP *getConstantFPValue() const { return Value; }
1626
1627 /// Return true if the value is positive or negative zero.
1628 bool isZero() const { return Value->isZero(); }
1629
1630 /// Return true if the value is a NaN.
1631 bool isNaN() const { return Value->isNaN(); }
1632
1633 /// Return true if the value is an infinity
1634 bool isInfinity() const { return Value->isInfinity(); }
1635
1636 /// Return true if the value is negative.
1637 bool isNegative() const { return Value->isNegative(); }
1638
1639 /// We don't rely on operator== working on double values, as
1640 /// it returns true for things that are clearly not equal, like -0.0 and 0.0.
1641 /// As such, this method can be used to do an exact bit-for-bit comparison of
1642 /// two floating point values.
1643
1644 /// We leave the version with the double argument here because it's just so
1645 /// convenient to write "2.0" and the like. Without this function we'd
1646 /// have to duplicate its logic everywhere it's called.
1647 bool isExactlyValue(double V) const {
1648 return Value->getValueAPF().isExactlyValue(V);
1649 }
1650 bool isExactlyValue(const APFloat& V) const;
1651
1652 static bool isValueValidForType(EVT VT, const APFloat& Val);
1653
1654 static bool classof(const SDNode *N) {
1655 return N->getOpcode() == ISD::ConstantFP ||
1656 N->getOpcode() == ISD::TargetConstantFP;
1657 }
1658};
1659
1660/// Returns true if \p V is a constant integer zero.
1661bool isNullConstant(SDValue V);
1662
1663/// Returns true if \p V is an FP constant with a value of positive zero.
1664bool isNullFPConstant(SDValue V);
1665
1666/// Returns true if \p V is an integer constant with all bits set.
1667bool isAllOnesConstant(SDValue V);
1668
1669/// Returns true if \p V is a constant integer one.
1670bool isOneConstant(SDValue V);
1671
1672/// Returns true if \p V is a constant min signed integer value.
1673bool isMinSignedConstant(SDValue V);
1674
1675/// Return the non-bitcasted source operand of \p V if it exists.
1676/// If \p V is not a bitcasted value, it is returned as-is.
1677SDValue peekThroughBitcasts(SDValue V);
1678
1679/// Return the non-bitcasted and one-use source operand of \p V if it exists.
1680/// If \p V is not a bitcasted one-use value, it is returned as-is.
1681SDValue peekThroughOneUseBitcasts(SDValue V);
1682
1683/// Return the non-extracted vector source operand of \p V if it exists.
1684/// If \p V is not an extracted subvector, it is returned as-is.
1685SDValue peekThroughExtractSubvectors(SDValue V);
1686
1687/// Returns true if \p V is a bitwise not operation. Assumes that an all ones
1688/// constant is canonicalized to be operand 1.
1689bool isBitwiseNot(SDValue V, bool AllowUndefs = false);
1690
1691/// Returns the SDNode if it is a constant splat BuildVector or constant int.
1692ConstantSDNode *isConstOrConstSplat(SDValue N, bool AllowUndefs = false,
1693 bool AllowTruncation = false);
1694
1695/// Returns the SDNode if it is a demanded constant splat BuildVector or
1696/// constant int.
1697ConstantSDNode *isConstOrConstSplat(SDValue N, const APInt &DemandedElts,
1698 bool AllowUndefs = false,
1699 bool AllowTruncation = false);
1700
1701/// Returns the SDNode if it is a constant splat BuildVector or constant float.
1702ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, bool AllowUndefs = false);
1703
1704/// Returns the SDNode if it is a demanded constant splat BuildVector or
1705/// constant float.
1706ConstantFPSDNode *isConstOrConstSplatFP(SDValue N, const APInt &DemandedElts,
1707 bool AllowUndefs = false);
1708
1709/// Return true if the value is a constant 0 integer or a splatted vector of
1710/// a constant 0 integer (with no undefs by default).
1711/// Build vector implicit truncation is not an issue for null values.
1712bool isNullOrNullSplat(SDValue V, bool AllowUndefs = false);
1713
1714/// Return true if the value is a constant 1 integer or a splatted vector of a
1715/// constant 1 integer (with no undefs).
1716/// Does not permit build vector implicit truncation.
1717bool isOneOrOneSplat(SDValue V, bool AllowUndefs = false);
1718
1719/// Return true if the value is a constant -1 integer or a splatted vector of a
1720/// constant -1 integer (with no undefs).
1721/// Does not permit build vector implicit truncation.
1722bool isAllOnesOrAllOnesSplat(SDValue V, bool AllowUndefs = false);
1723
1724/// Return true if \p V is either a integer or FP constant.
1725inline bool isIntOrFPConstant(SDValue V) {
1726 return isa<ConstantSDNode>(V) || isa<ConstantFPSDNode>(V);
1727}
1728
1729class GlobalAddressSDNode : public SDNode {
1730 friend class SelectionDAG;
1731
1732 const GlobalValue *TheGlobal;
1733 int64_t Offset;
1734 unsigned TargetFlags;
1735
1736 GlobalAddressSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL,
1737 const GlobalValue *GA, EVT VT, int64_t o,
1738 unsigned TF);
1739
1740public:
1741 const GlobalValue *getGlobal() const { return TheGlobal; }
1742 int64_t getOffset() const { return Offset; }
1743 unsigned getTargetFlags() const { return TargetFlags; }
1744 // Return the address space this GlobalAddress belongs to.
1745 unsigned getAddressSpace() const;
1746
1747 static bool classof(const SDNode *N) {
1748 return N->getOpcode() == ISD::GlobalAddress ||
1749 N->getOpcode() == ISD::TargetGlobalAddress ||
1750 N->getOpcode() == ISD::GlobalTLSAddress ||
1751 N->getOpcode() == ISD::TargetGlobalTLSAddress;
1752 }
1753};
1754
1755class FrameIndexSDNode : public SDNode {
1756 friend class SelectionDAG;
1757
1758 int FI;
1759
1760 FrameIndexSDNode(int fi, EVT VT, bool isTarg)
1761 : SDNode(isTarg ? ISD::TargetFrameIndex : ISD::FrameIndex,
1762 0, DebugLoc(), getSDVTList(VT)), FI(fi) {
1763 }
1764
1765public:
1766 int getIndex() const { return FI; }
1767
1768 static bool classof(const SDNode *N) {
1769 return N->getOpcode() == ISD::FrameIndex ||
1770 N->getOpcode() == ISD::TargetFrameIndex;
1771 }
1772};
1773
1774/// This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate
1775/// the offet and size that are started/ended in the underlying FrameIndex.
1776class LifetimeSDNode : public SDNode {
1777 friend class SelectionDAG;
1778 int64_t Size;
1779 int64_t Offset; // -1 if offset is unknown.
1780
1781 LifetimeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl,
1782 SDVTList VTs, int64_t Size, int64_t Offset)
1783 : SDNode(Opcode, Order, dl, VTs), Size(Size), Offset(Offset) {}
1784public:
1785 int64_t getFrameIndex() const {
1786 return cast<FrameIndexSDNode>(getOperand(1))->getIndex();
1787 }
1788
1789 bool hasOffset() const { return Offset >= 0; }
1790 int64_t getOffset() const {
1791 assert(hasOffset() && "offset is unknown")(static_cast <bool> (hasOffset() && "offset is unknown"
) ? void (0) : __assert_fail ("hasOffset() && \"offset is unknown\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1791, __extension__
__PRETTY_FUNCTION__))
;
1792 return Offset;
1793 }
1794 int64_t getSize() const {
1795 assert(hasOffset() && "offset is unknown")(static_cast <bool> (hasOffset() && "offset is unknown"
) ? void (0) : __assert_fail ("hasOffset() && \"offset is unknown\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1795, __extension__
__PRETTY_FUNCTION__))
;
1796 return Size;
1797 }
1798
1799 // Methods to support isa and dyn_cast
1800 static bool classof(const SDNode *N) {
1801 return N->getOpcode() == ISD::LIFETIME_START ||
1802 N->getOpcode() == ISD::LIFETIME_END;
1803 }
1804};
1805
1806/// This SDNode is used for PSEUDO_PROBE values, which are the function guid and
1807/// the index of the basic block being probed. A pseudo probe serves as a place
1808/// holder and will be removed at the end of compilation. It does not have any
1809/// operand because we do not want the instruction selection to deal with any.
1810class PseudoProbeSDNode : public SDNode {
1811 friend class SelectionDAG;
1812 uint64_t Guid;
1813 uint64_t Index;
1814 uint32_t Attributes;
1815
1816 PseudoProbeSDNode(unsigned Opcode, unsigned Order, const DebugLoc &Dl,
1817 SDVTList VTs, uint64_t Guid, uint64_t Index, uint32_t Attr)
1818 : SDNode(Opcode, Order, Dl, VTs), Guid(Guid), Index(Index),
1819 Attributes(Attr) {}
1820
1821public:
1822 uint64_t getGuid() const { return Guid; }
1823 uint64_t getIndex() const { return Index; }
1824 uint32_t getAttributes() const { return Attributes; }
1825
1826 // Methods to support isa and dyn_cast
1827 static bool classof(const SDNode *N) {
1828 return N->getOpcode() == ISD::PSEUDO_PROBE;
1829 }
1830};
1831
1832class JumpTableSDNode : public SDNode {
1833 friend class SelectionDAG;
1834
1835 int JTI;
1836 unsigned TargetFlags;
1837
1838 JumpTableSDNode(int jti, EVT VT, bool isTarg, unsigned TF)
1839 : SDNode(isTarg ? ISD::TargetJumpTable : ISD::JumpTable,
1840 0, DebugLoc(), getSDVTList(VT)), JTI(jti), TargetFlags(TF) {
1841 }
1842
1843public:
1844 int getIndex() const { return JTI; }
1845 unsigned getTargetFlags() const { return TargetFlags; }
1846
1847 static bool classof(const SDNode *N) {
1848 return N->getOpcode() == ISD::JumpTable ||
1849 N->getOpcode() == ISD::TargetJumpTable;
1850 }
1851};
1852
1853class ConstantPoolSDNode : public SDNode {
1854 friend class SelectionDAG;
1855
1856 union {
1857 const Constant *ConstVal;
1858 MachineConstantPoolValue *MachineCPVal;
1859 } Val;
1860 int Offset; // It's a MachineConstantPoolValue if top bit is set.
1861 Align Alignment; // Minimum alignment requirement of CP.
1862 unsigned TargetFlags;
1863
1864 ConstantPoolSDNode(bool isTarget, const Constant *c, EVT VT, int o,
1865 Align Alignment, unsigned TF)
1866 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1867 DebugLoc(), getSDVTList(VT)),
1868 Offset(o), Alignment(Alignment), TargetFlags(TF) {
1869 assert(Offset >= 0 && "Offset is too large")(static_cast <bool> (Offset >= 0 && "Offset is too large"
) ? void (0) : __assert_fail ("Offset >= 0 && \"Offset is too large\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1869, __extension__
__PRETTY_FUNCTION__))
;
1870 Val.ConstVal = c;
1871 }
1872
1873 ConstantPoolSDNode(bool isTarget, MachineConstantPoolValue *v, EVT VT, int o,
1874 Align Alignment, unsigned TF)
1875 : SDNode(isTarget ? ISD::TargetConstantPool : ISD::ConstantPool, 0,
1876 DebugLoc(), getSDVTList(VT)),
1877 Offset(o), Alignment(Alignment), TargetFlags(TF) {
1878 assert(Offset >= 0 && "Offset is too large")(static_cast <bool> (Offset >= 0 && "Offset is too large"
) ? void (0) : __assert_fail ("Offset >= 0 && \"Offset is too large\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1878, __extension__
__PRETTY_FUNCTION__))
;
1879 Val.MachineCPVal = v;
1880 Offset |= 1 << (sizeof(unsigned)*CHAR_BIT8-1);
1881 }
1882
1883public:
1884 bool isMachineConstantPoolEntry() const {
1885 return Offset < 0;
1886 }
1887
1888 const Constant *getConstVal() const {
1889 assert(!isMachineConstantPoolEntry() && "Wrong constantpool type")(static_cast <bool> (!isMachineConstantPoolEntry() &&
"Wrong constantpool type") ? void (0) : __assert_fail ("!isMachineConstantPoolEntry() && \"Wrong constantpool type\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1889, __extension__
__PRETTY_FUNCTION__))
;
1890 return Val.ConstVal;
1891 }
1892
1893 MachineConstantPoolValue *getMachineCPVal() const {
1894 assert(isMachineConstantPoolEntry() && "Wrong constantpool type")(static_cast <bool> (isMachineConstantPoolEntry() &&
"Wrong constantpool type") ? void (0) : __assert_fail ("isMachineConstantPoolEntry() && \"Wrong constantpool type\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 1894, __extension__
__PRETTY_FUNCTION__))
;
1895 return Val.MachineCPVal;
1896 }
1897
1898 int getOffset() const {
1899 return Offset & ~(1 << (sizeof(unsigned)*CHAR_BIT8-1));
1900 }
1901
1902 // Return the alignment of this constant pool object, which is either 0 (for
1903 // default alignment) or the desired value.
1904 Align getAlign() const { return Alignment; }
1905 unsigned getTargetFlags() const { return TargetFlags; }
1906
1907 Type *getType() const;
1908
1909 static bool classof(const SDNode *N) {
1910 return N->getOpcode() == ISD::ConstantPool ||
1911 N->getOpcode() == ISD::TargetConstantPool;
1912 }
1913};
1914
1915/// Completely target-dependent object reference.
1916class TargetIndexSDNode : public SDNode {
1917 friend class SelectionDAG;
1918
1919 unsigned TargetFlags;
1920 int Index;
1921 int64_t Offset;
1922
1923public:
1924 TargetIndexSDNode(int Idx, EVT VT, int64_t Ofs, unsigned TF)
1925 : SDNode(ISD::TargetIndex, 0, DebugLoc(), getSDVTList(VT)),
1926 TargetFlags(TF), Index(Idx), Offset(Ofs) {}
1927
1928 unsigned getTargetFlags() const { return TargetFlags; }
1929 int getIndex() const { return Index; }
1930 int64_t getOffset() const { return Offset; }
1931
1932 static bool classof(const SDNode *N) {
1933 return N->getOpcode() == ISD::TargetIndex;
1934 }
1935};
1936
1937class BasicBlockSDNode : public SDNode {
1938 friend class SelectionDAG;
1939
1940 MachineBasicBlock *MBB;
1941
1942 /// Debug info is meaningful and potentially useful here, but we create
1943 /// blocks out of order when they're jumped to, which makes it a bit
1944 /// harder. Let's see if we need it first.
1945 explicit BasicBlockSDNode(MachineBasicBlock *mbb)
1946 : SDNode(ISD::BasicBlock, 0, DebugLoc(), getSDVTList(MVT::Other)), MBB(mbb)
1947 {}
1948
1949public:
1950 MachineBasicBlock *getBasicBlock() const { return MBB; }
1951
1952 static bool classof(const SDNode *N) {
1953 return N->getOpcode() == ISD::BasicBlock;
1954 }
1955};
1956
1957/// A "pseudo-class" with methods for operating on BUILD_VECTORs.
1958class BuildVectorSDNode : public SDNode {
1959public:
1960 // These are constructed as SDNodes and then cast to BuildVectorSDNodes.
1961 explicit BuildVectorSDNode() = delete;
1962
1963 /// Check if this is a constant splat, and if so, find the
1964 /// smallest element size that splats the vector. If MinSplatBits is
1965 /// nonzero, the element size must be at least that large. Note that the
1966 /// splat element may be the entire vector (i.e., a one element vector).
1967 /// Returns the splat element value in SplatValue. Any undefined bits in
1968 /// that value are zero, and the corresponding bits in the SplatUndef mask
1969 /// are set. The SplatBitSize value is set to the splat element size in
1970 /// bits. HasAnyUndefs is set to true if any bits in the vector are
1971 /// undefined. isBigEndian describes the endianness of the target.
1972 bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
1973 unsigned &SplatBitSize, bool &HasAnyUndefs,
1974 unsigned MinSplatBits = 0,
1975 bool isBigEndian = false) const;
1976
1977 /// Returns the demanded splatted value or a null value if this is not a
1978 /// splat.
1979 ///
1980 /// The DemandedElts mask indicates the elements that must be in the splat.
1981 /// If passed a non-null UndefElements bitvector, it will resize it to match
1982 /// the vector width and set the bits where elements are undef.
1983 SDValue getSplatValue(const APInt &DemandedElts,
1984 BitVector *UndefElements = nullptr) const;
1985
1986 /// Returns the splatted value or a null value if this is not a splat.
1987 ///
1988 /// If passed a non-null UndefElements bitvector, it will resize it to match
1989 /// the vector width and set the bits where elements are undef.
1990 SDValue getSplatValue(BitVector *UndefElements = nullptr) const;
1991
1992 /// Find the shortest repeating sequence of values in the build vector.
1993 ///
1994 /// e.g. { u, X, u, X, u, u, X, u } -> { X }
1995 /// { X, Y, u, Y, u, u, X, u } -> { X, Y }
1996 ///
1997 /// Currently this must be a power-of-2 build vector.
1998 /// The DemandedElts mask indicates the elements that must be present,
1999 /// undemanded elements in Sequence may be null (SDValue()). If passed a
2000 /// non-null UndefElements bitvector, it will resize it to match the original
2001 /// vector width and set the bits where elements are undef. If result is
2002 /// false, Sequence will be empty.
2003 bool getRepeatedSequence(const APInt &DemandedElts,
2004 SmallVectorImpl<SDValue> &Sequence,
2005 BitVector *UndefElements = nullptr) const;
2006
2007 /// Find the shortest repeating sequence of values in the build vector.
2008 ///
2009 /// e.g. { u, X, u, X, u, u, X, u } -> { X }
2010 /// { X, Y, u, Y, u, u, X, u } -> { X, Y }
2011 ///
2012 /// Currently this must be a power-of-2 build vector.
2013 /// If passed a non-null UndefElements bitvector, it will resize it to match
2014 /// the original vector width and set the bits where elements are undef.
2015 /// If result is false, Sequence will be empty.
2016 bool getRepeatedSequence(SmallVectorImpl<SDValue> &Sequence,
2017 BitVector *UndefElements = nullptr) const;
2018
2019 /// Returns the demanded splatted constant or null if this is not a constant
2020 /// splat.
2021 ///
2022 /// The DemandedElts mask indicates the elements that must be in the splat.
2023 /// If passed a non-null UndefElements bitvector, it will resize it to match
2024 /// the vector width and set the bits where elements are undef.
2025 ConstantSDNode *
2026 getConstantSplatNode(const APInt &DemandedElts,
2027 BitVector *UndefElements = nullptr) const;
2028
2029 /// Returns the splatted constant or null if this is not a constant
2030 /// splat.
2031 ///
2032 /// If passed a non-null UndefElements bitvector, it will resize it to match
2033 /// the vector width and set the bits where elements are undef.
2034 ConstantSDNode *
2035 getConstantSplatNode(BitVector *UndefElements = nullptr) const;
2036
2037 /// Returns the demanded splatted constant FP or null if this is not a
2038 /// constant FP splat.
2039 ///
2040 /// The DemandedElts mask indicates the elements that must be in the splat.
2041 /// If passed a non-null UndefElements bitvector, it will resize it to match
2042 /// the vector width and set the bits where elements are undef.
2043 ConstantFPSDNode *
2044 getConstantFPSplatNode(const APInt &DemandedElts,
2045 BitVector *UndefElements = nullptr) const;
2046
2047 /// Returns the splatted constant FP or null if this is not a constant
2048 /// FP splat.
2049 ///
2050 /// If passed a non-null UndefElements bitvector, it will resize it to match
2051 /// the vector width and set the bits where elements are undef.
2052 ConstantFPSDNode *
2053 getConstantFPSplatNode(BitVector *UndefElements = nullptr) const;
2054
2055 /// If this is a constant FP splat and the splatted constant FP is an
2056 /// exact power or 2, return the log base 2 integer value. Otherwise,
2057 /// return -1.
2058 ///
2059 /// The BitWidth specifies the necessary bit precision.
2060 int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements,
2061 uint32_t BitWidth) const;
2062
2063 /// Extract the raw bit data from a build vector of Undef, Constant or
2064 /// ConstantFP node elements. Each raw bit element will be \p
2065 /// DstEltSizeInBits wide, undef elements are treated as zero, and entirely
2066 /// undefined elements are flagged in \p UndefElements.
2067 bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits,
2068 SmallVectorImpl<APInt> &RawBitElements,
2069 BitVector &UndefElements) const;
2070
2071 bool isConstant() const;
2072
2073 /// Recast bit data \p SrcBitElements to \p DstEltSizeInBits wide elements.
2074 /// Undef elements are treated as zero, and entirely undefined elements are
2075 /// flagged in \p DstUndefElements.
2076 static void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits,
2077 SmallVectorImpl<APInt> &DstBitElements,
2078 ArrayRef<APInt> SrcBitElements,
2079 BitVector &DstUndefElements,
2080 const BitVector &SrcUndefElements);
2081
2082 static bool classof(const SDNode *N) {
2083 return N->getOpcode() == ISD::BUILD_VECTOR;
2084 }
2085};
2086
2087/// An SDNode that holds an arbitrary LLVM IR Value. This is
2088/// used when the SelectionDAG needs to make a simple reference to something
2089/// in the LLVM IR representation.
2090///
2091class SrcValueSDNode : public SDNode {
2092 friend class SelectionDAG;
2093
2094 const Value *V;
2095
2096 /// Create a SrcValue for a general value.
2097 explicit SrcValueSDNode(const Value *v)
2098 : SDNode(ISD::SRCVALUE, 0, DebugLoc(), getSDVTList(MVT::Other)), V(v) {}
2099
2100public:
2101 /// Return the contained Value.
2102 const Value *getValue() const { return V; }
2103
2104 static bool classof(const SDNode *N) {
2105 return N->getOpcode() == ISD::SRCVALUE;
2106 }
2107};
2108
2109class MDNodeSDNode : public SDNode {
2110 friend class SelectionDAG;
2111
2112 const MDNode *MD;
2113
2114 explicit MDNodeSDNode(const MDNode *md)
2115 : SDNode(ISD::MDNODE_SDNODE, 0, DebugLoc(), getSDVTList(MVT::Other)), MD(md)
2116 {}
2117
2118public:
2119 const MDNode *getMD() const { return MD; }
2120
2121 static bool classof(const SDNode *N) {
2122 return N->getOpcode() == ISD::MDNODE_SDNODE;
2123 }
2124};
2125
2126class RegisterSDNode : public SDNode {
2127 friend class SelectionDAG;
2128
2129 Register Reg;
2130
2131 RegisterSDNode(Register reg, EVT VT)
2132 : SDNode(ISD::Register, 0, DebugLoc(), getSDVTList(VT)), Reg(reg) {}
2133
2134public:
2135 Register getReg() const { return Reg; }
2136
2137 static bool classof(const SDNode *N) {
2138 return N->getOpcode() == ISD::Register;
2139 }
2140};
2141
2142class RegisterMaskSDNode : public SDNode {
2143 friend class SelectionDAG;
2144
2145 // The memory for RegMask is not owned by the node.
2146 const uint32_t *RegMask;
2147
2148 RegisterMaskSDNode(const uint32_t *mask)
2149 : SDNode(ISD::RegisterMask, 0, DebugLoc(), getSDVTList(MVT::Untyped)),
2150 RegMask(mask) {}
2151
2152public:
2153 const uint32_t *getRegMask() const { return RegMask; }
2154
2155 static bool classof(const SDNode *N) {
2156 return N->getOpcode() == ISD::RegisterMask;
2157 }
2158};
2159
2160class BlockAddressSDNode : public SDNode {
2161 friend class SelectionDAG;
2162
2163 const BlockAddress *BA;
2164 int64_t Offset;
2165 unsigned TargetFlags;
2166
2167 BlockAddressSDNode(unsigned NodeTy, EVT VT, const BlockAddress *ba,
2168 int64_t o, unsigned Flags)
2169 : SDNode(NodeTy, 0, DebugLoc(), getSDVTList(VT)),
2170 BA(ba), Offset(o), TargetFlags(Flags) {}
2171
2172public:
2173 const BlockAddress *getBlockAddress() const { return BA; }
2174 int64_t getOffset() const { return Offset; }
2175 unsigned getTargetFlags() const { return TargetFlags; }
2176
2177 static bool classof(const SDNode *N) {
2178 return N->getOpcode() == ISD::BlockAddress ||
2179 N->getOpcode() == ISD::TargetBlockAddress;
2180 }
2181};
2182
2183class LabelSDNode : public SDNode {
2184 friend class SelectionDAG;
2185
2186 MCSymbol *Label;
2187
2188 LabelSDNode(unsigned Opcode, unsigned Order, const DebugLoc &dl, MCSymbol *L)
2189 : SDNode(Opcode, Order, dl, getSDVTList(MVT::Other)), Label(L) {
2190 assert(LabelSDNode::classof(this) && "not a label opcode")(static_cast <bool> (LabelSDNode::classof(this) &&
"not a label opcode") ? void (0) : __assert_fail ("LabelSDNode::classof(this) && \"not a label opcode\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2190, __extension__
__PRETTY_FUNCTION__))
;
2191 }
2192
2193public:
2194 MCSymbol *getLabel() const { return Label; }
2195
2196 static bool classof(const SDNode *N) {
2197 return N->getOpcode() == ISD::EH_LABEL ||
2198 N->getOpcode() == ISD::ANNOTATION_LABEL;
2199 }
2200};
2201
2202class ExternalSymbolSDNode : public SDNode {
2203 friend class SelectionDAG;
2204
2205 const char *Symbol;
2206 unsigned TargetFlags;
2207
2208 ExternalSymbolSDNode(bool isTarget, const char *Sym, unsigned TF, EVT VT)
2209 : SDNode(isTarget ? ISD::TargetExternalSymbol : ISD::ExternalSymbol, 0,
2210 DebugLoc(), getSDVTList(VT)),
2211 Symbol(Sym), TargetFlags(TF) {}
2212
2213public:
2214 const char *getSymbol() const { return Symbol; }
2215 unsigned getTargetFlags() const { return TargetFlags; }
2216
2217 static bool classof(const SDNode *N) {
2218 return N->getOpcode() == ISD::ExternalSymbol ||
2219 N->getOpcode() == ISD::TargetExternalSymbol;
2220 }
2221};
2222
2223class MCSymbolSDNode : public SDNode {
2224 friend class SelectionDAG;
2225
2226 MCSymbol *Symbol;
2227
2228 MCSymbolSDNode(MCSymbol *Symbol, EVT VT)
2229 : SDNode(ISD::MCSymbol, 0, DebugLoc(), getSDVTList(VT)), Symbol(Symbol) {}
2230
2231public:
2232 MCSymbol *getMCSymbol() const { return Symbol; }
2233
2234 static bool classof(const SDNode *N) {
2235 return N->getOpcode() == ISD::MCSymbol;
2236 }
2237};
2238
2239class CondCodeSDNode : public SDNode {
2240 friend class SelectionDAG;
2241
2242 ISD::CondCode Condition;
2243
2244 explicit CondCodeSDNode(ISD::CondCode Cond)
2245 : SDNode(ISD::CONDCODE, 0, DebugLoc(), getSDVTList(MVT::Other)),
2246 Condition(Cond) {}
2247
2248public:
2249 ISD::CondCode get() const { return Condition; }
2250
2251 static bool classof(const SDNode *N) {
2252 return N->getOpcode() == ISD::CONDCODE;
2253 }
2254};
2255
2256/// This class is used to represent EVT's, which are used
2257/// to parameterize some operations.
2258class VTSDNode : public SDNode {
2259 friend class SelectionDAG;
2260
2261 EVT ValueType;
2262
2263 explicit VTSDNode(EVT VT)
2264 : SDNode(ISD::VALUETYPE, 0, DebugLoc(), getSDVTList(MVT::Other)),
2265 ValueType(VT) {}
2266
2267public:
2268 EVT getVT() const { return ValueType; }
2269
2270 static bool classof(const SDNode *N) {
2271 return N->getOpcode() == ISD::VALUETYPE;
2272 }
2273};
2274
2275/// Base class for LoadSDNode and StoreSDNode
2276class LSBaseSDNode : public MemSDNode {
2277public:
2278 LSBaseSDNode(ISD::NodeType NodeTy, unsigned Order, const DebugLoc &dl,
2279 SDVTList VTs, ISD::MemIndexedMode AM, EVT MemVT,
2280 MachineMemOperand *MMO)
2281 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2282 LSBaseSDNodeBits.AddressingMode = AM;
2283 assert(getAddressingMode() == AM && "Value truncated")(static_cast <bool> (getAddressingMode() == AM &&
"Value truncated") ? void (0) : __assert_fail ("getAddressingMode() == AM && \"Value truncated\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2283, __extension__
__PRETTY_FUNCTION__))
;
2284 }
2285
2286 const SDValue &getOffset() const {
2287 return getOperand(getOpcode() == ISD::LOAD ? 2 : 3);
2288 }
2289
2290 /// Return the addressing mode for this load or store:
2291 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2292 ISD::MemIndexedMode getAddressingMode() const {
2293 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2294 }
2295
2296 /// Return true if this is a pre/post inc/dec load/store.
2297 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2298
2299 /// Return true if this is NOT a pre/post inc/dec load/store.
2300 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2301
2302 static bool classof(const SDNode *N) {
2303 return N->getOpcode() == ISD::LOAD ||
2304 N->getOpcode() == ISD::STORE;
2305 }
2306};
2307
2308/// This class is used to represent ISD::LOAD nodes.
2309class LoadSDNode : public LSBaseSDNode {
2310 friend class SelectionDAG;
2311
2312 LoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2313 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, EVT MemVT,
2314 MachineMemOperand *MMO)
2315 : LSBaseSDNode(ISD::LOAD, Order, dl, VTs, AM, MemVT, MMO) {
2316 LoadSDNodeBits.ExtTy = ETy;
2317 assert(readMem() && "Load MachineMemOperand is not a load!")(static_cast <bool> (readMem() && "Load MachineMemOperand is not a load!"
) ? void (0) : __assert_fail ("readMem() && \"Load MachineMemOperand is not a load!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2317, __extension__
__PRETTY_FUNCTION__))
;
2318 assert(!writeMem() && "Load MachineMemOperand is a store!")(static_cast <bool> (!writeMem() && "Load MachineMemOperand is a store!"
) ? void (0) : __assert_fail ("!writeMem() && \"Load MachineMemOperand is a store!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2318, __extension__
__PRETTY_FUNCTION__))
;
2319 }
2320
2321public:
2322 /// Return whether this is a plain node,
2323 /// or one of the varieties of value-extending loads.
2324 ISD::LoadExtType getExtensionType() const {
2325 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2326 }
2327
2328 const SDValue &getBasePtr() const { return getOperand(1); }
2329 const SDValue &getOffset() const { return getOperand(2); }
2330
2331 static bool classof(const SDNode *N) {
2332 return N->getOpcode() == ISD::LOAD;
2333 }
2334};
2335
2336/// This class is used to represent ISD::STORE nodes.
2337class StoreSDNode : public LSBaseSDNode {
2338 friend class SelectionDAG;
2339
2340 StoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2341 ISD::MemIndexedMode AM, bool isTrunc, EVT MemVT,
2342 MachineMemOperand *MMO)
2343 : LSBaseSDNode(ISD::STORE, Order, dl, VTs, AM, MemVT, MMO) {
2344 StoreSDNodeBits.IsTruncating = isTrunc;
2345 assert(!readMem() && "Store MachineMemOperand is a load!")(static_cast <bool> (!readMem() && "Store MachineMemOperand is a load!"
) ? void (0) : __assert_fail ("!readMem() && \"Store MachineMemOperand is a load!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2345, __extension__
__PRETTY_FUNCTION__))
;
2346 assert(writeMem() && "Store MachineMemOperand is not a store!")(static_cast <bool> (writeMem() && "Store MachineMemOperand is not a store!"
) ? void (0) : __assert_fail ("writeMem() && \"Store MachineMemOperand is not a store!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2346, __extension__
__PRETTY_FUNCTION__))
;
2347 }
2348
2349public:
2350 /// Return true if the op does a truncation before store.
2351 /// For integers this is the same as doing a TRUNCATE and storing the result.
2352 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2353 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2354 void setTruncatingStore(bool Truncating) {
2355 StoreSDNodeBits.IsTruncating = Truncating;
2356 }
2357
2358 const SDValue &getValue() const { return getOperand(1); }
2359 const SDValue &getBasePtr() const { return getOperand(2); }
2360 const SDValue &getOffset() const { return getOperand(3); }
2361
2362 static bool classof(const SDNode *N) {
2363 return N->getOpcode() == ISD::STORE;
2364 }
2365};
2366
2367/// This base class is used to represent VP_LOAD, VP_STORE,
2368/// EXPERIMENTAL_VP_STRIDED_LOAD and EXPERIMENTAL_VP_STRIDED_STORE nodes
2369class VPBaseLoadStoreSDNode : public MemSDNode {
2370public:
2371 friend class SelectionDAG;
2372
2373 VPBaseLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
2374 const DebugLoc &DL, SDVTList VTs,
2375 ISD::MemIndexedMode AM, EVT MemVT,
2376 MachineMemOperand *MMO)
2377 : MemSDNode(NodeTy, Order, DL, VTs, MemVT, MMO) {
2378 LSBaseSDNodeBits.AddressingMode = AM;
2379 assert(getAddressingMode() == AM && "Value truncated")(static_cast <bool> (getAddressingMode() == AM &&
"Value truncated") ? void (0) : __assert_fail ("getAddressingMode() == AM && \"Value truncated\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2379, __extension__
__PRETTY_FUNCTION__))
;
2380 }
2381
2382 // VPStridedStoreSDNode (Chain, Data, Ptr, Offset, Stride, Mask, EVL)
2383 // VPStoreSDNode (Chain, Data, Ptr, Offset, Mask, EVL)
2384 // VPStridedLoadSDNode (Chain, Ptr, Offset, Stride, Mask, EVL)
2385 // VPLoadSDNode (Chain, Ptr, Offset, Mask, EVL)
2386 // Mask is a vector of i1 elements;
2387 // the type of EVL is TLI.getVPExplicitVectorLengthTy().
2388 const SDValue &getOffset() const {
2389 return getOperand((getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD ||
2390 getOpcode() == ISD::VP_LOAD)
2391 ? 2
2392 : 3);
2393 }
2394 const SDValue &getBasePtr() const {
2395 return getOperand((getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD ||
2396 getOpcode() == ISD::VP_LOAD)
2397 ? 1
2398 : 2);
2399 }
2400 const SDValue &getMask() const {
2401 switch (getOpcode()) {
2402 default:
2403 llvm_unreachable("Invalid opcode")::llvm::llvm_unreachable_internal("Invalid opcode", "llvm/include/llvm/CodeGen/SelectionDAGNodes.h"
, 2403)
;
2404 case ISD::VP_LOAD:
2405 return getOperand(3);
2406 case ISD::VP_STORE:
2407 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
2408 return getOperand(4);
2409 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
2410 return getOperand(5);
2411 }
2412 }
2413 const SDValue &getVectorLength() const {
2414 switch (getOpcode()) {
2415 default:
2416 llvm_unreachable("Invalid opcode")::llvm::llvm_unreachable_internal("Invalid opcode", "llvm/include/llvm/CodeGen/SelectionDAGNodes.h"
, 2416)
;
2417 case ISD::VP_LOAD:
2418 return getOperand(4);
2419 case ISD::VP_STORE:
2420 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD:
2421 return getOperand(5);
2422 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
2423 return getOperand(6);
2424 }
2425 }
2426
2427 /// Return the addressing mode for this load or store:
2428 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2429 ISD::MemIndexedMode getAddressingMode() const {
2430 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2431 }
2432
2433 /// Return true if this is a pre/post inc/dec load/store.
2434 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2435
2436 /// Return true if this is NOT a pre/post inc/dec load/store.
2437 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2438
2439 static bool classof(const SDNode *N) {
2440 return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD ||
2441 N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE ||
2442 N->getOpcode() == ISD::VP_LOAD || N->getOpcode() == ISD::VP_STORE;
2443 }
2444};
2445
2446/// This class is used to represent a VP_LOAD node
2447class VPLoadSDNode : public VPBaseLoadStoreSDNode {
2448public:
2449 friend class SelectionDAG;
2450
2451 VPLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2452 ISD::MemIndexedMode AM, ISD::LoadExtType ETy, bool isExpanding,
2453 EVT MemVT, MachineMemOperand *MMO)
2454 : VPBaseLoadStoreSDNode(ISD::VP_LOAD, Order, dl, VTs, AM, MemVT, MMO) {
2455 LoadSDNodeBits.ExtTy = ETy;
2456 LoadSDNodeBits.IsExpanding = isExpanding;
2457 }
2458
2459 ISD::LoadExtType getExtensionType() const {
2460 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2461 }
2462
2463 const SDValue &getBasePtr() const { return getOperand(1); }
2464 const SDValue &getOffset() const { return getOperand(2); }
2465 const SDValue &getMask() const { return getOperand(3); }
2466 const SDValue &getVectorLength() const { return getOperand(4); }
2467
2468 static bool classof(const SDNode *N) {
2469 return N->getOpcode() == ISD::VP_LOAD;
2470 }
2471 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2472};
2473
2474/// This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
2475class VPStridedLoadSDNode : public VPBaseLoadStoreSDNode {
2476public:
2477 friend class SelectionDAG;
2478
2479 VPStridedLoadSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs,
2480 ISD::MemIndexedMode AM, ISD::LoadExtType ETy,
2481 bool IsExpanding, EVT MemVT, MachineMemOperand *MMO)
2482 : VPBaseLoadStoreSDNode(ISD::EXPERIMENTAL_VP_STRIDED_LOAD, Order, DL, VTs,
2483 AM, MemVT, MMO) {
2484 LoadSDNodeBits.ExtTy = ETy;
2485 LoadSDNodeBits.IsExpanding = IsExpanding;
2486 }
2487
2488 ISD::LoadExtType getExtensionType() const {
2489 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2490 }
2491
2492 const SDValue &getBasePtr() const { return getOperand(1); }
2493 const SDValue &getOffset() const { return getOperand(2); }
2494 const SDValue &getStride() const { return getOperand(3); }
2495 const SDValue &getMask() const { return getOperand(4); }
2496 const SDValue &getVectorLength() const { return getOperand(5); }
2497
2498 static bool classof(const SDNode *N) {
2499 return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_LOAD;
2500 }
2501 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2502};
2503
2504/// This class is used to represent a VP_STORE node
2505class VPStoreSDNode : public VPBaseLoadStoreSDNode {
2506public:
2507 friend class SelectionDAG;
2508
2509 VPStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2510 ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing,
2511 EVT MemVT, MachineMemOperand *MMO)
2512 : VPBaseLoadStoreSDNode(ISD::VP_STORE, Order, dl, VTs, AM, MemVT, MMO) {
2513 StoreSDNodeBits.IsTruncating = isTrunc;
2514 StoreSDNodeBits.IsCompressing = isCompressing;
2515 }
2516
2517 /// Return true if this is a truncating store.
2518 /// For integers this is the same as doing a TRUNCATE and storing the result.
2519 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2520 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2521
2522 /// Returns true if the op does a compression to the vector before storing.
2523 /// The node contiguously stores the active elements (integers or floats)
2524 /// in src (those with their respective bit set in writemask k) to unaligned
2525 /// memory at base_addr.
2526 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2527
2528 const SDValue &getValue() const { return getOperand(1); }
2529 const SDValue &getBasePtr() const { return getOperand(2); }
2530 const SDValue &getOffset() const { return getOperand(3); }
2531 const SDValue &getMask() const { return getOperand(4); }
2532 const SDValue &getVectorLength() const { return getOperand(5); }
2533
2534 static bool classof(const SDNode *N) {
2535 return N->getOpcode() == ISD::VP_STORE;
2536 }
2537};
2538
2539/// This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
2540class VPStridedStoreSDNode : public VPBaseLoadStoreSDNode {
2541public:
2542 friend class SelectionDAG;
2543
2544 VPStridedStoreSDNode(unsigned Order, const DebugLoc &DL, SDVTList VTs,
2545 ISD::MemIndexedMode AM, bool IsTrunc, bool IsCompressing,
2546 EVT MemVT, MachineMemOperand *MMO)
2547 : VPBaseLoadStoreSDNode(ISD::EXPERIMENTAL_VP_STRIDED_STORE, Order, DL,
2548 VTs, AM, MemVT, MMO) {
2549 StoreSDNodeBits.IsTruncating = IsTrunc;
2550 StoreSDNodeBits.IsCompressing = IsCompressing;
2551 }
2552
2553 /// Return true if this is a truncating store.
2554 /// For integers this is the same as doing a TRUNCATE and storing the result.
2555 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2556 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2557
2558 /// Returns true if the op does a compression to the vector before storing.
2559 /// The node contiguously stores the active elements (integers or floats)
2560 /// in src (those with their respective bit set in writemask k) to unaligned
2561 /// memory at base_addr.
2562 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2563
2564 const SDValue &getValue() const { return getOperand(1); }
2565 const SDValue &getBasePtr() const { return getOperand(2); }
2566 const SDValue &getOffset() const { return getOperand(3); }
2567 const SDValue &getStride() const { return getOperand(4); }
2568 const SDValue &getMask() const { return getOperand(5); }
2569 const SDValue &getVectorLength() const { return getOperand(6); }
2570
2571 static bool classof(const SDNode *N) {
2572 return N->getOpcode() == ISD::EXPERIMENTAL_VP_STRIDED_STORE;
2573 }
2574};
2575
2576/// This base class is used to represent MLOAD and MSTORE nodes
2577class MaskedLoadStoreSDNode : public MemSDNode {
2578public:
2579 friend class SelectionDAG;
2580
2581 MaskedLoadStoreSDNode(ISD::NodeType NodeTy, unsigned Order,
2582 const DebugLoc &dl, SDVTList VTs,
2583 ISD::MemIndexedMode AM, EVT MemVT,
2584 MachineMemOperand *MMO)
2585 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2586 LSBaseSDNodeBits.AddressingMode = AM;
2587 assert(getAddressingMode() == AM && "Value truncated")(static_cast <bool> (getAddressingMode() == AM &&
"Value truncated") ? void (0) : __assert_fail ("getAddressingMode() == AM && \"Value truncated\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2587, __extension__
__PRETTY_FUNCTION__))
;
2588 }
2589
2590 // MaskedLoadSDNode (Chain, ptr, offset, mask, passthru)
2591 // MaskedStoreSDNode (Chain, data, ptr, offset, mask)
2592 // Mask is a vector of i1 elements
2593 const SDValue &getOffset() const {
2594 return getOperand(getOpcode() == ISD::MLOAD ? 2 : 3);
2595 }
2596 const SDValue &getMask() const {
2597 return getOperand(getOpcode() == ISD::MLOAD ? 3 : 4);
2598 }
2599
2600 /// Return the addressing mode for this load or store:
2601 /// unindexed, pre-inc, pre-dec, post-inc, or post-dec.
2602 ISD::MemIndexedMode getAddressingMode() const {
2603 return static_cast<ISD::MemIndexedMode>(LSBaseSDNodeBits.AddressingMode);
2604 }
2605
2606 /// Return true if this is a pre/post inc/dec load/store.
2607 bool isIndexed() const { return getAddressingMode() != ISD::UNINDEXED; }
2608
2609 /// Return true if this is NOT a pre/post inc/dec load/store.
2610 bool isUnindexed() const { return getAddressingMode() == ISD::UNINDEXED; }
2611
2612 static bool classof(const SDNode *N) {
2613 return N->getOpcode() == ISD::MLOAD ||
2614 N->getOpcode() == ISD::MSTORE;
2615 }
2616};
2617
2618/// This class is used to represent an MLOAD node
2619class MaskedLoadSDNode : public MaskedLoadStoreSDNode {
2620public:
2621 friend class SelectionDAG;
2622
2623 MaskedLoadSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2624 ISD::MemIndexedMode AM, ISD::LoadExtType ETy,
2625 bool IsExpanding, EVT MemVT, MachineMemOperand *MMO)
2626 : MaskedLoadStoreSDNode(ISD::MLOAD, Order, dl, VTs, AM, MemVT, MMO) {
2627 LoadSDNodeBits.ExtTy = ETy;
2628 LoadSDNodeBits.IsExpanding = IsExpanding;
2629 }
2630
2631 ISD::LoadExtType getExtensionType() const {
2632 return static_cast<ISD::LoadExtType>(LoadSDNodeBits.ExtTy);
2633 }
2634
2635 const SDValue &getBasePtr() const { return getOperand(1); }
2636 const SDValue &getOffset() const { return getOperand(2); }
2637 const SDValue &getMask() const { return getOperand(3); }
2638 const SDValue &getPassThru() const { return getOperand(4); }
2639
2640 static bool classof(const SDNode *N) {
2641 return N->getOpcode() == ISD::MLOAD;
2642 }
2643
2644 bool isExpandingLoad() const { return LoadSDNodeBits.IsExpanding; }
2645};
2646
2647/// This class is used to represent an MSTORE node
2648class MaskedStoreSDNode : public MaskedLoadStoreSDNode {
2649public:
2650 friend class SelectionDAG;
2651
2652 MaskedStoreSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2653 ISD::MemIndexedMode AM, bool isTrunc, bool isCompressing,
2654 EVT MemVT, MachineMemOperand *MMO)
2655 : MaskedLoadStoreSDNode(ISD::MSTORE, Order, dl, VTs, AM, MemVT, MMO) {
2656 StoreSDNodeBits.IsTruncating = isTrunc;
2657 StoreSDNodeBits.IsCompressing = isCompressing;
2658 }
2659
2660 /// Return true if the op does a truncation before store.
2661 /// For integers this is the same as doing a TRUNCATE and storing the result.
2662 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2663 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2664
2665 /// Returns true if the op does a compression to the vector before storing.
2666 /// The node contiguously stores the active elements (integers or floats)
2667 /// in src (those with their respective bit set in writemask k) to unaligned
2668 /// memory at base_addr.
2669 bool isCompressingStore() const { return StoreSDNodeBits.IsCompressing; }
2670
2671 const SDValue &getValue() const { return getOperand(1); }
2672 const SDValue &getBasePtr() const { return getOperand(2); }
2673 const SDValue &getOffset() const { return getOperand(3); }
2674 const SDValue &getMask() const { return getOperand(4); }
2675
2676 static bool classof(const SDNode *N) {
2677 return N->getOpcode() == ISD::MSTORE;
2678 }
2679};
2680
2681/// This is a base class used to represent
2682/// VP_GATHER and VP_SCATTER nodes
2683///
2684class VPGatherScatterSDNode : public MemSDNode {
2685public:
2686 friend class SelectionDAG;
2687
2688 VPGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2689 const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2690 MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2691 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2692 LSBaseSDNodeBits.AddressingMode = IndexType;
2693 assert(getIndexType() == IndexType && "Value truncated")(static_cast <bool> (getIndexType() == IndexType &&
"Value truncated") ? void (0) : __assert_fail ("getIndexType() == IndexType && \"Value truncated\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2693, __extension__
__PRETTY_FUNCTION__))
;
2694 }
2695
2696 /// How is Index applied to BasePtr when computing addresses.
2697 ISD::MemIndexType getIndexType() const {
2698 return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
2699 }
2700 bool isIndexScaled() const {
2701 return (getIndexType() == ISD::SIGNED_SCALED) ||
2702 (getIndexType() == ISD::UNSIGNED_SCALED);
2703 }
2704 bool isIndexSigned() const {
2705 return (getIndexType() == ISD::SIGNED_SCALED) ||
2706 (getIndexType() == ISD::SIGNED_UNSCALED);
2707 }
2708
2709 // In the both nodes address is Op1, mask is Op2:
2710 // VPGatherSDNode (Chain, base, index, scale, mask, vlen)
2711 // VPScatterSDNode (Chain, value, base, index, scale, mask, vlen)
2712 // Mask is a vector of i1 elements
2713 const SDValue &getBasePtr() const {
2714 return getOperand((getOpcode() == ISD::VP_GATHER) ? 1 : 2);
2715 }
2716 const SDValue &getIndex() const {
2717 return getOperand((getOpcode() == ISD::VP_GATHER) ? 2 : 3);
2718 }
2719 const SDValue &getScale() const {
2720 return getOperand((getOpcode() == ISD::VP_GATHER) ? 3 : 4);
2721 }
2722 const SDValue &getMask() const {
2723 return getOperand((getOpcode() == ISD::VP_GATHER) ? 4 : 5);
2724 }
2725 const SDValue &getVectorLength() const {
2726 return getOperand((getOpcode() == ISD::VP_GATHER) ? 5 : 6);
2727 }
2728
2729 static bool classof(const SDNode *N) {
2730 return N->getOpcode() == ISD::VP_GATHER ||
2731 N->getOpcode() == ISD::VP_SCATTER;
2732 }
2733};
2734
2735/// This class is used to represent an VP_GATHER node
2736///
2737class VPGatherSDNode : public VPGatherScatterSDNode {
2738public:
2739 friend class SelectionDAG;
2740
2741 VPGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2742 MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2743 : VPGatherScatterSDNode(ISD::VP_GATHER, Order, dl, VTs, MemVT, MMO,
2744 IndexType) {}
2745
2746 static bool classof(const SDNode *N) {
2747 return N->getOpcode() == ISD::VP_GATHER;
2748 }
2749};
2750
2751/// This class is used to represent an VP_SCATTER node
2752///
2753class VPScatterSDNode : public VPGatherScatterSDNode {
2754public:
2755 friend class SelectionDAG;
2756
2757 VPScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2758 MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2759 : VPGatherScatterSDNode(ISD::VP_SCATTER, Order, dl, VTs, MemVT, MMO,
2760 IndexType) {}
2761
2762 const SDValue &getValue() const { return getOperand(1); }
2763
2764 static bool classof(const SDNode *N) {
2765 return N->getOpcode() == ISD::VP_SCATTER;
2766 }
2767};
2768
2769/// This is a base class used to represent
2770/// MGATHER and MSCATTER nodes
2771///
2772class MaskedGatherScatterSDNode : public MemSDNode {
2773public:
2774 friend class SelectionDAG;
2775
2776 MaskedGatherScatterSDNode(ISD::NodeType NodeTy, unsigned Order,
2777 const DebugLoc &dl, SDVTList VTs, EVT MemVT,
2778 MachineMemOperand *MMO, ISD::MemIndexType IndexType)
2779 : MemSDNode(NodeTy, Order, dl, VTs, MemVT, MMO) {
2780 LSBaseSDNodeBits.AddressingMode = IndexType;
2781 assert(getIndexType() == IndexType && "Value truncated")(static_cast <bool> (getIndexType() == IndexType &&
"Value truncated") ? void (0) : __assert_fail ("getIndexType() == IndexType && \"Value truncated\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2781, __extension__
__PRETTY_FUNCTION__))
;
2782 }
2783
2784 /// How is Index applied to BasePtr when computing addresses.
2785 ISD::MemIndexType getIndexType() const {
2786 return static_cast<ISD::MemIndexType>(LSBaseSDNodeBits.AddressingMode);
2787 }
2788 void setIndexType(ISD::MemIndexType IndexType) {
2789 LSBaseSDNodeBits.AddressingMode = IndexType;
2790 }
2791 bool isIndexScaled() const {
2792 return (getIndexType() == ISD::SIGNED_SCALED) ||
2793 (getIndexType() == ISD::UNSIGNED_SCALED);
2794 }
2795 bool isIndexSigned() const {
2796 return (getIndexType() == ISD::SIGNED_SCALED) ||
2797 (getIndexType() == ISD::SIGNED_UNSCALED);
2798 }
2799
2800 // In the both nodes address is Op1, mask is Op2:
2801 // MaskedGatherSDNode (Chain, passthru, mask, base, index, scale)
2802 // MaskedScatterSDNode (Chain, value, mask, base, index, scale)
2803 // Mask is a vector of i1 elements
2804 const SDValue &getBasePtr() const { return getOperand(3); }
2805 const SDValue &getIndex() const { return getOperand(4); }
2806 const SDValue &getMask() const { return getOperand(2); }
2807 const SDValue &getScale() const { return getOperand(5); }
2808
2809 static bool classof(const SDNode *N) {
2810 return N->getOpcode() == ISD::MGATHER ||
2811 N->getOpcode() == ISD::MSCATTER;
2812 }
2813};
2814
2815/// This class is used to represent an MGATHER node
2816///
2817class MaskedGatherSDNode : public MaskedGatherScatterSDNode {
2818public:
2819 friend class SelectionDAG;
2820
2821 MaskedGatherSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2822 EVT MemVT, MachineMemOperand *MMO,
2823 ISD::MemIndexType IndexType, ISD::LoadExtType ETy)
2824 : MaskedGatherScatterSDNode(ISD::MGATHER, Order, dl, VTs, MemVT, MMO,
2825 IndexType) {
2826 LoadSDNodeBits.ExtTy = ETy;
2827 }
2828
2829 const SDValue &getPassThru() const { return getOperand(1); }
2830
2831 ISD::LoadExtType getExtensionType() const {
2832 return ISD::LoadExtType(LoadSDNodeBits.ExtTy);
2833 }
2834
2835 static bool classof(const SDNode *N) {
2836 return N->getOpcode() == ISD::MGATHER;
2837 }
2838};
2839
2840/// This class is used to represent an MSCATTER node
2841///
2842class MaskedScatterSDNode : public MaskedGatherScatterSDNode {
2843public:
2844 friend class SelectionDAG;
2845
2846 MaskedScatterSDNode(unsigned Order, const DebugLoc &dl, SDVTList VTs,
2847 EVT MemVT, MachineMemOperand *MMO,
2848 ISD::MemIndexType IndexType, bool IsTrunc)
2849 : MaskedGatherScatterSDNode(ISD::MSCATTER, Order, dl, VTs, MemVT, MMO,
2850 IndexType) {
2851 StoreSDNodeBits.IsTruncating = IsTrunc;
2852 }
2853
2854 /// Return true if the op does a truncation before store.
2855 /// For integers this is the same as doing a TRUNCATE and storing the result.
2856 /// For floats, it is the same as doing an FP_ROUND and storing the result.
2857 bool isTruncatingStore() const { return StoreSDNodeBits.IsTruncating; }
2858
2859 const SDValue &getValue() const { return getOperand(1); }
2860
2861 static bool classof(const SDNode *N) {
2862 return N->getOpcode() == ISD::MSCATTER;
2863 }
2864};
2865
2866/// An SDNode that represents everything that will be needed
2867/// to construct a MachineInstr. These nodes are created during the
2868/// instruction selection proper phase.
2869///
2870/// Note that the only supported way to set the `memoperands` is by calling the
2871/// `SelectionDAG::setNodeMemRefs` function as the memory management happens
2872/// inside the DAG rather than in the node.
2873class MachineSDNode : public SDNode {
2874private:
2875 friend class SelectionDAG;
2876
2877 MachineSDNode(unsigned Opc, unsigned Order, const DebugLoc &DL, SDVTList VTs)
2878 : SDNode(Opc, Order, DL, VTs) {}
2879
2880 // We use a pointer union between a single `MachineMemOperand` pointer and
2881 // a pointer to an array of `MachineMemOperand` pointers. This is null when
2882 // the number of these is zero, the single pointer variant used when the
2883 // number is one, and the array is used for larger numbers.
2884 //
2885 // The array is allocated via the `SelectionDAG`'s allocator and so will
2886 // always live until the DAG is cleaned up and doesn't require ownership here.
2887 //
2888 // We can't use something simpler like `TinyPtrVector` here because `SDNode`
2889 // subclasses aren't managed in a conforming C++ manner. See the comments on
2890 // `SelectionDAG::MorphNodeTo` which details what all goes on, but the
2891 // constraint here is that these don't manage memory with their constructor or
2892 // destructor and can be initialized to a good state even if they start off
2893 // uninitialized.
2894 PointerUnion<MachineMemOperand *, MachineMemOperand **> MemRefs = {};
2895
2896 // Note that this could be folded into the above `MemRefs` member if doing so
2897 // is advantageous at some point. We don't need to store this in most cases.
2898 // However, at the moment this doesn't appear to make the allocation any
2899 // smaller and makes the code somewhat simpler to read.
2900 int NumMemRefs = 0;
2901
2902public:
2903 using mmo_iterator = ArrayRef<MachineMemOperand *>::const_iterator;
2904
2905 ArrayRef<MachineMemOperand *> memoperands() const {
2906 // Special case the common cases.
2907 if (NumMemRefs == 0)
2908 return {};
2909 if (NumMemRefs == 1)
2910 return makeArrayRef(MemRefs.getAddrOfPtr1(), 1);
2911
2912 // Otherwise we have an actual array.
2913 return makeArrayRef(MemRefs.get<MachineMemOperand **>(), NumMemRefs);
2914 }
2915 mmo_iterator memoperands_begin() const { return memoperands().begin(); }
2916 mmo_iterator memoperands_end() const { return memoperands().end(); }
2917 bool memoperands_empty() const { return memoperands().empty(); }
2918
2919 /// Clear out the memory reference descriptor list.
2920 void clearMemRefs() {
2921 MemRefs = nullptr;
2922 NumMemRefs = 0;
2923 }
2924
2925 static bool classof(const SDNode *N) {
2926 return N->isMachineOpcode();
2927 }
2928};
2929
2930/// An SDNode that records if a register contains a value that is guaranteed to
2931/// be aligned accordingly.
2932class AssertAlignSDNode : public SDNode {
2933 Align Alignment;
2934
2935public:
2936 AssertAlignSDNode(unsigned Order, const DebugLoc &DL, EVT VT, Align A)
2937 : SDNode(ISD::AssertAlign, Order, DL, getSDVTList(VT)), Alignment(A) {}
2938
2939 Align getAlign() const { return Alignment; }
2940
2941 static bool classof(const SDNode *N) {
2942 return N->getOpcode() == ISD::AssertAlign;
2943 }
2944};
2945
2946class SDNodeIterator {
2947 const SDNode *Node;
2948 unsigned Operand;
2949
2950 SDNodeIterator(const SDNode *N, unsigned Op) : Node(N), Operand(Op) {}
2951
2952public:
2953 using iterator_category = std::forward_iterator_tag;
2954 using value_type = SDNode;
2955 using difference_type = std::ptrdiff_t;
2956 using pointer = value_type *;
2957 using reference = value_type &;
2958
2959 bool operator==(const SDNodeIterator& x) const {
2960 return Operand == x.Operand;
2961 }
2962 bool operator!=(const SDNodeIterator& x) const { return !operator==(x); }
2963
2964 pointer operator*() const {
2965 return Node->getOperand(Operand).getNode();
2966 }
2967 pointer operator->() const { return operator*(); }
2968
2969 SDNodeIterator& operator++() { // Preincrement
2970 ++Operand;
2971 return *this;
2972 }
2973 SDNodeIterator operator++(int) { // Postincrement
2974 SDNodeIterator tmp = *this; ++*this; return tmp;
2975 }
2976 size_t operator-(SDNodeIterator Other) const {
2977 assert(Node == Other.Node &&(static_cast <bool> (Node == Other.Node && "Cannot compare iterators of two different nodes!"
) ? void (0) : __assert_fail ("Node == Other.Node && \"Cannot compare iterators of two different nodes!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2978, __extension__
__PRETTY_FUNCTION__))
2978 "Cannot compare iterators of two different nodes!")(static_cast <bool> (Node == Other.Node && "Cannot compare iterators of two different nodes!"
) ? void (0) : __assert_fail ("Node == Other.Node && \"Cannot compare iterators of two different nodes!\""
, "llvm/include/llvm/CodeGen/SelectionDAGNodes.h", 2978, __extension__
__PRETTY_FUNCTION__))
;
2979 return Operand - Other.Operand;
2980 }
2981
2982 static SDNodeIterator begin(const SDNode *N) { return SDNodeIterator(N, 0); }
2983 static SDNodeIterator end (const SDNode *N) {
2984 return SDNodeIterator(N, N->getNumOperands());
2985 }
2986
2987 unsigned getOperand() const { return Operand; }
2988 const SDNode *getNode() const { return Node; }
2989};
2990
2991template <> struct GraphTraits<SDNode*> {
2992 using NodeRef = SDNode *;
2993 using ChildIteratorType = SDNodeIterator;
2994
2995 static NodeRef getEntryNode(SDNode *N) { return N; }
2996
2997 static ChildIteratorType child_begin(NodeRef N) {
2998 return SDNodeIterator::begin(N);
2999 }
3000
3001 static ChildIteratorType child_end(NodeRef N) {
3002 return SDNodeIterator::end(N);
3003 }
3004};
3005
3006/// A representation of the largest SDNode, for use in sizeof().
3007///
3008/// This needs to be a union because the largest node differs on 32 bit systems
3009/// with 4 and 8 byte pointer alignment, respectively.
3010using LargestSDNode = AlignedCharArrayUnion<AtomicSDNode, TargetIndexSDNode,
3011 BlockAddressSDNode,
3012 GlobalAddressSDNode,
3013 PseudoProbeSDNode>;
3014
3015/// The SDNode class with the greatest alignment requirement.
3016using MostAlignedSDNode = GlobalAddressSDNode;
3017
3018namespace ISD {
3019
3020 /// Returns true if the specified node is a non-extending and unindexed load.
3021 inline bool isNormalLoad(const SDNode *N) {
3022 const LoadSDNode *Ld = dyn_cast<LoadSDNode>(N);
3023 return Ld && Ld->getExtensionType() == ISD::NON_EXTLOAD &&
3024 Ld->getAddressingMode() == ISD::UNINDEXED;
3025 }
3026
3027 /// Returns true if the specified node is a non-extending load.
3028 inline bool isNON_EXTLoad(const SDNode *N) {
3029 return isa<LoadSDNode>(N) &&
3030 cast<LoadSDNode>(N)->getExtensionType() == ISD::NON_EXTLOAD;
3031 }
3032
3033 /// Returns true if the specified node is a EXTLOAD.
3034 inline bool isEXTLoad(const SDNode *N) {
3035 return isa<LoadSDNode>(N) &&
3036 cast<LoadSDNode>(N)->getExtensionType() == ISD::EXTLOAD;
3037 }
3038
3039 /// Returns true if the specified node is a SEXTLOAD.
3040 inline bool isSEXTLoad(const SDNode *N) {
3041 return isa<LoadSDNode>(N) &&
3042 cast<LoadSDNode>(N)->getExtensionType() == ISD::SEXTLOAD;
3043 }
3044
3045 /// Returns true if the specified node is a ZEXTLOAD.
3046 inline bool isZEXTLoad(const SDNode *N) {
3047 return isa<LoadSDNode>(N) &&
3048 cast<LoadSDNode>(N)->getExtensionType() == ISD::ZEXTLOAD;
3049 }
3050
3051 /// Returns true if the specified node is an unindexed load.
3052 inline bool isUNINDEXEDLoad(const SDNode *N) {
3053 return isa<LoadSDNode>(N) &&
3054 cast<LoadSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
3055 }
3056
3057 /// Returns true if the specified node is a non-truncating
3058 /// and unindexed store.
3059 inline bool isNormalStore(const SDNode *N) {
3060 const StoreSDNode *St = dyn_cast<StoreSDNode>(N);
3061 return St && !St->isTruncatingStore() &&
3062 St->getAddressingMode() == ISD::UNINDEXED;
3063 }
3064
3065 /// Returns true if the specified node is an unindexed store.
3066 inline bool isUNINDEXEDStore(const SDNode *N) {
3067 return isa<StoreSDNode>(N) &&
3068 cast<StoreSDNode>(N)->getAddressingMode() == ISD::UNINDEXED;
3069 }
3070
3071 /// Attempt to match a unary predicate against a scalar/splat constant or
3072 /// every element of a constant BUILD_VECTOR.
3073 /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
3074 bool matchUnaryPredicate(SDValue Op,
3075 std::function<bool(ConstantSDNode *)> Match,
3076 bool AllowUndefs = false);
3077
3078 /// Attempt to match a binary predicate against a pair of scalar/splat
3079 /// constants or every element of a pair of constant BUILD_VECTORs.
3080 /// If AllowUndef is true, then UNDEF elements will pass nullptr to Match.
3081 /// If AllowTypeMismatch is true then RetType + ArgTypes don't need to match.
3082 bool matchBinaryPredicate(
3083 SDValue LHS, SDValue RHS,
3084 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
3085 bool AllowUndefs = false, bool AllowTypeMismatch = false);
3086
3087 /// Returns true if the specified value is the overflow result from one
3088 /// of the overflow intrinsic nodes.
3089 inline bool isOverflowIntrOpRes(SDValue Op) {
3090 unsigned Opc = Op.getOpcode();
3091 return (Op.getResNo() == 1 &&
3092 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3093 Opc == ISD::USUBO || Opc == ISD::SMULO || Opc == ISD::UMULO));
3094 }
3095
3096} // end namespace ISD
3097
3098} // end namespace llvm
3099
3100#endif // LLVM_CODEGEN_SELECTIONDAGNODES_H