Skip to content

Commit bce93c3

Browse files
committed
[TSAR, Transform] Add a pass to remove redundant edges in a CFG.
1 parent 6eac4f9 commit bce93c3

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- RedundantEdgeElimination.h - Redundant Edge Elimination --*- C++ -*-===//
2+
//
3+
// Traits Static Analyzer (SAPFOR)
4+
//
5+
// Copyright 2018 DVM System Group
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
//
21+
// This file defines a pass to replace the always true conditional branch
22+
// instructions with an unconditional branch instruction.
23+
//
24+
//===----------------------------------------------------------------------===//
25+
26+
#ifndef TSAR_REDUNDANT_EDGE_ELIMINATION_H
27+
#define TSAR_REDUNDANT_EDGE_ELIMINATION_H
28+
29+
#include "tsar/Transform/IR/Passes.h"
30+
#include <bcl/utility.h>
31+
#include <llvm/Pass.h>
32+
33+
namespace llvm {
34+
class RedundantEdgeEliminationPass :
35+
public FunctionPass, private bcl::Uncopyable {
36+
public:
37+
static char ID;
38+
39+
RedundantEdgeEliminationPass() : FunctionPass(ID) {
40+
initializeRedundantEdgeEliminationPassPass(*PassRegistry::getPassRegistry());
41+
}
42+
43+
bool runOnFunction(Function &F) override;
44+
};
45+
}
46+
#endif//TSAR_REDUNDANT_EDGE_ELIMINATION_H
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===-RedundantEdgeElimination.cpp - Redundant Edge Elimination -*- C++ -*-===//
2+
//
3+
// Traits Static Analyzer (SAPFOR)
4+
//
5+
// Copyright 2018 DVM System Group
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
//
21+
// This file implements a pass to replace the always true conditional branch
22+
// instructions with an unconditional branch instruction.
23+
//
24+
//===----------------------------------------------------------------------===//
25+
26+
#include "tsar/Transform/IR/RedundantEdgeElimination.h"
27+
#include <bcl/utility.h>
28+
#include <llvm/IR/Instructions.h>
29+
#include <llvm/Pass.h>
30+
#include <llvm/Support/Debug.h>
31+
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
32+
33+
#undef DEBUG_TYPE
34+
#define DEBUG_TYPE "red-edge"
35+
36+
using namespace llvm;
37+
38+
char RedundantEdgeEliminationPass::ID = 0;
39+
40+
INITIALIZE_PASS_BEGIN(RedundantEdgeEliminationPass, "red-edge",
41+
"Redundant Edge Elimination", true, false)
42+
INITIALIZE_PASS_END(RedundantEdgeEliminationPass, "red-edge",
43+
"Redundant Edge Elimination", true, false)
44+
45+
FunctionPass * llvm::createRedundantEdgeEliminationPass() {
46+
return new RedundantEdgeEliminationPass;
47+
}
48+
49+
bool RedundantEdgeEliminationPass::runOnFunction(Function &F) {
50+
LLVM_DEBUG(dbgs() << "[REDUNDANT EDGE]: analyze " << F.getName() << "\n");
51+
for (auto &BB : F.getBasicBlockList()) {
52+
if (auto *TI = BB.getTerminator()) {
53+
if (auto *BI = dyn_cast<BranchInst>(TI)) {
54+
LLVM_DEBUG(dbgs() << "[REDUNDANT EDGE] instruction: "; BI->dump());
55+
if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
56+
ReplaceInstWithInst(
57+
BI, cast<ConstantInt>(BI->getCondition())->equalsInt(0) ?
58+
BranchInst::Create(BI->getSuccessor(1)) :
59+
BranchInst::Create(BI->getSuccessor(0)));
60+
LLVM_DEBUG(dbgs() << "[REDUNDANT EDGE] after: "; BB.dump());
61+
}
62+
}
63+
}
64+
}
65+
LLVM_DEBUG(dbgs() << "[REDUNDANT EDGE]: leave " << F.getName() << "\n");
66+
return true;
67+
}

0 commit comments

Comments
 (0)