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