46 cl::init(std::numeric_limits<unsigned>::max()));
59 void getAnalysisUsage(AnalysisUsage &AU)
const override {
66 StringRef getPassName()
const override {
67 return "Hexagon RDF optimizations";
70 bool runOnMachineFunction(MachineFunction &MF)
override;
72 MachineFunctionProperties getRequiredProperties()
const override {
73 return MachineFunctionProperties().setNoVRegs();
79 MachineDominatorTree *MDT;
80 MachineRegisterInfo *MRI;
83struct HexagonCP :
public CopyPropagation {
84 HexagonCP(DataFlowGraph &
G) : CopyPropagation(
G) {}
86 bool interpretAsCopy(
const MachineInstr *
MI, EqualityMap &EM)
override;
89struct HexagonDCE :
public DeadCodeElimination {
90 HexagonDCE(DataFlowGraph &
G, MachineRegisterInfo &
MRI)
91 : DeadCodeElimination(
G,
MRI) {}
93 bool rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove);
94 void removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum);
101char HexagonRDFOpt::ID = 0;
104 "Hexagon RDF optimizations",
false,
false)
112 EM.insert(std::make_pair(DstR, SrcR));
116 unsigned Opc =
MI->getOpcode();
118 case Hexagon::A2_combinew: {
122 assert(
DstOp.getSubReg() == 0 &&
"Unexpected subregister");
129 case Hexagon::A2_addi: {
131 if (!
A.isImm() ||
A.getImm() != 0)
135 case Hexagon::A2_tfr: {
144 return CopyPropagation::interpretAsCopy(
MI, EM);
147bool HexagonDCE::run() {
148 bool Collected = collect();
152 const SetVector<NodeId> &DeadNodes = getDeadNodes();
153 const SetVector<NodeId> &DeadInstrs = getDeadInstrs();
155 using RefToInstrMap = DenseMap<NodeId, NodeId>;
158 SetVector<NodeId> PartlyDead;
159 DataFlowGraph &DFG = getDFG();
163 NodeAddr<StmtNode*> SA =
TA;
164 for (NodeAddr<RefNode*>
RA : SA.
Addr->members(DFG)) {
165 R2I.insert(std::make_pair(
RA.Id, SA.
Id));
174 SetVector<NodeId> Remove = DeadInstrs;
178 auto SA = DFG.
addr<StmtNode*>(
N);
180 dbgs() <<
"Partly dead: " << *SA.
Addr->getCode();
181 Changed |= rewrite(SA, Remove);
187void HexagonDCE::removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum) {
188 MachineInstr *
MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
190 auto getOpNum = [
MI] (MachineOperand &
Op) ->
unsigned {
191 for (
unsigned i = 0, n =
MI->getNumOperands(); i != n; ++i)
192 if (&
MI->getOperand(i) == &
Op)
196 DenseMap<NodeId,unsigned> OpMap;
197 DataFlowGraph &DFG = getDFG();
199 for (NodeAddr<RefNode*>
RA : Refs)
200 OpMap.
insert(std::make_pair(
RA.Id, getOpNum(
RA.Addr->getOp())));
202 MI->removeOperand(OpNum);
204 for (NodeAddr<RefNode*>
RA : Refs) {
205 unsigned N = OpMap[
RA.Id];
207 RA.Addr->setRegRef(&
MI->getOperand(
N), DFG);
209 RA.Addr->setRegRef(&
MI->getOperand(
N-1), DFG);
213bool HexagonDCE::rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove) {
214 if (!getDFG().IsCode<NodeAttrs::Stmt>(IA))
216 DataFlowGraph &DFG = getDFG();
217 MachineInstr &
MI = *NodeAddr<StmtNode*>(IA).Addr->getCode();
218 auto &HII =
static_cast<const HexagonInstrInfo&
>(DFG.
getTII());
221 unsigned Opc =
MI.getOpcode();
222 unsigned OpNum, NewOpc;
224 case Hexagon::L2_loadri_pi:
225 NewOpc = Hexagon::L2_loadri_io;
228 case Hexagon::L2_loadrd_pi:
229 NewOpc = Hexagon::L2_loadrd_io;
232 case Hexagon::V6_vL32b_pi:
233 NewOpc = Hexagon::V6_vL32b_ai;
236 case Hexagon::S2_storeri_pi:
237 NewOpc = Hexagon::S2_storeri_io;
240 case Hexagon::S2_storerd_pi:
241 NewOpc = Hexagon::S2_storerd_io;
244 case Hexagon::V6_vS32b_pi:
245 NewOpc = Hexagon::V6_vS32b_ai;
251 auto IsDead = [
this] (NodeAddr<DefNode*>
DA) ->
bool {
252 return getDeadNodes().count(
DA.Id);
255 MachineOperand &
Op =
MI.getOperand(OpNum);
256 for (NodeAddr<DefNode*> DA :
IA.Addr->members_if(DFG.
IsDef, DFG)) {
257 if (&
DA.Addr->getOp() != &
Op)
270 dbgs() <<
"Rewriting: " <<
MI;
271 MI.setDesc(HII.get(NewOpc));
272 MI.getOperand(OpNum+2).setImm(0);
273 removeOperand(IA, OpNum);
280bool HexagonRDFOpt::runOnMachineFunction(MachineFunction &MF) {
288 dbgs() <<
"Skipping " << getPassName() <<
": too many basic blocks\n";
298 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
299 const auto &MDF = getAnalysis<MachineDominanceFrontier>();
300 const auto &HII = *MF.
getSubtarget<HexagonSubtarget>().getInstrInfo();
301 const auto &HRI = *MF.
getSubtarget<HexagonSubtarget>().getRegisterInfo();
306 MF.
print(
dbgs() <<
"Before " << getPassName() <<
"\n",
nullptr);
308 DataFlowGraph
G(MF, HII, HRI, *MDT, MDF);
312 DataFlowGraph::Config Cfg;
319 dbgs() <<
"Starting copy propagation on: " << MF.
getName() <<
'\n'
320 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
326 dbgs() <<
"Starting dead code elimination on: " << MF.
getName() <<
'\n'
327 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
328 HexagonDCE DCE(
G, *
MRI);
334 dbgs() <<
"Starting liveness recomputation on: " << MF.
getName() <<
'\n'
335 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
337 Liveness LV(*
MRI,
G);
345 MF.
print(
dbgs() <<
"After " << getPassName() <<
"\n",
nullptr);
351 return new HexagonRDFOpt();
unsigned const MachineRegisterInfo * MRI
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")
This file defines the DenseMap class.
cl::opt< unsigned > RDFFuncBlockLimit
static cl::opt< bool > RDFTrackReserved("hexagon-rdf-track-reserved", 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)
#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.
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.
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 key_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.
#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)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the 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.
DWARFExpression::Operation Op
FunctionPass * createHexagonRDFOpt()
NodeList members(const DataFlowGraph &G) const
RegisterRef makeRegRef(unsigned Reg, unsigned Sub) const
static bool IsDef(const Node BA)
NodeList getRelatedRefs(Instr IA, Ref RA) const
const TargetInstrInfo & getTII() const
static bool IsCode(const Node BA)
NodeAddr< T > addr(NodeId N) const