24#define DEBUG_TYPE "riscv-sextw-removal"
26STATISTIC(NumRemovedSExtW,
"Number of removed sign-extensions");
28 "Number of instructions transformed to W-ops");
31 cl::desc(
"Disable removal of sext.w"),
55char RISCVSExtWRemoval::ID = 0;
60 return new RISCVSExtWRemoval();
74 switch (
MI.getOpcode()) {
77 return MI.getOperand(2).getImm() >= 32;
79 return MI.getOperand(2).getImm() > 32;
82 return MI.getOperand(1).isReg() &&
MI.getOperand(1).getReg() == RISCV::X0;
85 return isUInt<11>(
MI.getOperand(2).getImm());
88 return !isUInt<11>(
MI.getOperand(2).getImm());
91 return MI.getOperand(1).getReg() == RISCV::X0;
104 auto AddRegDefToWorkList = [&](
Register SrcReg) {
115 if (!AddRegDefToWorkList(SrcReg))
118 while (!Worklist.
empty()) {
130 switch (
MI->getOpcode()) {
141 if (
MI->getParent() == &MF->
front()) {
147 Register CopySrcReg =
MI->getOperand(1).getReg();
148 if (CopySrcReg == RISCV::X10) {
158 auto II =
MI->getIterator();
160 (--II)->
getOpcode() != RISCV::ADJCALLSTACKUP)
172 auto *IntTy = dyn_cast<IntegerType>(CalleeFn->getReturnType());
176 const AttributeSet &Attrs = CalleeFn->getAttributes().getRetAttrs();
177 unsigned BitWidth = IntTy->getBitWidth();
178 if ((
BitWidth <= 32 && Attrs.hasAttribute(Attribute::SExt)) ||
179 (
BitWidth < 32 && Attrs.hasAttribute(Attribute::ZExt)))
183 if (!AddRegDefToWorkList(CopySrcReg))
193 if (
MI->getOperand(2).getImm() >= 31)
203 if (!AddRegDefToWorkList(
MI->getOperand(1).getReg()))
207 case RISCV::PseudoCCADDW:
208 case RISCV::PseudoCCSUBW:
211 if (!AddRegDefToWorkList(
MI->getOperand(4).getReg()))
225 case RISCV::PseudoCCMOVGPR:
226 case RISCV::PseudoCCAND:
227 case RISCV::PseudoCCOR:
228 case RISCV::PseudoCCXOR:
237 unsigned B = 1,
E = 3,
D = 1;
238 switch (
MI->getOpcode()) {
240 E =
MI->getNumOperands();
243 case RISCV::PseudoCCMOVGPR:
247 case RISCV::PseudoCCAND:
248 case RISCV::PseudoCCOR:
249 case RISCV::PseudoCCXOR:
255 for (
unsigned I =
B;
I !=
E;
I +=
D) {
256 if (!
MI->getOperand(
I).isReg())
259 if (!AddRegDefToWorkList(
MI->getOperand(
I).getReg()))
266 case RISCV::VT_MASKC:
267 case RISCV::VT_MASKCN:
270 if (!AddRegDefToWorkList(
MI->getOperand(1).getReg()))
278 if (
MI->getOperand(2).getImm() >= 32)
331 bool MadeChange =
false;
353 if (!
MRI.constrainRegClass(SrcReg,
MRI.getRegClass(DstReg)))
359 Fixable->setDesc(
TII.get(
getWOp(Fixable->getOpcode())));
360 Fixable->clearFlag(MachineInstr::MIFlag::NoSWrap);
361 Fixable->clearFlag(MachineInstr::MIFlag::NoUWrap);
362 Fixable->clearFlag(MachineInstr::MIFlag::IsExact);
364 ++NumTransformedToWInstrs;
368 MRI.replaceRegWith(DstReg, SrcReg);
369 MRI.clearKillFlags(SrcReg);
370 MI->eraseFromParent();
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool isSignExtendingOpW(const MachineInstr &MI, const MachineRegisterInfo &MRI)
static bool isSignExtendedW(Register SrcReg, const MachineRegisterInfo &MRI, const RISCVInstrInfo &TII, SmallPtrSetImpl< MachineInstr * > &FixableDef)
static cl::opt< bool > DisableSExtWRemoval("riscv-disable-sextw-removal", cl::desc("Disable removal of sext.w"), cl::init(false), cl::Hidden)
static unsigned getWOp(unsigned Opcode)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
FunctionPass class - This class is used to implement most global optimizations.
instr_iterator instr_begin()
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
Representation of each machine instruction.
bool isCall(QueryType Type=AnyInBundle) const
const MachineOperand & getOperand(unsigned i) const
const GlobalValue * getGlobal() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isLiveIn(Register Reg) const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
RISCVMachineFunctionInfo - This class is derived from MachineFunctionInfo and contains private RISCV-...
bool isSExt32Register(Register Reg) const
Wrapper class representing virtual and physical registers.
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
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.
StringRef - Represent a constant reference to a string, i.e.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
bool isSEXT_W(const MachineInstr &MI)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void initializeRISCVSExtWRemovalPass(PassRegistry &)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr unsigned BitWidth
FunctionPass * createRISCVSExtWRemovalPass()