4949#include " llvm/Support/CommandLine.h"
5050#include " llvm/Support/Compiler.h"
5151#include " llvm/Support/Debug.h"
52- #include " llvm/Support/raw_ostream.h"
5352#include " llvm/Support/DebugCounter.h"
53+ #include " llvm/Support/raw_ostream.h"
5454#include < algorithm>
5555#include < cassert>
5656#include < limits>
@@ -64,42 +64,41 @@ using namespace llvm;
6464DEBUG_COUNTER (ProcessSlot, DEBUG_TYPE " -slot" ,
6565 " Controls which slot get processed" );
6666
67- static cl::opt<bool >
68- DisableColoring (" no-stack-coloring" ,
69- cl::init (false ), cl::Hidden,
70- cl::desc(" Disable stack coloring" ));
67+ static cl::opt<bool > DisableColoring (" no-stack-coloring" , cl::init(false ),
68+ cl::Hidden,
69+ cl::desc(" Disable stack coloring" ));
7170
7271// / The user may write code that uses allocas outside of the declared lifetime
7372// / zone. This can happen when the user returns a reference to a local
7473// / data-structure. We can detect these cases and decide not to optimize the
7574// / code. If this flag is enabled, we try to save the user. This option
7675// / is treated as overriding LifetimeStartOnFirstUse below.
7776static cl::opt<bool >
78- ProtectFromEscapedAllocas (" protect-from-escaped-allocas" ,
79- cl::init ( false ), cl::Hidden,
80- cl::desc(" Do not optimize lifetime zones that "
81- " are broken" ));
77+ ProtectFromEscapedAllocas (" protect-from-escaped-allocas" , cl::init( false ) ,
78+ cl::Hidden,
79+ cl::desc(" Do not optimize lifetime zones that "
80+ " are broken" ));
8281
8382// / Enable enhanced dataflow scheme for lifetime analysis (treat first
8483// / use of stack slot as start of slot lifetime, as opposed to looking
8584// / for LIFETIME_START marker). See "Implementation notes" below for
8685// / more info.
8786static cl::opt<bool >
88- LifetimeStartOnFirstUse (" stackcoloring-lifetime-start-on-first-use" ,
89- cl::init (true ), cl::Hidden,
90- cl::desc(" Treat stack lifetimes as starting on first use, not on START marker." ));
87+ LifetimeStartOnFirstUse (" stackcoloring-lifetime-start-on-first-use" ,
88+ cl::init (true ), cl::Hidden,
89+ cl::desc(" Treat stack lifetimes as starting on "
90+ " first use, not on START marker." ));
9191
9292static cl::opt<bool > UseNewStackColoring (
9393 " new-stack-coloring" , cl::init(false ), cl::Hidden,
9494 cl::desc(" Use a better logic to try to reduce stack usage" ));
9595
9696static cl::opt<unsigned > MaxCandidatesOpt (
97- " stackcoloring-max-candidates" , cl::init(0 ),
98- cl::Hidden,
97+ " stackcoloring-max-candidates" , cl::init(0 ), cl::Hidden,
9998 cl::desc(
10099 " Max number of candidates that will be evaluated, 0 means no limit" ));
101100
102- STATISTIC (NumMarkerSeen, " Number of lifetime markers found." );
101+ STATISTIC (NumMarkerSeen, " Number of lifetime markers found." );
103102STATISTIC (GeneratedWorse, " Number of times worse layout were generated" );
104103STATISTIC (StackSpaceSaved, " Number of bytes saved due to merging slots." );
105104STATISTIC (StackSlotMerged, " Number of stack slot merged." );
@@ -392,7 +391,7 @@ class StackColoring {
392391 MachineFrameInfo *MFI = nullptr ;
393392 MachineFunction *MF = nullptr ;
394393
395- LiveStacks* LS = nullptr ;
394+ LiveStacks * LS = nullptr ;
396395
397396 struct SlotInfo {
398397 // All places in the current function where this Slot is live
@@ -420,7 +419,7 @@ class StackColoring {
420419 [&](int Idx) { return Liveness[Idx]; });
421420 }
422421
423- LLVM_DUMP_METHOD void dump (const StackColoring* State = nullptr ) const ;
422+ LLVM_DUMP_METHOD void dump (const StackColoring * State = nullptr ) const ;
424423 };
425424
426425 // / A class representing liveness information for a single basic block.
@@ -465,7 +464,7 @@ class StackColoring {
465464
466465 // / The list of lifetime markers found. These markers are to be removed
467466 // / once the coloring is done.
468- SmallVector<MachineInstr*, 8 > Markers;
467+ SmallVector<MachineInstr *, 8 > Markers;
469468
470469 // / Record the FI slots for which we have seen some sort of
471470 // / lifetime marker (either start or end).
@@ -479,7 +478,8 @@ class StackColoring {
479478 unsigned NumIterations;
480479
481480public:
482- StackColoring (SlotIndexes *Indexes, LiveStacks* LS) : LS(LS), Indexes(Indexes) {}
481+ StackColoring (SlotIndexes *Indexes, LiveStacks *LS)
482+ : LS(LS), Indexes(Indexes) {}
483483 bool run (MachineFunction &Func);
484484
485485private:
@@ -506,7 +506,8 @@ class StackColoring {
506506 unsigned doMerging (unsigned NumSlots);
507507
508508 // / Returns TRUE if we're using the first-use-begins-lifetime method for
509- // / this slot (if FALSE, then the start marker is treated as start of lifetime).
509+ // / this slot (if FALSE, then the start marker is treated as start of
510+ // / lifetime).
510511 bool applyFirstUse (int Slot) {
511512 if (!LifetimeStartOnFirstUse || ProtectFromEscapedAllocas)
512513 return false ;
@@ -520,8 +521,7 @@ class StackColoring {
520521 // / starting or ending are added to the vector "slots" and "isStart" is set
521522 // / accordingly.
522523 // / \returns True if inst contains a lifetime start or end
523- bool isLifetimeStartOrEnd (const MachineInstr &MI,
524- SmallVector<int , 4 > &slots,
524+ bool isLifetimeStartOrEnd (const MachineInstr &MI, SmallVector<int , 4 > &slots,
525525 bool &isStart);
526526
527527 // / Construct the LiveIntervals for the slots.
@@ -623,8 +623,8 @@ LLVM_DUMP_METHOD void StackColoring::dumpBB(MachineBasicBlock *MBB) const {
623623
624624LLVM_DUMP_METHOD void StackColoring::dump () const {
625625 for (MachineBasicBlock *MBB : depth_first (MF)) {
626- dbgs () << " Inspecting block #" << MBB->getNumber () << " ["
627- << MBB-> getName () << " ]\n " ;
626+ dbgs () << " Inspecting block #" << MBB->getNumber () << " [" << MBB-> getName ()
627+ << " ]\n " ;
628628 dumpBB (MBB);
629629 }
630630}
@@ -642,7 +642,8 @@ LLVM_DUMP_METHOD void StackColoring::dumpIntervals() const {
642642 }
643643}
644644
645- LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump (const StackColoring* State) const {
645+ LLVM_DUMP_METHOD void
646+ StackColoring::SlotInfo::dump (const StackColoring *State) const {
646647 unsigned Slot = InvalidIdx;
647648 if (State) {
648649 Slot = this - State->Slot2Info .data ();
@@ -654,7 +655,8 @@ LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump(const StackColoring* State)
654655 dbgs () << " offset=" << Offset;
655656 if (State) {
656657 if (State->MFI ->getObjectAllocation (Slot))
657- dbgs () << " \" " << State->MFI ->getObjectAllocation (Slot)->getName () << " \" " ;
658+ dbgs () << " \" " << State->MFI ->getObjectAllocation (Slot)->getName ()
659+ << " \" " ;
658660 if (State->MFI ->isSpillSlotObjectIndex (Slot))
659661 dbgs () << " spill" ;
660662 }
@@ -673,8 +675,7 @@ LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump(const StackColoring* State)
673675
674676#endif
675677
676- static inline int getStartOrEndSlot (const MachineInstr &MI)
677- {
678+ static inline int getStartOrEndSlot (const MachineInstr &MI) {
678679 assert ((MI.getOpcode () == TargetOpcode::LIFETIME_START ||
679680 MI.getOpcode () == TargetOpcode::LIFETIME_END) &&
680681 " Expected LIFETIME_START or LIFETIME_END op" );
@@ -715,7 +716,7 @@ bool StackColoring::isLifetimeStartOrEnd(const MachineInstr &MI,
715716 if (!MO.isFI ())
716717 continue ;
717718 int Slot = MO.getIndex ();
718- if (Slot< 0 )
719+ if (Slot < 0 )
719720 continue ;
720721 if (InterestingSlots.test (Slot) && applyFirstUse (Slot)) {
721722 slots.push_back (Slot);
@@ -802,7 +803,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
802803 int Slot = MO.getIndex ();
803804 if (Slot < 0 )
804805 continue ;
805- if (! BetweenStartEnd.test (Slot)) {
806+ if (!BetweenStartEnd.test (Slot)) {
806807 ConservativeSlots.set (Slot);
807808 }
808809 }
@@ -954,7 +955,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
954955
955956 DefinitelyInUse.resize (NumSlots);
956957 struct SplitSlotChanges {
957- const MachineInstr* AtMI;
958+ const MachineInstr * AtMI;
958959 unsigned BlockIdx : 31 ;
959960 unsigned IsStart : 1 ;
960961 unsigned Slot;
@@ -995,7 +996,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
995996 BitVector IsStoredTo;
996997 IsStoredTo.resize (NumSlots, false );
997998 struct MIBlockIdx {
998- const MachineInstr* MI;
999+ const MachineInstr * MI;
9991000 unsigned BlockIdx;
10001001 };
10011002 unsigned BlockIdx = 0 ;
@@ -1004,7 +1005,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
10041005 for (const MachineInstr &MI : MBB) {
10051006 if (MI.isDebugInstr ())
10061007 continue ;
1007- for (MachineMemOperand* MMO : MI.memoperands ()) {
1008+ for (MachineMemOperand * MMO : MI.memoperands ()) {
10081009 auto *PSV = dyn_cast_if_present<FixedStackPseudoSourceValue>(
10091010 MMO->getPseudoValue ());
10101011 if (!PSV)
@@ -1207,10 +1208,10 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
12071208 }
12081209
12091210 // Keep a list of *allocas* which need to be remapped.
1210- DenseMap<const AllocaInst*, const AllocaInst*> Allocas;
1211+ DenseMap<const AllocaInst *, const AllocaInst *> Allocas;
12111212
12121213 // Keep a list of allocas which has been affected by the remap.
1213- SmallPtrSet<const AllocaInst*, 32 > MergedAllocas;
1214+ SmallPtrSet<const AllocaInst *, 32 > MergedAllocas;
12141215
12151216 for (const std::pair<int , int > &SI : SlotRemap) {
12161217 const AllocaInst *From = MFI->getObjectAllocation (SI.first );
@@ -1244,8 +1245,8 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
12441245 // Transfer the stack protector layout tag, but make sure that SSPLK_AddrOf
12451246 // does not overwrite SSPLK_SmallArray or SSPLK_LargeArray, and make sure
12461247 // that SSPLK_SmallArray does not overwrite SSPLK_LargeArray.
1247- MachineFrameInfo::SSPLayoutKind FromKind
1248- = MFI->getObjectSSPLayout (SI.first );
1248+ MachineFrameInfo::SSPLayoutKind FromKind =
1249+ MFI->getObjectSSPLayout (SI.first );
12491250 MachineFrameInfo::SSPLayoutKind ToKind = MFI->getObjectSSPLayout (SI.second );
12501251 if (FromKind != MachineFrameInfo::SSPLK_None &&
12511252 (ToKind == MachineFrameInfo::SSPLK_None ||
@@ -1303,20 +1304,20 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
13031304 int FromSlot = MO.getIndex ();
13041305
13051306 // Don't touch arguments.
1306- if (FromSlot< 0 )
1307+ if (FromSlot < 0 )
13071308 continue ;
13081309
13091310 // Only look at mapped slots.
13101311 if (!SlotRemap.count (FromSlot))
13111312 continue ;
13121313
1313- // In a debug build, check that the instruction that we are modifying is
1314- // inside the expected live range. If the instruction is not inside
1315- // the calculated range then it means that the alloca usage moved
1316- // outside of the lifetime markers, or that the user has a bug.
1317- // NOTE: Alloca address calculations which happen outside the lifetime
1318- // zone are okay, despite the fact that we don't have a good way
1319- // for validating all of the usages of the calculation.
1314+ // In a debug build, check that the instruction that we are modifying is
1315+ // inside the expected live range. If the instruction is not inside
1316+ // the calculated range then it means that the alloca usage moved
1317+ // outside of the lifetime markers, or that the user has a bug.
1318+ // NOTE: Alloca address calculations which happen outside the lifetime
1319+ // zone are okay, despite the fact that we don't have a good way
1320+ // for validating all of the usages of the calculation.
13201321#ifndef NDEBUG
13211322 bool TouchesMemory = I.mayLoadOrStore ();
13221323 // If we *don't* protect the user from escaped allocas, don't bother
@@ -1395,7 +1396,7 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
13951396 Ref->setValue (NewSV);
13961397 }
13971398
1398- // Update the location of C++ catch objects for the MSVC personality routine.
1399+ // Update the location of C++ catch objects for the MSVC personality routine.
13991400 if (WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo ())
14001401 for (WinEHTryBlockMapEntry &TBME : EHInfo->TryBlockMap )
14011402 for (WinEHHandlerType &H : TBME.HandlerArray )
@@ -1407,9 +1408,9 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
14071408 LLVM_DEBUG (dbgs () << " Fixed " << FixedMemOp << " machine memory operands.\n " );
14081409 LLVM_DEBUG (dbgs () << " Fixed " << FixedDbg << " debug locations.\n " );
14091410 LLVM_DEBUG (dbgs () << " Fixed " << FixedInstr << " machine instructions.\n " );
1410- (void ) FixedMemOp;
1411- (void ) FixedDbg;
1412- (void ) FixedInstr;
1411+ (void )FixedMemOp;
1412+ (void )FixedDbg;
1413+ (void )FixedInstr;
14131414}
14141415
14151416void StackColoring::removeInvalidSlotRanges () {
@@ -1435,7 +1436,7 @@ void StackColoring::removeInvalidSlotRanges() {
14351436
14361437 int Slot = MO.getIndex ();
14371438
1438- if (Slot< 0 )
1439+ if (Slot < 0 )
14391440 continue ;
14401441
14411442 if (Intervals[Slot]->empty ())
@@ -1457,7 +1458,7 @@ void StackColoring::removeInvalidSlotRanges() {
14571458void StackColoring::expungeSlotMap (DenseMap<int , int > &SlotRemap,
14581459 unsigned NumSlots) {
14591460 // Expunge slot remap map.
1460- for (unsigned i= 0 ; i < NumSlots; ++i) {
1461+ for (unsigned i = 0 ; i < NumSlots; ++i) {
14611462 // If we are remapping i
14621463 if (auto It = SlotRemap.find (i); It != SlotRemap.end ()) {
14631464 int Target = It->second ;
@@ -1477,8 +1478,9 @@ bool StackColoringLegacy::runOnMachineFunction(MachineFunction &MF) {
14771478 if (skipFunction (MF.getFunction ()))
14781479 return false ;
14791480
1480- LiveStacks* LS = nullptr ;
1481- LiveStacksWrapperLegacy* LSWL = getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
1481+ LiveStacks *LS = nullptr ;
1482+ LiveStacksWrapperLegacy *LSWL =
1483+ getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
14821484 if (LSWL)
14831485 LS = &LSWL->getLS ();
14841486
@@ -1502,7 +1504,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15021504 int64_t OrigOptSize = 0 ;
15031505 int64_t OrigPesSize = 0 ;
15041506 for (unsigned Slot = 0 ; Slot < NumSlots; Slot++) {
1505- SlotInfo& Info = Slot2Info[Slot];
1507+ SlotInfo & Info = Slot2Info[Slot];
15061508 if (Info.StartLiveness .empty ())
15071509 assert (!LS || !LS->hasInterval (Slot));
15081510 if (!Info.StartLiveness .empty () &&
@@ -1553,8 +1555,8 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15531555 });
15541556 }
15551557
1556- SlotInfo* LastQueryLhs = nullptr ;
1557- SlotInfo* LastQueryRhs = nullptr ;
1558+ SlotInfo * LastQueryLhs = nullptr ;
1559+ SlotInfo * LastQueryRhs = nullptr ;
15581560 bool LastQueryRes = false ;
15591561 // Maybe there should be real caching here
15601562 auto HasOverlapCached = [&](SlotInfo &Lhs, SlotInfo &Rhs) {
@@ -1582,7 +1584,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15821584 LatestStatus.resize (LivenessSize, Status{});
15831585 SmallVector<Status> OlderStatus;
15841586
1585- auto FindStatus = [&](SlotInfo &Info, unsigned Pt) -> Status& {
1587+ auto FindStatus = [&](SlotInfo &Info, unsigned Pt) -> Status & {
15861588 Status *Last = &LatestStatus[Pt];
15871589
15881590 // The slots in the linked-list are always kept in ascending order, so the
@@ -1597,7 +1599,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15971599 return *Last;
15981600 };
15991601 auto UpdateStatus = [&](SlotInfo &Info, unsigned Pt, unsigned Offset) {
1600- Status* Last = &LatestStatus[Pt];
1602+ Status * Last = &LatestStatus[Pt];
16011603 unsigned Idx = OlderStatus.size ();
16021604 OlderStatus.push_back (*Last);
16031605
@@ -1612,7 +1614,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
16121614 }
16131615
16141616 // Ensure ordering of slots
1615- Status* Inserted = &OlderStatus.back ();
1617+ Status * Inserted = &OlderStatus.back ();
16161618 Inserted->Offset = Offset;
16171619 Inserted->Slot = &Info - Slot2Info.data ();
16181620 Status *Curr = Last;
@@ -1719,7 +1721,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
17191721 }
17201722
17211723 unsigned FinalSize = 0 ;
1722- for (Status& U : LatestStatus)
1724+ for (Status & U : LatestStatus)
17231725 FinalSize = std::max (FinalSize, U.Offset );
17241726 LLVM_DEBUG (dbgs () << " MergedSize=" << FinalSize << " OrigPesSize="
17251727 << OrigPesSize << " OrigOptSize" << OrigOptSize << " \n " );
@@ -1778,7 +1780,7 @@ bool StackColoring::run(MachineFunction &Func) {
17781780 LLVM_DEBUG (dbgs () << " Found " << NumMarkers << " markers and " << NumSlots
17791781 << " slots\n " );
17801782
1781- for (int i= 0 ; i < MFI->getObjectIndexEnd (); ++i)
1783+ for (int i = 0 ; i < MFI->getObjectIndexEnd (); ++i)
17821784 TotalSize += MFI->getObjectSize (i);
17831785
17841786 LLVM_DEBUG (dbgs () << " Total Stack size: " << TotalSize << " bytes\n\n " );
@@ -1791,7 +1793,7 @@ bool StackColoring::run(MachineFunction &Func) {
17911793 }
17921794
17931795 Slot2Info.resize (NumSlots);
1794- for (unsigned i= 0 ; i < NumSlots; ++i) {
1796+ for (unsigned i = 0 ; i < NumSlots; ++i) {
17951797 std::unique_ptr<LiveRange> LI (new LiveRange ());
17961798 LI->getNextValue (Indexes->getZeroIndex (), VNInfoAllocator);
17971799 Intervals.push_back (std::move (LI));
0 commit comments