[InstCombine] Signed saturation patterns
[llvm-complete.git] / lib / CodeGen / SelectionDAG / LegalizeIntegerTypes.cpp
blobd5c1b539adbdefb84e790e7769dc7886a1511cea
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::SPLAT_VECTOR:
104 Res = PromoteIntRes_SPLAT_VECTOR(N); break;
105 case ISD::CONCAT_VECTORS:
106 Res = PromoteIntRes_CONCAT_VECTORS(N); break;
108 case ISD::ANY_EXTEND_VECTOR_INREG:
109 case ISD::SIGN_EXTEND_VECTOR_INREG:
110 case ISD::ZERO_EXTEND_VECTOR_INREG:
111 Res = PromoteIntRes_EXTEND_VECTOR_INREG(N); break;
113 case ISD::SIGN_EXTEND:
114 case ISD::ZERO_EXTEND:
115 case ISD::ANY_EXTEND: Res = PromoteIntRes_INT_EXTEND(N); break;
117 case ISD::STRICT_FP_TO_SINT:
118 case ISD::STRICT_FP_TO_UINT:
119 case ISD::FP_TO_SINT:
120 case ISD::FP_TO_UINT: Res = PromoteIntRes_FP_TO_XINT(N); break;
122 case ISD::FP_TO_FP16: Res = PromoteIntRes_FP_TO_FP16(N); break;
124 case ISD::FLT_ROUNDS_: Res = PromoteIntRes_FLT_ROUNDS(N); break;
126 case ISD::AND:
127 case ISD::OR:
128 case ISD::XOR:
129 case ISD::ADD:
130 case ISD::SUB:
131 case ISD::MUL: Res = PromoteIntRes_SimpleIntBinOp(N); break;
133 case ISD::SDIV:
134 case ISD::SREM: Res = PromoteIntRes_SExtIntBinOp(N); break;
136 case ISD::UDIV:
137 case ISD::UREM: Res = PromoteIntRes_ZExtIntBinOp(N); break;
139 case ISD::SADDO:
140 case ISD::SSUBO: Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
141 case ISD::UADDO:
142 case ISD::USUBO: Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
143 case ISD::SMULO:
144 case ISD::UMULO: Res = PromoteIntRes_XMULO(N, ResNo); break;
146 case ISD::ADDE:
147 case ISD::SUBE:
148 case ISD::ADDCARRY:
149 case ISD::SUBCARRY: Res = PromoteIntRes_ADDSUBCARRY(N, ResNo); break;
151 case ISD::SADDSAT:
152 case ISD::UADDSAT:
153 case ISD::SSUBSAT:
154 case ISD::USUBSAT: Res = PromoteIntRes_ADDSUBSAT(N); break;
156 case ISD::SMULFIX:
157 case ISD::SMULFIXSAT:
158 case ISD::UMULFIX:
159 case ISD::UMULFIXSAT: Res = PromoteIntRes_MULFIX(N); break;
161 case ISD::ABS: Res = PromoteIntRes_ABS(N); break;
163 case ISD::ATOMIC_LOAD:
164 Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
166 case ISD::ATOMIC_LOAD_ADD:
167 case ISD::ATOMIC_LOAD_SUB:
168 case ISD::ATOMIC_LOAD_AND:
169 case ISD::ATOMIC_LOAD_CLR:
170 case ISD::ATOMIC_LOAD_OR:
171 case ISD::ATOMIC_LOAD_XOR:
172 case ISD::ATOMIC_LOAD_NAND:
173 case ISD::ATOMIC_LOAD_MIN:
174 case ISD::ATOMIC_LOAD_MAX:
175 case ISD::ATOMIC_LOAD_UMIN:
176 case ISD::ATOMIC_LOAD_UMAX:
177 case ISD::ATOMIC_SWAP:
178 Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
180 case ISD::ATOMIC_CMP_SWAP:
181 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
182 Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
183 break;
185 case ISD::VECREDUCE_ADD:
186 case ISD::VECREDUCE_MUL:
187 case ISD::VECREDUCE_AND:
188 case ISD::VECREDUCE_OR:
189 case ISD::VECREDUCE_XOR:
190 case ISD::VECREDUCE_SMAX:
191 case ISD::VECREDUCE_SMIN:
192 case ISD::VECREDUCE_UMAX:
193 case ISD::VECREDUCE_UMIN:
194 Res = PromoteIntRes_VECREDUCE(N);
195 break;
198 // If the result is null then the sub-method took care of registering it.
199 if (Res.getNode())
200 SetPromotedInteger(SDValue(N, ResNo), Res);
203 SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
204 unsigned ResNo) {
205 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
206 return GetPromotedInteger(Op);
209 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
210 // Sign-extend the new bits, and continue the assertion.
211 SDValue Op = SExtPromotedInteger(N->getOperand(0));
212 return DAG.getNode(ISD::AssertSext, SDLoc(N),
213 Op.getValueType(), Op, N->getOperand(1));
216 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
217 // Zero the new bits, and continue the assertion.
218 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
219 return DAG.getNode(ISD::AssertZext, SDLoc(N),
220 Op.getValueType(), Op, N->getOperand(1));
223 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
224 EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
225 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
226 N->getMemoryVT(), ResVT,
227 N->getChain(), N->getBasePtr(),
228 N->getMemOperand());
229 // Legalize the chain result - switch anything that used the old chain to
230 // use the new one.
231 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
232 return Res;
235 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
236 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
237 SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
238 N->getMemoryVT(),
239 N->getChain(), N->getBasePtr(),
240 Op2, N->getMemOperand());
241 // Legalize the chain result - switch anything that used the old chain to
242 // use the new one.
243 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
244 return Res;
247 SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
248 unsigned ResNo) {
249 if (ResNo == 1) {
250 assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
251 EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
252 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
254 // Only use the result of getSetCCResultType if it is legal,
255 // otherwise just use the promoted result type (NVT).
256 if (!TLI.isTypeLegal(SVT))
257 SVT = NVT;
259 SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
260 SDValue Res = DAG.getAtomicCmpSwap(
261 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
262 N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
263 N->getMemOperand());
264 ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
265 ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
266 return Res.getValue(1);
269 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
270 SDValue Op3 = GetPromotedInteger(N->getOperand(3));
271 SDVTList VTs =
272 DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
273 SDValue Res = DAG.getAtomicCmpSwap(
274 N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
275 N->getBasePtr(), Op2, Op3, N->getMemOperand());
276 // Update the use to N with the newly created Res.
277 for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
278 ReplaceValueWith(SDValue(N, i), Res.getValue(i));
279 return Res;
282 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
283 SDValue InOp = N->getOperand(0);
284 EVT InVT = InOp.getValueType();
285 EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
286 EVT OutVT = N->getValueType(0);
287 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
288 SDLoc dl(N);
290 switch (getTypeAction(InVT)) {
291 case TargetLowering::TypeLegal:
292 break;
293 case TargetLowering::TypePromoteInteger:
294 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
295 // The input promotes to the same size. Convert the promoted value.
296 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
297 break;
298 case TargetLowering::TypeSoftenFloat:
299 // Promote the integer operand by hand.
300 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
301 case TargetLowering::TypePromoteFloat: {
302 // Convert the promoted float by hand.
303 if (!NOutVT.isVector())
304 return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, GetPromotedFloat(InOp));
305 break;
307 case TargetLowering::TypeExpandInteger:
308 case TargetLowering::TypeExpandFloat:
309 break;
310 case TargetLowering::TypeScalarizeVector:
311 // Convert the element to an integer and promote it by hand.
312 if (!NOutVT.isVector())
313 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
314 BitConvertToInteger(GetScalarizedVector(InOp)));
315 break;
316 case TargetLowering::TypeSplitVector: {
317 if (!NOutVT.isVector()) {
318 // For example, i32 = BITCAST v2i16 on alpha. Convert the split
319 // pieces of the input into integers and reassemble in the final type.
320 SDValue Lo, Hi;
321 GetSplitVector(N->getOperand(0), Lo, Hi);
322 Lo = BitConvertToInteger(Lo);
323 Hi = BitConvertToInteger(Hi);
325 if (DAG.getDataLayout().isBigEndian())
326 std::swap(Lo, Hi);
328 InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
329 EVT::getIntegerVT(*DAG.getContext(),
330 NOutVT.getSizeInBits()),
331 JoinIntegers(Lo, Hi));
332 return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
334 break;
336 case TargetLowering::TypeWidenVector:
337 // The input is widened to the same size. Convert to the widened value.
338 // Make sure that the outgoing value is not a vector, because this would
339 // make us bitcast between two vectors which are legalized in different ways.
340 if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector())
341 return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
342 // If the output type is also a vector and widening it to the same size
343 // as the widened input type would be a legal type, we can widen the bitcast
344 // and handle the promotion after.
345 if (NOutVT.isVector()) {
346 unsigned WidenInSize = NInVT.getSizeInBits();
347 unsigned OutSize = OutVT.getSizeInBits();
348 if (WidenInSize % OutSize == 0) {
349 unsigned Scale = WidenInSize / OutSize;
350 EVT WideOutVT = EVT::getVectorVT(*DAG.getContext(),
351 OutVT.getVectorElementType(),
352 OutVT.getVectorNumElements() * Scale);
353 if (isTypeLegal(WideOutVT)) {
354 InOp = DAG.getBitcast(WideOutVT, GetWidenedVector(InOp));
355 MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
356 InOp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, InOp,
357 DAG.getConstant(0, dl, IdxTy));
358 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, InOp);
364 return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
365 CreateStackStoreLoad(InOp, OutVT));
368 // Helper for BSWAP/BITREVERSE promotion to ensure we can fit the shift amount
369 // in the VT returned by getShiftAmountTy and to return a safe VT if we can't.
370 static EVT getShiftAmountTyForConstant(unsigned Val, EVT VT,
371 const TargetLowering &TLI,
372 SelectionDAG &DAG) {
373 EVT ShiftVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
374 // If the value won't fit in the prefered type, just use something safe. It
375 // will be legalized when the shift is expanded.
376 if ((Log2_32(Val) + 1) > ShiftVT.getScalarSizeInBits())
377 ShiftVT = MVT::i32;
378 return ShiftVT;
381 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
382 SDValue Op = GetPromotedInteger(N->getOperand(0));
383 EVT OVT = N->getValueType(0);
384 EVT NVT = Op.getValueType();
385 SDLoc dl(N);
387 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
388 EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
389 return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
390 DAG.getConstant(DiffBits, dl, ShiftVT));
393 SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
394 SDValue Op = GetPromotedInteger(N->getOperand(0));
395 EVT OVT = N->getValueType(0);
396 EVT NVT = Op.getValueType();
397 SDLoc dl(N);
399 unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
400 EVT ShiftVT = getShiftAmountTyForConstant(DiffBits, NVT, TLI, DAG);
401 return DAG.getNode(ISD::SRL, dl, NVT,
402 DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
403 DAG.getConstant(DiffBits, dl, ShiftVT));
406 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
407 // The pair element type may be legal, or may not promote to the same type as
408 // the result, for example i14 = BUILD_PAIR (i7, i7). Handle all cases.
409 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
410 TLI.getTypeToTransformTo(*DAG.getContext(),
411 N->getValueType(0)), JoinIntegers(N->getOperand(0),
412 N->getOperand(1)));
415 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
416 EVT VT = N->getValueType(0);
417 // FIXME there is no actual debug info here
418 SDLoc dl(N);
419 // Zero extend things like i1, sign extend everything else. It shouldn't
420 // matter in theory which one we pick, but this tends to give better code?
421 unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
422 SDValue Result = DAG.getNode(Opc, dl,
423 TLI.getTypeToTransformTo(*DAG.getContext(), VT),
424 SDValue(N, 0));
425 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
426 return Result;
429 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
430 // Zero extend to the promoted type and do the count there.
431 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
432 SDLoc dl(N);
433 EVT OVT = N->getValueType(0);
434 EVT NVT = Op.getValueType();
435 Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
436 // Subtract off the extra leading bits in the bigger type.
437 return DAG.getNode(
438 ISD::SUB, dl, NVT, Op,
439 DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
440 NVT));
443 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
444 // Zero extend to the promoted type and do the count there.
445 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
446 return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op);
449 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
450 SDValue Op = GetPromotedInteger(N->getOperand(0));
451 EVT OVT = N->getValueType(0);
452 EVT NVT = Op.getValueType();
453 SDLoc dl(N);
454 if (N->getOpcode() == ISD::CTTZ) {
455 // The count is the same in the promoted type except if the original
456 // value was zero. This can be handled by setting the bit just off
457 // the top of the original type.
458 auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
459 OVT.getScalarSizeInBits());
460 Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
462 return DAG.getNode(N->getOpcode(), dl, NVT, Op);
465 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
466 SDLoc dl(N);
467 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
469 SDValue Op0 = N->getOperand(0);
470 SDValue Op1 = N->getOperand(1);
472 // If the input also needs to be promoted, do that first so we can get a
473 // get a good idea for the output type.
474 if (TLI.getTypeAction(*DAG.getContext(), Op0.getValueType())
475 == TargetLowering::TypePromoteInteger) {
476 SDValue In = GetPromotedInteger(Op0);
478 // If the new type is larger than NVT, use it. We probably won't need to
479 // promote it again.
480 EVT SVT = In.getValueType().getScalarType();
481 if (SVT.bitsGE(NVT)) {
482 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, In, Op1);
483 return DAG.getAnyExtOrTrunc(Ext, dl, NVT);
487 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, Op0, Op1);
490 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
491 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
492 unsigned NewOpc = N->getOpcode();
493 SDLoc dl(N);
495 // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
496 // not Legal, check to see if we can use FP_TO_SINT instead. (If both UINT
497 // and SINT conversions are Custom, there is no way to tell which is
498 // preferable. We choose SINT because that's the right thing on PPC.)
499 if (N->getOpcode() == ISD::FP_TO_UINT &&
500 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
501 TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
502 NewOpc = ISD::FP_TO_SINT;
504 if (N->getOpcode() == ISD::STRICT_FP_TO_UINT &&
505 !TLI.isOperationLegal(ISD::STRICT_FP_TO_UINT, NVT) &&
506 TLI.isOperationLegalOrCustom(ISD::STRICT_FP_TO_SINT, NVT))
507 NewOpc = ISD::STRICT_FP_TO_SINT;
509 SDValue Res;
510 if (N->isStrictFPOpcode()) {
511 Res = DAG.getNode(NewOpc, dl, { NVT, MVT::Other },
512 { N->getOperand(0), N->getOperand(1) });
513 // Legalize the chain result - switch anything that used the old chain to
514 // use the new one.
515 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
516 } else
517 Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
519 // Assert that the converted value fits in the original type. If it doesn't
520 // (eg: because the value being converted is too big), then the result of the
521 // original operation was undefined anyway, so the assert is still correct.
523 // NOTE: fp-to-uint to fp-to-sint promotion guarantees zero extend. For example:
524 // before legalization: fp-to-uint16, 65534. -> 0xfffe
525 // after legalization: fp-to-sint32, 65534. -> 0x0000fffe
526 return DAG.getNode((N->getOpcode() == ISD::FP_TO_UINT ||
527 N->getOpcode() == ISD::STRICT_FP_TO_UINT) ?
528 ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
529 DAG.getValueType(N->getValueType(0).getScalarType()));
532 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
533 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
534 SDLoc dl(N);
536 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
539 SDValue DAGTypeLegalizer::PromoteIntRes_FLT_ROUNDS(SDNode *N) {
540 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
541 SDLoc dl(N);
543 return DAG.getNode(N->getOpcode(), dl, NVT);
546 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
547 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
548 SDLoc dl(N);
550 if (getTypeAction(N->getOperand(0).getValueType())
551 == TargetLowering::TypePromoteInteger) {
552 SDValue Res = GetPromotedInteger(N->getOperand(0));
553 assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
555 // If the result and operand types are the same after promotion, simplify
556 // to an in-register extension.
557 if (NVT == Res.getValueType()) {
558 // The high bits are not guaranteed to be anything. Insert an extend.
559 if (N->getOpcode() == ISD::SIGN_EXTEND)
560 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
561 DAG.getValueType(N->getOperand(0).getValueType()));
562 if (N->getOpcode() == ISD::ZERO_EXTEND)
563 return DAG.getZeroExtendInReg(Res, dl,
564 N->getOperand(0).getValueType().getScalarType());
565 assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
566 return Res;
570 // Otherwise, just extend the original operand all the way to the larger type.
571 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
574 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
575 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
576 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
577 ISD::LoadExtType ExtType =
578 ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
579 SDLoc dl(N);
580 SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
581 N->getMemoryVT(), N->getMemOperand());
583 // Legalize the chain result - switch anything that used the old chain to
584 // use the new one.
585 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
586 return Res;
589 SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
590 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
591 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
593 SDLoc dl(N);
594 SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
595 N->getMask(), ExtPassThru, N->getMemoryVT(),
596 N->getMemOperand(), ISD::EXTLOAD);
597 // Legalize the chain result - switch anything that used the old chain to
598 // use the new one.
599 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
600 return Res;
603 SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
604 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
605 SDValue ExtPassThru = GetPromotedInteger(N->getPassThru());
606 assert(NVT == ExtPassThru.getValueType() &&
607 "Gather result type and the passThru agrument type should be the same");
609 SDLoc dl(N);
610 SDValue Ops[] = {N->getChain(), ExtPassThru, N->getMask(), N->getBasePtr(),
611 N->getIndex(), N->getScale() };
612 SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
613 N->getMemoryVT(), dl, Ops,
614 N->getMemOperand(), N->getIndexType());
615 // Legalize the chain result - switch anything that used the old chain to
616 // use the new one.
617 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
618 return Res;
621 /// Promote the overflow flag of an overflowing arithmetic node.
622 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
623 // Change the return type of the boolean result while obeying
624 // getSetCCResultType.
625 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
626 EVT VT = N->getValueType(0);
627 EVT SVT = getSetCCResultType(VT);
628 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1) };
629 unsigned NumOps = N->getNumOperands();
630 assert(NumOps <= 3 && "Too many operands");
631 if (NumOps == 3)
632 Ops[2] = N->getOperand(2);
634 SDLoc dl(N);
635 SDValue Res = DAG.getNode(N->getOpcode(), dl, DAG.getVTList(VT, SVT),
636 makeArrayRef(Ops, NumOps));
638 // Modified the sum result - switch anything that used the old sum to use
639 // the new one.
640 ReplaceValueWith(SDValue(N, 0), Res);
642 // Convert to the expected type.
643 return DAG.getBoolExtOrTrunc(Res.getValue(1), dl, NVT, VT);
646 SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBSAT(SDNode *N) {
647 // If the promoted type is legal, we can convert this to:
648 // 1. ANY_EXTEND iN to iM
649 // 2. SHL by M-N
650 // 3. [US][ADD|SUB]SAT
651 // 4. L/ASHR by M-N
652 // Else it is more efficient to convert this to a min and a max
653 // operation in the higher precision arithmetic.
654 SDLoc dl(N);
655 SDValue Op1 = N->getOperand(0);
656 SDValue Op2 = N->getOperand(1);
657 unsigned OldBits = Op1.getScalarValueSizeInBits();
659 unsigned Opcode = N->getOpcode();
661 SDValue Op1Promoted, Op2Promoted;
662 if (Opcode == ISD::UADDSAT || Opcode == ISD::USUBSAT) {
663 Op1Promoted = ZExtPromotedInteger(Op1);
664 Op2Promoted = ZExtPromotedInteger(Op2);
665 } else {
666 Op1Promoted = SExtPromotedInteger(Op1);
667 Op2Promoted = SExtPromotedInteger(Op2);
669 EVT PromotedType = Op1Promoted.getValueType();
670 unsigned NewBits = PromotedType.getScalarSizeInBits();
672 if (TLI.isOperationLegalOrCustom(Opcode, PromotedType)) {
673 unsigned ShiftOp;
674 switch (Opcode) {
675 case ISD::SADDSAT:
676 case ISD::SSUBSAT:
677 ShiftOp = ISD::SRA;
678 break;
679 case ISD::UADDSAT:
680 case ISD::USUBSAT:
681 ShiftOp = ISD::SRL;
682 break;
683 default:
684 llvm_unreachable("Expected opcode to be signed or unsigned saturation "
685 "addition or subtraction");
688 unsigned SHLAmount = NewBits - OldBits;
689 EVT SHVT = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
690 SDValue ShiftAmount = DAG.getConstant(SHLAmount, dl, SHVT);
691 Op1Promoted =
692 DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted, ShiftAmount);
693 Op2Promoted =
694 DAG.getNode(ISD::SHL, dl, PromotedType, Op2Promoted, ShiftAmount);
696 SDValue Result =
697 DAG.getNode(Opcode, dl, PromotedType, Op1Promoted, Op2Promoted);
698 return DAG.getNode(ShiftOp, dl, PromotedType, Result, ShiftAmount);
699 } else {
700 if (Opcode == ISD::USUBSAT) {
701 SDValue Max =
702 DAG.getNode(ISD::UMAX, dl, PromotedType, Op1Promoted, Op2Promoted);
703 return DAG.getNode(ISD::SUB, dl, PromotedType, Max, Op2Promoted);
706 if (Opcode == ISD::UADDSAT) {
707 APInt MaxVal = APInt::getAllOnesValue(OldBits).zext(NewBits);
708 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
709 SDValue Add =
710 DAG.getNode(ISD::ADD, dl, PromotedType, Op1Promoted, Op2Promoted);
711 return DAG.getNode(ISD::UMIN, dl, PromotedType, Add, SatMax);
714 unsigned AddOp = Opcode == ISD::SADDSAT ? ISD::ADD : ISD::SUB;
715 APInt MinVal = APInt::getSignedMinValue(OldBits).sext(NewBits);
716 APInt MaxVal = APInt::getSignedMaxValue(OldBits).sext(NewBits);
717 SDValue SatMin = DAG.getConstant(MinVal, dl, PromotedType);
718 SDValue SatMax = DAG.getConstant(MaxVal, dl, PromotedType);
719 SDValue Result =
720 DAG.getNode(AddOp, dl, PromotedType, Op1Promoted, Op2Promoted);
721 Result = DAG.getNode(ISD::SMIN, dl, PromotedType, Result, SatMax);
722 Result = DAG.getNode(ISD::SMAX, dl, PromotedType, Result, SatMin);
723 return Result;
727 SDValue DAGTypeLegalizer::PromoteIntRes_MULFIX(SDNode *N) {
728 // Can just promote the operands then continue with operation.
729 SDLoc dl(N);
730 SDValue Op1Promoted, Op2Promoted;
731 bool Signed =
732 N->getOpcode() == ISD::SMULFIX || N->getOpcode() == ISD::SMULFIXSAT;
733 bool Saturating =
734 N->getOpcode() == ISD::SMULFIXSAT || N->getOpcode() == ISD::UMULFIXSAT;
735 if (Signed) {
736 Op1Promoted = SExtPromotedInteger(N->getOperand(0));
737 Op2Promoted = SExtPromotedInteger(N->getOperand(1));
738 } else {
739 Op1Promoted = ZExtPromotedInteger(N->getOperand(0));
740 Op2Promoted = ZExtPromotedInteger(N->getOperand(1));
742 EVT OldType = N->getOperand(0).getValueType();
743 EVT PromotedType = Op1Promoted.getValueType();
744 unsigned DiffSize =
745 PromotedType.getScalarSizeInBits() - OldType.getScalarSizeInBits();
747 if (Saturating) {
748 // Promoting the operand and result values changes the saturation width,
749 // which is extends the values that we clamp to on saturation. This could be
750 // resolved by shifting one of the operands the same amount, which would
751 // also shift the result we compare against, then shifting back.
752 EVT ShiftTy = TLI.getShiftAmountTy(PromotedType, DAG.getDataLayout());
753 Op1Promoted = DAG.getNode(ISD::SHL, dl, PromotedType, Op1Promoted,
754 DAG.getConstant(DiffSize, dl, ShiftTy));
755 SDValue Result = DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted,
756 Op2Promoted, N->getOperand(2));
757 unsigned ShiftOp = Signed ? ISD::SRA : ISD::SRL;
758 return DAG.getNode(ShiftOp, dl, PromotedType, Result,
759 DAG.getConstant(DiffSize, dl, ShiftTy));
761 return DAG.getNode(N->getOpcode(), dl, PromotedType, Op1Promoted, Op2Promoted,
762 N->getOperand(2));
765 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
766 if (ResNo == 1)
767 return PromoteIntRes_Overflow(N);
769 // The operation overflowed iff the result in the larger type is not the
770 // sign extension of its truncation to the original type.
771 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
772 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
773 EVT OVT = N->getOperand(0).getValueType();
774 EVT NVT = LHS.getValueType();
775 SDLoc dl(N);
777 // Do the arithmetic in the larger type.
778 unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
779 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
781 // Calculate the overflow flag: sign extend the arithmetic result from
782 // the original type.
783 SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
784 DAG.getValueType(OVT));
785 // Overflowed if and only if this is not equal to Res.
786 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
788 // Use the calculated overflow everywhere.
789 ReplaceValueWith(SDValue(N, 1), Ofl);
791 return Res;
794 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
795 SDValue LHS = GetPromotedInteger(N->getOperand(1));
796 SDValue RHS = GetPromotedInteger(N->getOperand(2));
797 return DAG.getSelect(SDLoc(N),
798 LHS.getValueType(), N->getOperand(0), LHS, RHS);
801 SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
802 SDValue Mask = N->getOperand(0);
804 SDValue LHS = GetPromotedInteger(N->getOperand(1));
805 SDValue RHS = GetPromotedInteger(N->getOperand(2));
806 return DAG.getNode(ISD::VSELECT, SDLoc(N),
807 LHS.getValueType(), Mask, LHS, RHS);
810 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
811 SDValue LHS = GetPromotedInteger(N->getOperand(2));
812 SDValue RHS = GetPromotedInteger(N->getOperand(3));
813 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
814 LHS.getValueType(), N->getOperand(0),
815 N->getOperand(1), LHS, RHS, N->getOperand(4));
818 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
819 EVT InVT = N->getOperand(0).getValueType();
820 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
822 EVT SVT = getSetCCResultType(InVT);
824 // If we got back a type that needs to be promoted, this likely means the
825 // the input type also needs to be promoted. So get the promoted type for
826 // the input and try the query again.
827 if (getTypeAction(SVT) == TargetLowering::TypePromoteInteger) {
828 if (getTypeAction(InVT) == TargetLowering::TypePromoteInteger) {
829 InVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
830 SVT = getSetCCResultType(InVT);
831 } else {
832 // Input type isn't promoted, just use the default promoted type.
833 SVT = NVT;
837 SDLoc dl(N);
838 assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() &&
839 "Vector compare must return a vector result!");
841 // Get the SETCC result using the canonical SETCC type.
842 SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, N->getOperand(0),
843 N->getOperand(1), N->getOperand(2));
845 // Convert to the expected type.
846 return DAG.getSExtOrTrunc(SetCC, dl, NVT);
849 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
850 SDValue LHS = GetPromotedInteger(N->getOperand(0));
851 SDValue RHS = N->getOperand(1);
852 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
853 RHS = ZExtPromotedInteger(RHS);
854 return DAG.getNode(ISD::SHL, SDLoc(N), LHS.getValueType(), LHS, RHS);
857 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
858 SDValue Op = GetPromotedInteger(N->getOperand(0));
859 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
860 Op.getValueType(), Op, N->getOperand(1));
863 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
864 // The input may have strange things in the top bits of the registers, but
865 // these operations don't care. They may have weird bits going out, but
866 // that too is okay if they are integer operations.
867 SDValue LHS = GetPromotedInteger(N->getOperand(0));
868 SDValue RHS = GetPromotedInteger(N->getOperand(1));
869 return DAG.getNode(N->getOpcode(), SDLoc(N),
870 LHS.getValueType(), LHS, RHS);
873 SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
874 // Sign extend the input.
875 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
876 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
877 return DAG.getNode(N->getOpcode(), SDLoc(N),
878 LHS.getValueType(), LHS, RHS);
881 SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
882 // Zero extend the input.
883 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
884 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
885 return DAG.getNode(N->getOpcode(), SDLoc(N),
886 LHS.getValueType(), LHS, RHS);
889 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
890 // The input value must be properly sign extended.
891 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
892 SDValue RHS = N->getOperand(1);
893 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
894 RHS = ZExtPromotedInteger(RHS);
895 return DAG.getNode(ISD::SRA, SDLoc(N), LHS.getValueType(), LHS, RHS);
898 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
899 // The input value must be properly zero extended.
900 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
901 SDValue RHS = N->getOperand(1);
902 if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
903 RHS = ZExtPromotedInteger(RHS);
904 return DAG.getNode(ISD::SRL, SDLoc(N), LHS.getValueType(), LHS, RHS);
907 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
908 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
909 SDValue Res;
910 SDValue InOp = N->getOperand(0);
911 SDLoc dl(N);
913 switch (getTypeAction(InOp.getValueType())) {
914 default: llvm_unreachable("Unknown type action!");
915 case TargetLowering::TypeLegal:
916 case TargetLowering::TypeExpandInteger:
917 Res = InOp;
918 break;
919 case TargetLowering::TypePromoteInteger:
920 Res = GetPromotedInteger(InOp);
921 break;
922 case TargetLowering::TypeSplitVector: {
923 EVT InVT = InOp.getValueType();
924 assert(InVT.isVector() && "Cannot split scalar types");
925 unsigned NumElts = InVT.getVectorNumElements();
926 assert(NumElts == NVT.getVectorNumElements() &&
927 "Dst and Src must have the same number of elements");
928 assert(isPowerOf2_32(NumElts) &&
929 "Promoted vector type must be a power of two");
931 SDValue EOp1, EOp2;
932 GetSplitVector(InOp, EOp1, EOp2);
934 EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
935 NumElts/2);
936 EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
937 EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
939 return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
941 case TargetLowering::TypeWidenVector: {
942 SDValue WideInOp = GetWidenedVector(InOp);
944 // Truncate widened InOp.
945 unsigned NumElem = WideInOp.getValueType().getVectorNumElements();
946 EVT TruncVT = EVT::getVectorVT(*DAG.getContext(),
947 N->getValueType(0).getScalarType(), NumElem);
948 SDValue WideTrunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, WideInOp);
950 // Zero extend so that the elements are of same type as those of NVT
951 EVT ExtVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
952 NumElem);
953 SDValue WideExt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, WideTrunc);
955 // Extract the low NVT subvector.
956 MVT IdxTy = TLI.getVectorIdxTy(DAG.getDataLayout());
957 SDValue ZeroIdx = DAG.getConstant(0, dl, IdxTy);
958 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT, WideExt, ZeroIdx);
962 // Truncate to NVT instead of VT
963 return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
966 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
967 if (ResNo == 1)
968 return PromoteIntRes_Overflow(N);
970 // The operation overflowed iff the result in the larger type is not the
971 // zero extension of its truncation to the original type.
972 SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
973 SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
974 EVT OVT = N->getOperand(0).getValueType();
975 EVT NVT = LHS.getValueType();
976 SDLoc dl(N);
978 // Do the arithmetic in the larger type.
979 unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
980 SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
982 // Calculate the overflow flag: zero extend the arithmetic result from
983 // the original type.
984 SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT.getScalarType());
985 // Overflowed if and only if this is not equal to Res.
986 Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
988 // Use the calculated overflow everywhere.
989 ReplaceValueWith(SDValue(N, 1), Ofl);
991 return Res;
994 // Handle promotion for the ADDE/SUBE/ADDCARRY/SUBCARRY nodes. Notice that
995 // the third operand of ADDE/SUBE nodes is carry flag, which differs from
996 // the ADDCARRY/SUBCARRY nodes in that the third operand is carry Boolean.
997 SDValue DAGTypeLegalizer::PromoteIntRes_ADDSUBCARRY(SDNode *N, unsigned ResNo) {
998 if (ResNo == 1)
999 return PromoteIntRes_Overflow(N);
1001 // We need to sign-extend the operands so the carry value computed by the
1002 // wide operation will be equivalent to the carry value computed by the
1003 // narrow operation.
1004 // An ADDCARRY can generate carry only if any of the operands has its
1005 // most significant bit set. Sign extension propagates the most significant
1006 // bit into the higher bits which means the extra bit that the narrow
1007 // addition would need (i.e. the carry) will be propagated through the higher
1008 // bits of the wide addition.
1009 // A SUBCARRY can generate borrow only if LHS < RHS and this property will be
1010 // preserved by sign extension.
1011 SDValue LHS = SExtPromotedInteger(N->getOperand(0));
1012 SDValue RHS = SExtPromotedInteger(N->getOperand(1));
1014 EVT ValueVTs[] = {LHS.getValueType(), N->getValueType(1)};
1016 // Do the arithmetic in the wide type.
1017 SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N), DAG.getVTList(ValueVTs),
1018 LHS, RHS, N->getOperand(2));
1020 // Update the users of the original carry/borrow value.
1021 ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
1023 return SDValue(Res.getNode(), 0);
1026 SDValue DAGTypeLegalizer::PromoteIntRes_ABS(SDNode *N) {
1027 SDValue Op0 = SExtPromotedInteger(N->getOperand(0));
1028 return DAG.getNode(ISD::ABS, SDLoc(N), Op0.getValueType(), Op0);
1031 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
1032 // Promote the overflow bit trivially.
1033 if (ResNo == 1)
1034 return PromoteIntRes_Overflow(N);
1036 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
1037 SDLoc DL(N);
1038 EVT SmallVT = LHS.getValueType();
1040 // To determine if the result overflowed in a larger type, we extend the
1041 // input to the larger type, do the multiply (checking if it overflows),
1042 // then also check the high bits of the result to see if overflow happened
1043 // there.
1044 if (N->getOpcode() == ISD::SMULO) {
1045 LHS = SExtPromotedInteger(LHS);
1046 RHS = SExtPromotedInteger(RHS);
1047 } else {
1048 LHS = ZExtPromotedInteger(LHS);
1049 RHS = ZExtPromotedInteger(RHS);
1051 SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
1052 SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
1054 // Overflow occurred if it occurred in the larger type, or if the high part
1055 // of the result does not zero/sign-extend the low part. Check this second
1056 // possibility first.
1057 SDValue Overflow;
1058 if (N->getOpcode() == ISD::UMULO) {
1059 // Unsigned overflow occurred if the high part is non-zero.
1060 unsigned Shift = SmallVT.getScalarSizeInBits();
1061 EVT ShiftTy = getShiftAmountTyForConstant(Shift, Mul.getValueType(),
1062 TLI, DAG);
1063 SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
1064 DAG.getConstant(Shift, DL, ShiftTy));
1065 Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
1066 DAG.getConstant(0, DL, Hi.getValueType()),
1067 ISD::SETNE);
1068 } else {
1069 // Signed overflow occurred if the high part does not sign extend the low.
1070 SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
1071 Mul, DAG.getValueType(SmallVT));
1072 Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
1075 // The only other way for overflow to occur is if the multiplication in the
1076 // larger type itself overflowed.
1077 Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
1078 SDValue(Mul.getNode(), 1));
1080 // Use the calculated overflow everywhere.
1081 ReplaceValueWith(SDValue(N, 1), Overflow);
1082 return Mul;
1085 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
1086 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
1087 N->getValueType(0)));
1090 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
1091 SDValue Chain = N->getOperand(0); // Get the chain.
1092 SDValue Ptr = N->getOperand(1); // Get the pointer.
1093 EVT VT = N->getValueType(0);
1094 SDLoc dl(N);
1096 MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
1097 unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
1098 // The argument is passed as NumRegs registers of type RegVT.
1100 SmallVector<SDValue, 8> Parts(NumRegs);
1101 for (unsigned i = 0; i < NumRegs; ++i) {
1102 Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
1103 N->getConstantOperandVal(3));
1104 Chain = Parts[i].getValue(1);
1107 // Handle endianness of the load.
1108 if (DAG.getDataLayout().isBigEndian())
1109 std::reverse(Parts.begin(), Parts.end());
1111 // Assemble the parts in the promoted type.
1112 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1113 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
1114 for (unsigned i = 1; i < NumRegs; ++i) {
1115 SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
1116 // Shift it to the right position and "or" it in.
1117 Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
1118 DAG.getConstant(i * RegVT.getSizeInBits(), dl,
1119 TLI.getPointerTy(DAG.getDataLayout())));
1120 Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
1123 // Modified the chain result - switch anything that used the old chain to
1124 // use the new one.
1125 ReplaceValueWith(SDValue(N, 1), Chain);
1127 return Res;
1130 //===----------------------------------------------------------------------===//
1131 // Integer Operand Promotion
1132 //===----------------------------------------------------------------------===//
1134 /// PromoteIntegerOperand - This method is called when the specified operand of
1135 /// the specified node is found to need promotion. At this point, all of the
1136 /// result types of the node are known to be legal, but other operands of the
1137 /// node may need promotion or expansion as well as the specified one.
1138 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
1139 LLVM_DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG);
1140 dbgs() << "\n");
1141 SDValue Res = SDValue();
1143 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false)) {
1144 LLVM_DEBUG(dbgs() << "Node has been custom lowered, done\n");
1145 return false;
1148 switch (N->getOpcode()) {
1149 default:
1150 #ifndef NDEBUG
1151 dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
1152 N->dump(&DAG); dbgs() << "\n";
1153 #endif
1154 llvm_unreachable("Do not know how to promote this operator's operand!");
1156 case ISD::ANY_EXTEND: Res = PromoteIntOp_ANY_EXTEND(N); break;
1157 case ISD::ATOMIC_STORE:
1158 Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
1159 break;
1160 case ISD::BITCAST: Res = PromoteIntOp_BITCAST(N); break;
1161 case ISD::BR_CC: Res = PromoteIntOp_BR_CC(N, OpNo); break;
1162 case ISD::BRCOND: Res = PromoteIntOp_BRCOND(N, OpNo); break;
1163 case ISD::BUILD_PAIR: Res = PromoteIntOp_BUILD_PAIR(N); break;
1164 case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
1165 case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
1166 case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
1167 case ISD::INSERT_VECTOR_ELT:
1168 Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
1169 case ISD::SCALAR_TO_VECTOR:
1170 Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
1171 case ISD::SPLAT_VECTOR:
1172 Res = PromoteIntOp_SPLAT_VECTOR(N); break;
1173 case ISD::VSELECT:
1174 case ISD::SELECT: Res = PromoteIntOp_SELECT(N, OpNo); break;
1175 case ISD::SELECT_CC: Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
1176 case ISD::SETCC: Res = PromoteIntOp_SETCC(N, OpNo); break;
1177 case ISD::SIGN_EXTEND: Res = PromoteIntOp_SIGN_EXTEND(N); break;
1178 case ISD::SINT_TO_FP: Res = PromoteIntOp_SINT_TO_FP(N); break;
1179 case ISD::STORE: Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
1180 OpNo); break;
1181 case ISD::MSTORE: Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
1182 OpNo); break;
1183 case ISD::MLOAD: Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
1184 OpNo); break;
1185 case ISD::MGATHER: Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
1186 OpNo); break;
1187 case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
1188 OpNo); break;
1189 case ISD::TRUNCATE: Res = PromoteIntOp_TRUNCATE(N); break;
1190 case ISD::FP16_TO_FP:
1191 case ISD::UINT_TO_FP: Res = PromoteIntOp_UINT_TO_FP(N); break;
1192 case ISD::ZERO_EXTEND: Res = PromoteIntOp_ZERO_EXTEND(N); break;
1193 case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
1195 case ISD::SHL:
1196 case ISD::SRA:
1197 case ISD::SRL:
1198 case ISD::ROTL:
1199 case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
1201 case ISD::ADDCARRY:
1202 case ISD::SUBCARRY: Res = PromoteIntOp_ADDSUBCARRY(N, OpNo); break;
1204 case ISD::FRAMEADDR:
1205 case ISD::RETURNADDR: Res = PromoteIntOp_FRAMERETURNADDR(N); break;
1207 case ISD::PREFETCH: Res = PromoteIntOp_PREFETCH(N, OpNo); break;
1209 case ISD::SMULFIX:
1210 case ISD::SMULFIXSAT:
1211 case ISD::UMULFIX:
1212 case ISD::UMULFIXSAT: Res = PromoteIntOp_MULFIX(N); break;
1214 case ISD::FPOWI: Res = PromoteIntOp_FPOWI(N); break;
1216 case ISD::VECREDUCE_ADD:
1217 case ISD::VECREDUCE_MUL:
1218 case ISD::VECREDUCE_AND:
1219 case ISD::VECREDUCE_OR:
1220 case ISD::VECREDUCE_XOR:
1221 case ISD::VECREDUCE_SMAX:
1222 case ISD::VECREDUCE_SMIN:
1223 case ISD::VECREDUCE_UMAX:
1224 case ISD::VECREDUCE_UMIN: Res = PromoteIntOp_VECREDUCE(N); break;
1227 // If the result is null, the sub-method took care of registering results etc.
1228 if (!Res.getNode()) return false;
1230 // If the result is N, the sub-method updated N in place. Tell the legalizer
1231 // core about this.
1232 if (Res.getNode() == N)
1233 return true;
1235 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1236 "Invalid operand expansion");
1238 ReplaceValueWith(SDValue(N, 0), Res);
1239 return false;
1242 /// PromoteSetCCOperands - Promote the operands of a comparison. This code is
1243 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1244 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
1245 ISD::CondCode CCCode) {
1246 // We have to insert explicit sign or zero extends. Note that we could
1247 // insert sign extends for ALL conditions. For those operations where either
1248 // zero or sign extension would be valid, use SExtOrZExtPromotedInteger
1249 // which will choose the cheapest for the target.
1250 switch (CCCode) {
1251 default: llvm_unreachable("Unknown integer comparison!");
1252 case ISD::SETEQ:
1253 case ISD::SETNE: {
1254 SDValue OpL = GetPromotedInteger(NewLHS);
1255 SDValue OpR = GetPromotedInteger(NewRHS);
1257 // We would prefer to promote the comparison operand with sign extension.
1258 // If the width of OpL/OpR excluding the duplicated sign bits is no greater
1259 // than the width of NewLHS/NewRH, we can avoid inserting real truncate
1260 // instruction, which is redundant eventually.
1261 unsigned OpLEffectiveBits =
1262 OpL.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpL) + 1;
1263 unsigned OpREffectiveBits =
1264 OpR.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(OpR) + 1;
1265 if (OpLEffectiveBits <= NewLHS.getScalarValueSizeInBits() &&
1266 OpREffectiveBits <= NewRHS.getScalarValueSizeInBits()) {
1267 NewLHS = OpL;
1268 NewRHS = OpR;
1269 } else {
1270 NewLHS = SExtOrZExtPromotedInteger(NewLHS);
1271 NewRHS = SExtOrZExtPromotedInteger(NewRHS);
1273 break;
1275 case ISD::SETUGE:
1276 case ISD::SETUGT:
1277 case ISD::SETULE:
1278 case ISD::SETULT:
1279 NewLHS = SExtOrZExtPromotedInteger(NewLHS);
1280 NewRHS = SExtOrZExtPromotedInteger(NewRHS);
1281 break;
1282 case ISD::SETGE:
1283 case ISD::SETGT:
1284 case ISD::SETLT:
1285 case ISD::SETLE:
1286 NewLHS = SExtPromotedInteger(NewLHS);
1287 NewRHS = SExtPromotedInteger(NewRHS);
1288 break;
1292 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
1293 SDValue Op = GetPromotedInteger(N->getOperand(0));
1294 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
1297 SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
1298 SDValue Op2 = GetPromotedInteger(N->getOperand(2));
1299 return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
1300 N->getChain(), N->getBasePtr(), Op2, N->getMemOperand());
1303 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
1304 // This should only occur in unusual situations like bitcasting to an
1305 // x86_fp80, so just turn it into a store+load
1306 return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
1309 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
1310 assert(OpNo == 2 && "Don't know how to promote this operand!");
1312 SDValue LHS = N->getOperand(2);
1313 SDValue RHS = N->getOperand(3);
1314 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
1316 // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
1317 // legal types.
1318 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1319 N->getOperand(1), LHS, RHS, N->getOperand(4)),
1323 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
1324 assert(OpNo == 1 && "only know how to promote condition");
1326 // Promote all the way up to the canonical SetCC type.
1327 SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
1329 // The chain (Op#0) and basic block destination (Op#2) are always legal types.
1330 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
1331 N->getOperand(2)), 0);
1334 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
1335 // Since the result type is legal, the operands must promote to it.
1336 EVT OVT = N->getOperand(0).getValueType();
1337 SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
1338 SDValue Hi = GetPromotedInteger(N->getOperand(1));
1339 assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
1340 SDLoc dl(N);
1342 Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
1343 DAG.getConstant(OVT.getSizeInBits(), dl,
1344 TLI.getPointerTy(DAG.getDataLayout())));
1345 return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
1348 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
1349 // The vector type is legal but the element type is not. This implies
1350 // that the vector is a power-of-two in length and that the element
1351 // type does not have a strange size (eg: it is not i1).
1352 EVT VecVT = N->getValueType(0);
1353 unsigned NumElts = VecVT.getVectorNumElements();
1354 assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
1355 "Legal vector of one illegal element?");
1357 // Promote the inserted value. The type does not need to match the
1358 // vector element type. Check that any extra bits introduced will be
1359 // truncated away.
1360 assert(N->getOperand(0).getValueSizeInBits() >=
1361 N->getValueType(0).getScalarSizeInBits() &&
1362 "Type of inserted value narrower than vector element type!");
1364 SmallVector<SDValue, 16> NewOps;
1365 for (unsigned i = 0; i < NumElts; ++i)
1366 NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
1368 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1371 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
1372 unsigned OpNo) {
1373 if (OpNo == 1) {
1374 // Promote the inserted value. This is valid because the type does not
1375 // have to match the vector element type.
1377 // Check that any extra bits introduced will be truncated away.
1378 assert(N->getOperand(1).getValueSizeInBits() >=
1379 N->getValueType(0).getScalarSizeInBits() &&
1380 "Type of inserted value narrower than vector element type!");
1381 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1382 GetPromotedInteger(N->getOperand(1)),
1383 N->getOperand(2)),
1387 assert(OpNo == 2 && "Different operand and result vector types?");
1389 // Promote the index.
1390 SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
1391 TLI.getVectorIdxTy(DAG.getDataLayout()));
1392 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1393 N->getOperand(1), Idx), 0);
1396 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
1397 // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
1398 // the operand in place.
1399 return SDValue(DAG.UpdateNodeOperands(N,
1400 GetPromotedInteger(N->getOperand(0))), 0);
1403 SDValue DAGTypeLegalizer::PromoteIntOp_SPLAT_VECTOR(SDNode *N) {
1404 // Integer SPLAT_VECTOR operands are implicitly truncated, so just promote the
1405 // operand in place.
1406 return SDValue(
1407 DAG.UpdateNodeOperands(N, GetPromotedInteger(N->getOperand(0))), 0);
1410 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
1411 assert(OpNo == 0 && "Only know how to promote the condition!");
1412 SDValue Cond = N->getOperand(0);
1413 EVT OpTy = N->getOperand(1).getValueType();
1415 if (N->getOpcode() == ISD::VSELECT)
1416 if (SDValue Res = WidenVSELECTAndMask(N))
1417 return Res;
1419 // Promote all the way up to the canonical SetCC type.
1420 EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
1421 Cond = PromoteTargetBoolean(Cond, OpVT);
1423 return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
1424 N->getOperand(2)), 0);
1427 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1428 assert(OpNo == 0 && "Don't know how to promote this operand!");
1430 SDValue LHS = N->getOperand(0);
1431 SDValue RHS = N->getOperand(1);
1432 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
1434 // The CC (#4) and the possible return values (#2 and #3) have legal types.
1435 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
1436 N->getOperand(3), N->getOperand(4)), 0);
1439 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
1440 assert(OpNo == 0 && "Don't know how to promote this operand!");
1442 SDValue LHS = N->getOperand(0);
1443 SDValue RHS = N->getOperand(1);
1444 PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
1446 // The CC (#2) is always legal.
1447 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
1450 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
1451 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1452 ZExtPromotedInteger(N->getOperand(1))), 0);
1455 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
1456 SDValue Op = GetPromotedInteger(N->getOperand(0));
1457 SDLoc dl(N);
1458 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1459 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
1460 Op, DAG.getValueType(N->getOperand(0).getValueType()));
1463 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
1464 return SDValue(DAG.UpdateNodeOperands(N,
1465 SExtPromotedInteger(N->getOperand(0))), 0);
1468 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
1469 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1470 SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
1471 SDLoc dl(N);
1473 SDValue Val = GetPromotedInteger(N->getValue()); // Get promoted value.
1475 // Truncate the value and store the result.
1476 return DAG.getTruncStore(Ch, dl, Val, Ptr,
1477 N->getMemoryVT(), N->getMemOperand());
1480 SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
1481 unsigned OpNo) {
1483 SDValue DataOp = N->getValue();
1484 EVT DataVT = DataOp.getValueType();
1485 SDValue Mask = N->getMask();
1486 SDLoc dl(N);
1488 bool TruncateStore = false;
1489 if (OpNo == 3) {
1490 Mask = PromoteTargetBoolean(Mask, DataVT);
1491 // Update in place.
1492 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1493 NewOps[3] = Mask;
1494 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1495 } else { // Data operand
1496 assert(OpNo == 1 && "Unexpected operand for promotion");
1497 DataOp = GetPromotedInteger(DataOp);
1498 TruncateStore = true;
1501 return DAG.getMaskedStore(N->getChain(), dl, DataOp, N->getBasePtr(), Mask,
1502 N->getMemoryVT(), N->getMemOperand(),
1503 TruncateStore, N->isCompressingStore());
1506 SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
1507 unsigned OpNo) {
1508 assert(OpNo == 2 && "Only know how to promote the mask!");
1509 EVT DataVT = N->getValueType(0);
1510 SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1511 SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
1512 NewOps[OpNo] = Mask;
1513 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1516 SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
1517 unsigned OpNo) {
1519 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1520 if (OpNo == 2) {
1521 // The Mask
1522 EVT DataVT = N->getValueType(0);
1523 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1524 } else if (OpNo == 4) {
1525 // The Index
1526 if (N->isIndexSigned())
1527 // Need to sign extend the index since the bits will likely be used.
1528 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
1529 else
1530 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
1531 } else
1532 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
1534 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1537 SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
1538 unsigned OpNo) {
1539 SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
1540 if (OpNo == 2) {
1541 // The Mask
1542 EVT DataVT = N->getValue().getValueType();
1543 NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
1544 } else if (OpNo == 4) {
1545 // The Index
1546 if (N->isIndexSigned())
1547 // Need to sign extend the index since the bits will likely be used.
1548 NewOps[OpNo] = SExtPromotedInteger(N->getOperand(OpNo));
1549 else
1550 NewOps[OpNo] = ZExtPromotedInteger(N->getOperand(OpNo));
1551 } else
1552 NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
1553 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
1556 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
1557 SDValue Op = GetPromotedInteger(N->getOperand(0));
1558 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
1561 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
1562 return SDValue(DAG.UpdateNodeOperands(N,
1563 ZExtPromotedInteger(N->getOperand(0))), 0);
1566 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
1567 SDLoc dl(N);
1568 SDValue Op = GetPromotedInteger(N->getOperand(0));
1569 Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
1570 return DAG.getZeroExtendInReg(Op, dl,
1571 N->getOperand(0).getValueType().getScalarType());
1574 SDValue DAGTypeLegalizer::PromoteIntOp_ADDSUBCARRY(SDNode *N, unsigned OpNo) {
1575 assert(OpNo == 2 && "Don't know how to promote this operand!");
1577 SDValue LHS = N->getOperand(0);
1578 SDValue RHS = N->getOperand(1);
1579 SDValue Carry = N->getOperand(2);
1580 SDLoc DL(N);
1582 Carry = PromoteTargetBoolean(Carry, LHS.getValueType());
1584 return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, Carry), 0);
1587 SDValue DAGTypeLegalizer::PromoteIntOp_MULFIX(SDNode *N) {
1588 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
1589 return SDValue(
1590 DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1), Op2), 0);
1593 SDValue DAGTypeLegalizer::PromoteIntOp_FRAMERETURNADDR(SDNode *N) {
1594 // Promote the RETURNADDR/FRAMEADDR argument to a supported integer width.
1595 SDValue Op = ZExtPromotedInteger(N->getOperand(0));
1596 return SDValue(DAG.UpdateNodeOperands(N, Op), 0);
1599 SDValue DAGTypeLegalizer::PromoteIntOp_PREFETCH(SDNode *N, unsigned OpNo) {
1600 assert(OpNo > 1 && "Don't know how to promote this operand!");
1601 // Promote the rw, locality, and cache type arguments to a supported integer
1602 // width.
1603 SDValue Op2 = ZExtPromotedInteger(N->getOperand(2));
1604 SDValue Op3 = ZExtPromotedInteger(N->getOperand(3));
1605 SDValue Op4 = ZExtPromotedInteger(N->getOperand(4));
1606 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), N->getOperand(1),
1607 Op2, Op3, Op4),
1611 SDValue DAGTypeLegalizer::PromoteIntOp_FPOWI(SDNode *N) {
1612 SDValue Op = SExtPromotedInteger(N->getOperand(1));
1613 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Op), 0);
1616 SDValue DAGTypeLegalizer::PromoteIntOp_VECREDUCE(SDNode *N) {
1617 SDLoc dl(N);
1618 SDValue Op;
1619 switch (N->getOpcode()) {
1620 default: llvm_unreachable("Expected integer vector reduction");
1621 case ISD::VECREDUCE_ADD:
1622 case ISD::VECREDUCE_MUL:
1623 case ISD::VECREDUCE_AND:
1624 case ISD::VECREDUCE_OR:
1625 case ISD::VECREDUCE_XOR:
1626 Op = GetPromotedInteger(N->getOperand(0));
1627 break;
1628 case ISD::VECREDUCE_SMAX:
1629 case ISD::VECREDUCE_SMIN:
1630 Op = SExtPromotedInteger(N->getOperand(0));
1631 break;
1632 case ISD::VECREDUCE_UMAX:
1633 case ISD::VECREDUCE_UMIN:
1634 Op = ZExtPromotedInteger(N->getOperand(0));
1635 break;
1638 EVT EltVT = Op.getValueType().getVectorElementType();
1639 EVT VT = N->getValueType(0);
1640 if (VT.bitsGE(EltVT))
1641 return DAG.getNode(N->getOpcode(), SDLoc(N), VT, Op);
1643 // Result size must be >= element size. If this is not the case after
1644 // promotion, also promote the result type and then truncate.
1645 SDValue Reduce = DAG.getNode(N->getOpcode(), dl, EltVT, Op);
1646 return DAG.getNode(ISD::TRUNCATE, dl, VT, Reduce);
1649 //===----------------------------------------------------------------------===//
1650 // Integer Result Expansion
1651 //===----------------------------------------------------------------------===//
1653 /// ExpandIntegerResult - This method is called when the specified result of the
1654 /// specified node is found to need expansion. At this point, the node may also
1655 /// have invalid operands or may have other results that need promotion, we just
1656 /// know that (at least) one result needs expansion.
1657 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
1658 LLVM_DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG);
1659 dbgs() << "\n");
1660 SDValue Lo, Hi;
1661 Lo = Hi = SDValue();
1663 // See if the target wants to custom expand this node.
1664 if (CustomLowerNode(N, N->getValueType(ResNo), true))
1665 return;
1667 switch (N->getOpcode()) {
1668 default:
1669 #ifndef NDEBUG
1670 dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
1671 N->dump(&DAG); dbgs() << "\n";
1672 #endif
1673 report_fatal_error("Do not know how to expand the result of this "
1674 "operator!");
1676 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
1677 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
1678 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
1679 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
1681 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
1682 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1683 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1684 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1685 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
1687 case ISD::ANY_EXTEND: ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
1688 case ISD::AssertSext: ExpandIntRes_AssertSext(N, Lo, Hi); break;
1689 case ISD::AssertZext: ExpandIntRes_AssertZext(N, Lo, Hi); break;
1690 case ISD::BITREVERSE: ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
1691 case ISD::BSWAP: ExpandIntRes_BSWAP(N, Lo, Hi); break;
1692 case ISD::Constant: ExpandIntRes_Constant(N, Lo, Hi); break;
1693 case ISD::ABS: ExpandIntRes_ABS(N, Lo, Hi); break;
1694 case ISD::CTLZ_ZERO_UNDEF:
1695 case ISD::CTLZ: ExpandIntRes_CTLZ(N, Lo, Hi); break;
1696 case ISD::CTPOP: ExpandIntRes_CTPOP(N, Lo, Hi); break;
1697 case ISD::CTTZ_ZERO_UNDEF:
1698 case ISD::CTTZ: ExpandIntRes_CTTZ(N, Lo, Hi); break;
1699 case ISD::FLT_ROUNDS_: ExpandIntRes_FLT_ROUNDS(N, Lo, Hi); break;
1700 case ISD::FP_TO_SINT: ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
1701 case ISD::FP_TO_UINT: ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
1702 case ISD::LLROUND: ExpandIntRes_LLROUND(N, Lo, Hi); break;
1703 case ISD::LLRINT: ExpandIntRes_LLRINT(N, Lo, Hi); break;
1704 case ISD::LOAD: ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
1705 case ISD::MUL: ExpandIntRes_MUL(N, Lo, Hi); break;
1706 case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
1707 case ISD::SDIV: ExpandIntRes_SDIV(N, Lo, Hi); break;
1708 case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1709 case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1710 case ISD::SREM: ExpandIntRes_SREM(N, Lo, Hi); break;
1711 case ISD::TRUNCATE: ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1712 case ISD::UDIV: ExpandIntRes_UDIV(N, Lo, Hi); break;
1713 case ISD::UREM: ExpandIntRes_UREM(N, Lo, Hi); break;
1714 case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1715 case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
1717 case ISD::ATOMIC_LOAD_ADD:
1718 case ISD::ATOMIC_LOAD_SUB:
1719 case ISD::ATOMIC_LOAD_AND:
1720 case ISD::ATOMIC_LOAD_CLR:
1721 case ISD::ATOMIC_LOAD_OR:
1722 case ISD::ATOMIC_LOAD_XOR:
1723 case ISD::ATOMIC_LOAD_NAND:
1724 case ISD::ATOMIC_LOAD_MIN:
1725 case ISD::ATOMIC_LOAD_MAX:
1726 case ISD::ATOMIC_LOAD_UMIN:
1727 case ISD::ATOMIC_LOAD_UMAX:
1728 case ISD::ATOMIC_SWAP:
1729 case ISD::ATOMIC_CMP_SWAP: {
1730 std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
1731 SplitInteger(Tmp.first, Lo, Hi);
1732 ReplaceValueWith(SDValue(N, 1), Tmp.second);
1733 break;
1735 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
1736 AtomicSDNode *AN = cast<AtomicSDNode>(N);
1737 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
1738 SDValue Tmp = DAG.getAtomicCmpSwap(
1739 ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
1740 N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
1741 AN->getMemOperand());
1743 // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
1744 // success simply by comparing the loaded value against the ingoing
1745 // comparison.
1746 SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
1747 N->getOperand(2), ISD::SETEQ);
1749 SplitInteger(Tmp, Lo, Hi);
1750 ReplaceValueWith(SDValue(N, 1), Success);
1751 ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
1752 break;
1755 case ISD::AND:
1756 case ISD::OR:
1757 case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1759 case ISD::UMAX:
1760 case ISD::SMAX:
1761 case ISD::UMIN:
1762 case ISD::SMIN: ExpandIntRes_MINMAX(N, Lo, Hi); break;
1764 case ISD::ADD:
1765 case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1767 case ISD::ADDC:
1768 case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1770 case ISD::ADDE:
1771 case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1773 case ISD::ADDCARRY:
1774 case ISD::SUBCARRY: ExpandIntRes_ADDSUBCARRY(N, Lo, Hi); break;
1776 case ISD::SHL:
1777 case ISD::SRA:
1778 case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1780 case ISD::SADDO:
1781 case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
1782 case ISD::UADDO:
1783 case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
1784 case ISD::UMULO:
1785 case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
1787 case ISD::SADDSAT:
1788 case ISD::UADDSAT:
1789 case ISD::SSUBSAT:
1790 case ISD::USUBSAT: ExpandIntRes_ADDSUBSAT(N, Lo, Hi); break;
1792 case ISD::SMULFIX:
1793 case ISD::SMULFIXSAT:
1794 case ISD::UMULFIX:
1795 case ISD::UMULFIXSAT: ExpandIntRes_MULFIX(N, Lo, Hi); break;
1797 case ISD::VECREDUCE_ADD:
1798 case ISD::VECREDUCE_MUL:
1799 case ISD::VECREDUCE_AND:
1800 case ISD::VECREDUCE_OR:
1801 case ISD::VECREDUCE_XOR:
1802 case ISD::VECREDUCE_SMAX:
1803 case ISD::VECREDUCE_SMIN:
1804 case ISD::VECREDUCE_UMAX:
1805 case ISD::VECREDUCE_UMIN: ExpandIntRes_VECREDUCE(N, Lo, Hi); break;
1808 // If Lo/Hi is null, the sub-method took care of registering results etc.
1809 if (Lo.getNode())
1810 SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1813 /// Lower an atomic node to the appropriate builtin call.
1814 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
1815 unsigned Opc = Node->getOpcode();
1816 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
1817 RTLIB::Libcall LC = RTLIB::getSYNC(Opc, VT);
1818 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
1820 return ExpandChainLibCall(LC, Node, false);
1823 /// N is a shift by a value that needs to be expanded,
1824 /// and the shift amount is a constant 'Amt'. Expand the operation.
1825 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
1826 SDValue &Lo, SDValue &Hi) {
1827 SDLoc DL(N);
1828 // Expand the incoming operand to be shifted, so that we have its parts
1829 SDValue InL, InH;
1830 GetExpandedInteger(N->getOperand(0), InL, InH);
1832 // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
1833 // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
1834 if (!Amt) {
1835 Lo = InL;
1836 Hi = InH;
1837 return;
1840 EVT NVT = InL.getValueType();
1841 unsigned VTBits = N->getValueType(0).getSizeInBits();
1842 unsigned NVTBits = NVT.getSizeInBits();
1843 EVT ShTy = N->getOperand(1).getValueType();
1845 if (N->getOpcode() == ISD::SHL) {
1846 if (Amt.ugt(VTBits)) {
1847 Lo = Hi = DAG.getConstant(0, DL, NVT);
1848 } else if (Amt.ugt(NVTBits)) {
1849 Lo = DAG.getConstant(0, DL, NVT);
1850 Hi = DAG.getNode(ISD::SHL, DL,
1851 NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
1852 } else if (Amt == NVTBits) {
1853 Lo = DAG.getConstant(0, DL, NVT);
1854 Hi = InL;
1855 } else {
1856 Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
1857 Hi = DAG.getNode(ISD::OR, DL, NVT,
1858 DAG.getNode(ISD::SHL, DL, NVT, InH,
1859 DAG.getConstant(Amt, DL, ShTy)),
1860 DAG.getNode(ISD::SRL, DL, NVT, InL,
1861 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
1863 return;
1866 if (N->getOpcode() == ISD::SRL) {
1867 if (Amt.ugt(VTBits)) {
1868 Lo = Hi = DAG.getConstant(0, DL, NVT);
1869 } else if (Amt.ugt(NVTBits)) {
1870 Lo = DAG.getNode(ISD::SRL, DL,
1871 NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
1872 Hi = DAG.getConstant(0, DL, NVT);
1873 } else if (Amt == NVTBits) {
1874 Lo = InH;
1875 Hi = DAG.getConstant(0, DL, NVT);
1876 } else {
1877 Lo = DAG.getNode(ISD::OR, DL, NVT,
1878 DAG.getNode(ISD::SRL, DL, NVT, InL,
1879 DAG.getConstant(Amt, DL, ShTy)),
1880 DAG.getNode(ISD::SHL, DL, NVT, InH,
1881 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
1882 Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
1884 return;
1887 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1888 if (Amt.ugt(VTBits)) {
1889 Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1890 DAG.getConstant(NVTBits - 1, DL, ShTy));
1891 } else if (Amt.ugt(NVTBits)) {
1892 Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
1893 DAG.getConstant(Amt - NVTBits, DL, ShTy));
1894 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1895 DAG.getConstant(NVTBits - 1, DL, ShTy));
1896 } else if (Amt == NVTBits) {
1897 Lo = InH;
1898 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
1899 DAG.getConstant(NVTBits - 1, DL, ShTy));
1900 } else {
1901 Lo = DAG.getNode(ISD::OR, DL, NVT,
1902 DAG.getNode(ISD::SRL, DL, NVT, InL,
1903 DAG.getConstant(Amt, DL, ShTy)),
1904 DAG.getNode(ISD::SHL, DL, NVT, InH,
1905 DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
1906 Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
1910 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1911 /// this shift based on knowledge of the high bit of the shift amount. If we
1912 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1913 /// shift amount.
1914 bool DAGTypeLegalizer::
1915 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1916 SDValue Amt = N->getOperand(1);
1917 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1918 EVT ShTy = Amt.getValueType();
1919 unsigned ShBits = ShTy.getScalarSizeInBits();
1920 unsigned NVTBits = NVT.getScalarSizeInBits();
1921 assert(isPowerOf2_32(NVTBits) &&
1922 "Expanded integer type size not a power of two!");
1923 SDLoc dl(N);
1925 APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1926 KnownBits Known = DAG.computeKnownBits(N->getOperand(1));
1928 // If we don't know anything about the high bits, exit.
1929 if (((Known.Zero|Known.One) & HighBitMask) == 0)
1930 return false;
1932 // Get the incoming operand to be shifted.
1933 SDValue InL, InH;
1934 GetExpandedInteger(N->getOperand(0), InL, InH);
1936 // If we know that any of the high bits of the shift amount are one, then we
1937 // can do this as a couple of simple shifts.
1938 if (Known.One.intersects(HighBitMask)) {
1939 // Mask out the high bit, which we know is set.
1940 Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
1941 DAG.getConstant(~HighBitMask, dl, ShTy));
1943 switch (N->getOpcode()) {
1944 default: llvm_unreachable("Unknown shift");
1945 case ISD::SHL:
1946 Lo = DAG.getConstant(0, dl, NVT); // Low part is zero.
1947 Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
1948 return true;
1949 case ISD::SRL:
1950 Hi = DAG.getConstant(0, dl, NVT); // Hi part is zero.
1951 Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
1952 return true;
1953 case ISD::SRA:
1954 Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign extend high part.
1955 DAG.getConstant(NVTBits - 1, dl, ShTy));
1956 Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
1957 return true;
1961 // If we know that all of the high bits of the shift amount are zero, then we
1962 // can do this as a couple of simple shifts.
1963 if (HighBitMask.isSubsetOf(Known.Zero)) {
1964 // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
1965 // shift if x is zero. We can use XOR here because x is known to be smaller
1966 // than 32.
1967 SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
1968 DAG.getConstant(NVTBits - 1, dl, ShTy));
1970 unsigned Op1, Op2;
1971 switch (N->getOpcode()) {
1972 default: llvm_unreachable("Unknown shift");
1973 case ISD::SHL: Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1974 case ISD::SRL:
1975 case ISD::SRA: Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1978 // When shifting right the arithmetic for Lo and Hi is swapped.
1979 if (N->getOpcode() != ISD::SHL)
1980 std::swap(InL, InH);
1982 // Use a little trick to get the bits that move from Lo to Hi. First
1983 // shift by one bit.
1984 SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
1985 // Then compute the remaining shift with amount-1.
1986 SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
1988 Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
1989 Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
1991 if (N->getOpcode() != ISD::SHL)
1992 std::swap(Hi, Lo);
1993 return true;
1996 return false;
1999 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
2000 /// of any size.
2001 bool DAGTypeLegalizer::
2002 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
2003 SDValue Amt = N->getOperand(1);
2004 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2005 EVT ShTy = Amt.getValueType();
2006 unsigned NVTBits = NVT.getSizeInBits();
2007 assert(isPowerOf2_32(NVTBits) &&
2008 "Expanded integer type size not a power of two!");
2009 SDLoc dl(N);
2011 // Get the incoming operand to be shifted.
2012 SDValue InL, InH;
2013 GetExpandedInteger(N->getOperand(0), InL, InH);
2015 SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
2016 SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
2017 SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
2018 SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2019 Amt, NVBitsNode, ISD::SETULT);
2020 SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
2021 Amt, DAG.getConstant(0, dl, ShTy),
2022 ISD::SETEQ);
2024 SDValue LoS, HiS, LoL, HiL;
2025 switch (N->getOpcode()) {
2026 default: llvm_unreachable("Unknown shift");
2027 case ISD::SHL:
2028 // Short: ShAmt < NVTBits
2029 LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
2030 HiS = DAG.getNode(ISD::OR, dl, NVT,
2031 DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
2032 DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
2034 // Long: ShAmt >= NVTBits
2035 LoL = DAG.getConstant(0, dl, NVT); // Lo part is zero.
2036 HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
2038 Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
2039 Hi = DAG.getSelect(dl, NVT, isZero, InH,
2040 DAG.getSelect(dl, NVT, isShort, HiS, HiL));
2041 return true;
2042 case ISD::SRL:
2043 // Short: ShAmt < NVTBits
2044 HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
2045 LoS = DAG.getNode(ISD::OR, dl, NVT,
2046 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2047 // FIXME: If Amt is zero, the following shift generates an undefined result
2048 // on some architectures.
2049 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2051 // Long: ShAmt >= NVTBits
2052 HiL = DAG.getConstant(0, dl, NVT); // Hi part is zero.
2053 LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2055 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2056 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2057 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2058 return true;
2059 case ISD::SRA:
2060 // Short: ShAmt < NVTBits
2061 HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
2062 LoS = DAG.getNode(ISD::OR, dl, NVT,
2063 DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
2064 DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
2066 // Long: ShAmt >= NVTBits
2067 HiL = DAG.getNode(ISD::SRA, dl, NVT, InH, // Sign of Hi part.
2068 DAG.getConstant(NVTBits - 1, dl, ShTy));
2069 LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
2071 Lo = DAG.getSelect(dl, NVT, isZero, InL,
2072 DAG.getSelect(dl, NVT, isShort, LoS, LoL));
2073 Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
2074 return true;
2078 static std::pair<ISD::CondCode, ISD::NodeType> getExpandedMinMaxOps(int Op) {
2080 switch (Op) {
2081 default: llvm_unreachable("invalid min/max opcode");
2082 case ISD::SMAX:
2083 return std::make_pair(ISD::SETGT, ISD::UMAX);
2084 case ISD::UMAX:
2085 return std::make_pair(ISD::SETUGT, ISD::UMAX);
2086 case ISD::SMIN:
2087 return std::make_pair(ISD::SETLT, ISD::UMIN);
2088 case ISD::UMIN:
2089 return std::make_pair(ISD::SETULT, ISD::UMIN);
2093 void DAGTypeLegalizer::ExpandIntRes_MINMAX(SDNode *N,
2094 SDValue &Lo, SDValue &Hi) {
2095 SDLoc DL(N);
2096 ISD::NodeType LoOpc;
2097 ISD::CondCode CondC;
2098 std::tie(CondC, LoOpc) = getExpandedMinMaxOps(N->getOpcode());
2100 // Expand the subcomponents.
2101 SDValue LHSL, LHSH, RHSL, RHSH;
2102 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2103 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2105 // Value types
2106 EVT NVT = LHSL.getValueType();
2107 EVT CCT = getSetCCResultType(NVT);
2109 // Hi part is always the same op
2110 Hi = DAG.getNode(N->getOpcode(), DL, NVT, {LHSH, RHSH});
2112 // We need to know whether to select Lo part that corresponds to 'winning'
2113 // Hi part or if Hi parts are equal.
2114 SDValue IsHiLeft = DAG.getSetCC(DL, CCT, LHSH, RHSH, CondC);
2115 SDValue IsHiEq = DAG.getSetCC(DL, CCT, LHSH, RHSH, ISD::SETEQ);
2117 // Lo part corresponding to the 'winning' Hi part
2118 SDValue LoCmp = DAG.getSelect(DL, NVT, IsHiLeft, LHSL, RHSL);
2120 // Recursed Lo part if Hi parts are equal, this uses unsigned version
2121 SDValue LoMinMax = DAG.getNode(LoOpc, DL, NVT, {LHSL, RHSL});
2123 Lo = DAG.getSelect(DL, NVT, IsHiEq, LoMinMax, LoCmp);
2126 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
2127 SDValue &Lo, SDValue &Hi) {
2128 SDLoc dl(N);
2129 // Expand the subcomponents.
2130 SDValue LHSL, LHSH, RHSL, RHSH;
2131 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2132 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2134 EVT NVT = LHSL.getValueType();
2135 SDValue LoOps[2] = { LHSL, RHSL };
2136 SDValue HiOps[3] = { LHSH, RHSH };
2138 bool HasOpCarry = TLI.isOperationLegalOrCustom(
2139 N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
2140 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2141 if (HasOpCarry) {
2142 SDVTList VTList = DAG.getVTList(NVT, getSetCCResultType(NVT));
2143 if (N->getOpcode() == ISD::ADD) {
2144 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2145 HiOps[2] = Lo.getValue(1);
2146 Hi = DAG.getNode(ISD::ADDCARRY, dl, VTList, HiOps);
2147 } else {
2148 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2149 HiOps[2] = Lo.getValue(1);
2150 Hi = DAG.getNode(ISD::SUBCARRY, dl, VTList, HiOps);
2152 return;
2155 // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
2156 // them. TODO: Teach operation legalization how to expand unsupported
2157 // ADDC/ADDE/SUBC/SUBE. The problem is that these operations generate
2158 // a carry of type MVT::Glue, but there doesn't seem to be any way to
2159 // generate a value of this type in the expanded code sequence.
2160 bool hasCarry =
2161 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2162 ISD::ADDC : ISD::SUBC,
2163 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2165 if (hasCarry) {
2166 SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
2167 if (N->getOpcode() == ISD::ADD) {
2168 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2169 HiOps[2] = Lo.getValue(1);
2170 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2171 } else {
2172 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2173 HiOps[2] = Lo.getValue(1);
2174 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2176 return;
2179 bool hasOVF =
2180 TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
2181 ISD::UADDO : ISD::USUBO,
2182 TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
2183 TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
2185 if (hasOVF) {
2186 EVT OvfVT = getSetCCResultType(NVT);
2187 SDVTList VTList = DAG.getVTList(NVT, OvfVT);
2188 int RevOpc;
2189 if (N->getOpcode() == ISD::ADD) {
2190 RevOpc = ISD::SUB;
2191 Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
2192 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2193 } else {
2194 RevOpc = ISD::ADD;
2195 Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
2196 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2198 SDValue OVF = Lo.getValue(1);
2200 switch (BoolType) {
2201 case TargetLoweringBase::UndefinedBooleanContent:
2202 OVF = DAG.getNode(ISD::AND, dl, OvfVT, DAG.getConstant(1, dl, OvfVT), OVF);
2203 LLVM_FALLTHROUGH;
2204 case TargetLoweringBase::ZeroOrOneBooleanContent:
2205 OVF = DAG.getZExtOrTrunc(OVF, dl, NVT);
2206 Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
2207 break;
2208 case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
2209 OVF = DAG.getSExtOrTrunc(OVF, dl, NVT);
2210 Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
2212 return;
2215 if (N->getOpcode() == ISD::ADD) {
2216 Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
2217 Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
2218 SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
2219 ISD::SETULT);
2221 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent) {
2222 SDValue Carry = DAG.getZExtOrTrunc(Cmp1, dl, NVT);
2223 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry);
2224 return;
2227 SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
2228 DAG.getConstant(1, dl, NVT),
2229 DAG.getConstant(0, dl, NVT));
2230 SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
2231 ISD::SETULT);
2232 SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
2233 DAG.getConstant(1, dl, NVT), Carry1);
2234 Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
2235 } else {
2236 Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
2237 Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
2238 SDValue Cmp =
2239 DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
2240 LoOps[0], LoOps[1], ISD::SETULT);
2242 SDValue Borrow;
2243 if (BoolType == TargetLoweringBase::ZeroOrOneBooleanContent)
2244 Borrow = DAG.getZExtOrTrunc(Cmp, dl, NVT);
2245 else
2246 Borrow = DAG.getSelect(dl, NVT, Cmp, DAG.getConstant(1, dl, NVT),
2247 DAG.getConstant(0, dl, NVT));
2249 Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
2253 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
2254 SDValue &Lo, SDValue &Hi) {
2255 // Expand the subcomponents.
2256 SDValue LHSL, LHSH, RHSL, RHSH;
2257 SDLoc dl(N);
2258 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2259 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2260 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2261 SDValue LoOps[2] = { LHSL, RHSL };
2262 SDValue HiOps[3] = { LHSH, RHSH };
2264 if (N->getOpcode() == ISD::ADDC) {
2265 Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
2266 HiOps[2] = Lo.getValue(1);
2267 Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
2268 } else {
2269 Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
2270 HiOps[2] = Lo.getValue(1);
2271 Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
2274 // Legalized the flag result - switch anything that used the old flag to
2275 // use the new one.
2276 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2279 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
2280 SDValue &Lo, SDValue &Hi) {
2281 // Expand the subcomponents.
2282 SDValue LHSL, LHSH, RHSL, RHSH;
2283 SDLoc dl(N);
2284 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2285 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2286 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
2287 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2288 SDValue HiOps[3] = { LHSH, RHSH };
2290 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2291 HiOps[2] = Lo.getValue(1);
2292 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2294 // Legalized the flag result - switch anything that used the old flag to
2295 // use the new one.
2296 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2299 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
2300 SDValue &Lo, SDValue &Hi) {
2301 SDValue LHS = N->getOperand(0);
2302 SDValue RHS = N->getOperand(1);
2303 SDLoc dl(N);
2305 SDValue Ovf;
2307 bool HasOpCarry = TLI.isOperationLegalOrCustom(
2308 N->getOpcode() == ISD::ADD ? ISD::ADDCARRY : ISD::SUBCARRY,
2309 TLI.getTypeToExpandTo(*DAG.getContext(), LHS.getValueType()));
2311 if (HasOpCarry) {
2312 // Expand the subcomponents.
2313 SDValue LHSL, LHSH, RHSL, RHSH;
2314 GetExpandedInteger(LHS, LHSL, LHSH);
2315 GetExpandedInteger(RHS, RHSL, RHSH);
2316 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2317 SDValue LoOps[2] = { LHSL, RHSL };
2318 SDValue HiOps[3] = { LHSH, RHSH };
2320 unsigned Opc = N->getOpcode() == ISD::UADDO ? ISD::ADDCARRY : ISD::SUBCARRY;
2321 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2322 HiOps[2] = Lo.getValue(1);
2323 Hi = DAG.getNode(Opc, dl, VTList, HiOps);
2325 Ovf = Hi.getValue(1);
2326 } else {
2327 // Expand the result by simply replacing it with the equivalent
2328 // non-overflow-checking operation.
2329 auto Opc = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
2330 SDValue Sum = DAG.getNode(Opc, dl, LHS.getValueType(), LHS, RHS);
2331 SplitInteger(Sum, Lo, Hi);
2333 // Calculate the overflow: addition overflows iff a + b < a, and subtraction
2334 // overflows iff a - b > a.
2335 auto Cond = N->getOpcode() == ISD::UADDO ? ISD::SETULT : ISD::SETUGT;
2336 Ovf = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS, Cond);
2339 // Legalized the flag result - switch anything that used the old flag to
2340 // use the new one.
2341 ReplaceValueWith(SDValue(N, 1), Ovf);
2344 void DAGTypeLegalizer::ExpandIntRes_ADDSUBCARRY(SDNode *N,
2345 SDValue &Lo, SDValue &Hi) {
2346 // Expand the subcomponents.
2347 SDValue LHSL, LHSH, RHSL, RHSH;
2348 SDLoc dl(N);
2349 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
2350 GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
2351 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), N->getValueType(1));
2352 SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
2353 SDValue HiOps[3] = { LHSH, RHSH, SDValue() };
2355 Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
2356 HiOps[2] = Lo.getValue(1);
2357 Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
2359 // Legalized the flag result - switch anything that used the old flag to
2360 // use the new one.
2361 ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
2364 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
2365 SDValue &Lo, SDValue &Hi) {
2366 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2367 SDLoc dl(N);
2368 SDValue Op = N->getOperand(0);
2369 if (Op.getValueType().bitsLE(NVT)) {
2370 // The low part is any extension of the input (which degenerates to a copy).
2371 Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
2372 Hi = DAG.getUNDEF(NVT); // The high part is undefined.
2373 } else {
2374 // For example, extension of an i48 to an i64. The operand type necessarily
2375 // promotes to the result type, so will end up being expanded too.
2376 assert(getTypeAction(Op.getValueType()) ==
2377 TargetLowering::TypePromoteInteger &&
2378 "Only know how to promote this result!");
2379 SDValue Res = GetPromotedInteger(Op);
2380 assert(Res.getValueType() == N->getValueType(0) &&
2381 "Operand over promoted?");
2382 // Split the promoted operand. This will simplify when it is expanded.
2383 SplitInteger(Res, Lo, Hi);
2387 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
2388 SDValue &Lo, SDValue &Hi) {
2389 SDLoc dl(N);
2390 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2391 EVT NVT = Lo.getValueType();
2392 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2393 unsigned NVTBits = NVT.getSizeInBits();
2394 unsigned EVTBits = EVT.getSizeInBits();
2396 if (NVTBits < EVTBits) {
2397 Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
2398 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2399 EVTBits - NVTBits)));
2400 } else {
2401 Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
2402 // The high part replicates the sign bit of Lo, make it explicit.
2403 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2404 DAG.getConstant(NVTBits - 1, dl,
2405 TLI.getPointerTy(DAG.getDataLayout())));
2409 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
2410 SDValue &Lo, SDValue &Hi) {
2411 SDLoc dl(N);
2412 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2413 EVT NVT = Lo.getValueType();
2414 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
2415 unsigned NVTBits = NVT.getSizeInBits();
2416 unsigned EVTBits = EVT.getSizeInBits();
2418 if (NVTBits < EVTBits) {
2419 Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
2420 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
2421 EVTBits - NVTBits)));
2422 } else {
2423 Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
2424 // The high part must be zero, make it explicit.
2425 Hi = DAG.getConstant(0, dl, NVT);
2429 void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
2430 SDValue &Lo, SDValue &Hi) {
2431 SDLoc dl(N);
2432 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
2433 Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
2434 Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
2437 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
2438 SDValue &Lo, SDValue &Hi) {
2439 SDLoc dl(N);
2440 GetExpandedInteger(N->getOperand(0), Hi, Lo); // Note swapped operands.
2441 Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
2442 Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
2445 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
2446 SDValue &Lo, SDValue &Hi) {
2447 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2448 unsigned NBitWidth = NVT.getSizeInBits();
2449 auto Constant = cast<ConstantSDNode>(N);
2450 const APInt &Cst = Constant->getAPIntValue();
2451 bool IsTarget = Constant->isTargetOpcode();
2452 bool IsOpaque = Constant->isOpaque();
2453 SDLoc dl(N);
2454 Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
2455 Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
2456 IsOpaque);
2459 void DAGTypeLegalizer::ExpandIntRes_ABS(SDNode *N, SDValue &Lo, SDValue &Hi) {
2460 SDLoc dl(N);
2462 // abs(HiLo) -> (Hi < 0 ? -HiLo : HiLo)
2463 EVT VT = N->getValueType(0);
2464 SDValue N0 = N->getOperand(0);
2465 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT,
2466 DAG.getConstant(0, dl, VT), N0);
2467 SDValue NegLo, NegHi;
2468 SplitInteger(Neg, NegLo, NegHi);
2470 GetExpandedInteger(N0, Lo, Hi);
2471 EVT NVT = Lo.getValueType();
2472 SDValue HiIsNeg = DAG.getSetCC(dl, getSetCCResultType(NVT),
2473 DAG.getConstant(0, dl, NVT), Hi, ISD::SETGT);
2474 Lo = DAG.getSelect(dl, NVT, HiIsNeg, NegLo, Lo);
2475 Hi = DAG.getSelect(dl, NVT, HiIsNeg, NegHi, Hi);
2478 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
2479 SDValue &Lo, SDValue &Hi) {
2480 SDLoc dl(N);
2481 // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
2482 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2483 EVT NVT = Lo.getValueType();
2485 SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
2486 DAG.getConstant(0, dl, NVT), ISD::SETNE);
2488 SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
2489 SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
2491 Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
2492 DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
2493 DAG.getConstant(NVT.getSizeInBits(), dl,
2494 NVT)));
2495 Hi = DAG.getConstant(0, dl, NVT);
2498 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
2499 SDValue &Lo, SDValue &Hi) {
2500 SDLoc dl(N);
2501 // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
2502 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2503 EVT NVT = Lo.getValueType();
2504 Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
2505 DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
2506 Hi = DAG.getConstant(0, dl, NVT);
2509 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
2510 SDValue &Lo, SDValue &Hi) {
2511 SDLoc dl(N);
2512 // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
2513 GetExpandedInteger(N->getOperand(0), Lo, Hi);
2514 EVT NVT = Lo.getValueType();
2516 SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
2517 DAG.getConstant(0, dl, NVT), ISD::SETNE);
2519 SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
2520 SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
2522 Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
2523 DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
2524 DAG.getConstant(NVT.getSizeInBits(), dl,
2525 NVT)));
2526 Hi = DAG.getConstant(0, dl, NVT);
2529 void DAGTypeLegalizer::ExpandIntRes_FLT_ROUNDS(SDNode *N, SDValue &Lo,
2530 SDValue &Hi) {
2531 SDLoc dl(N);
2532 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2533 unsigned NBitWidth = NVT.getSizeInBits();
2535 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
2536 Lo = DAG.getNode(ISD::FLT_ROUNDS_, dl, NVT);
2537 // The high part is the sign of Lo, as -1 is a valid value for FLT_ROUNDS
2538 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2539 DAG.getConstant(NBitWidth - 1, dl, ShiftAmtTy));
2542 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
2543 SDValue &Hi) {
2544 SDLoc dl(N);
2545 EVT VT = N->getValueType(0);
2547 SDValue Op = N->getOperand(0);
2548 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2549 Op = GetPromotedFloat(Op);
2551 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
2552 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
2553 TargetLowering::MakeLibCallOptions CallOptions;
2554 CallOptions.setSExt(true);
2555 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, dl).first,
2556 Lo, Hi);
2559 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
2560 SDValue &Hi) {
2561 SDLoc dl(N);
2562 EVT VT = N->getValueType(0);
2564 SDValue Op = N->getOperand(0);
2565 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2566 Op = GetPromotedFloat(Op);
2568 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
2569 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
2570 TargetLowering::MakeLibCallOptions CallOptions;
2571 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, dl).first,
2572 Lo, Hi);
2575 void DAGTypeLegalizer::ExpandIntRes_LLROUND(SDNode *N, SDValue &Lo,
2576 SDValue &Hi) {
2577 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2578 EVT VT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
2579 if (VT == MVT::f32)
2580 LC = RTLIB::LLROUND_F32;
2581 else if (VT == MVT::f64)
2582 LC = RTLIB::LLROUND_F64;
2583 else if (VT == MVT::f80)
2584 LC = RTLIB::LLROUND_F80;
2585 else if (VT == MVT::f128)
2586 LC = RTLIB::LLROUND_F128;
2587 else if (VT == MVT::ppcf128)
2588 LC = RTLIB::LLROUND_PPCF128;
2589 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llround input type!");
2591 SDValue Op = N->getOperand(0);
2592 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2593 Op = GetPromotedFloat(Op);
2595 SDLoc dl(N);
2596 EVT RetVT = N->getValueType(0);
2597 TargetLowering::MakeLibCallOptions CallOptions;
2598 CallOptions.setSExt(true);
2599 SplitInteger(TLI.makeLibCall(DAG, LC, RetVT, Op, CallOptions, dl).first,
2600 Lo, Hi);
2603 void DAGTypeLegalizer::ExpandIntRes_LLRINT(SDNode *N, SDValue &Lo,
2604 SDValue &Hi) {
2605 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2606 EVT VT = N->getOperand(0).getValueType().getSimpleVT().SimpleTy;
2607 if (VT == MVT::f32)
2608 LC = RTLIB::LLRINT_F32;
2609 else if (VT == MVT::f64)
2610 LC = RTLIB::LLRINT_F64;
2611 else if (VT == MVT::f80)
2612 LC = RTLIB::LLRINT_F80;
2613 else if (VT == MVT::f128)
2614 LC = RTLIB::LLRINT_F128;
2615 else if (VT == MVT::ppcf128)
2616 LC = RTLIB::LLRINT_PPCF128;
2617 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected llrint input type!");
2619 SDValue Op = N->getOperand(0);
2620 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
2621 Op = GetPromotedFloat(Op);
2623 SDLoc dl(N);
2624 EVT RetVT = N->getValueType(0);
2625 TargetLowering::MakeLibCallOptions CallOptions;
2626 CallOptions.setSExt(true);
2627 SplitInteger(TLI.makeLibCall(DAG, LC, RetVT, Op, CallOptions, dl).first,
2628 Lo, Hi);
2631 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
2632 SDValue &Lo, SDValue &Hi) {
2633 if (ISD::isNormalLoad(N)) {
2634 ExpandRes_NormalLoad(N, Lo, Hi);
2635 return;
2638 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
2640 EVT VT = N->getValueType(0);
2641 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2642 SDValue Ch = N->getChain();
2643 SDValue Ptr = N->getBasePtr();
2644 ISD::LoadExtType ExtType = N->getExtensionType();
2645 unsigned Alignment = N->getAlignment();
2646 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
2647 AAMDNodes AAInfo = N->getAAInfo();
2648 SDLoc dl(N);
2650 assert(NVT.isByteSized() && "Expanded type not byte sized!");
2652 if (N->getMemoryVT().bitsLE(NVT)) {
2653 EVT MemVT = N->getMemoryVT();
2655 Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(), MemVT,
2656 Alignment, MMOFlags, AAInfo);
2658 // Remember the chain.
2659 Ch = Lo.getValue(1);
2661 if (ExtType == ISD::SEXTLOAD) {
2662 // The high part is obtained by SRA'ing all but one of the bits of the
2663 // lo part.
2664 unsigned LoSize = Lo.getValueSizeInBits();
2665 Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
2666 DAG.getConstant(LoSize - 1, dl,
2667 TLI.getPointerTy(DAG.getDataLayout())));
2668 } else if (ExtType == ISD::ZEXTLOAD) {
2669 // The high part is just a zero.
2670 Hi = DAG.getConstant(0, dl, NVT);
2671 } else {
2672 assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
2673 // The high part is undefined.
2674 Hi = DAG.getUNDEF(NVT);
2676 } else if (DAG.getDataLayout().isLittleEndian()) {
2677 // Little-endian - low bits are at low addresses.
2678 Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
2679 AAInfo);
2681 unsigned ExcessBits =
2682 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2683 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
2685 // Increment the pointer to the other half.
2686 unsigned IncrementSize = NVT.getSizeInBits()/8;
2687 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2688 DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
2689 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
2690 N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
2691 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
2693 // Build a factor node to remember that this load is independent of the
2694 // other one.
2695 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2696 Hi.getValue(1));
2697 } else {
2698 // Big-endian - high bits are at low addresses. Favor aligned loads at
2699 // the cost of some bit-fiddling.
2700 EVT MemVT = N->getMemoryVT();
2701 unsigned EBytes = MemVT.getStoreSize();
2702 unsigned IncrementSize = NVT.getSizeInBits()/8;
2703 unsigned ExcessBits = (EBytes - IncrementSize)*8;
2705 // Load both the high bits and maybe some of the low bits.
2706 Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
2707 EVT::getIntegerVT(*DAG.getContext(),
2708 MemVT.getSizeInBits() - ExcessBits),
2709 Alignment, MMOFlags, AAInfo);
2711 // Increment the pointer to the other half.
2712 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2713 DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
2714 // Load the rest of the low bits.
2715 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
2716 N->getPointerInfo().getWithOffset(IncrementSize),
2717 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
2718 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
2720 // Build a factor node to remember that this load is independent of the
2721 // other one.
2722 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
2723 Hi.getValue(1));
2725 if (ExcessBits < NVT.getSizeInBits()) {
2726 // Transfer low bits from the bottom of Hi to the top of Lo.
2727 Lo = DAG.getNode(
2728 ISD::OR, dl, NVT, Lo,
2729 DAG.getNode(ISD::SHL, dl, NVT, Hi,
2730 DAG.getConstant(ExcessBits, dl,
2731 TLI.getPointerTy(DAG.getDataLayout()))));
2732 // Move high bits to the right position in Hi.
2733 Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
2735 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
2736 TLI.getPointerTy(DAG.getDataLayout())));
2740 // Legalize the chain result - switch anything that used the old chain to
2741 // use the new one.
2742 ReplaceValueWith(SDValue(N, 1), Ch);
2745 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
2746 SDValue &Lo, SDValue &Hi) {
2747 SDLoc dl(N);
2748 SDValue LL, LH, RL, RH;
2749 GetExpandedInteger(N->getOperand(0), LL, LH);
2750 GetExpandedInteger(N->getOperand(1), RL, RH);
2751 Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
2752 Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
2755 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
2756 SDValue &Lo, SDValue &Hi) {
2757 EVT VT = N->getValueType(0);
2758 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2759 SDLoc dl(N);
2761 SDValue LL, LH, RL, RH;
2762 GetExpandedInteger(N->getOperand(0), LL, LH);
2763 GetExpandedInteger(N->getOperand(1), RL, RH);
2765 if (TLI.expandMUL(N, Lo, Hi, NVT, DAG,
2766 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
2767 LL, LH, RL, RH))
2768 return;
2770 // If nothing else, we can make a libcall.
2771 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
2772 if (VT == MVT::i16)
2773 LC = RTLIB::MUL_I16;
2774 else if (VT == MVT::i32)
2775 LC = RTLIB::MUL_I32;
2776 else if (VT == MVT::i64)
2777 LC = RTLIB::MUL_I64;
2778 else if (VT == MVT::i128)
2779 LC = RTLIB::MUL_I128;
2781 if (LC == RTLIB::UNKNOWN_LIBCALL || !TLI.getLibcallName(LC)) {
2782 // We'll expand the multiplication by brute force because we have no other
2783 // options. This is a trivially-generalized version of the code from
2784 // Hacker's Delight (itself derived from Knuth's Algorithm M from section
2785 // 4.3.1).
2786 unsigned Bits = NVT.getSizeInBits();
2787 unsigned HalfBits = Bits >> 1;
2788 SDValue Mask = DAG.getConstant(APInt::getLowBitsSet(Bits, HalfBits), dl,
2789 NVT);
2790 SDValue LLL = DAG.getNode(ISD::AND, dl, NVT, LL, Mask);
2791 SDValue RLL = DAG.getNode(ISD::AND, dl, NVT, RL, Mask);
2793 SDValue T = DAG.getNode(ISD::MUL, dl, NVT, LLL, RLL);
2794 SDValue TL = DAG.getNode(ISD::AND, dl, NVT, T, Mask);
2796 EVT ShiftAmtTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
2797 if (APInt::getMaxValue(ShiftAmtTy.getSizeInBits()).ult(HalfBits)) {
2798 // The type from TLI is too small to fit the shift amount we want.
2799 // Override it with i32. The shift will have to be legalized.
2800 ShiftAmtTy = MVT::i32;
2802 SDValue Shift = DAG.getConstant(HalfBits, dl, ShiftAmtTy);
2803 SDValue TH = DAG.getNode(ISD::SRL, dl, NVT, T, Shift);
2804 SDValue LLH = DAG.getNode(ISD::SRL, dl, NVT, LL, Shift);
2805 SDValue RLH = DAG.getNode(ISD::SRL, dl, NVT, RL, Shift);
2807 SDValue U = DAG.getNode(ISD::ADD, dl, NVT,
2808 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLL), TH);
2809 SDValue UL = DAG.getNode(ISD::AND, dl, NVT, U, Mask);
2810 SDValue UH = DAG.getNode(ISD::SRL, dl, NVT, U, Shift);
2812 SDValue V = DAG.getNode(ISD::ADD, dl, NVT,
2813 DAG.getNode(ISD::MUL, dl, NVT, LLL, RLH), UL);
2814 SDValue VH = DAG.getNode(ISD::SRL, dl, NVT, V, Shift);
2816 SDValue W = DAG.getNode(ISD::ADD, dl, NVT,
2817 DAG.getNode(ISD::MUL, dl, NVT, LLH, RLH),
2818 DAG.getNode(ISD::ADD, dl, NVT, UH, VH));
2819 Lo = DAG.getNode(ISD::ADD, dl, NVT, TL,
2820 DAG.getNode(ISD::SHL, dl, NVT, V, Shift));
2822 Hi = DAG.getNode(ISD::ADD, dl, NVT, W,
2823 DAG.getNode(ISD::ADD, dl, NVT,
2824 DAG.getNode(ISD::MUL, dl, NVT, RH, LL),
2825 DAG.getNode(ISD::MUL, dl, NVT, RL, LH)));
2826 return;
2829 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
2830 TargetLowering::MakeLibCallOptions CallOptions;
2831 CallOptions.setSExt(true);
2832 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first,
2833 Lo, Hi);
2836 void DAGTypeLegalizer::ExpandIntRes_READCYCLECOUNTER(SDNode *N, SDValue &Lo,
2837 SDValue &Hi) {
2838 SDLoc DL(N);
2839 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
2840 SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
2841 SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
2842 Lo = R.getValue(0);
2843 Hi = R.getValue(1);
2844 ReplaceValueWith(SDValue(N, 1), R.getValue(2));
2847 void DAGTypeLegalizer::ExpandIntRes_ADDSUBSAT(SDNode *N, SDValue &Lo,
2848 SDValue &Hi) {
2849 SDValue Result = TLI.expandAddSubSat(N, DAG);
2850 SplitInteger(Result, Lo, Hi);
2853 /// This performs an expansion of the integer result for a fixed point
2854 /// multiplication. The default expansion performs rounding down towards
2855 /// negative infinity, though targets that do care about rounding should specify
2856 /// a target hook for rounding and provide their own expansion or lowering of
2857 /// fixed point multiplication to be consistent with rounding.
2858 void DAGTypeLegalizer::ExpandIntRes_MULFIX(SDNode *N, SDValue &Lo,
2859 SDValue &Hi) {
2860 SDLoc dl(N);
2861 EVT VT = N->getValueType(0);
2862 unsigned VTSize = VT.getScalarSizeInBits();
2863 SDValue LHS = N->getOperand(0);
2864 SDValue RHS = N->getOperand(1);
2865 uint64_t Scale = N->getConstantOperandVal(2);
2866 bool Saturating = (N->getOpcode() == ISD::SMULFIXSAT ||
2867 N->getOpcode() == ISD::UMULFIXSAT);
2868 bool Signed = (N->getOpcode() == ISD::SMULFIX ||
2869 N->getOpcode() == ISD::SMULFIXSAT);
2871 // Handle special case when scale is equal to zero.
2872 if (!Scale) {
2873 SDValue Result;
2874 if (!Saturating) {
2875 Result = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS);
2876 } else {
2877 EVT BoolVT = getSetCCResultType(VT);
2878 unsigned MulOp = Signed ? ISD::SMULO : ISD::UMULO;
2879 Result = DAG.getNode(MulOp, dl, DAG.getVTList(VT, BoolVT), LHS, RHS);
2880 SDValue Product = Result.getValue(0);
2881 SDValue Overflow = Result.getValue(1);
2882 if (Signed) {
2883 APInt MinVal = APInt::getSignedMinValue(VTSize);
2884 APInt MaxVal = APInt::getSignedMaxValue(VTSize);
2885 SDValue SatMin = DAG.getConstant(MinVal, dl, VT);
2886 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
2887 SDValue Zero = DAG.getConstant(0, dl, VT);
2888 SDValue ProdNeg = DAG.getSetCC(dl, BoolVT, Product, Zero, ISD::SETLT);
2889 Result = DAG.getSelect(dl, VT, ProdNeg, SatMax, SatMin);
2890 Result = DAG.getSelect(dl, VT, Overflow, Result, Product);
2891 } else {
2892 // For unsigned multiplication, we only need to check the max since we
2893 // can't really overflow towards zero.
2894 APInt MaxVal = APInt::getMaxValue(VTSize);
2895 SDValue SatMax = DAG.getConstant(MaxVal, dl, VT);
2896 Result = DAG.getSelect(dl, VT, Overflow, SatMax, Product);
2899 SplitInteger(Result, Lo, Hi);
2900 return;
2903 // For SMULFIX[SAT] we only expect to find Scale<VTSize, but this assert will
2904 // cover for unhandled cases below, while still being valid for UMULFIX[SAT].
2905 assert(Scale <= VTSize && "Scale can't be larger than the value type size.");
2907 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2908 SDValue LL, LH, RL, RH;
2909 GetExpandedInteger(LHS, LL, LH);
2910 GetExpandedInteger(RHS, RL, RH);
2911 SmallVector<SDValue, 4> Result;
2913 unsigned LoHiOp = Signed ? ISD::SMUL_LOHI : ISD::UMUL_LOHI;
2914 if (!TLI.expandMUL_LOHI(LoHiOp, VT, dl, LHS, RHS, Result, NVT, DAG,
2915 TargetLowering::MulExpansionKind::OnlyLegalOrCustom,
2916 LL, LH, RL, RH)) {
2917 report_fatal_error("Unable to expand MUL_FIX using MUL_LOHI.");
2918 return;
2921 unsigned NVTSize = NVT.getScalarSizeInBits();
2922 assert((VTSize == NVTSize * 2) && "Expected the new value type to be half "
2923 "the size of the current value type");
2924 EVT ShiftTy = TLI.getShiftAmountTy(NVT, DAG.getDataLayout());
2926 // After getting the multiplication result in 4 parts, we need to perform a
2927 // shift right by the amount of the scale to get the result in that scale.
2929 // Let's say we multiply 2 64 bit numbers. The resulting value can be held in
2930 // 128 bits that are cut into 4 32-bit parts:
2932 // HH HL LH LL
2933 // |---32---|---32---|---32---|---32---|
2934 // 128 96 64 32 0
2936 // |------VTSize-----|
2938 // |NVTSize-|
2940 // The resulting Lo and Hi would normally be in LL and LH after the shift. But
2941 // to avoid unneccessary shifting of all 4 parts, we can adjust the shift
2942 // amount and get Lo and Hi using two funnel shifts. Or for the special case
2943 // when Scale is a multiple of NVTSize we can just pick the result without
2944 // shifting.
2945 uint64_t Part0 = Scale / NVTSize; // Part holding lowest bit needed.
2946 if (Scale % NVTSize) {
2947 SDValue ShiftAmount = DAG.getConstant(Scale % NVTSize, dl, ShiftTy);
2948 Lo = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 1], Result[Part0],
2949 ShiftAmount);
2950 Hi = DAG.getNode(ISD::FSHR, dl, NVT, Result[Part0 + 2], Result[Part0 + 1],
2951 ShiftAmount);
2952 } else {
2953 Lo = Result[Part0];
2954 Hi = Result[Part0 + 1];
2957 // Unless saturation is requested we are done. The result is in <Hi,Lo>.
2958 if (!Saturating)
2959 return;
2961 // Can not overflow when there is no integer part.
2962 if (Scale == VTSize)
2963 return;
2965 // To handle saturation we must check for overflow in the multiplication.
2967 // Unsigned overflow happened if the upper (VTSize - Scale) bits (of Result)
2968 // aren't all zeroes.
2970 // Signed overflow happened if the upper (VTSize - Scale + 1) bits (of Result)
2971 // aren't all ones or all zeroes.
2973 // We cannot overflow past HH when multiplying 2 ints of size VTSize, so the
2974 // highest bit of HH determines saturation direction in the event of signed
2975 // saturation.
2977 SDValue ResultHL = Result[2];
2978 SDValue ResultHH = Result[3];
2980 SDValue SatMax, SatMin;
2981 SDValue NVTZero = DAG.getConstant(0, dl, NVT);
2982 SDValue NVTNeg1 = DAG.getConstant(-1, dl, NVT);
2983 EVT BoolNVT = getSetCCResultType(NVT);
2985 if (!Signed) {
2986 if (Scale < NVTSize) {
2987 // Overflow happened if ((HH | (HL >> Scale)) != 0).
2988 SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
2989 DAG.getConstant(Scale, dl, ShiftTy));
2990 SDValue Tmp = DAG.getNode(ISD::OR, dl, NVT, HLAdjusted, ResultHH);
2991 SatMax = DAG.getSetCC(dl, BoolNVT, Tmp, NVTZero, ISD::SETNE);
2992 } else if (Scale == NVTSize) {
2993 // Overflow happened if (HH != 0).
2994 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETNE);
2995 } else if (Scale < VTSize) {
2996 // Overflow happened if ((HH >> (Scale - NVTSize)) != 0).
2997 SDValue HLAdjusted = DAG.getNode(ISD::SRL, dl, NVT, ResultHL,
2998 DAG.getConstant(Scale - NVTSize, dl,
2999 ShiftTy));
3000 SatMax = DAG.getSetCC(dl, BoolNVT, HLAdjusted, NVTZero, ISD::SETNE);
3001 } else
3002 llvm_unreachable("Scale must be less or equal to VTSize for UMULFIXSAT"
3003 "(and saturation can't happen with Scale==VTSize).");
3005 Hi = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Hi);
3006 Lo = DAG.getSelect(dl, NVT, SatMax, NVTNeg1, Lo);
3007 return;
3010 if (Scale < NVTSize) {
3011 // The number of overflow bits we can check are VTSize - Scale + 1 (we
3012 // include the sign bit). If these top bits are > 0, then we overflowed past
3013 // the max value. If these top bits are < -1, then we overflowed past the
3014 // min value. Otherwise, we did not overflow.
3015 unsigned OverflowBits = VTSize - Scale + 1;
3016 assert(OverflowBits <= VTSize && OverflowBits > NVTSize &&
3017 "Extent of overflow bits must start within HL");
3018 SDValue HLHiMask = DAG.getConstant(
3019 APInt::getHighBitsSet(NVTSize, OverflowBits - NVTSize), dl, NVT);
3020 SDValue HLLoMask = DAG.getConstant(
3021 APInt::getLowBitsSet(NVTSize, VTSize - OverflowBits), dl, NVT);
3022 // We overflow max if HH > 0 or (HH == 0 && HL > HLLoMask).
3023 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
3024 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
3025 SDValue HLUGT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLLoMask, ISD::SETUGT);
3026 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
3027 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLUGT));
3028 // We overflow min if HH < -1 or (HH == -1 && HL < HLHiMask).
3029 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
3030 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
3031 SDValue HLULT = DAG.getSetCC(dl, BoolNVT, ResultHL, HLHiMask, ISD::SETULT);
3032 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
3033 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLULT));
3034 } else if (Scale == NVTSize) {
3035 // We overflow max if HH > 0 or (HH == 0 && HL sign bit is 1).
3036 SDValue HHGT0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETGT);
3037 SDValue HHEQ0 = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTZero, ISD::SETEQ);
3038 SDValue HLNeg = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETLT);
3039 SatMax = DAG.getNode(ISD::OR, dl, BoolNVT, HHGT0,
3040 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ0, HLNeg));
3041 // We overflow min if HH < -1 or (HH == -1 && HL sign bit is 0).
3042 SDValue HHLT = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETLT);
3043 SDValue HHEQ = DAG.getSetCC(dl, BoolNVT, ResultHH, NVTNeg1, ISD::SETEQ);
3044 SDValue HLPos = DAG.getSetCC(dl, BoolNVT, ResultHL, NVTZero, ISD::SETGE);
3045 SatMin = DAG.getNode(ISD::OR, dl, BoolNVT, HHLT,
3046 DAG.getNode(ISD::AND, dl, BoolNVT, HHEQ, HLPos));
3047 } else if (Scale < VTSize) {
3048 // This is similar to the case when we saturate if Scale < NVTSize, but we
3049 // only need to check HH.
3050 unsigned OverflowBits = VTSize - Scale + 1;
3051 SDValue HHHiMask = DAG.getConstant(
3052 APInt::getHighBitsSet(NVTSize, OverflowBits), dl, NVT);
3053 SDValue HHLoMask = DAG.getConstant(
3054 APInt::getLowBitsSet(NVTSize, NVTSize - OverflowBits), dl, NVT);
3055 SatMax = DAG.getSetCC(dl, BoolNVT, ResultHH, HHLoMask, ISD::SETGT);
3056 SatMin = DAG.getSetCC(dl, BoolNVT, ResultHH, HHHiMask, ISD::SETLT);
3057 } else
3058 llvm_unreachable("Illegal scale for signed fixed point mul.");
3060 // Saturate to signed maximum.
3061 APInt MaxHi = APInt::getSignedMaxValue(NVTSize);
3062 APInt MaxLo = APInt::getAllOnesValue(NVTSize);
3063 Hi = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxHi, dl, NVT), Hi);
3064 Lo = DAG.getSelect(dl, NVT, SatMax, DAG.getConstant(MaxLo, dl, NVT), Lo);
3065 // Saturate to signed minimum.
3066 APInt MinHi = APInt::getSignedMinValue(NVTSize);
3067 Hi = DAG.getSelect(dl, NVT, SatMin, DAG.getConstant(MinHi, dl, NVT), Hi);
3068 Lo = DAG.getSelect(dl, NVT, SatMin, NVTZero, Lo);
3071 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
3072 SDValue &Lo, SDValue &Hi) {
3073 SDValue LHS = Node->getOperand(0);
3074 SDValue RHS = Node->getOperand(1);
3075 SDLoc dl(Node);
3077 // Expand the result by simply replacing it with the equivalent
3078 // non-overflow-checking operation.
3079 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
3080 ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
3081 LHS, RHS);
3082 SplitInteger(Sum, Lo, Hi);
3084 // Compute the overflow.
3086 // LHSSign -> LHS >= 0
3087 // RHSSign -> RHS >= 0
3088 // SumSign -> Sum >= 0
3090 // Add:
3091 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
3092 // Sub:
3093 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
3095 EVT OType = Node->getValueType(1);
3096 SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
3098 SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
3099 SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
3100 SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
3101 Node->getOpcode() == ISD::SADDO ?
3102 ISD::SETEQ : ISD::SETNE);
3104 SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
3105 SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
3107 SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
3109 // Use the calculated overflow everywhere.
3110 ReplaceValueWith(SDValue(Node, 1), Cmp);
3113 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
3114 SDValue &Lo, SDValue &Hi) {
3115 EVT VT = N->getValueType(0);
3116 SDLoc dl(N);
3117 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3119 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3120 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3121 SplitInteger(Res.getValue(0), Lo, Hi);
3122 return;
3125 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3126 if (VT == MVT::i16)
3127 LC = RTLIB::SDIV_I16;
3128 else if (VT == MVT::i32)
3129 LC = RTLIB::SDIV_I32;
3130 else if (VT == MVT::i64)
3131 LC = RTLIB::SDIV_I64;
3132 else if (VT == MVT::i128)
3133 LC = RTLIB::SDIV_I128;
3134 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
3136 TargetLowering::MakeLibCallOptions CallOptions;
3137 CallOptions.setSExt(true);
3138 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3141 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
3142 SDValue &Lo, SDValue &Hi) {
3143 EVT VT = N->getValueType(0);
3144 SDLoc dl(N);
3146 // If we can emit an efficient shift operation, do so now. Check to see if
3147 // the RHS is a constant.
3148 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
3149 return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
3151 // If we can determine that the high bit of the shift is zero or one, even if
3152 // the low bits are variable, emit this shift in an optimized form.
3153 if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
3154 return;
3156 // If this target supports shift_PARTS, use it. First, map to the _PARTS opc.
3157 unsigned PartsOpc;
3158 if (N->getOpcode() == ISD::SHL) {
3159 PartsOpc = ISD::SHL_PARTS;
3160 } else if (N->getOpcode() == ISD::SRL) {
3161 PartsOpc = ISD::SRL_PARTS;
3162 } else {
3163 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3164 PartsOpc = ISD::SRA_PARTS;
3167 // Next check to see if the target supports this SHL_PARTS operation or if it
3168 // will custom expand it. Don't lower this to SHL_PARTS when we optimise for
3169 // size, but create a libcall instead.
3170 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3171 TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
3172 const bool LegalOrCustom =
3173 (Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
3174 Action == TargetLowering::Custom;
3176 if (LegalOrCustom && TLI.shouldExpandShift(DAG, N)) {
3177 // Expand the subcomponents.
3178 SDValue LHSL, LHSH;
3179 GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
3180 EVT VT = LHSL.getValueType();
3182 // If the shift amount operand is coming from a vector legalization it may
3183 // have an illegal type. Fix that first by casting the operand, otherwise
3184 // the new SHL_PARTS operation would need further legalization.
3185 SDValue ShiftOp = N->getOperand(1);
3186 EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3187 assert(ShiftTy.getScalarSizeInBits() >=
3188 Log2_32_Ceil(VT.getScalarSizeInBits()) &&
3189 "ShiftAmountTy is too small to cover the range of this type!");
3190 if (ShiftOp.getValueType() != ShiftTy)
3191 ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
3193 SDValue Ops[] = { LHSL, LHSH, ShiftOp };
3194 Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
3195 Hi = Lo.getValue(1);
3196 return;
3199 // Otherwise, emit a libcall.
3200 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3201 bool isSigned;
3202 if (N->getOpcode() == ISD::SHL) {
3203 isSigned = false; /*sign irrelevant*/
3204 if (VT == MVT::i16)
3205 LC = RTLIB::SHL_I16;
3206 else if (VT == MVT::i32)
3207 LC = RTLIB::SHL_I32;
3208 else if (VT == MVT::i64)
3209 LC = RTLIB::SHL_I64;
3210 else if (VT == MVT::i128)
3211 LC = RTLIB::SHL_I128;
3212 } else if (N->getOpcode() == ISD::SRL) {
3213 isSigned = false;
3214 if (VT == MVT::i16)
3215 LC = RTLIB::SRL_I16;
3216 else if (VT == MVT::i32)
3217 LC = RTLIB::SRL_I32;
3218 else if (VT == MVT::i64)
3219 LC = RTLIB::SRL_I64;
3220 else if (VT == MVT::i128)
3221 LC = RTLIB::SRL_I128;
3222 } else {
3223 assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
3224 isSigned = true;
3225 if (VT == MVT::i16)
3226 LC = RTLIB::SRA_I16;
3227 else if (VT == MVT::i32)
3228 LC = RTLIB::SRA_I32;
3229 else if (VT == MVT::i64)
3230 LC = RTLIB::SRA_I64;
3231 else if (VT == MVT::i128)
3232 LC = RTLIB::SRA_I128;
3235 if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
3236 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3237 TargetLowering::MakeLibCallOptions CallOptions;
3238 CallOptions.setSExt(isSigned);
3239 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3240 return;
3243 if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
3244 llvm_unreachable("Unsupported shift!");
3247 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
3248 SDValue &Lo, SDValue &Hi) {
3249 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3250 SDLoc dl(N);
3251 SDValue Op = N->getOperand(0);
3252 if (Op.getValueType().bitsLE(NVT)) {
3253 // The low part is sign extension of the input (degenerates to a copy).
3254 Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
3255 // The high part is obtained by SRA'ing all but one of the bits of low part.
3256 unsigned LoSize = NVT.getSizeInBits();
3257 Hi = DAG.getNode(
3258 ISD::SRA, dl, NVT, Lo,
3259 DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
3260 } else {
3261 // For example, extension of an i48 to an i64. The operand type necessarily
3262 // promotes to the result type, so will end up being expanded too.
3263 assert(getTypeAction(Op.getValueType()) ==
3264 TargetLowering::TypePromoteInteger &&
3265 "Only know how to promote this result!");
3266 SDValue Res = GetPromotedInteger(Op);
3267 assert(Res.getValueType() == N->getValueType(0) &&
3268 "Operand over promoted?");
3269 // Split the promoted operand. This will simplify when it is expanded.
3270 SplitInteger(Res, Lo, Hi);
3271 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
3272 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
3273 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3274 ExcessBits)));
3278 void DAGTypeLegalizer::
3279 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
3280 SDLoc dl(N);
3281 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3282 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3284 if (EVT.bitsLE(Lo.getValueType())) {
3285 // sext_inreg the low part if needed.
3286 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
3287 N->getOperand(1));
3289 // The high part gets the sign extension from the lo-part. This handles
3290 // things like sextinreg V:i64 from i8.
3291 Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
3292 DAG.getConstant(Hi.getValueSizeInBits() - 1, dl,
3293 TLI.getPointerTy(DAG.getDataLayout())));
3294 } else {
3295 // For example, extension of an i48 to an i64. Leave the low part alone,
3296 // sext_inreg the high part.
3297 unsigned ExcessBits = EVT.getSizeInBits() - Lo.getValueSizeInBits();
3298 Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
3299 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
3300 ExcessBits)));
3304 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
3305 SDValue &Lo, SDValue &Hi) {
3306 EVT VT = N->getValueType(0);
3307 SDLoc dl(N);
3308 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3310 if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
3311 SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3312 SplitInteger(Res.getValue(1), Lo, Hi);
3313 return;
3316 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3317 if (VT == MVT::i16)
3318 LC = RTLIB::SREM_I16;
3319 else if (VT == MVT::i32)
3320 LC = RTLIB::SREM_I32;
3321 else if (VT == MVT::i64)
3322 LC = RTLIB::SREM_I64;
3323 else if (VT == MVT::i128)
3324 LC = RTLIB::SREM_I128;
3325 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
3327 TargetLowering::MakeLibCallOptions CallOptions;
3328 CallOptions.setSExt(true);
3329 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3332 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
3333 SDValue &Lo, SDValue &Hi) {
3334 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3335 SDLoc dl(N);
3336 Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
3337 Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
3338 N->getOperand(0),
3339 DAG.getConstant(NVT.getSizeInBits(), dl,
3340 TLI.getPointerTy(DAG.getDataLayout())));
3341 Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
3344 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
3345 SDValue &Lo, SDValue &Hi) {
3346 EVT VT = N->getValueType(0);
3347 SDLoc dl(N);
3349 if (N->getOpcode() == ISD::UMULO) {
3350 // This section expands the operation into the following sequence of
3351 // instructions. `iNh` here refers to a type which has half the bit width of
3352 // the type the original operation operated on.
3354 // %0 = %LHS.HI != 0 && %RHS.HI != 0
3355 // %1 = { iNh, i1 } @umul.with.overflow.iNh(iNh %LHS.HI, iNh %RHS.LO)
3356 // %2 = { iNh, i1 } @umul.with.overflow.iNh(iNh %RHS.HI, iNh %LHS.LO)
3357 // %3 = mul nuw iN (%LHS.LOW as iN), (%RHS.LOW as iN)
3358 // %4 = add iN (%1.0 as iN) << Nh, (%2.0 as iN) << Nh
3359 // %5 = { iN, i1 } @uadd.with.overflow.iN( %4, %3 )
3361 // %res = { %5.0, %0 || %1.1 || %2.1 || %5.1 }
3362 SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
3363 SDValue LHSHigh, LHSLow, RHSHigh, RHSLow;
3364 SplitInteger(LHS, LHSLow, LHSHigh);
3365 SplitInteger(RHS, RHSLow, RHSHigh);
3366 EVT HalfVT = LHSLow.getValueType()
3367 , BitVT = N->getValueType(1);
3368 SDVTList VTHalfMulO = DAG.getVTList(HalfVT, BitVT);
3369 SDVTList VTFullAddO = DAG.getVTList(VT, BitVT);
3371 SDValue HalfZero = DAG.getConstant(0, dl, HalfVT);
3372 SDValue Overflow = DAG.getNode(ISD::AND, dl, BitVT,
3373 DAG.getSetCC(dl, BitVT, LHSHigh, HalfZero, ISD::SETNE),
3374 DAG.getSetCC(dl, BitVT, RHSHigh, HalfZero, ISD::SETNE));
3376 SDValue One = DAG.getNode(ISD::UMULO, dl, VTHalfMulO, LHSHigh, RHSLow);
3377 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, One.getValue(1));
3378 SDValue OneInHigh = DAG.getNode(ISD::BUILD_PAIR, dl, VT, HalfZero,
3379 One.getValue(0));
3381 SDValue Two = DAG.getNode(ISD::UMULO, dl, VTHalfMulO, RHSHigh, LHSLow);
3382 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Two.getValue(1));
3383 SDValue TwoInHigh = DAG.getNode(ISD::BUILD_PAIR, dl, VT, HalfZero,
3384 Two.getValue(0));
3386 // Cannot use `UMUL_LOHI` directly, because some 32-bit targets (ARM) do not
3387 // know how to expand `i64,i64 = umul_lohi a, b` and abort (why isn’t this
3388 // operation recursively legalized?).
3390 // Many backends understand this pattern and will convert into LOHI
3391 // themselves, if applicable.
3392 SDValue Three = DAG.getNode(ISD::MUL, dl, VT,
3393 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, LHSLow),
3394 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, RHSLow));
3395 SDValue Four = DAG.getNode(ISD::ADD, dl, VT, OneInHigh, TwoInHigh);
3396 SDValue Five = DAG.getNode(ISD::UADDO, dl, VTFullAddO, Three, Four);
3397 Overflow = DAG.getNode(ISD::OR, dl, BitVT, Overflow, Five.getValue(1));
3398 SplitInteger(Five, Lo, Hi);
3399 ReplaceValueWith(SDValue(N, 1), Overflow);
3400 return;
3403 Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
3404 EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
3405 Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
3407 // Replace this with a libcall that will check overflow.
3408 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3409 if (VT == MVT::i32)
3410 LC = RTLIB::MULO_I32;
3411 else if (VT == MVT::i64)
3412 LC = RTLIB::MULO_I64;
3413 else if (VT == MVT::i128)
3414 LC = RTLIB::MULO_I128;
3415 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!");
3417 SDValue Temp = DAG.CreateStackTemporary(PtrVT);
3418 // Temporary for the overflow value, default it to zero.
3419 SDValue Chain =
3420 DAG.getStore(DAG.getEntryNode(), dl, DAG.getConstant(0, dl, PtrVT), Temp,
3421 MachinePointerInfo());
3423 TargetLowering::ArgListTy Args;
3424 TargetLowering::ArgListEntry Entry;
3425 for (const SDValue &Op : N->op_values()) {
3426 EVT ArgVT = Op.getValueType();
3427 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
3428 Entry.Node = Op;
3429 Entry.Ty = ArgTy;
3430 Entry.IsSExt = true;
3431 Entry.IsZExt = false;
3432 Args.push_back(Entry);
3435 // Also pass the address of the overflow check.
3436 Entry.Node = Temp;
3437 Entry.Ty = PtrTy->getPointerTo();
3438 Entry.IsSExt = true;
3439 Entry.IsZExt = false;
3440 Args.push_back(Entry);
3442 SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
3444 TargetLowering::CallLoweringInfo CLI(DAG);
3445 CLI.setDebugLoc(dl)
3446 .setChain(Chain)
3447 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args))
3448 .setSExtResult();
3450 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
3452 SplitInteger(CallInfo.first, Lo, Hi);
3453 SDValue Temp2 =
3454 DAG.getLoad(PtrVT, dl, CallInfo.second, Temp, MachinePointerInfo());
3455 SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
3456 DAG.getConstant(0, dl, PtrVT),
3457 ISD::SETNE);
3458 // Use the overflow from the libcall everywhere.
3459 ReplaceValueWith(SDValue(N, 1), Ofl);
3462 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
3463 SDValue &Lo, SDValue &Hi) {
3464 EVT VT = N->getValueType(0);
3465 SDLoc dl(N);
3466 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3468 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
3469 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3470 SplitInteger(Res.getValue(0), Lo, Hi);
3471 return;
3474 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3475 if (VT == MVT::i16)
3476 LC = RTLIB::UDIV_I16;
3477 else if (VT == MVT::i32)
3478 LC = RTLIB::UDIV_I32;
3479 else if (VT == MVT::i64)
3480 LC = RTLIB::UDIV_I64;
3481 else if (VT == MVT::i128)
3482 LC = RTLIB::UDIV_I128;
3483 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
3485 TargetLowering::MakeLibCallOptions CallOptions;
3486 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3489 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
3490 SDValue &Lo, SDValue &Hi) {
3491 EVT VT = N->getValueType(0);
3492 SDLoc dl(N);
3493 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
3495 if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
3496 SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
3497 SplitInteger(Res.getValue(1), Lo, Hi);
3498 return;
3501 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3502 if (VT == MVT::i16)
3503 LC = RTLIB::UREM_I16;
3504 else if (VT == MVT::i32)
3505 LC = RTLIB::UREM_I32;
3506 else if (VT == MVT::i64)
3507 LC = RTLIB::UREM_I64;
3508 else if (VT == MVT::i128)
3509 LC = RTLIB::UREM_I128;
3510 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
3512 TargetLowering::MakeLibCallOptions CallOptions;
3513 SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, CallOptions, dl).first, Lo, Hi);
3516 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
3517 SDValue &Lo, SDValue &Hi) {
3518 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
3519 SDLoc dl(N);
3520 SDValue Op = N->getOperand(0);
3521 if (Op.getValueType().bitsLE(NVT)) {
3522 // The low part is zero extension of the input (degenerates to a copy).
3523 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
3524 Hi = DAG.getConstant(0, dl, NVT); // The high part is just a zero.
3525 } else {
3526 // For example, extension of an i48 to an i64. The operand type necessarily
3527 // promotes to the result type, so will end up being expanded too.
3528 assert(getTypeAction(Op.getValueType()) ==
3529 TargetLowering::TypePromoteInteger &&
3530 "Only know how to promote this result!");
3531 SDValue Res = GetPromotedInteger(Op);
3532 assert(Res.getValueType() == N->getValueType(0) &&
3533 "Operand over promoted?");
3534 // Split the promoted operand. This will simplify when it is expanded.
3535 SplitInteger(Res, Lo, Hi);
3536 unsigned ExcessBits = Op.getValueSizeInBits() - NVT.getSizeInBits();
3537 Hi = DAG.getZeroExtendInReg(Hi, dl,
3538 EVT::getIntegerVT(*DAG.getContext(),
3539 ExcessBits));
3543 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
3544 SDValue &Lo, SDValue &Hi) {
3545 SDLoc dl(N);
3546 EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
3547 SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
3548 SDValue Zero = DAG.getConstant(0, dl, VT);
3549 SDValue Swap = DAG.getAtomicCmpSwap(
3550 ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
3551 cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
3552 N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand());
3554 ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
3555 ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
3558 void DAGTypeLegalizer::ExpandIntRes_VECREDUCE(SDNode *N,
3559 SDValue &Lo, SDValue &Hi) {
3560 // TODO For VECREDUCE_(AND|OR|XOR) we could split the vector and calculate
3561 // both halves independently.
3562 SDValue Res = TLI.expandVecReduce(N, DAG);
3563 SplitInteger(Res, Lo, Hi);
3566 //===----------------------------------------------------------------------===//
3567 // Integer Operand Expansion
3568 //===----------------------------------------------------------------------===//
3570 /// ExpandIntegerOperand - This method is called when the specified operand of
3571 /// the specified node is found to need expansion. At this point, all of the
3572 /// result types of the node are known to be legal, but other operands of the
3573 /// node may need promotion or expansion as well as the specified one.
3574 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
3575 LLVM_DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG);
3576 dbgs() << "\n");
3577 SDValue Res = SDValue();
3579 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
3580 return false;
3582 switch (N->getOpcode()) {
3583 default:
3584 #ifndef NDEBUG
3585 dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
3586 N->dump(&DAG); dbgs() << "\n";
3587 #endif
3588 report_fatal_error("Do not know how to expand this operator's operand!");
3590 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
3591 case ISD::BR_CC: Res = ExpandIntOp_BR_CC(N); break;
3592 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
3593 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
3594 case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
3595 case ISD::SCALAR_TO_VECTOR: Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
3596 case ISD::SELECT_CC: Res = ExpandIntOp_SELECT_CC(N); break;
3597 case ISD::SETCC: Res = ExpandIntOp_SETCC(N); break;
3598 case ISD::SETCCCARRY: Res = ExpandIntOp_SETCCCARRY(N); break;
3599 case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
3600 case ISD::STORE: Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
3601 case ISD::TRUNCATE: Res = ExpandIntOp_TRUNCATE(N); break;
3602 case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
3604 case ISD::SHL:
3605 case ISD::SRA:
3606 case ISD::SRL:
3607 case ISD::ROTL:
3608 case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
3609 case ISD::RETURNADDR:
3610 case ISD::FRAMEADDR: Res = ExpandIntOp_RETURNADDR(N); break;
3612 case ISD::ATOMIC_STORE: Res = ExpandIntOp_ATOMIC_STORE(N); break;
3615 // If the result is null, the sub-method took care of registering results etc.
3616 if (!Res.getNode()) return false;
3618 // If the result is N, the sub-method updated N in place. Tell the legalizer
3619 // core about this.
3620 if (Res.getNode() == N)
3621 return true;
3623 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
3624 "Invalid operand expansion");
3626 ReplaceValueWith(SDValue(N, 0), Res);
3627 return false;
3630 /// IntegerExpandSetCCOperands - Expand the operands of a comparison. This code
3631 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
3632 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
3633 SDValue &NewRHS,
3634 ISD::CondCode &CCCode,
3635 const SDLoc &dl) {
3636 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
3637 GetExpandedInteger(NewLHS, LHSLo, LHSHi);
3638 GetExpandedInteger(NewRHS, RHSLo, RHSHi);
3640 if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
3641 if (RHSLo == RHSHi) {
3642 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
3643 if (RHSCST->isAllOnesValue()) {
3644 // Equality comparison to -1.
3645 NewLHS = DAG.getNode(ISD::AND, dl,
3646 LHSLo.getValueType(), LHSLo, LHSHi);
3647 NewRHS = RHSLo;
3648 return;
3653 NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
3654 NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
3655 NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
3656 NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
3657 return;
3660 // If this is a comparison of the sign bit, just look at the top part.
3661 // X > -1, x < 0
3662 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
3663 if ((CCCode == ISD::SETLT && CST->isNullValue()) || // X < 0
3664 (CCCode == ISD::SETGT && CST->isAllOnesValue())) { // X > -1
3665 NewLHS = LHSHi;
3666 NewRHS = RHSHi;
3667 return;
3670 // FIXME: This generated code sucks.
3671 ISD::CondCode LowCC;
3672 switch (CCCode) {
3673 default: llvm_unreachable("Unknown integer setcc!");
3674 case ISD::SETLT:
3675 case ISD::SETULT: LowCC = ISD::SETULT; break;
3676 case ISD::SETGT:
3677 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
3678 case ISD::SETLE:
3679 case ISD::SETULE: LowCC = ISD::SETULE; break;
3680 case ISD::SETGE:
3681 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
3684 // LoCmp = lo(op1) < lo(op2) // Always unsigned comparison
3685 // HiCmp = hi(op1) < hi(op2) // Signedness depends on operands
3686 // dest = hi(op1) == hi(op2) ? LoCmp : HiCmp;
3688 // NOTE: on targets without efficient SELECT of bools, we can always use
3689 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
3690 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
3691 nullptr);
3692 SDValue LoCmp, HiCmp;
3693 if (TLI.isTypeLegal(LHSLo.getValueType()) &&
3694 TLI.isTypeLegal(RHSLo.getValueType()))
3695 LoCmp = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()), LHSLo,
3696 RHSLo, LowCC, false, DagCombineInfo, dl);
3697 if (!LoCmp.getNode())
3698 LoCmp = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()), LHSLo,
3699 RHSLo, LowCC);
3700 if (TLI.isTypeLegal(LHSHi.getValueType()) &&
3701 TLI.isTypeLegal(RHSHi.getValueType()))
3702 HiCmp = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()), LHSHi,
3703 RHSHi, CCCode, false, DagCombineInfo, dl);
3704 if (!HiCmp.getNode())
3705 HiCmp =
3706 DAG.getNode(ISD::SETCC, dl, getSetCCResultType(LHSHi.getValueType()),
3707 LHSHi, RHSHi, DAG.getCondCode(CCCode));
3709 ConstantSDNode *LoCmpC = dyn_cast<ConstantSDNode>(LoCmp.getNode());
3710 ConstantSDNode *HiCmpC = dyn_cast<ConstantSDNode>(HiCmp.getNode());
3712 bool EqAllowed = (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
3713 CCCode == ISD::SETUGE || CCCode == ISD::SETULE);
3715 if ((EqAllowed && (HiCmpC && HiCmpC->isNullValue())) ||
3716 (!EqAllowed && ((HiCmpC && (HiCmpC->getAPIntValue() == 1)) ||
3717 (LoCmpC && LoCmpC->isNullValue())))) {
3718 // For LE / GE, if high part is known false, ignore the low part.
3719 // For LT / GT: if low part is known false, return the high part.
3720 // if high part is known true, ignore the low part.
3721 NewLHS = HiCmp;
3722 NewRHS = SDValue();
3723 return;
3726 if (LHSHi == RHSHi) {
3727 // Comparing the low bits is enough.
3728 NewLHS = LoCmp;
3729 NewRHS = SDValue();
3730 return;
3733 // Lower with SETCCCARRY if the target supports it.
3734 EVT HiVT = LHSHi.getValueType();
3735 EVT ExpandVT = TLI.getTypeToExpandTo(*DAG.getContext(), HiVT);
3736 bool HasSETCCCARRY = TLI.isOperationLegalOrCustom(ISD::SETCCCARRY, ExpandVT);
3738 // FIXME: Make all targets support this, then remove the other lowering.
3739 if (HasSETCCCARRY) {
3740 // SETCCCARRY can detect < and >= directly. For > and <=, flip
3741 // operands and condition code.
3742 bool FlipOperands = false;
3743 switch (CCCode) {
3744 case ISD::SETGT: CCCode = ISD::SETLT; FlipOperands = true; break;
3745 case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
3746 case ISD::SETLE: CCCode = ISD::SETGE; FlipOperands = true; break;
3747 case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
3748 default: break;
3750 if (FlipOperands) {
3751 std::swap(LHSLo, RHSLo);
3752 std::swap(LHSHi, RHSHi);
3754 // Perform a wide subtraction, feeding the carry from the low part into
3755 // SETCCCARRY. The SETCCCARRY operation is essentially looking at the high
3756 // part of the result of LHS - RHS. It is negative iff LHS < RHS. It is
3757 // zero or positive iff LHS >= RHS.
3758 EVT LoVT = LHSLo.getValueType();
3759 SDVTList VTList = DAG.getVTList(LoVT, getSetCCResultType(LoVT));
3760 SDValue LowCmp = DAG.getNode(ISD::USUBO, dl, VTList, LHSLo, RHSLo);
3761 SDValue Res = DAG.getNode(ISD::SETCCCARRY, dl, getSetCCResultType(HiVT),
3762 LHSHi, RHSHi, LowCmp.getValue(1),
3763 DAG.getCondCode(CCCode));
3764 NewLHS = Res;
3765 NewRHS = SDValue();
3766 return;
3769 NewLHS = TLI.SimplifySetCC(getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ,
3770 false, DagCombineInfo, dl);
3771 if (!NewLHS.getNode())
3772 NewLHS =
3773 DAG.getSetCC(dl, getSetCCResultType(HiVT), LHSHi, RHSHi, ISD::SETEQ);
3774 NewLHS = DAG.getSelect(dl, LoCmp.getValueType(), NewLHS, LoCmp, HiCmp);
3775 NewRHS = SDValue();
3778 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
3779 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
3780 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
3781 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
3783 // If ExpandSetCCOperands returned a scalar, we need to compare the result
3784 // against zero to select between true and false values.
3785 if (!NewRHS.getNode()) {
3786 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
3787 CCCode = ISD::SETNE;
3790 // Update N to have the operands specified.
3791 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
3792 DAG.getCondCode(CCCode), NewLHS, NewRHS,
3793 N->getOperand(4)), 0);
3796 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
3797 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
3798 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
3799 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
3801 // If ExpandSetCCOperands returned a scalar, we need to compare the result
3802 // against zero to select between true and false values.
3803 if (!NewRHS.getNode()) {
3804 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
3805 CCCode = ISD::SETNE;
3808 // Update N to have the operands specified.
3809 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
3810 N->getOperand(2), N->getOperand(3),
3811 DAG.getCondCode(CCCode)), 0);
3814 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
3815 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
3816 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
3817 IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
3819 // If ExpandSetCCOperands returned a scalar, use it.
3820 if (!NewRHS.getNode()) {
3821 assert(NewLHS.getValueType() == N->getValueType(0) &&
3822 "Unexpected setcc expansion!");
3823 return NewLHS;
3826 // Otherwise, update N to have the operands specified.
3827 return SDValue(
3828 DAG.UpdateNodeOperands(N, NewLHS, NewRHS, DAG.getCondCode(CCCode)), 0);
3831 SDValue DAGTypeLegalizer::ExpandIntOp_SETCCCARRY(SDNode *N) {
3832 SDValue LHS = N->getOperand(0);
3833 SDValue RHS = N->getOperand(1);
3834 SDValue Carry = N->getOperand(2);
3835 SDValue Cond = N->getOperand(3);
3836 SDLoc dl = SDLoc(N);
3838 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
3839 GetExpandedInteger(LHS, LHSLo, LHSHi);
3840 GetExpandedInteger(RHS, RHSLo, RHSHi);
3842 // Expand to a SUBE for the low part and a smaller SETCCCARRY for the high.
3843 SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), Carry.getValueType());
3844 SDValue LowCmp = DAG.getNode(ISD::SUBCARRY, dl, VTList, LHSLo, RHSLo, Carry);
3845 return DAG.getNode(ISD::SETCCCARRY, dl, N->getValueType(0), LHSHi, RHSHi,
3846 LowCmp.getValue(1), Cond);
3849 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
3850 // The value being shifted is legal, but the shift amount is too big.
3851 // It follows that either the result of the shift is undefined, or the
3852 // upper half of the shift amount is zero. Just use the lower half.
3853 SDValue Lo, Hi;
3854 GetExpandedInteger(N->getOperand(1), Lo, Hi);
3855 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
3858 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
3859 // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant. This
3860 // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
3861 // constant to valid type.
3862 SDValue Lo, Hi;
3863 GetExpandedInteger(N->getOperand(0), Lo, Hi);
3864 return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
3867 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
3868 SDValue Op = N->getOperand(0);
3869 EVT DstVT = N->getValueType(0);
3870 RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
3871 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
3872 "Don't know how to expand this SINT_TO_FP!");
3873 TargetLowering::MakeLibCallOptions CallOptions;
3874 CallOptions.setSExt(true);
3875 return TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, SDLoc(N)).first;
3878 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
3879 if (ISD::isNormalStore(N))
3880 return ExpandOp_NormalStore(N, OpNo);
3882 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
3883 assert(OpNo == 1 && "Can only expand the stored value so far");
3885 EVT VT = N->getOperand(1).getValueType();
3886 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
3887 SDValue Ch = N->getChain();
3888 SDValue Ptr = N->getBasePtr();
3889 unsigned Alignment = N->getAlignment();
3890 MachineMemOperand::Flags MMOFlags = N->getMemOperand()->getFlags();
3891 AAMDNodes AAInfo = N->getAAInfo();
3892 SDLoc dl(N);
3893 SDValue Lo, Hi;
3895 assert(NVT.isByteSized() && "Expanded type not byte sized!");
3897 if (N->getMemoryVT().bitsLE(NVT)) {
3898 GetExpandedInteger(N->getValue(), Lo, Hi);
3899 return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
3900 N->getMemoryVT(), Alignment, MMOFlags, AAInfo);
3903 if (DAG.getDataLayout().isLittleEndian()) {
3904 // Little-endian - low bits are at low addresses.
3905 GetExpandedInteger(N->getValue(), Lo, Hi);
3907 Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(), Alignment, MMOFlags,
3908 AAInfo);
3910 unsigned ExcessBits =
3911 N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
3912 EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
3914 // Increment the pointer to the other half.
3915 unsigned IncrementSize = NVT.getSizeInBits()/8;
3916 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3917 Hi = DAG.getTruncStore(
3918 Ch, dl, Hi, Ptr, N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
3919 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
3920 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
3923 // Big-endian - high bits are at low addresses. Favor aligned stores at
3924 // the cost of some bit-fiddling.
3925 GetExpandedInteger(N->getValue(), Lo, Hi);
3927 EVT ExtVT = N->getMemoryVT();
3928 unsigned EBytes = ExtVT.getStoreSize();
3929 unsigned IncrementSize = NVT.getSizeInBits()/8;
3930 unsigned ExcessBits = (EBytes - IncrementSize)*8;
3931 EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
3932 ExtVT.getSizeInBits() - ExcessBits);
3934 if (ExcessBits < NVT.getSizeInBits()) {
3935 // Transfer high bits from the top of Lo to the bottom of Hi.
3936 Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
3937 DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
3938 TLI.getPointerTy(DAG.getDataLayout())));
3939 Hi = DAG.getNode(
3940 ISD::OR, dl, NVT, Hi,
3941 DAG.getNode(ISD::SRL, dl, NVT, Lo,
3942 DAG.getConstant(ExcessBits, dl,
3943 TLI.getPointerTy(DAG.getDataLayout()))));
3946 // Store both the high bits and maybe some of the low bits.
3947 Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(), HiVT, Alignment,
3948 MMOFlags, AAInfo);
3950 // Increment the pointer to the other half.
3951 Ptr = DAG.getObjectPtrOffset(dl, Ptr, IncrementSize);
3952 // Store the lowest ExcessBits bits in the second half.
3953 Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
3954 N->getPointerInfo().getWithOffset(IncrementSize),
3955 EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
3956 MinAlign(Alignment, IncrementSize), MMOFlags, AAInfo);
3957 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
3960 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
3961 SDValue InL, InH;
3962 GetExpandedInteger(N->getOperand(0), InL, InH);
3963 // Just truncate the low part of the source.
3964 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
3967 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
3968 SDValue Op = N->getOperand(0);
3969 EVT SrcVT = Op.getValueType();
3970 EVT DstVT = N->getValueType(0);
3971 SDLoc dl(N);
3973 // The following optimization is valid only if every value in SrcVT (when
3974 // treated as signed) is representable in DstVT. Check that the mantissa
3975 // size of DstVT is >= than the number of bits in SrcVT -1.
3976 const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT);
3977 if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 &&
3978 TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
3979 // Do a signed conversion then adjust the result.
3980 SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
3981 SignedConv = TLI.LowerOperation(SignedConv, DAG);
3983 // The result of the signed conversion needs adjusting if the 'sign bit' of
3984 // the incoming integer was set. To handle this, we dynamically test to see
3985 // if it is set, and, if so, add a fudge factor.
3987 const uint64_t F32TwoE32 = 0x4F800000ULL;
3988 const uint64_t F32TwoE64 = 0x5F800000ULL;
3989 const uint64_t F32TwoE128 = 0x7F800000ULL;
3991 APInt FF(32, 0);
3992 if (SrcVT == MVT::i32)
3993 FF = APInt(32, F32TwoE32);
3994 else if (SrcVT == MVT::i64)
3995 FF = APInt(32, F32TwoE64);
3996 else if (SrcVT == MVT::i128)
3997 FF = APInt(32, F32TwoE128);
3998 else
3999 llvm_unreachable("Unsupported UINT_TO_FP!");
4001 // Check whether the sign bit is set.
4002 SDValue Lo, Hi;
4003 GetExpandedInteger(Op, Lo, Hi);
4004 SDValue SignSet = DAG.getSetCC(dl,
4005 getSetCCResultType(Hi.getValueType()),
4007 DAG.getConstant(0, dl, Hi.getValueType()),
4008 ISD::SETLT);
4010 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
4011 SDValue FudgePtr =
4012 DAG.getConstantPool(ConstantInt::get(*DAG.getContext(), FF.zext(64)),
4013 TLI.getPointerTy(DAG.getDataLayout()));
4015 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
4016 SDValue Zero = DAG.getIntPtrConstant(0, dl);
4017 SDValue Four = DAG.getIntPtrConstant(4, dl);
4018 if (DAG.getDataLayout().isBigEndian())
4019 std::swap(Zero, Four);
4020 SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet,
4021 Zero, Four);
4022 unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
4023 FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(),
4024 FudgePtr, Offset);
4025 Alignment = std::min(Alignment, 4u);
4027 // Load the value out, extending it from f32 to the destination float type.
4028 // FIXME: Avoid the extend by constructing the right constant pool?
4029 SDValue Fudge = DAG.getExtLoad(
4030 ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(), FudgePtr,
4031 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
4032 Alignment);
4033 return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
4036 // Otherwise, use a libcall.
4037 RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
4038 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4039 "Don't know how to expand this UINT_TO_FP!");
4040 TargetLowering::MakeLibCallOptions CallOptions;
4041 CallOptions.setSExt(true);
4042 return TLI.makeLibCall(DAG, LC, DstVT, Op, CallOptions, dl).first;
4045 SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
4046 SDLoc dl(N);
4047 SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
4048 cast<AtomicSDNode>(N)->getMemoryVT(),
4049 N->getOperand(0),
4050 N->getOperand(1), N->getOperand(2),
4051 cast<AtomicSDNode>(N)->getMemOperand());
4052 return Swap.getValue(1);
4056 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
4058 EVT OutVT = N->getValueType(0);
4059 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4060 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4061 unsigned OutNumElems = OutVT.getVectorNumElements();
4062 EVT NOutVTElem = NOutVT.getVectorElementType();
4064 SDLoc dl(N);
4065 SDValue BaseIdx = N->getOperand(1);
4067 SDValue InOp0 = N->getOperand(0);
4068 if (getTypeAction(InOp0.getValueType()) == TargetLowering::TypePromoteInteger)
4069 InOp0 = GetPromotedInteger(N->getOperand(0));
4071 EVT InVT = InOp0.getValueType();
4073 SmallVector<SDValue, 8> Ops;
4074 Ops.reserve(OutNumElems);
4075 for (unsigned i = 0; i != OutNumElems; ++i) {
4077 // Extract the element from the original vector.
4078 SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
4079 BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
4080 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
4081 InVT.getVectorElementType(), N->getOperand(0), Index);
4083 SDValue Op = DAG.getAnyExtOrTrunc(Ext, dl, NOutVTElem);
4084 // Insert the converted element to the new vector.
4085 Ops.push_back(Op);
4088 return DAG.getBuildVector(NOutVT, dl, Ops);
4092 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
4093 ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
4094 EVT VT = N->getValueType(0);
4095 SDLoc dl(N);
4097 ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
4099 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4100 SDValue V1 = GetPromotedInteger(N->getOperand(1));
4101 EVT OutVT = V0.getValueType();
4103 return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
4107 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
4108 EVT OutVT = N->getValueType(0);
4109 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4110 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4111 unsigned NumElems = N->getNumOperands();
4112 EVT NOutVTElem = NOutVT.getVectorElementType();
4114 SDLoc dl(N);
4116 SmallVector<SDValue, 8> Ops;
4117 Ops.reserve(NumElems);
4118 for (unsigned i = 0; i != NumElems; ++i) {
4119 SDValue Op;
4120 // BUILD_VECTOR integer operand types are allowed to be larger than the
4121 // result's element type. This may still be true after the promotion. For
4122 // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
4123 // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
4124 if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
4125 Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
4126 else
4127 Op = N->getOperand(i);
4128 Ops.push_back(Op);
4131 return DAG.getBuildVector(NOutVT, dl, Ops);
4134 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
4136 SDLoc dl(N);
4138 assert(!N->getOperand(0).getValueType().isVector() &&
4139 "Input must be a scalar");
4141 EVT OutVT = N->getValueType(0);
4142 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4143 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4144 EVT NOutVTElem = NOutVT.getVectorElementType();
4146 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
4148 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
4151 SDValue DAGTypeLegalizer::PromoteIntRes_SPLAT_VECTOR(SDNode *N) {
4152 SDLoc dl(N);
4154 SDValue SplatVal = N->getOperand(0);
4156 assert(!SplatVal.getValueType().isVector() && "Input must be a scalar");
4158 EVT OutVT = N->getValueType(0);
4159 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4160 assert(NOutVT.isVector() && "Type must be promoted to a vector type");
4161 EVT NOutElemVT = NOutVT.getVectorElementType();
4163 SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutElemVT, SplatVal);
4165 return DAG.getNode(ISD::SPLAT_VECTOR, dl, NOutVT, Op);
4168 SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
4169 SDLoc dl(N);
4171 EVT OutVT = N->getValueType(0);
4172 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4173 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4175 EVT OutElemTy = NOutVT.getVectorElementType();
4177 unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
4178 unsigned NumOutElem = NOutVT.getVectorNumElements();
4179 unsigned NumOperands = N->getNumOperands();
4180 assert(NumElem * NumOperands == NumOutElem &&
4181 "Unexpected number of elements");
4183 // Take the elements from the first vector.
4184 SmallVector<SDValue, 8> Ops(NumOutElem);
4185 for (unsigned i = 0; i < NumOperands; ++i) {
4186 SDValue Op = N->getOperand(i);
4187 if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteInteger)
4188 Op = GetPromotedInteger(Op);
4189 EVT SclrTy = Op.getValueType().getVectorElementType();
4190 assert(NumElem == Op.getValueType().getVectorNumElements() &&
4191 "Unexpected number of elements");
4193 for (unsigned j = 0; j < NumElem; ++j) {
4194 SDValue Ext = DAG.getNode(
4195 ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Op,
4196 DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4197 Ops[i * NumElem + j] = DAG.getAnyExtOrTrunc(Ext, dl, OutElemTy);
4201 return DAG.getBuildVector(NOutVT, dl, Ops);
4204 SDValue DAGTypeLegalizer::PromoteIntRes_EXTEND_VECTOR_INREG(SDNode *N) {
4205 EVT VT = N->getValueType(0);
4206 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
4207 assert(NVT.isVector() && "This type must be promoted to a vector type");
4209 SDLoc dl(N);
4211 // For operands whose TypeAction is to promote, extend the promoted node
4212 // appropriately (ZERO_EXTEND or SIGN_EXTEND) from the original pre-promotion
4213 // type, and then construct a new *_EXTEND_VECTOR_INREG node to the promote-to
4214 // type..
4215 if (getTypeAction(N->getOperand(0).getValueType())
4216 == TargetLowering::TypePromoteInteger) {
4217 SDValue Promoted;
4219 switch(N->getOpcode()) {
4220 case ISD::SIGN_EXTEND_VECTOR_INREG:
4221 Promoted = SExtPromotedInteger(N->getOperand(0));
4222 break;
4223 case ISD::ZERO_EXTEND_VECTOR_INREG:
4224 Promoted = ZExtPromotedInteger(N->getOperand(0));
4225 break;
4226 case ISD::ANY_EXTEND_VECTOR_INREG:
4227 Promoted = GetPromotedInteger(N->getOperand(0));
4228 break;
4229 default:
4230 llvm_unreachable("Node has unexpected Opcode");
4232 return DAG.getNode(N->getOpcode(), dl, NVT, Promoted);
4235 // Directly extend to the appropriate transform-to type.
4236 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
4239 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
4240 EVT OutVT = N->getValueType(0);
4241 EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
4242 assert(NOutVT.isVector() && "This type must be promoted to a vector type");
4244 EVT NOutVTElem = NOutVT.getVectorElementType();
4246 SDLoc dl(N);
4247 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4249 SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
4250 NOutVTElem, N->getOperand(1));
4251 return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
4252 V0, ConvElem, N->getOperand(2));
4255 SDValue DAGTypeLegalizer::PromoteIntRes_VECREDUCE(SDNode *N) {
4256 // The VECREDUCE result size may be larger than the element size, so
4257 // we can simply change the result type.
4258 SDLoc dl(N);
4259 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
4260 return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
4263 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
4264 SDLoc dl(N);
4265 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4266 SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
4267 TLI.getVectorIdxTy(DAG.getDataLayout()));
4268 SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
4269 V0->getValueType(0).getScalarType(), V0, V1);
4271 // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
4272 // element types. If this is the case then we need to expand the outgoing
4273 // value and not truncate it.
4274 return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
4277 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
4278 SDLoc dl(N);
4279 SDValue V0 = GetPromotedInteger(N->getOperand(0));
4280 MVT InVT = V0.getValueType().getSimpleVT();
4281 MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
4282 N->getValueType(0).getVectorNumElements());
4283 SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
4284 return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
4287 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
4288 SDLoc dl(N);
4289 unsigned NumElems = N->getNumOperands();
4291 EVT RetSclrTy = N->getValueType(0).getVectorElementType();
4293 SmallVector<SDValue, 8> NewOps;
4294 NewOps.reserve(NumElems);
4296 // For each incoming vector
4297 for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
4298 SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
4299 EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
4300 unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
4302 for (unsigned i=0; i<NumElem; ++i) {
4303 // Extract element from incoming vector
4304 SDValue Ex = DAG.getNode(
4305 ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
4306 DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
4307 SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
4308 NewOps.push_back(Tr);
4312 return DAG.getBuildVector(N->getValueType(0), dl, NewOps);