[Alignment] fix dubious min function alignment
[llvm-complete.git] / lib / CodeGen / SelectionDAG / LegalizeIntegerTypes.cpp
blob83d4249fcac1090033df3b60b849329cce57375b
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).
18 //===----------------------------------------------------------------------===//
20 #include "LegalizeTypes.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/KnownBits.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
27 #define DEBUG_TYPE "legalize-types"
29 //===----------------------------------------------------------------------===//
30 // Integer Result Promotion
31 //===----------------------------------------------------------------------===//
33 /// PromoteIntegerResult - This method is called when a result of a node is
34 /// found to be in need of promotion to a larger type. At this point, the node
35 /// may also have invalid operands or may have other results that need
36 /// expansion, we just know that (at least) one result needs promotion.
37 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
38 LLVM_DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG);
39 dbgs() << "\n");
40 SDValue Res = SDValue();
42 // See if the target wants to custom expand this node.
43 if (CustomLowerNode(N, N->getValueType(ResNo), true)) {
44 LLVM_DEBUG(dbgs() << "Node has been custom expanded, done\n");
45 return;
48 switch (N->getOpcode()) {
49 default:
50 #ifndef NDEBUG
51 dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
52 N->dump(&DAG); dbgs() << "\n";
53 #endif
54 llvm_unreachable("Do not know how to promote this operator!");
55 case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
56 case ISD::AssertSext: Res = PromoteIntRes_AssertSext(N); break;
57 case ISD::AssertZext: Res = PromoteIntRes_AssertZext(N); break;
58 case ISD::BITCAST: Res = PromoteIntRes_BITCAST(N); break;
59 case ISD::BITREVERSE: Res = PromoteIntRes_BITREVERSE(N); break;
60 case ISD::BSWAP: Res = PromoteIntRes_BSWAP(N); break;
61 case ISD::BUILD_PAIR: Res = PromoteIntRes_BUILD_PAIR(N); break;
62 case ISD::Constant: Res = PromoteIntRes_Constant(N); break;
63 case ISD::CTLZ_ZERO_UNDEF:
64 case ISD::CTLZ: Res = PromoteIntRes_CTLZ(N); break;
65 case ISD::CTPOP: Res = PromoteIntRes_CTPOP(N); break;
66 case ISD::CTTZ_ZERO_UNDEF:
67 case ISD::CTTZ: Res = PromoteIntRes_CTTZ(N); break;
68 case ISD::EXTRACT_VECTOR_ELT:
69 Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
70 case ISD::LOAD: Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
71 case ISD::MLOAD: Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
72 break;
73 case ISD::MGATHER: Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
74 break;
75 case ISD::SELECT: Res = PromoteIntRes_SELECT(N); break;
76 case ISD::VSELECT: Res = PromoteIntRes_VSELECT(N); break;
77 case ISD::SELECT_CC: Res = PromoteIntRes_SELECT_CC(N); break;
78 case ISD::SETCC: Res = PromoteIntRes_SETCC(N); break;
79 case ISD::SMIN:
80 case ISD::SMAX: Res = PromoteIntRes_SExtIntBinOp(N); break;
81 case ISD::UMIN:
82 case ISD::UMAX: Res = PromoteIntRes_ZExtIntBinOp(N); break;
84 case ISD::SHL: Res = PromoteIntRes_SHL(N); break;
85 case ISD::SIGN_EXTEND_INREG:
86 Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
87 case ISD::SRA: Res = PromoteIntRes_SRA(N); break;
88 case ISD::SRL: Res = PromoteIntRes_SRL(N); break;
89 case ISD::TRUNCATE: Res = PromoteIntRes_TRUNCATE(N); break;
90 case ISD::UNDEF: Res = PromoteIntRes_UNDEF(N); break;
91 case ISD::VAARG: Res = PromoteIntRes_VAARG(N); break;
93 case ISD::EXTRACT_SUBVECTOR:
94 Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
95 case ISD::VECTOR_SHUFFLE:
96 Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
97 case ISD::INSERT_VECTOR_ELT:
98 Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
99 case ISD::BUILD_VECTOR:
100 Res = PromoteIntRes_BUILD_VECTOR(N); break;
101 case ISD::SCALAR_TO_VECTOR:
102 Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
103 case ISD::CONCAT_VECTORS:
104 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
106 case ISD::ANY_EXTEND_VECTOR_INREG:
107 case ISD::SIGN_EXTEND_VECTOR_INREG:
108 case ISD::ZERO_EXTEND_VECTOR_INREG:
109 Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
111 case ISD::SIGN_EXTEND:
112 case ISD::ZERO_EXTEND:
113 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
115 case ISD::STRICT_FP_TO_SINT:
116 case ISD::STRICT_FP_TO_UINT:
117 case ISD::FP_TO_SINT:
118 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
120 case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break;
122 case ISD::FLT_ROUNDS_: Res = PromoteIntRes_FLT_ROUNDS(N); break;
124 case ISD::AND:
125 case ISD::OR:
126 case ISD::XOR:
127 case ISD::ADD:
128 case ISD::SUB:
129 case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
131 case ISD::SDIV:
132 case ISD::SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
134 case ISD::UDIV:
135 case ISD::UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
137 case ISD::SADDO:
138 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
139 case ISD::UADDO:
140 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
141 case ISD::SMULO:
142 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
144 case ISD::ADDE:
145 case ISD::SUBE:
146 case ISD::ADDCARRY:
147 case ISD::SUBCARRY: Res = PromoteIntRes_ADDSUBCARRY(N, ResNo); break;
149 case ISD::SADDSAT:
150 case ISD::UADDSAT:
151 case ISD::SSUBSAT:
152 case ISD::USUBSAT: Res = PromoteIntRes_ADDSUBSAT(N); break;
153 case ISD::SMULFIX:
154 case ISD::SMULFIXSAT:
155 case ISD::UMULFIX: Res = PromoteIntRes_MULFIX(N); break;
156 case ISD::ABS: Res = PromoteIntRes_ABS(N); break;
158 case ISD::ATOMIC_LOAD:
159 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
161 case ISD::ATOMIC_LOAD_ADD:
162 case ISD::ATOMIC_LOAD_SUB:
163 case ISD::ATOMIC_LOAD_AND:
164 case ISD::ATOMIC_LOAD_CLR:
165 case ISD::ATOMIC_LOAD_OR:
166 case ISD::ATOMIC_LOAD_XOR:
167 case ISD::ATOMIC_LOAD_NAND:
168 case ISD::ATOMIC_LOAD_MIN:
169 case ISD::ATOMIC_LOAD_MAX:
170 case ISD::ATOMIC_LOAD_UMIN:
171 case ISD::ATOMIC_LOAD_UMAX:
172 case ISD::ATOMIC_SWAP:
173 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
175 case ISD::ATOMIC_CMP_SWAP:
176 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
177 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
178 break;
180 case ISD::VECREDUCE_ADD:
181 case ISD::VECREDUCE_MUL:
182 case ISD::VECREDUCE_AND:
183 case ISD::VECREDUCE_OR:
184 case ISD::VECREDUCE_XOR:
185 case ISD::VECREDUCE_SMAX:
186 case ISD::VECREDUCE_SMIN:
187 case ISD::VECREDUCE_UMAX:
188 case ISD::VECREDUCE_UMIN:
189 Res = PromoteIntRes_VECREDUCE(N);
190 break;
193 // If the result is null then the sub-method took care of registering it.
194 if (Res.getNode())
195 SetPromotedInteger(SDValue(N, ResNo), Res);
198 SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
199 unsigned ResNo) {
200 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
201 return GetPromotedInteger(Op);
204 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
205 // Sign-extend the new bits, and continue the assertion.
206 SDValue Op = SExtPromotedInteger(N->getOperand(0));
207 return DAG.getNode(ISD::AssertSext, SDLoc(N),
208 Op.getValueType(), Op, N->getOperand(1));
211 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
212 // Zero the new bits, and continue the assertion.
213 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
214 return DAG.getNode(ISD::AssertZext, SDLoc(N),
215 Op.getValueType(), Op, N->getOperand(1));
218 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
219 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
220 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
221 N->getMemoryVT(), ResVT,
222 N->getChain(), N->getBasePtr(),
223 N->getMemOperand());
224 // Legalize the chain result - switch anything that used the old chain to
225 // use the new one.
226 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
227 return Res;
230 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
231 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
232 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
233 N->getMemoryVT(),
234 N->getChain(), N->getBasePtr(),
235 Op2, N->getMemOperand());
236 // Legalize the chain result - switch anything that used the old chain to
237 // use the new one.
238 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
239 return Res;
242 SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
243 unsigned ResNo) {
244 if (ResNo == 1) {
245 assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
246 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
247 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
249 // Only use the result of getSetCCResultType if it is legal,
250 // otherwise just use the promoted result type (NVT).
251 if (!TLI.isTypeLegal(SVT))
252 SVT = NVT;
254 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
255 SDValue Res = DAG.getAtomicCmpSwap(
256 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
257 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
258 N->getMemOperand());
259 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
260 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
261 return Res.getValue(1);
264 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
265 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
266 SDVTList VTs =
267 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
268 SDValue Res = DAG.getAtomicCmpSwap(
269 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
270 N->getBasePtr(), Op2, Op3, N->getMemOperand());
271 // Update the use to N with the newly created Res.
272 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
273 ReplaceValueWith(SDValue(N, i), Res.getValue(i));
274 return Res;
277 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
278 SDValue InOp = N->getOperand(0);
279 EVT InVT = InOp.getValueType();
280 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
281 EVT OutVT = N->getValueType(0);
282 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
283 SDLoc dl(N);
285 switch (getTypeAction(InVT)) {
286 case TargetLowering::TypeLegal:
287 break;
288 case TargetLowering::TypePromoteInteger:
289 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
290 // The input promotes to the same size. Convert the promoted value.
291 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
292 break;
293 case TargetLowering::TypeSoftenFloat:
294 // Promote the integer operand by hand.
295 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
296 case TargetLowering::TypePromoteFloat: {
297 // Convert the promoted float by hand.
298 if (!NOutVT.isVector())
299 return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, GetPromotedFloat(InOp));
300 break;
302 case TargetLowering::TypeExpandInteger:
303 case TargetLowering::TypeExpandFloat:
304 break;
305 case TargetLowering::TypeScalarizeVector:
306 // Convert the element to an integer and promote it by hand.
307 if (!NOutVT.isVector())
308 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
309 BitConvertToInteger(GetScalarizedVector(InOp)));
310 break;
311 case TargetLowering::TypeSplitVector: {
312 if (!NOutVT.isVector()) {
313 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
314 // pieces of the input into integers and reassemble in the final type.
315 SDValue Lo, Hi;
316 GetSplitVector(N->getOperand(0), Lo, Hi);
317 Lo = BitConvertToInteger(Lo);
318 Hi = BitConvertToInteger(Hi);
320 if (DAG.getDataLayout().isBigEndian())
321 std::swap(Lo, Hi);
323 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
324 EVT::getIntegerVT(*DAG.getContext(),
325 NOutVT.getSizeInBits()),
326 JoinIntegers(Lo, Hi));
327 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
329 break;
331 case TargetLowering::TypeWidenVector:
332 // The input is widened to the same size. Convert to the widened value.
333 // Make sure that the outgoing value is not a vector, because this would
334 // make us bitcast between two vectors which are legalized in different ways.
335 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector())
336 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
337 // If the output type is also a vector and widening it to the same size
338 // as the widened input type would be a legal type, we can widen the bitcast
339 // and handle the promotion after.
340 if (NOutVT.isVector()) {
341 unsigned WidenInSize = NInVT.getSizeInBits();
342 unsigned OutSize = OutVT.getSizeInBits();
343 if (WidenInSize % OutSize == 0) {
344 unsigned Scale = WidenInSize / OutSize;
345 EVT WideOutVT = EVT::getVectorVT(*DAG.getContext(),
346 OutVT.getVectorElementType(),
347 OutVT.getVectorNumElements() * Scale);
348 if (isTypeLegal(WideOutVT)) {
349 InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
350 MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
351 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
352 DAG.getConstant(0, dl, IdxTy));
353 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
359 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
360 CreateStackStoreLoad(InOp, OutVT));
363 // Helper for BSWAP/BITREVERSE promotion to ensure we can fit the shift amount
364 // in the VT returned by getShiftAmountTy and to return a safe VT if we can't.
365 static EVT getShiftAmountTyForConstant(unsigned Val, EVT VT,
366 const TargetLowering &TLI,
367 SelectionDAG &DAG) {
368 EVT ShiftVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
369 // If the value won't fit in the prefered type, just use something safe. It
370 // will be legalized when the shift is expanded.
371 if ((Log2_32(Val) + 1) > ShiftVT.getScalarSizeInBits())
372 ShiftVT = MVT::i32;
373 return ShiftVT;
376 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
377 SDValue Op = GetPromotedInteger(N->getOperand(0));
378 EVT OVT = N->getValueType(0);
379 EVT NVT = Op.getValueType();
380 SDLoc dl(N);
382 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
383 EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
384 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
385 DAG.getConstant(DiffBits, dl, ShiftVT));
388 SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
389 SDValue Op = GetPromotedInteger(N->getOperand(0));
390 EVT OVT = N->getValueType(0);
391 EVT NVT = Op.getValueType();
392 SDLoc dl(N);
394 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
395 EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
396 return DAG.getNode(ISD::SRL, dl, NVT,
397 DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
398 DAG.getConstant(DiffBits, dl, ShiftVT));
401 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
402 // The pair element type may be legal, or may not promote to the same type as
403 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
404 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
405 TLI.getTypeToTransformTo(*DAG.getContext(),
406 N->getValueType(0)), JoinIntegers(N->getOperand(0),
407 N->getOperand(1)));
410 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
411 EVT VT = N->getValueType(0);
412 // FIXME there is no actual debug info here
413 SDLoc dl(N);
414 // Zero extend things like i1, sign extend everything else. It shouldn't
415 // matter in theory which one we pick, but this tends to give better code?
416 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
417 SDValue Result = DAG.getNode(Opc, dl,
418 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
419 SDValue(N, 0));
420 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
421 return Result;
424 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
425 // Zero extend to the promoted type and do the count there.
426 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
427 SDLoc dl(N);
428 EVT OVT = N->getValueType(0);
429 EVT NVT = Op.getValueType();
430 Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
431 // Subtract off the extra leading bits in the bigger type.
432 return DAG.getNode(
433 ISD::SUB, dl, NVT, Op,
434 DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
435 NVT));
438 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
439 // Zero extend to the promoted type and do the count there.
440 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
441 return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op);
444 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
445 SDValue Op = GetPromotedInteger(N->getOperand(0));
446 EVT OVT = N->getValueType(0);
447 EVT NVT = Op.getValueType();
448 SDLoc dl(N);
449 if (N->getOpcode() == ISD::CTTZ) {
450 // The count is the same in the promoted type except if the original
451 // value was zero. This can be handled by setting the bit just off
452 // the top of the original type.
453 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
454 OVT.getScalarSizeInBits());
455 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
457 return DAG.getNode(N->getOpcode(), dl, NVT, Op);
460 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
461 SDLoc dl(N);
462 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
464 SDValue Op0 = N->getOperand(0);
465 SDValue Op1 = N->getOperand(1);
467 // If the input also needs to be promoted, do that first so we can get a
468 // get a good idea for the output type.
469 if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
470 == TargetLowering::TypePromoteInteger) {
471 SDValue In = GetPromotedInteger(Op0);
473 // If the new type is larger than NVT, use it. We probably won't need to
474 // promote it again.
475 EVT SVT = In.getValueType().getScalarType();
476 if (SVT.bitsGE(NVT)) {
477 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
478 return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
482 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
485 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
486 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
487 unsigned NewOpc = N->getOpcode();
488 SDLoc dl(N);
490 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
491 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
492 // and SINT conversions are Custom, there is no way to tell which is
493 // preferable. We choose SINT because that's the right thing on PPC.)
494 if (N->getOpcode() == ISD::FP_TO_UINT &&
495 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
496 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
497 NewOpc = ISD::FP_TO_SINT;
499 if (N->getOpcode() == ISD::STRICT_FP_TO_UINT &&
500 !TLI.isOperationLegal(ISD::STRICT_FP_TO_UINT, NVT) &&
501 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
502 NewOpc = ISD::STRICT_FP_TO_SINT;
504 SDValue Res;
505 if (N->isStrictFPOpcode()) {
506 Res = DAG.getNode(NewOpc, dl, { NVT, MVT::Other },
507 { N->getOperand(0), N->getOperand(1) });
508 // Legalize the chain result - switch anything that used the old chain to
509 // use the new one.
510 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
511 } else
512 Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
514 // Assert that the converted value fits in the original type. If it doesn't
515 // (eg: because the value being converted is too big), then the result of the
516 // original operation was undefined anyway, so the assert is still correct.
518 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
519 // before legalization: fp-to-uint16, 65534. -> 0xfffe
520 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
521 return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
522 N->getOpcode() == ISD::STRICT_FP_TO_UINT) ?
523 ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
524 DAG.getValueType(N->getValueType(0).getScalarType()));
527 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
528 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
529 SDLoc dl(N);
531 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
534 SDValue DAGTypeLegalizer::PromoteIntRes_FLT_ROUNDS(SDNode *N) {
535 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
536 SDLoc dl(N);
538 return DAG.getNode(N->getOpcode(), dl, NVT);
541 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
542 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
543 SDLoc dl(N);
545 if (getTypeAction(N->getOperand(0).getValueType())
546 == TargetLowering::TypePromoteInteger) {
547 SDValue Res = GetPromotedInteger(N->getOperand(0));
548 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
550 // If the result and operand types are the same after promotion, simplify
551 // to an in-register extension.
552 if (NVT == Res.getValueType()) {
553 // The high bits are not guaranteed to be anything. Insert an extend.
554 if (N->getOpcode() == ISD::SIGN_EXTEND)
555 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
556 DAG.getValueType(N->getOperand(0).getValueType()));
557 if (N->getOpcode() == ISD::ZERO_EXTEND)
558 return DAG.getZeroExtendInReg(Res, dl,
559 N->getOperand(0).getValueType().getScalarType());
560 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
561 return Res;
565 // Otherwise, just extend the original operand all the way to the larger type.
566 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
569 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
570 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
571 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
572 ISD::LoadExtType ExtType =
573 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
574 SDLoc dl(N);
575 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
576 N->getMemoryVT(), N->getMemOperand());
578 // Legalize the chain result - switch anything that used the old chain to
579 // use the new one.
580 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
581 return Res;
584 SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
585 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
586 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
588 SDLoc dl(N);
589 SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
590 N->getMask(), ExtPassThru, N->getMemoryVT(),
591 N->getMemOperand(), ISD::EXTLOAD);
592 // Legalize the chain result - switch anything that used the old chain to
593 // use the new one.
594 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
595 return Res;
598 SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
599 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
600 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
601 assert(NVT == ExtPassThru.getValueType() &&
602 "Gather result type and the passThru agrument type should be the same");
604 SDLoc dl(N);
605 SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
606 N->getIndex(), N->getScale() };
607 SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
608 N->getMemoryVT(), dl, Ops,
609 N->getMemOperand(), N->getIndexType());
610 // Legalize the chain result - switch anything that used the old chain to
611 // use the new one.
612 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
613 return Res;
616 /// Promote the overflow flag of an overflowing arithmetic node.
617 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
618 // Change the return type of the boolean result while obeying
619 // getSetCCResultType.
620 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
621 EVT VT = N->getValueType(0);
622 EVT SVT = getSetCCResultType(VT);
623 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
624 unsigned NumOps = N->getNumOperands();
625 assert(NumOps <= 3 && "Too many operands");
626 if (NumOps == 3)
627 Ops[2] = N->getOperand(2);
629 SDLoc dl(N);
630 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
631 makeArrayRef(Ops, NumOps));
633 // Modified the sum result - switch anything that used the old sum to use
634 // the new one.
635 ReplaceValueWith(SDValue(N, 0), Res);
637 // Convert to the expected type.
638 return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
641 SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSAT(SDNode *N) {
642 // For promoting iN -> iM, this can be expanded by
643 // 1. ANY_EXTEND iN to iM
644 // 2. SHL by M-N
645 // 3. [US][ADD|SUB]SAT
646 // 4. L/ASHR by M-N
647 SDLoc dl(N);
648 SDValue Op1 = N->getOperand(0);
649 SDValue Op2 = N->getOperand(1);
650 unsigned OldBits = Op1.getScalarValueSizeInBits();
652 unsigned Opcode = N->getOpcode();
653 unsigned ShiftOp;
654 switch (Opcode) {
655 case ISD::SADDSAT:
656 case ISD::SSUBSAT:
657 ShiftOp = ISD::SRA;
658 break;
659 case ISD::UADDSAT:
660 case ISD::USUBSAT:
661 ShiftOp = ISD::SRL;
662 break;
663 default:
664 llvm_unreachable("Expected opcode to be signed or unsigned saturation "
665 "addition or subtraction");
668 SDValue Op1Promoted = GetPromotedInteger(Op1);
669 SDValue Op2Promoted = GetPromotedInteger(Op2);
671 EVT PromotedType = Op1Promoted.getValueType();
672 unsigned NewBits = PromotedType.getScalarSizeInBits();
673 unsigned SHLAmount = NewBits - OldBits;
674 EVT SHVT = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
675 SDValue ShiftAmount = DAG.getConstant(SHLAmount, dl, SHVT);
676 Op1Promoted =
677 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted, ShiftAmount);
678 Op2Promoted =
679 DAG.getNode(ISD::SHL, dl, PromotedType, Op2Promoted, ShiftAmount);
681 SDValue Result =
682 DAG.getNode(Opcode, dl, PromotedType, Op1Promoted, Op2Promoted);
683 return DAG.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
686 SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
687 // Can just promote the operands then continue with operation.
688 SDLoc dl(N);
689 SDValue Op1Promoted, Op2Promoted;
690 bool Signed =
691 N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
692 if (Signed) {
693 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
694 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
695 } else {
696 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
697 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
699 EVT OldType = N->getOperand(0).getValueType();
700 EVT PromotedType = Op1Promoted.getValueType();
701 unsigned DiffSize =
702 PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
704 bool Saturating = N->getOpcode() == ISD::SMULFIXSAT;
705 if (Saturating) {
706 // Promoting the operand and result values changes the saturation width,
707 // which is extends the values that we clamp to on saturation. This could be
708 // resolved by shifting one of the operands the same amount, which would
709 // also shift the result we compare against, then shifting back.
710 EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
711 Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
712 DAG.getConstant(DiffSize, dl, ShiftTy));
713 SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
714 Op2Promoted, N->getOperand(2));
715 unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
716 return DAG.getNode(ShiftOp, dl, PromotedType, Result,
717 DAG.getConstant(DiffSize, dl, ShiftTy));
719 return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
720 N->getOperand(2));
723 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
724 if (ResNo == 1)
725 return PromoteIntRes_Overflow(N);
727 // The operation overflowed iff the result in the larger type is not the
728 // sign extension of its truncation to the original type.
729 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
730 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
731 EVT OVT = N->getOperand(0).getValueType();
732 EVT NVT = LHS.getValueType();
733 SDLoc dl(N);
735 // Do the arithmetic in the larger type.
736 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
737 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
739 // Calculate the overflow flag: sign extend the arithmetic result from
740 // the original type.
741 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
742 DAG.getValueType(OVT));
743 // Overflowed if and only if this is not equal to Res.
744 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
746 // Use the calculated overflow everywhere.
747 ReplaceValueWith(SDValue(N, 1), Ofl);
749 return Res;
752 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
753 SDValue LHS = GetPromotedInteger(N->getOperand(1));
754 SDValue RHS = GetPromotedInteger(N->getOperand(2));
755 return DAG.getSelect(SDLoc(N),
756 LHS.getValueType(), N->getOperand(0), LHS, RHS);
759 SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
760 SDValue Mask = N->getOperand(0);
762 SDValue LHS = GetPromotedInteger(N->getOperand(1));
763 SDValue RHS = GetPromotedInteger(N->getOperand(2));
764 return DAG.getNode(ISD::VSELECT, SDLoc(N),
765 LHS.getValueType(), Mask, LHS, RHS);
768 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
769 SDValue LHS = GetPromotedInteger(N->getOperand(2));
770 SDValue RHS = GetPromotedInteger(N->getOperand(3));
771 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
772 LHS.getValueType(), N->getOperand(0),
773 N->getOperand(1), LHS, RHS, N->getOperand(4));
776 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
777 EVT InVT = N->getOperand(0).getValueType();
778 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
780 EVT SVT = getSetCCResultType(InVT);
782 // If we got back a type that needs to be promoted, this likely means the
783 // the input type also needs to be promoted. So get the promoted type for
784 // the input and try the query again.
785 if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
786 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
787 InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
788 SVT = getSetCCResultType(InVT);
789 } else {
790 // Input type isn't promoted, just use the default promoted type.
791 SVT = NVT;
795 SDLoc dl(N);
796 assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() &&
797 "Vector compare must return a vector result!");
799 // Get the SETCC result using the canonical SETCC type.
800 SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
801 N->getOperand(1), N->getOperand(2));
803 // Convert to the expected type.
804 return DAG.getSExtOrTrunc(SetCC, dl, NVT);
807 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
808 SDValue LHS = GetPromotedInteger(N->getOperand(0));
809 SDValue RHS = N->getOperand(1);
810 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
811 RHS = ZExtPromotedInteger(RHS);
812 return DAG.getNode(ISD::SHL, SDLoc(N), LHS.getValueType(), LHS, RHS);
815 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
816 SDValue Op = GetPromotedInteger(N->getOperand(0));
817 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
818 Op.getValueType(), Op, N->getOperand(1));
821 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
822 // The input may have strange things in the top bits of the registers, but
823 // these operations don't care. They may have weird bits going out, but
824 // that too is okay if they are integer operations.
825 SDValue LHS = GetPromotedInteger(N->getOperand(0));
826 SDValue RHS = GetPromotedInteger(N->getOperand(1));
827 return DAG.getNode(N->getOpcode(), SDLoc(N),
828 LHS.getValueType(), LHS, RHS);
831 SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
832 // Sign extend the input.
833 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
834 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
835 return DAG.getNode(N->getOpcode(), SDLoc(N),
836 LHS.getValueType(), LHS, RHS);
839 SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
840 // Zero extend the input.
841 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
842 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
843 return DAG.getNode(N->getOpcode(), SDLoc(N),
844 LHS.getValueType(), LHS, RHS);
847 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
848 // The input value must be properly sign extended.
849 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
850 SDValue RHS = N->getOperand(1);
851 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
852 RHS = ZExtPromotedInteger(RHS);
853 return DAG.getNode(ISD::SRA, SDLoc(N), LHS.getValueType(), LHS, RHS);
856 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
857 // The input value must be properly zero extended.
858 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
859 SDValue RHS = N->getOperand(1);
860 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
861 RHS = ZExtPromotedInteger(RHS);
862 return DAG.getNode(ISD::SRL, SDLoc(N), LHS.getValueType(), LHS, RHS);
865 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
866 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
867 SDValue Res;
868 SDValue InOp = N->getOperand(0);
869 SDLoc dl(N);
871 switch (getTypeAction(InOp.getValueType())) {
872 default: llvm_unreachable("Unknown type action!");
873 case TargetLowering::TypeLegal:
874 case TargetLowering::TypeExpandInteger:
875 Res = InOp;
876 break;
877 case TargetLowering::TypePromoteInteger:
878 Res = GetPromotedInteger(InOp);
879 break;
880 case TargetLowering::TypeSplitVector: {
881 EVT InVT = InOp.getValueType();
882 assert(InVT.isVector() && "Cannot split scalar types");
883 unsigned NumElts = InVT.getVectorNumElements();
884 assert(NumElts == NVT.getVectorNumElements() &&
885 "Dst and Src must have the same number of elements");
886 assert(isPowerOf2_32(NumElts) &&
887 "Promoted vector type must be a power of two");
889 SDValue EOp1, EOp2;
890 GetSplitVector(InOp, EOp1, EOp2);
892 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
893 NumElts/2);
894 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
895 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
897 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
899 case TargetLowering::TypeWidenVector: {
900 SDValue WideInOp = GetWidenedVector(InOp);
902 // Truncate widened InOp.
903 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
904 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
905 N->getValueType(0).getScalarType(), NumElem);
906 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
908 // Zero extend so that the elements are of same type as those of NVT
909 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
910 NumElem);
911 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
913 // Extract the low NVT subvector.
914 MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
915 SDValue ZeroIdx = DAG.getConstant(0, dl, IdxTy);
916 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
920 // Truncate to NVT instead of VT
921 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
924 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
925 if (ResNo == 1)
926 return PromoteIntRes_Overflow(N);
928 // The operation overflowed iff the result in the larger type is not the
929 // zero extension of its truncation to the original type.
930 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
931 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
932 EVT OVT = N->getOperand(0).getValueType();
933 EVT NVT = LHS.getValueType();
934 SDLoc dl(N);
936 // Do the arithmetic in the larger type.
937 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
938 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
940 // Calculate the overflow flag: zero extend the arithmetic result from
941 // the original type.
942 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT.getScalarType());
943 // Overflowed if and only if this is not equal to Res.
944 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
946 // Use the calculated overflow everywhere.
947 ReplaceValueWith(SDValue(N, 1), Ofl);
949 return Res;
952 // Handle promotion for the ADDE/SUBE/ADDCARRY/SUBCARRY nodes. Notice that
953 // the third operand of ADDE/SUBE nodes is carry flag, which differs from
954 // the ADDCARRY/SUBCARRY nodes in that the third operand is carry Boolean.
955 SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBCARRY(SDNode *N, unsigned ResNo) {
956 if (ResNo == 1)
957 return PromoteIntRes_Overflow(N);
959 // We need to sign-extend the operands so the carry value computed by the
960 // wide operation will be equivalent to the carry value computed by the
961 // narrow operation.
962 // An ADDCARRY can generate carry only if any of the operands has its
963 // most significant bit set. Sign extension propagates the most significant
964 // bit into the higher bits which means the extra bit that the narrow
965 // addition would need (i.e. the carry) will be propagated through the higher
966 // bits of the wide addition.
967 // A SUBCARRY can generate borrow only if LHS < RHS and this property will be
968 // preserved by sign extension.
969 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
970 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
972 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
974 // Do the arithmetic in the wide type.
975 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
976 LHS, RHS, N->getOperand(2));
978 // Update the users of the original carry/borrow value.
979 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
981 return SDValue(Res.getNode(), 0);
984 SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
985 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
986 return DAG.getNode(ISD::ABS, SDLoc(N), Op0.getValueType(), Op0);
989 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
990 // Promote the overflow bit trivially.
991 if (ResNo == 1)
992 return PromoteIntRes_Overflow(N);
994 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
995 SDLoc DL(N);
996 EVT SmallVT = LHS.getValueType();
998 // To determine if the result overflowed in a larger type, we extend the
999 // input to the larger type, do the multiply (checking if it overflows),
1000 // then also check the high bits of the result to see if overflow happened
1001 // there.
1002 if (N->getOpcode() == ISD::SMULO) {
1003 LHS = SExtPromotedInteger(LHS);
1004 RHS = SExtPromotedInteger(RHS);
1005 } else {
1006 LHS = ZExtPromotedInteger(LHS);
1007 RHS = ZExtPromotedInteger(RHS);
1009 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
1010 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
1012 // Overflow occurred if it occurred in the larger type, or if the high part
1013 // of the result does not zero/sign-extend the low part. Check this second
1014 // possibility first.
1015 SDValue Overflow;
1016 if (N->getOpcode() == ISD::UMULO) {
1017 // Unsigned overflow occurred if the high part is non-zero.
1018 unsigned Shift = SmallVT.getScalarSizeInBits();
1019 EVT ShiftTy = getShiftAmountTyForConstant(Shift, Mul.getValueType(),
1020 TLI, DAG);
1021 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
1022 DAG.getConstant(Shift, DL, ShiftTy));
1023 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
1024 DAG.getConstant(0, DL, Hi.getValueType()),
1025 ISD::SETNE);
1026 } else {
1027 // Signed overflow occurred if the high part does not sign extend the low.
1028 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
1029 Mul, DAG.getValueType(SmallVT));
1030 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
1033 // The only other way for overflow to occur is if the multiplication in the
1034 // larger type itself overflowed.
1035 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
1036 SDValue(Mul.getNode(), 1));
1038 // Use the calculated overflow everywhere.
1039 ReplaceValueWith(SDValue(N, 1), Overflow);
1040 return Mul;
1043 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
1044 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1045 N->getValueType(0)));
1048 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
1049 SDValue Chain = N->getOperand(0); // Get the chain.
1050 SDValue Ptr = N->getOperand(1); // Get the pointer.
1051 EVT VT = N->getValueType(0);
1052 SDLoc dl(N);
1054 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
1055 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
1056 // The argument is passed as NumRegs registers of type RegVT.
1058 SmallVector<SDValue, 8> Parts(NumRegs);
1059 for (unsigned i = 0; i < NumRegs; ++i) {
1060 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
1061 N->getConstantOperandVal(3));
1062 Chain = Parts[i].getValue(1);
1065 // Handle endianness of the load.
1066 if (DAG.getDataLayout().isBigEndian())
1067 std::reverse(Parts.begin(), Parts.end());
1069 // Assemble the parts in the promoted type.
1070 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1071 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
1072 for (unsigned i = 1; i < NumRegs; ++i) {
1073 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
1074 // Shift it to the right position and "or" it in.
1075 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
1076 DAG.getConstant(i * RegVT.getSizeInBits(), dl,
1077 TLI.getPointerTy(DAG.getDataLayout())));
1078 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
1081 // Modified the chain result - switch anything that used the old chain to
1082 // use the new one.
1083 ReplaceValueWith(SDValue(N, 1), Chain);
1085 return Res;
1088 //===----------------------------------------------------------------------===//
1089 // Integer Operand Promotion
1090 //===----------------------------------------------------------------------===//
1092 /// PromoteIntegerOperand - This method is called when the specified operand of
1093 /// the specified node is found to need promotion. At this point, all of the
1094 /// result types of the node are known to be legal, but other operands of the
1095 /// node may need promotion or expansion as well as the specified one.
1096 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
1097 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG);
1098 dbgs() << "\n");
1099 SDValue Res = SDValue();
1101 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
1102 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
1103 return false;
1106 switch (N->getOpcode()) {
1107 default:
1108 #ifndef NDEBUG
1109 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
1110 N->dump(&DAG); dbgs() << "\n";
1111 #endif
1112 llvm_unreachable("Do not know how to promote this operator's operand!");
1114 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
1115 case ISD::ATOMIC_STORE:
1116 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
1117 break;
1118 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
1119 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
1120 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
1121 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
1122 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
1123 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
1124 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
1125 case ISD::INSERT_VECTOR_ELT:
1126 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
1127 case ISD::SCALAR_TO_VECTOR:
1128 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
1129 case ISD::VSELECT:
1130 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
1131 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
1132 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
1133 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
1134 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
1135 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
1136 OpNo); break;
1137 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
1138 OpNo); break;
1139 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
1140 OpNo); break;
1141 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
1142 OpNo); break;
1143 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
1144 OpNo); break;
1145 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
1146 case ISD::FP16_TO_FP:
1147 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
1148 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
1149 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
1151 case ISD::SHL:
1152 case ISD::SRA:
1153 case ISD::SRL:
1154 case ISD::ROTL:
1155 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
1157 case ISD::ADDCARRY:
1158 case ISD::SUBCARRY: Res = PromoteIntOp_ADDSUBCARRY(N, OpNo); break;
1160 case ISD::FRAMEADDR:
1161 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
1163 case ISD::PREFETCH: Res = PromoteIntOp_PREFETCH(N, OpNo); break;
1165 case ISD::SMULFIX:
1166 case ISD::SMULFIXSAT:
1167 case ISD::UMULFIX: Res = PromoteIntOp_MULFIX(N); break;
1169 case ISD::FPOWI: Res = PromoteIntOp_FPOWI(N); break;
1171 case ISD::VECREDUCE_ADD:
1172 case ISD::VECREDUCE_MUL:
1173 case ISD::VECREDUCE_AND:
1174 case ISD::VECREDUCE_OR:
1175 case ISD::VECREDUCE_XOR:
1176 case ISD::VECREDUCE_SMAX:
1177 case ISD::VECREDUCE_SMIN:
1178 case ISD::VECREDUCE_UMAX:
1179 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
1182 // If the result is null, the sub-method took care of registering results etc.
1183 if (!Res.getNode()) return false;
1185 // If the result is N, the sub-method updated N in place. Tell the legalizer
1186 // core about this.
1187 if (Res.getNode() == N)
1188 return true;
1190 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1191 "Invalid operand expansion");
1193 ReplaceValueWith(SDValue(N, 0), Res);
1194 return false;
1197 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1198 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1199 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
1200 ISD::CondCode CCCode) {
1201 // We have to insert explicit sign or zero extends. Note that we could
1202 // insert sign extends for ALL conditions. For those operations where either
1203 // zero or sign extension would be valid, use SExtOrZExtPromotedInteger
1204 // which will choose the cheapest for the target.
1205 switch (CCCode) {
1206 default: llvm_unreachable("Unknown integer comparison!");
1207 case ISD::SETEQ:
1208 case ISD::SETNE: {
1209 SDValue OpL = GetPromotedInteger(NewLHS);
1210 SDValue OpR = GetPromotedInteger(NewRHS);
1212 // We would prefer to promote the comparison operand with sign extension.
1213 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
1214 // than the width of NewLHS/NewRH, we can avoid inserting real truncate
1215 // instruction, which is redundant eventually.
1216 unsigned OpLEffectiveBits =
1217 OpL.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpL) + 1;
1218 unsigned OpREffectiveBits =
1219 OpR.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpR) + 1;
1220 if (OpLEffectiveBits <= NewLHS.getScalarValueSizeInBits() &&
1221 OpREffectiveBits <= NewRHS.getScalarValueSizeInBits()) {
1222 NewLHS = OpL;
1223 NewRHS = OpR;
1224 } else {
1225 NewLHS = SExtOrZExtPromotedInteger(NewLHS);
1226 NewRHS = SExtOrZExtPromotedInteger(NewRHS);
1228 break;
1230 case ISD::SETUGE:
1231 case ISD::SETUGT:
1232 case ISD::SETULE:
1233 case ISD::SETULT:
1234 NewLHS = SExtOrZExtPromotedInteger(NewLHS);
1235 NewRHS = SExtOrZExtPromotedInteger(NewRHS);
1236 break;
1237 case ISD::SETGE:
1238 case ISD::SETGT:
1239 case ISD::SETLT:
1240 case ISD::SETLE:
1241 NewLHS = SExtPromotedInteger(NewLHS);
1242 NewRHS = SExtPromotedInteger(NewRHS);
1243 break;
1247 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
1248 SDValue Op = GetPromotedInteger(N->getOperand(0));
1249 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
1252 SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
1253 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
1254 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
1255 N->getChain(), N->getBasePtr(), Op2, N->getMemOperand());
1258 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
1259 // This should only occur in unusual situations like bitcasting to an
1260 // x86_fp80, so just turn it into a store+load
1261 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1264 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
1265 assert(OpNo == 2 && "Don't know how to promote this operand!");
1267 SDValue LHS = N->getOperand(2);
1268 SDValue RHS = N->getOperand(3);
1269 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1271 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1272 // legal types.
1273 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1274 N->getOperand(1), LHS, RHS, N->getOperand(4)),
1278 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
1279 assert(OpNo == 1 && "only know how to promote condition");
1281 // Promote all the way up to the canonical SetCC type.
1282 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
1284 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1285 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
1286 N->getOperand(2)), 0);
1289 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
1290 // Since the result type is legal, the operands must promote to it.
1291 EVT OVT = N->getOperand(0).getValueType();
1292 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
1293 SDValue Hi = GetPromotedInteger(N->getOperand(1));
1294 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
1295 SDLoc dl(N);
1297 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
1298 DAG.getConstant(OVT.getSizeInBits(), dl,
1299 TLI.getPointerTy(DAG.getDataLayout())));
1300 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
1303 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
1304 // The vector type is legal but the element type is not. This implies
1305 // that the vector is a power-of-two in length and that the element
1306 // type does not have a strange size (eg: it is not i1).
1307 EVT VecVT = N->getValueType(0);
1308 unsigned NumElts = VecVT.getVectorNumElements();
1309 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
1310 "Legal vector of one illegal element?");
1312 // Promote the inserted value. The type does not need to match the
1313 // vector element type. Check that any extra bits introduced will be
1314 // truncated away.
1315 assert(N->getOperand(0).getValueSizeInBits() >=
1316 N->getValueType(0).getScalarSizeInBits() &&
1317 "Type of inserted value narrower than vector element type!");
1319 SmallVector<SDValue, 16> NewOps;
1320 for (unsigned i = 0; i < NumElts; ++i)
1321 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
1323 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1326 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
1327 unsigned OpNo) {
1328 if (OpNo == 1) {
1329 // Promote the inserted value. This is valid because the type does not
1330 // have to match the vector element type.
1332 // Check that any extra bits introduced will be truncated away.
1333 assert(N->getOperand(1).getValueSizeInBits() >=
1334 N->getValueType(0).getScalarSizeInBits() &&
1335 "Type of inserted value narrower than vector element type!");
1336 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1337 GetPromotedInteger(N->getOperand(1)),
1338 N->getOperand(2)),
1342 assert(OpNo == 2 && "Different operand and result vector types?");
1344 // Promote the index.
1345 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
1346 TLI.getVectorIdxTy(DAG.getDataLayout()));
1347 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1348 N->getOperand(1), Idx), 0);
1351 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
1352 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
1353 // the operand in place.
1354 return SDValue(DAG.UpdateNodeOperands(N,
1355 GetPromotedInteger(N->getOperand(0))), 0);
1358 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
1359 assert(OpNo == 0 && "Only know how to promote the condition!");
1360 SDValue Cond = N->getOperand(0);
1361 EVT OpTy = N->getOperand(1).getValueType();
1363 if (N->getOpcode() == ISD::VSELECT)
1364 if (SDValue Res = WidenVSELECTAndMask(N))
1365 return Res;
1367 // Promote all the way up to the canonical SetCC type.
1368 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
1369 Cond = PromoteTargetBoolean(Cond, OpVT);
1371 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
1372 N->getOperand(2)), 0);
1375 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1376 assert(OpNo == 0 && "Don't know how to promote this operand!");
1378 SDValue LHS = N->getOperand(0);
1379 SDValue RHS = N->getOperand(1);
1380 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
1382 // The CC (#4) and the possible return values (#2 and #3) have legal types.
1383 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
1384 N->getOperand(3), N->getOperand(4)), 0);
1387 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
1388 assert(OpNo == 0 && "Don't know how to promote this operand!");
1390 SDValue LHS = N->getOperand(0);
1391 SDValue RHS = N->getOperand(1);
1392 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
1394 // The CC (#2) is always legal.
1395 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
1398 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
1399 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1400 ZExtPromotedInteger(N->getOperand(1))), 0);
1403 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
1404 SDValue Op = GetPromotedInteger(N->getOperand(0));
1405 SDLoc dl(N);
1406 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1407 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
1408 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1411 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
1412 return SDValue(DAG.UpdateNodeOperands(N,
1413 SExtPromotedInteger(N->getOperand(0))), 0);
1416 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
1417 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1418 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
1419 SDLoc dl(N);
1421 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
1423 // Truncate the value and store the result.
1424 return DAG.getTruncStore(Ch, dl, Val, Ptr,
1425 N->getMemoryVT(), N->getMemOperand());
1428 SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
1429 unsigned OpNo) {
1431 SDValue DataOp = N->getValue();
1432 EVT DataVT = DataOp.getValueType();
1433 SDValue Mask = N->getMask();
1434 SDLoc dl(N);
1436 bool TruncateStore = false;
1437 if (OpNo == 3) {
1438 Mask = PromoteTargetBoolean(Mask, DataVT);
1439 // Update in place.
1440 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1441 NewOps[3] = Mask;
1442 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1443 } else { // Data operand
1444 assert(OpNo == 1 && "Unexpected operand for promotion");
1445 DataOp = GetPromotedInteger(DataOp);
1446 TruncateStore = true;
1449 return DAG.getMaskedStore(N->getChain(), dl, DataOp, N->getBasePtr(), Mask,
1450 N->getMemoryVT(), N->getMemOperand(),
1451 TruncateStore, N->isCompressingStore());
1454 SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
1455 unsigned OpNo) {
1456 assert(OpNo == 2 && "Only know how to promote the mask!");
1457 EVT DataVT = N->getValueType(0);
1458 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1459 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1460 NewOps[OpNo] = Mask;
1461 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1464 SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
1465 unsigned OpNo) {
1467 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1468 if (OpNo == 2) {
1469 // The Mask
1470 EVT DataVT = N->getValueType(0);
1471 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1472 } else if (OpNo == 4) {
1473 // The Index
1474 if (N->isIndexSigned())
1475 // Need to sign extend the index since the bits will likely be used.
1476 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
1477 else
1478 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
1479 } else
1480 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
1482 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1485 SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
1486 unsigned OpNo) {
1487 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1488 if (OpNo == 2) {
1489 // The Mask
1490 EVT DataVT = N->getValue().getValueType();
1491 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1492 } else if (OpNo == 4) {
1493 // The Index
1494 if (N->isIndexSigned())
1495 // Need to sign extend the index since the bits will likely be used.
1496 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
1497 else
1498 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
1499 } else
1500 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
1501 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1504 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
1505 SDValue Op = GetPromotedInteger(N->getOperand(0));
1506 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
1509 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
1510 return SDValue(DAG.UpdateNodeOperands(N,
1511 ZExtPromotedInteger(N->getOperand(0))), 0);
1514 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
1515 SDLoc dl(N);
1516 SDValue Op = GetPromotedInteger(N->getOperand(0));
1517 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1518 return DAG.getZeroExtendInReg(Op, dl,
1519 N->getOperand(0).getValueType().getScalarType());
1522 SDValue DAGTypeLegalizer::PromoteIntOp_ADDSUBCARRY(SDNode *N, unsigned OpNo) {
1523 assert(OpNo == 2 && "Don't know how to promote this operand!");
1525 SDValue LHS = N->getOperand(0);
1526 SDValue RHS = N->getOperand(1);
1527 SDValue Carry = N->getOperand(2);
1528 SDLoc DL(N);
1530 Carry = PromoteTargetBoolean(Carry, LHS.getValueType());
1532 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, Carry), 0);
1535 SDValue DAGTypeLegalizer::PromoteIntOp_MULFIX(SDNode *N) {
1536 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
1537 return SDValue(
1538 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
1541 SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
1542 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
1543 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
1544 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
1547 SDValue DAGTypeLegalizer::PromoteIntOp_PREFETCH(SDNode *N, unsigned OpNo) {
1548 assert(OpNo > 1 && "Don't know how to promote this operand!");
1549 // Promote the rw, locality, and cache type arguments to a supported integer
1550 // width.
1551 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
1552 SDValue Op3 = ZExtPromotedInteger(N->getOperand(3));
1553 SDValue Op4 = ZExtPromotedInteger(N->getOperand(4));
1554 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
1555 Op2, Op3, Op4),
1559 SDValue DAGTypeLegalizer::PromoteIntOp_FPOWI(SDNode *N) {
1560 SDValue Op = SExtPromotedInteger(N->getOperand(1));
1561 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
1564 SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
1565 SDLoc dl(N);
1566 SDValue Op;
1567 switch (N->getOpcode()) {
1568 default: llvm_unreachable("Expected integer vector reduction");
1569 case ISD::VECREDUCE_ADD:
1570 case ISD::VECREDUCE_MUL:
1571 case ISD::VECREDUCE_AND:
1572 case ISD::VECREDUCE_OR:
1573 case ISD::VECREDUCE_XOR:
1574 Op = GetPromotedInteger(N->getOperand(0));
1575 break;
1576 case ISD::VECREDUCE_SMAX:
1577 case ISD::VECREDUCE_SMIN:
1578 Op = SExtPromotedInteger(N->getOperand(0));
1579 break;
1580 case ISD::VECREDUCE_UMAX:
1581 case ISD::VECREDUCE_UMIN:
1582 Op = ZExtPromotedInteger(N->getOperand(0));
1583 break;
1586 EVT EltVT = Op.getValueType().getVectorElementType();
1587 EVT VT = N->getValueType(0);
1588 if (VT.bitsGE(EltVT))
1589 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, Op);
1591 // Result size must be >= element size. If this is not the case after
1592 // promotion, also promote the result type and then truncate.
1593 SDValue Reduce = DAG.getNode(N->getOpcode(), dl, EltVT, Op);
1594 return DAG.getNode(ISD::TRUNCATE, dl, VT, Reduce);
1597 //===----------------------------------------------------------------------===//
1598 // Integer Result Expansion
1599 //===----------------------------------------------------------------------===//
1601 /// ExpandIntegerResult - This method is called when the specified result of the
1602 /// specified node is found to need expansion. At this point, the node may also
1603 /// have invalid operands or may have other results that need promotion, we just
1604 /// know that (at least) one result needs expansion.
1605 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
1606 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG);
1607 dbgs() << "\n");
1608 SDValue Lo, Hi;
1609 Lo = Hi = SDValue();
1611 // See if the target wants to custom expand this node.
1612 if (CustomLowerNode(N, N->getValueType(ResNo), true))
1613 return;
1615 switch (N->getOpcode()) {
1616 default:
1617 #ifndef NDEBUG
1618 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
1619 N->dump(&DAG); dbgs() << "\n";
1620 #endif
1621 report_fatal_error("Do not know how to expand the result of this "
1622 "operator!");
1624 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
1625 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
1626 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1627 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1629 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
1630 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1631 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1632 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1633 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
1635 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
1636 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
1637 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
1638 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
1639 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
1640 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
1641 case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
1642 case ISD::CTLZ_ZERO_UNDEF:
1643 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
1644 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
1645 case ISD::CTTZ_ZERO_UNDEF:
1646 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
1647 case ISD::FLT_ROUNDS_: ExpandIntRes_FLT_ROUNDS(N, Lo, Hi); break;
1648 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
1649 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
1650 case ISD::LLROUND: ExpandIntRes_LLROUND(N, Lo, Hi); break;
1651 case ISD::LLRINT: ExpandIntRes_LLRINT(N, Lo, Hi); break;
1652 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
1653 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
1654 case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
1655 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
1656 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1657 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1658 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
1659 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1660 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
1661 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
1662 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1663 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
1665 case ISD::ATOMIC_LOAD_ADD:
1666 case ISD::ATOMIC_LOAD_SUB:
1667 case ISD::ATOMIC_LOAD_AND:
1668 case ISD::ATOMIC_LOAD_CLR:
1669 case ISD::ATOMIC_LOAD_OR:
1670 case ISD::ATOMIC_LOAD_XOR:
1671 case ISD::ATOMIC_LOAD_NAND:
1672 case ISD::ATOMIC_LOAD_MIN:
1673 case ISD::ATOMIC_LOAD_MAX:
1674 case ISD::ATOMIC_LOAD_UMIN:
1675 case ISD::ATOMIC_LOAD_UMAX:
1676 case ISD::ATOMIC_SWAP:
1677 case ISD::ATOMIC_CMP_SWAP: {
1678 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
1679 SplitInteger(Tmp.first, Lo, Hi);
1680 ReplaceValueWith(SDValue(N, 1), Tmp.second);
1681 break;
1683 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
1684 AtomicSDNode *AN = cast<AtomicSDNode>(N);
1685 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
1686 SDValue Tmp = DAG.getAtomicCmpSwap(
1687 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
1688 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
1689 AN->getMemOperand());
1691 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
1692 // success simply by comparing the loaded value against the ingoing
1693 // comparison.
1694 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
1695 N->getOperand(2), ISD::SETEQ);
1697 SplitInteger(Tmp, Lo, Hi);
1698 ReplaceValueWith(SDValue(N, 1), Success);
1699 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
1700 break;
1703 case ISD::AND:
1704 case ISD::OR:
1705 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1707 case ISD::UMAX:
1708 case ISD::SMAX:
1709 case ISD::UMIN:
1710 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
1712 case ISD::ADD:
1713 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1715 case ISD::ADDC:
1716 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1718 case ISD::ADDE:
1719 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1721 case ISD::ADDCARRY:
1722 case ISD::SUBCARRY: ExpandIntRes_ADDSUBCARRY(N, Lo, Hi); break;
1724 case ISD::SHL:
1725 case ISD::SRA:
1726 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1728 case ISD::SADDO:
1729 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
1730 case ISD::UADDO:
1731 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
1732 case ISD::UMULO:
1733 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
1735 case ISD::SADDSAT:
1736 case ISD::UADDSAT:
1737 case ISD::SSUBSAT:
1738 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
1740 case ISD::SMULFIX:
1741 case ISD::SMULFIXSAT:
1742 case ISD::UMULFIX: ExpandIntRes_MULFIX(N, Lo, Hi); break;
1744 case ISD::VECREDUCE_ADD:
1745 case ISD::VECREDUCE_MUL:
1746 case ISD::VECREDUCE_AND:
1747 case ISD::VECREDUCE_OR:
1748 case ISD::VECREDUCE_XOR:
1749 case ISD::VECREDUCE_SMAX:
1750 case ISD::VECREDUCE_SMIN:
1751 case ISD::VECREDUCE_UMAX:
1752 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
1755 // If Lo/Hi is null, the sub-method took care of registering results etc.
1756 if (Lo.getNode())
1757 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1760 /// Lower an atomic node to the appropriate builtin call.
1761 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
1762 unsigned Opc = Node->getOpcode();
1763 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
1764 RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT);
1765 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
1767 return ExpandChainLibCall(LC, Node, false);
1770 /// N is a shift by a value that needs to be expanded,
1771 /// and the shift amount is a constant 'Amt'. Expand the operation.
1772 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
1773 SDValue &Lo, SDValue &Hi) {
1774 SDLoc DL(N);
1775 // Expand the incoming operand to be shifted, so that we have its parts
1776 SDValue InL, InH;
1777 GetExpandedInteger(N->getOperand(0), InL, InH);
1779 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
1780 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
1781 if (!Amt) {
1782 Lo = InL;
1783 Hi = InH;
1784 return;
1787 EVT NVT = InL.getValueType();
1788 unsigned VTBits = N->getValueType(0).getSizeInBits();
1789 unsigned NVTBits = NVT.getSizeInBits();
1790 EVT ShTy = N->getOperand(1).getValueType();
1792 if (N->getOpcode() == ISD::SHL) {
1793 if (Amt.ugt(VTBits)) {
1794 Lo = Hi = DAG.getConstant(0, DL, NVT);
1795 } else if (Amt.ugt(NVTBits)) {
1796 Lo = DAG.getConstant(0, DL, NVT);
1797 Hi = DAG.getNode(ISD::SHL, DL,
1798 NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
1799 } else if (Amt == NVTBits) {
1800 Lo = DAG.getConstant(0, DL, NVT);
1801 Hi = InL;
1802 } else {
1803 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
1804 Hi = DAG.getNode(ISD::OR, DL, NVT,
1805 DAG.getNode(ISD::SHL, DL, NVT, InH,
1806 DAG.getConstant(Amt, DL, ShTy)),
1807 DAG.getNode(ISD::SRL, DL, NVT, InL,
1808 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
1810 return;
1813 if (N->getOpcode() == ISD::SRL) {
1814 if (Amt.ugt(VTBits)) {
1815 Lo = Hi = DAG.getConstant(0, DL, NVT);
1816 } else if (Amt.ugt(NVTBits)) {
1817 Lo = DAG.getNode(ISD::SRL, DL,
1818 NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
1819 Hi = DAG.getConstant(0, DL, NVT);
1820 } else if (Amt == NVTBits) {
1821 Lo = InH;
1822 Hi = DAG.getConstant(0, DL, NVT);
1823 } else {
1824 Lo = DAG.getNode(ISD::OR, DL, NVT,
1825 DAG.getNode(ISD::SRL, DL, NVT, InL,
1826 DAG.getConstant(Amt, DL, ShTy)),
1827 DAG.getNode(ISD::SHL, DL, NVT, InH,
1828 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
1829 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
1831 return;
1834 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1835 if (Amt.ugt(VTBits)) {
1836 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1837 DAG.getConstant(NVTBits - 1, DL, ShTy));
1838 } else if (Amt.ugt(NVTBits)) {
1839 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1840 DAG.getConstant(Amt - NVTBits, DL, ShTy));
1841 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1842 DAG.getConstant(NVTBits - 1, DL, ShTy));
1843 } else if (Amt == NVTBits) {
1844 Lo = InH;
1845 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1846 DAG.getConstant(NVTBits - 1, DL, ShTy));
1847 } else {
1848 Lo = DAG.getNode(ISD::OR, DL, NVT,
1849 DAG.getNode(ISD::SRL, DL, NVT, InL,
1850 DAG.getConstant(Amt, DL, ShTy)),
1851 DAG.getNode(ISD::SHL, DL, NVT, InH,
1852 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
1853 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
1857 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1858 /// this shift based on knowledge of the high bit of the shift amount. If we
1859 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1860 /// shift amount.
1861 bool DAGTypeLegalizer::
1862 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1863 SDValue Amt = N->getOperand(1);
1864 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1865 EVT ShTy = Amt.getValueType();
1866 unsigned ShBits = ShTy.getScalarSizeInBits();
1867 unsigned NVTBits = NVT.getScalarSizeInBits();
1868 assert(isPowerOf2_32(NVTBits) &&
1869 "Expanded integer type size not a power of two!");
1870 SDLoc dl(N);
1872 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1873 KnownBits Known = DAG.computeKnownBits(N->getOperand(1));
1875 // If we don't know anything about the high bits, exit.
1876 if (((Known.Zero|Known.One) & HighBitMask) == 0)
1877 return false;
1879 // Get the incoming operand to be shifted.
1880 SDValue InL, InH;
1881 GetExpandedInteger(N->getOperand(0), InL, InH);
1883 // If we know that any of the high bits of the shift amount are one, then we
1884 // can do this as a couple of simple shifts.
1885 if (Known.One.intersects(HighBitMask)) {
1886 // Mask out the high bit, which we know is set.
1887 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
1888 DAG.getConstant(~HighBitMask, dl, ShTy));
1890 switch (N->getOpcode()) {
1891 default: llvm_unreachable("Unknown shift");
1892 case ISD::SHL:
1893 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
1894 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
1895 return true;
1896 case ISD::SRL:
1897 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
1898 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
1899 return true;
1900 case ISD::SRA:
1901 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
1902 DAG.getConstant(NVTBits - 1, dl, ShTy));
1903 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
1904 return true;
1908 // If we know that all of the high bits of the shift amount are zero, then we
1909 // can do this as a couple of simple shifts.
1910 if (HighBitMask.isSubsetOf(Known.Zero)) {
1911 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
1912 // shift if x is zero. We can use XOR here because x is known to be smaller
1913 // than 32.
1914 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
1915 DAG.getConstant(NVTBits - 1, dl, ShTy));
1917 unsigned Op1, Op2;
1918 switch (N->getOpcode()) {
1919 default: llvm_unreachable("Unknown shift");
1920 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1921 case ISD::SRL:
1922 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1925 // When shifting right the arithmetic for Lo and Hi is swapped.
1926 if (N->getOpcode() != ISD::SHL)
1927 std::swap(InL, InH);
1929 // Use a little trick to get the bits that move from Lo to Hi. First
1930 // shift by one bit.
1931 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
1932 // Then compute the remaining shift with amount-1.
1933 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
1935 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
1936 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
1938 if (N->getOpcode() != ISD::SHL)
1939 std::swap(Hi, Lo);
1940 return true;
1943 return false;
1946 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
1947 /// of any size.
1948 bool DAGTypeLegalizer::
1949 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1950 SDValue Amt = N->getOperand(1);
1951 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1952 EVT ShTy = Amt.getValueType();
1953 unsigned NVTBits = NVT.getSizeInBits();
1954 assert(isPowerOf2_32(NVTBits) &&
1955 "Expanded integer type size not a power of two!");
1956 SDLoc dl(N);
1958 // Get the incoming operand to be shifted.
1959 SDValue InL, InH;
1960 GetExpandedInteger(N->getOperand(0), InL, InH);
1962 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
1963 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
1964 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
1965 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
1966 Amt, NVBitsNode, ISD::SETULT);
1967 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
1968 Amt, DAG.getConstant(0, dl, ShTy),
1969 ISD::SETEQ);
1971 SDValue LoS, HiS, LoL, HiL;
1972 switch (N->getOpcode()) {
1973 default: llvm_unreachable("Unknown shift");
1974 case ISD::SHL:
1975 // Short: ShAmt < NVTBits
1976 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
1977 HiS = DAG.getNode(ISD::OR, dl, NVT,
1978 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
1979 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
1981 // Long: ShAmt >= NVTBits
1982 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
1983 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
1985 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
1986 Hi = DAG.getSelect(dl, NVT, isZero, InH,
1987 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
1988 return true;
1989 case ISD::SRL:
1990 // Short: ShAmt < NVTBits
1991 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
1992 LoS = DAG.getNode(ISD::OR, dl, NVT,
1993 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
1994 // FIXME: If Amt is zero, the following shift generates an undefined result
1995 // on some architectures.
1996 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
1998 // Long: ShAmt >= NVTBits
1999 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2000 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2002 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2003 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2004 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2005 return true;
2006 case ISD::SRA:
2007 // Short: ShAmt < NVTBits
2008 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
2009 LoS = DAG.getNode(ISD::OR, dl, NVT,
2010 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2011 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2013 // Long: ShAmt >= NVTBits
2014 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
2015 DAG.getConstant(NVTBits - 1, dl, ShTy));
2016 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2018 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2019 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2020 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2021 return true;
2025 static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
2027 switch (Op) {
2028 default: llvm_unreachable("invalid min/max opcode");
2029 case ISD::SMAX:
2030 return std::make_pair(ISD::SETGT, ISD::UMAX);
2031 case ISD::UMAX:
2032 return std::make_pair(ISD::SETUGT, ISD::UMAX);
2033 case ISD::SMIN:
2034 return std::make_pair(ISD::SETLT, ISD::UMIN);
2035 case ISD::UMIN:
2036 return std::make_pair(ISD::SETULT, ISD::UMIN);
2040 void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
2041 SDValue &Lo, SDValue &Hi) {
2042 SDLoc DL(N);
2043 ISD::NodeType LoOpc;
2044 ISD::CondCode CondC;
2045 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
2047 // Expand the subcomponents.
2048 SDValue LHSL, LHSH, RHSL, RHSH;
2049 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2050 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2052 // Value types
2053 EVT NVT = LHSL.getValueType();
2054 EVT CCT = getSetCCResultType(NVT);
2056 // Hi part is always the same op
2057 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
2059 // We need to know whether to select Lo part that corresponds to 'winning'
2060 // Hi part or if Hi parts are equal.
2061 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
2062 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
2064 // Lo part corresponding to the 'winning' Hi part
2065 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
2067 // Recursed Lo part if Hi parts are equal, this uses unsigned version
2068 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
2070 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
2073 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
2074 SDValue &Lo, SDValue &Hi) {
2075 SDLoc dl(N);
2076 // Expand the subcomponents.
2077 SDValue LHSL, LHSH, RHSL, RHSH;
2078 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2079 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2081 EVT NVT = LHSL.getValueType();
2082 SDValue LoOps[2] = { LHSL, RHSL };
2083 SDValue HiOps[3] = { LHSH, RHSH };
2085 bool HasOpCarry = TLI.isOperationLegalOrCustom(
2086 N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
2087 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2088 if (HasOpCarry) {
2089 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
2090 if (N->getOpcode() == ISD::ADD) {
2091 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2092 HiOps[2] = Lo.getValue(1);
2093 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, HiOps);
2094 } else {
2095 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2096 HiOps[2] = Lo.getValue(1);
2097 Hi = DAG.getNode(ISD::SUBCARRY, dl, VTList, HiOps);
2099 return;
2102 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
2103 // them. TODO: Teach operation legalization how to expand unsupported
2104 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
2105 // a carry of type MVT::Glue, but there doesn't seem to be any way to
2106 // generate a value of this type in the expanded code sequence.
2107 bool hasCarry =
2108 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2109 ISD::ADDC : ISD::SUBC,
2110 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2112 if (hasCarry) {
2113 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
2114 if (N->getOpcode() == ISD::ADD) {
2115 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2116 HiOps[2] = Lo.getValue(1);
2117 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2118 } else {
2119 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2120 HiOps[2] = Lo.getValue(1);
2121 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2123 return;
2126 bool hasOVF =
2127 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2128 ISD::UADDO : ISD::USUBO,
2129 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2130 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
2132 if (hasOVF) {
2133 EVT OvfVT = getSetCCResultType(NVT);
2134 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
2135 int RevOpc;
2136 if (N->getOpcode() == ISD::ADD) {
2137 RevOpc = ISD::SUB;
2138 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2139 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2140 } else {
2141 RevOpc = ISD::ADD;
2142 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2143 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2145 SDValue OVF = Lo.getValue(1);
2147 switch (BoolType) {
2148 case TargetLoweringBase::UndefinedBooleanContent:
2149 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
2150 LLVM_FALLTHROUGH;
2151 case TargetLoweringBase::ZeroOrOneBooleanContent:
2152 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
2153 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
2154 break;
2155 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
2156 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
2157 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
2159 return;
2162 if (N->getOpcode() == ISD::ADD) {
2163 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
2164 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2165 SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
2166 ISD::SETULT);
2168 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent) {
2169 SDValue Carry = DAG.getZExtOrTrunc(Cmp1, dl, NVT);
2170 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
2171 return;
2174 SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
2175 DAG.getConstant(1, dl, NVT),
2176 DAG.getConstant(0, dl, NVT));
2177 SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
2178 ISD::SETULT);
2179 SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
2180 DAG.getConstant(1, dl, NVT), Carry1);
2181 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
2182 } else {
2183 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
2184 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2185 SDValue Cmp =
2186 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
2187 LoOps[0], LoOps[1], ISD::SETULT);
2189 SDValue Borrow;
2190 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent)
2191 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
2192 else
2193 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
2194 DAG.getConstant(0, dl, NVT));
2196 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
2200 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
2201 SDValue &Lo, SDValue &Hi) {
2202 // Expand the subcomponents.
2203 SDValue LHSL, LHSH, RHSL, RHSH;
2204 SDLoc dl(N);
2205 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2206 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2207 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2208 SDValue LoOps[2] = { LHSL, RHSL };
2209 SDValue HiOps[3] = { LHSH, RHSH };
2211 if (N->getOpcode() == ISD::ADDC) {
2212 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2213 HiOps[2] = Lo.getValue(1);
2214 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2215 } else {
2216 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2217 HiOps[2] = Lo.getValue(1);
2218 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2221 // Legalized the flag result - switch anything that used the old flag to
2222 // use the new one.
2223 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2226 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
2227 SDValue &Lo, SDValue &Hi) {
2228 // Expand the subcomponents.
2229 SDValue LHSL, LHSH, RHSL, RHSH;
2230 SDLoc dl(N);
2231 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2232 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2233 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2234 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2235 SDValue HiOps[3] = { LHSH, RHSH };
2237 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2238 HiOps[2] = Lo.getValue(1);
2239 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2241 // Legalized the flag result - switch anything that used the old flag to
2242 // use the new one.
2243 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2246 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
2247 SDValue &Lo, SDValue &Hi) {
2248 SDValue LHS = N->getOperand(0);
2249 SDValue RHS = N->getOperand(1);
2250 SDLoc dl(N);
2252 SDValue Ovf;
2254 bool HasOpCarry = TLI.isOperationLegalOrCustom(
2255 N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
2256 TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
2258 if (HasOpCarry) {
2259 // Expand the subcomponents.
2260 SDValue LHSL, LHSH, RHSL, RHSH;
2261 GetExpandedInteger(LHS, LHSL, LHSH);
2262 GetExpandedInteger(RHS, RHSL, RHSH);
2263 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2264 SDValue LoOps[2] = { LHSL, RHSL };
2265 SDValue HiOps[3] = { LHSH, RHSH };
2267 unsigned Opc = N->getOpcode() == ISD::UADDO ? ISD::ADDCARRY : ISD::SUBCARRY;
2268 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2269 HiOps[2] = Lo.getValue(1);
2270 Hi = DAG.getNode(Opc, dl, VTList, HiOps);
2272 Ovf = Hi.getValue(1);
2273 } else {
2274 // Expand the result by simply replacing it with the equivalent
2275 // non-overflow-checking operation.
2276 auto Opc = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
2277 SDValue Sum = DAG.getNode(Opc, dl, LHS.getValueType(), LHS, RHS);
2278 SplitInteger(Sum, Lo, Hi);
2280 // Calculate the overflow: addition overflows iff a + b < a, and subtraction
2281 // overflows iff a - b > a.
2282 auto Cond = N->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
2283 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
2286 // Legalized the flag result - switch anything that used the old flag to
2287 // use the new one.
2288 ReplaceValueWith(SDValue(N, 1), Ovf);
2291 void DAGTypeLegalizer::ExpandIntRes_ADDSUBCARRY(SDNode *N,
2292 SDValue &Lo, SDValue &Hi) {
2293 // Expand the subcomponents.
2294 SDValue LHSL, LHSH, RHSL, RHSH;
2295 SDLoc dl(N);
2296 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2297 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2298 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2299 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2300 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
2302 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2303 HiOps[2] = Lo.getValue(1);
2304 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2306 // Legalized the flag result - switch anything that used the old flag to
2307 // use the new one.
2308 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2311 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
2312 SDValue &Lo, SDValue &Hi) {
2313 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2314 SDLoc dl(N);
2315 SDValue Op = N->getOperand(0);
2316 if (Op.getValueType().bitsLE(NVT)) {
2317 // The low part is any extension of the input (which degenerates to a copy).
2318 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
2319 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
2320 } else {
2321 // For example, extension of an i48 to an i64. The operand type necessarily
2322 // promotes to the result type, so will end up being expanded too.
2323 assert(getTypeAction(Op.getValueType()) ==
2324 TargetLowering::TypePromoteInteger &&
2325 "Only know how to promote this result!");
2326 SDValue Res = GetPromotedInteger(Op);
2327 assert(Res.getValueType() == N->getValueType(0) &&
2328 "Operand over promoted?");
2329 // Split the promoted operand. This will simplify when it is expanded.
2330 SplitInteger(Res, Lo, Hi);
2334 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
2335 SDValue &Lo, SDValue &Hi) {
2336 SDLoc dl(N);
2337 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2338 EVT NVT = Lo.getValueType();
2339 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2340 unsigned NVTBits = NVT.getSizeInBits();
2341 unsigned EVTBits = EVT.getSizeInBits();
2343 if (NVTBits < EVTBits) {
2344 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
2345 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2346 EVTBits - NVTBits)));
2347 } else {
2348 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
2349 // The high part replicates the sign bit of Lo, make it explicit.
2350 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2351 DAG.getConstant(NVTBits - 1, dl,
2352 TLI.getPointerTy(DAG.getDataLayout())));
2356 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
2357 SDValue &Lo, SDValue &Hi) {
2358 SDLoc dl(N);
2359 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2360 EVT NVT = Lo.getValueType();
2361 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2362 unsigned NVTBits = NVT.getSizeInBits();
2363 unsigned EVTBits = EVT.getSizeInBits();
2365 if (NVTBits < EVTBits) {
2366 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
2367 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2368 EVTBits - NVTBits)));
2369 } else {
2370 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
2371 // The high part must be zero, make it explicit.
2372 Hi = DAG.getConstant(0, dl, NVT);
2376 void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
2377 SDValue &Lo, SDValue &Hi) {
2378 SDLoc dl(N);
2379 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
2380 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
2381 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
2384 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
2385 SDValue &Lo, SDValue &Hi) {
2386 SDLoc dl(N);
2387 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
2388 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
2389 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
2392 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
2393 SDValue &Lo, SDValue &Hi) {
2394 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2395 unsigned NBitWidth = NVT.getSizeInBits();
2396 auto Constant = cast<ConstantSDNode>(N);
2397 const APInt &Cst = Constant->getAPIntValue();
2398 bool IsTarget = Constant->isTargetOpcode();
2399 bool IsOpaque = Constant->isOpaque();
2400 SDLoc dl(N);
2401 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
2402 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
2403 IsOpaque);
2406 void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
2407 SDLoc dl(N);
2409 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
2410 EVT VT = N->getValueType(0);
2411 SDValue N0 = N->getOperand(0);
2412 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
2413 DAG.getConstant(0, dl, VT), N0);
2414 SDValue NegLo, NegHi;
2415 SplitInteger(Neg, NegLo, NegHi);
2417 GetExpandedInteger(N0, Lo, Hi);
2418 EVT NVT = Lo.getValueType();
2419 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT),
2420 DAG.getConstant(0, dl, NVT), Hi, ISD::SETGT);
2421 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
2422 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
2425 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
2426 SDValue &Lo, SDValue &Hi) {
2427 SDLoc dl(N);
2428 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
2429 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2430 EVT NVT = Lo.getValueType();
2432 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
2433 DAG.getConstant(0, dl, NVT), ISD::SETNE);
2435 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
2436 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
2438 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
2439 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
2440 DAG.getConstant(NVT.getSizeInBits(), dl,
2441 NVT)));
2442 Hi = DAG.getConstant(0, dl, NVT);
2445 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
2446 SDValue &Lo, SDValue &Hi) {
2447 SDLoc dl(N);
2448 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
2449 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2450 EVT NVT = Lo.getValueType();
2451 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
2452 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
2453 Hi = DAG.getConstant(0, dl, NVT);
2456 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
2457 SDValue &Lo, SDValue &Hi) {
2458 SDLoc dl(N);
2459 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
2460 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2461 EVT NVT = Lo.getValueType();
2463 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
2464 DAG.getConstant(0, dl, NVT), ISD::SETNE);
2466 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
2467 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
2469 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
2470 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
2471 DAG.getConstant(NVT.getSizeInBits(), dl,
2472 NVT)));
2473 Hi = DAG.getConstant(0, dl, NVT);
2476 void DAGTypeLegalizer::ExpandIntRes_FLT_ROUNDS(SDNode *N, SDValue &Lo,
2477 SDValue &Hi) {
2478 SDLoc dl(N);
2479 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2480 unsigned NBitWidth = NVT.getSizeInBits();
2482 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
2483 Lo = DAG.getNode(ISD::FLT_ROUNDS_, dl, NVT);
2484 // The high part is the sign of Lo, as -1 is a valid value for FLT_ROUNDS
2485 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2486 DAG.getConstant(NBitWidth - 1, dl, ShiftAmtTy));
2489 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
2490 SDValue &Hi) {
2491 SDLoc dl(N);
2492 EVT VT = N->getValueType(0);
2494 SDValue Op = N->getOperand(0);
2495 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2496 Op = GetPromotedFloat(Op);
2498 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
2499 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
2500 TargetLowering::MakeLibCallOptions CallOptions;
2501 CallOptions.setSExt(true);
2502 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, dl).first,
2503 Lo, Hi);
2506 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
2507 SDValue &Hi) {
2508 SDLoc dl(N);
2509 EVT VT = N->getValueType(0);
2511 SDValue Op = N->getOperand(0);
2512 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2513 Op = GetPromotedFloat(Op);
2515 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
2516 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
2517 TargetLowering::MakeLibCallOptions CallOptions;
2518 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, dl).first,
2519 Lo, Hi);
2522 void DAGTypeLegalizer::ExpandIntRes_LLROUND(SDNode *N, SDValue &Lo,
2523 SDValue &Hi) {
2524 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2525 EVT VT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
2526 if (VT == MVT::f32)
2527 LC = RTLIB::LLROUND_F32;
2528 else if (VT == MVT::f64)
2529 LC = RTLIB::LLROUND_F64;
2530 else if (VT == MVT::f80)
2531 LC = RTLIB::LLROUND_F80;
2532 else if (VT == MVT::f128)
2533 LC = RTLIB::LLROUND_F128;
2534 else if (VT == MVT::ppcf128)
2535 LC = RTLIB::LLROUND_PPCF128;
2536 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
2538 SDValue Op = N->getOperand(0);
2539 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2540 Op = GetPromotedFloat(Op);
2542 SDLoc dl(N);
2543 EVT RetVT = N->getValueType(0);
2544 TargetLowering::MakeLibCallOptions CallOptions;
2545 CallOptions.setSExt(true);
2546 SplitInteger(TLI.makeLibCall(DAG, LC, RetVT, Op, CallOptions, dl).first,
2547 Lo, Hi);
2550 void DAGTypeLegalizer::ExpandIntRes_LLRINT(SDNode *N, SDValue &Lo,
2551 SDValue &Hi) {
2552 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2553 EVT VT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
2554 if (VT == MVT::f32)
2555 LC = RTLIB::LLRINT_F32;
2556 else if (VT == MVT::f64)
2557 LC = RTLIB::LLRINT_F64;
2558 else if (VT == MVT::f80)
2559 LC = RTLIB::LLRINT_F80;
2560 else if (VT == MVT::f128)
2561 LC = RTLIB::LLRINT_F128;
2562 else if (VT == MVT::ppcf128)
2563 LC = RTLIB::LLRINT_PPCF128;
2564 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
2566 SDValue Op = N->getOperand(0);
2567 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2568 Op = GetPromotedFloat(Op);
2570 SDLoc dl(N);
2571 EVT RetVT = N->getValueType(0);
2572 TargetLowering::MakeLibCallOptions CallOptions;
2573 CallOptions.setSExt(true);
2574 SplitInteger(TLI.makeLibCall(DAG, LC, RetVT, Op, CallOptions, dl).first,
2575 Lo, Hi);
2578 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
2579 SDValue &Lo, SDValue &Hi) {
2580 if (ISD::isNormalLoad(N)) {
2581 ExpandRes_NormalLoad(N, Lo, Hi);
2582 return;
2585 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
2587 EVT VT = N->getValueType(0);
2588 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2589 SDValue Ch = N->getChain();
2590 SDValue Ptr = N->getBasePtr();
2591 ISD::LoadExtType ExtType = N->getExtensionType();
2592 unsigned Alignment = N->getAlignment();
2593 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
2594 AAMDNodes AAInfo = N->getAAInfo();
2595 SDLoc dl(N);
2597 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2599 if (N->getMemoryVT().bitsLE(NVT)) {
2600 EVT MemVT = N->getMemoryVT();
2602 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
2603 Alignment, MMOFlags, AAInfo);
2605 // Remember the chain.
2606 Ch = Lo.getValue(1);
2608 if (ExtType == ISD::SEXTLOAD) {
2609 // The high part is obtained by SRA'ing all but one of the bits of the
2610 // lo part.
2611 unsigned LoSize = Lo.getValueSizeInBits();
2612 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2613 DAG.getConstant(LoSize - 1, dl,
2614 TLI.getPointerTy(DAG.getDataLayout())));
2615 } else if (ExtType == ISD::ZEXTLOAD) {
2616 // The high part is just a zero.
2617 Hi = DAG.getConstant(0, dl, NVT);
2618 } else {
2619 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
2620 // The high part is undefined.
2621 Hi = DAG.getUNDEF(NVT);
2623 } else if (DAG.getDataLayout().isLittleEndian()) {
2624 // Little-endian - low bits are at low addresses.
2625 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
2626 AAInfo);
2628 unsigned ExcessBits =
2629 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2630 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
2632 // Increment the pointer to the other half.
2633 unsigned IncrementSize = NVT.getSizeInBits()/8;
2634 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2635 DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
2636 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
2637 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
2638 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
2640 // Build a factor node to remember that this load is independent of the
2641 // other one.
2642 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2643 Hi.getValue(1));
2644 } else {
2645 // Big-endian - high bits are at low addresses. Favor aligned loads at
2646 // the cost of some bit-fiddling.
2647 EVT MemVT = N->getMemoryVT();
2648 unsigned EBytes = MemVT.getStoreSize();
2649 unsigned IncrementSize = NVT.getSizeInBits()/8;
2650 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2652 // Load both the high bits and maybe some of the low bits.
2653 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
2654 EVT::getIntegerVT(*DAG.getContext(),
2655 MemVT.getSizeInBits() - ExcessBits),
2656 Alignment, MMOFlags, AAInfo);
2658 // Increment the pointer to the other half.
2659 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2660 DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
2661 // Load the rest of the low bits.
2662 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
2663 N->getPointerInfo().getWithOffset(IncrementSize),
2664 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
2665 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
2667 // Build a factor node to remember that this load is independent of the
2668 // other one.
2669 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2670 Hi.getValue(1));
2672 if (ExcessBits < NVT.getSizeInBits()) {
2673 // Transfer low bits from the bottom of Hi to the top of Lo.
2674 Lo = DAG.getNode(
2675 ISD::OR, dl, NVT, Lo,
2676 DAG.getNode(ISD::SHL, dl, NVT, Hi,
2677 DAG.getConstant(ExcessBits, dl,
2678 TLI.getPointerTy(DAG.getDataLayout()))));
2679 // Move high bits to the right position in Hi.
2680 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
2682 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
2683 TLI.getPointerTy(DAG.getDataLayout())));
2687 // Legalize the chain result - switch anything that used the old chain to
2688 // use the new one.
2689 ReplaceValueWith(SDValue(N, 1), Ch);
2692 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
2693 SDValue &Lo, SDValue &Hi) {
2694 SDLoc dl(N);
2695 SDValue LL, LH, RL, RH;
2696 GetExpandedInteger(N->getOperand(0), LL, LH);
2697 GetExpandedInteger(N->getOperand(1), RL, RH);
2698 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
2699 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
2702 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
2703 SDValue &Lo, SDValue &Hi) {
2704 EVT VT = N->getValueType(0);
2705 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2706 SDLoc dl(N);
2708 SDValue LL, LH, RL, RH;
2709 GetExpandedInteger(N->getOperand(0), LL, LH);
2710 GetExpandedInteger(N->getOperand(1), RL, RH);
2712 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
2713 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
2714 LL, LH, RL, RH))
2715 return;
2717 // If nothing else, we can make a libcall.
2718 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2719 if (VT == MVT::i16)
2720 LC = RTLIB::MUL_I16;
2721 else if (VT == MVT::i32)
2722 LC = RTLIB::MUL_I32;
2723 else if (VT == MVT::i64)
2724 LC = RTLIB::MUL_I64;
2725 else if (VT == MVT::i128)
2726 LC = RTLIB::MUL_I128;
2728 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
2729 // We'll expand the multiplication by brute force because we have no other
2730 // options. This is a trivially-generalized version of the code from
2731 // Hacker's Delight (itself derived from Knuth's Algorithm M from section
2732 // 4.3.1).
2733 unsigned Bits = NVT.getSizeInBits();
2734 unsigned HalfBits = Bits >> 1;
2735 SDValue Mask = DAG.getConstant(APInt::getLowBitsSet(Bits, HalfBits), dl,
2736 NVT);
2737 SDValue LLL = DAG.getNode(ISD::AND, dl, NVT, LL, Mask);
2738 SDValue RLL = DAG.getNode(ISD::AND, dl, NVT, RL, Mask);
2740 SDValue T = DAG.getNode(ISD::MUL, dl, NVT, LLL, RLL);
2741 SDValue TL = DAG.getNode(ISD::AND, dl, NVT, T, Mask);
2743 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
2744 if (APInt::getMaxValue(ShiftAmtTy.getSizeInBits()).ult(HalfBits)) {
2745 // The type from TLI is too small to fit the shift amount we want.
2746 // Override it with i32. The shift will have to be legalized.
2747 ShiftAmtTy = MVT::i32;
2749 SDValue Shift = DAG.getConstant(HalfBits, dl, ShiftAmtTy);
2750 SDValue TH = DAG.getNode(ISD::SRL, dl, NVT, T, Shift);
2751 SDValue LLH = DAG.getNode(ISD::SRL, dl, NVT, LL, Shift);
2752 SDValue RLH = DAG.getNode(ISD::SRL, dl, NVT, RL, Shift);
2754 SDValue U = DAG.getNode(ISD::ADD, dl, NVT,
2755 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLL), TH);
2756 SDValue UL = DAG.getNode(ISD::AND, dl, NVT, U, Mask);
2757 SDValue UH = DAG.getNode(ISD::SRL, dl, NVT, U, Shift);
2759 SDValue V = DAG.getNode(ISD::ADD, dl, NVT,
2760 DAG.getNode(ISD::MUL, dl, NVT, LLL, RLH), UL);
2761 SDValue VH = DAG.getNode(ISD::SRL, dl, NVT, V, Shift);
2763 SDValue W = DAG.getNode(ISD::ADD, dl, NVT,
2764 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLH),
2765 DAG.getNode(ISD::ADD, dl, NVT, UH, VH));
2766 Lo = DAG.getNode(ISD::ADD, dl, NVT, TL,
2767 DAG.getNode(ISD::SHL, dl, NVT, V, Shift));
2769 Hi = DAG.getNode(ISD::ADD, dl, NVT, W,
2770 DAG.getNode(ISD::ADD, dl, NVT,
2771 DAG.getNode(ISD::MUL, dl, NVT, RH, LL),
2772 DAG.getNode(ISD::MUL, dl, NVT, RL, LH)));
2773 return;
2776 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2777 TargetLowering::MakeLibCallOptions CallOptions;
2778 CallOptions.setSExt(true);
2779 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first,
2780 Lo, Hi);
2783 void DAGTypeLegalizer::ExpandIntRes_READCYCLECOUNTER(SDNode *N, SDValue &Lo,
2784 SDValue &Hi) {
2785 SDLoc DL(N);
2786 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2787 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
2788 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
2789 Lo = R.getValue(0);
2790 Hi = R.getValue(1);
2791 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
2794 void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
2795 SDValue &Hi) {
2796 SDValue Result = TLI.expandAddSubSat(N, DAG);
2797 SplitInteger(Result, Lo, Hi);
2800 /// This performs an expansion of the integer result for a fixed point
2801 /// multiplication. The default expansion performs rounding down towards
2802 /// negative infinity, though targets that do care about rounding should specify
2803 /// a target hook for rounding and provide their own expansion or lowering of
2804 /// fixed point multiplication to be consistent with rounding.
2805 void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
2806 SDValue &Hi) {
2807 SDLoc dl(N);
2808 EVT VT = N->getValueType(0);
2809 unsigned VTSize = VT.getScalarSizeInBits();
2810 SDValue LHS = N->getOperand(0);
2811 SDValue RHS = N->getOperand(1);
2812 uint64_t Scale = N->getConstantOperandVal(2);
2813 bool Saturating = N->getOpcode() == ISD::SMULFIXSAT;
2814 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
2815 N->getOpcode() == ISD::SMULFIXSAT);
2817 // Handle special case when scale is equal to zero.
2818 if (!Scale) {
2819 SDValue Result;
2820 if (!Saturating) {
2821 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
2822 } else {
2823 EVT BoolVT = getSetCCResultType(VT);
2824 Result = DAG.getNode(ISD::SMULO, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
2825 SDValue Product = Result.getValue(0);
2826 SDValue Overflow = Result.getValue(1);
2827 assert(Signed && "Unsigned saturation not supported (yet).");
2828 APInt MinVal = APInt::getSignedMinValue(VTSize);
2829 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
2830 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
2831 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
2832 SDValue Zero = DAG.getConstant(0, dl, VT);
2833 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Product, Zero, ISD::SETLT);
2834 Result = DAG.getSelect(dl, VT, ProdNeg, SatMax, SatMin);
2835 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
2837 SplitInteger(Result, Lo, Hi);
2838 return;
2841 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2842 SDValue LL, LH, RL, RH;
2843 GetExpandedInteger(LHS, LL, LH);
2844 GetExpandedInteger(RHS, RL, RH);
2845 SmallVector<SDValue, 4> Result;
2847 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
2848 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
2849 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
2850 LL, LH, RL, RH)) {
2851 report_fatal_error("Unable to expand MUL_FIX using MUL_LOHI.");
2852 return;
2855 unsigned NVTSize = NVT.getScalarSizeInBits();
2856 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
2857 "the size of the current value type");
2858 EVT ShiftTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
2860 // After getting the multiplication result in 4 parts, we need to perform a
2861 // shift right by the amount of the scale to get the result in that scale.
2863 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
2864 // 128 bits that are cut into 4 32-bit parts:
2866 // HH HL LH LL
2867 // |---32---|---32---|---32---|---32---|
2868 // 128 96 64 32 0
2870 // |------VTSize-----|
2872 // |NVTSize-|
2874 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
2875 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
2876 // amount and get Lo and Hi using two funnel shifts. Or for the special case
2877 // when Scale is a multiple of NVTSize we can just pick the result without
2878 // shifting.
2879 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
2880 if (Scale % NVTSize) {
2881 SDValue ShiftAmount = DAG.getConstant(Scale % NVTSize, dl, ShiftTy);
2882 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
2883 ShiftAmount);
2884 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
2885 ShiftAmount);
2886 } else {
2887 Lo = Result[Part0];
2888 Hi = Result[Part0 + 1];
2891 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
2892 if (!Saturating)
2893 return;
2895 // To handle saturation we must check for overflow in the multiplication.
2897 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
2898 // aren't all ones or all zeroes.
2900 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
2901 // highest bit of HH determines saturation direction in the event of
2902 // saturation.
2904 SDValue ResultHL = Result[2];
2905 SDValue ResultHH = Result[3];
2907 SDValue SatMax, SatMin;
2908 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
2909 SDValue NVTNeg1 = DAG.getConstant(-1, dl, NVT);
2910 EVT BoolNVT = getSetCCResultType(NVT);
2912 if (!Signed)
2913 llvm_unreachable("Unsigned saturation not supported (yet).");
2915 if (Scale < NVTSize) {
2916 // The number of overflow bits we can check are VTSize - Scale + 1 (we
2917 // include the sign bit). If these top bits are > 0, then we overflowed past
2918 // the max value. If these top bits are < -1, then we overflowed past the
2919 // min value. Otherwise, we did not overflow.
2920 unsigned OverflowBits = VTSize - Scale + 1;
2921 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
2922 "Extent of overflow bits must start within HL");
2923 SDValue HLHiMask = DAG.getConstant(
2924 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
2925 SDValue HLLoMask = DAG.getConstant(
2926 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
2927 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
2928 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
2929 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
2930 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
2931 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
2932 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
2933 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
2934 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
2935 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
2936 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
2937 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
2938 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
2939 } else if (Scale == NVTSize) {
2940 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
2941 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
2942 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
2943 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
2944 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
2945 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
2946 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
2947 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
2948 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
2949 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
2950 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
2951 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
2952 } else if (Scale < VTSize) {
2953 // This is similar to the case when we saturate if Scale < NVTSize, but we
2954 // only need to check HH.
2955 unsigned OverflowBits = VTSize - Scale + 1;
2956 SDValue HHHiMask = DAG.getConstant(
2957 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
2958 SDValue HHLoMask = DAG.getConstant(
2959 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
2960 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
2961 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
2962 } else
2963 llvm_unreachable("Illegal scale for signed fixed point mul.");
2965 // Saturate to signed maximum.
2966 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
2967 APInt MaxLo = APInt::getAllOnesValue(NVTSize);
2968 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
2969 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
2970 // Saturate to signed minimum.
2971 APInt MinHi = APInt::getSignedMinValue(NVTSize);
2972 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
2973 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
2976 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
2977 SDValue &Lo, SDValue &Hi) {
2978 SDValue LHS = Node->getOperand(0);
2979 SDValue RHS = Node->getOperand(1);
2980 SDLoc dl(Node);
2982 // Expand the result by simply replacing it with the equivalent
2983 // non-overflow-checking operation.
2984 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
2985 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
2986 LHS, RHS);
2987 SplitInteger(Sum, Lo, Hi);
2989 // Compute the overflow.
2991 // LHSSign -> LHS >= 0
2992 // RHSSign -> RHS >= 0
2993 // SumSign -> Sum >= 0
2995 // Add:
2996 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
2997 // Sub:
2998 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3000 EVT OType = Node->getValueType(1);
3001 SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
3003 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3004 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3005 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3006 Node->getOpcode() == ISD::SADDO ?
3007 ISD::SETEQ : ISD::SETNE);
3009 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3010 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3012 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3014 // Use the calculated overflow everywhere.
3015 ReplaceValueWith(SDValue(Node, 1), Cmp);
3018 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
3019 SDValue &Lo, SDValue &Hi) {
3020 EVT VT = N->getValueType(0);
3021 SDLoc dl(N);
3022 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3024 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3025 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3026 SplitInteger(Res.getValue(0), Lo, Hi);
3027 return;
3030 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3031 if (VT == MVT::i16)
3032 LC = RTLIB::SDIV_I16;
3033 else if (VT == MVT::i32)
3034 LC = RTLIB::SDIV_I32;
3035 else if (VT == MVT::i64)
3036 LC = RTLIB::SDIV_I64;
3037 else if (VT == MVT::i128)
3038 LC = RTLIB::SDIV_I128;
3039 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
3041 TargetLowering::MakeLibCallOptions CallOptions;
3042 CallOptions.setSExt(true);
3043 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3046 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
3047 SDValue &Lo, SDValue &Hi) {
3048 EVT VT = N->getValueType(0);
3049 SDLoc dl(N);
3051 // If we can emit an efficient shift operation, do so now. Check to see if
3052 // the RHS is a constant.
3053 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
3054 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
3056 // If we can determine that the high bit of the shift is zero or one, even if
3057 // the low bits are variable, emit this shift in an optimized form.
3058 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
3059 return;
3061 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
3062 unsigned PartsOpc;
3063 if (N->getOpcode() == ISD::SHL) {
3064 PartsOpc = ISD::SHL_PARTS;
3065 } else if (N->getOpcode() == ISD::SRL) {
3066 PartsOpc = ISD::SRL_PARTS;
3067 } else {
3068 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3069 PartsOpc = ISD::SRA_PARTS;
3072 // Next check to see if the target supports this SHL_PARTS operation or if it
3073 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
3074 // size, but create a libcall instead.
3075 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3076 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
3077 const bool LegalOrCustom =
3078 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3079 Action == TargetLowering::Custom;
3081 if (LegalOrCustom && TLI.shouldExpandShift(DAG, N)) {
3082 // Expand the subcomponents.
3083 SDValue LHSL, LHSH;
3084 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3085 EVT VT = LHSL.getValueType();
3087 // If the shift amount operand is coming from a vector legalization it may
3088 // have an illegal type. Fix that first by casting the operand, otherwise
3089 // the new SHL_PARTS operation would need further legalization.
3090 SDValue ShiftOp = N->getOperand(1);
3091 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3092 assert(ShiftTy.getScalarSizeInBits() >=
3093 Log2_32_Ceil(VT.getScalarSizeInBits()) &&
3094 "ShiftAmountTy is too small to cover the range of this type!");
3095 if (ShiftOp.getValueType() != ShiftTy)
3096 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
3098 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
3099 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
3100 Hi = Lo.getValue(1);
3101 return;
3104 // Otherwise, emit a libcall.
3105 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3106 bool isSigned;
3107 if (N->getOpcode() == ISD::SHL) {
3108 isSigned = false; /*sign irrelevant*/
3109 if (VT == MVT::i16)
3110 LC = RTLIB::SHL_I16;
3111 else if (VT == MVT::i32)
3112 LC = RTLIB::SHL_I32;
3113 else if (VT == MVT::i64)
3114 LC = RTLIB::SHL_I64;
3115 else if (VT == MVT::i128)
3116 LC = RTLIB::SHL_I128;
3117 } else if (N->getOpcode() == ISD::SRL) {
3118 isSigned = false;
3119 if (VT == MVT::i16)
3120 LC = RTLIB::SRL_I16;
3121 else if (VT == MVT::i32)
3122 LC = RTLIB::SRL_I32;
3123 else if (VT == MVT::i64)
3124 LC = RTLIB::SRL_I64;
3125 else if (VT == MVT::i128)
3126 LC = RTLIB::SRL_I128;
3127 } else {
3128 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3129 isSigned = true;
3130 if (VT == MVT::i16)
3131 LC = RTLIB::SRA_I16;
3132 else if (VT == MVT::i32)
3133 LC = RTLIB::SRA_I32;
3134 else if (VT == MVT::i64)
3135 LC = RTLIB::SRA_I64;
3136 else if (VT == MVT::i128)
3137 LC = RTLIB::SRA_I128;
3140 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
3141 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3142 TargetLowering::MakeLibCallOptions CallOptions;
3143 CallOptions.setSExt(isSigned);
3144 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3145 return;
3148 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
3149 llvm_unreachable("Unsupported shift!");
3152 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
3153 SDValue &Lo, SDValue &Hi) {
3154 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3155 SDLoc dl(N);
3156 SDValue Op = N->getOperand(0);
3157 if (Op.getValueType().bitsLE(NVT)) {
3158 // The low part is sign extension of the input (degenerates to a copy).
3159 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
3160 // The high part is obtained by SRA'ing all but one of the bits of low part.
3161 unsigned LoSize = NVT.getSizeInBits();
3162 Hi = DAG.getNode(
3163 ISD::SRA, dl, NVT, Lo,
3164 DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
3165 } else {
3166 // For example, extension of an i48 to an i64. The operand type necessarily
3167 // promotes to the result type, so will end up being expanded too.
3168 assert(getTypeAction(Op.getValueType()) ==
3169 TargetLowering::TypePromoteInteger &&
3170 "Only know how to promote this result!");
3171 SDValue Res = GetPromotedInteger(Op);
3172 assert(Res.getValueType() == N->getValueType(0) &&
3173 "Operand over promoted?");
3174 // Split the promoted operand. This will simplify when it is expanded.
3175 SplitInteger(Res, Lo, Hi);
3176 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
3177 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
3178 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3179 ExcessBits)));
3183 void DAGTypeLegalizer::
3184 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
3185 SDLoc dl(N);
3186 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3187 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3189 if (EVT.bitsLE(Lo.getValueType())) {
3190 // sext_inreg the low part if needed.
3191 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
3192 N->getOperand(1));
3194 // The high part gets the sign extension from the lo-part. This handles
3195 // things like sextinreg V:i64 from i8.
3196 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
3197 DAG.getConstant(Hi.getValueSizeInBits() - 1, dl,
3198 TLI.getPointerTy(DAG.getDataLayout())));
3199 } else {
3200 // For example, extension of an i48 to an i64. Leave the low part alone,
3201 // sext_inreg the high part.
3202 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
3203 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
3204 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3205 ExcessBits)));
3209 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
3210 SDValue &Lo, SDValue &Hi) {
3211 EVT VT = N->getValueType(0);
3212 SDLoc dl(N);
3213 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3215 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3216 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3217 SplitInteger(Res.getValue(1), Lo, Hi);
3218 return;
3221 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3222 if (VT == MVT::i16)
3223 LC = RTLIB::SREM_I16;
3224 else if (VT == MVT::i32)
3225 LC = RTLIB::SREM_I32;
3226 else if (VT == MVT::i64)
3227 LC = RTLIB::SREM_I64;
3228 else if (VT == MVT::i128)
3229 LC = RTLIB::SREM_I128;
3230 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
3232 TargetLowering::MakeLibCallOptions CallOptions;
3233 CallOptions.setSExt(true);
3234 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3237 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
3238 SDValue &Lo, SDValue &Hi) {
3239 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3240 SDLoc dl(N);
3241 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
3242 Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
3243 N->getOperand(0),
3244 DAG.getConstant(NVT.getSizeInBits(), dl,
3245 TLI.getPointerTy(DAG.getDataLayout())));
3246 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
3249 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
3250 SDValue &Lo, SDValue &Hi) {
3251 EVT VT = N->getValueType(0);
3252 SDLoc dl(N);
3254 if (N->getOpcode() == ISD::UMULO) {
3255 // This section expands the operation into the following sequence of
3256 // instructions. `iNh` here refers to a type which has half the bit width of
3257 // the type the original operation operated on.
3259 // %0 = %LHS.HI != 0 && %RHS.HI != 0
3260 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
3261 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
3262 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
3263 // %4 = add iN (%1.0 as iN) << Nh, (%2.0 as iN) << Nh
3264 // %5 = { iN, i1 } @uadd.with.overflow.iN( %4, %3 )
3266 // %res = { %5.0, %0 || %1.1 || %2.1 || %5.1 }
3267 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
3268 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
3269 SplitInteger(LHS, LHSLow, LHSHigh);
3270 SplitInteger(RHS, RHSLow, RHSHigh);
3271 EVT HalfVT = LHSLow.getValueType()
3272 , BitVT = N->getValueType(1);
3273 SDVTList VTHalfMulO = DAG.getVTList(HalfVT, BitVT);
3274 SDVTList VTFullAddO = DAG.getVTList(VT, BitVT);
3276 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
3277 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
3278 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
3279 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
3281 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfMulO, LHSHigh, RHSLow);
3282 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
3283 SDValue OneInHigh = DAG.getNode(ISD::BUILD_PAIR, dl, VT, HalfZero,
3284 One.getValue(0));
3286 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfMulO, RHSHigh, LHSLow);
3287 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
3288 SDValue TwoInHigh = DAG.getNode(ISD::BUILD_PAIR, dl, VT, HalfZero,
3289 Two.getValue(0));
3291 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
3292 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
3293 // operation recursively legalized?).
3295 // Many backends understand this pattern and will convert into LOHI
3296 // themselves, if applicable.
3297 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
3298 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
3299 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
3300 SDValue Four = DAG.getNode(ISD::ADD, dl, VT, OneInHigh, TwoInHigh);
3301 SDValue Five = DAG.getNode(ISD::UADDO, dl, VTFullAddO, Three, Four);
3302 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Five.getValue(1));
3303 SplitInteger(Five, Lo, Hi);
3304 ReplaceValueWith(SDValue(N, 1), Overflow);
3305 return;
3308 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
3309 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
3310 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
3312 // Replace this with a libcall that will check overflow.
3313 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3314 if (VT == MVT::i32)
3315 LC = RTLIB::MULO_I32;
3316 else if (VT == MVT::i64)
3317 LC = RTLIB::MULO_I64;
3318 else if (VT == MVT::i128)
3319 LC = RTLIB::MULO_I128;
3320 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!");
3322 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
3323 // Temporary for the overflow value, default it to zero.
3324 SDValue Chain =
3325 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
3326 MachinePointerInfo());
3328 TargetLowering::ArgListTy Args;
3329 TargetLowering::ArgListEntry Entry;
3330 for (const SDValue &Op : N->op_values()) {
3331 EVT ArgVT = Op.getValueType();
3332 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
3333 Entry.Node = Op;
3334 Entry.Ty = ArgTy;
3335 Entry.IsSExt = true;
3336 Entry.IsZExt = false;
3337 Args.push_back(Entry);
3340 // Also pass the address of the overflow check.
3341 Entry.Node = Temp;
3342 Entry.Ty = PtrTy->getPointerTo();
3343 Entry.IsSExt = true;
3344 Entry.IsZExt = false;
3345 Args.push_back(Entry);
3347 SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
3349 TargetLowering::CallLoweringInfo CLI(DAG);
3350 CLI.setDebugLoc(dl)
3351 .setChain(Chain)
3352 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args))
3353 .setSExtResult();
3355 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
3357 SplitInteger(CallInfo.first, Lo, Hi);
3358 SDValue Temp2 =
3359 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
3360 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
3361 DAG.getConstant(0, dl, PtrVT),
3362 ISD::SETNE);
3363 // Use the overflow from the libcall everywhere.
3364 ReplaceValueWith(SDValue(N, 1), Ofl);
3367 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
3368 SDValue &Lo, SDValue &Hi) {
3369 EVT VT = N->getValueType(0);
3370 SDLoc dl(N);
3371 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3373 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
3374 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3375 SplitInteger(Res.getValue(0), Lo, Hi);
3376 return;
3379 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3380 if (VT == MVT::i16)
3381 LC = RTLIB::UDIV_I16;
3382 else if (VT == MVT::i32)
3383 LC = RTLIB::UDIV_I32;
3384 else if (VT == MVT::i64)
3385 LC = RTLIB::UDIV_I64;
3386 else if (VT == MVT::i128)
3387 LC = RTLIB::UDIV_I128;
3388 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
3390 TargetLowering::MakeLibCallOptions CallOptions;
3391 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3394 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
3395 SDValue &Lo, SDValue &Hi) {
3396 EVT VT = N->getValueType(0);
3397 SDLoc dl(N);
3398 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3400 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
3401 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3402 SplitInteger(Res.getValue(1), Lo, Hi);
3403 return;
3406 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3407 if (VT == MVT::i16)
3408 LC = RTLIB::UREM_I16;
3409 else if (VT == MVT::i32)
3410 LC = RTLIB::UREM_I32;
3411 else if (VT == MVT::i64)
3412 LC = RTLIB::UREM_I64;
3413 else if (VT == MVT::i128)
3414 LC = RTLIB::UREM_I128;
3415 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
3417 TargetLowering::MakeLibCallOptions CallOptions;
3418 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3421 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
3422 SDValue &Lo, SDValue &Hi) {
3423 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3424 SDLoc dl(N);
3425 SDValue Op = N->getOperand(0);
3426 if (Op.getValueType().bitsLE(NVT)) {
3427 // The low part is zero extension of the input (degenerates to a copy).
3428 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
3429 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
3430 } else {
3431 // For example, extension of an i48 to an i64. The operand type necessarily
3432 // promotes to the result type, so will end up being expanded too.
3433 assert(getTypeAction(Op.getValueType()) ==
3434 TargetLowering::TypePromoteInteger &&
3435 "Only know how to promote this result!");
3436 SDValue Res = GetPromotedInteger(Op);
3437 assert(Res.getValueType() == N->getValueType(0) &&
3438 "Operand over promoted?");
3439 // Split the promoted operand. This will simplify when it is expanded.
3440 SplitInteger(Res, Lo, Hi);
3441 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
3442 Hi = DAG.getZeroExtendInReg(Hi, dl,
3443 EVT::getIntegerVT(*DAG.getContext(),
3444 ExcessBits));
3448 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
3449 SDValue &Lo, SDValue &Hi) {
3450 SDLoc dl(N);
3451 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
3452 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
3453 SDValue Zero = DAG.getConstant(0, dl, VT);
3454 SDValue Swap = DAG.getAtomicCmpSwap(
3455 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
3456 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
3457 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
3459 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
3460 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
3463 void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
3464 SDValue &Lo, SDValue &Hi) {
3465 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
3466 // both halves independently.
3467 SDValue Res = TLI.expandVecReduce(N, DAG);
3468 SplitInteger(Res, Lo, Hi);
3471 //===----------------------------------------------------------------------===//
3472 // Integer Operand Expansion
3473 //===----------------------------------------------------------------------===//
3475 /// ExpandIntegerOperand - This method is called when the specified operand of
3476 /// the specified node is found to need expansion. At this point, all of the
3477 /// result types of the node are known to be legal, but other operands of the
3478 /// node may need promotion or expansion as well as the specified one.
3479 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
3480 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG);
3481 dbgs() << "\n");
3482 SDValue Res = SDValue();
3484 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
3485 return false;
3487 switch (N->getOpcode()) {
3488 default:
3489 #ifndef NDEBUG
3490 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
3491 N->dump(&DAG); dbgs() << "\n";
3492 #endif
3493 report_fatal_error("Do not know how to expand this operator's operand!");
3495 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
3496 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
3497 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
3498 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
3499 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
3500 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
3501 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
3502 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
3503 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
3504 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
3505 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
3506 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
3507 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
3509 case ISD::SHL:
3510 case ISD::SRA:
3511 case ISD::SRL:
3512 case ISD::ROTL:
3513 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
3514 case ISD::RETURNADDR:
3515 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
3517 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
3520 // If the result is null, the sub-method took care of registering results etc.
3521 if (!Res.getNode()) return false;
3523 // If the result is N, the sub-method updated N in place. Tell the legalizer
3524 // core about this.
3525 if (Res.getNode() == N)
3526 return true;
3528 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
3529 "Invalid operand expansion");
3531 ReplaceValueWith(SDValue(N, 0), Res);
3532 return false;
3535 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
3536 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
3537 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
3538 SDValue &NewRHS,
3539 ISD::CondCode &CCCode,
3540 const SDLoc &dl) {
3541 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
3542 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
3543 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
3545 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
3546 if (RHSLo == RHSHi) {
3547 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
3548 if (RHSCST->isAllOnesValue()) {
3549 // Equality comparison to -1.
3550 NewLHS = DAG.getNode(ISD::AND, dl,
3551 LHSLo.getValueType(), LHSLo, LHSHi);
3552 NewRHS = RHSLo;
3553 return;
3558 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
3559 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
3560 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
3561 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
3562 return;
3565 // If this is a comparison of the sign bit, just look at the top part.
3566 // X > -1, x < 0
3567 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
3568 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
3569 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
3570 NewLHS = LHSHi;
3571 NewRHS = RHSHi;
3572 return;
3575 // FIXME: This generated code sucks.
3576 ISD::CondCode LowCC;
3577 switch (CCCode) {
3578 default: llvm_unreachable("Unknown integer setcc!");
3579 case ISD::SETLT:
3580 case ISD::SETULT: LowCC = ISD::SETULT; break;
3581 case ISD::SETGT:
3582 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3583 case ISD::SETLE:
3584 case ISD::SETULE: LowCC = ISD::SETULE; break;
3585 case ISD::SETGE:
3586 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3589 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
3590 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
3591 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
3593 // NOTE: on targets without efficient SELECT of bools, we can always use
3594 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3595 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
3596 nullptr);
3597 SDValue LoCmp, HiCmp;
3598 if (TLI.isTypeLegal(LHSLo.getValueType()) &&
3599 TLI.isTypeLegal(RHSLo.getValueType()))
3600 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
3601 RHSLo, LowCC, false, DagCombineInfo, dl);
3602 if (!LoCmp.getNode())
3603 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
3604 RHSLo, LowCC);
3605 if (TLI.isTypeLegal(LHSHi.getValueType()) &&
3606 TLI.isTypeLegal(RHSHi.getValueType()))
3607 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
3608 RHSHi, CCCode, false, DagCombineInfo, dl);
3609 if (!HiCmp.getNode())
3610 HiCmp =
3611 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
3612 LHSHi, RHSHi, DAG.getCondCode(CCCode));
3614 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
3615 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
3617 bool EqAllowed = (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3618 CCCode == ISD::SETUGE || CCCode == ISD::SETULE);
3620 if ((EqAllowed && (HiCmpC && HiCmpC->isNullValue())) ||
3621 (!EqAllowed && ((HiCmpC && (HiCmpC->getAPIntValue() == 1)) ||
3622 (LoCmpC && LoCmpC->isNullValue())))) {
3623 // For LE / GE, if high part is known false, ignore the low part.
3624 // For LT / GT: if low part is known false, return the high part.
3625 // if high part is known true, ignore the low part.
3626 NewLHS = HiCmp;
3627 NewRHS = SDValue();
3628 return;
3631 if (LHSHi == RHSHi) {
3632 // Comparing the low bits is enough.
3633 NewLHS = LoCmp;
3634 NewRHS = SDValue();
3635 return;
3638 // Lower with SETCCCARRY if the target supports it.
3639 EVT HiVT = LHSHi.getValueType();
3640 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
3641 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
3643 // FIXME: Make all targets support this, then remove the other lowering.
3644 if (HasSETCCCARRY) {
3645 // SETCCCARRY can detect < and >= directly. For > and <=, flip
3646 // operands and condition code.
3647 bool FlipOperands = false;
3648 switch (CCCode) {
3649 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
3650 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
3651 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
3652 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
3653 default: break;
3655 if (FlipOperands) {
3656 std::swap(LHSLo, RHSLo);
3657 std::swap(LHSHi, RHSHi);
3659 // Perform a wide subtraction, feeding the carry from the low part into
3660 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
3661 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
3662 // zero or positive iff LHS >= RHS.
3663 EVT LoVT = LHSLo.getValueType();
3664 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
3665 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
3666 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
3667 LHSHi, RHSHi, LowCmp.getValue(1),
3668 DAG.getCondCode(CCCode));
3669 NewLHS = Res;
3670 NewRHS = SDValue();
3671 return;
3674 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
3675 false, DagCombineInfo, dl);
3676 if (!NewLHS.getNode())
3677 NewLHS =
3678 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
3679 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
3680 NewRHS = SDValue();
3683 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
3684 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
3685 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
3686 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
3688 // If ExpandSetCCOperands returned a scalar, we need to compare the result
3689 // against zero to select between true and false values.
3690 if (!NewRHS.getNode()) {
3691 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
3692 CCCode = ISD::SETNE;
3695 // Update N to have the operands specified.
3696 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
3697 DAG.getCondCode(CCCode), NewLHS, NewRHS,
3698 N->getOperand(4)), 0);
3701 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
3702 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
3703 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
3704 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
3706 // If ExpandSetCCOperands returned a scalar, we need to compare the result
3707 // against zero to select between true and false values.
3708 if (!NewRHS.getNode()) {
3709 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
3710 CCCode = ISD::SETNE;
3713 // Update N to have the operands specified.
3714 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
3715 N->getOperand(2), N->getOperand(3),
3716 DAG.getCondCode(CCCode)), 0);
3719 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
3720 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
3721 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
3722 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
3724 // If ExpandSetCCOperands returned a scalar, use it.
3725 if (!NewRHS.getNode()) {
3726 assert(NewLHS.getValueType() == N->getValueType(0) &&
3727 "Unexpected setcc expansion!");
3728 return NewLHS;
3731 // Otherwise, update N to have the operands specified.
3732 return SDValue(
3733 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
3736 SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
3737 SDValue LHS = N->getOperand(0);
3738 SDValue RHS = N->getOperand(1);
3739 SDValue Carry = N->getOperand(2);
3740 SDValue Cond = N->getOperand(3);
3741 SDLoc dl = SDLoc(N);
3743 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
3744 GetExpandedInteger(LHS, LHSLo, LHSHi);
3745 GetExpandedInteger(RHS, RHSLo, RHSHi);
3747 // Expand to a SUBE for the low part and a smaller SETCCCARRY for the high.
3748 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
3749 SDValue LowCmp = DAG.getNode(ISD::SUBCARRY, dl, VTList, LHSLo, RHSLo, Carry);
3750 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
3751 LowCmp.getValue(1), Cond);
3754 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
3755 // The value being shifted is legal, but the shift amount is too big.
3756 // It follows that either the result of the shift is undefined, or the
3757 // upper half of the shift amount is zero. Just use the lower half.
3758 SDValue Lo, Hi;
3759 GetExpandedInteger(N->getOperand(1), Lo, Hi);
3760 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
3763 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
3764 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
3765 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
3766 // constant to valid type.
3767 SDValue Lo, Hi;
3768 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3769 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
3772 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
3773 SDValue Op = N->getOperand(0);
3774 EVT DstVT = N->getValueType(0);
3775 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
3776 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
3777 "Don't know how to expand this SINT_TO_FP!");
3778 TargetLowering::MakeLibCallOptions CallOptions;
3779 CallOptions.setSExt(true);
3780 return TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N)).first;
3783 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
3784 if (ISD::isNormalStore(N))
3785 return ExpandOp_NormalStore(N, OpNo);
3787 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
3788 assert(OpNo == 1 && "Can only expand the stored value so far");
3790 EVT VT = N->getOperand(1).getValueType();
3791 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3792 SDValue Ch = N->getChain();
3793 SDValue Ptr = N->getBasePtr();
3794 unsigned Alignment = N->getAlignment();
3795 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
3796 AAMDNodes AAInfo = N->getAAInfo();
3797 SDLoc dl(N);
3798 SDValue Lo, Hi;
3800 assert(NVT.isByteSized() && "Expanded type not byte sized!");
3802 if (N->getMemoryVT().bitsLE(NVT)) {
3803 GetExpandedInteger(N->getValue(), Lo, Hi);
3804 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
3805 N->getMemoryVT(), Alignment, MMOFlags, AAInfo);
3808 if (DAG.getDataLayout().isLittleEndian()) {
3809 // Little-endian - low bits are at low addresses.
3810 GetExpandedInteger(N->getValue(), Lo, Hi);
3812 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
3813 AAInfo);
3815 unsigned ExcessBits =
3816 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
3817 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
3819 // Increment the pointer to the other half.
3820 unsigned IncrementSize = NVT.getSizeInBits()/8;
3821 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3822 Hi = DAG.getTruncStore(
3823 Ch, dl, Hi, Ptr, N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
3824 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
3825 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
3828 // Big-endian - high bits are at low addresses. Favor aligned stores at
3829 // the cost of some bit-fiddling.
3830 GetExpandedInteger(N->getValue(), Lo, Hi);
3832 EVT ExtVT = N->getMemoryVT();
3833 unsigned EBytes = ExtVT.getStoreSize();
3834 unsigned IncrementSize = NVT.getSizeInBits()/8;
3835 unsigned ExcessBits = (EBytes - IncrementSize)*8;
3836 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
3837 ExtVT.getSizeInBits() - ExcessBits);
3839 if (ExcessBits < NVT.getSizeInBits()) {
3840 // Transfer high bits from the top of Lo to the bottom of Hi.
3841 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
3842 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
3843 TLI.getPointerTy(DAG.getDataLayout())));
3844 Hi = DAG.getNode(
3845 ISD::OR, dl, NVT, Hi,
3846 DAG.getNode(ISD::SRL, dl, NVT, Lo,
3847 DAG.getConstant(ExcessBits, dl,
3848 TLI.getPointerTy(DAG.getDataLayout()))));
3851 // Store both the high bits and maybe some of the low bits.
3852 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT, Alignment,
3853 MMOFlags, AAInfo);
3855 // Increment the pointer to the other half.
3856 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3857 // Store the lowest ExcessBits bits in the second half.
3858 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
3859 N->getPointerInfo().getWithOffset(IncrementSize),
3860 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
3861 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
3862 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
3865 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
3866 SDValue InL, InH;
3867 GetExpandedInteger(N->getOperand(0), InL, InH);
3868 // Just truncate the low part of the source.
3869 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
3872 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
3873 SDValue Op = N->getOperand(0);
3874 EVT SrcVT = Op.getValueType();
3875 EVT DstVT = N->getValueType(0);
3876 SDLoc dl(N);
3878 // The following optimization is valid only if every value in SrcVT (when
3879 // treated as signed) is representable in DstVT. Check that the mantissa
3880 // size of DstVT is >= than the number of bits in SrcVT -1.
3881 const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT);
3882 if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 &&
3883 TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
3884 // Do a signed conversion then adjust the result.
3885 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
3886 SignedConv = TLI.LowerOperation(SignedConv, DAG);
3888 // The result of the signed conversion needs adjusting if the 'sign bit' of
3889 // the incoming integer was set. To handle this, we dynamically test to see
3890 // if it is set, and, if so, add a fudge factor.
3892 const uint64_t F32TwoE32 = 0x4F800000ULL;
3893 const uint64_t F32TwoE64 = 0x5F800000ULL;
3894 const uint64_t F32TwoE128 = 0x7F800000ULL;
3896 APInt FF(32, 0);
3897 if (SrcVT == MVT::i32)
3898 FF = APInt(32, F32TwoE32);
3899 else if (SrcVT == MVT::i64)
3900 FF = APInt(32, F32TwoE64);
3901 else if (SrcVT == MVT::i128)
3902 FF = APInt(32, F32TwoE128);
3903 else
3904 llvm_unreachable("Unsupported UINT_TO_FP!");
3906 // Check whether the sign bit is set.
3907 SDValue Lo, Hi;
3908 GetExpandedInteger(Op, Lo, Hi);
3909 SDValue SignSet = DAG.getSetCC(dl,
3910 getSetCCResultType(Hi.getValueType()),
3912 DAG.getConstant(0, dl, Hi.getValueType()),
3913 ISD::SETLT);
3915 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
3916 SDValue FudgePtr =
3917 DAG.getConstantPool(ConstantInt::get(*DAG.getContext(), FF.zext(64)),
3918 TLI.getPointerTy(DAG.getDataLayout()));
3920 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
3921 SDValue Zero = DAG.getIntPtrConstant(0, dl);
3922 SDValue Four = DAG.getIntPtrConstant(4, dl);
3923 if (DAG.getDataLayout().isBigEndian())
3924 std::swap(Zero, Four);
3925 SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet,
3926 Zero, Four);
3927 unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
3928 FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(),
3929 FudgePtr, Offset);
3930 Alignment = std::min(Alignment, 4u);
3932 // Load the value out, extending it from f32 to the destination float type.
3933 // FIXME: Avoid the extend by constructing the right constant pool?
3934 SDValue Fudge = DAG.getExtLoad(
3935 ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(), FudgePtr,
3936 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
3937 Alignment);
3938 return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
3941 // Otherwise, use a libcall.
3942 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
3943 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
3944 "Don't know how to expand this UINT_TO_FP!");
3945 TargetLowering::MakeLibCallOptions CallOptions;
3946 CallOptions.setSExt(true);
3947 return TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, dl).first;
3950 SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
3951 SDLoc dl(N);
3952 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
3953 cast<AtomicSDNode>(N)->getMemoryVT(),
3954 N->getOperand(0),
3955 N->getOperand(1), N->getOperand(2),
3956 cast<AtomicSDNode>(N)->getMemOperand());
3957 return Swap.getValue(1);
3961 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
3963 EVT OutVT = N->getValueType(0);
3964 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
3965 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
3966 unsigned OutNumElems = OutVT.getVectorNumElements();
3967 EVT NOutVTElem = NOutVT.getVectorElementType();
3969 SDLoc dl(N);
3970 SDValue BaseIdx = N->getOperand(1);
3972 SDValue InOp0 = N->getOperand(0);
3973 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
3974 InOp0 = GetPromotedInteger(N->getOperand(0));
3976 EVT InVT = InOp0.getValueType();
3978 SmallVector<SDValue, 8> Ops;
3979 Ops.reserve(OutNumElems);
3980 for (unsigned i = 0; i != OutNumElems; ++i) {
3982 // Extract the element from the original vector.
3983 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
3984 BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
3985 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
3986 InVT.getVectorElementType(), N->getOperand(0), Index);
3988 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
3989 // Insert the converted element to the new vector.
3990 Ops.push_back(Op);
3993 return DAG.getBuildVector(NOutVT, dl, Ops);
3997 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
3998 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
3999 EVT VT = N->getValueType(0);
4000 SDLoc dl(N);
4002 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
4004 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4005 SDValue V1 = GetPromotedInteger(N->getOperand(1));
4006 EVT OutVT = V0.getValueType();
4008 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
4012 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
4013 EVT OutVT = N->getValueType(0);
4014 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4015 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4016 unsigned NumElems = N->getNumOperands();
4017 EVT NOutVTElem = NOutVT.getVectorElementType();
4019 SDLoc dl(N);
4021 SmallVector<SDValue, 8> Ops;
4022 Ops.reserve(NumElems);
4023 for (unsigned i = 0; i != NumElems; ++i) {
4024 SDValue Op;
4025 // BUILD_VECTOR integer operand types are allowed to be larger than the
4026 // result's element type. This may still be true after the promotion. For
4027 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
4028 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
4029 if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
4030 Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
4031 else
4032 Op = N->getOperand(i);
4033 Ops.push_back(Op);
4036 return DAG.getBuildVector(NOutVT, dl, Ops);
4039 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
4041 SDLoc dl(N);
4043 assert(!N->getOperand(0).getValueType().isVector() &&
4044 "Input must be a scalar");
4046 EVT OutVT = N->getValueType(0);
4047 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4048 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4049 EVT NOutVTElem = NOutVT.getVectorElementType();
4051 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
4053 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
4056 SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
4057 SDLoc dl(N);
4059 EVT OutVT = N->getValueType(0);
4060 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4061 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4063 EVT OutElemTy = NOutVT.getVectorElementType();
4065 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
4066 unsigned NumOutElem = NOutVT.getVectorNumElements();
4067 unsigned NumOperands = N->getNumOperands();
4068 assert(NumElem * NumOperands == NumOutElem &&
4069 "Unexpected number of elements");
4071 // Take the elements from the first vector.
4072 SmallVector<SDValue, 8> Ops(NumOutElem);
4073 for (unsigned i = 0; i < NumOperands; ++i) {
4074 SDValue Op = N->getOperand(i);
4075 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
4076 Op = GetPromotedInteger(Op);
4077 EVT SclrTy = Op.getValueType().getVectorElementType();
4078 assert(NumElem == Op.getValueType().getVectorNumElements() &&
4079 "Unexpected number of elements");
4081 for (unsigned j = 0; j < NumElem; ++j) {
4082 SDValue Ext = DAG.getNode(
4083 ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
4084 DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4085 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
4089 return DAG.getBuildVector(NOutVT, dl, Ops);
4092 SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
4093 EVT VT = N->getValueType(0);
4094 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4095 assert(NVT.isVector() && "This type must be promoted to a vector type");
4097 SDLoc dl(N);
4099 // For operands whose TypeAction is to promote, extend the promoted node
4100 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
4101 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
4102 // type..
4103 if (getTypeAction(N->getOperand(0).getValueType())
4104 == TargetLowering::TypePromoteInteger) {
4105 SDValue Promoted;
4107 switch(N->getOpcode()) {
4108 case ISD::SIGN_EXTEND_VECTOR_INREG:
4109 Promoted = SExtPromotedInteger(N->getOperand(0));
4110 break;
4111 case ISD::ZERO_EXTEND_VECTOR_INREG:
4112 Promoted = ZExtPromotedInteger(N->getOperand(0));
4113 break;
4114 case ISD::ANY_EXTEND_VECTOR_INREG:
4115 Promoted = GetPromotedInteger(N->getOperand(0));
4116 break;
4117 default:
4118 llvm_unreachable("Node has unexpected Opcode");
4120 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
4123 // Directly extend to the appropriate transform-to type.
4124 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
4127 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
4128 EVT OutVT = N->getValueType(0);
4129 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4130 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4132 EVT NOutVTElem = NOutVT.getVectorElementType();
4134 SDLoc dl(N);
4135 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4137 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
4138 NOutVTElem, N->getOperand(1));
4139 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
4140 V0, ConvElem, N->getOperand(2));
4143 SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
4144 // The VECREDUCE result size may be larger than the element size, so
4145 // we can simply change the result type.
4146 SDLoc dl(N);
4147 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4148 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
4151 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
4152 SDLoc dl(N);
4153 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4154 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
4155 TLI.getVectorIdxTy(DAG.getDataLayout()));
4156 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
4157 V0->getValueType(0).getScalarType(), V0, V1);
4159 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
4160 // element types. If this is the case then we need to expand the outgoing
4161 // value and not truncate it.
4162 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
4165 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
4166 SDLoc dl(N);
4167 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4168 MVT InVT = V0.getValueType().getSimpleVT();
4169 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
4170 N->getValueType(0).getVectorNumElements());
4171 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
4172 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
4175 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
4176 SDLoc dl(N);
4177 unsigned NumElems = N->getNumOperands();
4179 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
4181 SmallVector<SDValue, 8> NewOps;
4182 NewOps.reserve(NumElems);
4184 // For each incoming vector
4185 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
4186 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
4187 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
4188 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
4190 for (unsigned i=0; i<NumElem; ++i) {
4191 // Extract element from incoming vector
4192 SDValue Ex = DAG.getNode(
4193 ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
4194 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4195 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
4196 NewOps.push_back(Tr);
4200 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);