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