Static Value-Flow Analysis
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SVF::LLVMLoopAnalysis Class Reference

#include <LLVMLoopAnalysis.h>

Public Member Functions

 LLVMLoopAnalysis ()=default
 Constructor.
 
virtual ~LLVMLoopAnalysis ()=default
 Destructor.
 
virtual void buildLLVMLoops (SVFModule *mod, ICFG *icfg)
 Build llvm loops based on LoopInfo analysis.
 
virtual void build (ICFG *icfg)
 Start from here.
 
virtual void buildSVFLoops (ICFG *icfg, std::vector< const Loop * > &llvmLoops)
 Build SVF loops based on llvm loops.
 

Detailed Description

Definition at line 39 of file LLVMLoopAnalysis.h.

Constructor & Destructor Documentation

◆ LLVMLoopAnalysis()

SVF::LLVMLoopAnalysis::LLVMLoopAnalysis ( )
default

Constructor.

◆ ~LLVMLoopAnalysis()

virtual SVF::LLVMLoopAnalysis::~LLVMLoopAnalysis ( )
virtualdefault

Destructor.

Member Function Documentation

◆ build()

void LLVMLoopAnalysis::build ( ICFG icfg)
virtual

Start from here.

We start from here

Parameters
icfgICFG

Definition at line 89 of file LLVMLoopAnalysis.cpp.

90{
91 std::vector<const Loop *> llvmLoops;
92 buildLLVMLoops(PAG::getPAG()->getModule(), icfg);
93}
virtual void buildLLVMLoops(SVFModule *mod, ICFG *icfg)
Build llvm loops based on LoopInfo analysis.
static SVFIR * getPAG(bool buildFromFile=false)
Singleton design here to make sure we only have one instance during any analysis.
Definition SVFIR.h:116
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

◆ buildLLVMLoops()

void LLVMLoopAnalysis::buildLLVMLoops ( SVFModule mod,
ICFG icfg 
)
virtual

Build llvm loops based on LoopInfo analysis.

Build llvm loops based on LoopInfo analysis

Parameters
modSVF module
llvmLoopsoutput llvm loops

Definition at line 49 of file LLVMLoopAnalysis.cpp.

50{
51 std::vector<const Loop *> loop_stack;
52 for (Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
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}
#define F(f)
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)
SVFFunction * getSVFFunction(const Function *fun) const
Definition LLVMModule.h:260
bool isExtCall(const SVFFunction *fun)
Definition SVFUtil.h:278
llvm::Function Function
Definition BasicTypes.h:85
llvm::Module Module
Definition BasicTypes.h:84
llvm::Loop Loop
LLVM Loop.
Definition BasicTypes.h:140

◆ buildSVFLoops()

void LLVMLoopAnalysis::buildSVFLoops ( ICFG icfg,
std::vector< const Loop * > &  llvmLoops 
)
virtual

Build SVF loops based on llvm loops.

Build SVF loops based on llvm loops

Parameters
icfgICFG
llvmLoopsinput llvm loops

Definition at line 100 of file LLVMLoopAnalysis.cpp.

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;
114 loop_ids.insert(LLVMModuleSet::getLLVMModuleSet()->getICFGNode(&ins));
115 nodes.insert(LLVMModuleSet::getLLVMModuleSet()->getICFGNode(&ins));
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 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
ICFGNode * getICFGNode(const Instruction *inst)
Get a basic block ICFGNode.
static const Option< u32_t > LoopBound
Definition Options.h:243
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition LLVMUtil.cpp:203
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:86
llvm::Instruction Instruction
Definition BasicTypes.h:87
iter_range< typename GenericGraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)

The documentation for this class was generated from the following files: