#include "llvm/CodeGen/MachineCSE.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/MCRegister.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/Pass.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/RecyclingAllocator.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <iterator>
#include <utility>
usingnamespacellvm;
#define DEBUG_TYPE …
STATISTIC(NumCoalesces, "Number of copies coalesced");
STATISTIC(NumCSEs, "Number of common subexpression eliminated");
STATISTIC(NumPREs, "Number of partial redundant expression"
" transformed to fully redundant");
STATISTIC(NumPhysCSEs,
"Number of physreg referencing common subexpr eliminated");
STATISTIC(NumCrossBBCSEs,
"Number of cross-MBB physreg referencing CS eliminated");
STATISTIC(NumCommutes, "Number of copies coalesced after commuting");
static cl::opt<int>
CSUsesThreshold("csuses-threshold", cl::Hidden, cl::init(1024),
cl::desc("Threshold for the size of CSUses"));
static cl::opt<bool> AggressiveMachineCSE(
"aggressive-machine-cse", cl::Hidden, cl::init(false),
cl::desc("Override the profitability heuristics for Machine CSE"));
namespace {
class MachineCSEImpl { … };
class MachineCSELegacy : public MachineFunctionPass { … };
}
char MachineCSELegacy::ID = …;
char &llvm::MachineCSELegacyID = …;
INITIALIZE_PASS_BEGIN(MachineCSELegacy, DEBUG_TYPE,
"Machine Common Subexpression Elimination", false, false)
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_END(MachineCSELegacy, DEBUG_TYPE,
"Machine Common Subexpression Elimination", false, false)
bool MachineCSEImpl::PerformTrivialCopyPropagation(MachineInstr *MI,
MachineBasicBlock *MBB) { … }
bool MachineCSEImpl::isPhysDefTriviallyDead(
MCRegister Reg, MachineBasicBlock::const_iterator I,
MachineBasicBlock::const_iterator E) const { … }
static bool isCallerPreservedOrConstPhysReg(MCRegister Reg,
const MachineOperand &MO,
const MachineFunction &MF,
const TargetRegisterInfo &TRI,
const TargetInstrInfo &TII) { … }
bool MachineCSEImpl::hasLivePhysRegDefUses(const MachineInstr *MI,
const MachineBasicBlock *MBB,
SmallSet<MCRegister, 8> &PhysRefs,
PhysDefVector &PhysDefs,
bool &PhysUseDef) const { … }
bool MachineCSEImpl::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
SmallSet<MCRegister, 8> &PhysRefs,
PhysDefVector &PhysDefs,
bool &NonLocal) const { … }
bool MachineCSEImpl::isCSECandidate(MachineInstr *MI) { … }
bool MachineCSEImpl::isProfitableToCSE(Register CSReg, Register Reg,
MachineBasicBlock *CSBB,
MachineInstr *MI) { … }
void MachineCSEImpl::EnterScope(MachineBasicBlock *MBB) { … }
void MachineCSEImpl::ExitScope(MachineBasicBlock *MBB) { … }
bool MachineCSEImpl::ProcessBlockCSE(MachineBasicBlock *MBB) { … }
void MachineCSEImpl::ExitScopeIfDone(
MachineDomTreeNode *Node,
DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren) { … }
bool MachineCSEImpl::PerformCSE(MachineDomTreeNode *Node) { … }
bool MachineCSEImpl::isPRECandidate(MachineInstr *MI,
SmallSet<MCRegister, 8> &PhysRefs) { … }
bool MachineCSEImpl::ProcessBlockPRE(MachineDominatorTree *DT,
MachineBasicBlock *MBB) { … }
bool MachineCSEImpl::PerformSimplePRE(MachineDominatorTree *DT) { … }
bool MachineCSEImpl::isProfitableToHoistInto(MachineBasicBlock *CandidateBB,
MachineBasicBlock *MBB,
MachineBasicBlock *MBB1) { … }
void MachineCSEImpl::releaseMemory() { … }
bool MachineCSEImpl::run(MachineFunction &MF) { … }
PreservedAnalyses MachineCSEPass::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) { … }
bool MachineCSELegacy::runOnMachineFunction(MachineFunction &MF) { … }