37 #define DEBUG_TYPE "opt-addr-mode"
62 return "Optimize addressing mode of load/store";
87 bool analyzeUses(
unsigned DefR,
const NodeList &UNodeList,
88 InstrEvalMap &InstrEvalResult,
short &SizeInc);
107 "Optimize addressing mode",
false,
false)
113 bool HexagonOptAddrMode::hasRepForm(
MachineInstr &
MI,
unsigned TfrDefR) {
127 return (HII->getBaseWithLongOffset(MI) >= 0);
130 return (HII->getAbsoluteForm(MI) >= 0);
158 if (OffsetReg == RR.
Reg) {
160 OffsetRegRD = UA.Addr->getReachingDef();
170 if ((DFG->IsDef(AA) && AA.
Id != OffsetRegRD) ||
184 getBaseWithLongOffset(UseMI) < 0)
205 const auto &
P = LV->getAllReachingDefsRec(UR, UN, Visited, Defs);
208 dbgs() <<
"*** Unable to collect all reaching defs for use ***\n"
210 <<
"The program's complexity may exceed the limits.\n";
214 const auto &ReachingDefs =
P.first;
215 if (ReachingDefs.size() > 1) {
217 dbgs() <<
"*** Multiple Reaching Defs found!!! ***\n";
218 for (
auto DI : ReachingDefs) {
221 dbgs() <<
"\t\t[Reaching Def]: "
236 RegisterRef DR = DFG->getPRI().normalize(DA.Addr->getRegRef(*DFG));
238 auto UseSet = LV->getAllReachedUses(DR, DA);
240 for (
auto UI : UseSet) {
244 dbgs() <<
"\t\t\t[Reached Use]: "
254 if (!phiUse.empty()) {
255 for (
auto I : phiUse) {
258 auto phiUseSet =
I.second;
259 for (
auto phiUI : phiUseSet) {
271 bool HexagonOptAddrMode::analyzeUses(
unsigned tfrDefR,
273 InstrEvalMap &InstrEvalResult,
275 bool KeepTfr =
false;
276 bool HasRepInstr =
false;
277 InstrEvalResult.clear();
280 bool CanBeReplaced =
false;
286 if (!hasRepForm(MI, tfrDefR)) {
291 CanBeReplaced =
true;
292 }
else if (MI.
getOpcode() == Hexagon::S2_addasl_rrri) {
295 DEBUG(
dbgs() <<
"\nGetting ReachedUses for === " << MI <<
"\n");
296 getAllRealUses(SN, AddaslUseList);
298 if (allValidCandidates(SN, AddaslUseList) &&
299 canRemoveAddasl(SN, MI, AddaslUseList)) {
300 SizeInc += AddaslUseList.
size();
302 CanBeReplaced =
true;
312 InstrEvalResult[&
MI] = CanBeReplaced;
313 HasRepInstr |= CanBeReplaced;
325 bool Changed =
false;
336 short NewOpCode = HII->getBaseWithLongOffset(*OldMI);
337 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
346 short NewOpCode = HII->getAbsoluteForm(*OldMI);
347 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
359 DEBUG(
dbgs() <<
"[Changing]: " << *OldMI <<
"\n");
360 DEBUG(
dbgs() <<
"[TO]: " << MIB <<
"\n");
362 short NewOpCode = HII->xformRegToImmOffset(*OldMI);
363 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
370 DEBUG(
dbgs() <<
"[Changing]: " << *OldMI <<
"\n");
371 DEBUG(
dbgs() <<
"[TO]: " << MIB <<
"\n");
375 for (
unsigned i = OpStart; i < OpEnd; ++i)
383 bool Changed =
false;
393 short NewOpCode = HII->getBaseWithLongOffset(*OldMI);
394 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
402 short NewOpCode = HII->getAbsoluteForm(*OldMI);
403 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
412 DEBUG(
dbgs() <<
"[Changing]: " << *OldMI <<
"\n");
413 DEBUG(
dbgs() <<
"[TO]: " << MIB <<
"\n");
415 short NewOpCode = HII->xformRegToImmOffset(*OldMI);
416 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
423 DEBUG(
dbgs() <<
"[Changing]: " << *OldMI <<
"\n");
424 DEBUG(
dbgs() <<
"[TO]: " << MIB <<
"\n");
427 for (
unsigned i = OpStart; i < OpEnd; ++i)
433 short HexagonOptAddrMode::getBaseWithLongOffset(
const MachineInstr &MI)
const {
435 short TempOpCode = HII->getBaseWithRegOffset(MI);
436 return HII->getBaseWithLongOffset(TempOpCode);
438 return HII->getBaseWithLongOffset(MI);
447 DEBUG(
dbgs() <<
"Processing addasl :" << *AddAslMI <<
"\n");
450 getAllRealUses(SA, UNodeList);
455 "Can't transform this 'AddAsl' instruction!");
462 <<
">]: " << *UseMI <<
"\n");
468 short NewOpCode = getBaseWithLongOffset(*UseMI);
469 assert(NewOpCode >= 0 &&
"Invalid New opcode\n");
478 if (UseMID.mayLoad()) {
486 }
else if (UseMID.mayStore()) {
497 for (
unsigned i = OpStart; i < OpEnd; ++i)
500 Deleted.insert(UseMI);
511 unsigned Changed =
false;
513 Changed = changeLoad(UseMI, ImmOp, UseMOnum);
515 Changed = changeStore(UseMI, ImmOp, UseMOnum);
516 else if (UseMI->
getOpcode() == Hexagon::S2_addasl_rrri)
517 Changed = changeAddAsl(UseN, UseMI, ImmOp, UseMOnum);
520 Deleted.insert(UseMI);
526 bool Changed =
false;
534 if (MI->
getOpcode() != Hexagon::A2_tfrsi ||
539 << *MI <<
"\n\t[InstrNode]: "
543 getAllRealUses(SA, UNodeList);
545 if (!allValidCandidates(SA, UNodeList))
550 InstrEvalMap InstrEvalResult;
554 if (!analyzeUses(DefR, UNodeList, InstrEvalResult, SizeInc))
559 bool KeepTfr =
false;
561 DEBUG(
dbgs() <<
"\t[Total reached uses] : " << UNodeList.size() <<
"\n");
562 DEBUG(
dbgs() <<
"\t[Processing Reached Uses] ===\n");
563 for (
auto I = UNodeList.rbegin(),
E = UNodeList.rend();
I !=
E; ++
I) {
566 "Found a PhiRef node as a real reached use!!");
571 <<
">]: " << *UseMI <<
"\n");
575 for (
unsigned j = 0; j < NumOperands - 1; ++j) {
580 assert(UseMOnum >= 0 &&
"Invalid reached use!");
582 if (InstrEvalResult[UseMI])
584 Changed |= xformUseMI(MI, UseMI, UseN, UseMOnum);
598 bool Changed =
false;
601 HII = HST.getInstrInfo();
602 const auto &MDF = getAnalysis<MachineDominanceFrontier>();
603 MDT = &getAnalysis<MachineDominatorTree>();
619 DEBUG(
dbgs() <<
"==== [RefMap#]=====:\n "
623 Changed |= processBlock(BA);
625 for (
auto MI : Deleted)
643 return new HexagonOptAddrMode();
NodeList members(const DataFlowGraph &G) const
void push_back(const T &Elt)
const GlobalValue * getGlobal() const
uint16_t getFlags() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
Implements a dense probed hash-table based set.
bool mayStore() const
Return true if this instruction could possibly modify memory.
Describe properties that are true of each instruction in the target description file.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
iterator_range< mop_iterator > operands()
amode Optimize addressing mode
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
std::map< RegisterId, NodeRefSet > RefMap
INITIALIZE_PASS_BEGIN(HexagonOptAddrMode,"amode-opt","Optimize addressing mode", false, false) INITIALIZE_PASS_END(HexagonOptAddrMode
NodeList members_if(Predicate P, const DataFlowGraph &G) const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
unsigned getTargetFlags() const
MachineInstrBundleIterator< MachineInstr > iterator
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
int64_t getOffset() const
Return the offset from the symbol in this operand.
amode Optimize addressing false
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool mayLoad() const
Return true if this instruction could possibly read memory.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
std::unordered_map< RegisterId, DefStack > DefStackMap
std::set< NodeId > NodeSet
RegisterRef getRegRef(const DataFlowGraph &G) const
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void initializeHexagonOptAddrModePass(PassRegistry &)
const MachineInstrBuilder & addGlobalAddress(const GlobalValue *GV, int64_t Offset=0, unsigned char TargetFlags=0) const
llvm_unreachable("Unknown RegBank!")
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Representation of each machine instruction.
NodeAddr< NodeBase * > getOwner(const DataFlowGraph &G)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
void build(unsigned Options=BuildOptions::None)
Instructions::iterator instr_iterator
unsigned getReg() const
getReg - Returns the register number.
reverse_iterator rbegin()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineInstr * getCode() const
NodeId getReachingDef() const
FunctionPass * createHexagonOptAddrMode()
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static cl::opt< int > CodeGrowthLimit("hexagon-amode-growth-limit", cl::Hidden, cl::init(0), cl::desc("Code growth limit for address mode ""optimization"))
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...