LLVM 20.0.0git
LegalizeDAG.cpp
Go to the documentation of this file.
1//===- LegalizeDAG.cpp - Implement SelectionDAG::Legalize -----------------===//
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 the SelectionDAG::Legalize method.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/SetVector.h"
19#include "llvm/ADT/SmallSet.h"
36#include "llvm/IR/CallingConv.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/DataLayout.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/Metadata.h"
42#include "llvm/IR/Type.h"
45#include "llvm/Support/Debug.h"
51#include <cassert>
52#include <cstdint>
53#include <tuple>
54#include <utility>
55
56using namespace llvm;
57
58#define DEBUG_TYPE "legalizedag"
59
60namespace {
61
62/// Keeps track of state when getting the sign of a floating-point value as an
63/// integer.
64struct FloatSignAsInt {
65 EVT FloatVT;
66 SDValue Chain;
67 SDValue FloatPtr;
68 SDValue IntPtr;
69 MachinePointerInfo IntPointerInfo;
70 MachinePointerInfo FloatPointerInfo;
71 SDValue IntValue;
72 APInt SignMask;
73 uint8_t SignBit;
74};
75
76//===----------------------------------------------------------------------===//
77/// This takes an arbitrary SelectionDAG as input and
78/// hacks on it until the target machine can handle it. This involves
79/// eliminating value sizes the machine cannot handle (promoting small sizes to
80/// large sizes or splitting up large values into small values) as well as
81/// eliminating operations the machine cannot handle.
82///
83/// This code also does a small amount of optimization and recognition of idioms
84/// as part of its processing. For example, if a target does not support a
85/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
86/// will attempt merge setcc and brc instructions into brcc's.
87class SelectionDAGLegalize {
88 const TargetMachine &TM;
89 const TargetLowering &TLI;
90 SelectionDAG &DAG;
91
92 /// The set of nodes which have already been legalized. We hold a
93 /// reference to it in order to update as necessary on node deletion.
94 SmallPtrSetImpl<SDNode *> &LegalizedNodes;
95
96 /// A set of all the nodes updated during legalization.
97 SmallSetVector<SDNode *, 16> *UpdatedNodes;
98
99 EVT getSetCCResultType(EVT VT) const {
100 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
101 }
102
103 // Libcall insertion helpers.
104
105public:
106 SelectionDAGLegalize(SelectionDAG &DAG,
107 SmallPtrSetImpl<SDNode *> &LegalizedNodes,
108 SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
109 : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
110 LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
111
112 /// Legalizes the given operation.
113 void LegalizeOp(SDNode *Node);
114
115private:
116 SDValue OptimizeFloatStore(StoreSDNode *ST);
117
118 void LegalizeLoadOps(SDNode *Node);
119 void LegalizeStoreOps(SDNode *Node);
120
121 SDValue ExpandINSERT_VECTOR_ELT(SDValue Op);
122
123 /// Return a vector shuffle operation which
124 /// performs the same shuffe in terms of order or result bytes, but on a type
125 /// whose vector element type is narrower than the original shuffle type.
126 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
127 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
128 SDValue N1, SDValue N2,
129 ArrayRef<int> Mask) const;
130
131 std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
133 std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
134
135 void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC,
137 void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
138 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
139 RTLIB::Libcall Call_F128,
140 RTLIB::Libcall Call_PPCF128,
142 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
143 RTLIB::Libcall Call_I8,
144 RTLIB::Libcall Call_I16,
145 RTLIB::Libcall Call_I32,
146 RTLIB::Libcall Call_I64,
147 RTLIB::Libcall Call_I128);
148 void ExpandArgFPLibCall(SDNode *Node,
149 RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
150 RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
151 RTLIB::Libcall Call_PPCF128,
153 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
154 void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
155
156 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
157 const SDLoc &dl);
158 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
159 const SDLoc &dl, SDValue ChainIn);
160 SDValue ExpandBUILD_VECTOR(SDNode *Node);
161 SDValue ExpandSPLAT_VECTOR(SDNode *Node);
162 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
163 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
165 void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
166 SDValue Value) const;
167 SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
168 SDValue NewIntValue) const;
169 SDValue ExpandFCOPYSIGN(SDNode *Node) const;
170 SDValue ExpandFABS(SDNode *Node) const;
171 SDValue ExpandFNEG(SDNode *Node) const;
172 SDValue expandLdexp(SDNode *Node) const;
173 SDValue expandFrexp(SDNode *Node) const;
174
175 SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain);
176 void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl,
178 void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
180 SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl);
181
182 /// Implements vector reduce operation promotion.
183 ///
184 /// All vector operands are promoted to a vector type with larger element
185 /// type, and the start value is promoted to a larger scalar type. Then the
186 /// result is truncated back to the original scalar type.
187 SDValue PromoteReduction(SDNode *Node);
188
189 SDValue ExpandPARITY(SDValue Op, const SDLoc &dl);
190
191 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
192 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
193 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
194
195 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
196 SDValue ExpandConstant(ConstantSDNode *CP);
197
198 // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
199 bool ExpandNode(SDNode *Node);
200 void ConvertNodeToLibcall(SDNode *Node);
201 void PromoteNode(SDNode *Node);
202
203public:
204 // Node replacement helpers
205
206 void ReplacedNode(SDNode *N) {
207 LegalizedNodes.erase(N);
208 if (UpdatedNodes)
209 UpdatedNodes->insert(N);
210 }
211
212 void ReplaceNode(SDNode *Old, SDNode *New) {
213 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
214 dbgs() << " with: "; New->dump(&DAG));
215
216 assert(Old->getNumValues() == New->getNumValues() &&
217 "Replacing one node with another that produces a different number "
218 "of values!");
219 DAG.ReplaceAllUsesWith(Old, New);
220 if (UpdatedNodes)
221 UpdatedNodes->insert(New);
222 ReplacedNode(Old);
223 }
224
225 void ReplaceNode(SDValue Old, SDValue New) {
226 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
227 dbgs() << " with: "; New->dump(&DAG));
228
229 DAG.ReplaceAllUsesWith(Old, New);
230 if (UpdatedNodes)
231 UpdatedNodes->insert(New.getNode());
232 ReplacedNode(Old.getNode());
233 }
234
235 void ReplaceNode(SDNode *Old, const SDValue *New) {
236 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
237
238 DAG.ReplaceAllUsesWith(Old, New);
239 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
240 LLVM_DEBUG(dbgs() << (i == 0 ? " with: " : " and: ");
241 New[i]->dump(&DAG));
242 if (UpdatedNodes)
243 UpdatedNodes->insert(New[i].getNode());
244 }
245 ReplacedNode(Old);
246 }
247
248 void ReplaceNodeWithValue(SDValue Old, SDValue New) {
249 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
250 dbgs() << " with: "; New->dump(&DAG));
251
252 DAG.ReplaceAllUsesOfValueWith(Old, New);
253 if (UpdatedNodes)
254 UpdatedNodes->insert(New.getNode());
255 ReplacedNode(Old.getNode());
256 }
257};
258
259} // end anonymous namespace
260
261// Helper function that generates an MMO that considers the alignment of the
262// stack, and the size of the stack object
264 MachineFunction &MF,
265 bool isObjectScalable) {
266 auto &MFI = MF.getFrameInfo();
267 int FI = cast<FrameIndexSDNode>(StackPtr)->getIndex();
269 LocationSize ObjectSize = isObjectScalable
271 : LocationSize::precise(MFI.getObjectSize(FI));
273 ObjectSize, MFI.getObjectAlign(FI));
274}
275
276/// Return a vector shuffle operation which
277/// performs the same shuffle in terms of order or result bytes, but on a type
278/// whose vector element type is narrower than the original shuffle type.
279/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
280SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
281 EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
282 ArrayRef<int> Mask) const {
283 unsigned NumMaskElts = VT.getVectorNumElements();
284 unsigned NumDestElts = NVT.getVectorNumElements();
285 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
286
287 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
288
289 if (NumEltsGrowth == 1)
290 return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
291
292 SmallVector<int, 8> NewMask;
293 for (unsigned i = 0; i != NumMaskElts; ++i) {
294 int Idx = Mask[i];
295 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
296 if (Idx < 0)
297 NewMask.push_back(-1);
298 else
299 NewMask.push_back(Idx * NumEltsGrowth + j);
300 }
301 }
302 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
303 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
304 return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
305}
306
307/// Expands the ConstantFP node to an integer constant or
308/// a load from the constant pool.
310SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
311 bool Extend = false;
312 SDLoc dl(CFP);
313
314 // If a FP immediate is precise when represented as a float and if the
315 // target can do an extending load from float to double, we put it into
316 // the constant pool as a float, even if it's is statically typed as a
317 // double. This shrinks FP constants and canonicalizes them for targets where
318 // an FP extending load is the same cost as a normal load (such as on the x87
319 // fp stack or PPC FP unit).
320 EVT VT = CFP->getValueType(0);
321 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
322 if (!UseCP) {
323 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
324 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
325 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
326 }
327
328 APFloat APF = CFP->getValueAPF();
329 EVT OrigVT = VT;
330 EVT SVT = VT;
331
332 // We don't want to shrink SNaNs. Converting the SNaN back to its real type
333 // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ).
334 if (!APF.isSignaling()) {
335 while (SVT != MVT::f32 && SVT != MVT::f16 && SVT != MVT::bf16) {
336 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
338 // Only do this if the target has a native EXTLOAD instruction from
339 // smaller type.
340 TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
341 TLI.ShouldShrinkFPConstant(OrigVT)) {
342 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
343 LLVMC = cast<ConstantFP>(ConstantFoldCastOperand(
344 Instruction::FPTrunc, LLVMC, SType, DAG.getDataLayout()));
345 VT = SVT;
346 Extend = true;
347 }
348 }
349 }
350
351 SDValue CPIdx =
352 DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
353 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
354 if (Extend) {
355 SDValue Result = DAG.getExtLoad(
356 ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
357 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
358 Alignment);
359 return Result;
360 }
361 SDValue Result = DAG.getLoad(
362 OrigVT, dl, DAG.getEntryNode(), CPIdx,
363 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
364 return Result;
365}
366
367/// Expands the Constant node to a load from the constant pool.
368SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
369 SDLoc dl(CP);
370 EVT VT = CP->getValueType(0);
371 SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
372 TLI.getPointerTy(DAG.getDataLayout()));
373 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
374 SDValue Result = DAG.getLoad(
375 VT, dl, DAG.getEntryNode(), CPIdx,
376 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
377 return Result;
378}
379
380SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Op) {
381 SDValue Vec = Op.getOperand(0);
382 SDValue Val = Op.getOperand(1);
383 SDValue Idx = Op.getOperand(2);
384 SDLoc dl(Op);
385
386 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
387 // SCALAR_TO_VECTOR requires that the type of the value being inserted
388 // match the element type of the vector being created, except for
389 // integers in which case the inserted value can be over width.
390 EVT EltVT = Vec.getValueType().getVectorElementType();
391 if (Val.getValueType() == EltVT ||
392 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
393 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
394 Vec.getValueType(), Val);
395
396 unsigned NumElts = Vec.getValueType().getVectorNumElements();
397 // We generate a shuffle of InVec and ScVec, so the shuffle mask
398 // should be 0,1,2,3,4,5... with the appropriate element replaced with
399 // elt 0 of the RHS.
400 SmallVector<int, 8> ShufOps;
401 for (unsigned i = 0; i != NumElts; ++i)
402 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
403
404 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
405 }
406 }
407 return ExpandInsertToVectorThroughStack(Op);
408}
409
410SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
411 if (!ISD::isNormalStore(ST))
412 return SDValue();
413
414 LLVM_DEBUG(dbgs() << "Optimizing float store operations\n");
415 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
416 // FIXME: move this to the DAG Combiner! Note that we can't regress due
417 // to phase ordering between legalized code and the dag combiner. This
418 // probably means that we need to integrate dag combiner and legalizer
419 // together.
420 // We generally can't do this one for long doubles.
421 SDValue Chain = ST->getChain();
422 SDValue Ptr = ST->getBasePtr();
423 SDValue Value = ST->getValue();
424 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
425 AAMDNodes AAInfo = ST->getAAInfo();
426 SDLoc dl(ST);
427
428 // Don't optimise TargetConstantFP
429 if (Value.getOpcode() == ISD::TargetConstantFP)
430 return SDValue();
431
432 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
433 if (CFP->getValueType(0) == MVT::f32 &&
434 TLI.isTypeLegal(MVT::i32)) {
435 SDValue Con = DAG.getConstant(CFP->getValueAPF().
436 bitcastToAPInt().zextOrTrunc(32),
437 SDLoc(CFP), MVT::i32);
438 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
439 ST->getOriginalAlign(), MMOFlags, AAInfo);
440 }
441
442 if (CFP->getValueType(0) == MVT::f64 &&
443 !TLI.isFPImmLegal(CFP->getValueAPF(), MVT::f64)) {
444 // If this target supports 64-bit registers, do a single 64-bit store.
445 if (TLI.isTypeLegal(MVT::i64)) {
446 SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
447 zextOrTrunc(64), SDLoc(CFP), MVT::i64);
448 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
449 ST->getOriginalAlign(), MMOFlags, AAInfo);
450 }
451
452 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
453 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
454 // stores. If the target supports neither 32- nor 64-bits, this
455 // xform is certainly not worth it.
456 const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
457 SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
458 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
459 if (DAG.getDataLayout().isBigEndian())
460 std::swap(Lo, Hi);
461
462 Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(),
463 ST->getOriginalAlign(), MMOFlags, AAInfo);
464 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(4), dl);
465 Hi = DAG.getStore(Chain, dl, Hi, Ptr,
466 ST->getPointerInfo().getWithOffset(4),
467 ST->getOriginalAlign(), MMOFlags, AAInfo);
468
469 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
470 }
471 }
472 }
473 return SDValue();
474}
475
476void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
477 StoreSDNode *ST = cast<StoreSDNode>(Node);
478 SDValue Chain = ST->getChain();
479 SDValue Ptr = ST->getBasePtr();
480 SDLoc dl(Node);
481
482 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
483 AAMDNodes AAInfo = ST->getAAInfo();
484
485 if (!ST->isTruncatingStore()) {
486 LLVM_DEBUG(dbgs() << "Legalizing store operation\n");
487 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
488 ReplaceNode(ST, OptStore);
489 return;
490 }
491
492 SDValue Value = ST->getValue();
493 MVT VT = Value.getSimpleValueType();
494 switch (TLI.getOperationAction(ISD::STORE, VT)) {
495 default: llvm_unreachable("This action is not supported yet!");
496 case TargetLowering::Legal: {
497 // If this is an unaligned store and the target doesn't support it,
498 // expand it.
499 EVT MemVT = ST->getMemoryVT();
500 const DataLayout &DL = DAG.getDataLayout();
501 if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
502 *ST->getMemOperand())) {
503 LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n");
504 SDValue Result = TLI.expandUnalignedStore(ST, DAG);
505 ReplaceNode(SDValue(ST, 0), Result);
506 } else
507 LLVM_DEBUG(dbgs() << "Legal store\n");
508 break;
509 }
510 case TargetLowering::Custom: {
511 LLVM_DEBUG(dbgs() << "Trying custom lowering\n");
512 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
513 if (Res && Res != SDValue(Node, 0))
514 ReplaceNode(SDValue(Node, 0), Res);
515 return;
516 }
517 case TargetLowering::Promote: {
518 MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
519 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
520 "Can only promote stores to same size type");
521 Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
522 SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
523 ST->getOriginalAlign(), MMOFlags, AAInfo);
524 ReplaceNode(SDValue(Node, 0), Result);
525 break;
526 }
527 }
528 return;
529 }
530
531 LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n");
532 SDValue Value = ST->getValue();
533 EVT StVT = ST->getMemoryVT();
534 TypeSize StWidth = StVT.getSizeInBits();
535 TypeSize StSize = StVT.getStoreSizeInBits();
536 auto &DL = DAG.getDataLayout();
537
538 if (StWidth != StSize) {
539 // Promote to a byte-sized store with upper bits zero if not
540 // storing an integral number of bytes. For example, promote
541 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
542 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedValue());
543 Value = DAG.getZeroExtendInReg(Value, dl, StVT);
545 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT,
546 ST->getOriginalAlign(), MMOFlags, AAInfo);
547 ReplaceNode(SDValue(Node, 0), Result);
548 } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedValue())) {
549 // If not storing a power-of-2 number of bits, expand as two stores.
550 assert(!StVT.isVector() && "Unsupported truncstore!");
551 unsigned StWidthBits = StWidth.getFixedValue();
552 unsigned LogStWidth = Log2_32(StWidthBits);
553 assert(LogStWidth < 32);
554 unsigned RoundWidth = 1 << LogStWidth;
555 assert(RoundWidth < StWidthBits);
556 unsigned ExtraWidth = StWidthBits - RoundWidth;
557 assert(ExtraWidth < RoundWidth);
558 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
559 "Store size not an integral number of bytes!");
560 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
561 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
562 SDValue Lo, Hi;
563 unsigned IncrementSize;
564
565 if (DL.isLittleEndian()) {
566 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
567 // Store the bottom RoundWidth bits.
568 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
569 RoundVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
570
571 // Store the remaining ExtraWidth bits.
572 IncrementSize = RoundWidth / 8;
573 Ptr =
574 DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
575 Hi = DAG.getNode(
576 ISD::SRL, dl, Value.getValueType(), Value,
577 DAG.getConstant(RoundWidth, dl,
578 TLI.getShiftAmountTy(Value.getValueType(), DL)));
579 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
580 ST->getPointerInfo().getWithOffset(IncrementSize),
581 ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
582 } else {
583 // Big endian - avoid unaligned stores.
584 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
585 // Store the top RoundWidth bits.
586 Hi = DAG.getNode(
587 ISD::SRL, dl, Value.getValueType(), Value,
588 DAG.getConstant(ExtraWidth, dl,
589 TLI.getShiftAmountTy(Value.getValueType(), DL)));
590 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT,
591 ST->getOriginalAlign(), MMOFlags, AAInfo);
592
593 // Store the remaining ExtraWidth bits.
594 IncrementSize = RoundWidth / 8;
595 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
596 DAG.getConstant(IncrementSize, dl,
597 Ptr.getValueType()));
598 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
599 ST->getPointerInfo().getWithOffset(IncrementSize),
600 ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
601 }
602
603 // The order of the stores doesn't matter.
604 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
605 ReplaceNode(SDValue(Node, 0), Result);
606 } else {
607 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
608 default: llvm_unreachable("This action is not supported yet!");
609 case TargetLowering::Legal: {
610 EVT MemVT = ST->getMemoryVT();
611 // If this is an unaligned store and the target doesn't support it,
612 // expand it.
613 if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
614 *ST->getMemOperand())) {
615 SDValue Result = TLI.expandUnalignedStore(ST, DAG);
616 ReplaceNode(SDValue(ST, 0), Result);
617 }
618 break;
619 }
620 case TargetLowering::Custom: {
621 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
622 if (Res && Res != SDValue(Node, 0))
623 ReplaceNode(SDValue(Node, 0), Res);
624 return;
625 }
626 case TargetLowering::Expand:
627 assert(!StVT.isVector() &&
628 "Vector Stores are handled in LegalizeVectorOps");
629
631
632 // TRUNCSTORE:i16 i32 -> STORE i16
633 if (TLI.isTypeLegal(StVT)) {
634 Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
635 Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
636 ST->getOriginalAlign(), MMOFlags, AAInfo);
637 } else {
638 // The in-memory type isn't legal. Truncate to the type it would promote
639 // to, and then do a truncstore.
640 Value = DAG.getNode(ISD::TRUNCATE, dl,
641 TLI.getTypeToTransformTo(*DAG.getContext(), StVT),
642 Value);
643 Result =
644 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), StVT,
645 ST->getOriginalAlign(), MMOFlags, AAInfo);
646 }
647
648 ReplaceNode(SDValue(Node, 0), Result);
649 break;
650 }
651 }
652}
653
654void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
655 LoadSDNode *LD = cast<LoadSDNode>(Node);
656 SDValue Chain = LD->getChain(); // The chain.
657 SDValue Ptr = LD->getBasePtr(); // The base pointer.
658 SDValue Value; // The value returned by the load op.
659 SDLoc dl(Node);
660
661 ISD::LoadExtType ExtType = LD->getExtensionType();
662 if (ExtType == ISD::NON_EXTLOAD) {
663 LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n");
664 MVT VT = Node->getSimpleValueType(0);
665 SDValue RVal = SDValue(Node, 0);
666 SDValue RChain = SDValue(Node, 1);
667
668 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
669 default: llvm_unreachable("This action is not supported yet!");
670 case TargetLowering::Legal: {
671 EVT MemVT = LD->getMemoryVT();
672 const DataLayout &DL = DAG.getDataLayout();
673 // If this is an unaligned load and the target doesn't support it,
674 // expand it.
675 if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
676 *LD->getMemOperand())) {
677 std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
678 }
679 break;
680 }
681 case TargetLowering::Custom:
682 if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
683 RVal = Res;
684 RChain = Res.getValue(1);
685 }
686 break;
687
688 case TargetLowering::Promote: {
689 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
690 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
691 "Can only promote loads to same size type");
692
693 SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
694 RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
695 RChain = Res.getValue(1);
696 break;
697 }
698 }
699 if (RChain.getNode() != Node) {
700 assert(RVal.getNode() != Node && "Load must be completely replaced");
701 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
702 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
703 if (UpdatedNodes) {
704 UpdatedNodes->insert(RVal.getNode());
705 UpdatedNodes->insert(RChain.getNode());
706 }
707 ReplacedNode(Node);
708 }
709 return;
710 }
711
712 LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n");
713 EVT SrcVT = LD->getMemoryVT();
714 TypeSize SrcWidth = SrcVT.getSizeInBits();
715 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
716 AAMDNodes AAInfo = LD->getAAInfo();
717
718 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
719 // Some targets pretend to have an i1 loading operation, and actually
720 // load an i8. This trick is correct for ZEXTLOAD because the top 7
721 // bits are guaranteed to be zero; it helps the optimizers understand
722 // that these bits are zero. It is also useful for EXTLOAD, since it
723 // tells the optimizers that those bits are undefined. It would be
724 // nice to have an effective generic way of getting these benefits...
725 // Until such a way is found, don't insist on promoting i1 here.
726 (SrcVT != MVT::i1 ||
727 TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
728 TargetLowering::Promote)) {
729 // Promote to a byte-sized load if not loading an integral number of
730 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
731 unsigned NewWidth = SrcVT.getStoreSizeInBits();
732 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
733 SDValue Ch;
734
735 // The extra bits are guaranteed to be zero, since we stored them that
736 // way. A zext load from NVT thus automatically gives zext from SrcVT.
737
738 ISD::LoadExtType NewExtType =
740
741 SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
742 Chain, Ptr, LD->getPointerInfo(), NVT,
743 LD->getOriginalAlign(), MMOFlags, AAInfo);
744
745 Ch = Result.getValue(1); // The chain.
746
747 if (ExtType == ISD::SEXTLOAD)
748 // Having the top bits zero doesn't help when sign extending.
749 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
750 Result.getValueType(),
751 Result, DAG.getValueType(SrcVT));
752 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
753 // All the top bits are guaranteed to be zero - inform the optimizers.
754 Result = DAG.getNode(ISD::AssertZext, dl,
755 Result.getValueType(), Result,
756 DAG.getValueType(SrcVT));
757
758 Value = Result;
759 Chain = Ch;
760 } else if (!isPowerOf2_64(SrcWidth.getKnownMinValue())) {
761 // If not loading a power-of-2 number of bits, expand as two loads.
762 assert(!SrcVT.isVector() && "Unsupported extload!");
763 unsigned SrcWidthBits = SrcWidth.getFixedValue();
764 unsigned LogSrcWidth = Log2_32(SrcWidthBits);
765 assert(LogSrcWidth < 32);
766 unsigned RoundWidth = 1 << LogSrcWidth;
767 assert(RoundWidth < SrcWidthBits);
768 unsigned ExtraWidth = SrcWidthBits - RoundWidth;
769 assert(ExtraWidth < RoundWidth);
770 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
771 "Load size not an integral number of bytes!");
772 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
773 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
774 SDValue Lo, Hi, Ch;
775 unsigned IncrementSize;
776 auto &DL = DAG.getDataLayout();
777
778 if (DL.isLittleEndian()) {
779 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
780 // Load the bottom RoundWidth bits.
781 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
782 LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
783 MMOFlags, AAInfo);
784
785 // Load the remaining ExtraWidth bits.
786 IncrementSize = RoundWidth / 8;
787 Ptr =
788 DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
789 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
790 LD->getPointerInfo().getWithOffset(IncrementSize),
791 ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
792
793 // Build a factor node to remember that this load is independent of
794 // the other one.
795 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
796 Hi.getValue(1));
797
798 // Move the top bits to the right place.
799 Hi = DAG.getNode(
800 ISD::SHL, dl, Hi.getValueType(), Hi,
801 DAG.getConstant(RoundWidth, dl,
802 TLI.getShiftAmountTy(Hi.getValueType(), DL)));
803
804 // Join the hi and lo parts.
805 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
806 } else {
807 // Big endian - avoid unaligned loads.
808 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
809 // Load the top RoundWidth bits.
810 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
811 LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
812 MMOFlags, AAInfo);
813
814 // Load the remaining ExtraWidth bits.
815 IncrementSize = RoundWidth / 8;
816 Ptr =
817 DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
818 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
819 LD->getPointerInfo().getWithOffset(IncrementSize),
820 ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
821
822 // Build a factor node to remember that this load is independent of
823 // the other one.
824 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
825 Hi.getValue(1));
826
827 // Move the top bits to the right place.
828 Hi = DAG.getNode(
829 ISD::SHL, dl, Hi.getValueType(), Hi,
830 DAG.getConstant(ExtraWidth, dl,
831 TLI.getShiftAmountTy(Hi.getValueType(), DL)));
832
833 // Join the hi and lo parts.
834 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
835 }
836
837 Chain = Ch;
838 } else {
839 bool isCustom = false;
840 switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
841 SrcVT.getSimpleVT())) {
842 default: llvm_unreachable("This action is not supported yet!");
843 case TargetLowering::Custom:
844 isCustom = true;
845 [[fallthrough]];
846 case TargetLowering::Legal:
847 Value = SDValue(Node, 0);
848 Chain = SDValue(Node, 1);
849
850 if (isCustom) {
851 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
852 Value = Res;
853 Chain = Res.getValue(1);
854 }
855 } else {
856 // If this is an unaligned load and the target doesn't support it,
857 // expand it.
858 EVT MemVT = LD->getMemoryVT();
859 const DataLayout &DL = DAG.getDataLayout();
860 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT,
861 *LD->getMemOperand())) {
862 std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
863 }
864 }
865 break;
866
867 case TargetLowering::Expand: {
868 EVT DestVT = Node->getValueType(0);
869 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
870 // If the source type is not legal, see if there is a legal extload to
871 // an intermediate type that we can then extend further.
872 EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
873 if ((LoadVT.isFloatingPoint() == SrcVT.isFloatingPoint()) &&
874 (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
875 TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT))) {
876 // If we are loading a legal type, this is a non-extload followed by a
877 // full extend.
878 ISD::LoadExtType MidExtType =
879 (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
880
881 SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
882 SrcVT, LD->getMemOperand());
883 unsigned ExtendOp =
885 Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
886 Chain = Load.getValue(1);
887 break;
888 }
889
890 // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
891 // normal undefined upper bits behavior to allow using an in-reg extend
892 // with the illegal FP type, so load as an integer and do the
893 // from-integer conversion.
894 EVT SVT = SrcVT.getScalarType();
895 if (SVT == MVT::f16 || SVT == MVT::bf16) {
896 EVT ISrcVT = SrcVT.changeTypeToInteger();
897 EVT IDestVT = DestVT.changeTypeToInteger();
898 EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
899
900 SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain,
901 Ptr, ISrcVT, LD->getMemOperand());
902 Value =
903 DAG.getNode(SVT == MVT::f16 ? ISD::FP16_TO_FP : ISD::BF16_TO_FP,
904 dl, DestVT, Result);
905 Chain = Result.getValue(1);
906 break;
907 }
908 }
909
910 assert(!SrcVT.isVector() &&
911 "Vector Loads are handled in LegalizeVectorOps");
912
913 // FIXME: This does not work for vectors on most targets. Sign-
914 // and zero-extend operations are currently folded into extending
915 // loads, whether they are legal or not, and then we end up here
916 // without any support for legalizing them.
917 assert(ExtType != ISD::EXTLOAD &&
918 "EXTLOAD should always be supported!");
919 // Turn the unsupported load into an EXTLOAD followed by an
920 // explicit zero/sign extend inreg.
921 SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
922 Node->getValueType(0),
923 Chain, Ptr, SrcVT,
924 LD->getMemOperand());
925 SDValue ValRes;
926 if (ExtType == ISD::SEXTLOAD)
927 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
928 Result.getValueType(),
929 Result, DAG.getValueType(SrcVT));
930 else
931 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
932 Value = ValRes;
933 Chain = Result.getValue(1);
934 break;
935 }
936 }
937 }
938
939 // Since loads produce two values, make sure to remember that we legalized
940 // both of them.
941 if (Chain.getNode() != Node) {
942 assert(Value.getNode() != Node && "Load must be completely replaced");
943 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
944 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
945 if (UpdatedNodes) {
946 UpdatedNodes->insert(Value.getNode());
947 UpdatedNodes->insert(Chain.getNode());
948 }
949 ReplacedNode(Node);
950 }
951}
952
953/// Return a legal replacement for the given operation, with all legal operands.
954void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
955 LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
956
957 // Allow illegal target nodes and illegal registers.
958 if (Node->getOpcode() == ISD::TargetConstant ||
959 Node->getOpcode() == ISD::Register)
960 return;
961
962#ifndef NDEBUG
963 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
964 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
965 TargetLowering::TypeLegal &&
966 "Unexpected illegal type!");
967
968 for (const SDValue &Op : Node->op_values())
969 assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
970 TargetLowering::TypeLegal ||
971 Op.getOpcode() == ISD::TargetConstant ||
972 Op.getOpcode() == ISD::Register) &&
973 "Unexpected illegal type!");
974#endif
975
976 // Figure out the correct action; the way to query this varies by opcode
977 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
978 bool SimpleFinishLegalizing = true;
979 switch (Node->getOpcode()) {
983 case ISD::STACKSAVE:
984 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
985 break;
987 Action = TLI.getOperationAction(Node->getOpcode(),
988 Node->getValueType(0));
989 break;
990 case ISD::VAARG:
991 Action = TLI.getOperationAction(Node->getOpcode(),
992 Node->getValueType(0));
993 if (Action != TargetLowering::Promote)
994 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
995 break;
996 case ISD::SET_FPENV:
997 case ISD::SET_FPMODE:
998 Action = TLI.getOperationAction(Node->getOpcode(),
999 Node->getOperand(1).getValueType());
1000 break;
1001 case ISD::FP_TO_FP16:
1002 case ISD::FP_TO_BF16:
1003 case ISD::SINT_TO_FP:
1004 case ISD::UINT_TO_FP:
1006 case ISD::LROUND:
1007 case ISD::LLROUND:
1008 case ISD::LRINT:
1009 case ISD::LLRINT:
1010 Action = TLI.getOperationAction(Node->getOpcode(),
1011 Node->getOperand(0).getValueType());
1012 break;
1017 case ISD::STRICT_LRINT:
1018 case ISD::STRICT_LLRINT:
1019 case ISD::STRICT_LROUND:
1021 // These pseudo-ops are the same as the other STRICT_ ops except
1022 // they are registered with setOperationAction() using the input type
1023 // instead of the output type.
1024 Action = TLI.getOperationAction(Node->getOpcode(),
1025 Node->getOperand(1).getValueType());
1026 break;
1028 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
1029 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
1030 break;
1031 }
1032 case ISD::ATOMIC_STORE:
1033 Action = TLI.getOperationAction(Node->getOpcode(),
1034 Node->getOperand(1).getValueType());
1035 break;
1036 case ISD::SELECT_CC:
1037 case ISD::STRICT_FSETCC:
1039 case ISD::SETCC:
1040 case ISD::SETCCCARRY:
1041 case ISD::VP_SETCC:
1042 case ISD::BR_CC: {
1043 unsigned Opc = Node->getOpcode();
1044 unsigned CCOperand = Opc == ISD::SELECT_CC ? 4
1045 : Opc == ISD::STRICT_FSETCC ? 3
1046 : Opc == ISD::STRICT_FSETCCS ? 3
1047 : Opc == ISD::SETCCCARRY ? 3
1048 : (Opc == ISD::SETCC || Opc == ISD::VP_SETCC) ? 2
1049 : 1;
1050 unsigned CompareOperand = Opc == ISD::BR_CC ? 2
1051 : Opc == ISD::STRICT_FSETCC ? 1
1052 : Opc == ISD::STRICT_FSETCCS ? 1
1053 : 0;
1054 MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
1055 ISD::CondCode CCCode =
1056 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
1057 Action = TLI.getCondCodeAction(CCCode, OpVT);
1058 if (Action == TargetLowering::Legal) {
1059 if (Node->getOpcode() == ISD::SELECT_CC)
1060 Action = TLI.getOperationAction(Node->getOpcode(),
1061 Node->getValueType(0));
1062 else
1063 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
1064 }
1065 break;
1066 }
1067 case ISD::LOAD:
1068 case ISD::STORE:
1069 // FIXME: Model these properly. LOAD and STORE are complicated, and
1070 // STORE expects the unlegalized operand in some cases.
1071 SimpleFinishLegalizing = false;
1072 break;
1073 case ISD::CALLSEQ_START:
1074 case ISD::CALLSEQ_END:
1075 // FIXME: This shouldn't be necessary. These nodes have special properties
1076 // dealing with the recursive nature of legalization. Removing this
1077 // special case should be done as part of making LegalizeDAG non-recursive.
1078 SimpleFinishLegalizing = false;
1079 break;
1081 case ISD::GET_ROUNDING:
1082 case ISD::MERGE_VALUES:
1083 case ISD::EH_RETURN:
1085 case ISD::EH_DWARF_CFA:
1089 // These operations lie about being legal: when they claim to be legal,
1090 // they should actually be expanded.
1091 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1092 if (Action == TargetLowering::Legal)
1093 Action = TargetLowering::Expand;
1094 break;
1097 case ISD::FRAMEADDR:
1098 case ISD::RETURNADDR:
1100 case ISD::SPONENTRY:
1101 // These operations lie about being legal: when they claim to be legal,
1102 // they should actually be custom-lowered.
1103 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1104 if (Action == TargetLowering::Legal)
1105 Action = TargetLowering::Custom;
1106 break;
1107 case ISD::CLEAR_CACHE:
1108 // This operation is typically going to be LibCall unless the target wants
1109 // something differrent.
1110 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1111 break;
1114 // READCYCLECOUNTER and READSTEADYCOUNTER return a i64, even if type
1115 // legalization might have expanded that to several smaller types.
1116 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1117 break;
1118 case ISD::READ_REGISTER:
1120 // Named register is legal in the DAG, but blocked by register name
1121 // selection if not implemented by target (to chose the correct register)
1122 // They'll be converted to Copy(To/From)Reg.
1123 Action = TargetLowering::Legal;
1124 break;
1125 case ISD::UBSANTRAP:
1126 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1127 if (Action == TargetLowering::Expand) {
1128 // replace ISD::UBSANTRAP with ISD::TRAP
1129 SDValue NewVal;
1130 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1131 Node->getOperand(0));
1132 ReplaceNode(Node, NewVal.getNode());
1133 LegalizeOp(NewVal.getNode());
1134 return;
1135 }
1136 break;
1137 case ISD::DEBUGTRAP:
1138 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1139 if (Action == TargetLowering::Expand) {
1140 // replace ISD::DEBUGTRAP with ISD::TRAP
1141 SDValue NewVal;
1142 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1143 Node->getOperand(0));
1144 ReplaceNode(Node, NewVal.getNode());
1145 LegalizeOp(NewVal.getNode());
1146 return;
1147 }
1148 break;
1149 case ISD::SADDSAT:
1150 case ISD::UADDSAT:
1151 case ISD::SSUBSAT:
1152 case ISD::USUBSAT:
1153 case ISD::SSHLSAT:
1154 case ISD::USHLSAT:
1155 case ISD::SCMP:
1156 case ISD::UCMP:
1159 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1160 break;
1161 case ISD::SMULFIX:
1162 case ISD::SMULFIXSAT:
1163 case ISD::UMULFIX:
1164 case ISD::UMULFIXSAT:
1165 case ISD::SDIVFIX:
1166 case ISD::SDIVFIXSAT:
1167 case ISD::UDIVFIX:
1168 case ISD::UDIVFIXSAT: {
1169 unsigned Scale = Node->getConstantOperandVal(2);
1170 Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
1171 Node->getValueType(0), Scale);
1172 break;
1173 }
1174 case ISD::MSCATTER:
1175 Action = TLI.getOperationAction(Node->getOpcode(),
1176 cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
1177 break;
1178 case ISD::MSTORE:
1179 Action = TLI.getOperationAction(Node->getOpcode(),
1180 cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
1181 break;
1182 case ISD::VP_SCATTER:
1183 Action = TLI.getOperationAction(
1184 Node->getOpcode(),
1185 cast<VPScatterSDNode>(Node)->getValue().getValueType());
1186 break;
1187 case ISD::VP_STORE:
1188 Action = TLI.getOperationAction(
1189 Node->getOpcode(),
1190 cast<VPStoreSDNode>(Node)->getValue().getValueType());
1191 break;
1192 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
1193 Action = TLI.getOperationAction(
1194 Node->getOpcode(),
1195 cast<VPStridedStoreSDNode>(Node)->getValue().getValueType());
1196 break;
1199 case ISD::VECREDUCE_ADD:
1200 case ISD::VECREDUCE_MUL:
1201 case ISD::VECREDUCE_AND:
1202 case ISD::VECREDUCE_OR:
1203 case ISD::VECREDUCE_XOR:
1212 case ISD::IS_FPCLASS:
1213 Action = TLI.getOperationAction(
1214 Node->getOpcode(), Node->getOperand(0).getValueType());
1215 break;
1218 case ISD::VP_REDUCE_FADD:
1219 case ISD::VP_REDUCE_FMUL:
1220 case ISD::VP_REDUCE_ADD:
1221 case ISD::VP_REDUCE_MUL:
1222 case ISD::VP_REDUCE_AND:
1223 case ISD::VP_REDUCE_OR:
1224 case ISD::VP_REDUCE_XOR:
1225 case ISD::VP_REDUCE_SMAX:
1226 case ISD::VP_REDUCE_SMIN:
1227 case ISD::VP_REDUCE_UMAX:
1228 case ISD::VP_REDUCE_UMIN:
1229 case ISD::VP_REDUCE_FMAX:
1230 case ISD::VP_REDUCE_FMIN:
1231 case ISD::VP_REDUCE_FMAXIMUM:
1232 case ISD::VP_REDUCE_FMINIMUM:
1233 case ISD::VP_REDUCE_SEQ_FADD:
1234 case ISD::VP_REDUCE_SEQ_FMUL:
1235 Action = TLI.getOperationAction(
1236 Node->getOpcode(), Node->getOperand(1).getValueType());
1237 break;
1238 case ISD::VP_CTTZ_ELTS:
1239 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
1240 Action = TLI.getOperationAction(Node->getOpcode(),
1241 Node->getOperand(0).getValueType());
1242 break;
1244 Action = TLI.getOperationAction(
1245 Node->getOpcode(),
1246 cast<MaskedHistogramSDNode>(Node)->getIndex().getValueType());
1247 break;
1248 default:
1249 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1250 Action = TLI.getCustomOperationAction(*Node);
1251 } else {
1252 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1253 }
1254 break;
1255 }
1256
1257 if (SimpleFinishLegalizing) {
1258 SDNode *NewNode = Node;
1259 switch (Node->getOpcode()) {
1260 default: break;
1261 case ISD::SHL:
1262 case ISD::SRL:
1263 case ISD::SRA:
1264 case ISD::ROTL:
1265 case ISD::ROTR: {
1266 // Legalizing shifts/rotates requires adjusting the shift amount
1267 // to the appropriate width.
1268 SDValue Op0 = Node->getOperand(0);
1269 SDValue Op1 = Node->getOperand(1);
1270 if (!Op1.getValueType().isVector()) {
1271 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
1272 // The getShiftAmountOperand() may create a new operand node or
1273 // return the existing one. If new operand is created we need
1274 // to update the parent node.
1275 // Do not try to legalize SAO here! It will be automatically legalized
1276 // in the next round.
1277 if (SAO != Op1)
1278 NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
1279 }
1280 }
1281 break;
1282 case ISD::FSHL:
1283 case ISD::FSHR:
1284 case ISD::SRL_PARTS:
1285 case ISD::SRA_PARTS:
1286 case ISD::SHL_PARTS: {
1287 // Legalizing shifts/rotates requires adjusting the shift amount
1288 // to the appropriate width.
1289 SDValue Op0 = Node->getOperand(0);
1290 SDValue Op1 = Node->getOperand(1);
1291 SDValue Op2 = Node->getOperand(2);
1292 if (!Op2.getValueType().isVector()) {
1293 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
1294 // The getShiftAmountOperand() may create a new operand node or
1295 // return the existing one. If new operand is created we need
1296 // to update the parent node.
1297 if (SAO != Op2)
1298 NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
1299 }
1300 break;
1301 }
1302 }
1303
1304 if (NewNode != Node) {
1305 ReplaceNode(Node, NewNode);
1306 Node = NewNode;
1307 }
1308 switch (Action) {
1309 case TargetLowering::Legal:
1310 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
1311 return;
1312 case TargetLowering::Custom:
1313 LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
1314 // FIXME: The handling for custom lowering with multiple results is
1315 // a complete mess.
1316 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
1317 if (!(Res.getNode() != Node || Res.getResNo() != 0))
1318 return;
1319
1320 if (Node->getNumValues() == 1) {
1321 // Verify the new types match the original. Glue is waived because
1322 // ISD::ADDC can be legalized by replacing Glue with an integer type.
1323 assert((Res.getValueType() == Node->getValueType(0) ||
1324 Node->getValueType(0) == MVT::Glue) &&
1325 "Type mismatch for custom legalized operation");
1326 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
1327 // We can just directly replace this node with the lowered value.
1328 ReplaceNode(SDValue(Node, 0), Res);
1329 return;
1330 }
1331
1332 SmallVector<SDValue, 8> ResultVals;
1333 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1334 // Verify the new types match the original. Glue is waived because
1335 // ISD::ADDC can be legalized by replacing Glue with an integer type.
1336 assert((Res->getValueType(i) == Node->getValueType(i) ||
1337 Node->getValueType(i) == MVT::Glue) &&
1338 "Type mismatch for custom legalized operation");
1339 ResultVals.push_back(Res.getValue(i));
1340 }
1341 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
1342 ReplaceNode(Node, ResultVals.data());
1343 return;
1344 }
1345 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
1346 [[fallthrough]];
1347 case TargetLowering::Expand:
1348 if (ExpandNode(Node))
1349 return;
1350 [[fallthrough]];
1351 case TargetLowering::LibCall:
1352 ConvertNodeToLibcall(Node);
1353 return;
1354 case TargetLowering::Promote:
1355 PromoteNode(Node);
1356 return;
1357 }
1358 }
1359
1360 switch (Node->getOpcode()) {
1361 default:
1362#ifndef NDEBUG
1363 dbgs() << "NODE: ";
1364 Node->dump( &DAG);
1365 dbgs() << "\n";
1366#endif
1367 llvm_unreachable("Do not know how to legalize this operator!");
1368
1369 case ISD::CALLSEQ_START:
1370 case ISD::CALLSEQ_END:
1371 break;
1372 case ISD::LOAD:
1373 return LegalizeLoadOps(Node);
1374 case ISD::STORE:
1375 return LegalizeStoreOps(Node);
1376 }
1377}
1378
1379SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1380 SDValue Vec = Op.getOperand(0);
1381 SDValue Idx = Op.getOperand(1);
1382 SDLoc dl(Op);
1383
1384 // Before we generate a new store to a temporary stack slot, see if there is
1385 // already one that we can use. There often is because when we scalarize
1386 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1387 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1388 // the vector. If all are expanded here, we don't want one store per vector
1389 // element.
1390
1391 // Caches for hasPredecessorHelper
1394 Visited.insert(Op.getNode());
1395 Worklist.push_back(Idx.getNode());
1396 SDValue StackPtr, Ch;
1397 for (SDNode *User : Vec.getNode()->users()) {
1398 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1399 if (ST->isIndexed() || ST->isTruncatingStore() ||
1400 ST->getValue() != Vec)
1401 continue;
1402
1403 // Make sure that nothing else could have stored into the destination of
1404 // this store.
1405 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1406 continue;
1407
1408 // If the index is dependent on the store we will introduce a cycle when
1409 // creating the load (the load uses the index, and by replacing the chain
1410 // we will make the index dependent on the load). Also, the store might be
1411 // dependent on the extractelement and introduce a cycle when creating
1412 // the load.
1413 if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) ||
1414 ST->hasPredecessor(Op.getNode()))
1415 continue;
1416
1417 StackPtr = ST->getBasePtr();
1418 Ch = SDValue(ST, 0);
1419 break;
1420 }
1421 }
1422
1423 EVT VecVT = Vec.getValueType();
1424
1425 if (!Ch.getNode()) {
1426 // Store the value to a temporary stack slot, then LOAD the returned part.
1427 StackPtr = DAG.CreateStackTemporary(VecVT);
1429 StackPtr, DAG.getMachineFunction(), VecVT.isScalableVector());
1430 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, StoreMMO);
1431 }
1432
1433 SDValue NewLoad;
1434 Align ElementAlignment =
1435 std::min(cast<StoreSDNode>(Ch)->getAlign(),
1436 DAG.getDataLayout().getPrefTypeAlign(
1437 Op.getValueType().getTypeForEVT(*DAG.getContext())));
1438
1439 if (Op.getValueType().isVector()) {
1440 StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
1441 Op.getValueType(), Idx);
1442 NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1443 MachinePointerInfo(), ElementAlignment);
1444 } else {
1445 StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1446 NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1448 ElementAlignment);
1449 }
1450
1451 // Replace the chain going out of the store, by the one out of the load.
1452 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1453
1454 // We introduced a cycle though, so update the loads operands, making sure
1455 // to use the original store's chain as an incoming chain.
1456 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->ops());
1457 NewLoadOperands[0] = Ch;
1458 NewLoad =
1459 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1460 return NewLoad;
1461}
1462
1463SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1464 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1465
1466 SDValue Vec = Op.getOperand(0);
1467 SDValue Part = Op.getOperand(1);
1468 SDValue Idx = Op.getOperand(2);
1469 SDLoc dl(Op);
1470
1471 // Store the value to a temporary stack slot, then LOAD the returned part.
1472 EVT VecVT = Vec.getValueType();
1473 EVT PartVT = Part.getValueType();
1474 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1475 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1476 MachinePointerInfo PtrInfo =
1477 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1478
1479 // First store the whole vector.
1480 Align BaseVecAlignment =
1481 DAG.getMachineFunction().getFrameInfo().getObjectAlign(FI);
1482 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1483 BaseVecAlignment);
1484
1485 // Freeze the index so we don't poison the clamping code we're about to emit.
1486 Idx = DAG.getFreeze(Idx);
1487
1488 Type *PartTy = PartVT.getTypeForEVT(*DAG.getContext());
1489 Align PartAlignment = DAG.getDataLayout().getPrefTypeAlign(PartTy);
1490
1491 // Then store the inserted part.
1492 if (PartVT.isVector()) {
1493 SDValue SubStackPtr =
1494 TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, PartVT, Idx);
1495
1496 // Store the subvector.
1497 Ch = DAG.getStore(
1498 Ch, dl, Part, SubStackPtr,
1499 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
1500 PartAlignment);
1501 } else {
1502 SDValue SubStackPtr =
1503 TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1504
1505 // Store the scalar value.
1506 Ch = DAG.getTruncStore(
1507 Ch, dl, Part, SubStackPtr,
1508 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
1509 VecVT.getVectorElementType(), PartAlignment);
1510 }
1511
1512 assert(cast<StoreSDNode>(Ch)->getAlign() == PartAlignment &&
1513 "ElementAlignment does not match!");
1514
1515 // Finally, load the updated vector.
1516 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1517 BaseVecAlignment);
1518}
1519
1520SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1521 assert((Node->getOpcode() == ISD::BUILD_VECTOR ||
1522 Node->getOpcode() == ISD::CONCAT_VECTORS) &&
1523 "Unexpected opcode!");
1524
1525 // We can't handle this case efficiently. Allocate a sufficiently
1526 // aligned object on the stack, store each operand into it, then load
1527 // the result as a vector.
1528 // Create the stack frame object.
1529 EVT VT = Node->getValueType(0);
1530 EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType()
1531 : Node->getOperand(0).getValueType();
1532 SDLoc dl(Node);
1533 SDValue FIPtr = DAG.CreateStackTemporary(VT);
1534 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
1535 MachinePointerInfo PtrInfo =
1536 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1537
1538 // Emit a store of each element to the stack slot.
1540 unsigned TypeByteSize = MemVT.getSizeInBits() / 8;
1541 assert(TypeByteSize > 0 && "Vector element type too small for stack store!");
1542
1543 // If the destination vector element type of a BUILD_VECTOR is narrower than
1544 // the source element type, only store the bits necessary.
1545 bool Truncate = isa<BuildVectorSDNode>(Node) &&
1546 MemVT.bitsLT(Node->getOperand(0).getValueType());
1547
1548 // Store (in the right endianness) the elements to memory.
1549 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1550 // Ignore undef elements.
1551 if (Node->getOperand(i).isUndef()) continue;
1552
1553 unsigned Offset = TypeByteSize*i;
1554
1555 SDValue Idx =
1556 DAG.getMemBasePlusOffset(FIPtr, TypeSize::getFixed(Offset), dl);
1557
1558 if (Truncate)
1559 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
1560 Node->getOperand(i), Idx,
1561 PtrInfo.getWithOffset(Offset), MemVT));
1562 else
1563 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
1564 Idx, PtrInfo.getWithOffset(Offset)));
1565 }
1566
1567 SDValue StoreChain;
1568 if (!Stores.empty()) // Not all undef elements?
1569 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
1570 else
1571 StoreChain = DAG.getEntryNode();
1572
1573 // Result is a load from the stack slot.
1574 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
1575}
1576
1577/// Bitcast a floating-point value to an integer value. Only bitcast the part
1578/// containing the sign bit if the target has no integer value capable of
1579/// holding all bits of the floating-point value.
1580void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
1581 const SDLoc &DL,
1582 SDValue Value) const {
1583 EVT FloatVT = Value.getValueType();
1584 unsigned NumBits = FloatVT.getScalarSizeInBits();
1585 State.FloatVT = FloatVT;
1586 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1587 // Convert to an integer of the same size.
1588 if (TLI.isTypeLegal(IVT)) {
1589 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1590 State.SignMask = APInt::getSignMask(NumBits);
1591 State.SignBit = NumBits - 1;
1592 return;
1593 }
1594
1595 auto &DataLayout = DAG.getDataLayout();
1596 // Store the float to memory, then load the sign part out as an integer.
1597 MVT LoadTy = TLI.getRegisterType(MVT::i8);
1598 // First create a temporary that is aligned for both the load and store.
1599 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1600 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1601 // Then store the float to it.
1602 State.FloatPtr = StackPtr;
1603 MachineFunction &MF = DAG.getMachineFunction();
1604 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1605 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
1606 State.FloatPointerInfo);
1607
1608 SDValue IntPtr;
1609 if (DataLayout.isBigEndian()) {
1610 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1611 // Load out a legal integer with the same sign bit as the float.
1612 IntPtr = StackPtr;
1613 State.IntPointerInfo = State.FloatPointerInfo;
1614 } else {
1615 // Advance the pointer so that the loaded byte will contain the sign bit.
1616 unsigned ByteOffset = (NumBits / 8) - 1;
1617 IntPtr =
1618 DAG.getMemBasePlusOffset(StackPtr, TypeSize::getFixed(ByteOffset), DL);
1619 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1620 ByteOffset);
1621 }
1622
1623 State.IntPtr = IntPtr;
1624 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
1625 State.IntPointerInfo, MVT::i8);
1626 State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7);
1627 State.SignBit = 7;
1628}
1629
1630/// Replace the integer value produced by getSignAsIntValue() with a new value
1631/// and cast the result back to a floating-point type.
1632SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
1633 const SDLoc &DL,
1634 SDValue NewIntValue) const {
1635 if (!State.Chain)
1636 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1637
1638 // Override the part containing the sign bit in the value stored on the stack.
1639 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
1640 State.IntPointerInfo, MVT::i8);
1641 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
1642 State.FloatPointerInfo);
1643}
1644
1645SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1646 SDLoc DL(Node);
1647 SDValue Mag = Node->getOperand(0);
1648 SDValue Sign = Node->getOperand(1);
1649
1650 // Get sign bit into an integer value.
1651 FloatSignAsInt SignAsInt;
1652 getSignAsIntValue(SignAsInt, DL, Sign);
1653
1654 EVT IntVT = SignAsInt.IntValue.getValueType();
1655 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1656 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1657 SignMask);
1658
1659 // If FABS is legal transform
1660 // FCOPYSIGN(x, y) => SignBit(y) ? -FABS(x) : FABS(x)
1661 EVT FloatVT = Mag.getValueType();
1662 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1663 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1664 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1665 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1666 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1667 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1668 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1669 }
1670
1671 // Transform Mag value to integer, and clear the sign bit.
1672 FloatSignAsInt MagAsInt;
1673 getSignAsIntValue(MagAsInt, DL, Mag);
1674 EVT MagVT = MagAsInt.IntValue.getValueType();
1675 SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
1676 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
1677 ClearSignMask);
1678
1679 // Get the signbit at the right position for MagAsInt.
1680 int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
1681 EVT ShiftVT = IntVT;
1682 if (SignBit.getScalarValueSizeInBits() <
1683 ClearedSign.getScalarValueSizeInBits()) {
1684 SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
1685 ShiftVT = MagVT;
1686 }
1687 if (ShiftAmount > 0) {
1688 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT);
1689 SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst);
1690 } else if (ShiftAmount < 0) {
1691 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT);
1692 SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst);
1693 }
1694 if (SignBit.getScalarValueSizeInBits() >
1695 ClearedSign.getScalarValueSizeInBits()) {
1696 SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
1697 }
1698
1699 // Store the part with the modified sign and convert back to float.
1700 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit,
1702
1703 return modifySignAsInt(MagAsInt, DL, CopiedSign);
1704}
1705
1706SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const {
1707 // Get the sign bit as an integer.
1708 SDLoc DL(Node);
1709 FloatSignAsInt SignAsInt;
1710 getSignAsIntValue(SignAsInt, DL, Node->getOperand(0));
1711 EVT IntVT = SignAsInt.IntValue.getValueType();
1712
1713 // Flip the sign.
1714 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1715 SDValue SignFlip =
1716 DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask);
1717
1718 // Convert back to float.
1719 return modifySignAsInt(SignAsInt, DL, SignFlip);
1720}
1721
1722SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1723 SDLoc DL(Node);
1724 SDValue Value = Node->getOperand(0);
1725
1726 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1727 EVT FloatVT = Value.getValueType();
1728 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1729 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1730 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1731 }
1732
1733 // Transform value to integer, clear the sign bit and transform back.
1734 FloatSignAsInt ValueAsInt;
1735 getSignAsIntValue(ValueAsInt, DL, Value);
1736 EVT IntVT = ValueAsInt.IntValue.getValueType();
1737 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1738 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1739 ClearSignMask);
1740 return modifySignAsInt(ValueAsInt, DL, ClearedSign);
1741}
1742
1743void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1745 Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
1746 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1747 " not tell us which reg is the stack pointer!");
1748 SDLoc dl(Node);
1749 EVT VT = Node->getValueType(0);
1750 SDValue Tmp1 = SDValue(Node, 0);
1751 SDValue Tmp2 = SDValue(Node, 1);
1752 SDValue Tmp3 = Node->getOperand(2);
1753 SDValue Chain = Tmp1.getOperand(0);
1754
1755 // Chain the dynamic stack allocation so that it doesn't modify the stack
1756 // pointer when other instructions are using the stack.
1757 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
1758
1759 SDValue Size = Tmp2.getOperand(1);
1760 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1761 Chain = SP.getValue(1);
1762 Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue();
1763 const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering();
1764 unsigned Opc =
1767
1768 Align StackAlign = TFL->getStackAlign();
1769 Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size); // Value
1770 if (Alignment > StackAlign)
1771 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
1772 DAG.getSignedConstant(-Alignment.value(), dl, VT));
1773 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1774
1775 Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
1776
1777 Results.push_back(Tmp1);
1778 Results.push_back(Tmp2);
1779}
1780
1781/// Emit a store/load combination to the stack. This stores
1782/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1783/// a load from the stack slot to DestVT, extending it if needed.
1784/// The resultant code need not be legal.
1785SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1786 EVT DestVT, const SDLoc &dl) {
1787 return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode());
1788}
1789
1790SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1791 EVT DestVT, const SDLoc &dl,
1792 SDValue Chain) {
1793 EVT SrcVT = SrcOp.getValueType();
1794 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1795 Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType);
1796
1797 // Don't convert with stack if the load/store is expensive.
1798 if ((SrcVT.bitsGT(SlotVT) &&
1799 !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) ||
1800 (SlotVT.bitsLT(DestVT) &&
1801 !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT)))
1802 return SDValue();
1803
1804 // Create the stack frame object.
1805 Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign(
1806 SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1807 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign);
1808
1809 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1810 int SPFI = StackPtrFI->getIndex();
1811 MachinePointerInfo PtrInfo =
1812 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
1813
1814 // Emit a store to the stack slot. Use a truncstore if the input value is
1815 // later than DestVT.
1816 SDValue Store;
1817
1818 if (SrcVT.bitsGT(SlotVT))
1819 Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo,
1820 SlotVT, SrcAlign);
1821 else {
1822 assert(SrcVT.bitsEq(SlotVT) && "Invalid store");
1823 Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
1824 }
1825
1826 // Result is a load from the stack slot.
1827 if (SlotVT.bitsEq(DestVT))
1828 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
1829
1830 assert(SlotVT.bitsLT(DestVT) && "Unknown extension!");
1831 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
1832 DestAlign);
1833}
1834
1835SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
1836 SDLoc dl(Node);
1837 // Create a vector sized/aligned stack slot, store the value to element #0,
1838 // then load the whole vector back out.
1839 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
1840
1841 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1842 int SPFI = StackPtrFI->getIndex();
1843
1844 SDValue Ch = DAG.getTruncStore(
1845 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
1846 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
1847 Node->getValueType(0).getVectorElementType());
1848 return DAG.getLoad(
1849 Node->getValueType(0), dl, Ch, StackPtr,
1850 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
1851}
1852
1853static bool
1855 const TargetLowering &TLI, SDValue &Res) {
1856 unsigned NumElems = Node->getNumOperands();
1857 SDLoc dl(Node);
1858 EVT VT = Node->getValueType(0);
1859
1860 // Try to group the scalars into pairs, shuffle the pairs together, then
1861 // shuffle the pairs of pairs together, etc. until the vector has
1862 // been built. This will work only if all of the necessary shuffle masks
1863 // are legal.
1864
1865 // We do this in two phases; first to check the legality of the shuffles,
1866 // and next, assuming that all shuffles are legal, to create the new nodes.
1867 for (int Phase = 0; Phase < 2; ++Phase) {
1869 NewIntermedVals;
1870 for (unsigned i = 0; i < NumElems; ++i) {
1871 SDValue V = Node->getOperand(i);
1872 if (V.isUndef())
1873 continue;
1874
1875 SDValue Vec;
1876 if (Phase)
1877 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
1878 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
1879 }
1880
1881 while (IntermedVals.size() > 2) {
1882 NewIntermedVals.clear();
1883 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
1884 // This vector and the next vector are shuffled together (simply to
1885 // append the one to the other).
1886 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1887
1888 SmallVector<int, 16> FinalIndices;
1889 FinalIndices.reserve(IntermedVals[i].second.size() +
1890 IntermedVals[i+1].second.size());
1891
1892 int k = 0;
1893 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
1894 ++j, ++k) {
1895 ShuffleVec[k] = j;
1896 FinalIndices.push_back(IntermedVals[i].second[j]);
1897 }
1898 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
1899 ++j, ++k) {
1900 ShuffleVec[k] = NumElems + j;
1901 FinalIndices.push_back(IntermedVals[i+1].second[j]);
1902 }
1903
1904 SDValue Shuffle;
1905 if (Phase)
1906 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
1907 IntermedVals[i+1].first,
1908 ShuffleVec);
1909 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1910 return false;
1911 NewIntermedVals.push_back(
1912 std::make_pair(Shuffle, std::move(FinalIndices)));
1913 }
1914
1915 // If we had an odd number of defined values, then append the last
1916 // element to the array of new vectors.
1917 if ((IntermedVals.size() & 1) != 0)
1918 NewIntermedVals.push_back(IntermedVals.back());
1919
1920 IntermedVals.swap(NewIntermedVals);
1921 }
1922
1923 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
1924 "Invalid number of intermediate vectors");
1925 SDValue Vec1 = IntermedVals[0].first;
1926 SDValue Vec2;
1927 if (IntermedVals.size() > 1)
1928 Vec2 = IntermedVals[1].first;
1929 else if (Phase)
1930 Vec2 = DAG.getUNDEF(VT);
1931
1932 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1933 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
1934 ShuffleVec[IntermedVals[0].second[i]] = i;
1935 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
1936 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
1937
1938 if (Phase)
1939 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
1940 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1941 return false;
1942 }
1943
1944 return true;
1945}
1946
1947/// Expand a BUILD_VECTOR node on targets that don't
1948/// support the operation, but do support the resultant vector type.
1949SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
1950 unsigned NumElems = Node->getNumOperands();
1951 SDValue Value1, Value2;
1952 SDLoc dl(Node);
1953 EVT VT = Node->getValueType(0);
1954 EVT OpVT = Node->getOperand(0).getValueType();
1955 EVT EltVT = VT.getVectorElementType();
1956
1957 // If the only non-undef value is the low element, turn this into a
1958 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
1959 bool isOnlyLowElement = true;
1960 bool MoreThanTwoValues = false;
1961 bool isConstant = true;
1962 for (unsigned i = 0; i < NumElems; ++i) {
1963 SDValue V = Node->getOperand(i);
1964 if (V.isUndef())
1965 continue;
1966 if (i > 0)
1967 isOnlyLowElement = false;
1968 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
1969 isConstant = false;
1970
1971 if (!Value1.getNode()) {
1972 Value1 = V;
1973 } else if (!Value2.getNode()) {
1974 if (V != Value1)
1975 Value2 = V;
1976 } else if (V != Value1 && V != Value2) {
1977 MoreThanTwoValues = true;
1978 }
1979 }
1980
1981 if (!Value1.getNode())
1982 return DAG.getUNDEF(VT);
1983
1984 if (isOnlyLowElement)
1985 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
1986
1987 // If all elements are constants, create a load from the constant pool.
1988 if (isConstant) {
1990 for (unsigned i = 0, e = NumElems; i != e; ++i) {
1991 if (ConstantFPSDNode *V =
1992 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
1993 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
1994 } else if (ConstantSDNode *V =
1995 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
1996 if (OpVT==EltVT)
1997 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1998 else {
1999 // If OpVT and EltVT don't match, EltVT is not legal and the
2000 // element values have been promoted/truncated earlier. Undo this;
2001 // we don't want a v16i8 to become a v16i32 for example.
2002 const ConstantInt *CI = V->getConstantIntValue();
2003 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2004 CI->getZExtValue()));
2005 }
2006 } else {
2007 assert(Node->getOperand(i).isUndef());
2008 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
2009 CV.push_back(UndefValue::get(OpNTy));
2010 }
2011 }
2013 SDValue CPIdx =
2014 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
2015 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2016 return DAG.getLoad(
2017 VT, dl, DAG.getEntryNode(), CPIdx,
2018 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2019 Alignment);
2020 }
2021
2022 SmallSet<SDValue, 16> DefinedValues;
2023 for (unsigned i = 0; i < NumElems; ++i) {
2024 if (Node->getOperand(i).isUndef())
2025 continue;
2026 DefinedValues.insert(Node->getOperand(i));
2027 }
2028
2029 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
2030 if (!MoreThanTwoValues) {
2031 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2032 for (unsigned i = 0; i < NumElems; ++i) {
2033 SDValue V = Node->getOperand(i);
2034 if (V.isUndef())
2035 continue;
2036 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2037 }
2038 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
2039 // Get the splatted value into the low element of a vector register.
2040 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2041 SDValue Vec2;
2042 if (Value2.getNode())
2043 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2044 else
2045 Vec2 = DAG.getUNDEF(VT);
2046
2047 // Return shuffle(LowValVec, undef, <0,0,0,0>)
2048 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
2049 }
2050 } else {
2051 SDValue Res;
2052 if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
2053 return Res;
2054 }
2055 }
2056
2057 // Otherwise, we can't handle this case efficiently.
2058 return ExpandVectorBuildThroughStack(Node);
2059}
2060
2061SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
2062 SDLoc DL(Node);
2063 EVT VT = Node->getValueType(0);
2064 SDValue SplatVal = Node->getOperand(0);
2065
2066 return DAG.getSplatBuildVector(VT, DL, SplatVal);
2067}
2068
2069// Expand a node into a call to a libcall, returning the value as the first
2070// result and the chain as the second. If the result value does not fit into a
2071// register, return the lo part and set the hi part to the by-reg argument in
2072// the first. If it does fit into a single register, return the result and
2073// leave the Hi part unset.
2074std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2076 bool isSigned) {
2077 EVT CodePtrTy = TLI.getPointerTy(DAG.getDataLayout());
2079 if (const char *LibcallName = TLI.getLibcallName(LC))
2080 Callee = DAG.getExternalSymbol(LibcallName, CodePtrTy);
2081 else {
2082 Callee = DAG.getUNDEF(CodePtrTy);
2083 DAG.getContext()->emitError(Twine("no libcall available for ") +
2084 Node->getOperationName(&DAG));
2085 }
2086
2087 EVT RetVT = Node->getValueType(0);
2088 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2089
2090 // By default, the input chain to this libcall is the entry node of the
2091 // function. If the libcall is going to be emitted as a tail call then
2092 // TLI.isUsedByReturnOnly will change it to the right chain if the return
2093 // node which is being folded has a non-entry input chain.
2094 SDValue InChain = DAG.getEntryNode();
2095
2096 // isTailCall may be true since the callee does not reference caller stack
2097 // frame. Check if it's in the right position and that the return types match.
2098 SDValue TCChain = InChain;
2099 const Function &F = DAG.getMachineFunction().getFunction();
2100 bool isTailCall =
2101 TLI.isInTailCallPosition(DAG, Node, TCChain) &&
2102 (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
2103 if (isTailCall)
2104 InChain = TCChain;
2105
2107 bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetTy, isSigned);
2108 CLI.setDebugLoc(SDLoc(Node))
2109 .setChain(InChain)
2110 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
2111 std::move(Args))
2112 .setTailCall(isTailCall)
2113 .setSExtResult(signExtend)
2114 .setZExtResult(!signExtend)
2115 .setIsPostTypeLegalization(true);
2116
2117 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2118
2119 if (!CallInfo.second.getNode()) {
2120 LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG));
2121 // It's a tailcall, return the chain (which is the DAG root).
2122 return {DAG.getRoot(), DAG.getRoot()};
2123 }
2124
2125 LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG));
2126 return CallInfo;
2127}
2128
2129std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2130 bool isSigned) {
2133 for (const SDValue &Op : Node->op_values()) {
2134 EVT ArgVT = Op.getValueType();
2135 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2136 Entry.Node = Op;
2137 Entry.Ty = ArgTy;
2138 Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgTy, isSigned);
2139 Entry.IsZExt = !Entry.IsSExt;
2140 Args.push_back(Entry);
2141 }
2142
2143 return ExpandLibCall(LC, Node, std::move(Args), isSigned);
2144}
2145
2146void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2147 RTLIB::Libcall LC,
2149 if (LC == RTLIB::UNKNOWN_LIBCALL)
2150 llvm_unreachable("Can't create an unknown libcall!");
2151
2152 if (Node->isStrictFPOpcode()) {
2153 EVT RetVT = Node->getValueType(0);
2156 // FIXME: This doesn't support tail calls.
2157 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
2158 Ops, CallOptions,
2159 SDLoc(Node),
2160 Node->getOperand(0));
2161 Results.push_back(Tmp.first);
2162 Results.push_back(Tmp.second);
2163 } else {
2164 bool IsSignedArgument = Node->getOpcode() == ISD::FLDEXP;
2165 SDValue Tmp = ExpandLibCall(LC, Node, IsSignedArgument).first;
2166 Results.push_back(Tmp);
2167 }
2168}
2169
2170/// Expand the node to a libcall based on the result type.
2171void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2172 RTLIB::Libcall Call_F32,
2173 RTLIB::Libcall Call_F64,
2174 RTLIB::Libcall Call_F80,
2175 RTLIB::Libcall Call_F128,
2176 RTLIB::Libcall Call_PPCF128,
2178 RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0),
2179 Call_F32, Call_F64, Call_F80,
2180 Call_F128, Call_PPCF128);
2181 ExpandFPLibCall(Node, LC, Results);
2182}
2183
2184SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2185 RTLIB::Libcall Call_I8,
2186 RTLIB::Libcall Call_I16,
2187 RTLIB::Libcall Call_I32,
2188 RTLIB::Libcall Call_I64,
2189 RTLIB::Libcall Call_I128) {
2190 RTLIB::Libcall LC;
2191 switch (Node->getSimpleValueType(0).SimpleTy) {
2192 default: llvm_unreachable("Unexpected request for libcall!");
2193 case MVT::i8: LC = Call_I8; break;
2194 case MVT::i16: LC = Call_I16; break;
2195 case MVT::i32: LC = Call_I32; break;
2196 case MVT::i64: LC = Call_I64; break;
2197 case MVT::i128: LC = Call_I128; break;
2198 }
2199 return ExpandLibCall(LC, Node, isSigned).first;
2200}
2201
2202/// Expand the node to a libcall based on first argument type (for instance
2203/// lround and its variant).
2204void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
2205 RTLIB::Libcall Call_F32,
2206 RTLIB::Libcall Call_F64,
2207 RTLIB::Libcall Call_F80,
2208 RTLIB::Libcall Call_F128,
2209 RTLIB::Libcall Call_PPCF128,
2211 EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType();
2213 Call_F32, Call_F64, Call_F80,
2214 Call_F128, Call_PPCF128);
2215 ExpandFPLibCall(Node, LC, Results);
2216}
2217
2218/// Issue libcalls to __{u}divmod to compute div / rem pairs.
2219void
2220SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2222 unsigned Opcode = Node->getOpcode();
2223 bool isSigned = Opcode == ISD::SDIVREM;
2224
2225 RTLIB::Libcall LC;
2226 switch (Node->getSimpleValueType(0).SimpleTy) {
2227 default: llvm_unreachable("Unexpected request for libcall!");
2228 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2229 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2230 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2231 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2232 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2233 }
2234
2235 // The input chain to this libcall is the entry node of the function.
2236 // Legalizing the call will automatically add the previous call to the
2237 // dependence.
2238 SDValue InChain = DAG.getEntryNode();
2239
2240 EVT RetVT = Node->getValueType(0);
2241 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2242
2245 for (const SDValue &Op : Node->op_values()) {
2246 EVT ArgVT = Op.getValueType();
2247 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2248 Entry.Node = Op;
2249 Entry.Ty = ArgTy;
2250 Entry.IsSExt = isSigned;
2251 Entry.IsZExt = !isSigned;
2252 Args.push_back(Entry);
2253 }
2254
2255 // Also pass the return address of the remainder.
2256 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2257 Entry.Node = FIPtr;
2258 Entry.Ty = PointerType::getUnqual(RetTy->getContext());
2259 Entry.IsSExt = isSigned;
2260 Entry.IsZExt = !isSigned;
2261 Args.push_back(Entry);
2262
2263 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2264 TLI.getPointerTy(DAG.getDataLayout()));
2265
2266 SDLoc dl(Node);
2268 CLI.setDebugLoc(dl)
2269 .setChain(InChain)
2270 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
2271 std::move(Args))
2272 .setSExtResult(isSigned)
2273 .setZExtResult(!isSigned);
2274
2275 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2276
2277 // Remainder is loaded back from the stack frame.
2278 SDValue Rem =
2279 DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo());
2280 Results.push_back(CallInfo.first);
2281 Results.push_back(Rem);
2282}
2283
2284/// Return true if sincos libcall is available.
2286 RTLIB::Libcall LC = RTLIB::getFSINCOS(Node->getSimpleValueType(0).SimpleTy);
2287 return TLI.getLibcallName(LC) != nullptr;
2288}
2289
2290/// Only issue sincos libcall if both sin and cos are needed.
2291static bool useSinCos(SDNode *Node) {
2292 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2293 ? ISD::FCOS : ISD::FSIN;
2294
2295 SDValue Op0 = Node->getOperand(0);
2296 for (const SDNode *User : Op0.getNode()->users()) {
2297 if (User == Node)
2298 continue;
2299 // The other user might have been turned into sincos already.
2300 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2301 return true;
2302 }
2303 return false;
2304}
2305
2306SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
2307 SDLoc dl(Node);
2308 EVT VT = Node->getValueType(0);
2309 SDValue X = Node->getOperand(0);
2310 SDValue N = Node->getOperand(1);
2311 EVT ExpVT = N.getValueType();
2312 EVT AsIntVT = VT.changeTypeToInteger();
2313 if (AsIntVT == EVT()) // TODO: How to handle f80?
2314 return SDValue();
2315
2316 if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
2317 return SDValue();
2318
2319 SDNodeFlags NSW;
2320 NSW.setNoSignedWrap(true);
2321 SDNodeFlags NUW_NSW;
2322 NUW_NSW.setNoUnsignedWrap(true);
2323 NUW_NSW.setNoSignedWrap(true);
2324
2325 EVT SetCCVT =
2326 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
2327 const fltSemantics &FltSem = VT.getFltSemantics();
2328
2329 const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
2330 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2331 const int Precision = APFloat::semanticsPrecision(FltSem);
2332
2333 const SDValue MaxExp = DAG.getSignedConstant(MaxExpVal, dl, ExpVT);
2334 const SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
2335
2336 const SDValue DoubleMaxExp = DAG.getSignedConstant(2 * MaxExpVal, dl, ExpVT);
2337
2338 const APFloat One(FltSem, "1.0");
2339 APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
2340
2341 // Offset by precision to avoid denormal range.
2342 APFloat ScaleDownK =
2343 scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
2344
2345 // TODO: Should really introduce control flow and use a block for the >
2346 // MaxExp, < MinExp cases
2347
2348 // First, handle exponents Exp > MaxExp and scale down.
2349 SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
2350
2351 SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
2352 SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
2353 SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
2354 SDValue DecN1 =
2355 DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
2356
2357 SDValue ScaleUpTwice =
2358 DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
2359
2360 const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
2361 SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
2362 SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
2363
2364 SDValue SelectN_Big =
2365 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
2366 SDValue SelectX_Big =
2367 DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
2368
2369 // Now handle exponents Exp < MinExp
2370 SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
2371
2372 SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
2373 SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
2374
2375 SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
2376
2377 SDValue ClampMinVal =
2378 DAG.getSignedConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
2379 SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
2380 SDValue IncN1 =
2381 DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
2382
2383 const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
2384 SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
2385 SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
2386
2387 SDValue ScaleDownTwice = DAG.getSetCC(
2388 dl, SetCCVT, N,
2389 DAG.getSignedConstant(2 * MinExpVal + Precision, dl, ExpVT), ISD::SETULT);
2390
2391 SDValue SelectN_Small =
2392 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
2393 SDValue SelectX_Small =
2394 DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
2395
2396 // Now combine the two out of range exponent handling cases with the base
2397 // case.
2398 SDValue NewX = DAG.getNode(
2399 ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
2400 DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
2401
2402 SDValue NewN = DAG.getNode(
2403 ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
2404 DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
2405
2406 SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
2407
2408 SDValue ExponentShiftAmt =
2409 DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
2410 SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
2411
2412 SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
2413 ExponentShiftAmt, NUW_NSW);
2414 SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
2415 return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
2416}
2417
2418SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
2419 SDLoc dl(Node);
2420 SDValue Val = Node->getOperand(0);
2421 EVT VT = Val.getValueType();
2422 EVT ExpVT = Node->getValueType(1);
2423 EVT AsIntVT = VT.changeTypeToInteger();
2424 if (AsIntVT == EVT()) // TODO: How to handle f80?
2425 return SDValue();
2426
2427 const fltSemantics &FltSem = VT.getFltSemantics();
2428 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2429 const unsigned Precision = APFloat::semanticsPrecision(FltSem);
2430 const unsigned BitSize = VT.getScalarSizeInBits();
2431
2432 // TODO: Could introduce control flow and skip over the denormal handling.
2433
2434 // scale_up = fmul value, scalbn(1.0, precision + 1)
2435 // extracted_exp = (bitcast value to uint) >> precision - 1
2436 // biased_exp = extracted_exp + min_exp
2437 // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
2438 //
2439 // is_denormal = val < smallest_normalized
2440 // computed_fract = is_denormal ? scale_up : extracted_fract
2441 // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
2442 //
2443 // result_0 = (!isfinite(val) || iszero(val)) ? val : computed_fract
2444 // result_1 = (!isfinite(val) || iszero(val)) ? 0 : computed_exp
2445
2446 SDValue NegSmallestNormalizedInt = DAG.getConstant(
2447 APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
2448 AsIntVT);
2449
2450 SDValue SmallestNormalizedInt = DAG.getConstant(
2451 APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
2452 AsIntVT);
2453
2454 // Masks out the exponent bits.
2455 SDValue ExpMask =
2456 DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
2457
2458 // Mask out the exponent part of the value.
2459 //
2460 // e.g, for f32 FractSignMaskVal = 0x807fffff
2461 APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
2462 FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
2463
2464 APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
2465 SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
2466
2467 SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
2468
2469 const APFloat One(FltSem, "1.0");
2470 // Scale a possible denormal input.
2471 // e.g., for f64, 0x1p+54
2472 APFloat ScaleUpKVal =
2473 scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
2474
2475 SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
2476 SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
2477
2478 EVT SetCCVT =
2479 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2480
2481 SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
2482
2483 SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
2484
2485 SDValue AddNegSmallestNormal =
2486 DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
2487 SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
2488 NegSmallestNormalizedInt, ISD::SETULE);
2489
2490 SDValue IsDenormal =
2491 DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
2492
2493 SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
2494 SDValue Zero = DAG.getConstant(0, dl, ExpVT);
2495
2496 SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
2497 SDValue ScaledSelect =
2498 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
2499
2500 SDValue ExpMaskScaled =
2501 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
2502
2503 SDValue ScaledValue =
2504 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
2505
2506 // Extract the exponent bits.
2507 SDValue ExponentShiftAmt =
2508 DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
2509 SDValue ShiftedExp =
2510 DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
2511 SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
2512
2513 SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
2514 SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
2515 SDValue DenormalExpBias =
2516 DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
2517
2518 SDValue MaskedFractAsInt =
2519 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
2520 const APFloat Half(FltSem, "0.5");
2521 SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
2522 SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
2523 SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
2524
2525 SDValue ComputedExp =
2526 DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
2527
2528 SDValue Result0 =
2529 DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
2530
2531 SDValue Result1 =
2532 DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
2533
2534 return DAG.getMergeValues({Result0, Result1}, dl);
2535}
2536
2537/// This function is responsible for legalizing a
2538/// INT_TO_FP operation of the specified operand when the target requests that
2539/// we expand it. At this point, we know that the result and operand types are
2540/// legal for the target.
2541SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2542 SDValue &Chain) {
2543 bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2544 Node->getOpcode() == ISD::SINT_TO_FP);
2545 EVT DestVT = Node->getValueType(0);
2546 SDLoc dl(Node);
2547 unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2548 SDValue Op0 = Node->getOperand(OpNo);
2549 EVT SrcVT = Op0.getValueType();
2550
2551 // TODO: Should any fast-math-flags be set for the created nodes?
2552 LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2553 if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2554 (DestVT.bitsLE(MVT::f64) ||
2555 TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2557 DestVT))) {
2558 LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
2559 "expansion\n");
2560
2561 // Get the stack frame index of a 8 byte buffer.
2562 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
2563
2564 SDValue Lo = Op0;
2565 // if signed map to unsigned space
2566 if (isSigned) {
2567 // Invert sign bit (signed to unsigned mapping).
2568 Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
2569 DAG.getConstant(0x80000000u, dl, MVT::i32));
2570 }
2571 // Initial hi portion of constructed double.
2572 SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
2573
2574 // If this a big endian target, swap the lo and high data.
2575 if (DAG.getDataLayout().isBigEndian())
2576 std::swap(Lo, Hi);
2577
2578 SDValue MemChain = DAG.getEntryNode();
2579
2580 // Store the lo of the constructed double.
2581 SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
2583 // Store the hi of the constructed double.
2584 SDValue HiPtr =
2585 DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl);
2586 SDValue Store2 =
2587 DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
2588 MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
2589
2590 // load the constructed double
2591 SDValue Load =
2592 DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
2593 // FP constant to bias correct the final result
2594 SDValue Bias = DAG.getConstantFP(
2595 isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
2596 : llvm::bit_cast<double>(0x4330000000000000ULL),
2597 dl, MVT::f64);
2598 // Subtract the bias and get the final result.
2599 SDValue Sub;
2601 if (Node->isStrictFPOpcode()) {
2602 Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2603 {Node->getOperand(0), Load, Bias});
2604 Chain = Sub.getValue(1);
2605 if (DestVT != Sub.getValueType()) {
2606 std::pair<SDValue, SDValue> ResultPair;
2607 ResultPair =
2608 DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2609 Result = ResultPair.first;
2610 Chain = ResultPair.second;
2611 }
2612 else
2613 Result = Sub;
2614 } else {
2615 Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2616 Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2617 }
2618 return Result;
2619 }
2620
2621 if (isSigned)
2622 return SDValue();
2623
2624 // TODO: Generalize this for use with other types.
2625 if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2626 (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2627 LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
2628 // For unsigned conversions, convert them to signed conversions using the
2629 // algorithm from the x86_64 __floatundisf in compiler_rt. That method
2630 // should be valid for i32->f32 as well.
2631
2632 // More generally this transform should be valid if there are 3 more bits
2633 // in the integer type than the significand. Rounding uses the first bit
2634 // after the width of the significand and the OR of all bits after that. So
2635 // we need to be able to OR the shifted out bit into one of the bits that
2636 // participate in the OR.
2637
2638 // TODO: This really should be implemented using a branch rather than a
2639 // select. We happen to get lucky and machinesink does the right
2640 // thing most of the time. This would be a good candidate for a
2641 // pseudo-op, or, even better, for whole-function isel.
2642 EVT SetCCVT = getSetCCResultType(SrcVT);
2643
2644 SDValue SignBitTest = DAG.getSetCC(
2645 dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2646
2647 EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout());
2648 SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT);
2649 SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
2650 SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
2651 SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
2652 SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
2653
2654 SDValue Slow, Fast;
2655 if (Node->isStrictFPOpcode()) {
2656 // In strict mode, we must avoid spurious exceptions, and therefore
2657 // must make sure to only emit a single STRICT_SINT_TO_FP.
2658 SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
2659 Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2660 { Node->getOperand(0), InCvt });
2661 Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2662 { Fast.getValue(1), Fast, Fast });
2663 Chain = Slow.getValue(1);
2664 // The STRICT_SINT_TO_FP inherits the exception mode from the
2665 // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
2666 // never raise any exception.
2668 Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
2669 Fast->setFlags(Flags);
2670 Flags.setNoFPExcept(true);
2671 Slow->setFlags(Flags);
2672 } else {
2673 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
2674 Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
2675 Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2676 }
2677
2678 return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
2679 }
2680
2681 // Don't expand it if there isn't cheap fadd.
2682 if (!TLI.isOperationLegalOrCustom(
2683 Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2684 return SDValue();
2685
2686 // The following optimization is valid only if every value in SrcVT (when
2687 // treated as signed) is representable in DestVT. Check that the mantissa
2688 // size of DestVT is >= than the number of bits in SrcVT -1.
2689 assert(APFloat::semanticsPrecision(DestVT.getFltSemantics()) >=
2690 SrcVT.getSizeInBits() - 1 &&
2691 "Cannot perform lossless SINT_TO_FP!");
2692
2693 SDValue Tmp1;
2694 if (Node->isStrictFPOpcode()) {
2695 Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2696 { Node->getOperand(0), Op0 });
2697 } else
2698 Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2699
2700 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
2701 DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2702 SDValue Zero = DAG.getIntPtrConstant(0, dl),
2703 Four = DAG.getIntPtrConstant(4, dl);
2704 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
2705 SignSet, Four, Zero);
2706
2707 // If the sign bit of the integer is set, the large number will be treated
2708 // as a negative number. To counteract this, the dynamic code adds an
2709 // offset depending on the data type.
2710 uint64_t FF;
2711 switch (SrcVT.getSimpleVT().SimpleTy) {
2712 default:
2713 return SDValue();
2714 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2715 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2716 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2717 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2718 }
2719 if (DAG.getDataLayout().isLittleEndian())
2720 FF <<= 32;
2721 Constant *FudgeFactor = ConstantInt::get(
2722 Type::getInt64Ty(*DAG.getContext()), FF);
2723
2724 SDValue CPIdx =
2725 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
2726 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2727 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
2728 Alignment = commonAlignment(Alignment, 4);
2729 SDValue FudgeInReg;
2730 if (DestVT == MVT::f32)
2731 FudgeInReg = DAG.getLoad(
2732 MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2733 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2734 Alignment);
2735 else {
2736 SDValue Load = DAG.getExtLoad(
2737 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2738 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
2739 Alignment);
2740 HandleSDNode Handle(Load);
2741 LegalizeOp(Load.getNode());
2742 FudgeInReg = Handle.getValue();
2743 }
2744
2745 if (Node->isStrictFPOpcode()) {
2746 SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2747 { Tmp1.getValue(1), Tmp1, FudgeInReg });
2748 Chain = Result.getValue(1);
2749 return Result;
2750 }
2751
2752 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
2753}
2754
2755/// This function is responsible for legalizing a
2756/// *INT_TO_FP operation of the specified operand when the target requests that
2757/// we promote it. At this point, we know that the result and operand types are
2758/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2759/// operation that takes a larger input.
2760void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
2762 bool IsStrict = N->isStrictFPOpcode();
2763 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
2764 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
2765 EVT DestVT = N->getValueType(0);
2766 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2767 unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
2768 unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
2769
2770 // First step, figure out the appropriate *INT_TO_FP operation to use.
2771 EVT NewInTy = LegalOp.getValueType();
2772
2773 unsigned OpToUse = 0;
2774
2775 // Scan for the appropriate larger type to use.
2776 while (true) {
2777 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
2778 assert(NewInTy.isInteger() && "Ran out of possibilities!");
2779
2780 // If the target supports SINT_TO_FP of this type, use it.
2781 if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
2782 OpToUse = SIntOp;
2783 break;
2784 }
2785 if (IsSigned)
2786 continue;
2787
2788 // If the target supports UINT_TO_FP of this type, use it.
2789 if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
2790 OpToUse = UIntOp;
2791 break;
2792 }
2793
2794 // Otherwise, try a larger type.
2795 }
2796
2797 // Okay, we found the operation and type to use. Zero extend our input to the
2798 // desired type then run the operation on it.
2799 if (IsStrict) {
2800 SDValue Res =
2801 DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
2802 {N->getOperand(0),
2803 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2804 dl, NewInTy, LegalOp)});
2805 Results.push_back(Res);
2806 Results.push_back(Res.getValue(1));
2807 return;
2808 }
2809
2810 Results.push_back(
2811 DAG.getNode(OpToUse, dl, DestVT,
2812 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2813 dl, NewInTy, LegalOp)));
2814}
2815
2816/// This function is responsible for legalizing a
2817/// FP_TO_*INT operation of the specified operand when the target requests that
2818/// we promote it. At this point, we know that the result and operand types are
2819/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2820/// operation that returns a larger result.
2821void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
2823 bool IsStrict = N->isStrictFPOpcode();
2824 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
2825 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
2826 EVT DestVT = N->getValueType(0);
2827 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2828 // First step, figure out the appropriate FP_TO*INT operation to use.
2829 EVT NewOutTy = DestVT;
2830
2831 unsigned OpToUse = 0;
2832
2833 // Scan for the appropriate larger type to use.
2834 while (true) {
2835 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
2836 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2837
2838 // A larger signed type can hold all unsigned values of the requested type,
2839 // so using FP_TO_SINT is valid
2840 OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
2841 if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
2842 break;
2843
2844 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2845 OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
2846 if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
2847 break;
2848
2849 // Otherwise, try a larger type.
2850 }
2851
2852 // Okay, we found the operation and type to use.
2854 if (IsStrict) {
2855 SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
2856 Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
2857 } else
2858 Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
2859
2860 // Truncate the result of the extended FP_TO_*INT operation to the desired
2861 // size.
2862 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2863 Results.push_back(Trunc);
2864 if (IsStrict)
2865 Results.push_back(Operation.getValue(1));
2866}
2867
2868/// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
2869/// the result and operand types are legal and there must be a legal
2870/// FP_TO_*INT_SAT operation for a larger result type.
2871SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
2872 const SDLoc &dl) {
2873 unsigned Opcode = Node->getOpcode();
2874
2875 // Scan for the appropriate larger type to use.
2876 EVT NewOutTy = Node->getValueType(0);
2877 while (true) {
2878 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
2879 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2880
2881 if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
2882 break;
2883 }
2884
2885 // Saturation width is determined by second operand, so we don't have to
2886 // perform any fixup and can directly truncate the result.
2887 SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
2888 Node->getOperand(1));
2889 return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2890}
2891
2892/// Open code the operations for PARITY of the specified operation.
2893SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
2894 EVT VT = Op.getValueType();
2895 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2896 unsigned Sz = VT.getScalarSizeInBits();
2897
2898 // If CTPOP is legal, use it. Otherwise use shifts and xor.
2900 if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) {
2901 Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
2902 } else {
2903 Result = Op;
2904 for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
2905 SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
2906 DAG.getConstant(1ULL << (--i), dl, ShVT));
2907 Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
2908 }
2909 }
2910
2911 return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
2912}
2913
2914SDValue SelectionDAGLegalize::PromoteReduction(SDNode *Node) {
2915 MVT VecVT = Node->getOperand(1).getSimpleValueType();
2916 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
2917 MVT ScalarVT = Node->getSimpleValueType(0);
2918 MVT NewScalarVT = NewVecVT.getVectorElementType();
2919
2920 SDLoc DL(Node);
2921 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
2922
2923 // promote the initial value.
2924 // FIXME: Support integer.
2925 assert(Node->getOperand(0).getValueType().isFloatingPoint() &&
2926 "Only FP promotion is supported");
2927 Operands[0] =
2928 DAG.getNode(ISD::FP_EXTEND, DL, NewScalarVT, Node->getOperand(0));
2929
2930 for (unsigned j = 1; j != Node->getNumOperands(); ++j)
2931 if (Node->getOperand(j).getValueType().isVector() &&
2932 !(ISD::isVPOpcode(Node->getOpcode()) &&
2933 ISD::getVPMaskIdx(Node->getOpcode()) == j)) { // Skip mask operand.
2934 // promote the vector operand.
2935 // FIXME: Support integer.
2936 assert(Node->getOperand(j).getValueType().isFloatingPoint() &&
2937 "Only FP promotion is supported");
2938 Operands[j] =
2939 DAG.getNode(ISD::FP_EXTEND, DL, NewVecVT, Node->getOperand(j));
2940 } else {
2941 Operands[j] = Node->getOperand(j); // Skip VL operand.
2942 }
2943
2944 SDValue Res = DAG.getNode(Node->getOpcode(), DL, NewScalarVT, Operands,
2945 Node->getFlags());
2946
2947 assert(ScalarVT.isFloatingPoint() && "Only FP promotion is supported");
2948 return DAG.getNode(ISD::FP_ROUND, DL, ScalarVT, Res,
2949 DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
2950}
2951
2952bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
2953 LLVM_DEBUG(dbgs() << "Trying to expand node\n");
2955 SDLoc dl(Node);
2956 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
2957 bool NeedInvert;
2958 switch (Node->getOpcode()) {
2959 case ISD::ABS:
2960 if ((Tmp1 = TLI.expandABS(Node, DAG)))
2961 Results.push_back(Tmp1);
2962 break;
2963 case ISD::ABDS:
2964 case ISD::ABDU:
2965 if ((Tmp1 = TLI.expandABD(Node, DAG)))
2966 Results.push_back(Tmp1);
2967 break;
2968 case ISD::AVGCEILS:
2969 case ISD::AVGCEILU:
2970 case ISD::AVGFLOORS:
2971 case ISD::AVGFLOORU:
2972 if ((Tmp1 = TLI.expandAVG(Node, DAG)))
2973 Results.push_back(Tmp1);
2974 break;
2975 case ISD::CTPOP:
2976 if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
2977 Results.push_back(Tmp1);
2978 break;
2979 case ISD::CTLZ:
2981 if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
2982 Results.push_back(Tmp1);
2983 break;
2984 case ISD::CTTZ:
2986 if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
2987 Results.push_back(Tmp1);
2988 break;
2989 case ISD::BITREVERSE:
2990 if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
2991 Results.push_back(Tmp1);
2992 break;
2993 case ISD::BSWAP:
2994 if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
2995 Results.push_back(Tmp1);
2996 break;
2997 case ISD::PARITY:
2998 Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
2999 break;
3000 case ISD::FRAMEADDR:
3001 case ISD::RETURNADDR:
3003 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3004 break;
3005 case ISD::EH_DWARF_CFA: {
3006 SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
3007 TLI.getPointerTy(DAG.getDataLayout()));
3008 SDValue Offset = DAG.getNode(ISD::ADD, dl,
3009 CfaArg.getValueType(),
3010 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
3011 CfaArg.getValueType()),
3012 CfaArg);
3013 SDValue FA = DAG.getNode(
3014 ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
3015 DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
3016 Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
3017 FA, Offset));
3018 break;
3019 }
3020 case ISD::GET_ROUNDING:
3021 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
3022 Results.push_back(Node->getOperand(0));
3023 break;
3024 case ISD::EH_RETURN:
3025 case ISD::EH_LABEL:
3026 case ISD::PREFETCH:
3027 case ISD::VAEND:
3029 // If the target didn't expand these, there's nothing to do, so just
3030 // preserve the chain and be done.
3031 Results.push_back(Node->getOperand(0));
3032 break;
3035 // If the target didn't expand this, just return 'zero' and preserve the
3036 // chain.
3037 Results.append(Node->getNumValues() - 1,
3038 DAG.getConstant(0, dl, Node->getValueType(0)));
3039 Results.push_back(Node->getOperand(0));
3040 break;
3042 // If the target didn't expand this, just return 'zero' and preserve the
3043 // chain.
3044 Results.push_back(DAG.getConstant(0, dl, MVT::i32));
3045 Results.push_back(Node->getOperand(0));
3046 break;
3047 case ISD::ATOMIC_LOAD: {
3048 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
3049 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
3050 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3051 SDValue Swap = DAG.getAtomicCmpSwap(
3052 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3053 Node->getOperand(0), Node->getOperand(1), Zero, Zero,
3054 cast<AtomicSDNode>(Node)->getMemOperand());
3055 Results.push_back(Swap.getValue(0));
3056 Results.push_back(Swap.getValue(1));
3057 break;
3058 }
3059 case ISD::ATOMIC_STORE: {
3060 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3061 SDValue Swap = DAG.getAtomic(
3062 ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(),
3063 Node->getOperand(0), Node->getOperand(2), Node->getOperand(1),
3064 cast<AtomicSDNode>(Node)->getMemOperand());
3065 Results.push_back(Swap.getValue(1));
3066 break;
3067 }
3069 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
3070 // splits out the success value as a comparison. Expanding the resulting
3071 // ATOMIC_CMP_SWAP will produce a libcall.
3072 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3073 SDValue Res = DAG.getAtomicCmpSwap(
3074 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3075 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
3076 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
3077
3078 SDValue ExtRes = Res;
3079 SDValue LHS = Res;
3080 SDValue RHS = Node->getOperand(1);
3081
3082 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
3083 EVT OuterType = Node->getValueType(0);
3084 switch (TLI.getExtendForAtomicOps()) {
3085 case ISD::SIGN_EXTEND:
3086 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
3087 DAG.getValueType(AtomicType));
3088 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
3089 Node->getOperand(2), DAG.getValueType(AtomicType));
3090 ExtRes = LHS;
3091 break;
3092 case ISD::ZERO_EXTEND:
3093 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
3094 DAG.getValueType(AtomicType));
3095 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3096 ExtRes = LHS;
3097 break;
3098 case ISD::ANY_EXTEND:
3099 LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
3100 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3101 break;
3102 default:
3103 llvm_unreachable("Invalid atomic op extension");
3104 }
3105
3107 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
3108
3109 Results.push_back(ExtRes.getValue(0));
3110 Results.push_back(Success);
3111 Results.push_back(Res.getValue(1));
3112 break;
3113 }
3114 case ISD::ATOMIC_LOAD_SUB: {
3115 SDLoc DL(Node);
3116 EVT VT = Node->getValueType(0);
3117 SDValue RHS = Node->getOperand(2);
3118 AtomicSDNode *AN = cast<AtomicSDNode>(Node);
3119 if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG &&
3120 cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT())
3121 RHS = RHS->getOperand(0);
3122 SDValue NewRHS =
3123 DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS);
3124 SDValue Res = DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, AN->getMemoryVT(),
3125 Node->getOperand(0), Node->getOperand(1),
3126 NewRHS, AN->getMemOperand());
3127 Results.push_back(Res);
3128 Results.push_back(Res.getValue(1));
3129 break;
3130 }
3132 ExpandDYNAMIC_STACKALLOC(Node, Results);
3133 break;
3134 case ISD::MERGE_VALUES:
3135 for (unsigned i = 0; i < Node->getNumValues(); i++)
3136 Results.push_back(Node->getOperand(i));
3137 break;
3138 case ISD::UNDEF: {
3139 EVT VT = Node->getValueType(0);
3140 if (VT.isInteger())
3141 Results.push_back(DAG.getConstant(0, dl, VT));
3142 else {
3143 assert(VT.isFloatingPoint() && "Unknown value type!");
3144 Results.push_back(DAG.getConstantFP(0, dl, VT));
3145 }
3146 break;
3147 }
3149 // When strict mode is enforced we can't do expansion because it
3150 // does not honor the "strict" properties. Only libcall is allowed.
3151 if (TLI.isStrictFPEnabled())
3152 break;
3153 // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3154 // since this operation is more efficient than stack operation.
3155 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3156 Node->getValueType(0))
3157 == TargetLowering::Legal)
3158 break;
3159 // We fall back to use stack operation when the FP_ROUND operation
3160 // isn't available.
3161 if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3162 Node->getValueType(0), dl,
3163 Node->getOperand(0)))) {
3164 ReplaceNode(Node, Tmp1.getNode());
3165 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
3166 return true;
3167 }
3168 break;
3169 case ISD::FP_ROUND: {
3170 if ((Tmp1 = TLI.expandFP_ROUND(Node, DAG))) {
3171 Results.push_back(Tmp1);
3172 break;
3173 }
3174
3175 [[fallthrough]];
3176 }
3177 case ISD::BITCAST:
3178 if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3179 Node->getValueType(0), dl)))
3180 Results.push_back(Tmp1);
3181 break;
3183 // When strict mode is enforced we can't do expansion because it
3184 // does not honor the "strict" properties. Only libcall is allowed.
3185 if (TLI.isStrictFPEnabled())
3186 break;
3187 // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3188 // since this operation is more efficient than stack operation.
3189 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3190 Node->getValueType(0))
3191 == TargetLowering::Legal)
3192 break;
3193 // We fall back to use stack operation when the FP_EXTEND operation
3194 // isn't available.
3195 if ((Tmp1 = EmitStackConvert(
3196 Node->getOperand(1), Node->getOperand(1).getValueType(),
3197 Node->getValueType(0), dl, Node->getOperand(0)))) {
3198 ReplaceNode(Node, Tmp1.getNode());
3199 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
3200 return true;
3201 }
3202 break;
3203 case ISD::FP_EXTEND: {
3204 SDValue Op = Node->getOperand(0);
3205 EVT SrcVT = Op.getValueType();
3206 EVT DstVT = Node->getValueType(0);
3207 if (SrcVT.getScalarType() == MVT::bf16) {
3208 Results.push_back(DAG.getNode(ISD::BF16_TO_FP, SDLoc(Node), DstVT, Op));
3209 break;
3210 }
3211
3212 if ((Tmp1 = EmitStackConvert(Op, SrcVT, DstVT, dl)))
3213 Results.push_back(Tmp1);
3214 break;
3215 }
3216 case ISD::BF16_TO_FP: {
3217 // Always expand bf16 to f32 casts, they lower to ext + shift.
3218 //
3219 // Note that the operand of this code can be bf16 or an integer type in case
3220 // bf16 is not supported on the target and was softened.
3221 SDValue Op = Node->getOperand(0);
3222 if (Op.getValueType() == MVT::bf16) {
3223 Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3224 DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3225 } else {
3226 Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3227 }
3228 Op = DAG.getNode(
3229 ISD::SHL, dl, MVT::i32, Op,
3230 DAG.getConstant(16, dl,
3231 TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3232 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3233 // Add fp_extend in case the output is bigger than f32.
3234 if (Node->getValueType(0) != MVT::f32)
3235 Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3236 Results.push_back(Op);
3237 break;
3238 }
3239 case ISD::FP_TO_BF16: {
3240 SDValue Op = Node->getOperand(0);
3241 if (Op.getValueType() != MVT::f32)
3242 Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3243 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3244 // Certain SNaNs will turn into infinities if we do a simple shift right.
3245 if (!DAG.isKnownNeverSNaN(Op)) {
3246 Op = DAG.getNode(ISD::FCANONICALIZE, dl, MVT::f32, Op, Node->getFlags());
3247 }
3248 Op = DAG.getNode(
3249 ISD::SRL, dl, MVT::i32, DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3250 DAG.getConstant(16, dl,
3251 TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3252 // The result of this node can be bf16 or an integer type in case bf16 is
3253 // not supported on the target and was softened to i16 for storage.
3254 if (Node->getValueType(0) == MVT::bf16) {
3255 Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3256 DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3257 } else {
3258 Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3259 }
3260 Results.push_back(Op);
3261 break;
3262 }
3264 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3265 EVT VT = Node->getValueType(0);
3266
3267 // An in-register sign-extend of a boolean is a negation:
3268 // 'true' (1) sign-extended is -1.
3269 // 'false' (0) sign-extended is 0.
3270 // However, we must mask the high bits of the source operand because the
3271 // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
3272
3273 // TODO: Do this for vectors too?
3274 if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
3275 SDValue One = DAG.getConstant(1, dl, VT);
3276 SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
3277 SDValue Zero = DAG.getConstant(0, dl, VT);
3278 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
3279 Results.push_back(Neg);
3280 break;
3281 }
3282
3283 // NOTE: we could fall back on load/store here too for targets without
3284 // SRA. However, it is doubtful that any exist.
3285 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3286 unsigned BitsDiff = VT.getScalarSizeInBits() -
3287 ExtraVT.getScalarSizeInBits();
3288 SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
3289 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3290 Node->getOperand(0), ShiftCst);
3291 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3292 Results.push_back(Tmp1);
3293 break;
3294 }
3295 case ISD::UINT_TO_FP:
3297 if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
3298 Results.push_back(Tmp1);
3299 if (Node->isStrictFPOpcode())
3300 Results.push_back(Tmp2);
3301 break;
3302 }
3303 [[fallthrough]];
3304 case ISD::SINT_TO_FP:
3306 if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
3307 Results.push_back(Tmp1);
3308 if (Node->isStrictFPOpcode())
3309 Results.push_back(Tmp2);
3310 }
3311 break;
3312 case ISD::FP_TO_SINT:
3313 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
3314 Results.push_back(Tmp1);
3315 break;
3317 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
3318 ReplaceNode(Node, Tmp1.getNode());
3319 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
3320 return true;
3321 }
3322 break;
3323 case ISD::FP_TO_UINT:
3324 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
3325 Results.push_back(Tmp1);
3326 break;
3328 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
3329 // Relink the chain.
3330 DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
3331 // Replace the new UINT result.
3332 ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
3333 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
3334 return true;
3335 }
3336 break;
3339 Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3340 break;
3341 case ISD::LROUND:
3342 case ISD::LLROUND: {
3343 SDValue Arg = Node->getOperand(0);
3344 EVT ArgVT = Arg.getValueType();
3345 EVT ResVT = Node->getValueType(0);
3346 SDLoc dl(Node);
3347 SDValue RoundNode = DAG.getNode(ISD::FROUND, dl, ArgVT, Arg);
3348 Results.push_back(DAG.getNode(ISD::FP_TO_SINT, dl, ResVT, RoundNode));
3349 break;
3350 }
3351 case ISD::VAARG:
3352 Results.push_back(DAG.expandVAArg(Node));
3353 Results.push_back(Results[0].getValue(1));
3354 break;
3355 case ISD::VACOPY:
3356 Results.push_back(DAG.expandVACopy(Node));
3357 break;
3359 if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar())
3360 // This must be an access of the only element. Return it.
3361 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
3362 Node->getOperand(0));
3363 else
3364 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3365 Results.push_back(Tmp1);
3366 break;
3368 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3369 break;
3371 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3372 break;
3374 Results.push_back(ExpandVectorBuildThroughStack(Node));
3375 break;
3377 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3378 break;
3380 Results.push_back(ExpandINSERT_VECTOR_ELT(SDValue(Node, 0)));
3381 break;
3382 case ISD::VECTOR_SHUFFLE: {
3383 SmallVector<int, 32> NewMask;
3384 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
3385
3386 EVT VT = Node->getValueType(0);
3387 EVT EltVT = VT.getVectorElementType();
3388 SDValue Op0 = Node->getOperand(0);
3389 SDValue Op1 = Node->getOperand(1);
3390 if (!TLI.isTypeLegal(EltVT)) {
3391 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3392
3393 // BUILD_VECTOR operands are allowed to be wider than the element type.
3394 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3395 // it.
3396 if (NewEltVT.bitsLT(EltVT)) {
3397 // Convert shuffle node.
3398 // If original node was v4i64 and the new EltVT is i32,
3399 // cast operands to v8i32 and re-build the mask.
3400
3401 // Calculate new VT, the size of the new VT should be equal to original.
3402 EVT NewVT =
3403 EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3404 VT.getSizeInBits() / NewEltVT.getSizeInBits());
3405 assert(NewVT.bitsEq(VT));
3406
3407 // cast operands to new VT
3408 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3409 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3410
3411 // Convert the shuffle mask
3412 unsigned int factor =
3414
3415 // EltVT gets smaller
3416 assert(factor > 0);
3417
3418 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3419 if (Mask[i] < 0) {
3420 for (unsigned fi = 0; fi < factor; ++fi)
3421 NewMask.push_back(Mask[i]);
3422 }
3423 else {
3424 for (unsigned fi = 0; fi < factor; ++fi)
3425 NewMask.push_back(Mask[i]*factor+fi);
3426 }
3427 }
3428 Mask = NewMask;
3429 VT = NewVT;
3430 }
3431 EltVT = NewEltVT;
3432 }
3433 unsigned NumElems = VT.getVectorNumElements();
3435 for (unsigned i = 0; i != NumElems; ++i) {
3436 if (Mask[i] < 0) {
3437 Ops.push_back(DAG.getUNDEF(EltVT));
3438 continue;
3439 }
3440 unsigned Idx = Mask[i];
3441 if (Idx < NumElems)
3442 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3443 DAG.getVectorIdxConstant(Idx, dl)));
3444 else
3445 Ops.push_back(
3446 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3447 DAG.getVectorIdxConstant(Idx - NumElems, dl)));
3448 }
3449
3450 Tmp1 = DAG.getBuildVector(VT, dl, Ops);
3451 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3452 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
3453 Results.push_back(Tmp1);
3454 break;
3455 }
3456 case ISD::VECTOR_SPLICE: {
3457 Results.push_back(TLI.expandVectorSplice(Node, DAG));
3458 break;
3459 }
3460 case ISD::EXTRACT_ELEMENT: {
3461 EVT OpTy = Node->getOperand(0).getValueType();
3462 if (Node->getConstantOperandVal(1)) {
3463 // 1 -> Hi
3464 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3465 DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
3466 TLI.getShiftAmountTy(
3467 Node->getOperand(0).getValueType(),
3468 DAG.getDataLayout())));
3469 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3470 } else {
3471 // 0 -> Lo
3472 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3473 Node->getOperand(0));
3474 }
3475 Results.push_back(Tmp1);
3476 break;
3477 }
3478 case ISD::STACKSAVE:
3479 // Expand to CopyFromReg if the target set
3480 // StackPointerRegisterToSaveRestore.
3481 if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
3482 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3483 Node->getValueType(0)));
3484 Results.push_back(Results[0].getValue(1));
3485 } else {
3486 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3487 Results.push_back(Node->getOperand(0));
3488 }
3489 break;
3490 case ISD::STACKRESTORE:
3491 // Expand to CopyToReg if the target set
3492 // StackPointerRegisterToSaveRestore.
3493 if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
3494 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3495 Node->getOperand(1)));
3496 } else {
3497 Results.push_back(Node->getOperand(0));
3498 }
3499 break;
3501 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3502 Results.push_back(Results[0].getValue(0));
3503 break;
3504 case ISD::FCOPYSIGN:
3505 Results.push_back(ExpandFCOPYSIGN(Node));
3506 break;
3507 case ISD::FNEG:
3508 Results.push_back(ExpandFNEG(Node));
3509 break;
3510 case ISD::FABS:
3511 Results.push_back(ExpandFABS(Node));
3512 break;
3513 case ISD::IS_FPCLASS: {
3514 auto Test = static_cast<FPClassTest>(Node->getConstantOperandVal(1));
3515 if (SDValue Expanded =
3516 TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
3517 Test, Node->getFlags(), SDLoc(Node), DAG))
3518 Results.push_back(Expanded);
3519 break;
3520 }
3521 case ISD::SMIN:
3522 case ISD::SMAX:
3523 case ISD::UMIN:
3524 case ISD::UMAX: {
3525 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3526 ISD::CondCode Pred;
3527 switch (Node->getOpcode()) {
3528 default: llvm_unreachable("How did we get here?");
3529 case ISD::SMAX: Pred = ISD::SETGT; break;
3530 case ISD::SMIN: Pred = ISD::SETLT; break;
3531 case ISD::UMAX: Pred = ISD::SETUGT; break;
3532 case ISD::UMIN: Pred = ISD::SETULT; break;
3533 }
3534 Tmp1 = Node->getOperand(0);
3535 Tmp2 = Node->getOperand(1);
3536 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3537 Results.push_back(Tmp1);
3538 break;
3539 }
3540 case ISD::FMINNUM:
3541 case ISD::FMAXNUM: {
3542 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
3543 Results.push_back(Expanded);
3544 break;
3545 }
3546 case ISD::FMINIMUM:
3547 case ISD::FMAXIMUM: {
3548 if (SDValue Expanded = TLI.expandFMINIMUM_FMAXIMUM(Node, DAG))
3549 Results.push_back(Expanded);
3550 break;
3551 }
3552 case ISD::FMINIMUMNUM:
3553 case ISD::FMAXIMUMNUM: {
3554 Results.push_back(TLI.expandFMINIMUMNUM_FMAXIMUMNUM(Node, DAG));
3555 break;
3556 }
3557 case ISD::FSIN:
3558 case ISD::FCOS: {
3559 EVT VT = Node->getValueType(0);
3560 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3561 // fcos which share the same operand and both are used.
3562 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
3564 && useSinCos(Node)) {
3565 SDVTList VTs = DAG.getVTList(VT, VT);
3566 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
3567 if (Node->getOpcode() == ISD::FCOS)
3568 Tmp1 = Tmp1.getValue(1);
3569 Results.push_back(Tmp1);
3570 }
3571 break;
3572 }
3573 case ISD::FLDEXP:
3574 case ISD::STRICT_FLDEXP: {
3575 EVT VT = Node->getValueType(0);
3577 // Use the LibCall instead, it is very likely faster
3578 // FIXME: Use separate LibCall action.
3579 if (TLI.getLibcallName(LC))
3580 break;
3581
3582 if (SDValue Expanded = expandLdexp(Node)) {
3583 Results.push_back(Expanded);
3584 if (Node->getOpcode() == ISD::STRICT_FLDEXP)
3585 Results.push_back(Expanded.getValue(1));
3586 }
3587
3588 break;
3589 }
3590 case ISD::FFREXP: {
3591 RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
3592 // Use the LibCall instead, it is very likely faster
3593 // FIXME: Use separate LibCall action.
3594 if (TLI.getLibcallName(LC))
3595 break;
3596
3597 if (SDValue Expanded = expandFrexp(Node)) {
3598 Results.push_back(Expanded);
3599 Results.push_back(Expanded.getValue(1));
3600 }
3601 break;
3602 }
3603 case ISD::FSINCOS: {
3605 break;
3606 EVT VT = Node->getValueType(0);
3607 SDValue Op = Node->getOperand(0);
3608 SDNodeFlags Flags = Node->getFlags();
3609 Tmp1 = DAG.getNode(ISD::FSIN, dl, VT, Op, Flags);
3610 Tmp2 = DAG.getNode(ISD::FCOS, dl, VT, Op, Flags);
3611 Results.append({Tmp1, Tmp2});
3612 break;
3613 }
3614 case ISD::FMAD:
3615 llvm_unreachable("Illegal fmad should never be formed");
3616
3617 case ISD::FP16_TO_FP:
3618 if (Node->getValueType(0) != MVT::f32) {
3619 // We can extend to types bigger than f32 in two steps without changing
3620 // the result. Since "f16 -> f32" is much more commonly available, give
3621 // CodeGen the option of emitting that before resorting to a libcall.
3622 SDValue Res =
3623 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3624 Results.push_back(
3625 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
3626 }
3627 break;
3630 if (Node->getValueType(0) != MVT::f32) {
3631 // We can extend to types bigger than f32 in two steps without changing
3632 // the result. Since "f16 -> f32" is much more commonly available, give
3633 // CodeGen the option of emitting that before resorting to a libcall.
3634 SDValue Res = DAG.getNode(Node->getOpcode(), dl, {MVT::f32, MVT::Other},
3635 {Node->getOperand(0), Node->getOperand(1)});
3636 Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
3637 {Node->getValueType(0), MVT::Other},
3638 {Res.getValue(1), Res});
3639 Results.push_back(Res);
3640 Results.push_back(Res.getValue(1));
3641 }
3642 break;
3643 case ISD::FP_TO_FP16:
3644 LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
3645 if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
3646 SDValue Op = Node->getOperand(0);
3647 MVT SVT = Op.getSimpleValueType();
3648 if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3649 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3650 // Under fastmath, we can expand this node into a fround followed by
3651 // a float-half conversion.
3652 SDValue FloatVal =
3653 DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3654 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3655 Results.push_back(
3656 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
3657 }
3658 }
3659 break;
3660 case ISD::ConstantFP: {
3661 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
3662 // Check to see if this FP immediate is already legal.
3663 // If this is a legal constant, turn it into a TargetConstantFP node.
3664 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
3665 DAG.shouldOptForSize()))
3666 Results.push_back(ExpandConstantFP(CFP, true));
3667 break;
3668 }
3669 case ISD::Constant: {
3670 ConstantSDNode *CP = cast<ConstantSDNode>(Node);
3671 Results.push_back(ExpandConstant(CP));
3672 break;
3673 }
3674 case ISD::FSUB: {
3675 EVT VT = Node->getValueType(0);
3676 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3677 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
3678 const SDNodeFlags Flags = Node->getFlags();
3679 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
3680 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
3681 Results.push_back(Tmp1);
3682 }
3683 break;
3684 }
3685 case ISD::SUB: {
3686 EVT VT = Node->getValueType(0);
3687 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3688 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3689 "Don't know how to expand this subtraction!");
3690 Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
3691 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
3692 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3693 break;
3694 }
3695 case ISD::UREM:
3696 case ISD::SREM:
3697 if (TLI.expandREM(Node, Tmp1, DAG))
3698 Results.push_back(Tmp1);
3699 break;
3700 case ISD::UDIV:
3701 case ISD::SDIV: {
3702 bool isSigned = Node->getOpcode() == ISD::SDIV;
3703 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3704 EVT VT = Node->getValueType(0);
3705 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3706 SDVTList VTs = DAG.getVTList(VT, VT);
3707 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3708 Node->getOperand(1));
3709 Results.push_back(Tmp1);
3710 }
3711 break;
3712 }
3713 case ISD::MULHU:
3714 case ISD::MULHS: {
3715 unsigned ExpandOpcode =
3716 Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
3717 EVT VT = Node->getValueType(0);
3718 SDVTList VTs = DAG.getVTList(VT, VT);
3719
3720 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3721 Node->getOperand(1));
3722 Results.push_back(Tmp1.getValue(1));
3723 break;
3724 }
3725 case ISD::UMUL_LOHI:
3726 case ISD::SMUL_LOHI: {
3727 SDValue LHS = Node->getOperand(0);
3728 SDValue RHS = Node->getOperand(1);
3729 MVT VT = LHS.getSimpleValueType();
3730 unsigned MULHOpcode =
3731 Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
3732
3733 if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
3734 Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
3735 Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
3736 break;
3737 }
3738
3740 EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext());
3741 assert(TLI.isTypeLegal(HalfType));
3742 if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
3743 HalfType, DAG,
3744 TargetLowering::MulExpansionKind::Always)) {
3745 for (unsigned i = 0; i < 2; ++i) {
3746 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
3747 SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
3748 SDValue Shift = DAG.getConstant(
3749 HalfType.getScalarSizeInBits(), dl,
3750 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
3751 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3752 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3753 }
3754 break;
3755 }
3756 break;
3757 }
3758 case ISD::MUL: {
3759 EVT VT = Node->getValueType(0);
3760 SDVTList VTs = DAG.getVTList(VT, VT);
3761 // See if multiply or divide can be lowered using two-result operations.
3762 // We just need the low half of the multiply; try both the signed
3763 // and unsigned forms. If the target supports both SMUL_LOHI and
3764 // UMUL_LOHI, form a preference by checking which forms of plain
3765 // MULH it supports.
3766 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3767 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3768 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3769 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3770 unsigned OpToUse = 0;
3771 if (HasSMUL_LOHI && !HasMULHS) {
3772 OpToUse = ISD::SMUL_LOHI;
3773 } else if (HasUMUL_LOHI && !HasMULHU) {
3774 OpToUse = ISD::UMUL_LOHI;
3775 } else if (HasSMUL_LOHI) {
3776 OpToUse = ISD::SMUL_LOHI;
3777 } else if (HasUMUL_LOHI) {
3778 OpToUse = ISD::UMUL_LOHI;
3779 }
3780 if (OpToUse) {
3781 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3782 Node->getOperand(1)));
3783 break;
3784 }
3785
3786 SDValue Lo, Hi;
3787 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3788 if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
3789 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
3790 TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
3791 TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
3792 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
3793 TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
3794 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3795 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
3796 SDValue Shift =
3797 DAG.getConstant(HalfType.getSizeInBits(), dl,
3798 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
3799 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3800 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3801 }
3802 break;
3803 }
3804 case ISD::FSHL:
3805 case ISD::FSHR:
3806 if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
3807 Results.push_back(Expanded);
3808 break;
3809 case ISD::ROTL:
3810 case ISD::ROTR:
3811 if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
3812 Results.push_back(Expanded);
3813 break;
3814 case ISD::SADDSAT:
3815 case ISD::UADDSAT:
3816 case ISD::SSUBSAT:
3817 case ISD::USUBSAT:
3818 Results.push_back(TLI.expandAddSubSat(Node, DAG));
3819 break;
3820 case ISD::SCMP:
3821 case ISD::UCMP:
3822 Results.push_back(TLI.expandCMP(Node, DAG));
3823 break;
3824 case ISD::SSHLSAT:
3825 case ISD::USHLSAT:
3826 Results.push_back(TLI.expandShlSat(Node, DAG));
3827 break;
3828 case ISD::SMULFIX:
3829 case ISD::SMULFIXSAT:
3830 case ISD::UMULFIX:
3831 case ISD::UMULFIXSAT:
3832 Results.push_back(TLI.expandFixedPointMul(Node, DAG));
3833 break;
3834 case ISD::SDIVFIX:
3835 case ISD::SDIVFIXSAT:
3836 case ISD::UDIVFIX:
3837 case ISD::UDIVFIXSAT:
3838 if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
3839 Node->getOperand(0),
3840 Node->getOperand(1),
3841 Node->getConstantOperandVal(2),
3842 DAG)) {
3843 Results.push_back(V);
3844 break;
3845 }
3846 // FIXME: We might want to retry here with a wider type if we fail, if that
3847 // type is legal.
3848 // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
3849 // <= 128 (which is the case for all of the default Embedded-C types),
3850 // we will only get here with types and scales that we could always expand
3851 // if we were allowed to generate libcalls to division functions of illegal
3852 // type. But we cannot do that.
3853 llvm_unreachable("Cannot expand DIVFIX!");
3854 case ISD::UADDO_CARRY:
3855 case ISD::USUBO_CARRY: {
3856 SDValue LHS = Node->getOperand(0);
3857 SDValue RHS = Node->getOperand(1);
3858 SDValue Carry = Node->getOperand(2);
3859
3860 bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
3861
3862 // Initial add of the 2 operands.
3863 unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
3864 EVT VT = LHS.getValueType();
3865 SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
3866
3867 // Initial check for overflow.
3868 EVT CarryType = Node->getValueType(1);
3869 EVT SetCCType = getSetCCResultType(Node->getValueType(0));
3871 SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
3872
3873 // Add of the sum and the carry.
3874 SDValue One = DAG.getConstant(1, dl, VT);
3875 SDValue CarryExt =
3876 DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
3877 SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
3878
3879 // Second check for overflow. If we are adding, we can only overflow if the
3880 // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
3881 // If we are subtracting, we can only overflow if the initial sum is 0 and
3882 // the carry is set, resulting in a new sum of all 1s.
3883 SDValue Zero = DAG.getConstant(0, dl, VT);
3884 SDValue Overflow2 =
3885 IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
3886 : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
3887 Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
3888 DAG.getZExtOrTrunc(Carry, dl, SetCCType));
3889
3890 SDValue ResultCarry =
3891 DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
3892
3893 Results.push_back(Sum2);
3894 Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
3895 break;
3896 }
3897 case ISD::SADDO:
3898 case ISD::SSUBO: {
3899 SDValue Result, Overflow;
3900 TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
3901 Results.push_back(Result);
3902 Results.push_back(Overflow);
3903 break;
3904 }
3905 case ISD::UADDO:
3906 case ISD::USUBO: {
3907 SDValue Result, Overflow;
3908 TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
3909 Results.push_back(Result);
3910 Results.push_back(Overflow);
3911 break;
3912 }
3913 case ISD::UMULO:
3914 case ISD::SMULO: {
3915 SDValue Result, Overflow;
3916 if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
3917 Results.push_back(Result);
3918 Results.push_back(Overflow);
3919 }
3920 break;
3921 }
3922 case ISD::BUILD_PAIR: {
3923 EVT PairTy = Node->getValueType(0);
3924 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
3925 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
3926 Tmp2 = DAG.getNode(
3927 ISD::SHL, dl, PairTy, Tmp2,
3928 DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
3929 TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
3930 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
3931 break;
3932 }
3933 case ISD::SELECT:
3934 Tmp1 = Node->getOperand(0);
3935 Tmp2 = Node->getOperand(1);
3936 Tmp3 = Node->getOperand(2);
3937 if (Tmp1.getOpcode() == ISD::SETCC) {
3938 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
3939 Tmp2, Tmp3,
3940 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
3941 } else {
3942 Tmp1 = DAG.getSelectCC(dl, Tmp1,
3943 DAG.getConstant(0, dl, Tmp1.getValueType()),
3944 Tmp2, Tmp3, ISD::SETNE);
3945 }
3946 Tmp1->setFlags(Node->getFlags());
3947 Results.push_back(Tmp1);
3948 break;
3949 case ISD::BR_JT: {
3950 SDValue Chain = Node->getOperand(0);
3951 SDValue Table = Node->getOperand(1);
3952 SDValue Index = Node->getOperand(2);
3953 int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex();
3954
3955 const DataLayout &TD = DAG.getDataLayout();
3956 EVT PTy = TLI.getPointerTy(TD);
3957
3958 unsigned EntrySize =
3959 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
3960
3961 // For power-of-two jumptable entry sizes convert multiplication to a shift.
3962 // This transformation needs to be done here since otherwise the MIPS
3963 // backend will end up emitting a three instruction multiply sequence
3964 // instead of a single shift and MSP430 will call a runtime function.
3965 if (llvm::isPowerOf2_32(EntrySize))
3966 Index = DAG.getNode(
3967 ISD::SHL, dl, Index.getValueType(), Index,
3968 DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
3969 else
3970 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
3971 DAG.getConstant(EntrySize, dl, Index.getValueType()));
3972 SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
3973 Index, Table);
3974
3975 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
3976 SDValue LD = DAG.getExtLoad(
3977 ISD::SEXTLOAD, dl, PTy, Chain, Addr,
3978 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
3979 Addr = LD;
3980 if (TLI.isJumpTableRelative()) {
3981 // For PIC, the sequence is:
3982 // BRIND(load(Jumptable + index) + RelocBase)
3983 // RelocBase can be JumpTable, GOT or some sort of global base.
3984 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
3985 TLI.getPICJumpTableRelocBase(Table, DAG));
3986 }
3987
3988 Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG);
3989 Results.push_back(Tmp1);
3990 break;
3991 }
3992 case ISD::BRCOND:
3993 // Expand brcond's setcc into its constituent parts and create a BR_CC
3994 // Node.
3995 Tmp1 = Node->getOperand(0);
3996 Tmp2 = Node->getOperand(1);
3997 if (Tmp2.getOpcode() == ISD::SETCC &&
3998 TLI.isOperationLegalOrCustom(ISD::BR_CC,
3999 Tmp2.getOperand(0).getValueType())) {
4000 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
4001 Tmp2.getOperand(0), Tmp2.getOperand(1),
4002 Node->getOperand(2));
4003 } else {
4004 // We test only the i1 bit. Skip the AND if UNDEF or another AND.
4005 if (Tmp2.isUndef() ||
4006 (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
4007 Tmp3 = Tmp2;
4008 else
4009 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
4010 DAG.getConstant(1, dl, Tmp2.getValueType()));
4011 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
4012 DAG.getCondCode(ISD::SETNE), Tmp3,
4013 DAG.getConstant(0, dl, Tmp3.getValueType()),
4014 Node->getOperand(2));
4015 }
4016 Results.push_back(Tmp1);
4017 break;
4018 case ISD::SETCC:
4019 case ISD::VP_SETCC:
4020 case ISD::STRICT_FSETCC:
4021 case ISD::STRICT_FSETCCS: {
4022 bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
4023 bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
4024 Node->getOpcode() == ISD::STRICT_FSETCCS;
4025 bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
4026 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4027 unsigned Offset = IsStrict ? 1 : 0;
4028 Tmp1 = Node->getOperand(0 + Offset);
4029 Tmp2 = Node->getOperand(1 + Offset);
4030 Tmp3 = Node->getOperand(2 + Offset);
4031 SDValue Mask, EVL;
4032 if (IsVP) {
4033 Mask = Node->getOperand(3 + Offset);
4034 EVL = Node->getOperand(4 + Offset);
4035 }
4036 bool Legalized = TLI.LegalizeSetCCCondCode(
4037 DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
4038 Chain, IsSignaling);
4039
4040 if (Legalized) {
4041 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4042 // condition code, create a new SETCC node.
4043 if (Tmp3.getNode()) {
4044 if (IsStrict) {
4045 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
4046 {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
4047 Chain = Tmp1.getValue(1);
4048 } else if (IsVP) {
4049 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
4050 {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
4051 } else {
4052 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
4053 Tmp2, Tmp3, Node->getFlags());
4054 }
4055 }
4056
4057 // If we expanded the SETCC by inverting the condition code, then wrap
4058 // the existing SETCC in a NOT to restore the intended condition.
4059 if (NeedInvert) {
4060 if (!IsVP)
4061 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
4062 else
4063 Tmp1 =
4064 DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
4065 }
4066
4067 Results.push_back(Tmp1);
4068 if (IsStrict)
4069 Results.push_back(Chain);
4070
4071 break;
4072 }
4073
4074 // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4075 // understand if this code is useful for strict nodes.
4076 assert(!IsStrict && "Don't know how to expand for strict nodes.");
4077
4078 // Otherwise, SETCC for the given comparison type must be completely
4079 // illegal; expand it into a SELECT_CC.
4080 // FIXME: This drops the mask/evl for VP_SETCC.
4081 EVT VT = Node->getValueType(0);
4082 EVT Tmp1VT = Tmp1.getValueType();
4083 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
4084 DAG.getBoolConstant(true, dl, VT, Tmp1VT),
4085 DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3);
4086 Tmp1->setFlags(Node->getFlags());
4087 Results.push_back(Tmp1);
4088 break;
4089 }
4090 case ISD::SELECT_CC: {
4091 // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
4092 Tmp1 = Node->getOperand(0); // LHS
4093 Tmp2 = Node->getOperand(1); // RHS
4094 Tmp3 = Node->getOperand(2); // True
4095 Tmp4 = Node->getOperand(3); // False
4096 EVT VT = Node->getValueType(0);
4097 SDValue Chain;
4098 SDValue CC = Node->getOperand(4);
4099 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
4100
4101 if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
4102 // If the condition code is legal, then we need to expand this
4103 // node using SETCC and SELECT.
4104 EVT CmpVT = Tmp1.getValueType();
4105 assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
4106 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
4107 "expanded.");
4108 EVT CCVT = getSetCCResultType(CmpVT);
4109 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
4110 Results.push_back(
4111 DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4, Node->getFlags()));
4112 break;
4113 }
4114
4115 // SELECT_CC is legal, so the condition code must not be.
4116 bool Legalized = false;
4117 // Try to legalize by inverting the condition. This is for targets that
4118 // might support an ordered version of a condition, but not the unordered
4119 // version (or vice versa).
4120 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
4121 if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
4122 // Use the new condition code and swap true and false
4123 Legalized = true;
4124 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
4125 Tmp1->setFlags(Node->getFlags());
4126 } else {
4127 // If The inverse is not legal, then try to swap the arguments using
4128 // the inverse condition code.
4130 if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
4131 // The swapped inverse condition is legal, so swap true and false,
4132 // lhs and rhs.
4133 Legalized = true;
4134 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
4135 Tmp1->setFlags(Node->getFlags());
4136 }
4137 }
4138
4139 if (!Legalized) {
4140 Legalized = TLI.LegalizeSetCCCondCode(
4141 DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
4142 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4143
4144 assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
4145
4146 // If we expanded the SETCC by inverting the condition code, then swap
4147 // the True/False operands to match.
4148 if (NeedInvert)
4149 std::swap(Tmp3, Tmp4);
4150
4151 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4152 // condition code, create a new SELECT_CC node.
4153 if (CC.getNode()) {
4154 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
4155 Tmp1, Tmp2, Tmp3, Tmp4, CC);
4156 } else {
4157 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
4158 CC = DAG.getCondCode(ISD::SETNE);
4159 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
4160 Tmp2, Tmp3, Tmp4, CC);
4161 }
4162 Tmp1->setFlags(Node->getFlags());
4163 }
4164 Results.push_back(Tmp1);
4165 break;
4166 }
4167 case ISD::BR_CC: {
4168 // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4169 SDValue Chain;
4170 Tmp1 = Node->getOperand(0); // Chain
4171 Tmp2 = Node->getOperand(2); // LHS
4172 Tmp3 = Node->getOperand(3); // RHS
4173 Tmp4 = Node->getOperand(1); // CC
4174
4175 bool Legalized = TLI.LegalizeSetCCCondCode(
4176 DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
4177 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4178 (void)Legalized;
4179 assert(Legalized && "Can't legalize BR_CC with legal condition!");
4180
4181 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
4182 // node.
4183 if (Tmp4.getNode()) {
4184 assert(!NeedInvert && "Don't know how to invert BR_CC!");
4185
4186 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
4187 Tmp4, Tmp2, Tmp3, Node->getOperand(4));
4188 } else {
4189 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4190 Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
4191 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
4192 Tmp2, Tmp3, Node->getOperand(4));
4193 }
4194 Results.push_back(Tmp1);
4195 break;
4196 }
4197 case ISD::BUILD_VECTOR:
4198 Results.push_back(ExpandBUILD_VECTOR(Node));
4199 break;
4200 case ISD::SPLAT_VECTOR:
4201 Results.push_back(ExpandSPLAT_VECTOR(Node));
4202 break;
4203 case ISD::SRA:
4204 case ISD::SRL:
4205 case ISD::SHL: {
4206 // Scalarize vector SRA/SRL/SHL.
4207 EVT VT = Node->getValueType(0);
4208 assert(VT.isVector() && "Unable to legalize non-vector shift");
4209 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
4210 unsigned NumElem = VT.getVectorNumElements();
4211
4213 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
4214 SDValue Ex =
4216 Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
4217 SDValue Sh =
4219 Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
4220 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
4221 VT.getScalarType(), Ex, Sh));
4222 }
4223
4224 SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4225 Results.push_back(Result);
4226 break;
4227 }
4230 case ISD::VECREDUCE_ADD:
4231 case ISD::VECREDUCE_MUL:
4232 case ISD::VECREDUCE_AND:
4233 case ISD::VECREDUCE_OR:
4234 case ISD::VECREDUCE_XOR:
4243 Results.push_back(TLI.expandVecReduce(Node, DAG));
4244 break;
4245 case ISD::VP_CTTZ_ELTS:
4246 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
4247 Results.push_back(TLI.expandVPCTTZElements(Node, DAG));
4248 break;
4249 case ISD::CLEAR_CACHE:
4250 // The default expansion of llvm.clear_cache is simply a no-op for those
4251 // targets where it is not needed.
4252 Results.push_back(Node->getOperand(0));
4253 break;
4254 case ISD::LRINT:
4255 case ISD::LLRINT: {
4256 SDValue Arg = Node->getOperand(0);
4257 EVT ArgVT = Arg.getValueType();
4258 EVT ResVT = Node->getValueType(0);
4259 SDLoc dl(Node);
4260 SDValue RoundNode = DAG.getNode(ISD::FRINT, dl, ArgVT, Arg);
4261 Results.push_back(DAG.getNode(ISD::FP_TO_SINT, dl, ResVT, RoundNode));
4262 break;
4263 }
4264 case ISD::ADDRSPACECAST:
4265 Results.push_back(DAG.UnrollVectorOp(Node));
4266 break;
4268 case ISD::GlobalAddress:
4271 case ISD::ConstantPool:
4272 case ISD::JumpTable:
4276 // FIXME: Custom lowering for these operations shouldn't return null!
4277 // Return true so that we don't call ConvertNodeToLibcall which also won't
4278 // do anything.
4279 return true;
4280 }
4281
4282 if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
4283 // FIXME: We were asked to expand a strict floating-point operation,
4284 // but there is currently no expansion implemented that would preserve
4285 // the "strict" properties. For now, we just fall back to the non-strict
4286 // version if that is legal on the target. The actual mutation of the
4287 // operation will happen in SelectionDAGISel::DoInstructionSelection.
4288 switch (Node->getOpcode()) {
4289 default:
4290 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4291 Node->getValueType(0))
4292 == TargetLowering::Legal)
4293 return true;
4294 break;
4295 case ISD::STRICT_FSUB: {
4296 if (TLI.getStrictFPOperationAction(
4297 ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4298 return true;
4299 if (TLI.getStrictFPOperationAction(
4300 ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4301 break;
4302
4303 EVT VT = Node->getValueType(0);
4304 const SDNodeFlags Flags = Node->getFlags();
4305 SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4306 SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4307 {Node->getOperand(0), Node->getOperand(1), Neg},
4308 Flags);
4309
4310 Results.push_back(Fadd);
4311 Results.push_back(Fadd.getValue(1));
4312 break;
4313 }
4316 case ISD::STRICT_LRINT:
4317 case ISD::STRICT_LLRINT:
4318 case ISD::STRICT_LROUND:
4320 // These are registered by the operand type instead of the value
4321 // type. Reflect that here.
4322 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4323 Node->getOperand(1).getValueType())
4324 == TargetLowering::Legal)
4325 return true;
4326 break;
4327 }
4328 }
4329
4330 // Replace the original node with the legalized result.
4331 if (Results.empty()) {
4332 LLVM_DEBUG(dbgs() << "Cannot expand node\n");
4333 return false;
4334 }
4335
4336 LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
4337 ReplaceNode(Node, Results.data());
4338 return true;
4339}
4340
4341void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
4342 LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
4344 SDLoc dl(Node);
4345 // FIXME: Check flags on the node to see if we can use a finite call.
4346 unsigned Opc = Node->getOpcode();
4347 switch (Opc) {
4348 case ISD::ATOMIC_FENCE: {
4349 // If the target didn't lower this, lower it to '__sync_synchronize()' call
4350 // FIXME: handle "fence singlethread" more efficiently.
4352
4354 CLI.setDebugLoc(dl)
4355 .setChain(Node->getOperand(0))
4356 .setLibCallee(
4357 CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4358 DAG.getExternalSymbol("__sync_synchronize",
4359 TLI.getPointerTy(DAG.getDataLayout())),
4360 std::move(Args));
4361
4362 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4363
4364 Results.push_back(CallResult.second);
4365 break;
4366 }
4367 // By default, atomic intrinsics are marked Legal and lowered. Targets
4368 // which don't support them directly, however, may want libcalls, in which
4369 // case they mark them Expand, and we get here.
4370 case ISD::ATOMIC_SWAP:
4382 case ISD::ATOMIC_CMP_SWAP: {
4383 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4384 AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
4385 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
4386 EVT RetVT = Node->getValueType(0);
4389 if (TLI.getLibcallName(LC)) {
4390 // If outline atomic available, prepare its arguments and expand.
4391 Ops.append(Node->op_begin() + 2, Node->op_end());
4392 Ops.push_back(Node->getOperand(1));
4393
4394 } else {
4395 LC = RTLIB::getSYNC(Opc, VT);
4396 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4397 "Unexpected atomic op or value type!");
4398 // Arguments for expansion to sync libcall
4399 Ops.append(Node->op_begin() + 1, Node->op_end());
4400 }
4401 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4402 Ops, CallOptions,
4403 SDLoc(Node),
4404 Node->getOperand(0));
4405 Results.push_back(Tmp.first);
4406 Results.push_back(Tmp.second);
4407 break;
4408 }
4409 case ISD::TRAP: {
4410 // If this operation is not supported, lower it to 'abort()' call
4413 CLI.setDebugLoc(dl)
4414 .setChain(Node->getOperand(0))
4415 .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4416 DAG.getExternalSymbol(
4417 "abort", TLI.getPointerTy(DAG.getDataLayout())),
4418 std::move(Args));
4419 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4420
4421 Results.push_back(CallResult.second);
4422 break;
4423 }
4424 case ISD::CLEAR_CACHE: {
4426 SDValue InputChain = Node->getOperand(0);
4427 SDValue StartVal = Node->getOperand(1);
4428 SDValue EndVal = Node->getOperand(2);
4429 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4430 DAG, RTLIB::CLEAR_CACHE, MVT::isVoid, {StartVal, EndVal}, CallOptions,
4431 SDLoc(Node), InputChain);
4432 Results.push_back(Tmp.second);
4433 break;
4434 }
4435 case ISD::FMINNUM:
4437 ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
4438 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4439 RTLIB::FMIN_PPCF128, Results);
4440 break;
4441 // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
4442 // libcall legalization for these nodes, but there is no default expasion for
4443 // these nodes either (see PR63267 for example).
4444 case ISD::FMAXNUM:
4446 ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
4447 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4448 RTLIB::FMAX_PPCF128, Results);
4449 break;
4450 case ISD::FMINIMUMNUM:
4451 ExpandFPLibCall(Node, RTLIB::FMINIMUMNUM_F32, RTLIB::FMINIMUMNUM_F64,
4452 RTLIB::FMINIMUMNUM_F80, RTLIB::FMINIMUMNUM_F128,
4453 RTLIB::FMINIMUMNUM_PPCF128, Results);
4454 break;
4455 case ISD::FMAXIMUMNUM:
4456 ExpandFPLibCall(Node, RTLIB::FMAXIMUMNUM_F32, RTLIB::FMAXIMUMNUM_F64,
4457 RTLIB::FMAXIMUMNUM_F80, RTLIB::FMAXIMUMNUM_F128,
4458 RTLIB::FMAXIMUMNUM_PPCF128, Results);
4459 break;
4460 case ISD::FSQRT:
4461 case ISD::STRICT_FSQRT:
4462 ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
4463 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
4464 RTLIB::SQRT_PPCF128, Results);
4465 break;
4466 case ISD::FCBRT:
4467 ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
4468 RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4469 RTLIB::CBRT_PPCF128, Results);
4470 break;
4471 case ISD::FSIN:
4472 case ISD::STRICT_FSIN:
4473 ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
4474 RTLIB::SIN_F80, RTLIB::SIN_F128,
4475 RTLIB::SIN_PPCF128, Results);
4476 break;
4477 case ISD::FCOS:
4478 case ISD::STRICT_FCOS:
4479 ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
4480 RTLIB::COS_F80, RTLIB::COS_F128,
4481 RTLIB::COS_PPCF128, Results);
4482 break;
4483 case ISD::FTAN:
4484 case ISD::STRICT_FTAN:
4485 ExpandFPLibCall(Node, RTLIB::TAN_F32, RTLIB::TAN_F64, RTLIB::TAN_F80,
4486 RTLIB::TAN_F128, RTLIB::TAN_PPCF128, Results);
4487 break;
4488 case ISD::FASIN:
4489 case ISD::STRICT_FASIN:
4490 ExpandFPLibCall(Node, RTLIB::ASIN_F32, RTLIB::ASIN_F64, RTLIB::ASIN_F80,
4491 RTLIB::ASIN_F128, RTLIB::ASIN_PPCF128, Results);
4492 break;
4493 case ISD::FACOS:
4494 case ISD::STRICT_FACOS:
4495 ExpandFPLibCall(Node, RTLIB::ACOS_F32, RTLIB::ACOS_F64, RTLIB::ACOS_F80,
4496 RTLIB::ACOS_F128, RTLIB::ACOS_PPCF128, Results);
4497 break;
4498 case ISD::FATAN:
4499 case ISD::STRICT_FATAN:
4500 ExpandFPLibCall(Node, RTLIB::ATAN_F32, RTLIB::ATAN_F64, RTLIB::ATAN_F80,
4501 RTLIB::ATAN_F128, RTLIB::ATAN_PPCF128, Results);
4502 break;
4503 case ISD::FATAN2:
4504 case ISD::STRICT_FATAN2:
4505 ExpandFPLibCall(Node, RTLIB::ATAN2_F32, RTLIB::ATAN2_F64, RTLIB::ATAN2_F80,
4506 RTLIB::ATAN2_F128, RTLIB::ATAN2_PPCF128, Results);
4507 break;
4508 case ISD::FSINH:
4509 case ISD::STRICT_FSINH:
4510 ExpandFPLibCall(Node, RTLIB::SINH_F32, RTLIB::SINH_F64, RTLIB::SINH_F80,
4511 RTLIB::SINH_F128, RTLIB::SINH_PPCF128, Results);
4512 break;
4513 case ISD::FCOSH:
4514 case ISD::STRICT_FCOSH:
4515 ExpandFPLibCall(Node, RTLIB::COSH_F32, RTLIB::COSH_F64, RTLIB::COSH_F80,
4516 RTLIB::COSH_F128, RTLIB::COSH_PPCF128, Results);
4517 break;
4518 case ISD::FTANH:
4519 case ISD::STRICT_FTANH:
4520 ExpandFPLibCall(Node, RTLIB::TANH_F32, RTLIB::TANH_F64, RTLIB::TANH_F80,
4521 RTLIB::TANH_F128, RTLIB::TANH_PPCF128, Results);
4522 break;
4523 case ISD::FSINCOS: {
4524 RTLIB::Libcall LC = RTLIB::getFSINCOS(Node->getValueType(0));
4525 bool Expanded = DAG.expandMultipleResultFPLibCall(LC, Node, Results);
4526 if (!Expanded)
4527 llvm_unreachable("Expected scalar FSINCOS to expand to libcall!");
4528 break;
4529 }
4530 case ISD::FLOG:
4531 case ISD::STRICT_FLOG:
4532 ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
4533 RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
4534 break;
4535 case ISD::FLOG2:
4536 case ISD::STRICT_FLOG2:
4537 ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
4538 RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
4539 break;
4540 case ISD::FLOG10:
4541 case ISD::STRICT_FLOG10:
4542 ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
4543 RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
4544 break;
4545 case ISD::FEXP:
4546 case ISD::STRICT_FEXP:
4547 ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
4548 RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
4549 break;
4550 case ISD::FEXP2:
4551 case ISD::STRICT_FEXP2:
4552 ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
4553 RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
4554 break;
4555 case ISD::FEXP10:
4556 ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80,
4557 RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results);
4558 break;
4559 case ISD::FTRUNC:
4560 case ISD::STRICT_FTRUNC:
4561 ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
4562 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4563 RTLIB::TRUNC_PPCF128, Results);
4564 break;
4565 case ISD::FFLOOR:
4566 case ISD::STRICT_FFLOOR:
4567 ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
4568 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4569 RTLIB::FLOOR_PPCF128, Results);
4570 break;
4571 case ISD::FCEIL:
4572 case ISD::STRICT_FCEIL:
4573 ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
4574 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4575 RTLIB::CEIL_PPCF128, Results);
4576 break;
4577 case ISD::FRINT:
4578 case ISD::STRICT_FRINT:
4579 ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
4580 RTLIB::RINT_F80, RTLIB::RINT_F128,
4581 RTLIB::RINT_PPCF128, Results);
4582 break;
4583 case ISD::FNEARBYINT:
4585 ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
4586 RTLIB::NEARBYINT_F64,
4587 RTLIB::NEARBYINT_F80,
4588 RTLIB::NEARBYINT_F128,
4589 RTLIB::NEARBYINT_PPCF128, Results);
4590 break;
4591 case ISD::FROUND:
4592 case ISD::STRICT_FROUND:
4593 ExpandFPLibCall(Node, RTLIB::ROUND_F32,
4594 RTLIB::ROUND_F64,
4595 RTLIB::ROUND_F80,
4596 RTLIB::ROUND_F128,
4597 RTLIB::ROUND_PPCF128, Results);
4598 break;
4599 case ISD::FROUNDEVEN:
4601 ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
4602 RTLIB::ROUNDEVEN_F64,
4603 RTLIB::ROUNDEVEN_F80,
4604 RTLIB::ROUNDEVEN_F128,
4605 RTLIB::ROUNDEVEN_PPCF128, Results);
4606 break;
4607 case ISD::FLDEXP:
4608 case ISD::STRICT_FLDEXP:
4609 ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
4610 RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
4611 break;
4612 case ISD::FFREXP: {
4613 RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
4614 bool Expanded = DAG.expandMultipleResultFPLibCall(LC, Node, Results,
4615 /*CallRetResNo=*/0);
4616 if (!Expanded)
4617 llvm_unreachable("Expected scalar FFREXP to expand to libcall!");
4618 break;
4619 }
4620 case ISD::FPOWI:
4621 case ISD::STRICT_FPOWI: {
4622 RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
4623 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
4624 if (!TLI.getLibcallName(LC)) {
4625 // Some targets don't have a powi libcall; use pow instead.
4626 if (Node->isStrictFPOpcode()) {
4628 DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
4629 {Node->getValueType(0), Node->getValueType(1)},
4630 {