2012-09-06 17:17:37 +08:00
|
|
|
//===-- StackColoring.cpp -------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass implements the stack-coloring optimization that looks for
|
|
|
|
// lifetime markers machine instructions (LIFESTART_BEGIN and LIFESTART_END),
|
|
|
|
// which represent the possible lifetime of stack slots. It attempts to
|
|
|
|
// merge disjoint stack slots and reduce the used stack space.
|
|
|
|
// NOTE: This pass is not StackSlotColoring, which optimizes spill slots.
|
|
|
|
//
|
|
|
|
// TODO: In the future we plan to improve stack coloring in the following ways:
|
|
|
|
// 1. Allow merging multiple small slots into a single larger slot at different
|
|
|
|
// offsets.
|
|
|
|
// 2. Merge this pass with StackSlotColoring and allow merging of allocas with
|
|
|
|
// spill slots.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "stackcoloring"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SparseSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
#include "llvm/CodeGen/LiveInterval.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
|
|
|
#include "llvm/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
#include "llvm/MC/MCInstrItineraries.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
DisableColoring("no-stack-coloring",
|
2012-09-12 19:06:26 +08:00
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Disable stack coloring"));
|
2012-09-06 17:17:37 +08:00
|
|
|
|
2012-09-13 23:46:30 +08:00
|
|
|
/// The user may write code that uses allocas outside of the declared lifetime
|
|
|
|
/// zone. This can happen when the user returns a reference to a local
|
|
|
|
/// data-structure. We can detect these cases and decide not to optimize the
|
|
|
|
/// code. If this flag is enabled, we try to save the user.
|
2012-09-12 19:06:26 +08:00
|
|
|
static cl::opt<bool>
|
2012-09-13 23:46:30 +08:00
|
|
|
ProtectFromEscapedAllocas("protect-from-escaped-allocas",
|
2013-03-26 04:05:35 +08:00
|
|
|
cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Do not optimize lifetime zones that "
|
|
|
|
"are broken"));
|
2012-09-12 19:06:26 +08:00
|
|
|
|
|
|
|
STATISTIC(NumMarkerSeen, "Number of lifetime markers found.");
|
2012-09-06 17:17:37 +08:00
|
|
|
STATISTIC(StackSpaceSaved, "Number of bytes saved due to merging slots.");
|
|
|
|
STATISTIC(StackSlotMerged, "Number of stack slot merged.");
|
2013-03-26 04:05:35 +08:00
|
|
|
STATISTIC(EscapedAllocas, "Number of allocas that escaped the lifetime region");
|
2012-09-12 19:06:26 +08:00
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StackColoring Pass
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// StackColoring - A machine pass for merging disjoint stack allocations,
|
|
|
|
/// marked by the LIFETIME_START and LIFETIME_END pseudo instructions.
|
|
|
|
class StackColoring : public MachineFunctionPass {
|
|
|
|
MachineFrameInfo *MFI;
|
|
|
|
MachineFunction *MF;
|
|
|
|
|
|
|
|
/// A class representing liveness information for a single basic block.
|
|
|
|
/// Each bit in the BitVector represents the liveness property
|
|
|
|
/// for a different stack slot.
|
|
|
|
struct BlockLifetimeInfo {
|
|
|
|
/// Which slots BEGINs in each basic block.
|
|
|
|
BitVector Begin;
|
|
|
|
/// Which slots ENDs in each basic block.
|
|
|
|
BitVector End;
|
|
|
|
/// Which slots are marked as LIVE_IN, coming into each basic block.
|
|
|
|
BitVector LiveIn;
|
|
|
|
/// Which slots are marked as LIVE_OUT, coming out of each basic block.
|
|
|
|
BitVector LiveOut;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Maps active slots (per bit) for each basic block.
|
2013-02-19 13:32:02 +08:00
|
|
|
typedef DenseMap<const MachineBasicBlock*, BlockLifetimeInfo> LivenessMap;
|
|
|
|
LivenessMap BlockLiveness;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
/// Maps serial numbers to basic blocks.
|
2013-02-19 13:32:02 +08:00
|
|
|
DenseMap<const MachineBasicBlock*, int> BasicBlocks;
|
2012-09-06 17:17:37 +08:00
|
|
|
/// Maps basic blocks to a serial number.
|
2013-02-19 13:32:02 +08:00
|
|
|
SmallVector<const MachineBasicBlock*, 8> BasicBlockNumbering;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
/// Maps liveness intervals for each slot.
|
|
|
|
SmallVector<LiveInterval*, 16> Intervals;
|
|
|
|
/// VNInfo is used for the construction of LiveIntervals.
|
|
|
|
VNInfo::Allocator VNInfoAllocator;
|
|
|
|
/// SlotIndex analysis object.
|
2012-09-12 19:06:26 +08:00
|
|
|
SlotIndexes *Indexes;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
/// The list of lifetime markers found. These markers are to be removed
|
|
|
|
/// once the coloring is done.
|
|
|
|
SmallVector<MachineInstr*, 8> Markers;
|
|
|
|
|
|
|
|
/// SlotSizeSorter - A Sort utility for arranging stack slots according
|
|
|
|
/// to their size.
|
|
|
|
struct SlotSizeSorter {
|
|
|
|
MachineFrameInfo *MFI;
|
|
|
|
SlotSizeSorter(MachineFrameInfo *mfi) : MFI(mfi) { }
|
|
|
|
bool operator()(int LHS, int RHS) {
|
|
|
|
// We use -1 to denote a uninteresting slot. Place these slots at the end.
|
|
|
|
if (LHS == -1) return false;
|
|
|
|
if (RHS == -1) return true;
|
|
|
|
// Sort according to size.
|
|
|
|
return MFI->getObjectSize(LHS) > MFI->getObjectSize(RHS);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
StackColoring() : MachineFunctionPass(ID) {
|
|
|
|
initializeStackColoringPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Debug.
|
2013-02-19 11:14:22 +08:00
|
|
|
void dump() const;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
/// Removes all of the lifetime marker instructions from the function.
|
|
|
|
/// \returns true if any markers were removed.
|
|
|
|
bool removeAllMarkers();
|
|
|
|
|
|
|
|
/// Scan the machine function and find all of the lifetime markers.
|
|
|
|
/// Record the findings in the BEGIN and END vectors.
|
|
|
|
/// \returns the number of markers found.
|
|
|
|
unsigned collectMarkers(unsigned NumSlot);
|
|
|
|
|
|
|
|
/// Perform the dataflow calculation and calculate the lifetime for each of
|
|
|
|
/// the slots, based on the BEGIN/END vectors. Set the LifetimeLIVE_IN and
|
|
|
|
/// LifetimeLIVE_OUT maps that represent which stack slots are live coming
|
|
|
|
/// in and out blocks.
|
|
|
|
void calculateLocalLiveness();
|
|
|
|
|
|
|
|
/// Construct the LiveIntervals for the slots.
|
|
|
|
void calculateLiveIntervals(unsigned NumSlots);
|
|
|
|
|
|
|
|
/// Go over the machine function and change instructions which use stack
|
|
|
|
/// slots to use the joint slots.
|
|
|
|
void remapInstructions(DenseMap<int, int> &SlotRemap);
|
|
|
|
|
2012-09-12 12:57:37 +08:00
|
|
|
/// The input program may contain intructions which are not inside lifetime
|
|
|
|
/// markers. This can happen due to a bug in the compiler or due to a bug in
|
|
|
|
/// user code (for example, returning a reference to a local variable).
|
|
|
|
/// This procedure checks all of the instructions in the function and
|
|
|
|
/// invalidates lifetime ranges which do not contain all of the instructions
|
|
|
|
/// which access that frame slot.
|
|
|
|
void removeInvalidSlotRanges();
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
/// Map entries which point to other entries to their destination.
|
|
|
|
/// A->B->C becomes A->C.
|
|
|
|
void expungeSlotMap(DenseMap<int, int> &SlotRemap, unsigned NumSlots);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
char StackColoring::ID = 0;
|
|
|
|
char &llvm::StackColoringID = StackColoring::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(StackColoring,
|
|
|
|
"stack-coloring", "Merge disjoint stack slots", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
|
|
|
|
INITIALIZE_PASS_END(StackColoring,
|
|
|
|
"stack-coloring", "Merge disjoint stack slots", false, false)
|
|
|
|
|
|
|
|
void StackColoring::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
|
|
|
AU.addRequired<SlotIndexes>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2013-02-19 11:14:22 +08:00
|
|
|
void StackColoring::dump() const {
|
2012-09-06 17:17:37 +08:00
|
|
|
for (df_iterator<MachineFunction*> FI = df_begin(MF), FE = df_end(MF);
|
|
|
|
FI != FE; ++FI) {
|
2013-02-19 11:14:22 +08:00
|
|
|
DEBUG(dbgs()<<"Inspecting block #"<<BasicBlocks.lookup(*FI)<<
|
2013-01-30 01:42:24 +08:00
|
|
|
" ["<<FI->getName()<<"]\n");
|
2013-02-19 11:14:22 +08:00
|
|
|
|
2013-02-19 13:32:02 +08:00
|
|
|
LivenessMap::const_iterator BI = BlockLiveness.find(*FI);
|
2013-02-19 11:14:22 +08:00
|
|
|
assert(BI != BlockLiveness.end() && "Block not found");
|
|
|
|
const BlockLifetimeInfo &BlockInfo = BI->second;
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
DEBUG(dbgs()<<"BEGIN : {");
|
2013-02-19 11:14:22 +08:00
|
|
|
for (unsigned i=0; i < BlockInfo.Begin.size(); ++i)
|
|
|
|
DEBUG(dbgs()<<BlockInfo.Begin.test(i)<<" ");
|
2012-09-06 17:17:37 +08:00
|
|
|
DEBUG(dbgs()<<"}\n");
|
|
|
|
|
|
|
|
DEBUG(dbgs()<<"END : {");
|
2013-02-19 11:14:22 +08:00
|
|
|
for (unsigned i=0; i < BlockInfo.End.size(); ++i)
|
|
|
|
DEBUG(dbgs()<<BlockInfo.End.test(i)<<" ");
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs()<<"}\n");
|
|
|
|
|
|
|
|
DEBUG(dbgs()<<"LIVE_IN: {");
|
2013-02-19 11:14:22 +08:00
|
|
|
for (unsigned i=0; i < BlockInfo.LiveIn.size(); ++i)
|
|
|
|
DEBUG(dbgs()<<BlockInfo.LiveIn.test(i)<<" ");
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs()<<"}\n");
|
|
|
|
DEBUG(dbgs()<<"LIVEOUT: {");
|
2013-02-19 11:14:22 +08:00
|
|
|
for (unsigned i=0; i < BlockInfo.LiveOut.size(); ++i)
|
|
|
|
DEBUG(dbgs()<<BlockInfo.LiveOut.test(i)<<" ");
|
2012-09-06 17:17:37 +08:00
|
|
|
DEBUG(dbgs()<<"}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned StackColoring::collectMarkers(unsigned NumSlot) {
|
|
|
|
unsigned MarkersFound = 0;
|
|
|
|
// Scan the function to find all lifetime markers.
|
|
|
|
// NOTE: We use the a reverse-post-order iteration to ensure that we obtain a
|
|
|
|
// deterministic numbering, and because we'll need a post-order iteration
|
|
|
|
// later for solving the liveness dataflow problem.
|
|
|
|
for (df_iterator<MachineFunction*> FI = df_begin(MF), FE = df_end(MF);
|
|
|
|
FI != FE; ++FI) {
|
|
|
|
|
|
|
|
// Assign a serial number to this basic block.
|
2012-09-11 05:26:47 +08:00
|
|
|
BasicBlocks[*FI] = BasicBlockNumbering.size();
|
2012-09-06 17:17:37 +08:00
|
|
|
BasicBlockNumbering.push_back(*FI);
|
|
|
|
|
2013-02-19 11:06:17 +08:00
|
|
|
// Keep a reference to avoid repeated lookups.
|
|
|
|
BlockLifetimeInfo &BlockInfo = BlockLiveness[*FI];
|
|
|
|
|
|
|
|
BlockInfo.Begin.resize(NumSlot);
|
|
|
|
BlockInfo.End.resize(NumSlot);
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator BI = (*FI)->begin(), BE = (*FI)->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
|
|
|
if (BI->getOpcode() != TargetOpcode::LIFETIME_START &&
|
|
|
|
BI->getOpcode() != TargetOpcode::LIFETIME_END)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Markers.push_back(BI);
|
|
|
|
|
|
|
|
bool IsStart = BI->getOpcode() == TargetOpcode::LIFETIME_START;
|
2013-02-19 14:02:40 +08:00
|
|
|
const MachineOperand &MI = BI->getOperand(0);
|
2012-09-06 17:17:37 +08:00
|
|
|
unsigned Slot = MI.getIndex();
|
|
|
|
|
|
|
|
MarkersFound++;
|
|
|
|
|
2012-10-19 03:53:48 +08:00
|
|
|
const AllocaInst *Allocation = MFI->getObjectAllocation(Slot);
|
2012-09-06 17:17:37 +08:00
|
|
|
if (Allocation) {
|
2012-09-13 20:38:37 +08:00
|
|
|
DEBUG(dbgs()<<"Found a lifetime marker for slot #"<<Slot<<
|
|
|
|
" with allocation: "<< Allocation->getName()<<"\n");
|
2012-09-06 17:17:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsStart) {
|
2013-02-19 11:06:17 +08:00
|
|
|
BlockInfo.Begin.set(Slot);
|
2012-09-06 17:17:37 +08:00
|
|
|
} else {
|
2013-02-19 11:06:17 +08:00
|
|
|
if (BlockInfo.Begin.test(Slot)) {
|
2012-09-06 17:17:37 +08:00
|
|
|
// Allocas that start and end within a single block are handled
|
|
|
|
// specially when computing the LiveIntervals to avoid pessimizing
|
|
|
|
// the liveness propagation.
|
2013-02-19 11:06:17 +08:00
|
|
|
BlockInfo.Begin.reset(Slot);
|
2012-09-06 17:17:37 +08:00
|
|
|
} else {
|
2013-02-19 11:06:17 +08:00
|
|
|
BlockInfo.End.set(Slot);
|
2012-09-06 17:17:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update statistics.
|
|
|
|
NumMarkerSeen += MarkersFound;
|
|
|
|
return MarkersFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackColoring::calculateLocalLiveness() {
|
|
|
|
// Perform a standard reverse dataflow computation to solve for
|
|
|
|
// global liveness. The BEGIN set here is equivalent to KILL in the standard
|
|
|
|
// formulation, and END is equivalent to GEN. The result of this computation
|
|
|
|
// is a map from blocks to bitvectors where the bitvectors represent which
|
|
|
|
// allocas are live in/out of that block.
|
2013-02-19 13:32:02 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 8> BBSet(BasicBlockNumbering.begin(),
|
|
|
|
BasicBlockNumbering.end());
|
2012-09-06 17:17:37 +08:00
|
|
|
unsigned NumSSMIters = 0;
|
|
|
|
bool changed = true;
|
|
|
|
while (changed) {
|
|
|
|
changed = false;
|
|
|
|
++NumSSMIters;
|
|
|
|
|
2013-02-19 13:32:02 +08:00
|
|
|
SmallPtrSet<const MachineBasicBlock*, 8> NextBBSet;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
2013-02-19 13:32:02 +08:00
|
|
|
for (SmallVector<const MachineBasicBlock*, 8>::iterator
|
2012-09-06 17:17:37 +08:00
|
|
|
PI = BasicBlockNumbering.begin(), PE = BasicBlockNumbering.end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
|
2013-02-19 13:32:02 +08:00
|
|
|
const MachineBasicBlock *BB = *PI;
|
2012-09-06 17:17:37 +08:00
|
|
|
if (!BBSet.count(BB)) continue;
|
|
|
|
|
2013-02-19 12:47:31 +08:00
|
|
|
// Use an iterator to avoid repeated lookups.
|
2013-02-19 13:32:02 +08:00
|
|
|
LivenessMap::iterator BI = BlockLiveness.find(BB);
|
2013-02-19 12:47:31 +08:00
|
|
|
assert(BI != BlockLiveness.end() && "Block not found");
|
|
|
|
BlockLifetimeInfo &BlockInfo = BI->second;
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
BitVector LocalLiveIn;
|
|
|
|
BitVector LocalLiveOut;
|
|
|
|
|
|
|
|
// Forward propagation from begins to ends.
|
2013-02-19 12:47:31 +08:00
|
|
|
for (MachineBasicBlock::const_pred_iterator PI = BB->pred_begin(),
|
|
|
|
PE = BB->pred_end(); PI != PE; ++PI) {
|
2013-02-19 13:32:02 +08:00
|
|
|
LivenessMap::const_iterator I = BlockLiveness.find(*PI);
|
2013-02-19 12:47:31 +08:00
|
|
|
assert(I != BlockLiveness.end() && "Predecessor not found");
|
|
|
|
LocalLiveIn |= I->second.LiveOut;
|
|
|
|
}
|
|
|
|
LocalLiveIn |= BlockInfo.End;
|
|
|
|
LocalLiveIn.reset(BlockInfo.Begin);
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
// Reverse propagation from ends to begins.
|
2013-02-19 12:47:31 +08:00
|
|
|
for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
|
|
|
|
SE = BB->succ_end(); SI != SE; ++SI) {
|
2013-02-19 13:32:02 +08:00
|
|
|
LivenessMap::const_iterator I = BlockLiveness.find(*SI);
|
2013-02-19 12:47:31 +08:00
|
|
|
assert(I != BlockLiveness.end() && "Successor not found");
|
|
|
|
LocalLiveOut |= I->second.LiveIn;
|
|
|
|
}
|
|
|
|
LocalLiveOut |= BlockInfo.Begin;
|
|
|
|
LocalLiveOut.reset(BlockInfo.End);
|
2012-09-06 17:17:37 +08:00
|
|
|
|
|
|
|
LocalLiveIn |= LocalLiveOut;
|
|
|
|
LocalLiveOut |= LocalLiveIn;
|
|
|
|
|
|
|
|
// After adopting the live bits, we need to turn-off the bits which
|
|
|
|
// are de-activated in this block.
|
2013-02-19 12:47:31 +08:00
|
|
|
LocalLiveOut.reset(BlockInfo.End);
|
|
|
|
LocalLiveIn.reset(BlockInfo.Begin);
|
2012-09-06 17:17:37 +08:00
|
|
|
|
2012-09-11 02:51:09 +08:00
|
|
|
// If we have both BEGIN and END markers in the same basic block then
|
|
|
|
// we know that the BEGIN marker comes after the END, because we already
|
|
|
|
// handle the case where the BEGIN comes before the END when collecting
|
|
|
|
// the markers (and building the BEGIN/END vectore).
|
|
|
|
// Want to enable the LIVE_IN and LIVE_OUT of slots that have both
|
|
|
|
// BEGIN and END because it means that the value lives before and after
|
|
|
|
// this basic block.
|
2013-02-19 12:47:31 +08:00
|
|
|
BitVector LocalEndBegin = BlockInfo.End;
|
|
|
|
LocalEndBegin &= BlockInfo.Begin;
|
2012-09-11 02:51:09 +08:00
|
|
|
LocalLiveIn |= LocalEndBegin;
|
|
|
|
LocalLiveOut |= LocalEndBegin;
|
|
|
|
|
2013-02-19 12:47:31 +08:00
|
|
|
if (LocalLiveIn.test(BlockInfo.LiveIn)) {
|
2012-09-06 17:17:37 +08:00
|
|
|
changed = true;
|
2013-02-19 12:47:31 +08:00
|
|
|
BlockInfo.LiveIn |= LocalLiveIn;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
2013-02-19 13:32:02 +08:00
|
|
|
for (MachineBasicBlock::const_pred_iterator PI = BB->pred_begin(),
|
2012-09-06 17:17:37 +08:00
|
|
|
PE = BB->pred_end(); PI != PE; ++PI)
|
|
|
|
NextBBSet.insert(*PI);
|
|
|
|
}
|
|
|
|
|
2013-02-19 12:47:31 +08:00
|
|
|
if (LocalLiveOut.test(BlockInfo.LiveOut)) {
|
2012-09-06 17:17:37 +08:00
|
|
|
changed = true;
|
2013-02-19 12:47:31 +08:00
|
|
|
BlockInfo.LiveOut |= LocalLiveOut;
|
2012-09-06 17:17:37 +08:00
|
|
|
|
2013-02-19 13:32:02 +08:00
|
|
|
for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
|
2012-09-06 17:17:37 +08:00
|
|
|
SE = BB->succ_end(); SI != SE; ++SI)
|
|
|
|
NextBBSet.insert(*SI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BBSet = NextBBSet;
|
|
|
|
}// while changed.
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
|
|
|
|
SmallVector<SlotIndex, 16> Starts;
|
|
|
|
SmallVector<SlotIndex, 16> Finishes;
|
|
|
|
|
|
|
|
// For each block, find which slots are active within this block
|
|
|
|
// and update the live intervals.
|
|
|
|
for (MachineFunction::iterator MBB = MF->begin(), MBBe = MF->end();
|
|
|
|
MBB != MBBe; ++MBB) {
|
|
|
|
Starts.clear();
|
|
|
|
Starts.resize(NumSlots);
|
|
|
|
Finishes.clear();
|
|
|
|
Finishes.resize(NumSlots);
|
|
|
|
|
2012-09-10 20:39:35 +08:00
|
|
|
// Create the interval for the basic blocks with lifetime markers in them.
|
2013-02-19 14:02:40 +08:00
|
|
|
for (SmallVectorImpl<MachineInstr*>::const_iterator it = Markers.begin(),
|
2012-09-06 17:17:37 +08:00
|
|
|
e = Markers.end(); it != e; ++it) {
|
2013-02-19 14:02:40 +08:00
|
|
|
const MachineInstr *MI = *it;
|
2012-09-10 20:39:35 +08:00
|
|
|
if (MI->getParent() != MBB)
|
|
|
|
continue;
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
assert((MI->getOpcode() == TargetOpcode::LIFETIME_START ||
|
|
|
|
MI->getOpcode() == TargetOpcode::LIFETIME_END) &&
|
|
|
|
"Invalid Lifetime marker");
|
|
|
|
|
2012-09-10 20:39:35 +08:00
|
|
|
bool IsStart = MI->getOpcode() == TargetOpcode::LIFETIME_START;
|
2013-02-19 14:02:40 +08:00
|
|
|
const MachineOperand &Mo = MI->getOperand(0);
|
2012-09-10 20:39:35 +08:00
|
|
|
int Slot = Mo.getIndex();
|
|
|
|
assert(Slot >= 0 && "Invalid slot");
|
|
|
|
|
|
|
|
SlotIndex ThisIndex = Indexes->getInstructionIndex(MI);
|
|
|
|
|
|
|
|
if (IsStart) {
|
|
|
|
if (!Starts[Slot].isValid() || Starts[Slot] > ThisIndex)
|
|
|
|
Starts[Slot] = ThisIndex;
|
|
|
|
} else {
|
|
|
|
if (!Finishes[Slot].isValid() || Finishes[Slot] < ThisIndex)
|
|
|
|
Finishes[Slot] = ThisIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the interval of the blocks that we previously found to be 'alive'.
|
|
|
|
BitVector Alive = BlockLiveness[MBB].LiveIn;
|
|
|
|
Alive |= BlockLiveness[MBB].LiveOut;
|
|
|
|
|
|
|
|
if (Alive.any()) {
|
|
|
|
for (int pos = Alive.find_first(); pos != -1;
|
|
|
|
pos = Alive.find_next(pos)) {
|
|
|
|
if (!Starts[pos].isValid())
|
|
|
|
Starts[pos] = Indexes->getMBBStartIdx(MBB);
|
|
|
|
if (!Finishes[pos].isValid())
|
|
|
|
Finishes[pos] = Indexes->getMBBEndIdx(MBB);
|
2012-09-06 17:17:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumSlots; ++i) {
|
2012-09-10 20:39:35 +08:00
|
|
|
assert(Starts[i].isValid() == Finishes[i].isValid() && "Unmatched range");
|
|
|
|
if (!Starts[i].isValid())
|
2012-09-06 17:17:37 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
assert(Starts[i] && Finishes[i] && "Invalid interval");
|
|
|
|
VNInfo *ValNum = Intervals[i]->getValNumInfo(0);
|
|
|
|
SlotIndex S = Starts[i];
|
|
|
|
SlotIndex F = Finishes[i];
|
|
|
|
if (S < F) {
|
|
|
|
// We have a single consecutive region.
|
|
|
|
Intervals[i]->addRange(LiveRange(S, F, ValNum));
|
|
|
|
} else {
|
|
|
|
// We have two non consecutive regions. This happens when
|
|
|
|
// LIFETIME_START appears after the LIFETIME_END marker.
|
|
|
|
SlotIndex NewStart = Indexes->getMBBStartIdx(MBB);
|
|
|
|
SlotIndex NewFin = Indexes->getMBBEndIdx(MBB);
|
|
|
|
Intervals[i]->addRange(LiveRange(NewStart, F, ValNum));
|
|
|
|
Intervals[i]->addRange(LiveRange(S, NewFin, ValNum));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StackColoring::removeAllMarkers() {
|
|
|
|
unsigned Count = 0;
|
|
|
|
for (unsigned i = 0; i < Markers.size(); ++i) {
|
|
|
|
Markers[i]->eraseFromParent();
|
|
|
|
Count++;
|
|
|
|
}
|
|
|
|
Markers.clear();
|
|
|
|
|
|
|
|
DEBUG(dbgs()<<"Removed "<<Count<<" markers.\n");
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
|
|
|
|
unsigned FixedInstr = 0;
|
|
|
|
unsigned FixedMemOp = 0;
|
|
|
|
unsigned FixedDbg = 0;
|
|
|
|
MachineModuleInfo *MMI = &MF->getMMI();
|
|
|
|
|
|
|
|
// Remap debug information that refers to stack slots.
|
|
|
|
MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
|
|
|
|
for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
|
|
|
|
VE = VMap.end(); VI != VE; ++VI) {
|
|
|
|
const MDNode *Var = VI->first;
|
|
|
|
if (!Var) continue;
|
|
|
|
std::pair<unsigned, DebugLoc> &VP = VI->second;
|
|
|
|
if (SlotRemap.count(VP.first)) {
|
|
|
|
DEBUG(dbgs()<<"Remapping debug info for ["<<Var->getName()<<"].\n");
|
|
|
|
VP.first = SlotRemap[VP.first];
|
|
|
|
FixedDbg++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep a list of *allocas* which need to be remapped.
|
2012-10-19 03:53:48 +08:00
|
|
|
DenseMap<const AllocaInst*, const AllocaInst*> Allocas;
|
2013-02-19 14:02:40 +08:00
|
|
|
for (DenseMap<int, int>::const_iterator it = SlotRemap.begin(),
|
2012-09-06 17:17:37 +08:00
|
|
|
e = SlotRemap.end(); it != e; ++it) {
|
2012-10-19 03:53:48 +08:00
|
|
|
const AllocaInst *From = MFI->getObjectAllocation(it->first);
|
|
|
|
const AllocaInst *To = MFI->getObjectAllocation(it->second);
|
2012-09-06 17:17:37 +08:00
|
|
|
assert(To && From && "Invalid allocation object");
|
|
|
|
Allocas[From] = To;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remap all instructions to the new stack slots.
|
|
|
|
MachineFunction::iterator BB, BBE;
|
|
|
|
MachineBasicBlock::iterator I, IE;
|
|
|
|
for (BB = MF->begin(), BBE = MF->end(); BB != BBE; ++BB)
|
|
|
|
for (I = BB->begin(), IE = BB->end(); I != IE; ++I) {
|
|
|
|
|
2012-09-10 16:44:15 +08:00
|
|
|
// Skip lifetime markers. We'll remove them soon.
|
|
|
|
if (I->getOpcode() == TargetOpcode::LIFETIME_START ||
|
|
|
|
I->getOpcode() == TargetOpcode::LIFETIME_END)
|
|
|
|
continue;
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
// Update the MachineMemOperand to use the new alloca.
|
|
|
|
for (MachineInstr::mmo_iterator MM = I->memoperands_begin(),
|
|
|
|
E = I->memoperands_end(); MM != E; ++MM) {
|
|
|
|
MachineMemOperand *MMO = *MM;
|
|
|
|
|
|
|
|
const Value *V = MMO->getValue();
|
|
|
|
|
|
|
|
if (!V)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Climb up and find the original alloca.
|
|
|
|
V = GetUnderlyingObject(V);
|
|
|
|
// If we did not find one, or if the one that we found is not in our
|
|
|
|
// map, then move on.
|
2012-10-19 03:53:48 +08:00
|
|
|
if (!V || !isa<AllocaInst>(V)) {
|
|
|
|
// Clear mem operand since we don't know for sure that it doesn't
|
|
|
|
// alias a merged alloca.
|
|
|
|
MMO->setValue(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const AllocaInst *AI= cast<AllocaInst>(V);
|
|
|
|
if (!Allocas.count(AI))
|
2012-09-06 17:17:37 +08:00
|
|
|
continue;
|
|
|
|
|
2012-10-19 03:53:48 +08:00
|
|
|
MMO->setValue(Allocas[AI]);
|
2012-09-06 17:17:37 +08:00
|
|
|
FixedMemOp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all of the machine instruction operands.
|
|
|
|
for (unsigned i = 0 ; i < I->getNumOperands(); ++i) {
|
|
|
|
MachineOperand &MO = I->getOperand(i);
|
|
|
|
|
|
|
|
if (!MO.isFI())
|
|
|
|
continue;
|
|
|
|
int FromSlot = MO.getIndex();
|
|
|
|
|
|
|
|
// Don't touch arguments.
|
|
|
|
if (FromSlot<0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Only look at mapped slots.
|
|
|
|
if (!SlotRemap.count(FromSlot))
|
|
|
|
continue;
|
|
|
|
|
2012-09-10 16:51:46 +08:00
|
|
|
// In a debug build, check that the instruction that we are modifying is
|
|
|
|
// inside the expected live range. If the instruction is not inside
|
2012-09-10 16:44:15 +08:00
|
|
|
// the calculated range then it means that the alloca usage moved
|
2012-09-13 23:46:30 +08:00
|
|
|
// outside of the lifetime markers, or that the user has a bug.
|
2012-09-13 20:38:37 +08:00
|
|
|
// NOTE: Alloca address calculations which happen outside the lifetime
|
|
|
|
// zone are are okay, despite the fact that we don't have a good way
|
|
|
|
// for validating all of the usages of the calculation.
|
2012-09-10 16:44:15 +08:00
|
|
|
#ifndef NDEBUG
|
2012-09-13 20:38:37 +08:00
|
|
|
bool TouchesMemory = I->mayLoad() || I->mayStore();
|
2012-09-13 23:46:30 +08:00
|
|
|
// If we *don't* protect the user from escaped allocas, don't bother
|
|
|
|
// validating the instructions.
|
|
|
|
if (!I->isDebugValue() && TouchesMemory && ProtectFromEscapedAllocas) {
|
2012-09-11 20:34:27 +08:00
|
|
|
SlotIndex Index = Indexes->getInstructionIndex(I);
|
2012-09-12 19:06:26 +08:00
|
|
|
LiveInterval *Interval = Intervals[FromSlot];
|
2012-09-11 20:34:27 +08:00
|
|
|
assert(Interval->find(Index) != Interval->end() &&
|
2013-03-26 05:26:36 +08:00
|
|
|
"Found instruction usage outside of live range.");
|
2012-09-11 20:34:27 +08:00
|
|
|
}
|
2012-09-10 16:44:15 +08:00
|
|
|
#endif
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
// Fix the machine instructions.
|
|
|
|
int ToSlot = SlotRemap[FromSlot];
|
|
|
|
MO.setIndex(ToSlot);
|
|
|
|
FixedInstr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs()<<"Fixed "<<FixedMemOp<<" machine memory operands.\n");
|
|
|
|
DEBUG(dbgs()<<"Fixed "<<FixedDbg<<" debug locations.\n");
|
|
|
|
DEBUG(dbgs()<<"Fixed "<<FixedInstr<<" machine instructions.\n");
|
|
|
|
}
|
|
|
|
|
2012-09-12 12:57:37 +08:00
|
|
|
void StackColoring::removeInvalidSlotRanges() {
|
2013-02-19 14:02:40 +08:00
|
|
|
MachineFunction::const_iterator BB, BBE;
|
|
|
|
MachineBasicBlock::const_iterator I, IE;
|
2012-09-12 12:57:37 +08:00
|
|
|
for (BB = MF->begin(), BBE = MF->end(); BB != BBE; ++BB)
|
|
|
|
for (I = BB->begin(), IE = BB->end(); I != IE; ++I) {
|
|
|
|
|
|
|
|
if (I->getOpcode() == TargetOpcode::LIFETIME_START ||
|
|
|
|
I->getOpcode() == TargetOpcode::LIFETIME_END || I->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
2012-09-13 20:38:37 +08:00
|
|
|
// Some intervals are suspicious! In some cases we find address
|
|
|
|
// calculations outside of the lifetime zone, but not actual memory
|
|
|
|
// read or write. Memory accesses outside of the lifetime zone are a clear
|
|
|
|
// violation, but address calculations are okay. This can happen when
|
|
|
|
// GEPs are hoisted outside of the lifetime zone.
|
2012-09-13 22:51:00 +08:00
|
|
|
// So, in here we only check instructions which can read or write memory.
|
2012-09-13 20:38:37 +08:00
|
|
|
if (!I->mayLoad() && !I->mayStore())
|
|
|
|
continue;
|
|
|
|
|
2012-09-12 12:57:37 +08:00
|
|
|
// Check all of the machine operands.
|
|
|
|
for (unsigned i = 0 ; i < I->getNumOperands(); ++i) {
|
2013-02-19 14:02:40 +08:00
|
|
|
const MachineOperand &MO = I->getOperand(i);
|
2012-09-12 12:57:37 +08:00
|
|
|
|
|
|
|
if (!MO.isFI())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int Slot = MO.getIndex();
|
|
|
|
|
|
|
|
if (Slot<0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Intervals[Slot]->empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check that the used slot is inside the calculated lifetime range.
|
|
|
|
// If it is not, warn about it and invalidate the range.
|
|
|
|
LiveInterval *Interval = Intervals[Slot];
|
|
|
|
SlotIndex Index = Indexes->getInstructionIndex(I);
|
|
|
|
if (Interval->find(Index) == Interval->end()) {
|
|
|
|
Intervals[Slot]->clear();
|
|
|
|
DEBUG(dbgs()<<"Invalidating range #"<<Slot<<"\n");
|
2012-09-12 19:06:26 +08:00
|
|
|
EscapedAllocas++;
|
2012-09-12 12:57:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
void StackColoring::expungeSlotMap(DenseMap<int, int> &SlotRemap,
|
|
|
|
unsigned NumSlots) {
|
|
|
|
// Expunge slot remap map.
|
|
|
|
for (unsigned i=0; i < NumSlots; ++i) {
|
|
|
|
// If we are remapping i
|
|
|
|
if (SlotRemap.count(i)) {
|
|
|
|
int Target = SlotRemap[i];
|
|
|
|
// As long as our target is mapped to something else, follow it.
|
|
|
|
while (SlotRemap.count(Target)) {
|
|
|
|
Target = SlotRemap[Target];
|
|
|
|
SlotRemap[i] = Target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
|
|
|
|
DEBUG(dbgs() << "********** Stack Coloring **********\n"
|
|
|
|
<< "********** Function: "
|
2012-09-06 23:42:13 +08:00
|
|
|
<< ((const Value*)Func.getFunction())->getName() << '\n');
|
2012-09-06 17:17:37 +08:00
|
|
|
MF = &Func;
|
|
|
|
MFI = MF->getFrameInfo();
|
|
|
|
Indexes = &getAnalysis<SlotIndexes>();
|
|
|
|
BlockLiveness.clear();
|
|
|
|
BasicBlocks.clear();
|
|
|
|
BasicBlockNumbering.clear();
|
|
|
|
Markers.clear();
|
|
|
|
Intervals.clear();
|
|
|
|
VNInfoAllocator.Reset();
|
|
|
|
|
|
|
|
unsigned NumSlots = MFI->getObjectIndexEnd();
|
|
|
|
|
|
|
|
// If there are no stack slots then there are no markers to remove.
|
|
|
|
if (!NumSlots)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<int, 8> SortedSlots;
|
|
|
|
|
|
|
|
SortedSlots.reserve(NumSlots);
|
|
|
|
Intervals.reserve(NumSlots);
|
|
|
|
|
|
|
|
unsigned NumMarkers = collectMarkers(NumSlots);
|
|
|
|
|
|
|
|
unsigned TotalSize = 0;
|
|
|
|
DEBUG(dbgs()<<"Found "<<NumMarkers<<" markers and "<<NumSlots<<" slots\n");
|
|
|
|
DEBUG(dbgs()<<"Slot structure:\n");
|
|
|
|
|
|
|
|
for (int i=0; i < MFI->getObjectIndexEnd(); ++i) {
|
|
|
|
DEBUG(dbgs()<<"Slot #"<<i<<" - "<<MFI->getObjectSize(i)<<" bytes.\n");
|
|
|
|
TotalSize += MFI->getObjectSize(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs()<<"Total Stack size: "<<TotalSize<<" bytes\n\n");
|
|
|
|
|
|
|
|
// Don't continue because there are not enough lifetime markers, or the
|
2012-09-13 20:38:37 +08:00
|
|
|
// stack is too small, or we are told not to optimize the slots.
|
2012-09-06 17:17:37 +08:00
|
|
|
if (NumMarkers < 2 || TotalSize < 16 || DisableColoring) {
|
|
|
|
DEBUG(dbgs()<<"Will not try to merge slots.\n");
|
|
|
|
return removeAllMarkers();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i=0; i < NumSlots; ++i) {
|
|
|
|
LiveInterval *LI = new LiveInterval(i, 0);
|
|
|
|
Intervals.push_back(LI);
|
|
|
|
LI->getNextValue(Indexes->getZeroIndex(), VNInfoAllocator);
|
|
|
|
SortedSlots.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the liveness of each block.
|
|
|
|
calculateLocalLiveness();
|
|
|
|
|
|
|
|
// Propagate the liveness information.
|
|
|
|
calculateLiveIntervals(NumSlots);
|
|
|
|
|
2012-09-12 19:06:26 +08:00
|
|
|
// Search for allocas which are used outside of the declared lifetime
|
|
|
|
// markers.
|
2012-09-13 23:46:30 +08:00
|
|
|
if (ProtectFromEscapedAllocas)
|
2012-09-12 19:06:26 +08:00
|
|
|
removeInvalidSlotRanges();
|
2012-09-12 12:57:37 +08:00
|
|
|
|
2012-09-06 17:17:37 +08:00
|
|
|
// Maps old slots to new slots.
|
|
|
|
DenseMap<int, int> SlotRemap;
|
|
|
|
unsigned RemovedSlots = 0;
|
|
|
|
unsigned ReducedSize = 0;
|
|
|
|
|
|
|
|
// Do not bother looking at empty intervals.
|
|
|
|
for (unsigned I = 0; I < NumSlots; ++I) {
|
|
|
|
if (Intervals[SortedSlots[I]]->empty())
|
|
|
|
SortedSlots[I] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a simple greedy algorithm for merging allocas. First, sort the
|
|
|
|
// slots, placing the largest slots first. Next, perform an n^2 scan and look
|
|
|
|
// for disjoint slots. When you find disjoint slots, merge the samller one
|
|
|
|
// into the bigger one and update the live interval. Remove the small alloca
|
|
|
|
// and continue.
|
|
|
|
|
|
|
|
// Sort the slots according to their size. Place unused slots at the end.
|
2012-11-16 03:33:30 +08:00
|
|
|
// Use stable sort to guarantee deterministic code generation.
|
|
|
|
std::stable_sort(SortedSlots.begin(), SortedSlots.end(),
|
|
|
|
SlotSizeSorter(MFI));
|
2012-09-06 17:17:37 +08:00
|
|
|
|
2013-03-26 05:26:36 +08:00
|
|
|
bool Changed = true;
|
|
|
|
while (Changed) {
|
|
|
|
Changed = false;
|
2012-09-06 17:17:37 +08:00
|
|
|
for (unsigned I = 0; I < NumSlots; ++I) {
|
|
|
|
if (SortedSlots[I] == -1)
|
|
|
|
continue;
|
|
|
|
|
2012-09-10 20:47:38 +08:00
|
|
|
for (unsigned J=I+1; J < NumSlots; ++J) {
|
2012-09-06 17:17:37 +08:00
|
|
|
if (SortedSlots[J] == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int FirstSlot = SortedSlots[I];
|
|
|
|
int SecondSlot = SortedSlots[J];
|
|
|
|
LiveInterval *First = Intervals[FirstSlot];
|
|
|
|
LiveInterval *Second = Intervals[SecondSlot];
|
|
|
|
assert (!First->empty() && !Second->empty() && "Found an empty range");
|
|
|
|
|
|
|
|
// Merge disjoint slots.
|
|
|
|
if (!First->overlaps(*Second)) {
|
2013-03-26 05:26:36 +08:00
|
|
|
Changed = true;
|
2012-09-06 17:17:37 +08:00
|
|
|
First->MergeRangesInAsValue(*Second, First->getValNumInfo(0));
|
|
|
|
SlotRemap[SecondSlot] = FirstSlot;
|
|
|
|
SortedSlots[J] = -1;
|
2012-09-10 21:17:58 +08:00
|
|
|
DEBUG(dbgs()<<"Merging #"<<FirstSlot<<" and slots #"<<
|
|
|
|
SecondSlot<<" together.\n");
|
2012-09-06 17:17:37 +08:00
|
|
|
unsigned MaxAlignment = std::max(MFI->getObjectAlignment(FirstSlot),
|
|
|
|
MFI->getObjectAlignment(SecondSlot));
|
|
|
|
|
|
|
|
assert(MFI->getObjectSize(FirstSlot) >=
|
|
|
|
MFI->getObjectSize(SecondSlot) &&
|
|
|
|
"Merging a small object into a larger one");
|
|
|
|
|
|
|
|
RemovedSlots+=1;
|
|
|
|
ReducedSize += MFI->getObjectSize(SecondSlot);
|
|
|
|
MFI->setObjectAlignment(FirstSlot, MaxAlignment);
|
|
|
|
MFI->RemoveStackObject(SecondSlot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}// While changed.
|
|
|
|
|
|
|
|
// Record statistics.
|
|
|
|
StackSpaceSaved += ReducedSize;
|
|
|
|
StackSlotMerged += RemovedSlots;
|
|
|
|
DEBUG(dbgs()<<"Merge "<<RemovedSlots<<" slots. Saved "<<
|
|
|
|
ReducedSize<<" bytes\n");
|
|
|
|
|
|
|
|
// Scan the entire function and update all machine operands that use frame
|
|
|
|
// indices to use the remapped frame index.
|
|
|
|
expungeSlotMap(SlotRemap, NumSlots);
|
|
|
|
remapInstructions(SlotRemap);
|
|
|
|
|
|
|
|
// Release the intervals.
|
|
|
|
for (unsigned I = 0; I < NumSlots; ++I) {
|
|
|
|
delete Intervals[I];
|
|
|
|
}
|
|
|
|
|
|
|
|
return removeAllMarkers();
|
|
|
|
}
|