49
49
#include " llvm/Support/CommandLine.h"
50
50
#include " llvm/Support/Compiler.h"
51
51
#include " llvm/Support/Debug.h"
52
- #include " llvm/Support/raw_ostream.h"
53
52
#include " llvm/Support/DebugCounter.h"
53
+ #include " llvm/Support/raw_ostream.h"
54
54
#include < algorithm>
55
55
#include < cassert>
56
56
#include < cmath>
@@ -65,42 +65,41 @@ using namespace llvm;
65
65
DEBUG_COUNTER (ProcessSlot, DEBUG_TYPE " -slot" ,
66
66
" Controls which slot get processed" );
67
67
68
- static cl::opt<bool >
69
- DisableColoring (" no-stack-coloring" ,
70
- cl::init (false ), cl::Hidden,
71
- cl::desc(" Disable stack coloring" ));
68
+ static cl::opt<bool > DisableColoring (" no-stack-coloring" , cl::init(false ),
69
+ cl::Hidden,
70
+ cl::desc(" Disable stack coloring" ));
72
71
73
72
// / The user may write code that uses allocas outside of the declared lifetime
74
73
// / zone. This can happen when the user returns a reference to a local
75
74
// / data-structure. We can detect these cases and decide not to optimize the
76
75
// / code. If this flag is enabled, we try to save the user. This option
77
76
// / is treated as overriding LifetimeStartOnFirstUse below.
78
77
static cl::opt<bool >
79
- ProtectFromEscapedAllocas (" protect-from-escaped-allocas" ,
80
- cl::init ( false ), cl::Hidden,
81
- cl::desc(" Do not optimize lifetime zones that "
82
- " are broken" ));
78
+ ProtectFromEscapedAllocas (" protect-from-escaped-allocas" , cl::init( false ) ,
79
+ cl::Hidden,
80
+ cl::desc(" Do not optimize lifetime zones that "
81
+ " are broken" ));
83
82
84
83
// / Enable enhanced dataflow scheme for lifetime analysis (treat first
85
84
// / use of stack slot as start of slot lifetime, as opposed to looking
86
85
// / for LIFETIME_START marker). See "Implementation notes" below for
87
86
// / more info.
88
87
static cl::opt<bool >
89
- LifetimeStartOnFirstUse (" stackcoloring-lifetime-start-on-first-use" ,
90
- cl::init (true ), cl::Hidden,
91
- cl::desc(" Treat stack lifetimes as starting on first use, not on START marker." ));
88
+ LifetimeStartOnFirstUse (" stackcoloring-lifetime-start-on-first-use" ,
89
+ cl::init (true ), cl::Hidden,
90
+ cl::desc(" Treat stack lifetimes as starting on "
91
+ " first use, not on START marker." ));
92
92
93
93
static cl::opt<bool > UseNewStackColoring (
94
94
" new-stack-coloring" , cl::init(false ), cl::Hidden,
95
95
cl::desc(" Use a better logic to try to reduce stack usage" ));
96
96
97
97
static cl::opt<unsigned > MaxCandidatesOpt (
98
- " stackcoloring-max-candidates" , cl::init(0 ),
99
- cl::Hidden,
98
+ " stackcoloring-max-candidates" , cl::init(0 ), cl::Hidden,
100
99
cl::desc(
101
100
" Max number of candidates that will be evaluated, 0 means no limit" ));
102
101
103
- STATISTIC (NumMarkerSeen, " Number of lifetime markers found." );
102
+ STATISTIC (NumMarkerSeen, " Number of lifetime markers found." );
104
103
STATISTIC (GeneratedWorse, " Number of times worse layout were generated" );
105
104
STATISTIC (StackSpaceSaved, " Number of bytes saved due to merging slots." );
106
105
STATISTIC (StackSlotMerged, " Number of stack slot merged." );
@@ -393,7 +392,7 @@ class StackColoring {
393
392
MachineFrameInfo *MFI = nullptr ;
394
393
MachineFunction *MF = nullptr ;
395
394
396
- LiveStacks* LS = nullptr ;
395
+ LiveStacks * LS = nullptr ;
397
396
398
397
struct SlotInfo {
399
398
// All places in the current function where this Slot is live
@@ -421,7 +420,7 @@ class StackColoring {
421
420
[&](int Idx) { return Liveness[Idx]; });
422
421
}
423
422
424
- LLVM_DUMP_METHOD void dump (const StackColoring* State = nullptr ) const ;
423
+ LLVM_DUMP_METHOD void dump (const StackColoring * State = nullptr ) const ;
425
424
};
426
425
427
426
// / A class representing liveness information for a single basic block.
@@ -466,7 +465,7 @@ class StackColoring {
466
465
467
466
// / The list of lifetime markers found. These markers are to be removed
468
467
// / once the coloring is done.
469
- SmallVector<MachineInstr*, 8 > Markers;
468
+ SmallVector<MachineInstr *, 8 > Markers;
470
469
471
470
// / Record the FI slots for which we have seen some sort of
472
471
// / lifetime marker (either start or end).
@@ -480,7 +479,8 @@ class StackColoring {
480
479
unsigned NumIterations;
481
480
482
481
public:
483
- StackColoring (SlotIndexes *Indexes, LiveStacks* LS) : LS(LS), Indexes(Indexes) {}
482
+ StackColoring (SlotIndexes *Indexes, LiveStacks *LS)
483
+ : LS(LS), Indexes(Indexes) {}
484
484
bool run (MachineFunction &Func);
485
485
486
486
private:
@@ -507,7 +507,8 @@ class StackColoring {
507
507
unsigned doMerging (unsigned NumSlots);
508
508
509
509
// / Returns TRUE if we're using the first-use-begins-lifetime method for
510
- // / this slot (if FALSE, then the start marker is treated as start of lifetime).
510
+ // / this slot (if FALSE, then the start marker is treated as start of
511
+ // / lifetime).
511
512
bool applyFirstUse (int Slot) {
512
513
if (!LifetimeStartOnFirstUse || ProtectFromEscapedAllocas)
513
514
return false ;
@@ -521,8 +522,7 @@ class StackColoring {
521
522
// / starting or ending are added to the vector "slots" and "isStart" is set
522
523
// / accordingly.
523
524
// / \returns True if inst contains a lifetime start or end
524
- bool isLifetimeStartOrEnd (const MachineInstr &MI,
525
- SmallVector<int , 4 > &slots,
525
+ bool isLifetimeStartOrEnd (const MachineInstr &MI, SmallVector<int , 4 > &slots,
526
526
bool &isStart);
527
527
528
528
// / Construct the LiveIntervals for the slots.
@@ -624,8 +624,8 @@ LLVM_DUMP_METHOD void StackColoring::dumpBB(MachineBasicBlock *MBB) const {
624
624
625
625
LLVM_DUMP_METHOD void StackColoring::dump () const {
626
626
for (MachineBasicBlock *MBB : depth_first (MF)) {
627
- dbgs () << " Inspecting block #" << MBB->getNumber () << " ["
628
- << MBB-> getName () << " ]\n " ;
627
+ dbgs () << " Inspecting block #" << MBB->getNumber () << " [" << MBB-> getName ()
628
+ << " ]\n " ;
629
629
dumpBB (MBB);
630
630
}
631
631
}
@@ -644,7 +644,8 @@ LLVM_DUMP_METHOD void StackColoring::dumpIntervals() const {
644
644
}
645
645
}
646
646
647
- LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump (const StackColoring* State) const {
647
+ LLVM_DUMP_METHOD void
648
+ StackColoring::SlotInfo::dump (const StackColoring *State) const {
648
649
unsigned Slot = InvalidIdx;
649
650
if (State) {
650
651
Slot = this - State->Slot2Info .data ();
@@ -656,7 +657,8 @@ LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump(const StackColoring* State)
656
657
dbgs () << " offset=" << Offset;
657
658
if (State) {
658
659
if (State->MFI ->getObjectAllocation (Slot))
659
- dbgs () << " \" " << State->MFI ->getObjectAllocation (Slot)->getName () << " \" " ;
660
+ dbgs () << " \" " << State->MFI ->getObjectAllocation (Slot)->getName ()
661
+ << " \" " ;
660
662
if (State->MFI ->isSpillSlotObjectIndex (Slot))
661
663
dbgs () << " spill" ;
662
664
}
@@ -675,8 +677,7 @@ LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump(const StackColoring* State)
675
677
676
678
#endif
677
679
678
- static inline int getStartOrEndSlot (const MachineInstr &MI)
679
- {
680
+ static inline int getStartOrEndSlot (const MachineInstr &MI) {
680
681
assert ((MI.getOpcode () == TargetOpcode::LIFETIME_START ||
681
682
MI.getOpcode () == TargetOpcode::LIFETIME_END) &&
682
683
" Expected LIFETIME_START or LIFETIME_END op" );
@@ -717,7 +718,7 @@ bool StackColoring::isLifetimeStartOrEnd(const MachineInstr &MI,
717
718
if (!MO.isFI ())
718
719
continue ;
719
720
int Slot = MO.getIndex ();
720
- if (Slot< 0 )
721
+ if (Slot < 0 )
721
722
continue ;
722
723
if (InterestingSlots.test (Slot) && applyFirstUse (Slot)) {
723
724
slots.push_back (Slot);
@@ -804,7 +805,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
804
805
int Slot = MO.getIndex ();
805
806
if (Slot < 0 )
806
807
continue ;
807
- if (! BetweenStartEnd.test (Slot)) {
808
+ if (!BetweenStartEnd.test (Slot)) {
808
809
ConservativeSlots.set (Slot);
809
810
}
810
811
}
@@ -956,7 +957,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
956
957
957
958
DefinitelyInUse.resize (NumSlots);
958
959
struct SplitSlotChanges {
959
- const MachineInstr* AtMI;
960
+ const MachineInstr * AtMI;
960
961
unsigned BlockIdx : 31 ;
961
962
unsigned IsStart : 1 ;
962
963
unsigned Slot;
@@ -997,7 +998,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
997
998
BitVector IsStoredTo;
998
999
IsStoredTo.resize (NumSlots, false );
999
1000
struct MIBlockIdx {
1000
- const MachineInstr* MI;
1001
+ const MachineInstr * MI;
1001
1002
unsigned BlockIdx;
1002
1003
};
1003
1004
unsigned BlockIdx = 0 ;
@@ -1006,7 +1007,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
1006
1007
for (const MachineInstr &MI : MBB) {
1007
1008
if (MI.isDebugInstr ())
1008
1009
continue ;
1009
- for (MachineMemOperand* MMO : MI.memoperands ()) {
1010
+ for (MachineMemOperand * MMO : MI.memoperands ()) {
1010
1011
auto *PSV = dyn_cast_if_present<FixedStackPseudoSourceValue>(
1011
1012
MMO->getPseudoValue ());
1012
1013
if (!PSV)
@@ -1209,10 +1210,10 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
1209
1210
}
1210
1211
1211
1212
// Keep a list of *allocas* which need to be remapped.
1212
- DenseMap<const AllocaInst*, const AllocaInst*> Allocas;
1213
+ DenseMap<const AllocaInst *, const AllocaInst *> Allocas;
1213
1214
1214
1215
// Keep a list of allocas which has been affected by the remap.
1215
- SmallPtrSet<const AllocaInst*, 32 > MergedAllocas;
1216
+ SmallPtrSet<const AllocaInst *, 32 > MergedAllocas;
1216
1217
1217
1218
for (const std::pair<int , int > &SI : SlotRemap) {
1218
1219
const AllocaInst *From = MFI->getObjectAllocation (SI.first );
@@ -1246,8 +1247,8 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
1246
1247
// Transfer the stack protector layout tag, but make sure that SSPLK_AddrOf
1247
1248
// does not overwrite SSPLK_SmallArray or SSPLK_LargeArray, and make sure
1248
1249
// that SSPLK_SmallArray does not overwrite SSPLK_LargeArray.
1249
- MachineFrameInfo::SSPLayoutKind FromKind
1250
- = MFI->getObjectSSPLayout (SI.first );
1250
+ MachineFrameInfo::SSPLayoutKind FromKind =
1251
+ MFI->getObjectSSPLayout (SI.first );
1251
1252
MachineFrameInfo::SSPLayoutKind ToKind = MFI->getObjectSSPLayout (SI.second );
1252
1253
if (FromKind != MachineFrameInfo::SSPLK_None &&
1253
1254
(ToKind == MachineFrameInfo::SSPLK_None ||
@@ -1305,20 +1306,20 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
1305
1306
int FromSlot = MO.getIndex ();
1306
1307
1307
1308
// Don't touch arguments.
1308
- if (FromSlot< 0 )
1309
+ if (FromSlot < 0 )
1309
1310
continue ;
1310
1311
1311
1312
// Only look at mapped slots.
1312
1313
if (!SlotRemap.count (FromSlot))
1313
1314
continue ;
1314
1315
1315
- // In a debug build, check that the instruction that we are modifying is
1316
- // inside the expected live range. If the instruction is not inside
1317
- // the calculated range then it means that the alloca usage moved
1318
- // outside of the lifetime markers, or that the user has a bug.
1319
- // NOTE: Alloca address calculations which happen outside the lifetime
1320
- // zone are okay, despite the fact that we don't have a good way
1321
- // for validating all of the usages of the calculation.
1316
+ // In a debug build, check that the instruction that we are modifying is
1317
+ // inside the expected live range. If the instruction is not inside
1318
+ // the calculated range then it means that the alloca usage moved
1319
+ // outside of the lifetime markers, or that the user has a bug.
1320
+ // NOTE: Alloca address calculations which happen outside the lifetime
1321
+ // zone are okay, despite the fact that we don't have a good way
1322
+ // for validating all of the usages of the calculation.
1322
1323
#ifndef NDEBUG
1323
1324
bool TouchesMemory = I.mayLoadOrStore ();
1324
1325
// If we *don't* protect the user from escaped allocas, don't bother
@@ -1397,7 +1398,7 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
1397
1398
Ref->setValue (NewSV);
1398
1399
}
1399
1400
1400
- // Update the location of C++ catch objects for the MSVC personality routine.
1401
+ // Update the location of C++ catch objects for the MSVC personality routine.
1401
1402
if (WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo ())
1402
1403
for (WinEHTryBlockMapEntry &TBME : EHInfo->TryBlockMap )
1403
1404
for (WinEHHandlerType &H : TBME.HandlerArray )
@@ -1409,9 +1410,9 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
1409
1410
LLVM_DEBUG (dbgs () << " Fixed " << FixedMemOp << " machine memory operands.\n " );
1410
1411
LLVM_DEBUG (dbgs () << " Fixed " << FixedDbg << " debug locations.\n " );
1411
1412
LLVM_DEBUG (dbgs () << " Fixed " << FixedInstr << " machine instructions.\n " );
1412
- (void ) FixedMemOp;
1413
- (void ) FixedDbg;
1414
- (void ) FixedInstr;
1413
+ (void )FixedMemOp;
1414
+ (void )FixedDbg;
1415
+ (void )FixedInstr;
1415
1416
}
1416
1417
1417
1418
void StackColoring::removeInvalidSlotRanges () {
@@ -1437,7 +1438,7 @@ void StackColoring::removeInvalidSlotRanges() {
1437
1438
1438
1439
int Slot = MO.getIndex ();
1439
1440
1440
- if (Slot< 0 )
1441
+ if (Slot < 0 )
1441
1442
continue ;
1442
1443
1443
1444
if (Intervals[Slot]->empty ())
@@ -1459,7 +1460,7 @@ void StackColoring::removeInvalidSlotRanges() {
1459
1460
void StackColoring::expungeSlotMap (DenseMap<int , int > &SlotRemap,
1460
1461
unsigned NumSlots) {
1461
1462
// Expunge slot remap map.
1462
- for (unsigned i= 0 ; i < NumSlots; ++i) {
1463
+ for (unsigned i = 0 ; i < NumSlots; ++i) {
1463
1464
// If we are remapping i
1464
1465
if (auto It = SlotRemap.find (i); It != SlotRemap.end ()) {
1465
1466
int Target = It->second ;
@@ -1479,8 +1480,9 @@ bool StackColoringLegacy::runOnMachineFunction(MachineFunction &MF) {
1479
1480
if (skipFunction (MF.getFunction ()))
1480
1481
return false ;
1481
1482
1482
- LiveStacks* LS = nullptr ;
1483
- LiveStacksWrapperLegacy* LSWL = getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
1483
+ LiveStacks *LS = nullptr ;
1484
+ LiveStacksWrapperLegacy *LSWL =
1485
+ getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
1484
1486
if (LSWL)
1485
1487
LS = &LSWL->getLS ();
1486
1488
@@ -1504,7 +1506,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
1504
1506
int64_t OrigOptSize = 0 ;
1505
1507
int64_t OrigPesSize = 0 ;
1506
1508
for (unsigned Slot = 0 ; Slot < NumSlots; Slot++) {
1507
- SlotInfo& Info = Slot2Info[Slot];
1509
+ SlotInfo & Info = Slot2Info[Slot];
1508
1510
if (Info.StartLiveness .empty ())
1509
1511
assert (!LS || !LS->hasInterval (Slot));
1510
1512
if (!Info.StartLiveness .empty () &&
@@ -1555,8 +1557,8 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
1555
1557
});
1556
1558
}
1557
1559
1558
- SlotInfo* LastQueryLhs = nullptr ;
1559
- SlotInfo* LastQueryRhs = nullptr ;
1560
+ SlotInfo * LastQueryLhs = nullptr ;
1561
+ SlotInfo * LastQueryRhs = nullptr ;
1560
1562
bool LastQueryRes = false ;
1561
1563
// Maybe there should be real caching here
1562
1564
auto HasOverlapCached = [&](SlotInfo &Lhs, SlotInfo &Rhs) {
@@ -1584,7 +1586,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
1584
1586
LatestStatus.resize (LivenessSize, Status{});
1585
1587
SmallVector<Status> OlderStatus;
1586
1588
1587
- auto FindStatus = [&](SlotInfo &Info, unsigned Pt) -> Status& {
1589
+ auto FindStatus = [&](SlotInfo &Info, unsigned Pt) -> Status & {
1588
1590
Status *Last = &LatestStatus[Pt];
1589
1591
1590
1592
// The slots in the linked-list are always kept in ascending order, so the
@@ -1599,7 +1601,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
1599
1601
return *Last;
1600
1602
};
1601
1603
auto UpdateStatus = [&](SlotInfo &Info, unsigned Pt, unsigned Offset) {
1602
- Status* Last = &LatestStatus[Pt];
1604
+ Status * Last = &LatestStatus[Pt];
1603
1605
unsigned Idx = OlderStatus.size ();
1604
1606
OlderStatus.push_back (*Last);
1605
1607
@@ -1614,7 +1616,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
1614
1616
}
1615
1617
1616
1618
// Ensure ordering of slots
1617
- Status* Inserted = &OlderStatus.back ();
1619
+ Status * Inserted = &OlderStatus.back ();
1618
1620
Inserted->Offset = Offset;
1619
1621
Inserted->Slot = &Info - Slot2Info.data ();
1620
1622
Status *Curr = Last;
@@ -1721,7 +1723,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
1721
1723
}
1722
1724
1723
1725
unsigned FinalSize = 0 ;
1724
- for (Status& U : LatestStatus)
1726
+ for (Status & U : LatestStatus)
1725
1727
FinalSize = std::max (FinalSize, U.Offset );
1726
1728
LLVM_DEBUG (dbgs () << " MergedSize=" << FinalSize << " OrigPesSize="
1727
1729
<< OrigPesSize << " OrigOptSize" << OrigOptSize << " \n " );
@@ -1780,7 +1782,7 @@ bool StackColoring::run(MachineFunction &Func) {
1780
1782
LLVM_DEBUG (dbgs () << " Found " << NumMarkers << " markers and " << NumSlots
1781
1783
<< " slots\n " );
1782
1784
1783
- for (int i= 0 ; i < MFI->getObjectIndexEnd (); ++i)
1785
+ for (int i = 0 ; i < MFI->getObjectIndexEnd (); ++i)
1784
1786
TotalSize += MFI->getObjectSize (i);
1785
1787
1786
1788
LLVM_DEBUG (dbgs () << " Total Stack size: " << TotalSize << " bytes\n\n " );
@@ -1793,7 +1795,7 @@ bool StackColoring::run(MachineFunction &Func) {
1793
1795
}
1794
1796
1795
1797
Slot2Info.resize (NumSlots);
1796
- for (unsigned i= 0 ; i < NumSlots; ++i) {
1798
+ for (unsigned i = 0 ; i < NumSlots; ++i) {
1797
1799
std::unique_ptr<LiveRange> LI (new LiveRange ());
1798
1800
LI->getNextValue (Indexes->getZeroIndex (), VNInfoAllocator);
1799
1801
Intervals.push_back (std::move (LI));
0 commit comments