LLVM 23.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"
21#include "llvm/ADT/StringRef.h"
37#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/DataLayout.h"
41#include "llvm/IR/Function.h"
42#include "llvm/IR/Metadata.h"
43#include "llvm/IR/Type.h"
46#include "llvm/Support/Debug.h"
52#include <cassert>
53#include <cstdint>
54#include <tuple>
55#include <utility>
56
57using namespace llvm;
58
59#define DEBUG_TYPE "legalizedag"
60
61namespace {
62
63/// Keeps track of state when getting the sign of a floating-point value as an
64/// integer.
65struct FloatSignAsInt {
66 EVT FloatVT;
67 SDValue Chain;
68 SDValue FloatPtr;
69 SDValue IntPtr;
70 MachinePointerInfo IntPointerInfo;
71 MachinePointerInfo FloatPointerInfo;
72 SDValue IntValue;
73 APInt SignMask;
74 uint8_t SignBit;
75};
76
77//===----------------------------------------------------------------------===//
78/// This takes an arbitrary SelectionDAG as input and
79/// hacks on it until the target machine can handle it. This involves
80/// eliminating value sizes the machine cannot handle (promoting small sizes to
81/// large sizes or splitting up large values into small values) as well as
82/// eliminating operations the machine cannot handle.
83///
84/// This code also does a small amount of optimization and recognition of idioms
85/// as part of its processing. For example, if a target does not support a
86/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
87/// will attempt merge setcc and brc instructions into brcc's.
88class SelectionDAGLegalize {
89 const TargetMachine &TM;
90 const TargetLowering &TLI;
91 SelectionDAG &DAG;
92
93 /// The set of nodes which have already been legalized. We hold a
94 /// reference to it in order to update as necessary on node deletion.
95 SmallPtrSetImpl<SDNode *> &LegalizedNodes;
96
97 /// A set of all the nodes updated during legalization.
98 SmallSetVector<SDNode *, 16> *UpdatedNodes;
99
100 EVT getSetCCResultType(EVT VT) const {
101 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
102 }
103
104 // Libcall insertion helpers.
105
106public:
107 SelectionDAGLegalize(SelectionDAG &DAG,
108 SmallPtrSetImpl<SDNode *> &LegalizedNodes,
109 SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
110 : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
111 LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
112
113 /// Legalizes the given operation.
114 void LegalizeOp(SDNode *Node);
115
116private:
117 SDValue OptimizeFloatStore(StoreSDNode *ST);
118
119 void LegalizeLoadOps(SDNode *Node);
120 void LegalizeStoreOps(SDNode *Node);
121
122 SDValue ExpandINSERT_VECTOR_ELT(SDValue Op);
123
124 /// Return a vector shuffle operation which
125 /// performs the same shuffe in terms of order or result bytes, but on a type
126 /// whose vector element type is narrower than the original shuffle type.
127 /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
128 SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
129 SDValue N1, SDValue N2,
130 ArrayRef<int> Mask) const;
131
132 std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
134 bool IsSigned, EVT RetVT);
135 std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
136
137 void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC,
139 void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
140 RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
141 RTLIB::Libcall Call_F128,
142 RTLIB::Libcall Call_PPCF128,
144
145 void
146 ExpandFastFPLibCall(SDNode *Node, bool IsFast,
147 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F32,
148 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F64,
149 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F80,
150 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F128,
151 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_PPCF128,
153
154 SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, RTLIB::Libcall Call_I8,
155 RTLIB::Libcall Call_I16, RTLIB::Libcall Call_I32,
156 RTLIB::Libcall Call_I64, RTLIB::Libcall Call_I128);
157 void ExpandArgFPLibCall(SDNode *Node,
158 RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
159 RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
160 RTLIB::Libcall Call_PPCF128,
162 SDValue ExpandBitCountingLibCall(SDNode *Node, RTLIB::Libcall CallI32,
163 RTLIB::Libcall CallI64,
164 RTLIB::Libcall CallI128);
165 void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
166
167 SDValue ExpandSincosStretLibCall(SDNode *Node) const;
168
169 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
170 const SDLoc &dl);
171 SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
172 const SDLoc &dl, SDValue ChainIn);
173 SDValue ExpandBUILD_VECTOR(SDNode *Node);
174 SDValue ExpandSPLAT_VECTOR(SDNode *Node);
175 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
176 void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
178 void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
179 SDValue Value) const;
180 SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
181 SDValue NewIntValue) const;
182 SDValue ExpandFCOPYSIGN(SDNode *Node) const;
183 SDValue ExpandFABS(SDNode *Node) const;
184 SDValue ExpandFNEG(SDNode *Node) const;
185 SDValue expandLdexp(SDNode *Node) const;
186 SDValue expandFrexp(SDNode *Node) const;
187 SDValue expandModf(SDNode *Node) const;
188
189 SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain);
190 void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl,
192 void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
194 SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl);
195
196 /// Implements vector reduce operation promotion.
197 ///
198 /// All vector operands are promoted to a vector type with larger element
199 /// type, and the start value is promoted to a larger scalar type. Then the
200 /// result is truncated back to the original scalar type.
201 SDValue PromoteReduction(SDNode *Node);
202
203 SDValue ExpandPARITY(SDValue Op, const SDLoc &dl);
204
205 SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
206 SDValue ExpandInsertToVectorThroughStack(SDValue Op);
207 SDValue ExpandVectorBuildThroughStack(SDNode* Node);
208 SDValue ExpandConcatVectors(SDNode *Node);
209
210 SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
211 SDValue ExpandConstant(ConstantSDNode *CP);
212
213 // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
214 bool ExpandNode(SDNode *Node);
215 void ConvertNodeToLibcall(SDNode *Node);
216 void PromoteNode(SDNode *Node);
217
218public:
219 // Node replacement helpers
220
221 void ReplacedNode(SDNode *N) {
222 LegalizedNodes.erase(N);
223 if (UpdatedNodes)
224 UpdatedNodes->insert(N);
225 }
226
227 void ReplaceNode(SDNode *Old, SDNode *New) {
228 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
229 dbgs() << " with: "; New->dump(&DAG));
230
231 assert(Old->getNumValues() == New->getNumValues() &&
232 "Replacing one node with another that produces a different number "
233 "of values!");
234 DAG.ReplaceAllUsesWith(Old, New);
235 if (UpdatedNodes)
236 UpdatedNodes->insert(New);
237 ReplacedNode(Old);
238 }
239
240 void ReplaceNode(SDValue Old, SDValue New) {
241 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
242 dbgs() << " with: "; New->dump(&DAG));
243
244 DAG.ReplaceAllUsesWith(Old, New);
245 if (UpdatedNodes)
246 UpdatedNodes->insert(New.getNode());
247 ReplacedNode(Old.getNode());
248 }
249
250 void ReplaceNode(SDNode *Old, const SDValue *New) {
251 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
252
253 DAG.ReplaceAllUsesWith(Old, New);
254 for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
255 LLVM_DEBUG(dbgs() << (i == 0 ? " with: " : " and: ");
256 New[i]->dump(&DAG));
257 if (UpdatedNodes)
258 UpdatedNodes->insert(New[i].getNode());
259 }
260 ReplacedNode(Old);
261 }
262
263 void ReplaceNodeWithValue(SDValue Old, SDValue New) {
264 LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
265 dbgs() << " with: "; New->dump(&DAG));
266
267 DAG.ReplaceAllUsesOfValueWith(Old, New);
268 if (UpdatedNodes)
269 UpdatedNodes->insert(New.getNode());
270 ReplacedNode(Old.getNode());
271 }
272};
273
274} // end anonymous namespace
275
276// Helper function that generates an MMO that considers the alignment of the
277// stack, and the size of the stack object
279 MachineFunction &MF,
280 bool isObjectScalable) {
281 auto &MFI = MF.getFrameInfo();
282 int FI = cast<FrameIndexSDNode>(StackPtr)->getIndex();
284 LocationSize ObjectSize = isObjectScalable
286 : LocationSize::precise(MFI.getObjectSize(FI));
288 ObjectSize, MFI.getObjectAlign(FI));
289}
290
291/// Return a vector shuffle operation which
292/// performs the same shuffle in terms of order or result bytes, but on a type
293/// whose vector element type is narrower than the original shuffle type.
294/// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
295SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
296 EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
297 ArrayRef<int> Mask) const {
298 unsigned NumMaskElts = VT.getVectorNumElements();
299 unsigned NumDestElts = NVT.getVectorNumElements();
300 unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
301
302 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
303
304 if (NumEltsGrowth == 1)
305 return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
306
307 SmallVector<int, 8> NewMask;
308 for (unsigned i = 0; i != NumMaskElts; ++i) {
309 int Idx = Mask[i];
310 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
311 if (Idx < 0)
312 NewMask.push_back(-1);
313 else
314 NewMask.push_back(Idx * NumEltsGrowth + j);
315 }
316 }
317 assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
318 assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
319 return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
320}
321
322/// Expands the ConstantFP node to an integer constant or
323/// a load from the constant pool.
325SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
326 bool Extend = false;
327 SDLoc dl(CFP);
328
329 // If a FP immediate is precise when represented as a float and if the
330 // target can do an extending load from float to double, we put it into
331 // the constant pool as a float, even if it's is statically typed as a
332 // double. This shrinks FP constants and canonicalizes them for targets where
333 // an FP extending load is the same cost as a normal load (such as on the x87
334 // fp stack or PPC FP unit).
335 EVT VT = CFP->getValueType(0);
336 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
337 if (!UseCP) {
338 assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
339 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
340 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
341 }
342
343 APFloat APF = CFP->getValueAPF();
344 EVT OrigVT = VT;
345 EVT SVT = VT;
346
347 // We don't want to shrink SNaNs. Converting the SNaN back to its real type
348 // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ).
349 if (!APF.isSignaling()) {
350 while (SVT != MVT::f32 && SVT != MVT::f16 && SVT != MVT::bf16) {
351 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
353 // Only do this if the target has a native EXTLOAD instruction from
354 // smaller type.
355 TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
356 TLI.ShouldShrinkFPConstant(OrigVT)) {
357 Type *SType = SVT.getTypeForEVT(*DAG.getContext());
359 Instruction::FPTrunc, LLVMC, SType, DAG.getDataLayout()));
360 VT = SVT;
361 Extend = true;
362 }
363 }
364 }
365
366 SDValue CPIdx =
367 DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
368 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
369 if (Extend) {
371 ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
373 Alignment);
374 return Result;
375 }
376 SDValue Result = DAG.getLoad(
377 OrigVT, dl, DAG.getEntryNode(), CPIdx,
379 return Result;
380}
381
382/// Expands the Constant node to a load from the constant pool.
383SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
384 SDLoc dl(CP);
385 EVT VT = CP->getValueType(0);
387 TLI.getPointerTy(DAG.getDataLayout()));
388 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
389 SDValue Result = DAG.getLoad(
390 VT, dl, DAG.getEntryNode(), CPIdx,
392 return Result;
393}
394
395SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Op) {
396 SDValue Vec = Op.getOperand(0);
397 SDValue Val = Op.getOperand(1);
398 SDValue Idx = Op.getOperand(2);
399 SDLoc dl(Op);
400
401 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
402 // SCALAR_TO_VECTOR requires that the type of the value being inserted
403 // match the element type of the vector being created, except for
404 // integers in which case the inserted value can be over width.
405 EVT EltVT = Vec.getValueType().getVectorElementType();
406 if (Val.getValueType() == EltVT ||
407 (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
408 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
409 Vec.getValueType(), Val);
410
411 unsigned NumElts = Vec.getValueType().getVectorNumElements();
412 // We generate a shuffle of InVec and ScVec, so the shuffle mask
413 // should be 0,1,2,3,4,5... with the appropriate element replaced with
414 // elt 0 of the RHS.
415 SmallVector<int, 8> ShufOps;
416 for (unsigned i = 0; i != NumElts; ++i)
417 ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
418
419 return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
420 }
421 }
422 return ExpandInsertToVectorThroughStack(Op);
423}
424
425SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
426 if (!ISD::isNormalStore(ST))
427 return SDValue();
428
429 LLVM_DEBUG(dbgs() << "Optimizing float store operations\n");
430 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
431 // FIXME: move this to the DAG Combiner! Note that we can't regress due
432 // to phase ordering between legalized code and the dag combiner. This
433 // probably means that we need to integrate dag combiner and legalizer
434 // together.
435 // We generally can't do this one for long doubles.
436 SDValue Chain = ST->getChain();
437 SDValue Ptr = ST->getBasePtr();
438 SDValue Value = ST->getValue();
439 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
440 AAMDNodes AAInfo = ST->getAAInfo();
441 SDLoc dl(ST);
442
443 // Don't optimise TargetConstantFP
444 if (Value.getOpcode() == ISD::TargetConstantFP)
445 return SDValue();
446
447 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
448 if (CFP->getValueType(0) == MVT::f32 &&
449 TLI.isTypeLegal(MVT::i32)) {
450 SDValue Con = DAG.getConstant(CFP->getValueAPF().
451 bitcastToAPInt().zextOrTrunc(32),
452 SDLoc(CFP), MVT::i32);
453 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
454 ST->getBaseAlign(), MMOFlags, AAInfo);
455 }
456
457 if (CFP->getValueType(0) == MVT::f64 &&
458 !TLI.isFPImmLegal(CFP->getValueAPF(), MVT::f64)) {
459 // If this target supports 64-bit registers, do a single 64-bit store.
460 if (TLI.isTypeLegal(MVT::i64)) {
462 zextOrTrunc(64), SDLoc(CFP), MVT::i64);
463 return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
464 ST->getBaseAlign(), MMOFlags, AAInfo);
465 }
466
467 if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
468 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
469 // stores. If the target supports neither 32- nor 64-bits, this
470 // xform is certainly not worth it.
471 const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
472 SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
473 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
474 if (DAG.getDataLayout().isBigEndian())
475 std::swap(Lo, Hi);
476
477 Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(),
478 ST->getBaseAlign(), MMOFlags, AAInfo);
479 Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(4), dl);
480 Hi = DAG.getStore(Chain, dl, Hi, Ptr,
481 ST->getPointerInfo().getWithOffset(4),
482 ST->getBaseAlign(), MMOFlags, AAInfo);
483
484 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
485 }
486 }
487 }
488 return SDValue();
489}
490
491void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
492 StoreSDNode *ST = cast<StoreSDNode>(Node);
493 SDValue Chain = ST->getChain();
494 SDValue Ptr = ST->getBasePtr();
495 SDLoc dl(Node);
496
497 MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
498 AAMDNodes AAInfo = ST->getAAInfo();
499
500 if (!ST->isTruncatingStore()) {
501 LLVM_DEBUG(dbgs() << "Legalizing store operation\n");
502 if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
503 ReplaceNode(ST, OptStore);
504 return;
505 }
506
507 SDValue Value = ST->getValue();
508 MVT VT = Value.getSimpleValueType();
509 switch (TLI.getOperationAction(ISD::STORE, VT)) {
510 default: llvm_unreachable("This action is not supported yet!");
511 case TargetLowering::Legal: {
512 // If this is an unaligned store and the target doesn't support it,
513 // expand it.
514 EVT MemVT = ST->getMemoryVT();
515 const DataLayout &DL = DAG.getDataLayout();
516 if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
517 *ST->getMemOperand())) {
518 LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n");
519 SDValue Result = TLI.expandUnalignedStore(ST, DAG);
520 ReplaceNode(SDValue(ST, 0), Result);
521 } else
522 LLVM_DEBUG(dbgs() << "Legal store\n");
523 break;
524 }
525 case TargetLowering::Custom: {
526 LLVM_DEBUG(dbgs() << "Trying custom lowering\n");
527 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
528 if (Res && Res != SDValue(Node, 0))
529 ReplaceNode(SDValue(Node, 0), Res);
530 return;
531 }
532 case TargetLowering::Promote: {
533 MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
534 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
535 "Can only promote stores to same size type");
536 Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
537 SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
538 ST->getBaseAlign(), MMOFlags, AAInfo);
539 ReplaceNode(SDValue(Node, 0), Result);
540 break;
541 }
542 }
543 return;
544 }
545
546 LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n");
547 SDValue Value = ST->getValue();
548 EVT StVT = ST->getMemoryVT();
549 TypeSize StWidth = StVT.getSizeInBits();
550 TypeSize StSize = StVT.getStoreSizeInBits();
551 auto &DL = DAG.getDataLayout();
552
553 if (StWidth != StSize) {
554 // Promote to a byte-sized store with upper bits zero if not
555 // storing an integral number of bytes. For example, promote
556 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
557 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedValue());
558 Value = DAG.getZeroExtendInReg(Value, dl, StVT);
560 DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT,
561 ST->getBaseAlign(), MMOFlags, AAInfo);
562 ReplaceNode(SDValue(Node, 0), Result);
563 } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedValue())) {
564 // If not storing a power-of-2 number of bits, expand as two stores.
565 assert(!StVT.isVector() && "Unsupported truncstore!");
566 unsigned StWidthBits = StWidth.getFixedValue();
567 unsigned LogStWidth = Log2_32(StWidthBits);
568 assert(LogStWidth < 32);
569 unsigned RoundWidth = 1 << LogStWidth;
570 assert(RoundWidth < StWidthBits);
571 unsigned ExtraWidth = StWidthBits - RoundWidth;
572 assert(ExtraWidth < RoundWidth);
573 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
574 "Store size not an integral number of bytes!");
575 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
576 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
577 SDValue Lo, Hi;
578 unsigned IncrementSize;
579
580 if (DL.isLittleEndian()) {
581 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
582 // Store the bottom RoundWidth bits.
583 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
584 RoundVT, ST->getBaseAlign(), MMOFlags, AAInfo);
585
586 // Store the remaining ExtraWidth bits.
587 IncrementSize = RoundWidth / 8;
588 Ptr =
589 DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
590 Hi = DAG.getNode(
591 ISD::SRL, dl, Value.getValueType(), Value,
592 DAG.getShiftAmountConstant(RoundWidth, Value.getValueType(), dl));
593 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
594 ST->getPointerInfo().getWithOffset(IncrementSize),
595 ExtraVT, ST->getBaseAlign(), MMOFlags, AAInfo);
596 } else {
597 // Big endian - avoid unaligned stores.
598 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
599 // Store the top RoundWidth bits.
600 Hi = DAG.getNode(
601 ISD::SRL, dl, Value.getValueType(), Value,
602 DAG.getShiftAmountConstant(ExtraWidth, Value.getValueType(), dl));
603 Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT,
604 ST->getBaseAlign(), MMOFlags, AAInfo);
605
606 // Store the remaining ExtraWidth bits.
607 IncrementSize = RoundWidth / 8;
608 Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
609 DAG.getConstant(IncrementSize, dl,
610 Ptr.getValueType()));
611 Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
612 ST->getPointerInfo().getWithOffset(IncrementSize),
613 ExtraVT, ST->getBaseAlign(), MMOFlags, AAInfo);
614 }
615
616 // The order of the stores doesn't matter.
617 SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
618 ReplaceNode(SDValue(Node, 0), Result);
619 } else {
620 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
621 default: llvm_unreachable("This action is not supported yet!");
622 case TargetLowering::Legal: {
623 EVT MemVT = ST->getMemoryVT();
624 // If this is an unaligned store and the target doesn't support it,
625 // expand it.
626 if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
627 *ST->getMemOperand())) {
628 SDValue Result = TLI.expandUnalignedStore(ST, DAG);
629 ReplaceNode(SDValue(ST, 0), Result);
630 }
631 break;
632 }
633 case TargetLowering::Custom: {
634 SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
635 if (Res && Res != SDValue(Node, 0))
636 ReplaceNode(SDValue(Node, 0), Res);
637 return;
638 }
639 case TargetLowering::Expand:
640 assert(!StVT.isVector() &&
641 "Vector Stores are handled in LegalizeVectorOps");
642
644
645 // TRUNCSTORE:i16 i32 -> STORE i16
646 if (TLI.isTypeLegal(StVT)) {
647 Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
648 Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
649 ST->getBaseAlign(), MMOFlags, AAInfo);
650 } else {
651 // The in-memory type isn't legal. Truncate to the type it would promote
652 // to, and then do a truncstore.
653 Value = DAG.getNode(ISD::TRUNCATE, dl,
654 TLI.getTypeToTransformTo(*DAG.getContext(), StVT),
655 Value);
656 Result = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
657 StVT, ST->getBaseAlign(), MMOFlags, AAInfo);
658 }
659
660 ReplaceNode(SDValue(Node, 0), Result);
661 break;
662 }
663 }
664}
665
666void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
667 LoadSDNode *LD = cast<LoadSDNode>(Node);
668 SDValue Chain = LD->getChain(); // The chain.
669 SDValue Ptr = LD->getBasePtr(); // The base pointer.
670 SDValue Value; // The value returned by the load op.
671 SDLoc dl(Node);
672
673 ISD::LoadExtType ExtType = LD->getExtensionType();
674 if (ExtType == ISD::NON_EXTLOAD) {
675 LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n");
676 MVT VT = Node->getSimpleValueType(0);
677 SDValue RVal = SDValue(Node, 0);
678 SDValue RChain = SDValue(Node, 1);
679
680 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
681 default: llvm_unreachable("This action is not supported yet!");
682 case TargetLowering::Legal: {
683 EVT MemVT = LD->getMemoryVT();
684 const DataLayout &DL = DAG.getDataLayout();
685 // If this is an unaligned load and the target doesn't support it,
686 // expand it.
687 if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
688 *LD->getMemOperand())) {
689 std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
690 }
691 break;
692 }
693 case TargetLowering::Custom:
694 if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
695 RVal = Res;
696 RChain = Res.getValue(1);
697 }
698 break;
699
700 case TargetLowering::Promote: {
701 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
702 assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
703 "Can only promote loads to same size type");
704
705 // If the range metadata type does not match the legalized memory
706 // operation type, remove the range metadata.
707 if (const MDNode *MD = LD->getRanges()) {
708 ConstantInt *Lower = mdconst::extract<ConstantInt>(MD->getOperand(0));
709 if (Lower->getBitWidth() != NVT.getScalarSizeInBits() ||
710 !NVT.isInteger())
711 LD->getMemOperand()->clearRanges();
712 }
713 SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
714 RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
715 RChain = Res.getValue(1);
716 break;
717 }
718 }
719 if (RChain.getNode() != Node) {
720 assert(RVal.getNode() != Node && "Load must be completely replaced");
721 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
722 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
723 if (UpdatedNodes) {
724 UpdatedNodes->insert(RVal.getNode());
725 UpdatedNodes->insert(RChain.getNode());
726 }
727 ReplacedNode(Node);
728 }
729 return;
730 }
731
732 LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n");
733 EVT SrcVT = LD->getMemoryVT();
734 TypeSize SrcWidth = SrcVT.getSizeInBits();
735 MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
736 AAMDNodes AAInfo = LD->getAAInfo();
737
738 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
739 // Some targets pretend to have an i1 loading operation, and actually
740 // load an i8. This trick is correct for ZEXTLOAD because the top 7
741 // bits are guaranteed to be zero; it helps the optimizers understand
742 // that these bits are zero. It is also useful for EXTLOAD, since it
743 // tells the optimizers that those bits are undefined. It would be
744 // nice to have an effective generic way of getting these benefits...
745 // Until such a way is found, don't insist on promoting i1 here.
746 (SrcVT != MVT::i1 ||
747 TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
748 TargetLowering::Promote)) {
749 // Promote to a byte-sized load if not loading an integral number of
750 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
751 unsigned NewWidth = SrcVT.getStoreSizeInBits();
752 EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
753 SDValue Ch;
754
755 // The extra bits are guaranteed to be zero, since we stored them that
756 // way. A zext load from NVT thus automatically gives zext from SrcVT.
757
758 ISD::LoadExtType NewExtType =
760
761 SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
762 Chain, Ptr, LD->getPointerInfo(), NVT,
763 LD->getBaseAlign(), MMOFlags, AAInfo);
764
765 Ch = Result.getValue(1); // The chain.
766
767 if (ExtType == ISD::SEXTLOAD)
768 // Having the top bits zero doesn't help when sign extending.
770 Result.getValueType(),
771 Result, DAG.getValueType(SrcVT));
772 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
773 // All the top bits are guaranteed to be zero - inform the optimizers.
775 Result.getValueType(), Result,
776 DAG.getValueType(SrcVT));
777
778 Value = Result;
779 Chain = Ch;
780 } else if (!isPowerOf2_64(SrcWidth.getKnownMinValue())) {
781 // If not loading a power-of-2 number of bits, expand as two loads.
782 assert(!SrcVT.isVector() && "Unsupported extload!");
783 unsigned SrcWidthBits = SrcWidth.getFixedValue();
784 unsigned LogSrcWidth = Log2_32(SrcWidthBits);
785 assert(LogSrcWidth < 32);
786 unsigned RoundWidth = 1 << LogSrcWidth;
787 assert(RoundWidth < SrcWidthBits);
788 unsigned ExtraWidth = SrcWidthBits - RoundWidth;
789 assert(ExtraWidth < RoundWidth);
790 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
791 "Load size not an integral number of bytes!");
792 EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
793 EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
794 SDValue Lo, Hi, Ch;
795 unsigned IncrementSize;
796 auto &DL = DAG.getDataLayout();
797
798 if (DL.isLittleEndian()) {
799 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
800 // Load the bottom RoundWidth bits.
801 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
802 LD->getPointerInfo(), RoundVT, LD->getBaseAlign(),
803 MMOFlags, AAInfo);
804
805 // Load the remaining ExtraWidth bits.
806 IncrementSize = RoundWidth / 8;
807 Ptr =
808 DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
809 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
810 LD->getPointerInfo().getWithOffset(IncrementSize),
811 ExtraVT, LD->getBaseAlign(), MMOFlags, AAInfo);
812
813 // Build a factor node to remember that this load is independent of
814 // the other one.
815 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
816 Hi.getValue(1));
817
818 // Move the top bits to the right place.
819 Hi = DAG.getNode(
820 ISD::SHL, dl, Hi.getValueType(), Hi,
821 DAG.getShiftAmountConstant(RoundWidth, Hi.getValueType(), dl));
822
823 // Join the hi and lo parts.
824 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
825 } else {
826 // Big endian - avoid unaligned loads.
827 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
828 // Load the top RoundWidth bits.
829 Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
830 LD->getPointerInfo(), RoundVT, LD->getBaseAlign(),
831 MMOFlags, AAInfo);
832
833 // Load the remaining ExtraWidth bits.
834 IncrementSize = RoundWidth / 8;
835 Ptr =
836 DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
837 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
838 LD->getPointerInfo().getWithOffset(IncrementSize),
839 ExtraVT, LD->getBaseAlign(), MMOFlags, AAInfo);
840
841 // Build a factor node to remember that this load is independent of
842 // the other one.
843 Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
844 Hi.getValue(1));
845
846 // Move the top bits to the right place.
847 Hi = DAG.getNode(
848 ISD::SHL, dl, Hi.getValueType(), Hi,
849 DAG.getShiftAmountConstant(ExtraWidth, Hi.getValueType(), dl));
850
851 // Join the hi and lo parts.
852 Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
853 }
854
855 Chain = Ch;
856 } else {
857 bool isCustom = false;
858 switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
859 SrcVT.getSimpleVT())) {
860 default: llvm_unreachable("This action is not supported yet!");
861 case TargetLowering::Custom:
862 isCustom = true;
863 [[fallthrough]];
864 case TargetLowering::Legal:
865 Value = SDValue(Node, 0);
866 Chain = SDValue(Node, 1);
867
868 if (isCustom) {
869 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
870 Value = Res;
871 Chain = Res.getValue(1);
872 }
873 } else {
874 // If this is an unaligned load and the target doesn't support it,
875 // expand it.
876 EVT MemVT = LD->getMemoryVT();
877 const DataLayout &DL = DAG.getDataLayout();
878 if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT,
879 *LD->getMemOperand())) {
880 std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
881 }
882 }
883 break;
884
885 case TargetLowering::Expand: {
886 EVT DestVT = Node->getValueType(0);
887 if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
888 // If the source type is not legal, see if there is a legal extload to
889 // an intermediate type that we can then extend further.
890 EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
891 if ((LoadVT.isFloatingPoint() == SrcVT.isFloatingPoint()) &&
892 (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
893 TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT))) {
894 // If we are loading a legal type, this is a non-extload followed by a
895 // full extend.
896 ISD::LoadExtType MidExtType =
897 (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
898
899 SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
900 SrcVT, LD->getMemOperand());
901 unsigned ExtendOp =
903 Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
904 Chain = Load.getValue(1);
905 break;
906 }
907
908 // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
909 // normal undefined upper bits behavior to allow using an in-reg extend
910 // with the illegal FP type, so load as an integer and do the
911 // from-integer conversion.
912 EVT SVT = SrcVT.getScalarType();
913 if (SVT == MVT::f16 || SVT == MVT::bf16) {
914 EVT ISrcVT = SrcVT.changeTypeToInteger();
915 EVT IDestVT = DestVT.changeTypeToInteger();
916 EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
917
918 SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain,
919 Ptr, ISrcVT, LD->getMemOperand());
920 Value =
921 DAG.getNode(SVT == MVT::f16 ? ISD::FP16_TO_FP : ISD::BF16_TO_FP,
922 dl, DestVT, Result);
923 Chain = Result.getValue(1);
924 break;
925 }
926 }
927
928 assert(!SrcVT.isVector() &&
929 "Vector Loads are handled in LegalizeVectorOps");
930
931 // FIXME: This does not work for vectors on most targets. Sign-
932 // and zero-extend operations are currently folded into extending
933 // loads, whether they are legal or not, and then we end up here
934 // without any support for legalizing them.
935 assert(ExtType != ISD::EXTLOAD &&
936 "EXTLOAD should always be supported!");
937 // Turn the unsupported load into an EXTLOAD followed by an
938 // explicit zero/sign extend inreg.
940 Node->getValueType(0),
941 Chain, Ptr, SrcVT,
942 LD->getMemOperand());
943 SDValue ValRes;
944 if (ExtType == ISD::SEXTLOAD)
945 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
946 Result.getValueType(),
947 Result, DAG.getValueType(SrcVT));
948 else
949 ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
950 Value = ValRes;
951 Chain = Result.getValue(1);
952 break;
953 }
954 }
955 }
956
957 // Since loads produce two values, make sure to remember that we legalized
958 // both of them.
959 if (Chain.getNode() != Node) {
960 assert(Value.getNode() != Node && "Load must be completely replaced");
962 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
963 if (UpdatedNodes) {
964 UpdatedNodes->insert(Value.getNode());
965 UpdatedNodes->insert(Chain.getNode());
966 }
967 ReplacedNode(Node);
968 }
969}
970
971/// Return a legal replacement for the given operation, with all legal operands.
972void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
973 LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
974
975 // Allow illegal target nodes and illegal registers.
976 if (Node->getOpcode() == ISD::TargetConstant ||
977 Node->getOpcode() == ISD::Register)
978 return;
979
980#ifndef NDEBUG
981 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
982 assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
983 TargetLowering::TypeLegal &&
984 "Unexpected illegal type!");
985
986 for (const SDValue &Op : Node->op_values())
987 assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
988 TargetLowering::TypeLegal ||
989 Op.getOpcode() == ISD::TargetConstant ||
990 Op.getOpcode() == ISD::Register) &&
991 "Unexpected illegal type!");
992#endif
993
994 // Figure out the correct action; the way to query this varies by opcode
995 TargetLowering::LegalizeAction Action = TargetLowering::Legal;
996 bool SimpleFinishLegalizing = true;
997 switch (Node->getOpcode()) {
998 case ISD::POISON: {
999 // TODO: Currently, POISON is being lowered to UNDEF here. However, there is
1000 // an open concern that this transformation may not be ideal, as targets
1001 // should ideally handle POISON directly. Changing this behavior would
1002 // require adding support for POISON in TableGen, which is a large change.
1003 // Additionally, many existing test cases rely on the current behavior
1004 // (e.g., llvm/test/CodeGen/PowerPC/vec_shuffle.ll). A broader discussion
1005 // and incremental changes might be needed to properly support POISON
1006 // without breaking existing targets and tests.
1007 SDValue UndefNode = DAG.getUNDEF(Node->getValueType(0));
1008 ReplaceNode(Node, UndefNode.getNode());
1009 return;
1010 }
1014 case ISD::STACKSAVE:
1015 case ISD::STACKADDRESS:
1016 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
1017 break;
1019 Action = TLI.getOperationAction(Node->getOpcode(),
1020 Node->getValueType(0));
1021 break;
1022 case ISD::VAARG:
1023 Action = TLI.getOperationAction(Node->getOpcode(),
1024 Node->getValueType(0));
1025 if (Action != TargetLowering::Promote)
1026 Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
1027 break;
1028 case ISD::SET_FPENV:
1029 case ISD::SET_FPMODE:
1030 Action = TLI.getOperationAction(Node->getOpcode(),
1031 Node->getOperand(1).getValueType());
1032 break;
1033 case ISD::FP_TO_FP16:
1034 case ISD::FP_TO_BF16:
1035 case ISD::SINT_TO_FP:
1036 case ISD::UINT_TO_FP:
1038 case ISD::LROUND:
1039 case ISD::LLROUND:
1040 case ISD::LRINT:
1041 case ISD::LLRINT:
1042 Action = TLI.getOperationAction(Node->getOpcode(),
1043 Node->getOperand(0).getValueType());
1044 break;
1049 case ISD::STRICT_LRINT:
1050 case ISD::STRICT_LLRINT:
1051 case ISD::STRICT_LROUND:
1053 // These pseudo-ops are the same as the other STRICT_ ops except
1054 // they are registered with setOperationAction() using the input type
1055 // instead of the output type.
1056 Action = TLI.getOperationAction(Node->getOpcode(),
1057 Node->getOperand(1).getValueType());
1058 break;
1060 EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
1061 Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
1062 break;
1063 }
1064 case ISD::ATOMIC_STORE:
1065 Action = TLI.getOperationAction(Node->getOpcode(),
1066 Node->getOperand(1).getValueType());
1067 break;
1068 case ISD::SELECT_CC:
1069 case ISD::STRICT_FSETCC:
1071 case ISD::SETCC:
1072 case ISD::SETCCCARRY:
1073 case ISD::VP_SETCC:
1074 case ISD::BR_CC: {
1075 unsigned Opc = Node->getOpcode();
1076 unsigned CCOperand = Opc == ISD::SELECT_CC ? 4
1077 : Opc == ISD::STRICT_FSETCC ? 3
1078 : Opc == ISD::STRICT_FSETCCS ? 3
1079 : Opc == ISD::SETCCCARRY ? 3
1080 : (Opc == ISD::SETCC || Opc == ISD::VP_SETCC) ? 2
1081 : 1;
1082 unsigned CompareOperand = Opc == ISD::BR_CC ? 2
1083 : Opc == ISD::STRICT_FSETCC ? 1
1084 : Opc == ISD::STRICT_FSETCCS ? 1
1085 : 0;
1086 MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
1087 ISD::CondCode CCCode =
1088 cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
1089 Action = TLI.getCondCodeAction(CCCode, OpVT);
1090 if (Action == TargetLowering::Legal) {
1091 if (Node->getOpcode() == ISD::SELECT_CC)
1092 Action = TLI.getOperationAction(Node->getOpcode(),
1093 Node->getValueType(0));
1094 else
1095 Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
1096 }
1097 break;
1098 }
1099 case ISD::LOAD:
1100 case ISD::STORE:
1101 // FIXME: Model these properly. LOAD and STORE are complicated, and
1102 // STORE expects the unlegalized operand in some cases.
1103 SimpleFinishLegalizing = false;
1104 break;
1105 case ISD::CALLSEQ_START:
1106 case ISD::CALLSEQ_END:
1107 // FIXME: This shouldn't be necessary. These nodes have special properties
1108 // dealing with the recursive nature of legalization. Removing this
1109 // special case should be done as part of making LegalizeDAG non-recursive.
1110 SimpleFinishLegalizing = false;
1111 break;
1113 case ISD::GET_ROUNDING:
1114 case ISD::MERGE_VALUES:
1115 case ISD::EH_RETURN:
1117 case ISD::EH_DWARF_CFA:
1121 // These operations lie about being legal: when they claim to be legal,
1122 // they should actually be expanded.
1123 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1124 if (Action == TargetLowering::Legal)
1125 Action = TargetLowering::Expand;
1126 break;
1129 case ISD::FRAMEADDR:
1130 case ISD::RETURNADDR:
1132 case ISD::SPONENTRY:
1133 // These operations lie about being legal: when they claim to be legal,
1134 // they should actually be custom-lowered.
1135 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1136 if (Action == TargetLowering::Legal)
1137 Action = TargetLowering::Custom;
1138 break;
1139 case ISD::CLEAR_CACHE:
1140 // This operation is typically going to be LibCall unless the target wants
1141 // something differrent.
1142 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1143 break;
1146 // READCYCLECOUNTER and READSTEADYCOUNTER return a i64, even if type
1147 // legalization might have expanded that to several smaller types.
1148 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1149 break;
1150 case ISD::READ_REGISTER:
1152 // Named register is legal in the DAG, but blocked by register name
1153 // selection if not implemented by target (to chose the correct register)
1154 // They'll be converted to Copy(To/From)Reg.
1155 Action = TargetLowering::Legal;
1156 break;
1157 case ISD::UBSANTRAP:
1158 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1159 if (Action == TargetLowering::Expand) {
1160 // replace ISD::UBSANTRAP with ISD::TRAP
1161 SDValue NewVal;
1162 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1163 Node->getOperand(0));
1164 ReplaceNode(Node, NewVal.getNode());
1165 LegalizeOp(NewVal.getNode());
1166 return;
1167 }
1168 break;
1169 case ISD::DEBUGTRAP:
1170 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1171 if (Action == TargetLowering::Expand) {
1172 // replace ISD::DEBUGTRAP with ISD::TRAP
1173 SDValue NewVal;
1174 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1175 Node->getOperand(0));
1176 ReplaceNode(Node, NewVal.getNode());
1177 LegalizeOp(NewVal.getNode());
1178 return;
1179 }
1180 break;
1181 case ISD::SADDSAT:
1182 case ISD::UADDSAT:
1183 case ISD::SSUBSAT:
1184 case ISD::USUBSAT:
1185 case ISD::SSHLSAT:
1186 case ISD::USHLSAT:
1187 case ISD::SCMP:
1188 case ISD::UCMP:
1191 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1192 break;
1193 case ISD::SMULFIX:
1194 case ISD::SMULFIXSAT:
1195 case ISD::UMULFIX:
1196 case ISD::UMULFIXSAT:
1197 case ISD::SDIVFIX:
1198 case ISD::SDIVFIXSAT:
1199 case ISD::UDIVFIX:
1200 case ISD::UDIVFIXSAT: {
1201 unsigned Scale = Node->getConstantOperandVal(2);
1202 Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
1203 Node->getValueType(0), Scale);
1204 break;
1205 }
1206 case ISD::MSCATTER:
1207 Action = TLI.getOperationAction(Node->getOpcode(),
1208 cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
1209 break;
1210 case ISD::MSTORE:
1211 Action = TLI.getOperationAction(Node->getOpcode(),
1212 cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
1213 break;
1214 case ISD::VP_SCATTER:
1215 Action = TLI.getOperationAction(
1216 Node->getOpcode(),
1217 cast<VPScatterSDNode>(Node)->getValue().getValueType());
1218 break;
1219 case ISD::VP_STORE:
1220 Action = TLI.getOperationAction(
1221 Node->getOpcode(),
1222 cast<VPStoreSDNode>(Node)->getValue().getValueType());
1223 break;
1224 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
1225 Action = TLI.getOperationAction(
1226 Node->getOpcode(),
1227 cast<VPStridedStoreSDNode>(Node)->getValue().getValueType());
1228 break;
1231 case ISD::VECREDUCE_ADD:
1232 case ISD::VECREDUCE_MUL:
1233 case ISD::VECREDUCE_AND:
1234 case ISD::VECREDUCE_OR:
1235 case ISD::VECREDUCE_XOR:
1244 case ISD::IS_FPCLASS:
1245 Action = TLI.getOperationAction(
1246 Node->getOpcode(), Node->getOperand(0).getValueType());
1247 break;
1250 case ISD::VP_REDUCE_FADD:
1251 case ISD::VP_REDUCE_FMUL:
1252 case ISD::VP_REDUCE_ADD:
1253 case ISD::VP_REDUCE_MUL:
1254 case ISD::VP_REDUCE_AND:
1255 case ISD::VP_REDUCE_OR:
1256 case ISD::VP_REDUCE_XOR:
1257 case ISD::VP_REDUCE_SMAX:
1258 case ISD::VP_REDUCE_SMIN:
1259 case ISD::VP_REDUCE_UMAX:
1260 case ISD::VP_REDUCE_UMIN:
1261 case ISD::VP_REDUCE_FMAX:
1262 case ISD::VP_REDUCE_FMIN:
1263 case ISD::VP_REDUCE_FMAXIMUM:
1264 case ISD::VP_REDUCE_FMINIMUM:
1265 case ISD::VP_REDUCE_SEQ_FADD:
1266 case ISD::VP_REDUCE_SEQ_FMUL:
1267 Action = TLI.getOperationAction(
1268 Node->getOpcode(), Node->getOperand(1).getValueType());
1269 break;
1270 case ISD::VP_CTTZ_ELTS:
1271 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
1272 Action = TLI.getOperationAction(Node->getOpcode(),
1273 Node->getOperand(0).getValueType());
1274 break;
1276 Action = TLI.getOperationAction(
1277 Node->getOpcode(),
1278 cast<MaskedHistogramSDNode>(Node)->getIndex().getValueType());
1279 break;
1280 default:
1281 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1282 Action = TLI.getCustomOperationAction(*Node);
1283 } else {
1284 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1285 }
1286 break;
1287 }
1288
1289 if (SimpleFinishLegalizing) {
1290 SDNode *NewNode = Node;
1291 switch (Node->getOpcode()) {
1292 default: break;
1293 case ISD::SHL:
1294 case ISD::SRL:
1295 case ISD::SRA:
1296 case ISD::ROTL:
1297 case ISD::ROTR:
1298 case ISD::SSHLSAT:
1299 case ISD::USHLSAT: {
1300 // Legalizing shifts/rotates requires adjusting the shift amount
1301 // to the appropriate width.
1302 SDValue Op0 = Node->getOperand(0);
1303 SDValue Op1 = Node->getOperand(1);
1304 if (!Op1.getValueType().isVector()) {
1305 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
1306 // The getShiftAmountOperand() may create a new operand node or
1307 // return the existing one. If new operand is created we need
1308 // to update the parent node.
1309 // Do not try to legalize SAO here! It will be automatically legalized
1310 // in the next round.
1311 if (SAO != Op1)
1312 NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
1313 }
1314 break;
1315 }
1316 case ISD::FSHL:
1317 case ISD::FSHR:
1318 case ISD::SRL_PARTS:
1319 case ISD::SRA_PARTS:
1320 case ISD::SHL_PARTS: {
1321 // Legalizing shifts/rotates requires adjusting the shift amount
1322 // to the appropriate width.
1323 SDValue Op0 = Node->getOperand(0);
1324 SDValue Op1 = Node->getOperand(1);
1325 SDValue Op2 = Node->getOperand(2);
1326 if (!Op2.getValueType().isVector()) {
1327 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
1328 // The getShiftAmountOperand() may create a new operand node or
1329 // return the existing one. If new operand is created we need
1330 // to update the parent node.
1331 if (SAO != Op2)
1332 NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
1333 }
1334 break;
1335 }
1336 }
1337
1338 if (NewNode != Node) {
1339 ReplaceNode(Node, NewNode);
1340 Node = NewNode;
1341 }
1342 switch (Action) {
1343 case TargetLowering::Legal:
1344 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
1345 return;
1346 case TargetLowering::Custom:
1347 LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
1348 // FIXME: The handling for custom lowering with multiple results is
1349 // a complete mess.
1350 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
1351 if (!(Res.getNode() != Node || Res.getResNo() != 0))
1352 return;
1353
1354 if (Node->getNumValues() == 1) {
1355 // Verify the new types match the original. Glue is waived because
1356 // ISD::ADDC can be legalized by replacing Glue with an integer type.
1357 assert((Res.getValueType() == Node->getValueType(0) ||
1358 Node->getValueType(0) == MVT::Glue) &&
1359 "Type mismatch for custom legalized operation");
1360 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
1361 // We can just directly replace this node with the lowered value.
1362 ReplaceNode(SDValue(Node, 0), Res);
1363 return;
1364 }
1365
1366 SmallVector<SDValue, 8> ResultVals;
1367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1368 // Verify the new types match the original. Glue is waived because
1369 // ISD::ADDC can be legalized by replacing Glue with an integer type.
1370 assert((Res->getValueType(i) == Node->getValueType(i) ||
1371 Node->getValueType(i) == MVT::Glue) &&
1372 "Type mismatch for custom legalized operation");
1373 ResultVals.push_back(Res.getValue(i));
1374 }
1375 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
1376 ReplaceNode(Node, ResultVals.data());
1377 return;
1378 }
1379 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
1380 [[fallthrough]];
1381 case TargetLowering::Expand:
1382 if (ExpandNode(Node))
1383 return;
1384 [[fallthrough]];
1385 case TargetLowering::LibCall:
1386 ConvertNodeToLibcall(Node);
1387 return;
1388 case TargetLowering::Promote:
1389 PromoteNode(Node);
1390 return;
1391 }
1392 }
1393
1394 switch (Node->getOpcode()) {
1395 default:
1396#ifndef NDEBUG
1397 dbgs() << "NODE: ";
1398 Node->dump( &DAG);
1399 dbgs() << "\n";
1400#endif
1401 llvm_unreachable("Do not know how to legalize this operator!");
1402
1403 case ISD::CALLSEQ_START:
1404 case ISD::CALLSEQ_END:
1405 break;
1406 case ISD::LOAD:
1407 return LegalizeLoadOps(Node);
1408 case ISD::STORE:
1409 return LegalizeStoreOps(Node);
1410 }
1411}
1412
1413SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1414 SDValue Vec = Op.getOperand(0);
1415 SDValue Idx = Op.getOperand(1);
1416 SDLoc dl(Op);
1417
1418 // Before we generate a new store to a temporary stack slot, see if there is
1419 // already one that we can use. There often is because when we scalarize
1420 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1421 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1422 // the vector. If all are expanded here, we don't want one store per vector
1423 // element.
1424
1425 // Caches for hasPredecessorHelper
1426 SmallPtrSet<const SDNode *, 32> Visited;
1428 Visited.insert(Op.getNode());
1429 Worklist.push_back(Idx.getNode());
1430 SDValue StackPtr, Ch;
1431 for (SDNode *User : Vec.getNode()->users()) {
1432 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1433 if (ST->isIndexed() || ST->isTruncatingStore() ||
1434 ST->getValue() != Vec)
1435 continue;
1436
1437 // Make sure that nothing else could have stored into the destination of
1438 // this store.
1439 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1440 continue;
1441
1442 // If the index is dependent on the store we will introduce a cycle when
1443 // creating the load (the load uses the index, and by replacing the chain
1444 // we will make the index dependent on the load). Also, the store might be
1445 // dependent on the extractelement and introduce a cycle when creating
1446 // the load.
1447 if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) ||
1448 ST->hasPredecessor(Op.getNode()))
1449 continue;
1450
1451 StackPtr = ST->getBasePtr();
1452 Ch = SDValue(ST, 0);
1453 break;
1454 }
1455 }
1456
1457 EVT VecVT = Vec.getValueType();
1458
1459 if (!Ch.getNode()) {
1460 // Store the value to a temporary stack slot, then LOAD the returned part.
1461 StackPtr = DAG.CreateStackTemporary(VecVT);
1462 MachineMemOperand *StoreMMO = getStackAlignedMMO(
1463 StackPtr, DAG.getMachineFunction(), VecVT.isScalableVector());
1464 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, StoreMMO);
1465 }
1466
1467 SDValue NewLoad;
1468 Align ElementAlignment =
1469 std::min(cast<StoreSDNode>(Ch)->getAlign(),
1471 Op.getValueType().getTypeForEVT(*DAG.getContext())));
1472
1473 if (Op.getValueType().isVector()) {
1474 StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
1475 Op.getValueType(), Idx);
1476 NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1477 MachinePointerInfo(), ElementAlignment);
1478 } else {
1479 StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1480 NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1481 MachinePointerInfo(), VecVT.getVectorElementType(),
1482 ElementAlignment);
1483 }
1484
1485 // Replace the chain going out of the store, by the one out of the load.
1486 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1487
1488 // We introduced a cycle though, so update the loads operands, making sure
1489 // to use the original store's chain as an incoming chain.
1490 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->ops());
1491 NewLoadOperands[0] = Ch;
1492 NewLoad =
1493 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1494 return NewLoad;
1495}
1496
1497SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1498 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1499
1500 SDValue Vec = Op.getOperand(0);
1501 SDValue Part = Op.getOperand(1);
1502 SDValue Idx = Op.getOperand(2);
1503 SDLoc dl(Op);
1504
1505 // Store the value to a temporary stack slot, then LOAD the returned part.
1506 EVT VecVT = Vec.getValueType();
1507 EVT PartVT = Part.getValueType();
1509 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1510 MachinePointerInfo PtrInfo =
1512
1513 // First store the whole vector.
1514 Align BaseVecAlignment =
1516 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo,
1517 BaseVecAlignment);
1518
1519 // Freeze the index so we don't poison the clamping code we're about to emit.
1520 Idx = DAG.getFreeze(Idx);
1521
1522 Type *PartTy = PartVT.getTypeForEVT(*DAG.getContext());
1523 Align PartAlignment = DAG.getDataLayout().getPrefTypeAlign(PartTy);
1524
1525 // Then store the inserted part.
1526 if (PartVT.isVector()) {
1527 SDValue SubStackPtr =
1528 TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, PartVT, Idx);
1529
1530 // Store the subvector.
1531 Ch = DAG.getStore(
1532 Ch, dl, Part, SubStackPtr,
1534 PartAlignment);
1535 } else {
1536 SDValue SubStackPtr =
1537 TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1538
1539 // Store the scalar value.
1540 Ch = DAG.getTruncStore(
1541 Ch, dl, Part, SubStackPtr,
1543 VecVT.getVectorElementType(), PartAlignment);
1544 }
1545
1546 assert(cast<StoreSDNode>(Ch)->getAlign() == PartAlignment &&
1547 "ElementAlignment does not match!");
1548
1549 // Finally, load the updated vector.
1550 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo,
1551 BaseVecAlignment);
1552}
1553
1554SDValue SelectionDAGLegalize::ExpandConcatVectors(SDNode *Node) {
1555 assert(Node->getOpcode() == ISD::CONCAT_VECTORS && "Unexpected opcode!");
1556 SDLoc DL(Node);
1558 unsigned NumOperands = Node->getNumOperands();
1559 MVT VectorIdxType = TLI.getVectorIdxTy(DAG.getDataLayout());
1560 EVT VectorValueType = Node->getOperand(0).getValueType();
1561 unsigned NumSubElem = VectorValueType.getVectorNumElements();
1562 EVT ElementValueType = TLI.getTypeToTransformTo(
1563 *DAG.getContext(), VectorValueType.getVectorElementType());
1564 for (unsigned I = 0; I < NumOperands; ++I) {
1565 SDValue SubOp = Node->getOperand(I);
1566 for (unsigned Idx = 0; Idx < NumSubElem; ++Idx) {
1567 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElementValueType,
1568 SubOp,
1569 DAG.getConstant(Idx, DL, VectorIdxType)));
1570 }
1571 }
1572 return DAG.getBuildVector(Node->getValueType(0), DL, Ops);
1573}
1574
1575SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1576 assert((Node->getOpcode() == ISD::BUILD_VECTOR ||
1577 Node->getOpcode() == ISD::CONCAT_VECTORS) &&
1578 "Unexpected opcode!");
1579
1580 // We can't handle this case efficiently. Allocate a sufficiently
1581 // aligned object on the stack, store each operand into it, then load
1582 // the result as a vector.
1583 // Create the stack frame object.
1584 EVT VT = Node->getValueType(0);
1585 EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType()
1586 : Node->getOperand(0).getValueType();
1587 SDLoc dl(Node);
1588 SDValue FIPtr = DAG.CreateStackTemporary(VT);
1589 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
1590 MachinePointerInfo PtrInfo =
1592
1593 // Emit a store of each element to the stack slot.
1595 unsigned TypeByteSize = MemVT.getSizeInBits() / 8;
1596 assert(TypeByteSize > 0 && "Vector element type too small for stack store!");
1597
1598 // If the destination vector element type of a BUILD_VECTOR is narrower than
1599 // the source element type, only store the bits necessary.
1600 bool Truncate = isa<BuildVectorSDNode>(Node) &&
1601 MemVT.bitsLT(Node->getOperand(0).getValueType());
1602
1603 // Store (in the right endianness) the elements to memory.
1604 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1605 // Ignore undef elements.
1606 if (Node->getOperand(i).isUndef()) continue;
1607
1608 unsigned Offset = TypeByteSize*i;
1609
1610 SDValue Idx =
1612
1613 if (Truncate)
1614 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
1615 Node->getOperand(i), Idx,
1616 PtrInfo.getWithOffset(Offset), MemVT));
1617 else
1618 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
1619 Idx, PtrInfo.getWithOffset(Offset)));
1620 }
1621
1622 SDValue StoreChain;
1623 if (!Stores.empty()) // Not all undef elements?
1624 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
1625 else
1626 StoreChain = DAG.getEntryNode();
1627
1628 // Result is a load from the stack slot.
1629 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
1630}
1631
1632/// Bitcast a floating-point value to an integer value. Only bitcast the part
1633/// containing the sign bit if the target has no integer value capable of
1634/// holding all bits of the floating-point value.
1635void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
1636 const SDLoc &DL,
1637 SDValue Value) const {
1638 EVT FloatVT = Value.getValueType();
1639 unsigned NumBits = FloatVT.getScalarSizeInBits();
1640 State.FloatVT = FloatVT;
1641 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1642 // Convert to an integer of the same size.
1643 if (TLI.isTypeLegal(IVT)) {
1644 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1645 State.SignMask = APInt::getSignMask(NumBits);
1646 State.SignBit = NumBits - 1;
1647 return;
1648 }
1649
1650 auto &DataLayout = DAG.getDataLayout();
1651 // Store the float to memory, then load the sign part out as an integer.
1652 MVT LoadTy = TLI.getRegisterType(MVT::i8);
1653 // First create a temporary that is aligned for both the load and store.
1654 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1655 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1656 // Then store the float to it.
1657 State.FloatPtr = StackPtr;
1658 MachineFunction &MF = DAG.getMachineFunction();
1659 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1660 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
1661 State.FloatPointerInfo);
1662
1663 SDValue IntPtr;
1664 if (DataLayout.isBigEndian()) {
1665 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1666 // Load out a legal integer with the same sign bit as the float.
1667 IntPtr = StackPtr;
1668 State.IntPointerInfo = State.FloatPointerInfo;
1669 } else {
1670 // Advance the pointer so that the loaded byte will contain the sign bit.
1671 unsigned ByteOffset = (NumBits / 8) - 1;
1672 IntPtr =
1673 DAG.getMemBasePlusOffset(StackPtr, TypeSize::getFixed(ByteOffset), DL);
1674 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1675 ByteOffset);
1676 }
1677
1678 State.IntPtr = IntPtr;
1679 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
1680 State.IntPointerInfo, MVT::i8);
1681 State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7);
1682 State.SignBit = 7;
1683}
1684
1685/// Replace the integer value produced by getSignAsIntValue() with a new value
1686/// and cast the result back to a floating-point type.
1687SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
1688 const SDLoc &DL,
1689 SDValue NewIntValue) const {
1690 if (!State.Chain)
1691 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1692
1693 // Override the part containing the sign bit in the value stored on the stack.
1694 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
1695 State.IntPointerInfo, MVT::i8);
1696 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
1697 State.FloatPointerInfo);
1698}
1699
1700SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1701 SDLoc DL(Node);
1702 SDValue Mag = Node->getOperand(0);
1703 SDValue Sign = Node->getOperand(1);
1704
1705 // Get sign bit into an integer value.
1706 FloatSignAsInt SignAsInt;
1707 getSignAsIntValue(SignAsInt, DL, Sign);
1708
1709 EVT IntVT = SignAsInt.IntValue.getValueType();
1710 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1711 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1712 SignMask);
1713
1714 // If FABS is legal transform
1715 // FCOPYSIGN(x, y) => SignBit(y) ? -FABS(x) : FABS(x)
1716 EVT FloatVT = Mag.getValueType();
1717 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1718 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1719 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1720 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1721 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1722 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1723 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1724 }
1725
1726 // Transform Mag value to integer, and clear the sign bit.
1727 FloatSignAsInt MagAsInt;
1728 getSignAsIntValue(MagAsInt, DL, Mag);
1729 EVT MagVT = MagAsInt.IntValue.getValueType();
1730 SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
1731 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
1732 ClearSignMask);
1733
1734 // Get the signbit at the right position for MagAsInt.
1735 int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
1736 EVT ShiftVT = IntVT;
1737 if (SignBit.getScalarValueSizeInBits() <
1738 ClearedSign.getScalarValueSizeInBits()) {
1739 SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
1740 ShiftVT = MagVT;
1741 }
1742 if (ShiftAmount > 0) {
1743 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT);
1744 SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst);
1745 } else if (ShiftAmount < 0) {
1746 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT);
1747 SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst);
1748 }
1749 if (SignBit.getScalarValueSizeInBits() >
1750 ClearedSign.getScalarValueSizeInBits()) {
1751 SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
1752 }
1753
1754 // Store the part with the modified sign and convert back to float.
1755 SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit,
1757
1758 return modifySignAsInt(MagAsInt, DL, CopiedSign);
1759}
1760
1761SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const {
1762 // Get the sign bit as an integer.
1763 SDLoc DL(Node);
1764 FloatSignAsInt SignAsInt;
1765 getSignAsIntValue(SignAsInt, DL, Node->getOperand(0));
1766 EVT IntVT = SignAsInt.IntValue.getValueType();
1767
1768 // Flip the sign.
1769 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1770 SDValue SignFlip =
1771 DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask);
1772
1773 // Convert back to float.
1774 return modifySignAsInt(SignAsInt, DL, SignFlip);
1775}
1776
1777SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1778 SDLoc DL(Node);
1779 SDValue Value = Node->getOperand(0);
1780
1781 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1782 EVT FloatVT = Value.getValueType();
1783 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1784 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1785 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1786 }
1787
1788 // Transform value to integer, clear the sign bit and transform back.
1789 FloatSignAsInt ValueAsInt;
1790 getSignAsIntValue(ValueAsInt, DL, Value);
1791 EVT IntVT = ValueAsInt.IntValue.getValueType();
1792 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1793 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1794 ClearSignMask);
1795 return modifySignAsInt(ValueAsInt, DL, ClearedSign);
1796}
1797
1798void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1799 SmallVectorImpl<SDValue> &Results) {
1801 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1802 " not tell us which reg is the stack pointer!");
1803 SDLoc dl(Node);
1804 EVT VT = Node->getValueType(0);
1805 SDValue Tmp1 = SDValue(Node, 0);
1806 SDValue Tmp2 = SDValue(Node, 1);
1807 SDValue Tmp3 = Node->getOperand(2);
1808 SDValue Chain = Tmp1.getOperand(0);
1809
1810 // Chain the dynamic stack allocation so that it doesn't modify the stack
1811 // pointer when other instructions are using the stack.
1812 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
1813
1814 SDValue Size = Tmp2.getOperand(1);
1815 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1816 Chain = SP.getValue(1);
1817 Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue();
1818 const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering();
1819 unsigned Opc =
1822
1823 Align StackAlign = TFL->getStackAlign();
1824 Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size); // Value
1825 if (Alignment > StackAlign)
1826 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
1827 DAG.getSignedConstant(-Alignment.value(), dl, VT));
1828 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1829
1830 Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
1831
1832 Results.push_back(Tmp1);
1833 Results.push_back(Tmp2);
1834}
1835
1836/// Emit a store/load combination to the stack. This stores
1837/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1838/// a load from the stack slot to DestVT, extending it if needed.
1839/// The resultant code need not be legal.
1840SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1841 EVT DestVT, const SDLoc &dl) {
1842 return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode());
1843}
1844
1845SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1846 EVT DestVT, const SDLoc &dl,
1847 SDValue Chain) {
1848 EVT SrcVT = SrcOp.getValueType();
1849 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1850 Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType);
1851
1852 // Don't convert with stack if the load/store is expensive.
1853 if ((SrcVT.bitsGT(SlotVT) &&
1854 !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) ||
1855 (SlotVT.bitsLT(DestVT) &&
1856 !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT)))
1857 return SDValue();
1858
1859 // Create the stack frame object.
1860 Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign(
1861 SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1862 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign);
1863
1864 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1865 int SPFI = StackPtrFI->getIndex();
1866 MachinePointerInfo PtrInfo =
1868
1869 // Emit a store to the stack slot. Use a truncstore if the input value is
1870 // later than DestVT.
1871 SDValue Store;
1872
1873 if (SrcVT.bitsGT(SlotVT))
1874 Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo,
1875 SlotVT, SrcAlign);
1876 else {
1877 assert(SrcVT.bitsEq(SlotVT) && "Invalid store");
1878 Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
1879 }
1880
1881 // Result is a load from the stack slot.
1882 if (SlotVT.bitsEq(DestVT))
1883 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
1884
1885 assert(SlotVT.bitsLT(DestVT) && "Unknown extension!");
1886 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
1887 DestAlign);
1888}
1889
1890SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
1891 SDLoc dl(Node);
1892 // Create a vector sized/aligned stack slot, store the value to element #0,
1893 // then load the whole vector back out.
1894 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
1895
1896 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1897 int SPFI = StackPtrFI->getIndex();
1898
1899 SDValue Ch = DAG.getTruncStore(
1900 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
1902 Node->getValueType(0).getVectorElementType());
1903 return DAG.getLoad(
1904 Node->getValueType(0), dl, Ch, StackPtr,
1906}
1907
1908static bool
1910 const TargetLowering &TLI, SDValue &Res) {
1911 unsigned NumElems = Node->getNumOperands();
1912 SDLoc dl(Node);
1913 EVT VT = Node->getValueType(0);
1914
1915 // Try to group the scalars into pairs, shuffle the pairs together, then
1916 // shuffle the pairs of pairs together, etc. until the vector has
1917 // been built. This will work only if all of the necessary shuffle masks
1918 // are legal.
1919
1920 // We do this in two phases; first to check the legality of the shuffles,
1921 // and next, assuming that all shuffles are legal, to create the new nodes.
1922 for (int Phase = 0; Phase < 2; ++Phase) {
1924 NewIntermedVals;
1925 for (unsigned i = 0; i < NumElems; ++i) {
1926 SDValue V = Node->getOperand(i);
1927 if (V.isUndef())
1928 continue;
1929
1930 SDValue Vec;
1931 if (Phase)
1932 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
1933 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
1934 }
1935
1936 while (IntermedVals.size() > 2) {
1937 NewIntermedVals.clear();
1938 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
1939 // This vector and the next vector are shuffled together (simply to
1940 // append the one to the other).
1941 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1942
1943 SmallVector<int, 16> FinalIndices;
1944 FinalIndices.reserve(IntermedVals[i].second.size() +
1945 IntermedVals[i+1].second.size());
1946
1947 int k = 0;
1948 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
1949 ++j, ++k) {
1950 ShuffleVec[k] = j;
1951 FinalIndices.push_back(IntermedVals[i].second[j]);
1952 }
1953 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
1954 ++j, ++k) {
1955 ShuffleVec[k] = NumElems + j;
1956 FinalIndices.push_back(IntermedVals[i+1].second[j]);
1957 }
1958
1959 SDValue Shuffle;
1960 if (Phase)
1961 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
1962 IntermedVals[i+1].first,
1963 ShuffleVec);
1964 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1965 return false;
1966 NewIntermedVals.push_back(
1967 std::make_pair(Shuffle, std::move(FinalIndices)));
1968 }
1969
1970 // If we had an odd number of defined values, then append the last
1971 // element to the array of new vectors.
1972 if ((IntermedVals.size() & 1) != 0)
1973 NewIntermedVals.push_back(IntermedVals.back());
1974
1975 IntermedVals.swap(NewIntermedVals);
1976 }
1977
1978 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
1979 "Invalid number of intermediate vectors");
1980 SDValue Vec1 = IntermedVals[0].first;
1981 SDValue Vec2;
1982 if (IntermedVals.size() > 1)
1983 Vec2 = IntermedVals[1].first;
1984 else if (Phase)
1985 Vec2 = DAG.getPOISON(VT);
1986
1987 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1988 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
1989 ShuffleVec[IntermedVals[0].second[i]] = i;
1990 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
1991 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
1992
1993 if (Phase)
1994 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
1995 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1996 return false;
1997 }
1998
1999 return true;
2000}
2001
2002/// Expand a BUILD_VECTOR node on targets that don't
2003/// support the operation, but do support the resultant vector type.
2004SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
2005 unsigned NumElems = Node->getNumOperands();
2006 SDValue Value1, Value2;
2007 SDLoc dl(Node);
2008 EVT VT = Node->getValueType(0);
2009 EVT OpVT = Node->getOperand(0).getValueType();
2010 EVT EltVT = VT.getVectorElementType();
2011
2012 // If the only non-undef value is the low element, turn this into a
2013 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
2014 bool isOnlyLowElement = true;
2015 bool MoreThanTwoValues = false;
2016 bool isConstant = true;
2017 for (unsigned i = 0; i < NumElems; ++i) {
2018 SDValue V = Node->getOperand(i);
2019 if (V.isUndef())
2020 continue;
2021 if (i > 0)
2022 isOnlyLowElement = false;
2024 isConstant = false;
2025
2026 if (!Value1.getNode()) {
2027 Value1 = V;
2028 } else if (!Value2.getNode()) {
2029 if (V != Value1)
2030 Value2 = V;
2031 } else if (V != Value1 && V != Value2) {
2032 MoreThanTwoValues = true;
2033 }
2034 }
2035
2036 if (!Value1.getNode())
2037 return DAG.getUNDEF(VT);
2038
2039 if (isOnlyLowElement)
2040 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
2041
2042 // If all elements are constants, create a load from the constant pool.
2043 if (isConstant) {
2045 for (unsigned i = 0, e = NumElems; i != e; ++i) {
2046 if (ConstantFPSDNode *V =
2047 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
2048 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
2049 } else if (ConstantSDNode *V =
2050 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
2051 if (OpVT==EltVT)
2052 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
2053 else {
2054 // If OpVT and EltVT don't match, EltVT is not legal and the
2055 // element values have been promoted/truncated earlier. Undo this;
2056 // we don't want a v16i8 to become a v16i32 for example.
2057 const ConstantInt *CI = V->getConstantIntValue();
2058 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
2059 CI->getZExtValue(), /*IsSigned=*/false,
2060 /*ImplicitTrunc=*/true));
2061 }
2062 } else {
2063 assert(Node->getOperand(i).isUndef());
2064 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
2065 CV.push_back(UndefValue::get(OpNTy));
2066 }
2067 }
2068 Constant *CP = ConstantVector::get(CV);
2069 SDValue CPIdx =
2070 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
2071 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2072 return DAG.getLoad(
2073 VT, dl, DAG.getEntryNode(), CPIdx,
2075 Alignment);
2076 }
2077
2078 SmallSet<SDValue, 16> DefinedValues;
2079 for (unsigned i = 0; i < NumElems; ++i) {
2080 if (Node->getOperand(i).isUndef())
2081 continue;
2082 DefinedValues.insert(Node->getOperand(i));
2083 }
2084
2085 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
2086 if (!MoreThanTwoValues) {
2087 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2088 for (unsigned i = 0; i < NumElems; ++i) {
2089 SDValue V = Node->getOperand(i);
2090 if (V.isUndef())
2091 continue;
2092 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2093 }
2094 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
2095 // Get the splatted value into the low element of a vector register.
2096 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2097 SDValue Vec2;
2098 if (Value2.getNode())
2099 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2100 else
2101 Vec2 = DAG.getPOISON(VT);
2102
2103 // Return shuffle(LowValVec, undef, <0,0,0,0>)
2104 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
2105 }
2106 } else {
2107 SDValue Res;
2108 if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
2109 return Res;
2110 }
2111 }
2112
2113 // Otherwise, we can't handle this case efficiently.
2114 return ExpandVectorBuildThroughStack(Node);
2115}
2116
2117SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
2118 SDLoc DL(Node);
2119 EVT VT = Node->getValueType(0);
2120 SDValue SplatVal = Node->getOperand(0);
2121
2122 return DAG.getSplatBuildVector(VT, DL, SplatVal);
2123}
2124
2125// Expand a node into a call to a libcall, returning the value as the first
2126// result and the chain as the second. If the result value does not fit into a
2127// register, return the lo part and set the hi part to the by-reg argument in
2128// the first. If it does fit into a single register, return the result and
2129// leave the Hi part unset.
2130std::pair<SDValue, SDValue>
2131SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2132 TargetLowering::ArgListTy &&Args,
2133 bool IsSigned, EVT RetVT) {
2134 EVT CodePtrTy = TLI.getPointerTy(DAG.getDataLayout());
2136 RTLIB::LibcallImpl LCImpl = DAG.getLibcalls().getLibcallImpl(LC);
2137 if (LCImpl != RTLIB::Unsupported)
2138 Callee = DAG.getExternalSymbol(LCImpl, CodePtrTy);
2139 else {
2140 Callee = DAG.getPOISON(CodePtrTy);
2141 DAG.getContext()->emitError(Twine("no libcall available for ") +
2142 Node->getOperationName(&DAG));
2143 }
2144
2145 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2146
2147 // By default, the input chain to this libcall is the entry node of the
2148 // function. If the libcall is going to be emitted as a tail call then
2149 // TLI.isUsedByReturnOnly will change it to the right chain if the return
2150 // node which is being folded has a non-entry input chain.
2151 SDValue InChain = DAG.getEntryNode();
2152
2153 // isTailCall may be true since the callee does not reference caller stack
2154 // frame. Check if it's in the right position and that the return types match.
2155 SDValue TCChain = InChain;
2156 const Function &F = DAG.getMachineFunction().getFunction();
2157 bool isTailCall =
2158 TLI.isInTailCallPosition(DAG, Node, TCChain) &&
2159 (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
2160 if (isTailCall)
2161 InChain = TCChain;
2162
2163 TargetLowering::CallLoweringInfo CLI(DAG);
2164 bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetTy, IsSigned);
2165 CLI.setDebugLoc(SDLoc(Node))
2166 .setChain(InChain)
2167 .setLibCallee(DAG.getLibcalls().getLibcallImplCallingConv(LCImpl), RetTy,
2168 Callee, std::move(Args))
2169 .setTailCall(isTailCall)
2170 .setSExtResult(signExtend)
2171 .setZExtResult(!signExtend)
2172 .setIsPostTypeLegalization(true);
2173
2174 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2175
2176 if (!CallInfo.second.getNode()) {
2177 LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG));
2178 // It's a tailcall, return the chain (which is the DAG root).
2179 return {DAG.getRoot(), DAG.getRoot()};
2180 }
2181
2182 LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG));
2183 return CallInfo;
2184}
2185
2186std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2187 bool isSigned) {
2188 TargetLowering::ArgListTy Args;
2189 for (const SDValue &Op : Node->op_values()) {
2190 EVT ArgVT = Op.getValueType();
2191 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2192 TargetLowering::ArgListEntry Entry(Op, ArgTy);
2193 Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgTy, isSigned);
2194 Entry.IsZExt = !Entry.IsSExt;
2195 Args.push_back(Entry);
2196 }
2197
2198 return ExpandLibCall(LC, Node, std::move(Args), isSigned,
2199 Node->getValueType(0));
2200}
2201
2202void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2203 RTLIB::Libcall LC,
2204 SmallVectorImpl<SDValue> &Results) {
2205 if (LC == RTLIB::UNKNOWN_LIBCALL)
2206 llvm_unreachable("Can't create an unknown libcall!");
2207
2208 if (Node->isStrictFPOpcode()) {
2209 EVT RetVT = Node->getValueType(0);
2211 TargetLowering::MakeLibCallOptions CallOptions;
2212 CallOptions.IsPostTypeLegalization = true;
2213 // FIXME: This doesn't support tail calls.
2214 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
2215 Ops, CallOptions,
2216 SDLoc(Node),
2217 Node->getOperand(0));
2218 Results.push_back(Tmp.first);
2219 Results.push_back(Tmp.second);
2220 } else {
2221 bool IsSignedArgument = Node->getOpcode() == ISD::FLDEXP;
2222 SDValue Tmp = ExpandLibCall(LC, Node, IsSignedArgument).first;
2223 Results.push_back(Tmp);
2224 }
2225}
2226
2227/// Expand the node to a libcall based on the result type.
2228void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2229 RTLIB::Libcall Call_F32,
2230 RTLIB::Libcall Call_F64,
2231 RTLIB::Libcall Call_F80,
2232 RTLIB::Libcall Call_F128,
2233 RTLIB::Libcall Call_PPCF128,
2234 SmallVectorImpl<SDValue> &Results) {
2235 RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0),
2236 Call_F32, Call_F64, Call_F80,
2237 Call_F128, Call_PPCF128);
2238 ExpandFPLibCall(Node, LC, Results);
2239}
2240
2241void SelectionDAGLegalize::ExpandFastFPLibCall(
2242 SDNode *Node, bool IsFast,
2243 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F32,
2244 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F64,
2245 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F80,
2246 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_F128,
2247 std::pair<RTLIB::Libcall, RTLIB::Libcall> Call_PPCF128,
2248 SmallVectorImpl<SDValue> &Results) {
2249
2250 EVT VT = Node->getSimpleValueType(0);
2251
2252 RTLIB::Libcall LC;
2253
2254 // FIXME: Probably should define fast to respect nan/inf and only be
2255 // approximate functions.
2256
2257 if (IsFast) {
2258 LC = RTLIB::getFPLibCall(VT, Call_F32.first, Call_F64.first, Call_F80.first,
2259 Call_F128.first, Call_PPCF128.first);
2260 }
2261
2262 if (!IsFast || DAG.getLibcalls().getLibcallImpl(LC) == RTLIB::Unsupported) {
2263 // Fall back if we don't have a fast implementation.
2264 LC = RTLIB::getFPLibCall(VT, Call_F32.second, Call_F64.second,
2265 Call_F80.second, Call_F128.second,
2266 Call_PPCF128.second);
2267 }
2268
2269 ExpandFPLibCall(Node, LC, Results);
2270}
2271
2272SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2273 RTLIB::Libcall Call_I8,
2274 RTLIB::Libcall Call_I16,
2275 RTLIB::Libcall Call_I32,
2276 RTLIB::Libcall Call_I64,
2277 RTLIB::Libcall Call_I128) {
2278 RTLIB::Libcall LC;
2279 switch (Node->getSimpleValueType(0).SimpleTy) {
2280 default: llvm_unreachable("Unexpected request for libcall!");
2281 case MVT::i8: LC = Call_I8; break;
2282 case MVT::i16: LC = Call_I16; break;
2283 case MVT::i32: LC = Call_I32; break;
2284 case MVT::i64: LC = Call_I64; break;
2285 case MVT::i128: LC = Call_I128; break;
2286 }
2287 return ExpandLibCall(LC, Node, isSigned).first;
2288}
2289
2290/// Expand the node to a libcall based on first argument type (for instance
2291/// lround and its variant).
2292void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
2293 RTLIB::Libcall Call_F32,
2294 RTLIB::Libcall Call_F64,
2295 RTLIB::Libcall Call_F80,
2296 RTLIB::Libcall Call_F128,
2297 RTLIB::Libcall Call_PPCF128,
2298 SmallVectorImpl<SDValue> &Results) {
2299 EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType();
2300 RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(),
2301 Call_F32, Call_F64, Call_F80,
2302 Call_F128, Call_PPCF128);
2303 ExpandFPLibCall(Node, LC, Results);
2304}
2305
2306SDValue SelectionDAGLegalize::ExpandBitCountingLibCall(
2307 SDNode *Node, RTLIB::Libcall CallI32, RTLIB::Libcall CallI64,
2308 RTLIB::Libcall CallI128) {
2309 RTLIB::Libcall LC;
2310 switch (Node->getSimpleValueType(0).SimpleTy) {
2311 default:
2312 llvm_unreachable("Unexpected request for libcall!");
2313 case MVT::i32:
2314 LC = CallI32;
2315 break;
2316 case MVT::i64:
2317 LC = CallI64;
2318 break;
2319 case MVT::i128:
2320 LC = CallI128;
2321 break;
2322 }
2323
2324 // Bit-counting libcalls have one unsigned argument and return `int`.
2325 // Note that `int` may be illegal on this target; ExpandLibCall will
2326 // take care of promoting it to a legal type.
2327 SDValue Op = Node->getOperand(0);
2328 EVT IntVT =
2330
2331 EVT ArgVT = Op.getValueType();
2332 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2333 TargetLowering::ArgListEntry Arg(Op, ArgTy);
2334 Arg.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgTy, /*IsSigned=*/false);
2335 Arg.IsZExt = !Arg.IsSExt;
2336
2337 SDValue Res = ExpandLibCall(LC, Node, TargetLowering::ArgListTy{Arg},
2338 /*IsSigned=*/true, IntVT)
2339 .first;
2340
2341 // If ExpandLibCall created a tail call, the result was already
2342 // of the correct type. Otherwise, we need to sign extend it.
2343 if (Res.getValueType() != MVT::Other)
2344 Res = DAG.getSExtOrTrunc(Res, SDLoc(Node), Node->getValueType(0));
2345 return Res;
2346}
2347
2348/// Issue libcalls to __{u}divmod to compute div / rem pairs.
2349void
2350SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2351 SmallVectorImpl<SDValue> &Results) {
2352 unsigned Opcode = Node->getOpcode();
2353 bool isSigned = Opcode == ISD::SDIVREM;
2354
2355 RTLIB::Libcall LC;
2356 switch (Node->getSimpleValueType(0).SimpleTy) {
2357 default: llvm_unreachable("Unexpected request for libcall!");
2358 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2359 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2360 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2361 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2362 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2363 }
2364
2365 // The input chain to this libcall is the entry node of the function.
2366 // Legalizing the call will automatically add the previous call to the
2367 // dependence.
2368 SDValue InChain = DAG.getEntryNode();
2369
2370 EVT RetVT = Node->getValueType(0);
2371 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2372
2373 TargetLowering::ArgListTy Args;
2374 for (const SDValue &Op : Node->op_values()) {
2375 EVT ArgVT = Op.getValueType();
2376 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2377 TargetLowering::ArgListEntry Entry(Op, ArgTy);
2378 Entry.IsSExt = isSigned;
2379 Entry.IsZExt = !isSigned;
2380 Args.push_back(Entry);
2381 }
2382
2383 // Also pass the return address of the remainder.
2384 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2385 TargetLowering::ArgListEntry Entry(
2386 FIPtr, PointerType::getUnqual(RetTy->getContext()));
2387 Entry.IsSExt = isSigned;
2388 Entry.IsZExt = !isSigned;
2389 Args.push_back(Entry);
2390
2391 RTLIB::LibcallImpl LibcallImpl = DAG.getLibcalls().getLibcallImpl(LC);
2392 if (LibcallImpl == RTLIB::Unsupported) {
2393 DAG.getContext()->emitError(Twine("no libcall available for ") +
2394 Node->getOperationName(&DAG));
2395 SDValue Poison = DAG.getPOISON(RetVT);
2396 Results.push_back(Poison);
2397 Results.push_back(Poison);
2398 return;
2399 }
2400
2401 SDValue Callee =
2402 DAG.getExternalSymbol(LibcallImpl, TLI.getPointerTy(DAG.getDataLayout()));
2403
2404 SDLoc dl(Node);
2405 TargetLowering::CallLoweringInfo CLI(DAG);
2406 CLI.setDebugLoc(dl)
2407 .setChain(InChain)
2408 .setLibCallee(DAG.getLibcalls().getLibcallImplCallingConv(LibcallImpl),
2409 RetTy, Callee, std::move(Args))
2410 .setSExtResult(isSigned)
2411 .setZExtResult(!isSigned);
2412
2413 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2414
2415 // Remainder is loaded back from the stack frame.
2416 int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex();
2417 MachinePointerInfo PtrInfo =
2419
2420 SDValue Rem = DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, PtrInfo);
2421 Results.push_back(CallInfo.first);
2422 Results.push_back(Rem);
2423}
2424
2425/// Return true if sincos or __sincos_stret libcall is available.
2427 const LibcallLoweringInfo &Libcalls) {
2428 MVT::SimpleValueType VT = Node->getSimpleValueType(0).SimpleTy;
2429 return Libcalls.getLibcallImpl(RTLIB::getSINCOS(VT)) != RTLIB::Unsupported ||
2430 Libcalls.getLibcallImpl(RTLIB::getSINCOS_STRET(VT)) !=
2431 RTLIB::Unsupported;
2432}
2433
2434/// Only issue sincos libcall if both sin and cos are needed.
2435static bool useSinCos(SDNode *Node) {
2436 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2437 ? ISD::FCOS : ISD::FSIN;
2438
2439 SDValue Op0 = Node->getOperand(0);
2440 for (const SDNode *User : Op0.getNode()->users()) {
2441 if (User == Node)
2442 continue;
2443 // The other user might have been turned into sincos already.
2444 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2445 return true;
2446 }
2447 return false;
2448}
2449
2450SDValue SelectionDAGLegalize::ExpandSincosStretLibCall(SDNode *Node) const {
2451 // For iOS, we want to call an alternative entry point: __sincos_stret,
2452 // which returns the values in two S / D registers.
2453 SDLoc dl(Node);
2454 SDValue Arg = Node->getOperand(0);
2455 EVT ArgVT = Arg.getValueType();
2456 RTLIB::Libcall LC = RTLIB::getSINCOS_STRET(ArgVT);
2457 RTLIB::LibcallImpl SincosStret = DAG.getLibcalls().getLibcallImpl(LC);
2458 if (SincosStret == RTLIB::Unsupported)
2459 return SDValue();
2460
2461 /// There are 3 different ABI cases to handle:
2462 /// - Direct return of separate fields in registers
2463 /// - Single return as vector elements
2464 /// - sret struct
2465
2466 const RTLIB::RuntimeLibcallsInfo &CallsInfo = TLI.getRuntimeLibcallsInfo();
2467
2468 const DataLayout &DL = DAG.getDataLayout();
2469
2470 auto [FuncTy, FuncAttrs] = CallsInfo.getFunctionTy(
2471 *DAG.getContext(), TM.getTargetTriple(), DL, SincosStret);
2472
2473 Type *SincosStretRetTy = FuncTy->getReturnType();
2474 CallingConv::ID CallConv = CallsInfo.getLibcallImplCallingConv(SincosStret);
2475
2476 SDValue Callee =
2477 DAG.getExternalSymbol(SincosStret, TLI.getProgramPointerTy(DL));
2478
2479 TargetLowering::ArgListTy Args;
2480 SDValue SRet;
2481
2482 int FrameIdx;
2483 if (FuncTy->getParamType(0)->isPointerTy()) {
2484 // Uses sret
2485 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
2486
2487 AttributeSet PtrAttrs = FuncAttrs.getParamAttrs(0);
2488 Type *StructTy = PtrAttrs.getStructRetType();
2489 const uint64_t ByteSize = DL.getTypeAllocSize(StructTy);
2490 const Align StackAlign = DL.getPrefTypeAlign(StructTy);
2491
2492 FrameIdx = MFI.CreateStackObject(ByteSize, StackAlign, false);
2493 SRet = DAG.getFrameIndex(FrameIdx, TLI.getFrameIndexTy(DL));
2494
2495 TargetLowering::ArgListEntry Entry(SRet, FuncTy->getParamType(0));
2496 Entry.IsSRet = true;
2497 Entry.IndirectType = StructTy;
2498 Entry.Alignment = StackAlign;
2499
2500 Args.push_back(Entry);
2501 Args.emplace_back(Arg, FuncTy->getParamType(1));
2502 } else {
2503 Args.emplace_back(Arg, FuncTy->getParamType(0));
2504 }
2505
2506 TargetLowering::CallLoweringInfo CLI(DAG);
2507 CLI.setDebugLoc(dl)
2508 .setChain(DAG.getEntryNode())
2509 .setLibCallee(CallConv, SincosStretRetTy, Callee, std::move(Args))
2510 .setIsPostTypeLegalization();
2511
2512 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
2513
2514 if (SRet) {
2515 MachinePointerInfo PtrInfo =
2517 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet, PtrInfo);
2518
2519 TypeSize StoreSize = ArgVT.getStoreSize();
2520
2521 // Address of cos field.
2522 SDValue Add = DAG.getObjectPtrOffset(dl, SRet, StoreSize);
2523 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
2524 PtrInfo.getWithOffset(StoreSize));
2525
2526 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
2527 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, LoadSin.getValue(0),
2528 LoadCos.getValue(0));
2529 }
2530
2531 if (!CallResult.first.getValueType().isVector())
2532 return CallResult.first;
2533
2534 SDValue SinVal =
2535 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT, CallResult.first,
2536 DAG.getVectorIdxConstant(0, dl));
2537 SDValue CosVal =
2538 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT, CallResult.first,
2539 DAG.getVectorIdxConstant(1, dl));
2540 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
2541 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal);
2542}
2543
2544SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
2545 SDLoc dl(Node);
2546 EVT VT = Node->getValueType(0);
2547 SDValue X = Node->getOperand(0);
2548 SDValue N = Node->getOperand(1);
2549 EVT ExpVT = N.getValueType();
2550 EVT AsIntVT = VT.changeTypeToInteger();
2551 if (AsIntVT == EVT()) // TODO: How to handle f80?
2552 return SDValue();
2553
2554 if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
2555 return SDValue();
2556
2557 SDNodeFlags NSW;
2558 NSW.setNoSignedWrap(true);
2559 SDNodeFlags NUW_NSW;
2560 NUW_NSW.setNoUnsignedWrap(true);
2561 NUW_NSW.setNoSignedWrap(true);
2562
2563 EVT SetCCVT =
2564 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
2565 const fltSemantics &FltSem = VT.getFltSemantics();
2566
2567 const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
2568 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2569 const int Precision = APFloat::semanticsPrecision(FltSem);
2570
2571 const SDValue MaxExp = DAG.getSignedConstant(MaxExpVal, dl, ExpVT);
2572 const SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
2573
2574 const SDValue DoubleMaxExp = DAG.getSignedConstant(2 * MaxExpVal, dl, ExpVT);
2575
2576 const APFloat One(FltSem, "1.0");
2577 APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
2578
2579 // Offset by precision to avoid denormal range.
2580 APFloat ScaleDownK =
2581 scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
2582
2583 // TODO: Should really introduce control flow and use a block for the >
2584 // MaxExp, < MinExp cases
2585
2586 // First, handle exponents Exp > MaxExp and scale down.
2587 SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
2588
2589 SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
2590 SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
2591 SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
2592 SDValue DecN1 =
2593 DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
2594
2595 SDValue ScaleUpTwice =
2596 DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
2597
2598 const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
2599 SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
2600 SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
2601
2602 SDValue SelectN_Big =
2603 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
2604 SDValue SelectX_Big =
2605 DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
2606
2607 // Now handle exponents Exp < MinExp
2608 SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
2609
2610 SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
2611 SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
2612
2613 SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
2614
2615 SDValue ClampMinVal =
2616 DAG.getSignedConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
2617 SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
2618 SDValue IncN1 =
2619 DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
2620
2621 const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
2622 SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
2623 SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
2624
2625 SDValue ScaleDownTwice = DAG.getSetCC(
2626 dl, SetCCVT, N,
2627 DAG.getSignedConstant(2 * MinExpVal + Precision, dl, ExpVT), ISD::SETULT);
2628
2629 SDValue SelectN_Small =
2630 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
2631 SDValue SelectX_Small =
2632 DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
2633
2634 // Now combine the two out of range exponent handling cases with the base
2635 // case.
2636 SDValue NewX = DAG.getNode(
2637 ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
2638 DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
2639
2640 SDValue NewN = DAG.getNode(
2641 ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
2642 DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
2643
2644 SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
2645
2646 SDValue ExponentShiftAmt =
2647 DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
2648 SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
2649
2650 SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
2651 ExponentShiftAmt, NUW_NSW);
2652 SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
2653 return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
2654}
2655
2656SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
2657 SDLoc dl(Node);
2658 SDValue Val = Node->getOperand(0);
2659 EVT VT = Val.getValueType();
2660 EVT ExpVT = Node->getValueType(1);
2661 EVT AsIntVT = VT.changeTypeToInteger();
2662 if (AsIntVT == EVT()) // TODO: How to handle f80?
2663 return SDValue();
2664
2665 const fltSemantics &FltSem = VT.getFltSemantics();
2666 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2667 const unsigned Precision = APFloat::semanticsPrecision(FltSem);
2668 const unsigned BitSize = VT.getScalarSizeInBits();
2669
2670 // TODO: Could introduce control flow and skip over the denormal handling.
2671
2672 // scale_up = fmul value, scalbn(1.0, precision + 1)
2673 // extracted_exp = (bitcast value to uint) >> precision - 1
2674 // biased_exp = extracted_exp + min_exp
2675 // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
2676 //
2677 // is_denormal = val < smallest_normalized
2678 // computed_fract = is_denormal ? scale_up : extracted_fract
2679 // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
2680 //
2681 // result_0 = (!isfinite(val) || iszero(val)) ? val : computed_fract
2682 // result_1 = (!isfinite(val) || iszero(val)) ? 0 : computed_exp
2683
2684 SDValue NegSmallestNormalizedInt = DAG.getConstant(
2685 APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
2686 AsIntVT);
2687
2688 SDValue SmallestNormalizedInt = DAG.getConstant(
2689 APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
2690 AsIntVT);
2691
2692 // Masks out the exponent bits.
2693 SDValue ExpMask =
2694 DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
2695
2696 // Mask out the exponent part of the value.
2697 //
2698 // e.g, for f32 FractSignMaskVal = 0x807fffff
2699 APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
2700 FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
2701
2702 APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
2703 SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
2704
2705 SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
2706
2707 const APFloat One(FltSem, "1.0");
2708 // Scale a possible denormal input.
2709 // e.g., for f64, 0x1p+54
2710 APFloat ScaleUpKVal =
2711 scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
2712
2713 SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
2714 SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
2715
2716 EVT SetCCVT =
2717 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2718
2719 SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
2720
2721 SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
2722
2723 SDValue AddNegSmallestNormal =
2724 DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
2725 SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
2726 NegSmallestNormalizedInt, ISD::SETULE);
2727
2728 SDValue IsDenormal =
2729 DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
2730
2731 SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
2732 SDValue Zero = DAG.getConstant(0, dl, ExpVT);
2733
2734 SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
2735 SDValue ScaledSelect =
2736 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
2737
2738 SDValue ExpMaskScaled =
2739 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
2740
2741 SDValue ScaledValue =
2742 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
2743
2744 // Extract the exponent bits.
2745 SDValue ExponentShiftAmt =
2746 DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
2747 SDValue ShiftedExp =
2748 DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
2749 SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
2750
2751 SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
2752 SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
2753 SDValue DenormalExpBias =
2754 DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
2755
2756 SDValue MaskedFractAsInt =
2757 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
2758 const APFloat Half(FltSem, "0.5");
2759 SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
2760 SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
2761 SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
2762
2763 SDValue ComputedExp =
2764 DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
2765
2766 SDValue Result0 =
2767 DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
2768
2769 SDValue Result1 =
2770 DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
2771
2772 return DAG.getMergeValues({Result0, Result1}, dl);
2773}
2774
2775SDValue SelectionDAGLegalize::expandModf(SDNode *Node) const {
2776 SDLoc dl(Node);
2777 SDValue Val = Node->getOperand(0);
2778 EVT VT = Val.getValueType();
2779 SDNodeFlags Flags = Node->getFlags();
2780
2781 SDValue IntPart = DAG.getNode(ISD::FTRUNC, dl, VT, Val, Flags);
2782 SDValue FracPart = DAG.getNode(ISD::FSUB, dl, VT, Val, IntPart, Flags);
2783
2784 SDValue FracToUse;
2785 if (Flags.hasNoInfs()) {
2786 FracToUse = FracPart;
2787 } else {
2788 SDValue Abs = DAG.getNode(ISD::FABS, dl, VT, Val, Flags);
2789 SDValue Inf =
2791 EVT SetCCVT =
2792 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2793 SDValue IsInf = DAG.getSetCC(dl, SetCCVT, Abs, Inf, ISD::SETOEQ);
2794 SDValue Zero = DAG.getConstantFP(0.0, dl, VT);
2795 FracToUse = DAG.getSelect(dl, VT, IsInf, Zero, FracPart);
2796 }
2797
2798 SDValue ResultFrac =
2799 DAG.getNode(ISD::FCOPYSIGN, dl, VT, FracToUse, Val, Flags);
2800 return DAG.getMergeValues({ResultFrac, IntPart}, dl);
2801}
2802
2803/// This function is responsible for legalizing a
2804/// INT_TO_FP operation of the specified operand when the target requests that
2805/// we expand it. At this point, we know that the result and operand types are
2806/// legal for the target.
2807SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2808 SDValue &Chain) {
2809 bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2810 Node->getOpcode() == ISD::SINT_TO_FP);
2811 EVT DestVT = Node->getValueType(0);
2812 SDLoc dl(Node);
2813 unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2814 SDValue Op0 = Node->getOperand(OpNo);
2815 EVT SrcVT = Op0.getValueType();
2816
2817 // TODO: Should any fast-math-flags be set for the created nodes?
2818 LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2819 if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2820 (DestVT.bitsLE(MVT::f64) ||
2821 TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2823 DestVT))) {
2824 LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
2825 "expansion\n");
2826
2827 // Get the stack frame index of a 8 byte buffer.
2828 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
2829
2830 SDValue Lo = Op0;
2831 // if signed map to unsigned space
2832 if (isSigned) {
2833 // Invert sign bit (signed to unsigned mapping).
2834 Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
2835 DAG.getConstant(0x80000000u, dl, MVT::i32));
2836 }
2837 // Initial hi portion of constructed double.
2838 SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
2839
2840 // If this a big endian target, swap the lo and high data.
2841 if (DAG.getDataLayout().isBigEndian())
2842 std::swap(Lo, Hi);
2843
2844 SDValue MemChain = DAG.getEntryNode();
2845
2846 // Store the lo of the constructed double.
2847 SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
2848 MachinePointerInfo());
2849 // Store the hi of the constructed double.
2850 SDValue HiPtr =
2851 DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl);
2852 SDValue Store2 =
2853 DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
2854 MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
2855
2856 // load the constructed double
2857 SDValue Load =
2858 DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
2859 // FP constant to bias correct the final result
2860 SDValue Bias = DAG.getConstantFP(
2861 isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
2862 : llvm::bit_cast<double>(0x4330000000000000ULL),
2863 dl, MVT::f64);
2864 // Subtract the bias and get the final result.
2865 SDValue Sub;
2867 if (Node->isStrictFPOpcode()) {
2868 Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2869 {Node->getOperand(0), Load, Bias});
2870 Chain = Sub.getValue(1);
2871 if (DestVT != Sub.getValueType()) {
2872 std::pair<SDValue, SDValue> ResultPair;
2873 ResultPair =
2874 DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2875 Result = ResultPair.first;
2876 Chain = ResultPair.second;
2877 }
2878 else
2879 Result = Sub;
2880 } else {
2881 Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2882 Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2883 }
2884 return Result;
2885 }
2886
2887 if (isSigned)
2888 return SDValue();
2889
2890 // TODO: Generalize this for use with other types.
2891 if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2892 (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2893 LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
2894 // For unsigned conversions, convert them to signed conversions using the
2895 // algorithm from the x86_64 __floatundisf in compiler_rt. That method
2896 // should be valid for i32->f32 as well.
2897
2898 // More generally this transform should be valid if there are 3 more bits
2899 // in the integer type than the significand. Rounding uses the first bit
2900 // after the width of the significand and the OR of all bits after that. So
2901 // we need to be able to OR the shifted out bit into one of the bits that
2902 // participate in the OR.
2903
2904 // TODO: This really should be implemented using a branch rather than a
2905 // select. We happen to get lucky and machinesink does the right
2906 // thing most of the time. This would be a good candidate for a
2907 // pseudo-op, or, even better, for whole-function isel.
2908 EVT SetCCVT = getSetCCResultType(SrcVT);
2909
2910 SDValue SignBitTest = DAG.getSetCC(
2911 dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2912
2913 SDValue ShiftConst = DAG.getShiftAmountConstant(1, SrcVT, dl);
2914 SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
2915 SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
2916 SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
2917 SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
2918
2919 SDValue Slow, Fast;
2920 if (Node->isStrictFPOpcode()) {
2921 // In strict mode, we must avoid spurious exceptions, and therefore
2922 // must make sure to only emit a single STRICT_SINT_TO_FP.
2923 SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
2924 // The STRICT_SINT_TO_FP inherits the exception mode from the
2925 // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
2926 // never raise any exception.
2927 SDNodeFlags Flags;
2928 Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
2929 Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {DestVT, MVT::Other},
2930 {Node->getOperand(0), InCvt}, Flags);
2931 Flags.setNoFPExcept(true);
2932 Slow = DAG.getNode(ISD::STRICT_FADD, dl, {DestVT, MVT::Other},
2933 {Fast.getValue(1), Fast, Fast}, Flags);
2934 Chain = Slow.getValue(1);
2935 } else {
2936 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
2937 Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
2938 Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2939 }
2940
2941 return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
2942 }
2943
2944 // Don't expand it if there isn't cheap fadd.
2945 if (!TLI.isOperationLegalOrCustom(
2946 Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2947 return SDValue();
2948
2949 // The following optimization is valid only if every value in SrcVT (when
2950 // treated as signed) is representable in DestVT. Check that the mantissa
2951 // size of DestVT is >= than the number of bits in SrcVT -1.
2952 assert(APFloat::semanticsPrecision(DestVT.getFltSemantics()) >=
2953 SrcVT.getSizeInBits() - 1 &&
2954 "Cannot perform lossless SINT_TO_FP!");
2955
2956 SDValue Tmp1;
2957 if (Node->isStrictFPOpcode()) {
2958 Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2959 { Node->getOperand(0), Op0 });
2960 } else
2961 Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2962
2963 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
2964 DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2965 SDValue Zero = DAG.getIntPtrConstant(0, dl),
2966 Four = DAG.getIntPtrConstant(4, dl);
2967 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
2968 SignSet, Four, Zero);
2969
2970 // If the sign bit of the integer is set, the large number will be treated
2971 // as a negative number. To counteract this, the dynamic code adds an
2972 // offset depending on the data type.
2973 uint64_t FF;
2974 switch (SrcVT.getSimpleVT().SimpleTy) {
2975 default:
2976 return SDValue();
2977 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2978 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2979 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2980 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2981 }
2982 if (DAG.getDataLayout().isLittleEndian())
2983 FF <<= 32;
2984 Constant *FudgeFactor = ConstantInt::get(
2985 Type::getInt64Ty(*DAG.getContext()), FF);
2986
2987 SDValue CPIdx =
2988 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
2989 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2990 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
2991 Alignment = commonAlignment(Alignment, 4);
2992 SDValue FudgeInReg;
2993 if (DestVT == MVT::f32)
2994 FudgeInReg = DAG.getLoad(
2995 MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2997 Alignment);
2998 else {
2999 SDValue Load = DAG.getExtLoad(
3000 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
3002 Alignment);
3003 HandleSDNode Handle(Load);
3004 LegalizeOp(Load.getNode());
3005 FudgeInReg = Handle.getValue();
3006 }
3007
3008 if (Node->isStrictFPOpcode()) {
3009 SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
3010 { Tmp1.getValue(1), Tmp1, FudgeInReg });
3011 Chain = Result.getValue(1);
3012 return Result;
3013 }
3014
3015 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
3016}
3017
3018/// This function is responsible for legalizing a
3019/// *INT_TO_FP operation of the specified operand when the target requests that
3020/// we promote it. At this point, we know that the result and operand types are
3021/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
3022/// operation that takes a larger input.
3023void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
3024 SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) {
3025 bool IsStrict = N->isStrictFPOpcode();
3026 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
3027 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
3028 EVT DestVT = N->getValueType(0);
3029 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
3030 unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
3031 unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
3032
3033 // First step, figure out the appropriate *INT_TO_FP operation to use.
3034 EVT NewInTy = LegalOp.getValueType();
3035
3036 unsigned OpToUse = 0;
3037
3038 // Scan for the appropriate larger type to use.
3039 while (true) {
3040 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
3041 assert(NewInTy.isInteger() && "Ran out of possibilities!");
3042
3043 // If the target supports SINT_TO_FP of this type, use it.
3044 if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
3045 OpToUse = SIntOp;
3046 break;
3047 }
3048 if (IsSigned)
3049 continue;
3050
3051 // If the target supports UINT_TO_FP of this type, use it.
3052 if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
3053 OpToUse = UIntOp;
3054 break;
3055 }
3056
3057 // Otherwise, try a larger type.
3058 }
3059
3060 // Okay, we found the operation and type to use. Zero extend our input to the
3061 // desired type then run the operation on it.
3062 if (IsStrict) {
3063 SDValue Res =
3064 DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
3065 {N->getOperand(0),
3066 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3067 dl, NewInTy, LegalOp)});
3068 Results.push_back(Res);
3069 Results.push_back(Res.getValue(1));
3070 return;
3071 }
3072
3073 Results.push_back(
3074 DAG.getNode(OpToUse, dl, DestVT,
3075 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
3076 dl, NewInTy, LegalOp)));
3077}
3078
3079/// This function is responsible for legalizing a
3080/// FP_TO_*INT operation of the specified operand when the target requests that
3081/// we promote it. At this point, we know that the result and operand types are
3082/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
3083/// operation that returns a larger result.
3084void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
3085 SmallVectorImpl<SDValue> &Results) {
3086 bool IsStrict = N->isStrictFPOpcode();
3087 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
3088 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
3089 EVT DestVT = N->getValueType(0);
3090 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
3091 // First step, figure out the appropriate FP_TO*INT operation to use.
3092 EVT NewOutTy = DestVT;
3093
3094 unsigned OpToUse = 0;
3095
3096 // Scan for the appropriate larger type to use.
3097 while (true) {
3098 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
3099 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
3100
3101 // A larger signed type can hold all unsigned values of the requested type,
3102 // so using FP_TO_SINT is valid
3103 OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
3104 if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
3105 break;
3106
3107 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
3108 OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
3109 if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
3110 break;
3111
3112 // Otherwise, try a larger type.
3113 }
3114
3115 // Okay, we found the operation and type to use.
3117 if (IsStrict) {
3118 SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
3119 Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
3120 } else
3121 Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
3122
3123 // Truncate the result of the extended FP_TO_*INT operation to the desired
3124 // size.
3125 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
3126 Results.push_back(Trunc);
3127 if (IsStrict)
3128 Results.push_back(Operation.getValue(1));
3129}
3130
3131/// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
3132/// the result and operand types are legal and there must be a legal
3133/// FP_TO_*INT_SAT operation for a larger result type.
3134SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
3135 const SDLoc &dl) {
3136 unsigned Opcode = Node->getOpcode();
3137
3138 // Scan for the appropriate larger type to use.
3139 EVT NewOutTy = Node->getValueType(0);
3140 while (true) {
3141 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
3142 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
3143
3144 if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
3145 break;
3146 }
3147
3148 // Saturation width is determined by second operand, so we don't have to
3149 // perform any fixup and can directly truncate the result.
3150 SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
3151 Node->getOperand(1));
3152 return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
3153}
3154
3155/// Open code the operations for PARITY of the specified operation.
3156SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
3157 EVT VT = Op.getValueType();
3158 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3159 unsigned Sz = VT.getScalarSizeInBits();
3160
3161 // If CTPOP is legal, use it. Otherwise use shifts and xor.
3164 Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
3165 } else {
3166 Result = Op;
3167 for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
3168 SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
3169 DAG.getConstant(1ULL << (--i), dl, ShVT));
3170 Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
3171 }
3172 }
3173
3174 return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
3175}
3176
3177SDValue SelectionDAGLegalize::PromoteReduction(SDNode *Node) {
3178 bool IsVPOpcode = ISD::isVPOpcode(Node->getOpcode());
3179 MVT VecVT = IsVPOpcode ? Node->getOperand(1).getSimpleValueType()
3180 : Node->getOperand(0).getSimpleValueType();
3181 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
3182 MVT ScalarVT = Node->getSimpleValueType(0);
3183 MVT NewScalarVT = NewVecVT.getVectorElementType();
3184
3185 SDLoc DL(Node);
3186 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
3187
3188 // FIXME: Support integer.
3189 assert(Node->getOperand(0).getValueType().isFloatingPoint() &&
3190 "Only FP promotion is supported");
3191
3192 for (unsigned j = 0; j != Node->getNumOperands(); ++j)
3193 if (Node->getOperand(j).getValueType().isVector() &&
3194 !(IsVPOpcode &&
3195 ISD::getVPMaskIdx(Node->getOpcode()) == j)) { // Skip mask operand.
3196 // promote the vector operand.
3197 // FIXME: Support integer.
3198 assert(Node->getOperand(j).getValueType().isFloatingPoint() &&
3199 "Only FP promotion is supported");
3200 Operands[j] =
3201 DAG.getNode(ISD::FP_EXTEND, DL, NewVecVT, Node->getOperand(j));
3202 } else if (Node->getOperand(j).getValueType().isFloatingPoint()) {
3203 // promote the initial value.
3204 Operands[j] =
3205 DAG.getNode(ISD::FP_EXTEND, DL, NewScalarVT, Node->getOperand(j));
3206 } else {
3207 Operands[j] = Node->getOperand(j); // Skip VL operand.
3208 }
3209
3210 SDValue Res = DAG.getNode(Node->getOpcode(), DL, NewScalarVT, Operands,
3211 Node->getFlags());
3212
3213 assert(ScalarVT.isFloatingPoint() && "Only FP promotion is supported");
3214 return DAG.getNode(ISD::FP_ROUND, DL, ScalarVT, Res,
3215 DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
3216}
3217
3218bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
3219 LLVM_DEBUG(dbgs() << "Trying to expand node\n");
3221 SDLoc dl(Node);
3222 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3223 bool NeedInvert;
3224 switch (Node->getOpcode()) {
3225 case ISD::ABS:
3226 if ((Tmp1 = TLI.expandABS(Node, DAG)))
3227 Results.push_back(Tmp1);
3228 break;
3229 case ISD::ABDS:
3230 case ISD::ABDU:
3231 if ((Tmp1 = TLI.expandABD(Node, DAG)))
3232 Results.push_back(Tmp1);
3233 break;
3234 case ISD::AVGCEILS:
3235 case ISD::AVGCEILU:
3236 case ISD::AVGFLOORS:
3237 case ISD::AVGFLOORU:
3238 if ((Tmp1 = TLI.expandAVG(Node, DAG)))
3239 Results.push_back(Tmp1);
3240 break;
3241 case ISD::CTPOP:
3242 if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
3243 Results.push_back(Tmp1);
3244 break;
3245 case ISD::CTLZ:
3247 if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
3248 Results.push_back(Tmp1);
3249 break;
3250 case ISD::CTLS:
3251 if ((Tmp1 = TLI.expandCTLS(Node, DAG)))
3252 Results.push_back(Tmp1);
3253 break;
3254 case ISD::CTTZ:
3256 if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
3257 Results.push_back(Tmp1);
3258 break;
3259 case ISD::BITREVERSE:
3260 if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
3261 Results.push_back(Tmp1);
3262 break;
3263 case ISD::BSWAP:
3264 if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
3265 Results.push_back(Tmp1);
3266 break;
3267 case ISD::PARITY:
3268 Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
3269 break;
3270 case ISD::FRAMEADDR:
3271 case ISD::RETURNADDR:
3273 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3274 break;
3275 case ISD::EH_DWARF_CFA: {
3276 SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
3277 TLI.getPointerTy(DAG.getDataLayout()));
3278 SDValue Offset = DAG.getNode(ISD::ADD, dl,
3279 CfaArg.getValueType(),
3281 CfaArg.getValueType()),
3282 CfaArg);
3283 SDValue FA = DAG.getNode(
3285 DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
3286 Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
3287 FA, Offset));
3288 break;
3289 }
3290 case ISD::GET_ROUNDING:
3291 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
3292 Results.push_back(Node->getOperand(0));
3293 break;
3294 case ISD::EH_RETURN:
3295 case ISD::EH_LABEL:
3296 case ISD::PREFETCH:
3297 case ISD::VAEND:
3299 // If the target didn't expand these, there's nothing to do, so just
3300 // preserve the chain and be done.
3301 Results.push_back(Node->getOperand(0));
3302 break;
3305 // If the target didn't expand this, just return 'zero' and preserve the
3306 // chain.
3307 Results.append(Node->getNumValues() - 1,
3308 DAG.getConstant(0, dl, Node->getValueType(0)));
3309 Results.push_back(Node->getOperand(0));
3310 break;
3312 // If the target didn't expand this, just return 'zero' and preserve the
3313 // chain.
3314 Results.push_back(DAG.getConstant(0, dl, MVT::i32));
3315 Results.push_back(Node->getOperand(0));
3316 break;
3317 case ISD::ATOMIC_LOAD: {
3318 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
3319 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
3320 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3321 SDValue Swap = DAG.getAtomicCmpSwap(
3322 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3323 Node->getOperand(0), Node->getOperand(1), Zero, Zero,
3324 cast<AtomicSDNode>(Node)->getMemOperand());
3325 Results.push_back(Swap.getValue(0));
3326 Results.push_back(Swap.getValue(1));
3327 break;
3328 }
3329 case ISD::ATOMIC_STORE: {
3330 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3331 SDValue Swap = DAG.getAtomic(
3332 ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(),
3333 Node->getOperand(0), Node->getOperand(2), Node->getOperand(1),
3334 cast<AtomicSDNode>(Node)->getMemOperand());
3335 Results.push_back(Swap.getValue(1));
3336 break;
3337 }
3339 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
3340 // splits out the success value as a comparison. Expanding the resulting
3341 // ATOMIC_CMP_SWAP will produce a libcall.
3342 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3343 SDValue Res = DAG.getAtomicCmpSwap(
3344 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3345 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
3346 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
3347
3348 SDValue ExtRes = Res;
3349 SDValue LHS = Res;
3350 SDValue RHS = Node->getOperand(1);
3351
3352 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
3353 EVT OuterType = Node->getValueType(0);
3354 switch (TLI.getExtendForAtomicOps()) {
3355 case ISD::SIGN_EXTEND:
3356 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
3357 DAG.getValueType(AtomicType));
3358 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
3359 Node->getOperand(2), DAG.getValueType(AtomicType));
3360 ExtRes = LHS;
3361 break;
3362 case ISD::ZERO_EXTEND:
3363 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
3364 DAG.getValueType(AtomicType));
3365 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3366 ExtRes = LHS;
3367 break;
3368 case ISD::ANY_EXTEND:
3369 LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
3370 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3371 break;
3372 default:
3373 llvm_unreachable("Invalid atomic op extension");
3374 }
3375
3377 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
3378
3379 Results.push_back(ExtRes.getValue(0));
3380 Results.push_back(Success);
3381 Results.push_back(Res.getValue(1));
3382 break;
3383 }
3384 case ISD::ATOMIC_LOAD_SUB: {
3385 SDLoc DL(Node);
3386 EVT VT = Node->getValueType(0);
3387 SDValue RHS = Node->getOperand(2);
3388 AtomicSDNode *AN = cast<AtomicSDNode>(Node);
3389 if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG &&
3390 cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT())
3391 RHS = RHS->getOperand(0);
3392 SDValue NewRHS =
3393 DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS);
3395 Node->getOperand(0), Node->getOperand(1),
3396 NewRHS, AN->getMemOperand());
3397 Results.push_back(Res);
3398 Results.push_back(Res.getValue(1));
3399 break;
3400 }
3402 ExpandDYNAMIC_STACKALLOC(Node, Results);
3403 break;
3404 case ISD::MERGE_VALUES:
3405 for (unsigned i = 0; i < Node->getNumValues(); i++)
3406 Results.push_back(Node->getOperand(i));
3407 break;
3408 case ISD::POISON:
3409 case ISD::UNDEF: {
3410 EVT VT = Node->getValueType(0);
3411 if (VT.isInteger())
3412 Results.push_back(DAG.getConstant(0, dl, VT));
3413 else {
3414 assert(VT.isFloatingPoint() && "Unknown value type!");
3415 Results.push_back(DAG.getConstantFP(0, dl, VT));
3416 }
3417 break;
3418 }
3420 // When strict mode is enforced we can't do expansion because it
3421 // does not honor the "strict" properties. Only libcall is allowed.
3422 if (TLI.isStrictFPEnabled())
3423 break;
3424 // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3425 // since this operation is more efficient than stack operation.
3426 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3427 Node->getValueType(0))
3428 == TargetLowering::Legal)
3429 break;
3430 // We fall back to use stack operation when the FP_ROUND operation
3431 // isn't available.
3432 if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3433 Node->getValueType(0), dl,
3434 Node->getOperand(0)))) {
3435 ReplaceNode(Node, Tmp1.getNode());
3436 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
3437 return true;
3438 }
3439 break;
3440 case ISD::FP_ROUND: {
3441 if ((Tmp1 = TLI.expandFP_ROUND(Node, DAG))) {
3442 Results.push_back(Tmp1);
3443 break;
3444 }
3445
3446 [[fallthrough]];
3447 }
3448 case ISD::BITCAST:
3449 if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3450 Node->getValueType(0), dl)))
3451 Results.push_back(Tmp1);
3452 break;
3454 // When strict mode is enforced we can't do expansion because it
3455 // does not honor the "strict" properties. Only libcall is allowed.
3456 if (TLI.isStrictFPEnabled())
3457 break;
3458 // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3459 // since this operation is more efficient than stack operation.
3460 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3461 Node->getValueType(0))
3462 == TargetLowering::Legal)
3463 break;
3464 // We fall back to use stack operation when the FP_EXTEND operation
3465 // isn't available.
3466 if ((Tmp1 = EmitStackConvert(
3467 Node->getOperand(1), Node->getOperand(1).getValueType(),
3468 Node->getValueType(0), dl, Node->getOperand(0)))) {
3469 ReplaceNode(Node, Tmp1.getNode());
3470 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
3471 return true;
3472 }
3473 break;
3474 case ISD::FP_EXTEND: {
3475 SDValue Op = Node->getOperand(0);
3476 EVT SrcVT = Op.getValueType();
3477 EVT DstVT = Node->getValueType(0);
3478 if (SrcVT.getScalarType() == MVT::bf16) {
3479 Results.push_back(DAG.getNode(ISD::BF16_TO_FP, SDLoc(Node), DstVT, Op));
3480 break;
3481 }
3482
3483 if ((Tmp1 = EmitStackConvert(Op, SrcVT, DstVT, dl)))
3484 Results.push_back(Tmp1);
3485 break;
3486 }
3487 case ISD::BF16_TO_FP: {
3488 // Always expand bf16 to f32 casts, they lower to ext + shift.
3489 //
3490 // Note that the operand of this code can be bf16 or an integer type in case
3491 // bf16 is not supported on the target and was softened.
3492 SDValue Op = Node->getOperand(0);
3493 if (Op.getValueType() == MVT::bf16) {
3494 Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3495 DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3496 } else {
3497 Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3498 }
3499 Op = DAG.getNode(ISD::SHL, dl, MVT::i32, Op,
3500 DAG.getShiftAmountConstant(16, MVT::i32, dl));
3501 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3502 // Add fp_extend in case the output is bigger than f32.
3503 if (Node->getValueType(0) != MVT::f32)
3504 Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3505 Results.push_back(Op);
3506 break;
3507 }
3508 case ISD::FP_TO_BF16: {
3509 SDValue Op = Node->getOperand(0);
3510 if (Op.getValueType() != MVT::f32)
3511 Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3512 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3513 // Certain SNaNs will turn into infinities if we do a simple shift right.
3514 if (!DAG.isKnownNeverSNaN(Op)) {
3515 Op = DAG.getNode(ISD::FCANONICALIZE, dl, MVT::f32, Op, Node->getFlags());
3516 }
3517 Op = DAG.getNode(ISD::SRL, dl, MVT::i32,
3518 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3519 DAG.getShiftAmountConstant(16, MVT::i32, dl));
3520 // The result of this node can be bf16 or an integer type in case bf16 is
3521 // not supported on the target and was softened to i16 for storage.
3522 if (Node->getValueType(0) == MVT::bf16) {
3523 Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3524 DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3525 } else {
3526 Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3527 }
3528 Results.push_back(Op);
3529 break;
3530 }
3532 // Expand conversion from arbitrary FP format stored in an integer to a
3533 // native IEEE float type using integer bit manipulation.
3534 //
3535 // TODO: currently only conversions from FP4, FP6 and FP8 formats from OCP
3536 // specification are expanded. Remaining arbitrary FP types: Float8E4M3,
3537 // Float8E3M4, Float8E5M2FNUZ, Float8E4M3FNUZ, Float8E4M3B11FNUZ,
3538 // Float8E8M0FNU.
3539 EVT DstVT = Node->getValueType(0);
3540
3541 SDValue IntVal = Node->getOperand(0);
3542 const uint64_t SemEnum = Node->getConstantOperandVal(1);
3543 const auto Sem = static_cast<APFloatBase::Semantics>(SemEnum);
3544
3545 // Supported source formats.
3546 switch (Sem) {
3552 break;
3553 default:
3554 DAG.getContext()->emitError("CONVERT_FROM_ARBITRARY_FP: not implemented "
3555 "source format (semantics enum " +
3556 Twine(SemEnum) + ")");
3557 Results.push_back(DAG.getPOISON(DstVT));
3558 break;
3559 }
3560 if (!Results.empty())
3561 break;
3562
3563 const fltSemantics &SrcSem = APFloatBase::EnumToSemantics(Sem);
3564
3565 const unsigned SrcBits = APFloat::getSizeInBits(SrcSem);
3566 const unsigned SrcPrecision = APFloat::semanticsPrecision(SrcSem);
3567 const unsigned SrcMant = SrcPrecision - 1;
3568 const unsigned SrcExp = SrcBits - SrcMant - 1;
3569 const int SrcBias = 1 - APFloat::semanticsMinExponent(SrcSem);
3570
3571 const fltNonfiniteBehavior NFBehavior = SrcSem.nonFiniteBehavior;
3572
3573 // Destination format parameters.
3574 const fltSemantics &DstSem = DstVT.getFltSemantics();
3575
3576 const unsigned DstBits = APFloat::getSizeInBits(DstSem);
3577 const unsigned DstMant = APFloat::semanticsPrecision(DstSem) - 1;
3578 const unsigned DstExpBits = DstBits - DstMant - 1;
3579 const int DstMinExp = APFloat::semanticsMinExponent(DstSem);
3580 const int DstBias = 1 - DstMinExp;
3581 const uint64_t DstExpAllOnes = (1ULL << DstExpBits) - 1;
3582
3583 // Work in an integer type matching the destination float width.
3584 // Use zero-extend to preserve the raw bit-pattern.
3585 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), DstBits);
3586 SDValue Src = DAG.getZExtOrTrunc(IntVal, dl, IntVT);
3587
3588 EVT SetCCVT = getSetCCResultType(IntVT);
3589
3590 SDValue Zero = DAG.getConstant(0, dl, IntVT);
3591 SDValue One = DAG.getConstant(1, dl, IntVT);
3592
3593 // Extract bit fields.
3594 const uint64_t MantMask = (SrcMant > 0) ? ((1ULL << SrcMant) - 1) : 0;
3595 const uint64_t ExpMask = (1ULL << SrcExp) - 1;
3596
3597 SDValue MantField = DAG.getNode(ISD::AND, dl, IntVT, Src,
3598 DAG.getConstant(MantMask, dl, IntVT));
3599
3600 SDValue ExpField =
3601 DAG.getNode(ISD::AND, dl, IntVT,
3602 DAG.getNode(ISD::SRL, dl, IntVT, Src,
3603 DAG.getShiftAmountConstant(SrcMant, IntVT, dl)),
3604 DAG.getConstant(ExpMask, dl, IntVT));
3605
3606 SDValue SignBit =
3607 DAG.getNode(ISD::SRL, dl, IntVT, Src,
3608 DAG.getShiftAmountConstant(SrcBits - 1, IntVT, dl));
3609
3610 // Precompute sign shifted to MSB of destination.
3611 SDValue SignShifted =
3612 DAG.getNode(ISD::SHL, dl, IntVT, SignBit,
3613 DAG.getShiftAmountConstant(DstBits - 1, IntVT, dl));
3614
3615 // Classify the input value based on compile-time format properties.
3616 SDValue ExpAllOnes = DAG.getConstant(ExpMask, dl, IntVT);
3617 SDValue IsExpAllOnes =
3618 DAG.getSetCC(dl, SetCCVT, ExpField, ExpAllOnes, ISD::SETEQ);
3619 SDValue IsExpZero = DAG.getSetCC(dl, SetCCVT, ExpField, Zero, ISD::SETEQ);
3620 SDValue IsMantZero = DAG.getSetCC(dl, SetCCVT, MantField, Zero, ISD::SETEQ);
3621 SDValue IsMantNonZero =
3622 DAG.getSetCC(dl, SetCCVT, MantField, Zero, ISD::SETNE);
3623
3624 // NaN detection.
3625 SDValue IsNaN;
3626 if (NFBehavior == fltNonfiniteBehavior::FiniteOnly) {
3627 // FiniteOnly formats (E2M1FN, E3M2FN, E2M3FN) never produce NaN.
3628 IsNaN = DAG.getBoolConstant(false, dl, SetCCVT, IntVT);
3629 } else if (NFBehavior == fltNonfiniteBehavior::IEEE754) {
3630 // E5M2 produces NaN when exp == all-ones AND mantissa != 0.
3631 IsNaN = DAG.getNode(ISD::AND, dl, SetCCVT, IsExpAllOnes, IsMantNonZero);
3632 } else {
3633 // NanOnly + AllOnes (E4M3FN): NaN when all exp and mantissa bits are 1.
3634 assert(SrcSem.nanEncoding == fltNanEncoding::AllOnes);
3635 SDValue MantAllOnes = DAG.getConstant(MantMask, dl, IntVT);
3636 SDValue IsMantAllOnes =
3637 DAG.getSetCC(dl, SetCCVT, MantField, MantAllOnes, ISD::SETEQ);
3638 IsNaN = DAG.getNode(ISD::AND, dl, SetCCVT, IsExpAllOnes, IsMantAllOnes);
3639 }
3640
3641 // Inf detection.
3642 SDValue IsInf;
3643 if (NFBehavior == fltNonfiniteBehavior::IEEE754) {
3644 // E5M2: Inf when exp == all-ones AND mantissa == 0.
3645 IsInf = DAG.getNode(ISD::AND, dl, SetCCVT, IsExpAllOnes, IsMantZero);
3646 } else {
3647 // NanOnly and FiniteOnly formats have no Inf representation.
3648 IsInf = DAG.getBoolConstant(false, dl, SetCCVT, IntVT);
3649 }
3650
3651 // Zero detection.
3652 SDValue IsZero = DAG.getNode(ISD::AND, dl, SetCCVT, IsExpZero, IsMantZero);
3653
3654 // Denorm detection: exp == 0 AND mant != 0.
3655 SDValue IsDenorm =
3656 DAG.getNode(ISD::AND, dl, SetCCVT, IsExpZero, IsMantNonZero);
3657
3658 // Normal value conversion.
3659 // dst_exp = exp_field + (DstBias - SrcBias)
3660 // dst_mant = mant << (DstMant - SrcMant)
3661 const int BiasAdjust = DstBias - SrcBias;
3662 SDValue NormDstExp = DAG.getNode(
3663 ISD::ADD, dl, IntVT, ExpField,
3664 DAG.getConstant(APInt(DstBits, BiasAdjust, true), dl, IntVT));
3665
3666 SDValue NormDstMant;
3667 if (DstMant > SrcMant) {
3668 SDValue NormDstMantShift =
3669 DAG.getShiftAmountConstant(DstMant - SrcMant, IntVT, dl);
3670 NormDstMant =
3671 DAG.getNode(ISD::SHL, dl, IntVT, MantField, NormDstMantShift);
3672 } else {
3673 NormDstMant = MantField;
3674 }
3675
3676 // Assemble normal result.
3677 SDValue DstMantShift = DAG.getShiftAmountConstant(DstMant, IntVT, dl);
3678 SDValue NormExpShifted =
3679 DAG.getNode(ISD::SHL, dl, IntVT, NormDstExp, DstMantShift);
3680 SDValue NormResult = DAG.getNode(
3681 ISD::OR, dl, IntVT,
3682 DAG.getNode(ISD::OR, dl, IntVT, SignShifted, NormExpShifted),
3683 NormDstMant);
3684
3685 // Denormal value conversion.
3686 // For a denormal source (exp_field == 0, mant != 0), normalize by finding
3687 // the MSB position of mant using CTLZ, then compute the correct
3688 // exponent and mantissa for the destination format.
3689 SDValue DenormResult;
3690 {
3691 const unsigned IntVTBits = DstBits;
3692 SDValue LeadingZeros =
3693 DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, IntVT, MantField);
3694
3695 // dst_exp_denorm = (IntVTBits + DstBias - SrcBias - SrcMant) -
3696 // LeadingZeros
3697 const int DenormExpConst =
3698 (int)IntVTBits + DstBias - SrcBias - (int)SrcMant;
3699 SDValue DenormDstExp = DAG.getNode(
3700 ISD::SUB, dl, IntVT,
3701 DAG.getConstant(APInt(DstBits, DenormExpConst, true), dl, IntVT),
3702 LeadingZeros);
3703
3704 // MSB position of the mantissa (0-indexed from LSB).
3705 SDValue MantMSB =
3706 DAG.getNode(ISD::SUB, dl, IntVT,
3707 DAG.getConstant(IntVTBits - 1, dl, IntVT), LeadingZeros);
3708
3709 // leading_one = 1 << MantMSB
3710 SDValue LeadingOne = DAG.getNode(ISD::SHL, dl, IntVT, One, MantMSB);
3711
3712 // frac = mant XOR leading_one (strip the implicit 1)
3713 SDValue Frac = DAG.getNode(ISD::XOR, dl, IntVT, MantField, LeadingOne);
3714
3715 // shift_amount = DstMant - MantMSB
3716 // = DstMant - (IntVTBits - 1 - LeadingZeros)
3717 // = LeadingZeros - (IntVTBits - 1 - DstMant)
3718 const unsigned ShiftSub = IntVTBits - 1 - DstMant; // always >= 0
3719 SDValue ShiftAmount = DAG.getNode(ISD::SUB, dl, IntVT, LeadingZeros,
3720 DAG.getConstant(ShiftSub, dl, IntVT));
3721
3722 SDValue DenormDstMant =
3723 DAG.getNode(ISD::SHL, dl, IntVT, Frac, ShiftAmount);
3724
3725 // Assemble denorm as sign | (denorm_dst_exp << DstMant) | denorm_dst_mant
3726 SDValue DenormExpShifted =
3727 DAG.getNode(ISD::SHL, dl, IntVT, DenormDstExp, DstMantShift);
3728 DenormResult = DAG.getNode(
3729 ISD::OR, dl, IntVT,
3730 DAG.getNode(ISD::OR, dl, IntVT, SignShifted, DenormExpShifted),
3731 DenormDstMant);
3732 }
3733
3734 // Select between normal and denorm paths.
3735 SDValue FiniteResult =
3736 DAG.getSelect(dl, IntVT, IsDenorm, DenormResult, NormResult);
3737
3738 // Build special-value results.
3739 // NaN -> canonical quiet NaN: sign=0, exp=all-ones, qNaN bit set.
3740 // Encoding: (DstExpAllOnes << DstMant) | (1 << (DstMant - 1))
3741 const uint64_t QNaNBit = (DstMant > 0) ? (1ULL << (DstMant - 1)) : 0;
3742 SDValue NaNResult =
3743 DAG.getConstant((DstExpAllOnes << DstMant) | QNaNBit, dl, IntVT);
3744
3745 // Inf -> destination Inf.
3746 // sign | (DstExpAllOnes << DstMant)
3747 SDValue InfResult =
3748 DAG.getNode(ISD::OR, dl, IntVT, SignShifted,
3749 DAG.getConstant(DstExpAllOnes << DstMant, dl, IntVT));
3750
3751 // Zero -> signed zero.
3752 // Sign bit only.
3753 SDValue ZeroResult = SignShifted;
3754
3755 // Final selection goes in order: NaN takes priority, then Inf, then Zero.
3756 SDValue Result = FiniteResult;
3757 Result = DAG.getSelect(dl, IntVT, IsZero, ZeroResult, Result);
3758 Result = DAG.getSelect(dl, IntVT, IsInf, InfResult, Result);
3759 Result = DAG.getSelect(dl, IntVT, IsNaN, NaNResult, Result);
3760
3761 // Bitcast integer result to destination float type.
3762 Result = DAG.getNode(ISD::BITCAST, dl, DstVT, Result);
3763
3764 Results.push_back(Result);
3765 break;
3766 }
3767 case ISD::FCANONICALIZE: {
3768 // This implements llvm.canonicalize.f* by multiplication with 1.0, as
3769 // suggested in
3770 // https://llvm.org/docs/LangRef.html#llvm-canonicalize-intrinsic.
3771 // It uses strict_fp operations even outside a strict_fp context in order
3772 // to guarantee that the canonicalization is not optimized away by later
3773 // passes. The result chain introduced by that is intentionally ignored
3774 // since no ordering requirement is intended here.
3775
3776 // Create strict multiplication by 1.0.
3777 SDValue Operand = Node->getOperand(0);
3778 EVT VT = Operand.getValueType();
3779 SDValue One = DAG.getConstantFP(1.0, dl, VT);
3780 SDValue Chain = DAG.getEntryNode();
3781 // Propagate existing flags on canonicalize, and additionally set
3782 // NoFPExcept.
3783 SDNodeFlags CanonicalizeFlags = Node->getFlags();
3784 CanonicalizeFlags.setNoFPExcept(true);
3785 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, dl, {VT, MVT::Other},
3786 {Chain, Operand, One}, CanonicalizeFlags);
3787
3788 Results.push_back(Mul);
3789 break;
3790 }
3792 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3793 EVT VT = Node->getValueType(0);
3794
3795 // An in-register sign-extend of a boolean is a negation:
3796 // 'true' (1) sign-extended is -1.
3797 // 'false' (0) sign-extended is 0.
3798 // However, we must mask the high bits of the source operand because the
3799 // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
3800
3801 // TODO: Do this for vectors too?
3802 if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
3803 SDValue One = DAG.getConstant(1, dl, VT);
3804 SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
3805 SDValue Zero = DAG.getConstant(0, dl, VT);
3806 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
3807 Results.push_back(Neg);
3808 break;
3809 }
3810
3811 // NOTE: we could fall back on load/store here too for targets without
3812 // SRA. However, it is doubtful that any exist.
3813 unsigned BitsDiff = VT.getScalarSizeInBits() -
3814 ExtraVT.getScalarSizeInBits();
3815 SDValue ShiftCst = DAG.getShiftAmountConstant(BitsDiff, VT, dl);
3816 Tmp1 = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftCst);
3817 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, Tmp1, ShiftCst);
3818 Results.push_back(Tmp1);
3819 break;
3820 }
3821 case ISD::UINT_TO_FP:
3823 if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
3824 Results.push_back(Tmp1);
3825 if (Node->isStrictFPOpcode())
3826 Results.push_back(Tmp2);
3827 break;
3828 }
3829 [[fallthrough]];
3830 case ISD::SINT_TO_FP:
3832 if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
3833 Results.push_back(Tmp1);
3834 if (Node->isStrictFPOpcode())
3835 Results.push_back(Tmp2);
3836 }
3837 break;
3838 case ISD::FP_TO_SINT:
3839 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
3840 Results.push_back(Tmp1);
3841 break;
3843 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
3844 ReplaceNode(Node, Tmp1.getNode());
3845 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
3846 return true;
3847 }
3848 break;
3849 case ISD::FP_TO_UINT:
3850 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
3851 Results.push_back(Tmp1);
3852 break;
3854 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
3855 // Relink the chain.
3856 DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
3857 // Replace the new UINT result.
3858 ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
3859 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
3860 return true;
3861 }
3862 break;
3865 Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3866 break;
3867 case ISD::LROUND:
3868 case ISD::LLROUND: {
3869 SDValue Arg = Node->getOperand(0);
3870 EVT ArgVT = Arg.getValueType();
3871 EVT ResVT = Node->getValueType(0);
3872 SDLoc dl(Node);
3873 SDValue RoundNode = DAG.getNode(ISD::FROUND, dl, ArgVT, Arg);
3874 Results.push_back(DAG.getNode(ISD::FP_TO_SINT, dl, ResVT, RoundNode));
3875 break;
3876 }
3877 case ISD::VAARG:
3878 Results.push_back(DAG.expandVAArg(Node));
3879 Results.push_back(Results[0].getValue(1));
3880 break;
3881 case ISD::VACOPY:
3882 Results.push_back(DAG.expandVACopy(Node));
3883 break;
3885 if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar())
3886 // This must be an access of the only element. Return it.
3887 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
3888 Node->getOperand(0));
3889 else
3890 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3891 Results.push_back(Tmp1);
3892 break;
3894 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3895 break;
3897 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3898 break;
3900 if (EVT VectorValueType = Node->getOperand(0).getValueType();
3901 VectorValueType.isScalableVector() ||
3902 TLI.isOperationExpand(ISD::EXTRACT_VECTOR_ELT, VectorValueType))
3903 Results.push_back(ExpandVectorBuildThroughStack(Node));
3904 else
3905 Results.push_back(ExpandConcatVectors(Node));
3906 break;
3908 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3909 break;
3911 Results.push_back(ExpandINSERT_VECTOR_ELT(SDValue(Node, 0)));
3912 break;
3913 case ISD::VECTOR_SHUFFLE: {
3914 SmallVector<int, 32> NewMask;
3915 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
3916
3917 EVT VT = Node->getValueType(0);
3918 EVT EltVT = VT.getVectorElementType();
3919 SDValue Op0 = Node->getOperand(0);
3920 SDValue Op1 = Node->getOperand(1);
3921 if (!TLI.isTypeLegal(EltVT)) {
3922 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3923
3924 // BUILD_VECTOR operands are allowed to be wider than the element type.
3925 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3926 // it.
3927 if (NewEltVT.bitsLT(EltVT)) {
3928 // Convert shuffle node.
3929 // If original node was v4i64 and the new EltVT is i32,
3930 // cast operands to v8i32 and re-build the mask.
3931
3932 // Calculate new VT, the size of the new VT should be equal to original.
3933 EVT NewVT =
3934 EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3935 VT.getSizeInBits() / NewEltVT.getSizeInBits());
3936 assert(NewVT.bitsEq(VT));
3937
3938 // cast operands to new VT
3939 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3940 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3941
3942 // Convert the shuffle mask
3943 unsigned int factor =
3945
3946 // EltVT gets smaller
3947 assert(factor > 0);
3948
3949 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3950 if (Mask[i] < 0) {
3951 for (unsigned fi = 0; fi < factor; ++fi)
3952 NewMask.push_back(Mask[i]);
3953 }
3954 else {
3955 for (unsigned fi = 0; fi < factor; ++fi)
3956 NewMask.push_back(Mask[i]*factor+fi);
3957 }
3958 }
3959 Mask = NewMask;
3960 VT = NewVT;
3961 }
3962 EltVT = NewEltVT;
3963 }
3964 unsigned NumElems = VT.getVectorNumElements();
3966 for (unsigned i = 0; i != NumElems; ++i) {
3967 if (Mask[i] < 0) {
3968 Ops.push_back(DAG.getUNDEF(EltVT));
3969 continue;
3970 }
3971 unsigned Idx = Mask[i];
3972 if (Idx < NumElems)
3973 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3974 DAG.getVectorIdxConstant(Idx, dl)));
3975 else
3976 Ops.push_back(
3977 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3978 DAG.getVectorIdxConstant(Idx - NumElems, dl)));
3979 }
3980
3981 Tmp1 = DAG.getBuildVector(VT, dl, Ops);
3982 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3983 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
3984 Results.push_back(Tmp1);
3985 break;
3986 }
3989 Results.push_back(TLI.expandVectorSplice(Node, DAG));
3990 break;
3991 }
3993 unsigned Factor = Node->getNumOperands();
3994 if (Factor <= 2 || !isPowerOf2_32(Factor))
3995 break;
3997 EVT VecVT = Node->getValueType(0);
3998 SmallVector<EVT> HalfVTs(Factor / 2, VecVT);
3999 // Deinterleave at Factor/2 so each result contains two factors interleaved:
4000 // a0b0 c0d0 a1b1 c1d1 -> [a0c0 b0d0] [a1c1 b1d1]
4001 SDValue L = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, dl, HalfVTs,
4002 ArrayRef(Ops).take_front(Factor / 2));
4003 SDValue R = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, dl, HalfVTs,
4004 ArrayRef(Ops).take_back(Factor / 2));
4005 Results.resize(Factor);
4006 // Deinterleave the 2 factors out:
4007 // [a0c0 a1c1] [b0d0 b1d1] -> a0a1 b0b1 c0c1 d0d1
4008 for (unsigned I = 0; I < Factor / 2; I++) {
4010 DAG.getNode(ISD::VECTOR_DEINTERLEAVE, dl, {VecVT, VecVT},
4011 {L.getValue(I), R.getValue(I)});
4012 Results[I] = Deinterleave.getValue(0);
4013 Results[I + Factor / 2] = Deinterleave.getValue(1);
4014 }
4015 break;
4016 }
4018 unsigned Factor = Node->getNumOperands();
4019 if (Factor <= 2 || !isPowerOf2_32(Factor))
4020 break;
4021 EVT VecVT = Node->getValueType(0);
4022 SmallVector<EVT> HalfVTs(Factor / 2, VecVT);
4023 SmallVector<SDValue, 8> LOps, ROps;
4024 // Interleave so we have 2 factors per result:
4025 // a0a1 b0b1 c0c1 d0d1 -> [a0c0 b0d0] [a1c1 b1d1]
4026 for (unsigned I = 0; I < Factor / 2; I++) {
4027 SDValue Interleave =
4028 DAG.getNode(ISD::VECTOR_INTERLEAVE, dl, {VecVT, VecVT},
4029 {Node->getOperand(I), Node->getOperand(I + Factor / 2)});
4030 LOps.push_back(Interleave.getValue(0));
4031 ROps.push_back(Interleave.getValue(1));
4032 }
4033 // Interleave at Factor/2:
4034 // [a0c0 b0d0] [a1c1 b1d1] -> a0b0 c0d0 a1b1 c1d1
4035 SDValue L = DAG.getNode(ISD::VECTOR_INTERLEAVE, dl, HalfVTs, LOps);
4036 SDValue R = DAG.getNode(ISD::VECTOR_INTERLEAVE, dl, HalfVTs, ROps);
4037 for (unsigned I = 0; I < Factor / 2; I++)
4038 Results.push_back(L.getValue(I));
4039 for (unsigned I = 0; I < Factor / 2; I++)
4040 Results.push_back(R.getValue(I));
4041 break;
4042 }
4043 case ISD::EXTRACT_ELEMENT: {
4044 EVT OpTy = Node->getOperand(0).getValueType();
4045 if (Node->getConstantOperandVal(1)) {
4046 // 1 -> Hi
4047 Tmp1 = DAG.getNode(
4048 ISD::SRL, dl, OpTy, Node->getOperand(0),
4049 DAG.getShiftAmountConstant(OpTy.getSizeInBits() / 2, OpTy, dl));
4050 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
4051 } else {
4052 // 0 -> Lo
4053 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
4054 Node->getOperand(0));
4055 }
4056 Results.push_back(Tmp1);
4057 break;
4058 }
4059 case ISD::STACKADDRESS:
4060 case ISD::STACKSAVE:
4061 // Expand to CopyFromReg if the target set
4062 // StackPointerRegisterToSaveRestore.
4064 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
4065 Node->getValueType(0)));
4066 Results.push_back(Results[0].getValue(1));
4067 } else {
4068 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
4069 Results.push_back(Node->getOperand(0));
4070
4071 StringRef IntrinsicName = Node->getOpcode() == ISD::STACKADDRESS
4072 ? "llvm.stackaddress"
4073 : "llvm.stacksave";
4074 DAG.getContext()->diagnose(DiagnosticInfoLegalizationFailure(
4075 Twine(IntrinsicName) + " is not supported on this target.",
4077 }
4078 break;
4079 case ISD::STACKRESTORE:
4080 // Expand to CopyToReg if the target set
4081 // StackPointerRegisterToSaveRestore.
4083 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
4084 Node->getOperand(1)));
4085 } else {
4086 Results.push_back(Node->getOperand(0));
4087 }
4088 break;
4090 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
4091 Results.push_back(Results[0].getValue(0));
4092 break;
4093 case ISD::FCOPYSIGN:
4094 Results.push_back(ExpandFCOPYSIGN(Node));
4095 break;
4096 case ISD::FNEG:
4097 Results.push_back(ExpandFNEG(Node));
4098 break;
4099 case ISD::FABS:
4100 Results.push_back(ExpandFABS(Node));
4101 break;
4102 case ISD::IS_FPCLASS: {
4103 auto Test = static_cast<FPClassTest>(Node->getConstantOperandVal(1));
4104 if (SDValue Expanded =
4105 TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
4106 Test, Node->getFlags(), SDLoc(Node), DAG))
4107 Results.push_back(Expanded);
4108 break;
4109 }
4110 case ISD::SMIN:
4111 case ISD::SMAX:
4112 case ISD::UMIN:
4113 case ISD::UMAX: {
4114 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
4115 ISD::CondCode Pred;
4116 switch (Node->getOpcode()) {
4117 default: llvm_unreachable("How did we get here?");
4118 case ISD::SMAX: Pred = ISD::SETGT; break;
4119 case ISD::SMIN: Pred = ISD::SETLT; break;
4120 case ISD::UMAX: Pred = ISD::SETUGT; break;
4121 case ISD::UMIN: Pred = ISD::SETULT; break;
4122 }
4123 Tmp1 = Node->getOperand(0);
4124 Tmp2 = Node->getOperand(1);
4125 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
4126 Results.push_back(Tmp1);
4127 break;
4128 }
4129 case ISD::FMINNUM:
4130 case ISD::FMAXNUM: {
4131 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
4132 Results.push_back(Expanded);
4133 break;
4134 }
4135 case ISD::FMINIMUM:
4136 case ISD::FMAXIMUM: {
4137 if (SDValue Expanded = TLI.expandFMINIMUM_FMAXIMUM(Node, DAG))
4138 Results.push_back(Expanded);
4139 break;
4140 }
4141 case ISD::FMINIMUMNUM:
4142 case ISD::FMAXIMUMNUM: {
4143 Results.push_back(TLI.expandFMINIMUMNUM_FMAXIMUMNUM(Node, DAG));
4144 break;
4145 }
4146 case ISD::FSIN:
4147 case ISD::FCOS: {
4148 EVT VT = Node->getValueType(0);
4149 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
4150 // fcos which share the same operand and both are used.
4151 if ((TLI.isOperationLegal(ISD::FSINCOS, VT) ||
4152 isSinCosLibcallAvailable(Node, DAG.getLibcalls())) &&
4153 useSinCos(Node)) {
4154 SDVTList VTs = DAG.getVTList(VT, VT);
4155 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
4156 if (Node->getOpcode() == ISD::FCOS)
4157 Tmp1 = Tmp1.getValue(1);
4158 Results.push_back(Tmp1);
4159 }
4160 break;
4161 }
4162 case ISD::FLDEXP:
4163 case ISD::STRICT_FLDEXP: {
4164 EVT VT = Node->getValueType(0);
4165 RTLIB::Libcall LC = RTLIB::getLDEXP(VT);
4166 // Use the LibCall instead, it is very likely faster
4167 // FIXME: Use separate LibCall action.
4168 if (DAG.getLibcalls().getLibcallImpl(LC) != RTLIB::Unsupported)
4169 break;
4170
4171 if (SDValue Expanded = expandLdexp(Node)) {
4172 Results.push_back(Expanded);
4173 if (Node->getOpcode() == ISD::STRICT_FLDEXP)
4174 Results.push_back(Expanded.getValue(1));
4175 }
4176
4177 break;
4178 }
4179 case ISD::FFREXP: {
4180 RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
4181 // Use the LibCall instead, it is very likely faster
4182 // FIXME: Use separate LibCall action.
4183 if (DAG.getLibcalls().getLibcallImpl(LC) != RTLIB::Unsupported)
4184 break;
4185
4186 if (SDValue Expanded = expandFrexp(Node)) {
4187 Results.push_back(Expanded);
4188 Results.push_back(Expanded.getValue(1));
4189 }
4190 break;
4191 }
4192 case ISD::FMODF: {
4193 RTLIB::Libcall LC = RTLIB::getMODF(Node->getValueType(0));
4194 // Use the LibCall instead, it is very likely faster
4195 // FIXME: Use separate LibCall action.
4196 if (DAG.getLibcalls().getLibcallImpl(LC) != RTLIB::Unsupported)
4197 break;
4198
4199 if (SDValue Expanded = expandModf(Node)) {
4200 Results.push_back(Expanded);
4201 Results.push_back(Expanded.getValue(1));
4202 }
4203 break;
4204 }
4205 case ISD::FSINCOS: {
4206 if (isSinCosLibcallAvailable(Node, DAG.getLibcalls()))
4207 break;
4208 EVT VT = Node->getValueType(0);
4209 SDValue Op = Node->getOperand(0);
4210 SDNodeFlags Flags = Node->getFlags();
4211 Tmp1 = DAG.getNode(ISD::FSIN, dl, VT, Op, Flags);
4212 Tmp2 = DAG.getNode(ISD::FCOS, dl, VT, Op, Flags);
4213 Results.append({Tmp1, Tmp2});
4214 break;
4215 }
4216 case ISD::FMAD:
4217 llvm_unreachable("Illegal fmad should never be formed");
4218
4219 case ISD::FP16_TO_FP:
4220 if (Node->getValueType(0) != MVT::f32) {
4221 // We can extend to types bigger than f32 in two steps without changing
4222 // the result. Since "f16 -> f32" is much more commonly available, give
4223 // CodeGen the option of emitting that before resorting to a libcall.
4224 SDValue Res =
4225 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
4226 Results.push_back(
4227 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
4228 }
4229 break;
4232 if (Node->getValueType(0) != MVT::f32) {
4233 // We can extend to types bigger than f32 in two steps without changing
4234 // the result. Since "f16 -> f32" is much more commonly available, give
4235 // CodeGen the option of emitting that before resorting to a libcall.
4236 SDValue Res = DAG.getNode(Node->getOpcode(), dl, {MVT::f32, MVT::Other},
4237 {Node->getOperand(0), Node->getOperand(1)});
4238 Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
4239 {Node->getValueType(0), MVT::Other},
4240 {Res.getValue(1), Res});
4241 Results.push_back(Res);
4242 Results.push_back(Res.getValue(1));
4243 }
4244 break;
4245 case ISD::FP_TO_FP16:
4246 LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
4247 if (Node->getFlags().hasApproximateFuncs() && !TLI.useSoftFloat()) {
4248 SDValue Op = Node->getOperand(0);
4249 MVT SVT = Op.getSimpleValueType();
4250 if ((SVT == MVT::f64 || SVT == MVT::f80) &&
4252 // Under fastmath, we can expand this node into a fround followed by
4253 // a float-half conversion.
4254 SDValue FloatVal =
4255 DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
4256 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
4257 Results.push_back(
4258 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
4259 }
4260 }
4261 break;
4262 case ISD::ConstantFP: {
4263 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
4264 // Check to see if this FP immediate is already legal.
4265 // If this is a legal constant, turn it into a TargetConstantFP node.
4266 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
4267 DAG.shouldOptForSize()))
4268 Results.push_back(ExpandConstantFP(CFP, true));
4269 break;
4270 }
4271 case ISD::Constant: {
4272 ConstantSDNode *CP = cast<ConstantSDNode>(Node);
4273 Results.push_back(ExpandConstant(CP));
4274 break;
4275 }
4276 case ISD::FSUB: {
4277 EVT VT = Node->getValueType(0);
4278 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
4280 const SDNodeFlags Flags = Node->getFlags();
4281 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
4282 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
4283 Results.push_back(Tmp1);
4284 }
4285 break;
4286 }
4287 case ISD::SUB: {
4288 EVT VT = Node->getValueType(0);
4291 "Don't know how to expand this subtraction!");
4292 Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
4293 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
4294 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
4295 break;
4296 }
4297 case ISD::UREM:
4298 case ISD::SREM:
4299 if (TLI.expandREM(Node, Tmp1, DAG))
4300 Results.push_back(Tmp1);
4301 break;
4302 case ISD::UDIV:
4303 case ISD::SDIV: {
4304 bool isSigned = Node->getOpcode() == ISD::SDIV;
4305 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
4306 EVT VT = Node->getValueType(0);
4307 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
4308 SDVTList VTs = DAG.getVTList(VT, VT);
4309 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
4310 Node->getOperand(1));
4311 Results.push_back(Tmp1);
4312 }
4313 break;
4314 }
4315 case ISD::MULHU:
4316 case ISD::MULHS: {
4317 unsigned ExpandOpcode =
4318 Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
4319 EVT VT = Node->getValueType(0);
4320 SDVTList VTs = DAG.getVTList(VT, VT);
4321
4322 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
4323 Node->getOperand(1));
4324 Results.push_back(Tmp1.getValue(1));
4325 break;
4326 }
4327 case ISD::UMUL_LOHI:
4328 case ISD::SMUL_LOHI: {
4329 SDValue LHS = Node->getOperand(0);
4330 SDValue RHS = Node->getOperand(1);
4331 EVT VT = LHS.getValueType();
4332 unsigned MULHOpcode =
4333 Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
4334
4335 if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
4336 Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
4337 Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
4338 break;
4339 }
4340
4342 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
4343 assert(TLI.isTypeLegal(HalfType));
4344 if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
4345 HalfType, DAG,
4346 TargetLowering::MulExpansionKind::Always)) {
4347 for (unsigned i = 0; i < 2; ++i) {
4348 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
4349 SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
4350 SDValue Shift =
4351 DAG.getShiftAmountConstant(HalfType.getScalarSizeInBits(), VT, dl);
4352 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
4353 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
4354 }
4355 break;
4356 }
4357 break;
4358 }
4359 case ISD::MUL: {
4360 EVT VT = Node->getValueType(0);
4361 SDVTList VTs = DAG.getVTList(VT, VT);
4362 // See if multiply or divide can be lowered using two-result operations.
4363 // We just need the low half of the multiply; try both the signed
4364 // and unsigned forms. If the target supports both SMUL_LOHI and
4365 // UMUL_LOHI, form a preference by checking which forms of plain
4366 // MULH it supports.
4367 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
4368 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
4369 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
4370 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
4371 unsigned OpToUse = 0;
4372 if (HasSMUL_LOHI && !HasMULHS) {
4373 OpToUse = ISD::SMUL_LOHI;
4374 } else if (HasUMUL_LOHI && !HasMULHU) {
4375 OpToUse = ISD::UMUL_LOHI;
4376 } else if (HasSMUL_LOHI) {
4377 OpToUse = ISD::SMUL_LOHI;
4378 } else if (HasUMUL_LOHI) {
4379 OpToUse = ISD::UMUL_LOHI;
4380 }
4381 if (OpToUse) {
4382 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
4383 Node->getOperand(1)));
4384 break;
4385 }
4386
4387 SDValue Lo, Hi;
4388 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
4393 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
4394 TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
4395 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
4396 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
4397 SDValue Shift =
4398 DAG.getShiftAmountConstant(HalfType.getSizeInBits(), VT, dl);
4399 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
4400 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
4401 }
4402 break;
4403 }
4404 case ISD::FSHL:
4405 case ISD::FSHR:
4406 if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
4407 Results.push_back(Expanded);
4408 break;
4409 case ISD::ROTL:
4410 case ISD::ROTR:
4411 if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
4412 Results.push_back(Expanded);
4413 break;
4414 case ISD::CLMUL:
4415 case ISD::CLMULR:
4416 case ISD::CLMULH:
4417 if (SDValue Expanded = TLI.expandCLMUL(Node, DAG))
4418 Results.push_back(Expanded);
4419 break;
4420 case ISD::SADDSAT:
4421 case ISD::UADDSAT:
4422 case ISD::SSUBSAT:
4423 case ISD::USUBSAT:
4424 Results.push_back(TLI.expandAddSubSat(Node, DAG));
4425 break;
4426 case ISD::SCMP:
4427 case ISD::UCMP:
4428 Results.push_back(TLI.expandCMP(Node, DAG));
4429 break;
4430 case ISD::SSHLSAT:
4431 case ISD::USHLSAT:
4432 Results.push_back(TLI.expandShlSat(Node, DAG));
4433 break;
4434 case ISD::SMULFIX:
4435 case ISD::SMULFIXSAT:
4436 case ISD::UMULFIX:
4437 case ISD::UMULFIXSAT:
4438 Results.push_back(TLI.expandFixedPointMul(Node, DAG));
4439 break;
4440 case ISD::SDIVFIX:
4441 case ISD::SDIVFIXSAT:
4442 case ISD::UDIVFIX:
4443 case ISD::UDIVFIXSAT:
4444 if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
4445 Node->getOperand(0),
4446 Node->getOperand(1),
4447 Node->getConstantOperandVal(2),
4448 DAG)) {
4449 Results.push_back(V);
4450 break;
4451 }
4452 // FIXME: We might want to retry here with a wider type if we fail, if that
4453 // type is legal.
4454 // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
4455 // <= 128 (which is the case for all of the default Embedded-C types),
4456 // we will only get here with types and scales that we could always expand
4457 // if we were allowed to generate libcalls to division functions of illegal
4458 // type. But we cannot do that.
4459 llvm_unreachable("Cannot expand DIVFIX!");
4460 case ISD::UADDO_CARRY:
4461 case ISD::USUBO_CARRY: {
4462 SDValue LHS = Node->getOperand(0);
4463 SDValue RHS = Node->getOperand(1);
4464 SDValue Carry = Node->getOperand(2);
4465
4466 bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
4467
4468 // Initial add of the 2 operands.
4469 unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
4470 EVT VT = LHS.getValueType();
4471 SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
4472
4473 // Initial check for overflow.
4474 EVT CarryType = Node->getValueType(1);
4475 EVT SetCCType = getSetCCResultType(Node->getValueType(0));
4476 ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT;
4477 SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
4478
4479 // Add of the sum and the carry.
4480 SDValue One = DAG.getConstant(1, dl, VT);
4481 SDValue CarryExt =
4482 DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
4483 SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
4484
4485 // Second check for overflow. If we are adding, we can only overflow if the
4486 // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
4487 // If we are subtracting, we can only overflow if the initial sum is 0 and
4488 // the carry is set, resulting in a new sum of all 1s.
4489 SDValue Zero = DAG.getConstant(0, dl, VT);
4490 SDValue Overflow2 =
4491 IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
4492 : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
4493 Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
4494 DAG.getZExtOrTrunc(Carry, dl, SetCCType));
4495
4496 SDValue ResultCarry =
4497 DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
4498
4499 Results.push_back(Sum2);
4500 Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
4501 break;
4502 }
4503 case ISD::SADDO:
4504 case ISD::SSUBO: {
4505 SDValue Result, Overflow;
4506 TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
4507 Results.push_back(Result);
4508 Results.push_back(Overflow);
4509 break;
4510 }
4511 case ISD::UADDO:
4512 case ISD::USUBO: {
4513 SDValue Result, Overflow;
4514 TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
4515 Results.push_back(Result);
4516 Results.push_back(Overflow);
4517 break;
4518 }
4519 case ISD::UMULO:
4520 case ISD::SMULO: {
4521 SDValue Result, Overflow;
4522 if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
4523 Results.push_back(Result);
4524 Results.push_back(Overflow);
4525 }
4526 break;
4527 }
4528 case ISD::BUILD_PAIR: {
4529 EVT PairTy = Node->getValueType(0);
4530 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
4531 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
4532 Tmp2 = DAG.getNode(
4533 ISD::SHL, dl, PairTy, Tmp2,
4534 DAG.getShiftAmountConstant(PairTy.getSizeInBits() / 2, PairTy, dl));
4535 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
4536 break;
4537 }
4538 case ISD::SELECT:
4539 Tmp1 = Node->getOperand(0);
4540 Tmp2 = Node->getOperand(1);
4541 Tmp3 = Node->getOperand(2);
4542 if (Tmp1.getOpcode() == ISD::SETCC) {
4543 Tmp1 = DAG.getSelectCC(
4544 dl, Tmp1.getOperand(0), Tmp1.getOperand(1), Tmp2, Tmp3,
4545 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get(), Node->getFlags());
4546 } else {
4547 Tmp1 =
4548 DAG.getSelectCC(dl, Tmp1, DAG.getConstant(0, dl, Tmp1.getValueType()),
4549 Tmp2, Tmp3, ISD::SETNE, Node->getFlags());
4550 }
4551 Results.push_back(Tmp1);
4552 break;
4553 case ISD::BR_JT: {
4554 SDValue Chain = Node->getOperand(0);
4555 SDValue Table = Node->getOperand(1);
4556 SDValue Index = Node->getOperand(2);
4557 int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex();
4558
4559 const DataLayout &TD = DAG.getDataLayout();
4560 EVT PTy = TLI.getPointerTy(TD);
4561
4562 unsigned EntrySize =
4564
4565 // For power-of-two jumptable entry sizes convert multiplication to a shift.
4566 // This transformation needs to be done here since otherwise the MIPS
4567 // backend will end up emitting a three instruction multiply sequence
4568 // instead of a single shift and MSP430 will call a runtime function.
4569 if (llvm::isPowerOf2_32(EntrySize))
4570 Index = DAG.getNode(
4571 ISD::SHL, dl, Index.getValueType(), Index,
4572 DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
4573 else
4574 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
4575 DAG.getConstant(EntrySize, dl, Index.getValueType()));
4576 SDValue Addr = DAG.getMemBasePlusOffset(Table, Index, dl);
4577
4578 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
4579 SDValue LD = DAG.getExtLoad(
4580 ISD::SEXTLOAD, dl, PTy, Chain, Addr,
4582 Addr = LD;
4583 if (TLI.isJumpTableRelative()) {
4584 // For PIC, the sequence is:
4585 // BRIND(RelocBase + load(Jumptable + index))
4586 // RelocBase can be JumpTable, GOT or some sort of global base.
4587 Addr = DAG.getMemBasePlusOffset(TLI.getPICJumpTableRelocBase(Table, DAG),
4588 Addr, dl);
4589 }
4590
4591 Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG);
4592 Results.push_back(Tmp1);
4593 break;
4594 }
4595 case ISD::BRCOND:
4596 // Expand brcond's setcc into its constituent parts and create a BR_CC
4597 // Node.
4598 Tmp1 = Node->getOperand(0);
4599 Tmp2 = Node->getOperand(1);
4600 if (Tmp2.getOpcode() == ISD::SETCC &&
4602 Tmp2.getOperand(0).getValueType())) {
4603 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
4604 Tmp2.getOperand(0), Tmp2.getOperand(1),
4605 Node->getOperand(2));
4606 } else {
4607 // We test only the i1 bit. Skip the AND if UNDEF or another AND.
4608 if (Tmp2.isUndef() ||
4609 (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
4610 Tmp3 = Tmp2;
4611 else
4612 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
4613 DAG.getConstant(1, dl, Tmp2.getValueType()));
4614 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
4615 DAG.getCondCode(ISD::SETNE), Tmp3,
4616 DAG.getConstant(0, dl, Tmp3.getValueType()),
4617 Node->getOperand(2));
4618 }
4619 Results.push_back(Tmp1);
4620 break;
4621 case ISD::SETCC:
4622 case ISD::VP_SETCC:
4623 case ISD::STRICT_FSETCC:
4624 case ISD::STRICT_FSETCCS: {
4625 bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
4626 bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
4627 Node->getOpcode() == ISD::STRICT_FSETCCS;
4628 bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
4629 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4630 unsigned Offset = IsStrict ? 1 : 0;
4631 Tmp1 = Node->getOperand(0 + Offset);
4632 Tmp2 = Node->getOperand(1 + Offset);
4633 Tmp3 = Node->getOperand(2 + Offset);
4634 SDValue Mask, EVL;
4635 if (IsVP) {
4636 Mask = Node->getOperand(3 + Offset);
4637 EVL = Node->getOperand(4 + Offset);
4638 }
4639 bool Legalized = TLI.LegalizeSetCCCondCode(
4640 DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
4641 Chain, IsSignaling);
4642
4643 if (Legalized) {
4644 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4645 // condition code, create a new SETCC node.
4646 if (Tmp3.getNode()) {
4647 if (IsStrict) {
4648 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
4649 {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
4650 Chain = Tmp1.getValue(1);
4651 } else if (IsVP) {
4652 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
4653 {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
4654 } else {
4655 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
4656 Tmp2, Tmp3, Node->getFlags());
4657 }
4658 }
4659
4660 // If we expanded the SETCC by inverting the condition code, then wrap
4661 // the existing SETCC in a NOT to restore the intended condition.
4662 if (NeedInvert) {
4663 if (!IsVP)
4664 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
4665 else
4666 Tmp1 =
4667 DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
4668 }
4669
4670 Results.push_back(Tmp1);
4671 if (IsStrict)
4672 Results.push_back(Chain);
4673
4674 break;
4675 }
4676
4677 // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4678 // understand if this code is useful for strict nodes.
4679 assert(!IsStrict && "Don't know how to expand for strict nodes.");
4680
4681 // Otherwise, SETCC for the given comparison type must be completely
4682 // illegal; expand it into a SELECT_CC.
4683 // FIXME: This drops the mask/evl for VP_SETCC.
4684 EVT VT = Node->getValueType(0);
4685 EVT Tmp1VT = Tmp1.getValueType();
4686 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
4687 DAG.getBoolConstant(true, dl, VT, Tmp1VT),
4688 DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3,
4689 Node->getFlags());
4690 Results.push_back(Tmp1);
4691 break;
4692 }
4693 case ISD::SELECT_CC: {
4694 // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
4695 Tmp1 = Node->getOperand(0); // LHS
4696 Tmp2 = Node->getOperand(1); // RHS
4697 Tmp3 = Node->getOperand(2); // True
4698 Tmp4 = Node->getOperand(3); // False
4699 EVT VT = Node->getValueType(0);
4700 SDValue Chain;
4701 SDValue CC = Node->getOperand(4);
4702 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
4703
4704 if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
4705 // If the condition code is legal, then we need to expand this
4706 // node using SETCC and SELECT.
4707 EVT CmpVT = Tmp1.getValueType();
4709 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
4710 "expanded.");
4711 EVT CCVT = getSetCCResultType(CmpVT);
4712 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
4713 Results.push_back(
4714 DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4, Node->getFlags()));
4715 break;
4716 }
4717
4718 // SELECT_CC is legal, so the condition code must not be.
4719 bool Legalized = false;
4720 // Try to legalize by inverting the condition. This is for targets that
4721 // might support an ordered version of a condition, but not the unordered
4722 // version (or vice versa).
4723 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
4724 if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
4725 // Use the new condition code and swap true and false
4726 Legalized = true;
4727 Tmp1 =
4728 DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC, Node->getFlags());
4729 } else {
4730 // If The inverse is not legal, then try to swap the arguments using
4731 // the inverse condition code.
4733 if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
4734 // The swapped inverse condition is legal, so swap true and false,
4735 // lhs and rhs.
4736 Legalized = true;
4737 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC,
4738 Node->getFlags());
4739 }
4740 }
4741
4742 if (!Legalized) {
4743 Legalized = TLI.LegalizeSetCCCondCode(
4744 DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
4745 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4746
4747 assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
4748
4749 // If we expanded the SETCC by inverting the condition code, then swap
4750 // the True/False operands to match.
4751 if (NeedInvert)
4752 std::swap(Tmp3, Tmp4);
4753
4754 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4755 // condition code, create a new SELECT_CC node.
4756 if (CC.getNode()) {
4757 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
4758 Tmp2, Tmp3, Tmp4, CC, Node->getFlags());
4759 } else {
4760 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
4761 CC = DAG.getCondCode(ISD::SETNE);
4762 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
4763 Tmp2, Tmp3, Tmp4, CC, Node->getFlags());
4764 }
4765 }
4766 Results.push_back(Tmp1);
4767 break;
4768 }
4769 case ISD::BR_CC: {
4770 // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4771 SDValue Chain;
4772 Tmp1 = Node->getOperand(0); // Chain
4773 Tmp2 = Node->getOperand(2); // LHS
4774 Tmp3 = Node->getOperand(3); // RHS
4775 Tmp4 = Node->getOperand(1); // CC
4776
4777 bool Legalized = TLI.LegalizeSetCCCondCode(
4778 DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
4779 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4780 (void)Legalized;
4781 assert(Legalized && "Can't legalize BR_CC with legal condition!");
4782
4783 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
4784 // node.
4785 if (Tmp4.getNode()) {
4786 assert(!NeedInvert && "Don't know how to invert BR_CC!");
4787
4788 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
4789 Tmp4, Tmp2, Tmp3, Node->getOperand(4));
4790 } else {
4791 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4792 Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
4793 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
4794 Tmp2, Tmp3, Node->getOperand(4));
4795 }
4796 Results.push_back(Tmp1);
4797 break;
4798 }
4799 case ISD::BUILD_VECTOR:
4800 Results.push_back(ExpandBUILD_VECTOR(Node));
4801 break;
4802 case ISD::SPLAT_VECTOR:
4803 Results.push_back(ExpandSPLAT_VECTOR(Node));
4804 break;
4805 case ISD::SRA:
4806 case ISD::SRL:
4807 case ISD::SHL: {
4808 // Scalarize vector SRA/SRL/SHL.
4809 EVT VT = Node->getValueType(0);
4810 assert(VT.isVector() && "Unable to legalize non-vector shift");
4811 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
4812 unsigned NumElem = VT.getVectorNumElements();
4813
4815 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
4816 SDValue Ex =
4818 Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
4819 SDValue Sh =
4821 Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
4822 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
4823 VT.getScalarType(), Ex, Sh));
4824 }
4825
4826 SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4827 Results.push_back(Result);
4828 break;
4829 }
4832 case ISD::VECREDUCE_ADD:
4833 case ISD::VECREDUCE_MUL:
4834 case ISD::VECREDUCE_AND:
4835 case ISD::VECREDUCE_OR:
4836 case ISD::VECREDUCE_XOR:
4845 Results.push_back(TLI.expandVecReduce(Node, DAG));
4846 break;
4847 case ISD::VP_CTTZ_ELTS:
4848 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
4849 Results.push_back(TLI.expandVPCTTZElements(Node, DAG));
4850 break;
4851 case ISD::CLEAR_CACHE:
4852 // The default expansion of llvm.clear_cache is simply a no-op for those
4853 // targets where it is not needed.
4854 Results.push_back(Node->getOperand(0));
4855 break;
4856 case ISD::LRINT:
4857 case ISD::LLRINT: {
4858 SDValue Arg = Node->getOperand(0);
4859 EVT ArgVT = Arg.getValueType();
4860 EVT ResVT = Node->getValueType(0);
4861 SDLoc dl(Node);
4862 SDValue RoundNode = DAG.getNode(ISD::FRINT, dl, ArgVT, Arg);
4863 Results.push_back(DAG.getNode(ISD::FP_TO_SINT, dl, ResVT, RoundNode));
4864 break;
4865 }
4866 case ISD::ADDRSPACECAST:
4867 Results.push_back(DAG.UnrollVectorOp(Node));
4868 break;
4870 case ISD::GlobalAddress:
4873 case ISD::ConstantPool:
4874 case ISD::JumpTable:
4878 // FIXME: Custom lowering for these operations shouldn't return null!
4879 // Return true so that we don't call ConvertNodeToLibcall which also won't
4880 // do anything.
4881 return true;
4882 }
4883
4884 if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
4885 // FIXME: We were asked to expand a strict floating-point operation,
4886 // but there is currently no expansion implemented that would preserve
4887 // the "strict" properties. For now, we just fall back to the non-strict
4888 // version if that is legal on the target. The actual mutation of the
4889 // operation will happen in SelectionDAGISel::DoInstructionSelection.
4890 switch (Node->getOpcode()) {
4891 default:
4892 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4893 Node->getValueType(0))
4894 == TargetLowering::Legal)
4895 return true;
4896 break;
4897 case ISD::STRICT_FSUB: {
4899 ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4900 return true;
4902 ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4903 break;
4904
4905 EVT VT = Node->getValueType(0);
4906 const SDNodeFlags Flags = Node->getFlags();
4907 SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4908 SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4909 {Node->getOperand(0), Node->getOperand(1), Neg},
4910 Flags);
4911
4912 Results.push_back(Fadd);
4913 Results.push_back(Fadd.getValue(1));
4914 break;
4915 }
4918 case ISD::STRICT_LRINT:
4919 case ISD::STRICT_LLRINT:
4920 case ISD::STRICT_LROUND:
4922 // These are registered by the operand type instead of the value
4923 // type. Reflect that here.
4924 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4925 Node->getOperand(1).getValueType())
4926 == TargetLowering::Legal)
4927 return true;
4928 break;
4929 }
4930 }
4931
4932 // Replace the original node with the legalized result.
4933 if (Results.empty()) {
4934 LLVM_DEBUG(dbgs() << "Cannot expand node\n");
4935 return false;
4936 }
4937
4938 LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
4939 ReplaceNode(Node, Results.data());
4940 return true;
4941}
4942
4943/// Return if we can use the FAST_* variant of a math libcall for the node.
4944/// FIXME: This is just guessing, we probably should have unique specific sets
4945/// flags required per libcall.
4946static bool canUseFastMathLibcall(const SDNode *Node) {
4947 // FIXME: Probably should define fast to respect nan/inf and only be
4948 // approximate functions.
4949
4950 SDNodeFlags Flags = Node->getFlags();
4951 return Flags.hasApproximateFuncs() && Flags.hasNoNaNs() &&
4952 Flags.hasNoInfs() && Flags.hasNoSignedZeros();
4953}
4954
4955void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
4956 LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
4958 SDLoc dl(Node);
4959 TargetLowering::MakeLibCallOptions CallOptions;
4960 CallOptions.IsPostTypeLegalization = true;
4961 // FIXME: Check flags on the node to see if we can use a finite call.
4962 unsigned Opc = Node->getOpcode();
4963 switch (Opc) {
4964 case ISD::ATOMIC_FENCE: {
4965 // If the target didn't lower this, lower it to '__sync_synchronize()' call
4966 // FIXME: handle "fence singlethread" more efficiently.
4967 TargetLowering::ArgListTy Args;
4968
4969 TargetLowering::CallLoweringInfo CLI(DAG);
4970 CLI.setDebugLoc(dl)
4971 .setChain(Node->getOperand(0))
4972 .setLibCallee(
4973 CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4974 DAG.getExternalSymbol("__sync_synchronize",
4975 TLI.getPointerTy(DAG.getDataLayout())),
4976 std::move(Args));
4977
4978 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4979
4980 Results.push_back(CallResult.second);
4981 break;
4982 }
4983 // By default, atomic intrinsics are marked Legal and lowered. Targets
4984 // which don't support them directly, however, may want libcalls, in which
4985 // case they mark them Expand, and we get here.
4986 case ISD::ATOMIC_SWAP:
4998 case ISD::ATOMIC_CMP_SWAP: {
4999 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
5000 AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
5001 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
5002 EVT RetVT = Node->getValueType(0);
5004 if (DAG.getLibcalls().getLibcallImpl(LC) != RTLIB::Unsupported) {
5005 // If outline atomic available, prepare its arguments and expand.
5006 Ops.append(Node->op_begin() + 2, Node->op_end());
5007 Ops.push_back(Node->getOperand(1));
5008
5009 } else {
5010 LC = RTLIB::getSYNC(Opc, VT);
5011 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
5012 "Unexpected atomic op or value type!");
5013 // Arguments for expansion to sync libcall
5014 Ops.append(Node->op_begin() + 1, Node->op_end());
5015 }
5016 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
5017 Ops, CallOptions,
5018 SDLoc(Node),
5019 Node->getOperand(0));
5020 Results.push_back(Tmp.first);
5021 Results.push_back(Tmp.second);
5022 break;
5023 }
5024 case ISD::TRAP: {
5025 // If this operation is not supported, lower it to 'abort()' call
5026 TargetLowering::ArgListTy Args;
5027 TargetLowering::CallLoweringInfo CLI(DAG);
5028 CLI.setDebugLoc(dl)
5029 .setChain(Node->getOperand(0))
5030 .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
5032 "abort", TLI.getPointerTy(DAG.getDataLayout())),
5033 std::move(Args));
5034 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
5035
5036 Results.push_back(CallResult.second);
5037 break;
5038 }
5039 case ISD::CLEAR_CACHE: {
5040 SDValue InputChain = Node->getOperand(0);
5041 SDValue StartVal = Node->getOperand(1);
5042 SDValue EndVal = Node->getOperand(2);
5043 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
5044 DAG, RTLIB::CLEAR_CACHE, MVT::isVoid, {StartVal, EndVal}, CallOptions,
5045 SDLoc(Node), InputChain);
5046 Results.push_back(Tmp.second);
5047 break;
5048 }
5049 case ISD::FMINNUM:
5051 ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
5052 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
5053 RTLIB::FMIN_PPCF128, Results);
5054 break;
5055 // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
5056 // libcall legalization for these nodes, but there is no default expasion for
5057 // these nodes either (see PR63267 for example).
5058 case ISD::FMAXNUM:
5060 ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
5061 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
5062 RTLIB::FMAX_PPCF128, Results);
5063 break;
5064 case ISD::FMINIMUMNUM:
5065 ExpandFPLibCall(Node, RTLIB::FMINIMUM_NUM_F32, RTLIB::FMINIMUM_NUM_F64,
5066 RTLIB::FMINIMUM_NUM_F80, RTLIB::FMINIMUM_NUM_F128,
5067 RTLIB::FMINIMUM_NUM_PPCF128, Results);
5068 break;
5069 case ISD::FMAXIMUMNUM:
5070 ExpandFPLibCall(Node, RTLIB::FMAXIMUM_NUM_F32, RTLIB::FMAXIMUM_NUM_F64,
5071 RTLIB::FMAXIMUM_NUM_F80, RTLIB::FMAXIMUM_NUM_F128,
5072 RTLIB::FMAXIMUM_NUM_PPCF128, Results);
5073 break;
5074 case ISD::FSQRT:
5075 case ISD::STRICT_FSQRT: {
5076 // FIXME: Probably should define fast to respect nan/inf and only be
5077 // approximate functions.
5078 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
5079 {RTLIB::FAST_SQRT_F32, RTLIB::SQRT_F32},
5080 {RTLIB::FAST_SQRT_F64, RTLIB::SQRT_F64},
5081 {RTLIB::FAST_SQRT_F80, RTLIB::SQRT_F80},
5082 {RTLIB::FAST_SQRT_F128, RTLIB::SQRT_F128},
5083 {RTLIB::FAST_SQRT_PPCF128, RTLIB::SQRT_PPCF128},
5084 Results);
5085 break;
5086 }
5087 case ISD::FCBRT:
5088 ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
5089 RTLIB::CBRT_F80, RTLIB::CBRT_F128,
5090 RTLIB::CBRT_PPCF128, Results);
5091 break;
5092 case ISD::FSIN:
5093 case ISD::STRICT_FSIN:
5094 ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
5095 RTLIB::SIN_F80, RTLIB::SIN_F128,
5096 RTLIB::SIN_PPCF128, Results);
5097 break;
5098 case ISD::FCOS:
5099 case ISD::STRICT_FCOS:
5100 ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
5101 RTLIB::COS_F80, RTLIB::COS_F128,
5102 RTLIB::COS_PPCF128, Results);
5103 break;
5104 case ISD::FTAN:
5105 case ISD::STRICT_FTAN:
5106 ExpandFPLibCall(Node, RTLIB::TAN_F32, RTLIB::TAN_F64, RTLIB::TAN_F80,
5107 RTLIB::TAN_F128, RTLIB::TAN_PPCF128, Results);
5108 break;
5109 case ISD::FASIN:
5110 case ISD::STRICT_FASIN:
5111 ExpandFPLibCall(Node, RTLIB::ASIN_F32, RTLIB::ASIN_F64, RTLIB::ASIN_F80,
5112 RTLIB::ASIN_F128, RTLIB::ASIN_PPCF128, Results);
5113 break;
5114 case ISD::FACOS:
5115 case ISD::STRICT_FACOS:
5116 ExpandFPLibCall(Node, RTLIB::ACOS_F32, RTLIB::ACOS_F64, RTLIB::ACOS_F80,
5117 RTLIB::ACOS_F128, RTLIB::ACOS_PPCF128, Results);
5118 break;
5119 case ISD::FATAN:
5120 case ISD::STRICT_FATAN:
5121 ExpandFPLibCall(Node, RTLIB::ATAN_F32, RTLIB::ATAN_F64, RTLIB::ATAN_F80,
5122 RTLIB::ATAN_F128, RTLIB::ATAN_PPCF128, Results);
5123 break;
5124 case ISD::FATAN2:
5125 case ISD::STRICT_FATAN2:
5126 ExpandFPLibCall(Node, RTLIB::ATAN2_F32, RTLIB::ATAN2_F64, RTLIB::ATAN2_F80,
5127 RTLIB::ATAN2_F128, RTLIB::ATAN2_PPCF128, Results);
5128 break;
5129 case ISD::FSINH:
5130 case ISD::STRICT_FSINH:
5131 ExpandFPLibCall(Node, RTLIB::SINH_F32, RTLIB::SINH_F64, RTLIB::SINH_F80,
5132 RTLIB::SINH_F128, RTLIB::SINH_PPCF128, Results);
5133 break;
5134 case ISD::FCOSH:
5135 case ISD::STRICT_FCOSH:
5136 ExpandFPLibCall(Node, RTLIB::COSH_F32, RTLIB::COSH_F64, RTLIB::COSH_F80,
5137 RTLIB::COSH_F128, RTLIB::COSH_PPCF128, Results);
5138 break;
5139 case ISD::FTANH:
5140 case ISD::STRICT_FTANH:
5141 ExpandFPLibCall(Node, RTLIB::TANH_F32, RTLIB::TANH_F64, RTLIB::TANH_F80,
5142 RTLIB::TANH_F128, RTLIB::TANH_PPCF128, Results);
5143 break;
5144 case ISD::FSINCOS:
5145 case ISD::FSINCOSPI: {
5146 EVT VT = Node->getValueType(0);
5147
5148 if (Node->getOpcode() == ISD::FSINCOS) {
5149 RTLIB::Libcall SincosStret = RTLIB::getSINCOS_STRET(VT);
5150 if (SincosStret != RTLIB::UNKNOWN_LIBCALL) {
5151 if (SDValue Expanded = ExpandSincosStretLibCall(Node)) {
5152 Results.push_back(Expanded);
5153 Results.push_back(Expanded.getValue(1));
5154 break;
5155 }
5156 }
5157 }
5158
5159 RTLIB::Libcall LC = Node->getOpcode() == ISD::FSINCOS
5160 ? RTLIB::getSINCOS(VT)
5161 : RTLIB::getSINCOSPI(VT);
5162 bool Expanded = TLI.expandMultipleResultFPLibCall(DAG, LC, Node, Results);
5163 if (!Expanded) {
5164 DAG.getContext()->emitError(Twine("no libcall available for ") +
5165 Node->getOperationName(&DAG));
5166 SDValue Poison = DAG.getPOISON(VT);
5167 Results.push_back(Poison);
5168 Results.push_back(Poison);
5169 }
5170
5171 break;
5172 }
5173 case ISD::FLOG:
5174 case ISD::STRICT_FLOG:
5175 ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
5176 RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
5177 break;
5178 case ISD::FLOG2:
5179 case ISD::STRICT_FLOG2:
5180 ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
5181 RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
5182 break;
5183 case ISD::FLOG10:
5184 case ISD::STRICT_FLOG10:
5185 ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
5186 RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
5187 break;
5188 case ISD::FEXP:
5189 case ISD::STRICT_FEXP:
5190 ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
5191 RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
5192 break;
5193 case ISD::FEXP2:
5194 case ISD::STRICT_FEXP2:
5195 ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
5196 RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
5197 break;
5198 case ISD::FEXP10:
5199 ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80,
5200 RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results);
5201 break;
5202 case ISD::FTRUNC:
5203 case ISD::STRICT_FTRUNC:
5204 ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
5205 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
5206 RTLIB::TRUNC_PPCF128, Results);
5207 break;
5208 case ISD::FFLOOR:
5209 case ISD::STRICT_FFLOOR:
5210 ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
5211 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
5212 RTLIB::FLOOR_PPCF128, Results);
5213 break;
5214 case ISD::FCEIL:
5215 case ISD::STRICT_FCEIL:
5216 ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
5217 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
5218 RTLIB::CEIL_PPCF128, Results);
5219 break;
5220 case ISD::FRINT:
5221 case ISD::STRICT_FRINT:
5222 ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
5223 RTLIB::RINT_F80, RTLIB::RINT_F128,
5224 RTLIB::RINT_PPCF128, Results);
5225 break;
5226 case ISD::FNEARBYINT:
5228 ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
5229 RTLIB::NEARBYINT_F64,
5230 RTLIB::NEARBYINT_F80,
5231 RTLIB::NEARBYINT_F128,
5232 RTLIB::NEARBYINT_PPCF128, Results);
5233 break;
5234 case ISD::FROUND:
5235 case ISD::STRICT_FROUND:
5236 ExpandFPLibCall(Node, RTLIB::ROUND_F32,
5237 RTLIB::ROUND_F64,
5238 RTLIB::ROUND_F80,
5239 RTLIB::ROUND_F128,
5240 RTLIB::ROUND_PPCF128, Results);
5241 break;
5242 case ISD::FROUNDEVEN:
5244 ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
5245 RTLIB::ROUNDEVEN_F64,
5246 RTLIB::ROUNDEVEN_F80,
5247 RTLIB::ROUNDEVEN_F128,
5248 RTLIB::ROUNDEVEN_PPCF128, Results);
5249 break;
5250 case ISD::FLDEXP:
5251 case ISD::STRICT_FLDEXP:
5252 ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
5253 RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
5254 break;
5255 case ISD::FMODF:
5256 case ISD::FFREXP: {
5257 EVT VT = Node->getValueType(0);
5258 RTLIB::Libcall LC = Node->getOpcode() == ISD::FMODF ? RTLIB::getMODF(VT)
5259 : RTLIB::getFREXP(VT);
5260 bool Expanded = TLI.expandMultipleResultFPLibCall(DAG, LC, Node, Results,
5261 /*CallRetResNo=*/0);
5262 if (!Expanded)
5263 llvm_unreachable("Expected scalar FFREXP/FMODF to expand to libcall!");
5264 break;
5265 }
5266 case ISD::FPOWI:
5267 case ISD::STRICT_FPOWI: {
5268 RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
5269 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
5270 if (DAG.getLibcalls().getLibcallImpl(LC) == RTLIB::Unsupported) {
5271 // Some targets don't have a powi libcall; use pow instead.
5272 if (Node->isStrictFPOpcode()) {
5274 DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
5275 {Node->getValueType(0), Node->getValueType(1)},
5276 {Node->getOperand(0), Node->getOperand(2)});
5277 SDValue FPOW =
5278 DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node),
5279 {Node->getValueType(0), Node->getValueType(1)},
5280 {Exponent.getValue(1), Node->getOperand(1), Exponent});
5281 Results.push_back(FPOW);
5282 Results.push_back(FPOW.getValue(1));
5283 } else {
5285 DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0),
5286 Node->getOperand(1));
5287 Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node),
5288 Node->getValueType(0),
5289 Node->getOperand(0), Exponent));
5290 }
5291 break;
5292 }
5293 unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0;
5294 bool ExponentHasSizeOfInt =
5295 DAG.getLibInfo().getIntSize() ==
5296 Node->getOperand(1 + Offset).getValueType().getSizeInBits();
5297 if (!ExponentHasSizeOfInt) {
5298 // If the exponent does not match with sizeof(int) a libcall to
5299 // RTLIB::POWI would use the wrong type for the argument.
5300 DAG.getContext()->emitError("POWI exponent does not match sizeof(int)");
5301 Results.push_back(DAG.getPOISON(Node->getValueType(0)));
5302 break;
5303 }
5304 ExpandFPLibCall(Node, LC, Results);
5305 break;
5306 }
5307 case ISD::FPOW:
5308 case ISD::STRICT_FPOW:
5309 ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
5310 RTLIB::POW_F128, RTLIB::POW_PPCF128, Results);
5311 break;
5312 case ISD::LROUND:
5313 case ISD::STRICT_LROUND:
5314 ExpandArgFPLibCall(Node, RTLIB::LROUND_F32,
5315 RTLIB::LROUND_F64, RTLIB::LROUND_F80,
5316 RTLIB::LROUND_F128,
5317 RTLIB::LROUND_PPCF128, Results);
5318 break;
5319 case ISD::LLROUND:
5321 ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32,
5322 RTLIB::LLROUND_F64, RTLIB::LLROUND_F80,
5323 RTLIB::LLROUND_F128,
5324 RTLIB::LLROUND_PPCF128, Results);
5325 break;
5326 case ISD::LRINT:
5327 case ISD::STRICT_LRINT:
5328 ExpandArgFPLibCall(Node, RTLIB::LRINT_F32,
5329 RTLIB::LRINT_F64, RTLIB::LRINT_F80,
5330 RTLIB::LRINT_F128,
5331 RTLIB::LRINT_PPCF128, Results);
5332 break;
5333 case ISD::LLRINT:
5334 case ISD::STRICT_LLRINT:
5335 ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32,
5336 RTLIB::LLRINT_F64, RTLIB::LLRINT_F80,
5337 RTLIB::LLRINT_F128,
5338 RTLIB::LLRINT_PPCF128, Results);
5339 break;
5340 case ISD::FDIV:
5341 case ISD::STRICT_FDIV: {
5342 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
5343 {RTLIB::FAST_DIV_F32, RTLIB::DIV_F32},
5344 {RTLIB::FAST_DIV_F64, RTLIB::DIV_F64},
5345 {RTLIB::FAST_DIV_F80, RTLIB::DIV_F80},
5346 {RTLIB::FAST_DIV_F128, RTLIB::DIV_F128},
5347 {RTLIB::FAST_DIV_PPCF128, RTLIB::DIV_PPCF128}, Results);
5348 break;
5349 }
5350 case ISD::FREM:
5351 case ISD::STRICT_FREM:
5352 ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
5353 RTLIB::REM_F80, RTLIB::REM_F128,
5354 RTLIB::REM_PPCF128, Results);
5355 break;
5356 case ISD::FMA:
5357 case ISD::STRICT_FMA:
5358 ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
5359 RTLIB::FMA_F80, RTLIB::FMA_F128,
5360 RTLIB::FMA_PPCF128, Results);
5361 break;
5362 case ISD::FADD:
5363 case ISD::STRICT_FADD: {
5364 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
5365 {RTLIB::FAST_ADD_F32, RTLIB::ADD_F32},
5366 {RTLIB::FAST_ADD_F64, RTLIB::ADD_F64},
5367 {RTLIB::FAST_ADD_F80, RTLIB::ADD_F80},
5368 {RTLIB::FAST_ADD_F128, RTLIB::ADD_F128},
5369 {RTLIB::FAST_ADD_PPCF128, RTLIB::ADD_PPCF128}, Results);
5370 break;
5371 }
5372 case ISD::FMUL:
5373 case ISD::STRICT_FMUL: {
5374 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
5375 {RTLIB::FAST_MUL_F32, RTLIB::MUL_F32},
5376 {RTLIB::FAST_MUL_F64, RTLIB::MUL_F64},
5377 {RTLIB::FAST_MUL_F80, RTLIB::MUL_F80},
5378 {RTLIB::FAST_MUL_F128, RTLIB::MUL_F128},
5379 {RTLIB::FAST_MUL_PPCF128, RTLIB::MUL_PPCF128}, Results);
5380 break;
5381 }
5382 case ISD::FP16_TO_FP:
5383 if (Node->getValueType(0) == MVT::f32) {
5384 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first);
5385 }
5386 break;
5388 if (Node->getValueType(0) == MVT::f32) {
5389 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
5390 DAG, RTLIB::FPEXT_BF16_F32, MVT::f32, Node->getOperand(1),
5391 CallOptions, SDLoc(Node), Node->getOperand(0));
5392 Results.push_back(Tmp.first);
5393 Results.push_back(Tmp.second);
5394 }
5395 break;
5397 if (Node->getValueType(0) == MVT::f32) {
5398 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
5399 DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions,
5400 SDLoc(Node), Node->getOperand(0));
5401 Results.push_back(Tmp.first);
5402 Results.push_back(Tmp.second);
5403 }
5404 break;
5405 }
5406 case ISD::FP_TO_FP16: {
5407 RTLIB::Libcall LC =
5408 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
5409 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
5410 Results.push_back(ExpandLibCall(LC, Node, false).first);
5411 break;
5412 }
5413 case ISD::FP_TO_BF16: {
5414 RTLIB::Libcall LC =
5415 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16);
5416 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16");
5417 Results.push_back(ExpandLibCall(LC, Node, false).first);
5418 break;
5419 }
5422 case ISD::SINT_TO_FP:
5423 case ISD::UINT_TO_FP: {
5424 // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP
5425 bool IsStrict = Node->isStrictFPOpcode();
5426 bool Signed = Node->getOpcode() == ISD::SINT_TO_FP ||
5427 Node->getOpcode() == ISD::STRICT_SINT_TO_FP;
5428 EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType();
5429 EVT RVT = Node->getValueType(0);
5430 EVT NVT = EVT();
5431 SDLoc dl(Node);
5432
5433 // Even if the input is legal, no libcall may exactly match, eg. we don't
5434 // have i1 -> fp conversions. So, it needs to be promoted to a larger type,
5435 // eg: i13 -> fp. Then, look for an appropriate libcall.
5436 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5437 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
5438 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
5439 ++t) {
5440 NVT = (MVT::SimpleValueType)t;
5441 // The source needs to big enough to hold the operand.
5442 if (NVT.bitsGE(SVT))
5443 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT)
5444 : RTLIB::getUINTTOFP(NVT, RVT);
5445 }
5446 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5447
5448 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
5449 // Sign/zero extend the argument if the libcall takes a larger type.
5451 NVT, Node->getOperand(IsStrict ? 1 : 0));
5452 CallOptions.setIsSigned(Signed);
5453 std::pair<SDValue, SDValue> Tmp =
5454 TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain);
5455 Results.push_back(Tmp.first);
5456 if (IsStrict)
5457 Results.push_back(Tmp.second);
5458 break;
5459 }
5460 case ISD::FP_TO_SINT:
5461 case ISD::FP_TO_UINT:
5464 // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT.
5465 bool IsStrict = Node->isStrictFPOpcode();
5466 bool Signed = Node->getOpcode() == ISD::FP_TO_SINT ||
5467 Node->getOpcode() == ISD::STRICT_FP_TO_SINT;
5468
5469 SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
5470 EVT SVT = Op.getValueType();
5471 EVT RVT = Node->getValueType(0);
5472 EVT NVT = EVT();
5473 SDLoc dl(Node);
5474
5475 // Even if the result is legal, no libcall may exactly match, eg. we don't
5476 // have fp -> i1 conversions. So, it needs to be promoted to a larger type,
5477 // eg: fp -> i32. Then, look for an appropriate libcall.
5478 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5479 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
5480 IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
5481 ++IntVT) {
5482 NVT = (MVT::SimpleValueType)IntVT;
5483 // The type needs to big enough to hold the result.
5484 if (NVT.bitsGE(RVT))
5485 LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT)
5486 : RTLIB::getFPTOUINT(SVT, NVT);
5487 }
5488 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5489
5490 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
5491 std::pair<SDValue, SDValue> Tmp =
5492 TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain);
5493
5494 // Truncate the result if the libcall returns a larger type.
5495 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first));
5496 if (IsStrict)
5497 Results.push_back(Tmp.second);
5498 break;
5499 }
5500
5501 case ISD::FP_ROUND:
5502 case ISD::STRICT_FP_ROUND: {
5503 // X = FP_ROUND(Y, TRUNC)
5504 // TRUNC is a flag, which is always an integer that is zero or one.
5505 // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND
5506 // is known to not change the value of Y.
5507 // We can only expand it into libcall if the TRUNC is 0.
5508 bool IsStrict = Node->isStrictFPOpcode();
5509 SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
5510 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
5511 EVT VT = Node->getValueType(0);
5512 assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() &&
5513 "Unable to expand as libcall if it is not normal rounding");
5514
5515 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT);
5516 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5517
5518 std::pair<SDValue, SDValue> Tmp =
5519 TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain);
5520 Results.push_back(Tmp.first);
5521 if (IsStrict)
5522 Results.push_back(Tmp.second);
5523 break;
5524 }
5525 case ISD::FP_EXTEND: {
5526 Results.push_back(
5527 ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(),
5528 Node->getValueType(0)),
5529 Node, false).first);
5530 break;
5531 }
5535 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5536 if (Node->getOpcode() == ISD::STRICT_FP_TO_FP16)
5537 LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16);
5538 else if (Node->getOpcode() == ISD::STRICT_FP_TO_BF16)
5539 LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::bf16);
5540 else
5541 LC = RTLIB::getFPEXT(Node->getOperand(1).getValueType(),
5542 Node->getValueType(0));
5543
5544 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5545
5546 std::pair<SDValue, SDValue> Tmp =
5547 TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1),
5548 CallOptions, SDLoc(Node), Node->getOperand(0));
5549 Results.push_back(Tmp.first);
5550 Results.push_back(Tmp.second);
5551 break;
5552 }
5553 case ISD::FSUB:
5554 case ISD::STRICT_FSUB: {
5555 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
5556 {RTLIB::FAST_SUB_F32, RTLIB::SUB_F32},
5557 {RTLIB::FAST_SUB_F64, RTLIB::SUB_F64},
5558 {RTLIB::FAST_SUB_F80, RTLIB::SUB_F80},
5559 {RTLIB::FAST_SUB_F128, RTLIB::SUB_F128},
5560 {RTLIB::FAST_SUB_PPCF128, RTLIB::SUB_PPCF128}, Results);
5561 break;
5562 }
5563 case ISD::SREM:
5564 Results.push_back(ExpandIntLibCall(Node, true,
5565 RTLIB::SREM_I8,
5566 RTLIB::SREM_I16, RTLIB::SREM_I32,
5567 RTLIB::SREM_I64, RTLIB::SREM_I128));
5568 break;
5569 case ISD::UREM:
5570 Results.push_back(ExpandIntLibCall(Node, false,
5571 RTLIB::UREM_I8,
5572 RTLIB::UREM_I16, RTLIB::UREM_I32,
5573 RTLIB::UREM_I64, RTLIB::UREM_I128));
5574 break;
5575 case ISD::SDIV:
5576 Results.push_back(ExpandIntLibCall(Node, true,
5577 RTLIB::SDIV_I8,
5578 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
5579 RTLIB::SDIV_I64, RTLIB::SDIV_I128));
5580 break;
5581 case ISD::UDIV:
5582 Results.push_back(ExpandIntLibCall(Node, false,
5583 RTLIB::UDIV_I8,
5584 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
5585 RTLIB::UDIV_I64, RTLIB::UDIV_I128));
5586 break;
5587 case ISD::SDIVREM:
5588 case ISD::UDIVREM:
5589 // Expand into divrem libcall
5590 ExpandDivRemLibCall(Node, Results);
5591 break;
5592 case ISD::MUL:
5593 Results.push_back(ExpandIntLibCall(Node, false,
5594 RTLIB::MUL_I8,
5595 RTLIB::MUL_I16, RTLIB::MUL_I32,
5596 RTLIB::MUL_I64, RTLIB::MUL_I128));
5597 break;
5599 Results.push_back(ExpandBitCountingLibCall(
5600 Node, RTLIB::CTLZ_I32, RTLIB::CTLZ_I64, RTLIB::CTLZ_I128));
5601 break;
5602 case ISD::CTPOP:
5603 Results.push_back(ExpandBitCountingLibCall(
5604 Node, RTLIB::CTPOP_I32, RTLIB::CTPOP_I64, RTLIB::CTPOP_I128));
5605 break;
5606 case ISD::RESET_FPENV: {
5607 // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
5608 // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc.
5609 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
5610 SDValue Ptr = DAG.getAllOnesConstant(dl, PtrTy);
5611 SDValue Chain = Node->getOperand(0);
5612 Results.push_back(
5613 DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl));
5614 break;
5615 }
5616 case ISD::GET_FPENV_MEM: {
5617 SDValue Chain = Node->getOperand(0);
5618 SDValue EnvPtr = Node->getOperand(1);
5619 Results.push_back(
5620 DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl));
5621 break;
5622 }
5623 case ISD::SET_FPENV_MEM: {
5624 SDValue Chain = Node->getOperand(0);
5625 SDValue EnvPtr = Node->getOperand(1);
5626 Results.push_back(
5627 DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl));
5628 break;
5629 }
5630 case ISD::GET_FPMODE: {
5631 // Call fegetmode, which saves control modes into a stack slot. Then load
5632 // the value to return from the stack.
5633 EVT ModeVT = Node->getValueType(0);
5635 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
5636 SDValue Chain = DAG.makeStateFunctionCall(RTLIB::FEGETMODE, StackPtr,
5637 Node->getOperand(0), dl);
5638 SDValue LdInst = DAG.getLoad(
5639 ModeVT, dl, Chain, StackPtr,
5641 Results.push_back(LdInst);
5642 Results.push_back(LdInst.getValue(1));
5643 break;
5644 }
5645 case ISD::SET_FPMODE: {
5646 // Move control modes to stack slot and then call fesetmode with the pointer
5647 // to the slot as argument.
5648 SDValue Mode = Node->getOperand(1);
5649 EVT ModeVT = Mode.getValueType();
5651 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
5652 SDValue StInst = DAG.getStore(
5653 Node->getOperand(0), dl, Mode, StackPtr,
5655 Results.push_back(
5656 DAG.makeStateFunctionCall(RTLIB::FESETMODE, StackPtr, StInst, dl));
5657 break;
5658 }
5659 case ISD::RESET_FPMODE: {
5660 // It is legalized to a call 'fesetmode(FE_DFL_MODE)'. On most targets
5661 // FE_DFL_MODE is defined as '((const femode_t *) -1)' in glibc. If not, the
5662 // target must provide custom lowering.
5663 const DataLayout &DL = DAG.getDataLayout();
5664 EVT PtrTy = TLI.getPointerTy(DL);
5665 SDValue Mode = DAG.getAllOnesConstant(dl, PtrTy);
5666 Results.push_back(DAG.makeStateFunctionCall(RTLIB::FESETMODE, Mode,
5667 Node->getOperand(0), dl));
5668 break;
5669 }
5670 }
5671
5672 // Replace the original node with the legalized result.
5673 if (!Results.empty()) {
5674 LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n");
5675 ReplaceNode(Node, Results.data());
5676 } else
5677 LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n");
5678}
5679
5680// Determine the vector type to use in place of an original scalar element when
5681// promoting equally sized vectors.
5683 MVT EltVT, MVT NewEltVT) {
5684 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
5685 MVT MidVT = OldEltsPerNewElt == 1
5686 ? NewEltVT
5687 : MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
5688 assert(TLI.isTypeLegal(MidVT) && "unexpected");
5689 return MidVT;
5690}
5691
5692void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
5693 LLVM_DEBUG(dbgs() << "Trying to promote node\n");
5695 MVT OVT = Node->getSimpleValueType(0);
5696 if (Node->getOpcode() == ISD::UINT_TO_FP ||
5697 Node->getOpcode() == ISD::SINT_TO_FP ||
5698 Node->getOpcode() == ISD::SETCC ||
5699 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
5700 Node->getOpcode() == ISD::INSERT_VECTOR_ELT ||
5701 Node->getOpcode() == ISD::VECREDUCE_FMAX ||
5702 Node->getOpcode() == ISD::VECREDUCE_FMIN ||
5703 Node->getOpcode() == ISD::VECREDUCE_FMAXIMUM ||
5704 Node->getOpcode() == ISD::VECREDUCE_FMINIMUM) {
5705 OVT = Node->getOperand(0).getSimpleValueType();
5706 }
5707 if (Node->getOpcode() == ISD::ATOMIC_STORE ||
5708 Node->getOpcode() == ISD::STRICT_UINT_TO_FP ||
5709 Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
5710 Node->getOpcode() == ISD::STRICT_FSETCC ||
5711 Node->getOpcode() == ISD::STRICT_FSETCCS ||
5712 Node->getOpcode() == ISD::STRICT_LRINT ||
5713 Node->getOpcode() == ISD::STRICT_LLRINT ||
5714 Node->getOpcode() == ISD::STRICT_LROUND ||
5715 Node->getOpcode() == ISD::STRICT_LLROUND ||
5716 Node->getOpcode() == ISD::VP_REDUCE_FADD ||
5717 Node->getOpcode() == ISD::VP_REDUCE_FMUL ||
5718 Node->getOpcode() == ISD::VP_REDUCE_FMAX ||
5719 Node->getOpcode() == ISD::VP_REDUCE_FMIN ||
5720 Node->getOpcode() == ISD::VP_REDUCE_FMAXIMUM ||
5721 Node->getOpcode() == ISD::VP_REDUCE_FMINIMUM ||
5722 Node->getOpcode() == ISD::VP_REDUCE_SEQ_FADD)
5723 OVT = Node->getOperand(1).getSimpleValueType();
5724 if (Node->getOpcode() == ISD::BR_CC ||
5725 Node->getOpcode() == ISD::SELECT_CC)
5726 OVT = Node->getOperand(2).getSimpleValueType();
5727 // Preserve fast math flags
5728 SDNodeFlags FastMathFlags = Node->getFlags() & SDNodeFlags::FastMathFlags;
5729 SelectionDAG::FlagInserter FlagsInserter(DAG, FastMathFlags);
5730 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
5731 SDLoc dl(Node);
5732 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
5733 switch (Node->getOpcode()) {
5734 case ISD::CTTZ:
5736 case ISD::CTLZ:
5737 case ISD::CTPOP: {
5738 // Zero extend the argument unless its cttz, then use any_extend.
5739 if (Node->getOpcode() == ISD::CTTZ ||
5740 Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF)
5741 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5742 else
5743 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
5744
5745 unsigned NewOpc = Node->getOpcode();
5746 if (NewOpc == ISD::CTTZ) {
5747 // The count is the same in the promoted type except if the original
5748 // value was zero. This can be handled by setting the bit just off
5749 // the top of the original type.
5750 auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
5751 OVT.getSizeInBits());
5752 Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
5753 DAG.getConstant(TopBit, dl, NVT));
5754 NewOpc = ISD::CTTZ_ZERO_UNDEF;
5755 }
5756 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
5757 // already the correct result.
5758 Tmp1 = DAG.getNode(NewOpc, dl, NVT, Tmp1);
5759 if (NewOpc == ISD::CTLZ) {
5760 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
5761 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
5762 DAG.getConstant(NVT.getSizeInBits() -
5763 OVT.getSizeInBits(), dl, NVT));
5764 }
5765 Results.push_back(
5766 DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1, SDNodeFlags::NoWrap));
5767 break;
5768 }
5769 case ISD::CTLZ_ZERO_UNDEF: {
5770 // We know that the argument is unlikely to be zero, hence we can take a
5771 // different approach as compared to ISD::CTLZ
5772
5773 // Any Extend the argument
5774 auto AnyExtendedNode =
5775 DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5776
5777 // Tmp1 = Tmp1 << (sizeinbits(NVT) - sizeinbits(Old VT))
5778 auto ShiftConstant = DAG.getShiftAmountConstant(
5779 NVT.getSizeInBits() - OVT.getSizeInBits(), NVT, dl);
5780 auto LeftShiftResult =
5781 DAG.getNode(ISD::SHL, dl, NVT, AnyExtendedNode, ShiftConstant);
5782
5783 // Perform the larger operation
5784 auto CTLZResult = DAG.getNode(Node->getOpcode(), dl, NVT, LeftShiftResult);
5785 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, CTLZResult));
5786 break;
5787 }
5788 case ISD::BITREVERSE:
5789 case ISD::BSWAP: {
5790 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
5791 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
5792 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5793 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
5794 DAG.getShiftAmountConstant(DiffBits, NVT, dl));
5795
5796 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5797 break;
5798 }
5799 case ISD::FP_TO_UINT:
5801 case ISD::FP_TO_SINT:
5803 PromoteLegalFP_TO_INT(Node, dl, Results);
5804 break;
5807 Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl));
5808 break;
5809 case ISD::UINT_TO_FP:
5811 case ISD::SINT_TO_FP:
5813 PromoteLegalINT_TO_FP(Node, dl, Results);
5814 break;
5815 case ISD::VAARG: {
5816 SDValue Chain = Node->getOperand(0); // Get the chain.
5817 SDValue Ptr = Node->getOperand(1); // Get the pointer.
5818
5819 unsigned TruncOp;
5820 if (OVT.isVector()) {
5821 TruncOp = ISD::BITCAST;
5822 } else {
5823 assert(OVT.isInteger()
5824 && "VAARG promotion is supported only for vectors or integer types");
5825 TruncOp = ISD::TRUNCATE;
5826 }
5827
5828 // Perform the larger operation, then convert back
5829 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
5830 Node->getConstantOperandVal(3));
5831 Chain = Tmp1.getValue(1);
5832
5833 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
5834
5835 // Modified the chain result - switch anything that used the old chain to
5836 // use the new one.
5837 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
5838 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
5839 if (UpdatedNodes) {
5840 UpdatedNodes->insert(Tmp2.getNode());
5841 UpdatedNodes->insert(Chain.getNode());
5842 }
5843 ReplacedNode(Node);
5844 break;
5845 }
5846 case ISD::MUL:
5847 case ISD::SDIV:
5848 case ISD::SREM:
5849 case ISD::UDIV:
5850 case ISD::UREM:
5851 case ISD::SMIN:
5852 case ISD::SMAX:
5853 case ISD::UMIN:
5854 case ISD::UMAX:
5855 case ISD::AND:
5856 case ISD::OR:
5857 case ISD::XOR: {
5858 unsigned ExtOp, TruncOp;
5859 if (OVT.isVector()) {
5860 ExtOp = ISD::BITCAST;
5861 TruncOp = ISD::BITCAST;
5862 } else {
5863 assert(OVT.isInteger() && "Cannot promote logic operation");
5864
5865 switch (Node->getOpcode()) {
5866 default:
5867 ExtOp = ISD::ANY_EXTEND;
5868 break;
5869 case ISD::SDIV:
5870 case ISD::SREM:
5871 case ISD::SMIN:
5872 case ISD::SMAX:
5873 ExtOp = ISD::SIGN_EXTEND;
5874 break;
5875 case ISD::UDIV:
5876 case ISD::UREM:
5877 ExtOp = ISD::ZERO_EXTEND;
5878 break;
5879 case ISD::UMIN:
5880 case ISD::UMAX:
5881 if (TLI.isSExtCheaperThanZExt(OVT, NVT))
5882 ExtOp = ISD::SIGN_EXTEND;
5883 else
5884 ExtOp = ISD::ZERO_EXTEND;
5885 break;
5886 }
5887 TruncOp = ISD::TRUNCATE;
5888 }
5889 // Promote each of the values to the new type.
5890 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5891 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5892 // Perform the larger operation, then convert back
5893 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
5894 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
5895 break;
5896 }
5897 case ISD::UMUL_LOHI:
5898 case ISD::SMUL_LOHI: {
5899 // Promote to a multiply in a wider integer type.
5900 unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND
5902 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5903 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5904 Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2);
5905
5906 unsigned OriginalSize = OVT.getScalarSizeInBits();
5907 Tmp2 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
5908 DAG.getShiftAmountConstant(OriginalSize, NVT, dl));
5909 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5910 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
5911 break;
5912 }
5913 case ISD::SELECT: {
5914 unsigned ExtOp, TruncOp;
5915 if (Node->getValueType(0).isVector() ||
5916 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
5917 ExtOp = ISD::BITCAST;
5918 TruncOp = ISD::BITCAST;
5919 } else if (Node->getValueType(0).isInteger()) {
5920 ExtOp = ISD::ANY_EXTEND;
5921 TruncOp = ISD::TRUNCATE;
5922 } else {
5923 ExtOp = ISD::FP_EXTEND;
5924 TruncOp = ISD::FP_ROUND;
5925 }
5926 Tmp1 = Node->getOperand(0);
5927 // Promote each of the values to the new type.
5928 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5929 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5930 // Perform the larger operation, then round down.
5931 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
5932 if (TruncOp != ISD::FP_ROUND)
5933 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
5934 else
5935 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
5936 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5937 Results.push_back(Tmp1);
5938 break;
5939 }
5940 case ISD::VECTOR_SHUFFLE: {
5941 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
5942
5943 // Cast the two input vectors.
5944 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
5945 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
5946
5947 // Convert the shuffle mask to the right # elements.
5948 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
5949 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
5950 Results.push_back(Tmp1);
5951 break;
5952 }
5955 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5956 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1));
5957 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
5958 Node->getOperand(2));
5959 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3));
5960 break;
5961 }
5962 case ISD::SELECT_CC: {
5963 SDValue Cond = Node->getOperand(4);
5964 ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get();
5965 // Type of the comparison operands.
5966 MVT CVT = Node->getSimpleValueType(0);
5967 assert(CVT == OVT && "not handled");
5968
5969 unsigned ExtOp = ISD::FP_EXTEND;
5970 if (NVT.isInteger()) {
5972 }
5973
5974 // Promote the comparison operands, if needed.
5975 if (TLI.isCondCodeLegal(CCCode, CVT)) {
5976 Tmp1 = Node->getOperand(0);
5977 Tmp2 = Node->getOperand(1);
5978 } else {
5979 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5980 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5981 }
5982 // Cast the true/false operands.
5983 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5984 Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5985
5986 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond},
5987 Node->getFlags());
5988
5989 // Cast the result back to the original type.
5990 if (ExtOp != ISD::FP_EXTEND)
5991 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1);
5992 else
5993 Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1,
5994 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5995
5996 Results.push_back(Tmp1);
5997 break;
5998 }
5999 case ISD::SETCC:
6000 case ISD::STRICT_FSETCC:
6001 case ISD::STRICT_FSETCCS: {
6002 unsigned ExtOp = ISD::FP_EXTEND;
6003 if (NVT.isInteger()) {
6004 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
6005 if (isSignedIntSetCC(CCCode) ||
6006 TLI.isSExtCheaperThanZExt(Node->getOperand(0).getValueType(), NVT))
6007 ExtOp = ISD::SIGN_EXTEND;
6008 else
6009 ExtOp = ISD::ZERO_EXTEND;
6010 }
6011 if (Node->isStrictFPOpcode()) {
6012 SDValue InChain = Node->getOperand(0);
6013 std::tie(Tmp1, std::ignore) =
6014 DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT);
6015 std::tie(Tmp2, std::ignore) =
6016 DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT);
6017 SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)};
6018 SDValue OutChain = DAG.getTokenFactor(dl, TmpChains);
6019 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
6020 Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs,
6021 {OutChain, Tmp1, Tmp2, Node->getOperand(3)},
6022 Node->getFlags()));
6023 Results.push_back(Results.back().getValue(1));
6024 break;
6025 }
6026 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
6027 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
6028 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1,
6029 Tmp2, Node->getOperand(2), Node->getFlags()));
6030 break;
6031 }
6032 case ISD::BR_CC: {
6033 unsigned ExtOp = ISD::FP_EXTEND;
6034 if (NVT.isInteger()) {
6035 ISD::CondCode CCCode =
6036 cast<CondCodeSDNode>(Node->getOperand(1))->get();
6038 }
6039 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
6040 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
6041 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
6042 Node->getOperand(0), Node->getOperand(1),
6043 Tmp1, Tmp2, Node->getOperand(4)));
6044 break;
6045 }
6046 case ISD::FADD:
6047 case ISD::FSUB:
6048 case ISD::FMUL:
6049 case ISD::FDIV:
6050 case ISD::FREM:
6051 case ISD::FMINNUM:
6052 case ISD::FMAXNUM:
6053 case ISD::FMINIMUM:
6054 case ISD::FMAXIMUM:
6055 case ISD::FMINIMUMNUM:
6056 case ISD::FMAXIMUMNUM:
6057 case ISD::FPOW:
6058 case ISD::FATAN2:
6059 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6060 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
6061 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
6062 Results.push_back(
6063 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
6064 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
6065 break;
6066
6068 case ISD::STRICT_FMAXIMUM: {
6069 SDValue InChain = Node->getOperand(0);
6070 SDVTList VTs = DAG.getVTList(NVT, MVT::Other);
6071 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, VTs, InChain,
6072 Node->getOperand(1));
6073 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, VTs, InChain,
6074 Node->getOperand(2));
6075 SmallVector<SDValue, 4> Ops = {InChain, Tmp1, Tmp2};
6076 Tmp3 = DAG.getNode(Node->getOpcode(), dl, VTs, Ops, Node->getFlags());
6077 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, DAG.getVTList(OVT, MVT::Other),
6078 InChain, Tmp3,
6079 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
6080 Results.push_back(Tmp4);
6081 Results.push_back(Tmp4.getValue(1));
6082 break;
6083 }
6084
6085 case ISD::STRICT_FADD:
6086 case ISD::STRICT_FSUB:
6087 case ISD::STRICT_FMUL:
6088 case ISD::STRICT_FDIV:
6091 case ISD::STRICT_FREM:
6092 case ISD::STRICT_FPOW:
6093 case ISD::STRICT_FATAN2:
6094 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6095 {Node->getOperand(0), Node->getOperand(1)});
6096 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6097 {Node->getOperand(0), Node->getOperand(2)});
6098 Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
6099 Tmp2.getValue(1));
6100 Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
6101 {Tmp3, Tmp1, Tmp2});
6102 Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
6103 {Tmp1.getValue(1), Tmp1,
6104 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
6105 Results.push_back(Tmp1);
6106 Results.push_back(Tmp1.getValue(1));
6107 break;
6108 case ISD::FMA:
6109 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6110 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
6111 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
6112 Results.push_back(
6113 DAG.getNode(ISD::FP_ROUND, dl, OVT,
6114 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
6115 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
6116 break;
6117 case ISD::STRICT_FMA:
6118 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6119 {Node->getOperand(0), Node->getOperand(1)});
6120 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6121 {Node->getOperand(0), Node->getOperand(2)});
6122 Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6123 {Node->getOperand(0), Node->getOperand(3)});
6124 Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
6125 Tmp2.getValue(1), Tmp3.getValue(1));
6126 Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
6127 {Tmp4, Tmp1, Tmp2, Tmp3});
6128 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
6129 {Tmp4.getValue(1), Tmp4,
6130 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
6131 Results.push_back(Tmp4);
6132 Results.push_back(Tmp4.getValue(1));
6133 break;
6134 case ISD::FCOPYSIGN:
6135 case ISD::FLDEXP:
6136 case ISD::FPOWI: {
6137 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6138 Tmp2 = Node->getOperand(1);
6139 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
6140
6141 // fcopysign doesn't change anything but the sign bit, so
6142 // (fp_round (fcopysign (fpext a), b))
6143 // is as precise as
6144 // (fp_round (fpext a))
6145 // which is a no-op. Mark it as a TRUNCating FP_ROUND.
6146 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
6147 Results.push_back(
6148 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
6149 DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true)));
6150 break;
6151 }
6152 case ISD::STRICT_FLDEXP: {
6153 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6154 {Node->getOperand(0), Node->getOperand(1)});
6155 Tmp2 = Node->getOperand(2);
6156 Tmp3 = DAG.getNode(ISD::STRICT_FLDEXP, dl, {NVT, MVT::Other},
6157 {Tmp1.getValue(1), Tmp1, Tmp2});
6158 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
6159 {Tmp3.getValue(1), Tmp3,
6160 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
6161 Results.push_back(Tmp4);
6162 Results.push_back(Tmp4.getValue(1));
6163 break;
6164 }
6165 case ISD::STRICT_FPOWI:
6166 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6167 {Node->getOperand(0), Node->getOperand(1)});
6168 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
6169 {Tmp1.getValue(1), Tmp1, Node->getOperand(2)});
6170 Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
6171 {Tmp2.getValue(1), Tmp2,
6172 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
6173 Results.push_back(Tmp3);
6174 Results.push_back(Tmp3.getValue(1));
6175 break;
6176 case ISD::FFREXP: {
6177 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6178 Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1);
6179
6180 Results.push_back(
6181 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
6182 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
6183
6184 Results.push_back(Tmp2.getValue(1));
6185 break;
6186 }
6187 case ISD::FMODF:
6188 case ISD::FSINCOS:
6189 case ISD::FSINCOSPI: {
6190 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6191 Tmp2 = DAG.getNode(Node->getOpcode(), dl, DAG.getVTList(NVT, NVT), Tmp1);
6192 Tmp3 = DAG.getIntPtrConstant(0, dl, /*isTarget=*/true);
6193 for (unsigned ResNum = 0; ResNum < Node->getNumValues(); ResNum++)
6194 Results.push_back(
6195 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2.getValue(ResNum), Tmp3));
6196 break;
6197 }
6198 case ISD::FFLOOR:
6199 case ISD::FCEIL:
6200 case ISD::FRINT:
6201 case ISD::FNEARBYINT:
6202 case ISD::FROUND:
6203 case ISD::FROUNDEVEN:
6204 case ISD::FTRUNC:
6205 case ISD::FNEG:
6206 case ISD::FSQRT:
6207 case ISD::FSIN:
6208 case ISD::FCOS:
6209 case ISD::FTAN:
6210 case ISD::FASIN:
6211 case ISD::FACOS:
6212 case ISD::FATAN:
6213 case ISD::FSINH:
6214 case ISD::FCOSH:
6215 case ISD::FTANH:
6216 case ISD::FLOG:
6217 case ISD::FLOG2:
6218 case ISD::FLOG10:
6219 case ISD::FABS:
6220 case ISD::FEXP:
6221 case ISD::FEXP2:
6222 case ISD::FEXP10:
6223 case ISD::FCANONICALIZE:
6224 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6225 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
6226 Results.push_back(
6227 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
6228 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
6229 break;
6230 case ISD::STRICT_FFLOOR:
6231 case ISD::STRICT_FCEIL:
6232 case ISD::STRICT_FRINT:
6234 case ISD::STRICT_FROUND:
6236 case ISD::STRICT_FTRUNC:
6237 case ISD::STRICT_FSQRT:
6238 case ISD::STRICT_FSIN:
6239 case ISD::STRICT_FCOS:
6240 case ISD::STRICT_FTAN:
6241 case ISD::STRICT_FASIN:
6242 case ISD::STRICT_FACOS:
6243 case ISD::STRICT_FATAN:
6244 case ISD::STRICT_FSINH:
6245 case ISD::STRICT_FCOSH:
6246 case ISD::STRICT_FTANH:
6247 case ISD::STRICT_FLOG:
6248 case ISD::STRICT_FLOG2:
6249 case ISD::STRICT_FLOG10:
6250 case ISD::STRICT_FEXP:
6251 case ISD::STRICT_FEXP2:
6252 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6253 {Node->getOperand(0), Node->getOperand(1)});
6254 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
6255 {Tmp1.getValue(1), Tmp1});
6256 Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
6257 {Tmp2.getValue(1), Tmp2,
6258 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
6259 Results.push_back(Tmp3);
6260 Results.push_back(Tmp3.getValue(1));
6261 break;
6262 case ISD::LLROUND:
6263 case ISD::LROUND:
6264 case ISD::LRINT:
6265 case ISD::LLRINT:
6266 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
6267 Tmp2 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1);
6268 Results.push_back(Tmp2);
6269 break;
6271 case ISD::STRICT_LROUND:
6272 case ISD::STRICT_LRINT:
6273 case ISD::STRICT_LLRINT:
6274 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
6275 {Node->getOperand(0), Node->getOperand(1)});
6276 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
6277 {Tmp1.getValue(1), Tmp1});
6278 Results.push_back(Tmp2);
6279 Results.push_back(Tmp2.getValue(1));
6280 break;
6281 case ISD::BUILD_VECTOR: {
6282 MVT EltVT = OVT.getVectorElementType();
6283 MVT NewEltVT = NVT.getVectorElementType();
6284
6285 // Handle bitcasts to a different vector type with the same total bit size
6286 //
6287 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
6288 // =>
6289 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
6290
6291 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
6292 "Invalid promote type for build_vector");
6293 assert(NewEltVT.bitsLE(EltVT) && "not handled");
6294
6295 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
6296
6298 for (const SDValue &Op : Node->op_values())
6299 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
6300
6301 SDLoc SL(Node);
6302 SDValue Concat =
6303 DAG.getNode(MidVT == NewEltVT ? ISD::BUILD_VECTOR : ISD::CONCAT_VECTORS,
6304 SL, NVT, NewOps);
6305 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
6306 Results.push_back(CvtVec);
6307 break;
6308 }
6310 MVT EltVT = OVT.getVectorElementType();
6311 MVT NewEltVT = NVT.getVectorElementType();
6312
6313 // Handle bitcasts to a different vector type with the same total bit size.
6314 //
6315 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
6316 // =>
6317 // v4i32:castx = bitcast x:v2i64
6318 //
6319 // i64 = bitcast
6320 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
6321 // (i32 (extract_vector_elt castx, (2 * y + 1)))
6322 //
6323
6324 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
6325 "Invalid promote type for extract_vector_elt");
6326 assert(NewEltVT.bitsLT(EltVT) && "not handled");
6327
6328 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
6329 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
6330
6331 SDValue Idx = Node->getOperand(1);
6332 EVT IdxVT = Idx.getValueType();
6333 SDLoc SL(Node);
6334 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
6335 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
6336
6337 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
6338
6340 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
6341 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
6342 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
6343
6344 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
6345 CastVec, TmpIdx);
6346 NewOps.push_back(Elt);
6347 }
6348
6349 SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps);
6350 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
6351 break;
6352 }
6354 MVT EltVT = OVT.getVectorElementType();
6355 MVT NewEltVT = NVT.getVectorElementType();
6356
6357 // Handle bitcasts to a different vector type with the same total bit size
6358 //
6359 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
6360 // =>
6361 // v4i32:castx = bitcast x:v2i64
6362 // v2i32:casty = bitcast y:i64
6363 //
6364 // v2i64 = bitcast
6365 // (v4i32 insert_vector_elt
6366 // (v4i32 insert_vector_elt v4i32:castx,
6367 // (extract_vector_elt casty, 0), 2 * z),
6368 // (extract_vector_elt casty, 1), (2 * z + 1))
6369
6370 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
6371 "Invalid promote type for insert_vector_elt");
6372 assert(NewEltVT.bitsLT(EltVT) && "not handled");
6373
6374 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
6375 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
6376
6377 SDValue Val = Node->getOperand(1);
6378 SDValue Idx = Node->getOperand(2);
6379 EVT IdxVT = Idx.getValueType();
6380 SDLoc SL(Node);
6381
6382 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
6383 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
6384
6385 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
6386 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
6387
6388 SDValue NewVec = CastVec;
6389 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
6390 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
6391 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
6392
6393 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
6394 CastVal, IdxOffset);
6395
6396 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
6397 NewVec, Elt, InEltIdx);
6398 }
6399
6400 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
6401 break;
6402 }
6403 case ISD::SCALAR_TO_VECTOR: {
6404 MVT EltVT = OVT.getVectorElementType();
6405 MVT NewEltVT = NVT.getVectorElementType();
6406
6407 // Handle bitcasts to different vector type with the same total bit size.
6408 //
6409 // e.g. v2i64 = scalar_to_vector x:i64
6410 // =>
6411 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
6412 //
6413
6414 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
6415 SDValue Val = Node->getOperand(0);
6416 SDLoc SL(Node);
6417
6418 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
6419 SDValue Undef = DAG.getUNDEF(MidVT);
6420
6422 NewElts.push_back(CastVal);
6423 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
6424 NewElts.push_back(Undef);
6425
6426 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
6427 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
6428 Results.push_back(CvtVec);
6429 break;
6430 }
6431 case ISD::ATOMIC_SWAP:
6432 case ISD::ATOMIC_STORE: {
6433 AtomicSDNode *AM = cast<AtomicSDNode>(Node);
6434 SDLoc SL(Node);
6435 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal());
6436 assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
6437 "unexpected promotion type");
6438 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
6439 "unexpected atomic_swap with illegal type");
6440
6441 SDValue Op0 = AM->getBasePtr();
6442 SDValue Op1 = CastVal;
6443
6444 // ATOMIC_STORE uses a swapped operand order from every other AtomicSDNode,
6445 // but really it should merge with ISD::STORE.
6446 if (AM->getOpcode() == ISD::ATOMIC_STORE)
6447 std::swap(Op0, Op1);
6448
6449 SDValue NewAtomic = DAG.getAtomic(AM->getOpcode(), SL, NVT, AM->getChain(),
6450 Op0, Op1, AM->getMemOperand());
6451
6452 if (AM->getOpcode() != ISD::ATOMIC_STORE) {
6453 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
6454 Results.push_back(NewAtomic.getValue(1));
6455 } else
6456 Results.push_back(NewAtomic);
6457 break;
6458 }
6459 case ISD::ATOMIC_LOAD: {
6460 AtomicSDNode *AM = cast<AtomicSDNode>(Node);
6461 SDLoc SL(Node);
6462 assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
6463 "unexpected promotion type");
6464 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
6465 "unexpected atomic_load with illegal type");
6466
6467 SDValue NewAtomic =
6468 DAG.getAtomic(ISD::ATOMIC_LOAD, SL, NVT, DAG.getVTList(NVT, MVT::Other),
6469 {AM->getChain(), AM->getBasePtr()}, AM->getMemOperand());
6470 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
6471 Results.push_back(NewAtomic.getValue(1));
6472 break;
6473 }
6474 case ISD::SPLAT_VECTOR: {
6475 SDValue Scalar = Node->getOperand(0);
6476 MVT ScalarType = Scalar.getSimpleValueType();
6477 MVT NewScalarType = NVT.getVectorElementType();
6478 if (ScalarType.isInteger()) {
6479 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NewScalarType, Scalar);
6480 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
6481 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
6482 break;
6483 }
6484 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewScalarType, Scalar);
6485 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
6486 Results.push_back(
6487 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
6488 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
6489 break;
6490 }
6495 case ISD::VP_REDUCE_FMAX:
6496 case ISD::VP_REDUCE_FMIN:
6497 case ISD::VP_REDUCE_FMAXIMUM:
6498 case ISD::VP_REDUCE_FMINIMUM:
6499 Results.push_back(PromoteReduction(Node));
6500 break;
6501 }
6502
6503 // Replace the original node with the legalized result.
6504 if (!Results.empty()) {
6505 LLVM_DEBUG(dbgs() << "Successfully promoted node\n");
6506 ReplaceNode(Node, Results.data());
6507 } else
6508 LLVM_DEBUG(dbgs() << "Could not promote node\n");
6509}
6510
6511/// This is the entry point for the file.
6514
6515 SmallPtrSet<SDNode *, 16> LegalizedNodes;
6516 // Use a delete listener to remove nodes which were deleted during
6517 // legalization from LegalizeNodes. This is needed to handle the situation
6518 // where a new node is allocated by the object pool to the same address of a
6519 // previously deleted node.
6520 DAGNodeDeletedListener DeleteListener(
6521 *this,
6522 [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); });
6523
6524 SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
6525
6526 // Visit all the nodes. We start in topological order, so that we see
6527 // nodes with their original operands intact. Legalization can produce
6528 // new nodes which may themselves need to be legalized. Iterate until all
6529 // nodes have been legalized.
6530 while (true) {
6531 bool AnyLegalized = false;
6532 for (auto NI = allnodes_end(); NI != allnodes_begin();) {
6533 --NI;
6534
6535 SDNode *N = &*NI;
6536 if (N->use_empty() && N != getRoot().getNode()) {
6537 ++NI;
6538 DeleteNode(N);
6539 continue;
6540 }
6541
6542 if (LegalizedNodes.insert(N).second) {
6543 AnyLegalized = true;
6544 Legalizer.LegalizeOp(N);
6545
6546 if (N->use_empty() && N != getRoot().getNode()) {
6547 ++NI;
6548 DeleteNode(N);
6549 }
6550 }
6551 }
6552 if (!AnyLegalized)
6553 break;
6554
6555 }
6556
6557 // Remove dead nodes now.
6559}
6560
6562 SmallSetVector<SDNode *, 16> &UpdatedNodes) {
6563 SmallPtrSet<SDNode *, 16> LegalizedNodes;
6564 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
6565
6566 // Directly insert the node in question, and legalize it. This will recurse
6567 // as needed through operands.
6568 LegalizedNodes.insert(N);
6569 Legalizer.LegalizeOp(N);
6570
6571 return LegalizedNodes.count(N);
6572}
#define Success
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Legalizer
Utilities for dealing with flags related to floating point properties and mode controls.
static MaybeAlign getAlign(Value *Ptr)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG, const TargetLowering &TLI, SDValue &Res)
static bool isSinCosLibcallAvailable(SDNode *Node, const LibcallLoweringInfo &Libcalls)
Return true if sincos or __sincos_stret libcall is available.
static bool useSinCos(SDNode *Node)
Only issue sincos libcall if both sin and cos are needed.
static bool canUseFastMathLibcall(const SDNode *Node)
Return if we can use the FAST_* variant of a math libcall for the node.
static MachineMemOperand * getStackAlignedMMO(SDValue StackPtr, MachineFunction &MF, bool isObjectScalable)
static MVT getPromotedVectorElementType(const TargetLowering &TLI, MVT EltVT, MVT NewEltVT)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
std::pair< MCSymbol *, MachineModuleInfoImpl::StubValueTy > PairTy
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file contains the declarations for metadata subclasses.
PowerPC Reduce CR logical Operation
static constexpr MCPhysReg SPReg
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file describes how to lower LLVM code to machine code.
static constexpr int Concat[]
Value * RHS
Value * LHS
BinaryOperator * Mul
static LLVM_ABI const llvm::fltSemantics & EnumToSemantics(Semantics S)
Definition APFloat.cpp:98
bool isSignaling() const
Definition APFloat.h:1518
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.h:1213
APInt bitcastToAPInt() const
Definition APFloat.h:1408
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const SDValue & getBasePtr() const
const SDValue & getVal() const
LLVM_ABI Type * getStructRetType() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
const ConstantFP * getConstantFPValue() const
const APFloat & getValueAPF() const
Definition Constants.h:325
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
const ConstantInt * getConstantIntValue() const
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
bool isBigEndian() const
Definition DataLayout.h:215
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
bool empty() const
Definition Function.h:859
const BasicBlock & back() const
Definition Function.h:862
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
Tracks which library functions to use for a particular subtarget.
LLVM_ABI CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
LLVM_ABI RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
static LocationSize precise(uint64_t Value)
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
Machine Value Type.
SimpleValueType SimpleTy
uint64_t getScalarSizeInBits() const
bool bitsLE(MVT VT) const
Return true if this has no more bits than VT.
unsigned getVectorNumElements() const
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
bool bitsLT(MVT VT) const
Return true if this has less bits than VT.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
static MVT getVectorVT(MVT VT, unsigned NumElements)
MVT getVectorElementType() const
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
LLVM_ABI unsigned getEntrySize(const DataLayout &TD) const
getEntrySize - Return the size of each entry in the jump table.
A description of a memory reference used in the backend.
Flags
Flags values. These may be or'd together.
@ MOStore
The memory access writes data.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const SDValue & getChain() const
EVT getMemoryVT() const
Return the type of the in-memory value.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
Represents one node in the SelectionDAG.
bool isStrictFPOpcode()
Test if this node is a strict floating point pseudo-op.
ArrayRef< SDUse > ops() const
LLVM_ABI void dump() const
Dump this node, for debugging.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
const SDValue & getOperand(unsigned Num) const
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
iterator_range< user_iterator > users()
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOperand(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getOpcode() const
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
const TargetSubtargetInfo & getSubtarget() const
SDValue getCopyToReg(SDValue Chain, const SDLoc &dl, Register Reg, SDValue N)
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
SDValue getCALLSEQ_END(SDValue Chain, SDValue Op1, SDValue Op2, SDValue InGlue, const SDLoc &DL)
Return a new CALLSEQ_END node, which always must have a glue result (to ensure it's not CSE'd).
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
allnodes_const_iterator allnodes_end() const
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
SDValue getCopyFromReg(SDValue Chain, const SDLoc &dl, Register Reg, EVT VT)
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI void Legalize()
This transforms the SelectionDAG into a SelectionDAG that is compatible with the target instruction s...
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool LegalizeOp(SDNode *N, SmallSetVector< SDNode *, 16 > &UpdatedNodes)
Transforms a SelectionDAG node and any operands to it into a node that is compatible with the target ...
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
SDValue getCALLSEQ_START(SDValue Chain, uint64_t InSize, uint64_t OutSize, const SDLoc &DL)
Return a new CALLSEQ_START node, that starts new call frame, in which InSize bytes are set up inside ...
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
SDValue getSelectCC(const SDLoc &DL, SDValue LHS, SDValue RHS, SDValue True, SDValue False, ISD::CondCode Cond, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build SelectCC's if you just have an ISD::CondCode instead of an...
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
const TargetLibraryInfo & getLibInfo() const
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
size_type size() const
Definition SmallSet.h:171
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void swap(SmallVectorImpl &RHS)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
unsigned getIntSize() const
Get size of a C-level int or unsigned int, in bits.
bool isOperationExpand(unsigned Op, EVT VT) const
Return true if the specified operation is illegal on this target or unlikely to be made legal with cu...
virtual bool isShuffleMaskLegal(ArrayRef< int >, EVT) const
Targets can use this to indicate that they only support some VECTOR_SHUFFLE operations,...
virtual bool shouldExpandBuildVectorWithShuffles(EVT, unsigned DefinedValues) const
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
bool isOperationLegalOrPromote(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal using promotion.
LegalizeAction getCondCodeAction(ISD::CondCode CC, MVT VT) const
Return how the condition code should be treated: either it is legal, needs to be expanded to some oth...
virtual bool isFPImmLegal(const APFloat &, EVT, bool ForCodeSize=false) const
Returns true if the target can instruction select the specified FP immediate natively.
Register getStackPointerRegisterToSaveRestore() const
If a physical register, this specifies the register that llvm.savestack/llvm.restorestack should save...
LegalizeAction getFixedPointOperationAction(unsigned Op, EVT VT, unsigned Scale) const
Some fixed point operations may be natively supported by the target but only for specific scales.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
bool isStrictFPEnabled() const
Return true if the target support strict float operation.
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal for a comparison of the specified types on this ...
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
MVT getProgramPointerTy(const DataLayout &DL) const
Return the type for code pointers, which is determined by the program address space specified through...
virtual bool isJumpTableRelative() const
virtual bool ShouldShrinkFPConstant(EVT) const
If true, then instruction selection should seek to shrink the FP constant of the specified type to a ...
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
LegalizeAction getTruncStoreAction(EVT ValVT, EVT MemVT) const
Return how this store with truncation should be treated: either it is legal, needs to be promoted to ...
LegalizeAction getLoadExtAction(unsigned ExtType, EVT ValVT, EVT MemVT) const
Return how this load with extension should be treated: either it is legal, needs to be promoted to a ...
bool isOperationLegalOrCustom(unsigned Op, EVT VT, bool LegalOnly=false) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
virtual bool allowsMemoryAccess(LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *Fast=nullptr) const
Return true if the target supports a memory access of this type for the given address space and align...
virtual LegalizeAction getCustomOperationAction(SDNode &Op) const
How to legalize this custom operation?
bool isLoadExtLegalOrCustom(unsigned ExtType, EVT ValVT, EVT MemVT) const
Return true if the specified load with extension is legal or custom on this target.
LegalizeAction getStrictFPOperationAction(unsigned Op, EVT VT) const
bool isLoadExtLegal(unsigned ExtType, EVT ValVT, EVT MemVT) const
Return true if the specified load with extension is legal on this target.
virtual bool useSoftFloat() const
bool isTruncStoreLegalOrCustom(EVT ValVT, EVT MemVT) const
Return true if the specified store with truncation has solution on this target.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
virtual bool shouldSignExtendTypeInLibCall(Type *Ty, bool IsSigned) const
Returns true if arguments should be sign-extended in lib calls.
std::vector< ArgListEntry > ArgListTy
bool allowsMemoryAccessForAlignment(LLVMContext &Context, const DataLayout &DL, EVT VT, unsigned AddrSpace=0, Align Alignment=Align(1), MachineMemOperand::Flags Flags=MachineMemOperand::MONone, unsigned *Fast=nullptr) const
This function returns true if the memory access is aligned or if the target allows this specific unal...
bool isCondCodeLegalOrCustom(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal or custom for a comparison of the specified type...
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
LegalizeAction getOperationAction(unsigned Op, EVT VT) const
Return how this operation should be treated: either it is legal, needs to be promoted to a larger siz...
MVT getTypeToPromoteTo(unsigned Op, MVT VT) const
If the action for this operation is to promote, this method returns the ValueType to promote to.
const RTLIB::RuntimeLibcallsInfo & getRuntimeLibcallsInfo() const
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
SDValue expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US][ADD|SUB]SAT.
bool expandMultipleResultFPLibCall(SelectionDAG &DAG, RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl< SDValue > &Results, std::optional< unsigned > CallRetResNo={}) const
Expands a node with multiple results to an FP or vector libcall.
bool expandMULO(SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]MULO.
bool expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT, SelectionDAG &DAG, MulExpansionKind Kind, SDValue LL=SDValue(), SDValue LH=SDValue(), SDValue RL=SDValue(), SDValue RH=SDValue()) const
Expand a MUL into two nodes.
SDValue expandCTLZ(SDNode *N, SelectionDAG &DAG) const
Expand CTLZ/CTLZ_ZERO_UNDEF nodes.
SDValue expandBITREVERSE(SDNode *N, SelectionDAG &DAG) const
Expand BITREVERSE nodes.
SDValue expandCTTZ(SDNode *N, SelectionDAG &DAG) const
Expand CTTZ/CTTZ_ZERO_UNDEF nodes.
virtual SDValue expandIndirectJTBranch(const SDLoc &dl, SDValue Value, SDValue Addr, int JTI, SelectionDAG &DAG) const
Expands target specific indirect branch for the case of JumpTable expansion.
SDValue expandABD(SDNode *N, SelectionDAG &DAG) const
Expand ABDS/ABDU nodes.
SDValue expandCLMUL(SDNode *N, SelectionDAG &DAG) const
Expand carryless multiply.
SDValue expandShlSat(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]SHLSAT.
SDValue expandIS_FPCLASS(EVT ResultVT, SDValue Op, FPClassTest Test, SDNodeFlags Flags, const SDLoc &DL, SelectionDAG &DAG) const
Expand check for floating point class.
SDValue expandFP_TO_INT_SAT(SDNode *N, SelectionDAG &DAG) const
Expand FP_TO_[US]INT_SAT into FP_TO_[US]INT and selects or min/max.
SDValue expandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG) const
Expands an unaligned store to 2 half-size stores for integer values, and possibly more for vectors.
void expandSADDSUBO(SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::S(ADD|SUB)O.
SDValue expandABS(SDNode *N, SelectionDAG &DAG, bool IsNegative=false) const
Expand ABS nodes.
SDValue expandVecReduce(SDNode *Node, SelectionDAG &DAG) const
Expand a VECREDUCE_* into an explicit calculation.
SDValue expandVPCTTZElements(SDNode *N, SelectionDAG &DAG) const
Expand VP_CTTZ_ELTS/VP_CTTZ_ELTS_ZERO_UNDEF nodes.
bool expandFP_TO_UINT(SDNode *N, SDValue &Result, SDValue &Chain, SelectionDAG &DAG) const
Expand float to UINT conversion.
bool expandREM(SDNode *Node, SDValue &Result, SelectionDAG &DAG) const
Expand an SREM or UREM using SDIV/UDIV or SDIVREM/UDIVREM, if legal.
std::pair< SDValue, SDValue > expandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG) const
Expands an unaligned load to 2 half-size loads for an integer, and possibly more for vectors.
SDValue expandFMINIMUMNUM_FMAXIMUMNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminimumnum/fmaximumnum into multiple comparison with selects.
SDValue expandVectorSplice(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::VECTOR_SPLICE.
SDValue getVectorSubVecPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, EVT SubVecVT, SDValue Index, const SDNodeFlags PtrArithFlags=SDNodeFlags()) const
Get a pointer to a sub-vector of type SubVecVT at index Idx located in memory for a vector of type Ve...
SDValue expandCTPOP(SDNode *N, SelectionDAG &DAG) const
Expand CTPOP nodes.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
SDValue expandBSWAP(SDNode *N, SelectionDAG &DAG) const
Expand BSWAP nodes.
SDValue expandFMINIMUM_FMAXIMUM(SDNode *N, SelectionDAG &DAG) const
Expand fminimum/fmaximum into multiple comparison with selects.
bool expandFP_TO_SINT(SDNode *N, SDValue &Result, SelectionDAG &DAG) const
Expand float(f32) to SINT(i64) conversion.
virtual SDValue getPICJumpTableRelocBase(SDValue Table, SelectionDAG &DAG) const
Returns relocation base for the given PIC jumptable.
bool isInTailCallPosition(SelectionDAG &DAG, SDNode *Node, SDValue &Chain) const
Check whether a given call node is in tail position within its function.
SDValue expandFunnelShift(SDNode *N, SelectionDAG &DAG) const
Expand funnel shift.
bool LegalizeSetCCCondCode(SelectionDAG &DAG, EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, SDValue Mask, SDValue EVL, bool &NeedInvert, const SDLoc &dl, SDValue &Chain, bool IsSignaling=false) const
Legalize a SETCC or VP_SETCC with given LHS and RHS and condition code CC on the current target.
virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const
This callback is invoked for operations that are unsupported by the target, which are registered to u...
SDValue expandFixedPointDiv(unsigned Opcode, const SDLoc &dl, SDValue LHS, SDValue RHS, unsigned Scale, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]DIVFIX[SAT].
SDValue expandFP_ROUND(SDNode *Node, SelectionDAG &DAG) const
Expand round(fp) to fp conversion.
SDValue expandROT(SDNode *N, bool AllowVectorOps, SelectionDAG &DAG) const
Expand rotations.
SDValue getVectorElementPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, SDValue Index, const SDNodeFlags PtrArithFlags=SDNodeFlags()) const
Get a pointer to vector element Idx located in memory for a vector of type VecVT starting at a base a...
SDValue expandFMINNUM_FMAXNUM(SDNode *N, SelectionDAG &DAG) const
Expand fminnum/fmaxnum into fminnum_ieee/fmaxnum_ieee with quieted inputs.
std::pair< SDValue, SDValue > makeLibCall(SelectionDAG &DAG, RTLIB::LibcallImpl LibcallImpl, EVT RetVT, ArrayRef< SDValue > Ops, MakeLibCallOptions CallOptions, const SDLoc &dl, SDValue Chain=SDValue()) const
Returns a pair of (return value, chain).
SDValue expandCMP(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[US]CMP.
SDValue expandFixedPointMul(SDNode *Node, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::[U|S]MULFIX[SAT].
void expandUADDSUBO(SDNode *Node, SDValue &Result, SDValue &Overflow, SelectionDAG &DAG) const
Method for building the DAG expansion of ISD::U(ADD|SUB)O.
bool expandUINT_TO_FP(SDNode *N, SDValue &Result, SDValue &Chain, SelectionDAG &DAG) const
Expand UINT(i64) to double(f64) conversion.
bool expandMUL_LOHI(unsigned Opcode, EVT VT, const SDLoc &dl, SDValue LHS, SDValue RHS, SmallVectorImpl< SDValue > &Result, EVT HiLoVT, SelectionDAG &DAG, MulExpansionKind Kind, SDValue LL=SDValue(), SDValue LH=SDValue(), SDValue RL=SDValue(), SDValue RH=SDValue()) const
Expand a MUL or [US]MUL_LOHI of n-bit values into two or four nodes, respectively,...
SDValue expandAVG(SDNode *N, SelectionDAG &DAG) const
Expand vector/scalar AVGCEILS/AVGCEILU/AVGFLOORS/AVGFLOORU nodes.
SDValue expandCTLS(SDNode *N, SelectionDAG &DAG) const
Expand CTLS (count leading sign bits) nodes.
Primary interface to the complete machine description for the target machine.
const Triple & getTargetTriple() const
virtual const TargetFrameLowering * getFrameLowering() const
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
LLVM Value Representation.
Definition Value.h:75
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:819
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:788
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ SET_FPENV
Sets the current floating-point environment.
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition ISDOpcodes.h:168
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ STACKADDRESS
STACKADDRESS - Represents the llvm.stackaddress intrinsic.
Definition ISDOpcodes.h:127
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:779
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition ISDOpcodes.h:394
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ FRAME_TO_ARGS_OFFSET
FRAME_TO_ARGS_OFFSET - This node represents offset from frame pointer to first (possible) on-stack ar...
Definition ISDOpcodes.h:145
@ RESET_FPENV
Set floating-point environment to default state.
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:522
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:400
@ SET_FPMODE
Sets the current dynamic floating-point control modes.
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:853
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ FMODF
FMODF - Decomposes the operand into integral and fractional parts, each having the same type and sign...
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ FSINCOSPI
FSINCOSPI - Compute both the sine and cosine times pi more accurately than FSINCOS(pi*x),...
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition ISDOpcodes.h:172
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ STRICT_FMINIMUM
Definition ISDOpcodes.h:471
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:880
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:747
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
@ RESET_FPMODE
Sets default dynamic floating-point control modes.
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition ISDOpcodes.h:280
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:993
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:774
@ INIT_TRAMPOLINE
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition ISDOpcodes.h:407
@ STRICT_FSQRT
Constrained versions of libm-equivalent floating point intrinsics.
Definition ISDOpcodes.h:438
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ CONVERT_FROM_ARBITRARY_FP
CONVERT_FROM_ARBITRARY_FP - This operator converts from an arbitrary floating-point represented as an...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition ISDOpcodes.h:156
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:844
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:715
@ STRICT_UINT_TO_FP
Definition ISDOpcodes.h:485
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:665
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition ISDOpcodes.h:117
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:787
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:827
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ BR_JT
BR_JT - Jumptable branch.
@ VECTOR_INTERLEAVE
VECTOR_INTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor to...
Definition ISDOpcodes.h:635
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:548
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:796
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:792
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ GET_ROUNDING
Returns current rounding mode: -1 Undefined 0 Round to 0 1 Round to nearest, ties to even 2 Round to ...
Definition ISDOpcodes.h:970
@ STRICT_FP_TO_FP16
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:704
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
@ STRICT_FP16_TO_FP
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:765
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ STRICT_FMAXIMUM
Definition ISDOpcodes.h:470
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:139
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:850
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:811
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ UBSANTRAP
UBSANTRAP - Trap with an immediate describing the kind of sanitizer failure.
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:888
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition ISDOpcodes.h:413
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:978
@ GLOBAL_OFFSET_TABLE
The address of the GOT.
Definition ISDOpcodes.h:103
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition ISDOpcodes.h:484
@ STRICT_BF16_TO_FP
@ STRICT_FROUNDEVEN
Definition ISDOpcodes.h:464
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition ISDOpcodes.h:150
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition ISDOpcodes.h:110
@ STRICT_FP_TO_UINT
Definition ISDOpcodes.h:478
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:477
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:926
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ TRAP
TRAP - Trapping instruction.
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ STRICT_FP_TO_BF16
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:710
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition ISDOpcodes.h:427
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:959
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition ISDOpcodes.h:122
@ CLEAR_CACHE
llvm.clear_cache intrinsic Operands: Input Chain, Start Addres, End Address Outputs: Output Chain
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:997
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ STRICT_FNEARBYINT
Definition ISDOpcodes.h:458
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:945
@ VECREDUCE_FMINIMUM
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition ISDOpcodes.h:162
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ BRCOND
BRCOND - Conditional branch.
@ VECREDUCE_SEQ_FMUL
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:833
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ CALLSEQ_START
CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of a call sequence,...
@ VECTOR_DEINTERLEAVE
VECTOR_DEINTERLEAVE(VEC1, VEC2, ...) - Returns N vectors from N input vectors, where N is the factor ...
Definition ISDOpcodes.h:624
@ GET_DYNAMIC_AREA_OFFSET
GET_DYNAMIC_AREA_OFFSET - get offset from native SP to the address of the most recent dynamic alloca.
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:722
@ ADJUST_TRAMPOLINE
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
bool isSignedIntSetCC(CondCode Code)
Return true if this is a setcc instruction that performs a signed comparison when used with integer o...
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI Libcall getPOWI(EVT RetVT)
getPOWI - Return the POWI_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSINTTOFP(EVT OpVT, EVT RetVT)
getSINTTOFP - Return the SINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSYNC(unsigned Opc, MVT VT)
Return the SYNC_FETCH_AND_* value for the given opcode and type, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getLDEXP(EVT RetVT)
getLDEXP - Return the LDEXP_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getUINTTOFP(EVT OpVT, EVT RetVT)
getUINTTOFP - Return the UINTTOFP_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getFREXP(EVT RetVT)
getFREXP - Return the FREXP_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSINCOSPI(EVT RetVT)
getSINCOSPI - Return the SINCOSPI_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getFPLibCall(EVT VT, Libcall Call_F32, Libcall Call_F64, Libcall Call_F80, Libcall Call_F128, Libcall Call_PPCF128)
GetFPLibCall - Helper to return the right libcall for the given floating point type,...
LLVM_ABI Libcall getFPTOUINT(EVT OpVT, EVT RetVT)
getFPTOUINT - Return the FPTOUINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getMODF(EVT VT)
getMODF - Return the MODF_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getFPTOSINT(EVT OpVT, EVT RetVT)
getFPTOSINT - Return the FPTOSINT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getOUTLINE_ATOMIC(unsigned Opc, AtomicOrdering Order, MVT VT)
Return the outline atomics value for the given opcode, atomic ordering and type, or UNKNOWN_LIBCALL i...
LLVM_ABI Libcall getFPEXT(EVT OpVT, EVT RetVT)
getFPEXT - Return the FPEXT_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getFPROUND(EVT OpVT, EVT RetVT)
getFPROUND - Return the FPROUND_*_* value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSINCOS_STRET(EVT RetVT)
Return the SINCOS_STRET_ value for the given types, or UNKNOWN_LIBCALL if there is none.
LLVM_ABI Libcall getSINCOS(EVT RetVT)
getSINCOS - Return the SINCOS_* value for the given types, or UNKNOWN_LIBCALL if there is none.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
constexpr double e
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
@ Undef
Value of the register doesn't matter.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Returns: X * 2^Exp for integral exponents.
Definition APFloat.h:1610
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
AtomicOrdering
Atomic ordering for LLVM's memory model.
To bit_cast(const From &from) noexcept
Definition bit.h:90
@ Or
Bitwise or logical OR of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
fltNonfiniteBehavior
Definition APFloat.h:948
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:403
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:129
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:292
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:308
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:381
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition ValueTypes.h:251
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:393
EVT getHalfSizedIntegerVT(LLVMContext &Context) const
Finds the smallest simple value type that is greater than or equal to half the width of this EVT.
Definition ValueTypes.h:438
TypeSize getStoreSizeInBits() const
Return the number of bits overwritten by a store of the specified value type.
Definition ValueTypes.h:420
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:324
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:331
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:300
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:264
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:182
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:165
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:344
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:316
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
This class contains a discriminated union of information about pointers in memory operands,...
static LLVM_ABI MachinePointerInfo getJumpTable(MachineFunction &MF)
Return a MachinePointerInfo record that refers to a jump table entry.
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getUnknownStack(MachineFunction &MF)
Stack memory without other information.
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
std::pair< FunctionType *, AttributeList > getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
These are IR-level optimization flags that may be propagated to SDNodes.
void setNoFPExcept(bool b)
void setNoUnsignedWrap(bool b)
void setNoSignedWrap(bool b)
MakeLibCallOptions & setIsSigned(bool Value=true)
fltNonfiniteBehavior nonFiniteBehavior
Definition APFloat.h:1009
fltNanEncoding nanEncoding
Definition APFloat.h:1011