Skip to content

Commit 595d5af

Browse files
committed
format StackColoring.cpp
1 parent 6e29a87 commit 595d5af

File tree

1 file changed

+64
-62
lines changed

1 file changed

+64
-62
lines changed

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
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 <cmath>
@@ -65,42 +65,41 @@ using namespace llvm;
6565
DEBUG_COUNTER(ProcessSlot, DEBUG_TYPE "-slot",
6666
"Controls which slot get processed");
6767

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"));
7271

7372
/// The user may write code that uses allocas outside of the declared lifetime
7473
/// zone. This can happen when the user returns a reference to a local
7574
/// data-structure. We can detect these cases and decide not to optimize the
7675
/// code. If this flag is enabled, we try to save the user. This option
7776
/// is treated as overriding LifetimeStartOnFirstUse below.
7877
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"));
8382

8483
/// Enable enhanced dataflow scheme for lifetime analysis (treat first
8584
/// use of stack slot as start of slot lifetime, as opposed to looking
8685
/// for LIFETIME_START marker). See "Implementation notes" below for
8786
/// more info.
8887
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."));
9292

9393
static cl::opt<bool> UseNewStackColoring(
9494
"new-stack-coloring", cl::init(false), cl::Hidden,
9595
cl::desc("Use a better logic to try to reduce stack usage"));
9696

9797
static cl::opt<unsigned> MaxCandidatesOpt(
98-
"stackcoloring-max-candidates", cl::init(0),
99-
cl::Hidden,
98+
"stackcoloring-max-candidates", cl::init(0), cl::Hidden,
10099
cl::desc(
101100
"Max number of candidates that will be evaluated, 0 means no limit"));
102101

103-
STATISTIC(NumMarkerSeen, "Number of lifetime markers found.");
102+
STATISTIC(NumMarkerSeen, "Number of lifetime markers found.");
104103
STATISTIC(GeneratedWorse, "Number of times worse layout were generated");
105104
STATISTIC(StackSpaceSaved, "Number of bytes saved due to merging slots.");
106105
STATISTIC(StackSlotMerged, "Number of stack slot merged.");
@@ -393,7 +392,7 @@ class StackColoring {
393392
MachineFrameInfo *MFI = nullptr;
394393
MachineFunction *MF = nullptr;
395394

396-
LiveStacks* LS = nullptr;
395+
LiveStacks *LS = nullptr;
397396

398397
struct SlotInfo {
399398
// All places in the current function where this Slot is live
@@ -421,7 +420,7 @@ class StackColoring {
421420
[&](int Idx) { return Liveness[Idx]; });
422421
}
423422

424-
LLVM_DUMP_METHOD void dump(const StackColoring* State = nullptr) const;
423+
LLVM_DUMP_METHOD void dump(const StackColoring *State = nullptr) const;
425424
};
426425

