32 #define DEBUG_TYPE "wasm-explicit-locals" 39 cl::desc(
"WebAssembly: Disable emission of get_local/set_local."),
45 return "WebAssembly Explicit Locals";
64 "Convert registers to WebAssembly locals",
false,
false)
67 return new WebAssemblyExplicitLocals();
73 unsigned &CurLocal,
unsigned Reg) {
74 auto P = Reg2Local.
insert(std::make_pair(Reg, CurLocal));
77 return P.first->second;
82 if (RC == &WebAssembly::I32RegClass)
83 return WebAssembly::DROP_I32;
84 if (RC == &WebAssembly::I64RegClass)
85 return WebAssembly::DROP_I64;
86 if (RC == &WebAssembly::F32RegClass)
87 return WebAssembly::DROP_F32;
88 if (RC == &WebAssembly::F64RegClass)
89 return WebAssembly::DROP_F64;
90 if (RC == &WebAssembly::V128RegClass)
91 return WebAssembly::DROP_V128;
92 if (RC == &WebAssembly::EXCEPT_REFRegClass)
93 return WebAssembly::DROP_EXCEPT_REF;
99 if (RC == &WebAssembly::I32RegClass)
100 return WebAssembly::GET_LOCAL_I32;
101 if (RC == &WebAssembly::I64RegClass)
102 return WebAssembly::GET_LOCAL_I64;
103 if (RC == &WebAssembly::F32RegClass)
104 return WebAssembly::GET_LOCAL_F32;
105 if (RC == &WebAssembly::F64RegClass)
106 return WebAssembly::GET_LOCAL_F64;
107 if (RC == &WebAssembly::V128RegClass)
108 return WebAssembly::GET_LOCAL_V128;
109 if (RC == &WebAssembly::EXCEPT_REFRegClass)
110 return WebAssembly::GET_LOCAL_EXCEPT_REF;
116 if (RC == &WebAssembly::I32RegClass)
117 return WebAssembly::SET_LOCAL_I32;
118 if (RC == &WebAssembly::I64RegClass)
119 return WebAssembly::SET_LOCAL_I64;
120 if (RC == &WebAssembly::F32RegClass)
121 return WebAssembly::SET_LOCAL_F32;
122 if (RC == &WebAssembly::F64RegClass)
123 return WebAssembly::SET_LOCAL_F64;
124 if (RC == &WebAssembly::V128RegClass)
125 return WebAssembly::SET_LOCAL_V128;
126 if (RC == &WebAssembly::EXCEPT_REFRegClass)
127 return WebAssembly::SET_LOCAL_EXCEPT_REF;
133 if (RC == &WebAssembly::I32RegClass)
134 return WebAssembly::TEE_LOCAL_I32;
135 if (RC == &WebAssembly::I64RegClass)
136 return WebAssembly::TEE_LOCAL_I64;
137 if (RC == &WebAssembly::F32RegClass)
138 return WebAssembly::TEE_LOCAL_F32;
139 if (RC == &WebAssembly::F64RegClass)
140 return WebAssembly::TEE_LOCAL_F64;
141 if (RC == &WebAssembly::V128RegClass)
142 return WebAssembly::TEE_LOCAL_V128;
143 if (RC == &WebAssembly::EXCEPT_REFRegClass)
144 return WebAssembly::TEE_LOCAL_EXCEPT_REF;
150 if (RC == &WebAssembly::I32RegClass)
152 if (RC == &WebAssembly::I64RegClass)
154 if (RC == &WebAssembly::F32RegClass)
156 if (RC == &WebAssembly::F64RegClass)
158 if (RC == &WebAssembly::EXCEPT_REFRegClass)
183 bool WebAssemblyExplicitLocals::runOnMachineFunction(
MachineFunction &MF) {
184 LLVM_DEBUG(
dbgs() <<
"********** Make Locals Explicit **********\n" 185 "********** Function: " 192 bool Changed =
false;
215 unsigned CurLocal = MFI.
getParams().size();
243 unsigned LocalId =
getLocalId(Reg2Local, CurLocal, OldReg);
275 if (MI.
getOpcode() == WebAssembly::IMPLICIT_DEF) {
288 unsigned LocalId =
getLocalId(Reg2Local, CurLocal, OldReg);
309 unsigned OldReg = MO.getReg();
316 unsigned LocalId =
getLocalId(Reg2Local, CurLocal, OldReg);
332 unsigned LocalId =
getLocalId(Reg2Local, CurLocal, OldReg);
339 unsigned LocalId =
getLocalId(Reg2Local, CurLocal, OldReg);
366 auto I = Reg2Local.
find(Reg);
379 if (
MI.isDebugInstr() ||
MI.isLabel())
383 (!MO.isReg() || MRI.
use_empty(MO.getReg()) ||
385 "WebAssemblyExplicitLocals failed to stackify a register operand");
static unsigned getLocalId(DenseMap< unsigned, unsigned > &Reg2Local, unsigned &CurLocal, unsigned Reg)
Return a local id number for the given register, assigning it a new one if it doesn't yet have one...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool isLabel() const
Returns true if the MachineInstr represents a label.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
void removeRegOperandFromUseList(MachineOperand *MO)
Remove MO from its use-def list.
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
Compute iterated dominance frontiers using a linear time algorithm.
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
unsigned getReg() const
getReg - Returns the register number.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
void setIsDead(bool Val=true)
static unsigned getSetLocalOpcode(const TargetRegisterClass *RC)
Get the appropriate set_local opcode for the given register class.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
INLINEASM - Represents an inline asm block.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
static unsigned getDropOpcode(const TargetRegisterClass *RC)
Get the appropriate drop opcode for the given register class.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
static MachineInstr * FindStartOfTree(MachineOperand &MO, MachineRegisterInfo &MRI, WebAssemblyFunctionInfo &MFI)
Given a MachineOperand of a stackified vreg, return the instruction at the start of the expression tr...
bool isVRegStackified(unsigned VReg) const
bool isCopy(const MachineInstr &MI)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
iterator find(const_arg_type_t< KeyT > Val)
This file contains the declaration of the WebAssembly-specific utility functions. ...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file provides WebAssembly-specific target descriptions.
void setNumLocals(size_t NumLocals)
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
static unsigned getGetLocalOpcode(const TargetRegisterClass *RC)
Get the appropriate get_local opcode for the given register class.
FunctionPass class - This class is used to implement most global optimizations.
void stackifyVReg(unsigned VReg)
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
static cl::opt< bool > DisableWebAssemblyExplicitLocals("disable-wasm-explicit-locals", cl::ReallyHidden, cl::desc("WebAssembly: Disable emission of get_local/set_local."), cl::init(false))
bool isDebugInstr() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
iterator_range< mop_iterator > explicit_uses()
void setIsKill(bool Val=true)
This file declares the WebAssembly-specific subclass of TargetSubtarget.
bool isTee(const MachineInstr &MI)
bool isArgument(const MachineInstr &MI)
MachineOperand class - Representation of each machine instruction operand.
void setLocal(size_t i, MVT VT)
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static unsigned getTeeLocalOpcode(const TargetRegisterClass *RC)
Get the appropriate tee_local opcode for the given register class.
INITIALIZE_PASS(WebAssemblyExplicitLocals, DEBUG_TYPE, "Convert registers to WebAssembly locals", false, false) FunctionPass *llvm
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
static MachineOperand CreateImm(int64_t Val)
This file declares WebAssembly-specific per-machine-function information.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static MVT typeForRegClass(const TargetRegisterClass *RC)
Get the type associated with the given register class.
StringRef - Represent a constant reference to a string, i.e.
const std::vector< MVT > & getParams() const
const MachineOperand & getOperand(unsigned i) const
FunctionPass * createWebAssemblyExplicitLocals()
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...