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