2009-11-26 00:32:21 +00:00
|
|
|
//===-- TailDuplication.cpp - Duplicate blocks into predecessors' tails ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass duplicates basic blocks ending in unconditional branches into
|
|
|
|
// the tails of their predecessors.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-02-12 18:09:18 +00:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
2009-11-26 00:32:21 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2010-07-10 22:42:59 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2009-12-03 08:43:53 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineSSAUpdater.h"
|
2012-05-30 00:42:39 +00:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2009-11-26 00:32:21 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-12-07 10:15:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-11-26 00:32:21 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2009-11-26 00:32:21 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "tailduplication"
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
STATISTIC(NumTails , "Number of tails duplicated");
|
2009-11-26 00:32:21 +00:00
|
|
|
STATISTIC(NumTailDups , "Number of tail duplicated blocks");
|
|
|
|
STATISTIC(NumInstrDups , "Additional instructions due to tail duplication");
|
|
|
|
STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
|
2011-06-08 14:13:31 +00:00
|
|
|
STATISTIC(NumAddedPHIs , "Number of phis added");
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
// Heuristic for tail duplication.
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
TailDuplicateSize("tail-dup-size",
|
|
|
|
cl::desc("Maximum instructions to consider tail duplicating"),
|
|
|
|
cl::init(2), cl::Hidden);
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
TailDupVerify("tail-dup-verify",
|
|
|
|
cl::desc("Verify sanity of PHI instructions during taildup"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden);
|
|
|
|
|
2009-12-04 19:09:10 +00:00
|
|
|
typedef std::vector<std::pair<MachineBasicBlock*,unsigned> > AvailableValsTy;
|
2009-12-03 08:43:53 +00:00
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
namespace {
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Perform tail duplication.
|
2009-11-26 21:38:41 +00:00
|
|
|
class TailDuplicatePass : public MachineFunctionPass {
|
2009-11-26 00:32:21 +00:00
|
|
|
const TargetInstrInfo *TII;
|
2012-05-30 00:42:39 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
2014-02-12 18:09:18 +00:00
|
|
|
const MachineBranchProbabilityInfo *MBPI;
|
2009-11-26 00:32:21 +00:00
|
|
|
MachineModuleInfo *MMI;
|
2009-12-03 08:43:53 +00:00
|
|
|
MachineRegisterInfo *MRI;
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<RegScavenger> RS;
|
2012-02-08 21:22:30 +00:00
|
|
|
bool PreRegAlloc;
|
2009-12-03 08:43:53 +00:00
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
// A list of virtual registers for which to update SSA form.
|
2009-12-03 08:43:53 +00:00
|
|
|
SmallVector<unsigned, 16> SSAUpdateVRs;
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
// For each virtual register in SSAUpdateVals keep a list of source virtual
|
|
|
|
// registers.
|
2009-12-03 08:43:53 +00:00
|
|
|
DenseMap<unsigned, AvailableValsTy> SSAUpdateVals;
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
2012-02-08 21:22:30 +00:00
|
|
|
explicit TailDuplicatePass() :
|
|
|
|
MachineFunctionPass(ID), PreRegAlloc(false) {}
|
2009-11-26 00:32:21 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2009-11-26 00:32:21 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2014-02-12 18:09:18 +00:00
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
private:
|
2009-12-04 19:09:10 +00:00
|
|
|
void AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
|
|
|
|
MachineBasicBlock *BB);
|
2009-12-04 09:42:45 +00:00
|
|
|
void ProcessPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
|
|
|
|
MachineBasicBlock *PredBB,
|
2009-12-07 10:15:19 +00:00
|
|
|
DenseMap<unsigned, unsigned> &LocalVRMap,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<std::pair<unsigned,unsigned> > &Copies,
|
2011-06-09 23:22:56 +00:00
|
|
|
const DenseSet<unsigned> &UsedByPhi,
|
|
|
|
bool Remove);
|
2009-12-04 09:42:45 +00:00
|
|
|
void DuplicateInstruction(MachineInstr *MI,
|
|
|
|
MachineBasicBlock *TailBB,
|
|
|
|
MachineBasicBlock *PredBB,
|
|
|
|
MachineFunction &MF,
|
2011-06-09 22:53:47 +00:00
|
|
|
DenseMap<unsigned, unsigned> &LocalVRMap,
|
|
|
|
const DenseSet<unsigned> &UsedByPhi);
|
2009-12-07 10:15:19 +00:00
|
|
|
void UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
2009-12-07 10:15:19 +00:00
|
|
|
SmallSetVector<MachineBasicBlock*, 8> &Succs);
|
2009-11-26 00:32:21 +00:00
|
|
|
bool TailDuplicateBlocks(MachineFunction &MF);
|
2011-06-09 19:54:42 +00:00
|
|
|
bool shouldTailDuplicate(const MachineFunction &MF,
|
2011-06-23 03:41:29 +00:00
|
|
|
bool IsSimple, MachineBasicBlock &TailBB);
|
2011-06-20 04:16:35 +00:00
|
|
|
bool isSimpleBB(MachineBasicBlock *TailBB);
|
2011-06-24 15:50:56 +00:00
|
|
|
bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
|
2011-06-24 15:47:41 +00:00
|
|
|
bool duplicateSimpleBB(MachineBasicBlock *TailBB,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
2011-06-20 04:16:35 +00:00
|
|
|
const DenseSet<unsigned> &RegsUsedByPhi,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineInstr *> &Copies);
|
2011-07-04 01:21:42 +00:00
|
|
|
bool TailDuplicate(MachineBasicBlock *TailBB,
|
|
|
|
bool IsSimple,
|
|
|
|
MachineFunction &MF,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
|
|
|
SmallVectorImpl<MachineInstr *> &Copies);
|
2011-07-04 01:21:42 +00:00
|
|
|
bool TailDuplicateAndUpdate(MachineBasicBlock *MBB,
|
|
|
|
bool IsSimple,
|
|
|
|
MachineFunction &MF);
|
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
void RemoveDeadBlock(MachineBasicBlock *MBB);
|
|
|
|
};
|
|
|
|
|
2009-11-26 21:38:41 +00:00
|
|
|
char TailDuplicatePass::ID = 0;
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2009-11-26 00:32:21 +00:00
|
|
|
|
2012-02-08 21:23:13 +00:00
|
|
|
char &llvm::TailDuplicateID = TailDuplicatePass::ID;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(TailDuplicatePass, "tailduplication", "Tail Duplication",
|
|
|
|
false, false)
|
2009-11-26 00:32:21 +00:00
|
|
|
|
2009-11-26 21:38:41 +00:00
|
|
|
bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) {
|
2014-03-31 17:43:35 +00:00
|
|
|
if (skipOptnoneFunction(*MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
2014-08-05 02:39:49 +00:00
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
2009-12-03 08:43:53 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
2009-11-26 00:32:21 +00:00
|
|
|
MMI = getAnalysisIfAvailable<MachineModuleInfo>();
|
2014-02-12 18:09:18 +00:00
|
|
|
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
|
|
|
|
|
2012-02-08 21:22:30 +00:00
|
|
|
PreRegAlloc = MRI->isSSA();
|
2012-06-06 13:53:41 +00:00
|
|
|
RS.reset();
|
2012-05-30 00:42:39 +00:00
|
|
|
if (MRI->tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF))
|
2012-06-06 13:53:41 +00:00
|
|
|
RS.reset(new RegScavenger());
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
bool MadeChange = false;
|
2010-01-15 19:59:57 +00:00
|
|
|
while (TailDuplicateBlocks(MF))
|
|
|
|
MadeChange = true;
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2014-02-12 18:09:18 +00:00
|
|
|
void TailDuplicatePass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
static void VerifyPHIs(MachineFunction &MF, bool CheckExtra) {
|
|
|
|
for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ++I) {
|
|
|
|
MachineBasicBlock *MBB = I;
|
|
|
|
SmallSetVector<MachineBasicBlock*, 8> Preds(MBB->pred_begin(),
|
|
|
|
MBB->pred_end());
|
|
|
|
MachineBasicBlock::iterator MI = MBB->begin();
|
|
|
|
while (MI != MBB->end()) {
|
2010-02-09 19:54:29 +00:00
|
|
|
if (!MI->isPHI())
|
2009-12-07 10:15:19 +00:00
|
|
|
break;
|
|
|
|
for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(),
|
|
|
|
PE = Preds.end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *PredBB = *PI;
|
|
|
|
bool Found = false;
|
|
|
|
for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
|
|
|
|
MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB();
|
|
|
|
if (PHIBB == PredBB) {
|
|
|
|
Found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Found) {
|
2010-01-05 01:25:15 +00:00
|
|
|
dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI;
|
|
|
|
dbgs() << " missing input from predecessor BB#"
|
2009-12-07 10:15:19 +00:00
|
|
|
<< PredBB->getNumber() << '\n';
|
2014-04-14 00:51:57 +00:00
|
|
|
llvm_unreachable(nullptr);
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) {
|
|
|
|
MachineBasicBlock *PHIBB = MI->getOperand(i+1).getMBB();
|
|
|
|
if (CheckExtra && !Preds.count(PHIBB)) {
|
2010-01-05 01:25:15 +00:00
|
|
|
dbgs() << "Warning: malformed PHI in BB#" << MBB->getNumber()
|
2009-12-07 10:15:19 +00:00
|
|
|
<< ": " << *MI;
|
2010-01-05 01:25:15 +00:00
|
|
|
dbgs() << " extra input from predecessor BB#"
|
2009-12-07 10:15:19 +00:00
|
|
|
<< PHIBB->getNumber() << '\n';
|
2014-04-14 00:51:57 +00:00
|
|
|
llvm_unreachable(nullptr);
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
|
|
|
if (PHIBB->getNumber() < 0) {
|
2010-01-05 01:25:15 +00:00
|
|
|
dbgs() << "Malformed PHI in BB#" << MBB->getNumber() << ": " << *MI;
|
|
|
|
dbgs() << " non-existing BB#" << PHIBB->getNumber() << '\n';
|
2014-04-14 00:51:57 +00:00
|
|
|
llvm_unreachable(nullptr);
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++MI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Tail duplicate the block and cleanup.
|
2011-07-04 01:21:42 +00:00
|
|
|
bool
|
|
|
|
TailDuplicatePass::TailDuplicateAndUpdate(MachineBasicBlock *MBB,
|
|
|
|
bool IsSimple,
|
|
|
|
MachineFunction &MF) {
|
|
|
|
// Save the successors list.
|
|
|
|
SmallSetVector<MachineBasicBlock*, 8> Succs(MBB->succ_begin(),
|
|
|
|
MBB->succ_end());
|
|
|
|
|
|
|
|
SmallVector<MachineBasicBlock*, 8> TDBBs;
|
|
|
|
SmallVector<MachineInstr*, 16> Copies;
|
|
|
|
if (!TailDuplicate(MBB, IsSimple, MF, TDBBs, Copies))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++NumTails;
|
|
|
|
|
|
|
|
SmallVector<MachineInstr*, 8> NewPHIs;
|
|
|
|
MachineSSAUpdater SSAUpdate(MF, &NewPHIs);
|
|
|
|
|
|
|
|
// TailBB's immediate successors are now successors of those predecessors
|
|
|
|
// which duplicated TailBB. Add the predecessors as sources to the PHI
|
|
|
|
// instructions.
|
|
|
|
bool isDead = MBB->pred_empty() && !MBB->hasAddressTaken();
|
|
|
|
if (PreRegAlloc)
|
|
|
|
UpdateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
|
|
|
|
|
|
|
|
// If it is dead, remove it.
|
|
|
|
if (isDead) {
|
|
|
|
NumInstrDups -= MBB->size();
|
|
|
|
RemoveDeadBlock(MBB);
|
|
|
|
++NumDeadBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update SSA form.
|
|
|
|
if (!SSAUpdateVRs.empty()) {
|
|
|
|
for (unsigned i = 0, e = SSAUpdateVRs.size(); i != e; ++i) {
|
|
|
|
unsigned VReg = SSAUpdateVRs[i];
|
|
|
|
SSAUpdate.Initialize(VReg);
|
|
|
|
|
|
|
|
// If the original definition is still around, add it as an available
|
|
|
|
// value.
|
|
|
|
MachineInstr *DefMI = MRI->getVRegDef(VReg);
|
2014-04-14 00:51:57 +00:00
|
|
|
MachineBasicBlock *DefBB = nullptr;
|
2011-07-04 01:21:42 +00:00
|
|
|
if (DefMI) {
|
|
|
|
DefBB = DefMI->getParent();
|
|
|
|
SSAUpdate.AddAvailableValue(DefBB, VReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new vregs as available values.
|
|
|
|
DenseMap<unsigned, AvailableValsTy>::iterator LI =
|
|
|
|
SSAUpdateVals.find(VReg);
|
|
|
|
for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
|
|
|
|
MachineBasicBlock *SrcBB = LI->second[j].first;
|
|
|
|
unsigned SrcReg = LI->second[j].second;
|
|
|
|
SSAUpdate.AddAvailableValue(SrcBB, SrcReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite uses that are outside of the original def's block.
|
|
|
|
MachineRegisterInfo::use_iterator UI = MRI->use_begin(VReg);
|
|
|
|
while (UI != MRI->use_end()) {
|
2014-03-13 23:12:04 +00:00
|
|
|
MachineOperand &UseMO = *UI;
|
|
|
|
MachineInstr *UseMI = UseMO.getParent();
|
2011-07-04 01:21:42 +00:00
|
|
|
++UI;
|
|
|
|
if (UseMI->isDebugValue()) {
|
|
|
|
// SSAUpdate can replace the use with an undef. That creates
|
|
|
|
// a debug instruction that is a kill.
|
|
|
|
// FIXME: Should it SSAUpdate job to delete debug instructions
|
|
|
|
// instead of replacing the use with undef?
|
|
|
|
UseMI->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (UseMI->getParent() == DefBB && !UseMI->isPHI())
|
|
|
|
continue;
|
|
|
|
SSAUpdate.RewriteUse(UseMO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SSAUpdateVRs.clear();
|
|
|
|
SSAUpdateVals.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eliminate some of the copies inserted by tail duplication to maintain
|
|
|
|
// SSA form.
|
|
|
|
for (unsigned i = 0, e = Copies.size(); i != e; ++i) {
|
|
|
|
MachineInstr *Copy = Copies[i];
|
|
|
|
if (!Copy->isCopy())
|
|
|
|
continue;
|
|
|
|
unsigned Dst = Copy->getOperand(0).getReg();
|
|
|
|
unsigned Src = Copy->getOperand(1).getReg();
|
2012-05-20 18:42:51 +00:00
|
|
|
if (MRI->hasOneNonDBGUse(Src) &&
|
|
|
|
MRI->constrainRegClass(Src, MRI->getRegClass(Dst))) {
|
2011-07-04 01:21:42 +00:00
|
|
|
// Copy is the only use. Do trivial copy propagation here.
|
|
|
|
MRI->replaceRegWith(Dst, Src);
|
|
|
|
Copy->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NewPHIs.size())
|
|
|
|
NumAddedPHIs += NewPHIs.size();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Look for small blocks that are unconditionally branched to and do not fall
|
|
|
|
/// through. Tail-duplicate their instructions into their predecessors to
|
|
|
|
/// eliminate (dynamic) branches.
|
2009-11-26 21:38:41 +00:00
|
|
|
bool TailDuplicatePass::TailDuplicateBlocks(MachineFunction &MF) {
|
2009-11-26 00:32:21 +00:00
|
|
|
bool MadeChange = false;
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
if (PreRegAlloc && TailDupVerify) {
|
2010-01-05 01:25:15 +00:00
|
|
|
DEBUG(dbgs() << "\n*** Before tail-duplicating\n");
|
2009-12-07 10:15:19 +00:00
|
|
|
VerifyPHIs(MF, true);
|
|
|
|
}
|
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
|
|
|
|
MachineBasicBlock *MBB = I++;
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
if (NumTails == TailDupLimit)
|
|
|
|
break;
|
|
|
|
|
2011-07-04 01:21:42 +00:00
|
|
|
bool IsSimple = isSimpleBB(MBB);
|
2009-12-07 10:15:19 +00:00
|
|
|
|
2011-07-04 01:21:42 +00:00
|
|
|
if (!shouldTailDuplicate(MF, IsSimple, *MBB))
|
2011-07-04 00:13:36 +00:00
|
|
|
continue;
|
2009-12-07 10:15:19 +00:00
|
|
|
|
2011-07-04 01:21:42 +00:00
|
|
|
MadeChange |= TailDuplicateAndUpdate(MBB, IsSimple, MF);
|
2009-11-26 00:32:21 +00:00
|
|
|
}
|
2011-07-04 00:13:36 +00:00
|
|
|
|
2011-07-04 01:21:42 +00:00
|
|
|
if (PreRegAlloc && TailDupVerify)
|
|
|
|
VerifyPHIs(MF, false);
|
2009-12-03 08:43:53 +00:00
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
2009-12-03 08:43:53 +00:00
|
|
|
static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB,
|
|
|
|
const MachineRegisterInfo *MRI) {
|
2014-03-17 19:36:09 +00:00
|
|
|
for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
|
|
|
|
if (UseMI.isDebugValue())
|
2011-06-17 13:59:43 +00:00
|
|
|
continue;
|
2014-03-17 19:36:09 +00:00
|
|
|
if (UseMI.getParent() != BB)
|
2009-12-03 08:43:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) {
|
|
|
|
for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2)
|
|
|
|
if (MI->getOperand(i+1).getMBB() == SrcBB)
|
|
|
|
return i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-09 22:53:47 +00:00
|
|
|
|
|
|
|
// Remember which registers are used by phis in this block. This is
|
|
|
|
// used to determine which registers are liveout while modifying the
|
|
|
|
// block (which is why we need to copy the information).
|
|
|
|
static void getRegsUsedByPHIs(const MachineBasicBlock &BB,
|
2011-06-10 21:01:53 +00:00
|
|
|
DenseSet<unsigned> *UsedByPhi) {
|
2014-04-30 22:17:38 +00:00
|
|
|
for (const auto &MI : BB) {
|
2011-06-09 22:53:47 +00:00
|
|
|
if (!MI.isPHI())
|
|
|
|
break;
|
|
|
|
for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
|
|
|
|
unsigned SrcReg = MI.getOperand(i).getReg();
|
|
|
|
UsedByPhi->insert(SrcReg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Add a definition and source virtual registers pair for SSA update.
|
2009-12-04 19:09:10 +00:00
|
|
|
void TailDuplicatePass::AddSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
|
|
|
|
MachineBasicBlock *BB) {
|
|
|
|
DenseMap<unsigned, AvailableValsTy>::iterator LI= SSAUpdateVals.find(OrigReg);
|
2009-12-03 08:43:53 +00:00
|
|
|
if (LI != SSAUpdateVals.end())
|
2009-12-04 19:09:10 +00:00
|
|
|
LI->second.push_back(std::make_pair(BB, NewReg));
|
2009-12-03 08:43:53 +00:00
|
|
|
else {
|
|
|
|
AvailableValsTy Vals;
|
2009-12-04 19:09:10 +00:00
|
|
|
Vals.push_back(std::make_pair(BB, NewReg));
|
2009-12-03 08:43:53 +00:00
|
|
|
SSAUpdateVals.insert(std::make_pair(OrigReg, Vals));
|
|
|
|
SSAUpdateVRs.push_back(OrigReg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Process PHI node in TailBB by turning it into a copy in PredBB. Remember the
|
|
|
|
/// source register that's contributed by PredBB and update SSA update map.
|
2013-07-14 06:12:01 +00:00
|
|
|
void TailDuplicatePass::ProcessPHI(
|
|
|
|
MachineInstr *MI, MachineBasicBlock *TailBB, MachineBasicBlock *PredBB,
|
|
|
|
DenseMap<unsigned, unsigned> &LocalVRMap,
|
|
|
|
SmallVectorImpl<std::pair<unsigned, unsigned> > &Copies,
|
|
|
|
const DenseSet<unsigned> &RegsUsedByPhi, bool Remove) {
|
2009-12-04 09:42:45 +00:00
|
|
|
unsigned DefReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned SrcOpIdx = getPHISrcRegOpIdx(MI, PredBB);
|
|
|
|
assert(SrcOpIdx && "Unable to find matching PHI source?");
|
|
|
|
unsigned SrcReg = MI->getOperand(SrcOpIdx).getReg();
|
2009-12-07 10:15:19 +00:00
|
|
|
const TargetRegisterClass *RC = MRI->getRegClass(DefReg);
|
2009-12-04 09:42:45 +00:00
|
|
|
LocalVRMap.insert(std::make_pair(DefReg, SrcReg));
|
2009-12-07 10:15:19 +00:00
|
|
|
|
|
|
|
// Insert a copy from source to the end of the block. The def register is the
|
|
|
|
// available value liveout of the block.
|
|
|
|
unsigned NewDef = MRI->createVirtualRegister(RC);
|
|
|
|
Copies.push_back(std::make_pair(NewDef, SrcReg));
|
2011-06-09 22:53:47 +00:00
|
|
|
if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg))
|
2009-12-07 10:15:19 +00:00
|
|
|
AddSSAUpdateEntry(DefReg, NewDef, PredBB);
|
2009-12-04 09:42:45 +00:00
|
|
|
|
2011-06-09 23:22:56 +00:00
|
|
|
if (!Remove)
|
|
|
|
return;
|
|
|
|
|
2009-12-04 09:42:45 +00:00
|
|
|
// Remove PredBB from the PHI node.
|
|
|
|
MI->RemoveOperand(SrcOpIdx+1);
|
|
|
|
MI->RemoveOperand(SrcOpIdx);
|
|
|
|
if (MI->getNumOperands() == 1)
|
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Duplicate a TailBB instruction to PredBB and update
|
2009-12-04 09:42:45 +00:00
|
|
|
/// the source operands due to earlier PHI translation.
|
|
|
|
void TailDuplicatePass::DuplicateInstruction(MachineInstr *MI,
|
|
|
|
MachineBasicBlock *TailBB,
|
|
|
|
MachineBasicBlock *PredBB,
|
|
|
|
MachineFunction &MF,
|
2011-06-09 22:53:47 +00:00
|
|
|
DenseMap<unsigned, unsigned> &LocalVRMap,
|
|
|
|
const DenseSet<unsigned> &UsedByPhi) {
|
2010-01-06 23:47:07 +00:00
|
|
|
MachineInstr *NewMI = TII->duplicate(MI, MF);
|
2009-12-04 09:42:45 +00:00
|
|
|
for (unsigned i = 0, e = NewMI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = NewMI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 02:58:51 +00:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
2009-12-04 09:42:45 +00:00
|
|
|
continue;
|
|
|
|
if (MO.isDef()) {
|
|
|
|
const TargetRegisterClass *RC = MRI->getRegClass(Reg);
|
|
|
|
unsigned NewReg = MRI->createVirtualRegister(RC);
|
|
|
|
MO.setReg(NewReg);
|
|
|
|
LocalVRMap.insert(std::make_pair(Reg, NewReg));
|
2011-06-09 22:53:47 +00:00
|
|
|
if (isDefLiveOut(Reg, TailBB, MRI) || UsedByPhi.count(Reg))
|
2009-12-04 19:09:10 +00:00
|
|
|
AddSSAUpdateEntry(Reg, NewReg, PredBB);
|
2009-12-04 09:42:45 +00:00
|
|
|
} else {
|
|
|
|
DenseMap<unsigned, unsigned>::iterator VI = LocalVRMap.find(Reg);
|
2012-05-20 18:42:51 +00:00
|
|
|
if (VI != LocalVRMap.end()) {
|
2009-12-04 09:42:45 +00:00
|
|
|
MO.setReg(VI->second);
|
2015-05-07 21:48:26 +00:00
|
|
|
// Clear any kill flags from this operand. The new register could have
|
|
|
|
// uses after this one, so kills are not valid here.
|
|
|
|
MO.setIsKill(false);
|
2012-05-20 18:42:51 +00:00
|
|
|
MRI->constrainRegClass(VI->second, MRI->getRegClass(Reg));
|
|
|
|
}
|
2009-12-04 09:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-20 07:51:58 +00:00
|
|
|
PredBB->insert(PredBB->instr_end(), NewMI);
|
2009-12-04 09:42:45 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// After FromBB is tail duplicated into its predecessor blocks, the successors
|
|
|
|
/// have gained new predecessors. Update the PHI instructions in them
|
|
|
|
/// accordingly.
|
2009-12-07 10:15:19 +00:00
|
|
|
void
|
|
|
|
TailDuplicatePass::UpdateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
2009-12-04 09:42:45 +00:00
|
|
|
SmallSetVector<MachineBasicBlock*,8> &Succs) {
|
|
|
|
for (SmallSetVector<MachineBasicBlock*, 8>::iterator SI = Succs.begin(),
|
|
|
|
SE = Succs.end(); SI != SE; ++SI) {
|
|
|
|
MachineBasicBlock *SuccBB = *SI;
|
|
|
|
for (MachineBasicBlock::iterator II = SuccBB->begin(), EE = SuccBB->end();
|
|
|
|
II != EE; ++II) {
|
2010-02-09 19:54:29 +00:00
|
|
|
if (!II->isPHI())
|
2009-12-04 09:42:45 +00:00
|
|
|
break;
|
2012-12-20 18:08:06 +00:00
|
|
|
MachineInstrBuilder MIB(*FromBB->getParent(), II);
|
2009-12-07 10:15:19 +00:00
|
|
|
unsigned Idx = 0;
|
2009-12-04 09:42:45 +00:00
|
|
|
for (unsigned i = 1, e = II->getNumOperands(); i != e; i += 2) {
|
2009-12-07 10:15:19 +00:00
|
|
|
MachineOperand &MO = II->getOperand(i+1);
|
|
|
|
if (MO.getMBB() == FromBB) {
|
|
|
|
Idx = i;
|
2009-12-04 09:42:45 +00:00
|
|
|
break;
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Idx != 0);
|
|
|
|
MachineOperand &MO0 = II->getOperand(Idx);
|
|
|
|
unsigned Reg = MO0.getReg();
|
|
|
|
if (isDead) {
|
|
|
|
// Folded into the previous BB.
|
|
|
|
// There could be duplicate phi source entries. FIXME: Should sdisel
|
|
|
|
// or earlier pass fixed this?
|
|
|
|
for (unsigned i = II->getNumOperands()-2; i != Idx; i -= 2) {
|
|
|
|
MachineOperand &MO = II->getOperand(i+1);
|
|
|
|
if (MO.getMBB() == FromBB) {
|
|
|
|
II->RemoveOperand(i+1);
|
|
|
|
II->RemoveOperand(i);
|
|
|
|
}
|
|
|
|
}
|
2010-02-11 00:34:33 +00:00
|
|
|
} else
|
|
|
|
Idx = 0;
|
|
|
|
|
|
|
|
// If Idx is set, the operands at Idx and Idx+1 must be removed.
|
|
|
|
// We reuse the location to avoid expensive RemoveOperand calls.
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
DenseMap<unsigned,AvailableValsTy>::iterator LI=SSAUpdateVals.find(Reg);
|
|
|
|
if (LI != SSAUpdateVals.end()) {
|
|
|
|
// This register is defined in the tail block.
|
2009-12-04 09:42:45 +00:00
|
|
|
for (unsigned j = 0, ee = LI->second.size(); j != ee; ++j) {
|
2009-12-04 19:09:10 +00:00
|
|
|
MachineBasicBlock *SrcBB = LI->second[j].first;
|
2011-06-09 23:55:56 +00:00
|
|
|
// If we didn't duplicate a bb into a particular predecessor, we
|
|
|
|
// might still have added an entry to SSAUpdateVals to correcly
|
|
|
|
// recompute SSA. If that case, avoid adding a dummy extra argument
|
|
|
|
// this PHI.
|
|
|
|
if (!SrcBB->isSuccessor(SuccBB))
|
|
|
|
continue;
|
|
|
|
|
2009-12-04 19:09:10 +00:00
|
|
|
unsigned SrcReg = LI->second[j].second;
|
2010-02-11 00:34:33 +00:00
|
|
|
if (Idx != 0) {
|
|
|
|
II->getOperand(Idx).setReg(SrcReg);
|
|
|
|
II->getOperand(Idx+1).setMBB(SrcBB);
|
|
|
|
Idx = 0;
|
|
|
|
} else {
|
2012-12-20 18:08:06 +00:00
|
|
|
MIB.addReg(SrcReg).addMBB(SrcBB);
|
2010-02-11 00:34:33 +00:00
|
|
|
}
|
2009-12-04 09:42:45 +00:00
|
|
|
}
|
2009-12-07 10:15:19 +00:00
|
|
|
} else {
|
|
|
|
// Live in tail block, must also be live in predecessors.
|
|
|
|
for (unsigned j = 0, ee = TDBBs.size(); j != ee; ++j) {
|
|
|
|
MachineBasicBlock *SrcBB = TDBBs[j];
|
2010-02-11 00:34:33 +00:00
|
|
|
if (Idx != 0) {
|
|
|
|
II->getOperand(Idx).setReg(Reg);
|
|
|
|
II->getOperand(Idx+1).setMBB(SrcBB);
|
|
|
|
Idx = 0;
|
|
|
|
} else {
|
2012-12-20 18:08:06 +00:00
|
|
|
MIB.addReg(Reg).addMBB(SrcBB);
|
2010-02-11 00:34:33 +00:00
|
|
|
}
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
2009-12-04 09:42:45 +00:00
|
|
|
}
|
2010-02-11 00:34:33 +00:00
|
|
|
if (Idx != 0) {
|
|
|
|
II->RemoveOperand(Idx+1);
|
|
|
|
II->RemoveOperand(Idx);
|
|
|
|
}
|
2009-12-04 09:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Determine if it is profitable to duplicate this block.
|
2009-12-07 10:15:19 +00:00
|
|
|
bool
|
2011-06-09 19:54:42 +00:00
|
|
|
TailDuplicatePass::shouldTailDuplicate(const MachineFunction &MF,
|
2011-06-23 03:41:29 +00:00
|
|
|
bool IsSimple,
|
2011-06-09 19:54:42 +00:00
|
|
|
MachineBasicBlock &TailBB) {
|
|
|
|
// Only duplicate blocks that end with unconditional branches.
|
|
|
|
if (TailBB.canFallThrough())
|
|
|
|
return false;
|
|
|
|
|
2011-06-17 05:54:50 +00:00
|
|
|
// Don't try to tail-duplicate single-block loops.
|
|
|
|
if (TailBB.isSuccessor(&TailBB))
|
|
|
|
return false;
|
|
|
|
|
2011-06-09 19:54:42 +00:00
|
|
|
// Set the limit on the cost to duplicate. When optimizing for size,
|
2009-11-26 00:32:21 +00:00
|
|
|
// duplicate only one, because one branch instruction can be eliminated to
|
|
|
|
// compensate for the duplication.
|
|
|
|
unsigned MaxDuplicateCount;
|
2011-01-30 20:38:12 +00:00
|
|
|
if (TailDuplicateSize.getNumOccurrences() == 0 &&
|
2015-08-04 15:49:57 +00:00
|
|
|
// FIXME: Use Function::optForSize().
|
2015-02-14 01:44:41 +00:00
|
|
|
MF.getFunction()->hasFnAttribute(Attribute::OptimizeForSize))
|
2010-01-16 00:42:25 +00:00
|
|
|
MaxDuplicateCount = 1;
|
|
|
|
else
|
|
|
|
MaxDuplicateCount = TailDuplicateSize;
|
|
|
|
|
2011-06-17 05:54:50 +00:00
|
|
|
// If the target has hardware branch prediction that can handle indirect
|
|
|
|
// branches, duplicating them can often make them predictable when there
|
|
|
|
// are common paths through the code. The limit needs to be high enough
|
|
|
|
// to allow undoing the effects of tail merging and other optimizations
|
|
|
|
// that rearrange the predecessors of the indirect branch.
|
|
|
|
|
2011-07-04 01:21:42 +00:00
|
|
|
bool HasIndirectbr = false;
|
|
|
|
if (!TailBB.empty())
|
2011-12-07 07:15:52 +00:00
|
|
|
HasIndirectbr = TailBB.back().isIndirectBranch();
|
2011-07-04 01:21:42 +00:00
|
|
|
|
|
|
|
if (HasIndirectbr && PreRegAlloc)
|
|
|
|
MaxDuplicateCount = 20;
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
// Check the instructions in the block to determine whether tail-duplication
|
|
|
|
// is invalid or unlikely to be profitable.
|
2009-12-02 17:15:24 +00:00
|
|
|
unsigned InstrCount = 0;
|
2015-08-10 23:29:41 +00:00
|
|
|
for (MachineInstr &MI : TailBB) {
|
2009-11-26 00:32:21 +00:00
|
|
|
// Non-duplicable things shouldn't be tail-duplicated.
|
2015-08-10 23:29:41 +00:00
|
|
|
if (MI.isNotDuplicable())
|
2011-06-17 05:54:50 +00:00
|
|
|
return false;
|
|
|
|
|
2009-12-04 09:42:45 +00:00
|
|
|
// Do not duplicate 'return' instructions if this is a pre-regalloc run.
|
|
|
|
// A return may expand into a lot more instructions (e.g. reload of callee
|
|
|
|
// saved registers) after PEI.
|
2015-08-10 23:29:41 +00:00
|
|
|
if (PreRegAlloc && MI.isReturn())
|
2011-06-17 05:54:50 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Avoid duplicating calls before register allocation. Calls presents a
|
|
|
|
// barrier to register allocation so duplicating them may end up increasing
|
|
|
|
// spills.
|
2015-08-10 23:29:41 +00:00
|
|
|
if (PreRegAlloc && MI.isCall())
|
2011-06-17 05:54:50 +00:00
|
|
|
return false;
|
|
|
|
|
2015-08-10 23:29:41 +00:00
|
|
|
if (!MI.isPHI() && !MI.isDebugValue())
|
2009-12-02 17:15:24 +00:00
|
|
|
InstrCount += 1;
|
2011-06-17 05:54:50 +00:00
|
|
|
|
|
|
|
if (InstrCount > MaxDuplicateCount)
|
|
|
|
return false;
|
2009-11-26 00:32:21 +00:00
|
|
|
}
|
|
|
|
|
2011-07-04 01:21:42 +00:00
|
|
|
if (HasIndirectbr && PreRegAlloc)
|
2011-06-23 03:41:29 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (IsSimple)
|
2011-06-24 15:47:41 +00:00
|
|
|
return true;
|
2011-06-23 03:41:29 +00:00
|
|
|
|
|
|
|
if (!PreRegAlloc)
|
|
|
|
return true;
|
|
|
|
|
2011-06-24 15:50:56 +00:00
|
|
|
return canCompletelyDuplicateBB(TailBB);
|
2011-06-09 19:54:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// True if this BB has only one unconditional jump.
|
2011-06-20 04:16:35 +00:00
|
|
|
bool
|
|
|
|
TailDuplicatePass::isSimpleBB(MachineBasicBlock *TailBB) {
|
|
|
|
if (TailBB->succ_size() != 1)
|
|
|
|
return false;
|
2011-06-23 03:41:29 +00:00
|
|
|
if (TailBB->pred_empty())
|
|
|
|
return false;
|
2015-06-23 14:47:29 +00:00
|
|
|
MachineBasicBlock::iterator I = TailBB->getFirstNonDebugInstr();
|
|
|
|
if (I == TailBB->end())
|
2011-06-20 04:16:35 +00:00
|
|
|
return true;
|
2011-12-07 07:15:52 +00:00
|
|
|
return I->isUnconditionalBranch();
|
2011-06-20 04:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
bothUsedInPHI(const MachineBasicBlock &A,
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> SuccsB) {
|
2015-08-11 00:26:05 +00:00
|
|
|
for (MachineBasicBlock *BB : A.successors())
|
2011-06-20 04:16:35 +00:00
|
|
|
if (SuccsB.count(BB) && !BB->empty() && BB->begin()->isPHI())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-06-24 15:50:56 +00:00
|
|
|
TailDuplicatePass::canCompletelyDuplicateBB(MachineBasicBlock &BB) {
|
2015-08-11 00:26:05 +00:00
|
|
|
for (MachineBasicBlock *PredBB : BB.predecessors()) {
|
2011-06-24 15:50:56 +00:00
|
|
|
if (PredBB->succ_size() > 1)
|
|
|
|
return false;
|
2011-06-23 03:41:29 +00:00
|
|
|
|
2014-04-14 00:51:57 +00:00
|
|
|
MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
|
2011-06-20 04:16:35 +00:00
|
|
|
SmallVector<MachineOperand, 4> PredCond;
|
|
|
|
if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
|
|
|
|
return false;
|
2011-06-23 03:41:29 +00:00
|
|
|
|
2011-06-24 15:50:56 +00:00
|
|
|
if (!PredCond.empty())
|
2011-06-23 03:41:29 +00:00
|
|
|
return false;
|
2011-06-20 04:16:35 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-24 15:47:41 +00:00
|
|
|
bool
|
2011-06-20 04:16:35 +00:00
|
|
|
TailDuplicatePass::duplicateSimpleBB(MachineBasicBlock *TailBB,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
|
|
|
const DenseSet<unsigned> &UsedByPhi,
|
|
|
|
SmallVectorImpl<MachineInstr *> &Copies) {
|
2011-06-24 15:47:41 +00:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 8> Succs(TailBB->succ_begin(),
|
|
|
|
TailBB->succ_end());
|
2011-06-20 04:16:35 +00:00
|
|
|
SmallVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(),
|
|
|
|
TailBB->pred_end());
|
2011-06-24 15:47:41 +00:00
|
|
|
bool Changed = false;
|
2011-06-20 04:16:35 +00:00
|
|
|
for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(),
|
|
|
|
PE = Preds.end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *PredBB = *PI;
|
|
|
|
|
2011-06-24 15:47:41 +00:00
|
|
|
if (PredBB->getLandingPadSuccessor())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (bothUsedInPHI(*PredBB, Succs))
|
|
|
|
continue;
|
|
|
|
|
2014-04-14 00:51:57 +00:00
|
|
|
MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr;
|
2011-06-20 04:16:35 +00:00
|
|
|
SmallVector<MachineOperand, 4> PredCond;
|
2011-06-24 15:47:41 +00:00
|
|
|
if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
|
|
|
|
continue;
|
2011-06-20 04:16:35 +00:00
|
|
|
|
2011-06-24 15:47:41 +00:00
|
|
|
Changed = true;
|
2011-06-20 04:16:35 +00:00
|
|
|
DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
|
|
|
|
<< "From simple Succ: " << *TailBB);
|
|
|
|
|
|
|
|
MachineBasicBlock *NewTarget = *TailBB->succ_begin();
|
2014-03-02 12:27:27 +00:00
|
|
|
MachineBasicBlock *NextBB = std::next(MachineFunction::iterator(PredBB));
|
2011-06-20 04:16:35 +00:00
|
|
|
|
|
|
|
// Make PredFBB explicit.
|
|
|
|
if (PredCond.empty())
|
|
|
|
PredFBB = PredTBB;
|
|
|
|
|
|
|
|
// Make fall through explicit.
|
|
|
|
if (!PredTBB)
|
|
|
|
PredTBB = NextBB;
|
|
|
|
if (!PredFBB)
|
|
|
|
PredFBB = NextBB;
|
|
|
|
|
|
|
|
// Redirect
|
|
|
|
if (PredFBB == TailBB)
|
|
|
|
PredFBB = NewTarget;
|
|
|
|
if (PredTBB == TailBB)
|
|
|
|
PredTBB = NewTarget;
|
|
|
|
|
|
|
|
// Make the branch unconditional if possible
|
2011-06-20 14:11:42 +00:00
|
|
|
if (PredTBB == PredFBB) {
|
|
|
|
PredCond.clear();
|
2014-04-14 00:51:57 +00:00
|
|
|
PredFBB = nullptr;
|
2011-06-20 14:11:42 +00:00
|
|
|
}
|
2011-06-20 04:16:35 +00:00
|
|
|
|
|
|
|
// Avoid adding fall through branches.
|
|
|
|
if (PredFBB == NextBB)
|
2014-04-14 00:51:57 +00:00
|
|
|
PredFBB = nullptr;
|
|
|
|
if (PredTBB == NextBB && PredFBB == nullptr)
|
|
|
|
PredTBB = nullptr;
|
2011-06-20 04:16:35 +00:00
|
|
|
|
|
|
|
TII->RemoveBranch(*PredBB);
|
|
|
|
|
|
|
|
if (PredTBB)
|
|
|
|
TII->InsertBranch(*PredBB, PredTBB, PredFBB, PredCond, DebugLoc());
|
|
|
|
|
2014-02-12 18:09:18 +00:00
|
|
|
uint32_t Weight = MBPI->getEdgeWeight(PredBB, TailBB);
|
2011-06-20 04:16:35 +00:00
|
|
|
PredBB->removeSuccessor(TailBB);
|
2011-06-20 14:11:42 +00:00
|
|
|
unsigned NumSuccessors = PredBB->succ_size();
|
|
|
|
assert(NumSuccessors <= 1);
|
|
|
|
if (NumSuccessors == 0 || *PredBB->succ_begin() != NewTarget)
|
2014-02-12 18:09:18 +00:00
|
|
|
PredBB->addSuccessor(NewTarget, Weight);
|
2011-06-20 04:16:35 +00:00
|
|
|
|
|
|
|
TDBBs.push_back(PredBB);
|
|
|
|
}
|
2011-06-24 15:47:41 +00:00
|
|
|
return Changed;
|
2011-06-20 04:16:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// If it is profitable, duplicate TailBB's contents in each
|
2011-06-09 19:54:42 +00:00
|
|
|
/// of its predecessors.
|
|
|
|
bool
|
2011-07-04 01:21:42 +00:00
|
|
|
TailDuplicatePass::TailDuplicate(MachineBasicBlock *TailBB,
|
|
|
|
bool IsSimple,
|
|
|
|
MachineFunction &MF,
|
2013-07-14 04:42:23 +00:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
|
|
|
SmallVectorImpl<MachineInstr *> &Copies) {
|
2010-01-05 01:25:15 +00:00
|
|
|
DEBUG(dbgs() << "\n*** Tail-duplicating BB#" << TailBB->getNumber() << '\n');
|
2009-12-07 10:15:19 +00:00
|
|
|
|
2011-06-20 04:16:35 +00:00
|
|
|
DenseSet<unsigned> UsedByPhi;
|
|
|
|
getRegsUsedByPHIs(*TailBB, &UsedByPhi);
|
|
|
|
|
2011-06-24 15:47:41 +00:00
|
|
|
if (IsSimple)
|
|
|
|
return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi, Copies);
|
2011-06-20 04:16:35 +00:00
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
// Iterate through all the unique predecessors and tail-duplicate this
|
|
|
|
// block into them, if possible. Copying the list ahead of time also
|
|
|
|
// avoids trouble with the predecessor list reallocating.
|
|
|
|
bool Changed = false;
|
2009-12-04 09:42:45 +00:00
|
|
|
SmallSetVector<MachineBasicBlock*, 8> Preds(TailBB->pred_begin(),
|
|
|
|
TailBB->pred_end());
|
2009-11-26 00:32:21 +00:00
|
|
|
for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(),
|
|
|
|
PE = Preds.end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *PredBB = *PI;
|
|
|
|
|
|
|
|
assert(TailBB != PredBB &&
|
|
|
|
"Single-block loop should have been rejected earlier!");
|
2011-06-10 20:08:23 +00:00
|
|
|
// EH edges are ignored by AnalyzeBranch.
|
|
|
|
if (PredBB->succ_size() > 1)
|
|
|
|
continue;
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
MachineBasicBlock *PredTBB, *PredFBB;
|
|
|
|
SmallVector<MachineOperand, 4> PredCond;
|
|
|
|
if (TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true))
|
|
|
|
continue;
|
|
|
|
if (!PredCond.empty())
|
|
|
|
continue;
|
|
|
|
// Don't duplicate into a fall-through predecessor (at least for now).
|
|
|
|
if (PredBB->isLayoutSuccessor(TailBB) && PredBB->canFallThrough())
|
|
|
|
continue;
|
|
|
|
|
2010-01-05 01:25:15 +00:00
|
|
|
DEBUG(dbgs() << "\nTail-duplicating into PredBB: " << *PredBB
|
2009-11-26 00:32:21 +00:00
|
|
|
<< "From Succ: " << *TailBB);
|
|
|
|
|
2009-12-07 10:15:19 +00:00
|
|
|
TDBBs.push_back(PredBB);
|
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
// Remove PredBB's unconditional branch.
|
|
|
|
TII->RemoveBranch(*PredBB);
|
2009-12-03 08:43:53 +00:00
|
|
|
|
2012-05-30 00:42:39 +00:00
|
|
|
if (RS && !TailBB->livein_empty()) {
|
|
|
|
// Update PredBB livein.
|
|
|
|
RS->enterBasicBlock(PredBB);
|
|
|
|
if (!PredBB->empty())
|
2014-03-02 12:27:27 +00:00
|
|
|
RS->forward(std::prev(PredBB->end()));
|
2015-09-09 18:08:03 +00:00
|
|
|
for (const auto &LI : TailBB->liveins()) {
|
|
|
|
if (!RS->isRegUsed(LI.PhysReg, false))
|
2012-05-30 00:42:39 +00:00
|
|
|
// If a register is previously livein to the tail but it's not live
|
|
|
|
// at the end of predecessor BB, then it should be added to its
|
|
|
|
// livein list.
|
2015-08-24 22:59:52 +00:00
|
|
|
PredBB->addLiveIn(LI);
|
2012-05-30 00:42:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
// Clone the contents of TailBB into PredBB.
|
2009-12-03 08:43:53 +00:00
|
|
|
DenseMap<unsigned, unsigned> LocalVRMap;
|
2009-12-15 01:44:10 +00:00
|
|
|
SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos;
|
2012-02-20 07:51:58 +00:00
|
|
|
// Use instr_iterator here to properly handle bundles, e.g.
|
|
|
|
// ARM Thumb2 IT block.
|
|
|
|
MachineBasicBlock::instr_iterator I = TailBB->instr_begin();
|
|
|
|
while (I != TailBB->instr_end()) {
|
2009-12-04 09:42:45 +00:00
|
|
|
MachineInstr *MI = &*I;
|
|
|
|
++I;
|
2010-02-09 19:54:29 +00:00
|
|
|
if (MI->isPHI()) {
|
2009-12-03 08:43:53 +00:00
|
|
|
// Replace the uses of the def of the PHI with the register coming
|
|
|
|
// from PredBB.
|
2011-06-09 23:22:56 +00:00
|
|
|
ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, true);
|
2009-12-04 09:42:45 +00:00
|
|
|
} else {
|
|
|
|
// Replace def of virtual registers with new registers, and update
|
|
|
|
// uses with PHI source register or the new registers.
|
2011-06-09 22:53:47 +00:00
|
|
|
DuplicateInstruction(MI, TailBB, PredBB, MF, LocalVRMap, UsedByPhi);
|
2009-12-03 08:43:53 +00:00
|
|
|
}
|
2009-11-26 00:32:21 +00:00
|
|
|
}
|
2009-12-07 10:15:19 +00:00
|
|
|
MachineBasicBlock::iterator Loc = PredBB->getFirstTerminator();
|
2009-12-15 01:44:10 +00:00
|
|
|
for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) {
|
2010-07-10 22:42:59 +00:00
|
|
|
Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(),
|
|
|
|
TII->get(TargetOpcode::COPY),
|
|
|
|
CopyInfos[i].first).addReg(CopyInfos[i].second));
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
2011-06-17 05:54:50 +00:00
|
|
|
|
|
|
|
// Simplify
|
|
|
|
TII->AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true);
|
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
NumInstrDups += TailBB->size() - 1; // subtract one for removed branch
|
|
|
|
|
|
|
|
// Update the CFG.
|
|
|
|
PredBB->removeSuccessor(PredBB->succ_begin());
|
|
|
|
assert(PredBB->succ_empty() &&
|
|
|
|
"TailDuplicate called on block with multiple successors!");
|
|
|
|
for (MachineBasicBlock::succ_iterator I = TailBB->succ_begin(),
|
2009-12-04 09:42:45 +00:00
|
|
|
E = TailBB->succ_end(); I != E; ++I)
|
2014-02-12 18:09:18 +00:00
|
|
|
PredBB->addSuccessor(*I, MBPI->getEdgeWeight(TailBB, I));
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
++NumTailDups;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If TailBB was duplicated into all its predecessors except for the prior
|
|
|
|
// block, which falls through unconditionally, move the contents of this
|
|
|
|
// block into the prior block.
|
2014-03-02 12:27:27 +00:00
|
|
|
MachineBasicBlock *PrevBB = std::prev(MachineFunction::iterator(TailBB));
|
2014-04-14 00:51:57 +00:00
|
|
|
MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr;
|
2009-11-26 00:32:21 +00:00
|
|
|
SmallVector<MachineOperand, 4> PriorCond;
|
|
|
|
// This has to check PrevBB->succ_size() because EH edges are ignored by
|
|
|
|
// AnalyzeBranch.
|
2012-02-08 21:22:30 +00:00
|
|
|
if (PrevBB->succ_size() == 1 &&
|
2011-06-09 21:43:25 +00:00
|
|
|
!TII->AnalyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond, true) &&
|
|
|
|
PriorCond.empty() && !PriorTBB && TailBB->pred_size() == 1 &&
|
2009-11-26 00:32:21 +00:00
|
|
|
!TailBB->hasAddressTaken()) {
|
2010-01-05 01:25:15 +00:00
|
|
|
DEBUG(dbgs() << "\nMerging into block: " << *PrevBB
|
2009-11-26 00:32:21 +00:00
|
|
|
<< "From MBB: " << *TailBB);
|
2009-12-04 09:42:45 +00:00
|
|
|
if (PreRegAlloc) {
|
|
|
|
DenseMap<unsigned, unsigned> LocalVRMap;
|
2009-12-15 01:44:10 +00:00
|
|
|
SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos;
|
2009-12-04 09:42:45 +00:00
|
|
|
MachineBasicBlock::iterator I = TailBB->begin();
|
|
|
|
// Process PHI instructions first.
|
2010-02-09 19:54:29 +00:00
|
|
|
while (I != TailBB->end() && I->isPHI()) {
|
2009-12-04 09:42:45 +00:00
|
|
|
// Replace the uses of the def of the PHI with the register coming
|
|
|
|
// from PredBB.
|
|
|
|
MachineInstr *MI = &*I++;
|
2011-06-09 23:22:56 +00:00
|
|
|
ProcessPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi, true);
|
2009-12-04 09:42:45 +00:00
|
|
|
if (MI->getParent())
|
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now copy the non-PHI instructions.
|
|
|
|
while (I != TailBB->end()) {
|
|
|
|
// Replace def of virtual registers with new registers, and update
|
|
|
|
// uses with PHI source register or the new registers.
|
|
|
|
MachineInstr *MI = &*I++;
|
2012-02-20 07:51:58 +00:00
|
|
|
assert(!MI->isBundle() && "Not expecting bundles before regalloc!");
|
2011-06-09 22:53:47 +00:00
|
|
|
DuplicateInstruction(MI, TailBB, PrevBB, MF, LocalVRMap, UsedByPhi);
|
2009-12-04 09:42:45 +00:00
|
|
|
MI->eraseFromParent();
|
|
|
|
}
|
2009-12-07 10:15:19 +00:00
|
|
|
MachineBasicBlock::iterator Loc = PrevBB->getFirstTerminator();
|
2009-12-15 01:44:10 +00:00
|
|
|
for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) {
|
2010-07-10 22:42:59 +00:00
|
|
|
Copies.push_back(BuildMI(*PrevBB, Loc, DebugLoc(),
|
|
|
|
TII->get(TargetOpcode::COPY),
|
|
|
|
CopyInfos[i].first)
|
|
|
|
.addReg(CopyInfos[i].second));
|
2009-12-07 10:15:19 +00:00
|
|
|
}
|
2009-12-04 09:42:45 +00:00
|
|
|
} else {
|
|
|
|
// No PHIs to worry about, just splice the instructions over.
|
|
|
|
PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end());
|
|
|
|
}
|
|
|
|
PrevBB->removeSuccessor(PrevBB->succ_begin());
|
|
|
|
assert(PrevBB->succ_empty());
|
|
|
|
PrevBB->transferSuccessors(TailBB);
|
2009-12-07 10:15:19 +00:00
|
|
|
TDBBs.push_back(PrevBB);
|
2009-11-26 00:32:21 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2011-06-09 23:22:56 +00:00
|
|
|
// If this is after register allocation, there are no phis to fix.
|
|
|
|
if (!PreRegAlloc)
|
|
|
|
return Changed;
|
|
|
|
|
|
|
|
// If we made no changes so far, we are safe.
|
|
|
|
if (!Changed)
|
|
|
|
return Changed;
|
|
|
|
|
|
|
|
|
|
|
|
// Handle the nasty case in that we duplicated a block that is part of a loop
|
|
|
|
// into some but not all of its predecessors. For example:
|
2011-06-09 23:51:45 +00:00
|
|
|
// 1 -> 2 <-> 3 |
|
|
|
|
// \ |
|
|
|
|
// \---> rest |
|
2011-06-09 23:22:56 +00:00
|
|
|
// if we duplicate 2 into 1 but not into 3, we end up with
|
2011-06-09 23:51:45 +00:00
|
|
|
// 12 -> 3 <-> 2 -> rest |
|
|
|
|
// \ / |
|
|
|
|
// \----->-----/ |
|
2011-06-09 23:22:56 +00:00
|
|
|
// If there was a "var = phi(1, 3)" in 2, it has to be ultimately replaced
|
|
|
|
// with a phi in 3 (which now dominates 2).
|
|
|
|
// What we do here is introduce a copy in 3 of the register defined by the
|
|
|
|
// phi, just like when we are duplicating 2 into 3, but we don't copy any
|
|
|
|
// real instructions or remove the 3 -> 2 edge from the phi in 2.
|
|
|
|
for (SmallSetVector<MachineBasicBlock *, 8>::iterator PI = Preds.begin(),
|
|
|
|
PE = Preds.end(); PI != PE; ++PI) {
|
|
|
|
MachineBasicBlock *PredBB = *PI;
|
|
|
|
if (std::find(TDBBs.begin(), TDBBs.end(), PredBB) != TDBBs.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// EH edges
|
|
|
|
if (PredBB->succ_size() != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DenseMap<unsigned, unsigned> LocalVRMap;
|
|
|
|
SmallVector<std::pair<unsigned,unsigned>, 4> CopyInfos;
|
|
|
|
MachineBasicBlock::iterator I = TailBB->begin();
|
|
|
|
// Process PHI instructions first.
|
|
|
|
while (I != TailBB->end() && I->isPHI()) {
|
|
|
|
// Replace the uses of the def of the PHI with the register coming
|
|
|
|
// from PredBB.
|
|
|
|
MachineInstr *MI = &*I++;
|
|
|
|
ProcessPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi, false);
|
|
|
|
}
|
|
|
|
MachineBasicBlock::iterator Loc = PredBB->getFirstTerminator();
|
|
|
|
for (unsigned i = 0, e = CopyInfos.size(); i != e; ++i) {
|
|
|
|
Copies.push_back(BuildMI(*PredBB, Loc, DebugLoc(),
|
|
|
|
TII->get(TargetOpcode::COPY),
|
|
|
|
CopyInfos[i].first).addReg(CopyInfos[i].second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-26 00:32:21 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:16 +00:00
|
|
|
/// Remove the specified dead machine basic block from the function, updating
|
|
|
|
/// the CFG.
|
2009-11-26 21:38:41 +00:00
|
|
|
void TailDuplicatePass::RemoveDeadBlock(MachineBasicBlock *MBB) {
|
2009-11-26 00:32:21 +00:00
|
|
|
assert(MBB->pred_empty() && "MBB must be dead!");
|
2010-01-05 01:25:15 +00:00
|
|
|
DEBUG(dbgs() << "\nRemoving MBB: " << *MBB);
|
2009-11-26 00:32:21 +00:00
|
|
|
|
|
|
|
// Remove all successors.
|
|
|
|
while (!MBB->succ_empty())
|
|
|
|
MBB->removeSuccessor(MBB->succ_end()-1);
|
|
|
|
|
|
|
|
// Remove the block.
|
|
|
|
MBB->eraseFromParent();
|
|
|
|
}
|