StackSlotColoring.cpp 16 KB

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