Newer
Older
//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
Alkis Evlogimenos
committed
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
Alkis Evlogimenos
committed
//
//===----------------------------------------------------------------------===//
//
// This file implements the LiveInterval analysis pass which is used
// by the Linear Scan Register allocator. This pass linearizes the
// basic blocks of the function in DFS order and uses the
// LiveVariables pass to conservatively compute live intervals for
// each virtual and physical register.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "liveintervals"
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
#include "VirtRegMap.h"
#include "llvm/Value.h"
Dan Gohman
committed
#include "llvm/Analysis/AliasAnalysis.h"
Alkis Evlogimenos
committed
#include "llvm/CodeGen/LiveVariables.h"
#include "llvm/CodeGen/MachineFrameInfo.h"
#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng
committed
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
Alkis Evlogimenos
committed
#include "llvm/CodeGen/Passes.h"
#include "llvm/Target/TargetRegisterInfo.h"
Alkis Evlogimenos
committed
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
Evan Cheng
committed
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/STLExtras.h"
Lang Hames
committed
#include <limits>
Alkis Evlogimenos
committed
using namespace llvm;
// Hidden options for help debugging.
static cl::opt<bool> DisableReMat("disable-rematerialization",
cl::init(false), cl::Hidden);
static cl::opt<bool> EnableFastSpilling("fast-spill",
cl::init(false), cl::Hidden);
Evan Cheng
committed
STATISTIC(numIntervals , "Number of original intervals");
STATISTIC(numFolds , "Number of loads/stores folded into instructions");
STATISTIC(numSplits , "Number of intervals split");
static RegisterPass<LiveIntervals> X("liveintervals", "Live Interval Analysis");
Alkis Evlogimenos
committed
Chris Lattner
committed
void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
Dan Gohman
committed
AU.addRequired<AliasAnalysis>();
AU.addPreserved<AliasAnalysis>();
AU.addPreserved<LiveVariables>();
AU.addPreservedID(MachineLoopInfoID);
AU.addPreservedID(MachineDominatorsID);
if (!StrongPHIElim) {
AU.addPreservedID(PHIEliminationID);
AU.addRequiredID(PHIEliminationID);
}
AU.addRequiredID(TwoAddressInstructionPassID);
AU.addPreserved<ProcessImplicitDefs>();
AU.addRequired<ProcessImplicitDefs>();
AU.addPreserved<SlotIndexes>();
AU.addRequiredTransitive<SlotIndexes>();
MachineFunctionPass::getAnalysisUsage(AU);
Alkis Evlogimenos
committed
}
Chris Lattner
committed
void LiveIntervals::releaseMemory() {
Owen Anderson
committed
// Free the live intervals themselves.
for (DenseMap<unsigned, LiveInterval*>::iterator I = r2iMap_.begin(),
Bob Wilson
committed
E = r2iMap_.end(); I != E; ++I)
Owen Anderson
committed
delete I->second;
// Release VNInfo memroy regions after all VNInfo objects are dtor'd.
Benjamin Kramer
committed
VNInfoAllocator.DestroyAll();
Evan Cheng
committed
while (!CloneMIs.empty()) {
MachineInstr *MI = CloneMIs.back();
CloneMIs.pop_back();
mf_->DeleteMachineInstr(MI);
}
}
Owen Anderson
committed
/// runOnMachineFunction - Register allocate the whole function
///
bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
mf_ = &fn;
mri_ = &mf_->getRegInfo();
tm_ = &fn.getTarget();
tri_ = tm_->getRegisterInfo();
tii_ = tm_->getInstrInfo();
Dan Gohman
committed
aa_ = &getAnalysis<AliasAnalysis>();
Owen Anderson
committed
lv_ = &getAnalysis<LiveVariables>();
Owen Anderson
committed
allocatableRegs_ = tri_->getAllocatableSet(fn);
Alkis Evlogimenos
committed
Alkis Evlogimenos
committed
Alkis Evlogimenos
committed
}
void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
OS << "********** INTERVALS **********\n";
for (const_iterator I = begin(), E = end(); I != E; ++I) {
I->second->print(OS, tri_);
OS << "\n";
Evan Cheng
committed
printInstrs(OS);
}
void LiveIntervals::printInstrs(raw_ostream &OS) const {
OS << "********** MACHINEINSTRS **********\n";
for (MachineFunction::iterator mbbi = mf_->begin(), mbbe = mf_->end();
mbbi != mbbe; ++mbbi) {
OS << "BB#" << mbbi->getNumber()
<< ":\t\t# derived from " << mbbi->getName() << "\n";
for (MachineBasicBlock::iterator mii = mbbi->begin(),
mie = mbbi->end(); mii != mie; ++mii) {
if (mii->isDebugValue())
else
OS << getInstructionIndex(mii) << '\t' << *mii;
Evan Cheng
committed
void LiveIntervals::dumpInstrs() const {
Evan Cheng
committed
}
Jakob Stoklund Olesen
committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
bool LiveIntervals::conflictsWithPhysReg(const LiveInterval &li,
VirtRegMap &vrm, unsigned reg) {
// We don't handle fancy stuff crossing basic block boundaries
if (li.ranges.size() != 1)
return true;
const LiveRange &range = li.ranges.front();
SlotIndex idx = range.start.getBaseIndex();
SlotIndex end = range.end.getPrevSlot().getBaseIndex().getNextIndex();
// Skip deleted instructions
MachineInstr *firstMI = getInstructionFromIndex(idx);
while (!firstMI && idx != end) {
idx = idx.getNextIndex();
firstMI = getInstructionFromIndex(idx);
}
if (!firstMI)
return false;
// Find last instruction in range
SlotIndex lastIdx = end.getPrevIndex();
MachineInstr *lastMI = getInstructionFromIndex(lastIdx);
while (!lastMI && lastIdx != idx) {
lastIdx = lastIdx.getPrevIndex();
lastMI = getInstructionFromIndex(lastIdx);
}
if (!lastMI)
return false;
// Range cannot cross basic block boundaries or terminators
MachineBasicBlock *MBB = firstMI->getParent();
if (MBB != lastMI->getParent() || lastMI->getDesc().isTerminator())
return true;
MachineBasicBlock::const_iterator E = lastMI;
++E;
for (MachineBasicBlock::const_iterator I = firstMI; I != E; ++I) {
const MachineInstr &MI = *I;
// Allow copies to and from li.reg
unsigned SrcReg, DstReg, SrcSubReg, DstSubReg;
if (tii_->isMoveInstr(MI, SrcReg, DstReg, SrcSubReg, DstSubReg))
if (SrcReg == li.reg || DstReg == li.reg)
continue;
// Check for operands using reg
for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Loading
Loading full blame...