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 libcall is available.
2405 RTLIB::Libcall LC = RTLIB::getSINCOS(Node->getSimpleValueType(0).SimpleTy);
2406 return TLI.getLibcallName(LC) != nullptr;
2407}
2408
2409/// Only issue sincos libcall if both sin and cos are needed.
2410static bool useSinCos(SDNode *Node) {
2411 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2412 ? ISD::FCOS : ISD::FSIN;
2413
2414 SDValue Op0 = Node->getOperand(0);
2415 for (const SDNode *User : Op0.getNode()->users()) {
2416 if (User == Node)
2417 continue;
2418 // The other user might have been turned into sincos already.
2419 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2420 return true;
2421 }
2422 return false;
2423}
2424
2425SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
2426 SDLoc dl(Node);
2427 EVT VT = Node->getValueType(0);
2428 SDValue X = Node->getOperand(0);
2429 SDValue N = Node->getOperand(1);
2430 EVT ExpVT = N.getValueType();
2431 EVT AsIntVT = VT.changeTypeToInteger();
2432 if (AsIntVT == EVT()) // TODO: How to handle f80?
2433 return SDValue();
2434
2435 if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
2436 return SDValue();
2437
2438 SDNodeFlags NSW;
2439 NSW.setNoSignedWrap(true);
2440 SDNodeFlags NUW_NSW;
2441 NUW_NSW.setNoUnsignedWrap(true);
2442 NUW_NSW.setNoSignedWrap(true);
2443
2444 EVT SetCCVT =
2445 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
2446 const fltSemantics &FltSem = VT.getFltSemantics();
2447
2448 const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
2449 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2450 const int Precision = APFloat::semanticsPrecision(FltSem);
2451
2452 const SDValue MaxExp = DAG.getSignedConstant(MaxExpVal, dl, ExpVT);
2453 const SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
2454
2455 const SDValue DoubleMaxExp = DAG.getSignedConstant(2 * MaxExpVal, dl, ExpVT);
2456
2457 const APFloat One(FltSem, "1.0");
2458 APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
2459
2460 // Offset by precision to avoid denormal range.
2461 APFloat ScaleDownK =
2462 scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
2463
2464 // TODO: Should really introduce control flow and use a block for the >
2465 // MaxExp, < MinExp cases
2466
2467 // First, handle exponents Exp > MaxExp and scale down.
2468 SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
2469
2470 SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
2471 SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
2472 SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
2473 SDValue DecN1 =
2474 DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
2475
2476 SDValue ScaleUpTwice =
2477 DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
2478
2479 const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
2480 SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
2481 SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
2482
2483 SDValue SelectN_Big =
2484 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
2485 SDValue SelectX_Big =
2486 DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
2487
2488 // Now handle exponents Exp < MinExp
2489 SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
2490
2491 SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
2492 SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
2493
2494 SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
2495
2496 SDValue ClampMinVal =
2497 DAG.getSignedConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
2498 SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
2499 SDValue IncN1 =
2500 DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
2501
2502 const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
2503 SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
2504 SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
2505
2506 SDValue ScaleDownTwice = DAG.getSetCC(
2507 dl, SetCCVT, N,
2508 DAG.getSignedConstant(2 * MinExpVal + Precision, dl, ExpVT), ISD::SETULT);
2509
2510 SDValue SelectN_Small =
2511 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
2512 SDValue SelectX_Small =
2513 DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
2514
2515 // Now combine the two out of range exponent handling cases with the base
2516 // case.
2517 SDValue NewX = DAG.getNode(
2518 ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
2519 DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
2520
2521 SDValue NewN = DAG.getNode(
2522 ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
2523 DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
2524
2525 SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
2526
2527 SDValue ExponentShiftAmt =
2528 DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
2529 SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
2530
2531 SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
2532 ExponentShiftAmt, NUW_NSW);
2533 SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
2534 return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
2535}
2536
2537SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
2538 SDLoc dl(Node);
2539 SDValue Val = Node->getOperand(0);
2540 EVT VT = Val.getValueType();
2541 EVT ExpVT = Node->getValueType(1);
2542 EVT AsIntVT = VT.changeTypeToInteger();
2543 if (AsIntVT == EVT()) // TODO: How to handle f80?
2544 return SDValue();
2545
2546 const fltSemantics &FltSem = VT.getFltSemantics();
2547 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2548 const unsigned Precision = APFloat::semanticsPrecision(FltSem);
2549 const unsigned BitSize = VT.getScalarSizeInBits();
2550
2551 // TODO: Could introduce control flow and skip over the denormal handling.
2552
2553 // scale_up = fmul value, scalbn(1.0, precision + 1)
2554 // extracted_exp = (bitcast value to uint) >> precision - 1
2555 // biased_exp = extracted_exp + min_exp
2556 // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
2557 //
2558 // is_denormal = val < smallest_normalized
2559 // computed_fract = is_denormal ? scale_up : extracted_fract
2560 // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
2561 //
2562 // result_0 = (!isfinite(val) || iszero(val)) ? val : computed_fract
2563 // result_1 = (!isfinite(val) || iszero(val)) ? 0 : computed_exp
2564
2565 SDValue NegSmallestNormalizedInt = DAG.getConstant(
2566 APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
2567 AsIntVT);
2568
2569 SDValue SmallestNormalizedInt = DAG.getConstant(
2570 APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
2571 AsIntVT);
2572
2573 // Masks out the exponent bits.
2574 SDValue ExpMask =
2575 DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
2576
2577 // Mask out the exponent part of the value.
2578 //
2579 // e.g, for f32 FractSignMaskVal = 0x807fffff
2580 APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
2581 FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
2582
2583 APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
2584 SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
2585
2586 SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
2587
2588 const APFloat One(FltSem, "1.0");
2589 // Scale a possible denormal input.
2590 // e.g., for f64, 0x1p+54
2591 APFloat ScaleUpKVal =
2592 scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
2593
2594 SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
2595 SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
2596
2597 EVT SetCCVT =
2598 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2599
2600 SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
2601
2602 SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
2603
2604 SDValue AddNegSmallestNormal =
2605 DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
2606 SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
2607 NegSmallestNormalizedInt, ISD::SETULE);
2608
2609 SDValue IsDenormal =
2610 DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
2611
2612 SDValue MinExp = DAG.getSignedConstant(MinExpVal, dl, ExpVT);
2613 SDValue Zero = DAG.getConstant(0, dl, ExpVT);
2614
2615 SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
2616 SDValue ScaledSelect =
2617 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
2618
2619 SDValue ExpMaskScaled =
2620 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
2621
2622 SDValue ScaledValue =
2623 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
2624
2625 // Extract the exponent bits.
2626 SDValue ExponentShiftAmt =
2627 DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
2628 SDValue ShiftedExp =
2629 DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
2630 SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
2631
2632 SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
2633 SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
2634 SDValue DenormalExpBias =
2635 DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
2636
2637 SDValue MaskedFractAsInt =
2638 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
2639 const APFloat Half(FltSem, "0.5");
2640 SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
2641 SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
2642 SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
2643
2644 SDValue ComputedExp =
2645 DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
2646
2647 SDValue Result0 =
2648 DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
2649
2650 SDValue Result1 =
2651 DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
2652
2653 return DAG.getMergeValues({Result0, Result1}, dl);
2654}
2655
2656/// This function is responsible for legalizing a
2657/// INT_TO_FP operation of the specified operand when the target requests that
2658/// we expand it. At this point, we know that the result and operand types are
2659/// legal for the target.
2660SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2661 SDValue &Chain) {
2662 bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2663 Node->getOpcode() == ISD::SINT_TO_FP);
2664 EVT DestVT = Node->getValueType(0);
2665 SDLoc dl(Node);
2666 unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2667 SDValue Op0 = Node->getOperand(OpNo);
2668 EVT SrcVT = Op0.getValueType();
2669
2670 // TODO: Should any fast-math-flags be set for the created nodes?
2671 LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2672 if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2673 (DestVT.bitsLE(MVT::f64) ||
2674 TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2675 : ISD::FP_EXTEND,
2676 DestVT))) {
2677 LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
2678 "expansion\n");
2679
2680 // Get the stack frame index of a 8 byte buffer.
2681 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
2682
2683 SDValue Lo = Op0;
2684 // if signed map to unsigned space
2685 if (isSigned) {
2686 // Invert sign bit (signed to unsigned mapping).
2687 Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
2688 DAG.getConstant(0x80000000u, dl, MVT::i32));
2689 }
2690 // Initial hi portion of constructed double.
2691 SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
2692
2693 // If this a big endian target, swap the lo and high data.
2694 if (DAG.getDataLayout().isBigEndian())
2695 std::swap(Lo, Hi);
2696
2697 SDValue MemChain = DAG.getEntryNode();
2698
2699 // Store the lo of the constructed double.
2700 SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
2701 MachinePointerInfo());
2702 // Store the hi of the constructed double.
2703 SDValue HiPtr =
2704 DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl);
2705 SDValue Store2 =
2706 DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
2707 MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
2708
2709 // load the constructed double
2710 SDValue Load =
2711 DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
2712 // FP constant to bias correct the final result
2713 SDValue Bias = DAG.getConstantFP(
2714 isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
2715 : llvm::bit_cast<double>(0x4330000000000000ULL),
2716 dl, MVT::f64);
2717 // Subtract the bias and get the final result.
2718 SDValue Sub;
2720 if (Node->isStrictFPOpcode()) {
2721 Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2722 {Node->getOperand(0), Load, Bias});
2723 Chain = Sub.getValue(1);
2724 if (DestVT != Sub.getValueType()) {
2725 std::pair<SDValue, SDValue> ResultPair;
2726 ResultPair =
2727 DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2728 Result = ResultPair.first;
2729 Chain = ResultPair.second;
2730 }
2731 else
2732 Result = Sub;
2733 } else {
2734 Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2735 Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2736 }
2737 return Result;
2738 }
2739
2740 if (isSigned)
2741 return SDValue();
2742
2743 // TODO: Generalize this for use with other types.
2744 if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2745 (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2746 LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
2747 // For unsigned conversions, convert them to signed conversions using the
2748 // algorithm from the x86_64 __floatundisf in compiler_rt. That method
2749 // should be valid for i32->f32 as well.
2750
2751 // More generally this transform should be valid if there are 3 more bits
2752 // in the integer type than the significand. Rounding uses the first bit
2753 // after the width of the significand and the OR of all bits after that. So
2754 // we need to be able to OR the shifted out bit into one of the bits that
2755 // participate in the OR.
2756
2757 // TODO: This really should be implemented using a branch rather than a
2758 // select. We happen to get lucky and machinesink does the right
2759 // thing most of the time. This would be a good candidate for a
2760 // pseudo-op, or, even better, for whole-function isel.
2761 EVT SetCCVT = getSetCCResultType(SrcVT);
2762
2763 SDValue SignBitTest = DAG.getSetCC(
2764 dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2765
2766 SDValue ShiftConst = DAG.getShiftAmountConstant(1, SrcVT, dl);
2767 SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
2768 SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
2769 SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
2770 SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
2771
2772 SDValue Slow, Fast;
2773 if (Node->isStrictFPOpcode()) {
2774 // In strict mode, we must avoid spurious exceptions, and therefore
2775 // must make sure to only emit a single STRICT_SINT_TO_FP.
2776 SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
2777 // The STRICT_SINT_TO_FP inherits the exception mode from the
2778 // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
2779 // never raise any exception.
2780 SDNodeFlags Flags;
2781 Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
2782 Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {DestVT, MVT::Other},
2783 {Node->getOperand(0), InCvt}, Flags);
2784 Flags.setNoFPExcept(true);
2785 Slow = DAG.getNode(ISD::STRICT_FADD, dl, {DestVT, MVT::Other},
2786 {Fast.getValue(1), Fast, Fast}, Flags);
2787 Chain = Slow.getValue(1);
2788 } else {
2789 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
2790 Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
2791 Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2792 }
2793
2794 return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
2795 }
2796
2797 // Don't expand it if there isn't cheap fadd.
2798 if (!TLI.isOperationLegalOrCustom(
2799 Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2800 return SDValue();
2801
2802 // The following optimization is valid only if every value in SrcVT (when
2803 // treated as signed) is representable in DestVT. Check that the mantissa
2804 // size of DestVT is >= than the number of bits in SrcVT -1.
2805 assert(APFloat::semanticsPrecision(DestVT.getFltSemantics()) >=
2806 SrcVT.getSizeInBits() - 1 &&
2807 "Cannot perform lossless SINT_TO_FP!");
2808
2809 SDValue Tmp1;
2810 if (Node->isStrictFPOpcode()) {
2811 Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2812 { Node->getOperand(0), Op0 });
2813 } else
2814 Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2815
2816 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
2817 DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2818 SDValue Zero = DAG.getIntPtrConstant(0, dl),
2819 Four = DAG.getIntPtrConstant(4, dl);
2820 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
2821 SignSet, Four, Zero);
2822
2823 // If the sign bit of the integer is set, the large number will be treated
2824 // as a negative number. To counteract this, the dynamic code adds an
2825 // offset depending on the data type.
2826 uint64_t FF;
2827 switch (SrcVT.getSimpleVT().SimpleTy) {
2828 default:
2829 return SDValue();
2830 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2831 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2832 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2833 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2834 }
2835 if (DAG.getDataLayout().isLittleEndian())
2836 FF <<= 32;
2837 Constant *FudgeFactor = ConstantInt::get(
2838 Type::getInt64Ty(*DAG.getContext()), FF);
2839
2840 SDValue CPIdx =
2841 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
2842 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2843 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
2844 Alignment = commonAlignment(Alignment, 4);
2845 SDValue FudgeInReg;
2846 if (DestVT == MVT::f32)
2847 FudgeInReg = DAG.getLoad(
2848 MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2850 Alignment);
2851 else {
2852 SDValue Load = DAG.getExtLoad(
2853 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2855 Alignment);
2856 HandleSDNode Handle(Load);
2857 LegalizeOp(Load.getNode());
2858 FudgeInReg = Handle.getValue();
2859 }
2860
2861 if (Node->isStrictFPOpcode()) {
2862 SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2863 { Tmp1.getValue(1), Tmp1, FudgeInReg });
2864 Chain = Result.getValue(1);
2865 return Result;
2866 }
2867
2868 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
2869}
2870
2871/// This function is responsible for legalizing a
2872/// *INT_TO_FP operation of the specified operand when the target requests that
2873/// we promote it. At this point, we know that the result and operand types are
2874/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2875/// operation that takes a larger input.
2876void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
2877 SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) {
2878 bool IsStrict = N->isStrictFPOpcode();
2879 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
2880 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
2881 EVT DestVT = N->getValueType(0);
2882 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2883 unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
2884 unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
2885
2886 // First step, figure out the appropriate *INT_TO_FP operation to use.
2887 EVT NewInTy = LegalOp.getValueType();
2888
2889 unsigned OpToUse = 0;
2890
2891 // Scan for the appropriate larger type to use.
2892 while (true) {
2893 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
2894 assert(NewInTy.isInteger() && "Ran out of possibilities!");
2895
2896 // If the target supports SINT_TO_FP of this type, use it.
2897 if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
2898 OpToUse = SIntOp;
2899 break;
2900 }
2901 if (IsSigned)
2902 continue;
2903
2904 // If the target supports UINT_TO_FP of this type, use it.
2905 if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
2906 OpToUse = UIntOp;
2907 break;
2908 }
2909
2910 // Otherwise, try a larger type.
2911 }
2912
2913 // Okay, we found the operation and type to use. Zero extend our input to the
2914 // desired type then run the operation on it.
2915 if (IsStrict) {
2916 SDValue Res =
2917 DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
2918 {N->getOperand(0),
2919 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2920 dl, NewInTy, LegalOp)});
2921 Results.push_back(Res);
2922 Results.push_back(Res.getValue(1));
2923 return;
2924 }
2925
2926 Results.push_back(
2927 DAG.getNode(OpToUse, dl, DestVT,
2928 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2929 dl, NewInTy, LegalOp)));
2930}
2931
2932/// This function is responsible for legalizing a
2933/// FP_TO_*INT operation of the specified operand when the target requests that
2934/// we promote it. At this point, we know that the result and operand types are
2935/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2936/// operation that returns a larger result.
2937void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
2938 SmallVectorImpl<SDValue> &Results) {
2939 bool IsStrict = N->isStrictFPOpcode();
2940 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
2941 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
2942 EVT DestVT = N->getValueType(0);
2943 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2944 // First step, figure out the appropriate FP_TO*INT operation to use.
2945 EVT NewOutTy = DestVT;
2946
2947 unsigned OpToUse = 0;
2948
2949 // Scan for the appropriate larger type to use.
2950 while (true) {
2951 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
2952 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2953
2954 // A larger signed type can hold all unsigned values of the requested type,
2955 // so using FP_TO_SINT is valid
2956 OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
2957 if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
2958 break;
2959
2960 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2961 OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
2962 if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
2963 break;
2964
2965 // Otherwise, try a larger type.
2966 }
2967
2968 // Okay, we found the operation and type to use.
2970 if (IsStrict) {
2971 SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
2972 Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
2973 } else
2974 Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
2975
2976 // Truncate the result of the extended FP_TO_*INT operation to the desired
2977 // size.
2978 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2979 Results.push_back(Trunc);
2980 if (IsStrict)
2981 Results.push_back(Operation.getValue(1));
2982}
2983
2984/// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
2985/// the result and operand types are legal and there must be a legal
2986/// FP_TO_*INT_SAT operation for a larger result type.
2987SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
2988 const SDLoc &dl) {
2989 unsigned Opcode = Node->getOpcode();
2990
2991 // Scan for the appropriate larger type to use.
2992 EVT NewOutTy = Node->getValueType(0);
2993 while (true) {
2994 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
2995 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2996
2997 if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
2998 break;
2999 }
3000
3001 // Saturation width is determined by second operand, so we don't have to
3002 // perform any fixup and can directly truncate the result.
3003 SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
3004 Node->getOperand(1));
3005 return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
3006}
3007
3008/// Open code the operations for PARITY of the specified operation.
3009SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
3010 EVT VT = Op.getValueType();
3011 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3012 unsigned Sz = VT.getScalarSizeInBits();
3013
3014 // If CTPOP is legal, use it. Otherwise use shifts and xor.
3017 Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
3018 } else {
3019 Result = Op;
3020 for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
3021 SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
3022 DAG.getConstant(1ULL << (--i), dl, ShVT));
3023 Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
3024 }
3025 }
3026
3027 return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
3028}
3029
3030SDValue SelectionDAGLegalize::PromoteReduction(SDNode *Node) {
3031 bool IsVPOpcode = ISD::isVPOpcode(Node->getOpcode());
3032 MVT VecVT = IsVPOpcode ? Node->getOperand(1).getSimpleValueType()
3033 : Node->getOperand(0).getSimpleValueType();
3034 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
3035 MVT ScalarVT = Node->getSimpleValueType(0);
3036 MVT NewScalarVT = NewVecVT.getVectorElementType();
3037
3038 SDLoc DL(Node);
3039 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
3040
3041 // FIXME: Support integer.
3042 assert(Node->getOperand(0).getValueType().isFloatingPoint() &&
3043 "Only FP promotion is supported");
3044
3045 for (unsigned j = 0; j != Node->getNumOperands(); ++j)
3046 if (Node->getOperand(j).getValueType().isVector() &&
3047 !(IsVPOpcode &&
3048 ISD::getVPMaskIdx(Node->getOpcode()) == j)) { // Skip mask operand.
3049 // promote the vector operand.
3050 // FIXME: Support integer.
3051 assert(Node->getOperand(j).getValueType().isFloatingPoint() &&
3052 "Only FP promotion is supported");
3053 Operands[j] =
3054 DAG.getNode(ISD::FP_EXTEND, DL, NewVecVT, Node->getOperand(j));
3055 } else if (Node->getOperand(j).getValueType().isFloatingPoint()) {
3056 // promote the initial value.
3057 Operands[j] =
3058 DAG.getNode(ISD::FP_EXTEND, DL, NewScalarVT, Node->getOperand(j));
3059 } else {
3060 Operands[j] = Node->getOperand(j); // Skip VL operand.
3061 }
3062
3063 SDValue Res = DAG.getNode(Node->getOpcode(), DL, NewScalarVT, Operands,
3064 Node->getFlags());
3065
3066 assert(ScalarVT.isFloatingPoint() && "Only FP promotion is supported");
3067 return DAG.getNode(ISD::FP_ROUND, DL, ScalarVT, Res,
3068 DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
3069}
3070
3071bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
3072 LLVM_DEBUG(dbgs() << "Trying to expand node\n");
3074 SDLoc dl(Node);
3075 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3076 bool NeedInvert;
3077 switch (Node->getOpcode()) {
3078 case ISD::ABS:
3079 if ((Tmp1 = TLI.expandABS(Node, DAG)))
3080 Results.push_back(Tmp1);
3081 break;
3082 case ISD::ABDS:
3083 case ISD::ABDU:
3084 if ((Tmp1 = TLI.expandABD(Node, DAG)))
3085 Results.push_back(Tmp1);
3086 break;
3087 case ISD::AVGCEILS:
3088 case ISD::AVGCEILU:
3089 case ISD::AVGFLOORS:
3090 case ISD::AVGFLOORU:
3091 if ((Tmp1 = TLI.expandAVG(Node, DAG)))
3092 Results.push_back(Tmp1);
3093 break;
3094 case ISD::CTPOP:
3095 if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
3096 Results.push_back(Tmp1);
3097 break;
3098 case ISD::CTLZ:
3100 if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
3101 Results.push_back(Tmp1);
3102 break;
3103 case ISD::CTTZ:
3105 if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
3106 Results.push_back(Tmp1);
3107 break;
3108 case ISD::BITREVERSE:
3109 if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
3110 Results.push_back(Tmp1);
3111 break;
3112 case ISD::BSWAP:
3113 if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
3114 Results.push_back(Tmp1);
3115 break;
3116 case ISD::PARITY:
3117 Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
3118 break;
3119 case ISD::FRAMEADDR:
3120 case ISD::RETURNADDR:
3122 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3123 break;
3124 case ISD::EH_DWARF_CFA: {
3125 SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
3126 TLI.getPointerTy(DAG.getDataLayout()));
3127 SDValue Offset = DAG.getNode(ISD::ADD, dl,
3128 CfaArg.getValueType(),
3130 CfaArg.getValueType()),
3131 CfaArg);
3132 SDValue FA = DAG.getNode(
3134 DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
3135 Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
3136 FA, Offset));
3137 break;
3138 }
3139 case ISD::GET_ROUNDING:
3140 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
3141 Results.push_back(Node->getOperand(0));
3142 break;
3143 case ISD::EH_RETURN:
3144 case ISD::EH_LABEL:
3145 case ISD::PREFETCH:
3146 case ISD::VAEND:
3148 // If the target didn't expand these, there's nothing to do, so just
3149 // preserve the chain and be done.
3150 Results.push_back(Node->getOperand(0));
3151 break;
3152 case ISD::READCYCLECOUNTER:
3153 case ISD::READSTEADYCOUNTER:
3154 // If the target didn't expand this, just return 'zero' and preserve the
3155 // chain.
3156 Results.append(Node->getNumValues() - 1,
3157 DAG.getConstant(0, dl, Node->getValueType(0)));
3158 Results.push_back(Node->getOperand(0));
3159 break;
3161 // If the target didn't expand this, just return 'zero' and preserve the
3162 // chain.
3163 Results.push_back(DAG.getConstant(0, dl, MVT::i32));
3164 Results.push_back(Node->getOperand(0));
3165 break;
3166 case ISD::ATOMIC_LOAD: {
3167 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
3168 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
3169 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3170 SDValue Swap = DAG.getAtomicCmpSwap(
3171 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3172 Node->getOperand(0), Node->getOperand(1), Zero, Zero,
3173 cast<AtomicSDNode>(Node)->getMemOperand());
3174 Results.push_back(Swap.getValue(0));
3175 Results.push_back(Swap.getValue(1));
3176 break;
3177 }
3178 case ISD::ATOMIC_STORE: {
3179 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3180 SDValue Swap = DAG.getAtomic(
3181 ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(),
3182 Node->getOperand(0), Node->getOperand(2), Node->getOperand(1),
3183 cast<AtomicSDNode>(Node)->getMemOperand());
3184 Results.push_back(Swap.getValue(1));
3185 break;
3186 }
3187 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
3188 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
3189 // splits out the success value as a comparison. Expanding the resulting
3190 // ATOMIC_CMP_SWAP will produce a libcall.
3191 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3192 SDValue Res = DAG.getAtomicCmpSwap(
3193 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3194 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
3195 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
3196
3197 SDValue ExtRes = Res;
3198 SDValue LHS = Res;
3199 SDValue RHS = Node->getOperand(1);
3200
3201 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
3202 EVT OuterType = Node->getValueType(0);
3203 switch (TLI.getExtendForAtomicOps()) {
3204 case ISD::SIGN_EXTEND:
3205 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
3206 DAG.getValueType(AtomicType));
3207 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
3208 Node->getOperand(2), DAG.getValueType(AtomicType));
3209 ExtRes = LHS;
3210 break;
3211 case ISD::ZERO_EXTEND:
3212 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
3213 DAG.getValueType(AtomicType));
3214 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3215 ExtRes = LHS;
3216 break;
3217 case ISD::ANY_EXTEND:
3218 LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
3219 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3220 break;
3221 default:
3222 llvm_unreachable("Invalid atomic op extension");
3223 }
3224
3226 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
3227
3228 Results.push_back(ExtRes.getValue(0));
3229 Results.push_back(Success);
3230 Results.push_back(Res.getValue(1));
3231 break;
3232 }
3233 case ISD::ATOMIC_LOAD_SUB: {
3234 SDLoc DL(Node);
3235 EVT VT = Node->getValueType(0);
3236 SDValue RHS = Node->getOperand(2);
3237 AtomicSDNode *AN = cast<AtomicSDNode>(Node);
3238 if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG &&
3239 cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT())
3240 RHS = RHS->getOperand(0);
3241 SDValue NewRHS =
3242 DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS);
3243 SDValue Res = DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, AN->getMemoryVT(),
3244 Node->getOperand(0), Node->getOperand(1),
3245 NewRHS, AN->getMemOperand());
3246 Results.push_back(Res);
3247 Results.push_back(Res.getValue(1));
3248 break;
3249 }
3250 case ISD::DYNAMIC_STACKALLOC:
3251 ExpandDYNAMIC_STACKALLOC(Node, Results);
3252 break;
3253 case ISD::MERGE_VALUES:
3254 for (unsigned i = 0; i < Node->getNumValues(); i++)
3255 Results.push_back(Node->getOperand(i));
3256 break;
3257 case ISD::POISON:
3258 case ISD::UNDEF: {
3259 EVT VT = Node->getValueType(0);
3260 if (VT.isInteger())
3261 Results.push_back(DAG.getConstant(0, dl, VT));
3262 else {
3263 assert(VT.isFloatingPoint() && "Unknown value type!");
3264 Results.push_back(DAG.getConstantFP(0, dl, VT));
3265 }
3266 break;
3267 }
3269 // When strict mode is enforced we can't do expansion because it
3270 // does not honor the "strict" properties. Only libcall is allowed.
3271 if (TLI.isStrictFPEnabled())
3272 break;
3273 // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3274 // since this operation is more efficient than stack operation.
3275 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3276 Node->getValueType(0))
3277 == TargetLowering::Legal)
3278 break;
3279 // We fall back to use stack operation when the FP_ROUND operation
3280 // isn't available.
3281 if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3282 Node->getValueType(0), dl,
3283 Node->getOperand(0)))) {
3284 ReplaceNode(Node, Tmp1.getNode());
3285 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
3286 return true;
3287 }
3288 break;
3289 case ISD::FP_ROUND: {
3290 if ((Tmp1 = TLI.expandFP_ROUND(Node, DAG))) {
3291 Results.push_back(Tmp1);
3292 break;
3293 }
3294
3295 [[fallthrough]];
3296 }
3297 case ISD::BITCAST:
3298 if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3299 Node->getValueType(0), dl)))
3300 Results.push_back(Tmp1);
3301 break;
3303 // When strict mode is enforced we can't do expansion because it
3304 // does not honor the "strict" properties. Only libcall is allowed.
3305 if (TLI.isStrictFPEnabled())
3306 break;
3307 // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3308 // since this operation is more efficient than stack operation.
3309 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3310 Node->getValueType(0))
3311 == TargetLowering::Legal)
3312 break;
3313 // We fall back to use stack operation when the FP_EXTEND operation
3314 // isn't available.
3315 if ((Tmp1 = EmitStackConvert(
3316 Node->getOperand(1), Node->getOperand(1).getValueType(),
3317 Node->getValueType(0), dl, Node->getOperand(0)))) {
3318 ReplaceNode(Node, Tmp1.getNode());
3319 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
3320 return true;
3321 }
3322 break;
3323 case ISD::FP_EXTEND: {
3324 SDValue Op = Node->getOperand(0);
3325 EVT SrcVT = Op.getValueType();
3326 EVT DstVT = Node->getValueType(0);
3327 if (SrcVT.getScalarType() == MVT::bf16) {
3328 Results.push_back(DAG.getNode(ISD::BF16_TO_FP, SDLoc(Node), DstVT, Op));
3329 break;
3330 }
3331
3332 if ((Tmp1 = EmitStackConvert(Op, SrcVT, DstVT, dl)))
3333 Results.push_back(Tmp1);
3334 break;
3335 }
3336 case ISD::BF16_TO_FP: {
3337 // Always expand bf16 to f32 casts, they lower to ext + shift.
3338 //
3339 // Note that the operand of this code can be bf16 or an integer type in case
3340 // bf16 is not supported on the target and was softened.
3341 SDValue Op = Node->getOperand(0);
3342 if (Op.getValueType() == MVT::bf16) {
3343 Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3344 DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3345 } else {
3346 Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3347 }
3348 Op = DAG.getNode(ISD::SHL, dl, MVT::i32, Op,
3349 DAG.getShiftAmountConstant(16, MVT::i32, dl));
3350 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3351 // Add fp_extend in case the output is bigger than f32.
3352 if (Node->getValueType(0) != MVT::f32)
3353 Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3354 Results.push_back(Op);
3355 break;
3356 }
3357 case ISD::FP_TO_BF16: {
3358 SDValue Op = Node->getOperand(0);
3359 if (Op.getValueType() != MVT::f32)
3360 Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3361 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3362 // Certain SNaNs will turn into infinities if we do a simple shift right.
3363 if (!DAG.isKnownNeverSNaN(Op)) {
3364 Op = DAG.getNode(ISD::FCANONICALIZE, dl, MVT::f32, Op, Node->getFlags());
3365 }
3366 Op = DAG.getNode(ISD::SRL, dl, MVT::i32,
3367 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3368 DAG.getShiftAmountConstant(16, MVT::i32, dl));
3369 // The result of this node can be bf16 or an integer type in case bf16 is
3370 // not supported on the target and was softened to i16 for storage.
3371 if (Node->getValueType(0) == MVT::bf16) {
3372 Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3373 DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3374 } else {
3375 Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3376 }
3377 Results.push_back(Op);
3378 break;
3379 }
3380 case ISD::FCANONICALIZE: {
3381 // This implements llvm.canonicalize.f* by multiplication with 1.0, as
3382 // suggested in
3383 // https://llvm.org/docs/LangRef.html#llvm-canonicalize-intrinsic.
3384 // It uses strict_fp operations even outside a strict_fp context in order
3385 // to guarantee that the canonicalization is not optimized away by later
3386 // passes. The result chain introduced by that is intentionally ignored
3387 // since no ordering requirement is intended here.
3388
3389 // Create strict multiplication by 1.0.
3390 SDValue Operand = Node->getOperand(0);
3391 EVT VT = Operand.getValueType();
3392 SDValue One = DAG.getConstantFP(1.0, dl, VT);
3393 SDValue Chain = DAG.getEntryNode();
3394 // Propagate existing flags on canonicalize, and additionally set
3395 // NoFPExcept.
3396 SDNodeFlags CanonicalizeFlags = Node->getFlags();
3397 CanonicalizeFlags.setNoFPExcept(true);
3398 SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, dl, {VT, MVT::Other},
3399 {Chain, Operand, One}, CanonicalizeFlags);
3400
3401 Results.push_back(Mul);
3402 break;
3403 }
3405 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3406 EVT VT = Node->getValueType(0);
3407
3408 // An in-register sign-extend of a boolean is a negation:
3409 // 'true' (1) sign-extended is -1.
3410 // 'false' (0) sign-extended is 0.
3411 // However, we must mask the high bits of the source operand because the
3412 // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
3413
3414 // TODO: Do this for vectors too?
3415 if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
3416 SDValue One = DAG.getConstant(1, dl, VT);
3417 SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
3418 SDValue Zero = DAG.getConstant(0, dl, VT);
3419 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
3420 Results.push_back(Neg);
3421 break;
3422 }
3423
3424 // NOTE: we could fall back on load/store here too for targets without
3425 // SRA. However, it is doubtful that any exist.
3426 unsigned BitsDiff = VT.getScalarSizeInBits() -
3427 ExtraVT.getScalarSizeInBits();
3428 SDValue ShiftCst = DAG.getShiftAmountConstant(BitsDiff, VT, dl);
3429 Tmp1 = DAG.getNode(ISD::SHL, dl, VT, Node->getOperand(0), ShiftCst);
3430 Tmp1 = DAG.getNode(ISD::SRA, dl, VT, Tmp1, ShiftCst);
3431 Results.push_back(Tmp1);
3432 break;
3433 }
3434 case ISD::UINT_TO_FP:
3436 if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
3437 Results.push_back(Tmp1);
3438 if (Node->isStrictFPOpcode())
3439 Results.push_back(Tmp2);
3440 break;
3441 }
3442 [[fallthrough]];
3443 case ISD::SINT_TO_FP:
3445 if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
3446 Results.push_back(Tmp1);
3447 if (Node->isStrictFPOpcode())
3448 Results.push_back(Tmp2);
3449 }
3450 break;
3451 case ISD::FP_TO_SINT:
3452 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
3453 Results.push_back(Tmp1);
3454 break;
3456 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
3457 ReplaceNode(Node, Tmp1.getNode());
3458 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
3459 return true;
3460 }
3461 break;
3462 case ISD::FP_TO_UINT:
3463 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
3464 Results.push_back(Tmp1);
3465 break;
3467 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
3468 // Relink the chain.
3469 DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
3470 // Replace the new UINT result.
3471 ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
3472 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
3473 return true;
3474 }
3475 break;
3478 Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3479 break;
3480 case ISD::LROUND:
3481 case ISD::LLROUND: {
3482 SDValue Arg = Node->getOperand(0);
3483 EVT ArgVT = Arg.getValueType();
3484 EVT ResVT = Node->getValueType(0);
3485 SDLoc dl(Node);
3486 SDValue RoundNode = DAG.getNode(ISD::FROUND, dl, ArgVT, Arg);
3487 Results.push_back(DAG.getNode(ISD::FP_TO_SINT, dl, ResVT, RoundNode));
3488 break;
3489 }
3490 case ISD::VAARG:
3491 Results.push_back(DAG.expandVAArg(Node));
3492 Results.push_back(Results[0].getValue(1));
3493 break;
3494 case ISD::VACOPY:
3495 Results.push_back(DAG.expandVACopy(Node));
3496 break;
3498 if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar())
3499 // This must be an access of the only element. Return it.
3500 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
3501 Node->getOperand(0));
3502 else
3503 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3504 Results.push_back(Tmp1);
3505 break;
3507 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3508 break;
3510 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3511 break;
3513 if (EVT VectorValueType = Node->getOperand(0).getValueType();
3514 VectorValueType.isScalableVector() ||
3515 TLI.isOperationExpand(ISD::EXTRACT_VECTOR_ELT, VectorValueType))
3516 Results.push_back(ExpandVectorBuildThroughStack(Node));
3517 else
3518 Results.push_back(ExpandConcatVectors(Node));
3519 break;
3521 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3522 break;
3524 Results.push_back(ExpandINSERT_VECTOR_ELT(SDValue(Node, 0)));
3525 break;
3526 case ISD::VECTOR_SHUFFLE: {
3527 SmallVector<int, 32> NewMask;
3528 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
3529
3530 EVT VT = Node->getValueType(0);
3531 EVT EltVT = VT.getVectorElementType();
3532 SDValue Op0 = Node->getOperand(0);
3533 SDValue Op1 = Node->getOperand(1);
3534 if (!TLI.isTypeLegal(EltVT)) {
3535 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3536
3537 // BUILD_VECTOR operands are allowed to be wider than the element type.
3538 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3539 // it.
3540 if (NewEltVT.bitsLT(EltVT)) {
3541 // Convert shuffle node.
3542 // If original node was v4i64 and the new EltVT is i32,
3543 // cast operands to v8i32 and re-build the mask.
3544
3545 // Calculate new VT, the size of the new VT should be equal to original.
3546 EVT NewVT =
3547 EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3548 VT.getSizeInBits() / NewEltVT.getSizeInBits());
3549 assert(NewVT.bitsEq(VT));
3550
3551 // cast operands to new VT
3552 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3553 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3554
3555 // Convert the shuffle mask
3556 unsigned int factor =
3558
3559 // EltVT gets smaller
3560 assert(factor > 0);
3561
3562 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3563 if (Mask[i] < 0) {
3564 for (unsigned fi = 0; fi < factor; ++fi)
3565 NewMask.push_back(Mask[i]);
3566 }
3567 else {
3568 for (unsigned fi = 0; fi < factor; ++fi)
3569 NewMask.push_back(Mask[i]*factor+fi);
3570 }
3571 }
3572 Mask = NewMask;
3573 VT = NewVT;
3574 }
3575 EltVT = NewEltVT;
3576 }
3577 unsigned NumElems = VT.getVectorNumElements();
3579 for (unsigned i = 0; i != NumElems; ++i) {
3580 if (Mask[i] < 0) {
3581 Ops.push_back(DAG.getUNDEF(EltVT));
3582 continue;
3583 }
3584 unsigned Idx = Mask[i];
3585 if (Idx < NumElems)
3586 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3587 DAG.getVectorIdxConstant(Idx, dl)));
3588 else
3589 Ops.push_back(
3590 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3591 DAG.getVectorIdxConstant(Idx - NumElems, dl)));
3592 }
3593
3594 Tmp1 = DAG.getBuildVector(VT, dl, Ops);
3595 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3596 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
3597 Results.push_back(Tmp1);
3598 break;
3599 }
3600 case ISD::VECTOR_SPLICE: {
3601 Results.push_back(TLI.expandVectorSplice(Node, DAG));
3602 break;
3603 }
3605 unsigned Factor = Node->getNumOperands();
3606 if (Factor <= 2 || !isPowerOf2_32(Factor))
3607 break;
3609 EVT VecVT = Node->getValueType(0);
3610 SmallVector<EVT> HalfVTs(Factor / 2, VecVT);
3611 // Deinterleave at Factor/2 so each result contains two factors interleaved:
3612 // a0b0 c0d0 a1b1 c1d1 -> [a0c0 b0d0] [a1c1 b1d1]
3613 SDValue L = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, dl, HalfVTs,
3614 ArrayRef(Ops).take_front(Factor / 2));
3615 SDValue R = DAG.getNode(ISD::VECTOR_DEINTERLEAVE, dl, HalfVTs,
3616 ArrayRef(Ops).take_back(Factor / 2));
3617 Results.resize(Factor);
3618 // Deinterleave the 2 factors out:
3619 // [a0c0 a1c1] [b0d0 b1d1] -> a0a1 b0b1 c0c1 d0d1
3620 for (unsigned I = 0; I < Factor / 2; I++) {
3622 DAG.getNode(ISD::VECTOR_DEINTERLEAVE, dl, {VecVT, VecVT},
3623 {L.getValue(I), R.getValue(I)});
3624 Results[I] = Deinterleave.getValue(0);
3625 Results[I + Factor / 2] = Deinterleave.getValue(1);
3626 }
3627 break;
3628 }
3630 unsigned Factor = Node->getNumOperands();
3631 if (Factor <= 2 || !isPowerOf2_32(Factor))
3632 break;
3633 EVT VecVT = Node->getValueType(0);
3634 SmallVector<EVT> HalfVTs(Factor / 2, VecVT);
3635 SmallVector<SDValue, 8> LOps, ROps;
3636 // Interleave so we have 2 factors per result:
3637 // a0a1 b0b1 c0c1 d0d1 -> [a0c0 b0d0] [a1c1 b1d1]
3638 for (unsigned I = 0; I < Factor / 2; I++) {
3639 SDValue Interleave =
3640 DAG.getNode(ISD::VECTOR_INTERLEAVE, dl, {VecVT, VecVT},
3641 {Node->getOperand(I), Node->getOperand(I + Factor / 2)});
3642 LOps.push_back(Interleave.getValue(0));
3643 ROps.push_back(Interleave.getValue(1));
3644 }
3645 // Interleave at Factor/2:
3646 // [a0c0 b0d0] [a1c1 b1d1] -> a0b0 c0d0 a1b1 c1d1
3647 SDValue L = DAG.getNode(ISD::VECTOR_INTERLEAVE, dl, HalfVTs, LOps);
3648 SDValue R = DAG.getNode(ISD::VECTOR_INTERLEAVE, dl, HalfVTs, ROps);
3649 for (unsigned I = 0; I < Factor / 2; I++)
3650 Results.push_back(L.getValue(I));
3651 for (unsigned I = 0; I < Factor / 2; I++)
3652 Results.push_back(R.getValue(I));
3653 break;
3654 }
3655 case ISD::EXTRACT_ELEMENT: {
3656 EVT OpTy = Node->getOperand(0).getValueType();
3657 if (Node->getConstantOperandVal(1)) {
3658 // 1 -> Hi
3659 Tmp1 = DAG.getNode(
3660 ISD::SRL, dl, OpTy, Node->getOperand(0),
3661 DAG.getShiftAmountConstant(OpTy.getSizeInBits() / 2, OpTy, dl));
3662 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3663 } else {
3664 // 0 -> Lo
3665 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3666 Node->getOperand(0));
3667 }
3668 Results.push_back(Tmp1);
3669 break;
3670 }
3671 case ISD::STACKSAVE:
3672 // Expand to CopyFromReg if the target set
3673 // StackPointerRegisterToSaveRestore.
3675 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3676 Node->getValueType(0)));
3677 Results.push_back(Results[0].getValue(1));
3678 } else {
3679 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3680 Results.push_back(Node->getOperand(0));
3681 }
3682 break;
3683 case ISD::STACKRESTORE:
3684 // Expand to CopyToReg if the target set
3685 // StackPointerRegisterToSaveRestore.
3687 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3688 Node->getOperand(1)));
3689 } else {
3690 Results.push_back(Node->getOperand(0));
3691 }
3692 break;
3693 case ISD::GET_DYNAMIC_AREA_OFFSET:
3694 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3695 Results.push_back(Results[0].getValue(0));
3696 break;
3697 case ISD::FCOPYSIGN:
3698 Results.push_back(ExpandFCOPYSIGN(Node));
3699 break;
3700 case ISD::FNEG:
3701 Results.push_back(ExpandFNEG(Node));
3702 break;
3703 case ISD::FABS:
3704 Results.push_back(ExpandFABS(Node));
3705 break;
3706 case ISD::IS_FPCLASS: {
3707 auto Test = static_cast<FPClassTest>(Node->getConstantOperandVal(1));
3708 if (SDValue Expanded =
3709 TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
3710 Test, Node->getFlags(), SDLoc(Node), DAG))
3711 Results.push_back(Expanded);
3712 break;
3713 }
3714 case ISD::SMIN:
3715 case ISD::SMAX:
3716 case ISD::UMIN:
3717 case ISD::UMAX: {
3718 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3719 ISD::CondCode Pred;
3720 switch (Node->getOpcode()) {
3721 default: llvm_unreachable("How did we get here?");
3722 case ISD::SMAX: Pred = ISD::SETGT; break;
3723 case ISD::SMIN: Pred = ISD::SETLT; break;
3724 case ISD::UMAX: Pred = ISD::SETUGT; break;
3725 case ISD::UMIN: Pred = ISD::SETULT; break;
3726 }
3727 Tmp1 = Node->getOperand(0);
3728 Tmp2 = Node->getOperand(1);
3729 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3730 Results.push_back(Tmp1);
3731 break;
3732 }
3733 case ISD::FMINNUM:
3734 case ISD::FMAXNUM: {
3735 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
3736 Results.push_back(Expanded);
3737 break;
3738 }
3739 case ISD::FMINIMUM:
3740 case ISD::FMAXIMUM: {
3741 if (SDValue Expanded = TLI.expandFMINIMUM_FMAXIMUM(Node, DAG))
3742 Results.push_back(Expanded);
3743 break;
3744 }
3745 case ISD::FMINIMUMNUM:
3746 case ISD::FMAXIMUMNUM: {
3747 Results.push_back(TLI.expandFMINIMUMNUM_FMAXIMUMNUM(Node, DAG));
3748 break;
3749 }
3750 case ISD::FSIN:
3751 case ISD::FCOS: {
3752 EVT VT = Node->getValueType(0);
3753 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3754 // fcos which share the same operand and both are used.
3755 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
3756 isSinCosLibcallAvailable(Node, TLI))
3757 && useSinCos(Node)) {
3758 SDVTList VTs = DAG.getVTList(VT, VT);
3759 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
3760 if (Node->getOpcode() == ISD::FCOS)
3761 Tmp1 = Tmp1.getValue(1);
3762 Results.push_back(Tmp1);
3763 }
3764 break;
3765 }
3766 case ISD::FLDEXP:
3767 case ISD::STRICT_FLDEXP: {
3768 EVT VT = Node->getValueType(0);
3769 RTLIB::Libcall LC = RTLIB::getLDEXP(VT);
3770 // Use the LibCall instead, it is very likely faster
3771 // FIXME: Use separate LibCall action.
3772 if (TLI.getLibcallName(LC))
3773 break;
3774
3775 if (SDValue Expanded = expandLdexp(Node)) {
3776 Results.push_back(Expanded);
3777 if (Node->getOpcode() == ISD::STRICT_FLDEXP)
3778 Results.push_back(Expanded.getValue(1));
3779 }
3780
3781 break;
3782 }
3783 case ISD::FFREXP: {
3784 RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
3785 // Use the LibCall instead, it is very likely faster
3786 // FIXME: Use separate LibCall action.
3787 if (TLI.getLibcallName(LC))
3788 break;
3789
3790 if (SDValue Expanded = expandFrexp(Node)) {
3791 Results.push_back(Expanded);
3792 Results.push_back(Expanded.getValue(1));
3793 }
3794 break;
3795 }
3796 case ISD::FSINCOS: {
3797 if (isSinCosLibcallAvailable(Node, TLI))
3798 break;
3799 EVT VT = Node->getValueType(0);
3800 SDValue Op = Node->getOperand(0);
3801 SDNodeFlags Flags = Node->getFlags();
3802 Tmp1 = DAG.getNode(ISD::FSIN, dl, VT, Op, Flags);
3803 Tmp2 = DAG.getNode(ISD::FCOS, dl, VT, Op, Flags);
3804 Results.append({Tmp1, Tmp2});
3805 break;
3806 }
3807 case ISD::FMAD:
3808 llvm_unreachable("Illegal fmad should never be formed");
3809
3810 case ISD::FP16_TO_FP:
3811 if (Node->getValueType(0) != MVT::f32) {
3812 // We can extend to types bigger than f32 in two steps without changing
3813 // the result. Since "f16 -> f32" is much more commonly available, give
3814 // CodeGen the option of emitting that before resorting to a libcall.
3815 SDValue Res =
3816 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3817 Results.push_back(
3818 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
3819 }
3820 break;
3821 case ISD::STRICT_BF16_TO_FP:
3822 case ISD::STRICT_FP16_TO_FP:
3823 if (Node->getValueType(0) != MVT::f32) {
3824 // We can extend to types bigger than f32 in two steps without changing
3825 // the result. Since "f16 -> f32" is much more commonly available, give
3826 // CodeGen the option of emitting that before resorting to a libcall.
3827 SDValue Res = DAG.getNode(Node->getOpcode(), dl, {MVT::f32, MVT::Other},
3828 {Node->getOperand(0), Node->getOperand(1)});
3829 Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
3830 {Node->getValueType(0), MVT::Other},
3831 {Res.getValue(1), Res});
3832 Results.push_back(Res);
3833 Results.push_back(Res.getValue(1));
3834 }
3835 break;
3836 case ISD::FP_TO_FP16:
3837 LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
3838 if (Node->getFlags().hasApproximateFuncs() && !TLI.useSoftFloat()) {
3839 SDValue Op = Node->getOperand(0);
3840 MVT SVT = Op.getSimpleValueType();
3841 if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3842 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3843 // Under fastmath, we can expand this node into a fround followed by
3844 // a float-half conversion.
3845 SDValue FloatVal =
3846 DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3847 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3848 Results.push_back(
3849 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
3850 }
3851 }
3852 break;
3853 case ISD::ConstantFP: {
3854 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
3855 // Check to see if this FP immediate is already legal.
3856 // If this is a legal constant, turn it into a TargetConstantFP node.
3857 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
3858 DAG.shouldOptForSize()))
3859 Results.push_back(ExpandConstantFP(CFP, true));
3860 break;
3861 }
3862 case ISD::Constant: {
3863 ConstantSDNode *CP = cast<ConstantSDNode>(Node);
3864 Results.push_back(ExpandConstant(CP));
3865 break;
3866 }
3867 case ISD::FSUB: {
3868 EVT VT = Node->getValueType(0);
3869 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3870 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
3871 const SDNodeFlags Flags = Node->getFlags();
3872 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
3873 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
3874 Results.push_back(Tmp1);
3875 }
3876 break;
3877 }
3878 case ISD::SUB: {
3879 EVT VT = Node->getValueType(0);
3882 "Don't know how to expand this subtraction!");
3883 Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
3884 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
3885 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3886 break;
3887 }
3888 case ISD::UREM:
3889 case ISD::SREM:
3890 if (TLI.expandREM(Node, Tmp1, DAG))
3891 Results.push_back(Tmp1);
3892 break;
3893 case ISD::UDIV:
3894 case ISD::SDIV: {
3895 bool isSigned = Node->getOpcode() == ISD::SDIV;
3896 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3897 EVT VT = Node->getValueType(0);
3898 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3899 SDVTList VTs = DAG.getVTList(VT, VT);
3900 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3901 Node->getOperand(1));
3902 Results.push_back(Tmp1);
3903 }
3904 break;
3905 }
3906 case ISD::MULHU:
3907 case ISD::MULHS: {
3908 unsigned ExpandOpcode =
3909 Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
3910 EVT VT = Node->getValueType(0);
3911 SDVTList VTs = DAG.getVTList(VT, VT);
3912
3913 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3914 Node->getOperand(1));
3915 Results.push_back(Tmp1.getValue(1));
3916 break;
3917 }
3918 case ISD::UMUL_LOHI:
3919 case ISD::SMUL_LOHI: {
3920 SDValue LHS = Node->getOperand(0);
3921 SDValue RHS = Node->getOperand(1);
3922 MVT VT = LHS.getSimpleValueType();
3923 unsigned MULHOpcode =
3924 Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
3925
3926 if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
3927 Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
3928 Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
3929 break;
3930 }
3931
3933 EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext());
3934 assert(TLI.isTypeLegal(HalfType));
3935 if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
3936 HalfType, DAG,
3937 TargetLowering::MulExpansionKind::Always)) {
3938 for (unsigned i = 0; i < 2; ++i) {
3939 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
3940 SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
3941 SDValue Shift =
3942 DAG.getShiftAmountConstant(HalfType.getScalarSizeInBits(), VT, dl);
3943 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3944 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3945 }
3946 break;
3947 }
3948 break;
3949 }
3950 case ISD::MUL: {
3951 EVT VT = Node->getValueType(0);
3952 SDVTList VTs = DAG.getVTList(VT, VT);
3953 // See if multiply or divide can be lowered using two-result operations.
3954 // We just need the low half of the multiply; try both the signed
3955 // and unsigned forms. If the target supports both SMUL_LOHI and
3956 // UMUL_LOHI, form a preference by checking which forms of plain
3957 // MULH it supports.
3958 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3959 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3960 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3961 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3962 unsigned OpToUse = 0;
3963 if (HasSMUL_LOHI && !HasMULHS) {
3964 OpToUse = ISD::SMUL_LOHI;
3965 } else if (HasUMUL_LOHI && !HasMULHU) {
3966 OpToUse = ISD::UMUL_LOHI;
3967 } else if (HasSMUL_LOHI) {
3968 OpToUse = ISD::SMUL_LOHI;
3969 } else if (HasUMUL_LOHI) {
3970 OpToUse = ISD::UMUL_LOHI;
3971 }
3972 if (OpToUse) {
3973 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3974 Node->getOperand(1)));
3975 break;
3976 }
3977
3978 SDValue Lo, Hi;
3979 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3984 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
3985 TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
3986 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3987 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
3988 SDValue Shift =
3989 DAG.getShiftAmountConstant(HalfType.getSizeInBits(), VT, dl);
3990 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3991 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3992 }
3993 break;
3994 }
3995 case ISD::FSHL:
3996 case ISD::FSHR:
3997 if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
3998 Results.push_back(Expanded);
3999 break;
4000 case ISD::ROTL:
4001 case ISD::ROTR:
4002 if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
4003 Results.push_back(Expanded);
4004 break;
4005 case ISD::SADDSAT:
4006 case ISD::UADDSAT:
4007 case ISD::SSUBSAT:
4008 case ISD::USUBSAT:
4009 Results.push_back(TLI.expandAddSubSat(Node, DAG));
4010 break;
4011 case ISD::SCMP:
4012 case ISD::UCMP:
4013 Results.push_back(TLI.expandCMP(Node, DAG));
4014 break;
4015 case ISD::SSHLSAT:
4016 case ISD::USHLSAT:
4017 Results.push_back(TLI.expandShlSat(Node, DAG));
4018 break;
4019 case ISD::SMULFIX:
4020 case ISD::SMULFIXSAT:
4021 case ISD::UMULFIX:
4022 case ISD::UMULFIXSAT:
4023 Results.push_back(TLI.expandFixedPointMul(Node, DAG));
4024 break;
4025 case ISD::SDIVFIX:
4026 case ISD::SDIVFIXSAT:
4027 case ISD::UDIVFIX:
4028 case ISD::UDIVFIXSAT:
4029 if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
4030 Node->getOperand(0),
4031 Node->getOperand(1),
4032 Node->getConstantOperandVal(2),
4033 DAG)) {
4034 Results.push_back(V);
4035 break;
4036 }
4037 // FIXME: We might want to retry here with a wider type if we fail, if that
4038 // type is legal.
4039 // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
4040 // <= 128 (which is the case for all of the default Embedded-C types),
4041 // we will only get here with types and scales that we could always expand
4042 // if we were allowed to generate libcalls to division functions of illegal
4043 // type. But we cannot do that.
4044 llvm_unreachable("Cannot expand DIVFIX!");
4045 case ISD::UADDO_CARRY:
4046 case ISD::USUBO_CARRY: {
4047 SDValue LHS = Node->getOperand(0);
4048 SDValue RHS = Node->getOperand(1);
4049 SDValue Carry = Node->getOperand(2);
4050
4051 bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
4052
4053 // Initial add of the 2 operands.
4054 unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
4055 EVT VT = LHS.getValueType();
4056 SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
4057
4058 // Initial check for overflow.
4059 EVT CarryType = Node->getValueType(1);
4060 EVT SetCCType = getSetCCResultType(Node->getValueType(0));
4061 ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT;
4062 SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
4063
4064 // Add of the sum and the carry.
4065 SDValue One = DAG.getConstant(1, dl, VT);
4066 SDValue CarryExt =
4067 DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
4068 SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
4069
4070 // Second check for overflow. If we are adding, we can only overflow if the
4071 // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
4072 // If we are subtracting, we can only overflow if the initial sum is 0 and
4073 // the carry is set, resulting in a new sum of all 1s.
4074 SDValue Zero = DAG.getConstant(0, dl, VT);
4075 SDValue Overflow2 =
4076 IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
4077 : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
4078 Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
4079 DAG.getZExtOrTrunc(Carry, dl, SetCCType));
4080
4081 SDValue ResultCarry =
4082 DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
4083
4084 Results.push_back(Sum2);
4085 Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
4086 break;
4087 }
4088 case ISD::SADDO:
4089 case ISD::SSUBO: {
4090 SDValue Result, Overflow;
4091 TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
4092 Results.push_back(Result);
4093 Results.push_back(Overflow);
4094 break;
4095 }
4096 case ISD::UADDO:
4097 case ISD::USUBO: {
4098 SDValue Result, Overflow;
4099 TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
4100 Results.push_back(Result);
4101 Results.push_back(Overflow);
4102 break;
4103 }
4104 case ISD::UMULO:
4105 case ISD::SMULO: {
4106 SDValue Result, Overflow;
4107 if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
4108 Results.push_back(Result);
4109 Results.push_back(Overflow);
4110 }
4111 break;
4112 }
4113 case ISD::BUILD_PAIR: {
4114 EVT PairTy = Node->getValueType(0);
4115 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
4116 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
4117 Tmp2 = DAG.getNode(
4118 ISD::SHL, dl, PairTy, Tmp2,
4119 DAG.getShiftAmountConstant(PairTy.getSizeInBits() / 2, PairTy, dl));
4120 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
4121 break;
4122 }
4123 case ISD::SELECT:
4124 Tmp1 = Node->getOperand(0);
4125 Tmp2 = Node->getOperand(1);
4126 Tmp3 = Node->getOperand(2);
4127 if (Tmp1.getOpcode() == ISD::SETCC) {
4128 Tmp1 = DAG.getSelectCC(
4129 dl, Tmp1.getOperand(0), Tmp1.getOperand(1), Tmp2, Tmp3,
4130 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get(), Node->getFlags());
4131 } else {
4132 Tmp1 =
4133 DAG.getSelectCC(dl, Tmp1, DAG.getConstant(0, dl, Tmp1.getValueType()),
4134 Tmp2, Tmp3, ISD::SETNE, Node->getFlags());
4135 }
4136 Results.push_back(Tmp1);
4137 break;
4138 case ISD::BR_JT: {
4139 SDValue Chain = Node->getOperand(0);
4140 SDValue Table = Node->getOperand(1);
4141 SDValue Index = Node->getOperand(2);
4142 int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex();
4143
4144 const DataLayout &TD = DAG.getDataLayout();
4145 EVT PTy = TLI.getPointerTy(TD);
4146
4147 unsigned EntrySize =
4149
4150 // For power-of-two jumptable entry sizes convert multiplication to a shift.
4151 // This transformation needs to be done here since otherwise the MIPS
4152 // backend will end up emitting a three instruction multiply sequence
4153 // instead of a single shift and MSP430 will call a runtime function.
4154 if (llvm::isPowerOf2_32(EntrySize))
4155 Index = DAG.getNode(
4156 ISD::SHL, dl, Index.getValueType(), Index,
4157 DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
4158 else
4159 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
4160 DAG.getConstant(EntrySize, dl, Index.getValueType()));
4161 SDValue Addr = DAG.getMemBasePlusOffset(Table, Index, dl);
4162
4163 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
4164 SDValue LD = DAG.getExtLoad(
4165 ISD::SEXTLOAD, dl, PTy, Chain, Addr,
4167 Addr = LD;
4168 if (TLI.isJumpTableRelative()) {
4169 // For PIC, the sequence is:
4170 // BRIND(RelocBase + load(Jumptable + index))
4171 // RelocBase can be JumpTable, GOT or some sort of global base.
4172 Addr = DAG.getMemBasePlusOffset(TLI.getPICJumpTableRelocBase(Table, DAG),
4173 Addr, dl);
4174 }
4175
4176 Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG);
4177 Results.push_back(Tmp1);
4178 break;
4179 }
4180 case ISD::BRCOND:
4181 // Expand brcond's setcc into its constituent parts and create a BR_CC
4182 // Node.
4183 Tmp1 = Node->getOperand(0);
4184 Tmp2 = Node->getOperand(1);
4185 if (Tmp2.getOpcode() == ISD::SETCC &&
4186 TLI.isOperationLegalOrCustom(ISD::BR_CC,
4187 Tmp2.getOperand(0).getValueType())) {
4188 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
4189 Tmp2.getOperand(0), Tmp2.getOperand(1),
4190 Node->getOperand(2));
4191 } else {
4192 // We test only the i1 bit. Skip the AND if UNDEF or another AND.
4193 if (Tmp2.isUndef() ||
4194 (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
4195 Tmp3 = Tmp2;
4196 else
4197 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
4198 DAG.getConstant(1, dl, Tmp2.getValueType()));
4199 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
4200 DAG.getCondCode(ISD::SETNE), Tmp3,
4201 DAG.getConstant(0, dl, Tmp3.getValueType()),
4202 Node->getOperand(2));
4203 }
4204 Results.push_back(Tmp1);
4205 break;
4206 case ISD::SETCC:
4207 case ISD::VP_SETCC:
4208 case ISD::STRICT_FSETCC:
4209 case ISD::STRICT_FSETCCS: {
4210 bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
4211 bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
4212 Node->getOpcode() == ISD::STRICT_FSETCCS;
4213 bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
4214 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4215 unsigned Offset = IsStrict ? 1 : 0;
4216 Tmp1 = Node->getOperand(0 + Offset);
4217 Tmp2 = Node->getOperand(1 + Offset);
4218 Tmp3 = Node->getOperand(2 + Offset);
4219 SDValue Mask, EVL;
4220 if (IsVP) {
4221 Mask = Node->getOperand(3 + Offset);
4222 EVL = Node->getOperand(4 + Offset);
4223 }
4224 bool Legalized = TLI.LegalizeSetCCCondCode(
4225 DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
4226 Chain, IsSignaling);
4227
4228 if (Legalized) {
4229 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4230 // condition code, create a new SETCC node.
4231 if (Tmp3.getNode()) {
4232 if (IsStrict) {
4233 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
4234 {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
4235 Chain = Tmp1.getValue(1);
4236 } else if (IsVP) {
4237 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
4238 {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
4239 } else {
4240 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
4241 Tmp2, Tmp3, Node->getFlags());
4242 }
4243 }
4244
4245 // If we expanded the SETCC by inverting the condition code, then wrap
4246 // the existing SETCC in a NOT to restore the intended condition.
4247 if (NeedInvert) {
4248 if (!IsVP)
4249 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
4250 else
4251 Tmp1 =
4252 DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
4253 }
4254
4255 Results.push_back(Tmp1);
4256 if (IsStrict)
4257 Results.push_back(Chain);
4258
4259 break;
4260 }
4261
4262 // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4263 // understand if this code is useful for strict nodes.
4264 assert(!IsStrict && "Don't know how to expand for strict nodes.");
4265
4266 // Otherwise, SETCC for the given comparison type must be completely
4267 // illegal; expand it into a SELECT_CC.
4268 // FIXME: This drops the mask/evl for VP_SETCC.
4269 EVT VT = Node->getValueType(0);
4270 EVT Tmp1VT = Tmp1.getValueType();
4271 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
4272 DAG.getBoolConstant(true, dl, VT, Tmp1VT),
4273 DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3,
4274 Node->getFlags());
4275 Results.push_back(Tmp1);
4276 break;
4277 }
4278 case ISD::SELECT_CC: {
4279 // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
4280 Tmp1 = Node->getOperand(0); // LHS
4281 Tmp2 = Node->getOperand(1); // RHS
4282 Tmp3 = Node->getOperand(2); // True
4283 Tmp4 = Node->getOperand(3); // False
4284 EVT VT = Node->getValueType(0);
4285 SDValue Chain;
4286 SDValue CC = Node->getOperand(4);
4287 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
4288
4289 if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
4290 // If the condition code is legal, then we need to expand this
4291 // node using SETCC and SELECT.
4292 EVT CmpVT = Tmp1.getValueType();
4294 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
4295 "expanded.");
4296 EVT CCVT = getSetCCResultType(CmpVT);
4297 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
4298 Results.push_back(
4299 DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4, Node->getFlags()));
4300 break;
4301 }
4302
4303 // SELECT_CC is legal, so the condition code must not be.
4304 bool Legalized = false;
4305 // Try to legalize by inverting the condition. This is for targets that
4306 // might support an ordered version of a condition, but not the unordered
4307 // version (or vice versa).
4308 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
4309 if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
4310 // Use the new condition code and swap true and false
4311 Legalized = true;
4312 Tmp1 =
4313 DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC, Node->getFlags());
4314 } else {
4315 // If The inverse is not legal, then try to swap the arguments using
4316 // the inverse condition code.
4318 if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
4319 // The swapped inverse condition is legal, so swap true and false,
4320 // lhs and rhs.
4321 Legalized = true;
4322 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC,
4323 Node->getFlags());
4324 }
4325 }
4326
4327 if (!Legalized) {
4328 Legalized = TLI.LegalizeSetCCCondCode(
4329 DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
4330 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4331
4332 assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
4333
4334 // If we expanded the SETCC by inverting the condition code, then swap
4335 // the True/False operands to match.
4336 if (NeedInvert)
4337 std::swap(Tmp3, Tmp4);
4338
4339 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4340 // condition code, create a new SELECT_CC node.
4341 if (CC.getNode()) {
4342 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
4343 Tmp2, Tmp3, Tmp4, CC, Node->getFlags());
4344 } else {
4345 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
4346 CC = DAG.getCondCode(ISD::SETNE);
4347 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
4348 Tmp2, Tmp3, Tmp4, CC, Node->getFlags());
4349 }
4350 }
4351 Results.push_back(Tmp1);
4352 break;
4353 }
4354 case ISD::BR_CC: {
4355 // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4356 SDValue Chain;
4357 Tmp1 = Node->getOperand(0); // Chain
4358 Tmp2 = Node->getOperand(2); // LHS
4359 Tmp3 = Node->getOperand(3); // RHS
4360 Tmp4 = Node->getOperand(1); // CC
4361
4362 bool Legalized = TLI.LegalizeSetCCCondCode(
4363 DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
4364 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4365 (void)Legalized;
4366 assert(Legalized && "Can't legalize BR_CC with legal condition!");
4367
4368 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
4369 // node.
4370 if (Tmp4.getNode()) {
4371 assert(!NeedInvert && "Don't know how to invert BR_CC!");
4372
4373 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
4374 Tmp4, Tmp2, Tmp3, Node->getOperand(4));
4375 } else {
4376 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4377 Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
4378 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
4379 Tmp2, Tmp3, Node->getOperand(4));
4380 }
4381 Results.push_back(Tmp1);
4382 break;
4383 }
4384 case ISD::BUILD_VECTOR:
4385 Results.push_back(ExpandBUILD_VECTOR(Node));
4386 break;
4387 case ISD::SPLAT_VECTOR:
4388 Results.push_back(ExpandSPLAT_VECTOR(Node));
4389 break;
4390 case ISD::SRA:
4391 case ISD::SRL:
4392 case ISD::SHL: {
4393 // Scalarize vector SRA/SRL/SHL.
4394 EVT VT = Node->getValueType(0);
4395 assert(VT.isVector() && "Unable to legalize non-vector shift");
4396 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
4397 unsigned NumElem = VT.getVectorNumElements();
4398
4400 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
4401 SDValue Ex =
4403 Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
4404 SDValue Sh =
4406 Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
4407 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
4408 VT.getScalarType(), Ex, Sh));
4409 }
4410
4411 SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4412 Results.push_back(Result);
4413 break;
4414 }
4415 case ISD::VECREDUCE_FADD:
4416 case ISD::VECREDUCE_FMUL:
4417 case ISD::VECREDUCE_ADD:
4418 case ISD::VECREDUCE_MUL:
4419 case ISD::VECREDUCE_AND:
4420 case ISD::VECREDUCE_OR:
4421 case ISD::VECREDUCE_XOR:
4422 case ISD::VECREDUCE_SMAX:
4423 case ISD::VECREDUCE_SMIN:
4424 case ISD::VECREDUCE_UMAX:
4425 case ISD::VECREDUCE_UMIN:
4426 case ISD::VECREDUCE_FMAX:
4427 case ISD::VECREDUCE_FMIN:
4428 case ISD::VECREDUCE_FMAXIMUM:
4429 case ISD::VECREDUCE_FMINIMUM:
4430 Results.push_back(TLI.expandVecReduce(Node, DAG));
4431 break;
4432 case ISD::VP_CTTZ_ELTS:
4433 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
4434 Results.push_back(TLI.expandVPCTTZElements(Node, DAG));
4435 break;
4436 case ISD::CLEAR_CACHE:
4437 // The default expansion of llvm.clear_cache is simply a no-op for those
4438 // targets where it is not needed.
4439 Results.push_back(Node->getOperand(0));
4440 break;
4441 case ISD::LRINT:
4442 case ISD::LLRINT: {
4443 SDValue Arg = Node->getOperand(0);
4444 EVT ArgVT = Arg.getValueType();
4445 EVT ResVT = Node->getValueType(0);
4446 SDLoc dl(Node);
4447 SDValue RoundNode = DAG.getNode(ISD::FRINT, dl, ArgVT, Arg);
4448 Results.push_back(DAG.getNode(ISD::FP_TO_SINT, dl, ResVT, RoundNode));
4449 break;
4450 }
4451 case ISD::ADDRSPACECAST:
4452 Results.push_back(DAG.UnrollVectorOp(Node));
4453 break;
4455 case ISD::GlobalAddress:
4458 case ISD::ConstantPool:
4459 case ISD::JumpTable:
4463 // FIXME: Custom lowering for these operations shouldn't return null!
4464 // Return true so that we don't call ConvertNodeToLibcall which also won't
4465 // do anything.
4466 return true;
4467 }
4468
4469 if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
4470 // FIXME: We were asked to expand a strict floating-point operation,
4471 // but there is currently no expansion implemented that would preserve
4472 // the "strict" properties. For now, we just fall back to the non-strict
4473 // version if that is legal on the target. The actual mutation of the
4474 // operation will happen in SelectionDAGISel::DoInstructionSelection.
4475 switch (Node->getOpcode()) {
4476 default:
4477 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4478 Node->getValueType(0))
4479 == TargetLowering::Legal)
4480 return true;
4481 break;
4482 case ISD::STRICT_FSUB: {
4484 ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4485 return true;
4487 ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4488 break;
4489
4490 EVT VT = Node->getValueType(0);
4491 const SDNodeFlags Flags = Node->getFlags();
4492 SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4493 SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4494 {Node->getOperand(0), Node->getOperand(1), Neg},
4495 Flags);
4496
4497 Results.push_back(Fadd);
4498 Results.push_back(Fadd.getValue(1));
4499 break;
4500 }
4503 case ISD::STRICT_LRINT:
4504 case ISD::STRICT_LLRINT:
4505 case ISD::STRICT_LROUND:
4507 // These are registered by the operand type instead of the value
4508 // type. Reflect that here.
4509 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4510 Node->getOperand(1).getValueType())
4511 == TargetLowering::Legal)
4512 return true;
4513 break;
4514 }
4515 }
4516
4517 // Replace the original node with the legalized result.
4518 if (Results.empty()) {
4519 LLVM_DEBUG(dbgs() << "Cannot expand node\n");
4520 return false;
4521 }
4522
4523 LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
4524 ReplaceNode(Node, Results.data());
4525 return true;
4526}
4527
4528/// Return if we can use the FAST_* variant of a math libcall for the node.
4529/// FIXME: This is just guessing, we probably should have unique specific sets
4530/// flags required per libcall.
4531static bool canUseFastMathLibcall(const SDNode *Node) {
4532 // FIXME: Probably should define fast to respect nan/inf and only be
4533 // approximate functions.
4534
4535 SDNodeFlags Flags = Node->getFlags();
4536 return Flags.hasApproximateFuncs() && Flags.hasNoNaNs() &&
4537 Flags.hasNoInfs() && Flags.hasNoSignedZeros();
4538}
4539
4540void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
4541 LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
4543 SDLoc dl(Node);
4544 TargetLowering::MakeLibCallOptions CallOptions;
4545 CallOptions.IsPostTypeLegalization = true;
4546 // FIXME: Check flags on the node to see if we can use a finite call.
4547 unsigned Opc = Node->getOpcode();
4548 switch (Opc) {
4549 case ISD::ATOMIC_FENCE: {
4550 // If the target didn't lower this, lower it to '__sync_synchronize()' call
4551 // FIXME: handle "fence singlethread" more efficiently.
4552 TargetLowering::ArgListTy Args;
4553
4554 TargetLowering::CallLoweringInfo CLI(DAG);
4555 CLI.setDebugLoc(dl)
4556 .setChain(Node->getOperand(0))
4557 .setLibCallee(
4558 CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4559 DAG.getExternalSymbol("__sync_synchronize",
4560 TLI.getPointerTy(DAG.getDataLayout())),
4561 std::move(Args));
4562
4563 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4564
4565 Results.push_back(CallResult.second);
4566 break;
4567 }
4568 // By default, atomic intrinsics are marked Legal and lowered. Targets
4569 // which don't support them directly, however, may want libcalls, in which
4570 // case they mark them Expand, and we get here.
4571 case ISD::ATOMIC_SWAP:
4572 case ISD::ATOMIC_LOAD_ADD:
4573 case ISD::ATOMIC_LOAD_SUB:
4574 case ISD::ATOMIC_LOAD_AND:
4575 case ISD::ATOMIC_LOAD_CLR:
4576 case ISD::ATOMIC_LOAD_OR:
4577 case ISD::ATOMIC_LOAD_XOR:
4578 case ISD::ATOMIC_LOAD_NAND:
4579 case ISD::ATOMIC_LOAD_MIN:
4580 case ISD::ATOMIC_LOAD_MAX:
4581 case ISD::ATOMIC_LOAD_UMIN:
4582 case ISD::ATOMIC_LOAD_UMAX:
4583 case ISD::ATOMIC_CMP_SWAP: {
4584 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4585 AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
4586 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
4587 EVT RetVT = Node->getValueType(0);
4589 if (TLI.getLibcallName(LC)) {
4590 // If outline atomic available, prepare its arguments and expand.
4591 Ops.append(Node->op_begin() + 2, Node->op_end());
4592 Ops.push_back(Node->getOperand(1));
4593
4594 } else {
4595 LC = RTLIB::getSYNC(Opc, VT);
4596 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4597 "Unexpected atomic op or value type!");
4598 // Arguments for expansion to sync libcall
4599 Ops.append(Node->op_begin() + 1, Node->op_end());
4600 }
4601 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4602 Ops, CallOptions,
4603 SDLoc(Node),
4604 Node->getOperand(0));
4605 Results.push_back(Tmp.first);
4606 Results.push_back(Tmp.second);
4607 break;
4608 }
4609 case ISD::TRAP: {
4610 // If this operation is not supported, lower it to 'abort()' call
4611 TargetLowering::ArgListTy Args;
4612 TargetLowering::CallLoweringInfo CLI(DAG);
4613 CLI.setDebugLoc(dl)
4614 .setChain(Node->getOperand(0))
4615 .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4617 "abort", TLI.getPointerTy(DAG.getDataLayout())),
4618 std::move(Args));
4619 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4620
4621 Results.push_back(CallResult.second);
4622 break;
4623 }
4624 case ISD::CLEAR_CACHE: {
4625 SDValue InputChain = Node->getOperand(0);
4626 SDValue StartVal = Node->getOperand(1);
4627 SDValue EndVal = Node->getOperand(2);
4628 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4629 DAG, RTLIB::CLEAR_CACHE, MVT::isVoid, {StartVal, EndVal}, CallOptions,
4630 SDLoc(Node), InputChain);
4631 Results.push_back(Tmp.second);
4632 break;
4633 }
4634 case ISD::FMINNUM:
4636 ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
4637 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4638 RTLIB::FMIN_PPCF128, Results);
4639 break;
4640 // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
4641 // libcall legalization for these nodes, but there is no default expasion for
4642 // these nodes either (see PR63267 for example).
4643 case ISD::FMAXNUM:
4645 ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
4646 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4647 RTLIB::FMAX_PPCF128, Results);
4648 break;
4649 case ISD::FMINIMUMNUM:
4650 ExpandFPLibCall(Node, RTLIB::FMINIMUM_NUM_F32, RTLIB::FMINIMUM_NUM_F64,
4651 RTLIB::FMINIMUM_NUM_F80, RTLIB::FMINIMUM_NUM_F128,
4652 RTLIB::FMINIMUM_NUM_PPCF128, Results);
4653 break;
4654 case ISD::FMAXIMUMNUM:
4655 ExpandFPLibCall(Node, RTLIB::FMAXIMUM_NUM_F32, RTLIB::FMAXIMUM_NUM_F64,
4656 RTLIB::FMAXIMUM_NUM_F80, RTLIB::FMAXIMUM_NUM_F128,
4657 RTLIB::FMAXIMUM_NUM_PPCF128, Results);
4658 break;
4659 case ISD::FSQRT:
4660 case ISD::STRICT_FSQRT: {
4661 // FIXME: Probably should define fast to respect nan/inf and only be
4662 // approximate functions.
4663 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
4664 {RTLIB::FAST_SQRT_F32, RTLIB::SQRT_F32},
4665 {RTLIB::FAST_SQRT_F64, RTLIB::SQRT_F64},
4666 {RTLIB::FAST_SQRT_F80, RTLIB::SQRT_F80},
4667 {RTLIB::FAST_SQRT_F128, RTLIB::SQRT_F128},
4668 {RTLIB::FAST_SQRT_PPCF128, RTLIB::SQRT_PPCF128},
4669 Results);
4670 break;
4671 }
4672 case ISD::FCBRT:
4673 ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
4674 RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4675 RTLIB::CBRT_PPCF128, Results);
4676 break;
4677 case ISD::FSIN:
4678 case ISD::STRICT_FSIN:
4679 ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
4680 RTLIB::SIN_F80, RTLIB::SIN_F128,
4681 RTLIB::SIN_PPCF128, Results);
4682 break;
4683 case ISD::FCOS:
4684 case ISD::STRICT_FCOS:
4685 ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
4686 RTLIB::COS_F80, RTLIB::COS_F128,
4687 RTLIB::COS_PPCF128, Results);
4688 break;
4689 case ISD::FTAN:
4690 case ISD::STRICT_FTAN:
4691 ExpandFPLibCall(Node, RTLIB::TAN_F32, RTLIB::TAN_F64, RTLIB::TAN_F80,
4692 RTLIB::TAN_F128, RTLIB::TAN_PPCF128, Results);
4693 break;
4694 case ISD::FASIN:
4695 case ISD::STRICT_FASIN:
4696 ExpandFPLibCall(Node, RTLIB::ASIN_F32, RTLIB::ASIN_F64, RTLIB::ASIN_F80,
4697 RTLIB::ASIN_F128, RTLIB::ASIN_PPCF128, Results);
4698 break;
4699 case ISD::FACOS:
4700 case ISD::STRICT_FACOS:
4701 ExpandFPLibCall(Node, RTLIB::ACOS_F32, RTLIB::ACOS_F64, RTLIB::ACOS_F80,
4702 RTLIB::ACOS_F128, RTLIB::ACOS_PPCF128, Results);
4703 break;
4704 case ISD::FATAN:
4705 case ISD::STRICT_FATAN:
4706 ExpandFPLibCall(Node, RTLIB::ATAN_F32, RTLIB::ATAN_F64, RTLIB::ATAN_F80,
4707 RTLIB::ATAN_F128, RTLIB::ATAN_PPCF128, Results);
4708 break;
4709 case ISD::FATAN2:
4710 case ISD::STRICT_FATAN2:
4711 ExpandFPLibCall(Node, RTLIB::ATAN2_F32, RTLIB::ATAN2_F64, RTLIB::ATAN2_F80,
4712 RTLIB::ATAN2_F128, RTLIB::ATAN2_PPCF128, Results);
4713 break;
4714 case ISD::FSINH:
4715 case ISD::STRICT_FSINH:
4716 ExpandFPLibCall(Node, RTLIB::SINH_F32, RTLIB::SINH_F64, RTLIB::SINH_F80,
4717 RTLIB::SINH_F128, RTLIB::SINH_PPCF128, Results);
4718 break;
4719 case ISD::FCOSH:
4720 case ISD::STRICT_FCOSH:
4721 ExpandFPLibCall(Node, RTLIB::COSH_F32, RTLIB::COSH_F64, RTLIB::COSH_F80,
4722 RTLIB::COSH_F128, RTLIB::COSH_PPCF128, Results);
4723 break;
4724 case ISD::FTANH:
4725 case ISD::STRICT_FTANH:
4726 ExpandFPLibCall(Node, RTLIB::TANH_F32, RTLIB::TANH_F64, RTLIB::TANH_F80,
4727 RTLIB::TANH_F128, RTLIB::TANH_PPCF128, Results);
4728 break;
4729 case ISD::FSINCOS:
4730 case ISD::FSINCOSPI: {
4731 EVT VT = Node->getValueType(0);
4732 RTLIB::Libcall LC = Node->getOpcode() == ISD::FSINCOS
4733 ? RTLIB::getSINCOS(VT)
4734 : RTLIB::getSINCOSPI(VT);
4735 bool Expanded = DAG.expandMultipleResultFPLibCall(LC, Node, Results);
4736 if (!Expanded)
4737 llvm_unreachable("Expected scalar FSINCOS[PI] to expand to libcall!");
4738 break;
4739 }
4740 case ISD::FLOG:
4741 case ISD::STRICT_FLOG:
4742 ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
4743 RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
4744 break;
4745 case ISD::FLOG2:
4746 case ISD::STRICT_FLOG2:
4747 ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
4748 RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
4749 break;
4750 case ISD::FLOG10:
4751 case ISD::STRICT_FLOG10:
4752 ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
4753 RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
4754 break;
4755 case ISD::FEXP:
4756 case ISD::STRICT_FEXP:
4757 ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
4758 RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
4759 break;
4760 case ISD::FEXP2:
4761 case ISD::STRICT_FEXP2:
4762 ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
4763 RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
4764 break;
4765 case ISD::FEXP10:
4766 ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80,
4767 RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results);
4768 break;
4769 case ISD::FTRUNC:
4770 case ISD::STRICT_FTRUNC:
4771 ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
4772 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4773 RTLIB::TRUNC_PPCF128, Results);
4774 break;
4775 case ISD::FFLOOR:
4776 case ISD::STRICT_FFLOOR:
4777 ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
4778 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4779 RTLIB::FLOOR_PPCF128, Results);
4780 break;
4781 case ISD::FCEIL:
4782 case ISD::STRICT_FCEIL:
4783 ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
4784 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4785 RTLIB::CEIL_PPCF128, Results);
4786 break;
4787 case ISD::FRINT:
4788 case ISD::STRICT_FRINT:
4789 ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
4790 RTLIB::RINT_F80, RTLIB::RINT_F128,
4791 RTLIB::RINT_PPCF128, Results);
4792 break;
4793 case ISD::FNEARBYINT:
4795 ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
4796 RTLIB::NEARBYINT_F64,
4797 RTLIB::NEARBYINT_F80,
4798 RTLIB::NEARBYINT_F128,
4799 RTLIB::NEARBYINT_PPCF128, Results);
4800 break;
4801 case ISD::FROUND:
4802 case ISD::STRICT_FROUND:
4803 ExpandFPLibCall(Node, RTLIB::ROUND_F32,
4804 RTLIB::ROUND_F64,
4805 RTLIB::ROUND_F80,
4806 RTLIB::ROUND_F128,
4807 RTLIB::ROUND_PPCF128, Results);
4808 break;
4809 case ISD::FROUNDEVEN:
4811 ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
4812 RTLIB::ROUNDEVEN_F64,
4813 RTLIB::ROUNDEVEN_F80,
4814 RTLIB::ROUNDEVEN_F128,
4815 RTLIB::ROUNDEVEN_PPCF128, Results);
4816 break;
4817 case ISD::FLDEXP:
4818 case ISD::STRICT_FLDEXP:
4819 ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
4820 RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
4821 break;
4822 case ISD::FMODF:
4823 case ISD::FFREXP: {
4824 EVT VT = Node->getValueType(0);
4825 RTLIB::Libcall LC = Node->getOpcode() == ISD::FMODF ? RTLIB::getMODF(VT)
4826 : RTLIB::getFREXP(VT);
4827 bool Expanded = DAG.expandMultipleResultFPLibCall(LC, Node, Results,
4828 /*CallRetResNo=*/0);
4829 if (!Expanded)
4830 llvm_unreachable("Expected scalar FFREXP/FMODF to expand to libcall!");
4831 break;
4832 }
4833 case ISD::FPOWI:
4834 case ISD::STRICT_FPOWI: {
4835 RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
4836 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
4837 if (!TLI.getLibcallName(LC)) {
4838 // Some targets don't have a powi libcall; use pow instead.
4839 if (Node->isStrictFPOpcode()) {
4841 DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
4842 {Node->getValueType(0), Node->getValueType(1)},
4843 {Node->getOperand(0), Node->getOperand(2)});
4844 SDValue FPOW =
4845 DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node),
4846 {Node->getValueType(0), Node->getValueType(1)},
4847 {Exponent.getValue(1), Node->getOperand(1), Exponent});
4848 Results.push_back(FPOW);
4849 Results.push_back(FPOW.getValue(1));
4850 } else {
4852 DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0),
4853 Node->getOperand(1));
4854 Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node),
4855 Node->getValueType(0),
4856 Node->getOperand(0), Exponent));
4857 }
4858 break;
4859 }
4860 unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0;
4861 bool ExponentHasSizeOfInt =
4862 DAG.getLibInfo().getIntSize() ==
4863 Node->getOperand(1 + Offset).getValueType().getSizeInBits();
4864 if (!ExponentHasSizeOfInt) {
4865 // If the exponent does not match with sizeof(int) a libcall to
4866 // RTLIB::POWI would use the wrong type for the argument.
4867 DAG.getContext()->emitError("POWI exponent does not match sizeof(int)");
4868 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
4869 break;
4870 }
4871 ExpandFPLibCall(Node, LC, Results);
4872 break;
4873 }
4874 case ISD::FPOW:
4875 case ISD::STRICT_FPOW:
4876 ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
4877 RTLIB::POW_F128, RTLIB::POW_PPCF128, Results);
4878 break;
4879 case ISD::LROUND:
4880 case ISD::STRICT_LROUND:
4881 ExpandArgFPLibCall(Node, RTLIB::LROUND_F32,
4882 RTLIB::LROUND_F64, RTLIB::LROUND_F80,
4883 RTLIB::LROUND_F128,
4884 RTLIB::LROUND_PPCF128, Results);
4885 break;
4886 case ISD::LLROUND:
4888 ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32,
4889 RTLIB::LLROUND_F64, RTLIB::LLROUND_F80,
4890 RTLIB::LLROUND_F128,
4891 RTLIB::LLROUND_PPCF128, Results);
4892 break;
4893 case ISD::LRINT:
4894 case ISD::STRICT_LRINT:
4895 ExpandArgFPLibCall(Node, RTLIB::LRINT_F32,
4896 RTLIB::LRINT_F64, RTLIB::LRINT_F80,
4897 RTLIB::LRINT_F128,
4898 RTLIB::LRINT_PPCF128, Results);
4899 break;
4900 case ISD::LLRINT:
4901 case ISD::STRICT_LLRINT:
4902 ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32,
4903 RTLIB::LLRINT_F64, RTLIB::LLRINT_F80,
4904 RTLIB::LLRINT_F128,
4905 RTLIB::LLRINT_PPCF128, Results);
4906 break;
4907 case ISD::FDIV:
4908 case ISD::STRICT_FDIV: {
4909 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
4910 {RTLIB::FAST_DIV_F32, RTLIB::DIV_F32},
4911 {RTLIB::FAST_DIV_F64, RTLIB::DIV_F64},
4912 {RTLIB::FAST_DIV_F80, RTLIB::DIV_F80},
4913 {RTLIB::FAST_DIV_F128, RTLIB::DIV_F128},
4914 {RTLIB::FAST_DIV_PPCF128, RTLIB::DIV_PPCF128}, Results);
4915 break;
4916 }
4917 case ISD::FREM:
4918 case ISD::STRICT_FREM:
4919 ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
4920 RTLIB::REM_F80, RTLIB::REM_F128,
4921 RTLIB::REM_PPCF128, Results);
4922 break;
4923 case ISD::FMA:
4924 case ISD::STRICT_FMA:
4925 ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
4926 RTLIB::FMA_F80, RTLIB::FMA_F128,
4927 RTLIB::FMA_PPCF128, Results);
4928 break;
4929 case ISD::FADD:
4930 case ISD::STRICT_FADD: {
4931 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
4932 {RTLIB::FAST_ADD_F32, RTLIB::ADD_F32},
4933 {RTLIB::FAST_ADD_F64, RTLIB::ADD_F64},
4934 {RTLIB::FAST_ADD_F80, RTLIB::ADD_F80},
4935 {RTLIB::FAST_ADD_F128, RTLIB::ADD_F128},
4936 {RTLIB::FAST_ADD_PPCF128, RTLIB::ADD_PPCF128}, Results);
4937 break;
4938 }
4939 case ISD::FMUL:
4940 case ISD::STRICT_FMUL: {
4941 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
4942 {RTLIB::FAST_MUL_F32, RTLIB::MUL_F32},
4943 {RTLIB::FAST_MUL_F64, RTLIB::MUL_F64},
4944 {RTLIB::FAST_MUL_F80, RTLIB::MUL_F80},
4945 {RTLIB::FAST_MUL_F128, RTLIB::MUL_F128},
4946 {RTLIB::FAST_MUL_PPCF128, RTLIB::MUL_PPCF128}, Results);
4947 break;
4948 }
4949 case ISD::FP16_TO_FP:
4950 if (Node->getValueType(0) == MVT::f32) {
4951 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first);
4952 }
4953 break;
4954 case ISD::STRICT_BF16_TO_FP:
4955 if (Node->getValueType(0) == MVT::f32) {
4956 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4957 DAG, RTLIB::FPEXT_BF16_F32, MVT::f32, Node->getOperand(1),
4958 CallOptions, SDLoc(Node), Node->getOperand(0));
4959 Results.push_back(Tmp.first);
4960 Results.push_back(Tmp.second);
4961 }
4962 break;
4963 case ISD::STRICT_FP16_TO_FP: {
4964 if (Node->getValueType(0) == MVT::f32) {
4965 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4966 DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions,
4967 SDLoc(Node), Node->getOperand(0));
4968 Results.push_back(Tmp.first);
4969 Results.push_back(Tmp.second);
4970 }
4971 break;
4972 }
4973 case ISD::FP_TO_FP16: {
4974 RTLIB::Libcall LC =
4975 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
4976 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
4977 Results.push_back(ExpandLibCall(LC, Node, false).first);
4978 break;
4979 }
4980 case ISD::FP_TO_BF16: {
4981 RTLIB::Libcall LC =
4982 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16);
4983 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16");
4984 Results.push_back(ExpandLibCall(LC, Node, false).first);
4985 break;
4986 }
4989 case ISD::SINT_TO_FP:
4990 case ISD::UINT_TO_FP: {
4991 // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP
4992 bool IsStrict = Node->isStrictFPOpcode();
4993 bool Signed = Node->getOpcode() == ISD::SINT_TO_FP ||
4994 Node->getOpcode() == ISD::STRICT_SINT_TO_FP;
4995 EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType();
4996 EVT RVT = Node->getValueType(0);
4997 EVT NVT = EVT();
4998 SDLoc dl(Node);
4999
5000 // Even if the input is legal, no libcall may exactly match, eg. we don't
5001 // have i1 -> fp conversions. So, it needs to be promoted to a larger type,
5002 // eg: i13 -> fp. Then, look for an appropriate libcall.
5003 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5004 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
5005 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
5006 ++t) {
5007 NVT = (MVT::SimpleValueType)t;
5008 // The source needs to big enough to hold the operand.
5009 if (NVT.bitsGE(SVT))
5010 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT)
5011 : RTLIB::getUINTTOFP(NVT, RVT);
5012 }
5013 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5014
5015 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
5016 // Sign/zero extend the argument if the libcall takes a larger type.
5018 NVT, Node->getOperand(IsStrict ? 1 : 0));
5019 CallOptions.setIsSigned(Signed);
5020 std::pair<SDValue, SDValue> Tmp =
5021 TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain);
5022 Results.push_back(Tmp.first);
5023 if (IsStrict)
5024 Results.push_back(Tmp.second);
5025 break;
5026 }
5027 case ISD::FP_TO_SINT:
5028 case ISD::FP_TO_UINT:
5031 // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT.
5032 bool IsStrict = Node->isStrictFPOpcode();
5033 bool Signed = Node->getOpcode() == ISD::FP_TO_SINT ||
5034 Node->getOpcode() == ISD::STRICT_FP_TO_SINT;
5035
5036 SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
5037 EVT SVT = Op.getValueType();
5038 EVT RVT = Node->getValueType(0);
5039 EVT NVT = EVT();
5040 SDLoc dl(Node);
5041
5042 // Even if the result is legal, no libcall may exactly match, eg. we don't
5043 // have fp -> i1 conversions. So, it needs to be promoted to a larger type,
5044 // eg: fp -> i32. Then, look for an appropriate libcall.
5045 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5046 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
5047 IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
5048 ++IntVT) {
5049 NVT = (MVT::SimpleValueType)IntVT;
5050 // The type needs to big enough to hold the result.
5051 if (NVT.bitsGE(RVT))
5052 LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT)
5053 : RTLIB::getFPTOUINT(SVT, NVT);
5054 }
5055 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5056
5057 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
5058 std::pair<SDValue, SDValue> Tmp =
5059 TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain);
5060
5061 // Truncate the result if the libcall returns a larger type.
5062 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first));
5063 if (IsStrict)
5064 Results.push_back(Tmp.second);
5065 break;
5066 }
5067
5068 case ISD::FP_ROUND:
5069 case ISD::STRICT_FP_ROUND: {
5070 // X = FP_ROUND(Y, TRUNC)
5071 // TRUNC is a flag, which is always an integer that is zero or one.
5072 // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND
5073 // is known to not change the value of Y.
5074 // We can only expand it into libcall if the TRUNC is 0.
5075 bool IsStrict = Node->isStrictFPOpcode();
5076 SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
5077 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
5078 EVT VT = Node->getValueType(0);
5079 assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() &&
5080 "Unable to expand as libcall if it is not normal rounding");
5081
5082 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT);
5083 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5084
5085 std::pair<SDValue, SDValue> Tmp =
5086 TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain);
5087 Results.push_back(Tmp.first);
5088 if (IsStrict)
5089 Results.push_back(Tmp.second);
5090 break;
5091 }
5092 case ISD::FP_EXTEND: {
5093 Results.push_back(
5094 ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(),
5095 Node->getValueType(0)),
5096 Node, false).first);
5097 break;
5098 }
5100 case ISD::STRICT_FP_TO_FP16:
5101 case ISD::STRICT_FP_TO_BF16: {
5102 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
5103 if (Node->getOpcode() == ISD::STRICT_FP_TO_FP16)
5104 LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16);
5105 else if (Node->getOpcode() == ISD::STRICT_FP_TO_BF16)
5106 LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::bf16);
5107 else
5108 LC = RTLIB::getFPEXT(Node->getOperand(1).getValueType(),
5109 Node->getValueType(0));
5110
5111 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
5112
5113 std::pair<SDValue, SDValue> Tmp =
5114 TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1),
5115 CallOptions, SDLoc(Node), Node->getOperand(0));
5116 Results.push_back(Tmp.first);
5117 Results.push_back(Tmp.second);
5118 break;
5119 }
5120 case ISD::FSUB:
5121 case ISD::STRICT_FSUB: {
5122 ExpandFastFPLibCall(Node, canUseFastMathLibcall(Node),
5123 {RTLIB::FAST_SUB_F32, RTLIB::SUB_F32},
5124 {RTLIB::FAST_SUB_F64, RTLIB::SUB_F64},
5125 {RTLIB::FAST_SUB_F80, RTLIB::SUB_F80},
5126 {RTLIB::FAST_SUB_F128, RTLIB::SUB_F128},
5127 {RTLIB::FAST_SUB_PPCF128, RTLIB::SUB_PPCF128}, Results);
5128 break;
5129 }
5130 case ISD::SREM:
5131 Results.push_back(ExpandIntLibCall(Node, true,
5132 RTLIB::SREM_I8,
5133 RTLIB::SREM_I16, RTLIB::SREM_I32,
5134 RTLIB::SREM_I64, RTLIB::SREM_I128));
5135 break;
5136 case ISD::UREM:
5137 Results.push_back(ExpandIntLibCall(Node, false,
5138 RTLIB::UREM_I8,
5139 RTLIB::UREM_I16, RTLIB::UREM_I32,
5140 RTLIB::UREM_I64, RTLIB::UREM_I128));
5141 break;
5142 case ISD::SDIV:
5143 Results.push_back(ExpandIntLibCall(Node, true,
5144 RTLIB::SDIV_I8,
5145 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
5146 RTLIB::SDIV_I64, RTLIB::SDIV_I128));
5147 break;
5148 case ISD::UDIV:
5149 Results.push_back(ExpandIntLibCall(Node, false,
5150 RTLIB::UDIV_I8,
5151 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
5152 RTLIB::UDIV_I64, RTLIB::UDIV_I128));
5153 break;
5154 case ISD::SDIVREM:
5155 case ISD::UDIVREM:
5156 // Expand into divrem libcall
5157 ExpandDivRemLibCall(Node, Results);
5158 break;
5159 case ISD::MUL:
5160 Results.push_back(ExpandIntLibCall(Node, false,
5161 RTLIB::MUL_I8,
5162 RTLIB::MUL_I16, RTLIB::MUL_I32,
5163 RTLIB::MUL_I64, RTLIB::MUL_I128));
5164 break;
5166 Results.push_back(ExpandBitCountingLibCall(
5167 Node, RTLIB::CTLZ_I32, RTLIB::CTLZ_I64, RTLIB::CTLZ_I128));
5168 break;
5169 case ISD::CTPOP:
5170 Results.push_back(ExpandBitCountingLibCall(
5171 Node, RTLIB::CTPOP_I32, RTLIB::CTPOP_I64, RTLIB::CTPOP_I128));
5172 break;
5173 case ISD::RESET_FPENV: {
5174 // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
5175 // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc.
5176 EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
5177 SDValue Ptr = DAG.getAllOnesConstant(dl, PtrTy);
5178 SDValue Chain = Node->getOperand(0);
5179 Results.push_back(
5180 DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl));
5181 break;
5182 }
5183 case ISD::GET_FPENV_MEM: {
5184 SDValue Chain = Node->getOperand(0);
5185 SDValue EnvPtr = Node->getOperand(1);
5186 Results.push_back(
5187 DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl));
5188 break;
5189 }
5190 case ISD::SET_FPENV_MEM: {
5191 SDValue Chain = Node->getOperand(0);
5192 SDValue EnvPtr = Node->getOperand(1);
5193 Results.push_back(
5194 DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl));
5195 break;
5196 }
5197 case ISD::GET_FPMODE: {
5198 // Call fegetmode, which saves control modes into a stack slot. Then load
5199 // the value to return from the stack.
5200 EVT ModeVT = Node->getValueType(0);
5202 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
5203 SDValue Chain = DAG.makeStateFunctionCall(RTLIB::FEGETMODE, StackPtr,
5204 Node->getOperand(0), dl);
5205 SDValue LdInst = DAG.getLoad(
5206 ModeVT, dl, Chain, StackPtr,
5208 Results.push_back(LdInst);
5209 Results.push_back(LdInst.getValue(1));
5210 break;
5211 }
5212 case ISD::SET_FPMODE: {
5213 // Move control modes to stack slot and then call fesetmode with the pointer
5214 // to the slot as argument.
5215 SDValue Mode = Node->getOperand(1);
5216 EVT ModeVT = Mode.getValueType();
5218 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
5219 SDValue StInst = DAG.getStore(
5220 Node->getOperand(0), dl, Mode, StackPtr,
5222 Results.push_back(
5223 DAG.makeStateFunctionCall(RTLIB::FESETMODE, StackPtr, StInst, dl));
5224 break;
5225 }
5226 case ISD::RESET_FPMODE: {
5227 // It is legalized to a call 'fesetmode(FE_DFL_MODE)'. On most targets
5228 // FE_DFL_MODE is defined as '((const femode_t *) -1)' in glibc. If not, the
5229 // target must provide custom lowering.
5230 const DataLayout &DL = DAG.getDataLayout();
5231 EVT PtrTy = TLI.getPointerTy(DL);
5232 SDValue Mode = DAG.getAllOnesConstant(dl, PtrTy);
5233 Results.push_back(DAG.makeStateFunctionCall(RTLIB::FESETMODE, Mode,
5234 Node->getOperand(0), dl));
5235 break;
5236 }
5237 }
5238
5239 // Replace the original node with the legalized result.
5240 if (!Results.empty()) {
5241 LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n");
5242 ReplaceNode(Node, Results.data());
5243 } else
5244 LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n");
5245}
5246
5247// Determine the vector type to use in place of an original scalar element when
5248// promoting equally sized vectors.
5250 MVT EltVT, MVT NewEltVT) {
5251 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
5252 MVT MidVT = OldEltsPerNewElt == 1
5253 ? NewEltVT
5254 : MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
5255 assert(TLI.isTypeLegal(MidVT) && "unexpected");
5256 return MidVT;
5257}
5258
5259void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
5260 LLVM_DEBUG(dbgs() << "Trying to promote node\n");
5262 MVT OVT = Node->getSimpleValueType(0);
5263 if (Node->getOpcode() == ISD::UINT_TO_FP ||
5264 Node->getOpcode() == ISD::SINT_TO_FP ||
5265 Node->getOpcode() == ISD::SETCC ||
5266 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
5267 Node->getOpcode() == ISD::INSERT_VECTOR_ELT ||
5268 Node->getOpcode() == ISD::VECREDUCE_FMAX ||
5269 Node->getOpcode() == ISD::VECREDUCE_FMIN ||
5270 Node->getOpcode() == ISD::VECREDUCE_FMAXIMUM ||
5271 Node->getOpcode() == ISD::VECREDUCE_FMINIMUM) {
5272 OVT = Node->getOperand(0).getSimpleValueType();
5273 }
5274 if (Node->getOpcode() == ISD::ATOMIC_STORE ||
5275 Node->getOpcode() == ISD::STRICT_UINT_TO_FP ||
5276 Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
5277 Node->getOpcode() == ISD::STRICT_FSETCC ||
5278 Node->getOpcode() == ISD::STRICT_FSETCCS ||
5279 Node->getOpcode() == ISD::VP_REDUCE_FADD ||
5280 Node->getOpcode() == ISD::VP_REDUCE_FMUL ||
5281 Node->getOpcode() == ISD::VP_REDUCE_FMAX ||
5282 Node->getOpcode() == ISD::VP_REDUCE_FMIN ||
5283 Node->getOpcode() == ISD::VP_REDUCE_FMAXIMUM ||
5284 Node->getOpcode() == ISD::VP_REDUCE_FMINIMUM ||
5285 Node->getOpcode() == ISD::VP_REDUCE_SEQ_FADD)
5286 OVT = Node->getOperand(1).getSimpleValueType();
5287 if (Node->getOpcode() == ISD::BR_CC ||
5288 Node->getOpcode() == ISD::SELECT_CC)
5289 OVT = Node->getOperand(2).getSimpleValueType();
5290 // Preserve fast math flags
5291 SDNodeFlags FastMathFlags = Node->getFlags() & SDNodeFlags::FastMathFlags;
5292 SelectionDAG::FlagInserter FlagsInserter(DAG, FastMathFlags);
5293 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
5294 SDLoc dl(Node);
5295 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
5296 switch (Node->getOpcode()) {
5297 case ISD::CTTZ:
5299 case ISD::CTLZ:
5300 case ISD::CTPOP: {
5301 // Zero extend the argument unless its cttz, then use any_extend.
5302 if (Node->getOpcode() == ISD::CTTZ ||
5303 Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF)
5304 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5305 else
5306 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
5307
5308 unsigned NewOpc = Node->getOpcode();
5309 if (NewOpc == ISD::CTTZ) {
5310 // The count is the same in the promoted type except if the original
5311 // value was zero. This can be handled by setting the bit just off
5312 // the top of the original type.
5313 auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
5314 OVT.getSizeInBits());
5315 Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
5316 DAG.getConstant(TopBit, dl, NVT));
5317 NewOpc = ISD::CTTZ_ZERO_UNDEF;
5318 }
5319 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
5320 // already the correct result.
5321 Tmp1 = DAG.getNode(NewOpc, dl, NVT, Tmp1);
5322 if (NewOpc == ISD::CTLZ) {
5323 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
5324 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
5325 DAG.getConstant(NVT.getSizeInBits() -
5326 OVT.getSizeInBits(), dl, NVT));
5327 }
5328 Results.push_back(
5329 DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1, SDNodeFlags::NoWrap));
5330 break;
5331 }
5332 case ISD::CTLZ_ZERO_UNDEF: {
5333 // We know that the argument is unlikely to be zero, hence we can take a
5334 // different approach as compared to ISD::CTLZ
5335
5336 // Any Extend the argument
5337 auto AnyExtendedNode =
5338 DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5339
5340 // Tmp1 = Tmp1 << (sizeinbits(NVT) - sizeinbits(Old VT))
5341 auto ShiftConstant = DAG.getShiftAmountConstant(
5342 NVT.getSizeInBits() - OVT.getSizeInBits(), NVT, dl);
5343 auto LeftShiftResult =
5344 DAG.getNode(ISD::SHL, dl, NVT, AnyExtendedNode, ShiftConstant);
5345
5346 // Perform the larger operation
5347 auto CTLZResult = DAG.getNode(Node->getOpcode(), dl, NVT, LeftShiftResult);
5348 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, CTLZResult));
5349 break;
5350 }
5351 case ISD::BITREVERSE:
5352 case ISD::BSWAP: {
5353 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
5354 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
5355 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5356 Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
5357 DAG.getShiftAmountConstant(DiffBits, NVT, dl));
5358
5359 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5360 break;
5361 }
5362 case ISD::FP_TO_UINT:
5364 case ISD::FP_TO_SINT:
5366 PromoteLegalFP_TO_INT(Node, dl, Results);
5367 break;
5370 Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl));
5371 break;
5372 case ISD::UINT_TO_FP:
5374 case ISD::SINT_TO_FP:
5376 PromoteLegalINT_TO_FP(Node, dl, Results);
5377 break;
5378 case ISD::VAARG: {
5379 SDValue Chain = Node->getOperand(0); // Get the chain.
5380 SDValue Ptr = Node->getOperand(1); // Get the pointer.
5381
5382 unsigned TruncOp;
5383 if (OVT.isVector()) {
5384 TruncOp = ISD::BITCAST;
5385 } else {
5386 assert(OVT.isInteger()
5387 && "VAARG promotion is supported only for vectors or integer types");
5388 TruncOp = ISD::TRUNCATE;
5389 }
5390
5391 // Perform the larger operation, then convert back
5392 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
5393 Node->getConstantOperandVal(3));
5394 Chain = Tmp1.getValue(1);
5395
5396 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
5397
5398 // Modified the chain result - switch anything that used the old chain to
5399 // use the new one.
5400 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
5401 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
5402 if (UpdatedNodes) {
5403 UpdatedNodes->insert(Tmp2.getNode());
5404 UpdatedNodes->insert(Chain.getNode());
5405 }
5406 ReplacedNode(Node);
5407 break;
5408 }
5409 case ISD::MUL:
5410 case ISD::SDIV:
5411 case ISD::SREM:
5412 case ISD::UDIV:
5413 case ISD::UREM:
5414 case ISD::SMIN:
5415 case ISD::SMAX:
5416 case ISD::UMIN:
5417 case ISD::UMAX:
5418 case ISD::AND:
5419 case ISD::OR:
5420 case ISD::XOR: {
5421 unsigned ExtOp, TruncOp;
5422 if (OVT.isVector()) {
5423 ExtOp = ISD::BITCAST;
5424 TruncOp = ISD::BITCAST;
5425 } else {
5426 assert(OVT.isInteger() && "Cannot promote logic operation");
5427
5428 switch (Node->getOpcode()) {
5429 default:
5430 ExtOp = ISD::ANY_EXTEND;
5431 break;
5432 case ISD::SDIV:
5433 case ISD::SREM:
5434 case ISD::SMIN:
5435 case ISD::SMAX:
5436 ExtOp = ISD::SIGN_EXTEND;
5437 break;
5438 case ISD::UDIV:
5439 case ISD::UREM:
5440 ExtOp = ISD::ZERO_EXTEND;
5441 break;
5442 case ISD::UMIN:
5443 case ISD::UMAX:
5444 if (TLI.isSExtCheaperThanZExt(OVT, NVT))
5445 ExtOp = ISD::SIGN_EXTEND;
5446 else
5447 ExtOp = ISD::ZERO_EXTEND;
5448 break;
5449 }
5450 TruncOp = ISD::TRUNCATE;
5451 }
5452 // Promote each of the values to the new type.
5453 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5454 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5455 // Perform the larger operation, then convert back
5456 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
5457 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
5458 break;
5459 }
5460 case ISD::UMUL_LOHI:
5461 case ISD::SMUL_LOHI: {
5462 // Promote to a multiply in a wider integer type.
5463 unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND
5465 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5466 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5467 Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2);
5468
5469 unsigned OriginalSize = OVT.getScalarSizeInBits();
5470 Tmp2 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1,
5471 DAG.getShiftAmountConstant(OriginalSize, NVT, dl));
5472 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5473 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
5474 break;
5475 }
5476 case ISD::SELECT: {
5477 unsigned ExtOp, TruncOp;
5478 if (Node->getValueType(0).isVector() ||
5479 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
5480 ExtOp = ISD::BITCAST;
5481 TruncOp = ISD::BITCAST;
5482 } else if (Node->getValueType(0).isInteger()) {
5483 ExtOp = ISD::ANY_EXTEND;
5484 TruncOp = ISD::TRUNCATE;
5485 } else {
5486 ExtOp = ISD::FP_EXTEND;
5487 TruncOp = ISD::FP_ROUND;
5488 }
5489 Tmp1 = Node->getOperand(0);
5490 // Promote each of the values to the new type.
5491 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5492 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5493 // Perform the larger operation, then round down.
5494 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
5495 if (TruncOp != ISD::FP_ROUND)
5496 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
5497 else
5498 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
5499 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5500 Results.push_back(Tmp1);
5501 break;
5502 }
5503 case ISD::VECTOR_SHUFFLE: {
5504 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
5505
5506 // Cast the two input vectors.
5507 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
5508 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
5509
5510 // Convert the shuffle mask to the right # elements.
5511 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
5512 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
5513 Results.push_back(Tmp1);
5514 break;
5515 }
5516 case ISD::VECTOR_SPLICE: {
5517 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5518 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1));
5519 Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2,
5520 Node->getOperand(2));
5521 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3));
5522 break;
5523 }
5524 case ISD::SELECT_CC: {
5525 SDValue Cond = Node->getOperand(4);
5526 ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get();
5527 // Type of the comparison operands.
5528 MVT CVT = Node->getSimpleValueType(0);
5529 assert(CVT == OVT && "not handled");
5530
5531 unsigned ExtOp = ISD::FP_EXTEND;
5532 if (NVT.isInteger()) {
5534 }
5535
5536 // Promote the comparison operands, if needed.
5537 if (TLI.isCondCodeLegal(CCCode, CVT)) {
5538 Tmp1 = Node->getOperand(0);
5539 Tmp2 = Node->getOperand(1);
5540 } else {
5541 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5542 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5543 }
5544 // Cast the true/false operands.
5545 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5546 Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5547
5548 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond},
5549 Node->getFlags());
5550
5551 // Cast the result back to the original type.
5552 if (ExtOp != ISD::FP_EXTEND)
5553 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1);
5554 else
5555 Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1,
5556 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5557
5558 Results.push_back(Tmp1);
5559 break;
5560 }
5561 case ISD::SETCC:
5562 case ISD::STRICT_FSETCC:
5563 case ISD::STRICT_FSETCCS: {
5564 unsigned ExtOp = ISD::FP_EXTEND;
5565 if (NVT.isInteger()) {
5566 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
5567 if (isSignedIntSetCC(CCCode) ||
5568 TLI.isSExtCheaperThanZExt(Node->getOperand(0).getValueType(), NVT))
5569 ExtOp = ISD::SIGN_EXTEND;
5570 else
5571 ExtOp = ISD::ZERO_EXTEND;
5572 }
5573 if (Node->isStrictFPOpcode()) {
5574 SDValue InChain = Node->getOperand(0);
5575 std::tie(Tmp1, std::ignore) =
5576 DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT);
5577 std::tie(Tmp2, std::ignore) =
5578 DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT);
5579 SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)};
5580 SDValue OutChain = DAG.getTokenFactor(dl, TmpChains);
5581 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
5582 Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs,
5583 {OutChain, Tmp1, Tmp2, Node->getOperand(3)},
5584 Node->getFlags()));
5585 Results.push_back(Results.back().getValue(1));
5586 break;
5587 }
5588 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5589 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5590 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1,
5591 Tmp2, Node->getOperand(2), Node->getFlags()));
5592 break;
5593 }
5594 case ISD::BR_CC: {
5595 unsigned ExtOp = ISD::FP_EXTEND;
5596 if (NVT.isInteger()) {
5597 ISD::CondCode CCCode =
5598 cast<CondCodeSDNode>(Node->getOperand(1))->get();
5600 }
5601 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5602 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5603 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
5604 Node->getOperand(0), Node->getOperand(1),
5605 Tmp1, Tmp2, Node->getOperand(4)));
5606 break;
5607 }
5608 case ISD::FADD:
5609 case ISD::FSUB:
5610 case ISD::FMUL:
5611 case ISD::FDIV:
5612 case ISD::FREM:
5613 case ISD::FMINNUM:
5614 case ISD::FMAXNUM:
5615 case ISD::FMINIMUM:
5616 case ISD::FMAXIMUM:
5617 case ISD::FMINIMUMNUM:
5618 case ISD::FMAXIMUMNUM:
5619 case ISD::FPOW:
5620 case ISD::FATAN2:
5621 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5622 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
5623 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
5624 Results.push_back(
5625 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5626 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5627 break;
5628
5630 case ISD::STRICT_FMAXIMUM: {
5631 SDValue InChain = Node->getOperand(0);
5632 SDVTList VTs = DAG.getVTList(NVT, MVT::Other);
5633 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, VTs, InChain,
5634 Node->getOperand(1));
5635 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, VTs, InChain,
5636 Node->getOperand(2));
5637 SmallVector<SDValue, 4> Ops = {InChain, Tmp1, Tmp2};
5638 Tmp3 = DAG.getNode(Node->getOpcode(), dl, VTs, Ops, Node->getFlags());
5639 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, DAG.getVTList(OVT, MVT::Other),
5640 InChain, Tmp3,
5641 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5642 Results.push_back(Tmp4);
5643 Results.push_back(Tmp4.getValue(1));
5644 break;
5645 }
5646
5647 case ISD::STRICT_FADD:
5648 case ISD::STRICT_FSUB:
5649 case ISD::STRICT_FMUL:
5650 case ISD::STRICT_FDIV:
5653 case ISD::STRICT_FREM:
5654 case ISD::STRICT_FPOW:
5655 case ISD::STRICT_FATAN2:
5656 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5657 {Node->getOperand(0), Node->getOperand(1)});
5658 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5659 {Node->getOperand(0), Node->getOperand(2)});
5660 Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5661 Tmp2.getValue(1));
5662 Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5663 {Tmp3, Tmp1, Tmp2});
5664 Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5665 {Tmp1.getValue(1), Tmp1,
5666 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
5667 Results.push_back(Tmp1);
5668 Results.push_back(Tmp1.getValue(1));
5669 break;
5670 case ISD::FMA:
5671 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5672 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
5673 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
5674 Results.push_back(
5675 DAG.getNode(ISD::FP_ROUND, dl, OVT,
5676 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
5677 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5678 break;
5679 case ISD::STRICT_FMA:
5680 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5681 {Node->getOperand(0), Node->getOperand(1)});
5682 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5683 {Node->getOperand(0), Node->getOperand(2)});
5684 Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5685 {Node->getOperand(0), Node->getOperand(3)});
5686 Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5687 Tmp2.getValue(1), Tmp3.getValue(1));
5688 Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5689 {Tmp4, Tmp1, Tmp2, Tmp3});
5690 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5691 {Tmp4.getValue(1), Tmp4,
5692 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
5693 Results.push_back(Tmp4);
5694 Results.push_back(Tmp4.getValue(1));
5695 break;
5696 case ISD::FCOPYSIGN:
5697 case ISD::FLDEXP:
5698 case ISD::FPOWI: {
5699 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5700 Tmp2 = Node->getOperand(1);
5701 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
5702
5703 // fcopysign doesn't change anything but the sign bit, so
5704 // (fp_round (fcopysign (fpext a), b))
5705 // is as precise as
5706 // (fp_round (fpext a))
5707 // which is a no-op. Mark it as a TRUNCating FP_ROUND.
5708 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
5709 Results.push_back(
5710 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5711 DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true)));
5712 break;
5713 }
5714 case ISD::STRICT_FLDEXP: {
5715 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5716 {Node->getOperand(0), Node->getOperand(1)});
5717 Tmp2 = Node->getOperand(2);
5718 Tmp3 = DAG.getNode(ISD::STRICT_FLDEXP, dl, {NVT, MVT::Other},
5719 {Tmp1.getValue(1), Tmp1, Tmp2});
5720 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5721 {Tmp3.getValue(1), Tmp3,
5722 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
5723 Results.push_back(Tmp4);
5724 Results.push_back(Tmp4.getValue(1));
5725 break;
5726 }
5727 case ISD::STRICT_FPOWI:
5728 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5729 {Node->getOperand(0), Node->getOperand(1)});
5730 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5731 {Tmp1.getValue(1), Tmp1, Node->getOperand(2)});
5732 Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5733 {Tmp2.getValue(1), Tmp2,
5734 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
5735 Results.push_back(Tmp3);
5736 Results.push_back(Tmp3.getValue(1));
5737 break;
5738 case ISD::FFREXP: {
5739 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5740 Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1);
5741
5742 Results.push_back(
5743 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5744 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5745
5746 Results.push_back(Tmp2.getValue(1));
5747 break;
5748 }
5749 case ISD::FMODF:
5750 case ISD::FSINCOS:
5751 case ISD::FSINCOSPI: {
5752 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5753 Tmp2 = DAG.getNode(Node->getOpcode(), dl, DAG.getVTList(NVT, NVT), Tmp1);
5754 Tmp3 = DAG.getIntPtrConstant(0, dl, /*isTarget=*/true);
5755 for (unsigned ResNum = 0; ResNum < Node->getNumValues(); ResNum++)
5756 Results.push_back(
5757 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2.getValue(ResNum), Tmp3));
5758 break;
5759 }
5760 case ISD::FFLOOR:
5761 case ISD::FCEIL:
5762 case ISD::FRINT:
5763 case ISD::FNEARBYINT:
5764 case ISD::FROUND:
5765 case ISD::FROUNDEVEN:
5766 case ISD::FTRUNC:
5767 case ISD::FNEG:
5768 case ISD::FSQRT:
5769 case ISD::FSIN:
5770 case ISD::FCOS:
5771 case ISD::FTAN:
5772 case ISD::FASIN:
5773 case ISD::FACOS:
5774 case ISD::FATAN:
5775 case ISD::FSINH:
5776 case ISD::FCOSH:
5777 case ISD::FTANH:
5778 case ISD::FLOG:
5779 case ISD::FLOG2:
5780 case ISD::FLOG10:
5781 case ISD::FABS:
5782 case ISD::FEXP:
5783 case ISD::FEXP2:
5784 case ISD::FEXP10:
5785 case ISD::FCANONICALIZE:
5786 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5787 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5788 Results.push_back(
5789 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5790 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5791 break;
5792 case ISD::STRICT_FFLOOR:
5793 case ISD::STRICT_FCEIL:
5794 case ISD::STRICT_FRINT:
5796 case ISD::STRICT_FROUND:
5798 case ISD::STRICT_FTRUNC:
5799 case ISD::STRICT_FSQRT:
5800 case ISD::STRICT_FSIN:
5801 case ISD::STRICT_FCOS:
5802 case ISD::STRICT_FTAN:
5803 case ISD::STRICT_FASIN:
5804 case ISD::STRICT_FACOS:
5805 case ISD::STRICT_FATAN:
5806 case ISD::STRICT_FSINH:
5807 case ISD::STRICT_FCOSH:
5808 case ISD::STRICT_FTANH:
5809 case ISD::STRICT_FLOG:
5810 case ISD::STRICT_FLOG2:
5811 case ISD::STRICT_FLOG10:
5812 case ISD::STRICT_FEXP:
5813 case ISD::STRICT_FEXP2:
5814 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5815 {Node->getOperand(0), Node->getOperand(1)});
5816 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5817 {Tmp1.getValue(1), Tmp1});
5818 Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5819 {Tmp2.getValue(1), Tmp2,
5820 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)});
5821 Results.push_back(Tmp3);
5822 Results.push_back(Tmp3.getValue(1));
5823 break;
5824 case ISD::BUILD_VECTOR: {
5825 MVT EltVT = OVT.getVectorElementType();
5826 MVT NewEltVT = NVT.getVectorElementType();
5827
5828 // Handle bitcasts to a different vector type with the same total bit size
5829 //
5830 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
5831 // =>
5832 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
5833
5834 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
5835 "Invalid promote type for build_vector");
5836 assert(NewEltVT.bitsLE(EltVT) && "not handled");
5837
5838 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5839
5841 for (const SDValue &Op : Node->op_values())
5842 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
5843
5844 SDLoc SL(Node);
5845 SDValue Concat =
5846 DAG.getNode(MidVT == NewEltVT ? ISD::BUILD_VECTOR : ISD::CONCAT_VECTORS,
5847 SL, NVT, NewOps);
5848 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
5849 Results.push_back(CvtVec);
5850 break;
5851 }
5853 MVT EltVT = OVT.getVectorElementType();
5854 MVT NewEltVT = NVT.getVectorElementType();
5855
5856 // Handle bitcasts to a different vector type with the same total bit size.
5857 //
5858 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
5859 // =>
5860 // v4i32:castx = bitcast x:v2i64
5861 //
5862 // i64 = bitcast
5863 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
5864 // (i32 (extract_vector_elt castx, (2 * y + 1)))
5865 //
5866
5867 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
5868 "Invalid promote type for extract_vector_elt");
5869 assert(NewEltVT.bitsLT(EltVT) && "not handled");
5870
5871 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5872 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
5873
5874 SDValue Idx = Node->getOperand(1);
5875 EVT IdxVT = Idx.getValueType();
5876 SDLoc SL(Node);
5877 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
5878 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
5879
5880 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
5881
5883 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
5884 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
5885 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
5886
5887 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
5888 CastVec, TmpIdx);
5889 NewOps.push_back(Elt);
5890 }
5891
5892 SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps);
5893 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
5894 break;
5895 }
5897 MVT EltVT = OVT.getVectorElementType();
5898 MVT NewEltVT = NVT.getVectorElementType();
5899
5900 // Handle bitcasts to a different vector type with the same total bit size
5901 //
5902 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
5903 // =>
5904 // v4i32:castx = bitcast x:v2i64
5905 // v2i32:casty = bitcast y:i64
5906 //
5907 // v2i64 = bitcast
5908 // (v4i32 insert_vector_elt
5909 // (v4i32 insert_vector_elt v4i32:castx,
5910 // (extract_vector_elt casty, 0), 2 * z),
5911 // (extract_vector_elt casty, 1), (2 * z + 1))
5912
5913 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
5914 "Invalid promote type for insert_vector_elt");
5915 assert(NewEltVT.bitsLT(EltVT) && "not handled");
5916
5917 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5918 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
5919
5920 SDValue Val = Node->getOperand(1);
5921 SDValue Idx = Node->getOperand(2);
5922 EVT IdxVT = Idx.getValueType();
5923 SDLoc SL(Node);
5924
5925 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
5926 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
5927
5928 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
5929 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
5930
5931 SDValue NewVec = CastVec;
5932 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
5933 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
5934 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
5935
5936 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
5937 CastVal, IdxOffset);
5938
5939 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
5940 NewVec, Elt, InEltIdx);
5941 }
5942
5943 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
5944 break;
5945 }
5946 case ISD::SCALAR_TO_VECTOR: {
5947 MVT EltVT = OVT.getVectorElementType();
5948 MVT NewEltVT = NVT.getVectorElementType();
5949
5950 // Handle bitcasts to different vector type with the same total bit size.
5951 //
5952 // e.g. v2i64 = scalar_to_vector x:i64
5953 // =>
5954 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
5955 //
5956
5957 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5958 SDValue Val = Node->getOperand(0);
5959 SDLoc SL(Node);
5960
5961 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
5962 SDValue Undef = DAG.getUNDEF(MidVT);
5963
5965 NewElts.push_back(CastVal);
5966 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
5967 NewElts.push_back(Undef);
5968
5969 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
5970 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
5971 Results.push_back(CvtVec);
5972 break;
5973 }
5974 case ISD::ATOMIC_SWAP:
5975 case ISD::ATOMIC_STORE: {
5976 AtomicSDNode *AM = cast<AtomicSDNode>(Node);
5977 SDLoc SL(Node);
5978 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal());
5979 assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
5980 "unexpected promotion type");
5981 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
5982 "unexpected atomic_swap with illegal type");
5983
5984 SDValue Op0 = AM->getBasePtr();
5985 SDValue Op1 = CastVal;
5986
5987 // ATOMIC_STORE uses a swapped operand order from every other AtomicSDNode,
5988 // but really it should merge with ISD::STORE.
5989 if (AM->getOpcode() == ISD::ATOMIC_STORE)
5990 std::swap(Op0, Op1);
5991
5992 SDValue NewAtomic = DAG.getAtomic(AM->getOpcode(), SL, NVT, AM->getChain(),
5993 Op0, Op1, AM->getMemOperand());
5994
5995 if (AM->getOpcode() != ISD::ATOMIC_STORE) {
5996 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
5997 Results.push_back(NewAtomic.getValue(1));
5998 } else
5999 Results.push_back(NewAtomic);
6000 break;
6001 }
6002 case ISD::ATOMIC_LOAD: {
6003 AtomicSDNode *AM = cast<AtomicSDNode>(Node);
6004 SDLoc SL(Node);
6005 assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
6006 "unexpected promotion type");
6007 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
6008 "unexpected atomic_load with illegal type");
6009
6010 SDValue NewAtomic =
6011 DAG.getAtomic(ISD::ATOMIC_LOAD, SL, NVT, DAG.getVTList(NVT, MVT::Other),
6012 {AM->getChain(), AM->getBasePtr()}, AM->getMemOperand());
6013 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
6014 Results.push_back(NewAtomic.getValue(1));
6015 break;
6016 }
6017 case ISD::SPLAT_VECTOR: {
6018 SDValue Scalar = Node->getOperand(0);
6019 MVT ScalarType = Scalar.getSimpleValueType();
6020 MVT NewScalarType = NVT.getVectorElementType();
6021 if (ScalarType.isInteger()) {
6022 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NewScalarType, Scalar);
6023 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
6024 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
6025 break;
6026 }
6027 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewScalarType, Scalar);
6028 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
6029 Results.push_back(
6030 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
6031 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
6032 break;
6033 }
6034 case ISD::VECREDUCE_FMAX:
6035 case ISD::VECREDUCE_FMIN:
6036 case ISD::VECREDUCE_FMAXIMUM:
6037 case ISD::VECREDUCE_FMINIMUM:
6038 case ISD::VP_REDUCE_FMAX:
6039 case ISD::VP_REDUCE_FMIN:
6040 case ISD::VP_REDUCE_FMAXIMUM:
6041 case ISD::VP_REDUCE_FMINIMUM:
6042 Results.push_back(PromoteReduction(Node));
6043 break;
6044 }
6045
6046 // Replace the original node with the legalized result.
6047 if (!Results.empty()) {
6048 LLVM_DEBUG(dbgs() << "Successfully promoted node\n");
6049 ReplaceNode(Node, Results.data());
6050 } else
6051 LLVM_DEBUG(dbgs() << "Could not promote node\n");
6052}
6053
6054/// This is the entry point for the file.
6057
6058 SmallPtrSet<SDNode *, 16> LegalizedNodes;
6059 // Use a delete listener to remove nodes which were deleted during
6060 // legalization from LegalizeNodes. This is needed to handle the situation
6061 // where a new node is allocated by the object pool to the same address of a
6062 // previously deleted node.
6063 DAGNodeDeletedListener DeleteListener(
6064 *this,
6065 [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); });
6066
6067 SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
6068
6069 // Visit all the nodes. We start in topological order, so that we see
6070 // nodes with their original operands intact. Legalization can produce
6071 // new nodes which may themselves need to be legalized. Iterate until all
6072 // nodes have been legalized.
6073 while (true) {
6074 bool AnyLegalized = false;
6075 for (auto NI = allnodes_end(); NI != allnodes_begin();) {
6076 --NI;
6077
6078 SDNode *N = &*NI;
6079 if (N->use_empty() && N != getRoot().getNode()) {
6080 ++NI;
6081 DeleteNode(N);
6082 continue;
6083 }
6084
6085 if (LegalizedNodes.insert(N).second) {
6086 AnyLegalized = true;
6087 Legalizer.LegalizeOp(N);
6088
6089 if (N->use_empty() && N != getRoot().getNode()) {
6090 ++NI;
6091 DeleteNode(N);
6092 }
6093 }
6094 }
6095 if (!AnyLegalized)
6096 break;
6097
6098 }
6099
6100 // Remove dead nodes now.
6102}
6103
6105 SmallSetVector<SDNode *, 16> &UpdatedNodes) {
6106 SmallPtrSet<SDNode *, 16> LegalizedNodes;
6107 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
6108
6109 // Directly insert the node in question, and legalize it. This will recurse
6110 // as needed through operands.
6111 LegalizedNodes.insert(N);
6112 Legalizer.LegalizeOp(N);
6113
6114 return LegalizedNodes.count(N);
6115}
#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 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
mir Rename Register Operands
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:1451
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition APFloat.h:1158
APInt bitcastToAPInt() const
Definition APFloat.h:1353
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
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:198
bool isBigEndian() const
Definition DataLayout.h:199
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:168
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:356
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:181
size_type size() const
Definition SmallSet.h:170
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 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.
SDValue getVectorSubVecPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, EVT SubVecVT, SDValue Index) const
Get a pointer to a sub-vector of type SubVecVT at index Idx located in memory for a vector of type Ve...
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 getVectorElementPointer(SelectionDAG &DAG, SDValue VecPtr, EVT VecVT, SDValue Index) const
Get a pointer to vector element Idx located in memory for a vector of type VecVT starting at a base a...
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 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:343
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
LLVM Value Representation.
Definition Value.h:75
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h: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:801
@ 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:774
@ 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:587
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:765
@ 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:835
@ 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:862
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:571
@ 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:738
@ 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:826
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:706
@ 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:656
@ 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:773
@ 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:809
@ 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:622
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:528
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition ISDOpcodes.h:535
@ 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:778
@ 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:663
@ 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:952
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:695
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:756
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:636
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:601
@ 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:563
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:832
@ 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:793
@ 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:870
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:718
@ 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:908
@ 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:730
@ 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:726
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:701
@ 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:552
@ 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:648
@ 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:941
@ 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:927
@ 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:838
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:815
@ 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:521
@ 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:611
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:713
@ 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:543
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(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:666
constexpr double e
Definition MathExtras.h:47
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:310
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:355
@ 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:649
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
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:342
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:288
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:1543
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:548
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:565
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:212
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:85
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h: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)