29#define DEBUG_TYPE "localizer"
60 LocalizedSetVecT &LocalizedInstrs);
63 bool localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs);
74 "Move/duplicate certain instructions close to their use",
78 "Move/duplicate certain instructions close to their use",
102 return InsertMBB == Def.getParent();
111 unsigned NumUses = 0;
112 for (
unsigned I = 0, NumVals =
MI->getNumIncomingValues();
I < NumVals; ++
I) {
113 if (
MI->getIncomingValue(
I) == SrcReg)
120 LocalizedSetVecT &LocalizedInstrs) {
122 DenseMap<std::pair<MachineBasicBlock *, Register>,
Register> MBBWithLocalDef;
131 if (!TL.shouldLocalize(
MI,
TTI))
134 assert(
MI.getDesc().getNumDefs() == 1 &&
135 "More than one definition not supported yet");
140 for (MachineOperand &MOUse :
143 MachineBasicBlock *InsertMBB;
145 dbgs() <<
"Checking use: " << MIUse
147 if (isLocalUse(MOUse,
MI, InsertMBB)) {
151 LocalizedInstrs.insert(&
MI);
158 unsigned NumPhiUses = getNumPhiUses(MOUse);
159 const unsigned PhiThreshold = 2;
160 if (NumPhiUses > PhiThreshold)
165 auto MBBAndReg = std::make_pair(InsertMBB,
Reg);
166 auto NewVRegIt = MBBWithLocalDef.
find(MBBAndReg);
167 if (NewVRegIt == MBBWithLocalDef.
end()) {
169 MachineInstr *LocalizedMI = MF.CloneMachineInstr(&
MI);
170 LocalizedInstrs.
insert(LocalizedMI);
182 MBBWithLocalDef.
try_emplace(MBBAndReg, NewReg).first;
188 MOUse.
setReg(NewVRegIt->second);
194bool LocalizerImpl::localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs) {
203 for (MachineInstr *
MI : LocalizedInstrs) {
207 SmallPtrSet<MachineInstr *, 32>
Users;
233 SmallVector<MachineInstr *, 4> DbgUsers;
234 SmallVector<MachineInstr *, 4> DbgUsersToCopy;
235 for (MachineInstr &DbgMI :
237 if (!DbgMI.isDebugValue())
239 erase_if(DbgUsersToCopy, [&](
const MachineInstr *Earlier) {
242 DbgMI.getDebugLoc().getInlinedAt() &&
244 DbgMI.getDebugExpression());
246 if (DbgMI.hasDebugOperandForReg(
Reg)) {
252 MI->removeFromParent();
256 for (MachineInstr *DbgMI : DbgUsersToCopy)
258 for (MachineInstr *DbgMI : DbgUsers)
259 DbgMI->setDebugValueUndef();
263 if (
Users.size() == 1) {
264 const auto &DefDL =
MI->getDebugLoc();
265 const auto &UserDL = (*
Users.begin())->getDebugLoc();
267 if ((!DefDL || DefDL.getLine() == 0) && UserDL && UserDL.getLine() != 0) {
268 MI->setDebugLoc(UserDL);
275bool LocalizerImpl::runOnMachineFunction(
287 LocalizedSetVecT LocalizedInstrs;
289 bool Changed = localizeInterBlock(MF, LocalizedInstrs);
290 Changed |= localizeIntraBlock(LocalizedInstrs);
296 return Impl.runOnMachineFunction(MF, [&]() {
306 bool Changed = Impl.runOnMachineFunction(MF, [&]() {
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
iv Induction Variable Users
Promote Memory to Register
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file implements a set that has insertion order iteration characteristics.
This file describes how to lower LLVM code to machine code.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
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.
static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B)
Check if fragments overlap between a pair of FragmentInfos.
LLVM_ABI DILocation * getInlinedAt() const
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
This pass implements the localization mechanism described at the top of this file.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
An RAII based helper class to modify MachineFunctionProperties when running pass.
LLVM_ABI iterator getFirstTerminatorForward()
Finds the first terminator in a block by scanning forward.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass(char &ID)
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
void insert(iterator MBBI, MachineBasicBlock *MBB)
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
LLVM_ABI const DILocalVariable * getDebugVariable() const
Return the debug variable referenced by this DBG_VALUE instruction.
LLVM_ABI const DIExpression * getDebugExpression() const
Return the complex address expression referenced by this DBG_VALUE instruction.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
LLVM_ABI Register cloneVirtualRegister(Register VReg, StringRef Name="")
Create and return a new virtual register in the function with the same attributes as the given regist...
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Wrapper class representing virtual and physical registers.
A SetVector that performs no allocations if smaller than a certain size.
void push_back(const T &Elt)
Analysis pass providing the TargetTransformInfo.
virtual const TargetLowering * getTargetLowering() const
An efficient, type-erasing, non-owning reference to a callable.
Pass manager infrastructure for declaring and invalidating analyses.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
DWARFExpression::Operation Op
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.