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