82#define DEBUG_TYPE "riscv-make-compressible"
83#define RISCV_COMPRESS_INSTRS_NAME "RISC-V Make Compressible"
103char RISCVMakeCompressibleLegacy::ID = 0;
108static
unsigned log2LdstWidth(
unsigned Opcode) {
114 case RISCV::QC_E_LBU:
123 case RISCV::QC_E_LHU:
152 case RISCV::QC_E_LBU:
161 case RISCV::QC_E_LHU:
185 return offsetMask(Opcode) << log2LdstWidth(Opcode);
192 switch (log2LdstWidth(Opcode)) {
211 return RISCV::GPRCRegClass.contains(
Reg) ||
212 RISCV::GPRF16CRegClass.contains(
Reg) ||
213 RISCV::GPRF32CRegClass.contains(
Reg) ||
214 RISCV::FPR32CRegClass.contains(
Reg) ||
215 RISCV::FPR64CRegClass.contains(
Reg) ||
216 RISCV::GPRPairCRegClass.contains(
Reg);
223 switch (
MI.getOpcode()) {
230 return STI.hasStdExtZcb();
234 return STI.hasStdExtZca();
236 return STI.hasStdExtZclsd();
238 return !STI.
is64Bit() && STI.hasStdExtZcf();
240 return STI.hasStdExtZcd();
243 case RISCV::QC_E_LBU:
245 case RISCV::QC_E_LHU:
246 return !STI.
is64Bit() && STI.hasVendorXqcilo() && STI.hasVendorXqcilia() &&
249 return !STI.
is64Bit() && STI.hasVendorXqcilo() && STI.hasVendorXqcilia();
257 switch (
MI.getOpcode()) {
263 return STI.hasStdExtZcb();
267 return STI.hasStdExtZca();
269 return STI.hasStdExtZclsd();
271 return !STI.
is64Bit() && STI.hasStdExtZcf();
273 return STI.hasStdExtZcd();
278 return !STI.
is64Bit() && STI.hasVendorXqcilo() && STI.hasVendorXqcilia() &&
281 return !STI.
is64Bit() && STI.hasVendorXqcilo() && STI.hasVendorXqcilia();
298 const unsigned Opcode =
MI.getOpcode();
321 if ((!BaseCompressed || NewBaseAdjust) && SrcDestCompressed)
330 if (!SrcDestCompressed && (BaseCompressed || SrcDest ==
Base) &&
340 switch (
MI.getOpcode()) {
346 case RISCV::QC_E_LBU:
348 case RISCV::QC_E_LHU:
365 MBB.getParent()->getSubtarget().getRegisterInfo();
366 bool XqciloLdSt =
false;
376 if (CandidateRegImm.
Reg == RegImm.Reg &&
377 CandidateRegImm.
Imm == RegImm.Imm) {
387 if (
MI.modifiesRegister(RegImm.Reg,
TRI))
407 unsigned BaseCost = XqciloLdSt ? 2 : 3;
408 unsigned CopyCost = RISCV::GPRPairRegClass.contains(RegImm.Reg) ? 2 : 1;
409 assert((RegImm.Imm == 0 || CopyCost == 1) &&
"GPRPair should have zero imm");
410 if (MIs.
size() <= CopyCost || (RegImm.Imm != 0 && MIs.
size() < BaseCost))
418 if (RISCV::GPRRegClass.
contains(RegImm.Reg))
419 RCToScavenge = &RISCV::GPRCRegClass;
420 else if (RISCV::GPRF16RegClass.
contains(RegImm.Reg))
421 RCToScavenge = &RISCV::GPRF16CRegClass;
422 else if (RISCV::GPRF32RegClass.
contains(RegImm.Reg))
423 RCToScavenge = &RISCV::GPRF32CRegClass;
424 else if (RISCV::FPR32RegClass.
contains(RegImm.Reg))
425 RCToScavenge = &RISCV::FPR32CRegClass;
426 else if (RISCV::FPR64RegClass.
contains(RegImm.Reg))
427 RCToScavenge = &RISCV::FPR64CRegClass;
428 else if (RISCV::GPRPairRegClass.
contains(RegImm.Reg))
429 RCToScavenge = &RISCV::GPRPairCRegClass;
434 RS.enterBasicBlockEnd(
MBB);
435 RS.backward(std::next(MIs.
back()->getIterator()));
436 return RS.scavengeRegisterBackwards(*RCToScavenge, FirstMI.
getIterator(),
444 unsigned Opcode =
MI.getOpcode();
449 "Unsupported instruction for this optimization.");
461 if (MO.isReg() && MO.getReg() == OldRegImm.
Reg) {
490 if (!STI.hasStdExtZca())
499 if (!RegImm.Reg && RegImm.Imm == 0)
511 if (RISCV::GPRRegClass.
contains(RegImm.Reg)) {
524 TII.copyPhysReg(
MBB,
MI,
MI.getDebugLoc(), NewReg, RegImm.Reg,
542 return new RISCVMakeCompressibleLegacy();
545bool RISCVMakeCompressibleLegacy::runOnMachineFunction(
MachineFunction &MF) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool isXqciloLdSt(const MachineInstr &MI)
static bool isCompressibleLoad(const MachineInstr &MI)
#define RISCV_COMPRESS_INSTRS_NAME
static unsigned offsetMask(unsigned Opcode)
static bool isCompressibleStore(const MachineInstr &MI)
static uint8_t compressedLDSTOffsetMask(unsigned Opcode)
static bool isCompressedReg(Register Reg)
static Register analyzeCompressibleUses(MachineInstr &FirstMI, RegImmPair RegImm, SmallVectorImpl< MachineInstr * > &MIs)
static int64_t getBaseAdjustForCompression(int64_t Offset, unsigned Opcode)
static void updateOperands(MachineInstr &MI, RegImmPair OldRegImm, Register NewReg)
static bool compressibleSPOffset(int64_t Offset, unsigned Opcode)
static RegImmPair getRegImmPairPreventingCompression(const MachineInstr &MI)
static bool runMakeCompressibleOpt(MachineFunction &Fn)
This file declares the machine register scavenger class.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Instructions::iterator instr_iterator
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const RISCVInstrInfo * getInstrInfo() const override
Wrapper class representing virtual and physical registers.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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...
self_iterator getIterator()
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionPass * createRISCVMakeCompressibleLegacyPass()
Returns an instance of the Make Compressible Optimization pass.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
MCRegisterClass TargetRegisterClass
Used to describe a register and immediate addition.