//===---------- SplitKit.cpp - Toolkit for splitting live ranges ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the SplitAnalysis class as well as mutator functions for
// live range splitting.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "splitter"
#include "SplitKit.h"
#include "VirtRegMap.h"
#include "llvm/CodeGen/CalcSpillWeights.h"
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetInstrInfo.h"
#include "llvm/Target/TargetMachine.h"
using namespace llvm;
static cl::opt<bool>
AllowSplit("spiller-splits-edges",
cl::desc("Allow critical edge splitting during spilling"));
//===----------------------------------------------------------------------===//
// Split Analysis
//===----------------------------------------------------------------------===//
SplitAnalysis::SplitAnalysis(const MachineFunction &mf,
const LiveIntervals &lis,
const MachineLoopInfo &mli)
: mf_(mf),
lis_(lis),
loops_(mli),
tii_(*mf.getTarget().getInstrInfo()),
curli_(0) {}
void SplitAnalysis::clear() {
usingInstrs_.clear();
usingBlocks_.clear();
usingLoops_.clear();
curli_ = 0;
}
bool SplitAnalysis::canAnalyzeBranch(const MachineBasicBlock *MBB) {
MachineBasicBlock *T, *F;
SmallVector<MachineOperand, 4> Cond;
return !tii_.AnalyzeBranch(const_cast<MachineBasicBlock&>(*MBB), T, F, Cond);
}
/// analyzeUses - Count instructions, basic blocks, and loops using curli.
void SplitAnalysis::analyzeUses() {
const MachineRegisterInfo &MRI = mf_.getRegInfo();
for (MachineRegisterInfo::reg_iterator I = MRI.reg_begin(curli_->reg);
MachineInstr *MI = I.skipInstruction();) {
if (MI->isDebugValue() || !usingInstrs_.insert(MI))
continue;
MachineBasicBlock *MBB = MI->getParent();
if (usingBlocks_[MBB]++)
continue;
if (MachineLoop *Loop = loops_.getLoopFor(MBB))
usingLoops_[Loop]++;
}
DEBUG(dbgs() << " counted "
<< usingInstrs_.size() << " instrs, "
<< usingBlocks_.size() << " blocks, "
<< usingLoops_.size() << " loops.\n");
}
/// removeUse - Update statistics by noting that MI no longer uses curli.
void SplitAnalysis::removeUse(const MachineInstr *MI) {
if (!usingInstrs_.erase(MI))
return;
// Decrement MBB count.
const MachineBasicBlock *MBB = MI->getParent();
BlockCountMap::iterator bi = usingBlocks_.find(MBB);
assert(bi != usingBlocks_.end() && "MBB missing");
assert(bi->second && "0 count in map");
if (--bi->second)
return;
// No more uses in MBB.
usingBlocks_.erase(bi);
// Decrement loop count.
MachineLoop *Loop = loops_.getLoopFor(MBB);
if (!Loop)
return;
LoopCountMap::iterator li = usingLoops_.find(Loop);
assert(li != usingLoops_.end() && "Loop missing");
assert(li->second && "0 count in map");
if (--li->second)
return;
// No more blocks in Loop.
usingLoops_.erase(li);
}
// Get three sets of basic blocks surrounding a loop: Blocks inside the loop,
// predecessor blocks, and exit blocks.
void SplitAnalysis::getLoopBlocks