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