Static Value-Flow Analysis
Public Member Functions | List of all members
SVF::LLVMLoopAnalysis Class Reference

#include <LLVMLoopAnalysis.h>

Public Member Functions

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

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 90 of file LLVMLoopAnalysis.cpp.

91 {
92  std::vector<const Loop *> llvmLoops;
93  buildLLVMLoops(PAG::getPAG()->getModule(), icfg);
94 }
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:115

◆ 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 {
52  std::vector<const Loop *> loop_stack;
53  for (Module& M : LLVMModuleSet::getLLVMModuleSet()->getLLVMModules())
54  {
55  for (Module::const_iterator F = M.begin(), E = M.end(); F != E; ++F)
56  {
57  const Function* func = &*F;
59  if (func->isDeclaration()) continue;
60  // do not analyze external call
61  if (SVFUtil::isExtCall(svffun)) continue;
62  DT.recalculate(const_cast<Function&>(*func));
63  llvm::LoopInfoBase<llvm::BasicBlock, llvm::Loop> loopInfo;
64  std::vector<const Loop*> llvmLoops;
65  loopInfo.analyze(DT);
66  for (const auto &loop: loopInfo)
67  {
68  loop_stack.push_back(loop);
69  }
70  // pre-order traversal on loop-subloop tree
71  while (!loop_stack.empty())
72  {
73  const Loop *loop = loop_stack.back();
74  loop_stack.pop_back();
75  llvmLoops.push_back(loop);
76  for (const auto &subloop: loop->getSubLoops())
77  {
78  loop_stack.push_back(subloop);
79  }
80  }
81  buildSVFLoops(icfg, llvmLoops);
82  }
83  }
84 }
#define F(f)
virtual void buildSVFLoops(ICFG *icfg, std::vector< const Loop * > &llvmLoops)
Build SVF loops based on llvm loops.
SVFFunction * getSVFFunction(const Function *fun) const
Definition: LLVMModule.h:230
static LLVMModuleSet * getLLVMModuleSet()
Definition: LLVMModule.h:118
const SVFBasicBlock * back() const
Definition: SVFValue.h:426
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
llvm::DominatorTree DominatorTree
LLVM Dominators.
Definition: BasicTypes.h:133

◆ 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 101 of file LLVMLoopAnalysis.cpp.

102 {
103  for (const auto &llvmLoop: llvmLoops)
104  {
105  DBOUT(DPAGBuild, outs() << "loop name: " << llvmLoop->getName().data() << "\n");
106  // count all node id in loop
107  Set<ICFGNode *> loop_ids;
109  for (const auto &BB: llvmLoop->getBlocks())
110  {
111  for (const auto &ins: *BB)
112  {
113  if(LLVMUtil::isIntrinsicInst(&ins))
114  continue;
115  loop_ids.insert(LLVMModuleSet::getLLVMModuleSet()->getICFGNode(&ins));
116  nodes.insert(LLVMModuleSet::getLLVMModuleSet()->getICFGNode(&ins));
117  }
118  }
119  SVFLoop *svf_loop = new SVFLoop(nodes, Options::LoopBound());
120  for (const auto &node: nodes)
121  {
122  icfg->addNodeToSVFLoop(node, svf_loop);
123  }
124  // mark loop header's first inst
125  BasicBlock* header_blk = llvmLoop->getHeader();
126  Instruction* in_ins = &(*header_blk->begin());
127 
128  while(LLVMUtil::isIntrinsicInst(in_ins))
129  {
130  in_ins = in_ins->getNextNode();
131  }
133  for (const auto &edge: in_node->getInEdges())
134  {
135  if (loop_ids.find(edge->getSrcNode()) == loop_ids.end())
136  {
137  // entry edge
138  svf_loop->addEntryICFGEdge(edge);
139  DBOUT(DPAGBuild, outs() << " entry edge: " << edge->toString() << "\n");
140  }
141  else
142  {
143  // back edge
144  svf_loop->addBackICFGEdge(edge);
145  DBOUT(DPAGBuild, outs() << " back edge: " << edge->toString() << "\n");
146  }
147  }
148  // handle in edge
149  llvm::Instruction &br_ins = header_blk->back();
151  for (const auto &edge: br_node->getOutEdges())
152  {
153  if (loop_ids.find(edge->getDstNode()) != loop_ids.end())
154  {
155  svf_loop->addInICFGEdge(edge);
156  DBOUT(DPAGBuild, outs() << " in edge: " << edge->toString() << "\n");
157  }
158  else
159  {
160  continue;
161  }
162  }
163  // mark loop end's first inst
164  llvm::SmallVector<BasicBlock*, 8> ExitBlocks;
165  llvmLoop->getExitBlocks(ExitBlocks);
166  for (const auto& exit_blk: ExitBlocks)
167  {
168  assert(!exit_blk->empty() && "exit block is empty?");
169  llvm::Instruction* out_ins = &(*exit_blk->begin());
170 
171  while(LLVMUtil::isIntrinsicInst(out_ins))
172  {
173  out_ins = out_ins->getNextNode();
174  }
175 
176  ICFGNode *out_node = LLVMModuleSet::getLLVMModuleSet()->getICFGNode(out_ins);
177  for (const auto &edge: out_node->getInEdges())
178  {
179  svf_loop->addOutICFGEdge(edge);
180  DBOUT(DPAGBuild, outs() << " out edge: " << edge->toString() << "\n");
181  }
182  }
183  }
184 }
#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
const GEdgeSetTy & getOutEdges() const
Definition: GenericGraph.h:430
const GEdgeSetTy & getInEdges() const
Definition: GenericGraph.h:434
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
void addBackICFGEdge(const ICFGEdge *edge)
Definition: SVFLoop.h:124
void addOutICFGEdge(const ICFGEdge *edge)
Definition: SVFLoop.h:109
void addEntryICFGEdge(const ICFGEdge *edge)
Definition: SVFLoop.h:94
void addInICFGEdge(const ICFGEdge *edge)
Definition: SVFLoop.h:139
bool isIntrinsicInst(const Instruction *inst)
Return true if it is an intrinsic instruction.
Definition: LLVMUtil.cpp:204
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)
Definition: GraphTraits.h:111
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96

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