64#define FIXUPBW_DESC "X86 Byte/Word Instruction Fixup"
65#define FIXUPBW_NAME "x86-fixup-bw-insts"
67#define DEBUG_TYPE FIXUPBW_NAME
72 cl::desc(
"Change byte and word instructions to larger sizes"),
112 FixupBWInstPass() : MachineFunctionPass(ID) { }
114 void getAnalysisUsage(AnalysisUsage &AU)
const override {
116 AU.
addRequired<LazyMachineBlockFrequencyInfoPass>();
123 bool runOnMachineFunction(MachineFunction &MF)
override;
125 MachineFunctionProperties getRequiredProperties()
const override {
126 return MachineFunctionProperties().setNoVRegs();
130 MachineFunction *MF =
nullptr;
133 const X86InstrInfo *TII =
nullptr;
135 const TargetRegisterInfo *TRI =
nullptr;
138 bool OptForSize =
false;
141 LiveRegUnits LiveUnits;
143 ProfileSummaryInfo *PSI =
nullptr;
144 MachineBlockFrequencyInfo *MBFI =
nullptr;
146char FixupBWInstPass::ID = 0;
160 PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
162 &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
164 LiveUnits.
init(
TII->getRegisterInfo());
170 processBasicBlock(MF,
MBB);
182Register FixupBWInstPass::getSuperRegDestIfDead(MachineInstr *OrigMI)
const {
183 const X86RegisterInfo *
TRI = &
TII->getRegisterInfo();
188 const auto SubRegIdx =
TRI->getSubRegIndex(SuperDestReg, OrigDestReg);
194 if (SubRegIdx == X86::sub_8bit_hi)
200 bool SuperIsLive =
false;
201 auto Range =
TRI->regunits(OrigDestReg);
204 I = std::lower_bound(
I,
E, S);
247 if (
Opc != X86::MOV8rm &&
Opc != X86::MOV16rm &&
Opc != X86::MOV8rr &&
251 bool IsDefined =
false;
256 if (MO.isDef() &&
TRI->isSuperRegisterEq(OrigDestReg, MO.getReg()))
263 if (MO.isUse() && !
TRI->isSubRegisterEq(OrigDestReg, MO.getReg()) &&
264 TRI->regsOverlap(SuperDestReg, MO.getReg()))
276MachineInstr *FixupBWInstPass::tryReplaceLoad(
unsigned New32BitOpcode,
277 MachineInstr *
MI)
const {
282 Register NewDestReg = getSuperRegDestIfDead(
MI);
287 MachineInstrBuilder MIB =
288 BuildMI(*MF, MIMetadata(*
MI),
TII->get(New32BitOpcode), NewDestReg);
290 unsigned NumArgs =
MI->getNumOperands();
291 for (
unsigned i = 1; i < NumArgs; ++i)
292 MIB.
add(
MI->getOperand(i));
297 if (
unsigned OldInstrNum =
MI->peekDebugInstrNum()) {
299 MI->getOperand(0).getReg());
301 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, Subreg);
307MachineInstr *FixupBWInstPass::tryReplaceCopy(MachineInstr *
MI)
const {
308 assert(
MI->getNumExplicitOperands() == 2);
309 auto &OldDest =
MI->getOperand(0);
310 auto &OldSrc =
MI->getOperand(1);
312 Register NewDestReg = getSuperRegDestIfDead(
MI);
321 const X86RegisterInfo *
TRI = &
TII->getRegisterInfo();
322 if (
TRI->getSubRegIndex(NewSrcReg, OldSrc.getReg()) !=
323 TRI->getSubRegIndex(NewDestReg, OldDest.getReg()))
331 MachineInstrBuilder MIB =
332 BuildMI(*MF, MIMetadata(*
MI),
TII->get(X86::MOV32rr), NewDestReg)
337 for (
auto &
Op :
MI->implicit_operands())
338 if (
Op.getReg() != (
Op.isDef() ? NewDestReg : NewSrcReg))
344MachineInstr *FixupBWInstPass::tryReplaceExtend(
unsigned New32BitOpcode,
345 MachineInstr *
MI)
const {
346 Register NewDestReg = getSuperRegDestIfDead(
MI);
353 if (
MI->getOpcode() == X86::MOVSX16rr8 &&
354 MI->getOperand(0).getReg() == X86::AX &&
355 MI->getOperand(1).getReg() == X86::AL)
359 MachineInstrBuilder MIB =
360 BuildMI(*MF, MIMetadata(*
MI),
TII->get(New32BitOpcode), NewDestReg);
362 unsigned NumArgs =
MI->getNumOperands();
363 for (
unsigned i = 1; i < NumArgs; ++i)
364 MIB.
add(
MI->getOperand(i));
368 if (
unsigned OldInstrNum =
MI->peekDebugInstrNum()) {
370 MI->getOperand(0).getReg());
372 MF->makeDebugValueSubstitution({OldInstrNum, 0}, {NewInstrNum, 0}, Subreg);
378MachineInstr *FixupBWInstPass::tryReplaceInstr(MachineInstr *
MI,
379 MachineBasicBlock &
MBB)
const {
381 switch (
MI->getOpcode()) {
389 return tryReplaceLoad(X86::MOVZX32rm8,
MI);
397 return tryReplaceLoad(X86::MOVZX32rm16,
MI);
405 return tryReplaceCopy(
MI);
407 case X86::MOVSX16rr8:
408 return tryReplaceExtend(X86::MOVSX32rr8,
MI);
409 case X86::MOVSX16rm8:
410 return tryReplaceExtend(X86::MOVSX32rm8,
MI);
411 case X86::MOVZX16rr8:
412 return tryReplaceExtend(X86::MOVZX32rr8,
MI);
413 case X86::MOVZX16rm8:
414 return tryReplaceExtend(X86::MOVZX32rm8,
MI);
424void FixupBWInstPass::processBasicBlock(MachineFunction &MF,
425 MachineBasicBlock &
MBB) {
448 if (MachineInstr *NewMI = tryReplaceInstr(&
MI,
MBB))
449 MIReplacements.
push_back(std::make_pair(&
MI, NewMI));
455 while (!MIReplacements.
empty()) {
456 MachineInstr *
MI = MIReplacements.
back().first;
457 MachineInstr *NewMI = MIReplacements.
back().second;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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 cl::opt< bool > FixupBWInsts("fixup-byte-word-insts", cl::desc("Change byte and word instructions to larger sizes"), cl::init(true), cl::Hidden)
AnalysisUsage & addRequired()
bool test(unsigned Idx) const
FunctionPass class - This class is used to implement most global optimizations.
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.
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 MachineInstrBuilder & setMemRefs(ArrayRef< MachineMemOperand * > MMOs) const
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
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
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)
StringRef - Represent a constant reference to a string, i.e.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Undef
Value of the register doesn't matter.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MCRegister getX86SubSuperRegister(MCRegister Reg, unsigned Size, bool High=false)
FunctionPass * createX86FixupBWInsts()
Return a Machine IR pass that selectively replaces certain byte and word instructions by equivalent 3...
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.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned MCRegUnit
Register units are used to compute register aliasing.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DWARFExpression::Operation Op