427426
/// A class representing liveness information for a single basic block.
@@ -466,7 +465,7 @@ class StackColoring {
466465

467466
/// The list of lifetime markers found. These markers are to be removed
468467
/// once the coloring is done.
469-
SmallVector<MachineInstr*, 8> Markers;
468+
SmallVector<MachineInstr *, 8> Markers;
470469

471470
/// Record the FI slots for which we have seen some sort of
472471
/// lifetime marker (either start or end).
@@ -480,7 +479,8 @@ class StackColoring {
480479
unsigned NumIterations;
481480

482481
public:
483-
StackColoring(SlotIndexes *Indexes, LiveStacks* LS) : LS(LS), Indexes(Indexes) {}
482+
StackColoring(SlotIndexes *Indexes, LiveStacks *LS)
483+
: LS(LS), Indexes(Indexes) {}
484484
bool run(MachineFunction &Func);
485485

486486
private:
@@ -507,7 +507,8 @@ class StackColoring {
507507
unsigned doMerging(unsigned NumSlots);
508508

509509
/// 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).
511512
bool applyFirstUse(int Slot) {
512513
if (!LifetimeStartOnFirstUse || ProtectFromEscapedAllocas)
513514
return false;
@@ -521,8 +522,7 @@ class StackColoring {
521522
/// starting or ending are added to the vector "slots" and "isStart" is set
522523
/// accordingly.
523524
/// \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,
526526
bool &isStart);
527527

528528
/// Construct the LiveIntervals for the slots.
@@ -624,8 +624,8 @@ LLVM_DUMP_METHOD void StackColoring::dumpBB(MachineBasicBlock *MBB) const {
624624

625625
LLVM_DUMP_METHOD void StackColoring::dump() const {
626626
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";
629629
dumpBB(MBB);
630630
}
631631
}
@@ -644,7 +644,8 @@ LLVM_DUMP_METHOD void StackColoring::dumpIntervals() const {
644644
}
645645
}
646646

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 {
648649
unsigned Slot = InvalidIdx;
649650
if (State) {
650651
Slot = this - State->Slot2Info.data();
@@ -656,7 +657,8 @@ LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump(const StackColoring* State)
656657
dbgs() << " offset=" << Offset;
657658
if (State) {
658659
if (State->MFI->getObjectAllocation(Slot))
659-
dbgs() << " \"" << State->MFI->getObjectAllocation(Slot)->getName() << "\"";
660+
dbgs() << " \"" << State->MFI->getObjectAllocation(Slot)->getName()
661+
<< "\"";
660662
if (State->MFI->isSpillSlotObjectIndex(Slot))
661663
dbgs() << " spill";
662664
}
@@ -675,8 +677,7 @@ LLVM_DUMP_METHOD void StackColoring::SlotInfo::dump(const StackColoring* State)
675677

676678
#endif
677679

678-
static inline int getStartOrEndSlot(const MachineInstr &MI)
679-
{
680+
static inline int getStartOrEndSlot(const MachineInstr &MI) {
680681
assert((MI.getOpcode() == TargetOpcode::LIFETIME_START ||
681682
MI.getOpcode() == TargetOpcode::LIFETIME_END) &&
682683
"Expected LIFETIME_START or LIFETIME_END op");
@@ -717,7 +718,7 @@ bool StackColoring::isLifetimeStartOrEnd(const MachineInstr &MI,
717718
if (!MO.isFI())
718719
continue;
719720
int Slot = MO.getIndex();
720-
if (Slot<0)
721+
if (Slot < 0)
721722
continue;
722723
if (InterestingSlots.test(Slot) && applyFirstUse(Slot)) {
723724
slots.push_back(Slot);
@@ -804,7 +805,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
804805
int Slot = MO.getIndex();
805806
if (Slot < 0)
806807
continue;
807-
if (! BetweenStartEnd.test(Slot)) {
808+
if (!BetweenStartEnd.test(Slot)) {
808809
ConservativeSlots.set(Slot);
809810
}
810811
}
@@ -956,7 +957,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
956957

957958
DefinitelyInUse.resize(NumSlots);
958959
struct SplitSlotChanges {
959-
const MachineInstr* AtMI;
960+
const MachineInstr *AtMI;
960961
unsigned BlockIdx : 31;
961962
unsigned IsStart : 1;
962963
unsigned Slot;
@@ -997,7 +998,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
997998
BitVector IsStoredTo;
998999
IsStoredTo.resize(NumSlots, false);
9991000
struct MIBlockIdx {
1000-
const MachineInstr* MI;
1001+
const MachineInstr *MI;
10011002
unsigned BlockIdx;
10021003
};
10031004
unsigned BlockIdx = 0;
@@ -1006,7 +1007,7 @@ void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
10061007
for (const MachineInstr &MI : MBB) {
10071008
if (MI.isDebugInstr())
10081009
continue;
1009-
for (MachineMemOperand* MMO : MI.memoperands()) {
1010+
for (MachineMemOperand *MMO : MI.memoperands()) {
10101011
auto *PSV = dyn_cast_if_present<FixedStackPseudoSourceValue>(
10111012
MMO->getPseudoValue());
10121013
if (!PSV)
@@ -1209,10 +1210,10 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
12091210
}
12101211

12111212
// Keep a list of *allocas* which need to be remapped.
1212-
DenseMap<const AllocaInst*, const AllocaInst*> Allocas;
1213+
DenseMap<const AllocaInst *, const AllocaInst *> Allocas;
12131214

12141215
// Keep a list of allocas which has been affected by the remap.
1215-
SmallPtrSet<const AllocaInst*, 32> MergedAllocas;
1216+
SmallPtrSet<const AllocaInst *, 32> MergedAllocas;
12161217

12171218
for (const std::pair<int, int> &SI : SlotRemap) {
12181219
const AllocaInst *From = MFI->getObjectAllocation(SI.first);
@@ -1246,8 +1247,8 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
12461247
// Transfer the stack protector layout tag, but make sure that SSPLK_AddrOf
12471248
// does not overwrite SSPLK_SmallArray or SSPLK_LargeArray, and make sure
12481249
// 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);
12511252
MachineFrameInfo::SSPLayoutKind ToKind = MFI->getObjectSSPLayout(SI.second);
12521253
if (FromKind != MachineFrameInfo::SSPLK_None &&
12531254
(ToKind == MachineFrameInfo::SSPLK_None ||
@@ -1305,20 +1306,20 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
13051306
int FromSlot = MO.getIndex();
13061307

13071308
// Don't touch arguments.
1308-
if (FromSlot<0)
1309+
if (FromSlot < 0)
13091310
continue;
13101311

13111312
// Only look at mapped slots.
13121313
if (!SlotRemap.count(FromSlot))
13131314
continue;
13141315

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.
13221323
#ifndef NDEBUG
13231324
bool TouchesMemory = I.mayLoadOrStore();
13241325
// If we *don't* protect the user from escaped allocas, don't bother
@@ -1397,7 +1398,7 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
13971398
Ref->setValue(NewSV);
13981399
}
13991400

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.
14011402
if (WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo())
14021403
for (WinEHTryBlockMapEntry &TBME : EHInfo->TryBlockMap)
14031404
for (WinEHHandlerType &H : TBME.HandlerArray)
@@ -1409,9 +1410,9 @@ void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
14091410
LLVM_DEBUG(dbgs() << "Fixed " << FixedMemOp << " machine memory operands.\n");
14101411
LLVM_DEBUG(dbgs() << "Fixed " << FixedDbg << " debug locations.\n");
14111412
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;
14151416
}
14161417

14171418
void StackColoring::removeInvalidSlotRanges() {
@@ -1437,7 +1438,7 @@ void StackColoring::removeInvalidSlotRanges() {
14371438

14381439
int Slot = MO.getIndex();
14391440

1440-
if (Slot<0)
1441+
if (Slot < 0)
14411442
continue;
14421443

14431444
if (Intervals[Slot]->empty())
@@ -1459,7 +1460,7 @@ void StackColoring::removeInvalidSlotRanges() {
14591460
void StackColoring::expungeSlotMap(DenseMap<int, int> &SlotRemap,
14601461
unsigned NumSlots) {
14611462
// Expunge slot remap map.
1462-
for (unsigned i=0; i < NumSlots; ++i) {
1463+
for (unsigned i = 0; i < NumSlots; ++i) {
14631464
// If we are remapping i
14641465
if (auto It = SlotRemap.find(i); It != SlotRemap.end()) {
14651466
int Target = It->second;
@@ -1479,8 +1480,9 @@ bool StackColoringLegacy::runOnMachineFunction(MachineFunction &MF) {
14791480
if (skipFunction(MF.getFunction()))
14801481
return false;
14811482

1482-
LiveStacks* LS = nullptr;
1483-
LiveStacksWrapperLegacy* LSWL = getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
1483+
LiveStacks *LS = nullptr;
1484+
LiveStacksWrapperLegacy *LSWL =
1485+
getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
14841486
if (LSWL)
14851487
LS = &LSWL->getLS();
14861488

@@ -1504,7 +1506,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15041506
int64_t OrigOptSize = 0;
15051507
int64_t OrigPesSize = 0;
15061508
for (unsigned Slot = 0; Slot < NumSlots; Slot++) {
1507-
SlotInfo& Info = Slot2Info[Slot];
1509+
SlotInfo &Info = Slot2Info[Slot];
15081510
if (Info.StartLiveness.empty())
15091511
assert(!LS || !LS->hasInterval(Slot));
15101512
if (!Info.StartLiveness.empty() &&
@@ -1555,8 +1557,8 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15551557
});
15561558
}
15571559

1558-
SlotInfo* LastQueryLhs = nullptr;
1559-
SlotInfo* LastQueryRhs = nullptr;
1560+
SlotInfo *LastQueryLhs = nullptr;
1561+
SlotInfo *LastQueryRhs = nullptr;
15601562
bool LastQueryRes = false;
15611563
// Maybe there should be real caching here
15621564
auto HasOverlapCached = [&](SlotInfo &Lhs, SlotInfo &Rhs) {
@@ -1584,7 +1586,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15841586
LatestStatus.resize(LivenessSize, Status{});
15851587
SmallVector<Status> OlderStatus;
15861588

1587-
auto FindStatus = [&](SlotInfo &Info, unsigned Pt) -> Status& {
1589+
auto FindStatus = [&](SlotInfo &Info, unsigned Pt) -> Status & {
15881590
Status *Last = &LatestStatus[Pt];
15891591

15901592
// The slots in the linked-list are always kept in ascending order, so the
@@ -1599,7 +1601,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
15991601
return *Last;
16001602
};
16011603
auto UpdateStatus = [&](SlotInfo &Info, unsigned Pt, unsigned Offset) {
1602-
Status* Last = &LatestStatus[Pt];
1604+
Status *Last = &LatestStatus[Pt];
16031605
unsigned Idx = OlderStatus.size();
16041606
OlderStatus.push_back(*Last);
16051607

@@ -1614,7 +1616,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
16141616
}
16151617

16161618
// Ensure ordering of slots
1617-
Status* Inserted = &OlderStatus.back();
1619+
Status *Inserted = &OlderStatus.back();
16181620
Inserted->Offset = Offset;
16191621
Inserted->Slot = &Info - Slot2Info.data();
16201622
Status *Curr = Last;
@@ -1721,7 +1723,7 @@ unsigned StackColoring::doMerging(unsigned NumSlots) {
17211723
}
17221724

17231725
unsigned FinalSize = 0;
1724-
for (Status& U : LatestStatus)
1726+
for (Status &U : LatestStatus)
17251727
FinalSize = std::max(FinalSize, U.Offset);
17261728
LLVM_DEBUG(dbgs() << "MergedSize=" << FinalSize << " OrigPesSize="
17271729
<< OrigPesSize << " OrigOptSize" << OrigOptSize << "\n");
@@ -1780,7 +1782,7 @@ bool StackColoring::run(MachineFunction &Func) {
17801782
LLVM_DEBUG(dbgs() << "Found " << NumMarkers << " markers and " << NumSlots
17811783
<< " slots\n");
17821784

1783-
for (int i=0; i < MFI->getObjectIndexEnd(); ++i)
1785+
for (int i = 0; i < MFI->getObjectIndexEnd(); ++i)
17841786
TotalSize += MFI->getObjectSize(i);
17851787

17861788
LLVM_DEBUG(dbgs() << "Total Stack size: " << TotalSize << " bytes\n\n");
@@ -1793,7 +1795,7 @@ bool StackColoring::run(MachineFunction &Func) {
17931795
}
17941796

17951797
Slot2Info.resize(NumSlots);
1796-
for (unsigned i=0; i < NumSlots; ++i) {
1798+
for (unsigned i = 0; i < NumSlots; ++i) {
17971799
std::unique_ptr<LiveRange> LI(new LiveRange());
17981800
LI->getNextValue(Indexes->getZeroIndex(), VNInfoAllocator);
17991801
Intervals.push_back(std::move(LI));

0 commit comments

Comments
 (0)