27 #define DEBUG_TYPE "legalizer"
32 :
MRI(MF.getRegInfo()), LI(*MF.getSubtarget().getLegalizerInfo()) {
41 switch (std::get<0>(Action)) {
46 DEBUG(
dbgs() <<
".. Convert to libcall\n");
50 return narrowScalar(MI, std::get<1>(Action), std::get<2>(Action));
53 return widenScalar(MI, std::get<1>(Action), std::get<2>(Action));
56 return lower(MI, std::get<1>(Action), std::get<2>(Action));
58 DEBUG(
dbgs() <<
".. Reduce number of elements\n");
61 DEBUG(
dbgs() <<
".. Custom legalization\n");
65 DEBUG(
dbgs() <<
".. Unable to legalize\n");
70 void LegalizerHelper::extractParts(
unsigned Reg,
LLT Ty,
int NumParts,
72 for (
int i = 0; i < NumParts; ++i)
79 case TargetOpcode::G_SDIV:
80 assert(Size == 32 &&
"Unsupported size");
82 case TargetOpcode::G_UDIV:
83 assert(Size == 32 &&
"Unsupported size");
85 case TargetOpcode::G_SREM:
86 assert(Size == 32 &&
"Unsupported size");
88 case TargetOpcode::G_UREM:
89 assert(Size == 32 &&
"Unsupported size");
91 case TargetOpcode::G_FADD:
92 assert((Size == 32 || Size == 64) &&
"Unsupported size");
94 case TargetOpcode::G_FREM:
96 case TargetOpcode::G_FPOW:
108 const char *
Name = TLI.getLibcallName(Libcall);
111 if (!CLI.lowerCall(MIRBuilder, TLI.getLibcallCallingConv(Libcall),
138 case TargetOpcode::G_SDIV:
139 case TargetOpcode::G_UDIV:
140 case TargetOpcode::G_SREM:
141 case TargetOpcode::G_UREM: {
148 case TargetOpcode::G_FADD:
149 case TargetOpcode::G_FPOW:
150 case TargetOpcode::G_FREM: {
167 if (TypeIdx != 0 && MI.
getOpcode() != TargetOpcode::G_EXTRACT)
175 case TargetOpcode::G_IMPLICIT_DEF: {
180 for (
int i = 0; i < NumParts; ++i) {
189 case TargetOpcode::G_ADD: {
201 for (
int i = 0; i < NumParts; ++i) {
206 Src2Regs[i], CarryIn);
216 case TargetOpcode::G_EXTRACT: {
231 for (
int i = 0; i < NumParts; ++i) {
232 unsigned SrcStart = i * NarrowSize;
234 if (SrcStart + NarrowSize <= OpStart || SrcStart >= OpStart + OpSize) {
237 }
else if (SrcStart == OpStart && NarrowTy == MRI.
getType(OpReg)) {
245 int64_t ExtractOffset, SegSize;
246 if (OpStart < SrcStart) {
248 SegSize =
std::min(NarrowSize, OpStart + OpSize - SrcStart);
250 ExtractOffset = OpStart - SrcStart;
251 SegSize =
std::min(SrcStart + NarrowSize - OpStart, OpSize);
254 unsigned SegReg = SrcRegs[i];
255 if (ExtractOffset != 0 || SegSize != NarrowSize) {
268 case TargetOpcode::G_INSERT: {
283 for (
int i = 0; i < NumParts; ++i) {
284 unsigned DstStart = i * NarrowSize;
286 if (DstStart + NarrowSize <= OpStart || DstStart >= OpStart + OpSize) {
290 }
else if (DstStart == OpStart && NarrowTy == MRI.
getType(OpReg)) {
299 int64_t ExtractOffset, InsertOffset, SegSize;
300 if (OpStart < DstStart) {
302 ExtractOffset = DstStart - OpStart;
303 SegSize =
std::min(NarrowSize, OpStart + OpSize - DstStart);
305 InsertOffset = OpStart - DstStart;
308 std::min(NarrowSize - InsertOffset, OpStart + OpSize - DstStart);
311 unsigned SegReg = OpReg;
312 if (ExtractOffset != 0 || SegSize != OpSize) {
328 case TargetOpcode::G_LOAD: {
336 for (
int i = 0; i < NumParts; ++i) {
339 unsigned Adjustment = i * NarrowSize / 8;
348 DstRegs.push_back(DstReg);
355 case TargetOpcode::G_STORE: {
365 for (
int i = 0; i < NumParts; ++i) {
367 unsigned Adjustment = i * NarrowSize / 8;
379 case TargetOpcode::G_CONSTANT: {
387 for (
int i = 0; i < NumParts; ++i) {
409 case TargetOpcode::G_ADD:
410 case TargetOpcode::G_AND:
411 case TargetOpcode::G_MUL:
412 case TargetOpcode::G_OR:
413 case TargetOpcode::G_XOR:
414 case TargetOpcode::G_SUB:
415 case TargetOpcode::G_SHL: {
434 case TargetOpcode::G_SDIV:
435 case TargetOpcode::G_UDIV:
436 case TargetOpcode::G_SREM:
437 case TargetOpcode::G_UREM:
438 case TargetOpcode::G_ASHR:
439 case TargetOpcode::G_LSHR: {
440 unsigned ExtOp = MI.
getOpcode() == TargetOpcode::G_SDIV ||
441 MI.
getOpcode() == TargetOpcode::G_SREM ||
443 ? TargetOpcode::G_SEXT
444 : TargetOpcode::G_ZEXT;
464 case TargetOpcode::G_SELECT: {
487 case TargetOpcode::G_FPTOSI:
488 case TargetOpcode::G_FPTOUI: {
501 case TargetOpcode::G_SITOFP:
502 case TargetOpcode::G_UITOFP: {
509 if (MI.
getOpcode() == TargetOpcode::G_SITOFP) {
512 assert(MI.
getOpcode() == TargetOpcode::G_UITOFP &&
"Unexpected conv op");
523 case TargetOpcode::G_INSERT: {
534 for (
unsigned OpNum = 4; OpNum < MI.
getNumOperands(); OpNum += 2) {
543 case TargetOpcode::G_LOAD: {
546 "illegal to increase number of bytes loaded");
555 case TargetOpcode::G_STORE: {
563 unsigned ExtOp = TargetOpcode::G_ANYEXT;
565 ExtOp = TargetOpcode::G_ZEXT;
567 ExtOp = TargetOpcode::G_SEXT;
569 ExtOp = TargetOpcode::G_ANYEXT;
579 case TargetOpcode::G_CONSTANT: {
586 case TargetOpcode::G_FCONSTANT: {
593 case TargetOpcode::G_BRCOND: {
600 case TargetOpcode::G_ICMP: {
601 assert(TypeIdx == 1 &&
"unable to legalize predicate");
619 case TargetOpcode::G_GEP: {
620 assert(TypeIdx == 1 &&
"unable to legalize pointer of GEP");
631 using namespace TargetOpcode;
637 case TargetOpcode::G_SREM:
638 case TargetOpcode::G_UREM: {
639 unsigned QuotReg =
MRI.createGenericVirtualRegister(Ty);
645 unsigned ProdReg =
MRI.createGenericVirtualRegister(Ty);
652 case TargetOpcode::G_SMULO:
653 case TargetOpcode::G_UMULO: {
663 unsigned Opcode = MI.
getOpcode() == TargetOpcode::G_SMULO
664 ? TargetOpcode::G_SMULH
665 : TargetOpcode::G_UMULH;
667 unsigned HiPart =
MRI.createGenericVirtualRegister(Ty);
673 unsigned Zero =
MRI.createGenericVirtualRegister(Ty);
679 case TargetOpcode::G_FNEG: {
702 unsigned Zero =
MRI.createGenericVirtualRegister(Ty);
711 case TargetOpcode::G_FSUB: {
720 unsigned Neg =
MRI.createGenericVirtualRegister(Ty);
741 case TargetOpcode::G_ADD: {
752 for (
int i = 0; i < NumParts; ++i) {
static LegalizerHelper::LegalizeResult simpleLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, unsigned Size, Type *OpType)
void push_back(const T &Elt)
MachineIRBuilder MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions. ...
static Type * getDoubleTy(LLVMContext &C)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
LLT getType(unsigned VReg) const
Get the low-level type of VReg or LLT{} if VReg is not a generic (target independent) virtual registe...
const ConstantFP * getFPImm() const
MachineBasicBlock * getMBB() const
MachineInstrBuilder buildZExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_ZEXT Op.
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
std::pair< LegalizeAction, LLT > getAction(const InstrAspect &Aspect) const
Determine what action should be taken to legalize the given generic instruction opcode, type-index and type.
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit...
bool isSigned() const
Determine if this instruction is using a signed comparison.
static const MCPhysReg VRegs[32]
MachineInstrBuilder buildTrunc(unsigned Res, unsigned Op)
Build and insert Res<def> = G_TRUNC Op.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
MachineInstrBuilder buildSub(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_SUB Op0, Op1.
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
MachineInstrBuilder buildAnyExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_ANYEXT Op0.
virtual const CallLowering * getCallLowering() const
void print(raw_ostream &OS, bool SkipOpers=false, bool SkipDebugLoc=false, const TargetInstrInfo *TII=nullptr) const
Debugging supportPrint this MI to OS.
unsigned createGenericVirtualRegister(LLT Ty)
Create and return a new generic virtual register with low-level type Ty.
MachineInstrBuilder buildExtract(unsigned Res, unsigned Src, uint64_t Index)
Build and insert `Res0<def>, ...
const MachineInstrBuilder & addDef(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
MachineInstrBuilder buildFPTrunc(unsigned Res, unsigned Op)
Build and insert Res<def> = G_FPTRUNC Op.
LegalizerHelper(MachineFunction &MF)
MachineInstrBuilder buildStore(unsigned Val, unsigned Addr, MachineMemOperand &MMO)
Build and insert G_STORE Val, Addr, MMO.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
LegalizeResult widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy)
Legalize an instruction by performing the operation on a wider scalar type (for example a 16-bit addi...
static Type * getFloatTy(LLVMContext &C)
const APInt & getValue() const
Return the constant as an APInt value reference.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineInstrBuilder buildFConstant(unsigned Res, const ConstantFP &Val)
Build and insert Res = G_FCONSTANT Val.
Reg
All possible values of the reg field in the ModR/M byte.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
LegalizerHelper::LegalizeResult createLibcall(MachineIRBuilder &MIRBuilder, RTLIB::Libcall Libcall, const CallLowering::ArgInfo &Result, ArrayRef< CallLowering::ArgInfo > Args)
Helper function that creates the given libcall.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
MachineFunction & getMF()
Getter for the function we currently build.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
The target wants to do something special with this combination of operand and type.
unsigned const MachineRegisterInfo * MRI
The (vector) operation should be implemented by splitting it into sub-vectors where the operation is ...
APInt trunc(unsigned width) const
Truncate to new width.
LegalizeResult legalizeInstrStep(MachineInstr &MI)
Replace MI by a sequence of legal instructions that can implement the same operation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static unsigned getSizeInBits(unsigned Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
Get the size in bits of Reg.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
const MachineOperand & getOperand(unsigned i) const
ConstantFP - Floating Point Values [float, double].
Helper class to build MachineInstr.
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, unsigned Res, unsigned Op0, unsigned Op1)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
void setInstr(MachineInstr &MI)
Set the insertion point to before MI.
MachineInstrBuilder buildInsert(unsigned Res, unsigned Src, unsigned Op, unsigned Index)
void setMF(MachineFunction &)
Some kind of error has occurred and we could not legalize this instruction.
Instruction was already legal and no change was made to the MachineFunction.
MachineInstrBuilder buildUAdde(unsigned Res, unsigned CarryOut, unsigned Op0, unsigned Op1, unsigned CarryIn)
Build and insert Res<def>, CarryOut<def> = G_UADDE Op0, Op1, CarryIn.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
virtual bool legalizeCustom(MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &MIRBuilder) const
The operation should be synthesized from multiple instructions acting on a narrower scalar base-type...
static Type * getHalfTy(LLVMContext &C)
The operation should be implemented as a call to some kind of runtime support library.
This is the shared class of boolean and integer constants.
LegalizeResult libcall(MachineInstr &MI)
Legalize an instruction by emiting a runtime library call instead.
static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size)
virtual const TargetLowering * getTargetLowering() const
The operation should be implemented in terms of a wider scalar base-type.
unsigned getPredicate() const
LegalizeResult lower(MachineInstr &MI, unsigned TypeIdx, LLT Ty)
Legalize an instruction by splitting it into simpler parts, hopefully understood by the target...
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
LegalizeResult fewerElementsVector(MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy)
Legalize a vector instruction by splitting into multiple components, each acting on the same scalar t...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const ConstantInt * getCImm() const
Class for arbitrary precision integers.
The operation itself must be expressed in terms of simpler actions on this target.
static MachineOperand CreateES(const char *SymName, unsigned char TargetFlags=0)
MachineInstrBuilder buildSExt(unsigned Res, unsigned Op)
Build and insert Res<def> = G_SEXT Op.
The operation is expected to be selectable directly by the target, and no transformation is necessary...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
Representation of each machine instruction.
Instruction has been legalized and the MachineFunction changed.
MachineInstrBuilder buildUndef(unsigned Dst)
Build and insert Res = IMPLICIT_DEF.
static IntegerType * getInt32Ty(LLVMContext &C)
MachineInstrBuilder buildAdd(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_ADD Op0, Op1.
MachineInstrBuilder buildConstant(unsigned Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
static Constant * getZeroValueForNegation(Type *Ty)
Floating point negation must be implemented with f(x) = -0.0 - x.
MachineInstrBuilder buildMul(unsigned Res, unsigned Op0, unsigned Op1)
Build and insert Res<def> = G_MUL Op0, Op1.
Optional< MachineInstrBuilder > materializeGEP(unsigned &Res, unsigned Op0, const LLT &ValueTy, uint64_t Value)
Materialize and insert Res<def> = G_GEP Op0, (G_CONSTANT Value)
MachineInstrBuilder buildUnmerge(ArrayRef< unsigned > Res, unsigned Op)
Build and insert Res0<def>, ...
unsigned getReg() const
getReg - Returns the register number.
LegalizeResult narrowScalar(MachineInstr &MI, unsigned TypeIdx, LLT NarrowTy)
Legalize an instruction by reducing the width of the underlying scalar type.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MachineInstrBuilder & addUse(unsigned RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
This file describes how to lower LLVM calls to machine code calls.
MachineInstrBuilder buildLoad(unsigned Res, unsigned Addr, MachineMemOperand &MMO)
Build and insert Res<def> = G_LOAD Addr, MMO.
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
MachineInstrBuilder buildMerge(unsigned Res, ArrayRef< unsigned > Ops)
Build and insert Res<def> = G_MERGE_VALUES Op0, ...
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
This file describes how to lower LLVM code to machine code.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
MachineInstrBuilder buildBrCond(unsigned Tst, MachineBasicBlock &BB)
Build and insert G_BRCOND Tst, Dest.