mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-27 02:31:09 +00:00
Implement PR1786 by iterating between dead cycle elimination
and simplifycfg in the rare cases when it is needed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44044 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
41fcea3bdb
commit
b42c8f7101
@ -27,6 +27,7 @@
|
|||||||
#include "llvm/Support/CFG.h"
|
#include "llvm/Support/CFG.h"
|
||||||
#include "llvm/Support/Compiler.h"
|
#include "llvm/Support/Compiler.h"
|
||||||
#include "llvm/Pass.h"
|
#include "llvm/Pass.h"
|
||||||
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include "llvm/ADT/SmallPtrSet.h"
|
#include "llvm/ADT/SmallPtrSet.h"
|
||||||
#include "llvm/ADT/Statistic.h"
|
#include "llvm/ADT/Statistic.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
@ -50,9 +51,9 @@ FunctionPass *llvm::createCFGSimplificationPass() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool MarkAliveBlocks(BasicBlock *BB,
|
static bool MarkAliveBlocks(BasicBlock *BB,
|
||||||
SmallPtrSet<BasicBlock*, 16> &Reachable) {
|
SmallPtrSet<BasicBlock*, 128> &Reachable) {
|
||||||
|
|
||||||
std::vector<BasicBlock*> Worklist;
|
SmallVector<BasicBlock*, 128> Worklist;
|
||||||
Worklist.push_back(BB);
|
Worklist.push_back(BB);
|
||||||
bool Changed = false;
|
bool Changed = false;
|
||||||
while (!Worklist.empty()) {
|
while (!Worklist.empty()) {
|
||||||
@ -93,38 +94,42 @@ static bool MarkAliveBlocks(BasicBlock *BB,
|
|||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// RemoveUnreachableBlocks - Remove blocks that are not reachable, even if they
|
||||||
// It is possible that we may require multiple passes over the code to fully
|
/// are in a dead cycle. Return true if a change was made, false otherwise.
|
||||||
// simplify the CFG.
|
static bool RemoveUnreachableBlocks(Function &F) {
|
||||||
//
|
SmallPtrSet<BasicBlock*, 128> Reachable;
|
||||||
bool CFGSimplifyPass::runOnFunction(Function &F) {
|
|
||||||
SmallPtrSet<BasicBlock*, 16> Reachable;
|
|
||||||
bool Changed = MarkAliveBlocks(F.begin(), Reachable);
|
bool Changed = MarkAliveBlocks(F.begin(), Reachable);
|
||||||
|
|
||||||
// If there are unreachable blocks in the CFG...
|
// If there are unreachable blocks in the CFG...
|
||||||
if (Reachable.size() != F.size()) {
|
if (Reachable.size() == F.size())
|
||||||
assert(Reachable.size() < F.size());
|
return Changed;
|
||||||
NumSimpl += F.size()-Reachable.size();
|
|
||||||
|
|
||||||
// Loop over all of the basic blocks that are not reachable, dropping all of
|
assert(Reachable.size() < F.size());
|
||||||
// their internal references...
|
NumSimpl += F.size()-Reachable.size();
|
||||||
for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
|
|
||||||
if (!Reachable.count(BB)) {
|
|
||||||
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
|
|
||||||
if (Reachable.count(*SI))
|
|
||||||
(*SI)->removePredecessor(BB);
|
|
||||||
BB->dropAllReferences();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Function::iterator I = ++F.begin(); I != F.end();)
|
// Loop over all of the basic blocks that are not reachable, dropping all of
|
||||||
if (!Reachable.count(I))
|
// their internal references...
|
||||||
I = F.getBasicBlockList().erase(I);
|
for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB)
|
||||||
else
|
if (!Reachable.count(BB)) {
|
||||||
++I;
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI!=SE; ++SI)
|
||||||
|
if (Reachable.count(*SI))
|
||||||
|
(*SI)->removePredecessor(BB);
|
||||||
|
BB->dropAllReferences();
|
||||||
|
}
|
||||||
|
|
||||||
Changed = true;
|
for (Function::iterator I = ++F.begin(); I != F.end();)
|
||||||
}
|
if (!Reachable.count(I))
|
||||||
|
I = F.getBasicBlockList().erase(I);
|
||||||
|
else
|
||||||
|
++I;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// IterativeSimplifyCFG - Call SimplifyCFG on all the blocks in the function,
|
||||||
|
/// iterating until no more changes are made.
|
||||||
|
static bool IterativeSimplifyCFG(Function &F) {
|
||||||
|
bool Changed = false;
|
||||||
bool LocalChange = true;
|
bool LocalChange = true;
|
||||||
while (LocalChange) {
|
while (LocalChange) {
|
||||||
LocalChange = false;
|
LocalChange = false;
|
||||||
@ -140,6 +145,31 @@ bool CFGSimplifyPass::runOnFunction(Function &F) {
|
|||||||
}
|
}
|
||||||
Changed |= LocalChange;
|
Changed |= LocalChange;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// It is possible that we may require multiple passes over the code to fully
|
||||||
|
// simplify the CFG.
|
||||||
|
//
|
||||||
|
bool CFGSimplifyPass::runOnFunction(Function &F) {
|
||||||
|
bool EverChanged = RemoveUnreachableBlocks(F);
|
||||||
|
EverChanged |= IterativeSimplifyCFG(F);
|
||||||
|
|
||||||
|
// If neither pass changed anything, we're done.
|
||||||
|
if (!EverChanged) return false;
|
||||||
|
|
||||||
|
// IterativeSimplifyCFG can (rarely) make some loops dead. If this happens,
|
||||||
|
// RemoveUnreachableBlocks is needed to nuke them, which means we should
|
||||||
|
// iterate between the two optimizations. We structure the code like this to
|
||||||
|
// avoid reruning IterativeSimplifyCFG if the second pass of
|
||||||
|
// RemoveUnreachableBlocks doesn't do anything.
|
||||||
|
if (!RemoveUnreachableBlocks(F))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
do {
|
||||||
|
EverChanged = IterativeSimplifyCFG(F);
|
||||||
|
EverChanged |= RemoveUnreachableBlocks(F);
|
||||||
|
} while (EverChanged);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
100
test/Transforms/SimplifyCFG/iterative-simplify.ll
Normal file
100
test/Transforms/SimplifyCFG/iterative-simplify.ll
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | not grep bb17
|
||||||
|
; PR1786
|
||||||
|
|
||||||
|
define i32 @main() {
|
||||||
|
entry:
|
||||||
|
%retval = alloca i32, align 4 ; <i32*> [#uses=1]
|
||||||
|
%i = alloca i32, align 4 ; <i32*> [#uses=7]
|
||||||
|
%z = alloca i32, align 4 ; <i32*> [#uses=4]
|
||||||
|
%z16 = alloca i32, align 4 ; <i32*> [#uses=4]
|
||||||
|
%"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
|
||||||
|
store i32 0, i32* %i
|
||||||
|
%toBool = icmp ne i8 1, 0 ; <i1> [#uses=1]
|
||||||
|
br i1 %toBool, label %cond_true, label %cond_false
|
||||||
|
|
||||||
|
cond_true: ; preds = %entry
|
||||||
|
store i32 0, i32* %z
|
||||||
|
br label %bb
|
||||||
|
|
||||||
|
bb: ; preds = %cond_next, %cond_true
|
||||||
|
%tmp = load i32* %z ; <i32> [#uses=1]
|
||||||
|
%tmp1 = sub i32 %tmp, 16384 ; <i32> [#uses=1]
|
||||||
|
store i32 %tmp1, i32* %z
|
||||||
|
%tmp2 = load i32* %i ; <i32> [#uses=1]
|
||||||
|
%tmp3 = add i32 %tmp2, 1 ; <i32> [#uses=1]
|
||||||
|
store i32 %tmp3, i32* %i
|
||||||
|
%tmp4 = load i32* %i ; <i32> [#uses=1]
|
||||||
|
%tmp5 = icmp sgt i32 %tmp4, 262144 ; <i1> [#uses=1]
|
||||||
|
%tmp56 = zext i1 %tmp5 to i8 ; <i8> [#uses=1]
|
||||||
|
%toBool7 = icmp ne i8 %tmp56, 0 ; <i1> [#uses=1]
|
||||||
|
br i1 %toBool7, label %cond_true8, label %cond_next
|
||||||
|
|
||||||
|
cond_true8: ; preds = %bb
|
||||||
|
call void @abort( )
|
||||||
|
unreachable
|
||||||
|
|
||||||
|
cond_next: ; preds = %bb
|
||||||
|
%tmp9 = load i32* %z ; <i32> [#uses=1]
|
||||||
|
%tmp10 = icmp ne i32 %tmp9, 0 ; <i1> [#uses=1]
|
||||||
|
%tmp1011 = zext i1 %tmp10 to i8 ; <i8> [#uses=1]
|
||||||
|
%toBool12 = icmp ne i8 %tmp1011, 0 ; <i1> [#uses=1]
|
||||||
|
br i1 %toBool12, label %bb, label %bb13
|
||||||
|
|
||||||
|
bb13: ; preds = %cond_next
|
||||||
|
call void @exit( i32 0 )
|
||||||
|
unreachable
|
||||||
|
|
||||||
|
cond_false: ; preds = %entry
|
||||||
|
%toBool14 = icmp ne i8 1, 0 ; <i1> [#uses=1]
|
||||||
|
br i1 %toBool14, label %cond_true15, label %cond_false33
|
||||||
|
|
||||||
|
cond_true15: ; preds = %cond_false
|
||||||
|
store i32 0, i32* %z16
|
||||||
|
br label %bb17
|
||||||
|
|
||||||
|
bb17: ; preds = %cond_next27, %cond_true15
|
||||||
|
%tmp18 = load i32* %z16 ; <i32> [#uses=1]
|
||||||
|
%tmp19 = sub i32 %tmp18, 16384 ; <i32> [#uses=1]
|
||||||
|
store i32 %tmp19, i32* %z16
|
||||||
|
%tmp20 = load i32* %i ; <i32> [#uses=1]
|
||||||
|
%tmp21 = add i32 %tmp20, 1 ; <i32> [#uses=1]
|
||||||
|
store i32 %tmp21, i32* %i
|
||||||
|
%tmp22 = load i32* %i ; <i32> [#uses=1]
|
||||||
|
%tmp23 = icmp sgt i32 %tmp22, 262144 ; <i1> [#uses=1]
|
||||||
|
%tmp2324 = zext i1 %tmp23 to i8 ; <i8> [#uses=1]
|
||||||
|
%toBool25 = icmp ne i8 %tmp2324, 0 ; <i1> [#uses=1]
|
||||||
|
br i1 %toBool25, label %cond_true26, label %cond_next27
|
||||||
|
|
||||||
|
cond_true26: ; preds = %bb17
|
||||||
|
call void @abort( )
|
||||||
|
unreachable
|
||||||
|
|
||||||
|
cond_next27: ; preds = %bb17
|
||||||
|
%tmp28 = load i32* %z16 ; <i32> [#uses=1]
|
||||||
|
%tmp29 = icmp ne i32 %tmp28, 0 ; <i1> [#uses=1]
|
||||||
|
%tmp2930 = zext i1 %tmp29 to i8 ; <i8> [#uses=1]
|
||||||
|
%toBool31 = icmp ne i8 %tmp2930, 0 ; <i1> [#uses=1]
|
||||||
|
br i1 %toBool31, label %bb17, label %bb32
|
||||||
|
|
||||||
|
bb32: ; preds = %cond_next27
|
||||||
|
call void @exit( i32 0 )
|
||||||
|
unreachable
|
||||||
|
|
||||||
|
cond_false33: ; preds = %cond_false
|
||||||
|
call void @exit( i32 0 )
|
||||||
|
unreachable
|
||||||
|
|
||||||
|
cond_next34: ; No predecessors!
|
||||||
|
br label %cond_next35
|
||||||
|
|
||||||
|
cond_next35: ; preds = %cond_next34
|
||||||
|
br label %return
|
||||||
|
|
||||||
|
return: ; preds = %cond_next35
|
||||||
|
%retval36 = load i32* %retval ; <i32> [#uses=1]
|
||||||
|
ret i32 %retval36
|
||||||
|
}
|
||||||
|
|
||||||
|
declare void @abort()
|
||||||
|
|
||||||
|
declare void @exit(i32)
|
Loading…
x
Reference in New Issue
Block a user