Skip to content

Commit

Permalink
[SimplifyCFG] Use BB::instructionsWithoutDebug to skip DbgInfo (NFC).
Browse files Browse the repository at this point in the history
This patch updates some code responsible the skip debug info to use
BasicBlock::instructionsWithoutDebug. I think this makes things slightly
simpler and more direct.

Reviewers: aprantl, vsk, hans, danielcdh

Reviewed By: hans

Differential Revision: https://reviews.llvm.org/D46252


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331221 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
fhahn committed Apr 30, 2018
1 parent 8b88721 commit 557a92c
Showing 1 changed file with 14 additions and 37 deletions.
51 changes: 14 additions & 37 deletions lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,12 +1864,9 @@ static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,

// Look for a store to the same pointer in BrBB.
unsigned MaxNumInstToLookAt = 9;
for (Instruction &CurI : reverse(*BrBB)) {
for (Instruction &CurI : reverse(BrBB->instructionsWithoutDebug())) {
if (!MaxNumInstToLookAt)
break;
// Skip debug info.
if (isa<DbgInfoIntrinsic>(CurI))
continue;
--MaxNumInstToLookAt;

// Could be calling an instruction that affects memory like free().
Expand Down Expand Up @@ -2119,19 +2116,16 @@ static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB,

/// Return true if we can thread a branch across this block.
static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
BranchInst *BI = cast<BranchInst>(BB->getTerminator());
unsigned Size = 0;

for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
if (isa<DbgInfoIntrinsic>(BBI))
continue;
for (Instruction &I : BB->instructionsWithoutDebug()) {
if (Size > 10)
return false; // Don't clone large BB's.
++Size;

// We can only support instructions that do not define values that are
// live outside of the current basic block.
for (User *U : BBI->users()) {
for (User *U : I.users()) {
Instruction *UI = cast<Instruction>(U);
if (UI->getParent() != BB || isa<PHINode>(UI))
return false;
Expand Down Expand Up @@ -2920,14 +2914,13 @@ static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB,
// instructions inside are all cheap (arithmetic/GEPs), it's worthwhile to
// thread this store.
unsigned N = 0;
for (auto &I : *BB) {
for (auto &I : BB->instructionsWithoutDebug()) {
// Cheap instructions viable for folding.
if (isa<BinaryOperator>(I) || isa<GetElementPtrInst>(I) ||
isa<StoreInst>(I))
++N;
// Free instructions.
else if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(I) ||
IsaBitcastOfPointerType(I))
else if (isa<TerminatorInst>(I) || IsaBitcastOfPointerType(I))
continue;
else
return false;
Expand Down Expand Up @@ -3232,11 +3225,9 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
// If this is a conditional branch in an empty block, and if any
// predecessors are a conditional branch to one of our destinations,
// fold the conditions into logical ops and one cond br.
BasicBlock::iterator BBI = BB->begin();

// Ignore dbg intrinsics.
while (isa<DbgInfoIntrinsic>(BBI))
++BBI;
if (&*BBI != BI)
if (&*BB->instructionsWithoutDebug().begin() != BI)
return false;

int PBIOp, BIOp;
Expand Down Expand Up @@ -4639,24 +4630,20 @@ GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
// which we can constant-propagate the CaseVal, continue to its successor.
SmallDenseMap<Value *, Constant *> ConstantPool;
ConstantPool.insert(std::make_pair(SI->getCondition(), CaseVal));
for (BasicBlock::iterator I = CaseDest->begin(), E = CaseDest->end(); I != E;
++I) {
if (TerminatorInst *T = dyn_cast<TerminatorInst>(I)) {
for (Instruction &I :CaseDest->instructionsWithoutDebug()) {
if (TerminatorInst *T = dyn_cast<TerminatorInst>(&I)) {
// If the terminator is a simple branch, continue to the next block.
if (T->getNumSuccessors() != 1 || T->isExceptional())
return false;
Pred = CaseDest;
CaseDest = T->getSuccessor(0);
} else if (isa<DbgInfoIntrinsic>(I)) {
// Skip debug intrinsic.
continue;
} else if (Constant *C = ConstantFold(&*I, DL, ConstantPool)) {
} else if (Constant *C = ConstantFold(&I, DL, ConstantPool)) {
// Instruction is side-effect free and constant.

// If the instruction has uses outside this block or a phi node slot for
// the block, it is not safe to bypass the instruction since it would then
// no longer dominate all its uses.
for (auto &Use : I->uses()) {
for (auto &Use : I.uses()) {
User *User = Use.getUser();
if (Instruction *I = dyn_cast<Instruction>(User))
if (I->getParent() == CaseDest)
Expand All @@ -4667,7 +4654,7 @@ GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest,
return false;
}

ConstantPool.insert(std::make_pair(&*I, C));
ConstantPool.insert(std::make_pair(&I, C));
} else {
break;
}
Expand Down Expand Up @@ -5602,11 +5589,7 @@ bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {

// If the block only contains the switch, see if we can fold the block
// away into any preds.
BasicBlock::iterator BBI = BB->begin();
// Ignore dbg intrinsics.
while (isa<DbgInfoIntrinsic>(BBI))
++BBI;
if (SI == &*BBI)
if (SI == &*BB->instructionsWithoutDebug().begin())
if (FoldValueComparisonIntoPredecessors(SI, Builder))
return simplifyCFG(BB, TTI, Options) | true;
}
Expand Down Expand Up @@ -5833,18 +5816,12 @@ bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {

// This block must be empty, except for the setcond inst, if it exists.
// Ignore dbg intrinsics.
BasicBlock::iterator I = BB->begin();
// Ignore dbg intrinsics.
while (isa<DbgInfoIntrinsic>(I))
++I;
auto I = BB->instructionsWithoutDebug().begin();
if (&*I == BI) {
if (FoldValueComparisonIntoPredecessors(BI, Builder))
return simplifyCFG(BB, TTI, Options) | true;
} else if (&*I == cast<Instruction>(BI->getCondition())) {
++I;
// Ignore dbg intrinsics.
while (isa<DbgInfoIntrinsic>(I))
++I;
if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
return simplifyCFG(BB, TTI, Options) | true;
}
Expand Down

0 comments on commit 557a92c

Please sign in to comment.