44#include "llvm/Config/llvm-config.h"
66#define DEBUG_TYPE "stack-coloring"
81 cl::desc(
"Do not optimize lifetime zones that "
91 cl::desc(
"Treat stack lifetimes as starting on first use, not on START marker."));
94STATISTIC(NumMarkerSeen,
"Number of lifetime markers found.");
95STATISTIC(StackSpaceSaved,
"Number of bytes saved due to merging slots.");
96STATISTIC(StackSlotMerged,
"Number of stack slot merged.");
97STATISTIC(EscapedAllocas,
"Number of allocas that escaped the lifetime region");
387 struct BlockLifetimeInfo {
403 LivenessMap BlockLiveness;
437 unsigned NumIterations;
440 StackColoring(
SlotIndexes *Indexes) : Indexes(Indexes) {}
449 void dumpIntervals()
const;
451 void dumpBV(
const char *tag,
const BitVector &BV)
const;
455 bool removeAllMarkers();
460 unsigned collectMarkers(
unsigned NumSlot);
466 void calculateLocalLiveness();
470 bool applyFirstUse(
int Slot) {
473 if (ConservativeSlots.
test(Slot))
488 void calculateLiveIntervals(
unsigned NumSlots);
500 void removeInvalidSlotRanges();
519char StackColoringLegacy::ID = 0;
524 "Merge disjoint stack slots",
false,
false)
529void StackColoringLegacy::getAnalysisUsage(
AnalysisUsage &AU)
const {
534#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
537 dbgs() << tag <<
" : { ";
538 for (
unsigned I = 0, E = BV.
size();
I != E; ++
I)
544 LivenessMap::const_iterator BI = BlockLiveness.find(
MBB);
545 assert(BI != BlockLiveness.end() &&
"Block not found");
546 const BlockLifetimeInfo &BlockInfo = BI->second;
548 dumpBV(
"BEGIN", BlockInfo.Begin);
549 dumpBV(
"END", BlockInfo.End);
550 dumpBV(
"LIVE_IN", BlockInfo.LiveIn);
551 dumpBV(
"LIVE_OUT", BlockInfo.LiveOut);
563 for (
unsigned I = 0, E = Intervals.
size();
I != E; ++
I) {
564 dbgs() <<
"Interval[" <<
I <<
"]:\n";
565 Intervals[
I]->dump();
572 assert((
MI.getOpcode() == TargetOpcode::LIFETIME_START ||
573 MI.getOpcode() == TargetOpcode::LIFETIME_END) &&
574 "Expected LIFETIME_START or LIFETIME_END op");
589 if (
MI.getOpcode() == TargetOpcode::LIFETIME_START ||
590 MI.getOpcode() == TargetOpcode::LIFETIME_END) {
594 if (!InterestingSlots.
test(Slot))
596 slots.push_back(Slot);
597 if (
MI.getOpcode() == TargetOpcode::LIFETIME_END) {
601 if (!applyFirstUse(Slot)) {
606 if (!
MI.isDebugInstr()) {
611 int Slot = MO.getIndex();
614 if (InterestingSlots.
test(Slot) && applyFirstUse(Slot)) {
615 slots.push_back(Slot);
628unsigned StackColoring::collectMarkers(
unsigned NumSlot) {
629 unsigned MarkersFound = 0;
630 BlockBitVecMap SeenStartMap;
631 InterestingSlots.
clear();
632 InterestingSlots.
resize(NumSlot);
633 ConservativeSlots.
clear();
634 ConservativeSlots.
resize(NumSlot);
647 BetweenStartEnd.
resize(NumSlot);
649 BlockBitVecMap::const_iterator
I = SeenStartMap.find(Pred);
650 if (
I != SeenStartMap.end()) {
651 BetweenStartEnd |=
I->second;
657 if (
MI.isDebugInstr())
659 if (
MI.getOpcode() == TargetOpcode::LIFETIME_START ||
660 MI.getOpcode() == TargetOpcode::LIFETIME_END) {
664 InterestingSlots.
set(Slot);
665 if (
MI.getOpcode() == TargetOpcode::LIFETIME_START) {
666 BetweenStartEnd.
set(Slot);
667 NumStartLifetimes[
Slot] += 1;
669 BetweenStartEnd.
reset(Slot);
670 NumEndLifetimes[
Slot] += 1;
680 <<
" with allocation: " << Allocation->
getName() <<
"\n");
688 int Slot = MO.getIndex();
691 if (! BetweenStartEnd.
test(Slot)) {
692 ConservativeSlots.
set(Slot);
698 SeenStart |= BetweenStartEnd;
706 for (
unsigned slot = 0; slot < NumSlot; ++slot) {
707 if (NumStartLifetimes[slot] > 1 || NumEndLifetimes[slot] > 1)
708 ConservativeSlots.
set(slot);
718 if (
H.CatchObj.FrameIndex != std::numeric_limits<int>::max() &&
719 H.CatchObj.FrameIndex >= 0)
720 ConservativeSlots.
set(
H.CatchObj.FrameIndex);
722 LLVM_DEBUG(dumpBV(
"Conservative slots", ConservativeSlots));
730 BasicBlocks[
MBB] = BasicBlockNumbering.
size();
734 BlockLifetimeInfo &BlockInfo = BlockLiveness[
MBB];
736 BlockInfo.Begin.resize(NumSlot);
737 BlockInfo.End.resize(NumSlot);
741 bool isStart =
false;
743 if (isLifetimeStartOrEnd(
MI,
slots, isStart)) {
745 assert(
slots.size() == 1 &&
"unexpected: MI ends multiple slots");
747 if (BlockInfo.Begin.test(Slot)) {
748 BlockInfo.Begin.reset(Slot);
750 BlockInfo.End.set(Slot);
752 for (
auto Slot :
slots) {
760 <<
" with allocation: " << Allocation->
getName());
763 if (BlockInfo.End.test(Slot)) {
764 BlockInfo.End.reset(Slot);
766 BlockInfo.Begin.set(Slot);
774 NumMarkerSeen += MarkersFound;
778void StackColoring::calculateLocalLiveness() {
779 unsigned NumIters = 0;
791 LivenessMap::iterator BI = BlockLiveness.find(BB);
792 assert(BI != BlockLiveness.end() &&
"Block not found");
793 BlockLifetimeInfo &BlockInfo = BI->second;
798 LivenessMap::const_iterator
I = BlockLiveness.find(Pred);
802 if (
I != BlockLiveness.end())
803 LocalLiveIn |=
I->second.LiveOut;
813 LocalLiveOut = LocalLiveIn;
814 LocalLiveOut.
reset(BlockInfo.End);
815 LocalLiveOut |= BlockInfo.Begin;
818 if (LocalLiveIn.
test(BlockInfo.LiveIn)) {
820 BlockInfo.LiveIn |= LocalLiveIn;
824 if (LocalLiveOut.
test(BlockInfo.LiveOut)) {
826 BlockInfo.LiveOut |= LocalLiveOut;
831 NumIterations = NumIters;
834void StackColoring::calculateLiveIntervals(
unsigned NumSlots) {
843 DefinitelyInUse.
clear();
844 DefinitelyInUse.
resize(NumSlots);
847 BlockLifetimeInfo &MBBLiveness = BlockLiveness[&
MBB];
848 for (
int pos = MBBLiveness.LiveIn.find_first(); pos != -1;
849 pos = MBBLiveness.LiveIn.find_next(pos)) {
856 bool IsStart =
false;
857 if (!isLifetimeStartOrEnd(
MI,
slots, IsStart))
860 for (
auto Slot :
slots) {
865 if (!DefinitelyInUse[Slot]) {
867 DefinitelyInUse[
Slot] =
true;
870 Starts[
Slot] = ThisIndex;
873 VNInfo *VNI = Intervals[
Slot]->getValNumInfo(0);
874 Intervals[
Slot]->addSegment(
877 DefinitelyInUse[
Slot] =
false;
884 for (
unsigned i = 0; i < NumSlots; ++i) {
889 VNInfo *VNI = Intervals[i]->getValNumInfo(0);
895bool StackColoring::removeAllMarkers() {
898 MI->eraseFromParent();
908 unsigned FixedInstr = 0;
909 unsigned FixedMemOp = 0;
910 unsigned FixedDbg = 0;
913 for (
auto &VI : MF->getVariableDbgInfo()) {
914 if (!
VI.Var || !
VI.inStackSlot())
916 int Slot =
VI.getStackSlot();
917 if (
auto It = SlotRemap.
find(Slot); It != SlotRemap.
end()) {
919 << cast<DILocalVariable>(
VI.Var)->getName() <<
"].\n");
920 VI.updateStackSlot(It->second);
931 for (
const std::pair<int, int> &SI : SlotRemap) {
934 assert(To &&
From &&
"Invalid allocation object");
939 if (
From->comesBefore(To))
977 for (
auto &
Use : FromAI->
uses()) {
979 if (BCI->isUsedByMetadata())
990 std::vector<std::vector<MachineMemOperand *>> SSRefs(
995 if (
I.getOpcode() == TargetOpcode::LIFETIME_START ||
996 I.getOpcode() == TargetOpcode::LIFETIME_END)
1003 const AllocaInst *AI = dyn_cast_or_null<AllocaInst>(MMO->getValue());
1007 auto It = Allocas.
find(AI);
1008 if (It == Allocas.
end())
1011 MMO->setValue(It->second);
1019 int FromSlot = MO.getIndex();
1026 if (!SlotRemap.count(FromSlot))
1037 bool TouchesMemory =
I.mayLoadOrStore();
1044 "Found instruction usage outside of live range.");
1049 int ToSlot = SlotRemap[FromSlot];
1050 MO.setIndex(ToSlot);
1056 bool ReplaceMemOps =
false;
1060 if (
const auto *FSV = dyn_cast_or_null<FixedStackPseudoSourceValue>(
1061 MMO->getPseudoValue())) {
1062 int FI = FSV->getFrameIndex();
1063 auto To = SlotRemap.find(FI);
1064 if (To != SlotRemap.end())
1065 SSRefs[FI].push_back(MMO);
1070 bool MayHaveConflictingAAMD =
false;
1071 if (MMO->getAAInfo()) {
1072 if (
const Value *MMOV = MMO->getValue()) {
1077 MayHaveConflictingAAMD =
true;
1079 for (
Value *V : Objs) {
1083 const AllocaInst *AI = dyn_cast_or_null<AllocaInst>(V);
1084 if (AI && MergedAllocas.
count(AI)) {
1085 MayHaveConflictingAAMD =
true;
1091 if (MayHaveConflictingAAMD) {
1093 ReplaceMemOps =
true;
1102 I.setMemRefs(*MF, NewMMOs);
1107 if (!E.value().empty()) {
1109 MF->getPSVManager().getFixedStack(SlotRemap.find(E.index())->second);
1111 Ref->setValue(NewSV);
1118 if (
H.CatchObj.FrameIndex != std::numeric_limits<int>::max())
1119 if (
auto It = SlotRemap.find(
H.CatchObj.FrameIndex);
1120 It != SlotRemap.end())
1121 H.CatchObj.FrameIndex = It->second;
1123 LLVM_DEBUG(
dbgs() <<
"Fixed " << FixedMemOp <<
" machine memory operands.\n");
1124 LLVM_DEBUG(
dbgs() <<
"Fixed " << FixedDbg <<
" debug locations.\n");
1125 LLVM_DEBUG(
dbgs() <<
"Fixed " << FixedInstr <<
" machine instructions.\n");
1131void StackColoring::removeInvalidSlotRanges() {
1134 if (
I.getOpcode() == TargetOpcode::LIFETIME_START ||
1135 I.getOpcode() == TargetOpcode::LIFETIME_END ||
I.isDebugInstr())
1144 if (!
I.mayLoad() && !
I.mayStore())
1152 int Slot = MO.getIndex();
1157 if (Intervals[Slot]->empty())
1174 unsigned NumSlots) {
1176 for (
unsigned i=0; i < NumSlots; ++i) {
1178 if (
auto It = SlotRemap.
find(i); It != SlotRemap.
end()) {
1183 if (It == SlotRemap.
end())
1196 StackColoring SC(&getAnalysis<SlotIndexesWrapperPass>().getSI());
1210 <<
"********** Function: " << Func.getName() <<
'\n');
1213 BlockLiveness.clear();
1214 BasicBlocks.
clear();
1215 BasicBlockNumbering.clear();
1219 VNInfoAllocator.
Reset();
1228 SortedSlots.
reserve(NumSlots);
1230 LiveStarts.
resize(NumSlots);
1232 unsigned NumMarkers = collectMarkers(NumSlots);
1234 unsigned TotalSize = 0;
1235 LLVM_DEBUG(
dbgs() <<
"Found " << NumMarkers <<
" markers and " << NumSlots
1245 LLVM_DEBUG(
dbgs() <<
"Total Stack size: " << TotalSize <<
" bytes\n\n");
1251 return removeAllMarkers();
1254 for (
unsigned i=0; i < NumSlots; ++i) {
1255 std::unique_ptr<LiveInterval> LI(
new LiveInterval(i, 0));
1256 LI->getNextValue(Indexes->
getZeroIndex(), VNInfoAllocator);
1262 calculateLocalLiveness();
1263 LLVM_DEBUG(
dbgs() <<
"Dataflow iterations: " << NumIterations <<
"\n");
1267 calculateLiveIntervals(NumSlots);
1273 removeInvalidSlotRanges();
1277 unsigned RemovedSlots = 0;
1278 unsigned ReducedSize = 0;
1281 for (
unsigned I = 0;
I < NumSlots; ++
I) {
1282 if (Intervals[SortedSlots[
I]]->empty())
1283 SortedSlots[
I] = -1;
1304 for (
auto &s : LiveStarts)
1307 bool Changed =
true;
1310 for (
unsigned I = 0;
I < NumSlots; ++
I) {
1311 if (SortedSlots[
I] == -1)
1314 for (
unsigned J=
I+1; J < NumSlots; ++J) {
1315 if (SortedSlots[J] == -1)
1318 int FirstSlot = SortedSlots[
I];
1319 int SecondSlot = SortedSlots[J];
1327 auto &FirstS = LiveStarts[FirstSlot];
1328 auto &SecondS = LiveStarts[SecondSlot];
1333 if (!
First->isLiveAtIndexes(SecondS) &&
1336 First->MergeSegmentsInAsValue(*Second,
First->getValNumInfo(0));
1338 int OldSize = FirstS.size();
1339 FirstS.append(SecondS.begin(), SecondS.end());
1340 auto Mid = FirstS.begin() + OldSize;
1341 std::inplace_merge(FirstS.begin(), Mid, FirstS.end());
1343 SlotRemap[SecondSlot] = FirstSlot;
1344 SortedSlots[J] = -1;
1346 << SecondSlot <<
" together.\n");
1352 "Merging a small object into a larger one");
1364 StackSpaceSaved += ReducedSize;
1365 StackSlotMerged += RemovedSlots;
1366 LLVM_DEBUG(
dbgs() <<
"Merge " << RemovedSlots <<
" slots. Saved "
1367 << ReducedSize <<
" bytes\n");
1371 if (!SlotRemap.
empty()) {
1372 expungeSlotMap(SlotRemap, NumSlots);
1373 remapInstructions(SlotRemap);
1376 return removeAllMarkers();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
BlockVerifier::State From
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
This defines the Use class.
std::pair< uint64_t, uint64_t > Interval
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static int getStartOrEndSlot(const MachineInstr &MI)
static cl::opt< bool > DisableColoring("no-stack-coloring", cl::init(false), cl::Hidden, cl::desc("Disable stack coloring"))
static cl::opt< bool > ProtectFromEscapedAllocas("protect-from-escaped-allocas", cl::init(false), cl::Hidden, cl::desc("Do not optimize lifetime zones that " "are broken"))
The user may write code that uses allocas outside of the declared lifetime zone.
static cl::opt< bool > LifetimeStartOnFirstUse("stackcoloring-lifetime-start-on-first-use", cl::init(true), cl::Hidden, cl::desc("Treat stack lifetimes as starting on first use, not on START marker."))
Enable enhanced dataflow scheme for lifetime analysis (treat first use of stack slot as start of slot...
Merge disjoint stack slots
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
an instruction to allocate memory on the stack
PointerType * getType() const
Overload to return most specific pointer type.
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
This class represents a no-op cast from one type to another.
bool test(unsigned Idx) const
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
void clear()
clear - Removes all bits from the bitvector.
size_type size() const
size - Returns the number of bits in this bitvector.
Allocate memory in an ever growing pool, as if by bump-pointer.
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
iterator find(const_arg_type_t< KeyT > Val)
LLVM_ABI void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction.
LiveInterval - This class represents the liveness of a register, or stack slot.
LLVM_ABI bool isLiveAtIndexes(ArrayRef< SlotIndex > Slots) const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
iterator_range< pred_iterator > predecessors()
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
SSPLayoutKind getObjectSSPLayout(int ObjectIdx) const
const AllocaInst * getObjectAllocation(int ObjectIdx) const
Return the underlying Alloca of the specified stack object if it exists.
SSPLayoutKind
Stack Smashing Protection (SSP) rules require that vulnerable stack allocations are located close the...
@ SSPLK_LargeArray
Array or nested array >= SSP-buffer-size.
@ SSPLK_AddrOf
The address of this allocation is exposed and triggered protection.
@ SSPLK_None
Did not trigger a stack protector.
void setObjectSSPLayout(int ObjectIdx, SSPLayoutKind Kind)
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
void RemoveStackObject(int ObjectIdx)
Remove or mark dead a statically sized stack object.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
uint8_t getStackID(int ObjectIdx) const
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const WinEHFuncInfo * getWinEHFuncInfo() const
getWinEHFuncInfo - Return information about how the current function uses Windows exception handling.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
A description of a memory reference used in the backend.
MachineOperand class - Representation of each machine instruction operand.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Special value supplied for machine level alias analysis.
SlotIndex - An opaque wrapper around machine indexes.
LLVM_ABI void print(raw_ostream &os) const
Print this index to the given raw_ostream.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
SlotIndex getZeroIndex()
Returns the zero index for this analysis.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Target - Wrapper for Target specific information.
A Use represents the edge between a Value definition and its users.
VNInfo - Value Number Information.
LLVM Value Representation.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
iterator_range< use_iterator > uses()
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
self_iterator getIterator()
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
void stable_sort(R &&Range)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ Ref
The access may reference the value stored in memory.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
LLVM_ABI char & StackColoringLegacyID
StackSlotColoring - This pass performs stack coloring and merging.
iterator_range< df_iterator< T > > depth_first(const T &G)
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
This struct is a compact representation of a valid (non-zero power of two) alignment.
This represents a simple continuous liveness interval for a value.
SmallVector< WinEHHandlerType, 1 > HandlerArray