48 cl::init(std::numeric_limits<unsigned>::max()));
50 "hexagon-aggressive-rdf-copy",
51 cl::desc(
"Enable aggressive RDF copy propagation with super-register "
63 void getAnalysisUsage(AnalysisUsage &AU)
const override {
65 AU.
addRequired<MachineDominanceFrontierWrapperPass>();
70 StringRef getPassName()
const override {
71 return "Hexagon RDF optimizations";
74 bool runOnMachineFunction(MachineFunction &MF)
override;
76 MachineFunctionProperties getRequiredProperties()
const override {
77 return MachineFunctionProperties().setNoVRegs();
83 MachineDominatorTree *MDT;
84 MachineRegisterInfo *MRI;
87struct HexagonCP :
public CopyPropagation {
88 HexagonCP(DataFlowGraph &
G) : CopyPropagation(
G) {}
90 bool interpretAsCopy(
const MachineInstr *
MI, EqualityMap &EM)
override;
94 HexagonAggressiveCP(DataFlowGraph &
G) : AggressiveCopyPropagation(
G) {}
96 bool interpretAsCopy(
const MachineInstr *
MI, EqualityMap &EM)
override;
99struct HexagonDCE :
public DeadCodeElimination {
100 HexagonDCE(DataFlowGraph &
G, MachineRegisterInfo &MRI)
101 : DeadCodeElimination(
G, MRI) {}
103 bool rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove);
104 void removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum);
111char HexagonRDFOpt::ID = 0;
114 "Hexagon RDF optimizations",
false,
false)
122 EM.insert(std::make_pair(DstR, SrcR));
126 unsigned Opc =
MI->getOpcode();
128 case Hexagon::A2_combinew: {
132 assert(
DstOp.getSubReg() == 0 &&
"Unexpected subregister");
139 case Hexagon::A2_addi: {
141 if (!
A.isImm() ||
A.getImm() != 0)
145 case Hexagon::A2_tfr: {
154 return CopyPropagation::interpretAsCopy(
MI, EM);
159 auto mapRegs = [&EM](RegisterRef DstR, RegisterRef SrcR) ->
void {
160 EM.insert(std::make_pair(DstR, SrcR));
163 DataFlowGraph &DFG = getDFG();
164 const TargetRegisterInfo &
TRI = DFG.
getTRI();
165 unsigned Opc =
MI->getOpcode();
167 case Hexagon::A2_combinew: {
170 const MachineOperand &DstOp =
MI->getOperand(0);
171 const MachineOperand &HiOp =
MI->getOperand(1);
172 const MachineOperand &LoOp =
MI->getOperand(2);
174 unsigned DoubleRegDest =
TRI.getMatchingSuperReg(
175 LoOp.
getReg(), Hexagon::isub_lo, &Hexagon::DoubleRegsRegClass);
176 if (DoubleRegDest != 0 &&
177 TRI.isSuperRegister(HiOp.
getReg(), DoubleRegDest))
185 case Hexagon::A2_addi: {
186 const MachineOperand &
A =
MI->getOperand(2);
187 if (!
A.isImm() ||
A.getImm() != 0)
191 case Hexagon::A2_tfr: {
192 const MachineOperand &DstOp =
MI->getOperand(0);
193 const MachineOperand &SrcOp =
MI->getOperand(1);
203bool HexagonDCE::run() {
204 bool Collected = collect();
208 const SetVector<NodeId> &DeadNodes = getDeadNodes();
209 const SetVector<NodeId> &DeadInstrs = getDeadInstrs();
211 using RefToInstrMap = DenseMap<NodeId, NodeId>;
214 SetVector<NodeId> PartlyDead;
215 DataFlowGraph &DFG = getDFG();
219 NodeAddr<StmtNode*> SA =
TA;
220 for (NodeAddr<RefNode*>
RA : SA.
Addr->members(DFG)) {
221 R2I.insert(std::make_pair(
RA.Id, SA.
Id));
230 SetVector<NodeId> Remove = DeadInstrs;
234 auto SA = DFG.
addr<StmtNode*>(
N);
236 dbgs() <<
"Partly dead: " << *SA.
Addr->getCode();
237 Changed |= rewrite(SA, Remove);
243void HexagonDCE::removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum) {
244 MachineInstr *
MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
246 auto getOpNum = [
MI] (MachineOperand &
Op) ->
unsigned {
247 for (
unsigned i = 0, n =
MI->getNumOperands(); i != n; ++i)
248 if (&
MI->getOperand(i) == &
Op)
252 DenseMap<NodeId,unsigned> OpMap;
253 DataFlowGraph &DFG = getDFG();
255 for (NodeAddr<RefNode*>
RA : Refs)
256 OpMap.
insert(std::make_pair(
RA.Id, getOpNum(
RA.Addr->getOp())));
258 MI->removeOperand(OpNum);
260 for (NodeAddr<RefNode*>
RA : Refs) {
261 unsigned N = OpMap[
RA.Id];
263 RA.Addr->setRegRef(&
MI->getOperand(
N), DFG);
265 RA.Addr->setRegRef(&
MI->getOperand(
N-1), DFG);
269bool HexagonDCE::rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove) {
270 if (!getDFG().IsCode<NodeAttrs::Stmt>(IA))
272 DataFlowGraph &DFG = getDFG();
273 MachineInstr &
MI = *NodeAddr<StmtNode*>(IA).Addr->getCode();
274 auto &HII =
static_cast<const HexagonInstrInfo&
>(DFG.
getTII());
277 unsigned Opc =
MI.getOpcode();
278 unsigned OpNum, NewOpc;
280 case Hexagon::L2_loadri_pi:
281 NewOpc = Hexagon::L2_loadri_io;
284 case Hexagon::L2_loadrd_pi:
285 NewOpc = Hexagon::L2_loadrd_io;
288 case Hexagon::V6_vL32b_pi:
289 NewOpc = Hexagon::V6_vL32b_ai;
292 case Hexagon::S2_storeri_pi:
293 NewOpc = Hexagon::S2_storeri_io;
296 case Hexagon::S2_storerd_pi:
297 NewOpc = Hexagon::S2_storerd_io;
300 case Hexagon::V6_vS32b_pi:
301 NewOpc = Hexagon::V6_vS32b_ai;
307 auto IsDead = [
this] (NodeAddr<DefNode*>
DA) ->
bool {
308 return getDeadNodes().count(
DA.Id);
311 MachineOperand &
Op =
MI.getOperand(OpNum);
312 for (NodeAddr<DefNode*> DA :
IA.Addr->members_if(DFG.
IsDef, DFG)) {
313 if (&
DA.Addr->getOp() != &
Op)
326 dbgs() <<
"Rewriting: " <<
MI;
327 MI.setDesc(HII.get(NewOpc));
328 MI.getOperand(OpNum+2).setImm(0);
329 removeOperand(IA, OpNum);
336bool HexagonRDFOpt::runOnMachineFunction(MachineFunction &MF) {
344 dbgs() <<
"Skipping " << getPassName() <<
": too many basic blocks\n";
354 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
355 const auto &MDF = getAnalysis<MachineDominanceFrontierWrapperPass>().getMDF();
356 const auto &HII = *MF.
getSubtarget<HexagonSubtarget>().getInstrInfo();
357 const auto &HRI = *MF.
getSubtarget<HexagonSubtarget>().getRegisterInfo();
362 MF.
print(
dbgs() <<
"Before " << getPassName() <<
"\n",
nullptr);
364 DataFlowGraph
G(MF, HII, HRI, *MDT, MDF);
368 DataFlowGraph::Config Cfg;
376 dbgs() <<
"Starting aggressive copy propagation on: " << MF.
getName()
378 << PrintNode<FuncNode *>(
G.getFunc(),
G) <<
'\n';
379 HexagonAggressiveCP CP(
G);
384 dbgs() <<
"Starting copy propagation on: " << MF.
getName() <<
'\n'
385 << PrintNode<FuncNode *>(
G.getFunc(),
G) <<
'\n';
392 dbgs() <<
"Starting dead code elimination on: " << MF.
getName() <<
'\n'
393 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
394 HexagonDCE DCE(
G, *MRI);
400 dbgs() <<
"Starting liveness recomputation on: " << MF.
getName() <<
'\n'
401 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
403 Liveness LV(*MRI,
G);
410 MachineBasicBlock &EntryMBB = MF.
front();
412 std::vector<MCRegister> Old;
413 for (
const MachineBasicBlock::RegisterMaskPair &LI : EntryMBB.
liveins())
414 Old.push_back(LI.PhysReg);
415 for (MCRegister R : Old)
417 for (RegisterRef R : LV.getLiveMap()[&EntryMBB].refs())
430 for (MachineBasicBlock &
B : MF)
431 if (!
B.isEntryBlock() && !
B.isEHPad())
440 MF.print(
dbgs() <<
"After " << getPassName() <<
"\n",
nullptr);
446 return new HexagonRDFOpt();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
cl::opt< unsigned > RDFFuncBlockLimit
static cl::opt< bool > RDFTrackReserved("hexagon-rdf-track-reserved", cl::Hidden)
static cl::opt< bool > EnableAggressiveRDFCopy("hexagon-aggressive-rdf-copy", cl::desc("Enable aggressive RDF copy propagation with super-register " "support"), cl::init(false), cl::Hidden)
static cl::opt< unsigned > RDFLimit("hexagon-rdf-limit", cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< bool > RDFDump("hexagon-rdf-dump", cl::Hidden)
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
SI optimize exec mask operations pre RA
This file implements a set that has insertion order iteration characteristics.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< livein_iterator > liveins() const
LLVM_ABI void removeLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
LLVM_ABI void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
Analysis pass which computes a MachineDominatorTree.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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 MachineBasicBlock & front() const
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
Register getReg() const
getReg - Returns the register number.
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
void push_back(const T &Elt)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
DXILDebugInfoMap run(Module &M)
SmallVector< Node, 4 > NodeList
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DWARFExpression::Operation Op
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
FunctionPass * createHexagonRDFOpt()
virtual bool interpretAsCopy(const MachineInstr *MI, EqualityMap &EM)
LLVM_ABI NodeList members(const DataFlowGraph &G) const
LLVM_ABI RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const
static bool IsDef(const Node BA)
LLVM_ABI NodeList getRelatedRefs(Instr IA, Ref RA) const
const TargetInstrInfo & getTII() const
static bool IsCode(const Node BA)
const TargetRegisterInfo & getTRI() const
NodeAddr< T > addr(NodeId N) const