79#include "llvm/IR/IntrinsicsAArch64.h"
80#include "llvm/IR/IntrinsicsAMDGPU.h"
81#include "llvm/IR/IntrinsicsWebAssembly.h"
114#define DEBUG_TYPE "isel"
122 cl::desc(
"Insert the experimental `assertalign` node."),
127 cl::desc(
"Generate low-precision inline sequences "
128 "for some float libcalls"),
134 cl::desc(
"Set the case probability threshold for peeling the case from a "
135 "switch statement. A value greater than 100 will void this "
155 const SDValue *Parts,
unsigned NumParts,
158 std::optional<CallingConv::ID> CC);
167 unsigned NumParts,
MVT PartVT,
EVT ValueVT,
const Value *V,
169 std::optional<CallingConv::ID> CC = std::nullopt,
170 std::optional<ISD::NodeType> AssertOp = std::nullopt) {
174 PartVT, ValueVT, CC))
181 assert(NumParts > 0 &&
"No parts to assemble!");
192 unsigned RoundBits = PartBits * RoundParts;
193 EVT RoundVT = RoundBits == ValueBits ?
199 if (RoundParts > 2) {
203 PartVT, HalfVT, V, InChain);
214 if (RoundParts < NumParts) {
216 unsigned OddParts = NumParts - RoundParts;
219 OddVT, V, InChain, CC);
235 assert(ValueVT ==
EVT(MVT::ppcf128) && PartVT == MVT::f64 &&
246 !PartVT.
isVector() &&
"Unexpected split");
258 if (PartEVT == ValueVT)
262 ValueVT.
bitsLT(PartEVT)) {
275 if (ValueVT.
bitsLT(PartEVT)) {
280 Val = DAG.
getNode(*AssertOp,
DL, PartEVT, Val,
295 llvm::Attribute::StrictFP)) {
297 DAG.
getVTList(ValueVT, MVT::Other), InChain, Val,
309 if (PartEVT == MVT::x86mmx && ValueVT.
isInteger() &&
310 ValueVT.
bitsLT(PartEVT)) {
319 const Twine &ErrMsg) {
322 return Ctx.emitError(ErrMsg);
325 if (CI->isInlineAsm()) {
327 *CI, ErrMsg +
", possible invalid constraint for vector type"));
330 return Ctx.emitError(
I, ErrMsg);
339 const SDValue *Parts,
unsigned NumParts,
342 std::optional<CallingConv::ID> CallConv) {
344 assert(NumParts > 0 &&
"No parts to assemble!");
345 const bool IsABIRegCopy = CallConv.has_value();
354 unsigned NumIntermediates;
359 *DAG.
getContext(), *CallConv, ValueVT, IntermediateVT,
360 NumIntermediates, RegisterVT);
364 NumIntermediates, RegisterVT);
367 assert(NumRegs == NumParts &&
"Part count doesn't match vector breakdown!");
369 assert(RegisterVT == PartVT &&
"Part type doesn't match vector breakdown!");
372 "Part type sizes don't match!");
376 if (NumIntermediates == NumParts) {
379 for (
unsigned i = 0; i != NumParts; ++i)
381 V, InChain, CallConv);
382 }
else if (NumParts > 0) {
385 assert(NumParts % NumIntermediates == 0 &&
386 "Must expand into a divisible number of parts!");
387 unsigned Factor = NumParts / NumIntermediates;
388 for (
unsigned i = 0; i != NumIntermediates; ++i)
390 IntermediateVT, V, InChain, CallConv);
405 DL, BuiltVectorTy,
Ops);
411 if (PartEVT == ValueVT)
427 "Cannot narrow, it would be a lossy transformation");
433 if (PartEVT == ValueVT)
458 }
else if (ValueVT.
bitsLT(PartEVT)) {
467 *DAG.
getContext(), V,
"non-trivial scalar-to-vector conversion");
498 std::optional<CallingConv::ID> CallConv);
505 unsigned NumParts,
MVT PartVT,
const Value *V,
506 std::optional<CallingConv::ID> CallConv = std::nullopt,
520 unsigned OrigNumParts = NumParts;
522 "Copying to an illegal type!");
528 EVT PartEVT = PartVT;
529 if (PartEVT == ValueVT) {
530 assert(NumParts == 1 &&
"No-op copy with multiple parts!");
539 assert(NumParts == 1 &&
"Do not know what to promote to!");
550 "Unknown mismatch!");
552 Val = DAG.
getNode(ExtendKind,
DL, ValueVT, Val);
553 if (PartVT == MVT::x86mmx)
558 assert(NumParts == 1 && PartEVT != ValueVT);
564 "Unknown mismatch!");
567 if (PartVT == MVT::x86mmx)
574 "Failed to tile the value with PartVT!");
577 if (PartEVT != ValueVT) {
579 "scalar-to-vector conversion failed");
588 if (NumParts & (NumParts - 1)) {
591 "Do not know what to expand to!");
593 unsigned RoundBits = RoundParts * PartBits;
594 unsigned OddParts = NumParts - RoundParts;
603 std::reverse(Parts + RoundParts, Parts + NumParts);
605 NumParts = RoundParts;
617 for (
unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
618 for (
unsigned i = 0; i < NumParts; i += StepSize) {
619 unsigned ThisBits = StepSize * PartBits / 2;
622 SDValue &Part1 = Parts[i+StepSize/2];
629 if (ThisBits == PartBits && ThisVT != PartVT) {
637 std::reverse(Parts, Parts + OrigNumParts);
659 if (ValueEVT == MVT::bf16 && PartEVT == MVT::f16) {
661 "Cannot widen to illegal type");
665 }
else if (PartEVT != ValueEVT) {
680 Ops.append((PartNumElts - ValueNumElts).getFixedValue(), EltUndef);
691 std::optional<CallingConv::ID> CallConv) {
695 const bool IsABIRegCopy = CallConv.has_value();
698 EVT PartEVT = PartVT;
699 if (PartEVT == ValueVT) {
745 "lossy conversion of vector to scalar type");
760 unsigned NumIntermediates;
764 *DAG.
getContext(), *CallConv, ValueVT, IntermediateVT, NumIntermediates,
769 NumIntermediates, RegisterVT);
772 assert(NumRegs == NumParts &&
"Part count doesn't match vector breakdown!");
774 assert(RegisterVT == PartVT &&
"Part type doesn't match vector breakdown!");
777 "Mixing scalable and fixed vectors when copying in parts");
779 std::optional<ElementCount> DestEltCnt;
789 if (ValueVT == BuiltVectorTy) {
813 for (
unsigned i = 0; i != NumIntermediates; ++i) {
828 if (NumParts == NumIntermediates) {
831 for (
unsigned i = 0; i != NumParts; ++i)
833 }
else if (NumParts > 0) {
836 assert(NumIntermediates != 0 &&
"division by zero");
837 assert(NumParts % NumIntermediates == 0 &&
838 "Must expand into a divisible number of parts!");
839 unsigned Factor = NumParts / NumIntermediates;
840 for (
unsigned i = 0; i != NumIntermediates; ++i)
848 if (
I.hasOperandBundlesOtherThan(AllowedBundles)) {
852 for (
unsigned i = 0, e =
I.getNumOperandBundles(); i != e; ++i) {
855 OS << LS << U.getTagName();
858 Twine(
"cannot lower ", Name)
864 EVT valuevt, std::optional<CallingConv::ID> CC)
870 std::optional<CallingConv::ID> CC) {
884 for (
unsigned i = 0; i != NumRegs; ++i)
885 Regs.push_back(Reg + i);
886 RegVTs.push_back(RegisterVT);
888 Reg = Reg.id() + NumRegs;
915 for (
unsigned i = 0; i != NumRegs; ++i) {
921 *Glue =
P.getValue(2);
924 Chain =
P.getValue(1);
952 EVT FromVT(MVT::Other);
956 }
else if (NumSignBits > 1) {
964 assert(FromVT != MVT::Other);
970 RegisterVT, ValueVT, V, Chain,
CallConv);
986 unsigned NumRegs =
Regs.size();
1001 NumParts, RegisterVT, V,
CallConv, ExtendKind);
1007 for (
unsigned i = 0; i != NumRegs; ++i) {
1019 if (NumRegs == 1 || Glue)
1030 Chain = Chains[NumRegs-1];
1036 unsigned MatchingIdx,
const SDLoc &dl,
1038 std::vector<SDValue> &
Ops)
const {
1043 Flag.setMatchingOp(MatchingIdx);
1044 else if (!
Regs.empty() &&
Regs.front().isVirtual()) {
1052 Flag.setRegClass(RC->
getID());
1063 "No 1:1 mapping from clobbers to regs?");
1066 for (
unsigned I = 0, E =
ValueVTs.size();
I != E; ++
I) {
1071 "If we clobbered the stack pointer, MFI should know about it.");
1080 for (
unsigned i = 0; i != NumRegs; ++i) {
1081 assert(Reg <
Regs.size() &&
"Mismatch in # registers expected");
1093 unsigned RegCount = std::get<0>(CountAndVT);
1094 MVT RegisterVT = std::get<1>(CountAndVT);
1112 SL->init(
DAG.getTargetLoweringInfo(), TM,
DAG.getDataLayout());
1114 *
DAG.getMachineFunction().getFunction().getParent());
1119 UnusedArgNodeMap.clear();
1121 PendingExports.clear();
1122 PendingConstrainedFP.clear();
1123 PendingConstrainedFPStrict.clear();
1131 DanglingDebugInfoMap.clear();
1138 if (Pending.
empty())
1144 unsigned i = 0, e = Pending.
size();
1145 for (; i != e; ++i) {
1147 if (Pending[i].
getNode()->getOperand(0) == Root)
1155 if (Pending.
size() == 1)
1182 if (!PendingConstrainedFPStrict.empty()) {
1183 assert(PendingConstrainedFP.empty());
1184 updateRoot(PendingConstrainedFPStrict);
1197 if (!PendingConstrainedFP.empty()) {
1198 assert(PendingConstrainedFPStrict.empty());
1199 updateRoot(PendingConstrainedFP);
1203 return DAG.getRoot();
1211 PendingConstrainedFP.size() +
1212 PendingConstrainedFPStrict.size());
1214 PendingConstrainedFP.end());
1215 PendingLoads.append(PendingConstrainedFPStrict.begin(),
1216 PendingConstrainedFPStrict.end());
1217 PendingConstrainedFP.clear();
1218 PendingConstrainedFPStrict.clear();
1225 PendingExports.append(PendingConstrainedFPStrict.begin(),
1226 PendingConstrainedFPStrict.end());
1227 PendingConstrainedFPStrict.clear();
1228 return updateRoot(PendingExports);
1235 assert(Variable &&
"Missing variable");
1242 <<
"dbg_declare: Dropping debug info (bad/undef/unused-arg address)\n");
1258 if (IsParameter && FINode) {
1260 SDV =
DAG.getFrameIndexDbgValue(Variable,
Expression, FINode->getIndex(),
1261 true,
DL, SDNodeOrder);
1266 FuncArgumentDbgValueKind::Declare,
N);
1269 SDV =
DAG.getDbgValue(Variable,
Expression,
N.getNode(),
N.getResNo(),
1270 true,
DL, SDNodeOrder);
1272 DAG.AddDbgValue(SDV, IsParameter);
1277 FuncArgumentDbgValueKind::Declare,
N)) {
1279 <<
" (could not emit func-arg dbg_value)\n");
1290 for (
auto It = FnVarLocs->locs_begin(&
I), End = FnVarLocs->locs_end(&
I);
1292 auto *Var = FnVarLocs->getDILocalVariable(It->VariableID);
1294 if (It->Values.isKillLocation(It->Expr)) {
1300 It->Values.hasArgList())) {
1303 FnVarLocs->getDILocalVariable(It->VariableID),
1304 It->Expr, Vals.
size() > 1, It->DL, SDNodeOrder);
1317 bool SkipDbgVariableRecords =
DAG.getFunctionVarLocs();
1320 for (
DbgRecord &DR :
I.getDbgRecordRange()) {
1322 assert(DLR->getLabel() &&
"Missing label");
1324 DAG.getDbgLabel(DLR->getLabel(), DLR->getDebugLoc(), SDNodeOrder);
1325 DAG.AddDbgLabel(SDV);
1329 if (SkipDbgVariableRecords)
1337 if (
FuncInfo.PreprocessedDVRDeclares.contains(&DVR))
1339 LLVM_DEBUG(
dbgs() <<
"SelectionDAG visiting dbg_declare: " << DVR
1365 SDNodeOrder, IsVariadic)) {
1376 if (
I.isTerminator()) {
1377 HandlePHINodesInSuccessorBlocks(
I.getParent());
1384 bool NodeInserted =
false;
1385 std::unique_ptr<SelectionDAG::DAGNodeInsertedListener> InsertedListener;
1386 MDNode *PCSectionsMD =
I.getMetadata(LLVMContext::MD_pcsections);
1387 MDNode *MMRA =
I.getMetadata(LLVMContext::MD_mmra);
1388 if (PCSectionsMD || MMRA) {
1389 InsertedListener = std::make_unique<SelectionDAG::DAGNodeInsertedListener>(
1390 DAG, [&](
SDNode *) { NodeInserted =
true; });
1400 if (PCSectionsMD || MMRA) {
1401 auto It = NodeMap.find(&
I);
1402 if (It != NodeMap.end()) {
1404 DAG.addPCSections(It->second.getNode(), PCSectionsMD);
1406 DAG.addMMRAMetadata(It->second.getNode(), MMRA);
1407 }
else if (NodeInserted) {
1410 errs() <<
"warning: loosing !pcsections and/or !mmra metadata ["
1411 <<
I.getModule()->getName() <<
"]\n";
1420void SelectionDAGBuilder::visitPHI(
const PHINode &) {
1430#define HANDLE_INST(NUM, OPCODE, CLASS) \
1431 case Instruction::OPCODE: visit##OPCODE((const CLASS&)I); break;
1432#include "llvm/IR/Instruction.def"
1469 DanglingDebugInfoMap[
Values[0]].emplace_back(Var, Expr,
DL, Order);
1474 auto isMatchingDbgValue = [&](DanglingDebugInfo &DDI) {
1475 DIVariable *DanglingVariable = DDI.getVariable();
1477 if (DanglingVariable == Variable && Expr->
fragmentsOverlap(DanglingExpr)) {
1479 << printDDI(
nullptr, DDI) <<
"\n");
1485 for (
auto &DDIMI : DanglingDebugInfoMap) {
1486 DanglingDebugInfoVector &DDIV = DDIMI.second;
1490 for (
auto &DDI : DDIV)
1491 if (isMatchingDbgValue(DDI))
1494 erase_if(DDIV, isMatchingDbgValue);
1502 auto DanglingDbgInfoIt = DanglingDebugInfoMap.find(V);
1503 if (DanglingDbgInfoIt == DanglingDebugInfoMap.end())
1506 DanglingDebugInfoVector &DDIV = DanglingDbgInfoIt->second;
1507 for (
auto &DDI : DDIV) {
1509 unsigned DbgSDNodeOrder = DDI.getSDNodeOrder();
1512 assert(Variable->isValidLocationForIntrinsic(
DL) &&
1513 "Expected inlined-at fields to agree");
1523 if (!EmitFuncArgumentDbgValue(V, Variable, Expr,
DL,
1524 FuncArgumentDbgValueKind::Value, Val)) {
1526 << printDDI(V, DDI) <<
"\n");
1533 <<
"changing SDNodeOrder from " << DbgSDNodeOrder <<
" to "
1534 << ValSDNodeOrder <<
"\n");
1535 SDV = getDbgValue(Val, Variable, Expr,
DL,
1536 std::max(DbgSDNodeOrder, ValSDNodeOrder));
1537 DAG.AddDbgValue(SDV,
false);
1541 <<
" in EmitFuncArgumentDbgValue\n");
1543 LLVM_DEBUG(
dbgs() <<
"Dropping debug info for " << printDDI(V, DDI)
1547 DAG.getConstantDbgValue(Variable, Expr,
Poison,
DL, DbgSDNodeOrder);
1548 DAG.AddDbgValue(SDV,
false);
1555 DanglingDebugInfo &DDI) {
1560 const Value *OrigV = V;
1564 unsigned SDOrder = DDI.getSDNodeOrder();
1568 bool StackValue =
true;
1593 if (!AdditionalValues.
empty())
1603 dbgs() <<
"Salvaged debug location info for:\n " << *Var <<
"\n"
1604 << *OrigV <<
"\nBy stripping back to:\n " << *V <<
"\n");
1612 assert(OrigV &&
"V shouldn't be null");
1614 auto *SDV =
DAG.getConstantDbgValue(Var, Expr,
Poison,
DL, SDNodeOrder);
1615 DAG.AddDbgValue(SDV,
false);
1617 << printDDI(OrigV, DDI) <<
"\n");
1634 unsigned Order,
bool IsVariadic) {
1639 if (visitEntryValueDbgValue(
Values, Var, Expr, DbgLoc))
1644 for (
const Value *V :
Values) {
1654 if (CE->getOpcode() == Instruction::IntToPtr) {
1673 N = UnusedArgNodeMap[V];
1678 EmitFuncArgumentDbgValue(V, Var, Expr, DbgLoc,
1679 FuncArgumentDbgValueKind::Value,
N))
1706 bool IsParamOfFunc =
1714 auto VMI =
FuncInfo.ValueMap.find(V);
1715 if (VMI !=
FuncInfo.ValueMap.end()) {
1720 V->getType(), std::nullopt);
1726 unsigned BitsToDescribe = 0;
1728 BitsToDescribe = *VarSize;
1730 BitsToDescribe = Fragment->SizeInBits;
1733 if (
Offset >= BitsToDescribe)
1736 unsigned RegisterSize = RegAndSize.second;
1737 unsigned FragmentSize = (
Offset + RegisterSize > BitsToDescribe)
1738 ? BitsToDescribe -
Offset
1741 Expr,
Offset, FragmentSize);
1745 Var, *FragmentExpr, RegAndSize.first,
false, DbgLoc, Order);
1746 DAG.AddDbgValue(SDV,
false);
1762 DAG.getDbgValueList(Var, Expr, LocationOps, Dependencies,
1763 false, DbgLoc, Order, IsVariadic);
1764 DAG.AddDbgValue(SDV,
false);
1770 for (
auto &Pair : DanglingDebugInfoMap)
1771 for (
auto &DDI : Pair.second)
1779 auto It =
FuncInfo.ValueMap.find(V);
1782 if (It !=
FuncInfo.ValueMap.end()) {
1786 DAG.getDataLayout(), InReg, Ty,
1803 if (
N.getNode())
return N;
1818 if (V->getType()->isVoidTy())
1823 V->getType(), ValueVTs);
1873 return DAG.getSplatBuildVector(
1876 return DAG.getConstant(*CI,
DL, VT);
1888 getValue(CPA->getAddrDiscriminator()),
1889 getValue(CPA->getDiscriminator()));
1905 visit(CE->getOpcode(), *CE);
1907 assert(N1.
getNode() &&
"visit didn't populate the NodeMap!");
1913 for (
const Use &U :
C->operands()) {
1919 for (
unsigned i = 0, e = Val->
getNumValues(); i != e; ++i)
1920 Constants.push_back(
SDValue(Val, i));
1929 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
1933 for (
unsigned i = 0, e = Val->
getNumValues(); i != e; ++i)
1942 if (
C->getType()->isStructTy() ||
C->getType()->isArrayTy()) {
1944 "Unknown struct or array constant!");
1948 unsigned NumElts = ValueVTs.
size();
1952 for (
unsigned i = 0; i != NumElts; ++i) {
1953 EVT EltVT = ValueVTs[i];
1955 Constants[i] =
DAG.getUNDEF(EltVT);
1966 return DAG.getBlockAddress(BA, VT);
1969 return getValue(Equiv->getGlobalValue());
1974 if (VT == MVT::aarch64svcount) {
1975 assert(
C->isNullValue() &&
"Can only zero this target type!");
1981 assert(
C->isNullValue() &&
"Can only zero this target type!");
1991 if (VT == MVT::externref || VT == MVT::funcref) {
1992 assert(
C->isNullValue() &&
"Can only zero this target type!");
1995 Intrinsic::ID IID = VT == MVT::externref ? Intrinsic::wasm_ref_null_extern
1996 : Intrinsic::wasm_ref_null_func;
2008 for (
unsigned i = 0; i != NumElements; ++i)
2035 return DAG.getFrameIndex(
2043 Inst->getType(), std::nullopt);
2057void SelectionDAGBuilder::visitCatchPad(
const CatchPadInst &
I) {
2070 if (IsMSVCCXX || IsCoreCLR)
2076 MachineBasicBlock *TargetMBB =
FuncInfo.getMBB(
I.getSuccessor());
2077 FuncInfo.MBB->addSuccessor(TargetMBB);
2084 if (TargetMBB != NextBlock(
FuncInfo.MBB) ||
2093 DAG.getMachineFunction().setHasEHContTarget(
true);
2099 Value *ParentPad =
I.getCatchSwitchParentPad();
2102 SuccessorColor = &
FuncInfo.Fn->getEntryBlock();
2105 assert(SuccessorColor &&
"No parent funclet for catchret!");
2106 MachineBasicBlock *SuccessorColorMBB =
FuncInfo.getMBB(SuccessorColor);
2107 assert(SuccessorColorMBB &&
"No MBB for SuccessorColor!");
2112 DAG.getBasicBlock(SuccessorColorMBB));
2116void SelectionDAGBuilder::visitCleanupPad(
const CleanupPadInst &CPI) {
2122 FuncInfo.MBB->setIsEHFuncletEntry();
2123 FuncInfo.MBB->setIsCleanupFuncletEntry();
2152 UnwindDests.emplace_back(FuncInfo.
getMBB(EHPadBB), Prob);
2158 UnwindDests.emplace_back(FuncInfo.
getMBB(EHPadBB), Prob);
2159 UnwindDests.back().first->setIsEHScopeEntry();
2162 UnwindDests.back().first->setIsEHFuncletEntry();
2166 for (
const BasicBlock *CatchPadBB : CatchSwitch->handlers()) {
2167 UnwindDests.emplace_back(FuncInfo.
getMBB(CatchPadBB), Prob);
2169 if (IsMSVCCXX || IsCoreCLR)
2170 UnwindDests.back().first->setIsEHFuncletEntry();
2172 UnwindDests.back().first->setIsEHScopeEntry();
2174 NewEHPadBB = CatchSwitch->getUnwindDest();
2180 if (BPI && NewEHPadBB)
2182 EHPadBB = NewEHPadBB;
2189 auto UnwindDest =
I.getUnwindDest();
2190 BranchProbabilityInfo *BPI =
FuncInfo.BPI;
2191 BranchProbability UnwindDestProb =
2196 for (
auto &UnwindDest : UnwindDests) {
2197 UnwindDest.first->setIsEHPad();
2198 addSuccessorWithProb(
FuncInfo.MBB, UnwindDest.first, UnwindDest.second);
2200 FuncInfo.MBB->normalizeSuccProbs();
2203 MachineBasicBlock *CleanupPadMBB =
2204 FuncInfo.getMBB(
I.getCleanupPad()->getParent());
2210void SelectionDAGBuilder::visitCatchSwitch(
const CatchSwitchInst &CSI) {
2214void SelectionDAGBuilder::visitRet(
const ReturnInst &
I) {
2215 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
2216 auto &
DL =
DAG.getDataLayout();
2228 if (
I.getParent()->getTerminatingDeoptimizeCall()) {
2242 Type *RetTy =
I.getOperand(0)->getType();
2243 Align BaseAlign =
DL.getPrefTypeAlign(RetTy);
2249 SmallVector<uint64_t, 4>
Offsets;
2251 unsigned NumValues = ValueVTs.
size();
2254 for (
unsigned i = 0; i != NumValues; ++i) {
2261 if (MemVTs[i] != ValueVTs[i])
2263 Chains[i] =
DAG.getStore(
2271 MVT::Other, Chains);
2272 }
else if (
I.getNumOperands() != 0) {
2275 unsigned NumValues =
Types.size();
2279 const Function *
F =
I.getParent()->getParent();
2282 I.getOperand(0)->getType(),
F->getCallingConv(),
2286 if (
F->getAttributes().hasRetAttr(Attribute::SExt))
2288 else if (
F->getAttributes().hasRetAttr(Attribute::ZExt))
2291 LLVMContext &
Context =
F->getContext();
2292 bool RetInReg =
F->getAttributes().hasRetAttr(Attribute::InReg);
2294 for (
unsigned j = 0;
j != NumValues; ++
j) {
2307 &Parts[0], NumParts, PartVT, &
I, CC, ExtendKind);
2310 ISD::ArgFlagsTy
Flags = ISD::ArgFlagsTy();
2314 if (
I.getOperand(0)->getType()->isPointerTy()) {
2316 Flags.setPointerAddrSpace(
2320 if (NeedsRegBlock) {
2321 Flags.setInConsecutiveRegs();
2322 if (j == NumValues - 1)
2323 Flags.setInConsecutiveRegsLast();
2331 else if (
F->getAttributes().hasRetAttr(Attribute::NoExt))
2334 for (
unsigned i = 0; i < NumParts; ++i) {
2337 VT, Types[j], 0, 0));
2347 const Function *
F =
I.getParent()->getParent();
2349 F->getAttributes().hasAttrSomewhere(Attribute::SwiftError)) {
2351 ISD::ArgFlagsTy
Flags = ISD::ArgFlagsTy();
2352 Flags.setSwiftError();
2364 bool isVarArg =
DAG.getMachineFunction().getFunction().isVarArg();
2366 DAG.getMachineFunction().getFunction().getCallingConv();
2367 Chain =
DAG.getTargetLoweringInfo().LowerReturn(
2372 "LowerReturn didn't return a valid chain!");
2383 if (V->getType()->isEmptyTy())
2386 auto VMI =
FuncInfo.ValueMap.find(V);
2387 if (VMI !=
FuncInfo.ValueMap.end()) {
2389 "Unused value assigned virtual registers!");
2402 if (
FuncInfo.isExportedInst(V))
return;
2414 if (VI->getParent() == FromBB)
2440 const BasicBlock *SrcBB = Src->getBasicBlock();
2441 const BasicBlock *DstBB = Dst->getBasicBlock();
2445 auto SuccSize = std::max<uint32_t>(
succ_size(SrcBB), 1);
2455 Src->addSuccessorWithoutProb(Dst);
2458 Prob = getEdgeProbability(Src, Dst);
2459 Src->addSuccessor(Dst, Prob);
2465 return I->getParent() == BB;
2489 if (CurBB == SwitchBB ||
2495 InvertCond ? IC->getInversePredicate() : IC->getPredicate();
2500 InvertCond ? FC->getInversePredicate() : FC->getPredicate();
2502 if (FC->hasNoNaNs() ||
2510 CaseBlock CB(Condition, BOp->getOperand(0), BOp->getOperand(1),
nullptr,
2512 SL->SwitchCases.push_back(CB);
2521 SL->SwitchCases.push_back(CB);
2529 unsigned Depth = 0) {
2538 if (Necessary !=
nullptr) {
2541 if (Necessary->contains(
I))
2549 for (
unsigned OpIdx = 0,
E =
I->getNumOperands(); OpIdx <
E; ++OpIdx)
2569 if (BPI !=
nullptr) {
2575 std::optional<bool> Likely;
2578 else if (BPI->
isEdgeHot(
I.getParent(), IfFalse))
2582 if (
Opc == (*Likely ? Instruction::And : Instruction::Or))
2594 if (CostThresh <= 0)
2615 Value *BrCond =
I.getCondition();
2616 auto ShouldCountInsn = [&RhsDeps, &BrCond](
const Instruction *Ins) {
2617 for (
const auto *U : Ins->users()) {
2620 if (UIns != BrCond && !RhsDeps.
contains(UIns))
2633 for (
unsigned PruneIters = 0; PruneIters < MaxPruneIters; ++PruneIters) {
2635 for (
const auto &InsPair : RhsDeps) {
2636 if (!ShouldCountInsn(InsPair.first)) {
2637 ToDrop = InsPair.first;
2641 if (ToDrop ==
nullptr)
2643 RhsDeps.erase(ToDrop);
2646 for (
const auto &InsPair : RhsDeps) {
2651 CostOfIncluding +=
TTI->getInstructionCost(
2654 if (CostOfIncluding > CostThresh)
2680 const Value *BOpOp0, *BOpOp1;
2694 if (BOpc == Instruction::And)
2695 BOpc = Instruction::Or;
2696 else if (BOpc == Instruction::Or)
2697 BOpc = Instruction::And;
2703 bool BOpIsInOrAndTree = BOpc && BOpc ==
Opc && BOp->
hasOneUse();
2708 TProb, FProb, InvertCond);
2718 if (
Opc == Instruction::Or) {
2739 auto NewTrueProb = TProb / 2;
2740 auto NewFalseProb = TProb / 2 + FProb;
2743 NewFalseProb, InvertCond);
2750 Probs[1], InvertCond);
2752 assert(
Opc == Instruction::And &&
"Unknown merge op!");
2772 auto NewTrueProb = TProb + FProb / 2;
2773 auto NewFalseProb = FProb / 2;
2776 NewFalseProb, InvertCond);
2783 Probs[1], InvertCond);
2792 if (Cases.size() != 2)
return true;
2796 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
2797 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
2798 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
2799 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
2805 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
2806 Cases[0].CC == Cases[1].CC &&
2809 if (Cases[0].CC ==
ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
2811 if (Cases[0].CC ==
ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
2818void SelectionDAGBuilder::visitUncondBr(
const UncondBrInst &
I) {
2828 if (Succ0MBB != NextBlock(BrMBB) ||
2837void SelectionDAGBuilder::visitCondBr(
const CondBrInst &
I) {
2838 MachineBasicBlock *BrMBB =
FuncInfo.MBB;
2840 MachineBasicBlock *Succ0MBB =
FuncInfo.getMBB(
I.getSuccessor(0));
2844 const Value *CondVal =
I.getCondition();
2845 MachineBasicBlock *Succ1MBB =
FuncInfo.getMBB(
I.getSuccessor(1));
2864 bool IsUnpredictable =
I.hasMetadata(LLVMContext::MD_unpredictable);
2866 if (!
DAG.getTargetLoweringInfo().isJumpExpensive() && BOp &&
2869 const Value *BOp0, *BOp1;
2872 Opcode = Instruction::And;
2874 Opcode = Instruction::Or;
2881 DAG.getTargetLoweringInfo().getJumpConditionMergingParams(
2882 Opcode, BOp0, BOp1,
FuncInfo.Fn))) {
2884 getEdgeProbability(BrMBB, Succ0MBB),
2885 getEdgeProbability(BrMBB, Succ1MBB),
2890 assert(
SL->SwitchCases[0].ThisBB == BrMBB &&
"Unexpected lowering!");
2894 for (
unsigned i = 1, e =
SL->SwitchCases.size(); i != e; ++i) {
2901 SL->SwitchCases.erase(
SL->SwitchCases.begin());
2907 for (
unsigned i = 1, e =
SL->SwitchCases.size(); i != e; ++i)
2908 FuncInfo.MF->erase(
SL->SwitchCases[i].ThisBB);
2910 SL->SwitchCases.clear();
2916 nullptr, Succ0MBB, Succ1MBB, BrMBB,
getCurSDLoc(),
2937 if (CB.
TrueBB != NextBlock(SwitchBB)) {
2944 auto &TLI =
DAG.getTargetLoweringInfo();
2968 Cond =
DAG.getSetCC(dl, MVT::i1, CondLHS, CondRHS, CB.
CC);
2980 Cond =
DAG.getSetCC(dl, MVT::i1, CmpOp,
DAG.getConstant(
High, dl, VT),
2984 VT, CmpOp,
DAG.getConstant(
Low, dl, VT));
2985 Cond =
DAG.getSetCC(dl, MVT::i1, SUB,
3000 if (CB.
TrueBB == NextBlock(SwitchBB)) {
3016 BrCond =
DAG.getNode(
ISD::BR, dl, MVT::Other, BrCond,
3019 DAG.setRoot(BrCond);
3025 assert(JT.
SL &&
"Should set SDLoc for SelectionDAG!");
3026 assert(JT.
Reg &&
"Should lower JT Header first!");
3027 EVT PTy =
DAG.getTargetLoweringInfo().getJumpTableRegTy(
DAG.getDataLayout());
3031 Index.getValue(1),
Table, Index);
3032 DAG.setRoot(BrJumpTable);
3040 assert(JT.
SL &&
"Should set SDLoc for SelectionDAG!");
3047 DAG.getConstant(JTH.
First, dl, VT));
3062 JT.
Reg = JumpTableReg;
3070 Sub.getValueType()),
3074 MVT::Other, CopyTo, CMP,
3078 if (JT.
MBB != NextBlock(SwitchBB))
3079 BrCond =
DAG.getNode(
ISD::BR, dl, MVT::Other, BrCond,
3080 DAG.getBasicBlock(JT.
MBB));
3082 DAG.setRoot(BrCond);
3085 if (JT.
MBB != NextBlock(SwitchBB))
3087 DAG.getBasicBlock(JT.
MBB)));
3089 DAG.setRoot(CopyTo);
3113 if (PtrTy != PtrMemTy)
3129 auto &
DL =
DAG.getDataLayout();
3138 SDValue StackSlotPtr =
DAG.getFrameIndex(FI, PtrTy);
3145 PtrMemTy, dl,
DAG.getEntryNode(), StackSlotPtr,
3163 assert(GuardCheckFn &&
"Guard check function is null");
3174 Entry.IsInReg =
true;
3175 Args.push_back(Entry);
3181 getValue(GuardCheckFn), std::move(Args));
3183 std::pair<SDValue, SDValue> Result = TLI.
LowerCallTo(CLI);
3184 DAG.setRoot(Result.second);
3196 Guard =
DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3202 Guard =
DAG.getPOISON(PtrMemTy);
3211 Guard =
DAG.getLoad(PtrMemTy, dl, Chain, GuardPtr,
3217 Guard =
DAG.getPOISON(PtrMemTy);
3263 auto &
DL =
DAG.getDataLayout();
3271 SDValue StackSlotPtr =
DAG.getFrameIndex(FI, PtrTy);
3277 PtrMemTy, dl,
DAG.getEntryNode(), StackSlotPtr,
3292 if (GuardCheckFn->hasParamAttribute(0, Attribute::AttrKind::InReg))
3293 Entry.IsInReg =
true;
3294 Args.push_back(Entry);
3300 getValue(GuardCheckFn), std::move(Args));
3306 Chain = TLI.
makeLibCall(
DAG, RTLIB::STACKPROTECTOR_CHECK_FAIL, MVT::isVoid,
3329 DAG.getNode(
ISD::SUB, dl, VT, SwitchOp,
DAG.getConstant(
B.First, dl, VT));
3333 bool UsePtrType =
false;
3357 if (!
B.FallthroughUnreachable)
3358 addSuccessorWithProb(SwitchBB,
B.Default,
B.DefaultProb);
3359 addSuccessorWithProb(SwitchBB,
MBB,
B.Prob);
3363 if (!
B.FallthroughUnreachable) {
3372 DAG.getBasicBlock(
B.Default));
3376 if (
MBB != NextBlock(SwitchBB))
3394 if (PopCount == 1) {
3401 }
else if (PopCount == BB.
Range) {
3409 DAG.getConstant(1, dl, VT), ShiftOp);
3413 VT, SwitchVal,
DAG.getConstant(
B.Mask, dl, VT));
3420 addSuccessorWithProb(SwitchBB,
B.TargetBB,
B.ExtraProb);
3422 addSuccessorWithProb(SwitchBB, NextMBB, BranchProbToNext);
3430 Cmp,
DAG.getBasicBlock(
B.TargetBB));
3433 if (NextMBB != NextBlock(SwitchBB))
3434 BrAnd =
DAG.getNode(
ISD::BR, dl, MVT::Other, BrAnd,
3435 DAG.getBasicBlock(NextMBB));
3440void SelectionDAGBuilder::visitInvoke(
const InvokeInst &
I) {
3458 const Value *Callee(
I.getCalledOperand());
3461 visitInlineAsm(
I, EHPadBB);
3466 case Intrinsic::donothing:
3468 case Intrinsic::seh_try_begin:
3469 case Intrinsic::seh_scope_begin:
3470 case Intrinsic::seh_try_end:
3471 case Intrinsic::seh_scope_end:
3477 case Intrinsic::experimental_patchpoint_void:
3478 case Intrinsic::experimental_patchpoint:
3479 visitPatchpoint(
I, EHPadBB);
3481 case Intrinsic::experimental_gc_statepoint:
3487 case Intrinsic::wasm_throw: {
3489 std::array<SDValue, 4>
Ops = {
3500 case Intrinsic::wasm_rethrow: {
3501 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
3502 std::array<SDValue, 2>
Ops = {
3511 }
else if (
I.hasDeoptState()) {
3532 BranchProbabilityInfo *BPI =
FuncInfo.BPI;
3533 BranchProbability EHPadBBProb =
3539 addSuccessorWithProb(InvokeMBB, Return);
3540 for (
auto &UnwindDest : UnwindDests) {
3541 UnwindDest.first->setIsEHPad();
3542 addSuccessorWithProb(InvokeMBB, UnwindDest.first, UnwindDest.second);
3548 DAG.getBasicBlock(Return)));
3557void SelectionDAGBuilder::visitCallBrIntrinsic(
const CallBrInst &
I) {
3560 DAG.getTargetLoweringInfo().getTgtMemIntrinsic(
3561 Infos,
I,
DAG.getMachineFunction(),
I.getIntrinsicID());
3562 assert(Infos.
empty() &&
"Intrinsic touches memory");
3565 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(
I);
3568 getTargetIntrinsicOperands(
I, HasChain, OnlyLoad);
3569 SDVTList VTs = getTargetIntrinsicVTList(
I, HasChain);
3573 getTargetNonMemIntrinsicNode(*
I.getType(), HasChain,
Ops, VTs);
3574 Result = handleTargetIntrinsicRet(
I, HasChain, OnlyLoad, Result);
3579void SelectionDAGBuilder::visitCallBr(
const CallBrInst &
I) {
3580 MachineBasicBlock *CallBrMBB =
FuncInfo.MBB;
3582 if (
I.isInlineAsm()) {
3589 assert(!
I.hasOperandBundles() &&
3590 "Can't have operand bundles for intrinsics");
3591 visitCallBrIntrinsic(
I);
3596 SmallPtrSet<BasicBlock *, 8> Dests;
3597 Dests.
insert(
I.getDefaultDest());
3607 if (
I.isInlineAsm()) {
3608 for (BasicBlock *Dest :
I.getIndirectDests()) {
3610 Target->setIsInlineAsmBrIndirectTarget();
3616 Target->setLabelMustBeEmitted();
3618 if (Dests.
insert(Dest).second)
3627 DAG.getBasicBlock(Return)));
3630void SelectionDAGBuilder::visitResume(
const ResumeInst &RI) {
3631 llvm_unreachable(
"SelectionDAGBuilder shouldn't visit resume instructions!");
3634void SelectionDAGBuilder::visitLandingPad(
const LandingPadInst &LP) {
3636 "Call to landingpad not in landing pad!");
3640 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
3658 assert(ValueVTs.
size() == 2 &&
"Only two-valued landingpads are supported");
3663 if (
FuncInfo.ExceptionPointerVirtReg) {
3664 Ops[0] =
DAG.getZExtOrTrunc(
3665 DAG.getCopyFromReg(
DAG.getEntryNode(), dl,
3672 Ops[1] =
DAG.getZExtOrTrunc(
3673 DAG.getCopyFromReg(
DAG.getEntryNode(), dl,
3680 DAG.getVTList(ValueVTs),
Ops);
3688 if (JTB.first.HeaderBB ==
First)
3689 JTB.first.HeaderBB =
Last;
3702 for (
unsigned i = 0, e =
I.getNumSuccessors(); i != e; ++i) {
3704 bool Inserted =
Done.insert(BB).second;
3709 addSuccessorWithProb(IndirectBrMBB, Succ);
3719 if (!
I.shouldLowerToTrap(
DAG.getTarget().Options.TrapUnreachable,
3720 DAG.getTarget().Options.NoTrapAfterNoreturn))
3726void SelectionDAGBuilder::visitUnary(
const User &
I,
unsigned Opcode) {
3729 Flags.copyFMF(*FPOp);
3737void SelectionDAGBuilder::visitBinary(
const User &
I,
unsigned Opcode) {
3740 Flags.setNoSignedWrap(OFBinOp->hasNoSignedWrap());
3741 Flags.setNoUnsignedWrap(OFBinOp->hasNoUnsignedWrap());
3744 Flags.setExact(ExactOp->isExact());
3746 Flags.setDisjoint(DisjointOp->isDisjoint());
3748 Flags.copyFMF(*FPOp);
3757void SelectionDAGBuilder::visitShift(
const User &
I,
unsigned Opcode) {
3761 EVT ShiftTy =
DAG.getTargetLoweringInfo().getShiftAmountTy(
3766 if (!
I.getType()->isVectorTy() && Op2.
getValueType() != ShiftTy) {
3768 "Unexpected shift type");
3778 if (
const OverflowingBinaryOperator *OFBinOp =
3780 nuw = OFBinOp->hasNoUnsignedWrap();
3781 nsw = OFBinOp->hasNoSignedWrap();
3783 if (
const PossiblyExactOperator *ExactOp =
3785 exact = ExactOp->isExact();
3788 Flags.setExact(exact);
3789 Flags.setNoSignedWrap(nsw);
3790 Flags.setNoUnsignedWrap(nuw);
3796void SelectionDAGBuilder::visitSDiv(
const User &
I) {
3807void SelectionDAGBuilder::visitICmp(
const ICmpInst &
I) {
3813 auto &TLI =
DAG.getTargetLoweringInfo();
3826 Flags.setSameSign(
I.hasSameSign());
3828 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
3834void SelectionDAGBuilder::visitFCmp(
const FCmpInst &
I) {
3841 if (FPMO->hasNoNaNs() ||
3842 (
DAG.isKnownNeverNaN(Op1) &&
DAG.isKnownNeverNaN(Op2)))
3846 Flags.copyFMF(*FPMO);
3848 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
3858 return isa<SelectInst>(V);
3862void SelectionDAGBuilder::visitSelect(
const User &
I) {
3866 unsigned NumValues = ValueVTs.
size();
3867 if (NumValues == 0)
return;
3877 bool IsUnaryAbs =
false;
3878 bool Negate =
false;
3882 Flags.copyFMF(*FPOp);
3884 Flags.setUnpredictable(
3889 EVT VT = ValueVTs[0];
3890 LLVMContext &Ctx = *
DAG.getContext();
3891 auto &TLI =
DAG.getTargetLoweringInfo();
3901 bool UseScalarMinMax = VT.
isVector() &&
3910 switch (SPR.Flavor) {
3919 switch (SPR.NaNBehavior) {
3926 Flags.setNoSignedZeros(
true);
3934 switch (SPR.NaNBehavior) {
3941 Flags.setNoSignedZeros(
true);
3977 for (
unsigned i = 0; i != NumValues; ++i) {
3986 for (
unsigned i = 0; i != NumValues; ++i) {
4000void SelectionDAGBuilder::visitTrunc(
const User &
I) {
4003 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4007 Flags.setNoSignedWrap(Trunc->hasNoSignedWrap());
4008 Flags.setNoUnsignedWrap(Trunc->hasNoUnsignedWrap());
4014void SelectionDAGBuilder::visitZExt(
const User &
I) {
4018 auto &TLI =
DAG.getTargetLoweringInfo();
4023 Flags.setNonNeg(PNI->hasNonNeg());
4028 if (
Flags.hasNonNeg() &&
4037void SelectionDAGBuilder::visitSExt(
const User &
I) {
4041 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4046void SelectionDAGBuilder::visitFPTrunc(
const User &
I) {
4052 Flags.copyFMF(*FPOp);
4053 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4056 DAG.getTargetConstant(
4061void SelectionDAGBuilder::visitFPExt(
const User &
I) {
4064 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4068 Flags.copyFMF(*FPOp);
4072void SelectionDAGBuilder::visitFPToUI(
const User &
I) {
4075 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4080void SelectionDAGBuilder::visitFPToSI(
const User &
I) {
4083 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4088void SelectionDAGBuilder::visitUIToFP(
const User &
I) {
4091 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4100void SelectionDAGBuilder::visitSIToFP(
const User &
I) {
4103 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4111void SelectionDAGBuilder::visitPtrToAddr(
const User &
I) {
4114 const auto &TLI =
DAG.getTargetLoweringInfo();
4122void SelectionDAGBuilder::visitPtrToInt(
const User &
I) {
4126 auto &TLI =
DAG.getTargetLoweringInfo();
4127 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4136void SelectionDAGBuilder::visitIntToPtr(
const User &
I) {
4140 auto &TLI =
DAG.getTargetLoweringInfo();
4148void SelectionDAGBuilder::visitBitCast(
const User &
I) {
4151 EVT DestVT =
DAG.getTargetLoweringInfo().getValueType(
DAG.getDataLayout(),
4156 if (DestVT !=
N.getValueType())
4164 setValue(&
I,
DAG.getConstant(
C->getValue(), dl, DestVT,
false,
4170void SelectionDAGBuilder::visitAddrSpaceCast(
const User &
I) {
4171 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4172 const Value *SV =
I.getOperand(0);
4177 unsigned DestAS =
I.getType()->getPointerAddressSpace();
4179 if (!TM.isNoopAddrSpaceCast(SrcAS, DestAS))
4185void SelectionDAGBuilder::visitInsertElement(
const User &
I) {
4186 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4193 InVec, InVal, InIdx));
4196void SelectionDAGBuilder::visitExtractElement(
const User &
I) {
4197 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4206void SelectionDAGBuilder::visitShuffleVector(
const User &
I) {
4211 Mask = SVI->getShuffleMask();
4215 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4223 DAG.getVectorIdxConstant(0,
DL));
4234 unsigned MaskNumElts =
Mask.size();
4236 if (SrcNumElts == MaskNumElts) {
4242 if (SrcNumElts < MaskNumElts) {
4246 if (MaskNumElts % SrcNumElts == 0) {
4250 unsigned NumConcat = MaskNumElts / SrcNumElts;
4251 bool IsConcat =
true;
4252 SmallVector<int, 8> ConcatSrcs(NumConcat, -1);
4253 for (
unsigned i = 0; i != MaskNumElts; ++i) {
4259 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
4260 (ConcatSrcs[i / SrcNumElts] >= 0 &&
4261 ConcatSrcs[i / SrcNumElts] != (
int)(Idx / SrcNumElts))) {
4266 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
4273 for (
auto Src : ConcatSrcs) {
4286 unsigned PaddedMaskNumElts =
alignTo(MaskNumElts, SrcNumElts);
4287 unsigned NumConcat = PaddedMaskNumElts / SrcNumElts;
4303 SmallVector<int, 8> MappedOps(PaddedMaskNumElts, -1);
4304 for (
unsigned i = 0; i != MaskNumElts; ++i) {
4306 if (Idx >= (
int)SrcNumElts)
4307 Idx -= SrcNumElts - PaddedMaskNumElts;
4315 if (MaskNumElts != PaddedMaskNumElts)
4317 DAG.getVectorIdxConstant(0,
DL));
4323 assert(SrcNumElts > MaskNumElts);
4327 int StartIdx[2] = {-1, -1};
4328 bool CanExtract =
true;
4329 for (
int Idx : Mask) {
4334 if (Idx >= (
int)SrcNumElts) {
4342 int NewStartIdx =
alignDown(Idx, MaskNumElts);
4343 if (NewStartIdx + MaskNumElts > SrcNumElts ||
4344 (StartIdx[Input] >= 0 && StartIdx[Input] != NewStartIdx))
4348 StartIdx[Input] = NewStartIdx;
4351 if (StartIdx[0] < 0 && StartIdx[1] < 0) {
4357 for (
unsigned Input = 0; Input < 2; ++Input) {
4358 SDValue &Src = Input == 0 ? Src1 : Src2;
4359 if (StartIdx[Input] < 0)
4360 Src =
DAG.getUNDEF(VT);
4363 DAG.getVectorIdxConstant(StartIdx[Input],
DL));
4368 SmallVector<int, 8> MappedOps(Mask);
4369 for (
int &Idx : MappedOps) {
4370 if (Idx >= (
int)SrcNumElts)
4371 Idx -= SrcNumElts + StartIdx[1] - MaskNumElts;
4376 setValue(&
I,
DAG.getVectorShuffle(VT,
DL, Src1, Src2, MappedOps));
4385 for (
int Idx : Mask) {
4389 Res =
DAG.getUNDEF(EltVT);
4391 SDValue &Src = Idx < (int)SrcNumElts ? Src1 : Src2;
4392 if (Idx >= (
int)SrcNumElts) Idx -= SrcNumElts;
4395 DAG.getVectorIdxConstant(Idx,
DL));
4405 ArrayRef<unsigned> Indices =
I.getIndices();
4406 const Value *Op0 =
I.getOperand(0);
4408 Type *AggTy =
I.getType();
4415 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4421 unsigned NumAggValues = AggValueVTs.
size();
4422 unsigned NumValValues = ValValueVTs.
size();
4426 if (!NumAggValues) {
4434 for (; i != LinearIndex; ++i)
4435 Values[i] = IntoUndef ?
DAG.getUNDEF(AggValueVTs[i]) :
4440 for (; i != LinearIndex + NumValValues; ++i)
4441 Values[i] = FromUndef ?
DAG.getUNDEF(AggValueVTs[i]) :
4445 for (; i != NumAggValues; ++i)
4446 Values[i] = IntoUndef ?
DAG.getUNDEF(AggValueVTs[i]) :
4454 ArrayRef<unsigned> Indices =
I.getIndices();
4455 const Value *Op0 =
I.getOperand(0);
4457 Type *ValTy =
I.getType();
4462 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4466 unsigned NumValValues = ValValueVTs.
size();
4469 if (!NumValValues) {
4478 for (
unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
4479 Values[i - LinearIndex] =
4488void SelectionDAGBuilder::visitGetElementPtr(
const User &
I) {
4489 Value *Op0 =
I.getOperand(0);
4495 auto &TLI =
DAG.getTargetLoweringInfo();
4500 bool IsVectorGEP =
I.getType()->isVectorTy();
4501 ElementCount VectorElementCount =
4507 const Value *Idx = GTI.getOperand();
4508 if (StructType *StTy = GTI.getStructTypeOrNull()) {
4513 DAG.getDataLayout().getStructLayout(StTy)->getElementOffset(
Field);
4523 N =
DAG.getMemBasePlusOffset(
4524 N,
DAG.getConstant(
Offset, dl,
N.getValueType()), dl, Flags);
4530 unsigned IdxSize =
DAG.getDataLayout().getIndexSizeInBits(AS);
4532 TypeSize ElementSize =
4533 GTI.getSequentialElementStride(
DAG.getDataLayout());
4538 bool ElementScalable = ElementSize.
isScalable();
4544 C =
C->getSplatValue();
4547 if (CI && CI->isZero())
4549 if (CI && !ElementScalable) {
4550 APInt Offs = ElementMul * CI->getValue().sextOrTrunc(IdxSize);
4553 if (
N.getValueType().isVector())
4554 OffsVal =
DAG.getConstant(
4557 OffsVal =
DAG.getConstant(Offs, dl, IdxTy);
4564 Flags.setNoUnsignedWrap(
true);
4567 OffsVal =
DAG.getSExtOrTrunc(OffsVal, dl,
N.getValueType());
4569 N =
DAG.getMemBasePlusOffset(
N, OffsVal, dl, Flags);
4577 if (
N.getValueType().isVector()) {
4579 VectorElementCount);
4580 IdxN =
DAG.getSplat(VT, dl, IdxN);
4584 N =
DAG.getSplat(VT, dl,
N);
4590 IdxN =
DAG.getSExtOrTrunc(IdxN, dl,
N.getValueType());
4592 SDNodeFlags ScaleFlags;
4601 if (ElementScalable) {
4602 EVT VScaleTy =
N.getValueType().getScalarType();
4605 DAG.getConstant(ElementMul.getZExtValue(), dl, VScaleTy));
4606 if (
N.getValueType().isVector())
4607 VScale =
DAG.getSplatVector(
N.getValueType(), dl, VScale);
4608 IdxN =
DAG.getNode(
ISD::MUL, dl,
N.getValueType(), IdxN, VScale,
4613 if (ElementMul != 1) {
4614 if (ElementMul.isPowerOf2()) {
4615 unsigned Amt = ElementMul.logBase2();
4618 DAG.getShiftAmountConstant(Amt,
N.getValueType(), dl),
4621 SDValue Scale =
DAG.getConstant(ElementMul.getZExtValue(), dl,
4623 IdxN =
DAG.getNode(
ISD::MUL, dl,
N.getValueType(), IdxN, Scale,
4633 SDNodeFlags AddFlags;
4637 N =
DAG.getMemBasePlusOffset(
N, IdxN, dl, AddFlags);
4641 if (IsVectorGEP && !
N.getValueType().isVector()) {
4643 N =
DAG.getSplat(VT, dl,
N);
4654 N =
DAG.getPtrExtendInReg(
N, dl, PtrMemTy);
4659void SelectionDAGBuilder::visitAlloca(
const AllocaInst &
I) {
4666 Type *Ty =
I.getAllocatedType();
4667 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4668 auto &
DL =
DAG.getDataLayout();
4669 TypeSize TySize =
DL.getTypeAllocSize(Ty);
4676 AllocSize =
DAG.getZExtOrTrunc(AllocSize, dl, IntPtr);
4678 AllocSize =
DAG.getNode(
4680 DAG.getZExtOrTrunc(
DAG.getTypeSize(dl, MVT::i64, TySize), dl, IntPtr));
4685 Align StackAlign =
DAG.getSubtarget().getFrameLowering()->getStackAlign();
4686 if (*Alignment <= StackAlign)
4694 DAG.getConstant(StackAlignMask, dl, IntPtr),
4699 DAG.getSignedConstant(~StackAlignMask, dl, IntPtr));
4703 DAG.getConstant(Alignment ?
Alignment->value() : 0, dl, IntPtr)};
4713 return I.getMetadata(LLVMContext::MD_range);
4718 if (std::optional<ConstantRange> CR = CB->getRange())
4722 return std::nullopt;
4727 return CB->getRetNoFPClass();
4731void SelectionDAGBuilder::visitLoad(
const LoadInst &
I) {
4733 return visitAtomicLoad(
I);
4735 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4736 const Value *SV =
I.getOperand(0);
4741 if (Arg->hasSwiftErrorAttr())
4742 return visitLoadFromSwiftError(
I);
4746 if (Alloca->isSwiftError())
4747 return visitLoadFromSwiftError(
I);
4753 Type *Ty =
I.getType();
4757 unsigned NumValues = ValueVTs.
size();
4762 AAMDNodes AAInfo =
I.getAAMetadata();
4765 bool isVolatile =
I.isVolatile();
4770 bool ConstantMemory =
false;
4777 BatchAA->pointsToConstantMemory(MemoryLocation(
4782 Root =
DAG.getEntryNode();
4783 ConstantMemory =
true;
4787 Root =
DAG.getRoot();
4798 unsigned ChainI = 0;
4799 for (
unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4815 MachinePointerInfo PtrInfo =
4817 ? MachinePointerInfo(SV, Offsets[i].getKnownMinValue())
4818 : MachinePointerInfo();
4820 SDValue A =
DAG.getObjectPtrOffset(dl, Ptr, Offsets[i]);
4822 DAG.getLoad(MemVTs[i], dl, Root,
A, PtrInfo, Alignment, MMOFlags,
4823 MMOMetadata(AAInfo, Ranges, MemCacheHint));
4824 Chains[ChainI] =
L.getValue(1);
4826 if (MemVTs[i] != ValueVTs[i])
4827 L =
DAG.getPtrExtOrTrunc(L, dl, ValueVTs[i]);
4829 if (MDNode *NoFPClassMD =
I.getMetadata(LLVMContext::MD_nofpclass)) {
4830 uint64_t FPTestInt =
4832 cast<ConstantAsMetadata>(NoFPClassMD->getOperand(0))->getValue())
4834 if (FPTestInt != fcNone) {
4835 SDValue FPTestConst =
4836 DAG.getTargetConstant(FPTestInt, SDLoc(), MVT::i32);
4837 L = DAG.getNode(ISD::AssertNoFPClass, dl, L.getValueType(), L,
4844 if (!ConstantMemory) {
4850 PendingLoads.push_back(Chain);
4854 DAG.getVTList(ValueVTs),
Values));
4857void SelectionDAGBuilder::visitStoreToSwiftError(
const StoreInst &
I) {
4858 assert(
DAG.getTargetLoweringInfo().supportSwiftError() &&
4859 "call visitStoreToSwiftError when backend supports swifterror");
4862 SmallVector<uint64_t, 4>
Offsets;
4863 const Value *SrcV =
I.getOperand(0);
4865 SrcV->
getType(), ValueVTs,
nullptr, &Offsets, 0);
4866 assert(ValueVTs.
size() == 1 && Offsets[0] == 0 &&
4867 "expect a single EVT for swifterror");
4876 SDValue(Src.getNode(), Src.getResNo()));
4877 DAG.setRoot(CopyNode);
4880void SelectionDAGBuilder::visitLoadFromSwiftError(
const LoadInst &
I) {
4881 assert(
DAG.getTargetLoweringInfo().supportSwiftError() &&
4882 "call visitLoadFromSwiftError when backend supports swifterror");
4885 !
I.hasMetadata(LLVMContext::MD_nontemporal) &&
4886 !
I.hasMetadata(LLVMContext::MD_invariant_load) &&
4887 "Support volatile, non temporal, invariant for load_from_swift_error");
4889 const Value *SV =
I.getOperand(0);
4890 Type *Ty =
I.getType();
4893 !
BatchAA->pointsToConstantMemory(MemoryLocation(
4895 I.getAAMetadata()))) &&
4896 "load_from_swift_error should not be constant memory");
4899 SmallVector<uint64_t, 4>
Offsets;
4901 ValueVTs,
nullptr, &Offsets, 0);
4902 assert(ValueVTs.
size() == 1 && Offsets[0] == 0 &&
4903 "expect a single EVT for swifterror");
4913void SelectionDAGBuilder::visitStore(
const StoreInst &
I) {
4915 return visitAtomicStore(
I);
4917 const Value *SrcV =
I.getOperand(0);
4918 const Value *PtrV =
I.getOperand(1);
4920 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
4925 if (Arg->hasSwiftErrorAttr())
4926 return visitStoreToSwiftError(
I);
4930 if (Alloca->isSwiftError())
4931 return visitStoreToSwiftError(
I);
4938 SrcV->
getType(), ValueVTs, &MemVTs, &Offsets);
4939 unsigned NumValues = ValueVTs.
size();
4953 AAMDNodes AAInfo =
I.getAAMetadata();
4954 const MDNode *MemCacheHint =
4959 unsigned ChainI = 0;
4960 for (
unsigned i = 0; i != NumValues; ++i, ++ChainI) {
4970 MachinePointerInfo PtrInfo =
4972 ? MachinePointerInfo(PtrV, Offsets[i].getKnownMinValue())
4973 : MachinePointerInfo();
4977 if (MemVTs[i] != ValueVTs[i])
4978 Val =
DAG.getPtrExtOrTrunc(Val, dl, MemVTs[i]);
4980 DAG.getStore(Root, dl, Val,
Add, PtrInfo, Alignment, MMOFlags,
4981 MMOMetadata(AAInfo,
nullptr, MemCacheHint));
4982 Chains[ChainI] = St;
4988 DAG.setRoot(StoreNode);
4991void SelectionDAGBuilder::visitMaskedStore(
const CallInst &
I,
4992 bool IsCompressing) {
4995 Value *Src0Operand =
I.getArgOperand(0);
4996 Value *PtrOperand =
I.getArgOperand(1);
4997 Value *MaskOperand =
I.getArgOperand(2);
5007 const auto &TLI =
DAG.getTargetLoweringInfo();
5011 if (
I.hasMetadata(LLVMContext::MD_nontemporal))
5014 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
5015 MachinePointerInfo(PtrOperand), MMOFlags,
5020 !IsCompressing &&
TTI->hasConditionalLoadStoreForType(
5021 I.getArgOperand(0)->getType(),
true)
5027 DAG.setRoot(StoreNode);
5057 C =
C->getSplatValue();
5071 if (!
GEP ||
GEP->getParent() != CurBB)
5074 if (
GEP->getNumOperands() != 2)
5077 const Value *BasePtr =
GEP->getPointerOperand();
5078 const Value *IndexVal =
GEP->getOperand(
GEP->getNumOperands() - 1);
5084 TypeSize ScaleVal =
DL.getTypeAllocSize(
GEP->getResultElementType());
5089 if (ScaleVal != 1 &&
5101void SelectionDAGBuilder::visitMaskedScatter(
const CallInst &
I) {
5105 const Value *Ptr =
I.getArgOperand(1);
5110 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5119 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
5129 EVT IdxVT =
Index.getValueType();
5137 SDValue Scatter =
DAG.getMaskedScatter(
DAG.getVTList(MVT::Other), VT, sdl,
5139 DAG.setRoot(Scatter);
5143void SelectionDAGBuilder::visitMaskedLoad(
const CallInst &
I,
bool IsExpanding) {
5146 Value *PtrOperand =
I.getArgOperand(0);
5147 Value *MaskOperand =
I.getArgOperand(1);
5148 Value *Src0Operand =
I.getArgOperand(2);
5157 AAMDNodes AAInfo =
I.getAAMetadata();
5164 SDValue InChain = AddToChain ?
DAG.getRoot() :
DAG.getEntryNode();
5166 const auto &TLI =
DAG.getTargetLoweringInfo();
5170 if (
I.hasMetadata(LLVMContext::MD_nontemporal))
5172 if (
I.hasMetadata(LLVMContext::MD_invariant_load))
5175 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
5176 MachinePointerInfo(PtrOperand), MMOFlags,
5178 MMOMetadata(AAInfo, Ranges));
5185 TTI->hasConditionalLoadStoreForType(Src0Operand->
getType(),
5190 DAG.getMaskedLoad(VT, sdl, InChain, Ptr,
Offset, Mask, Src0, VT, MMO,
5197void SelectionDAGBuilder::visitMaskedGather(
const CallInst &
I) {
5201 const Value *Ptr =
I.getArgOperand(0);
5205 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5218 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
5221 MMOMetadata(
I.getAAMetadata(), Ranges));
5230 EVT IdxVT =
Index.getValueType();
5239 DAG.getMaskedGather(
DAG.getVTList(VT, MVT::Other), VT, sdl,
Ops, MMO,
5255 SDVTList VTs =
DAG.getVTList(MemVT, MVT::i1, MVT::Other);
5257 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5262 MachinePointerInfo(
I.getPointerOperand()), Flags, MemVT.
getStoreSize(),
5263 I.getAlign(), MMOMetadata(), SSID, SuccessOrdering, FailureOrdering);
5266 dl, MemVT, VTs, InChain,
5274 DAG.setRoot(OutChain);
5277void SelectionDAGBuilder::visitAtomicRMW(
const AtomicRMWInst &
I) {
5280 switch (
I.getOperation()) {
5328 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5333 MachinePointerInfo(
I.getPointerOperand()), Flags, MemVT.
getStoreSize(),
5334 I.getAlign(), MMOMetadata(), SSID, Ordering);
5337 DAG.getAtomic(NT, dl, MemVT, InChain,
5344 DAG.setRoot(OutChain);
5347void SelectionDAGBuilder::visitFence(
const FenceInst &
I) {
5349 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5352 Ops[1] =
DAG.getTargetConstant((
unsigned)
I.getOrdering(), dl,
5354 Ops[2] =
DAG.getTargetConstant(
I.getSyncScopeID(), dl,
5361void SelectionDAGBuilder::visitAtomicLoad(
const LoadInst &
I) {
5368 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5379 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
5380 MachinePointerInfo(
I.getPointerOperand()), Flags, MemVT.
getStoreSize(),
5381 I.getAlign(), MMOMetadata(AAMDNodes(), Ranges), SSID, Order);
5391 L =
DAG.getPtrExtOrTrunc(L, dl, VT);
5394 DAG.setRoot(OutChain);
5397void SelectionDAGBuilder::visitAtomicStore(
const StoreInst &
I) {
5405 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5417 MachinePointerInfo(
I.getPointerOperand()), Flags, MemVT.
getStoreSize(),
5418 I.getAlign(), MMOMetadata(), SSID, Ordering);
5422 Val =
DAG.getPtrExtOrTrunc(Val, dl, MemVT);
5429 DAG.setRoot(OutChain);
5437std::pair<bool, bool>
5438SelectionDAGBuilder::getTargetIntrinsicCallProperties(
const CallBase &
I) {
5440 bool HasChain = !
F->doesNotAccessMemory();
5442 HasChain &&
F->onlyReadsMemory() &&
F->willReturn() &&
F->doesNotThrow();
5444 return {HasChain, OnlyLoad};
5448 const CallBase &
I,
bool HasChain,
bool OnlyLoad,
5450 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5457 Ops.push_back(
DAG.getRoot());
5470 for (
unsigned i = 0, e =
I.arg_size(); i != e; ++i) {
5471 const Value *Arg =
I.getArgOperand(i);
5472 if (!
I.paramHasAttr(i, Attribute::ImmArg)) {
5480 assert(CI->getBitWidth() <= 64 &&
5481 "large intrinsic immediates not handled");
5482 Ops.push_back(
DAG.getTargetConstant(*CI, SDLoc(), VT));
5489 if (std::optional<OperandBundleUse> Bundle =
5491 auto *Sym = Bundle->Inputs[0].get();
5494 Ops.push_back(SDSym);
5497 if (std::optional<OperandBundleUse> Bundle =
5499 Value *Token = Bundle->Inputs[0].get();
5501 assert(
Ops.back().getValueType() != MVT::Glue &&
5502 "Did not expect another glue node here.");
5505 Ops.push_back(ConvControlToken);
5513 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5521 return DAG.getVTList(ValueVTs);
5525SDValue SelectionDAGBuilder::getTargetNonMemIntrinsicNode(
5548 if (
I.getType()->isVoidTy())
5563void SelectionDAGBuilder::visitTargetIntrinsic(
const CallInst &
I,
5565 auto [HasChain, OnlyLoad] = getTargetIntrinsicCallProperties(
I);
5568 if (!
DAG.getMachineFunction().getSubtarget().isIntrinsicSupported(
5571 DAG.getContext()->diagnose(DiagnosticInfoUnsupportedTargetIntrinsic(
5572 *
I.getFunction(), IntrinsicID,
DL.getDebugLoc()));
5577 if (HasChain && !OnlyLoad)
5586 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
5589 TargetLowering::IntrinsicInfo *
Info = !Infos.
empty() ? &Infos[0] :
nullptr;
5592 getTargetIntrinsicOperands(
I, HasChain, OnlyLoad, Info);
5593 SDVTList VTs = getTargetIntrinsicVTList(
I, HasChain);
5598 Flags.copyFMF(*FPMO);
5599 SelectionDAG::FlagInserter FlagsInserter(
DAG, Flags);
5606 if (!Infos.
empty()) {
5611 for (
const auto &Info : Infos) {
5614 MachinePointerInfo MPI;
5616 MPI = MachinePointerInfo(
Info.ptrVal,
Info.offset);
5617 else if (
Info.fallbackAddressSpace)
5618 MPI = MachinePointerInfo(*
Info.fallbackAddressSpace);
5619 EVT MemVT =
Info.memVT;
5621 if (
Size.hasValue() && !
Size.getValue())
5625 MPI,
Info.flags,
Size, Alignment,
I.getAAMetadata(),
Info.ssid,
5633 Result = getTargetNonMemIntrinsicNode(*
I.getType(), HasChain,
Ops, VTs);
5636 Result = handleTargetIntrinsicRet(
I, HasChain, OnlyLoad, Result);
5693 SDValue TwoToFractionalPartOfX;
5770 if (
Op.getValueType() == MVT::f32 &&
5794 if (
Op.getValueType() == MVT::f32 &&
5893 if (
Op.getValueType() == MVT::f32 &&
5977 return DAG.
getNode(
ISD::FADD, dl, MVT::f32, LogOfExponent, Log2ofMantissa);
5990 if (
Op.getValueType() == MVT::f32 &&
6067 return DAG.
getNode(
ISD::FADD, dl, MVT::f32, LogOfExponent, Log10ofMantissa);
6078 if (
Op.getValueType() == MVT::f32 &&
6091 bool IsExp10 =
false;
6092 if (
LHS.getValueType() == MVT::f32 &&
RHS.getValueType() == MVT::f32 &&
6096 IsExp10 = LHSC->isExactlyValue(Ten);
6123 unsigned Val = RHSC->getSExtValue();
6152 CurSquare, CurSquare);
6157 if (RHSC->getSExtValue() < 0)
6171 EVT VT =
LHS.getValueType();
6194 if ((ScaleInt > 0 || (Saturating &&
Signed)) &&
6198 Opcode, VT, ScaleInt);
6233 switch (
N.getOpcode()) {
6237 Op.getValueType().getSizeInBits());
6262bool SelectionDAGBuilder::EmitFuncArgumentDbgValue(
6270 const TargetInstrInfo *
TII =
DAG.getSubtarget().getInstrInfo();
6274 auto MakeVRegDbgValue = [&](
Register Reg, DIExpression *FragExpr,
6279 auto &Inst =
TII->get(TargetOpcode::DBG_INSTR_REF);
6286 auto *NewDIExpr = FragExpr;
6293 return BuildMI(MF,
DL, Inst,
false, MOs, Variable, NewDIExpr);
6296 auto &Inst =
TII->get(TargetOpcode::DBG_VALUE);
6297 return BuildMI(MF,
DL, Inst, Indirect,
Reg, Variable, FragExpr);
6301 if (Kind == FuncArgumentDbgValueKind::Value) {
6306 if (!IsInEntryBlock)
6322 bool VariableIsFunctionInputArg =
Variable->isParameter() &&
6323 !
DL->getInlinedAt();
6325 if (!IsInPrologue && !VariableIsFunctionInputArg)
6359 if (VariableIsFunctionInputArg) {
6361 if (ArgNo >=
FuncInfo.DescribedArgs.size())
6362 FuncInfo.DescribedArgs.resize(ArgNo + 1,
false);
6363 else if (!IsInPrologue &&
FuncInfo.DescribedArgs.test(ArgNo))
6364 return !NodeMap[
V].getNode();
6369 bool IsIndirect =
false;
6370 std::optional<MachineOperand>
Op;
6372 int FI =
FuncInfo.getArgumentFrameIndex(Arg);
6373 if (FI != std::numeric_limits<int>::max())
6377 if (!
Op &&
N.getNode()) {
6380 if (ArgRegsAndSizes.
size() == 1)
6381 Reg = ArgRegsAndSizes.
front().first;
6384 MachineRegisterInfo &RegInfo = MF.
getRegInfo();
6391 IsIndirect =
Kind != FuncArgumentDbgValueKind::Value;
6395 if (!
Op &&
N.getNode()) {
6399 if (FrameIndexSDNode *FINode =
6406 auto splitMultiRegDbgValue =
6419 uint64_t ExprFragmentSizeInBits = ExprFragmentInfo->SizeInBits;
6422 if (
Offset >= ExprFragmentSizeInBits)
6426 if (
Offset + RegFragmentSizeInBits > ExprFragmentSizeInBits) {
6427 RegFragmentSizeInBits = ExprFragmentSizeInBits -
Offset;
6432 Expr,
Offset, RegFragmentSizeInBits);
6436 if (!FragmentExpr) {
6437 SDDbgValue *SDV =
DAG.getConstantDbgValue(
6439 DAG.AddDbgValue(SDV,
false);
6442 MachineInstr *NewMI = MakeVRegDbgValue(
6443 Reg, *FragmentExpr, Kind != FuncArgumentDbgValueKind::Value);
6444 FuncInfo.ArgDbgValues.push_back(NewMI);
6453 if (VMI !=
FuncInfo.ValueMap.end()) {
6454 const auto &TLI =
DAG.getTargetLoweringInfo();
6455 RegsForValue RFV(
V->getContext(), TLI,
DAG.getDataLayout(), VMI->second,
6456 V->getType(), std::nullopt);
6457 if (RFV.occupiesMultipleRegs())
6458 return splitMultiRegDbgValue(RFV.getRegsAndSizes());
6461 IsIndirect =
Kind != FuncArgumentDbgValueKind::Value;
6462 }
else if (ArgRegsAndSizes.
size() > 1) {
6465 return splitMultiRegDbgValue(ArgRegsAndSizes);
6473 "Expected inlined-at fields to agree");
6474 MachineInstr *NewMI =
nullptr;
6477 NewMI = MakeVRegDbgValue(
Op->getReg(), Expr, IsIndirect);
6479 NewMI =
BuildMI(MF,
DL,
TII->get(TargetOpcode::DBG_VALUE),
true, *
Op,
6483 FuncInfo.ArgDbgValues.push_back(NewMI);
6492 unsigned DbgSDNodeOrder) {
6504 return DAG.getFrameIndexDbgValue(Variable, Expr, FISDN->getIndex(),
6505 false, dl, DbgSDNodeOrder);
6507 return DAG.getDbgValue(Variable, Expr,
N.getNode(),
N.getResNo(),
6508 false, dl, DbgSDNodeOrder);
6513 case Intrinsic::smul_fix:
6515 case Intrinsic::umul_fix:
6517 case Intrinsic::smul_fix_sat:
6519 case Intrinsic::umul_fix_sat:
6521 case Intrinsic::sdiv_fix:
6523 case Intrinsic::udiv_fix:
6525 case Intrinsic::sdiv_fix_sat:
6527 case Intrinsic::udiv_fix_sat:
6540 "expected call_preallocated_setup Value");
6541 for (
const auto *U : PreallocatedSetup->
users()) {
6543 const Function *Fn = UseCall->getCalledFunction();
6544 if (!Fn || Fn->
getIntrinsicID() != Intrinsic::call_preallocated_arg) {
6554bool SelectionDAGBuilder::visitEntryValueDbgValue(
6564 auto ArgIt =
FuncInfo.ValueMap.find(Arg);
6565 if (ArgIt ==
FuncInfo.ValueMap.end()) {
6567 dbgs() <<
"Dropping dbg.value: expression is entry_value but "
6568 "couldn't find an associated register for the Argument\n");
6571 Register ArgVReg = ArgIt->getSecond();
6573 for (
auto [PhysReg, VirtReg] :
FuncInfo.RegInfo->liveins())
6574 if (ArgVReg == VirtReg || ArgVReg == PhysReg) {
6575 SDDbgValue *SDV =
DAG.getVRegDbgValue(
6576 Variable, Expr, PhysReg,
false , DbgLoc, SDNodeOrder);
6577 DAG.AddDbgValue(SDV,
false );
6580 LLVM_DEBUG(
dbgs() <<
"Dropping dbg.value: expression is entry_value but "
6581 "couldn't find a physical register\n");
6586void SelectionDAGBuilder::visitConvergenceControl(
const CallInst &
I,
6589 switch (Intrinsic) {
6590 case Intrinsic::experimental_convergence_anchor:
6593 case Intrinsic::experimental_convergence_entry:
6596 case Intrinsic::experimental_convergence_loop: {
6598 auto *Token = Bundle->Inputs[0].get();
6606void SelectionDAGBuilder::visitVectorHistogram(
const CallInst &
I,
6607 unsigned IntrinsicID) {
6610 assert(IntrinsicID == Intrinsic::experimental_vector_histogram_add &&
6611 "Tried to lower unsupported histogram type");
6617 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
6618 DataLayout TargetDL =
DAG.getDataLayout();
6633 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
6634 MachinePointerInfo(AS),
6637 MMOMetadata(
I.getAAMetadata(), Ranges));
6646 EVT IdxVT =
Index.getValueType();
6657 SDValue ID =
DAG.getTargetConstant(IntrinsicID, sdl, MVT::i32);
6660 SDValue Histogram =
DAG.getMaskedHistogram(
DAG.getVTList(MVT::Other), VT, sdl,
6664 DAG.setRoot(Histogram);
6667void SelectionDAGBuilder::visitVectorExtractLastActive(
const CallInst &
I,
6669 assert(Intrinsic == Intrinsic::experimental_vector_extract_last_active &&
6670 "Tried lowering invalid vector extract last");
6672 const DataLayout &Layout =
DAG.getDataLayout();
6676 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
6686 EVT BoolVT =
Mask.getValueType().getScalarType();
6688 Result =
DAG.getSelect(sdl, ResVT, AnyActive, Result, PassThru);
6695void SelectionDAGBuilder::visitIntrinsicCall(
const CallInst &
I,
6697 const TargetLowering &TLI =
DAG.getTargetLoweringInfo();
6704 Flags.copyFMF(*FPOp);
6706 switch (Intrinsic) {
6709 visitTargetIntrinsic(
I, Intrinsic);
6711 case Intrinsic::vscale: {
6716 case Intrinsic::vastart: visitVAStart(
I);
return;
6717 case Intrinsic::vaend: visitVAEnd(
I);
return;
6718 case Intrinsic::vacopy: visitVACopy(
I);
return;
6719 case Intrinsic::returnaddress:
6724 case Intrinsic::addressofreturnaddress:
6729 case Intrinsic::sponentry:
6734 case Intrinsic::frameaddress:
6739 case Intrinsic::read_volatile_register:
6740 case Intrinsic::read_register: {
6741 Value *
Reg =
I.getArgOperand(0);
6747 DAG.getVTList(VT, MVT::Other), Chain,
RegName);
6752 case Intrinsic::write_register: {
6753 Value *
Reg =
I.getArgOperand(0);
6754 Value *RegValue =
I.getArgOperand(1);
6762 case Intrinsic::write_volatile_register: {
6763 Value *
Reg =
I.getArgOperand(0);
6764 Value *RegValue =
I.getArgOperand(1);
6781 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
6782 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysReg);
6783 MVT RegVT = *TRI->legalclasstypes_begin(*RC);
6784 DAG.setRoot(DAG.getNode(ISD::FAKE_USE, sdl, MVT::Other,
6785 {WriteChain, DAG.getRegister(PhysReg, RegVT)}));
6787 DAG.setRoot(WriteChain);
6791 case Intrinsic::memcpy:
6792 case Intrinsic::memcpy_inline: {
6798 "memcpy_inline needs constant size");
6800 Align DstAlign = MCI.getDestAlign().valueOrOne();
6801 Align SrcAlign = MCI.getSourceAlign().valueOrOne();
6802 bool isVol = MCI.isVolatile();
6804 SDValue MC =
DAG.getMemcpy(Root, sdl, Dst, Src,
Size, DstAlign, SrcAlign,
6805 isVol, MCI.isForceInlined(), &
I, std::nullopt,
6806 MachinePointerInfo(
I.getArgOperand(0)),
6807 MachinePointerInfo(
I.getArgOperand(1)),
6809 updateDAGForMaybeTailCall(MC);
6812 case Intrinsic::memset:
6813 case Intrinsic::memset_inline: {
6819 "memset_inline needs constant size");
6821 Align DstAlign = MSII.getDestAlign().valueOrOne();
6822 bool isVol = MSII.isVolatile();
6825 Root, sdl, Dst, Value,
Size, DstAlign, isVol, MSII.isForceInlined(),
6826 &
I, MachinePointerInfo(
I.getArgOperand(0)),
I.getAAMetadata());
6827 updateDAGForMaybeTailCall(MC);
6830 case Intrinsic::memmove: {
6836 Align DstAlign = MMI.getDestAlign().valueOrOne();
6837 Align SrcAlign = MMI.getSourceAlign().valueOrOne();
6838 bool isVol = MMI.isVolatile();
6841 Root, sdl, Op1, Op2, Op3, DstAlign, SrcAlign, isVol, &
I,
6843 MachinePointerInfo(
I.getArgOperand(0)),
6844 MachinePointerInfo(
I.getArgOperand(1)),
I.getAAMetadata(),
BatchAA);
6845 updateDAGForMaybeTailCall(MM);
6848 case Intrinsic::memcpy_element_unordered_atomic: {
6854 Type *LengthTy =
MI.getLength()->getType();
6855 unsigned ElemSz =
MI.getElementSizeInBytes();
6859 isTC, MachinePointerInfo(
MI.getRawDest()),
6860 MachinePointerInfo(
MI.getRawSource()));
6861 updateDAGForMaybeTailCall(MC);
6864 case Intrinsic::memmove_element_unordered_atomic: {
6870 Type *LengthTy =
MI.getLength()->getType();
6871 unsigned ElemSz =
MI.getElementSizeInBytes();
6875 isTC, MachinePointerInfo(
MI.getRawDest()),
6876 MachinePointerInfo(
MI.getRawSource()));
6877 updateDAGForMaybeTailCall(MC);
6880 case Intrinsic::memset_element_unordered_atomic: {
6886 Type *LengthTy =
MI.getLength()->getType();
6887 unsigned ElemSz =
MI.getElementSizeInBytes();
6891 isTC, MachinePointerInfo(
MI.getRawDest()));
6892 updateDAGForMaybeTailCall(MC);
6895 case Intrinsic::call_preallocated_setup: {
6897 SDValue SrcValue =
DAG.getSrcValue(PreallocatedCall);
6904 case Intrinsic::call_preallocated_arg: {
6906 SDValue SrcValue =
DAG.getSrcValue(PreallocatedCall);
6920 case Intrinsic::eh_typeid_for: {
6923 unsigned TypeID =
DAG.getMachineFunction().getTypeIDFor(GV);
6924 Res =
DAG.getConstant(
TypeID, sdl, MVT::i32);
6929 case Intrinsic::eh_return_i32:
6930 case Intrinsic::eh_return_i64:
6931 DAG.getMachineFunction().setCallsEHReturn(
true);
6938 case Intrinsic::eh_unwind_init:
6939 DAG.getMachineFunction().setCallsUnwindInit(
true);
6941 case Intrinsic::eh_dwarf_cfa:
6946 case Intrinsic::eh_sjlj_callsite: {
6948 assert(
FuncInfo.getCurrentCallSite() == 0 &&
"Overlapping call sites!");
6953 case Intrinsic::eh_sjlj_functioncontext: {
6955 MachineFrameInfo &MFI =
DAG.getMachineFunction().getFrameInfo();
6958 int FI =
FuncInfo.StaticAllocaMap[FnCtx];
6962 case Intrinsic::eh_sjlj_setjmp: {
6967 DAG.getVTList(MVT::i32, MVT::Other),
Ops);
6969 DAG.setRoot(
Op.getValue(1));
6972 case Intrinsic::eh_sjlj_longjmp:
6976 case Intrinsic::eh_sjlj_setup_dispatch:
6980 case Intrinsic::masked_gather:
6981 visitMaskedGather(
I);
6983 case Intrinsic::masked_load:
6986 case Intrinsic::masked_scatter:
6987 visitMaskedScatter(
I);
6989 case Intrinsic::masked_store:
6990 visitMaskedStore(
I);
6992 case Intrinsic::masked_expandload:
6993 visitMaskedLoad(
I,
true );
6995 case Intrinsic::masked_compressstore:
6996 visitMaskedStore(
I,
true );
6998 case Intrinsic::powi:
7002 case Intrinsic::log:
7005 case Intrinsic::log2:
7009 case Intrinsic::log10:
7013 case Intrinsic::exp:
7016 case Intrinsic::exp2:
7020 case Intrinsic::pow:
7024 case Intrinsic::sqrt:
7025 case Intrinsic::fabs:
7026 case Intrinsic::sin:
7027 case Intrinsic::cos:
7028 case Intrinsic::tan:
7029 case Intrinsic::asin:
7030 case Intrinsic::acos:
7031 case Intrinsic::atan:
7032 case Intrinsic::sinh:
7033 case Intrinsic::cosh:
7034 case Intrinsic::tanh:
7035 case Intrinsic::exp10:
7036 case Intrinsic::floor:
7037 case Intrinsic::ceil:
7038 case Intrinsic::trunc:
7039 case Intrinsic::rint:
7040 case Intrinsic::nearbyint:
7041 case Intrinsic::round:
7042 case Intrinsic::roundeven:
7043 case Intrinsic::canonicalize: {
7046 switch (Intrinsic) {
7048 case Intrinsic::sqrt: Opcode =
ISD::FSQRT;
break;
7049 case Intrinsic::fabs: Opcode =
ISD::FABS;
break;
7050 case Intrinsic::sin: Opcode =
ISD::FSIN;
break;
7051 case Intrinsic::cos: Opcode =
ISD::FCOS;
break;
7052 case Intrinsic::tan: Opcode =
ISD::FTAN;
break;
7053 case Intrinsic::asin: Opcode =
ISD::FASIN;
break;
7054 case Intrinsic::acos: Opcode =
ISD::FACOS;
break;
7055 case Intrinsic::atan: Opcode =
ISD::FATAN;
break;
7056 case Intrinsic::sinh: Opcode =
ISD::FSINH;
break;
7057 case Intrinsic::cosh: Opcode =
ISD::FCOSH;
break;
7058 case Intrinsic::tanh: Opcode =
ISD::FTANH;
break;
7059 case Intrinsic::exp10: Opcode =
ISD::FEXP10;
break;
7060 case Intrinsic::floor: Opcode =
ISD::FFLOOR;
break;
7061 case Intrinsic::ceil: Opcode =
ISD::FCEIL;
break;
7062 case Intrinsic::trunc: Opcode =
ISD::FTRUNC;
break;
7063 case Intrinsic::rint: Opcode =
ISD::FRINT;
break;
7065 case Intrinsic::round: Opcode =
ISD::FROUND;
break;
7072 getValue(
I.getArgOperand(0)).getValueType(),
7076 case Intrinsic::atan2:
7078 getValue(
I.getArgOperand(0)).getValueType(),
7082 case Intrinsic::lround:
7083 case Intrinsic::llround:
7084 case Intrinsic::lrint:
7085 case Intrinsic::llrint: {
7088 switch (Intrinsic) {
7090 case Intrinsic::lround: Opcode =
ISD::LROUND;
break;
7092 case Intrinsic::lrint: Opcode =
ISD::LRINT;
break;
7093 case Intrinsic::llrint: Opcode =
ISD::LLRINT;
break;
7102 case Intrinsic::minnum:
7104 getValue(
I.getArgOperand(0)).getValueType(),
7108 case Intrinsic::maxnum:
7110 getValue(
I.getArgOperand(0)).getValueType(),
7114 case Intrinsic::minimum:
7116 getValue(
I.getArgOperand(0)).getValueType(),
7120 case Intrinsic::maximum:
7122 getValue(
I.getArgOperand(0)).getValueType(),
7126 case Intrinsic::minimumnum:
7128 getValue(
I.getArgOperand(0)).getValueType(),
7132 case Intrinsic::maximumnum:
7134 getValue(
I.getArgOperand(0)).getValueType(),
7138 case Intrinsic::copysign:
7140 getValue(
I.getArgOperand(0)).getValueType(),
7144 case Intrinsic::ldexp:
7146 getValue(
I.getArgOperand(0)).getValueType(),
7150 case Intrinsic::modf:
7151 case Intrinsic::sincos:
7152 case Intrinsic::sincospi:
7153 case Intrinsic::frexp: {
7155 switch (Intrinsic) {
7158 case Intrinsic::sincos:
7161 case Intrinsic::sincospi:
7164 case Intrinsic::modf:
7167 case Intrinsic::frexp:
7173 SDVTList VTs =
DAG.getVTList(ValueVTs);
7175 &
I,
DAG.getNode(Opcode, sdl, VTs,
getValue(
I.getArgOperand(0)), Flags));
7178 case Intrinsic::arithmetic_fence: {
7180 getValue(
I.getArgOperand(0)).getValueType(),
7184 case Intrinsic::fma:
7190#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
7191 case Intrinsic::INTRINSIC:
7192#include "llvm/IR/ConstrainedOps.def"
7195#define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
7196#include "llvm/IR/VPIntrinsics.def"
7199 case Intrinsic::fptrunc_round: {
7203 std::optional<RoundingMode> RoundMode =
7211 SelectionDAG::FlagInserter FlagsInserter(
DAG, Flags);
7216 DAG.getTargetConstant((
int)*RoundMode, sdl, MVT::i32));
7221 case Intrinsic::fmuladd: {
7226 getValue(
I.getArgOperand(0)).getValueType(),
7233 getValue(
I.getArgOperand(0)).getValueType(),
7249 case Intrinsic::fptosi_sat: {
7256 case Intrinsic::fptoui_sat: {
7263 case Intrinsic::convert_from_arbitrary_fp: {
7268 const fltSemantics *SrcSem =
7271 DAG.getContext()->emitError(
7272 "convert_from_arbitrary_fp: not implemented format '" + FormatStr +
7283 DAG.getTargetConstant(
static_cast<int>(SemEnum), sdl, MVT::i32);
7288 case Intrinsic::convert_to_arbitrary_fp: {
7293 const fltSemantics *DstSem =
7296 DAG.getContext()->emitError(
7297 "convert_to_arbitrary_fp: not implemented format '" + FormatStr +
7309 "Dynamic rounding mode should have been rejected by the verifier");
7317 DAG.getTargetConstant(
static_cast<int>(SemEnum), sdl, MVT::i32);
7319 DAG.getTargetConstant(
static_cast<int>(*RoundMode), sdl, MVT::i32);
7320 SDValue SatConst =
DAG.getTargetConstant(Saturate, sdl, MVT::i32);
7322 SemConst, RoundConst, SatConst));
7325 case Intrinsic::set_rounding:
7331 case Intrinsic::is_fpclass: {
7332 const DataLayout DLayout =
DAG.getDataLayout();
7334 EVT ArgVT = TLI.
getValueType(DLayout,
I.getArgOperand(0)->getType());
7341 Flags.setNoFPExcept(
7342 !
F.getAttributes().hasFnAttr(llvm::Attribute::StrictFP));
7358 case Intrinsic::get_fpenv: {
7359 const DataLayout DLayout =
DAG.getDataLayout();
7361 Align TempAlign =
DAG.getEVTAlign(EnvVT);
7376 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
7379 Chain =
DAG.getGetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7380 Res =
DAG.getLoad(EnvVT, sdl, Chain, Temp, MPI);
7386 case Intrinsic::set_fpenv: {
7387 const DataLayout DLayout =
DAG.getDataLayout();
7390 Align TempAlign =
DAG.getEVTAlign(EnvVT);
7403 Chain =
DAG.getStore(Chain, sdl, Env, Temp, MPI, TempAlign,
7405 MachineMemOperand *MMO =
DAG.getMachineFunction().getMachineMemOperand(
7408 Chain =
DAG.getSetFPEnv(Chain, sdl, Temp, EnvVT, MMO);
7413 case Intrinsic::reset_fpenv:
7416 case Intrinsic::get_fpmode:
7425 case Intrinsic::set_fpmode:
7430 case Intrinsic::reset_fpmode: {
7435 case Intrinsic::pcmarker: {
7440 case Intrinsic::readcyclecounter: {
7443 DAG.getVTList(MVT::i64, MVT::Other),
Op);
7448 case Intrinsic::readsteadycounter: {
7451 DAG.getVTList(MVT::i64, MVT::Other),
Op);
7456 case Intrinsic::bitreverse:
7458 getValue(
I.getArgOperand(0)).getValueType(),
7461 case Intrinsic::bswap:
7463 getValue(
I.getArgOperand(0)).getValueType(),
7466 case Intrinsic::cttz: {
7474 case Intrinsic::ctlz: {
7482 case Intrinsic::ctpop: {
7488 case Intrinsic::fshl:
7489 case Intrinsic::fshr: {
7490 bool IsFSHL =
Intrinsic == Intrinsic::fshl;
7494 EVT VT =
X.getValueType();
7505 case Intrinsic::clmul: {
7511 case Intrinsic::pext: {
7517 case Intrinsic::pdep: {
7523 case Intrinsic::sadd_sat: {
7529 case Intrinsic::uadd_sat: {
7535 case Intrinsic::ssub_sat: {
7541 case Intrinsic::usub_sat: {
7547 case Intrinsic::sshl_sat:
7548 case Intrinsic::ushl_sat: {
7552 EVT ShiftTy =
DAG.getTargetLoweringInfo().getShiftAmountTy(
7557 if (!
I.getType()->isVectorTy() && Op2.
getValueType() != ShiftTy) {
7560 "Unexpected shift type");
7569 case Intrinsic::smul_fix:
7570 case Intrinsic::umul_fix:
7571 case Intrinsic::smul_fix_sat:
7572 case Intrinsic::umul_fix_sat: {
7580 case Intrinsic::sdiv_fix:
7581 case Intrinsic::udiv_fix:
7582 case Intrinsic::sdiv_fix_sat:
7583 case Intrinsic::udiv_fix_sat: {
7588 Op1, Op2, Op3,
DAG, TLI));
7591 case Intrinsic::smax: {
7597 case Intrinsic::smin: {
7603 case Intrinsic::umax: {
7609 case Intrinsic::umin: {
7615 case Intrinsic::abs: {
7622 case Intrinsic::scmp: {
7629 case Intrinsic::ucmp: {
7636 case Intrinsic::stackaddress:
7637 case Intrinsic::stacksave: {
7642 Res =
DAG.getNode(SDOpcode, sdl,
DAG.getVTList(VT, MVT::Other),
Op);
7647 case Intrinsic::stackrestore:
7651 case Intrinsic::get_dynamic_area_offset: {
7660 case Intrinsic::stackguard: {
7667 Res =
DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
7671 LLVMContext &Ctx = *
DAG.getContext();
7672 Ctx.
diagnose(DiagnosticInfoGeneric(
"unable to lower stackguard"));
7679 MachinePointerInfo(
Global, 0), Align,
7691 case Intrinsic::stackprotector: {
7713 Chain, sdl, Src, FIN,
7720 case Intrinsic::objectsize:
7723 case Intrinsic::is_constant:
7726 case Intrinsic::annotation:
7727 case Intrinsic::ptr_annotation:
7728 case Intrinsic::launder_invariant_group:
7729 case Intrinsic::strip_invariant_group:
7734 case Intrinsic::type_test:
7735 case Intrinsic::public_type_test:
7736 case Intrinsic::type_checked_load:
7737 case Intrinsic::type_checked_load_relative: {
7742 DAG.getContext()->diagnose(DiagnosticInfoUnsupported(
7745 " intrinsic must be lowered by the LowerTypeTests pass "
7746 "before code generation",
7755 case Intrinsic::assume:
7756 case Intrinsic::experimental_noalias_scope_decl:
7757 case Intrinsic::var_annotation:
7758 case Intrinsic::sideeffect:
7763 case Intrinsic::codeview_annotation: {
7774 case Intrinsic::init_trampoline: {
7782 Ops[4] =
DAG.getSrcValue(
I.getArgOperand(0));
7790 case Intrinsic::adjust_trampoline:
7795 case Intrinsic::gcroot: {
7796 assert(
DAG.getMachineFunction().getFunction().hasGC() &&
7797 "only valid in functions with gc specified, enforced by Verifier");
7799 const Value *Alloca =
I.getArgOperand(0)->stripPointerCasts();
7806 case Intrinsic::gcread:
7807 case Intrinsic::gcwrite:
7809 case Intrinsic::get_rounding:
7815 case Intrinsic::expect:
7816 case Intrinsic::expect_with_probability:
7822 case Intrinsic::ubsantrap:
7823 case Intrinsic::debugtrap:
7824 case Intrinsic::trap: {
7825 StringRef TrapFuncName =
7826 I.getAttributes().getFnAttr(
"trap-func-name").getValueAsString();
7827 if (TrapFuncName.
empty()) {
7828 switch (Intrinsic) {
7829 case Intrinsic::trap:
7832 case Intrinsic::debugtrap:
7835 case Intrinsic::ubsantrap:
7838 DAG.getTargetConstant(
7844 DAG.addNoMergeSiteInfo(
DAG.getRoot().getNode(),
7845 I.hasFnAttr(Attribute::NoMerge));
7849 if (Intrinsic == Intrinsic::ubsantrap) {
7850 Value *Arg =
I.getArgOperand(0);
7854 TargetLowering::CallLoweringInfo CLI(
DAG);
7855 CLI.setDebugLoc(sdl).setChain(
getRoot()).setLibCallee(
7857 DAG.getExternalSymbol(TrapFuncName.
data(),
7860 CLI.NoMerge =
I.hasFnAttr(Attribute::NoMerge);
7866 case Intrinsic::allow_runtime_check:
7867 case Intrinsic::allow_ubsan_check:
7871 case Intrinsic::uadd_with_overflow:
7872 case Intrinsic::sadd_with_overflow:
7873 case Intrinsic::usub_with_overflow:
7874 case Intrinsic::ssub_with_overflow:
7875 case Intrinsic::umul_with_overflow:
7876 case Intrinsic::smul_with_overflow: {
7878 switch (Intrinsic) {
7880 case Intrinsic::uadd_with_overflow:
Op =
ISD::UADDO;
break;
7881 case Intrinsic::sadd_with_overflow:
Op =
ISD::SADDO;
break;
7882 case Intrinsic::usub_with_overflow:
Op =
ISD::USUBO;
break;
7883 case Intrinsic::ssub_with_overflow:
Op =
ISD::SSUBO;
break;
7884 case Intrinsic::umul_with_overflow:
Op =
ISD::UMULO;
break;
7885 case Intrinsic::smul_with_overflow:
Op =
ISD::SMULO;
break;
7893 SDVTList VTs =
DAG.getVTList(ResultVT, OverflowVT);
7897 case Intrinsic::prefetch: {
7912 std::nullopt, Flags);
7918 DAG.setRoot(Result);
7921 case Intrinsic::lifetime_start:
7922 case Intrinsic::lifetime_end: {
7923 bool IsStart = (
Intrinsic == Intrinsic::lifetime_start);
7929 if (!LifetimeObject)
7934 auto SI =
FuncInfo.StaticAllocaMap.find(LifetimeObject);
7935 if (SI ==
FuncInfo.StaticAllocaMap.end())
7939 Res =
DAG.getLifetimeNode(IsStart, sdl,
getRoot(), FrameIndex);
7943 case Intrinsic::pseudoprobe: {
7951 case Intrinsic::invariant_start:
7956 case Intrinsic::invariant_end:
7959 case Intrinsic::clear_cache: {
7964 {InputChain, StartVal, EndVal});
7969 case Intrinsic::donothing:
7970 case Intrinsic::seh_try_begin:
7971 case Intrinsic::seh_scope_begin:
7972 case Intrinsic::seh_try_end:
7973 case Intrinsic::seh_scope_end:
7976 case Intrinsic::experimental_stackmap:
7979 case Intrinsic::experimental_patchpoint_void:
7980 case Intrinsic::experimental_patchpoint:
7983 case Intrinsic::experimental_gc_statepoint:
7986 case Intrinsic::experimental_gc_result:
7989 case Intrinsic::experimental_gc_relocate:
7992 case Intrinsic::instrprof_cover:
7994 case Intrinsic::instrprof_increment:
7996 case Intrinsic::instrprof_timestamp:
7998 case Intrinsic::instrprof_value_profile:
8000 case Intrinsic::instrprof_mcdc_parameters:
8002 case Intrinsic::instrprof_mcdc_tvbitmap_update:
8004 case Intrinsic::localescape: {
8006 const TargetInstrInfo *
TII =
DAG.getSubtarget().getInstrInfo();
8010 for (
unsigned Idx = 0,
E =
I.arg_size(); Idx <
E; ++Idx) {
8016 "can only escape static allocas");
8021 TII->get(TargetOpcode::LOCAL_ESCAPE))
8029 case Intrinsic::localrecover: {
8037 unsigned(Idx->getLimitedValue(std::numeric_limits<int>::max()));
8041 Value *
FP =
I.getArgOperand(1);
8047 SDValue OffsetSym =
DAG.getMCSymbol(FrameAllocSym, PtrVT);
8052 SDValue Add =
DAG.getMemBasePlusOffset(FPVal, OffsetVal, sdl);
8058 case Intrinsic::fake_use: {
8059 Value *
V =
I.getArgOperand(0);
8064 auto FakeUseValue = [&]() ->
SDValue {
8078 if (!FakeUseValue || FakeUseValue.isUndef())
8081 Ops[1] = FakeUseValue;
8090 case Intrinsic::reloc_none: {
8095 DAG.getTargetExternalSymbol(
8101 case Intrinsic::cond_loop: {
8111 case Intrinsic::eh_exceptionpointer:
8112 case Intrinsic::eh_exceptioncode: {
8118 SDValue N =
DAG.getCopyFromReg(
DAG.getEntryNode(), sdl, VReg, PtrVT);
8119 if (Intrinsic == Intrinsic::eh_exceptioncode)
8120 N =
DAG.getZExtOrTrunc(
N, sdl, MVT::i32);
8124 case Intrinsic::xray_customevent: {
8127 const auto &Triple =
DAG.getTarget().getTargetTriple();
8128 if (!Triple.isAArch64(64) && Triple.getArch() !=
Triple::x86_64 &&
8137 SDVTList NodeTys =
DAG.getVTList(MVT::Other, MVT::Glue);
8139 Ops.push_back(LogEntryVal);
8140 Ops.push_back(StrSizeVal);
8141 Ops.push_back(Chain);
8147 MachineSDNode *MN =
DAG.getMachineNode(TargetOpcode::PATCHABLE_EVENT_CALL,
8150 DAG.setRoot(patchableNode);
8154 case Intrinsic::xray_typedevent: {
8157 const auto &Triple =
DAG.getTarget().getTargetTriple();
8158 if (!Triple.isAArch64(64) && Triple.getArch() !=
Triple::x86_64 &&
8170 SDVTList NodeTys =
DAG.getVTList(MVT::Other, MVT::Glue);
8172 Ops.push_back(LogTypeId);
8173 Ops.push_back(LogEntryVal);
8174 Ops.push_back(StrSizeVal);
8175 Ops.push_back(Chain);
8181 MachineSDNode *MN =
DAG.getMachineNode(
8182 TargetOpcode::PATCHABLE_TYPED_EVENT_CALL, sdl, NodeTys,
Ops);
8184 DAG.setRoot(patchableNode);
8188 case Intrinsic::experimental_deoptimize:
8191 case Intrinsic::stepvector:
8194 case Intrinsic::vector_reduce_fadd:
8195 case Intrinsic::vector_reduce_fmul:
8196 case Intrinsic::vector_reduce_add:
8197 case Intrinsic::vector_reduce_mul:
8198 case Intrinsic::vector_reduce_and:
8199 case Intrinsic::vector_reduce_or:
8200 case Intrinsic::vector_reduce_xor:
8201 case Intrinsic::vector_reduce_smax:
8202 case Intrinsic::vector_reduce_smin:
8203 case Intrinsic::vector_reduce_umax:
8204 case Intrinsic::vector_reduce_umin:
8205 case Intrinsic::vector_reduce_fmax:
8206 case Intrinsic::vector_reduce_fmin:
8207 case Intrinsic::vector_reduce_fmaximum:
8208 case Intrinsic::vector_reduce_fminimum:
8209 case Intrinsic::vector_reduce_fmaximumnum:
8210 case Intrinsic::vector_reduce_fminimumnum:
8211 visitVectorReduce(
I, Intrinsic);
8214 case Intrinsic::icall_branch_funnel: {
8220 I.getArgOperand(1),
Offset,
DAG.getDataLayout()));
8223 "llvm.icall.branch.funnel operand must be a GlobalValue");
8224 Ops.push_back(
DAG.getTargetGlobalAddress(
Base, sdl, MVT::i64, 0));
8226 struct BranchFunnelTarget {
8232 for (
unsigned Op = 1,
N =
I.arg_size();
Op !=
N;
Op += 2) {
8235 if (ElemBase !=
Base)
8237 "to the same GlobalValue");
8243 "llvm.icall.branch.funnel operand must be a GlobalValue");
8249 [](
const BranchFunnelTarget &
T1,
const BranchFunnelTarget &T2) {
8250 return T1.Offset < T2.Offset;
8253 for (
auto &
T : Targets) {
8254 Ops.push_back(
DAG.getTargetConstant(
T.Offset, sdl, MVT::i32));
8255 Ops.push_back(
T.Target);
8258 Ops.push_back(
DAG.getRoot());
8259 SDValue N(
DAG.getMachineNode(TargetOpcode::ICALL_BRANCH_FUNNEL, sdl,
8268 case Intrinsic::wasm_landingpad_index:
8274 case Intrinsic::aarch64_settag:
8275 case Intrinsic::aarch64_settag_zero: {
8276 const SelectionDAGTargetInfo &TSI =
DAG.getSelectionDAGInfo();
8277 bool ZeroMemory =
Intrinsic == Intrinsic::aarch64_settag_zero;
8280 getValue(
I.getArgOperand(1)), MachinePointerInfo(
I.getArgOperand(0)),
8286 case Intrinsic::amdgcn_cs_chain: {
8291 Type *RetTy =
I.getType();
8301 for (
unsigned Idx : {2, 3, 1}) {
8302 TargetLowering::ArgListEntry Arg(
getValue(
I.getOperand(Idx)),
8304 Arg.setAttributes(&
I, Idx);
8305 Args.push_back(Arg);
8308 assert(Args[0].IsInReg &&
"SGPR args should be marked inreg");
8309 assert(!Args[1].IsInReg &&
"VGPR args should not be marked inreg");
8310 Args[2].IsInReg =
true;
8313 for (
unsigned Idx = 4; Idx <
I.arg_size(); ++Idx) {
8314 TargetLowering::ArgListEntry Arg(
getValue(
I.getOperand(Idx)),
8316 Arg.setAttributes(&
I, Idx);
8317 Args.push_back(Arg);
8320 TargetLowering::CallLoweringInfo CLI(
DAG);
8323 .setCallee(CC, RetTy, Callee, std::move(Args))
8326 .setConvergent(
I.isConvergent());
8328 std::pair<SDValue, SDValue>
Result =
8332 "Should've lowered as tail call");
8337 case Intrinsic::amdgcn_call_whole_wave: {
8339 bool isTailCall =
I.isTailCall();
8342 for (
unsigned Idx = 1; Idx <
I.arg_size(); ++Idx) {
8343 TargetLowering::ArgListEntry Arg(
getValue(
I.getArgOperand(Idx)),
8344 I.getArgOperand(Idx)->getType());
8345 Arg.setAttributes(&
I, Idx);
8352 Args.push_back(Arg);
8357 auto *Token = Bundle->Inputs[0].get();
8358 ConvControlToken =
getValue(Token);
8361 TargetLowering::CallLoweringInfo CLI(
DAG);
8365 getValue(
I.getArgOperand(0)), std::move(Args))
8369 .setConvergent(
I.isConvergent())
8370 .setConvergenceControlToken(ConvControlToken);
8373 std::pair<SDValue, SDValue>
Result =
8376 if (
Result.first.getNode())
8380 case Intrinsic::ptrmask: {