StackSlotColoring.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //===- StackSlotColoring.cpp - Stack slot coloring pass. ------------------===//
  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 stack slot coloring pass.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/BitVector.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/CodeGen/LiveInterval.h"
  16. #include "llvm/CodeGen/LiveIntervals.h"
  17. #include "llvm/CodeGen/LiveStacks.h"
  18. #include "llvm/CodeGen/MachineBasicBlock.h"
  19. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineFunctionPass.h"
  23. #include "llvm/CodeGen/MachineInstr.h"
  24. #include "llvm/CodeGen/MachineMemOperand.h"
  25. #include "llvm/CodeGen/MachineOperand.h"
  26. #include "llvm/CodeGen/Passes.h"
  27. #include "llvm/CodeGen/PseudoSourceValue.h"
  28. #include "llvm/CodeGen/SlotIndexes.h"
  29. #include "llvm/CodeGen/TargetInstrInfo.h"
  30. #include "llvm/CodeGen/TargetRegisterInfo.h"
  31. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  32. #include "llvm/Pass.h"
  33. #include "llvm/Support/Casting.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/Debug.h"
  36. #include "llvm/Support/raw_ostream.h"
  37. #include <algorithm>
  38. #include <cassert>
  39. #include <cstdint>
  40. #include <iterator>
  41. #include <vector>
  42. using namespace llvm;
  43. #define DEBUG_TYPE "stack-slot-coloring"
  44. static cl::opt<bool>
  45. DisableSharing("no-stack-slot-sharing",
  46. cl::init(false), cl::Hidden,
  47. cl::desc("Suppress slot sharing during stack coloring"));
  48. static cl::opt<int> DCELimit("ssc-dce-limit", cl::init(-1), cl::Hidden);
  49. STATISTIC(NumEliminated, "Number of stack slots eliminated due to coloring");
  50. STATISTIC(NumDead, "Number of trivially dead stack accesses eliminated");
  51. namespace {
  52. class StackSlotColoring : public MachineFunctionPass {
  53. LiveStacks* LS;
  54. MachineFrameInfo *MFI;
  55. const TargetInstrInfo *TII;
  56. const MachineBlockFrequencyInfo *MBFI;
  57. // SSIntervals - Spill slot intervals.
  58. std::vector<LiveInterval*> SSIntervals;
  59. // SSRefs - Keep a list of MachineMemOperands for each spill slot.
  60. // MachineMemOperands can be shared between instructions, so we need
  61. // to be careful that renames like [FI0, FI1] -> [FI1, FI2] do not
  62. // become FI0 -> FI1 -> FI2.
  63. SmallVector<SmallVector<MachineMemOperand *, 8>, 16> SSRefs;
  64. // OrigAlignments - Alignments of stack objects before coloring.
  65. SmallVector<unsigned, 16> OrigAlignments;
  66. // OrigSizes - Sizess of stack objects before coloring.
  67. SmallVector<unsigned, 16> OrigSizes;
  68. // AllColors - If index is set, it's a spill slot, i.e. color.
  69. // FIXME: This assumes PEI locate spill slot with smaller indices
  70. // closest to stack pointer / frame pointer. Therefore, smaller
  71. // index == better color. This is per stack ID.
  72. SmallVector<BitVector, 2> AllColors;
  73. // NextColor - Next "color" that's not yet used. This is per stack ID.
  74. SmallVector<int, 2> NextColors = { -1 };
  75. // UsedColors - "Colors" that have been assigned. This is per stack ID
  76. SmallVector<BitVector, 2> UsedColors;
  77. // Assignments - Color to intervals mapping.
  78. SmallVector<SmallVector<LiveInterval*,4>, 16> Assignments;
  79. public:
  80. static char ID; // Pass identification
  81. StackSlotColoring() : MachineFunctionPass(ID) {
  82. initializeStackSlotColoringPass(*PassRegistry::getPassRegistry());
  83. }
  84. void getAnalysisUsage(AnalysisUsage &AU) const override {
  85. AU.setPreservesCFG();
  86. AU.addRequired<SlotIndexes>();
  87. AU.addPreserved<SlotIndexes>();
  88. AU.addRequired<LiveStacks>();
  89. AU.addRequired<MachineBlockFrequencyInfo>();
  90. AU.addPreserved<MachineBlockFrequencyInfo>();
  91. AU.addPreservedID(MachineDominatorsID);
  92. MachineFunctionPass::getAnalysisUsage(AU);
  93. }
  94. bool runOnMachineFunction(MachineFunction &MF) override;
  95. private:
  96. void InitializeSlots();
  97. void ScanForSpillSlotRefs(MachineFunction &MF);
  98. bool OverlapWithAssignments(LiveInterval *li, int Color) const;
  99. int ColorSlot(LiveInterval *li);
  100. bool ColorSlots(MachineFunction &MF);
  101. void RewriteInstruction(MachineInstr &MI, SmallVectorImpl<int> &SlotMapping,
  102. MachineFunction &MF);
  103. bool RemoveDeadStores(MachineBasicBlock* MBB);
  104. };
  105. } // end anonymous namespace
  106. char StackSlotColoring::ID = 0;
  107. char &llvm::StackSlotColoringID = StackSlotColoring::ID;
  108. INITIALIZE_PASS_BEGIN(StackSlotColoring, DEBUG_TYPE,
  109. "Stack Slot Coloring", false, false)
  110. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  111. INITIALIZE_PASS_DEPENDENCY(LiveStacks)
  112. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  113. INITIALIZE_PASS_END(StackSlotColoring, DEBUG_TYPE,
  114. "Stack Slot Coloring", false, false)
  115. namespace {
  116. // IntervalSorter - Comparison predicate that sort live intervals by
  117. // their weight.
  118. struct IntervalSorter {
  119. bool operator()(LiveInterval* LHS, LiveInterval* RHS) const {
  120. return LHS->weight > RHS->weight;
  121. }
  122. };
  123. } // end anonymous namespace
  124. /// ScanForSpillSlotRefs - Scan all the machine instructions for spill slot
  125. /// references and update spill slot weights.
  126. void StackSlotColoring::ScanForSpillSlotRefs(MachineFunction &MF) {
  127. SSRefs.resize(MFI->getObjectIndexEnd());
  128. // FIXME: Need the equivalent of MachineRegisterInfo for frameindex operands.
  129. for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
  130. MBBI != E; ++MBBI) {
  131. MachineBasicBlock *MBB = &*MBBI;
  132. for (MachineBasicBlock::iterator MII = MBB->begin(), EE = MBB->end();
  133. MII != EE; ++MII) {
  134. MachineInstr &MI = *MII;
  135. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  136. MachineOperand &MO = MI.getOperand(i);
  137. if (!MO.isFI())
  138. continue;
  139. int FI = MO.getIndex();
  140. if (FI < 0)
  141. continue;
  142. if (!LS->hasInterval(FI))
  143. continue;
  144. LiveInterval &li = LS->getInterval(FI);
  145. if (!MI.isDebugValue())
  146. li.weight += LiveIntervals::getSpillWeight(false, true, MBFI, MI);
  147. }
  148. for (MachineInstr::mmo_iterator MMOI = MI.memoperands_begin(),
  149. EE = MI.memoperands_end();
  150. MMOI != EE; ++MMOI) {
  151. MachineMemOperand *MMO = *MMOI;
  152. if (const FixedStackPseudoSourceValue *FSV =
  153. dyn_cast_or_null<FixedStackPseudoSourceValue>(
  154. MMO->getPseudoValue())) {
  155. int FI = FSV->getFrameIndex();
  156. if (FI >= 0)
  157. SSRefs[FI].push_back(MMO);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. /// InitializeSlots - Process all spill stack slot liveintervals and add them
  164. /// to a sorted (by weight) list.
  165. void StackSlotColoring::InitializeSlots() {
  166. int LastFI = MFI->getObjectIndexEnd();
  167. // There is always at least one stack ID.
  168. AllColors.resize(1);
  169. UsedColors.resize(1);
  170. OrigAlignments.resize(LastFI);
  171. OrigSizes.resize(LastFI);
  172. AllColors[0].resize(LastFI);
  173. UsedColors[0].resize(LastFI);
  174. Assignments.resize(LastFI);
  175. using Pair = std::iterator_traits<LiveStacks::iterator>::value_type;
  176. SmallVector<Pair *, 16> Intervals;
  177. Intervals.reserve(LS->getNumIntervals());
  178. for (auto &I : *LS)
  179. Intervals.push_back(&I);
  180. llvm::sort(Intervals,
  181. [](Pair *LHS, Pair *RHS) { return LHS->first < RHS->first; });
  182. // Gather all spill slots into a list.
  183. LLVM_DEBUG(dbgs() << "Spill slot intervals:\n");
  184. for (auto *I : Intervals) {
  185. LiveInterval &li = I->second;
  186. LLVM_DEBUG(li.dump());
  187. int FI = Register::stackSlot2Index(li.reg);
  188. if (MFI->isDeadObjectIndex(FI))
  189. continue;
  190. SSIntervals.push_back(&li);
  191. OrigAlignments[FI] = MFI->getObjectAlignment(FI);
  192. OrigSizes[FI] = MFI->getObjectSize(FI);
  193. auto StackID = MFI->getStackID(FI);
  194. if (StackID != 0) {
  195. AllColors.resize(StackID + 1);
  196. UsedColors.resize(StackID + 1);
  197. AllColors[StackID].resize(LastFI);
  198. UsedColors[StackID].resize(LastFI);
  199. }
  200. AllColors[StackID].set(FI);
  201. }
  202. LLVM_DEBUG(dbgs() << '\n');
  203. // Sort them by weight.
  204. llvm::stable_sort(SSIntervals, IntervalSorter());
  205. NextColors.resize(AllColors.size());
  206. // Get first "color".
  207. for (unsigned I = 0, E = AllColors.size(); I != E; ++I)
  208. NextColors[I] = AllColors[I].find_first();
  209. }
  210. /// OverlapWithAssignments - Return true if LiveInterval overlaps with any
  211. /// LiveIntervals that have already been assigned to the specified color.
  212. bool
  213. StackSlotColoring::OverlapWithAssignments(LiveInterval *li, int Color) const {
  214. const SmallVectorImpl<LiveInterval *> &OtherLIs = Assignments[Color];
  215. for (unsigned i = 0, e = OtherLIs.size(); i != e; ++i) {
  216. LiveInterval *OtherLI = OtherLIs[i];
  217. if (OtherLI->overlaps(*li))
  218. return true;
  219. }
  220. return false;
  221. }
  222. /// ColorSlot - Assign a "color" (stack slot) to the specified stack slot.
  223. int StackSlotColoring::ColorSlot(LiveInterval *li) {
  224. int Color = -1;
  225. bool Share = false;
  226. int FI = Register::stackSlot2Index(li->reg);
  227. uint8_t StackID = MFI->getStackID(FI);
  228. if (!DisableSharing) {
  229. // Check if it's possible to reuse any of the used colors.
  230. Color = UsedColors[StackID].find_first();
  231. while (Color != -1) {
  232. if (!OverlapWithAssignments(li, Color)) {
  233. Share = true;
  234. ++NumEliminated;
  235. break;
  236. }
  237. Color = UsedColors[StackID].find_next(Color);
  238. }
  239. }
  240. if (Color != -1 && MFI->getStackID(Color) != MFI->getStackID(FI)) {
  241. LLVM_DEBUG(dbgs() << "cannot share FIs with different stack IDs\n");
  242. Share = false;
  243. }
  244. // Assign it to the first available color (assumed to be the best) if it's
  245. // not possible to share a used color with other objects.
  246. if (!Share) {
  247. assert(NextColors[StackID] != -1 && "No more spill slots?");
  248. Color = NextColors[StackID];
  249. UsedColors[StackID].set(Color);
  250. NextColors[StackID] = AllColors[StackID].find_next(NextColors[StackID]);
  251. }
  252. assert(MFI->getStackID(Color) == MFI->getStackID(FI));
  253. // Record the assignment.
  254. Assignments[Color].push_back(li);
  255. LLVM_DEBUG(dbgs() << "Assigning fi#" << FI << " to fi#" << Color << "\n");
  256. // Change size and alignment of the allocated slot. If there are multiple
  257. // objects sharing the same slot, then make sure the size and alignment
  258. // are large enough for all.
  259. unsigned Align = OrigAlignments[FI];
  260. if (!Share || Align > MFI->getObjectAlignment(Color))
  261. MFI->setObjectAlignment(Color, Align);
  262. int64_t Size = OrigSizes[FI];
  263. if (!Share || Size > MFI->getObjectSize(Color))
  264. MFI->setObjectSize(Color, Size);
  265. return Color;
  266. }
  267. /// Colorslots - Color all spill stack slots and rewrite all frameindex machine
  268. /// operands in the function.
  269. bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
  270. unsigned NumObjs = MFI->getObjectIndexEnd();
  271. SmallVector<int, 16> SlotMapping(NumObjs, -1);
  272. SmallVector<float, 16> SlotWeights(NumObjs, 0.0);
  273. SmallVector<SmallVector<int, 4>, 16> RevMap(NumObjs);
  274. BitVector UsedColors(NumObjs);
  275. LLVM_DEBUG(dbgs() << "Color spill slot intervals:\n");
  276. bool Changed = false;
  277. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
  278. LiveInterval *li = SSIntervals[i];
  279. int SS = Register::stackSlot2Index(li->reg);
  280. int NewSS = ColorSlot(li);
  281. assert(NewSS >= 0 && "Stack coloring failed?");
  282. SlotMapping[SS] = NewSS;
  283. RevMap[NewSS].push_back(SS);
  284. SlotWeights[NewSS] += li->weight;
  285. UsedColors.set(NewSS);
  286. Changed |= (SS != NewSS);
  287. }
  288. LLVM_DEBUG(dbgs() << "\nSpill slots after coloring:\n");
  289. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
  290. LiveInterval *li = SSIntervals[i];
  291. int SS = Register::stackSlot2Index(li->reg);
  292. li->weight = SlotWeights[SS];
  293. }
  294. // Sort them by new weight.
  295. llvm::stable_sort(SSIntervals, IntervalSorter());
  296. #ifndef NDEBUG
  297. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i)
  298. LLVM_DEBUG(SSIntervals[i]->dump());
  299. LLVM_DEBUG(dbgs() << '\n');
  300. #endif
  301. if (!Changed)
  302. return false;
  303. // Rewrite all MachineMemOperands.
  304. for (unsigned SS = 0, SE = SSRefs.size(); SS != SE; ++SS) {
  305. int NewFI = SlotMapping[SS];
  306. if (NewFI == -1 || (NewFI == (int)SS))
  307. continue;
  308. const PseudoSourceValue *NewSV = MF.getPSVManager().getFixedStack(NewFI);
  309. SmallVectorImpl<MachineMemOperand *> &RefMMOs = SSRefs[SS];
  310. for (unsigned i = 0, e = RefMMOs.size(); i != e; ++i)
  311. RefMMOs[i]->setValue(NewSV);
  312. }
  313. // Rewrite all MO_FrameIndex operands. Look for dead stores.
  314. for (MachineBasicBlock &MBB : MF) {
  315. for (MachineInstr &MI : MBB)
  316. RewriteInstruction(MI, SlotMapping, MF);
  317. RemoveDeadStores(&MBB);
  318. }
  319. // Delete unused stack slots.
  320. for (int StackID = 0, E = AllColors.size(); StackID != E; ++StackID) {
  321. int NextColor = NextColors[StackID];
  322. while (NextColor != -1) {
  323. LLVM_DEBUG(dbgs() << "Removing unused stack object fi#" << NextColor << "\n");
  324. MFI->RemoveStackObject(NextColor);
  325. NextColor = AllColors[StackID].find_next(NextColor);
  326. }
  327. }
  328. return true;
  329. }
  330. /// RewriteInstruction - Rewrite specified instruction by replacing references
  331. /// to old frame index with new one.
  332. void StackSlotColoring::RewriteInstruction(MachineInstr &MI,
  333. SmallVectorImpl<int> &SlotMapping,
  334. MachineFunction &MF) {
  335. // Update the operands.
  336. for (unsigned i = 0, ee = MI.getNumOperands(); i != ee; ++i) {
  337. MachineOperand &MO = MI.getOperand(i);
  338. if (!MO.isFI())
  339. continue;
  340. int OldFI = MO.getIndex();
  341. if (OldFI < 0)
  342. continue;
  343. int NewFI = SlotMapping[OldFI];
  344. if (NewFI == -1 || NewFI == OldFI)
  345. continue;
  346. assert(MFI->getStackID(OldFI) == MFI->getStackID(NewFI));
  347. MO.setIndex(NewFI);
  348. }
  349. // The MachineMemOperands have already been updated.
  350. }
  351. /// RemoveDeadStores - Scan through a basic block and look for loads followed
  352. /// by stores. If they're both using the same stack slot, then the store is
  353. /// definitely dead. This could obviously be much more aggressive (consider
  354. /// pairs with instructions between them), but such extensions might have a
  355. /// considerable compile time impact.
  356. bool StackSlotColoring::RemoveDeadStores(MachineBasicBlock* MBB) {
  357. // FIXME: This could be much more aggressive, but we need to investigate
  358. // the compile time impact of doing so.
  359. bool changed = false;
  360. SmallVector<MachineInstr*, 4> toErase;
  361. for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
  362. I != E; ++I) {
  363. if (DCELimit != -1 && (int)NumDead >= DCELimit)
  364. break;
  365. int FirstSS, SecondSS;
  366. if (TII->isStackSlotCopy(*I, FirstSS, SecondSS) && FirstSS == SecondSS &&
  367. FirstSS != -1) {
  368. ++NumDead;
  369. changed = true;
  370. toErase.push_back(&*I);
  371. continue;
  372. }
  373. MachineBasicBlock::iterator NextMI = std::next(I);
  374. MachineBasicBlock::iterator ProbableLoadMI = I;
  375. unsigned LoadReg = 0;
  376. unsigned StoreReg = 0;
  377. unsigned LoadSize = 0;
  378. unsigned StoreSize = 0;
  379. if (!(LoadReg = TII->isLoadFromStackSlot(*I, FirstSS, LoadSize)))
  380. continue;
  381. // Skip the ...pseudo debugging... instructions between a load and store.
  382. while ((NextMI != E) && NextMI->isDebugInstr()) {
  383. ++NextMI;
  384. ++I;
  385. }
  386. if (NextMI == E) continue;
  387. if (!(StoreReg = TII->isStoreToStackSlot(*NextMI, SecondSS, StoreSize)))
  388. continue;
  389. if (FirstSS != SecondSS || LoadReg != StoreReg || FirstSS == -1 ||
  390. LoadSize != StoreSize)
  391. continue;
  392. ++NumDead;
  393. changed = true;
  394. if (NextMI->findRegisterUseOperandIdx(LoadReg, true, nullptr) != -1) {
  395. ++NumDead;
  396. toErase.push_back(&*ProbableLoadMI);
  397. }
  398. toErase.push_back(&*NextMI);
  399. ++I;
  400. }
  401. for (SmallVectorImpl<MachineInstr *>::iterator I = toErase.begin(),
  402. E = toErase.end(); I != E; ++I)
  403. (*I)->eraseFromParent();
  404. return changed;
  405. }
  406. bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
  407. LLVM_DEBUG({
  408. dbgs() << "********** Stack Slot Coloring **********\n"
  409. << "********** Function: " << MF.getName() << '\n';
  410. });
  411. if (skipFunction(MF.getFunction()))
  412. return false;
  413. MFI = &MF.getFrameInfo();
  414. TII = MF.getSubtarget().getInstrInfo();
  415. LS = &getAnalysis<LiveStacks>();
  416. MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
  417. bool Changed = false;
  418. unsigned NumSlots = LS->getNumIntervals();
  419. if (NumSlots == 0)
  420. // Nothing to do!
  421. return false;
  422. // If there are calls to setjmp or sigsetjmp, don't perform stack slot
  423. // coloring. The stack could be modified before the longjmp is executed,
  424. // resulting in the wrong value being used afterwards. (See
  425. // <rdar://problem/8007500>.)
  426. if (MF.exposesReturnsTwice())
  427. return false;
  428. // Gather spill slot references
  429. ScanForSpillSlotRefs(MF);
  430. InitializeSlots();
  431. Changed = ColorSlots(MF);
  432. for (int &Next : NextColors)
  433. Next = -1;
  434. SSIntervals.clear();
  435. for (unsigned i = 0, e = SSRefs.size(); i != e; ++i)
  436. SSRefs[i].clear();
  437. SSRefs.clear();
  438. OrigAlignments.clear();
  439. OrigSizes.clear();
  440. AllColors.clear();
  441. UsedColors.clear();
  442. for (unsigned i = 0, e = Assignments.size(); i != e; ++i)
  443. Assignments[i].clear();
  444. Assignments.clear();
  445. return Changed;
  446. }