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