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