47 cl::init(std::numeric_limits<unsigned>::max()));
49 "hexagon-aggressive-rdf-copy",
50 cl::desc(
"Enable aggressive RDF copy propagation with super-register "
62 void getAnalysisUsage(AnalysisUsage &AU)
const override {
64 AU.
addRequired<MachineDominanceFrontierWrapperPass>();
69 StringRef getPassName()
const override {
70 return "Hexagon RDF optimizations";
73 bool runOnMachineFunction(MachineFunction &MF)
override;
75 MachineFunctionProperties getRequiredProperties()
const override {
76 return MachineFunctionProperties().setNoVRegs();
82 MachineDominatorTree *MDT;
83 MachineRegisterInfo *MRI;
86struct HexagonCP :
public CopyPropagation {
87 HexagonCP(DataFlowGraph &
G) : CopyPropagation(
G) {}
89 bool interpretAsCopy(
const MachineInstr *
MI, EqualityMap &EM)
override;
93 HexagonAggressiveCP(DataFlowGraph &
G) : AggressiveCopyPropagation(
G) {}
95 bool interpretAsCopy(
const MachineInstr *
MI, EqualityMap &EM)
override;
98struct HexagonDCE :
public DeadCodeElimination {
99 HexagonDCE(DataFlowGraph &
G, MachineRegisterInfo &MRI)
100 : DeadCodeElimination(
G, MRI) {}
102 bool rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove);
103 void removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum);
110char HexagonRDFOpt::ID = 0;
113 "Hexagon RDF optimizations",
false,
false)
121 EM.insert(std::make_pair(DstR, SrcR));
125 unsigned Opc =
MI->getOpcode();
127 case Hexagon::A2_combinew: {
131 assert(
DstOp.getSubReg() == 0 &&
"Unexpected subregister");
138 case Hexagon::A2_addi: {
140 if (!
A.isImm() ||
A.getImm() != 0)
144 case Hexagon::A2_tfr: {
153 return CopyPropagation::interpretAsCopy(
MI, EM);
158 auto mapRegs = [&EM](RegisterRef DstR, RegisterRef SrcR) ->
void {
159 EM.insert(std::make_pair(DstR, SrcR));
162 DataFlowGraph &DFG = getDFG();
163 const TargetRegisterInfo &
TRI = DFG.
getTRI();
164 unsigned Opc =
MI->getOpcode();
166 case Hexagon::A2_combinew: {
169 const MachineOperand &DstOp =
MI->getOperand(0);
170 const MachineOperand &HiOp =
MI->getOperand(1);
171 const MachineOperand &LoOp =
MI->getOperand(2);
173 unsigned DoubleRegDest =
TRI.getMatchingSuperReg(
174 LoOp.
getReg(), Hexagon::isub_lo, &Hexagon::DoubleRegsRegClass);
175 if (DoubleRegDest != 0 &&
176 TRI.isSuperRegister(HiOp.
getReg(), DoubleRegDest))
184 case Hexagon::A2_addi: {
185 const MachineOperand &
A =
MI->getOperand(2);
186 if (!
A.isImm() ||
A.getImm() != 0)
190 case Hexagon::A2_tfr: {
191 const MachineOperand &DstOp =
MI->getOperand(0);
192 const MachineOperand &SrcOp =
MI->getOperand(1);
202bool HexagonDCE::run() {
203 bool Collected = collect();
207 const SetVector<NodeId> &DeadNodes = getDeadNodes();
208 const SetVector<NodeId> &DeadInstrs = getDeadInstrs();
210 using RefToInstrMap = DenseMap<NodeId, NodeId>;
213 SetVector<NodeId> PartlyDead;
214 DataFlowGraph &DFG = getDFG();
218 NodeAddr<StmtNode*> SA =
TA;
219 for (NodeAddr<RefNode*>
RA : SA.
Addr->members(DFG)) {
220 R2I.insert(std::make_pair(
RA.Id, SA.
Id));
229 SetVector<NodeId> Remove = DeadInstrs;
233 auto SA = DFG.
addr<StmtNode*>(
N);
235 dbgs() <<
"Partly dead: " << *SA.
Addr->getCode();
236 Changed |= rewrite(SA, Remove);
242void HexagonDCE::removeOperand(NodeAddr<InstrNode*> IA,
unsigned OpNum) {
243 MachineInstr *
MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
245 auto getOpNum = [
MI] (MachineOperand &
Op) ->
unsigned {
246 for (
unsigned i = 0, n =
MI->getNumOperands(); i != n; ++i)
247 if (&
MI->getOperand(i) == &
Op)
251 DenseMap<NodeId,unsigned> OpMap;
252 DataFlowGraph &DFG = getDFG();
254 for (NodeAddr<RefNode*>
RA : Refs)
255 OpMap.
insert(std::make_pair(
RA.Id, getOpNum(
RA.Addr->getOp())));
257 MI->removeOperand(OpNum);
259 for (NodeAddr<RefNode*>
RA : Refs) {
260 unsigned N = OpMap[
RA.Id];
262 RA.Addr->setRegRef(&
MI->getOperand(
N), DFG);
264 RA.Addr->setRegRef(&
MI->getOperand(
N-1), DFG);
268bool HexagonDCE::rewrite(NodeAddr<InstrNode*> IA, SetVector<NodeId> &Remove) {
269 if (!getDFG().IsCode<NodeAttrs::Stmt>(IA))
271 DataFlowGraph &DFG = getDFG();
272 MachineInstr &
MI = *NodeAddr<StmtNode*>(IA).Addr->getCode();
273 auto &HII =
static_cast<const HexagonInstrInfo&
>(DFG.
getTII());
276 unsigned Opc =
MI.getOpcode();
277 unsigned OpNum, NewOpc;
279 case Hexagon::L2_loadri_pi:
280 NewOpc = Hexagon::L2_loadri_io;
283 case Hexagon::L2_loadrd_pi:
284 NewOpc = Hexagon::L2_loadrd_io;
287 case Hexagon::V6_vL32b_pi:
288 NewOpc = Hexagon::V6_vL32b_ai;
291 case Hexagon::S2_storeri_pi:
292 NewOpc = Hexagon::S2_storeri_io;
295 case Hexagon::S2_storerd_pi:
296 NewOpc = Hexagon::S2_storerd_io;
299 case Hexagon::V6_vS32b_pi:
300 NewOpc = Hexagon::V6_vS32b_ai;
306 auto IsDead = [
this] (NodeAddr<DefNode*>
DA) ->
bool {
307 return getDeadNodes().count(
DA.Id);
310 MachineOperand &
Op =
MI.getOperand(OpNum);
311 for (NodeAddr<DefNode*> DA :
IA.Addr->members_if(DFG.
IsDef, DFG)) {
312 if (&
DA.Addr->getOp() != &
Op)
325 dbgs() <<
"Rewriting: " <<
MI;
326 MI.setDesc(HII.get(NewOpc));
327 MI.getOperand(OpNum+2).setImm(0);
328 removeOperand(IA, OpNum);
335bool HexagonRDFOpt::runOnMachineFunction(MachineFunction &MF) {
343 dbgs() <<
"Skipping " << getPassName() <<
": too many basic blocks\n";
353 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
354 const auto &MDF = getAnalysis<MachineDominanceFrontierWrapperPass>().getMDF();
355 const auto &HII = *MF.
getSubtarget<HexagonSubtarget>().getInstrInfo();
356 const auto &HRI = *MF.
getSubtarget<HexagonSubtarget>().getRegisterInfo();
361 MF.
print(
dbgs() <<
"Before " << getPassName() <<
"\n",
nullptr);
363 DataFlowGraph
G(MF, HII, HRI, *MDT, MDF);
367 DataFlowGraph::Config Cfg;
375 dbgs() <<
"Starting aggressive copy propagation on: " << MF.
getName()
377 << PrintNode<FuncNode *>(
G.getFunc(),
G) <<
'\n';
378 HexagonAggressiveCP CP(
G);
383 dbgs() <<
"Starting copy propagation on: " << MF.
getName() <<
'\n'
384 << PrintNode<FuncNode *>(
G.getFunc(),
G) <<
'\n';
391 dbgs() <<
"Starting dead code elimination on: " << MF.
getName() <<
'\n'
392 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
393 HexagonDCE DCE(
G, *MRI);
399 dbgs() <<
"Starting liveness recomputation on: " << MF.
getName() <<
'\n'
400 << PrintNode<FuncNode*>(
G.getFunc(),
G) <<
'\n';
402 Liveness LV(*MRI,
G);
410 MF.
print(
dbgs() <<
"After " << getPassName() <<
"\n",
nullptr);
416 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")
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)
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.
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_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.
#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.
DWARFExpression::Operation Op
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