Static Value-Flow Analysis
Loading...
Searching...
No Matches
LLVMLoopAnalysis.cpp
Go to the documentation of this file.
1//===- LLVMLoopAnalysis.cpp -- LoopAnalysis of SVF ------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2022> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23
24/*
25 * LLVMLoopAnalysis.cpp
26 *
27 * Created on: 14, 06, 2022
28 * Author: Jiawei Wang, Xiao Cheng
29 */
30
32#include "Util/Options.h"
33#include "SVF-LLVM/LLVMUtil.h"
34#include "llvm/Analysis/LoopInfo.h"
35
36#include "llvm/Transforms/Utils/Mem2Reg.h"
37#include "llvm/Passes/PassBuilder.h"
38
39#include "SVF-LLVM/LLVMModule.h"
40
41using namespace SVF;
42using namespace SVFUtil;
43
50{
51 std::vector<const Loop *> loop_stack;
53 {
54 for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
55 {
56 const Function* func = &*F;
58 if (func->isDeclaration()) continue;
59 // do not analyze external call
60 if (SVFUtil::isExtCall(svffun)) continue;
61 llvm::DominatorTree& DT = LLVMModuleSet::getLLVMModuleSet()->getDomTree(func);
62 llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop> loopInfo;
63 std::vector<const Loop*> llvmLoops;
64 loopInfo.analyze(DT);
65 for (const auto &loop: loopInfo)
66 {
67 loop_stack.push_back(loop);
68 }
69 // pre-order traversal on loop-subloop tree
70 while (!loop_stack.empty())
71 {
72 const Loop *loop = loop_stack.back();
73 loop_stack.pop_back();
74 llvmLoops.push_back(loop);
75 for (const auto &subloop: loop->getSubLoops())
76 {
77 loop_stack.push_back(subloop);
78 }
79 }
81 }
82 }
83}
84
90{
91 std::vector<const Loop *> llvmLoops;
92 buildLLVMLoops(PAG::getPAG()->getModule(), icfg);
93}
94
100void LLVMLoopAnalysis::buildSVFLoops(ICFG *icfg, std::vector<const Loop *> &llvmLoops)
101{
102 for (const auto &llvmLoop: llvmLoops)
103 {
104 DBOUT(DPAGBuild, outs() << "loop name: " << llvmLoop->getName().data() << "\n");
105 // count all node id in loop
108 for (const auto &BB: llvmLoop->getBlocks())
109 {
110 for (const auto &ins: *BB)
111 {
113 continue;
116 }
117 }
119 for (const auto &node: nodes)
120 {
121 icfg->addNodeToSVFLoop(node, svf_loop);
122 }
123 // mark loop header's first inst
124 BasicBlock* header_blk = llvmLoop->getHeader();
125 Instruction* in_ins = &(*header_blk->begin());
126
128 {
129 in_ins = in_ins->getNextNode();
130 }
132 for (const auto &edge: in_node->getInEdges())
133 {
134 if (loop_ids.find(edge->getSrcNode()) == loop_ids.end())
135 {
136 // entry edge
137 svf_loop->addEntryICFGEdge(edge);
138 DBOUT(DPAGBuild, outs() << " entry edge: " << edge->toString() << "\n");
139 }
140 else
141 {
142 // back edge
143 svf_loop->addBackICFGEdge(edge);
144 DBOUT(DPAGBuild, outs() << " back edge: " << edge->toString() << "\n");
145 }
146 }
147 // handle in edge
148 llvm::Instruction &br_ins = header_blk->back();
150 for (const auto &edge: br_node->getOutEdges())
151 {
152 if (loop_ids.find(edge->getDstNode()) != loop_ids.end())
153 {
154 svf_loop->addInICFGEdge(edge);
155 DBOUT(DPAGBuild, outs() << " in edge: " << edge->toString() << "\n");
156 }
157 else
158 {
159 continue;
160 }
161 }
162 // mark loop end's first inst
163 llvm::SmallVector<BasicBlock*, 8> ExitBlocks;
164 llvmLoop->getExitBlocks(ExitBlocks);
165 for (const auto& exit_blk: ExitBlocks)
166 {
167 assert(!exit_blk->empty() && "exit block is empty?");
168 llvm::Instruction* out_ins = &(*exit_blk->begin());
169
171 {
172 out_ins = out_ins->getNextNode();
173 }
174
176 for (const auto &edge: out_node->getInEdges())
177 {
178 svf_loop->addOutICFGEdge(edge);
179 DBOUT(DPAGBuild, outs() << " out edge: " << edge->toString() << "\n");
180 }
181 }
182 }
183}
#define F(f)
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition SVFType.h:484
#define DPAGBuild
Definition SVFType.h:492
void addNodeToSVFLoop(const ICFGNode *node, const SVFLoop *loop)
Insert (node, loop) to icfgNodeToSVFLoopVec.
Definition ICFG.h:124
virtual void build(ICFG *icfg)
Start from here.
virtual void buildLLVMLoops(SVFModule *mod, ICFG *icfg)
Build llvm loops based on LoopInfo analysis.
virtual void buildSVFLoops(ICFG *icfg, std::vector< const Loop * > &llvmLoops)
Build SVF loops based on llvm loops.
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:122
DominatorTree & getDomTree(const Function *fun)
ICFGNode * getICFGNode(const Instruction *inst)
Get a basic block ICFGNode.
SVFFunction * getSVFFunction(const Function *fun) const
Definition LLVMModule.h:260
const std::vector< std::reference_wrapper< Module > > & getLLVMModules() const
Definition LLVMModule.h:153
static const Option< u32_t > LoopBound
Definition Options.h:243
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:116
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition LLVMUtil.cpp:203
bool isExtCall(const SVFFunction *fun)
Definition SVFUtil.h:278
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50
for isBitcode
Definition BasicTypes.h:68
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:86
llvm::Function Function
Definition BasicTypes.h:85
llvm::Instruction Instruction
Definition BasicTypes.h:87
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
llvm::Module Module
Definition BasicTypes.h:84
iter_range< typename GenericGraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
llvm::Loop Loop
LLVM Loop.
Definition BasicTypes.h:140