StackSlotColoring.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. #define DEBUG_TYPE "stackcoloring"
  14. #include "VirtRegMap.h"
  15. #include "llvm/Function.h"
  16. #include "llvm/Module.h"
  17. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  19. #include "llvm/CodeGen/LiveStackAnalysis.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineLoopInfo.h"
  23. #include "llvm/CodeGen/MachineMemOperand.h"
  24. #include "llvm/CodeGen/MachineRegisterInfo.h"
  25. #include "llvm/CodeGen/PseudoSourceValue.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Target/TargetInstrInfo.h"
  29. #include "llvm/Target/TargetMachine.h"
  30. #include "llvm/ADT/BitVector.h"
  31. #include "llvm/ADT/SmallSet.h"
  32. #include "llvm/ADT/SmallVector.h"
  33. #include "llvm/ADT/Statistic.h"
  34. #include <vector>
  35. using namespace llvm;
  36. static cl::opt<bool>
  37. DisableSharing("no-stack-slot-sharing",
  38. cl::init(false), cl::Hidden,
  39. cl::desc("Suppress slot sharing during stack coloring"));
  40. static cl::opt<bool>
  41. ColorWithRegsOpt("color-ss-with-regs",
  42. cl::init(false), cl::Hidden,
  43. cl::desc("Color stack slots with free registers"));
  44. static cl::opt<int> DCELimit("ssc-dce-limit", cl::init(-1), cl::Hidden);
  45. STATISTIC(NumEliminated, "Number of stack slots eliminated due to coloring");
  46. STATISTIC(NumRegRepl, "Number of stack slot refs replaced with reg refs");
  47. STATISTIC(NumLoadElim, "Number of loads eliminated");
  48. STATISTIC(NumStoreElim, "Number of stores eliminated");
  49. STATISTIC(NumDead, "Number of trivially dead stack accesses eliminated");
  50. namespace {
  51. class StackSlotColoring : public MachineFunctionPass {
  52. bool ColorWithRegs;
  53. LiveStacks* LS;
  54. VirtRegMap* VRM;
  55. MachineFrameInfo *MFI;
  56. MachineRegisterInfo *MRI;
  57. const TargetInstrInfo *TII;
  58. const TargetRegisterInfo *TRI;
  59. const MachineLoopInfo *loopInfo;
  60. // SSIntervals - Spill slot intervals.
  61. std::vector<LiveInterval*> SSIntervals;
  62. // SSRefs - Keep a list of frame index references for each spill slot.
  63. SmallVector<SmallVector<MachineInstr*, 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.
  72. BitVector AllColors;
  73. // NextColor - Next "color" that's not yet used.
  74. int NextColor;
  75. // UsedColors - "Colors" that have been assigned.
  76. BitVector UsedColors;
  77. // Assignments - Color to intervals mapping.
  78. SmallVector<SmallVector<LiveInterval*,4>, 16> Assignments;
  79. public:
  80. static char ID; // Pass identification
  81. StackSlotColoring() :
  82. MachineFunctionPass(ID), ColorWithRegs(false), NextColor(-1) {
  83. initializeStackSlotColoringPass(*PassRegistry::getPassRegistry());
  84. }
  85. StackSlotColoring(bool RegColor) :
  86. MachineFunctionPass(ID), ColorWithRegs(RegColor), NextColor(-1) {
  87. initializeStackSlotColoringPass(*PassRegistry::getPassRegistry());
  88. }
  89. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  90. AU.setPreservesCFG();
  91. AU.addRequired<SlotIndexes>();
  92. AU.addPreserved<SlotIndexes>();
  93. AU.addRequired<LiveStacks>();
  94. AU.addRequired<VirtRegMap>();
  95. AU.addPreserved<VirtRegMap>();
  96. AU.addRequired<MachineLoopInfo>();
  97. AU.addPreserved<MachineLoopInfo>();
  98. AU.addPreservedID(MachineDominatorsID);
  99. MachineFunctionPass::getAnalysisUsage(AU);
  100. }
  101. virtual bool runOnMachineFunction(MachineFunction &MF);
  102. virtual const char* getPassName() const {
  103. return "Stack Slot Coloring";
  104. }
  105. private:
  106. void InitializeSlots();
  107. void ScanForSpillSlotRefs(MachineFunction &MF);
  108. bool OverlapWithAssignments(LiveInterval *li, int Color) const;
  109. int ColorSlot(LiveInterval *li);
  110. bool ColorSlots(MachineFunction &MF);
  111. bool ColorSlotsWithFreeRegs(SmallVector<int, 16> &SlotMapping,
  112. SmallVector<SmallVector<int, 4>, 16> &RevMap,
  113. BitVector &SlotIsReg);
  114. void RewriteInstruction(MachineInstr *MI, int OldFI, int NewFI,
  115. MachineFunction &MF);
  116. bool PropagateBackward(MachineBasicBlock::iterator MII,
  117. MachineBasicBlock *MBB,
  118. unsigned OldReg, unsigned NewReg);
  119. bool PropagateForward(MachineBasicBlock::iterator MII,
  120. MachineBasicBlock *MBB,
  121. unsigned OldReg, unsigned NewReg);
  122. void UnfoldAndRewriteInstruction(MachineInstr *MI, int OldFI,
  123. unsigned Reg, const TargetRegisterClass *RC,
  124. SmallSet<unsigned, 4> &Defs,
  125. MachineFunction &MF);
  126. bool AllMemRefsCanBeUnfolded(int SS);
  127. bool RemoveDeadStores(MachineBasicBlock* MBB);
  128. };
  129. } // end anonymous namespace
  130. char StackSlotColoring::ID = 0;
  131. INITIALIZE_PASS_BEGIN(StackSlotColoring, "stack-slot-coloring",
  132. "Stack Slot Coloring", false, false)
  133. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  134. INITIALIZE_PASS_DEPENDENCY(LiveStacks)
  135. INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
  136. INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
  137. INITIALIZE_PASS_END(StackSlotColoring, "stack-slot-coloring",
  138. "Stack Slot Coloring", false, false)
  139. FunctionPass *llvm::createStackSlotColoringPass(bool RegColor) {
  140. return new StackSlotColoring(RegColor);
  141. }
  142. namespace {
  143. // IntervalSorter - Comparison predicate that sort live intervals by
  144. // their weight.
  145. struct IntervalSorter {
  146. bool operator()(LiveInterval* LHS, LiveInterval* RHS) const {
  147. return LHS->weight > RHS->weight;
  148. }
  149. };
  150. }
  151. /// ScanForSpillSlotRefs - Scan all the machine instructions for spill slot
  152. /// references and update spill slot weights.
  153. void StackSlotColoring::ScanForSpillSlotRefs(MachineFunction &MF) {
  154. SSRefs.resize(MFI->getObjectIndexEnd());
  155. // FIXME: Need the equivalent of MachineRegisterInfo for frameindex operands.
  156. for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
  157. MBBI != E; ++MBBI) {
  158. MachineBasicBlock *MBB = &*MBBI;
  159. unsigned loopDepth = loopInfo->getLoopDepth(MBB);
  160. for (MachineBasicBlock::iterator MII = MBB->begin(), EE = MBB->end();
  161. MII != EE; ++MII) {
  162. MachineInstr *MI = &*MII;
  163. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  164. MachineOperand &MO = MI->getOperand(i);
  165. if (!MO.isFI())
  166. continue;
  167. int FI = MO.getIndex();
  168. if (FI < 0)
  169. continue;
  170. if (!LS->hasInterval(FI))
  171. continue;
  172. LiveInterval &li = LS->getInterval(FI);
  173. if (!MI->isDebugValue())
  174. li.weight += LiveIntervals::getSpillWeight(false, true, loopDepth);
  175. SSRefs[FI].push_back(MI);
  176. }
  177. }
  178. }
  179. }
  180. /// InitializeSlots - Process all spill stack slot liveintervals and add them
  181. /// to a sorted (by weight) list.
  182. void StackSlotColoring::InitializeSlots() {
  183. int LastFI = MFI->getObjectIndexEnd();
  184. OrigAlignments.resize(LastFI);
  185. OrigSizes.resize(LastFI);
  186. AllColors.resize(LastFI);
  187. UsedColors.resize(LastFI);
  188. Assignments.resize(LastFI);
  189. // Gather all spill slots into a list.
  190. DEBUG(dbgs() << "Spill slot intervals:\n");
  191. for (LiveStacks::iterator i = LS->begin(), e = LS->end(); i != e; ++i) {
  192. LiveInterval &li = i->second;
  193. DEBUG(li.dump());
  194. int FI = TargetRegisterInfo::stackSlot2Index(li.reg);
  195. if (MFI->isDeadObjectIndex(FI))
  196. continue;
  197. SSIntervals.push_back(&li);
  198. OrigAlignments[FI] = MFI->getObjectAlignment(FI);
  199. OrigSizes[FI] = MFI->getObjectSize(FI);
  200. AllColors.set(FI);
  201. }
  202. DEBUG(dbgs() << '\n');
  203. // Sort them by weight.
  204. std::stable_sort(SSIntervals.begin(), SSIntervals.end(), IntervalSorter());
  205. // Get first "color".
  206. NextColor = AllColors.find_first();
  207. }
  208. /// OverlapWithAssignments - Return true if LiveInterval overlaps with any
  209. /// LiveIntervals that have already been assigned to the specified color.
  210. bool
  211. StackSlotColoring::OverlapWithAssignments(LiveInterval *li, int Color) const {
  212. const SmallVector<LiveInterval*,4> &OtherLIs = Assignments[Color];
  213. for (unsigned i = 0, e = OtherLIs.size(); i != e; ++i) {
  214. LiveInterval *OtherLI = OtherLIs[i];
  215. if (OtherLI->overlaps(*li))
  216. return true;
  217. }
  218. return false;
  219. }
  220. /// ColorSlotsWithFreeRegs - If there are any free registers available, try
  221. /// replacing spill slots references with registers instead.
  222. bool
  223. StackSlotColoring::ColorSlotsWithFreeRegs(SmallVector<int, 16> &SlotMapping,
  224. SmallVector<SmallVector<int, 4>, 16> &RevMap,
  225. BitVector &SlotIsReg) {
  226. if (!(ColorWithRegs || ColorWithRegsOpt) || !VRM->HasUnusedRegisters())
  227. return false;
  228. bool Changed = false;
  229. DEBUG(dbgs() << "Assigning unused registers to spill slots:\n");
  230. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
  231. LiveInterval *li = SSIntervals[i];
  232. int SS = TargetRegisterInfo::stackSlot2Index(li->reg);
  233. if (!UsedColors[SS] || li->weight < 20)
  234. // If the weight is < 20, i.e. two references in a loop with depth 1,
  235. // don't bother with it.
  236. continue;
  237. // These slots allow to share the same registers.
  238. bool AllColored = true;
  239. SmallVector<unsigned, 4> ColoredRegs;
  240. for (unsigned j = 0, ee = RevMap[SS].size(); j != ee; ++j) {
  241. int RSS = RevMap[SS][j];
  242. const TargetRegisterClass *RC = LS->getIntervalRegClass(RSS);
  243. // If it's not colored to another stack slot, try coloring it
  244. // to a "free" register.
  245. if (!RC) {
  246. AllColored = false;
  247. continue;
  248. }
  249. unsigned Reg = VRM->getFirstUnusedRegister(RC);
  250. if (!Reg) {
  251. AllColored = false;
  252. continue;
  253. }
  254. if (!AllMemRefsCanBeUnfolded(RSS)) {
  255. AllColored = false;
  256. continue;
  257. } else {
  258. DEBUG(dbgs() << "Assigning fi#" << RSS << " to "
  259. << TRI->getName(Reg) << '\n');
  260. ColoredRegs.push_back(Reg);
  261. SlotMapping[RSS] = Reg;
  262. SlotIsReg.set(RSS);
  263. Changed = true;
  264. }
  265. }
  266. // Register and its sub-registers are no longer free.
  267. while (!ColoredRegs.empty()) {
  268. unsigned Reg = ColoredRegs.back();
  269. ColoredRegs.pop_back();
  270. VRM->setRegisterUsed(Reg);
  271. // If reg is a callee-saved register, it will have to be spilled in
  272. // the prologue.
  273. MRI->setPhysRegUsed(Reg);
  274. for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) {
  275. VRM->setRegisterUsed(*AS);
  276. MRI->setPhysRegUsed(*AS);
  277. }
  278. }
  279. // This spill slot is dead after the rewrites
  280. if (AllColored) {
  281. MFI->RemoveStackObject(SS);
  282. ++NumEliminated;
  283. }
  284. }
  285. DEBUG(dbgs() << '\n');
  286. return Changed;
  287. }
  288. /// ColorSlot - Assign a "color" (stack slot) to the specified stack slot.
  289. ///
  290. int StackSlotColoring::ColorSlot(LiveInterval *li) {
  291. int Color = -1;
  292. bool Share = false;
  293. if (!DisableSharing) {
  294. // Check if it's possible to reuse any of the used colors.
  295. Color = UsedColors.find_first();
  296. while (Color != -1) {
  297. if (!OverlapWithAssignments(li, Color)) {
  298. Share = true;
  299. ++NumEliminated;
  300. break;
  301. }
  302. Color = UsedColors.find_next(Color);
  303. }
  304. }
  305. // Assign it to the first available color (assumed to be the best) if it's
  306. // not possible to share a used color with other objects.
  307. if (!Share) {
  308. assert(NextColor != -1 && "No more spill slots?");
  309. Color = NextColor;
  310. UsedColors.set(Color);
  311. NextColor = AllColors.find_next(NextColor);
  312. }
  313. // Record the assignment.
  314. Assignments[Color].push_back(li);
  315. int FI = TargetRegisterInfo::stackSlot2Index(li->reg);
  316. DEBUG(dbgs() << "Assigning fi#" << FI << " to fi#" << Color << "\n");
  317. // Change size and alignment of the allocated slot. If there are multiple
  318. // objects sharing the same slot, then make sure the size and alignment
  319. // are large enough for all.
  320. unsigned Align = OrigAlignments[FI];
  321. if (!Share || Align > MFI->getObjectAlignment(Color))
  322. MFI->setObjectAlignment(Color, Align);
  323. int64_t Size = OrigSizes[FI];
  324. if (!Share || Size > MFI->getObjectSize(Color))
  325. MFI->setObjectSize(Color, Size);
  326. return Color;
  327. }
  328. /// Colorslots - Color all spill stack slots and rewrite all frameindex machine
  329. /// operands in the function.
  330. bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
  331. unsigned NumObjs = MFI->getObjectIndexEnd();
  332. SmallVector<int, 16> SlotMapping(NumObjs, -1);
  333. SmallVector<float, 16> SlotWeights(NumObjs, 0.0);
  334. SmallVector<SmallVector<int, 4>, 16> RevMap(NumObjs);
  335. BitVector SlotIsReg(NumObjs);
  336. BitVector UsedColors(NumObjs);
  337. DEBUG(dbgs() << "Color spill slot intervals:\n");
  338. bool Changed = false;
  339. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
  340. LiveInterval *li = SSIntervals[i];
  341. int SS = TargetRegisterInfo::stackSlot2Index(li->reg);
  342. int NewSS = ColorSlot(li);
  343. assert(NewSS >= 0 && "Stack coloring failed?");
  344. SlotMapping[SS] = NewSS;
  345. RevMap[NewSS].push_back(SS);
  346. SlotWeights[NewSS] += li->weight;
  347. UsedColors.set(NewSS);
  348. Changed |= (SS != NewSS);
  349. }
  350. DEBUG(dbgs() << "\nSpill slots after coloring:\n");
  351. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
  352. LiveInterval *li = SSIntervals[i];
  353. int SS = TargetRegisterInfo::stackSlot2Index(li->reg);
  354. li->weight = SlotWeights[SS];
  355. }
  356. // Sort them by new weight.
  357. std::stable_sort(SSIntervals.begin(), SSIntervals.end(), IntervalSorter());
  358. #ifndef NDEBUG
  359. for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i)
  360. DEBUG(SSIntervals[i]->dump());
  361. DEBUG(dbgs() << '\n');
  362. #endif
  363. // Can we "color" a stack slot with a unused register?
  364. Changed |= ColorSlotsWithFreeRegs(SlotMapping, RevMap, SlotIsReg);
  365. if (!Changed)
  366. return false;
  367. // Rewrite all MO_FrameIndex operands.
  368. SmallVector<SmallSet<unsigned, 4>, 4> NewDefs(MF.getNumBlockIDs());
  369. for (unsigned SS = 0, SE = SSRefs.size(); SS != SE; ++SS) {
  370. bool isReg = SlotIsReg[SS];
  371. int NewFI = SlotMapping[SS];
  372. if (NewFI == -1 || (NewFI == (int)SS && !isReg))
  373. continue;
  374. const TargetRegisterClass *RC = LS->getIntervalRegClass(SS);
  375. SmallVector<MachineInstr*, 8> &RefMIs = SSRefs[SS];
  376. for (unsigned i = 0, e = RefMIs.size(); i != e; ++i)
  377. if (!isReg)
  378. RewriteInstruction(RefMIs[i], SS, NewFI, MF);
  379. else {
  380. // Rewrite to use a register instead.
  381. unsigned MBBId = RefMIs[i]->getParent()->getNumber();
  382. SmallSet<unsigned, 4> &Defs = NewDefs[MBBId];
  383. UnfoldAndRewriteInstruction(RefMIs[i], SS, NewFI, RC, Defs, MF);
  384. }
  385. }
  386. // Delete unused stack slots.
  387. while (NextColor != -1) {
  388. DEBUG(dbgs() << "Removing unused stack object fi#" << NextColor << "\n");
  389. MFI->RemoveStackObject(NextColor);
  390. NextColor = AllColors.find_next(NextColor);
  391. }
  392. return true;
  393. }
  394. /// AllMemRefsCanBeUnfolded - Return true if all references of the specified
  395. /// spill slot index can be unfolded.
  396. bool StackSlotColoring::AllMemRefsCanBeUnfolded(int SS) {
  397. SmallVector<MachineInstr*, 8> &RefMIs = SSRefs[SS];
  398. for (unsigned i = 0, e = RefMIs.size(); i != e; ++i) {
  399. MachineInstr *MI = RefMIs[i];
  400. if (TII->isLoadFromStackSlot(MI, SS) ||
  401. TII->isStoreToStackSlot(MI, SS))
  402. // Restore and spill will become copies.
  403. return true;
  404. if (!TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), false, false))
  405. return false;
  406. for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
  407. MachineOperand &MO = MI->getOperand(j);
  408. if (MO.isFI() && MO.getIndex() != SS)
  409. // If it uses another frameindex, we can, currently* unfold it.
  410. return false;
  411. }
  412. }
  413. return true;
  414. }
  415. /// RewriteInstruction - Rewrite specified instruction by replacing references
  416. /// to old frame index with new one.
  417. void StackSlotColoring::RewriteInstruction(MachineInstr *MI, int OldFI,
  418. int NewFI, MachineFunction &MF) {
  419. // Update the operands.
  420. for (unsigned i = 0, ee = MI->getNumOperands(); i != ee; ++i) {
  421. MachineOperand &MO = MI->getOperand(i);
  422. if (!MO.isFI())
  423. continue;
  424. int FI = MO.getIndex();
  425. if (FI != OldFI)
  426. continue;
  427. MO.setIndex(NewFI);
  428. }
  429. // Update the memory references. This changes the MachineMemOperands
  430. // directly. They may be in use by multiple instructions, however all
  431. // instructions using OldFI are being rewritten to use NewFI.
  432. const Value *OldSV = PseudoSourceValue::getFixedStack(OldFI);
  433. const Value *NewSV = PseudoSourceValue::getFixedStack(NewFI);
  434. for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
  435. E = MI->memoperands_end(); I != E; ++I)
  436. if ((*I)->getValue() == OldSV)
  437. (*I)->setValue(NewSV);
  438. }
  439. /// PropagateBackward - Traverse backward and look for the definition of
  440. /// OldReg. If it can successfully update all of the references with NewReg,
  441. /// do so and return true.
  442. bool StackSlotColoring::PropagateBackward(MachineBasicBlock::iterator MII,
  443. MachineBasicBlock *MBB,
  444. unsigned OldReg, unsigned NewReg) {
  445. if (MII == MBB->begin())
  446. return false;
  447. SmallVector<MachineOperand*, 4> Uses;
  448. SmallVector<MachineOperand*, 4> Refs;
  449. while (--MII != MBB->begin()) {
  450. bool FoundDef = false; // Not counting 2address def.
  451. Uses.clear();
  452. const TargetInstrDesc &TID = MII->getDesc();
  453. for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
  454. MachineOperand &MO = MII->getOperand(i);
  455. if (!MO.isReg())
  456. continue;
  457. unsigned Reg = MO.getReg();
  458. if (Reg == 0)
  459. continue;
  460. if (Reg == OldReg) {
  461. if (MO.isImplicit())
  462. return false;
  463. // Abort the use is actually a sub-register def. We don't have enough
  464. // information to figure out if it is really legal.
  465. if (MO.getSubReg() || MII->isSubregToReg())
  466. return false;
  467. const TargetRegisterClass *RC = TID.OpInfo[i].getRegClass(TRI);
  468. if (RC && !RC->contains(NewReg))
  469. return false;
  470. if (MO.isUse()) {
  471. Uses.push_back(&MO);
  472. } else {
  473. Refs.push_back(&MO);
  474. if (!MII->isRegTiedToUseOperand(i))
  475. FoundDef = true;
  476. }
  477. } else if (TRI->regsOverlap(Reg, NewReg)) {
  478. return false;
  479. } else if (TRI->regsOverlap(Reg, OldReg)) {
  480. if (!MO.isUse() || !MO.isKill())
  481. return false;
  482. }
  483. }
  484. if (FoundDef) {
  485. // Found non-two-address def. Stop here.
  486. for (unsigned i = 0, e = Refs.size(); i != e; ++i)
  487. Refs[i]->setReg(NewReg);
  488. return true;
  489. }
  490. // Two-address uses must be updated as well.
  491. for (unsigned i = 0, e = Uses.size(); i != e; ++i)
  492. Refs.push_back(Uses[i]);
  493. }
  494. return false;
  495. }
  496. /// PropagateForward - Traverse forward and look for the kill of OldReg. If
  497. /// it can successfully update all of the uses with NewReg, do so and
  498. /// return true.
  499. bool StackSlotColoring::PropagateForward(MachineBasicBlock::iterator MII,
  500. MachineBasicBlock *MBB,
  501. unsigned OldReg, unsigned NewReg) {
  502. if (MII == MBB->end())
  503. return false;
  504. SmallVector<MachineOperand*, 4> Uses;
  505. while (++MII != MBB->end()) {
  506. bool FoundKill = false;
  507. const TargetInstrDesc &TID = MII->getDesc();
  508. for (unsigned i = 0, e = MII->getNumOperands(); i != e; ++i) {
  509. MachineOperand &MO = MII->getOperand(i);
  510. if (!MO.isReg())
  511. continue;
  512. unsigned Reg = MO.getReg();
  513. if (Reg == 0)
  514. continue;
  515. if (Reg == OldReg) {
  516. if (MO.isDef() || MO.isImplicit())
  517. return false;
  518. // Abort the use is actually a sub-register use. We don't have enough
  519. // information to figure out if it is really legal.
  520. if (MO.getSubReg())
  521. return false;
  522. const TargetRegisterClass *RC = TID.OpInfo[i].getRegClass(TRI);
  523. if (RC && !RC->contains(NewReg))
  524. return false;
  525. if (MO.isKill())
  526. FoundKill = true;
  527. Uses.push_back(&MO);
  528. } else if (TRI->regsOverlap(Reg, NewReg) ||
  529. TRI->regsOverlap(Reg, OldReg))
  530. return false;
  531. }
  532. if (FoundKill) {
  533. for (unsigned i = 0, e = Uses.size(); i != e; ++i)
  534. Uses[i]->setReg(NewReg);
  535. return true;
  536. }
  537. }
  538. return false;
  539. }
  540. /// UnfoldAndRewriteInstruction - Rewrite specified instruction by unfolding
  541. /// folded memory references and replacing those references with register
  542. /// references instead.
  543. void
  544. StackSlotColoring::UnfoldAndRewriteInstruction(MachineInstr *MI, int OldFI,
  545. unsigned Reg,
  546. const TargetRegisterClass *RC,
  547. SmallSet<unsigned, 4> &Defs,
  548. MachineFunction &MF) {
  549. MachineBasicBlock *MBB = MI->getParent();
  550. if (unsigned DstReg = TII->isLoadFromStackSlot(MI, OldFI)) {
  551. if (PropagateForward(MI, MBB, DstReg, Reg)) {
  552. DEBUG(dbgs() << "Eliminated load: ");
  553. DEBUG(MI->dump());
  554. ++NumLoadElim;
  555. } else {
  556. BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(TargetOpcode::COPY),
  557. DstReg).addReg(Reg);
  558. ++NumRegRepl;
  559. }
  560. if (!Defs.count(Reg)) {
  561. // If this is the first use of Reg in this MBB and it wasn't previously
  562. // defined in MBB, add it to livein.
  563. MBB->addLiveIn(Reg);
  564. Defs.insert(Reg);
  565. }
  566. } else if (unsigned SrcReg = TII->isStoreToStackSlot(MI, OldFI)) {
  567. if (MI->killsRegister(SrcReg) && PropagateBackward(MI, MBB, SrcReg, Reg)) {
  568. DEBUG(dbgs() << "Eliminated store: ");
  569. DEBUG(MI->dump());
  570. ++NumStoreElim;
  571. } else {
  572. BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(TargetOpcode::COPY), Reg)
  573. .addReg(SrcReg);
  574. ++NumRegRepl;
  575. }
  576. // Remember reg has been defined in MBB.
  577. Defs.insert(Reg);
  578. } else {
  579. SmallVector<MachineInstr*, 4> NewMIs;
  580. bool Success = TII->unfoldMemoryOperand(MF, MI, Reg, false, false, NewMIs);
  581. (void)Success; // Silence compiler warning.
  582. assert(Success && "Failed to unfold!");
  583. MachineInstr *NewMI = NewMIs[0];
  584. MBB->insert(MI, NewMI);
  585. ++NumRegRepl;
  586. if (NewMI->readsRegister(Reg)) {
  587. if (!Defs.count(Reg))
  588. // If this is the first use of Reg in this MBB and it wasn't previously
  589. // defined in MBB, add it to livein.
  590. MBB->addLiveIn(Reg);
  591. Defs.insert(Reg);
  592. }
  593. }
  594. MBB->erase(MI);
  595. }
  596. /// RemoveDeadStores - Scan through a basic block and look for loads followed
  597. /// by stores. If they're both using the same stack slot, then the store is
  598. /// definitely dead. This could obviously be much more aggressive (consider
  599. /// pairs with instructions between them), but such extensions might have a
  600. /// considerable compile time impact.
  601. bool StackSlotColoring::RemoveDeadStores(MachineBasicBlock* MBB) {
  602. // FIXME: This could be much more aggressive, but we need to investigate
  603. // the compile time impact of doing so.
  604. bool changed = false;
  605. SmallVector<MachineInstr*, 4> toErase;
  606. for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
  607. I != E; ++I) {
  608. if (DCELimit != -1 && (int)NumDead >= DCELimit)
  609. break;
  610. MachineBasicBlock::iterator NextMI = llvm::next(I);
  611. if (NextMI == MBB->end()) continue;
  612. int FirstSS, SecondSS;
  613. unsigned LoadReg = 0;
  614. unsigned StoreReg = 0;
  615. if (!(LoadReg = TII->isLoadFromStackSlot(I, FirstSS))) continue;
  616. if (!(StoreReg = TII->isStoreToStackSlot(NextMI, SecondSS))) continue;
  617. if (FirstSS != SecondSS || LoadReg != StoreReg || FirstSS == -1) continue;
  618. ++NumDead;
  619. changed = true;
  620. if (NextMI->findRegisterUseOperandIdx(LoadReg, true, 0) != -1) {
  621. ++NumDead;
  622. toErase.push_back(I);
  623. }
  624. toErase.push_back(NextMI);
  625. ++I;
  626. }
  627. for (SmallVector<MachineInstr*, 4>::iterator I = toErase.begin(),
  628. E = toErase.end(); I != E; ++I)
  629. (*I)->eraseFromParent();
  630. return changed;
  631. }
  632. bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
  633. DEBUG({
  634. dbgs() << "********** Stack Slot Coloring **********\n"
  635. << "********** Function: "
  636. << MF.getFunction()->getName() << '\n';
  637. });
  638. MFI = MF.getFrameInfo();
  639. MRI = &MF.getRegInfo();
  640. TII = MF.getTarget().getInstrInfo();
  641. TRI = MF.getTarget().getRegisterInfo();
  642. LS = &getAnalysis<LiveStacks>();
  643. VRM = &getAnalysis<VirtRegMap>();
  644. loopInfo = &getAnalysis<MachineLoopInfo>();
  645. bool Changed = false;
  646. unsigned NumSlots = LS->getNumIntervals();
  647. if (NumSlots < 2) {
  648. if (NumSlots == 0 || !VRM->HasUnusedRegisters())
  649. // Nothing to do!
  650. return false;
  651. }
  652. // If there are calls to setjmp or sigsetjmp, don't perform stack slot
  653. // coloring. The stack could be modified before the longjmp is executed,
  654. // resulting in the wrong value being used afterwards. (See
  655. // <rdar://problem/8007500>.)
  656. if (MF.callsSetJmp())
  657. return false;
  658. // Gather spill slot references
  659. ScanForSpillSlotRefs(MF);
  660. InitializeSlots();
  661. Changed = ColorSlots(MF);
  662. NextColor = -1;
  663. SSIntervals.clear();
  664. for (unsigned i = 0, e = SSRefs.size(); i != e; ++i)
  665. SSRefs[i].clear();
  666. SSRefs.clear();
  667. OrigAlignments.clear();
  668. OrigSizes.clear();
  669. AllColors.clear();
  670. UsedColors.clear();
  671. for (unsigned i = 0, e = Assignments.size(); i != e; ++i)
  672. Assignments[i].clear();
  673. Assignments.clear();
  674. if (Changed) {
  675. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
  676. Changed |= RemoveDeadStores(I);
  677. }
  678. return Changed;
  679. }