40 #define DEBUG_TYPE "wasm-reg-stackify"
45 return "WebAssembly Register Stackify";
71 return new WebAssemblyRegStackify();
99 const auto *RegClass =
101 if (RegClass == &WebAssembly::I32RegClass) {
102 MI->
setDesc(TII->
get(WebAssembly::CONST_I32));
104 }
else if (RegClass == &WebAssembly::I64RegClass) {
105 MI->
setDesc(TII->
get(WebAssembly::CONST_I64));
107 }
else if (RegClass == &WebAssembly::F32RegClass) {
108 MI->
setDesc(TII->
get(WebAssembly::CONST_F32));
112 }
else if (RegClass == &WebAssembly::F64RegClass) {
113 MI->
setDesc(TII->
get(WebAssembly::CONST_F64));
126 bool &Write,
bool &Effects,
bool &StackPointer) {
133 if (
const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
134 if (!GA->isInterposable())
135 GV = GA->getAliasee();
137 if (
const Function *
F = dyn_cast<Function>(GV)) {
138 if (!
F->doesNotThrow())
140 if (
F->doesNotAccessMemory())
142 if (
F->onlyReadsMemory()) {
158 bool &Write,
bool &Effects,
bool &StackPointer) {
179 dyn_cast<ExternalSymbolPseudoSourceValue>(PSV))
180 if (
StringRef(EPSV->getSymbol()) ==
"__stack_pointer") {
187 case WebAssembly::DIV_S_I32:
case WebAssembly::DIV_S_I64:
188 case WebAssembly::REM_S_I32:
case WebAssembly::REM_S_I64:
189 case WebAssembly::DIV_U_I32:
case WebAssembly::DIV_U_I64:
190 case WebAssembly::REM_U_I32:
case WebAssembly::REM_U_I64:
191 case WebAssembly::I32_TRUNC_S_F32:
case WebAssembly::I64_TRUNC_S_F32:
192 case WebAssembly::I32_TRUNC_S_F64:
case WebAssembly::I64_TRUNC_S_F64:
193 case WebAssembly::I32_TRUNC_U_F32:
case WebAssembly::I64_TRUNC_U_F32:
194 case WebAssembly::I32_TRUNC_U_F64:
case WebAssembly::I64_TRUNC_U_F64:
214 case WebAssembly::DIV_S_I32:
case WebAssembly::DIV_S_I64:
215 case WebAssembly::REM_S_I32:
case WebAssembly::REM_S_I64:
216 case WebAssembly::DIV_U_I32:
case WebAssembly::DIV_U_I64:
217 case WebAssembly::REM_U_I32:
case WebAssembly::REM_U_I64:
218 case WebAssembly::I32_TRUNC_S_F32:
case WebAssembly::I64_TRUNC_S_F32:
219 case WebAssembly::I32_TRUNC_S_F64:
case WebAssembly::I64_TRUNC_S_F64:
220 case WebAssembly::I32_TRUNC_U_F32:
case WebAssembly::I64_TRUNC_U_F32:
221 case WebAssembly::I32_TRUNC_U_F64:
case WebAssembly::I64_TRUNC_U_F64:
236 case WebAssembly::CALL_VOID:
237 case WebAssembly::CALL_INDIRECT_VOID:
238 QueryCallee(MI, 0, Read, Write, Effects, StackPointer);
240 case WebAssembly::CALL_I32:
case WebAssembly::CALL_I64:
241 case WebAssembly::CALL_F32:
case WebAssembly::CALL_F64:
242 case WebAssembly::CALL_INDIRECT_I32:
case WebAssembly::CALL_INDIRECT_I64:
243 case WebAssembly::CALL_INDIRECT_F32:
case WebAssembly::CALL_INDIRECT_F64:
244 QueryCallee(MI, 1, Read, Write, Effects, StackPointer);
255 return Def.
isAsCheapAsAMove() && TII->isTriviallyReMaterializable(Def, &AA);
294 if (Result.valueIn() == DefVNI) {
295 if (!Result.isKill())
317 if (!MO.isReg() || MO.isUndef())
319 unsigned Reg = MO.getReg();
329 if (Reg == WebAssembly::ARGUMENTS)
342 MutableRegisters.push_back(Reg);
345 bool Read =
false, Write =
false, Effects =
false, StackPointer =
false;
346 Query(*Def, AA, Read, Write, Effects, StackPointer);
350 bool HasMutableRegisters = !MutableRegisters.empty();
351 if (!Read && !Write && !Effects && !StackPointer && !HasMutableRegisters)
356 for (--I; I !=
D; --
I) {
357 bool InterveningRead =
false;
358 bool InterveningWrite =
false;
359 bool InterveningEffects =
false;
360 bool InterveningStackPointer =
false;
361 Query(*I, AA, InterveningRead, InterveningWrite, InterveningEffects,
362 InterveningStackPointer);
363 if (Effects && InterveningEffects)
365 if (Read && InterveningWrite)
367 if (Write && (InterveningRead || InterveningWrite))
369 if (StackPointer && InterveningStackPointer)
372 for (
unsigned Reg : MutableRegisters)
374 if (MO.isReg() && MO.isDef() && MO.getReg() ==
Reg)
400 if (UseVNI != OneUseVNI)
404 if (UseInst == OneUseInst) {
411 while (!MDT.
dominates(OneUseInst, UseInst)) {
422 unsigned DefReg = MO.
getReg();
429 if (NewUseInst == OneUseInst) {
430 if (&OneUse > &NewUse)
434 UseInst = NewUseInst;
443 if (RC == &WebAssembly::I32RegClass)
444 return WebAssembly::TEE_I32;
445 if (RC == &WebAssembly::I64RegClass)
446 return WebAssembly::TEE_I64;
447 if (RC == &WebAssembly::F32RegClass)
448 return WebAssembly::TEE_F32;
449 if (RC == &WebAssembly::F64RegClass)
450 return WebAssembly::TEE_F64;
451 if (RC == &WebAssembly::V128RegClass)
452 return WebAssembly::TEE_V128;
474 MBB.
splice(Insert, &MBB, Def);
517 TII->reMaterialize(MBB, Insert, NewReg, 0, Def, *TRI);
537 DEBUG(
dbgs() <<
" - Deleting original\n");
575 MBB.
splice(Insert, &MBB, Def);
616 class TreeWalkerState {
618 typedef std::reverse_iterator<mop_iterator> mop_reverse_iterator;
626 Worklist.push_back(
reverse(Range));
629 bool Done()
const {
return Worklist.empty(); }
632 RangeTy &Range = Worklist.back();
635 if (Range.begin() == Range.end())
637 assert((Worklist.empty() ||
638 Worklist.back().begin() != Worklist.back().end()) &&
639 "Empty ranges shouldn't remain in the worklist");
646 if (Range.begin() != Range.end())
647 Worklist.push_back(
reverse(Range));
653 assert(HasRemainingOperands(Instr) &&
654 "Reseting operands should only be done when the instruction has "
655 "an operand still on the stack");
661 bool HasRemainingOperands(
const MachineInstr *Instr)
const {
662 if (Worklist.empty())
664 const RangeTy &Range = Worklist.back();
665 return Range.begin() != Range.end() && Range.begin()->getParent() == Instr;
674 bool IsOnStack(
unsigned Reg)
const {
675 for (
const RangeTy &Range : Worklist)
677 if (MO.isReg() && MO.getReg() ==
Reg)
685 class CommutingState {
691 bool TentativelyCommuting;
696 unsigned Operand0, Operand1;
699 CommutingState() : TentativelyCommuting(
false), Declined(
false) {}
704 void MaybeCommute(
MachineInstr *Insert, TreeWalkerState &TreeWalker,
706 if (TentativelyCommuting) {
708 "Don't decline commuting until you've finished trying it");
710 TII->commuteInstruction(*Insert,
false, Operand0, Operand1);
711 TentativelyCommuting =
false;
713 }
else if (!Declined && TreeWalker.HasRemainingOperands(Insert)) {
716 if (TII->findCommutedOpIndices(*Insert, Operand0, Operand1)) {
718 TII->commuteInstruction(*Insert,
false, Operand0, Operand1);
719 TreeWalker.ResetTopOperands(Insert);
720 TentativelyCommuting =
true;
729 TentativelyCommuting =
false;
735 bool WebAssemblyRegStackify::runOnMachineFunction(
MachineFunction &MF) {
736 DEBUG(
dbgs() <<
"********** Register Stackifying **********\n"
737 "********** Function: "
740 bool Changed =
false;
745 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
755 for (
auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
763 if (Insert->
getOpcode() == TargetOpcode::DBG_VALUE)
768 CommutingState Commuting;
769 TreeWalkerState TreeWalker(Insert);
770 while (!TreeWalker.Done()) {
777 unsigned Reg = Op.
getReg();
778 assert(Op.
isUse() &&
"explicit_uses() should only iterate over uses");
780 "explicit_uses() should only iterate over explicit operands");
805 bool SameBlock = Def->
getParent() == &MBB;
806 bool CanMove = SameBlock &&
IsSafeToMove(Def, Insert, AA, MRI) &&
807 !TreeWalker.IsOnStack(Reg);
808 if (CanMove &&
HasOneUse(Reg, Def, MRI, MDT, LIS)) {
814 }
else if (CanMove &&
821 if (!CanMove && SameBlock)
822 Commuting.MaybeCommute(Insert, TreeWalker, TII);
830 if (Insert->
getOpcode() == TargetOpcode::IMPLICIT_DEF)
836 TreeWalker.PushOperands(Insert);
841 if (Insert != &*MII) {
852 MF.getRegInfo().addLiveIn(WebAssembly::VALUE_STACK);
854 MBB.addLiveIn(WebAssembly::VALUE_STACK);
862 if (
MI.isDebugValue())
867 unsigned Reg = MO.getReg();
874 "Register stack pop should be paired with a push");
881 "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.
const GlobalValue * getGlobal() const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
SlotIndex def
The index of the defining instruction.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
void removePhysRegDefAt(unsigned Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Segments::iterator iterator
LiveInterval - This class represents the liveness of a register, or stack slot.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
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...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
bool isVRegStackified(unsigned VReg) const
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void splitSeparateComponents(LiveInterval &LI, SmallVectorImpl< LiveInterval * > &SplitLIs)
Split separate components in LiveInterval LI into separate intervals.
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...
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
bool isDereferenceableInvariantLoad(AliasAnalysis *AA) const
Return true if this load instruction never traps and points to a memory location whose value doesn't ...
AnalysisUsage & addRequired()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static Type * getFloatTy(LLVMContext &C)
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)
static void ImposeStackOrdering(MachineInstr *MI)
A Use represents the edge between a Value definition and its users.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
INLINEASM - Represents an inline asm block.
LLVM_NODISCARD bool empty() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
static bool HasOneUse(unsigned Reg, MachineInstr *Def, MachineRegisterInfo &MRI, MachineDominatorTree &MDT, LiveIntervals &LIS)
bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr * > *dead=nullptr)
After removing some uses of a register, shrink its live range to just the remaining uses...
AnalysisUsage & addPreservedID(const void *ID)
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)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static void ConvertImplicitDefToConstZero(MachineInstr *MI, MachineRegisterInfo &MRI, const TargetInstrInfo *TII, MachineFunction &MF)
const MachineBasicBlock * getParent() const
TargetInstrInfo - Interface to description of machine instruction set.
bool isDebugValue() const
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
Function Alias Analysis false
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineInstrBundleIterator< MachineInstr > iterator
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
unsigned const MachineRegisterInfo * MRI
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
void removeInterval(unsigned Reg)
Interval removal.
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.
bool isAsCheapAsAMove(QueryType Type=AllInBundle) const
Returns true if this instruction has the same cost (or less) than a move instruction.
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
ConstantFP - Floating Point Values [float, double].
This file provides WebAssembly-specific target descriptions.
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...
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
FunctionPass class - This class is used to implement most global optimizations.
self_iterator getIterator()
void stackifyVReg(unsigned VReg)
reverse_iterator getReverse() const
Get a reverse iterator to the same node.
bool liveAt(SlotIndex index) const
This class contains a discriminated union of information about pointers in memory operands...
static void QueryCallee(const MachineInstr &MI, unsigned CalleeOpNo, bool &Read, bool &Write, bool &Effects, bool &StackPointer)
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
bool definesRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr fully defines the specified register.
iterator_range< mop_iterator > explicit_uses()
void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
Remove the specified segment from this range.
bool readsRegister(unsigned Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
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.
static unsigned GetTeeOpcode(const TargetRegisterClass *RC)
Get the appropriate tee opcode for the given register class.
bool isArgument(const MachineInstr &MI)
T get() const
Returns the value of the specified pointer type.
MachineOperand class - Representation of each machine instruction operand.
FunctionPass * createWebAssemblyRegStackify()
bool hasOneUse(unsigned RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
LLVM_NODISCARD T pop_back_val()
static void ShrinkToUses(LiveInterval &LI, LiveIntervals &LIS)
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)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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.
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
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 ...
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
bool hasOneDef(unsigned RegNo) const
Return true if there is exactly one operand defining 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
void setReg(unsigned Reg)
Change the register this operand corresponds to.
static MachineOperand CreateImm(int64_t Val)
bool isCall(QueryType Type=AnyInBundle) const
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.
bool isPhysRegModified(unsigned PhysReg, bool SkipNoReturnDef=false) const
Return true if the specified register is modified in this function.
Instructions::iterator instr_iterator
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
StringRef - Represent a constant reference to a string, i.e.
iterator FindSegmentContaining(SlotIndex Idx)
Return an iterator to the segment that contains the specified index, or end() if there is none...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
iterator_range< decltype(begin(std::declval< T >)))> drop_begin(T &&t, int n)
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
int is() const
Test if the Union currently holds the type matching T.
bool dominates(const MachineDomTreeNode *A, const MachineDomTreeNode *B) const
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
SlotIndex - An opaque wrapper around machine indexes.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
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 ...
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarilly including Idx...
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)
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.