BranchFolding: Use LivePhysReg to update live in lists.

Use LivePhysRegs with a backwards walking algorithm to update live in
lists, this way the results do not depend on the presence of kill flags
anymore.

This patch also reduces the number of registers added as live-in.
Previously all pristine registers as well as all sub registers of a
super register were added resulting in unnecessarily large live in
lists. This fixed https://llvm.org/PR25263.

Differential Revision: http://reviews.llvm.org/D22027

llvm-svn: 275201
This commit is contained in:
Matthias Braun 2016-07-12 18:44:33 +00:00
parent 10531d1020
commit aeab09fb8f
2 changed files with 30 additions and 29 deletions

View File

@ -32,7 +32,6 @@
#include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/RegisterScavenging.h"
#include "llvm/IR/Function.h" #include "llvm/IR/Function.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
@ -212,13 +211,10 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF,
TRI = tri; TRI = tri;
MMI = mmi; MMI = mmi;
MLI = mli; MLI = mli;
RS = nullptr;
// Use a RegScavenger to help update liveness when required.
MachineRegisterInfo &MRI = MF.getRegInfo(); MachineRegisterInfo &MRI = MF.getRegInfo();
if (MRI.tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF)) UpdateLiveIns = MRI.tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF);
RS = new RegScavenger(); if (!UpdateLiveIns)
else
MRI.invalidateLiveness(); MRI.invalidateLiveness();
// Fix CFG. The later algorithms expect it to be right. // Fix CFG. The later algorithms expect it to be right.
@ -249,10 +245,8 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF,
// See if any jump tables have become dead as the code generator // See if any jump tables have become dead as the code generator
// did its thing. // did its thing.
MachineJumpTableInfo *JTI = MF.getJumpTableInfo(); MachineJumpTableInfo *JTI = MF.getJumpTableInfo();
if (!JTI) { if (!JTI)
delete RS;
return MadeChange; return MadeChange;
}
// Walk the function to find jump tables that are live. // Walk the function to find jump tables that are live.
BitVector JTIsLive(JTI->getJumpTables().size()); BitVector JTIsLive(JTI->getJumpTables().size());
@ -274,7 +268,6 @@ bool BranchFolder::OptimizeFunction(MachineFunction &MF,
MadeChange = true; MadeChange = true;
} }
delete RS;
return MadeChange; return MadeChange;
} }
@ -406,15 +399,27 @@ static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1,
return TailLen; return TailLen;
} }
void BranchFolder::MaintainLiveIns(MachineBasicBlock *CurMBB, void BranchFolder::computeLiveIns(MachineBasicBlock &MBB) {
MachineBasicBlock *NewMBB) { if (!UpdateLiveIns)
if (RS) { return;
RS->enterBasicBlock(*CurMBB);
if (!CurMBB->empty()) LiveRegs.init(TRI);
RS->forward(std::prev(CurMBB->end())); LiveRegs.addLiveOutsNoPristines(MBB);
for (unsigned int i = 1, e = TRI->getNumRegs(); i != e; i++) for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
if (RS->isRegUsed(i, false)) LiveRegs.stepBackward(MI);
NewMBB->addLiveIn(i);
for (unsigned Reg : LiveRegs) {
// Skip the register if we are about to add one of its super registers.
bool ContainsSuperReg = false;
for (MCSuperRegIterator SReg(Reg, TRI); SReg.isValid(); ++SReg) {
if (LiveRegs.contains(*SReg)) {
ContainsSuperReg = true;
break;
}
}
if (ContainsSuperReg)
continue;
MBB.addLiveIn(Reg);
} }
} }
@ -422,12 +427,9 @@ void BranchFolder::MaintainLiveIns(MachineBasicBlock *CurMBB,
/// after it, replacing it with an unconditional branch to NewDest. /// after it, replacing it with an unconditional branch to NewDest.
void BranchFolder::ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, void BranchFolder::ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
MachineBasicBlock *NewDest) { MachineBasicBlock *NewDest) {
MachineBasicBlock *CurMBB = OldInst->getParent();
TII->ReplaceTailWithBranchTo(OldInst, NewDest); TII->ReplaceTailWithBranchTo(OldInst, NewDest);
// For targets that use the register scavenger, we must maintain LiveIns. computeLiveIns(*NewDest);
MaintainLiveIns(CurMBB, NewDest);
++NumTailMerge; ++NumTailMerge;
} }
@ -465,8 +467,7 @@ MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
// NewMBB inherits CurMBB's block frequency. // NewMBB inherits CurMBB's block frequency.
MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB)); MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB));
// For targets that use the register scavenger, we must maintain LiveIns. computeLiveIns(*NewMBB);
MaintainLiveIns(&CurMBB, NewMBB);
// Add the new block to the funclet. // Add the new block to the funclet.
const auto &FuncletI = FuncletMembership.find(&CurMBB); const auto &FuncletI = FuncletMembership.find(&CurMBB);

View File

@ -11,6 +11,7 @@
#define LLVM_LIB_CODEGEN_BRANCHFOLDING_H #define LLVM_LIB_CODEGEN_BRANCHFOLDING_H
#include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallPtrSet.h"
#include "llvm/CodeGen/LivePhysRegs.h"
#include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/Support/BlockFrequency.h" #include "llvm/Support/BlockFrequency.h"
#include <vector> #include <vector>
@ -21,7 +22,6 @@ namespace llvm {
class MachineFunction; class MachineFunction;
class MachineModuleInfo; class MachineModuleInfo;
class MachineLoopInfo; class MachineLoopInfo;
class RegScavenger;
class TargetInstrInfo; class TargetInstrInfo;
class TargetRegisterInfo; class TargetRegisterInfo;
@ -98,11 +98,12 @@ namespace llvm {
bool AfterBlockPlacement; bool AfterBlockPlacement;
bool EnableTailMerge; bool EnableTailMerge;
bool EnableHoistCommonCode; bool EnableHoistCommonCode;
bool UpdateLiveIns;
const TargetInstrInfo *TII; const TargetInstrInfo *TII;
const TargetRegisterInfo *TRI; const TargetRegisterInfo *TRI;
MachineModuleInfo *MMI; MachineModuleInfo *MMI;
MachineLoopInfo *MLI; MachineLoopInfo *MLI;
RegScavenger *RS; LivePhysRegs LiveRegs;
public: public:
/// \brief This class keeps track of branch frequencies of newly created /// \brief This class keeps track of branch frequencies of newly created
@ -130,8 +131,7 @@ namespace llvm {
bool TryTailMergeBlocks(MachineBasicBlock* SuccBB, bool TryTailMergeBlocks(MachineBasicBlock* SuccBB,
MachineBasicBlock* PredBB); MachineBasicBlock* PredBB);
void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB); void setCommonTailEdgeWeights(MachineBasicBlock &TailMBB);
void MaintainLiveIns(MachineBasicBlock *CurMBB, void computeLiveIns(MachineBasicBlock &MBB);
MachineBasicBlock *NewMBB);
void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
MachineBasicBlock *NewDest); MachineBasicBlock *NewDest);
MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB, MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,