AggressiveAntiDepBreaker.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. //===- AggressiveAntiDepBreaker.cpp - Anti-dep breaker --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the AggressiveAntiDepBreaker class, which
  10. // implements register anti-dependence breaking during post-RA
  11. // scheduling. It attempts to break all anti-dependencies within a
  12. // block.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "AggressiveAntiDepBreaker.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/SmallSet.h"
  18. #include "llvm/ADT/iterator_range.h"
  19. #include "llvm/CodeGen/MachineBasicBlock.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineInstr.h"
  23. #include "llvm/CodeGen/MachineOperand.h"
  24. #include "llvm/CodeGen/MachineRegisterInfo.h"
  25. #include "llvm/CodeGen/RegisterClassInfo.h"
  26. #include "llvm/CodeGen/ScheduleDAG.h"
  27. #include "llvm/CodeGen/TargetInstrInfo.h"
  28. #include "llvm/CodeGen/TargetRegisterInfo.h"
  29. #include "llvm/MC/MCInstrDesc.h"
  30. #include "llvm/MC/MCRegisterInfo.h"
  31. #include "llvm/Support/CommandLine.h"
  32. #include "llvm/Support/Debug.h"
  33. #include "llvm/Support/MachineValueType.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include <cassert>
  36. #include <utility>
  37. using namespace llvm;
  38. #define DEBUG_TYPE "post-RA-sched"
  39. // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
  40. static cl::opt<int>
  41. DebugDiv("agg-antidep-debugdiv",
  42. cl::desc("Debug control for aggressive anti-dep breaker"),
  43. cl::init(0), cl::Hidden);
  44. static cl::opt<int>
  45. DebugMod("agg-antidep-debugmod",
  46. cl::desc("Debug control for aggressive anti-dep breaker"),
  47. cl::init(0), cl::Hidden);
  48. AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
  49. MachineBasicBlock *BB)
  50. : NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
  51. GroupNodeIndices(TargetRegs, 0), KillIndices(TargetRegs, 0),
  52. DefIndices(TargetRegs, 0) {
  53. const unsigned BBSize = BB->size();
  54. for (unsigned i = 0; i < NumTargetRegs; ++i) {
  55. // Initialize all registers to be in their own group. Initially we
  56. // assign the register to the same-indexed GroupNode.
  57. GroupNodeIndices[i] = i;
  58. // Initialize the indices to indicate that no registers are live.
  59. KillIndices[i] = ~0u;
  60. DefIndices[i] = BBSize;
  61. }
  62. }
  63. unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
  64. unsigned Node = GroupNodeIndices[Reg];
  65. while (GroupNodes[Node] != Node)
  66. Node = GroupNodes[Node];
  67. return Node;
  68. }
  69. void AggressiveAntiDepState::GetGroupRegs(
  70. unsigned Group,
  71. std::vector<unsigned> &Regs,
  72. std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
  73. {
  74. for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
  75. if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
  76. Regs.push_back(Reg);
  77. }
  78. }
  79. unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) {
  80. assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
  81. assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
  82. // find group for each register
  83. unsigned Group1 = GetGroup(Reg1);
  84. unsigned Group2 = GetGroup(Reg2);
  85. // if either group is 0, then that must become the parent
  86. unsigned Parent = (Group1 == 0) ? Group1 : Group2;
  87. unsigned Other = (Parent == Group1) ? Group2 : Group1;
  88. GroupNodes.at(Other) = Parent;
  89. return Parent;
  90. }
  91. unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) {
  92. // Create a new GroupNode for Reg. Reg's existing GroupNode must
  93. // stay as is because there could be other GroupNodes referring to
  94. // it.
  95. unsigned idx = GroupNodes.size();
  96. GroupNodes.push_back(idx);
  97. GroupNodeIndices[Reg] = idx;
  98. return idx;
  99. }
  100. bool AggressiveAntiDepState::IsLive(unsigned Reg) {
  101. // KillIndex must be defined and DefIndex not defined for a register
  102. // to be live.
  103. return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
  104. }
  105. AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(
  106. MachineFunction &MFi, const RegisterClassInfo &RCI,
  107. TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
  108. : MF(MFi), MRI(MF.getRegInfo()), TII(MF.getSubtarget().getInstrInfo()),
  109. TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI) {
  110. /* Collect a bitset of all registers that are only broken if they
  111. are on the critical path. */
  112. for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
  113. BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
  114. if (CriticalPathSet.none())
  115. CriticalPathSet = CPSet;
  116. else
  117. CriticalPathSet |= CPSet;
  118. }
  119. LLVM_DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
  120. LLVM_DEBUG(for (unsigned r
  121. : CriticalPathSet.set_bits()) dbgs()
  122. << " " << printReg(r, TRI));
  123. LLVM_DEBUG(dbgs() << '\n');
  124. }
  125. AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
  126. delete State;
  127. }
  128. void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
  129. assert(!State);
  130. State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
  131. bool IsReturnBlock = BB->isReturnBlock();
  132. std::vector<unsigned> &KillIndices = State->GetKillIndices();
  133. std::vector<unsigned> &DefIndices = State->GetDefIndices();
  134. // Examine the live-in regs of all successors.
  135. for (MachineBasicBlock *Succ : BB->successors())
  136. for (const auto &LI : Succ->liveins()) {
  137. for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
  138. unsigned Reg = *AI;
  139. State->UnionGroups(Reg, 0);
  140. KillIndices[Reg] = BB->size();
  141. DefIndices[Reg] = ~0u;
  142. }
  143. }
  144. // Mark live-out callee-saved registers. In a return block this is
  145. // all callee-saved registers. In non-return this is any
  146. // callee-saved register that is not saved in the prolog.
  147. const MachineFrameInfo &MFI = MF.getFrameInfo();
  148. BitVector Pristine = MFI.getPristineRegs(MF);
  149. for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
  150. ++I) {
  151. unsigned Reg = *I;
  152. if (!IsReturnBlock && !Pristine.test(Reg))
  153. continue;
  154. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  155. unsigned AliasReg = *AI;
  156. State->UnionGroups(AliasReg, 0);
  157. KillIndices[AliasReg] = BB->size();
  158. DefIndices[AliasReg] = ~0u;
  159. }
  160. }
  161. }
  162. void AggressiveAntiDepBreaker::FinishBlock() {
  163. delete State;
  164. State = nullptr;
  165. }
  166. void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
  167. unsigned InsertPosIndex) {
  168. assert(Count < InsertPosIndex && "Instruction index out of expected range!");
  169. std::set<unsigned> PassthruRegs;
  170. GetPassthruRegs(MI, PassthruRegs);
  171. PrescanInstruction(MI, Count, PassthruRegs);
  172. ScanInstruction(MI, Count);
  173. LLVM_DEBUG(dbgs() << "Observe: ");
  174. LLVM_DEBUG(MI.dump());
  175. LLVM_DEBUG(dbgs() << "\tRegs:");
  176. std::vector<unsigned> &DefIndices = State->GetDefIndices();
  177. for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
  178. // If Reg is current live, then mark that it can't be renamed as
  179. // we don't know the extent of its live-range anymore (now that it
  180. // has been scheduled). If it is not live but was defined in the
  181. // previous schedule region, then set its def index to the most
  182. // conservative location (i.e. the beginning of the previous
  183. // schedule region).
  184. if (State->IsLive(Reg)) {
  185. LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs()
  186. << " " << printReg(Reg, TRI) << "=g" << State->GetGroup(Reg)
  187. << "->g0(region live-out)");
  188. State->UnionGroups(Reg, 0);
  189. } else if ((DefIndices[Reg] < InsertPosIndex)
  190. && (DefIndices[Reg] >= Count)) {
  191. DefIndices[Reg] = Count;
  192. }
  193. }
  194. LLVM_DEBUG(dbgs() << '\n');
  195. }
  196. bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,
  197. MachineOperand &MO) {
  198. if (!MO.isReg() || !MO.isImplicit())
  199. return false;
  200. Register Reg = MO.getReg();
  201. if (Reg == 0)
  202. return false;
  203. MachineOperand *Op = nullptr;
  204. if (MO.isDef())
  205. Op = MI.findRegisterUseOperand(Reg, true);
  206. else
  207. Op = MI.findRegisterDefOperand(Reg);
  208. return(Op && Op->isImplicit());
  209. }
  210. void AggressiveAntiDepBreaker::GetPassthruRegs(
  211. MachineInstr &MI, std::set<unsigned> &PassthruRegs) {
  212. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  213. MachineOperand &MO = MI.getOperand(i);
  214. if (!MO.isReg()) continue;
  215. if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||
  216. IsImplicitDefUse(MI, MO)) {
  217. const Register Reg = MO.getReg();
  218. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  219. SubRegs.isValid(); ++SubRegs)
  220. PassthruRegs.insert(*SubRegs);
  221. }
  222. }
  223. }
  224. /// AntiDepEdges - Return in Edges the anti- and output- dependencies
  225. /// in SU that we want to consider for breaking.
  226. static void AntiDepEdges(const SUnit *SU, std::vector<const SDep *> &Edges) {
  227. SmallSet<unsigned, 4> RegSet;
  228. for (const SDep &Pred : SU->Preds) {
  229. if ((Pred.getKind() == SDep::Anti) || (Pred.getKind() == SDep::Output)) {
  230. if (RegSet.insert(Pred.getReg()).second)
  231. Edges.push_back(&Pred);
  232. }
  233. }
  234. }
  235. /// CriticalPathStep - Return the next SUnit after SU on the bottom-up
  236. /// critical path.
  237. static const SUnit *CriticalPathStep(const SUnit *SU) {
  238. const SDep *Next = nullptr;
  239. unsigned NextDepth = 0;
  240. // Find the predecessor edge with the greatest depth.
  241. if (SU) {
  242. for (const SDep &Pred : SU->Preds) {
  243. const SUnit *PredSU = Pred.getSUnit();
  244. unsigned PredLatency = Pred.getLatency();
  245. unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
  246. // In the case of a latency tie, prefer an anti-dependency edge over
  247. // other types of edges.
  248. if (NextDepth < PredTotalLatency ||
  249. (NextDepth == PredTotalLatency && Pred.getKind() == SDep::Anti)) {
  250. NextDepth = PredTotalLatency;
  251. Next = &Pred;
  252. }
  253. }
  254. }
  255. return (Next) ? Next->getSUnit() : nullptr;
  256. }
  257. void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
  258. const char *tag,
  259. const char *header,
  260. const char *footer) {
  261. std::vector<unsigned> &KillIndices = State->GetKillIndices();
  262. std::vector<unsigned> &DefIndices = State->GetDefIndices();
  263. std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
  264. RegRefs = State->GetRegRefs();
  265. // FIXME: We must leave subregisters of live super registers as live, so that
  266. // we don't clear out the register tracking information for subregisters of
  267. // super registers we're still tracking (and with which we're unioning
  268. // subregister definitions).
  269. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
  270. if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
  271. LLVM_DEBUG(if (!header && footer) dbgs() << footer);
  272. return;
  273. }
  274. if (!State->IsLive(Reg)) {
  275. KillIndices[Reg] = KillIdx;
  276. DefIndices[Reg] = ~0u;
  277. RegRefs.erase(Reg);
  278. State->LeaveGroup(Reg);
  279. LLVM_DEBUG(if (header) {
  280. dbgs() << header << printReg(Reg, TRI);
  281. header = nullptr;
  282. });
  283. LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
  284. // Repeat for subregisters. Note that we only do this if the superregister
  285. // was not live because otherwise, regardless whether we have an explicit
  286. // use of the subregister, the subregister's contents are needed for the
  287. // uses of the superregister.
  288. for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
  289. unsigned SubregReg = *SubRegs;
  290. if (!State->IsLive(SubregReg)) {
  291. KillIndices[SubregReg] = KillIdx;
  292. DefIndices[SubregReg] = ~0u;
  293. RegRefs.erase(SubregReg);
  294. State->LeaveGroup(SubregReg);
  295. LLVM_DEBUG(if (header) {
  296. dbgs() << header << printReg(Reg, TRI);
  297. header = nullptr;
  298. });
  299. LLVM_DEBUG(dbgs() << " " << printReg(SubregReg, TRI) << "->g"
  300. << State->GetGroup(SubregReg) << tag);
  301. }
  302. }
  303. }
  304. LLVM_DEBUG(if (!header && footer) dbgs() << footer);
  305. }
  306. void AggressiveAntiDepBreaker::PrescanInstruction(
  307. MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) {
  308. std::vector<unsigned> &DefIndices = State->GetDefIndices();
  309. std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
  310. RegRefs = State->GetRegRefs();
  311. // Handle dead defs by simulating a last-use of the register just
  312. // after the def. A dead def can occur because the def is truly
  313. // dead, or because only a subregister is live at the def. If we
  314. // don't do this the dead def will be incorrectly merged into the
  315. // previous def.
  316. for (const MachineOperand &MO : MI.operands()) {
  317. if (!MO.isReg() || !MO.isDef()) continue;
  318. Register Reg = MO.getReg();
  319. if (Reg == 0) continue;
  320. HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
  321. }
  322. LLVM_DEBUG(dbgs() << "\tDef Groups:");
  323. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  324. MachineOperand &MO = MI.getOperand(i);
  325. if (!MO.isReg() || !MO.isDef()) continue;
  326. Register Reg = MO.getReg();
  327. if (Reg == 0) continue;
  328. LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
  329. << State->GetGroup(Reg));
  330. // If MI's defs have a special allocation requirement, don't allow
  331. // any def registers to be changed. Also assume all registers
  332. // defined in a call must not be changed (ABI). Inline assembly may
  333. // reference either system calls or the register directly. Skip it until we
  334. // can tell user specified registers from compiler-specified.
  335. if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||
  336. MI.isInlineAsm()) {
  337. LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
  338. State->UnionGroups(Reg, 0);
  339. }
  340. // Any aliased that are live at this point are completely or
  341. // partially defined here, so group those aliases with Reg.
  342. for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
  343. unsigned AliasReg = *AI;
  344. if (State->IsLive(AliasReg)) {
  345. State->UnionGroups(Reg, AliasReg);
  346. LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via "
  347. << printReg(AliasReg, TRI) << ")");
  348. }
  349. }
  350. // Note register reference...
  351. const TargetRegisterClass *RC = nullptr;
  352. if (i < MI.getDesc().getNumOperands())
  353. RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
  354. AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
  355. RegRefs.insert(std::make_pair(Reg, RR));
  356. }
  357. LLVM_DEBUG(dbgs() << '\n');
  358. // Scan the register defs for this instruction and update
  359. // live-ranges.
  360. for (const MachineOperand &MO : MI.operands()) {
  361. if (!MO.isReg() || !MO.isDef()) continue;
  362. Register Reg = MO.getReg();
  363. if (Reg == 0) continue;
  364. // Ignore KILLs and passthru registers for liveness...
  365. if (MI.isKill() || (PassthruRegs.count(Reg) != 0))
  366. continue;
  367. // Update def for Reg and aliases.
  368. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
  369. // We need to be careful here not to define already-live super registers.
  370. // If the super register is already live, then this definition is not
  371. // a definition of the whole super register (just a partial insertion
  372. // into it). Earlier subregister definitions (which we've not yet visited
  373. // because we're iterating bottom-up) need to be linked to the same group
  374. // as this definition.
  375. if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
  376. continue;
  377. DefIndices[*AI] = Count;
  378. }
  379. }
  380. }
  381. void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,
  382. unsigned Count) {
  383. LLVM_DEBUG(dbgs() << "\tUse Groups:");
  384. std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
  385. RegRefs = State->GetRegRefs();
  386. // If MI's uses have special allocation requirement, don't allow
  387. // any use registers to be changed. Also assume all registers
  388. // used in a call must not be changed (ABI).
  389. // Inline Assembly register uses also cannot be safely changed.
  390. // FIXME: The issue with predicated instruction is more complex. We are being
  391. // conservatively here because the kill markers cannot be trusted after
  392. // if-conversion:
  393. // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14]
  394. // ...
  395. // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395]
  396. // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12]
  397. // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8)
  398. //
  399. // The first R6 kill is not really a kill since it's killed by a predicated
  400. // instruction which may not be executed. The second R6 def may or may not
  401. // re-define R6 so it's not safe to change it since the last R6 use cannot be
  402. // changed.
  403. bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||
  404. TII->isPredicated(MI) || MI.isInlineAsm();
  405. // Scan the register uses for this instruction and update
  406. // live-ranges, groups and RegRefs.
  407. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  408. MachineOperand &MO = MI.getOperand(i);
  409. if (!MO.isReg() || !MO.isUse()) continue;
  410. Register Reg = MO.getReg();
  411. if (Reg == 0) continue;
  412. LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g"
  413. << State->GetGroup(Reg));
  414. // It wasn't previously live but now it is, this is a kill. Forget
  415. // the previous live-range information and start a new live-range
  416. // for the register.
  417. HandleLastUse(Reg, Count, "(last-use)");
  418. if (Special) {
  419. LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
  420. State->UnionGroups(Reg, 0);
  421. }
  422. // Note register reference...
  423. const TargetRegisterClass *RC = nullptr;
  424. if (i < MI.getDesc().getNumOperands())
  425. RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
  426. AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
  427. RegRefs.insert(std::make_pair(Reg, RR));
  428. }
  429. LLVM_DEBUG(dbgs() << '\n');
  430. // Form a group of all defs and uses of a KILL instruction to ensure
  431. // that all registers are renamed as a group.
  432. if (MI.isKill()) {
  433. LLVM_DEBUG(dbgs() << "\tKill Group:");
  434. unsigned FirstReg = 0;
  435. for (const MachineOperand &MO : MI.operands()) {
  436. if (!MO.isReg()) continue;
  437. Register Reg = MO.getReg();
  438. if (Reg == 0) continue;
  439. if (FirstReg != 0) {
  440. LLVM_DEBUG(dbgs() << "=" << printReg(Reg, TRI));
  441. State->UnionGroups(FirstReg, Reg);
  442. } else {
  443. LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
  444. FirstReg = Reg;
  445. }
  446. }
  447. LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
  448. }
  449. }
  450. BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
  451. BitVector BV(TRI->getNumRegs(), false);
  452. bool first = true;
  453. // Check all references that need rewriting for Reg. For each, use
  454. // the corresponding register class to narrow the set of registers
  455. // that are appropriate for renaming.
  456. for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
  457. const TargetRegisterClass *RC = Q.second.RC;
  458. if (!RC) continue;
  459. BitVector RCBV = TRI->getAllocatableSet(MF, RC);
  460. if (first) {
  461. BV |= RCBV;
  462. first = false;
  463. } else {
  464. BV &= RCBV;
  465. }
  466. LLVM_DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
  467. }
  468. return BV;
  469. }
  470. bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
  471. unsigned AntiDepGroupIndex,
  472. RenameOrderType& RenameOrder,
  473. std::map<unsigned, unsigned> &RenameMap) {
  474. std::vector<unsigned> &KillIndices = State->GetKillIndices();
  475. std::vector<unsigned> &DefIndices = State->GetDefIndices();
  476. std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
  477. RegRefs = State->GetRegRefs();
  478. // Collect all referenced registers in the same group as
  479. // AntiDepReg. These all need to be renamed together if we are to
  480. // break the anti-dependence.
  481. std::vector<unsigned> Regs;
  482. State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
  483. assert(!Regs.empty() && "Empty register group!");
  484. if (Regs.empty())
  485. return false;
  486. // Find the "superest" register in the group. At the same time,
  487. // collect the BitVector of registers that can be used to rename
  488. // each register.
  489. LLVM_DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
  490. << ":\n");
  491. std::map<unsigned, BitVector> RenameRegisterMap;
  492. unsigned SuperReg = 0;
  493. for (unsigned Reg : Regs) {
  494. if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
  495. SuperReg = Reg;
  496. // If Reg has any references, then collect possible rename regs
  497. if (RegRefs.count(Reg) > 0) {
  498. LLVM_DEBUG(dbgs() << "\t\t" << printReg(Reg, TRI) << ":");
  499. BitVector &BV = RenameRegisterMap[Reg];
  500. assert(BV.empty());
  501. BV = GetRenameRegisters(Reg);
  502. LLVM_DEBUG({
  503. dbgs() << " ::";
  504. for (unsigned r : BV.set_bits())
  505. dbgs() << " " << printReg(r, TRI);
  506. dbgs() << "\n";
  507. });
  508. }
  509. }
  510. // All group registers should be a subreg of SuperReg.
  511. for (unsigned Reg : Regs) {
  512. if (Reg == SuperReg) continue;
  513. bool IsSub = TRI->isSubRegister(SuperReg, Reg);
  514. // FIXME: remove this once PR18663 has been properly fixed. For now,
  515. // return a conservative answer:
  516. // assert(IsSub && "Expecting group subregister");
  517. if (!IsSub)
  518. return false;
  519. }
  520. #ifndef NDEBUG
  521. // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
  522. if (DebugDiv > 0) {
  523. static int renamecnt = 0;
  524. if (renamecnt++ % DebugDiv != DebugMod)
  525. return false;
  526. dbgs() << "*** Performing rename " << printReg(SuperReg, TRI)
  527. << " for debug ***\n";
  528. }
  529. #endif
  530. // Check each possible rename register for SuperReg in round-robin
  531. // order. If that register is available, and the corresponding
  532. // registers are available for the other group subregisters, then we
  533. // can use those registers to rename.
  534. // FIXME: Using getMinimalPhysRegClass is very conservative. We should
  535. // check every use of the register and find the largest register class
  536. // that can be used in all of them.
  537. const TargetRegisterClass *SuperRC =
  538. TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
  539. ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
  540. if (Order.empty()) {
  541. LLVM_DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
  542. return false;
  543. }
  544. LLVM_DEBUG(dbgs() << "\tFind Registers:");
  545. RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
  546. unsigned OrigR = RenameOrder[SuperRC];
  547. unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
  548. unsigned R = OrigR;
  549. do {
  550. if (R == 0) R = Order.size();
  551. --R;
  552. const unsigned NewSuperReg = Order[R];
  553. // Don't consider non-allocatable registers
  554. if (!MRI.isAllocatable(NewSuperReg)) continue;
  555. // Don't replace a register with itself.
  556. if (NewSuperReg == SuperReg) continue;
  557. LLVM_DEBUG(dbgs() << " [" << printReg(NewSuperReg, TRI) << ':');
  558. RenameMap.clear();
  559. // For each referenced group register (which must be a SuperReg or
  560. // a subregister of SuperReg), find the corresponding subregister
  561. // of NewSuperReg and make sure it is free to be renamed.
  562. for (unsigned Reg : Regs) {
  563. unsigned NewReg = 0;
  564. if (Reg == SuperReg) {
  565. NewReg = NewSuperReg;
  566. } else {
  567. unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
  568. if (NewSubRegIdx != 0)
  569. NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
  570. }
  571. LLVM_DEBUG(dbgs() << " " << printReg(NewReg, TRI));
  572. // Check if Reg can be renamed to NewReg.
  573. if (!RenameRegisterMap[Reg].test(NewReg)) {
  574. LLVM_DEBUG(dbgs() << "(no rename)");
  575. goto next_super_reg;
  576. }
  577. // If NewReg is dead and NewReg's most recent def is not before
  578. // Regs's kill, it's safe to replace Reg with NewReg. We
  579. // must also check all aliases of NewReg, because we can't define a
  580. // register when any sub or super is already live.
  581. if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
  582. LLVM_DEBUG(dbgs() << "(live)");
  583. goto next_super_reg;
  584. } else {
  585. bool found = false;
  586. for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
  587. unsigned AliasReg = *AI;
  588. if (State->IsLive(AliasReg) ||
  589. (KillIndices[Reg] > DefIndices[AliasReg])) {
  590. LLVM_DEBUG(dbgs()
  591. << "(alias " << printReg(AliasReg, TRI) << " live)");
  592. found = true;
  593. break;
  594. }
  595. }
  596. if (found)
  597. goto next_super_reg;
  598. }
  599. // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
  600. // defines 'NewReg' via an early-clobber operand.
  601. for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
  602. MachineInstr *UseMI = Q.second.Operand->getParent();
  603. int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
  604. if (Idx == -1)
  605. continue;
  606. if (UseMI->getOperand(Idx).isEarlyClobber()) {
  607. LLVM_DEBUG(dbgs() << "(ec)");
  608. goto next_super_reg;
  609. }
  610. }
  611. // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
  612. // 'Reg' is an early-clobber define and that instruction also uses
  613. // 'NewReg'.
  614. for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
  615. if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
  616. continue;
  617. MachineInstr *DefMI = Q.second.Operand->getParent();
  618. if (DefMI->readsRegister(NewReg, TRI)) {
  619. LLVM_DEBUG(dbgs() << "(ec)");
  620. goto next_super_reg;
  621. }
  622. }
  623. // Record that 'Reg' can be renamed to 'NewReg'.
  624. RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
  625. }
  626. // If we fall-out here, then every register in the group can be
  627. // renamed, as recorded in RenameMap.
  628. RenameOrder.erase(SuperRC);
  629. RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
  630. LLVM_DEBUG(dbgs() << "]\n");
  631. return true;
  632. next_super_reg:
  633. LLVM_DEBUG(dbgs() << ']');
  634. } while (R != EndR);
  635. LLVM_DEBUG(dbgs() << '\n');
  636. // No registers are free and available!
  637. return false;
  638. }
  639. /// BreakAntiDependencies - Identifiy anti-dependencies within the
  640. /// ScheduleDAG and break them by renaming registers.
  641. unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
  642. const std::vector<SUnit> &SUnits,
  643. MachineBasicBlock::iterator Begin,
  644. MachineBasicBlock::iterator End,
  645. unsigned InsertPosIndex,
  646. DbgValueVector &DbgValues) {
  647. std::vector<unsigned> &KillIndices = State->GetKillIndices();
  648. std::vector<unsigned> &DefIndices = State->GetDefIndices();
  649. std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
  650. RegRefs = State->GetRegRefs();
  651. // The code below assumes that there is at least one instruction,
  652. // so just duck out immediately if the block is empty.
  653. if (SUnits.empty()) return 0;
  654. // For each regclass the next register to use for renaming.
  655. RenameOrderType RenameOrder;
  656. // ...need a map from MI to SUnit.
  657. std::map<MachineInstr *, const SUnit *> MISUnitMap;
  658. for (const SUnit &SU : SUnits)
  659. MISUnitMap.insert(std::make_pair(SU.getInstr(), &SU));
  660. // Track progress along the critical path through the SUnit graph as
  661. // we walk the instructions. This is needed for regclasses that only
  662. // break critical-path anti-dependencies.
  663. const SUnit *CriticalPathSU = nullptr;
  664. MachineInstr *CriticalPathMI = nullptr;
  665. if (CriticalPathSet.any()) {
  666. for (const SUnit &SU : SUnits) {
  667. if (!CriticalPathSU ||
  668. ((SU.getDepth() + SU.Latency) >
  669. (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
  670. CriticalPathSU = &SU;
  671. }
  672. }
  673. assert(CriticalPathSU && "Failed to find SUnit critical path");
  674. CriticalPathMI = CriticalPathSU->getInstr();
  675. }
  676. #ifndef NDEBUG
  677. LLVM_DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
  678. LLVM_DEBUG(dbgs() << "Available regs:");
  679. for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
  680. if (!State->IsLive(Reg))
  681. LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI));
  682. }
  683. LLVM_DEBUG(dbgs() << '\n');
  684. #endif
  685. BitVector RegAliases(TRI->getNumRegs());
  686. // Attempt to break anti-dependence edges. Walk the instructions
  687. // from the bottom up, tracking information about liveness as we go
  688. // to help determine which registers are available.
  689. unsigned Broken = 0;
  690. unsigned Count = InsertPosIndex - 1;
  691. for (MachineBasicBlock::iterator I = End, E = Begin;
  692. I != E; --Count) {
  693. MachineInstr &MI = *--I;
  694. if (MI.isDebugInstr())
  695. continue;
  696. LLVM_DEBUG(dbgs() << "Anti: ");
  697. LLVM_DEBUG(MI.dump());
  698. std::set<unsigned> PassthruRegs;
  699. GetPassthruRegs(MI, PassthruRegs);
  700. // Process the defs in MI...
  701. PrescanInstruction(MI, Count, PassthruRegs);
  702. // The dependence edges that represent anti- and output-
  703. // dependencies that are candidates for breaking.
  704. std::vector<const SDep *> Edges;
  705. const SUnit *PathSU = MISUnitMap[&MI];
  706. AntiDepEdges(PathSU, Edges);
  707. // If MI is not on the critical path, then we don't rename
  708. // registers in the CriticalPathSet.
  709. BitVector *ExcludeRegs = nullptr;
  710. if (&MI == CriticalPathMI) {
  711. CriticalPathSU = CriticalPathStep(CriticalPathSU);
  712. CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
  713. } else if (CriticalPathSet.any()) {
  714. ExcludeRegs = &CriticalPathSet;
  715. }
  716. // Ignore KILL instructions (they form a group in ScanInstruction
  717. // but don't cause any anti-dependence breaking themselves)
  718. if (!MI.isKill()) {
  719. // Attempt to break each anti-dependency...
  720. for (const SDep *Edge : Edges) {
  721. SUnit *NextSU = Edge->getSUnit();
  722. if ((Edge->getKind() != SDep::Anti) &&
  723. (Edge->getKind() != SDep::Output)) continue;
  724. unsigned AntiDepReg = Edge->getReg();
  725. LLVM_DEBUG(dbgs() << "\tAntidep reg: " << printReg(AntiDepReg, TRI));
  726. assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
  727. if (!MRI.isAllocatable(AntiDepReg)) {
  728. // Don't break anti-dependencies on non-allocatable registers.
  729. LLVM_DEBUG(dbgs() << " (non-allocatable)\n");
  730. continue;
  731. } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
  732. // Don't break anti-dependencies for critical path registers
  733. // if not on the critical path
  734. LLVM_DEBUG(dbgs() << " (not critical-path)\n");
  735. continue;
  736. } else if (PassthruRegs.count(AntiDepReg) != 0) {
  737. // If the anti-dep register liveness "passes-thru", then
  738. // don't try to change it. It will be changed along with
  739. // the use if required to break an earlier antidep.
  740. LLVM_DEBUG(dbgs() << " (passthru)\n");
  741. continue;
  742. } else {
  743. // No anti-dep breaking for implicit deps
  744. MachineOperand *AntiDepOp = MI.findRegisterDefOperand(AntiDepReg);
  745. assert(AntiDepOp && "Can't find index for defined register operand");
  746. if (!AntiDepOp || AntiDepOp->isImplicit()) {
  747. LLVM_DEBUG(dbgs() << " (implicit)\n");
  748. continue;
  749. }
  750. // If the SUnit has other dependencies on the SUnit that
  751. // it anti-depends on, don't bother breaking the
  752. // anti-dependency since those edges would prevent such
  753. // units from being scheduled past each other
  754. // regardless.
  755. //
  756. // Also, if there are dependencies on other SUnits with the
  757. // same register as the anti-dependency, don't attempt to
  758. // break it.
  759. for (const SDep &Pred : PathSU->Preds) {
  760. if (Pred.getSUnit() == NextSU ? (Pred.getKind() != SDep::Anti ||
  761. Pred.getReg() != AntiDepReg)
  762. : (Pred.getKind() == SDep::Data &&
  763. Pred.getReg() == AntiDepReg)) {
  764. AntiDepReg = 0;
  765. break;
  766. }
  767. }
  768. for (const SDep &Pred : PathSU->Preds) {
  769. if ((Pred.getSUnit() == NextSU) && (Pred.getKind() != SDep::Anti) &&
  770. (Pred.getKind() != SDep::Output)) {
  771. LLVM_DEBUG(dbgs() << " (real dependency)\n");
  772. AntiDepReg = 0;
  773. break;
  774. } else if ((Pred.getSUnit() != NextSU) &&
  775. (Pred.getKind() == SDep::Data) &&
  776. (Pred.getReg() == AntiDepReg)) {
  777. LLVM_DEBUG(dbgs() << " (other dependency)\n");
  778. AntiDepReg = 0;
  779. break;
  780. }
  781. }
  782. if (AntiDepReg == 0) continue;
  783. // If the definition of the anti-dependency register does not start
  784. // a new live range, bail out. This can happen if the anti-dep
  785. // register is a sub-register of another register whose live range
  786. // spans over PathSU. In such case, PathSU defines only a part of
  787. // the larger register.
  788. RegAliases.reset();
  789. for (MCRegAliasIterator AI(AntiDepReg, TRI, true); AI.isValid(); ++AI)
  790. RegAliases.set(*AI);
  791. for (SDep S : PathSU->Succs) {
  792. SDep::Kind K = S.getKind();
  793. if (K != SDep::Data && K != SDep::Output && K != SDep::Anti)
  794. continue;
  795. unsigned R = S.getReg();
  796. if (!RegAliases[R])
  797. continue;
  798. if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R))
  799. continue;
  800. AntiDepReg = 0;
  801. break;
  802. }
  803. if (AntiDepReg == 0) continue;
  804. }
  805. assert(AntiDepReg != 0);
  806. if (AntiDepReg == 0) continue;
  807. // Determine AntiDepReg's register group.
  808. const unsigned GroupIndex = State->GetGroup(AntiDepReg);
  809. if (GroupIndex == 0) {
  810. LLVM_DEBUG(dbgs() << " (zero group)\n");
  811. continue;
  812. }
  813. LLVM_DEBUG(dbgs() << '\n');
  814. // Look for a suitable register to use to break the anti-dependence.
  815. std::map<unsigned, unsigned> RenameMap;
  816. if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
  817. LLVM_DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
  818. << printReg(AntiDepReg, TRI) << ":");
  819. // Handle each group register...
  820. for (const auto &P : RenameMap) {
  821. unsigned CurrReg = P.first;
  822. unsigned NewReg = P.second;
  823. LLVM_DEBUG(dbgs() << " " << printReg(CurrReg, TRI) << "->"
  824. << printReg(NewReg, TRI) << "("
  825. << RegRefs.count(CurrReg) << " refs)");
  826. // Update the references to the old register CurrReg to
  827. // refer to the new register NewReg.
  828. for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
  829. Q.second.Operand->setReg(NewReg);
  830. // If the SU for the instruction being updated has debug
  831. // information related to the anti-dependency register, make
  832. // sure to update that as well.
  833. const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
  834. if (!SU) continue;
  835. UpdateDbgValues(DbgValues, Q.second.Operand->getParent(),
  836. AntiDepReg, NewReg);
  837. }
  838. // We just went back in time and modified history; the
  839. // liveness information for CurrReg is now inconsistent. Set
  840. // the state as if it were dead.
  841. State->UnionGroups(NewReg, 0);
  842. RegRefs.erase(NewReg);
  843. DefIndices[NewReg] = DefIndices[CurrReg];
  844. KillIndices[NewReg] = KillIndices[CurrReg];
  845. State->UnionGroups(CurrReg, 0);
  846. RegRefs.erase(CurrReg);
  847. DefIndices[CurrReg] = KillIndices[CurrReg];
  848. KillIndices[CurrReg] = ~0u;
  849. assert(((KillIndices[CurrReg] == ~0u) !=
  850. (DefIndices[CurrReg] == ~0u)) &&
  851. "Kill and Def maps aren't consistent for AntiDepReg!");
  852. }
  853. ++Broken;
  854. LLVM_DEBUG(dbgs() << '\n');
  855. }
  856. }
  857. }
  858. ScanInstruction(MI, Count);
  859. }
  860. return Broken;
  861. }
  862. AntiDepBreaker *llvm::createAggressiveAntiDepBreaker(
  863. MachineFunction &MFi, const RegisterClassInfo &RCI,
  864. TargetSubtargetInfo::RegClassVector &CriticalPathRCs) {
  865. return new AggressiveAntiDepBreaker(MFi, RCI, CriticalPathRCs);
  866. }