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