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