70#define FIXUPBW_DESC "X86 Byte/Word Instruction Fixup"
71#define FIXUPBW_NAME "x86-fixup-bw-insts"
73#define DEBUG_TYPE FIXUPBW_NAME
76class X86FixupBWInstImpl {
79 : PSI(PSI), MBFI(MBFI) {}
125 bool OptForSize =
false;
158char X86FixupBWInstLegacy::ID = 0;
165 return new X86FixupBWInstLegacy();
170 if (!ST.getCLOpts().fixup_byte_word_insts)
174 TII = ST.getInstrInfo();
176 LiveUnits.
init(
TII->getRegisterInfo());
194Register X86FixupBWInstImpl::getSuperRegDestIfDead(MachineInstr *OrigMI)
const {
200 const auto SubRegIdx =
TRI->getSubRegIndex(SuperDestReg, OrigDestReg);
206 if (SubRegIdx == X86::sub_8bit_hi)
212 bool SuperIsLive =
false;
213 auto Range =
TRI->regunits(OrigDestReg);
215 for (MCRegUnit S :
TRI->regunits(SuperDestReg)) {
216 I = std::lower_bound(
I,
E, S);
217 if ((
I ==
E || *
I > S) &&
260 if (
Opc != X86::MOV8rm &&
Opc != X86::MOV16rm &&
Opc != X86::MOV8rr &&
264 bool IsDefined =
false;
269 if (MO.isDef() &&
TRI->isSuperRegisterEq(OrigDestReg, MO.getReg()))
276 if (MO.isUse() && !
TRI->isSubRegisterEq(OrigDestReg, MO.getReg()) &&
277 TRI->regsOverlap(SuperDestReg, MO.getReg()))
289MachineInstr *X86FixupBWInstImpl::tryReplaceLoad(
unsigned New32BitOpcode,
290 MachineInstr *
MI)
const {
295 Register NewDestReg = getSuperRegDestIfDead(
MI);
300 MachineInstrBuilder MIB =
301 BuildMI(*MF, MIMetadata(*
MI),
TII->get(New32BitOpcode), NewDestReg);
303 unsigned NumArgs =
MI->getNumOperands();
304 for (
unsigned i = 1; i < NumArgs; ++i)
305 MIB.
add(
MI->getOperand(i));
310 if (
unsigned OldInstrNum =
MI->peekDebugInstrNum()) {
312 MI->getOperand(0).getReg());
314 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, Subreg);
320MachineInstr *X86FixupBWInstImpl::tryReplaceCopy(MachineInstr *
MI)
const {
321 assert(
MI->getNumExplicitOperands() == 2);
322 auto &OldDest =
MI->getOperand(0);
323 auto &OldSrc =
MI->getOperand(1);
325 Register NewDestReg = getSuperRegDestIfDead(
MI);
335 if (
TRI->getSubRegIndex(NewSrcReg, OldSrc.getReg()) !=
336 TRI->getSubRegIndex(NewDestReg, OldDest.getReg()))
344 MachineInstrBuilder MIB =
345 BuildMI(*MF, MIMetadata(*
MI),
TII->get(X86::MOV32rr), NewDestReg)
346 .
addReg(NewSrcReg, RegState::Undef)
347 .
addReg(OldSrc.getReg(), RegState::Implicit);
350 for (
auto &
Op :
MI->implicit_operands())
351 if (
Op.getReg() != (
Op.isDef() ? NewDestReg : NewSrcReg))
357MachineInstr *X86FixupBWInstImpl::tryReplaceExtend(
unsigned New32BitOpcode,
358 MachineInstr *
MI)
const {
359 Register NewDestReg = getSuperRegDestIfDead(
MI);
366 if (
MI->getOpcode() == X86::MOVSX16rr8 &&
367 MI->getOperand(0).getReg() == X86::AX &&
368 MI->getOperand(1).getReg() == X86::AL)
372 MachineInstrBuilder MIB =
373 BuildMI(*MF, MIMetadata(*
MI),
TII->get(New32BitOpcode), NewDestReg);
375 unsigned NumArgs =
MI->getNumOperands();
376 for (
unsigned i = 1; i < NumArgs; ++i)
377 MIB.
add(
MI->getOperand(i));
381 if (
unsigned OldInstrNum =
MI->peekDebugInstrNum()) {
383 MI->getOperand(0).getReg());
385 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, Subreg);
392X86FixupBWInstImpl::tryReplaceInstr(MachineInstr *
MI,
393 MachineBasicBlock &
MBB)
const {
395 switch (
MI->getOpcode()) {
403 return tryReplaceLoad(X86::MOVZX32rm8,
MI);
411 return tryReplaceLoad(X86::MOVZX32rm16,
MI);
419 return tryReplaceCopy(
MI);
421 case X86::MOVSX16rr8:
422 return tryReplaceExtend(X86::MOVSX32rr8,
MI);
423 case X86::MOVSX16rm8:
424 return tryReplaceExtend(X86::MOVSX32rm8,
MI);
425 case X86::MOVZX16rr8:
426 return tryReplaceExtend(X86::MOVZX32rr8,
MI);
427 case X86::MOVZX16rm8:
428 return tryReplaceExtend(X86::MOVZX32rm8,
MI);
438void X86FixupBWInstImpl::eliminateRedundantExtends(MachineBasicBlock &
MBB) {
446 auto DefinedExtension =
447 [](
const MachineInstr &
MI) -> std::optional<Extension> {
448 switch (
MI.getOpcode()) {
449 case X86::MOVZX32rm8:
450 case X86::MOVZX32rr8:
452 case X86::MOVZX32rm16:
453 case X86::MOVZX32rr16:
455 case X86::MOVSX32rm8:
456 case X86::MOVSX32rr8:
458 case X86::MOVSX32rm16:
459 case X86::MOVSX32rr16:
472 if (KnownExt.IsSigned == Ext.IsSigned)
473 return KnownExt.Bits <= Ext.Bits;
474 return !KnownExt.IsSigned && KnownExt.Bits < Ext.Bits;
481 SmallDenseMap<MCRegister, Extension, 8>
Known;
484 if (
MI.isDebugInstr())
487 std::optional<Extension> Ext = DefinedExtension(
MI);
492 unsigned Opc =
MI.getOpcode();
493 if (
Opc == X86::MOVZX32rr8 ||
Opc == X86::MOVZX32rr16 ||
494 Opc == X86::MOVSX32rr8 ||
Opc == X86::MOVSX32rr16) {
495 MCRegister Dst =
MI.getOperand(0).getReg().asMCReg();
496 MCRegister Src =
MI.getOperand(1).getReg().asMCReg();
498 auto It =
Known.find(SrcSuper);
501 if (It !=
Known.end() && IsRedundant(It->second, *Ext) &&
504 if (Dst == SrcSuper) {
507 MI.eraseFromParent();
514 MachineInstrBuilder MIB =
517 if (
unsigned OldInstrNum =
MI.peekDebugInstrNum()) {
519 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, 0);
521 MI.eraseFromParent();
534 for (
const MachineOperand &MO :
MI.operands()) {
535 if (MO.isRegMask()) {
537 for (
auto &KnownReg :
Known)
538 if (MO.clobbersPhysReg(KnownReg.first))
540 for (MCRegister
Reg : Clobbered)
542 }
else if (MO.isReg() && MO.isDef()) {
548 MCRegister Dst =
MI.getOperand(0).getReg().asMCReg();
555 MachineBasicBlock &
MBB) {
578 if (MachineInstr *NewMI = tryReplaceInstr(&
MI,
MBB))
579 MIReplacements.
push_back(std::make_pair(&
MI, NewMI));
582 if (!
MI.isDebugInstr())
586 while (!MIReplacements.
empty()) {
587 MachineInstr *
MI = MIReplacements.
back().first;
588 MachineInstr *NewMI = MIReplacements.
back().second;
595 eliminateRedundantExtends(
MBB);
601 ProfileSummaryInfo *PSI =
602 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
603 MachineBlockFrequencyInfo *MBFI =
605 ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
607 X86FixupBWInstImpl Impl(PSI, MBFI);
608 return Impl.runOnMachineFunction(MF);
616 .getCachedResult<ProfileSummaryAnalysis>(
621 X86FixupBWInstImpl Impl(PSI, MBFI);
622 bool Changed = Impl.runOnMachineFunction(MF);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
const HexagonInstrInfo * TII
===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*–===//
Register const TargetRegisterInfo * TRI
Promote Memory to Register
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
static bool processBasicBlock(MachineBasicBlock &MBB, BlockStateMap &BlockStates, DirtySuccessorsWorkList &DirtySuccessors, bool IsX86INTR, const TargetInstrInfo *TII)
Loop over all of the instructions in the basic block, inserting vzeroupper instructions before functi...
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()
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
Module * getParent()
Get the module that this global value is contained inside of...
const HexagonRegisterInfo & getRegisterInfo() const
This is an alternative analysis pass to MachineBlockFrequencyInfo.
A set of register units used to track register liveness.
const BitVector & getBitVector() const
Return the internal bitvector representation of the set.
void init(const TargetRegisterInfo &TRI)
Initialize and clear the set.
LLVM_ABI void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
void clear()
Clears the set.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
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.
Properties which a MachineFunction may have at a given point in time.
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 MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
mop_range implicit_operands()
LLVM_ABI unsigned getDebugInstrNum()
Fetch the instruction number of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
Register getReg() const
getReg - Returns the register number.
const TargetRegisterInfo * getTargetRegisterInfo() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This is an optimization pass for GlobalISel generic memory operations.
OuterAnalysisManagerProxy< ModuleAnalysisManager, MachineFunction > ModuleAnalysisManagerMachineFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Known
Known to have no common set bits.
MCRegister getX86SubSuperRegister(MCRegister Reg, unsigned Size, bool High=false)
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createX86FixupBWInstsLegacyPass()
DWARFExpression::Operation Op