29#include "llvm/IR/IntrinsicsSPIRV.h"
32#define DEBUG_TYPE "spirv-prelegalizer"
47void SPIRVPreLegalizerLegacy::getAnalysisUsage(
AnalysisUsage &AU)
const {
56 MI->eraseFromParent();
74 MI.getOperand(3).getMetadata()->getOperand(0))
79 GR->
add(GV,
MRI.getVRegDef(SrcReg));
82 RegsAlreadyAddedToDT[&
MI] =
Reg;
87 auto *BuildVec =
MRI.getVRegDef(SrcReg);
89 BuildVec->getOpcode() == TargetOpcode::G_BUILD_VECTOR);
90 GR->
add(Const, BuildVec);
91 for (
unsigned i = 0; i < ConstVec->getNumElements(); ++i) {
94 Constant *ElemConst = ConstVec->getElementAsConstant(i);
98 MRI.getVRegDef(BuildVec->getOperand(1 + i).getReg()));
100 BuildVec->getOperand(1 + i).setReg(ElemReg);
103 if (Const->getType()->isTargetExtTy()) {
107 GR->
add(Const, SrcMI);
108 if (SrcMI && (SrcMI->
getOpcode() == TargetOpcode::G_CONSTANT ||
109 SrcMI->
getOpcode() == TargetOpcode::G_IMPLICIT_DEF))
110 TargetExtConstTypes[SrcMI] = Const->getType();
111 if (Const->isNullValue()) {
115 Const->getType(), MIB, SPIRV::AccessQualifier::ReadWrite,
117 assert(SrcMI &&
"Expected source instruction to be valid");
124 RegsAlreadyAddedToDT[&
MI] =
Reg;
127 assert(
MI.getOperand(2).isReg() &&
"Reg operand is expected");
129 if (SrcMI &&
isSpvIntrinsic(*SrcMI, Intrinsic::spv_const_composite))
137 auto It = RegsAlreadyAddedToDT.
find(
MI);
138 if (It != RegsAlreadyAddedToDT.
end())
140 auto *RC =
MRI.getRegClassOrNull(
MI->getOperand(0).getReg());
141 if (!
MRI.getRegClassOrNull(
Reg) && RC)
143 MRI.replaceRegWith(
MI->getOperand(0).getReg(),
Reg);
158 const MDNode *MD =
MI.getOperand(2).getMetadata();
175 IE =
MRI->use_instr_end();
180 UseMI->getOperand(1).getReg() ==
Reg)
190 assert(ResType && OpType &&
"Operand types are expected");
194 if (!
MRI->getRegClassOrNull(ResVReg))
196 if (ResType == OpType)
227 "Expected destination SPIR-V type to have been assigned already.");
230 "Expected source SPIR-V type to have been assigned already.");
231 if (DstType == SrcType) {
239 if (
MI.getOpcode() != TargetOpcode::G_BITCAST)
244 MI.getOperand(1).getReg());
271 SPIRV::Extension::SPV_INTEL_function_pointers)
272 ? SPIRV::StorageClass::CodeSectionINTEL
287 AssignedPtrType->
getOpcode() == SPIRV::OpTypeUntypedPointerKHR
290 SPIRV::AccessQualifier::ReadWrite,
297 MRI->replaceRegWith(Def, Source);
328 assert(
MI &&
"Machine instr is expected");
329 if (
MI->getOperand(0).isReg()) {
334 switch (
MI->getOpcode()) {
335 case TargetOpcode::G_FCONSTANT:
336 case TargetOpcode::G_CONSTANT: {
337 Type *Ty =
MI->getOperand(1).getCImm()->getType();
339 Ty, MIB, SPIRV::AccessQualifier::ReadWrite,
true);
342 case TargetOpcode::G_GLOBAL_VALUE: {
345 unsigned AddrSpace =
Global->getType()->getAddressSpace();
350 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers))
355 Ty, MIB, SPIRV::AccessQualifier::ReadWrite,
true);
358 case TargetOpcode::G_ANYEXT:
359 case TargetOpcode::G_SEXT:
360 case TargetOpcode::G_ZEXT: {
361 if (
MI->getOperand(1).isReg()) {
363 MRI.getVRegDef(
MI->getOperand(1).getReg())) {
367 unsigned ExpectedBW =
368 std::max(
MRI.getType(
Reg).getScalarSizeInBits(), CurrentBW);
379 case TargetOpcode::G_PTRTOINT:
381 MRI.getType(
Reg).getScalarSizeInBits(), MIB);
383 case TargetOpcode::G_TRUNC:
384 case TargetOpcode::G_ADDRSPACE_CAST:
385 case TargetOpcode::G_PTR_ADD:
386 case TargetOpcode::COPY: {
399 if (SpvType.
isPointer() && RegType.isPointer() &&
401 RegType.getAddressSpace()) {
405 bool SkipCorrection =
406 MI->getOpcode() == TargetOpcode::G_GLOBAL_VALUE &&
408 SPIRV::StorageClass::CodeSectionINTEL;
409 if (!SkipCorrection) {
419 if (!
MRI.getRegClassOrNull(
Reg))
421 : &SPIRV::iIDRegClass);
436static std::optional<unsigned>
441 unsigned W = Ty.getScalarSizeInBits();
450 if (!RegType.isScalar())
452 unsigned CurrentWidth = RegType.getScalarSizeInBits();
454 if (NewWidth != CurrentWidth)
462 if (NewWidth != CurrentWidth) {
472 Def->getNextNode() ? Def->getNextNode()->getIterator() :
MBB.end();
474 while (DefIt !=
MBB.end() &&
475 (DefIt->isPHI() || DefIt->isDebugOrPseudoInstr()))
476 DefIt = std::next(DefIt);
484 assert((Ty || SpvType) &&
"Either LLVM or SPIRV type is expected.");
489 SPIRV::AccessQualifier::ReadWrite,
true);
490 if (!
MRI.getRegClassOrNull(Reg))
492 if (!
MRI.getType(Reg).isValid())
501 for (
auto &
Op :
MI.operands()) {
502 if (!
Op.isReg() ||
Op.isDef())
506 if (!SpvType && KnownResType) {
507 SpvType = KnownResType;
511 if (!
MRI.getRegClassOrNull(OpReg))
513 if (!
MRI.getType(OpReg).isValid())
530 switch (
MI.getOpcode()) {
531 case TargetOpcode::G_ASHR:
532 case TargetOpcode::G_SDIV:
533 case TargetOpcode::G_SREM:
534 case TargetOpcode::G_SITOFP:
535 case TargetOpcode::G_SMIN:
536 case TargetOpcode::G_SMAX:
538 case TargetOpcode::G_ICMP:
561 case TargetOpcode::G_CTLZ:
562 case TargetOpcode::G_CTLZ_ZERO_POISON:
563 case TargetOpcode::G_CTTZ:
564 case TargetOpcode::G_CTPOP:
574static NarrowWideningInfo
581 Info.OrigWidth.try_emplace(
Reg, *W);
587 if (std::optional<unsigned> W =
589 Info.BitCountWorklist.emplace_back(&
MI, *W);
594 bool NeedsRewrite =
false;
596 NeedsRewrite = RecordIfNarrow(MO.getReg()) || NeedsRewrite;
598 Info.SignSensitiveWorklist.push_back(&
MI);
600 RecordIfNarrow(
MI.getOperand(0).getReg());
622 auto SignExtendReg = [&](
Register Reg,
unsigned OldW,
628 Register SExted =
MRI.createGenericVirtualRegister(NewLLT);
645 MI.getOperand(0).setReg(Result);
657 auto It = Info.OrigWidth.find(
Reg);
658 if (It == Info.OrigWidth.end())
662 MI->substituteRegister(
Reg, SignExtendReg(
Reg, It->second, *
MI),
666 if (
auto It = Info.OrigWidth.find(Dst); It != Info.OrigWidth.end())
667 MaskResult(*
MI, It->second);
679 for (
auto [
MI, OldWidth] : Info.BitCountWorklist) {
690 bool HighBitsAlreadyZero =
693 auto ClearHighBits = [&](
unsigned Width) ->
Register {
694 if (HighBitsAlreadyZero)
702 switch (
MI->getOpcode()) {
703 case TargetOpcode::G_CTLZ_ZERO_POISON: {
711 case TargetOpcode::G_CTTZ: {
719 case TargetOpcode::G_CTPOP:
720 Input = ClearHighBits(OldWidth);
722 case TargetOpcode::G_CTLZ: {
724 Input = ClearHighBits(OldWidth);
728 MI->getOperand(0).setReg(
Count);
737 MI->getOperand(1).setReg(
Input);
754 bool IsExtendedInts =
756 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers) ||
757 ST->canUseExtension(SPIRV::Extension::SPV_KHR_bit_instructions) ||
758 ST->canUseExtension(SPIRV::Extension::SPV_INTEL_int4);
760 if (!IsExtendedInts) {
786 unsigned MIOp =
MI.getOpcode();
787 if (MIOp != TargetOpcode::G_TRUNC)
796 LLT DstTy =
MRI.getType(DstReg);
797 LLT SrcTy =
MRI.getType(SrcReg);
799 (SrcTy.isScalar() || SrcTy.isVector()) &&
800 "Expected scalar or vector G_TRUNC types");
802 "Expected matching scalar/vector G_TRUNC types");
805 "Expected equal vector element counts");
808 unsigned OriginalSrcWidth = SrcTy.getScalarSizeInBits();
817 if (OriginalDstWidth == NewDstWidth) {
818 MRI.setType(SrcReg, NewSrcTy);
822 MRI.setType(SrcReg, NewSrcTy);
823 MRI.setType(DstReg, NewDstTy);
833 Register MaskedReg =
MRI.createGenericVirtualRegister(NewSrcTy);
834 MIB.
buildAnd(MaskedReg, SrcReg, MaskReg);
836 if (NewSrcWidth == NewDstWidth) {
841 unsigned W = It->second;
845 MRI.replaceRegWith(DstReg, MaskedReg);
848 MI.getOperand(1).setReg(MaskedReg);
853 MI->eraseFromParent();
863 bool ReachedBegin =
false;
864 for (
auto MII = std::prev(
MBB->end()), Begin =
MBB->begin();
867 unsigned MIOp =
MI.getOpcode();
869 if (!IsExtendedInts) {
871 for (
auto &MOP :
MI.operands()) {
874 else if (MOP.isCImm())
884 if (SC == SPIRV::StorageClass::Function &&
886 ST->canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers))
887 SC = SPIRV::StorageClass::CodeSectionINTEL;
892 if (ST->canUseExtension(SPIRV::Extension::SPV_KHR_untyped_pointers) &&
895 ElementTy, MIB, SPIRV::AccessQualifier::ReadWrite,
909 assert(Def &&
"Expecting an instruction that defines the register");
911 if (Def->getOpcode() != TargetOpcode::G_GLOBAL_VALUE)
919 assert(Def &&
"Expecting an instruction that defines the register");
921 if (Def->getOpcode() != TargetOpcode::G_GLOBAL_VALUE)
923 if (Def->getOpcode() == TargetOpcode::COPY &&
isVector1(Ty))
927 }
else if (MIOp == TargetOpcode::FAKE_USE &&
MI.getNumOperands() > 0) {
932 for (
unsigned I = 1,
E =
MI.getNumOperands();
I !=
E && Def; ++
I)
944 if (STy && STy->getNumElements() == Def->getNumDefs())
945 for (
auto [
Op,
ElemTy] :
zip(Def->defs(), STy->elements()))
952 }
else if (MIOp == TargetOpcode::G_CONSTANT ||
953 MIOp == TargetOpcode::G_FCONSTANT ||
954 MIOp == TargetOpcode::G_BUILD_VECTOR) {
960 if (MIOp == TargetOpcode::G_CONSTANT) {
961 auto TargetExtIt = TargetExtConstTypes.
find(&
MI);
962 Ty = TargetExtIt == TargetExtConstTypes.
end()
963 ?
MI.getOperand(1).getCImm()->getType()
964 : TargetExtIt->second;
977 }
else if (PrimaryReg !=
Reg &&
978 MRI.getType(
Reg) ==
MRI.getType(PrimaryReg)) {
979 auto *RCReg =
MRI.getRegClassOrNull(
Reg);
980 auto *RCPrimary =
MRI.getRegClassOrNull(PrimaryReg);
982 if ((!RCReg || RCPrimary == RCReg) &&
984 RegsAlreadyAddedToDT[&
MI] = PrimaryReg;
986 NeedAssignType =
false;
989 }
else if (MIOp == TargetOpcode::G_FCONSTANT) {
990 Ty =
MI.getOperand(1).getFPImm()->getType();
992 assert(MIOp == TargetOpcode::G_BUILD_VECTOR);
997 if (ElemMI->
getOpcode() == TargetOpcode::G_CONSTANT) {
999 }
else if (ElemMI->
getOpcode() == TargetOpcode::G_FCONSTANT) {
1008 ElemTy,
MI.getNumExplicitOperands() -
MI.getNumExplicitDefs(),
1011 NeedAssignType =
false;
1015 }
else if (MIOp == TargetOpcode::G_GLOBAL_VALUE) {
1020 ReachedBegin =
true;
1026 auto It = RegsAlreadyAddedToDT.
find(
MI);
1027 if (It != RegsAlreadyAddedToDT.
end())
1028 MRI.replaceRegWith(
MI->getOperand(0).getReg(), It->second);
1036 switch (
MI.getOpcode()) {
1037 case TargetOpcode::G_TRUNC:
1038 case TargetOpcode::G_ANYEXT:
1039 case TargetOpcode::G_SEXT:
1040 case TargetOpcode::G_ZEXT:
1041 case TargetOpcode::G_PTRTOINT:
1042 case TargetOpcode::COPY:
1043 case TargetOpcode::G_ADDRSPACE_CAST:
1067 for (
unsigned Idx = StartOp, MISz =
MI->getNumOperands(); Idx != MISz;
1072 if (Idx == AsmDescOp && MO.
isImm()) {
1075 AsmDescOp += 1 +
F.getNumOperandRegisters();
1083 Ops->push_back(Idx);
1095 for (
unsigned i = 0, Sz = ToProcess.
size(); i + 1 < Sz; i += 2) {
1096 MachineInstr *I1 = ToProcess[i], *I2 = ToProcess[i + 1];
1100 if (!AsmTargetReg.
isValid()) {
1102 AsmTargetReg =
MRI.createGenericVirtualRegister(
LLT::scalar(32));
1103 MRI.setRegClass(AsmTargetReg, &SPIRV::iIDRegClass);
1107 GR->
add(AsmTargetMIB.getInstr(), AsmTargetMIB);
1111 const MDNode *IAMD = I1->getOperand(1).getMetadata();
1114 for (
const auto &ArgTy : FTy->params())
1116 ArgTy, MIRBuilder, SPIRV::AccessQualifier::ReadWrite,
true));
1119 SPIRV::AccessQualifier::ReadWrite,
true);
1121 FTy, RetType, ArgTypes, MIRBuilder);
1125 MRI.setRegClass(AsmReg, &SPIRV::iIDRegClass);
1126 auto AsmMIB = MIRBuilder.
buildInstr(SPIRV::OpAsmINTEL)
1138 GR->
add(AsmMIB.getInstr(), AsmMIB);
1145 .
addImm(
static_cast<uint32_t>(SPIRV::Decoration::SideEffectsINTEL));
1150 MRI.setRegClass(DefReg, &SPIRV::iIDRegClass);
1153 SPIRV::AccessQualifier::ReadWrite,
true);
1157 auto AsmCall = MIRBuilder.
buildInstr(SPIRV::OpAsmCallINTEL)
1161 for (
unsigned IntrIdx = 3; IntrIdx < I1->getNumOperands(); ++IntrIdx)
1162 AsmCall.
addUse(I1->getOperand(IntrIdx).getReg());
1170 if (
MRI.hasOneUse(DefReg)) {
1172 if (CopyMI.
getOpcode() == TargetOpcode::COPY) {
1174 if (
MRI.hasOneUse(CopyDst)) {
1176 if (TruncMI.
getOpcode() == TargetOpcode::G_TRUNC) {
1179 MRI.replaceRegWith(TruncReg, DefReg);
1198 MI.getOpcode() == TargetOpcode::INLINEASM)
1202 if (ToProcess.
size() == 0)
1205 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_inline_assembly))
1207 "following SPIR-V extension: SPV_INTEL_inline_assembly",
1226 MI.getOperand(2).getMetadata(), ST);
1228 Intrinsic::spv_assign_fpmaxerror_decoration)) {
1230 MI.getOperand(2).getMetadata()->getOperand(0));
1234 SPIRV::Decoration::FPMaxErrorDecorationINTEL,
1238 MI.getOperand(2).getImm(),
1239 MI.getOperand(3).getMetadata());
1258 return ConstantInt::get(Ctx, Val);
1265 assert(Ty.isValid() &&
"Expected a typed switch case value");
1266 Val =
APInt(Ty.getScalarSizeInBits(), 0);
1268 switch (Def->getOpcode()) {
1269 case SPIRV::OpConstantNull:
1270 case SPIRV::OpConstantI:
1274 for (
unsigned I = 2,
E = Def->getNumExplicitOperands();
I !=
E; ++
I) {
1282 return ConstantInt::get(Ctx, Val);
1302 for (
unsigned i = 3; i <
MI.getNumOperands(); i += 2) {
1311 while (
MI.getNumOperands() > 0)
1312 MI.removeOperand(0);
1313 for (
auto &MO : NewOperands)
1327 MI.getOpcode() == TargetOpcode::G_BRINDIRECT)
1343 BB2MBB[
MBB.getBasicBlock()] = &
MBB;
1365 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
1367 if (!
MI->getOperand(i).isReg()) {
1375 if (!BuildMBB || BuildMBB->
getOpcode() != TargetOpcode::G_BLOCK_ADDR) {
1380 assert(BuildMBB && BuildMBB->
getOpcode() == TargetOpcode::G_BLOCK_ADDR &&
1385 auto It = BB2MBB.
find(BB);
1386 if (It == BB2MBB.
end())
1388 "in a switch statement");
1392 ClearAddressTaken.
insert(ReferencedBlock);
1393 ToEraseMI.
insert(BuildMBB);
1398 while (
MI->getNumOperands() > 0)
1399 MI->removeOperand(0);
1400 for (
auto &MO : NewOps)
1406 Next =
MI->getNextNode();
1408 if (
Next &&
Next->getOpcode() == TargetOpcode::G_BRINDIRECT)
1417 Succ->setAddressTakenIRBlock(
nullptr);
1428 if (BlockAddrI->getOpcode() == TargetOpcode::G_BLOCK_ADDR) {
1430 BlockAddrI->getOperand(1).getBlockAddress());
1441 return MBB.getNextNode() !=
nullptr;
1445 if (!
MBB.canFallThrough())
1477 GR->setCurrentFunc(MF);
1503char SPIRVPreLegalizerLegacy::ID = 0;
1506 return new SPIRVPreLegalizerLegacy();
1509bool SPIRVPreLegalizerLegacy::runOnMachineFunction(
MachineFunction &MF) {
1511 MF, getAnalysis<MachineDominatorTreeWrapperPass>().
getDomTree());
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides analysis for continuously CSEing during GISel passes.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Provides analysis for querying information about KnownBits during GISel passes.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Contains matchers for matching SSA Machine Instructions.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
MachineRegisterInfo & MRI
static DominatorTree getDomTree(Function &F)
static std::optional< unsigned > getNarrowScalarWidth(Register Reg, const MachineRegisterInfo &MRI)
static Register collectInlineAsmInstrOperands(MachineInstr *MI, SmallVector< unsigned, 4 > *Ops=nullptr)
static void insertInlineAsm(MachineFunction &MF, SPIRVGlobalRegistry *GR, const SPIRVSubtarget &ST, MachineIRBuilder MIRBuilder)
static void cleanupHelperInstructions(MachineFunction &MF, SPIRVGlobalRegistry *GR)
static void insertInlineAsmProcess(MachineFunction &MF, SPIRVGlobalRegistry *GR, const SPIRVSubtarget &ST, MachineIRBuilder MIRBuilder, const SmallVector< MachineInstr * > &ToProcess)
static NarrowWideningInfo recordNarrowOperandWidths(MachineFunction &MF, const MachineRegisterInfo &MRI)
static void removeImplicitFallthroughs(MachineFunction &MF, MachineIRBuilder MIB)
static unsigned widenBitWidthToNextPow2(unsigned BitWidth)
static void generateAssignInstrs(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB, DenseMap< MachineInstr *, Type * > &TargetExtConstTypes, const MachineDominatorTree &MDT)
static void setInsertPtAfterDef(MachineIRBuilder &MIB, MachineInstr *Def)
static bool isWidthSensitiveBitCountOp(unsigned Opcode)
static bool isImplicitFallthrough(MachineBasicBlock &MBB)
static void insertSpirvDecorations(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void insertBitcasts(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void processInstrsWithTypeFolding(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static bool runPreLegalizer(MachineFunction &MF, const MachineDominatorTree &MDT)
static void processSwitchesConstants(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void lowerBitcasts(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static MachineInstr * findAssignTypeInstr(Register Reg, MachineRegisterInfo *MRI)
static void widenCImmType(MachineOperand &MOP)
static void widenSignSensitiveOps(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo &MRI, const NarrowWideningInfo &Info)
static void buildOpBitcast(SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, Register ResVReg, Register OpReg)
static void processBlockAddr(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void widenBitCountOps(SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo &MRI, const NarrowWideningInfo &Info)
static void widenScalarType(Register Reg, MachineRegisterInfo &MRI)
static const ConstantInt * getSwitchCaseValue(Register Reg, const MachineRegisterInfo &MRI, LLVMContext &Ctx)
static void foldConstantsIntoIntrinsics(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void addConstantsToTrack(MachineFunction &MF, SPIRVGlobalRegistry *GR, const SPIRVSubtarget &STI, DenseMap< MachineInstr *, Type * > &TargetExtConstTypes)
static SPIRVTypeInst propagateSPIRVType(MachineInstr *MI, SPIRVGlobalRegistry *GR, MachineRegisterInfo &MRI, MachineIRBuilder &MIB)
static bool isSignSensitiveOp(const MachineInstr &MI)
static void invalidateAndEraseMI(SPIRVGlobalRegistry *GR, MachineInstr *MI)
APInt bitcastToAPInt() const
Class for arbitrary precision integers.
uint64_t getZExtValue() const
Get zero extended value.
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
unsigned getBitWidth() const
Return the number of bits in the APInt.
APInt shl(unsigned shiftAmt) const
Left-shift function.
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
The address of a basic block.
BasicBlock * getBasicBlock() const
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
const APFloat & getValueAPF() const
This is the shared class of boolean and integer constants.
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
const APInt & getValue() const
Return the constant as an APInt value reference.
This is an important base class in LLVM.
LLVM_ABI void destroyConstant()
Called if some element of this constant is no longer valid.
iterator find(const_arg_type_t< KeyT > Val)
bool erase(const KeyT &Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr ElementCount getElementCount() const
LLT changeElementSize(unsigned NewEltSize) const
If this type is a vector, return a vector with the same number of elements but the new element size.
This is an important class for using LLVM in a threaded context.
const MDOperand & getOperand(unsigned I) const
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
Helper class to build MachineInstr.
MachineInstrBuilder buildBr(MachineBasicBlock &Dest)
Build and insert G_BR Dest.
void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II)
Set the insertion point before the specified position.
MachineInstrBuilder buildZExtInReg(const DstOp &Res, const SrcOp &Op, int64_t ImmOp)
Build and inserts Res = G_AND Op, LowBitsSet(ImmOp) Since there is no G_ZEXT_INREG like G_SEXT_INREG,...
MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1)
Build and insert Res = G_AND Op0, Op1.
MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_SUB Op0, Op1.
MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineInstrBuilder buildBuildVectorConstant(const DstOp &Res, ArrayRef< APInt > Ops)
Build and insert Res = G_BUILD_VECTOR Op0, ... where each OpN is built with G_CONSTANT.
MachineFunction & getMF()
Getter for the function we currently build.
void setInstrAndDebugLoc(MachineInstr &MI)
Set the insertion point to before MI, and set the debug loc to MI's loc.
MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src)
Build and insert Dst = G_BITCAST Src.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_OR Op0, Op1.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
MachineInstrBuilder buildSExtInReg(const DstOp &Res, const SrcOp &Op, int64_t ImmOp)
Build and insert Res = G_SEXT_INREG Op, ImmOp.
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
mop_range defs()
Returns all explicit operands that are register definitions.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
const MachineOperand & getOperand(unsigned i) const
void setDebugLoc(DebugLoc DL)
Replace current source information with new such.
MachineOperand class - Representation of each machine instruction operand.
const ConstantInt * getCImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const MDNode * getMetadata() const
static MachineOperand CreateCImm(const ConstantInt *CI)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isMetadata() const
isMetadata - Tests if this is a MO_Metadata operand.
const BlockAddress * getBlockAddress() const
void setCImm(const ConstantInt *CI)
bool isBlockAddress() const
isBlockAddress - Tests if this is a MO_BlockAddress operand.
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static MachineOperand CreateMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
defusechain_instr_iterator< true, false, false, true > use_instr_iterator
use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the specified register,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
void assignSPIRVTypeToVReg(SPIRVTypeInst Type, Register VReg, const MachineFunction &MF)
SPIRVTypeInst getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVTypeInst RetType, const SmallVectorImpl< SPIRVTypeInst > &ArgTypes, MachineIRBuilder &MIRBuilder)
const TargetRegisterClass * getRegClass(SPIRVTypeInst SpvType) const
SPIRVTypeInst getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC, bool ForceTyped=false, bool EmitIR=true)
bool isAggregateType(SPIRVTypeInst Type) const
unsigned getScalarOrVectorBitWidth(SPIRVTypeInst Type) const
void setUntypedPtrElementType(Register Reg, SPIRVTypeInst ElemType)
SPIRVTypeInst getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
SPIRVTypeInst getOrCreateSPIRVVectorType(SPIRVTypeInst BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
unsigned getScalarOrVectorComponentCount(Register VReg) const
const Type * getTypeForSPIRVType(SPIRVTypeInst Ty) const
bool isBitcastCompatible(SPIRVTypeInst Type1, SPIRVTypeInst Type2) const
LLT getRegType(SPIRVTypeInst SpvType) const
void invalidateMachineInstr(MachineInstr *MI)
Register getSPIRVTypeID(SPIRVTypeInst SpirvType) const
SPIRVTypeInst changePointerStorageClass(SPIRVTypeInst PtrType, SPIRV::StorageClass::StorageClass SC, MachineInstr &I)
void addGlobalObject(const Value *V, const MachineFunction *MF, Register R)
SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
SPIRVTypeInst getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
Type * getDeducedGlobalValueType(const GlobalValue *Global)
void addValueAttrs(MachineInstr *Key, std::pair< Type *, std::string > Val)
void buildMemAliasingOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, uint32_t Dec, const MDNode *GVarMD)
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
SPIRVTypeInst getUntypedPtrElementType(Register Reg) const
bool add(SPIRV::IRHandle Handle, const MachineInstr *MI)
Register find(SPIRV::IRHandle Handle, const MachineFunction *MF)
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const SPIRVInstrInfo * getInstrInfo() const override
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static LLVM_ABI TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
operand_type_match m_Reg()
ConstantMatch< APInt > m_ICst(APInt &Cst)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
BinaryOp_match< LHS, RHS, TargetOpcode::G_AND, true > m_GAnd(const LHS &L, const RHS &R)
bind_ty< MachineInstr * > m_MInstr(MachineInstr *&MI)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
This is an optimization pass for GlobalISel generic memory operations.
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
StringMapEntry< Value * > ValueName
void addStringImm(StringRef Str, MCInst &Inst)
bool isTypeFoldingSupported(unsigned Opcode)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Register createVirtualRegister(SPIRVTypeInst SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
void updateRegType(Register Reg, Type *Ty, SPIRVTypeInst SpirvTy, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
Helper external function for assigning a SPIRV type to a register, ensuring the register class and ty...
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, ArrayRef< uint32_t > DecArgs, StringRef StrImm)
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void buildOpName(Register Target, StringRef Name, MachineIRBuilder &MIRBuilder)
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
Type * toTypedPointer(Type *Ty)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
auto post_order(const T &G)
Post-order traversal of a graph.
MachineInstr * passCopy(MachineInstr *Def, const MachineRegisterInfo *MRI)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
@ Global
Append to llvm.global_dtors.
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
void buildOpSpirvDecorations(Register Reg, MachineIRBuilder &MIRBuilder, const MDNode *GVarMD, const SPIRVSubtarget &ST)
void processInstr(MachineInstr &MI, MachineIRBuilder &MIB, MachineRegisterInfo &MRI, SPIRVGlobalRegistry *GR, SPIRVTypeInst KnownResType)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
DWARFExpression::Operation Op
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Type * getMDOperandAsType(const MDNode *N, unsigned I)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
FunctionPass * createSPIRVPreLegalizerLegacyPass()
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
MachineInstr * getVRegDef(MachineRegisterInfo &MRI, Register Reg)
SmallVector< MachineInstr * > SignSensitiveWorklist
DenseMap< Register, unsigned > OrigWidth
SmallVector< std::pair< MachineInstr *, unsigned > > BitCountWorklist