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