2008-04-20 20:35:01 +00:00
|
|
|
//===- JumpThreading.cpp - Thread control through conditional blocks ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-04-20 21:13:06 +00:00
|
|
|
// This file implements the Jump Threading pass.
|
2008-04-20 20:35:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "jump-threading"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2008-04-20 21:13:06 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2009-07-03 00:54:20 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2008-04-20 20:35:01 +00:00
|
|
|
#include "llvm/Pass.h"
|
2011-04-14 21:35:50 +00:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2009-11-09 23:00:14 +00:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2009-11-11 02:08:33 +00:00
|
|
|
#include "llvm/Analysis/LazyValueInfo.h"
|
2010-05-28 16:19:17 +00:00
|
|
|
#include "llvm/Analysis/Loads.h"
|
2008-04-21 02:57:57 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2008-04-20 22:39:42 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2009-10-10 09:05:58 +00:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2008-12-01 04:48:07 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2011-12-02 01:26:24 +00:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
2009-05-04 18:40:41 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2010-08-31 07:36:34 +00:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2009-05-04 18:40:41 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2008-04-20 20:35:01 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-04-20 21:13:06 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-12-28 08:20:46 +00:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-07-26 07:49:05 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-04-20 20:35:01 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
STATISTIC(NumThreads, "Number of jumps threaded");
|
|
|
|
STATISTIC(NumFolds, "Number of terminators folded");
|
2009-10-11 07:24:57 +00:00
|
|
|
STATISTIC(NumDupes, "Number of branch blocks duplicated to eliminate phi");
|
2008-04-20 20:35:01 +00:00
|
|
|
|
2008-04-20 21:13:06 +00:00
|
|
|
static cl::opt<unsigned>
|
2010-12-05 19:02:47 +00:00
|
|
|
Threshold("jump-threading-threshold",
|
2008-04-20 21:13:06 +00:00
|
|
|
cl::desc("Max block size to duplicate for jump threading"),
|
|
|
|
cl::init(6), cl::Hidden);
|
|
|
|
|
2008-04-20 20:35:01 +00:00
|
|
|
namespace {
|
2010-12-05 19:06:41 +00:00
|
|
|
// These are at global scope so static functions can use them too.
|
|
|
|
typedef SmallVectorImpl<std::pair<Constant*, BasicBlock*> > PredValueInfo;
|
|
|
|
typedef SmallVector<std::pair<Constant*, BasicBlock*>, 8> PredValueInfoTy;
|
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
// This is used to keep track of what kind of constant we're currently hoping
|
|
|
|
// to find.
|
|
|
|
enum ConstantPreference {
|
|
|
|
WantInteger,
|
|
|
|
WantBlockAddress
|
|
|
|
};
|
|
|
|
|
2008-05-09 04:43:13 +00:00
|
|
|
/// This pass performs 'jump threading', which looks at blocks that have
|
|
|
|
/// multiple predecessors and multiple successors. If one or more of the
|
|
|
|
/// predecessors of the block can be proven to always jump to one of the
|
|
|
|
/// successors, we forward the edge from the predecessor to the successor by
|
|
|
|
/// duplicating the contents of this block.
|
|
|
|
///
|
|
|
|
/// An example of when this can occur is code like this:
|
|
|
|
///
|
|
|
|
/// if () { ...
|
|
|
|
/// X = 4;
|
|
|
|
/// }
|
|
|
|
/// if (X < 3) {
|
|
|
|
///
|
|
|
|
/// In this case, the unconditional branch at the end of the first if can be
|
|
|
|
/// revectored to the false side of the second if.
|
|
|
|
///
|
2009-09-02 06:11:42 +00:00
|
|
|
class JumpThreading : public FunctionPass {
|
2008-12-01 04:48:07 +00:00
|
|
|
TargetData *TD;
|
2011-12-02 01:26:24 +00:00
|
|
|
TargetLibraryInfo *TLI;
|
2009-11-11 02:08:33 +00:00
|
|
|
LazyValueInfo *LVI;
|
2009-05-04 18:40:41 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
SmallPtrSet<BasicBlock*, 16> LoopHeaders;
|
|
|
|
#else
|
|
|
|
SmallSet<AssertingVH<BasicBlock>, 16> LoopHeaders;
|
|
|
|
#endif
|
2010-08-31 07:36:34 +00:00
|
|
|
DenseSet<std::pair<Value*, BasicBlock*> > RecursionSet;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 19:24:27 +00:00
|
|
|
// RAII helper for updating the recursion stack.
|
|
|
|
struct RecursionSetRemover {
|
|
|
|
DenseSet<std::pair<Value*, BasicBlock*> > &TheSet;
|
|
|
|
std::pair<Value*, BasicBlock*> ThePair;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 19:24:27 +00:00
|
|
|
RecursionSetRemover(DenseSet<std::pair<Value*, BasicBlock*> > &S,
|
|
|
|
std::pair<Value*, BasicBlock*> P)
|
|
|
|
: TheSet(S), ThePair(P) { }
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 19:24:27 +00:00
|
|
|
~RecursionSetRemover() {
|
|
|
|
TheSet.erase(ThePair);
|
|
|
|
}
|
|
|
|
};
|
2008-04-20 20:35:01 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification
|
2010-10-19 17:21:58 +00:00
|
|
|
JumpThreading() : FunctionPass(ID) {
|
|
|
|
initializeJumpThreadingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2008-04-20 20:35:01 +00:00
|
|
|
|
|
|
|
bool runOnFunction(Function &F);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-11 02:08:33 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2010-09-14 20:57:41 +00:00
|
|
|
AU.addRequired<LazyValueInfo>();
|
|
|
|
AU.addPreserved<LazyValueInfo>();
|
2011-12-02 01:26:24 +00:00
|
|
|
AU.addRequired<TargetLibraryInfo>();
|
2009-11-11 02:08:33 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-11 02:08:33 +00:00
|
|
|
void FindLoopHeaders(Function &F);
|
2008-11-27 07:20:04 +00:00
|
|
|
bool ProcessBlock(BasicBlock *BB);
|
2009-11-07 08:05:03 +00:00
|
|
|
bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock*> &PredBBs,
|
|
|
|
BasicBlock *SuccBB);
|
2009-10-11 07:24:57 +00:00
|
|
|
bool DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,
|
2010-01-12 02:07:17 +00:00
|
|
|
const SmallVectorImpl<BasicBlock *> &PredBBs);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB,
|
2010-12-06 23:36:56 +00:00
|
|
|
PredValueInfo &Result,
|
|
|
|
ConstantPreference Preference);
|
|
|
|
bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
|
|
|
|
ConstantPreference Preference);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-11 23:41:09 +00:00
|
|
|
bool ProcessBranchOnPHI(PHINode *PN);
|
2010-01-12 02:07:17 +00:00
|
|
|
bool ProcessBranchOnXOR(BinaryOperator *BO);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
bool SimplifyPartiallyRedundantLoad(LoadInst *LI);
|
2008-04-20 20:35:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char JumpThreading::ID = 0;
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading",
|
|
|
|
"Jump Threading", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LazyValueInfo)
|
2011-12-02 01:26:24 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_END(JumpThreading, "jump-threading",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Jump Threading", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2008-04-20 20:35:01 +00:00
|
|
|
// Public interface to the Jump Threading pass
|
|
|
|
FunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); }
|
|
|
|
|
|
|
|
/// runOnFunction - Top level algorithm.
|
|
|
|
///
|
|
|
|
bool JumpThreading::runOnFunction(Function &F) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n");
|
2009-07-24 18:13:53 +00:00
|
|
|
TD = getAnalysisIfAvailable<TargetData>();
|
2011-12-02 01:26:24 +00:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfo>();
|
2010-09-14 20:57:41 +00:00
|
|
|
LVI = &getAnalysis<LazyValueInfo>();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-05-04 18:40:41 +00:00
|
|
|
FindLoopHeaders(F);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-07 13:50:07 +00:00
|
|
|
bool Changed, EverChanged = false;
|
|
|
|
do {
|
|
|
|
Changed = false;
|
2008-12-03 07:48:08 +00:00
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E;) {
|
|
|
|
BasicBlock *BB = I;
|
2010-12-05 19:02:47 +00:00
|
|
|
// Thread all of the branches we can over this block.
|
2008-12-03 07:48:08 +00:00
|
|
|
while (ProcessBlock(BB))
|
2008-04-20 22:39:42 +00:00
|
|
|
Changed = true;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-12-03 07:48:08 +00:00
|
|
|
++I;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-12-03 07:48:08 +00:00
|
|
|
// If the block is trivially dead, zap it. This eliminates the successor
|
|
|
|
// edges which simplifies the CFG.
|
|
|
|
if (pred_begin(BB) == pred_end(BB) &&
|
2008-12-08 22:44:07 +00:00
|
|
|
BB != &BB->getParent()->getEntryBlock()) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " JT: Deleting dead block '" << BB->getName()
|
2009-10-11 07:24:57 +00:00
|
|
|
<< "' with terminator: " << *BB->getTerminator() << '\n');
|
2009-05-04 18:40:41 +00:00
|
|
|
LoopHeaders.erase(BB);
|
2010-09-14 20:57:41 +00:00
|
|
|
LVI->eraseBlock(BB);
|
2008-12-03 07:48:08 +00:00
|
|
|
DeleteDeadBlock(BB);
|
|
|
|
Changed = true;
|
2010-12-13 02:38:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-04-14 21:35:50 +00:00
|
|
|
|
2010-12-13 02:38:13 +00:00
|
|
|
BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
|
2011-04-14 21:35:50 +00:00
|
|
|
|
2010-12-13 02:38:13 +00:00
|
|
|
// Can't thread an unconditional jump, but if the block is "almost
|
|
|
|
// empty", we can replace uses of it with uses of the successor and make
|
|
|
|
// this dead.
|
|
|
|
if (BI && BI->isUnconditional() &&
|
|
|
|
BB != &BB->getParent()->getEntryBlock() &&
|
2009-11-10 21:40:01 +00:00
|
|
|
// If the terminator is the only non-phi instruction, try to nuke it.
|
2010-12-13 02:38:13 +00:00
|
|
|
BB->getFirstNonPHIOrDbg()->isTerminator()) {
|
|
|
|
// Since TryToSimplifyUncondBranchFromEmptyBlock may delete the
|
|
|
|
// block, we have to make sure it isn't in the LoopHeaders set. We
|
|
|
|
// reinsert afterward if needed.
|
|
|
|
bool ErasedFromLoopHeaders = LoopHeaders.erase(BB);
|
|
|
|
BasicBlock *Succ = BI->getSuccessor(0);
|
|
|
|
|
|
|
|
// FIXME: It is always conservatively correct to drop the info
|
|
|
|
// for a block even if it doesn't get erased. This isn't totally
|
|
|
|
// awesome, but it allows us to use AssertingVH to prevent nasty
|
|
|
|
// dangling pointer issues within LazyValueInfo.
|
|
|
|
LVI->eraseBlock(BB);
|
|
|
|
if (TryToSimplifyUncondBranchFromEmptyBlock(BB)) {
|
|
|
|
Changed = true;
|
|
|
|
// If we deleted BB and BB was the header of a loop, then the
|
|
|
|
// successor is now the header of the loop.
|
|
|
|
BB = Succ;
|
2009-11-10 21:40:01 +00:00
|
|
|
}
|
2010-12-13 02:38:13 +00:00
|
|
|
|
|
|
|
if (ErasedFromLoopHeaders)
|
|
|
|
LoopHeaders.insert(BB);
|
2008-12-03 07:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-20 22:39:42 +00:00
|
|
|
EverChanged |= Changed;
|
2010-01-07 13:50:07 +00:00
|
|
|
} while (Changed);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-05-04 18:40:41 +00:00
|
|
|
LoopHeaders.clear();
|
2008-04-20 22:39:42 +00:00
|
|
|
return EverChanged;
|
2008-04-20 20:35:01 +00:00
|
|
|
}
|
2008-04-20 21:13:06 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
/// getJumpThreadDuplicationCost - Return the cost of duplicating this block to
|
|
|
|
/// thread across it.
|
|
|
|
static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) {
|
|
|
|
/// Ignore PHI nodes, these will be flattened when duplication happens.
|
|
|
|
BasicBlock::const_iterator I = BB->getFirstNonPHI();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-11 00:21:58 +00:00
|
|
|
// FIXME: THREADING will delete values that are just used to compute the
|
|
|
|
// branch, so they shouldn't count against the duplication cost.
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Sum up the cost of each instruction until we get to the terminator. Don't
|
|
|
|
// include the terminator because the copy won't include it.
|
|
|
|
unsigned Size = 0;
|
|
|
|
for (; !isa<TerminatorInst>(I); ++I) {
|
|
|
|
// Debugger intrinsics don't incur code size.
|
|
|
|
if (isa<DbgInfoIntrinsic>(I)) continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// If this is a pointer->pointer bitcast, it is free.
|
2010-02-16 11:11:14 +00:00
|
|
|
if (isa<BitCastInst>(I) && I->getType()->isPointerTy())
|
2009-10-11 07:24:57 +00:00
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// All other instructions count for at least one unit.
|
|
|
|
++Size;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Calls are more expensive. If they are non-intrinsic calls, we model them
|
|
|
|
// as having cost of 4. If they are a non-vector intrinsic, we model them
|
|
|
|
// as having cost of 2 total, and if they are a vector intrinsic, we model
|
|
|
|
// them as having cost 1.
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I)) {
|
|
|
|
if (!isa<IntrinsicInst>(CI))
|
|
|
|
Size += 3;
|
2010-02-16 11:11:14 +00:00
|
|
|
else if (!CI->getType()->isVectorTy())
|
2009-10-11 07:24:57 +00:00
|
|
|
Size += 1;
|
|
|
|
}
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Threading through a switch statement is particularly profitable. If this
|
|
|
|
// block ends in a switch, decrease its cost to make it more likely to happen.
|
|
|
|
if (isa<SwitchInst>(I))
|
|
|
|
Size = Size > 6 ? Size-6 : 0;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
// The same holds for indirect branches, but slightly more so.
|
|
|
|
if (isa<IndirectBrInst>(I))
|
|
|
|
Size = Size > 8 ? Size-8 : 0;
|
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
2009-05-04 18:40:41 +00:00
|
|
|
/// FindLoopHeaders - We do not want jump threading to turn proper loop
|
|
|
|
/// structures into irreducible loops. Doing this breaks up the loop nesting
|
|
|
|
/// hierarchy and pessimizes later transformations. To prevent this from
|
|
|
|
/// happening, we first have to find the loop headers. Here we approximate this
|
|
|
|
/// by finding targets of backedges in the CFG.
|
|
|
|
///
|
|
|
|
/// Note that there definitely are cases when we want to allow threading of
|
|
|
|
/// edges across a loop header. For example, threading a jump from outside the
|
|
|
|
/// loop (the preheader) to an exit block of the loop is definitely profitable.
|
|
|
|
/// It is also almost always profitable to thread backedges from within the loop
|
|
|
|
/// to exit blocks, and is often profitable to thread backedges to other blocks
|
|
|
|
/// within the loop (forming a nested loop). This simple analysis is not rich
|
|
|
|
/// enough to track all of these properties and keep it up-to-date as the CFG
|
|
|
|
/// mutates, so we don't allow any of these transformations.
|
|
|
|
///
|
|
|
|
void JumpThreading::FindLoopHeaders(Function &F) {
|
|
|
|
SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges;
|
|
|
|
FindFunctionBackedges(F, Edges);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-05-04 18:40:41 +00:00
|
|
|
for (unsigned i = 0, e = Edges.size(); i != e; ++i)
|
|
|
|
LoopHeaders.insert(const_cast<BasicBlock*>(Edges[i].second));
|
|
|
|
}
|
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
/// getKnownConstant - Helper method to determine if we can thread over a
|
|
|
|
/// terminator with the given value as its condition, and if so what value to
|
2010-12-06 23:36:56 +00:00
|
|
|
/// use for that. What kind of value this is depends on whether we want an
|
|
|
|
/// integer or a block address, but an undef is always accepted.
|
2010-12-05 19:06:41 +00:00
|
|
|
/// Returns null if Val is null or not an appropriate constant.
|
2010-12-06 23:36:56 +00:00
|
|
|
static Constant *getKnownConstant(Value *Val, ConstantPreference Preference) {
|
2010-12-05 19:06:41 +00:00
|
|
|
if (!Val)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Undef is "known" enough.
|
|
|
|
if (UndefValue *U = dyn_cast<UndefValue>(Val))
|
|
|
|
return U;
|
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Preference == WantBlockAddress)
|
|
|
|
return dyn_cast<BlockAddress>(Val->stripPointerCasts());
|
2010-12-05 19:06:41 +00:00
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
return dyn_cast<ConstantInt>(Val);
|
2010-08-31 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
/// ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see
|
2010-12-06 23:36:56 +00:00
|
|
|
/// if we can infer that the value is a known ConstantInt/BlockAddress or undef
|
|
|
|
/// in any of our predecessors. If so, return the known list of value and pred
|
|
|
|
/// BB in the result vector.
|
2009-11-07 08:05:03 +00:00
|
|
|
///
|
|
|
|
/// This returns true if there were any known values.
|
|
|
|
///
|
|
|
|
bool JumpThreading::
|
2010-12-06 23:36:56 +00:00
|
|
|
ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, PredValueInfo &Result,
|
|
|
|
ConstantPreference Preference) {
|
2010-08-31 19:24:27 +00:00
|
|
|
// This method walks up use-def chains recursively. Because of this, we could
|
|
|
|
// get into an infinite loop going around loops in the use-def chain. To
|
|
|
|
// prevent this, keep track of what (value, block) pairs we've already visited
|
|
|
|
// and terminate the search if we loop back to them
|
2010-08-31 07:36:34 +00:00
|
|
|
if (!RecursionSet.insert(std::make_pair(V, BB)).second)
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 19:24:27 +00:00
|
|
|
// An RAII help to remove this pair from the recursion set once the recursion
|
|
|
|
// stack pops back out again.
|
|
|
|
RecursionSetRemover remover(RecursionSet, std::make_pair(V, BB));
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
// If V is a constant, then it is known in all predecessors.
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(V, Preference)) {
|
2009-11-11 02:08:33 +00:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
2010-12-05 19:06:41 +00:00
|
|
|
Result.push_back(std::make_pair(KC, *PI));
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// If V is a non-instruction value, or an instruction in a different block,
|
|
|
|
// then it can't be derived from a PHI.
|
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
2009-11-11 02:08:33 +00:00
|
|
|
if (I == 0 || I->getParent() != BB) {
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-11 02:08:33 +00:00
|
|
|
// Okay, if this is a live-in value, see if it has a known value at the end
|
|
|
|
// of any of our predecessors.
|
|
|
|
//
|
|
|
|
// FIXME: This should be an edge property, not a block end property.
|
|
|
|
/// TODO: Per PR2563, we could infer value range information about a
|
|
|
|
/// predecessor based on its terminator.
|
|
|
|
//
|
2010-09-14 20:57:41 +00:00
|
|
|
// FIXME: change this to use the more-rich 'getPredicateOnEdge' method if
|
|
|
|
// "I" is a non-local compare-with-a-constant instruction. This would be
|
|
|
|
// able to handle value inequalities better, for example if the compare is
|
|
|
|
// "X < 4" and "X < 3" is known true but "X < 4" itself is not available.
|
|
|
|
// Perhaps getConstantOnEdge should be smart enough to do this?
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-09-14 20:57:41 +00:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
|
|
|
|
BasicBlock *P = *PI;
|
|
|
|
// If the value is known by LazyValueInfo to be a constant in a
|
|
|
|
// predecessor, use that information to try to thread this block.
|
|
|
|
Constant *PredCst = LVI->getConstantOnEdge(V, P, BB);
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(PredCst, Preference))
|
2010-12-05 19:06:41 +00:00
|
|
|
Result.push_back(std::make_pair(KC, P));
|
2009-11-11 02:08:33 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-09-14 20:57:41 +00:00
|
|
|
return !Result.empty();
|
2009-11-11 02:08:33 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
/// If I is a PHI node, then we know the incoming values for any constants.
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(I)) {
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
Value *InVal = PN->getIncomingValue(i);
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(InVal, Preference)) {
|
2010-12-05 19:06:41 +00:00
|
|
|
Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i)));
|
2010-09-14 20:57:41 +00:00
|
|
|
} else {
|
2010-08-26 17:40:24 +00:00
|
|
|
Constant *CI = LVI->getConstantOnEdge(InVal,
|
|
|
|
PN->getIncomingBlock(i), BB);
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(CI, Preference))
|
|
|
|
Result.push_back(std::make_pair(KC, PN->getIncomingBlock(i)));
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
return !Result.empty();
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
PredValueInfoTy LHSVals, RHSVals;
|
2009-11-07 08:05:03 +00:00
|
|
|
|
|
|
|
// Handle some boolean conditions.
|
2010-12-05 19:02:47 +00:00
|
|
|
if (I->getType()->getPrimitiveSizeInBits() == 1) {
|
2010-12-06 23:36:56 +00:00
|
|
|
assert(Preference == WantInteger && "One-bit non-integer type?");
|
2009-11-07 08:05:03 +00:00
|
|
|
// X | true -> true
|
|
|
|
// X & false -> false
|
|
|
|
if (I->getOpcode() == Instruction::Or ||
|
|
|
|
I->getOpcode() == Instruction::And) {
|
2010-12-06 23:36:56 +00:00
|
|
|
ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals,
|
|
|
|
WantInteger);
|
|
|
|
ComputeValueKnownInPredecessors(I->getOperand(1), BB, RHSVals,
|
|
|
|
WantInteger);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 19:24:27 +00:00
|
|
|
if (LHSVals.empty() && RHSVals.empty())
|
2009-11-07 08:05:03 +00:00
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
ConstantInt *InterestingVal;
|
|
|
|
if (I->getOpcode() == Instruction::Or)
|
|
|
|
InterestingVal = ConstantInt::getTrue(I->getContext());
|
|
|
|
else
|
|
|
|
InterestingVal = ConstantInt::getFalse(I->getContext());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-18 03:14:36 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 4> LHSKnownBBs;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-02-11 04:40:44 +00:00
|
|
|
// Scan for the sentinel. If we find an undef, force it to the
|
|
|
|
// interesting value: x|undef -> true and x&undef -> false.
|
2009-11-07 08:05:03 +00:00
|
|
|
for (unsigned i = 0, e = LHSVals.size(); i != e; ++i)
|
2010-12-05 19:06:41 +00:00
|
|
|
if (LHSVals[i].first == InterestingVal ||
|
|
|
|
isa<UndefValue>(LHSVals[i].first)) {
|
2009-11-07 08:05:03 +00:00
|
|
|
Result.push_back(LHSVals[i]);
|
2010-02-11 04:40:44 +00:00
|
|
|
Result.back().first = InterestingVal;
|
2010-08-18 03:14:36 +00:00
|
|
|
LHSKnownBBs.insert(LHSVals[i].second);
|
2010-02-11 04:40:44 +00:00
|
|
|
}
|
2009-11-07 08:05:03 +00:00
|
|
|
for (unsigned i = 0, e = RHSVals.size(); i != e; ++i)
|
2010-12-05 19:06:41 +00:00
|
|
|
if (RHSVals[i].first == InterestingVal ||
|
|
|
|
isa<UndefValue>(RHSVals[i].first)) {
|
2010-07-12 00:47:34 +00:00
|
|
|
// If we already inferred a value for this block on the LHS, don't
|
|
|
|
// re-add it.
|
2010-08-18 03:14:36 +00:00
|
|
|
if (!LHSKnownBBs.count(RHSVals[i].second)) {
|
2010-07-12 00:47:34 +00:00
|
|
|
Result.push_back(RHSVals[i]);
|
|
|
|
Result.back().first = InterestingVal;
|
|
|
|
}
|
2010-02-11 04:40:44 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
return !Result.empty();
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-10 22:39:16 +00:00
|
|
|
// Handle the NOT form of XOR.
|
|
|
|
if (I->getOpcode() == Instruction::Xor &&
|
|
|
|
isa<ConstantInt>(I->getOperand(1)) &&
|
|
|
|
cast<ConstantInt>(I->getOperand(1))->isOne()) {
|
2010-12-06 23:36:56 +00:00
|
|
|
ComputeValueKnownInPredecessors(I->getOperand(0), BB, Result,
|
|
|
|
WantInteger);
|
2010-08-31 19:24:27 +00:00
|
|
|
if (Result.empty())
|
2009-11-10 22:39:16 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Invert the known values.
|
|
|
|
for (unsigned i = 0, e = Result.size(); i != e; ++i)
|
2010-12-05 19:06:41 +00:00
|
|
|
Result[i].first = ConstantExpr::getNot(Result[i].first);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-10 22:39:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-26 17:40:24 +00:00
|
|
|
// Try to simplify some other binary operator values.
|
|
|
|
} else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
|
2010-12-06 23:36:56 +00:00
|
|
|
assert(Preference != WantBlockAddress
|
|
|
|
&& "A binary operator creating a block address?");
|
2010-08-31 20:26:04 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
|
2010-12-05 19:06:41 +00:00
|
|
|
PredValueInfoTy LHSVals;
|
2010-12-06 23:36:56 +00:00
|
|
|
ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals,
|
|
|
|
WantInteger);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 07:36:34 +00:00
|
|
|
// Try to use constant folding to simplify the binary operator.
|
|
|
|
for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
|
2010-09-05 20:03:09 +00:00
|
|
|
Constant *V = LHSVals[i].first;
|
2010-08-31 20:26:04 +00:00
|
|
|
Constant *Folded = ConstantExpr::get(BO->getOpcode(), V, CI);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(Folded, WantInteger))
|
|
|
|
Result.push_back(std::make_pair(KC, LHSVals[i].second));
|
2010-08-31 07:36:34 +00:00
|
|
|
}
|
2010-08-26 17:40:24 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 07:36:34 +00:00
|
|
|
return !Result.empty();
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Handle compare with phi operand, where the PHI is defined in this block.
|
|
|
|
if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
|
2010-12-06 23:36:56 +00:00
|
|
|
assert(Preference == WantInteger && "Compares only produce integers");
|
2009-11-07 08:05:03 +00:00
|
|
|
PHINode *PN = dyn_cast<PHINode>(Cmp->getOperand(0));
|
|
|
|
if (PN && PN->getParent() == BB) {
|
|
|
|
// We can do this simplification if any comparisons fold to true or false.
|
|
|
|
// See if any do.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
BasicBlock *PredBB = PN->getIncomingBlock(i);
|
|
|
|
Value *LHS = PN->getIncomingValue(i);
|
|
|
|
Value *RHS = Cmp->getOperand(1)->DoPHITranslation(BB, PredBB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-11 22:31:38 +00:00
|
|
|
Value *Res = SimplifyCmpInst(Cmp->getPredicate(), LHS, RHS, TD);
|
2009-11-12 05:24:05 +00:00
|
|
|
if (Res == 0) {
|
2010-09-14 20:57:41 +00:00
|
|
|
if (!isa<Constant>(RHS))
|
2009-11-12 05:24:05 +00:00
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
LazyValueInfo::Tristate
|
2009-11-12 05:24:05 +00:00
|
|
|
ResT = LVI->getPredicateOnEdge(Cmp->getPredicate(), LHS,
|
|
|
|
cast<Constant>(RHS), PredBB, BB);
|
|
|
|
if (ResT == LazyValueInfo::Unknown)
|
|
|
|
continue;
|
|
|
|
Res = ConstantInt::get(Type::getInt1Ty(LHS->getContext()), ResT);
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(Res, WantInteger))
|
|
|
|
Result.push_back(std::make_pair(KC, PredBB));
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
return !Result.empty();
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
|
2009-11-11 22:31:38 +00:00
|
|
|
// If comparing a live-in value against a constant, see if we know the
|
|
|
|
// live-in value on any predecessors.
|
2010-09-14 20:57:41 +00:00
|
|
|
if (isa<Constant>(Cmp->getOperand(1)) && Cmp->getType()->isIntegerTy()) {
|
2010-08-26 17:40:24 +00:00
|
|
|
if (!isa<Instruction>(Cmp->getOperand(0)) ||
|
2010-08-30 23:22:36 +00:00
|
|
|
cast<Instruction>(Cmp->getOperand(0))->getParent() != BB) {
|
2010-08-26 17:40:24 +00:00
|
|
|
Constant *RHSCst = cast<Constant>(Cmp->getOperand(1));
|
2010-07-12 14:10:24 +00:00
|
|
|
|
2010-08-26 17:40:24 +00:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);PI != E; ++PI){
|
|
|
|
BasicBlock *P = *PI;
|
|
|
|
// If the value is known by LazyValueInfo to be a constant in a
|
|
|
|
// predecessor, use that information to try to thread this block.
|
|
|
|
LazyValueInfo::Tristate Res =
|
|
|
|
LVI->getPredicateOnEdge(Cmp->getPredicate(), Cmp->getOperand(0),
|
|
|
|
RHSCst, P, BB);
|
|
|
|
if (Res == LazyValueInfo::Unknown)
|
|
|
|
continue;
|
2009-11-12 04:37:50 +00:00
|
|
|
|
2010-08-26 17:40:24 +00:00
|
|
|
Constant *ResC = ConstantInt::get(Cmp->getType(), Res);
|
2010-12-05 19:06:41 +00:00
|
|
|
Result.push_back(std::make_pair(ResC, P));
|
2010-08-26 17:40:24 +00:00
|
|
|
}
|
2010-07-12 14:10:24 +00:00
|
|
|
|
2010-08-26 17:40:24 +00:00
|
|
|
return !Result.empty();
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 07:36:34 +00:00
|
|
|
// Try to find a constant value for the LHS of a comparison,
|
2010-08-26 17:40:24 +00:00
|
|
|
// and evaluate it statically if we can.
|
2010-08-30 23:22:36 +00:00
|
|
|
if (Constant *CmpConst = dyn_cast<Constant>(Cmp->getOperand(1))) {
|
2010-12-05 19:06:41 +00:00
|
|
|
PredValueInfoTy LHSVals;
|
2010-12-06 23:36:56 +00:00
|
|
|
ComputeValueKnownInPredecessors(I->getOperand(0), BB, LHSVals,
|
|
|
|
WantInteger);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-26 17:40:24 +00:00
|
|
|
for (unsigned i = 0, e = LHSVals.size(); i != e; ++i) {
|
2010-09-05 20:03:09 +00:00
|
|
|
Constant *V = LHSVals[i].first;
|
2010-08-31 20:26:04 +00:00
|
|
|
Constant *Folded = ConstantExpr::getCompare(Cmp->getPredicate(),
|
|
|
|
V, CmpConst);
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(Folded, WantInteger))
|
|
|
|
Result.push_back(std::make_pair(KC, LHSVals[i].second));
|
2010-08-26 17:40:24 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-26 17:40:24 +00:00
|
|
|
return !Result.empty();
|
|
|
|
}
|
2009-11-11 22:31:38 +00:00
|
|
|
}
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-15 09:51:20 +00:00
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
|
|
|
|
// Handle select instructions where at least one operand is a known constant
|
|
|
|
// and we can figure out the condition value for any predecessor block.
|
|
|
|
Constant *TrueVal = getKnownConstant(SI->getTrueValue(), Preference);
|
|
|
|
Constant *FalseVal = getKnownConstant(SI->getFalseValue(), Preference);
|
|
|
|
PredValueInfoTy Conds;
|
|
|
|
if ((TrueVal || FalseVal) &&
|
|
|
|
ComputeValueKnownInPredecessors(SI->getCondition(), BB, Conds,
|
|
|
|
WantInteger)) {
|
|
|
|
for (unsigned i = 0, e = Conds.size(); i != e; ++i) {
|
|
|
|
Constant *Cond = Conds[i].first;
|
|
|
|
|
|
|
|
// Figure out what value to use for the condition.
|
|
|
|
bool KnownCond;
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Cond)) {
|
|
|
|
// A known boolean.
|
|
|
|
KnownCond = CI->isOne();
|
|
|
|
} else {
|
|
|
|
assert(isa<UndefValue>(Cond) && "Unexpected condition value");
|
|
|
|
// Either operand will do, so be sure to pick the one that's a known
|
|
|
|
// constant.
|
|
|
|
// FIXME: Do this more cleverly if both values are known constants?
|
|
|
|
KnownCond = (TrueVal != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if the select has a known constant value for this predecessor.
|
|
|
|
if (Constant *Val = KnownCond ? TrueVal : FalseVal)
|
|
|
|
Result.push_back(std::make_pair(Val, Conds[i].second));
|
|
|
|
}
|
|
|
|
|
|
|
|
return !Result.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-14 20:57:41 +00:00
|
|
|
// If all else fails, see if LVI can figure out a constant value for us.
|
|
|
|
Constant *CI = LVI->getConstant(V, BB);
|
2010-12-06 23:36:56 +00:00
|
|
|
if (Constant *KC = getKnownConstant(CI, Preference)) {
|
2010-09-14 20:57:41 +00:00
|
|
|
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
|
2010-12-05 19:06:41 +00:00
|
|
|
Result.push_back(std::make_pair(KC, *PI));
|
2010-08-26 17:40:24 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-09-14 20:57:41 +00:00
|
|
|
return !Result.empty();
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
Teach jump threading to thread through blocks like:
br (and X, phi(Y, Z, false)), label L1, label L2
This triggers once on 252.eon and 6 times on 176.gcc. Blocks
in question often look like this:
bb262: ; preds = %bb261, %bb248
%iftmp.251.0 = phi i1 [ true, %bb261 ], [ false, %bb248 ] ; <i1> [#uses=4]
%tmp270 = icmp eq %struct.rtx_def* %tmp.0.i, null ; <i1> [#uses=1]
%bothcond = or i1 %iftmp.251.0, %tmp270 ; <i1> [#uses=1]
br i1 %bothcond, label %bb288, label %bb273
In this case, it is clear that it doesn't matter if tmp.0.i is null when coming from bb261. When coming from bb248, it is all that matters.
Another random example:
check_asm_operands.exit: ; preds = %check_asm_operands.exit.thr_comm, %bb30.i, %bb12.i, %bb6.i413
%tmp.0.i420 = phi i1 [ true, %bb6.i413 ], [ true, %bb12.i ], [ true, %bb30.i ], [ false, %check_asm_operands.exit.thr_comm ; <i1> [#uses=1]
call void @llvm.stackrestore( i8* %savedstack ) nounwind
%tmp4389 = icmp eq i32 %added_sets_1.0, 0 ; <i1> [#uses=1]
%tmp4394 = icmp eq i32 %added_sets_2.0, 0 ; <i1> [#uses=1]
%bothcond80 = and i1 %tmp4389, %tmp4394 ; <i1> [#uses=1]
%bothcond81 = and i1 %bothcond80, %tmp.0.i420 ; <i1> [#uses=1]
br i1 %bothcond81, label %bb4398, label %bb4397
Here is the case from 252.eon:
bb290.i.i: ; preds = %bb23.i57.i.i, %bb8.i39.i.i, %bb100.i.i, %bb100.i.i, %bb85.i.i110
%myEOF.1.i.i = phi i1 [ true, %bb100.i.i ], [ true, %bb100.i.i ], [ true, %bb85.i.i110 ], [ true, %bb8.i39.i.i ], [ false, %bb23.i57.i.i ] ; <i1> [#uses=2]
%i.4.i.i = phi i32 [ %i.1.i.i, %bb85.i.i110 ], [ %i.0.i.i, %bb100.i.i ], [ %i.0.i.i, %bb100.i.i ], [ %i.3.i.i, %bb8.i39.i.i ], [ %i.3.i.i, %bb23.i57.i.i ] ; <i32> [#uses=3]
%tmp292.i.i = load i8* %tmp16.i.i100, align 1 ; <i8> [#uses=1]
%tmp293.not.i.i = icmp ne i8 %tmp292.i.i, 0 ; <i1> [#uses=1]
%bothcond.i.i = and i1 %tmp293.not.i.i, %myEOF.1.i.i ; <i1> [#uses=1]
br i1 %bothcond.i.i, label %bb202.i.i, label %bb301.i.i
Factoring out 3 common predecessors.
On the path from any blocks other than bb23.i57.i.i, the load and compare
are dead.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50096 91177308-0d34-0410-b5e6-96231b3b80d8
2008-04-22 07:05:46 +00:00
|
|
|
|
2009-10-11 04:18:15 +00:00
|
|
|
/// GetBestDestForBranchOnUndef - If we determine that the specified block ends
|
|
|
|
/// in an undefined jump, decide which block is best to revector to.
|
|
|
|
///
|
|
|
|
/// Since we can pick an arbitrary destination, we pick the successor with the
|
|
|
|
/// fewest predecessors. This should reduce the in-degree of the others.
|
|
|
|
///
|
|
|
|
static unsigned GetBestDestForJumpOnUndef(BasicBlock *BB) {
|
|
|
|
TerminatorInst *BBTerm = BB->getTerminator();
|
|
|
|
unsigned MinSucc = 0;
|
|
|
|
BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc);
|
|
|
|
// Compute the successor with the minimum number of predecessors.
|
|
|
|
unsigned MinNumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB));
|
|
|
|
for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) {
|
|
|
|
TestBB = BBTerm->getSuccessor(i);
|
|
|
|
unsigned NumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB));
|
2011-06-27 21:51:12 +00:00
|
|
|
if (NumPreds < MinNumPreds) {
|
2009-10-11 04:18:15 +00:00
|
|
|
MinSucc = i;
|
2011-06-27 21:51:12 +00:00
|
|
|
MinNumPreds = NumPreds;
|
|
|
|
}
|
2009-10-11 04:18:15 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 04:18:15 +00:00
|
|
|
return MinSucc;
|
|
|
|
}
|
|
|
|
|
2011-02-18 04:43:06 +00:00
|
|
|
static bool hasAddressTakenAndUsed(BasicBlock *BB) {
|
|
|
|
if (!BB->hasAddressTaken()) return false;
|
2011-04-14 21:35:50 +00:00
|
|
|
|
2011-02-18 04:43:06 +00:00
|
|
|
// If the block has its address taken, it may be a tree of dead constants
|
|
|
|
// hanging off of it. These shouldn't keep the block alive.
|
|
|
|
BlockAddress *BA = BlockAddress::get(BB);
|
|
|
|
BA->removeDeadConstantUsers();
|
|
|
|
return !BA->use_empty();
|
|
|
|
}
|
|
|
|
|
2008-11-27 07:20:04 +00:00
|
|
|
/// ProcessBlock - If there are any predecessors whose control can be threaded
|
2008-04-20 21:13:06 +00:00
|
|
|
/// through to a successor, transform them now.
|
2008-11-27 07:20:04 +00:00
|
|
|
bool JumpThreading::ProcessBlock(BasicBlock *BB) {
|
2010-01-23 18:56:07 +00:00
|
|
|
// If the block is trivially dead, just return and let the caller nuke it.
|
|
|
|
// This simplifies other transformations.
|
|
|
|
if (pred_begin(BB) == pred_end(BB) &&
|
|
|
|
BB != &BB->getParent()->getEntryBlock())
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If this block has a single predecessor, and if that pred has a single
|
|
|
|
// successor, merge the blocks. This encourages recursive jump threading
|
|
|
|
// because now the condition in this block can be threaded through
|
|
|
|
// predecessors of our predecessor block.
|
2009-11-07 08:05:03 +00:00
|
|
|
if (BasicBlock *SinglePred = BB->getSinglePredecessor()) {
|
2008-11-28 19:54:49 +00:00
|
|
|
if (SinglePred->getTerminator()->getNumSuccessors() == 1 &&
|
2011-02-18 04:43:06 +00:00
|
|
|
SinglePred != BB && !hasAddressTakenAndUsed(BB)) {
|
2009-05-04 18:40:41 +00:00
|
|
|
// If SinglePred was a loop header, BB becomes one.
|
|
|
|
if (LoopHeaders.erase(SinglePred))
|
|
|
|
LoopHeaders.insert(BB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 19:25:19 +00:00
|
|
|
// Remember if SinglePred was the entry block of the function. If so, we
|
|
|
|
// will need to move BB back to the entry position.
|
|
|
|
bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
|
2010-09-14 20:57:41 +00:00
|
|
|
LVI->eraseBlock(SinglePred);
|
2008-11-27 05:07:53 +00:00
|
|
|
MergeBasicBlockIntoOnlyPred(BB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 19:25:19 +00:00
|
|
|
if (isEntry && BB != &BB->getParent()->getEntryBlock())
|
|
|
|
BB->moveBefore(&BB->getParent()->getEntryBlock());
|
2008-11-27 05:07:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
// What kind of constant we're looking for.
|
|
|
|
ConstantPreference Preference = WantInteger;
|
|
|
|
|
|
|
|
// Look to see if the terminator is a conditional branch, switch or indirect
|
|
|
|
// branch, if not we can't thread it.
|
2008-04-20 21:13:06 +00:00
|
|
|
Value *Condition;
|
2010-12-06 23:36:56 +00:00
|
|
|
Instruction *Terminator = BB->getTerminator();
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
|
2008-04-20 22:39:42 +00:00
|
|
|
// Can't thread an unconditional jump.
|
|
|
|
if (BI->isUnconditional()) return false;
|
2008-04-20 21:13:06 +00:00
|
|
|
Condition = BI->getCondition();
|
2010-12-06 23:36:56 +00:00
|
|
|
} else if (SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) {
|
2008-04-20 21:13:06 +00:00
|
|
|
Condition = SI->getCondition();
|
2010-12-06 23:36:56 +00:00
|
|
|
} else if (IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
|
2012-07-20 10:36:17 +00:00
|
|
|
// Can't thread indirect branch with no successors.
|
|
|
|
if (IB->getNumSuccessors() == 0) return false;
|
2010-12-06 23:36:56 +00:00
|
|
|
Condition = IB->getAddress()->stripPointerCasts();
|
|
|
|
Preference = WantBlockAddress;
|
|
|
|
} else {
|
2008-04-20 21:13:06 +00:00
|
|
|
return false; // Must be an invoke.
|
2010-12-06 23:36:56 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2011-04-14 21:35:50 +00:00
|
|
|
// Run constant folding to see if we can reduce the condition to a simple
|
|
|
|
// constant.
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Condition)) {
|
2011-12-02 01:26:24 +00:00
|
|
|
Value *SimpleVal = ConstantFoldInstruction(I, TD, TLI);
|
2011-04-14 21:35:50 +00:00
|
|
|
if (SimpleVal) {
|
|
|
|
I->replaceAllUsesWith(SimpleVal);
|
|
|
|
I->eraseFromParent();
|
|
|
|
Condition = SimpleVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-03 07:48:08 +00:00
|
|
|
// If the terminator is branching on an undef, we can pick any of the
|
2009-10-11 04:18:15 +00:00
|
|
|
// successors to branch to. Let GetBestDestForJumpOnUndef decide.
|
2008-12-03 07:48:08 +00:00
|
|
|
if (isa<UndefValue>(Condition)) {
|
2009-10-11 04:18:15 +00:00
|
|
|
unsigned BestSucc = GetBestDestForJumpOnUndef(BB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-12-03 07:48:08 +00:00
|
|
|
// Fold the branch/switch.
|
2009-10-11 04:18:15 +00:00
|
|
|
TerminatorInst *BBTerm = BB->getTerminator();
|
2008-12-03 07:48:08 +00:00
|
|
|
for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) {
|
2009-10-11 04:18:15 +00:00
|
|
|
if (i == BestSucc) continue;
|
2010-09-29 20:34:41 +00:00
|
|
|
BBTerm->getSuccessor(i)->removePredecessor(BB, true);
|
2008-12-03 07:48:08 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " In block '" << BB->getName()
|
2009-10-11 07:24:57 +00:00
|
|
|
<< "' folding undef terminator: " << *BBTerm << '\n');
|
2009-10-11 04:18:15 +00:00
|
|
|
BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
|
2008-12-03 07:48:08 +00:00
|
|
|
BBTerm->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
// If the terminator of this block is branching on a constant, simplify the
|
|
|
|
// terminator to an unconditional branch. This can occur due to threading in
|
|
|
|
// other blocks.
|
2010-12-06 23:36:56 +00:00
|
|
|
if (getKnownConstant(Condition, Preference)) {
|
2010-12-05 19:06:41 +00:00
|
|
|
DEBUG(dbgs() << " In block '" << BB->getName()
|
|
|
|
<< "' folding terminator: " << *BB->getTerminator() << '\n');
|
|
|
|
++NumFolds;
|
2011-05-22 16:24:18 +00:00
|
|
|
ConstantFoldTerminator(BB, true);
|
2010-12-05 19:06:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-03 07:48:08 +00:00
|
|
|
Instruction *CondInst = dyn_cast<Instruction>(Condition);
|
|
|
|
|
|
|
|
// All the rest of our checks depend on the condition being an instruction.
|
2009-11-12 01:41:34 +00:00
|
|
|
if (CondInst == 0) {
|
|
|
|
// FIXME: Unify this with code below.
|
2010-12-06 23:36:56 +00:00
|
|
|
if (ProcessThreadableEdges(Condition, BB, Preference))
|
2009-11-12 01:41:34 +00:00
|
|
|
return true;
|
2008-12-03 07:48:08 +00:00
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-19 04:56:29 +00:00
|
|
|
if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) {
|
2010-08-27 17:12:29 +00:00
|
|
|
// For a comparison where the LHS is outside this block, it's possible
|
2010-08-27 20:32:56 +00:00
|
|
|
// that we've branched on it before. Used LVI to see if we can simplify
|
2010-08-27 17:12:29 +00:00
|
|
|
// the branch based on that.
|
|
|
|
BranchInst *CondBr = dyn_cast<BranchInst>(BB->getTerminator());
|
|
|
|
Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1));
|
2010-08-31 18:48:48 +00:00
|
|
|
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
2010-09-14 20:57:41 +00:00
|
|
|
if (CondBr && CondConst && CondBr->isConditional() && PI != PE &&
|
2010-08-27 17:12:29 +00:00
|
|
|
(!isa<Instruction>(CondCmp->getOperand(0)) ||
|
|
|
|
cast<Instruction>(CondCmp->getOperand(0))->getParent() != BB)) {
|
|
|
|
// For predecessor edge, determine if the comparison is true or false
|
|
|
|
// on that edge. If they're all true or all false, we can simplify the
|
|
|
|
// branch.
|
|
|
|
// FIXME: We could handle mixed true/false by duplicating code.
|
2010-12-05 19:02:47 +00:00
|
|
|
LazyValueInfo::Tristate Baseline =
|
2010-08-31 18:48:48 +00:00
|
|
|
LVI->getPredicateOnEdge(CondCmp->getPredicate(), CondCmp->getOperand(0),
|
|
|
|
CondConst, *PI, BB);
|
|
|
|
if (Baseline != LazyValueInfo::Unknown) {
|
|
|
|
// Check that all remaining incoming values match the first one.
|
|
|
|
while (++PI != PE) {
|
2010-09-05 20:10:47 +00:00
|
|
|
LazyValueInfo::Tristate Ret =
|
|
|
|
LVI->getPredicateOnEdge(CondCmp->getPredicate(),
|
|
|
|
CondCmp->getOperand(0), CondConst, *PI, BB);
|
2010-08-31 18:48:48 +00:00
|
|
|
if (Ret != Baseline) break;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-08-31 18:48:48 +00:00
|
|
|
// If we terminated early, then one of the values didn't match.
|
|
|
|
if (PI == PE) {
|
|
|
|
unsigned ToRemove = Baseline == LazyValueInfo::True ? 1 : 0;
|
|
|
|
unsigned ToKeep = Baseline == LazyValueInfo::True ? 0 : 1;
|
2010-09-29 20:34:41 +00:00
|
|
|
CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true);
|
2010-08-31 18:48:48 +00:00
|
|
|
BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
|
|
|
|
CondBr->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-27 17:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-19 04:56:29 +00:00
|
|
|
}
|
2008-11-27 05:07:53 +00:00
|
|
|
|
|
|
|
// Check for some cases that are worth simplifying. Right now we want to look
|
|
|
|
// for loads that are used by a switch or by the condition for the branch. If
|
|
|
|
// we see one, check to see if it's partially redundant. If so, insert a PHI
|
|
|
|
// which can then be used to thread the values.
|
|
|
|
//
|
2008-12-03 07:48:08 +00:00
|
|
|
Value *SimplifyValue = CondInst;
|
2008-11-27 05:07:53 +00:00
|
|
|
if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
|
|
|
|
if (isa<Constant>(CondCmp->getOperand(1)))
|
|
|
|
SimplifyValue = CondCmp->getOperand(0);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-15 19:58:31 +00:00
|
|
|
// TODO: There are other places where load PRE would be profitable, such as
|
|
|
|
// more complex comparisons.
|
2008-11-27 05:07:53 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue))
|
|
|
|
if (SimplifyPartiallyRedundantLoad(LI))
|
|
|
|
return true;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Handle a variety of cases where we are branching on something derived from
|
|
|
|
// a PHI node in the current block. If we can prove that any predecessors
|
|
|
|
// compute a predictable value based on a PHI node, thread those predecessors.
|
|
|
|
//
|
2010-12-06 23:36:56 +00:00
|
|
|
if (ProcessThreadableEdges(CondInst, BB, Preference))
|
2009-11-11 02:08:33 +00:00
|
|
|
return true;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-11 23:41:09 +00:00
|
|
|
// If this is an otherwise-unfoldable branch on a phi node in the current
|
|
|
|
// block, see if we can simplify.
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(CondInst))
|
|
|
|
if (PN->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
|
|
|
|
return ProcessBranchOnPHI(PN);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// If this is an otherwise-unfoldable branch on a XOR, see if we can simplify.
|
|
|
|
if (CondInst->getOpcode() == Instruction::Xor &&
|
|
|
|
CondInst->getParent() == BB && isa<BranchInst>(BB->getTerminator()))
|
|
|
|
return ProcessBranchOnXOR(cast<BinaryOperator>(CondInst));
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// TODO: If we have: "br (X > 0)" and we have a predecessor where we know
|
2010-01-11 23:41:09 +00:00
|
|
|
// "(X == 4)", thread through this block.
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-22 06:36:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-04 06:31:07 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
/// SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant
|
|
|
|
/// load instruction, eliminate it by replacing it with a PHI node. This is an
|
|
|
|
/// important optimization that encourages jump threading, and needs to be run
|
|
|
|
/// interlaced with other jump threading tasks.
|
|
|
|
bool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) {
|
2011-09-12 20:23:13 +00:00
|
|
|
// Don't hack volatile/atomic loads.
|
|
|
|
if (!LI->isSimple()) return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If the load is defined in a block with exactly one predecessor, it can't be
|
|
|
|
// partially redundant.
|
|
|
|
BasicBlock *LoadBB = LI->getParent();
|
|
|
|
if (LoadBB->getSinglePredecessor())
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
Value *LoadedPtr = LI->getOperand(0);
|
|
|
|
|
|
|
|
// If the loaded operand is defined in the LoadBB, it can't be available.
|
2009-11-15 19:58:31 +00:00
|
|
|
// TODO: Could do simple PHI translation, that would be fun :)
|
2008-11-27 05:07:53 +00:00
|
|
|
if (Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr))
|
|
|
|
if (PtrOp->getParent() == LoadBB)
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// Scan a few instructions up from the load, to see if it is obviously live at
|
|
|
|
// the entry to its block.
|
|
|
|
BasicBlock::iterator BBIt = LI;
|
|
|
|
|
2010-12-05 19:02:47 +00:00
|
|
|
if (Value *AvailableVal =
|
2009-11-15 19:58:31 +00:00
|
|
|
FindAvailableLoadedValue(LoadedPtr, LoadBB, BBIt, 6)) {
|
2008-11-27 05:07:53 +00:00
|
|
|
// If the value if the load is locally available within the block, just use
|
|
|
|
// it. This frequently occurs for reg2mem'd allocas.
|
|
|
|
//cerr << "LOAD ELIMINATED:\n" << *BBIt << *LI << "\n";
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-01-09 06:08:12 +00:00
|
|
|
// If the returned value is the load itself, replace with an undef. This can
|
|
|
|
// only happen in dead loops.
|
2009-07-30 23:03:37 +00:00
|
|
|
if (AvailableVal == LI) AvailableVal = UndefValue::get(LI->getType());
|
2008-11-27 05:07:53 +00:00
|
|
|
LI->replaceAllUsesWith(AvailableVal);
|
|
|
|
LI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if we scanned the whole block and got to the top of the block,
|
|
|
|
// we know the block is locally transparent to the load. If not, something
|
|
|
|
// might clobber its value.
|
|
|
|
if (BBIt != LoadBB->begin())
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2012-03-13 18:07:41 +00:00
|
|
|
// If all of the loads and stores that feed the value have the same TBAA tag,
|
|
|
|
// then we can propagate it onto any newly inserted loads.
|
2012-07-24 10:51:42 +00:00
|
|
|
MDNode *TBAATag = LI->getMetadata(LLVMContext::MD_tbaa);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 8> PredsScanned;
|
|
|
|
typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy;
|
|
|
|
AvailablePredsTy AvailablePreds;
|
|
|
|
BasicBlock *OneUnavailablePred = 0;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If we got here, the loaded value is transparent through to the start of the
|
|
|
|
// block. Check to see if it is available in any of the predecessor blocks.
|
|
|
|
for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
BasicBlock *PredBB = *PI;
|
|
|
|
|
|
|
|
// If we already scanned this predecessor, skip it.
|
|
|
|
if (!PredsScanned.insert(PredBB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Scan the predecessor to see if the value is available in the pred.
|
|
|
|
BBIt = PredBB->end();
|
2012-03-13 18:07:41 +00:00
|
|
|
MDNode *ThisTBAATag = 0;
|
|
|
|
Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 6,
|
|
|
|
0, &ThisTBAATag);
|
2008-11-27 05:07:53 +00:00
|
|
|
if (!PredAvailable) {
|
|
|
|
OneUnavailablePred = PredBB;
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2012-03-13 18:07:41 +00:00
|
|
|
// If tbaa tags disagree or are not present, forget about them.
|
|
|
|
if (TBAATag != ThisTBAATag) TBAATag = 0;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If so, this load is partially redundant. Remember this info so that we
|
|
|
|
// can create a PHI node.
|
|
|
|
AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable));
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If the loaded value isn't available in any predecessor, it isn't partially
|
|
|
|
// redundant.
|
|
|
|
if (AvailablePreds.empty()) return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// Okay, the loaded value is available in at least one (and maybe all!)
|
|
|
|
// predecessors. If the value is unavailable in more than one unique
|
|
|
|
// predecessor, we want to insert a merge block for those common predecessors.
|
|
|
|
// This ensures that we only have to insert one reload, thus not increasing
|
|
|
|
// code size.
|
|
|
|
BasicBlock *UnavailablePred = 0;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If there is exactly one predecessor where the value is unavailable, the
|
|
|
|
// already computed 'OneUnavailablePred' block is it. If it ends in an
|
|
|
|
// unconditional branch, we know that it isn't a critical edge.
|
|
|
|
if (PredsScanned.size() == AvailablePreds.size()+1 &&
|
|
|
|
OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) {
|
|
|
|
UnavailablePred = OneUnavailablePred;
|
|
|
|
} else if (PredsScanned.size() != AvailablePreds.size()) {
|
|
|
|
// Otherwise, we had multiple unavailable predecessors or we had a critical
|
|
|
|
// edge from the one.
|
|
|
|
SmallVector<BasicBlock*, 8> PredsToSplit;
|
|
|
|
SmallPtrSet<BasicBlock*, 8> AvailablePredSet;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = AvailablePreds.size(); i != e; ++i)
|
|
|
|
AvailablePredSet.insert(AvailablePreds[i].first);
|
|
|
|
|
|
|
|
// Add all the unavailable predecessors to the PredsToSplit list.
|
|
|
|
for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB);
|
2010-06-14 19:45:43 +00:00
|
|
|
PI != PE; ++PI) {
|
2010-07-12 14:10:24 +00:00
|
|
|
BasicBlock *P = *PI;
|
2010-06-14 19:45:43 +00:00
|
|
|
// If the predecessor is an indirect goto, we can't split the edge.
|
2010-07-12 14:10:24 +00:00
|
|
|
if (isa<IndirectBrInst>(P->getTerminator()))
|
2010-06-14 19:45:43 +00:00
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-07-12 14:10:24 +00:00
|
|
|
if (!AvailablePredSet.count(P))
|
|
|
|
PredsToSplit.push_back(P);
|
2010-06-14 19:45:43 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// Split them out to their own block.
|
|
|
|
UnavailablePred =
|
2011-12-09 21:19:53 +00:00
|
|
|
SplitBlockPredecessors(LoadBB, PredsToSplit, "thread-pre-split", this);
|
2008-11-27 05:07:53 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// If the value isn't available in all predecessors, then there will be
|
|
|
|
// exactly one where it isn't available. Insert a load on that edge and add
|
|
|
|
// it to the AvailablePreds list.
|
|
|
|
if (UnavailablePred) {
|
|
|
|
assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 &&
|
|
|
|
"Can't handle critical edge here!");
|
2011-05-04 22:48:19 +00:00
|
|
|
LoadInst *NewVal = new LoadInst(LoadedPtr, LI->getName()+".pr", false,
|
2009-11-15 19:58:31 +00:00
|
|
|
LI->getAlignment(),
|
2008-11-27 05:07:53 +00:00
|
|
|
UnavailablePred->getTerminator());
|
2011-05-04 22:48:19 +00:00
|
|
|
NewVal->setDebugLoc(LI->getDebugLoc());
|
2012-03-13 18:07:41 +00:00
|
|
|
if (TBAATag)
|
|
|
|
NewVal->setMetadata(LLVMContext::MD_tbaa, TBAATag);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal));
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// Now we know that each predecessor of this block has a value in
|
|
|
|
// AvailablePreds, sort them for efficient access as we're walking the preds.
|
2008-12-01 06:52:57 +00:00
|
|
|
array_pod_sort(AvailablePreds.begin(), AvailablePreds.end());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// Create a PHI node at the start of the block for the PRE'd load value.
|
2011-03-30 11:19:20 +00:00
|
|
|
pred_iterator PB = pred_begin(LoadBB), PE = pred_end(LoadBB);
|
2011-03-30 11:28:46 +00:00
|
|
|
PHINode *PN = PHINode::Create(LI->getType(), std::distance(PB, PE), "",
|
|
|
|
LoadBB->begin());
|
2008-11-27 05:07:53 +00:00
|
|
|
PN->takeName(LI);
|
2011-05-04 22:48:19 +00:00
|
|
|
PN->setDebugLoc(LI->getDebugLoc());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
// Insert new entries into the PHI for each predecessor. A single block may
|
|
|
|
// have multiple entries here.
|
2011-03-30 11:19:20 +00:00
|
|
|
for (pred_iterator PI = PB; PI != PE; ++PI) {
|
2010-07-12 14:10:24 +00:00
|
|
|
BasicBlock *P = *PI;
|
2010-12-05 19:02:47 +00:00
|
|
|
AvailablePredsTy::iterator I =
|
2008-11-27 05:07:53 +00:00
|
|
|
std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(),
|
2010-07-12 14:10:24 +00:00
|
|
|
std::make_pair(P, (Value*)0));
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-07-12 14:10:24 +00:00
|
|
|
assert(I != AvailablePreds.end() && I->first == P &&
|
2008-11-27 05:07:53 +00:00
|
|
|
"Didn't find entry for predecessor!");
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
PN->addIncoming(I->second, I->first);
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
//cerr << "PRE: " << *LI << *PN << "\n";
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
LI->replaceAllUsesWith(PN);
|
|
|
|
LI->eraseFromParent();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-11-27 05:07:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
/// FindMostPopularDest - The specified list contains multiple possible
|
|
|
|
/// threadable destinations. Pick the one that occurs the most frequently in
|
|
|
|
/// the list.
|
|
|
|
static BasicBlock *
|
|
|
|
FindMostPopularDest(BasicBlock *BB,
|
|
|
|
const SmallVectorImpl<std::pair<BasicBlock*,
|
|
|
|
BasicBlock*> > &PredToDestList) {
|
|
|
|
assert(!PredToDestList.empty());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Determine popularity. If there are multiple possible destinations, we
|
|
|
|
// explicitly choose to ignore 'undef' destinations. We prefer to thread
|
|
|
|
// blocks with known and real destinations to threading undef. We'll handle
|
|
|
|
// them later if interesting.
|
|
|
|
DenseMap<BasicBlock*, unsigned> DestPopularity;
|
|
|
|
for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i)
|
|
|
|
if (PredToDestList[i].second)
|
|
|
|
DestPopularity[PredToDestList[i].second]++;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Find the most popular dest.
|
|
|
|
DenseMap<BasicBlock*, unsigned>::iterator DPI = DestPopularity.begin();
|
|
|
|
BasicBlock *MostPopularDest = DPI->first;
|
|
|
|
unsigned Popularity = DPI->second;
|
|
|
|
SmallVector<BasicBlock*, 4> SamePopularity;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
for (++DPI; DPI != DestPopularity.end(); ++DPI) {
|
|
|
|
// If the popularity of this entry isn't higher than the popularity we've
|
|
|
|
// seen so far, ignore it.
|
|
|
|
if (DPI->second < Popularity)
|
|
|
|
; // ignore.
|
|
|
|
else if (DPI->second == Popularity) {
|
|
|
|
// If it is the same as what we've seen so far, keep track of it.
|
|
|
|
SamePopularity.push_back(DPI->first);
|
|
|
|
} else {
|
|
|
|
// If it is more popular, remember it.
|
|
|
|
SamePopularity.clear();
|
|
|
|
MostPopularDest = DPI->first;
|
|
|
|
Popularity = DPI->second;
|
2010-12-05 19:02:47 +00:00
|
|
|
}
|
2009-10-11 07:24:57 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-16 12:16:00 +00:00
|
|
|
// Okay, now we know the most popular destination. If there is more than one
|
2009-11-07 08:05:03 +00:00
|
|
|
// destination, we need to determine one. This is arbitrary, but we need
|
|
|
|
// to make a deterministic decision. Pick the first one that appears in the
|
|
|
|
// successor list.
|
|
|
|
if (!SamePopularity.empty()) {
|
|
|
|
SamePopularity.push_back(MostPopularDest);
|
|
|
|
TerminatorInst *TI = BB->getTerminator();
|
|
|
|
for (unsigned i = 0; ; ++i) {
|
|
|
|
assert(i != TI->getNumSuccessors() && "Didn't find any successor!");
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
if (std::find(SamePopularity.begin(), SamePopularity.end(),
|
|
|
|
TI->getSuccessor(i)) == SamePopularity.end())
|
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
MostPopularDest = TI->getSuccessor(i);
|
2009-11-07 01:32:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Okay, we have finally picked the most popular destination.
|
|
|
|
return MostPopularDest;
|
|
|
|
}
|
|
|
|
|
2010-12-06 23:36:56 +00:00
|
|
|
bool JumpThreading::ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
|
|
|
|
ConstantPreference Preference) {
|
2009-11-07 08:05:03 +00:00
|
|
|
// If threading this would thread across a loop header, don't even try to
|
|
|
|
// thread the edge.
|
|
|
|
if (LoopHeaders.count(BB))
|
2009-11-07 01:32:59 +00:00
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
PredValueInfoTy PredValues;
|
2010-12-06 23:36:56 +00:00
|
|
|
if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference))
|
2009-11-07 01:32:59 +00:00
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
assert(!PredValues.empty() &&
|
|
|
|
"ComputeValueKnownInPredecessors returned true with no values");
|
|
|
|
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << "IN BB: " << *BB;
|
2009-11-07 08:05:03 +00:00
|
|
|
for (unsigned i = 0, e = PredValues.size(); i != e; ++i) {
|
2010-12-05 19:06:41 +00:00
|
|
|
dbgs() << " BB '" << BB->getName() << "': FOUND condition = "
|
|
|
|
<< *PredValues[i].first
|
|
|
|
<< " for pred '" << PredValues[i].second->getName() << "'.\n";
|
2009-11-07 08:05:03 +00:00
|
|
|
});
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Decide what we want to thread through. Convert our list of known values to
|
|
|
|
// a list of known destinations for each pred. This also discards duplicate
|
|
|
|
// predecessors and keeps track of the undefined inputs (which are represented
|
2009-11-09 00:41:49 +00:00
|
|
|
// as a null dest in the PredToDestList).
|
2009-11-07 08:05:03 +00:00
|
|
|
SmallPtrSet<BasicBlock*, 16> SeenPreds;
|
|
|
|
SmallVector<std::pair<BasicBlock*, BasicBlock*>, 16> PredToDestList;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
BasicBlock *OnlyDest = 0;
|
|
|
|
BasicBlock *MultipleDestSentinel = (BasicBlock*)(intptr_t)~0ULL;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
for (unsigned i = 0, e = PredValues.size(); i != e; ++i) {
|
|
|
|
BasicBlock *Pred = PredValues[i].second;
|
|
|
|
if (!SeenPreds.insert(Pred))
|
|
|
|
continue; // Duplicate predecessor entry.
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// If the predecessor ends with an indirect goto, we can't change its
|
|
|
|
// destination.
|
|
|
|
if (isa<IndirectBrInst>(Pred->getTerminator()))
|
2009-11-07 01:32:59 +00:00
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
Constant *Val = PredValues[i].first;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
BasicBlock *DestBB;
|
2010-12-05 19:06:41 +00:00
|
|
|
if (isa<UndefValue>(Val))
|
2009-11-07 08:05:03 +00:00
|
|
|
DestBB = 0;
|
|
|
|
else if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()))
|
2010-12-05 19:06:41 +00:00
|
|
|
DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->isZero());
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149481 91177308-0d34-0410-b5e6-96231b3b80d8
2012-02-01 07:49:51 +00:00
|
|
|
else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
|
2012-03-08 07:06:20 +00:00
|
|
|
DestBB = SI->findCaseValue(cast<ConstantInt>(Val)).getCaseSuccessor();
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149481 91177308-0d34-0410-b5e6-96231b3b80d8
2012-02-01 07:49:51 +00:00
|
|
|
} else {
|
2010-12-06 23:36:56 +00:00
|
|
|
assert(isa<IndirectBrInst>(BB->getTerminator())
|
|
|
|
&& "Unexpected terminator");
|
|
|
|
DestBB = cast<BlockAddress>(Val)->getBasicBlock();
|
2009-11-07 01:32:59 +00:00
|
|
|
}
|
2009-11-07 08:05:03 +00:00
|
|
|
|
|
|
|
// If we have exactly one destination, remember it for efficiency below.
|
2010-12-16 12:16:00 +00:00
|
|
|
if (PredToDestList.empty())
|
2009-11-07 08:05:03 +00:00
|
|
|
OnlyDest = DestBB;
|
|
|
|
else if (OnlyDest != DestBB)
|
|
|
|
OnlyDest = MultipleDestSentinel;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
PredToDestList.push_back(std::make_pair(Pred, DestBB));
|
2009-11-07 01:32:59 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// If all edges were unthreadable, we fail.
|
|
|
|
if (PredToDestList.empty())
|
2009-11-07 01:32:59 +00:00
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Determine which is the most common successor. If we have many inputs and
|
|
|
|
// this block is a switch, we want to start by threading the batch that goes
|
|
|
|
// to the most popular destination first. If we only know about one
|
|
|
|
// threadable destination (the common case) we can avoid this.
|
|
|
|
BasicBlock *MostPopularDest = OnlyDest;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
if (MostPopularDest == MultipleDestSentinel)
|
|
|
|
MostPopularDest = FindMostPopularDest(BB, PredToDestList);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// Now that we know what the most popular destination is, factor all
|
|
|
|
// predecessors that will jump to it into a single predecessor.
|
|
|
|
SmallVector<BasicBlock*, 16> PredsToFactor;
|
|
|
|
for (unsigned i = 0, e = PredToDestList.size(); i != e; ++i)
|
|
|
|
if (PredToDestList[i].second == MostPopularDest) {
|
|
|
|
BasicBlock *Pred = PredToDestList[i].first;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// This predecessor may be a switch or something else that has multiple
|
|
|
|
// edges to the block. Factor each of these edges by listing them
|
|
|
|
// according to # occurrences in PredsToFactor.
|
|
|
|
TerminatorInst *PredTI = Pred->getTerminator();
|
|
|
|
for (unsigned i = 0, e = PredTI->getNumSuccessors(); i != e; ++i)
|
|
|
|
if (PredTI->getSuccessor(i) == BB)
|
|
|
|
PredsToFactor.push_back(Pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the threadable edges are branching on an undefined value, we get to pick
|
|
|
|
// the destination that these predecessors should get to.
|
|
|
|
if (MostPopularDest == 0)
|
|
|
|
MostPopularDest = BB->getTerminator()->
|
|
|
|
getSuccessor(GetBestDestForJumpOnUndef(BB));
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 01:32:59 +00:00
|
|
|
// Ok, try to thread it!
|
2009-11-07 08:05:03 +00:00
|
|
|
return ThreadEdge(BB, PredsToFactor, MostPopularDest);
|
|
|
|
}
|
|
|
|
|
2010-01-11 23:41:09 +00:00
|
|
|
/// ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on
|
|
|
|
/// a PHI node in the current block. See if there are any simplifications we
|
|
|
|
/// can do based on inputs to the phi node.
|
2010-12-05 19:02:47 +00:00
|
|
|
///
|
2010-01-11 23:41:09 +00:00
|
|
|
bool JumpThreading::ProcessBranchOnPHI(PHINode *PN) {
|
2009-11-07 08:05:03 +00:00
|
|
|
BasicBlock *BB = PN->getParent();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// TODO: We could make use of this to do it once for blocks with common PHI
|
|
|
|
// values.
|
|
|
|
SmallVector<BasicBlock*, 1> PredBBs;
|
|
|
|
PredBBs.resize(1);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// If any of the predecessor blocks end in an unconditional branch, we can
|
2010-01-11 23:41:09 +00:00
|
|
|
// *duplicate* the conditional branch into that block in order to further
|
|
|
|
// encourage jump threading and to eliminate cases where we have branch on a
|
|
|
|
// phi of an icmp (branch on icmp is much better).
|
2009-11-07 08:05:03 +00:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
|
|
|
|
BasicBlock *PredBB = PN->getIncomingBlock(i);
|
|
|
|
if (BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator()))
|
2010-01-12 02:07:17 +00:00
|
|
|
if (PredBr->isUnconditional()) {
|
|
|
|
PredBBs[0] = PredBB;
|
|
|
|
// Try to duplicate BB into PredBB.
|
|
|
|
if (DuplicateCondBranchOnPHIIntoPred(BB, PredBBs))
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-11-07 01:32:59 +00:00
|
|
|
}
|
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
/// ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on
|
|
|
|
/// a xor instruction in the current block. See if there are any
|
|
|
|
/// simplifications we can do based on inputs to the xor.
|
2010-12-05 19:02:47 +00:00
|
|
|
///
|
2010-01-12 02:07:17 +00:00
|
|
|
bool JumpThreading::ProcessBranchOnXOR(BinaryOperator *BO) {
|
|
|
|
BasicBlock *BB = BO->getParent();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// If either the LHS or RHS of the xor is a constant, don't do this
|
|
|
|
// optimization.
|
|
|
|
if (isa<ConstantInt>(BO->getOperand(0)) ||
|
|
|
|
isa<ConstantInt>(BO->getOperand(1)))
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-23 19:16:25 +00:00
|
|
|
// If the first instruction in BB isn't a phi, we won't be able to infer
|
|
|
|
// anything special about any particular predecessor.
|
|
|
|
if (!isa<PHINode>(BB->front()))
|
|
|
|
return false;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// If we have a xor as the branch input to this block, and we know that the
|
|
|
|
// LHS or RHS of the xor in any predecessor is true/false, then we can clone
|
|
|
|
// the condition into the predecessor and fix that value to true, saving some
|
|
|
|
// logical ops on that path and encouraging other paths to simplify.
|
|
|
|
//
|
|
|
|
// This copies something like this:
|
|
|
|
//
|
|
|
|
// BB:
|
|
|
|
// %X = phi i1 [1], [%X']
|
|
|
|
// %Y = icmp eq i32 %A, %B
|
|
|
|
// %Z = xor i1 %X, %Y
|
|
|
|
// br i1 %Z, ...
|
|
|
|
//
|
|
|
|
// Into:
|
|
|
|
// BB':
|
|
|
|
// %Y = icmp ne i32 %A, %B
|
|
|
|
// br i1 %Z, ...
|
|
|
|
|
2010-12-05 19:06:41 +00:00
|
|
|
PredValueInfoTy XorOpValues;
|
2010-01-12 02:07:17 +00:00
|
|
|
bool isLHS = true;
|
2010-12-06 23:36:56 +00:00
|
|
|
if (!ComputeValueKnownInPredecessors(BO->getOperand(0), BB, XorOpValues,
|
|
|
|
WantInteger)) {
|
2010-01-12 02:07:17 +00:00
|
|
|
assert(XorOpValues.empty());
|
2010-12-06 23:36:56 +00:00
|
|
|
if (!ComputeValueKnownInPredecessors(BO->getOperand(1), BB, XorOpValues,
|
|
|
|
WantInteger))
|
2010-01-12 02:07:17 +00:00
|
|
|
return false;
|
|
|
|
isLHS = false;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
assert(!XorOpValues.empty() &&
|
|
|
|
"ComputeValueKnownInPredecessors returned true with no values");
|
|
|
|
|
|
|
|
// Scan the information to see which is most popular: true or false. The
|
|
|
|
// predecessors can be of the set true, false, or undef.
|
|
|
|
unsigned NumTrue = 0, NumFalse = 0;
|
|
|
|
for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) {
|
2010-12-05 19:06:41 +00:00
|
|
|
if (isa<UndefValue>(XorOpValues[i].first))
|
|
|
|
// Ignore undefs for the count.
|
|
|
|
continue;
|
|
|
|
if (cast<ConstantInt>(XorOpValues[i].first)->isZero())
|
2010-01-12 02:07:17 +00:00
|
|
|
++NumFalse;
|
|
|
|
else
|
|
|
|
++NumTrue;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// Determine which value to split on, true, false, or undef if neither.
|
|
|
|
ConstantInt *SplitVal = 0;
|
|
|
|
if (NumTrue > NumFalse)
|
|
|
|
SplitVal = ConstantInt::getTrue(BB->getContext());
|
|
|
|
else if (NumTrue != 0 || NumFalse != 0)
|
|
|
|
SplitVal = ConstantInt::getFalse(BB->getContext());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// Collect all of the blocks that this can be folded into so that we can
|
|
|
|
// factor this once and clone it once.
|
|
|
|
SmallVector<BasicBlock*, 8> BlocksToFoldInto;
|
|
|
|
for (unsigned i = 0, e = XorOpValues.size(); i != e; ++i) {
|
2010-12-05 19:06:41 +00:00
|
|
|
if (XorOpValues[i].first != SplitVal &&
|
|
|
|
!isa<UndefValue>(XorOpValues[i].first))
|
|
|
|
continue;
|
2010-01-12 02:07:17 +00:00
|
|
|
|
|
|
|
BlocksToFoldInto.push_back(XorOpValues[i].second);
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-23 19:16:25 +00:00
|
|
|
// If we inferred a value for all of the predecessors, then duplication won't
|
|
|
|
// help us. However, we can just replace the LHS or RHS with the constant.
|
|
|
|
if (BlocksToFoldInto.size() ==
|
|
|
|
cast<PHINode>(BB->front()).getNumIncomingValues()) {
|
|
|
|
if (SplitVal == 0) {
|
|
|
|
// If all preds provide undef, just nuke the xor, because it is undef too.
|
|
|
|
BO->replaceAllUsesWith(UndefValue::get(BO->getType()));
|
|
|
|
BO->eraseFromParent();
|
|
|
|
} else if (SplitVal->isZero()) {
|
|
|
|
// If all preds provide 0, replace the xor with the other input.
|
|
|
|
BO->replaceAllUsesWith(BO->getOperand(isLHS));
|
|
|
|
BO->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
// If all preds provide 1, set the computed value to 1.
|
|
|
|
BO->setOperand(!isLHS, SplitVal);
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-23 19:16:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// Try to duplicate BB into PredBB.
|
2010-01-12 02:07:50 +00:00
|
|
|
return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto);
|
2010-01-12 02:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
/// AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new
|
|
|
|
/// predecessor to the PHIBB block. If it has PHI nodes, add entries for
|
|
|
|
/// NewPred using the entries from OldPred (suitably mapped).
|
|
|
|
static void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB,
|
|
|
|
BasicBlock *OldPred,
|
|
|
|
BasicBlock *NewPred,
|
|
|
|
DenseMap<Instruction*, Value*> &ValueMap) {
|
|
|
|
for (BasicBlock::iterator PNI = PHIBB->begin();
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(PNI); ++PNI) {
|
|
|
|
// Ok, we have a PHI node. Figure out what the incoming value was for the
|
|
|
|
// DestBlock.
|
|
|
|
Value *IV = PN->getIncomingValueForBlock(OldPred);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Remap the value if necessary.
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(IV)) {
|
|
|
|
DenseMap<Instruction*, Value*>::iterator I = ValueMap.find(Inst);
|
|
|
|
if (I != ValueMap.end())
|
|
|
|
IV = I->second;
|
2009-10-11 04:33:43 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
PN->addIncoming(IV, NewPred);
|
2009-10-11 04:33:43 +00:00
|
|
|
}
|
2009-05-04 18:40:41 +00:00
|
|
|
}
|
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
/// ThreadEdge - We have decided that it is safe and profitable to factor the
|
|
|
|
/// blocks in PredBBs to one predecessor, then thread an edge from it to SuccBB
|
|
|
|
/// across BB. Transform the IR to reflect this change.
|
2010-12-05 19:02:47 +00:00
|
|
|
bool JumpThreading::ThreadEdge(BasicBlock *BB,
|
|
|
|
const SmallVectorImpl<BasicBlock*> &PredBBs,
|
2009-10-11 04:33:43 +00:00
|
|
|
BasicBlock *SuccBB) {
|
Don't infininitely thread branches when a threaded edge
goes back to the block, e.g.:
Threading edge through bool from 'bb37.us.thread3829' to 'bb37.us' with cost: 1, across block:
bb37.us: ; preds = %bb37.us.thread3829, %bb37.us, %bb33
%D1361.1.us = phi i32 [ %tmp36, %bb33 ], [ %D1361.1.us, %bb37.us ], [ 0, %bb37.us.thread3829 ] ; <i32> [#uses=2]
%tmp39.us = icmp eq i32 %D1361.1.us, 0 ; <i1> [#uses=1]
br i1 %tmp39.us, label %bb37.us, label %bb42.us
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50251 91177308-0d34-0410-b5e6-96231b3b80d8
2008-04-25 04:12:29 +00:00
|
|
|
// If threading to the same block as we come from, we would infinite loop.
|
|
|
|
if (SuccBB == BB) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Not threading across BB '" << BB->getName()
|
2009-07-26 07:49:05 +00:00
|
|
|
<< "' - would thread to self!\n");
|
Don't infininitely thread branches when a threaded edge
goes back to the block, e.g.:
Threading edge through bool from 'bb37.us.thread3829' to 'bb37.us' with cost: 1, across block:
bb37.us: ; preds = %bb37.us.thread3829, %bb37.us, %bb33
%D1361.1.us = phi i32 [ %tmp36, %bb33 ], [ %D1361.1.us, %bb37.us ], [ 0, %bb37.us.thread3829 ] ; <i32> [#uses=2]
%tmp39.us = icmp eq i32 %D1361.1.us, 0 ; <i1> [#uses=1]
br i1 %tmp39.us, label %bb37.us, label %bb42.us
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50251 91177308-0d34-0410-b5e6-96231b3b80d8
2008-04-25 04:12:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-05-04 18:40:41 +00:00
|
|
|
// If threading this would thread across a loop header, don't thread the edge.
|
|
|
|
// See the comments above FindLoopHeaders for justifications and caveats.
|
|
|
|
if (LoopHeaders.count(BB)) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Not threading across loop header BB '" << BB->getName()
|
2009-07-26 07:49:05 +00:00
|
|
|
<< "' to dest BB '" << SuccBB->getName()
|
|
|
|
<< "' - it might create an irreducible loop!\n");
|
2009-05-04 18:40:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB);
|
|
|
|
if (JumpThreadCost > Threshold) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Not threading BB '" << BB->getName()
|
2009-10-11 07:24:57 +00:00
|
|
|
<< "' - Cost is too high: " << JumpThreadCost << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-11-07 08:05:03 +00:00
|
|
|
// And finally, do it! Start by factoring the predecessors is needed.
|
|
|
|
BasicBlock *PredBB;
|
|
|
|
if (PredBBs.size() == 1)
|
|
|
|
PredBB = PredBBs[0];
|
|
|
|
else {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Factoring out " << PredBBs.size()
|
2009-11-07 08:05:03 +00:00
|
|
|
<< " common predecessors.\n");
|
2011-12-09 21:19:53 +00:00
|
|
|
PredBB = SplitBlockPredecessors(BB, PredBBs, ".thr_comm", this);
|
2009-11-07 08:05:03 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
Start doing the significantly useful part of jump threading: handle cases
where a comparison has a phi input and that phi is a constant. For example,
stuff like:
Threading edge through bool from 'bb2149' to 'bb2231' with cost: 1, across block:
bb2237: ; preds = %bb2231, %bb2149
%tmp2328.rle = phi i32 [ %tmp2232, %bb2231 ], [ %tmp2232439, %bb2149 ] ; <i32> [#uses=2]
%done.0 = phi i32 [ %done.2, %bb2231 ], [ 0, %bb2149 ] ; <i32> [#uses=1]
%tmp2239 = icmp eq i32 %done.0, 0 ; <i1> [#uses=1]
br i1 %tmp2239, label %bb2231, label %bb2327
or
bb38.i298: ; preds = %bb33.i295, %bb1693
%tmp39.i296.rle = phi %struct.ibox* [ null, %bb1693 ], [ %tmp39.i296.rle1109, %bb33.i295 ] ; <%struct.ibox*> [#uses=2]
%minspan.1.i291.reg2mem.1 = phi i32 [ 32000, %bb1693 ], [ %minspan.0.i288, %bb33.i295 ] ; <i32> [#uses=1]
%tmp40.i297 = icmp eq %struct.ibox* %tmp39.i296.rle, null ; <i1> [#uses=1]
br i1 %tmp40.i297, label %implfeeds.exit311, label %bb43.i301
This triggers thousands of times in spec.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50110 91177308-0d34-0410-b5e6-96231b3b80d8
2008-04-22 21:40:39 +00:00
|
|
|
// And finally, do it!
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Threading edge from '" << PredBB->getName() << "' to '"
|
2009-07-26 09:48:23 +00:00
|
|
|
<< SuccBB->getName() << "' with cost: " << JumpThreadCost
|
2009-07-26 07:49:05 +00:00
|
|
|
<< ", across block:\n "
|
|
|
|
<< *BB << "\n");
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-09-14 20:57:41 +00:00
|
|
|
LVI->threadEdge(PredBB, BB, SuccBB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
// We are going to have to map operands from the original BB block to the new
|
|
|
|
// copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to
|
|
|
|
// account for entry from PredBB.
|
|
|
|
DenseMap<Instruction*, Value*> ValueMapping;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
BasicBlock *NewBB = BasicBlock::Create(BB->getContext(),
|
|
|
|
BB->getName()+".thread",
|
2009-08-13 21:58:54 +00:00
|
|
|
BB->getParent(), BB);
|
2008-04-20 22:39:42 +00:00
|
|
|
NewBB->moveAfter(PredBB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
BasicBlock::iterator BI = BB->begin();
|
|
|
|
for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
|
|
|
|
ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
// Clone the non-phi instructions of BB into NewBB, keeping track of the
|
|
|
|
// mapping and using it to remap operands in the cloned instructions.
|
|
|
|
for (; !isa<TerminatorInst>(BI); ++BI) {
|
2009-09-27 07:38:41 +00:00
|
|
|
Instruction *New = BI->clone();
|
2009-07-26 09:48:23 +00:00
|
|
|
New->setName(BI->getName());
|
2008-04-20 22:39:42 +00:00
|
|
|
NewBB->getInstList().push_back(New);
|
|
|
|
ValueMapping[BI] = New;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
// Remap operands to patch up intra-block references.
|
|
|
|
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
|
2009-07-02 00:17:47 +00:00
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
|
|
|
|
DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
|
|
|
|
if (I != ValueMapping.end())
|
|
|
|
New->setOperand(i, I->second);
|
|
|
|
}
|
2008-04-20 22:39:42 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
// We didn't copy the terminator from BB over to NewBB, because there is now
|
|
|
|
// an unconditional jump to SuccBB. Insert the unconditional jump.
|
2011-05-04 22:48:19 +00:00
|
|
|
BranchInst *NewBI =BranchInst::Create(SuccBB, NewBB);
|
|
|
|
NewBI->setDebugLoc(BB->getTerminator()->getDebugLoc());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-04-20 22:39:42 +00:00
|
|
|
// Check to see if SuccBB has PHI nodes. If so, we need to add entries to the
|
|
|
|
// PHI nodes for NewBB now.
|
2009-10-11 07:24:57 +00:00
|
|
|
AddPHINodeEntriesForMappedBlock(SuccBB, BB, NewBB, ValueMapping);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-10 09:05:58 +00:00
|
|
|
// If there were values defined in BB that are used outside the block, then we
|
|
|
|
// now have to update all uses of the value to use either the original value,
|
|
|
|
// the cloned value, or some PHI derived value. This can require arbitrary
|
|
|
|
// PHI insertion, of which we are prepared to do, clean these up now.
|
|
|
|
SSAUpdater SSAUpdate;
|
|
|
|
SmallVector<Use*, 16> UsesToRename;
|
|
|
|
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
|
|
|
|
// Scan all uses of this instruction to see if it is used outside of its
|
|
|
|
// block, and if so, record them in UsesToRename.
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
|
|
|
|
++UI) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI);
|
|
|
|
if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
|
|
|
|
if (UserPN->getIncomingBlock(UI) == BB)
|
|
|
|
continue;
|
|
|
|
} else if (User->getParent() == BB)
|
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-10 09:05:58 +00:00
|
|
|
UsesToRename.push_back(&UI.getUse());
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-10 09:05:58 +00:00
|
|
|
// If there are no uses outside the block, we're done with this instruction.
|
|
|
|
if (UsesToRename.empty())
|
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n");
|
2009-10-10 09:05:58 +00:00
|
|
|
|
|
|
|
// We found a use of I outside of BB. Rename all uses of I that are outside
|
|
|
|
// its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
|
|
|
|
// with the two values we know.
|
2010-09-02 08:14:03 +00:00
|
|
|
SSAUpdate.Initialize(I->getType(), I->getName());
|
2009-10-10 09:05:58 +00:00
|
|
|
SSAUpdate.AddAvailableValue(BB, I);
|
|
|
|
SSAUpdate.AddAvailableValue(NewBB, ValueMapping[I]);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-10 09:05:58 +00:00
|
|
|
while (!UsesToRename.empty())
|
|
|
|
SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << "\n");
|
2009-10-10 09:05:58 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
|
|
|
|
2008-12-01 04:48:07 +00:00
|
|
|
// Ok, NewBB is good to go. Update the terminator of PredBB to jump to
|
2008-04-20 22:39:42 +00:00
|
|
|
// NewBB instead of BB. This eliminates predecessors from BB, which requires
|
|
|
|
// us to simplify any PHI nodes in BB.
|
|
|
|
TerminatorInst *PredTerm = PredBB->getTerminator();
|
|
|
|
for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i)
|
|
|
|
if (PredTerm->getSuccessor(i) == BB) {
|
2010-09-29 20:34:41 +00:00
|
|
|
BB->removePredecessor(PredBB, true);
|
2008-04-20 22:39:42 +00:00
|
|
|
PredTerm->setSuccessor(i, NewBB);
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2008-12-01 04:48:07 +00:00
|
|
|
// At this point, the IR is fully up to date and consistent. Do a quick scan
|
|
|
|
// over the new instructions and zap any that are constants or dead. This
|
|
|
|
// frequently happens because of phi translation.
|
2010-01-12 20:41:47 +00:00
|
|
|
SimplifyInstructionsInBlock(NewBB, TD);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-05-04 18:40:41 +00:00
|
|
|
// Threaded an edge!
|
|
|
|
++NumThreads;
|
|
|
|
return true;
|
2008-04-20 21:13:06 +00:00
|
|
|
}
|
2009-10-11 07:24:57 +00:00
|
|
|
|
|
|
|
/// DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch
|
|
|
|
/// to BB which contains an i1 PHI node and a conditional branch on that PHI.
|
|
|
|
/// If we can duplicate the contents of BB up into PredBB do so now, this
|
|
|
|
/// improves the odds that the branch will be on an analyzable instruction like
|
|
|
|
/// a compare.
|
|
|
|
bool JumpThreading::DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB,
|
2010-01-12 02:07:17 +00:00
|
|
|
const SmallVectorImpl<BasicBlock *> &PredBBs) {
|
|
|
|
assert(!PredBBs.empty() && "Can't handle an empty set");
|
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// If BB is a loop header, then duplicating this block outside the loop would
|
|
|
|
// cause us to transform this into an irreducible loop, don't do this.
|
|
|
|
// See the comments above FindLoopHeaders for justifications and caveats.
|
|
|
|
if (LoopHeaders.count(BB)) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Not duplicating loop header '" << BB->getName()
|
2010-01-12 02:07:17 +00:00
|
|
|
<< "' into predecessor block '" << PredBBs[0]->getName()
|
2009-10-11 07:24:57 +00:00
|
|
|
<< "' - it might create an irreducible loop!\n");
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
unsigned DuplicationCost = getJumpThreadDuplicationCost(BB);
|
|
|
|
if (DuplicationCost > Threshold) {
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Not duplicating BB '" << BB->getName()
|
2009-10-11 07:24:57 +00:00
|
|
|
<< "' - Cost is too high: " << DuplicationCost << "\n");
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// And finally, do it! Start by factoring the predecessors is needed.
|
|
|
|
BasicBlock *PredBB;
|
|
|
|
if (PredBBs.size() == 1)
|
|
|
|
PredBB = PredBBs[0];
|
|
|
|
else {
|
|
|
|
DEBUG(dbgs() << " Factoring out " << PredBBs.size()
|
|
|
|
<< " common predecessors.\n");
|
2011-12-09 21:19:53 +00:00
|
|
|
PredBB = SplitBlockPredecessors(BB, PredBBs, ".thr_comm", this);
|
2010-01-12 02:07:17 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Okay, we decided to do this! Clone all the instructions in BB onto the end
|
|
|
|
// of PredBB.
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << " Duplicating block '" << BB->getName() << "' into end of '"
|
2009-10-11 07:24:57 +00:00
|
|
|
<< PredBB->getName() << "' to eliminate branch on phi. Cost: "
|
|
|
|
<< DuplicationCost << " block is:" << *BB << "\n");
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-12 02:07:17 +00:00
|
|
|
// Unless PredBB ends with an unconditional branch, split the edge so that we
|
|
|
|
// can just clone the bits from BB into the end of the new PredBB.
|
2010-01-23 19:21:31 +00:00
|
|
|
BranchInst *OldPredBranch = dyn_cast<BranchInst>(PredBB->getTerminator());
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-23 19:21:31 +00:00
|
|
|
if (OldPredBranch == 0 || !OldPredBranch->isUnconditional()) {
|
2010-01-12 02:07:17 +00:00
|
|
|
PredBB = SplitEdge(PredBB, BB, this);
|
|
|
|
OldPredBranch = cast<BranchInst>(PredBB->getTerminator());
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// We are going to have to map operands from the original BB block into the
|
|
|
|
// PredBB block. Evaluate PHI nodes in BB.
|
|
|
|
DenseMap<Instruction*, Value*> ValueMapping;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
BasicBlock::iterator BI = BB->begin();
|
|
|
|
for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
|
|
|
|
ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Clone the non-phi instructions of BB into PredBB, keeping track of the
|
|
|
|
// mapping and using it to remap operands in the cloned instructions.
|
|
|
|
for (; BI != BB->end(); ++BI) {
|
|
|
|
Instruction *New = BI->clone();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Remap operands to patch up intra-block references.
|
|
|
|
for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
|
|
|
|
DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst);
|
|
|
|
if (I != ValueMapping.end())
|
|
|
|
New->setOperand(i, I->second);
|
|
|
|
}
|
2010-01-12 20:41:47 +00:00
|
|
|
|
|
|
|
// If this instruction can be simplified after the operands are updated,
|
|
|
|
// just use the simplified value instead. This frequently happens due to
|
|
|
|
// phi translation.
|
|
|
|
if (Value *IV = SimplifyInstruction(New, TD)) {
|
|
|
|
delete New;
|
|
|
|
ValueMapping[BI] = IV;
|
|
|
|
} else {
|
|
|
|
// Otherwise, insert the new instruction into the block.
|
|
|
|
New->setName(BI->getName());
|
|
|
|
PredBB->getInstList().insert(OldPredBranch, New);
|
|
|
|
ValueMapping[BI] = New;
|
|
|
|
}
|
2009-10-11 07:24:57 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Check to see if the targets of the branch had PHI nodes. If so, we need to
|
|
|
|
// add entries to the PHI nodes for branch from PredBB now.
|
|
|
|
BranchInst *BBBranch = cast<BranchInst>(BB->getTerminator());
|
|
|
|
AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(0), BB, PredBB,
|
|
|
|
ValueMapping);
|
|
|
|
AddPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
|
|
|
|
ValueMapping);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// If there were values defined in BB that are used outside the block, then we
|
|
|
|
// now have to update all uses of the value to use either the original value,
|
|
|
|
// the cloned value, or some PHI derived value. This can require arbitrary
|
|
|
|
// PHI insertion, of which we are prepared to do, clean these up now.
|
|
|
|
SSAUpdater SSAUpdate;
|
|
|
|
SmallVector<Use*, 16> UsesToRename;
|
|
|
|
for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
|
|
|
|
// Scan all uses of this instruction to see if it is used outside of its
|
|
|
|
// block, and if so, record them in UsesToRename.
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
|
|
|
|
++UI) {
|
|
|
|
Instruction *User = cast<Instruction>(*UI);
|
|
|
|
if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
|
|
|
|
if (UserPN->getIncomingBlock(UI) == BB)
|
|
|
|
continue;
|
|
|
|
} else if (User->getParent() == BB)
|
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
UsesToRename.push_back(&UI.getUse());
|
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// If there are no uses outside the block, we're done with this instruction.
|
|
|
|
if (UsesToRename.empty())
|
|
|
|
continue;
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << "JT: Renaming non-local uses of: " << *I << "\n");
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// We found a use of I outside of BB. Rename all uses of I that are outside
|
|
|
|
// its block to be uses of the appropriate PHI node etc. See ValuesInBlocks
|
|
|
|
// with the two values we know.
|
2010-09-02 08:14:03 +00:00
|
|
|
SSAUpdate.Initialize(I->getType(), I->getName());
|
2009-10-11 07:24:57 +00:00
|
|
|
SSAUpdate.AddAvailableValue(BB, I);
|
|
|
|
SSAUpdate.AddAvailableValue(PredBB, ValueMapping[I]);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
while (!UsesToRename.empty())
|
|
|
|
SSAUpdate.RewriteUse(*UsesToRename.pop_back_val());
|
2010-01-05 01:27:19 +00:00
|
|
|
DEBUG(dbgs() << "\n");
|
2009-10-11 07:24:57 +00:00
|
|
|
}
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// PredBB no longer jumps to BB, remove entries in the PHI node for the edge
|
|
|
|
// that we nuked.
|
2010-09-29 20:34:41 +00:00
|
|
|
BB->removePredecessor(PredBB, true);
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
// Remove the unconditional branch at the end of the PredBB block.
|
|
|
|
OldPredBranch->eraseFromParent();
|
2010-12-05 19:02:47 +00:00
|
|
|
|
2009-10-11 07:24:57 +00:00
|
|
|
++NumDupes;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|