2005-04-15 19:28:32 +00:00
|
|
|
//===-- CondPropagate.cpp - Propagate Conditional Expressions -------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2005-04-15 19:28:32 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2005-04-15 19:28:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass propagates information about conditional expressions through the
|
|
|
|
// program, allowing it to eliminate conditional branches in some cases.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "condprop"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2009-02-05 23:32:52 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2005-04-15 19:28:32 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Type.h"
|
2008-12-03 19:44:02 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2005-04-15 19:28:32 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-02-05 19:59:42 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-11-26 09:46:52 +00:00
|
|
|
#include "llvm/Support/Streams.h"
|
2005-04-15 19:28:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
STATISTIC(NumBrThread, "Number of CFG edges threaded through branches");
|
|
|
|
STATISTIC(NumSwThread, "Number of CFG edges threaded through switches");
|
2005-04-15 19:28:32 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
struct VISIBILITY_HIDDEN CondProp : public FunctionPass {
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
CondProp() : FunctionPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2005-04-15 19:28:32 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequiredID(BreakCriticalEdgesID);
|
|
|
|
//AU.addRequired<DominanceFrontier>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool MadeChange;
|
2009-02-05 19:59:42 +00:00
|
|
|
SmallVector<BasicBlock *, 4> DeadBlocks;
|
2005-04-15 19:28:32 +00:00
|
|
|
void SimplifyBlock(BasicBlock *BB);
|
|
|
|
void SimplifyPredecessors(BranchInst *BI);
|
|
|
|
void SimplifyPredecessors(SwitchInst *SI);
|
|
|
|
void RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB);
|
|
|
|
};
|
|
|
|
}
|
2008-05-13 00:00:25 +00:00
|
|
|
|
|
|
|
char CondProp::ID = 0;
|
|
|
|
static RegisterPass<CondProp> X("condprop", "Conditional Propagation");
|
2005-04-15 19:28:32 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createCondPropagationPass() {
|
|
|
|
return new CondProp();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CondProp::runOnFunction(Function &F) {
|
|
|
|
bool EverMadeChange = false;
|
2009-02-05 19:59:42 +00:00
|
|
|
DeadBlocks.clear();
|
2005-04-15 19:28:32 +00:00
|
|
|
|
|
|
|
// While we are simplifying blocks, keep iterating.
|
|
|
|
do {
|
|
|
|
MadeChange = false;
|
2009-02-05 19:59:42 +00:00
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E;)
|
|
|
|
SimplifyBlock(BB++);
|
2007-07-26 20:21:42 +00:00
|
|
|
EverMadeChange = EverMadeChange || MadeChange;
|
2005-04-15 19:28:32 +00:00
|
|
|
} while (MadeChange);
|
2009-02-05 19:59:42 +00:00
|
|
|
|
|
|
|
if (EverMadeChange) {
|
|
|
|
while (!DeadBlocks.empty()) {
|
|
|
|
BasicBlock *BB = DeadBlocks.back(); DeadBlocks.pop_back();
|
|
|
|
DeleteDeadBlock(BB);
|
|
|
|
}
|
|
|
|
}
|
2005-04-15 19:28:32 +00:00
|
|
|
return EverMadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CondProp::SimplifyBlock(BasicBlock *BB) {
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
|
|
|
|
// If this is a conditional branch based on a phi node that is defined in
|
|
|
|
// this block, see if we can simplify predecessors of this block.
|
|
|
|
if (BI->isConditional() && isa<PHINode>(BI->getCondition()) &&
|
|
|
|
cast<PHINode>(BI->getCondition())->getParent() == BB)
|
|
|
|
SimplifyPredecessors(BI);
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2005-04-15 19:28:32 +00:00
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
|
|
|
|
if (isa<PHINode>(SI->getCondition()) &&
|
|
|
|
cast<PHINode>(SI->getCondition())->getParent() == BB)
|
|
|
|
SimplifyPredecessors(SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If possible, simplify the terminator of this block.
|
|
|
|
if (ConstantFoldTerminator(BB))
|
|
|
|
MadeChange = true;
|
|
|
|
|
|
|
|
// If this block ends with an unconditional branch and the only successor has
|
|
|
|
// only this block as a predecessor, merge the two blocks together.
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
|
2006-08-14 21:38:05 +00:00
|
|
|
if (BI->isUnconditional() && BI->getSuccessor(0)->getSinglePredecessor() &&
|
|
|
|
BB != BI->getSuccessor(0)) {
|
2005-04-15 19:28:32 +00:00
|
|
|
BasicBlock *Succ = BI->getSuccessor(0);
|
2006-08-14 21:38:05 +00:00
|
|
|
|
2008-12-03 19:44:02 +00:00
|
|
|
// If Succ has any PHI nodes, they are all single-entry PHI's. Eliminate
|
|
|
|
// them.
|
|
|
|
FoldSingleEntryPHINodes(Succ);
|
2006-08-14 21:38:05 +00:00
|
|
|
|
2005-04-15 19:28:32 +00:00
|
|
|
// Remove BI.
|
|
|
|
BI->eraseFromParent();
|
|
|
|
|
|
|
|
// Move over all of the instructions.
|
|
|
|
BB->getInstList().splice(BB->end(), Succ->getInstList());
|
|
|
|
|
|
|
|
// Any phi nodes that had entries for Succ now have entries from BB.
|
|
|
|
Succ->replaceAllUsesWith(BB);
|
|
|
|
|
|
|
|
// Succ is now dead, but we cannot delete it without potentially
|
|
|
|
// invalidating iterators elsewhere. Just insert an unreachable
|
2009-02-05 19:59:42 +00:00
|
|
|
// instruction in it and delete this block later on.
|
2005-04-15 19:28:32 +00:00
|
|
|
new UnreachableInst(Succ);
|
2009-02-05 19:59:42 +00:00
|
|
|
DeadBlocks.push_back(Succ);
|
2005-04-15 19:28:32 +00:00
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimplifyPredecessors(branches) - We know that BI is a conditional branch
|
|
|
|
// based on a PHI node defined in this block. If the phi node contains constant
|
|
|
|
// operands, then the blocks corresponding to those operands can be modified to
|
|
|
|
// jump directly to the destination instead of going through this block.
|
|
|
|
void CondProp::SimplifyPredecessors(BranchInst *BI) {
|
|
|
|
// TODO: We currently only handle the most trival case, where the PHI node has
|
2009-02-05 23:32:52 +00:00
|
|
|
// one use (the branch), and is the only instruction besides the branch and dbg
|
|
|
|
// intrinsics in the block.
|
2005-04-15 19:28:32 +00:00
|
|
|
PHINode *PN = cast<PHINode>(BI->getCondition());
|
2009-01-26 02:18:20 +00:00
|
|
|
|
|
|
|
if (PN->getNumIncomingValues() == 1) {
|
|
|
|
// Eliminate single-entry PHI nodes.
|
|
|
|
FoldSingleEntryPHINodes(PN->getParent());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-15 19:28:32 +00:00
|
|
|
if (!PN->hasOneUse()) return;
|
|
|
|
|
|
|
|
BasicBlock *BB = BI->getParent();
|
2009-02-05 23:32:52 +00:00
|
|
|
if (&*BB->begin() != PN)
|
|
|
|
return;
|
|
|
|
BasicBlock::iterator BBI = BB->begin();
|
|
|
|
BasicBlock::iterator BBE = BB->end();
|
2009-02-07 03:36:02 +00:00
|
|
|
while (BBI != BBE && isa<DbgInfoIntrinsic>(++BBI)) ;
|
2009-02-05 23:32:52 +00:00
|
|
|
if (&*BBI != BI)
|
2005-04-15 19:28:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Ok, we have this really simple case, walk the PHI operands, looking for
|
|
|
|
// constants. Walk from the end to remove operands from the end when
|
|
|
|
// possible, and to avoid invalidating "i".
|
|
|
|
for (unsigned i = PN->getNumIncomingValues(); i != 0; --i)
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i-1))) {
|
2005-04-15 19:28:32 +00:00
|
|
|
// If we have a constant, forward the edge from its current to its
|
|
|
|
// ultimate destination.
|
|
|
|
RevectorBlockTo(PN->getIncomingBlock(i-1),
|
2007-03-02 00:28:52 +00:00
|
|
|
BI->getSuccessor(CB->isZero()));
|
2005-04-15 19:28:32 +00:00
|
|
|
++NumBrThread;
|
|
|
|
|
2007-08-02 04:47:05 +00:00
|
|
|
// If there were two predecessors before this simplification, or if the
|
|
|
|
// PHI node contained all the same value except for the one we just
|
|
|
|
// substituted, the PHI node may be deleted. Don't iterate through it the
|
|
|
|
// last time.
|
|
|
|
if (BI->getCondition() != PN) return;
|
2005-04-15 19:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SimplifyPredecessors(switch) - We know that SI is switch based on a PHI node
|
|
|
|
// defined in this block. If the phi node contains constant operands, then the
|
|
|
|
// blocks corresponding to those operands can be modified to jump directly to
|
|
|
|
// the destination instead of going through this block.
|
|
|
|
void CondProp::SimplifyPredecessors(SwitchInst *SI) {
|
|
|
|
// TODO: We currently only handle the most trival case, where the PHI node has
|
2009-02-05 23:32:52 +00:00
|
|
|
// one use (the branch), and is the only instruction besides the branch and
|
|
|
|
// dbg intrinsics in the block.
|
2005-04-15 19:28:32 +00:00
|
|
|
PHINode *PN = cast<PHINode>(SI->getCondition());
|
|
|
|
if (!PN->hasOneUse()) return;
|
|
|
|
|
|
|
|
BasicBlock *BB = SI->getParent();
|
2009-02-05 23:32:52 +00:00
|
|
|
if (&*BB->begin() != PN)
|
|
|
|
return;
|
|
|
|
BasicBlock::iterator BBI = BB->begin();
|
|
|
|
BasicBlock::iterator BBE = BB->end();
|
2009-02-07 03:36:02 +00:00
|
|
|
while (BBI != BBE && isa<DbgInfoIntrinsic>(++BBI)) ;
|
2009-02-05 23:32:52 +00:00
|
|
|
if (&*BBI != SI)
|
2005-04-15 19:28:32 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
bool RemovedPreds = false;
|
|
|
|
|
|
|
|
// Ok, we have this really simple case, walk the PHI operands, looking for
|
|
|
|
// constants. Walk from the end to remove operands from the end when
|
|
|
|
// possible, and to avoid invalidating "i".
|
|
|
|
for (unsigned i = PN->getNumIncomingValues(); i != 0; --i)
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(PN->getIncomingValue(i-1))) {
|
|
|
|
// If we have a constant, forward the edge from its current to its
|
|
|
|
// ultimate destination.
|
|
|
|
unsigned DestCase = SI->findCaseValue(CI);
|
|
|
|
RevectorBlockTo(PN->getIncomingBlock(i-1),
|
|
|
|
SI->getSuccessor(DestCase));
|
|
|
|
++NumSwThread;
|
|
|
|
RemovedPreds = true;
|
|
|
|
|
2007-08-02 04:47:05 +00:00
|
|
|
// If there were two predecessors before this simplification, or if the
|
|
|
|
// PHI node contained all the same value except for the one we just
|
|
|
|
// substituted, the PHI node may be deleted. Don't iterate through it the
|
|
|
|
// last time.
|
|
|
|
if (SI->getCondition() != PN) return;
|
2005-04-15 19:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RevectorBlockTo - Revector the unconditional branch at the end of FromBB to
|
|
|
|
// the ToBB block, which is one of the successors of its current successor.
|
|
|
|
void CondProp::RevectorBlockTo(BasicBlock *FromBB, BasicBlock *ToBB) {
|
|
|
|
BranchInst *FromBr = cast<BranchInst>(FromBB->getTerminator());
|
|
|
|
assert(FromBr->isUnconditional() && "FromBB should end with uncond br!");
|
|
|
|
|
|
|
|
// Get the old block we are threading through.
|
|
|
|
BasicBlock *OldSucc = FromBr->getSuccessor(0);
|
|
|
|
|
2006-11-01 23:04:45 +00:00
|
|
|
// OldSucc had multiple successors. If ToBB has multiple predecessors, then
|
|
|
|
// the edge between them would be critical, which we already took care of.
|
|
|
|
// If ToBB has single operand PHI node then take care of it here.
|
2008-12-03 19:44:02 +00:00
|
|
|
FoldSingleEntryPHINodes(ToBB);
|
2005-04-15 19:28:32 +00:00
|
|
|
|
|
|
|
// Update PHI nodes in OldSucc to know that FromBB no longer branches to it.
|
|
|
|
OldSucc->removePredecessor(FromBB);
|
|
|
|
|
|
|
|
// Change FromBr to branch to the new destination.
|
|
|
|
FromBr->setSuccessor(0, ToBB);
|
|
|
|
|
|
|
|
MadeChange = true;
|
|
|
|
}
|