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