40 #define DEBUG_TYPE "wasm-reg-stackify" 45 return "WebAssembly Register Stackify";
71 "Reorder instructions to use the WebAssembly value stack",
75 return new WebAssemblyRegStackify();
103 const auto *RegClass =
105 if (RegClass == &WebAssembly::I32RegClass) {
106 MI->
setDesc(TII->
get(WebAssembly::CONST_I32));
108 }
else if (RegClass == &WebAssembly::I64RegClass) {
109 MI->
setDesc(TII->
get(WebAssembly::CONST_I64));
111 }
else if (RegClass == &WebAssembly::F32RegClass) {
112 MI->
setDesc(TII->
get(WebAssembly::CONST_F32));
116 }
else if (RegClass == &WebAssembly::F64RegClass) {
117 MI->
setDesc(TII->
get(WebAssembly::CONST_F64));
130 bool &Write,
bool &Effects,
bool &StackPointer) {
137 if (
const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
138 if (!GA->isInterposable())
139 GV = GA->getAliasee();
141 if (
const Function *
F = dyn_cast<Function>(GV)) {
142 if (!
F->doesNotThrow())
144 if (
F->doesNotAccessMemory())
146 if (
F->onlyReadsMemory()) {
162 bool &Write,
bool &Effects,
bool &StackPointer) {
182 dyn_cast<ExternalSymbolPseudoSourceValue>(PSV))
183 if (
StringRef(EPSV->getSymbol()) ==
"__stack_pointer") {
190 case WebAssembly::DIV_S_I32:
case WebAssembly::DIV_S_I64:
191 case WebAssembly::REM_S_I32:
case WebAssembly::REM_S_I64:
192 case WebAssembly::DIV_U_I32:
case WebAssembly::DIV_U_I64:
193 case WebAssembly::REM_U_I32:
case WebAssembly::REM_U_I64:
194 case WebAssembly::I32_TRUNC_S_F32:
case WebAssembly::I64_TRUNC_S_F32:
195 case WebAssembly::I32_TRUNC_S_F64:
case WebAssembly::I64_TRUNC_S_F64:
196 case WebAssembly::I32_TRUNC_U_F32:
case WebAssembly::I64_TRUNC_U_F32:
197 case WebAssembly::I32_TRUNC_U_F64:
case WebAssembly::I64_TRUNC_U_F64:
217 case WebAssembly::DIV_S_I32:
case WebAssembly::DIV_S_I64:
218 case WebAssembly::REM_S_I32:
case WebAssembly::REM_S_I64:
219 case WebAssembly::DIV_U_I32:
case WebAssembly::DIV_U_I64:
220 case WebAssembly::REM_U_I32:
case WebAssembly::REM_U_I64:
221 case WebAssembly::I32_TRUNC_S_F32:
case WebAssembly::I64_TRUNC_S_F32:
222 case WebAssembly::I32_TRUNC_S_F64:
case WebAssembly::I64_TRUNC_S_F64:
223 case WebAssembly::I32_TRUNC_U_F32:
case WebAssembly::I64_TRUNC_U_F32:
224 case WebAssembly::I32_TRUNC_U_F64:
case WebAssembly::I64_TRUNC_U_F64:
239 case WebAssembly::CALL_VOID:
240 case WebAssembly::CALL_INDIRECT_VOID:
241 QueryCallee(MI, 0, Read, Write, Effects, StackPointer);
243 case WebAssembly::CALL_I32:
case WebAssembly::CALL_I64:
244 case WebAssembly::CALL_F32:
case WebAssembly::CALL_F64:
245 case WebAssembly::CALL_INDIRECT_I32:
case WebAssembly::CALL_INDIRECT_I64:
246 case WebAssembly::CALL_INDIRECT_F32:
case WebAssembly::CALL_INDIRECT_F64:
247 QueryCallee(MI, 1, Read, Write, Effects, StackPointer);
258 return Def.
isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def, &AA);
297 if (
Result.valueIn() == DefVNI) {
320 if (!MO.isReg() || MO.isUndef())
322 unsigned Reg = MO.getReg();
332 if (Reg == WebAssembly::ARGUMENTS)
345 MutableRegisters.push_back(Reg);
348 bool Read =
false, Write =
false, Effects =
false, StackPointer =
false;
349 Query(*Def, AA, Read, Write, Effects, StackPointer);
353 bool HasMutableRegisters = !MutableRegisters.empty();
354 if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters)
359 for (--I; I !=
D; --
I) {
360 bool InterveningRead =
false;
361 bool InterveningWrite =
false;
362 bool InterveningEffects =
false;
363 bool InterveningStackPointer =
false;
364 Query(*I, AA, InterveningRead, InterveningWrite, InterveningEffects,
365 InterveningStackPointer);
366 if (Effects && InterveningEffects)
368 if (Read && InterveningWrite)
370 if (Write && (InterveningRead || InterveningWrite))
372 if (StackPointer && InterveningStackPointer)
375 for (
unsigned Reg : MutableRegisters)
377 if (MO.isReg() && MO.isDef() && MO.getReg() ==
Reg)
403 if (UseVNI != OneUseVNI)
407 if (UseInst == OneUseInst) {
414 while (!MDT.
dominates(OneUseInst, UseInst)) {
425 unsigned DefReg = MO.
getReg();
432 if (NewUseInst == OneUseInst) {
433 if (&OneUse > &NewUse)
437 UseInst = NewUseInst;
446 if (RC == &WebAssembly::I32RegClass)
447 return WebAssembly::TEE_I32;
448 if (RC == &WebAssembly::I64RegClass)
449 return WebAssembly::TEE_I64;
450 if (RC == &WebAssembly::F32RegClass)
451 return WebAssembly::TEE_F32;
452 if (RC == &WebAssembly::F64RegClass)
453 return WebAssembly::TEE_F64;
454 if (RC == &WebAssembly::V128RegClass)
455 return WebAssembly::TEE_V128;
477 MBB.
splice(Insert, &MBB, Def);
520 TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI);
578 MBB.
splice(Insert, &MBB, Def);
619 class TreeWalkerState {
621 typedef std::reverse_iterator<mop_iterator> mop_reverse_iterator;
632 bool Done()
const {
return Worklist.
empty(); }
635 RangeTy &Range = Worklist.
back();
638 if (Range.begin() == Range.end())
641 Worklist.
back().begin() != Worklist.
back().end()) &&
642 "Empty ranges shouldn't remain in the worklist");
649 if (Range.begin() != Range.end())
656 assert(HasRemainingOperands(Instr) &&
657 "Reseting operands should only be done when the instruction has " 658 "an operand still on the stack");
664 bool HasRemainingOperands(
const MachineInstr *Instr)
const {
665 if (Worklist.
empty())
667 const RangeTy &Range = Worklist.
back();
668 return Range.begin() != Range.end() && Range.begin()->getParent() == Instr;
677 bool IsOnStack(
unsigned Reg)
const {
678 for (
const RangeTy &Range : Worklist)
680 if (MO.isReg() && MO.getReg() ==
Reg)
688 class CommutingState {
694 bool TentativelyCommuting;
699 unsigned Operand0, Operand1;
702 CommutingState() : TentativelyCommuting(
false), Declined(
false) {}
707 void MaybeCommute(
MachineInstr *Insert, TreeWalkerState &TreeWalker,
709 if (TentativelyCommuting) {
711 "Don't decline commuting until you've finished trying it");
713 TII->commuteInstruction(*Insert,
false, Operand0, Operand1);
714 TentativelyCommuting =
false;
716 }
else if (!Declined && TreeWalker.HasRemainingOperands(Insert)) {
719 if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) {
721 TII->commuteInstruction(*Insert,
false, Operand0, Operand1);
722 TreeWalker.ResetTopOperands(Insert);
723 TentativelyCommuting =
true;
732 TentativelyCommuting =
false;
738 bool WebAssemblyRegStackify::runOnMachineFunction(
MachineFunction &MF) {
739 LLVM_DEBUG(
dbgs() <<
"********** Register Stackifying **********\n" 740 "********** Function: " 743 bool Changed =
false;
748 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
758 for (
auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
766 if (Insert->
getOpcode() == TargetOpcode::DBG_VALUE)
771 CommutingState Commuting;
772 TreeWalkerState TreeWalker(Insert);
773 while (!TreeWalker.Done()) {
781 assert(Op.
isUse() &&
"explicit_uses() should only iterate over uses");
783 "explicit_uses() should only iterate over explicit operands");
808 bool SameBlock = Def->
getParent() == &MBB;
809 bool CanMove = SameBlock &&
IsSafeToMove(Def, Insert, AA, MRI) &&
810 !TreeWalker.IsOnStack(Reg);
811 if (CanMove &&
HasOneUse(Reg, Def, MRI, MDT, LIS)) {
817 }
else if (CanMove &&
824 if (!CanMove && SameBlock)
825 Commuting.MaybeCommute(Insert, TreeWalker, TII);
833 if (Insert->
getOpcode() == TargetOpcode::IMPLICIT_DEF)
839 TreeWalker.PushOperands(Insert);
844 if (Insert != &*MII) {
855 MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK);
857 MBB.addLiveIn(WebAssembly::VALUE_STACK);
865 if (
MI.isDebugInstr())
870 unsigned Reg = MO.getReg();
877 "Register stack pop should be paired with a push");
884 "Register stack pushes and pops should be balanced");
static MachineInstr * GetVRegDef(unsigned Reg, const MachineInstr *Insert, const MachineRegisterInfo &MRI, const LiveIntervals &LIS)
void push_back(const T &Elt)
static Type * getDoubleTy(LLVMContext &C)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isCall(QueryType Type=AnyInBundle) const
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
SlotIndex def
The index of the defining instruction.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
Compute iterated dominance frontiers using a linear time algorithm.
void removePhysRegDefAt(unsigned Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
Segments::iterator iterator
bool isPhysRegModified(unsigned PhysReg, bool SkipNoReturnDef=false) const
Return true if the specified register is modified in this function.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LiveInterval - This class represents the liveness of a register, or stack slot.
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
static MachineInstr * RematerializeCheapDef(unsigned Reg, MachineOperand &Op, MachineInstr &Def, MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII, const WebAssemblyRegisterInfo *TRI)
A trivially cloneable instruction; clone it and nest the new copy with the current instruction...
unsigned const TargetRegisterInfo * TRI
bool hasOneDef(unsigned RegNo) const
Return true if there is exactly one operand defining the specified register.
iterator_range< mmo_iterator > memoperands()
VNInfo - Value Number Information.
iterator_range< mop_iterator > operands()
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
FunctionPass * createWebAssemblyRegStackify()
AnalysisUsage & addRequired()
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static Type * getFloatTy(LLVMContext &C)
static void ImposeStackOrdering(MachineInstr *MI)
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
INLINEASM - Represents an inline asm block.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
static bool HasOneUse(unsigned Reg, MachineInstr *Def, MachineRegisterInfo &MRI, MachineDominatorTree &MDT, LiveIntervals &LIS)
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
AnalysisUsage & addPreservedID(const void *ID)
bool isVRegStackified(unsigned VReg) const
static MachineInstr * MoveAndTeeForMultiUse(unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB, MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI, const WebAssemblyInstrInfo *TII)
A multiple-use def in the same block with no intervening memory or register dependencies; move the de...
static bool ShouldRematerialize(const MachineInstr &Def, AliasAnalysis &AA, const WebAssemblyInstrInfo *TII)
unsigned getUndefRegState(bool B)
bool isDereferenceableInvariantLoad(AliasAnalysis *AA) const
Return true if this load instruction never traps and points to a memory location whose value doesn't ...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static void ConvertImplicitDefToConstZero(MachineInstr *MI, MachineRegisterInfo &MRI, const TargetInstrInfo *TII, MachineFunction &MF)
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
TargetInstrInfo - Interface to description of machine instruction set.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
This file contains the declaration of the WebAssembly-specific utility functions. ...
char & LiveVariablesID
LiveVariables pass - This pass computes the set of blocks in which each variable is life and sets mac...
static const unsigned CommuteAnyOperandIndex
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
MachineInstrBundleIterator< MachineInstr > iterator
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.
void removeInterval(unsigned Reg)
Interval removal.
bool liveAt(SlotIndex index) const
static MachineOperand CreateFPImm(const ConstantFP *CFP)
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
This is an important base class in LLVM.
void splitSeparateComponents(LiveInterval &LI, SmallVectorImpl< LiveInterval *> &SplitLIs)
Split separate components in LiveInterval LI into separate intervals.
const GlobalValue * getGlobal() const
ConstantFP - Floating Point Values [float, double].
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr *> *dead=nullptr)
After removing some uses of a register, shrink its live range to just the remaining uses...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
self_iterator getIterator()
void stackifyVReg(unsigned VReg)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
iterator_range< decltype(adl_begin(std::declval< T >)))> drop_begin(T &&t, int n)
bool isDebugInstr() const
This class contains a discriminated union of information about pointers in memory operands...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static void QueryCallee(const MachineInstr &MI, unsigned CalleeOpNo, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
iterator_range< mop_iterator > explicit_uses()
void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
Remove the specified segment from this range.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
Iterator for intrusive lists based on ilist_node.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
static unsigned GetTeeOpcode(const TargetRegisterClass *RC)
Get the appropriate tee opcode for the given register class.
bool isArgument(const MachineInstr &MI)
MachineOperand class - Representation of each machine instruction operand.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
LLVM_NODISCARD T pop_back_val()
static void ShrinkToUses(LiveInterval &LI, LiveIntervals &LIS)
bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
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:
LiveInterval & getInterval(unsigned Reg)
static bool IsSafeToMove(const MachineInstr *Def, const MachineInstr *Insert, AliasAnalysis &AA, const MachineRegisterInfo &MRI)
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarilly including Idx...
A range adaptor for a pair of iterators.
A specialized pseudo source value for holding external symbol values.
Special value supplied for machine level alias analysis.
bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
const MachineBasicBlock * getParent() const
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.
INITIALIZE_PASS(WebAssemblyRegStackify, DEBUG_TYPE, "Reorder instructions to use the WebAssembly value stack", false, false) FunctionPass *llvm
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
bool hasOneUse(unsigned RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
use_iterator use_begin(unsigned RegNo) const
LLVM_NODISCARD bool empty() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
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.
static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
static bool OneUseDominatesOtherUses(unsigned Reg, const MachineOperand &OneUse, const MachineBasicBlock &MBB, const MachineRegisterInfo &MRI, const MachineDominatorTree &MDT, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI)
Test whether OneUse, a use of Reg, dominates all of Reg's other uses.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
T get() const
Returns the value of the specified pointer type.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
StringRef - Represent a constant reference to a string, i.e.
int is() const
Test if the Union currently holds the type matching T.
iterator FindSegmentContaining(SlotIndex Idx)
Return an iterator to the segment that contains the specified index, or end() if there is none...
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
const MachineOperand & getOperand(unsigned i) const
SlotIndex - An opaque wrapper around machine indexes.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool isAsCheapAsAMove(QueryType Type=AllInBundle) const
Returns true if this instruction has the same cost (or less) than a move instruction.
static MachineInstr * MoveForSingleUse(unsigned Reg, MachineOperand &Op, MachineInstr *Def, MachineBasicBlock &MBB, MachineInstr *Insert, LiveIntervals &LIS, WebAssemblyFunctionInfo &MFI, MachineRegisterInfo &MRI)
A single-use def in the same block with no intervening memory or register dependencies; move the def ...
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void handleMove(MachineInstr &MI, bool UpdateFlags=false)
Call this method to notify LiveIntervals that instruction MI has been moved within a basic block...
LiveInterval & createAndComputeVirtRegInterval(unsigned Reg)