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