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