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;
1108 case ISD::CLEAR_CACHE:
1109 // This operation is typically going to be LibCall unless the target wants
1110 // something differrent.
1111 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1112 break;
1115 // READCYCLECOUNTER and READSTEADYCOUNTER return a i64, even if type
1116 // legalization might have expanded that to several smaller types.
1117 Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
1118 break;
1119 case ISD::READ_REGISTER:
1121 // Named register is legal in the DAG, but blocked by register name
1122 // selection if not implemented by target (to chose the correct register)
1123 // They'll be converted to Copy(To/From)Reg.
1124 Action = TargetLowering::Legal;
1125 break;
1126 case ISD::UBSANTRAP:
1127 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1128 if (Action == TargetLowering::Expand) {
1129 // replace ISD::UBSANTRAP with ISD::TRAP
1130 SDValue NewVal;
1131 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1132 Node->getOperand(0));
1133 ReplaceNode(Node, NewVal.getNode());
1134 LegalizeOp(NewVal.getNode());
1135 return;
1136 }
1137 break;
1138 case ISD::DEBUGTRAP:
1139 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1140 if (Action == TargetLowering::Expand) {
1141 // replace ISD::DEBUGTRAP with ISD::TRAP
1142 SDValue NewVal;
1143 NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1144 Node->getOperand(0));
1145 ReplaceNode(Node, NewVal.getNode());
1146 LegalizeOp(NewVal.getNode());
1147 return;
1148 }
1149 break;
1150 case ISD::SADDSAT:
1151 case ISD::UADDSAT:
1152 case ISD::SSUBSAT:
1153 case ISD::USUBSAT:
1154 case ISD::SSHLSAT:
1155 case ISD::USHLSAT:
1156 case ISD::SCMP:
1157 case ISD::UCMP:
1160 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1161 break;
1162 case ISD::SMULFIX:
1163 case ISD::SMULFIXSAT:
1164 case ISD::UMULFIX:
1165 case ISD::UMULFIXSAT:
1166 case ISD::SDIVFIX:
1167 case ISD::SDIVFIXSAT:
1168 case ISD::UDIVFIX:
1169 case ISD::UDIVFIXSAT: {
1170 unsigned Scale = Node->getConstantOperandVal(2);
1171 Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
1172 Node->getValueType(0), Scale);
1173 break;
1174 }
1175 case ISD::MSCATTER:
1176 Action = TLI.getOperationAction(Node->getOpcode(),
1177 cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
1178 break;
1179 case ISD::MSTORE:
1180 Action = TLI.getOperationAction(Node->getOpcode(),
1181 cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
1182 break;
1183 case ISD::VP_SCATTER:
1184 Action = TLI.getOperationAction(
1185 Node->getOpcode(),
1186 cast<VPScatterSDNode>(Node)->getValue().getValueType());
1187 break;
1188 case ISD::VP_STORE:
1189 Action = TLI.getOperationAction(
1190 Node->getOpcode(),
1191 cast<VPStoreSDNode>(Node)->getValue().getValueType());
1192 break;
1193 case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
1194 Action = TLI.getOperationAction(
1195 Node->getOpcode(),
1196 cast<VPStridedStoreSDNode>(Node)->getValue().getValueType());
1197 break;
1200 case ISD::VECREDUCE_ADD:
1201 case ISD::VECREDUCE_MUL:
1202 case ISD::VECREDUCE_AND:
1203 case ISD::VECREDUCE_OR:
1204 case ISD::VECREDUCE_XOR:
1213 case ISD::IS_FPCLASS:
1214 Action = TLI.getOperationAction(
1215 Node->getOpcode(), Node->getOperand(0).getValueType());
1216 break;
1219 case ISD::VP_REDUCE_FADD:
1220 case ISD::VP_REDUCE_FMUL:
1221 case ISD::VP_REDUCE_ADD:
1222 case ISD::VP_REDUCE_MUL:
1223 case ISD::VP_REDUCE_AND:
1224 case ISD::VP_REDUCE_OR:
1225 case ISD::VP_REDUCE_XOR:
1226 case ISD::VP_REDUCE_SMAX:
1227 case ISD::VP_REDUCE_SMIN:
1228 case ISD::VP_REDUCE_UMAX:
1229 case ISD::VP_REDUCE_UMIN:
1230 case ISD::VP_REDUCE_FMAX:
1231 case ISD::VP_REDUCE_FMIN:
1232 case ISD::VP_REDUCE_FMAXIMUM:
1233 case ISD::VP_REDUCE_FMINIMUM:
1234 case ISD::VP_REDUCE_SEQ_FADD:
1235 case ISD::VP_REDUCE_SEQ_FMUL:
1236 Action = TLI.getOperationAction(
1237 Node->getOpcode(), Node->getOperand(1).getValueType());
1238 break;
1239 case ISD::VP_CTTZ_ELTS:
1240 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
1241 Action = TLI.getOperationAction(Node->getOpcode(),
1242 Node->getOperand(0).getValueType());
1243 break;
1244 default:
1245 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1246 Action = TLI.getCustomOperationAction(*Node);
1247 } else {
1248 Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1249 }
1250 break;
1251 }
1252
1253 if (SimpleFinishLegalizing) {
1254 SDNode *NewNode = Node;
1255 switch (Node->getOpcode()) {
1256 default: break;
1257 case ISD::SHL:
1258 case ISD::SRL:
1259 case ISD::SRA:
1260 case ISD::ROTL:
1261 case ISD::ROTR: {
1262 // Legalizing shifts/rotates requires adjusting the shift amount
1263 // to the appropriate width.
1264 SDValue Op0 = Node->getOperand(0);
1265 SDValue Op1 = Node->getOperand(1);
1266 if (!Op1.getValueType().isVector()) {
1267 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
1268 // The getShiftAmountOperand() may create a new operand node or
1269 // return the existing one. If new operand is created we need
1270 // to update the parent node.
1271 // Do not try to legalize SAO here! It will be automatically legalized
1272 // in the next round.
1273 if (SAO != Op1)
1274 NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
1275 }
1276 }
1277 break;
1278 case ISD::FSHL:
1279 case ISD::FSHR:
1280 case ISD::SRL_PARTS:
1281 case ISD::SRA_PARTS:
1282 case ISD::SHL_PARTS: {
1283 // Legalizing shifts/rotates requires adjusting the shift amount
1284 // to the appropriate width.
1285 SDValue Op0 = Node->getOperand(0);
1286 SDValue Op1 = Node->getOperand(1);
1287 SDValue Op2 = Node->getOperand(2);
1288 if (!Op2.getValueType().isVector()) {
1289 SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
1290 // The getShiftAmountOperand() may create a new operand node or
1291 // return the existing one. If new operand is created we need
1292 // to update the parent node.
1293 if (SAO != Op2)
1294 NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
1295 }
1296 break;
1297 }
1298 }
1299
1300 if (NewNode != Node) {
1301 ReplaceNode(Node, NewNode);
1302 Node = NewNode;
1303 }
1304 switch (Action) {
1305 case TargetLowering::Legal:
1306 LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
1307 return;
1308 case TargetLowering::Custom:
1309 LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
1310 // FIXME: The handling for custom lowering with multiple results is
1311 // a complete mess.
1312 if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
1313 if (!(Res.getNode() != Node || Res.getResNo() != 0))
1314 return;
1315
1316 if (Node->getNumValues() == 1) {
1317 // Verify the new types match the original. Glue is waived because
1318 // ISD::ADDC can be legalized by replacing Glue with an integer type.
1319 assert((Res.getValueType() == Node->getValueType(0) ||
1320 Node->getValueType(0) == MVT::Glue) &&
1321 "Type mismatch for custom legalized operation");
1322 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
1323 // We can just directly replace this node with the lowered value.
1324 ReplaceNode(SDValue(Node, 0), Res);
1325 return;
1326 }
1327
1328 SmallVector<SDValue, 8> ResultVals;
1329 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1330 // Verify the new types match the original. Glue is waived because
1331 // ISD::ADDC can be legalized by replacing Glue with an integer type.
1332 assert((Res->getValueType(i) == Node->getValueType(i) ||
1333 Node->getValueType(i) == MVT::Glue) &&
1334 "Type mismatch for custom legalized operation");
1335 ResultVals.push_back(Res.getValue(i));
1336 }
1337 LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
1338 ReplaceNode(Node, ResultVals.data());
1339 return;
1340 }
1341 LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
1342 [[fallthrough]];
1343 case TargetLowering::Expand:
1344 if (ExpandNode(Node))
1345 return;
1346 [[fallthrough]];
1347 case TargetLowering::LibCall:
1348 ConvertNodeToLibcall(Node);
1349 return;
1350 case TargetLowering::Promote:
1351 PromoteNode(Node);
1352 return;
1353 }
1354 }
1355
1356 switch (Node->getOpcode()) {
1357 default:
1358#ifndef NDEBUG
1359 dbgs() << "NODE: ";
1360 Node->dump( &DAG);
1361 dbgs() << "\n";
1362#endif
1363 llvm_unreachable("Do not know how to legalize this operator!");
1364
1365 case ISD::CALLSEQ_START:
1366 case ISD::CALLSEQ_END:
1367 break;
1368 case ISD::LOAD:
1369 return LegalizeLoadOps(Node);
1370 case ISD::STORE:
1371 return LegalizeStoreOps(Node);
1372 }
1373}
1374
1375SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
1376 SDValue Vec = Op.getOperand(0);
1377 SDValue Idx = Op.getOperand(1);
1378 SDLoc dl(Op);
1379
1380 // Before we generate a new store to a temporary stack slot, see if there is
1381 // already one that we can use. There often is because when we scalarize
1382 // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
1383 // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
1384 // the vector. If all are expanded here, we don't want one store per vector
1385 // element.
1386
1387 // Caches for hasPredecessorHelper
1390 Visited.insert(Op.getNode());
1391 Worklist.push_back(Idx.getNode());
1392 SDValue StackPtr, Ch;
1393 for (SDNode *User : Vec.getNode()->uses()) {
1394 if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
1395 if (ST->isIndexed() || ST->isTruncatingStore() ||
1396 ST->getValue() != Vec)
1397 continue;
1398
1399 // Make sure that nothing else could have stored into the destination of
1400 // this store.
1401 if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
1402 continue;
1403
1404 // If the index is dependent on the store we will introduce a cycle when
1405 // creating the load (the load uses the index, and by replacing the chain
1406 // we will make the index dependent on the load). Also, the store might be
1407 // dependent on the extractelement and introduce a cycle when creating
1408 // the load.
1409 if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) ||
1410 ST->hasPredecessor(Op.getNode()))
1411 continue;
1412
1413 StackPtr = ST->getBasePtr();
1414 Ch = SDValue(ST, 0);
1415 break;
1416 }
1417 }
1418
1419 EVT VecVT = Vec.getValueType();
1420
1421 if (!Ch.getNode()) {
1422 // Store the value to a temporary stack slot, then LOAD the returned part.
1423 StackPtr = DAG.CreateStackTemporary(VecVT);
1425 StackPtr, DAG.getMachineFunction(), VecVT.isScalableVector());
1426 Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, StoreMMO);
1427 }
1428
1429 SDValue NewLoad;
1430 Align ElementAlignment =
1431 std::min(cast<StoreSDNode>(Ch)->getAlign(),
1432 DAG.getDataLayout().getPrefTypeAlign(
1433 Op.getValueType().getTypeForEVT(*DAG.getContext())));
1434
1435 if (Op.getValueType().isVector()) {
1436 StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
1437 Op.getValueType(), Idx);
1438 NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1439 MachinePointerInfo(), ElementAlignment);
1440 } else {
1441 StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1442 NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1444 ElementAlignment);
1445 }
1446
1447 // Replace the chain going out of the store, by the one out of the load.
1448 DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
1449
1450 // We introduced a cycle though, so update the loads operands, making sure
1451 // to use the original store's chain as an incoming chain.
1452 SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
1453 NewLoad->op_end());
1454 NewLoadOperands[0] = Ch;
1455 NewLoad =
1456 SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
1457 return NewLoad;
1458}
1459
1460SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
1461 assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
1462
1463 SDValue Vec = Op.getOperand(0);
1464 SDValue Part = Op.getOperand(1);
1465 SDValue Idx = Op.getOperand(2);
1466 SDLoc dl(Op);
1467
1468 // Store the value to a temporary stack slot, then LOAD the returned part.
1469 EVT VecVT = Vec.getValueType();
1470 EVT PartVT = Part.getValueType();
1471 SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
1472 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1473 MachinePointerInfo PtrInfo =
1474 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1475
1476 // First store the whole vector.
1477 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
1478
1479 // Freeze the index so we don't poison the clamping code we're about to emit.
1480 Idx = DAG.getFreeze(Idx);
1481
1482 // Then store the inserted part.
1483 if (PartVT.isVector()) {
1484 SDValue SubStackPtr =
1485 TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, PartVT, Idx);
1486
1487 // Store the subvector.
1488 Ch = DAG.getStore(
1489 Ch, dl, Part, SubStackPtr,
1490 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
1491 } else {
1492 SDValue SubStackPtr =
1493 TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1494
1495 // Store the scalar value.
1496 Ch = DAG.getTruncStore(
1497 Ch, dl, Part, SubStackPtr,
1498 MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
1499 VecVT.getVectorElementType());
1500 }
1501
1502 // Finally, load the updated vector.
1503 return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo);
1504}
1505
1506SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
1507 assert((Node->getOpcode() == ISD::BUILD_VECTOR ||
1508 Node->getOpcode() == ISD::CONCAT_VECTORS) &&
1509 "Unexpected opcode!");
1510
1511 // We can't handle this case efficiently. Allocate a sufficiently
1512 // aligned object on the stack, store each operand into it, then load
1513 // the result as a vector.
1514 // Create the stack frame object.
1515 EVT VT = Node->getValueType(0);
1516 EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType()
1517 : Node->getOperand(0).getValueType();
1518 SDLoc dl(Node);
1519 SDValue FIPtr = DAG.CreateStackTemporary(VT);
1520 int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
1521 MachinePointerInfo PtrInfo =
1522 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
1523
1524 // Emit a store of each element to the stack slot.
1526 unsigned TypeByteSize = MemVT.getSizeInBits() / 8;
1527 assert(TypeByteSize > 0 && "Vector element type too small for stack store!");
1528
1529 // If the destination vector element type of a BUILD_VECTOR is narrower than
1530 // the source element type, only store the bits necessary.
1531 bool Truncate = isa<BuildVectorSDNode>(Node) &&
1532 MemVT.bitsLT(Node->getOperand(0).getValueType());
1533
1534 // Store (in the right endianness) the elements to memory.
1535 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1536 // Ignore undef elements.
1537 if (Node->getOperand(i).isUndef()) continue;
1538
1539 unsigned Offset = TypeByteSize*i;
1540
1541 SDValue Idx =
1542 DAG.getMemBasePlusOffset(FIPtr, TypeSize::getFixed(Offset), dl);
1543
1544 if (Truncate)
1545 Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
1546 Node->getOperand(i), Idx,
1547 PtrInfo.getWithOffset(Offset), MemVT));
1548 else
1549 Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
1550 Idx, PtrInfo.getWithOffset(Offset)));
1551 }
1552
1553 SDValue StoreChain;
1554 if (!Stores.empty()) // Not all undef elements?
1555 StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
1556 else
1557 StoreChain = DAG.getEntryNode();
1558
1559 // Result is a load from the stack slot.
1560 return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
1561}
1562
1563/// Bitcast a floating-point value to an integer value. Only bitcast the part
1564/// containing the sign bit if the target has no integer value capable of
1565/// holding all bits of the floating-point value.
1566void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
1567 const SDLoc &DL,
1568 SDValue Value) const {
1569 EVT FloatVT = Value.getValueType();
1570 unsigned NumBits = FloatVT.getScalarSizeInBits();
1571 State.FloatVT = FloatVT;
1572 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
1573 // Convert to an integer of the same size.
1574 if (TLI.isTypeLegal(IVT)) {
1575 State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
1576 State.SignMask = APInt::getSignMask(NumBits);
1577 State.SignBit = NumBits - 1;
1578 return;
1579 }
1580
1581 auto &DataLayout = DAG.getDataLayout();
1582 // Store the float to memory, then load the sign part out as an integer.
1583 MVT LoadTy = TLI.getRegisterType(MVT::i8);
1584 // First create a temporary that is aligned for both the load and store.
1585 SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
1586 int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
1587 // Then store the float to it.
1588 State.FloatPtr = StackPtr;
1589 MachineFunction &MF = DAG.getMachineFunction();
1590 State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
1591 State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
1592 State.FloatPointerInfo);
1593
1594 SDValue IntPtr;
1595 if (DataLayout.isBigEndian()) {
1596 assert(FloatVT.isByteSized() && "Unsupported floating point type!");
1597 // Load out a legal integer with the same sign bit as the float.
1598 IntPtr = StackPtr;
1599 State.IntPointerInfo = State.FloatPointerInfo;
1600 } else {
1601 // Advance the pointer so that the loaded byte will contain the sign bit.
1602 unsigned ByteOffset = (NumBits / 8) - 1;
1603 IntPtr =
1604 DAG.getMemBasePlusOffset(StackPtr, TypeSize::getFixed(ByteOffset), DL);
1605 State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
1606 ByteOffset);
1607 }
1608
1609 State.IntPtr = IntPtr;
1610 State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
1611 State.IntPointerInfo, MVT::i8);
1612 State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7);
1613 State.SignBit = 7;
1614}
1615
1616/// Replace the integer value produced by getSignAsIntValue() with a new value
1617/// and cast the result back to a floating-point type.
1618SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
1619 const SDLoc &DL,
1620 SDValue NewIntValue) const {
1621 if (!State.Chain)
1622 return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
1623
1624 // Override the part containing the sign bit in the value stored on the stack.
1625 SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
1626 State.IntPointerInfo, MVT::i8);
1627 return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
1628 State.FloatPointerInfo);
1629}
1630
1631SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
1632 SDLoc DL(Node);
1633 SDValue Mag = Node->getOperand(0);
1634 SDValue Sign = Node->getOperand(1);
1635
1636 // Get sign bit into an integer value.
1637 FloatSignAsInt SignAsInt;
1638 getSignAsIntValue(SignAsInt, DL, Sign);
1639
1640 EVT IntVT = SignAsInt.IntValue.getValueType();
1641 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1642 SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
1643 SignMask);
1644
1645 // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
1646 EVT FloatVT = Mag.getValueType();
1647 if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
1648 TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
1649 SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
1650 SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
1651 SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
1652 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
1653 return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
1654 }
1655
1656 // Transform Mag value to integer, and clear the sign bit.
1657 FloatSignAsInt MagAsInt;
1658 getSignAsIntValue(MagAsInt, DL, Mag);
1659 EVT MagVT = MagAsInt.IntValue.getValueType();
1660 SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
1661 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
1662 ClearSignMask);
1663
1664 // Get the signbit at the right position for MagAsInt.
1665 int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
1666 EVT ShiftVT = IntVT;
1667 if (SignBit.getScalarValueSizeInBits() <
1668 ClearedSign.getScalarValueSizeInBits()) {
1669 SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
1670 ShiftVT = MagVT;
1671 }
1672 if (ShiftAmount > 0) {
1673 SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT);
1674 SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst);
1675 } else if (ShiftAmount < 0) {
1676 SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT);
1677 SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst);
1678 }
1679 if (SignBit.getScalarValueSizeInBits() >
1680 ClearedSign.getScalarValueSizeInBits()) {
1681 SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
1682 }
1683
1685 Flags.setDisjoint(true);
1686
1687 // Store the part with the modified sign and convert back to float.
1688 SDValue CopiedSign =
1689 DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit, Flags);
1690
1691 return modifySignAsInt(MagAsInt, DL, CopiedSign);
1692}
1693
1694SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const {
1695 // Get the sign bit as an integer.
1696 SDLoc DL(Node);
1697 FloatSignAsInt SignAsInt;
1698 getSignAsIntValue(SignAsInt, DL, Node->getOperand(0));
1699 EVT IntVT = SignAsInt.IntValue.getValueType();
1700
1701 // Flip the sign.
1702 SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1703 SDValue SignFlip =
1704 DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask);
1705
1706 // Convert back to float.
1707 return modifySignAsInt(SignAsInt, DL, SignFlip);
1708}
1709
1710SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
1711 SDLoc DL(Node);
1712 SDValue Value = Node->getOperand(0);
1713
1714 // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
1715 EVT FloatVT = Value.getValueType();
1716 if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
1717 SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
1718 return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
1719 }
1720
1721 // Transform value to integer, clear the sign bit and transform back.
1722 FloatSignAsInt ValueAsInt;
1723 getSignAsIntValue(ValueAsInt, DL, Value);
1724 EVT IntVT = ValueAsInt.IntValue.getValueType();
1725 SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
1726 SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
1727 ClearSignMask);
1728 return modifySignAsInt(ValueAsInt, DL, ClearedSign);
1729}
1730
1731void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
1733 Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
1734 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1735 " not tell us which reg is the stack pointer!");
1736 SDLoc dl(Node);
1737 EVT VT = Node->getValueType(0);
1738 SDValue Tmp1 = SDValue(Node, 0);
1739 SDValue Tmp2 = SDValue(Node, 1);
1740 SDValue Tmp3 = Node->getOperand(2);
1741 SDValue Chain = Tmp1.getOperand(0);
1742
1743 // Chain the dynamic stack allocation so that it doesn't modify the stack
1744 // pointer when other instructions are using the stack.
1745 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
1746
1747 SDValue Size = Tmp2.getOperand(1);
1748 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
1749 Chain = SP.getValue(1);
1750 Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue();
1751 const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering();
1752 unsigned Opc =
1755
1756 Align StackAlign = TFL->getStackAlign();
1757 Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size); // Value
1758 if (Alignment > StackAlign)
1759 Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
1760 DAG.getConstant(-Alignment.value(), dl, VT));
1761 Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain
1762
1763 Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
1764
1765 Results.push_back(Tmp1);
1766 Results.push_back(Tmp2);
1767}
1768
1769/// Emit a store/load combination to the stack. This stores
1770/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
1771/// a load from the stack slot to DestVT, extending it if needed.
1772/// The resultant code need not be legal.
1773SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1774 EVT DestVT, const SDLoc &dl) {
1775 return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode());
1776}
1777
1778SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
1779 EVT DestVT, const SDLoc &dl,
1780 SDValue Chain) {
1781 EVT SrcVT = SrcOp.getValueType();
1782 Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1783 Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType);
1784
1785 // Don't convert with stack if the load/store is expensive.
1786 if ((SrcVT.bitsGT(SlotVT) &&
1787 !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) ||
1788 (SlotVT.bitsLT(DestVT) &&
1789 !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT)))
1790 return SDValue();
1791
1792 // Create the stack frame object.
1793 Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign(
1794 SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1795 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign);
1796
1797 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
1798 int SPFI = StackPtrFI->getIndex();
1799 MachinePointerInfo PtrInfo =
1800 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
1801
1802 // Emit a store to the stack slot. Use a truncstore if the input value is
1803 // later than DestVT.
1804 SDValue Store;
1805
1806 if (SrcVT.bitsGT(SlotVT))
1807 Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo,
1808 SlotVT, SrcAlign);
1809 else {
1810 assert(SrcVT.bitsEq(SlotVT) && "Invalid store");
1811 Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
1812 }
1813
1814 // Result is a load from the stack slot.
1815 if (SlotVT.bitsEq(DestVT))
1816 return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
1817
1818 assert(SlotVT.bitsLT(DestVT) && "Unknown extension!");
1819 return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
1820 DestAlign);
1821}
1822
1823SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
1824 SDLoc dl(Node);
1825 // Create a vector sized/aligned stack slot, store the value to element #0,
1826 // then load the whole vector back out.
1827 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
1828
1829 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
1830 int SPFI = StackPtrFI->getIndex();
1831
1832 SDValue Ch = DAG.getTruncStore(
1833 DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
1834 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
1835 Node->getValueType(0).getVectorElementType());
1836 return DAG.getLoad(
1837 Node->getValueType(0), dl, Ch, StackPtr,
1838 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
1839}
1840
1841static bool
1843 const TargetLowering &TLI, SDValue &Res) {
1844 unsigned NumElems = Node->getNumOperands();
1845 SDLoc dl(Node);
1846 EVT VT = Node->getValueType(0);
1847
1848 // Try to group the scalars into pairs, shuffle the pairs together, then
1849 // shuffle the pairs of pairs together, etc. until the vector has
1850 // been built. This will work only if all of the necessary shuffle masks
1851 // are legal.
1852
1853 // We do this in two phases; first to check the legality of the shuffles,
1854 // and next, assuming that all shuffles are legal, to create the new nodes.
1855 for (int Phase = 0; Phase < 2; ++Phase) {
1857 NewIntermedVals;
1858 for (unsigned i = 0; i < NumElems; ++i) {
1859 SDValue V = Node->getOperand(i);
1860 if (V.isUndef())
1861 continue;
1862
1863 SDValue Vec;
1864 if (Phase)
1865 Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
1866 IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
1867 }
1868
1869 while (IntermedVals.size() > 2) {
1870 NewIntermedVals.clear();
1871 for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
1872 // This vector and the next vector are shuffled together (simply to
1873 // append the one to the other).
1874 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1875
1876 SmallVector<int, 16> FinalIndices;
1877 FinalIndices.reserve(IntermedVals[i].second.size() +
1878 IntermedVals[i+1].second.size());
1879
1880 int k = 0;
1881 for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
1882 ++j, ++k) {
1883 ShuffleVec[k] = j;
1884 FinalIndices.push_back(IntermedVals[i].second[j]);
1885 }
1886 for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
1887 ++j, ++k) {
1888 ShuffleVec[k] = NumElems + j;
1889 FinalIndices.push_back(IntermedVals[i+1].second[j]);
1890 }
1891
1892 SDValue Shuffle;
1893 if (Phase)
1894 Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
1895 IntermedVals[i+1].first,
1896 ShuffleVec);
1897 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1898 return false;
1899 NewIntermedVals.push_back(
1900 std::make_pair(Shuffle, std::move(FinalIndices)));
1901 }
1902
1903 // If we had an odd number of defined values, then append the last
1904 // element to the array of new vectors.
1905 if ((IntermedVals.size() & 1) != 0)
1906 NewIntermedVals.push_back(IntermedVals.back());
1907
1908 IntermedVals.swap(NewIntermedVals);
1909 }
1910
1911 assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
1912 "Invalid number of intermediate vectors");
1913 SDValue Vec1 = IntermedVals[0].first;
1914 SDValue Vec2;
1915 if (IntermedVals.size() > 1)
1916 Vec2 = IntermedVals[1].first;
1917 else if (Phase)
1918 Vec2 = DAG.getUNDEF(VT);
1919
1920 SmallVector<int, 16> ShuffleVec(NumElems, -1);
1921 for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
1922 ShuffleVec[IntermedVals[0].second[i]] = i;
1923 for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
1924 ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
1925
1926 if (Phase)
1927 Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
1928 else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
1929 return false;
1930 }
1931
1932 return true;
1933}
1934
1935/// Expand a BUILD_VECTOR node on targets that don't
1936/// support the operation, but do support the resultant vector type.
1937SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
1938 unsigned NumElems = Node->getNumOperands();
1939 SDValue Value1, Value2;
1940 SDLoc dl(Node);
1941 EVT VT = Node->getValueType(0);
1942 EVT OpVT = Node->getOperand(0).getValueType();
1943 EVT EltVT = VT.getVectorElementType();
1944
1945 // If the only non-undef value is the low element, turn this into a
1946 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
1947 bool isOnlyLowElement = true;
1948 bool MoreThanTwoValues = false;
1949 bool isConstant = true;
1950 for (unsigned i = 0; i < NumElems; ++i) {
1951 SDValue V = Node->getOperand(i);
1952 if (V.isUndef())
1953 continue;
1954 if (i > 0)
1955 isOnlyLowElement = false;
1956 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
1957 isConstant = false;
1958
1959 if (!Value1.getNode()) {
1960 Value1 = V;
1961 } else if (!Value2.getNode()) {
1962 if (V != Value1)
1963 Value2 = V;
1964 } else if (V != Value1 && V != Value2) {
1965 MoreThanTwoValues = true;
1966 }
1967 }
1968
1969 if (!Value1.getNode())
1970 return DAG.getUNDEF(VT);
1971
1972 if (isOnlyLowElement)
1973 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
1974
1975 // If all elements are constants, create a load from the constant pool.
1976 if (isConstant) {
1978 for (unsigned i = 0, e = NumElems; i != e; ++i) {
1979 if (ConstantFPSDNode *V =
1980 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
1981 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
1982 } else if (ConstantSDNode *V =
1983 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
1984 if (OpVT==EltVT)
1985 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
1986 else {
1987 // If OpVT and EltVT don't match, EltVT is not legal and the
1988 // element values have been promoted/truncated earlier. Undo this;
1989 // we don't want a v16i8 to become a v16i32 for example.
1990 const ConstantInt *CI = V->getConstantIntValue();
1991 CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
1992 CI->getZExtValue()));
1993 }
1994 } else {
1995 assert(Node->getOperand(i).isUndef());
1996 Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
1997 CV.push_back(UndefValue::get(OpNTy));
1998 }
1999 }
2001 SDValue CPIdx =
2002 DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
2003 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2004 return DAG.getLoad(
2005 VT, dl, DAG.getEntryNode(), CPIdx,
2006 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2007 Alignment);
2008 }
2009
2010 SmallSet<SDValue, 16> DefinedValues;
2011 for (unsigned i = 0; i < NumElems; ++i) {
2012 if (Node->getOperand(i).isUndef())
2013 continue;
2014 DefinedValues.insert(Node->getOperand(i));
2015 }
2016
2017 if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
2018 if (!MoreThanTwoValues) {
2019 SmallVector<int, 8> ShuffleVec(NumElems, -1);
2020 for (unsigned i = 0; i < NumElems; ++i) {
2021 SDValue V = Node->getOperand(i);
2022 if (V.isUndef())
2023 continue;
2024 ShuffleVec[i] = V == Value1 ? 0 : NumElems;
2025 }
2026 if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
2027 // Get the splatted value into the low element of a vector register.
2028 SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
2029 SDValue Vec2;
2030 if (Value2.getNode())
2031 Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
2032 else
2033 Vec2 = DAG.getUNDEF(VT);
2034
2035 // Return shuffle(LowValVec, undef, <0,0,0,0>)
2036 return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
2037 }
2038 } else {
2039 SDValue Res;
2040 if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
2041 return Res;
2042 }
2043 }
2044
2045 // Otherwise, we can't handle this case efficiently.
2046 return ExpandVectorBuildThroughStack(Node);
2047}
2048
2049SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
2050 SDLoc DL(Node);
2051 EVT VT = Node->getValueType(0);
2052 SDValue SplatVal = Node->getOperand(0);
2053
2054 return DAG.getSplatBuildVector(VT, DL, SplatVal);
2055}
2056
2057// Expand a node into a call to a libcall, returning the value as the first
2058// result and the chain as the second. If the result value does not fit into a
2059// register, return the lo part and set the hi part to the by-reg argument in
2060// the first. If it does fit into a single register, return the result and
2061// leave the Hi part unset.
2062std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2064 bool isSigned) {
2065 EVT CodePtrTy = TLI.getPointerTy(DAG.getDataLayout());
2067 if (const char *LibcallName = TLI.getLibcallName(LC))
2068 Callee = DAG.getExternalSymbol(LibcallName, CodePtrTy);
2069 else {
2070 Callee = DAG.getUNDEF(CodePtrTy);
2071 DAG.getContext()->emitError(Twine("no libcall available for ") +
2072 Node->getOperationName(&DAG));
2073 }
2074
2075 EVT RetVT = Node->getValueType(0);
2076 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2077
2078 // By default, the input chain to this libcall is the entry node of the
2079 // function. If the libcall is going to be emitted as a tail call then
2080 // TLI.isUsedByReturnOnly will change it to the right chain if the return
2081 // node which is being folded has a non-entry input chain.
2082 SDValue InChain = DAG.getEntryNode();
2083
2084 // isTailCall may be true since the callee does not reference caller stack
2085 // frame. Check if it's in the right position and that the return types match.
2086 SDValue TCChain = InChain;
2087 const Function &F = DAG.getMachineFunction().getFunction();
2088 bool isTailCall =
2089 TLI.isInTailCallPosition(DAG, Node, TCChain) &&
2090 (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
2091 if (isTailCall)
2092 InChain = TCChain;
2093
2095 bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned);
2096 CLI.setDebugLoc(SDLoc(Node))
2097 .setChain(InChain)
2098 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
2099 std::move(Args))
2100 .setTailCall(isTailCall)
2101 .setSExtResult(signExtend)
2102 .setZExtResult(!signExtend)
2103 .setIsPostTypeLegalization(true);
2104
2105 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2106
2107 if (!CallInfo.second.getNode()) {
2108 LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG));
2109 // It's a tailcall, return the chain (which is the DAG root).
2110 return {DAG.getRoot(), DAG.getRoot()};
2111 }
2112
2113 LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG));
2114 return CallInfo;
2115}
2116
2117std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2118 bool isSigned) {
2121 for (const SDValue &Op : Node->op_values()) {
2122 EVT ArgVT = Op.getValueType();
2123 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2124 Entry.Node = Op;
2125 Entry.Ty = ArgTy;
2126 Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned);
2127 Entry.IsZExt = !Entry.IsSExt;
2128 Args.push_back(Entry);
2129 }
2130
2131 return ExpandLibCall(LC, Node, std::move(Args), isSigned);
2132}
2133
2134void SelectionDAGLegalize::ExpandFrexpLibCall(
2136 SDLoc dl(Node);
2137 EVT VT = Node->getValueType(0);
2138 EVT ExpVT = Node->getValueType(1);
2139
2140 SDValue FPOp = Node->getOperand(0);
2141
2142 EVT ArgVT = FPOp.getValueType();
2143 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2144
2146 FPArgEntry.Node = FPOp;
2147 FPArgEntry.Ty = ArgTy;
2148
2149 SDValue StackSlot = DAG.CreateStackTemporary(ExpVT);
2150 TargetLowering::ArgListEntry PtrArgEntry;
2151 PtrArgEntry.Node = StackSlot;
2152 PtrArgEntry.Ty = PointerType::get(*DAG.getContext(),
2153 DAG.getDataLayout().getAllocaAddrSpace());
2154
2155 TargetLowering::ArgListTy Args = {FPArgEntry, PtrArgEntry};
2156
2158 auto [Call, Chain] = ExpandLibCall(LC, Node, std::move(Args), false);
2159
2160 // FIXME: Get type of int for libcall declaration and cast
2161
2162 int FrameIdx = cast<FrameIndexSDNode>(StackSlot)->getIndex();
2163 auto PtrInfo =
2164 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
2165
2166 SDValue LoadExp = DAG.getLoad(ExpVT, dl, Chain, StackSlot, PtrInfo);
2167 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2168 LoadExp.getValue(1), DAG.getRoot());
2169 DAG.setRoot(OutputChain);
2170
2171 Results.push_back(Call);
2172 Results.push_back(LoadExp);
2173}
2174
2175void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2176 RTLIB::Libcall LC,
2178 if (LC == RTLIB::UNKNOWN_LIBCALL)
2179 llvm_unreachable("Can't create an unknown libcall!");
2180
2181 if (Node->isStrictFPOpcode()) {
2182 EVT RetVT = Node->getValueType(0);
2185 // FIXME: This doesn't support tail calls.
2186 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
2187 Ops, CallOptions,
2188 SDLoc(Node),
2189 Node->getOperand(0));
2190 Results.push_back(Tmp.first);
2191 Results.push_back(Tmp.second);
2192 } else {
2193 SDValue Tmp = ExpandLibCall(LC, Node, false).first;
2194 Results.push_back(Tmp);
2195 }
2196}
2197
2198/// Expand the node to a libcall based on the result type.
2199void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2200 RTLIB::Libcall Call_F32,
2201 RTLIB::Libcall Call_F64,
2202 RTLIB::Libcall Call_F80,
2203 RTLIB::Libcall Call_F128,
2204 RTLIB::Libcall Call_PPCF128,
2206 RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0),
2207 Call_F32, Call_F64, Call_F80,
2208 Call_F128, Call_PPCF128);
2209 ExpandFPLibCall(Node, LC, Results);
2210}
2211
2212SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2213 RTLIB::Libcall Call_I8,
2214 RTLIB::Libcall Call_I16,
2215 RTLIB::Libcall Call_I32,
2216 RTLIB::Libcall Call_I64,
2217 RTLIB::Libcall Call_I128) {
2218 RTLIB::Libcall LC;
2219 switch (Node->getSimpleValueType(0).SimpleTy) {
2220 default: llvm_unreachable("Unexpected request for libcall!");
2221 case MVT::i8: LC = Call_I8; break;
2222 case MVT::i16: LC = Call_I16; break;
2223 case MVT::i32: LC = Call_I32; break;
2224 case MVT::i64: LC = Call_I64; break;
2225 case MVT::i128: LC = Call_I128; break;
2226 }
2227 return ExpandLibCall(LC, Node, isSigned).first;
2228}
2229
2230/// Expand the node to a libcall based on first argument type (for instance
2231/// lround and its variant).
2232void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
2233 RTLIB::Libcall Call_F32,
2234 RTLIB::Libcall Call_F64,
2235 RTLIB::Libcall Call_F80,
2236 RTLIB::Libcall Call_F128,
2237 RTLIB::Libcall Call_PPCF128,
2239 EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType();
2241 Call_F32, Call_F64, Call_F80,
2242 Call_F128, Call_PPCF128);
2243 ExpandFPLibCall(Node, LC, Results);
2244}
2245
2246/// Issue libcalls to __{u}divmod to compute div / rem pairs.
2247void
2248SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
2250 unsigned Opcode = Node->getOpcode();
2251 bool isSigned = Opcode == ISD::SDIVREM;
2252
2253 RTLIB::Libcall LC;
2254 switch (Node->getSimpleValueType(0).SimpleTy) {
2255 default: llvm_unreachable("Unexpected request for libcall!");
2256 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
2257 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
2258 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
2259 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
2260 case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
2261 }
2262
2263 // The input chain to this libcall is the entry node of the function.
2264 // Legalizing the call will automatically add the previous call to the
2265 // dependence.
2266 SDValue InChain = DAG.getEntryNode();
2267
2268 EVT RetVT = Node->getValueType(0);
2269 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2270
2273 for (const SDValue &Op : Node->op_values()) {
2274 EVT ArgVT = Op.getValueType();
2275 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2276 Entry.Node = Op;
2277 Entry.Ty = ArgTy;
2278 Entry.IsSExt = isSigned;
2279 Entry.IsZExt = !isSigned;
2280 Args.push_back(Entry);
2281 }
2282
2283 // Also pass the return address of the remainder.
2284 SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
2285 Entry.Node = FIPtr;
2286 Entry.Ty = PointerType::getUnqual(RetTy->getContext());
2287 Entry.IsSExt = isSigned;
2288 Entry.IsZExt = !isSigned;
2289 Args.push_back(Entry);
2290
2291 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2292 TLI.getPointerTy(DAG.getDataLayout()));
2293
2294 SDLoc dl(Node);
2296 CLI.setDebugLoc(dl)
2297 .setChain(InChain)
2298 .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
2299 std::move(Args))
2300 .setSExtResult(isSigned)
2301 .setZExtResult(!isSigned);
2302
2303 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2304
2305 // Remainder is loaded back from the stack frame.
2306 SDValue Rem =
2307 DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo());
2308 Results.push_back(CallInfo.first);
2309 Results.push_back(Rem);
2310}
2311
2312/// Return true if sincos libcall is available.
2314 RTLIB::Libcall LC;
2315 switch (Node->getSimpleValueType(0).SimpleTy) {
2316 default: llvm_unreachable("Unexpected request for libcall!");
2317 case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2318 case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2319 case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2320 case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2321 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2322 }
2323 return TLI.getLibcallName(LC) != nullptr;
2324}
2325
2326/// Only issue sincos libcall if both sin and cos are needed.
2327static bool useSinCos(SDNode *Node) {
2328 unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
2329 ? ISD::FCOS : ISD::FSIN;
2330
2331 SDValue Op0 = Node->getOperand(0);
2332 for (const SDNode *User : Op0.getNode()->uses()) {
2333 if (User == Node)
2334 continue;
2335 // The other user might have been turned into sincos already.
2336 if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
2337 return true;
2338 }
2339 return false;
2340}
2341
2342/// Issue libcalls to sincos to compute sin / cos pairs.
2343void
2344SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
2346 RTLIB::Libcall LC;
2347 switch (Node->getSimpleValueType(0).SimpleTy) {
2348 default: llvm_unreachable("Unexpected request for libcall!");
2349 case MVT::f32: LC = RTLIB::SINCOS_F32; break;
2350 case MVT::f64: LC = RTLIB::SINCOS_F64; break;
2351 case MVT::f80: LC = RTLIB::SINCOS_F80; break;
2352 case MVT::f128: LC = RTLIB::SINCOS_F128; break;
2353 case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
2354 }
2355
2356 // The input chain to this libcall is the entry node of the function.
2357 // Legalizing the call will automatically add the previous call to the
2358 // dependence.
2359 SDValue InChain = DAG.getEntryNode();
2360
2361 EVT RetVT = Node->getValueType(0);
2362 Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
2363
2366
2367 // Pass the argument.
2368 Entry.Node = Node->getOperand(0);
2369 Entry.Ty = RetTy;
2370 Entry.IsSExt = false;
2371 Entry.IsZExt = false;
2372 Args.push_back(Entry);
2373
2374 // Pass the return address of sin.
2375 SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
2376 Entry.Node = SinPtr;
2377 Entry.Ty = PointerType::getUnqual(RetTy->getContext());
2378 Entry.IsSExt = false;
2379 Entry.IsZExt = false;
2380 Args.push_back(Entry);
2381
2382 // Also pass the return address of the cos.
2383 SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
2384 Entry.Node = CosPtr;
2385 Entry.Ty = PointerType::getUnqual(RetTy->getContext());
2386 Entry.IsSExt = false;
2387 Entry.IsZExt = false;
2388 Args.push_back(Entry);
2389
2390 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
2391 TLI.getPointerTy(DAG.getDataLayout()));
2392
2393 SDLoc dl(Node);
2395 CLI.setDebugLoc(dl).setChain(InChain).setLibCallee(
2396 TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee,
2397 std::move(Args));
2398
2399 std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
2400
2401 Results.push_back(
2402 DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo()));
2403 Results.push_back(
2404 DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo()));
2405}
2406
2407SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
2408 SDLoc dl(Node);
2409 EVT VT = Node->getValueType(0);
2410 SDValue X = Node->getOperand(0);
2411 SDValue N = Node->getOperand(1);
2412 EVT ExpVT = N.getValueType();
2413 EVT AsIntVT = VT.changeTypeToInteger();
2414 if (AsIntVT == EVT()) // TODO: How to handle f80?
2415 return SDValue();
2416
2417 if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
2418 return SDValue();
2419
2420 SDNodeFlags NSW;
2421 NSW.setNoSignedWrap(true);
2422 SDNodeFlags NUW_NSW;
2423 NUW_NSW.setNoUnsignedWrap(true);
2424 NUW_NSW.setNoSignedWrap(true);
2425
2426 EVT SetCCVT =
2427 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
2429
2430 const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
2431 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2432 const int Precision = APFloat::semanticsPrecision(FltSem);
2433
2434 const SDValue MaxExp = DAG.getConstant(MaxExpVal, dl, ExpVT);
2435 const SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
2436
2437 const SDValue DoubleMaxExp = DAG.getConstant(2 * MaxExpVal, dl, ExpVT);
2438
2439 const APFloat One(FltSem, "1.0");
2440 APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
2441
2442 // Offset by precision to avoid denormal range.
2443 APFloat ScaleDownK =
2444 scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
2445
2446 // TODO: Should really introduce control flow and use a block for the >
2447 // MaxExp, < MinExp cases
2448
2449 // First, handle exponents Exp > MaxExp and scale down.
2450 SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
2451
2452 SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
2453 SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
2454 SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
2455 SDValue DecN1 =
2456 DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
2457
2458 SDValue ScaleUpTwice =
2459 DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
2460
2461 const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
2462 SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
2463 SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
2464
2465 SDValue SelectN_Big =
2466 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
2467 SDValue SelectX_Big =
2468 DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
2469
2470 // Now handle exponents Exp < MinExp
2471 SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
2472
2473 SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
2474 SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
2475
2476 SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
2477
2478 SDValue ClampMinVal =
2479 DAG.getConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
2480 SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
2481 SDValue IncN1 =
2482 DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
2483
2484 const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
2485 SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
2486 SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
2487
2488 SDValue ScaleDownTwice = DAG.getSetCC(
2489 dl, SetCCVT, N, DAG.getConstant(2 * MinExpVal + Precision, dl, ExpVT),
2490 ISD::SETULT);
2491
2492 SDValue SelectN_Small =
2493 DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
2494 SDValue SelectX_Small =
2495 DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
2496
2497 // Now combine the two out of range exponent handling cases with the base
2498 // case.
2499 SDValue NewX = DAG.getNode(
2500 ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
2501 DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
2502
2503 SDValue NewN = DAG.getNode(
2504 ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
2505 DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
2506
2507 SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
2508
2509 SDValue ExponentShiftAmt =
2510 DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
2511 SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
2512
2513 SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
2514 ExponentShiftAmt, NUW_NSW);
2515 SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
2516 return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
2517}
2518
2519SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
2520 SDLoc dl(Node);
2521 SDValue Val = Node->getOperand(0);
2522 EVT VT = Val.getValueType();
2523 EVT ExpVT = Node->getValueType(1);
2524 EVT AsIntVT = VT.changeTypeToInteger();
2525 if (AsIntVT == EVT()) // TODO: How to handle f80?
2526 return SDValue();
2527
2529 const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2530 const unsigned Precision = APFloat::semanticsPrecision(FltSem);
2531 const unsigned BitSize = VT.getScalarSizeInBits();
2532
2533 // TODO: Could introduce control flow and skip over the denormal handling.
2534
2535 // scale_up = fmul value, scalbn(1.0, precision + 1)
2536 // extracted_exp = (bitcast value to uint) >> precision - 1
2537 // biased_exp = extracted_exp + min_exp
2538 // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
2539 //
2540 // is_denormal = val < smallest_normalized
2541 // computed_fract = is_denormal ? scale_up : extracted_fract
2542 // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
2543 //
2544 // result_0 = (!isfinite(val) || iszero(val)) ? val : computed_fract
2545 // result_1 = (!isfinite(val) || iszero(val)) ? 0 : computed_exp
2546
2547 SDValue NegSmallestNormalizedInt = DAG.getConstant(
2548 APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
2549 AsIntVT);
2550
2551 SDValue SmallestNormalizedInt = DAG.getConstant(
2552 APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
2553 AsIntVT);
2554
2555 // Masks out the exponent bits.
2556 SDValue ExpMask =
2557 DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
2558
2559 // Mask out the exponent part of the value.
2560 //
2561 // e.g, for f32 FractSignMaskVal = 0x807fffff
2562 APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
2563 FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
2564
2565 APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
2566 SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
2567
2568 SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
2569
2570 const APFloat One(FltSem, "1.0");
2571 // Scale a possible denormal input.
2572 // e.g., for f64, 0x1p+54
2573 APFloat ScaleUpKVal =
2574 scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
2575
2576 SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
2577 SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
2578
2579 EVT SetCCVT =
2580 TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2581
2582 SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
2583
2584 SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
2585
2586 SDValue AddNegSmallestNormal =
2587 DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
2588 SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
2589 NegSmallestNormalizedInt, ISD::SETULE);
2590
2591 SDValue IsDenormal =
2592 DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
2593
2594 SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
2595 SDValue Zero = DAG.getConstant(0, dl, ExpVT);
2596
2597 SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
2598 SDValue ScaledSelect =
2599 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
2600
2601 SDValue ExpMaskScaled =
2602 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
2603
2604 SDValue ScaledValue =
2605 DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
2606
2607 // Extract the exponent bits.
2608 SDValue ExponentShiftAmt =
2609 DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
2610 SDValue ShiftedExp =
2611 DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
2612 SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
2613
2614 SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
2615 SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
2616 SDValue DenormalExpBias =
2617 DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
2618
2619 SDValue MaskedFractAsInt =
2620 DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
2621 const APFloat Half(FltSem, "0.5");
2622 SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
2623 SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
2624 SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
2625
2626 SDValue ComputedExp =
2627 DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
2628
2629 SDValue Result0 =
2630 DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
2631
2632 SDValue Result1 =
2633 DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
2634
2635 return DAG.getMergeValues({Result0, Result1}, dl);
2636}
2637
2638/// This function is responsible for legalizing a
2639/// INT_TO_FP operation of the specified operand when the target requests that
2640/// we expand it. At this point, we know that the result and operand types are
2641/// legal for the target.
2642SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2643 SDValue &Chain) {
2644 bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2645 Node->getOpcode() == ISD::SINT_TO_FP);
2646 EVT DestVT = Node->getValueType(0);
2647 SDLoc dl(Node);
2648 unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2649 SDValue Op0 = Node->getOperand(OpNo);
2650 EVT SrcVT = Op0.getValueType();
2651
2652 // TODO: Should any fast-math-flags be set for the created nodes?
2653 LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2654 if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2655 (DestVT.bitsLE(MVT::f64) ||
2656 TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2658 DestVT))) {
2659 LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
2660 "expansion\n");
2661
2662 // Get the stack frame index of a 8 byte buffer.
2663 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
2664
2665 SDValue Lo = Op0;
2666 // if signed map to unsigned space
2667 if (isSigned) {
2668 // Invert sign bit (signed to unsigned mapping).
2669 Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
2670 DAG.getConstant(0x80000000u, dl, MVT::i32));
2671 }
2672 // Initial hi portion of constructed double.
2673 SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
2674
2675 // If this a big endian target, swap the lo and high data.
2676 if (DAG.getDataLayout().isBigEndian())
2677 std::swap(Lo, Hi);
2678
2679 SDValue MemChain = DAG.getEntryNode();
2680
2681 // Store the lo of the constructed double.
2682 SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
2684 // Store the hi of the constructed double.
2685 SDValue HiPtr =
2686 DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl);
2687 SDValue Store2 =
2688 DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
2689 MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
2690
2691 // load the constructed double
2692 SDValue Load =
2693 DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
2694 // FP constant to bias correct the final result
2695 SDValue Bias = DAG.getConstantFP(
2696 isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
2697 : llvm::bit_cast<double>(0x4330000000000000ULL),
2698 dl, MVT::f64);
2699 // Subtract the bias and get the final result.
2700 SDValue Sub;
2702 if (Node->isStrictFPOpcode()) {
2703 Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2704 {Node->getOperand(0), Load, Bias});
2705 Chain = Sub.getValue(1);
2706 if (DestVT != Sub.getValueType()) {
2707 std::pair<SDValue, SDValue> ResultPair;
2708 ResultPair =
2709 DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2710 Result = ResultPair.first;
2711 Chain = ResultPair.second;
2712 }
2713 else
2714 Result = Sub;
2715 } else {
2716 Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2717 Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2718 }
2719 return Result;
2720 }
2721
2722 if (isSigned)
2723 return SDValue();
2724
2725 // TODO: Generalize this for use with other types.
2726 if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2727 (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2728 LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
2729 // For unsigned conversions, convert them to signed conversions using the
2730 // algorithm from the x86_64 __floatundisf in compiler_rt. That method
2731 // should be valid for i32->f32 as well.
2732
2733 // More generally this transform should be valid if there are 3 more bits
2734 // in the integer type than the significand. Rounding uses the first bit
2735 // after the width of the significand and the OR of all bits after that. So
2736 // we need to be able to OR the shifted out bit into one of the bits that
2737 // participate in the OR.
2738
2739 // TODO: This really should be implemented using a branch rather than a
2740 // select. We happen to get lucky and machinesink does the right
2741 // thing most of the time. This would be a good candidate for a
2742 // pseudo-op, or, even better, for whole-function isel.
2743 EVT SetCCVT = getSetCCResultType(SrcVT);
2744
2745 SDValue SignBitTest = DAG.getSetCC(
2746 dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2747
2748 EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout());
2749 SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT);
2750 SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
2751 SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
2752 SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
2753 SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
2754
2755 SDValue Slow, Fast;
2756 if (Node->isStrictFPOpcode()) {
2757 // In strict mode, we must avoid spurious exceptions, and therefore
2758 // must make sure to only emit a single STRICT_SINT_TO_FP.
2759 SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
2760 Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2761 { Node->getOperand(0), InCvt });
2762 Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2763 { Fast.getValue(1), Fast, Fast });
2764 Chain = Slow.getValue(1);
2765 // The STRICT_SINT_TO_FP inherits the exception mode from the
2766 // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
2767 // never raise any exception.
2769 Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
2770 Fast->setFlags(Flags);
2771 Flags.setNoFPExcept(true);
2772 Slow->setFlags(Flags);
2773 } else {
2774 SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
2775 Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
2776 Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2777 }
2778
2779 return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
2780 }
2781
2782 // Don't expand it if there isn't cheap fadd.
2783 if (!TLI.isOperationLegalOrCustom(
2784 Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2785 return SDValue();
2786
2787 // The following optimization is valid only if every value in SrcVT (when
2788 // treated as signed) is representable in DestVT. Check that the mantissa
2789 // size of DestVT is >= than the number of bits in SrcVT -1.
2790 assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >=
2791 SrcVT.getSizeInBits() - 1 &&
2792 "Cannot perform lossless SINT_TO_FP!");
2793
2794 SDValue Tmp1;
2795 if (Node->isStrictFPOpcode()) {
2796 Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2797 { Node->getOperand(0), Op0 });
2798 } else
2799 Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
2800
2801 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
2802 DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
2803 SDValue Zero = DAG.getIntPtrConstant(0, dl),
2804 Four = DAG.getIntPtrConstant(4, dl);
2805 SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
2806 SignSet, Four, Zero);
2807
2808 // If the sign bit of the integer is set, the large number will be treated
2809 // as a negative number. To counteract this, the dynamic code adds an
2810 // offset depending on the data type.
2811 uint64_t FF;
2812 switch (SrcVT.getSimpleVT().SimpleTy) {
2813 default:
2814 return SDValue();
2815 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
2816 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
2817 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
2818 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
2819 }
2820 if (DAG.getDataLayout().isLittleEndian())
2821 FF <<= 32;
2822 Constant *FudgeFactor = ConstantInt::get(
2823 Type::getInt64Ty(*DAG.getContext()), FF);
2824
2825 SDValue CPIdx =
2826 DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
2827 Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
2828 CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
2829 Alignment = commonAlignment(Alignment, 4);
2830 SDValue FudgeInReg;
2831 if (DestVT == MVT::f32)
2832 FudgeInReg = DAG.getLoad(
2833 MVT::f32, dl, DAG.getEntryNode(), CPIdx,
2834 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
2835 Alignment);
2836 else {
2837 SDValue Load = DAG.getExtLoad(
2838 ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
2839 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
2840 Alignment);
2841 HandleSDNode Handle(Load);
2842 LegalizeOp(Load.getNode());
2843 FudgeInReg = Handle.getValue();
2844 }
2845
2846 if (Node->isStrictFPOpcode()) {
2847 SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2848 { Tmp1.getValue(1), Tmp1, FudgeInReg });
2849 Chain = Result.getValue(1);
2850 return Result;
2851 }
2852
2853 return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
2854}
2855
2856/// This function is responsible for legalizing a
2857/// *INT_TO_FP operation of the specified operand when the target requests that
2858/// we promote it. At this point, we know that the result and operand types are
2859/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
2860/// operation that takes a larger input.
2861void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
2863 bool IsStrict = N->isStrictFPOpcode();
2864 bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
2865 N->getOpcode() == ISD::STRICT_SINT_TO_FP;
2866 EVT DestVT = N->getValueType(0);
2867 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2868 unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
2869 unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
2870
2871 // First step, figure out the appropriate *INT_TO_FP operation to use.
2872 EVT NewInTy = LegalOp.getValueType();
2873
2874 unsigned OpToUse = 0;
2875
2876 // Scan for the appropriate larger type to use.
2877 while (true) {
2878 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
2879 assert(NewInTy.isInteger() && "Ran out of possibilities!");
2880
2881 // If the target supports SINT_TO_FP of this type, use it.
2882 if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
2883 OpToUse = SIntOp;
2884 break;
2885 }
2886 if (IsSigned)
2887 continue;
2888
2889 // If the target supports UINT_TO_FP of this type, use it.
2890 if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
2891 OpToUse = UIntOp;
2892 break;
2893 }
2894
2895 // Otherwise, try a larger type.
2896 }
2897
2898 // Okay, we found the operation and type to use. Zero extend our input to the
2899 // desired type then run the operation on it.
2900 if (IsStrict) {
2901 SDValue Res =
2902 DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
2903 {N->getOperand(0),
2904 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2905 dl, NewInTy, LegalOp)});
2906 Results.push_back(Res);
2907 Results.push_back(Res.getValue(1));
2908 return;
2909 }
2910
2911 Results.push_back(
2912 DAG.getNode(OpToUse, dl, DestVT,
2913 DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2914 dl, NewInTy, LegalOp)));
2915}
2916
2917/// This function is responsible for legalizing a
2918/// FP_TO_*INT operation of the specified operand when the target requests that
2919/// we promote it. At this point, we know that the result and operand types are
2920/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
2921/// operation that returns a larger result.
2922void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
2924 bool IsStrict = N->isStrictFPOpcode();
2925 bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
2926 N->getOpcode() == ISD::STRICT_FP_TO_SINT;
2927 EVT DestVT = N->getValueType(0);
2928 SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2929 // First step, figure out the appropriate FP_TO*INT operation to use.
2930 EVT NewOutTy = DestVT;
2931
2932 unsigned OpToUse = 0;
2933
2934 // Scan for the appropriate larger type to use.
2935 while (true) {
2936 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
2937 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2938
2939 // A larger signed type can hold all unsigned values of the requested type,
2940 // so using FP_TO_SINT is valid
2941 OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
2942 if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
2943 break;
2944
2945 // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2946 OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
2947 if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
2948 break;
2949
2950 // Otherwise, try a larger type.
2951 }
2952
2953 // Okay, we found the operation and type to use.
2955 if (IsStrict) {
2956 SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
2957 Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
2958 } else
2959 Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
2960
2961 // Truncate the result of the extended FP_TO_*INT operation to the desired
2962 // size.
2963 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2964 Results.push_back(Trunc);
2965 if (IsStrict)
2966 Results.push_back(Operation.getValue(1));
2967}
2968
2969/// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
2970/// the result and operand types are legal and there must be a legal
2971/// FP_TO_*INT_SAT operation for a larger result type.
2972SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
2973 const SDLoc &dl) {
2974 unsigned Opcode = Node->getOpcode();
2975
2976 // Scan for the appropriate larger type to use.
2977 EVT NewOutTy = Node->getValueType(0);
2978 while (true) {
2979 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
2980 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2981
2982 if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
2983 break;
2984 }
2985
2986 // Saturation width is determined by second operand, so we don't have to
2987 // perform any fixup and can directly truncate the result.
2988 SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
2989 Node->getOperand(1));
2990 return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2991}
2992
2993/// Open code the operations for PARITY of the specified operation.
2994SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
2995 EVT VT = Op.getValueType();
2996 EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2997 unsigned Sz = VT.getScalarSizeInBits();
2998
2999 // If CTPOP is legal, use it. Otherwise use shifts and xor.
3001 if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) {
3002 Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
3003 } else {
3004 Result = Op;
3005 for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
3006 SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
3007 DAG.getConstant(1ULL << (--i), dl, ShVT));
3008 Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
3009 }
3010 }
3011
3012 return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
3013}
3014
3015SDValue SelectionDAGLegalize::PromoteReduction(SDNode *Node) {
3016 MVT VecVT = Node->getOperand(1).getSimpleValueType();
3017 MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
3018 MVT ScalarVT = Node->getSimpleValueType(0);
3019 MVT NewScalarVT = NewVecVT.getVectorElementType();
3020
3021 SDLoc DL(Node);
3022 SmallVector<SDValue, 4> Operands(Node->getNumOperands());
3023
3024 // promote the initial value.
3025 // FIXME: Support integer.
3026 assert(Node->getOperand(0).getValueType().isFloatingPoint() &&
3027 "Only FP promotion is supported");
3028 Operands[0] =
3029 DAG.getNode(ISD::FP_EXTEND, DL, NewScalarVT, Node->getOperand(0));
3030
3031 for (unsigned j = 1; j != Node->getNumOperands(); ++j)
3032 if (Node->getOperand(j).getValueType().isVector() &&
3033 !(ISD::isVPOpcode(Node->getOpcode()) &&
3034 ISD::getVPMaskIdx(Node->getOpcode()) == j)) { // Skip mask operand.
3035 // promote the vector operand.
3036 // FIXME: Support integer.
3037 assert(Node->getOperand(j).getValueType().isFloatingPoint() &&
3038 "Only FP promotion is supported");
3039 Operands[j] =
3040 DAG.getNode(ISD::FP_EXTEND, DL, NewVecVT, Node->getOperand(j));
3041 } else {
3042 Operands[j] = Node->getOperand(j); // Skip VL operand.
3043 }
3044
3045 SDValue Res = DAG.getNode(Node->getOpcode(), DL, NewScalarVT, Operands,
3046 Node->getFlags());
3047
3048 assert(ScalarVT.isFloatingPoint() && "Only FP promotion is supported");
3049 return DAG.getNode(ISD::FP_ROUND, DL, ScalarVT, Res,
3050 DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
3051}
3052
3053bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
3054 LLVM_DEBUG(dbgs() << "Trying to expand node\n");
3056 SDLoc dl(Node);
3057 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
3058 bool NeedInvert;
3059 switch (Node->getOpcode()) {
3060 case ISD::ABS:
3061 if ((Tmp1 = TLI.expandABS(Node, DAG)))
3062 Results.push_back(Tmp1);
3063 break;
3064 case ISD::ABDS:
3065 case ISD::ABDU:
3066 if ((Tmp1 = TLI.expandABD(Node, DAG)))
3067 Results.push_back(Tmp1);
3068 break;
3069 case ISD::AVGCEILS:
3070 case ISD::AVGCEILU:
3071 case ISD::AVGFLOORS:
3072 case ISD::AVGFLOORU:
3073 if ((Tmp1 = TLI.expandAVG(Node, DAG)))
3074 Results.push_back(Tmp1);
3075 break;
3076 case ISD::CTPOP:
3077 if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
3078 Results.push_back(Tmp1);
3079 break;
3080 case ISD::CTLZ:
3082 if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
3083 Results.push_back(Tmp1);
3084 break;
3085 case ISD::CTTZ:
3087 if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
3088 Results.push_back(Tmp1);
3089 break;
3090 case ISD::BITREVERSE:
3091 if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
3092 Results.push_back(Tmp1);
3093 break;
3094 case ISD::BSWAP:
3095 if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
3096 Results.push_back(Tmp1);
3097 break;
3098 case ISD::PARITY:
3099 Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
3100 break;
3101 case ISD::FRAMEADDR:
3102 case ISD::RETURNADDR:
3104 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3105 break;
3106 case ISD::EH_DWARF_CFA: {
3107 SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
3108 TLI.getPointerTy(DAG.getDataLayout()));
3109 SDValue Offset = DAG.getNode(ISD::ADD, dl,
3110 CfaArg.getValueType(),
3111 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
3112 CfaArg.getValueType()),
3113 CfaArg);
3114 SDValue FA = DAG.getNode(
3115 ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
3116 DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
3117 Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
3118 FA, Offset));
3119 break;
3120 }
3121 case ISD::GET_ROUNDING:
3122 Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
3123 Results.push_back(Node->getOperand(0));
3124 break;
3125 case ISD::EH_RETURN:
3126 case ISD::EH_LABEL:
3127 case ISD::PREFETCH:
3128 case ISD::VAEND:
3130 // If the target didn't expand these, there's nothing to do, so just
3131 // preserve the chain and be done.
3132 Results.push_back(Node->getOperand(0));
3133 break;
3136 // If the target didn't expand this, just return 'zero' and preserve the
3137 // chain.
3138 Results.append(Node->getNumValues() - 1,
3139 DAG.getConstant(0, dl, Node->getValueType(0)));
3140 Results.push_back(Node->getOperand(0));
3141 break;
3143 // If the target didn't expand this, just return 'zero' and preserve the
3144 // chain.
3145 Results.push_back(DAG.getConstant(0, dl, MVT::i32));
3146 Results.push_back(Node->getOperand(0));
3147 break;
3148 case ISD::ATOMIC_LOAD: {
3149 // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
3150 SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
3151 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3152 SDValue Swap = DAG.getAtomicCmpSwap(
3153 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3154 Node->getOperand(0), Node->getOperand(1), Zero, Zero,
3155 cast<AtomicSDNode>(Node)->getMemOperand());
3156 Results.push_back(Swap.getValue(0));
3157 Results.push_back(Swap.getValue(1));
3158 break;
3159 }
3160 case ISD::ATOMIC_STORE: {
3161 // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3162 SDValue Swap = DAG.getAtomic(
3163 ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(),
3164 Node->getOperand(0), Node->getOperand(2), Node->getOperand(1),
3165 cast<AtomicSDNode>(Node)->getMemOperand());
3166 Results.push_back(Swap.getValue(1));
3167 break;
3168 }
3170 // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
3171 // splits out the success value as a comparison. Expanding the resulting
3172 // ATOMIC_CMP_SWAP will produce a libcall.
3173 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
3174 SDValue Res = DAG.getAtomicCmpSwap(
3175 ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
3176 Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
3177 Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
3178
3179 SDValue ExtRes = Res;
3180 SDValue LHS = Res;
3181 SDValue RHS = Node->getOperand(1);
3182
3183 EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
3184 EVT OuterType = Node->getValueType(0);
3185 switch (TLI.getExtendForAtomicOps()) {
3186 case ISD::SIGN_EXTEND:
3187 LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
3188 DAG.getValueType(AtomicType));
3189 RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
3190 Node->getOperand(2), DAG.getValueType(AtomicType));
3191 ExtRes = LHS;
3192 break;
3193 case ISD::ZERO_EXTEND:
3194 LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
3195 DAG.getValueType(AtomicType));
3196 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3197 ExtRes = LHS;
3198 break;
3199 case ISD::ANY_EXTEND:
3200 LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
3201 RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
3202 break;
3203 default:
3204 llvm_unreachable("Invalid atomic op extension");
3205 }
3206
3208 DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
3209
3210 Results.push_back(ExtRes.getValue(0));
3211 Results.push_back(Success);
3212 Results.push_back(Res.getValue(1));
3213 break;
3214 }
3215 case ISD::ATOMIC_LOAD_SUB: {
3216 SDLoc DL(Node);
3217 EVT VT = Node->getValueType(0);
3218 SDValue RHS = Node->getOperand(2);
3219 AtomicSDNode *AN = cast<AtomicSDNode>(Node);
3220 if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG &&
3221 cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT())
3222 RHS = RHS->getOperand(0);
3223 SDValue NewRHS =
3224 DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS);
3225 SDValue Res = DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, AN->getMemoryVT(),
3226 Node->getOperand(0), Node->getOperand(1),
3227 NewRHS, AN->getMemOperand());
3228 Results.push_back(Res);
3229 Results.push_back(Res.getValue(1));
3230 break;
3231 }
3233 ExpandDYNAMIC_STACKALLOC(Node, Results);
3234 break;
3235 case ISD::MERGE_VALUES:
3236 for (unsigned i = 0; i < Node->getNumValues(); i++)
3237 Results.push_back(Node->getOperand(i));
3238 break;
3239 case ISD::UNDEF: {
3240 EVT VT = Node->getValueType(0);
3241 if (VT.isInteger())
3242 Results.push_back(DAG.getConstant(0, dl, VT));
3243 else {
3244 assert(VT.isFloatingPoint() && "Unknown value type!");
3245 Results.push_back(DAG.getConstantFP(0, dl, VT));
3246 }
3247 break;
3248 }
3250 // When strict mode is enforced we can't do expansion because it
3251 // does not honor the "strict" properties. Only libcall is allowed.
3252 if (TLI.isStrictFPEnabled())
3253 break;
3254 // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3255 // since this operation is more efficient than stack operation.
3256 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3257 Node->getValueType(0))
3258 == TargetLowering::Legal)
3259 break;
3260 // We fall back to use stack operation when the FP_ROUND operation
3261 // isn't available.
3262 if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3263 Node->getValueType(0), dl,
3264 Node->getOperand(0)))) {
3265 ReplaceNode(Node, Tmp1.getNode());
3266 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
3267 return true;
3268 }
3269 break;
3270 case ISD::FP_ROUND: {
3271 if ((Tmp1 = TLI.expandFP_ROUND(Node, DAG))) {
3272 Results.push_back(Tmp1);
3273 break;
3274 }
3275
3276 [[fallthrough]];
3277 }
3278 case ISD::BITCAST:
3279 if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3280 Node->getValueType(0), dl)))
3281 Results.push_back(Tmp1);
3282 break;
3284 // When strict mode is enforced we can't do expansion because it
3285 // does not honor the "strict" properties. Only libcall is allowed.
3286 if (TLI.isStrictFPEnabled())
3287 break;
3288 // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3289 // since this operation is more efficient than stack operation.
3290 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
3291 Node->getValueType(0))
3292 == TargetLowering::Legal)
3293 break;
3294 // We fall back to use stack operation when the FP_EXTEND operation
3295 // isn't available.
3296 if ((Tmp1 = EmitStackConvert(
3297 Node->getOperand(1), Node->getOperand(1).getValueType(),
3298 Node->getValueType(0), dl, Node->getOperand(0)))) {
3299 ReplaceNode(Node, Tmp1.getNode());
3300 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
3301 return true;
3302 }
3303 break;
3304 case ISD::FP_EXTEND: {
3305 SDValue Op = Node->getOperand(0);
3306 EVT SrcVT = Op.getValueType();
3307 EVT DstVT = Node->getValueType(0);
3308 if (SrcVT.getScalarType() == MVT::bf16) {
3309 Results.push_back(DAG.getNode(ISD::BF16_TO_FP, SDLoc(Node), DstVT, Op));
3310 break;
3311 }
3312
3313 if ((Tmp1 = EmitStackConvert(Op, SrcVT, DstVT, dl)))
3314 Results.push_back(Tmp1);
3315 break;
3316 }
3317 case ISD::BF16_TO_FP: {
3318 // Always expand bf16 to f32 casts, they lower to ext + shift.
3319 //
3320 // Note that the operand of this code can be bf16 or an integer type in case
3321 // bf16 is not supported on the target and was softened.
3322 SDValue Op = Node->getOperand(0);
3323 if (Op.getValueType() == MVT::bf16) {
3324 Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3325 DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3326 } else {
3327 Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3328 }
3329 Op = DAG.getNode(
3330 ISD::SHL, dl, MVT::i32, Op,
3331 DAG.getConstant(16, dl,
3332 TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3333 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3334 // Add fp_extend in case the output is bigger than f32.
3335 if (Node->getValueType(0) != MVT::f32)
3336 Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3337 Results.push_back(Op);
3338 break;
3339 }
3340 case ISD::FP_TO_BF16: {
3341 SDValue Op = Node->getOperand(0);
3342 if (Op.getValueType() != MVT::f32)
3343 Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3344 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3345 // Certain SNaNs will turn into infinities if we do a simple shift right.
3346 if (!DAG.isKnownNeverSNaN(Op)) {
3347 Op = DAG.getNode(ISD::FCANONICALIZE, dl, MVT::f32, Op, Node->getFlags());
3348 }
3349 Op = DAG.getNode(
3350 ISD::SRL, dl, MVT::i32, DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3351 DAG.getConstant(16, dl,
3352 TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3353 // The result of this node can be bf16 or an integer type in case bf16 is
3354 // not supported on the target and was softened to i16 for storage.
3355 if (Node->getValueType(0) == MVT::bf16) {
3356 Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3357 DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3358 } else {
3359 Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3360 }
3361 Results.push_back(Op);
3362 break;
3363 }
3365 EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
3366 EVT VT = Node->getValueType(0);
3367
3368 // An in-register sign-extend of a boolean is a negation:
3369 // 'true' (1) sign-extended is -1.
3370 // 'false' (0) sign-extended is 0.
3371 // However, we must mask the high bits of the source operand because the
3372 // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
3373
3374 // TODO: Do this for vectors too?
3375 if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
3376 SDValue One = DAG.getConstant(1, dl, VT);
3377 SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
3378 SDValue Zero = DAG.getConstant(0, dl, VT);
3379 SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
3380 Results.push_back(Neg);
3381 break;
3382 }
3383
3384 // NOTE: we could fall back on load/store here too for targets without
3385 // SRA. However, it is doubtful that any exist.
3386 EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3387 unsigned BitsDiff = VT.getScalarSizeInBits() -
3388 ExtraVT.getScalarSizeInBits();
3389 SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
3390 Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
3391 Node->getOperand(0), ShiftCst);
3392 Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
3393 Results.push_back(Tmp1);
3394 break;
3395 }
3396 case ISD::UINT_TO_FP:
3398 if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
3399 Results.push_back(Tmp1);
3400 if (Node->isStrictFPOpcode())
3401 Results.push_back(Tmp2);
3402 break;
3403 }
3404 [[fallthrough]];
3405 case ISD::SINT_TO_FP:
3407 if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
3408 Results.push_back(Tmp1);
3409 if (Node->isStrictFPOpcode())
3410 Results.push_back(Tmp2);
3411 }
3412 break;
3413 case ISD::FP_TO_SINT:
3414 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
3415 Results.push_back(Tmp1);
3416 break;
3418 if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
3419 ReplaceNode(Node, Tmp1.getNode());
3420 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
3421 return true;
3422 }
3423 break;
3424 case ISD::FP_TO_UINT:
3425 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
3426 Results.push_back(Tmp1);
3427 break;
3429 if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
3430 // Relink the chain.
3431 DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
3432 // Replace the new UINT result.
3433 ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
3434 LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
3435 return true;
3436 }
3437 break;
3440 Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3441 break;
3442 case ISD::VAARG:
3443 Results.push_back(DAG.expandVAArg(Node));
3444 Results.push_back(Results[0].getValue(1));
3445 break;
3446 case ISD::VACOPY:
3447 Results.push_back(DAG.expandVACopy(Node));
3448 break;
3450 if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar())
3451 // This must be an access of the only element. Return it.
3452 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
3453 Node->getOperand(0));
3454 else
3455 Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
3456 Results.push_back(Tmp1);
3457 break;
3459 Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
3460 break;
3462 Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
3463 break;
3465 Results.push_back(ExpandVectorBuildThroughStack(Node));
3466 break;
3468 Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
3469 break;
3471 Results.push_back(ExpandINSERT_VECTOR_ELT(SDValue(Node, 0)));
3472 break;
3473 case ISD::VECTOR_SHUFFLE: {
3474 SmallVector<int, 32> NewMask;
3475 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
3476
3477 EVT VT = Node->getValueType(0);
3478 EVT EltVT = VT.getVectorElementType();
3479 SDValue Op0 = Node->getOperand(0);
3480 SDValue Op1 = Node->getOperand(1);
3481 if (!TLI.isTypeLegal(EltVT)) {
3482 EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
3483
3484 // BUILD_VECTOR operands are allowed to be wider than the element type.
3485 // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
3486 // it.
3487 if (NewEltVT.bitsLT(EltVT)) {
3488 // Convert shuffle node.
3489 // If original node was v4i64 and the new EltVT is i32,
3490 // cast operands to v8i32 and re-build the mask.
3491
3492 // Calculate new VT, the size of the new VT should be equal to original.
3493 EVT NewVT =
3494 EVT::getVectorVT(*DAG.getContext(), NewEltVT,
3495 VT.getSizeInBits() / NewEltVT.getSizeInBits());
3496 assert(NewVT.bitsEq(VT));
3497
3498 // cast operands to new VT
3499 Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
3500 Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
3501
3502 // Convert the shuffle mask
3503 unsigned int factor =
3505
3506 // EltVT gets smaller
3507 assert(factor > 0);
3508
3509 for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
3510 if (Mask[i] < 0) {
3511 for (unsigned fi = 0; fi < factor; ++fi)
3512 NewMask.push_back(Mask[i]);
3513 }
3514 else {
3515 for (unsigned fi = 0; fi < factor; ++fi)
3516 NewMask.push_back(Mask[i]*factor+fi);
3517 }
3518 }
3519 Mask = NewMask;
3520 VT = NewVT;
3521 }
3522 EltVT = NewEltVT;
3523 }
3524 unsigned NumElems = VT.getVectorNumElements();
3526 for (unsigned i = 0; i != NumElems; ++i) {
3527 if (Mask[i] < 0) {
3528 Ops.push_back(DAG.getUNDEF(EltVT));
3529 continue;
3530 }
3531 unsigned Idx = Mask[i];
3532 if (Idx < NumElems)
3533 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
3534 DAG.getVectorIdxConstant(Idx, dl)));
3535 else
3536 Ops.push_back(
3537 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
3538 DAG.getVectorIdxConstant(Idx - NumElems, dl)));
3539 }
3540
3541 Tmp1 = DAG.getBuildVector(VT, dl, Ops);
3542 // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
3543 Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
3544 Results.push_back(Tmp1);
3545 break;
3546 }
3547 case ISD::VECTOR_SPLICE: {
3548 Results.push_back(TLI.expandVectorSplice(Node, DAG));
3549 break;
3550 }
3551 case ISD::EXTRACT_ELEMENT: {
3552 EVT OpTy = Node->getOperand(0).getValueType();
3553 if (Node->getConstantOperandVal(1)) {
3554 // 1 -> Hi
3555 Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
3556 DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
3557 TLI.getShiftAmountTy(
3558 Node->getOperand(0).getValueType(),
3559 DAG.getDataLayout())));
3560 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
3561 } else {
3562 // 0 -> Lo
3563 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
3564 Node->getOperand(0));
3565 }
3566 Results.push_back(Tmp1);
3567 break;
3568 }
3569 case ISD::STACKSAVE:
3570 // Expand to CopyFromReg if the target set
3571 // StackPointerRegisterToSaveRestore.
3572 if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
3573 Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
3574 Node->getValueType(0)));
3575 Results.push_back(Results[0].getValue(1));
3576 } else {
3577 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
3578 Results.push_back(Node->getOperand(0));
3579 }
3580 break;
3581 case ISD::STACKRESTORE:
3582 // Expand to CopyToReg if the target set
3583 // StackPointerRegisterToSaveRestore.
3584 if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
3585 Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
3586 Node->getOperand(1)));
3587 } else {
3588 Results.push_back(Node->getOperand(0));
3589 }
3590 break;
3592 Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
3593 Results.push_back(Results[0].getValue(0));
3594 break;
3595 case ISD::FCOPYSIGN:
3596 Results.push_back(ExpandFCOPYSIGN(Node));
3597 break;
3598 case ISD::FNEG:
3599 Results.push_back(ExpandFNEG(Node));
3600 break;
3601 case ISD::FABS:
3602 Results.push_back(ExpandFABS(Node));
3603 break;
3604 case ISD::IS_FPCLASS: {
3605 auto Test = static_cast<FPClassTest>(Node->getConstantOperandVal(1));
3606 if (SDValue Expanded =
3607 TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
3608 Test, Node->getFlags(), SDLoc(Node), DAG))
3609 Results.push_back(Expanded);
3610 break;
3611 }
3612 case ISD::SMIN:
3613 case ISD::SMAX:
3614 case ISD::UMIN:
3615 case ISD::UMAX: {
3616 // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
3617 ISD::CondCode Pred;
3618 switch (Node->getOpcode()) {
3619 default: llvm_unreachable("How did we get here?");
3620 case ISD::SMAX: Pred = ISD::SETGT; break;
3621 case ISD::SMIN: Pred = ISD::SETLT; break;
3622 case ISD::UMAX: Pred = ISD::SETUGT; break;
3623 case ISD::UMIN: Pred = ISD::SETULT; break;
3624 }
3625 Tmp1 = Node->getOperand(0);
3626 Tmp2 = Node->getOperand(1);
3627 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
3628 Results.push_back(Tmp1);
3629 break;
3630 }
3631 case ISD::FMINNUM:
3632 case ISD::FMAXNUM: {
3633 if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
3634 Results.push_back(Expanded);
3635 break;
3636 }
3637 case ISD::FMINIMUM:
3638 case ISD::FMAXIMUM: {
3639 if (SDValue Expanded = TLI.expandFMINIMUM_FMAXIMUM(Node, DAG))
3640 Results.push_back(Expanded);
3641 break;
3642 }
3643 case ISD::FSIN:
3644 case ISD::FCOS: {
3645 EVT VT = Node->getValueType(0);
3646 // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
3647 // fcos which share the same operand and both are used.
3648 if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
3650 && useSinCos(Node)) {
3651 SDVTList VTs = DAG.getVTList(VT, VT);
3652 Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
3653 if (Node->getOpcode() == ISD::FCOS)
3654 Tmp1 = Tmp1.getValue(1);
3655 Results.push_back(Tmp1);
3656 }
3657 break;
3658 }
3659 case ISD::FLDEXP:
3660 case ISD::STRICT_FLDEXP: {
3661 EVT VT = Node->getValueType(0);
3663 // Use the LibCall instead, it is very likely faster
3664 // FIXME: Use separate LibCall action.
3665 if (TLI.getLibcallName(LC))
3666 break;
3667
3668 if (SDValue Expanded = expandLdexp(Node)) {
3669 Results.push_back(Expanded);
3670 if (Node->getOpcode() == ISD::STRICT_FLDEXP)
3671 Results.push_back(Expanded.getValue(1));
3672 }
3673
3674 break;
3675 }
3676 case ISD::FFREXP: {
3677 RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
3678 // Use the LibCall instead, it is very likely faster
3679 // FIXME: Use separate LibCall action.
3680 if (TLI.getLibcallName(LC))
3681 break;
3682
3683 if (SDValue Expanded = expandFrexp(Node)) {
3684 Results.push_back(Expanded);
3685 Results.push_back(Expanded.getValue(1));
3686 }
3687 break;
3688 }
3689 case ISD::FMAD:
3690 llvm_unreachable("Illegal fmad should never be formed");
3691
3692 case ISD::FP16_TO_FP:
3693 if (Node->getValueType(0) != MVT::f32) {
3694 // We can extend to types bigger than f32 in two steps without changing
3695 // the result. Since "f16 -> f32" is much more commonly available, give
3696 // CodeGen the option of emitting that before resorting to a libcall.
3697 SDValue Res =
3698 DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
3699 Results.push_back(
3700 DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
3701 }
3702 break;
3705 if (Node->getValueType(0) != MVT::f32) {
3706 // We can extend to types bigger than f32 in two steps without changing
3707 // the result. Since "f16 -> f32" is much more commonly available, give
3708 // CodeGen the option of emitting that before resorting to a libcall.
3709 SDValue Res = DAG.getNode(Node->getOpcode(), dl, {MVT::f32, MVT::Other},
3710 {Node->getOperand(0), Node->getOperand(1)});
3711 Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
3712 {Node->getValueType(0), MVT::Other},
3713 {Res.getValue(1), Res});
3714 Results.push_back(Res);
3715 Results.push_back(Res.getValue(1));
3716 }
3717 break;
3718 case ISD::FP_TO_FP16:
3719 LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
3720 if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
3721 SDValue Op = Node->getOperand(0);
3722 MVT SVT = Op.getSimpleValueType();
3723 if ((SVT == MVT::f64 || SVT == MVT::f80) &&
3724 TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
3725 // Under fastmath, we can expand this node into a fround followed by
3726 // a float-half conversion.
3727 SDValue FloatVal =
3728 DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3729 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3730 Results.push_back(
3731 DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
3732 }
3733 }
3734 break;
3735 case ISD::ConstantFP: {
3736 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
3737 // Check to see if this FP immediate is already legal.
3738 // If this is a legal constant, turn it into a TargetConstantFP node.
3739 if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
3740 DAG.shouldOptForSize()))
3741 Results.push_back(ExpandConstantFP(CFP, true));
3742 break;
3743 }
3744 case ISD::Constant: {
3745 ConstantSDNode *CP = cast<ConstantSDNode>(Node);
3746 Results.push_back(ExpandConstant(CP));
3747 break;
3748 }
3749 case ISD::FSUB: {
3750 EVT VT = Node->getValueType(0);
3751 if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
3752 TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
3753 const SDNodeFlags Flags = Node->getFlags();
3754 Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
3755 Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
3756 Results.push_back(Tmp1);
3757 }
3758 break;
3759 }
3760 case ISD::SUB: {
3761 EVT VT = Node->getValueType(0);
3762 assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
3763 TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
3764 "Don't know how to expand this subtraction!");
3765 Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
3766 Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
3767 Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
3768 break;
3769 }
3770 case ISD::UREM:
3771 case ISD::SREM:
3772 if (TLI.expandREM(Node, Tmp1, DAG))
3773 Results.push_back(Tmp1);
3774 break;
3775 case ISD::UDIV:
3776 case ISD::SDIV: {
3777 bool isSigned = Node->getOpcode() == ISD::SDIV;
3778 unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
3779 EVT VT = Node->getValueType(0);
3780 if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
3781 SDVTList VTs = DAG.getVTList(VT, VT);
3782 Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
3783 Node->getOperand(1));
3784 Results.push_back(Tmp1);
3785 }
3786 break;
3787 }
3788 case ISD::MULHU:
3789 case ISD::MULHS: {
3790 unsigned ExpandOpcode =
3791 Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
3792 EVT VT = Node->getValueType(0);
3793 SDVTList VTs = DAG.getVTList(VT, VT);
3794
3795 Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
3796 Node->getOperand(1));
3797 Results.push_back(Tmp1.getValue(1));
3798 break;
3799 }
3800 case ISD::UMUL_LOHI:
3801 case ISD::SMUL_LOHI: {
3802 SDValue LHS = Node->getOperand(0);
3803 SDValue RHS = Node->getOperand(1);
3804 MVT VT = LHS.getSimpleValueType();
3805 unsigned MULHOpcode =
3806 Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
3807
3808 if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
3809 Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
3810 Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
3811 break;
3812 }
3813
3815 EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext());
3816 assert(TLI.isTypeLegal(HalfType));
3817 if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
3818 HalfType, DAG,
3819 TargetLowering::MulExpansionKind::Always)) {
3820 for (unsigned i = 0; i < 2; ++i) {
3821 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
3822 SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
3823 SDValue Shift = DAG.getConstant(
3824 HalfType.getScalarSizeInBits(), dl,
3825 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
3826 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3827 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3828 }
3829 break;
3830 }
3831 break;
3832 }
3833 case ISD::MUL: {
3834 EVT VT = Node->getValueType(0);
3835 SDVTList VTs = DAG.getVTList(VT, VT);
3836 // See if multiply or divide can be lowered using two-result operations.
3837 // We just need the low half of the multiply; try both the signed
3838 // and unsigned forms. If the target supports both SMUL_LOHI and
3839 // UMUL_LOHI, form a preference by checking which forms of plain
3840 // MULH it supports.
3841 bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
3842 bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
3843 bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
3844 bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
3845 unsigned OpToUse = 0;
3846 if (HasSMUL_LOHI && !HasMULHS) {
3847 OpToUse = ISD::SMUL_LOHI;
3848 } else if (HasUMUL_LOHI && !HasMULHU) {
3849 OpToUse = ISD::UMUL_LOHI;
3850 } else if (HasSMUL_LOHI) {
3851 OpToUse = ISD::SMUL_LOHI;
3852 } else if (HasUMUL_LOHI) {
3853 OpToUse = ISD::UMUL_LOHI;
3854 }
3855 if (OpToUse) {
3856 Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
3857 Node->getOperand(1)));
3858 break;
3859 }
3860
3861 SDValue Lo, Hi;
3862 EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
3863 if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
3864 TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
3865 TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
3866 TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
3867 TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
3868 TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
3869 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
3870 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
3871 SDValue Shift =
3872 DAG.getConstant(HalfType.getSizeInBits(), dl,
3873 TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
3874 Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
3875 Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
3876 }
3877 break;
3878 }
3879 case ISD::FSHL:
3880 case ISD::FSHR:
3881 if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
3882 Results.push_back(Expanded);
3883 break;
3884 case ISD::ROTL:
3885 case ISD::ROTR:
3886 if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
3887 Results.push_back(Expanded);
3888 break;
3889 case ISD::SADDSAT:
3890 case ISD::UADDSAT:
3891 case ISD::SSUBSAT:
3892 case ISD::USUBSAT:
3893 Results.push_back(TLI.expandAddSubSat(Node, DAG));
3894 break;
3895 case ISD::SCMP:
3896 case ISD::UCMP:
3897 Results.push_back(TLI.expandCMP(Node, DAG));
3898 break;
3899 case ISD::SSHLSAT:
3900 case ISD::USHLSAT:
3901 Results.push_back(TLI.expandShlSat(Node, DAG));
3902 break;
3903 case ISD::SMULFIX:
3904 case ISD::SMULFIXSAT:
3905 case ISD::UMULFIX:
3906 case ISD::UMULFIXSAT:
3907 Results.push_back(TLI.expandFixedPointMul(Node, DAG));
3908 break;
3909 case ISD::SDIVFIX:
3910 case ISD::SDIVFIXSAT:
3911 case ISD::UDIVFIX:
3912 case ISD::UDIVFIXSAT:
3913 if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
3914 Node->getOperand(0),
3915 Node->getOperand(1),
3916 Node->getConstantOperandVal(2),
3917 DAG)) {
3918 Results.push_back(V);
3919 break;
3920 }
3921 // FIXME: We might want to retry here with a wider type if we fail, if that
3922 // type is legal.
3923 // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
3924 // <= 128 (which is the case for all of the default Embedded-C types),
3925 // we will only get here with types and scales that we could always expand
3926 // if we were allowed to generate libcalls to division functions of illegal
3927 // type. But we cannot do that.
3928 llvm_unreachable("Cannot expand DIVFIX!");
3929 case ISD::UADDO_CARRY:
3930 case ISD::USUBO_CARRY: {
3931 SDValue LHS = Node->getOperand(0);
3932 SDValue RHS = Node->getOperand(1);
3933 SDValue Carry = Node->getOperand(2);
3934
3935 bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
3936
3937 // Initial add of the 2 operands.
3938 unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
3939 EVT VT = LHS.getValueType();
3940 SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
3941
3942 // Initial check for overflow.
3943 EVT CarryType = Node->getValueType(1);
3944 EVT SetCCType = getSetCCResultType(Node->getValueType(0));
3946 SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
3947
3948 // Add of the sum and the carry.
3949 SDValue One = DAG.getConstant(1, dl, VT);
3950 SDValue CarryExt =
3951 DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
3952 SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
3953
3954 // Second check for overflow. If we are adding, we can only overflow if the
3955 // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
3956 // If we are subtracting, we can only overflow if the initial sum is 0 and
3957 // the carry is set, resulting in a new sum of all 1s.
3958 SDValue Zero = DAG.getConstant(0, dl, VT);
3959 SDValue Overflow2 =
3960 IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
3961 : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
3962 Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
3963 DAG.getZExtOrTrunc(Carry, dl, SetCCType));
3964
3965 SDValue ResultCarry =
3966 DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
3967
3968 Results.push_back(Sum2);
3969 Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
3970 break;
3971 }
3972 case ISD::SADDO:
3973 case ISD::SSUBO: {
3974 SDValue Result, Overflow;
3975 TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
3976 Results.push_back(Result);
3977 Results.push_back(Overflow);
3978 break;
3979 }
3980 case ISD::UADDO:
3981 case ISD::USUBO: {
3982 SDValue Result, Overflow;
3983 TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
3984 Results.push_back(Result);
3985 Results.push_back(Overflow);
3986 break;
3987 }
3988 case ISD::UMULO:
3989 case ISD::SMULO: {
3990 SDValue Result, Overflow;
3991 if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
3992 Results.push_back(Result);
3993 Results.push_back(Overflow);
3994 }
3995 break;
3996 }
3997 case ISD::BUILD_PAIR: {
3998 EVT PairTy = Node->getValueType(0);
3999 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
4000 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
4001 Tmp2 = DAG.getNode(
4002 ISD::SHL, dl, PairTy, Tmp2,
4003 DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
4004 TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
4005 Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
4006 break;
4007 }
4008 case ISD::SELECT:
4009 Tmp1 = Node->getOperand(0);
4010 Tmp2 = Node->getOperand(1);
4011 Tmp3 = Node->getOperand(2);
4012 if (Tmp1.getOpcode() == ISD::SETCC) {
4013 Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
4014 Tmp2, Tmp3,
4015 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
4016 } else {
4017 Tmp1 = DAG.getSelectCC(dl, Tmp1,
4018 DAG.getConstant(0, dl, Tmp1.getValueType()),
4019 Tmp2, Tmp3, ISD::SETNE);
4020 }
4021 Tmp1->setFlags(Node->getFlags());
4022 Results.push_back(Tmp1);
4023 break;
4024 case ISD::BR_JT: {
4025 SDValue Chain = Node->getOperand(0);
4026 SDValue Table = Node->getOperand(1);
4027 SDValue Index = Node->getOperand(2);
4028 int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex();
4029
4030 const DataLayout &TD = DAG.getDataLayout();
4031 EVT PTy = TLI.getPointerTy(TD);
4032
4033 unsigned EntrySize =
4034 DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
4035
4036 // For power-of-two jumptable entry sizes convert multiplication to a shift.
4037 // This transformation needs to be done here since otherwise the MIPS
4038 // backend will end up emitting a three instruction multiply sequence
4039 // instead of a single shift and MSP430 will call a runtime function.
4040 if (llvm::isPowerOf2_32(EntrySize))
4041 Index = DAG.getNode(
4042 ISD::SHL, dl, Index.getValueType(), Index,
4043 DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
4044 else
4045 Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
4046 DAG.getConstant(EntrySize, dl, Index.getValueType()));
4047 SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
4048 Index, Table);
4049
4050 EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
4051 SDValue LD = DAG.getExtLoad(
4052 ISD::SEXTLOAD, dl, PTy, Chain, Addr,
4053 MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
4054 Addr = LD;
4055 if (TLI.isJumpTableRelative()) {
4056 // For PIC, the sequence is:
4057 // BRIND(load(Jumptable + index) + RelocBase)
4058 // RelocBase can be JumpTable, GOT or some sort of global base.
4059 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
4060 TLI.getPICJumpTableRelocBase(Table, DAG));
4061 }
4062
4063 Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG);
4064 Results.push_back(Tmp1);
4065 break;
4066 }
4067 case ISD::BRCOND:
4068 // Expand brcond's setcc into its constituent parts and create a BR_CC
4069 // Node.
4070 Tmp1 = Node->getOperand(0);
4071 Tmp2 = Node->getOperand(1);
4072 if (Tmp2.getOpcode() == ISD::SETCC &&
4073 TLI.isOperationLegalOrCustom(ISD::BR_CC,
4074 Tmp2.getOperand(0).getValueType())) {
4075 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
4076 Tmp2.getOperand(0), Tmp2.getOperand(1),
4077 Node->getOperand(2));
4078 } else {
4079 // We test only the i1 bit. Skip the AND if UNDEF or another AND.
4080 if (Tmp2.isUndef() ||
4081 (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
4082 Tmp3 = Tmp2;
4083 else
4084 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
4085 DAG.getConstant(1, dl, Tmp2.getValueType()));
4086 Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
4087 DAG.getCondCode(ISD::SETNE), Tmp3,
4088 DAG.getConstant(0, dl, Tmp3.getValueType()),
4089 Node->getOperand(2));
4090 }
4091 Results.push_back(Tmp1);
4092 break;
4093 case ISD::SETCC:
4094 case ISD::VP_SETCC:
4095 case ISD::STRICT_FSETCC:
4096 case ISD::STRICT_FSETCCS: {
4097 bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
4098 bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
4099 Node->getOpcode() == ISD::STRICT_FSETCCS;
4100 bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
4101 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4102 unsigned Offset = IsStrict ? 1 : 0;
4103 Tmp1 = Node->getOperand(0 + Offset);
4104 Tmp2 = Node->getOperand(1 + Offset);
4105 Tmp3 = Node->getOperand(2 + Offset);
4106 SDValue Mask, EVL;
4107 if (IsVP) {
4108 Mask = Node->getOperand(3 + Offset);
4109 EVL = Node->getOperand(4 + Offset);
4110 }
4111 bool Legalized = TLI.LegalizeSetCCCondCode(
4112 DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
4113 Chain, IsSignaling);
4114
4115 if (Legalized) {
4116 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4117 // condition code, create a new SETCC node.
4118 if (Tmp3.getNode()) {
4119 if (IsStrict) {
4120 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
4121 {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
4122 Chain = Tmp1.getValue(1);
4123 } else if (IsVP) {
4124 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
4125 {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
4126 } else {
4127 Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
4128 Tmp2, Tmp3, Node->getFlags());
4129 }
4130 }
4131
4132 // If we expanded the SETCC by inverting the condition code, then wrap
4133 // the existing SETCC in a NOT to restore the intended condition.
4134 if (NeedInvert) {
4135 if (!IsVP)
4136 Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
4137 else
4138 Tmp1 =
4139 DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
4140 }
4141
4142 Results.push_back(Tmp1);
4143 if (IsStrict)
4144 Results.push_back(Chain);
4145
4146 break;
4147 }
4148
4149 // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4150 // understand if this code is useful for strict nodes.
4151 assert(!IsStrict && "Don't know how to expand for strict nodes.");
4152
4153 // Otherwise, SETCC for the given comparison type must be completely
4154 // illegal; expand it into a SELECT_CC.
4155 // FIXME: This drops the mask/evl for VP_SETCC.
4156 EVT VT = Node->getValueType(0);
4157 EVT Tmp1VT = Tmp1.getValueType();
4158 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
4159 DAG.getBoolConstant(true, dl, VT, Tmp1VT),
4160 DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3);
4161 Tmp1->setFlags(Node->getFlags());
4162 Results.push_back(Tmp1);
4163 break;
4164 }
4165 case ISD::SELECT_CC: {
4166 // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
4167 Tmp1 = Node->getOperand(0); // LHS
4168 Tmp2 = Node->getOperand(1); // RHS
4169 Tmp3 = Node->getOperand(2); // True
4170 Tmp4 = Node->getOperand(3); // False
4171 EVT VT = Node->getValueType(0);
4172 SDValue Chain;
4173 SDValue CC = Node->getOperand(4);
4174 ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
4175
4176 if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
4177 // If the condition code is legal, then we need to expand this
4178 // node using SETCC and SELECT.
4179 EVT CmpVT = Tmp1.getValueType();
4180 assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
4181 "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
4182 "expanded.");
4183 EVT CCVT = getSetCCResultType(CmpVT);
4184 SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
4185 Results.push_back(
4186 DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4, Node->getFlags()));
4187 break;
4188 }
4189
4190 // SELECT_CC is legal, so the condition code must not be.
4191 bool Legalized = false;
4192 // Try to legalize by inverting the condition. This is for targets that
4193 // might support an ordered version of a condition, but not the unordered
4194 // version (or vice versa).
4195 ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
4196 if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
4197 // Use the new condition code and swap true and false
4198 Legalized = true;
4199 Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
4200 Tmp1->setFlags(Node->getFlags());
4201 } else {
4202 // If The inverse is not legal, then try to swap the arguments using
4203 // the inverse condition code.
4205 if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
4206 // The swapped inverse condition is legal, so swap true and false,
4207 // lhs and rhs.
4208 Legalized = true;
4209 Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
4210 Tmp1->setFlags(Node->getFlags());
4211 }
4212 }
4213
4214 if (!Legalized) {
4215 Legalized = TLI.LegalizeSetCCCondCode(
4216 DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
4217 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4218
4219 assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
4220
4221 // If we expanded the SETCC by inverting the condition code, then swap
4222 // the True/False operands to match.
4223 if (NeedInvert)
4224 std::swap(Tmp3, Tmp4);
4225
4226 // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
4227 // condition code, create a new SELECT_CC node.
4228 if (CC.getNode()) {
4229 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
4230 Tmp1, Tmp2, Tmp3, Tmp4, CC);
4231 } else {
4232 Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
4233 CC = DAG.getCondCode(ISD::SETNE);
4234 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
4235 Tmp2, Tmp3, Tmp4, CC);
4236 }
4237 Tmp1->setFlags(Node->getFlags());
4238 }
4239 Results.push_back(Tmp1);
4240 break;
4241 }
4242 case ISD::BR_CC: {
4243 // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4244 SDValue Chain;
4245 Tmp1 = Node->getOperand(0); // Chain
4246 Tmp2 = Node->getOperand(2); // LHS
4247 Tmp3 = Node->getOperand(3); // RHS
4248 Tmp4 = Node->getOperand(1); // CC
4249
4250 bool Legalized = TLI.LegalizeSetCCCondCode(
4251 DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
4252 /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
4253 (void)Legalized;
4254 assert(Legalized && "Can't legalize BR_CC with legal condition!");
4255
4256 // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
4257 // node.
4258 if (Tmp4.getNode()) {
4259 assert(!NeedInvert && "Don't know how to invert BR_CC!");
4260
4261 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
4262 Tmp4, Tmp2, Tmp3, Node->getOperand(4));
4263 } else {
4264 Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4265 Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
4266 Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
4267 Tmp2, Tmp3, Node->getOperand(4));
4268 }
4269 Results.push_back(Tmp1);
4270 break;
4271 }
4272 case ISD::BUILD_VECTOR:
4273 Results.push_back(ExpandBUILD_VECTOR(Node));
4274 break;
4275 case ISD::SPLAT_VECTOR:
4276 Results.push_back(ExpandSPLAT_VECTOR(Node));
4277 break;
4278 case ISD::SRA:
4279 case ISD::SRL:
4280 case ISD::SHL: {
4281 // Scalarize vector SRA/SRL/SHL.
4282 EVT VT = Node->getValueType(0);
4283 assert(VT.isVector() && "Unable to legalize non-vector shift");
4284 assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
4285 unsigned NumElem = VT.getVectorNumElements();
4286
4288 for (unsigned Idx = 0; Idx < NumElem; Idx++) {
4289 SDValue Ex =
4291 Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
4292 SDValue Sh =
4294 Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
4295 Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
4296 VT.getScalarType(), Ex, Sh));
4297 }
4298
4299 SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4300 Results.push_back(Result);
4301 break;
4302 }
4305 case ISD::VECREDUCE_ADD:
4306 case ISD::VECREDUCE_MUL:
4307 case ISD::VECREDUCE_AND:
4308 case ISD::VECREDUCE_OR:
4309 case ISD::VECREDUCE_XOR:
4318 Results.push_back(TLI.expandVecReduce(Node, DAG));
4319 break;
4320 case ISD::VP_CTTZ_ELTS:
4321 case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
4322 Results.push_back(TLI.expandVPCTTZElements(Node, DAG));
4323 break;
4324 case ISD::CLEAR_CACHE:
4325 // The default expansion of llvm.clear_cache is simply a no-op for those
4326 // targets where it is not needed.
4327 Results.push_back(Node->getOperand(0));
4328 break;
4330 case ISD::GlobalAddress:
4333 case ISD::ConstantPool:
4334 case ISD::JumpTable:
4338 // FIXME: Custom lowering for these operations shouldn't return null!
4339 // Return true so that we don't call ConvertNodeToLibcall which also won't
4340 // do anything.
4341 return true;
4342 }
4343
4344 if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
4345 // FIXME: We were asked to expand a strict floating-point operation,
4346 // but there is currently no expansion implemented that would preserve
4347 // the "strict" properties. For now, we just fall back to the non-strict
4348 // version if that is legal on the target. The actual mutation of the
4349 // operation will happen in SelectionDAGISel::DoInstructionSelection.
4350 switch (Node->getOpcode()) {
4351 default:
4352 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4353 Node->getValueType(0))
4354 == TargetLowering::Legal)
4355 return true;
4356 break;
4357 case ISD::STRICT_FSUB: {
4358 if (TLI.getStrictFPOperationAction(
4359 ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4360 return true;
4361 if (TLI.getStrictFPOperationAction(
4362 ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4363 break;
4364
4365 EVT VT = Node->getValueType(0);
4366 const SDNodeFlags Flags = Node->getFlags();
4367 SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4368 SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4369 {Node->getOperand(0), Node->getOperand(1), Neg},
4370 Flags);
4371
4372 Results.push_back(Fadd);
4373 Results.push_back(Fadd.getValue(1));
4374 break;
4375 }
4378 case ISD::STRICT_LRINT:
4379 case ISD::STRICT_LLRINT:
4380 case ISD::STRICT_LROUND:
4382 // These are registered by the operand type instead of the value
4383 // type. Reflect that here.
4384 if (TLI.getStrictFPOperationAction(Node->getOpcode(),
4385 Node->getOperand(1).getValueType())
4386 == TargetLowering::Legal)
4387 return true;
4388 break;
4389 }
4390 }
4391
4392 // Replace the original node with the legalized result.
4393 if (Results.empty()) {
4394 LLVM_DEBUG(dbgs() << "Cannot expand node\n");
4395 return false;
4396 }
4397
4398 LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
4399 ReplaceNode(Node, Results.data());
4400 return true;
4401}
4402
4403void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
4404 LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
4406 SDLoc dl(Node);
4407 // FIXME: Check flags on the node to see if we can use a finite call.
4408 unsigned Opc = Node->getOpcode();
4409 switch (Opc) {
4410 case ISD::ATOMIC_FENCE: {
4411 // If the target didn't lower this, lower it to '__sync_synchronize()' call
4412 // FIXME: handle "fence singlethread" more efficiently.
4414
4416 CLI.setDebugLoc(dl)
4417 .setChain(Node->getOperand(0))
4418 .setLibCallee(
4419 CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4420 DAG.getExternalSymbol("__sync_synchronize",
4421 TLI.getPointerTy(DAG.getDataLayout())),
4422 std::move(Args));
4423
4424 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4425
4426 Results.push_back(CallResult.second);
4427 break;
4428 }
4429 // By default, atomic intrinsics are marked Legal and lowered. Targets
4430 // which don't support them directly, however, may want libcalls, in which
4431 // case they mark them Expand, and we get here.
4432 case ISD::ATOMIC_SWAP:
4444 case ISD::ATOMIC_CMP_SWAP: {
4445 MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4446 AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
4447 RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
4448 EVT RetVT = Node->getValueType(0);
4451 if (TLI.getLibcallName(LC)) {
4452 // If outline atomic available, prepare its arguments and expand.
4453 Ops.append(Node->op_begin() + 2, Node->op_end());
4454 Ops.push_back(Node->getOperand(1));
4455
4456 } else {
4457 LC = RTLIB::getSYNC(Opc, VT);
4458 assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4459 "Unexpected atomic op or value type!");
4460 // Arguments for expansion to sync libcall
4461 Ops.append(Node->op_begin() + 1, Node->op_end());
4462 }
4463 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4464 Ops, CallOptions,
4465 SDLoc(Node),
4466 Node->getOperand(0));
4467 Results.push_back(Tmp.first);
4468 Results.push_back(Tmp.second);
4469 break;
4470 }
4471 case ISD::TRAP: {
4472 // If this operation is not supported, lower it to 'abort()' call
4475 CLI.setDebugLoc(dl)
4476 .setChain(Node->getOperand(0))
4477 .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
4478 DAG.getExternalSymbol(
4479 "abort", TLI.getPointerTy(DAG.getDataLayout())),
4480 std::move(Args));
4481 std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
4482
4483 Results.push_back(CallResult.second);
4484 break;
4485 }
4486 case ISD::CLEAR_CACHE: {
4488 SDValue InputChain = Node->getOperand(0);
4489 SDValue StartVal = Node->getOperand(1);
4490 SDValue EndVal = Node->getOperand(2);
4491 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4492 DAG, RTLIB::CLEAR_CACHE, MVT::isVoid, {StartVal, EndVal}, CallOptions,
4493 SDLoc(Node), InputChain);
4494 Results.push_back(Tmp.second);
4495 break;
4496 }
4497 case ISD::FMINNUM:
4499 ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
4500 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4501 RTLIB::FMIN_PPCF128, Results);
4502 break;
4503 // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
4504 // libcall legalization for these nodes, but there is no default expasion for
4505 // these nodes either (see PR63267 for example).
4506 case ISD::FMAXNUM:
4508 ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
4509 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4510 RTLIB::FMAX_PPCF128, Results);
4511 break;
4512 case ISD::FSQRT:
4513 case ISD::STRICT_FSQRT:
4514 ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
4515 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
4516 RTLIB::SQRT_PPCF128, Results);
4517 break;
4518 case ISD::FCBRT:
4519 ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
4520 RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4521 RTLIB::CBRT_PPCF128, Results);
4522 break;
4523 case ISD::FSIN:
4524 case ISD::STRICT_FSIN:
4525 ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
4526 RTLIB::SIN_F80, RTLIB::SIN_F128,
4527 RTLIB::SIN_PPCF128, Results);
4528 break;
4529 case ISD::FCOS:
4530 case ISD::STRICT_FCOS:
4531 ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
4532 RTLIB::COS_F80, RTLIB::COS_F128,
4533 RTLIB::COS_PPCF128, Results);
4534 break;
4535 case ISD::FTAN:
4536 case ISD::STRICT_FTAN:
4537 ExpandFPLibCall(Node, RTLIB::TAN_F32, RTLIB::TAN_F64, RTLIB::TAN_F80,
4538 RTLIB::TAN_F128, RTLIB::TAN_PPCF128, Results);
4539 break;
4540 case ISD::FSINCOS:
4541 // Expand into sincos libcall.
4542 ExpandSinCosLibCall(Node, Results);
4543 break;
4544 case ISD::FLOG:
4545 case ISD::STRICT_FLOG:
4546 ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
4547 RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
4548 break;
4549 case ISD::FLOG2:
4550 case ISD::STRICT_FLOG2:
4551 ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
4552 RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
4553 break;
4554 case ISD::FLOG10:
4555 case ISD::STRICT_FLOG10:
4556 ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
4557 RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
4558 break;
4559 case ISD::FEXP:
4560 case ISD::STRICT_FEXP:
4561 ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
4562 RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
4563 break;
4564 case ISD::FEXP2:
4565 case ISD::STRICT_FEXP2:
4566 ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
4567 RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
4568 break;
4569 case ISD::FEXP10:
4570 ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80,
4571 RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results);
4572 break;
4573 case ISD::FTRUNC:
4574 case ISD::STRICT_FTRUNC:
4575 ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
4576 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4577 RTLIB::TRUNC_PPCF128, Results);
4578 break;
4579 case ISD::FFLOOR:
4580 case ISD::STRICT_FFLOOR:
4581 ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
4582 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4583 RTLIB::FLOOR_PPCF128, Results);
4584 break;
4585 case ISD::FCEIL:
4586 case ISD::STRICT_FCEIL:
4587 ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
4588 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4589 RTLIB::CEIL_PPCF128, Results);
4590 break;
4591 case ISD::FRINT:
4592 case ISD::STRICT_FRINT:
4593 ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
4594 RTLIB::RINT_F80, RTLIB::RINT_F128,
4595 RTLIB::RINT_PPCF128, Results);
4596 break;
4597 case ISD::FNEARBYINT:
4599 ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
4600 RTLIB::NEARBYINT_F64,
4601 RTLIB::NEARBYINT_F80,
4602 RTLIB::NEARBYINT_F128,
4603 RTLIB::NEARBYINT_PPCF128, Results);
4604 break;
4605 case ISD::FROUND:
4606 case ISD::STRICT_FROUND:
4607 ExpandFPLibCall(Node, RTLIB::ROUND_F32,
4608 RTLIB::ROUND_F64,
4609 RTLIB::ROUND_F80,
4610 RTLIB::ROUND_F128,
4611 RTLIB::ROUND_PPCF128, Results);
4612 break;
4613 case ISD::FROUNDEVEN:
4615 ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
4616 RTLIB::ROUNDEVEN_F64,
4617 RTLIB::ROUNDEVEN_F80,
4618 RTLIB::ROUNDEVEN_F128,
4619 RTLIB::ROUNDEVEN_PPCF128, Results);
4620 break;
4621 case ISD::FLDEXP:
4622 case ISD::STRICT_FLDEXP:
4623 ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
4624 RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
4625 break;
4626 case ISD::FFREXP: {
4627 ExpandFrexpLibCall(Node, Results);
4628 break;
4629 }
4630 case ISD::FPOWI:
4631 case ISD::STRICT_FPOWI: {
4632 RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
4633 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
4634 if (!TLI.getLibcallName(LC)) {
4635 // Some targets don't have a powi libcall; use pow instead.
4636 if (Node->isStrictFPOpcode()) {
4638 DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
4639 {Node->getValueType(0), Node->getValueType(1)},
4640 {Node->getOperand(0), Node->getOperand(2)});
4641 SDValue FPOW =
4642 DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node),
4643 {Node->getValueType(0), Node->getValueType(1)},
4644 {Exponent.getValue(1), Node->getOperand(1), Exponent});
4645 Results.push_back(FPOW);
4646 Results.push_back(FPOW.getValue(1));
4647 } else {
4649 DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0),
4650 Node->getOperand(1));
4651 Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node),
4652 Node->getValueType(0),
4653 Node->getOperand(0), Exponent));
4654 }
4655 break;
4656 }
4657 unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0;
4658 bool ExponentHasSizeOfInt =
4659 DAG.getLibInfo().getIntSize() ==
4660 Node->getOperand(1 + Offset).getValueType().getSizeInBits();
4661 if (!ExponentHasSizeOfInt) {
4662 // If the exponent does not match with sizeof(int) a libcall to
4663 // RTLIB::POWI would use the wrong type for the argument.
4664 DAG.getContext()->emitError("POWI exponent does not match sizeof(int)");
4665 Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
4666 break;
4667 }
4668 ExpandFPLibCall(Node, LC, Results);
4669 break;
4670 }
4671 case ISD::FPOW:
4672 case ISD::STRICT_FPOW:
4673 ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
4674 RTLIB::POW_F128, RTLIB::POW_PPCF128, Results);
4675 break;
4676 case ISD::LROUND:
4677 case ISD::STRICT_LROUND:
4678 ExpandArgFPLibCall(Node, RTLIB::LROUND_F32,
4679 RTLIB::LROUND_F64, RTLIB::LROUND_F80,
4680 RTLIB::LROUND_F128,
4681 RTLIB::LROUND_PPCF128, Results);
4682 break;
4683 case ISD::LLROUND:
4685 ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32,
4686 RTLIB::LLROUND_F64, RTLIB::LLROUND_F80,
4687 RTLIB::LLROUND_F128,
4688 RTLIB::LLROUND_PPCF128, Results);
4689 break;
4690 case ISD::LRINT:
4691 case ISD::STRICT_LRINT:
4692 ExpandArgFPLibCall(Node, RTLIB::LRINT_F32,
4693 RTLIB::LRINT_F64, RTLIB::LRINT_F80,
4694 RTLIB::LRINT_F128,
4695 RTLIB::LRINT_PPCF128, Results);
4696 break;
4697 case ISD::LLRINT:
4698 case ISD::STRICT_LLRINT:
4699 ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32,
4700 RTLIB::LLRINT_F64, RTLIB::LLRINT_F80,
4701 RTLIB::LLRINT_F128,
4702 RTLIB::LLRINT_PPCF128, Results);
4703 break;
4704 case ISD::FDIV:
4705 case ISD::STRICT_FDIV:
4706 ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
4707 RTLIB::DIV_F80, RTLIB::DIV_F128,
4708 RTLIB::DIV_PPCF128, Results);
4709 break;
4710 case ISD::FREM:
4711 case ISD::STRICT_FREM:
4712 ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
4713 RTLIB::REM_F80, RTLIB::REM_F128,
4714 RTLIB::REM_PPCF128, Results);
4715 break;
4716 case ISD::FMA:
4717 case ISD::STRICT_FMA:
4718 ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
4719 RTLIB::FMA_F80, RTLIB::FMA_F128,
4720 RTLIB::FMA_PPCF128, Results);
4721 break;
4722 case ISD::FADD:
4723 case ISD::STRICT_FADD:
4724 ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
4725 RTLIB::ADD_F80, RTLIB::ADD_F128,
4726 RTLIB::ADD_PPCF128, Results);
4727 break;
4728 case ISD::FMUL:
4729 case ISD::STRICT_FMUL:
4730 ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
4731 RTLIB::MUL_F80, RTLIB::MUL_F128,
4732 RTLIB::MUL_PPCF128, Results);
4733 break;
4734 case ISD::FP16_TO_FP:
4735 if (Node->getValueType(0) == MVT::f32) {
4736 Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first);
4737 }
4738 break;
4740 if (Node->getValueType(0) == MVT::f32) {
4742 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4743 DAG, RTLIB::FPEXT_BF16_F32, MVT::f32, Node->getOperand(1),
4744 CallOptions, SDLoc(Node), Node->getOperand(0));
4745 Results.push_back(Tmp.first);
4746 Results.push_back(Tmp.second);
4747 }
4748 break;
4750 if (Node->getValueType(0) == MVT::f32) {
4752 std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4753 DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions,
4754 SDLoc(Node), Node->getOperand(0));
4755 Results.push_back(Tmp.first);
4756 Results.push_back(Tmp.second);
4757 }
4758 break;
4759 }
4760 case ISD::FP_TO_FP16: {
4761 RTLIB::Libcall LC =
4762 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
4763 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
4764 Results.push_back(ExpandLibCall(LC, Node, false).first);
4765 break;
4766 }
4767 case ISD::FP_TO_BF16: {
4768 RTLIB::Libcall LC =
4769 RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16);
4770 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16");
4771 Results.push_back(ExpandLibCall(LC, Node, false).first);
4772 break;
4773 }
4776 case ISD::SINT_TO_FP:
4777 case ISD::UINT_TO_FP: {
4778 // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP
4779 bool IsStrict = Node->isStrictFPOpcode();
4780 bool Signed = Node->getOpcode() == ISD::SINT_TO_FP ||
4781 Node->getOpcode() == ISD::STRICT_SINT_TO_FP;
4782 EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType();
4783 EVT RVT = Node->getValueType(0);
4784 EVT NVT = EVT();
4785 SDLoc dl(Node);
4786
4787 // Even if the input is legal, no libcall may exactly match, eg. we don't
4788 // have i1 -> fp conversions. So, it needs to be promoted to a larger type,
4789 // eg: i13 -> fp. Then, look for an appropriate libcall.
4790 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4791 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
4792 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4793 ++t) {
4794 NVT = (MVT::SimpleValueType)t;
4795 // The source needs to big enough to hold the operand.
4796 if (NVT.bitsGE(SVT))
4797 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT)
4798 : RTLIB::getUINTTOFP(NVT, RVT);
4799 }
4800 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4801
4802 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4803 // Sign/zero extend the argument if the libcall takes a larger type.
4804 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
4805 NVT, Node->getOperand(IsStrict ? 1 : 0));
4807 CallOptions.setSExt(Signed);
4808 std::pair<SDValue, SDValue> Tmp =
4809 TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain);
4810 Results.push_back(Tmp.first);
4811 if (IsStrict)
4812 Results.push_back(Tmp.second);
4813 break;
4814 }
4815 case ISD::FP_TO_SINT:
4816 case ISD::FP_TO_UINT:
4819 // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT.
4820 bool IsStrict = Node->isStrictFPOpcode();
4821 bool Signed = Node->getOpcode() == ISD::FP_TO_SINT ||
4822 Node->getOpcode() == ISD::STRICT_FP_TO_SINT;
4823
4824 SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4825 EVT SVT = Op.getValueType();
4826 EVT RVT = Node->getValueType(0);
4827 EVT NVT = EVT();
4828 SDLoc dl(Node);
4829
4830 // Even if the result is legal, no libcall may exactly match, eg. we don't
4831 // have fp -> i1 conversions. So, it needs to be promoted to a larger type,
4832 // eg: fp -> i32. Then, look for an appropriate libcall.
4833 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4834 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
4835 IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4836 ++IntVT) {
4837 NVT = (MVT::SimpleValueType)IntVT;
4838 // The type needs to big enough to hold the result.
4839 if (NVT.bitsGE(RVT))
4840 LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT)
4841 : RTLIB::getFPTOUINT(SVT, NVT);
4842 }
4843 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4844
4845 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4847 std::pair<SDValue, SDValue> Tmp =
4848 TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain);
4849
4850 // Truncate the result if the libcall returns a larger type.
4851 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first));
4852 if (IsStrict)
4853 Results.push_back(Tmp.second);
4854 break;
4855 }
4856
4857 case ISD::FP_ROUND:
4858 case ISD::STRICT_FP_ROUND: {
4859 // X = FP_ROUND(Y, TRUNC)
4860 // TRUNC is a flag, which is always an integer that is zero or one.
4861 // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND
4862 // is known to not change the value of Y.
4863 // We can only expand it into libcall if the TRUNC is 0.
4864 bool IsStrict = Node->isStrictFPOpcode();
4865 SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4866 SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4867 EVT VT = Node->getValueType(0);
4868 assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() &&
4869 "Unable to expand as libcall if it is not normal rounding");
4870
4871 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT);
4872 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4873
4875 std::pair<SDValue, SDValue> Tmp =
4876 TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain);
4877 Results.push_back(Tmp.first);
4878 if (IsStrict)
4879 Results.push_back(Tmp.second);
4880 break;
4881 }
4882 case ISD::FP_EXTEND: {
4883 Results.push_back(
4884 ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(),
4885 Node->getValueType(0)),
4886 Node, false).first);
4887 break;
4888 }
4892 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4893 if (Node->getOpcode() == ISD::STRICT_FP_TO_FP16)
4894 LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16);
4895 else if (Node->getOpcode() == ISD::STRICT_FP_TO_BF16)
4896 LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::bf16);
4897 else
4898 LC = RTLIB::getFPEXT(Node->getOperand(1).getValueType(),
4899 Node->getValueType(0));
4900
4901 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4902
4904 std::pair<SDValue, SDValue> Tmp =
4905 TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1),
4906 CallOptions, SDLoc(Node), Node->getOperand(0));
4907 Results.push_back(Tmp.first);
4908 Results.push_back(Tmp.second);
4909 break;
4910 }
4911 case ISD::FSUB:
4912 case ISD::STRICT_FSUB:
4913 ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
4914 RTLIB::SUB_F80, RTLIB::SUB_F128,
4915 RTLIB::SUB_PPCF128, Results);
4916 break;
4917 case ISD::SREM:
4918 Results.push_back(ExpandIntLibCall(Node, true,
4919 RTLIB::SREM_I8,
4920 RTLIB::SREM_I16, RTLIB::SREM_I32,
4921 RTLIB::SREM_I64, RTLIB::SREM_I128));
4922 break;
4923 case ISD::UREM:
4924 Results.push_back(ExpandIntLibCall(Node, false,
4925 RTLIB::UREM_I8,
4926 RTLIB::UREM_I16, RTLIB::UREM_I32,
4927 RTLIB::UREM_I64, RTLIB::UREM_I128));
4928 break;
4929 case ISD::SDIV:
4930 Results.push_back(ExpandIntLibCall(Node, true,
4931 RTLIB::SDIV_I8,
4932 RTLIB::SDIV_I16, RTLIB::SDIV_I32,
4933 RTLIB::SDIV_I64, RTLIB::SDIV_I128));
4934 break;
4935 case ISD::UDIV:
4936 Results.push_back(ExpandIntLibCall(Node, false,
4937 RTLIB::UDIV_I8,
4938 RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4939 RTLIB::UDIV_I64, RTLIB::UDIV_I128));
4940 break;
4941 case ISD::SDIVREM:
4942 case ISD::UDIVREM:
4943 // Expand into divrem libcall
4944 ExpandDivRemLibCall(Node, Results);
4945 break;
4946 case ISD::MUL:
4947 Results.push_back(ExpandIntLibCall(Node, false,
4948 RTLIB::MUL_I8,
4949 RTLIB::MUL_I16, RTLIB::MUL_I32,
4950 RTLIB::MUL_I64, RTLIB::MUL_I128));
4951 break;
4953 switch (Node->getSimpleValueType(0).SimpleTy) {
4954 default:
4955 llvm_unreachable("LibCall explicitly requested, but not available");
4956 case MVT::i32:
4957 Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false).first);
4958 break;
4959 case MVT::i64:
4960 Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false).first);
4961 break;
4962 case MVT::i128:
4963 Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false).first);
4964 break;
4965 }
4966 break;
4967 case ISD::RESET_FPENV: {
4968 // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
4969 // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc.
4970 SDValue Ptr = DAG.getIntPtrConstant(-1LL, dl);
4971 SDValue Chain = Node->getOperand(0);
4972 Results.push_back(
4973 DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl));
4974 break;
4975 }
4976 case ISD::GET_FPENV_MEM: {
4977 SDValue Chain = Node->getOperand(0);
4978 SDValue EnvPtr = Node->getOperand(1);
4979 Results.push_back(
4980 DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl));
4981 break;
4982 }
4983 case ISD::SET_FPENV_MEM: {
4984 SDValue Chain = Node->getOperand(0);
4985 SDValue EnvPtr = Node->getOperand(1);
4986 Results.push_back(
4987 DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl));
4988 break;
4989 }
4990 case ISD::GET_FPMODE: {
4991 // Call fegetmode, which saves control modes into a stack slot. Then load
4992 // the value to return from the stack.
4993 EVT ModeVT = Node->getValueType(0);
4994 SDValue StackPtr = DAG.CreateStackTemporary(ModeVT);
4995 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
4996 SDValue Chain = DAG.makeStateFunctionCall(RTLIB::FEGETMODE, StackPtr,
4997 Node->getOperand(0), dl);
4998 SDValue LdInst = DAG.getLoad(
4999 ModeVT, dl, Chain, StackPtr,
5000 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
5001 Results.push_back(LdInst);
5002 Results.push_back(LdInst.getValue(1));
5003 break;
5004 }
5005 case ISD::SET_FPMODE: {
5006 // Move control modes to stack slot and then call fesetmode with the pointer
5007 // to the slot as argument.
5008 SDValue Mode = Node->getOperand(1);
5009 EVT ModeVT = Mode.getValueType();
5010 SDValue StackPtr = DAG.CreateStackTemporary(ModeVT);
5011 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
5012 SDValue StInst = DAG.getStore(
5013 Node->getOperand(0), dl, Mode, StackPtr,
5014 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
5015 Results.push_back(
5016 DAG.makeStateFunctionCall(RTLIB::FESETMODE, StackPtr, StInst, dl));
5017 break;
5018 }
5019 case ISD::RESET_FPMODE: {
5020 // It is legalized to a call 'fesetmode(FE_DFL_MODE)'. On most targets
5021 // FE_DFL_MODE is defined as '((const femode_t *) -1)' in glibc. If not, the
5022 // target must provide custom lowering.
5023 const DataLayout &DL = DAG.getDataLayout();
5024 EVT PtrTy = TLI.getPointerTy(DL);
5025 SDValue Mode = DAG.getConstant(-1LL, dl, PtrTy);
5026 Results.push_back(DAG.makeStateFunctionCall(RTLIB::FESETMODE, Mode,
5027 Node->getOperand(0), dl));
5028 break;
5029 }
5030 }
5031
5032 // Replace the original node with the legalized result.
5033 if (!Results.empty()) {
5034 LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n");
5035 ReplaceNode(Node, Results.data());
5036 } else
5037 LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n");
5038}
5039
5040// Determine the vector type to use in place of an original scalar element when
5041// promoting equally sized vectors.
5043 MVT EltVT, MVT NewEltVT) {
5044 unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
5045 MVT MidVT = OldEltsPerNewElt == 1
5046 ? NewEltVT
5047 : MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
5048 assert(TLI.isTypeLegal(MidVT) && "unexpected");
5049 return MidVT;
5050}
5051
5052void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
5053 LLVM_DEBUG(dbgs() << "Trying to promote node\n");
5055 MVT OVT = Node->getSimpleValueType(0);
5056 if (Node->getOpcode() == ISD::UINT_TO_FP ||
5057 Node->getOpcode() == ISD::SINT_TO_FP ||
5058 Node->getOpcode() == ISD::SETCC ||
5059 Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
5060 Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
5061 OVT = Node->getOperand(0).getSimpleValueType();
5062 }
5063 if (Node->getOpcode() == ISD::ATOMIC_STORE ||
5064 Node->getOpcode() == ISD::STRICT_UINT_TO_FP ||
5065 Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
5066 Node->getOpcode() == ISD::STRICT_FSETCC ||
5067 Node->getOpcode() == ISD::STRICT_FSETCCS ||
5068 Node->getOpcode() == ISD::VP_REDUCE_FADD ||
5069 Node->getOpcode() == ISD::VP_REDUCE_FMUL ||
5070 Node->getOpcode() == ISD::VP_REDUCE_FMAX ||
5071 Node->getOpcode() == ISD::VP_REDUCE_FMIN ||
5072 Node->getOpcode() == ISD::VP_REDUCE_FMAXIMUM ||
5073 Node->getOpcode() == ISD::VP_REDUCE_FMINIMUM ||
5074 Node->getOpcode() == ISD::VP_REDUCE_SEQ_FADD)
5075 OVT = Node->getOperand(1).getSimpleValueType();
5076 if (Node->getOpcode() == ISD::BR_CC ||
5077 Node->getOpcode() == ISD::SELECT_CC)
5078 OVT = Node->getOperand(2).getSimpleValueType();
5079 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
5080 SDLoc dl(Node);
5081 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
5082 switch (Node->getOpcode()) {
5083 case ISD::CTTZ:
5085 case ISD::CTLZ:
5087 case ISD::CTPOP: {
5088 // Zero extend the argument unless its cttz, then use any_extend.
5089 if (Node->getOpcode() == ISD::CTTZ ||
5090 Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF)
5091 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5092 else
5093 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
5094
5095 unsigned NewOpc = Node->getOpcode();
5096 if (NewOpc == ISD::CTTZ) {
5097 // The count is the same in the promoted type except if the original
5098 // value was zero. This can be handled by setting the bit just off
5099 // the top of the original type.
5100 auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
5101 OVT.getSizeInBits());
5102 Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
5103 DAG.getConstant(TopBit, dl, NVT));
5104 NewOpc = ISD::CTTZ_ZERO_UNDEF;
5105 }
5106 // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
5107 // already the correct result.
5108 Tmp1 = DAG.getNode(NewOpc, dl, NVT, Tmp1);
5109 if (NewOpc == ISD::CTLZ || NewOpc == ISD::CTLZ_ZERO_UNDEF) {
5110 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
5111 Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
5112 DAG.getConstant(NVT.getSizeInBits() -
5113 OVT.getSizeInBits(), dl, NVT));
5114 }
5115 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5116 break;
5117 }
5118 case ISD::BITREVERSE:
5119 case ISD::BSWAP: {
5120 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
5121 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
5122 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5123 Tmp1 = DAG.getNode(
5124 ISD::SRL, dl, NVT, Tmp1,
5125 DAG.getConstant(DiffBits, dl,
5126 TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
5127
5128 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5129 break;
5130 }
5131 case ISD::FP_TO_UINT:
5133 case ISD::FP_TO_SINT:
5135 PromoteLegalFP_TO_INT(Node, dl, Results);
5136 break;
5139 Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl));
5140 break;
5141 case ISD::UINT_TO_FP:
5143 case ISD::SINT_TO_FP:
5145 PromoteLegalINT_TO_FP(Node, dl, Results);
5146 break;
5147 case ISD::VAARG: {
5148 SDValue Chain = Node->getOperand(0); // Get the chain.
5149 SDValue Ptr = Node->getOperand(1); // Get the pointer.
5150
5151 unsigned TruncOp;
5152 if (OVT.isVector()) {
5153 TruncOp = ISD::BITCAST;
5154 } else {
5155 assert(OVT.isInteger()
5156 && "VAARG promotion is supported only for vectors or integer types");
5157 TruncOp = ISD::TRUNCATE;
5158 }
5159
5160 // Perform the larger operation, then convert back
5161 Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
5162 Node->getConstantOperandVal(3));
5163 Chain = Tmp1.getValue(1);
5164
5165 Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
5166
5167 // Modified the chain result - switch anything that used the old chain to
5168 // use the new one.
5169 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
5170 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
5171 if (UpdatedNodes) {
5172 UpdatedNodes->insert(Tmp2.getNode());
5173 UpdatedNodes->insert(Chain.getNode());
5174 }
5175 ReplacedNode(Node);
5176 break;
5177 }
5178 case ISD::MUL:
5179 case ISD::SDIV:
5180 case ISD::SREM:
5181 case ISD::UDIV:
5182 case ISD::UREM:
5183 case ISD::SMIN:
5184 case ISD::SMAX:
5185 case ISD::UMIN:
5186 case ISD::UMAX:
5187 case ISD::AND:
5188 case ISD::OR:
5189 case ISD::XOR: {
5190 unsigned ExtOp, TruncOp;
5191 if (OVT.isVector()) {
5192 ExtOp = ISD::BITCAST;
5193 TruncOp = ISD::BITCAST;
5194 } else {
5195 assert(OVT.isInteger() && "Cannot promote logic operation");
5196
5197 switch (Node->getOpcode()) {
5198 default:
5199 ExtOp = ISD::ANY_EXTEND;
5200 break;
5201 case ISD::SDIV:
5202 case ISD::SREM:
5203 case ISD::SMIN:
5204 case ISD::SMAX:
5205 ExtOp = ISD::SIGN_EXTEND;
5206 break;
5207 case ISD::UDIV:
5208 case ISD::UREM:
5209 ExtOp = ISD::ZERO_EXTEND;
5210 break;
5211 case ISD::UMIN:
5212 case ISD::UMAX:
5213 if (TLI.isSExtCheaperThanZExt(OVT, NVT))
5214 ExtOp = ISD::SIGN_EXTEND;
5215 else
5216 ExtOp = ISD::ZERO_EXTEND;
5217 break;
5218 }
5219 TruncOp = ISD::TRUNCATE;
5220 }
5221 // Promote each of the values to the new type.
5222 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5223 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5224 // Perform the larger operation, then convert back
5225 Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
5226 Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
5227 break;
5228 }
5229 case ISD::UMUL_LOHI:
5230 case ISD::SMUL_LOHI: {
5231 // Promote to a multiply in a wider integer type.
5232 unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND
5234 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5235 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5236 Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2);
5237
5238 auto &DL = DAG.getDataLayout();
5239 unsigned OriginalSize = OVT.getScalarSizeInBits();
5240 Tmp2 = DAG.getNode(
5241 ISD::SRL, dl, NVT, Tmp1,
5242 DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT)));
5243 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
5244 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
5245 break;
5246 }
5247 case ISD::SELECT: {
5248 unsigned ExtOp, TruncOp;
5249 if (Node->getValueType(0).isVector() ||
5250 Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
5251 ExtOp = ISD::BITCAST;
5252 TruncOp = ISD::BITCAST;
5253 } else if (Node->getValueType(0).isInteger()) {
5254 ExtOp = ISD::ANY_EXTEND;
5255 TruncOp = ISD::TRUNCATE;
5256 } else {
5257 ExtOp = ISD::FP_EXTEND;
5258 TruncOp = ISD::FP_ROUND;
5259 }
5260 Tmp1 = Node->getOperand(0);
5261 // Promote each of the values to the new type.
5262 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5263 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5264 // Perform the larger operation, then round down.
5265 Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
5266 Tmp1->setFlags(Node->getFlags());
5267 if (TruncOp != ISD::FP_ROUND)
5268 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
5269 else
5270 Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
5271 DAG.getIntPtrConstant(0, dl));
5272 Results.push_back(Tmp1);
5273 break;
5274 }
5275 case ISD::VECTOR_SHUFFLE: {
5276 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
5277
5278 // Cast the two input vectors.
5279 Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
5280 Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
5281
5282 // Convert the shuffle mask to the right # elements.
5283 Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
5284 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
5285 Results.push_back(Tmp1);
5286 break;
5287 }
5288 case ISD::VECTOR_SPLICE: {
5289 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5290 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1));
5291 Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2,
5292 Node->getOperand(2));
5293 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3));
5294 break;
5295 }
5296 case ISD::SELECT_CC: {
5297 SDValue Cond = Node->getOperand(4);
5298 ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get();
5299 // Type of the comparison operands.
5300 MVT CVT = Node->getSimpleValueType(0);
5301 assert(CVT == OVT && "not handled");
5302
5303 unsigned ExtOp = ISD::FP_EXTEND;
5304 if (NVT.isInteger()) {
5306 }
5307
5308 // Promote the comparison operands, if needed.
5309 if (TLI.isCondCodeLegal(CCCode, CVT)) {
5310 Tmp1 = Node->getOperand(0);
5311 Tmp2 = Node->getOperand(1);
5312 } else {
5313 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5314 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5315 }
5316 // Cast the true/false operands.
5317 Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5318 Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5319
5320 Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond},
5321 Node->getFlags());
5322
5323 // Cast the result back to the original type.
5324 if (ExtOp != ISD::FP_EXTEND)
5325 Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1);
5326 else
5327 Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1,
5328 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5329
5330 Results.push_back(Tmp1);
5331 break;
5332 }
5333 case ISD::SETCC:
5334 case ISD::STRICT_FSETCC:
5335 case ISD::STRICT_FSETCCS: {
5336 unsigned ExtOp = ISD::FP_EXTEND;
5337 if (NVT.isInteger()) {
5338 ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
5339 if (isSignedIntSetCC(CCCode) ||
5340 TLI.isSExtCheaperThanZExt(Node->getOperand(0).getValueType(), NVT))
5341 ExtOp = ISD::SIGN_EXTEND;
5342 else
5343 ExtOp = ISD::ZERO_EXTEND;
5344 }
5345 if (Node->isStrictFPOpcode()) {
5346 SDValue InChain = Node->getOperand(0);
5347 std::tie(Tmp1, std::ignore) =
5348 DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT);
5349 std::tie(Tmp2, std::ignore) =
5350 DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT);
5351 SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)};
5352 SDValue OutChain = DAG.getTokenFactor(dl, TmpChains);
5353 SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
5354 Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs,
5355 {OutChain, Tmp1, Tmp2, Node->getOperand(3)},
5356 Node->getFlags()));
5357 Results.push_back(Results.back().getValue(1));
5358 break;
5359 }
5360 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5361 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5362 Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1,
5363 Tmp2, Node->getOperand(2), Node->getFlags()));
5364 break;
5365 }
5366 case ISD::BR_CC: {
5367 unsigned ExtOp = ISD::FP_EXTEND;
5368 if (NVT.isInteger()) {
5369 ISD::CondCode CCCode =
5370 cast<CondCodeSDNode>(Node->getOperand(1))->get();
5372 }
5373 Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5374 Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5375 Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
5376 Node->getOperand(0), Node->getOperand(1),
5377 Tmp1, Tmp2, Node->getOperand(4)));
5378 break;
5379 }
5380 case ISD::FADD:
5381 case ISD::FSUB:
5382 case ISD::FMUL:
5383 case ISD::FDIV:
5384 case ISD::FREM:
5385 case ISD::FMINNUM:
5386 case ISD::FMAXNUM:
5387 case ISD::FMINIMUM:
5388 case ISD::FMAXIMUM:
5389 case ISD::FPOW:
5390 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5391 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
5392 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
5393 Node->getFlags());
5394 Results.push_back(
5395 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5396 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5397 break;
5398 case ISD::STRICT_FADD:
5399 case ISD::STRICT_FSUB:
5400 case ISD::STRICT_FMUL:
5401 case ISD::STRICT_FDIV:
5404 case ISD::STRICT_FREM:
5405 case ISD::STRICT_FPOW:
5406 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5407 {Node->getOperand(0), Node->getOperand(1)});
5408 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5409 {Node->getOperand(0), Node->getOperand(2)});
5410 Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5411 Tmp2.getValue(1));
5412 Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5413 {Tmp3, Tmp1, Tmp2});
5414 Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5415 {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)});
5416 Results.push_back(Tmp1);
5417 Results.push_back(Tmp1.getValue(1));
5418 break;
5419 case ISD::FMA:
5420 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5421 Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
5422 Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
5423 Results.push_back(
5424 DAG.getNode(ISD::FP_ROUND, dl, OVT,
5425 DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
5426 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5427 break;
5428 case ISD::STRICT_FMA:
5429 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5430 {Node->getOperand(0), Node->getOperand(1)});
5431 Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5432 {Node->getOperand(0), Node->getOperand(2)});
5433 Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5434 {Node->getOperand(0), Node->getOperand(3)});
5435 Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5436 Tmp2.getValue(1), Tmp3.getValue(1));
5437 Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5438 {Tmp4, Tmp1, Tmp2, Tmp3});
5439 Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5440 {Tmp4.getValue(1), Tmp4, DAG.getIntPtrConstant(0, dl)});
5441 Results.push_back(Tmp4);
5442 Results.push_back(Tmp4.getValue(1));
5443 break;
5444 case ISD::FCOPYSIGN:
5445 case ISD::FLDEXP:
5446 case ISD::FPOWI: {
5447 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5448 Tmp2 = Node->getOperand(1);
5449 Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
5450
5451 // fcopysign doesn't change anything but the sign bit, so
5452 // (fp_round (fcopysign (fpext a), b))
5453 // is as precise as
5454 // (fp_round (fpext a))
5455 // which is a no-op. Mark it as a TRUNCating FP_ROUND.
5456 const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
5457 Results.push_back(
5458 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5459 DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true)));
5460 break;
5461 }
5462 case ISD::STRICT_FPOWI:
5463 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5464 {Node->getOperand(0), Node->getOperand(1)});
5465 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5466 {Tmp1.getValue(1), Tmp1, Node->getOperand(2)});
5467 Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5468 {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
5469 Results.push_back(Tmp3);
5470 Results.push_back(Tmp3.getValue(1));
5471 break;
5472 case ISD::FFREXP: {
5473 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5474 Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1);
5475
5476 Results.push_back(
5477 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5478 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5479
5480 Results.push_back(Tmp2.getValue(1));
5481 break;
5482 }
5483 case ISD::FFLOOR:
5484 case ISD::FCEIL:
5485 case ISD::FRINT:
5486 case ISD::FNEARBYINT:
5487 case ISD::FROUND:
5488 case ISD::FROUNDEVEN:
5489 case ISD::FTRUNC:
5490 case ISD::FNEG:
5491 case ISD::FSQRT:
5492 case ISD::FSIN:
5493 case ISD::FCOS:
5494 case ISD::FTAN:
5495 case ISD::FLOG:
5496 case ISD::FLOG2:
5497 case ISD::FLOG10:
5498 case ISD::FABS:
5499 case ISD::FEXP:
5500 case ISD::FEXP2:
5501 case ISD::FEXP10:
5502 case ISD::FCANONICALIZE:
5503 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5504 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5505 Results.push_back(
5506 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5507 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5508 break;
5509 case ISD::STRICT_FFLOOR:
5510 case ISD::STRICT_FCEIL:
5511 case ISD::STRICT_FRINT:
5513 case ISD::STRICT_FROUND:
5515 case ISD::STRICT_FTRUNC:
5516 case ISD::STRICT_FSQRT:
5517 case ISD::STRICT_FSIN:
5518 case ISD::STRICT_FCOS:
5519 case ISD::STRICT_FTAN:
5520 case ISD::STRICT_FLOG:
5521 case ISD::STRICT_FLOG2:
5522 case ISD::STRICT_FLOG10:
5523 case ISD::STRICT_FEXP:
5524 case ISD::STRICT_FEXP2:
5525 Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5526 {Node->getOperand(0), Node->getOperand(1)});
5527 Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5528 {Tmp1.getValue(1), Tmp1});
5529 Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5530 {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
5531 Results.push_back(Tmp3);
5532 Results.push_back(Tmp3.getValue(1));
5533 break;
5534 case ISD::BUILD_VECTOR: {
5535 MVT EltVT = OVT.getVectorElementType();
5536 MVT NewEltVT = NVT.getVectorElementType();
5537
5538 // Handle bitcasts to a different vector type with the same total bit size
5539 //
5540 // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
5541 // =>
5542 // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
5543
5544 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
5545 "Invalid promote type for build_vector");
5546 assert(NewEltVT.bitsLE(EltVT) && "not handled");
5547
5548 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5549
5551 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
5552 SDValue Op = Node->getOperand(I);
5553 NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
5554 }
5555
5556 SDLoc SL(Node);
5557 SDValue Concat =
5558 DAG.getNode(MidVT == NewEltVT ? ISD::BUILD_VECTOR : ISD::CONCAT_VECTORS,
5559 SL, NVT, NewOps);
5560 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
5561 Results.push_back(CvtVec);
5562 break;
5563 }
5565 MVT EltVT = OVT.getVectorElementType();
5566 MVT NewEltVT = NVT.getVectorElementType();
5567
5568 // Handle bitcasts to a different vector type with the same total bit size.
5569 //
5570 // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
5571 // =>
5572 // v4i32:castx = bitcast x:v2i64
5573 //
5574 // i64 = bitcast
5575 // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
5576 // (i32 (extract_vector_elt castx, (2 * y + 1)))
5577 //
5578
5579 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
5580 "Invalid promote type for extract_vector_elt");
5581 assert(NewEltVT.bitsLT(EltVT) && "not handled");
5582
5583 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5584 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
5585
5586 SDValue Idx = Node->getOperand(1);
5587 EVT IdxVT = Idx.getValueType();
5588 SDLoc SL(Node);
5589 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
5590 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
5591
5592 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
5593
5595 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
5596 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
5597 SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
5598
5599 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
5600 CastVec, TmpIdx);
5601 NewOps.push_back(Elt);
5602 }
5603
5604 SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps);
5605 Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
5606 break;
5607 }
5609 MVT EltVT = OVT.getVectorElementType();
5610 MVT NewEltVT = NVT.getVectorElementType();
5611
5612 // Handle bitcasts to a different vector type with the same total bit size
5613 //
5614 // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
5615 // =>
5616 // v4i32:castx = bitcast x:v2i64
5617 // v2i32:casty = bitcast y:i64
5618 //
5619 // v2i64 = bitcast
5620 // (v4i32 insert_vector_elt
5621 // (v4i32 insert_vector_elt v4i32:castx,
5622 // (extract_vector_elt casty, 0), 2 * z),
5623 // (extract_vector_elt casty, 1), (2 * z + 1))
5624
5625 assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
5626 "Invalid promote type for insert_vector_elt");
5627 assert(NewEltVT.bitsLT(EltVT) && "not handled");
5628
5629 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5630 unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
5631
5632 SDValue Val = Node->getOperand(1);
5633 SDValue Idx = Node->getOperand(2);
5634 EVT IdxVT = Idx.getValueType();
5635 SDLoc SL(Node);
5636
5637 SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
5638 SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
5639
5640 SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
5641 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
5642
5643 SDValue NewVec = CastVec;
5644 for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
5645 SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
5646 SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
5647
5648 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
5649 CastVal, IdxOffset);
5650
5651 NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
5652 NewVec, Elt, InEltIdx);
5653 }
5654
5655 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
5656 break;
5657 }
5658 case ISD::SCALAR_TO_VECTOR: {
5659 MVT EltVT = OVT.getVectorElementType();
5660 MVT NewEltVT = NVT.getVectorElementType();
5661
5662 // Handle bitcasts to different vector type with the same total bit size.
5663 //
5664 // e.g. v2i64 = scalar_to_vector x:i64
5665 // =>
5666 // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
5667 //
5668
5669 MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
5670 SDValue Val = Node->getOperand(0);
5671 SDLoc SL(Node);
5672
5673 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
5674 SDValue Undef = DAG.getUNDEF(MidVT);
5675
5677 NewElts.push_back(CastVal);
5678 for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
5679 NewElts.push_back(Undef);
5680
5681 SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
5682 SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
5683 Results.push_back(CvtVec);
5684 break;
5685 }
5686 case ISD::ATOMIC_SWAP:
5687 case ISD::ATOMIC_STORE: {
5688 AtomicSDNode *AM = cast<AtomicSDNode>(Node);
5689 SDLoc SL(Node);
5690 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal());
5691 assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
5692 "unexpected promotion type");
5693 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
5694 "unexpected atomic_swap with illegal type");
5695
5696 SDValue Op0 = AM->getBasePtr();
5697 SDValue Op1 = CastVal;
5698
5699 // ATOMIC_STORE uses a swapped operand order from every other AtomicSDNode,
5700 // but really it should merge with ISD::STORE.
5701 if (AM->getOpcode() == ISD::ATOMIC_STORE)
5702 std::swap(Op0, Op1);
5703
5704 SDValue NewAtomic = DAG.getAtomic(AM->getOpcode(), SL, NVT, AM->getChain(),
5705 Op0, Op1, AM->getMemOperand());
5706
5707 if (AM->getOpcode() != ISD::ATOMIC_STORE) {
5708 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
5709 Results.push_back(NewAtomic.getValue(1));
5710 } else
5711 Results.push_back(NewAtomic);
5712 break;
5713 }
5714 case ISD::ATOMIC_LOAD: {
5715 AtomicSDNode *AM = cast<AtomicSDNode>(Node);
5716 SDLoc SL(Node);
5717 assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
5718 "unexpected promotion type");
5719 assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
5720 "unexpected atomic_load with illegal type");
5721
5722 SDValue NewAtomic =
5723 DAG.getAtomic(ISD::ATOMIC_LOAD, SL, NVT, DAG.getVTList(NVT, MVT::Other),
5724 {AM->getChain(), AM->getBasePtr()}, AM->getMemOperand());
5725 Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
5726 Results.push_back(NewAtomic.getValue(1));
5727 break;
5728 }
5729 case ISD::SPLAT_VECTOR: {
5730 SDValue Scalar = Node->getOperand(0);
5731 MVT ScalarType = Scalar.getSimpleValueType();
5732 MVT NewScalarType = NVT.getVectorElementType();
5733 if (ScalarType.isInteger()) {
5734 Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NewScalarType, Scalar);
5735 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5736 Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
5737 break;
5738 }
5739 Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewScalarType, Scalar);
5740 Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5741 Results.push_back(
5742 DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5743 DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5744 break;
5745 }
5746 case ISD::VP_REDUCE_FADD:
5747 case ISD::VP_REDUCE_FMUL:
5748 case ISD::VP_REDUCE_FMAX:
5749 case ISD::VP_REDUCE_FMIN:
5750 case ISD::VP_REDUCE_FMAXIMUM:
5751 case ISD::VP_REDUCE_FMINIMUM:
5752 case ISD::VP_REDUCE_SEQ_FADD:
5753 Results.push_back(PromoteReduction(Node));
5754 break;
5755 }
5756
5757 // Replace the original node with the legalized result.
5758 if (!Results.empty()) {
5759 LLVM_DEBUG(dbgs() << "Successfully promoted node\n");
5760 ReplaceNode(Node, Results.data());
5761 } else
5762 LLVM_DEBUG(dbgs() << "Could not promote node\n");
5763}
5764
5765/// This is the entry point for the file.
5768
5769 SmallPtrSet<SDNode *, 16> LegalizedNodes;
5770 // Use a delete listener to remove nodes which were deleted during
5771 // legalization from LegalizeNodes. This is needed to handle the situation
5772 // where a new node is allocated by the object pool to the same address of a
5773 // previously deleted node.
5774 DAGNodeDeletedListener DeleteListener(
5775 *this,
5776 [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); });
5777
5778 SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
5779
5780 // Visit all the nodes. We start in topological order, so that we see
5781 // nodes with their original operands intact. Legalization can produce
5782 // new nodes which may themselves need to be legalized. Iterate until all
5783 // nodes have been legalized.
5784 while (true) {
5785 bool AnyLegalized = false;
5786 for (auto NI = allnodes_end(); NI != allnodes_begin();) {
5787 --NI;
5788
5789 SDNode *N = &*NI;
5790 if (N->use_empty() && N != getRoot().getNode()) {
5791 ++NI;
5792 DeleteNode(N);
5793 continue;
5794 }
5795
5796 if (LegalizedNodes.insert(N).second) {
5797 AnyLegalized = true;
5798 Legalizer.LegalizeOp(N);
5799
5800 if (N->use_empty() && N != getRoot().getNode()) {
5801 ++NI;
5802 DeleteNode(N);
5803 }
5804 }
5805 }
5806 if (!AnyLegalized)
5807 break;
5808
5809 }
5810
5811 // Remove dead nodes now.
5813}
5814
5816 SmallSetVector<SDNode *, 16> &UpdatedNodes) {
5817 SmallPtrSet<SDNode *, 16> LegalizedNodes;
5818 SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
5819
5820 // Directly insert the node in question, and legalize it. This will recurse
5821 // as needed through operands.
5822 LegalizedNodes.insert(N);
5823 Legalizer.LegalizeOp(N);
5824
5825 return LegalizedNodes.count(N);
5826}
#define Success
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
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:531
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:1350
static APFloat getSmallestNormalized(const fltSemantics &Sem, bool Negative=false)
Returns the smallest (by magnitude) normalized finite number in the given semantics.
Definition: APFloat.h:1058
APInt bitcastToAPInt() const
Definition: APFloat.h:1254
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:998
Class for arbitrary precision integers.
Definition: APInt.h:77
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:208
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1309
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition: APInt.h:237
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:188
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:218
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:269
const APFloat & getValueAPF() const
Definition: Constants.h:312
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
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:155
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1399
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:822
const BasicBlock & back() const
Definition: Function.h:825
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:227
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
Definition: SelectionDAG.h:565
const TargetLowering & getTargetLoweringInfo() const
Definition: SelectionDAG.h:492
allnodes_const_iterator allnodes_begin() const
Definition: SelectionDAG.h:545
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
allnodes_const_iterator allnodes_end() const
Definition: SelectionDAG.h:546
void DeleteNode(SDNode *N)
Remove the specified node from the system.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:486
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:487
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:499
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:323
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:361
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:412
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:344
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
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:77
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
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:1795
LLVM Value Representation.
Definition: Value.h:74
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
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
@ Entry
Definition: COFF.h:811
@ 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:764
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:243
@ STACKRESTORE
STACKRESTORE has two operands, an input chain and a pointer to restore to it returns an output chain.
Definition: ISDOpcodes.h:1147
@ STACKSAVE
STACKSAVE - STACKSAVE has one operand, an input chain.
Definition: ISDOpcodes.h:1143
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:737
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:484
@ SET_FPENV
Sets the current floating-point environment.
Definition: ISDOpcodes.h:1019
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1360
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1391
@ EH_SJLJ_LONGJMP
OUTCHAIN = EH_SJLJ_LONGJMP(INCHAIN, buffer) This corresponds to the eh.sjlj.longjmp intrinsic.
Definition: ISDOpcodes.h:153
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:257
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1290
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:567
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:728
@ SMULFIX
RESULT = [US]MULFIX(LHS, RHS, SCALE) - Perform fixed point multiplication on 2 integers with the same...
Definition: ISDOpcodes.h:374
@ VAEND
VAEND, VASTART - VAEND and VASTART have three operands: an input chain, pointer, and a SRCVALUE.
Definition: ISDOpcodes.h:1176
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1292
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1262
@ STRICT_FCEIL
Definition: ISDOpcodes.h:434
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1293
@ FRAME_TO_ARGS_OFFSET
FRAME_TO_ARGS_OFFSET - This node represents offset from frame pointer to first (possible) on-stack ar...
Definition: ISDOpcodes.h:130
@ RESET_FPENV
Set floating-point environment to default state.
Definition: ISDOpcodes.h:1023
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:495
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:246
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1052
@ SMULFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition: ISDOpcodes.h:380
@ SET_FPMODE
Sets the current dynamic floating-point control modes.
Definition: ISDOpcodes.h:1042
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:797
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:491
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:205
@ EH_SJLJ_SETUP_DISPATCH
OUTCHAIN = EH_SJLJ_SETUP_DISPATCH(INCHAIN) The target initializes the dispatch table here.
Definition: ISDOpcodes.h:157
@ 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:1275
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:804
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:551
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1376
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:397
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1380
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:702
@ ATOMIC_FENCE
OUTCHAIN = ATOMIC_FENCE(INCHAIN, ordering, scope) This corresponds to the fence instruction.
Definition: ISDOpcodes.h:1254
@ RESET_FPMODE
Sets default dynamic floating-point control modes.
Definition: ISDOpcodes.h:1046
@ SDIVREM
SDIVREM/UDIVREM - Divide two integers and produce both a quotient and remainder result.
Definition: ISDOpcodes.h:262
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1390
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:485
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:927
@ STRICT_FLOG2
Definition: ISDOpcodes.h:429
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1288
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:917
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1289
@ INIT_TRAMPOLINE
INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
Definition: ISDOpcodes.h:1220
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:954
@ SDIVFIX
RESULT = [US]DIVFIX(LHS, RHS, SCALE) - Perform fixed point division on 2 integers with the same width...
Definition: ISDOpcodes.h:387
@ STRICT_FSQRT
Constrained versions of libm-equivalent floating point intrinsics.
Definition: ISDOpcodes.h:418
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1431
@ 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:1123
@ EH_RETURN
OUTCHAIN = EH_RETURN(INCHAIN, OFFSET, HANDLER) - This node represents 'eh_return' gcc dwarf builtin,...
Definition: ISDOpcodes.h:141
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:788
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:670
@ STRICT_UINT_TO_FP
Definition: ISDOpcodes.h:458
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:628
@ READSTEADYCOUNTER
READSTEADYCOUNTER - This corresponds to the readfixedcounter intrinsic.
Definition: ISDOpcodes.h:1209
@ ADDROFRETURNADDR
ADDROFRETURNADDR - Represents the llvm.addressofreturnaddress intrinsic.
Definition: ISDOpcodes.h:107
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1373
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:736
@ WRITE_REGISTER
Definition: ISDOpcodes.h:125
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1242
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1377
@ FSINCOS
FSINCOS - Compute both fsin and fcos as a single operation.
Definition: ISDOpcodes.h:1009
@ 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:772
@ STRICT_LROUND
Definition: ISDOpcodes.h:439
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:944
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1098
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1291
@ BR_JT
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:1077
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:508
@ IS_FPCLASS
Performs a check of floating point class property, defined by IEEE-754.
Definition: ISDOpcodes.h:515
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:356
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:741
@ STRICT_FPOWI
Definition: ISDOpcodes.h:420
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1258
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1392
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition: ISDOpcodes.h:229
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:635
@ VACOPY
VACOPY - VACOPY has 5 operands: an input chain, a destination pointer, a source pointer,...
Definition: ISDOpcodes.h:1172
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:330
@ STRICT_FTRUNC
Definition: ISDOpcodes.h:438
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1385
@ 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:894
@ STRICT_FP_TO_FP16
Definition: ISDOpcodes.h:930
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:659
@ GET_FPMODE
Reads the current dynamic floating-point control modes.
Definition: ISDOpcodes.h:1037
@ STRICT_FP16_TO_FP
Definition: ISDOpcodes.h:929
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:719
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1287
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:608
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1286
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:581
@ STRICT_FMAXNUM
Definition: ISDOpcodes.h:432
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition: ISDOpcodes.h:124
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:543
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:794
@ TargetConstantFP
Definition: ISDOpcodes.h:165
@ DEBUGTRAP
DEBUGTRAP - Trap intended to get the attention of a debugger.
Definition: ISDOpcodes.h:1232
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:870
@ STRICT_FMINNUM
Definition: ISDOpcodes.h:433
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:756
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1269
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1294
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:986
@ UBSANTRAP
UBSANTRAP - Trap with an immediate describing the kind of sanitizer failure.
Definition: ISDOpcodes.h:1236
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:366
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:338
@ DYNAMIC_STACKALLOC
DYNAMIC_STACKALLOC - Allocate some number of bytes on the stack aligned to a specified boundary.
Definition: ISDOpcodes.h:1062
@ STRICT_LRINT
Definition: ISDOpcodes.h:441
@ 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:812
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:682
@ SDIVFIXSAT
Same as the corresponding unsaturated fixed point instructions, but the result is clamped between the...
Definition: ISDOpcodes.h:393
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:902
@ GLOBAL_OFFSET_TABLE
The address of the GOT.
Definition: ISDOpcodes.h:93
@ STRICT_FROUND
Definition: ISDOpcodes.h:436
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:310
@ STRICT_SINT_TO_FP
STRICT_[US]INT_TO_FP - Convert a signed or unsigned integer to a floating point value.
Definition: ISDOpcodes.h:457
@ STRICT_BF16_TO_FP
Definition: ISDOpcodes.h:938
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1393
@ STRICT_FFLOOR
Definition: ISDOpcodes.h:435
@ STRICT_FROUNDEVEN
Definition: ISDOpcodes.h:437
@ EH_DWARF_CFA
EH_DWARF_CFA - This node represents the pointer to the DWARF Canonical Frame Address (CFA),...
Definition: ISDOpcodes.h:135
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:936
@ FRAMEADDR
FRAMEADDR, RETURNADDR - These nodes represent llvm.frameaddress and llvm.returnaddress on the DAG.
Definition: ISDOpcodes.h:100
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1284
@ STRICT_FP_TO_UINT
Definition: ISDOpcodes.h:451
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:473
@ STRICT_FP_TO_SINT
STRICT_FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:450
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1005
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1285
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:850
@ READCYCLECOUNTER
READCYCLECOUNTER - This corresponds to the readcyclecounter intrinsic.
Definition: ISDOpcodes.h:1203
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:164
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:478
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:694
@ TRAP
TRAP - Trapping instruction.
Definition: ISDOpcodes.h:1229
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:190
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1028
@ STRICT_FP_TO_BF16
Definition: ISDOpcodes.h:939
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:690
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:665
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1374
@ STRICT_FADD
Constrained versions of the binary floating point operators.
Definition: ISDOpcodes.h:407
@ STRICT_FLOG10
Definition: ISDOpcodes.h:428
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:532
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ STRICT_LLRINT
Definition: ISDOpcodes.h:442
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition: ISDOpcodes.h:620
@ STRICT_FEXP2
Definition: ISDOpcodes.h:426
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1283
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:959
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:883
@ SPONENTRY
SPONENTRY - Represents the llvm.sponentry intrinsic.
Definition: ISDOpcodes.h:112
@ STRICT_FLDEXP
Definition: ISDOpcodes.h:421
@ STRICT_LLROUND
Definition: ISDOpcodes.h:440
@ STRICT_FNEARBYINT
Definition: ISDOpcodes.h:431
@ 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:869
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1381
@ EH_SJLJ_SETJMP
RESULT, OUTCHAIN = EH_SJLJ_SETJMP(INCHAIN, buffer) This corresponds to the eh.sjlj....
Definition: ISDOpcodes.h:147
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:800
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1167
@ BRCOND
BRCOND - Conditional branch.
Definition: ISDOpcodes.h:1091
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1361
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:777
@ 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:501
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:347
@ 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:1161
@ STRICT_FRINT
Definition: ISDOpcodes.h:430
@ 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:1341
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1033
@ ADJUST_TRAMPOLINE
ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
Definition: ISDOpcodes.h:1226
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:198
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:523
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:1587
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
Definition: ISDOpcodes.h:1554
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
Definition: ISDOpcodes.h:1534
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:47
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h: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:353
@ Offset
Definition: DWP.cpp:480
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:296
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:340
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:291
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
APFloat scalbn(APFloat X, int Exp, APFloat::roundingMode RM)
Definition: APFloat.h:1414
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:148
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:203
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)