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