SVF
DataFlowUtil.h
Go to the documentation of this file.
1 //===- DataFlowUtil.h -- Helper functions for data-flow analysis--------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-2017> <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 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 General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 /*
24  * DataFlowUtil.h
25  *
26  * Created on: Jun 4, 2013
27  * Author: Yulei Sui
28  */
29 
30 #ifndef DATAFLOWUTIL_H_
31 #define DATAFLOWUTIL_H_
32 
33 #include "Util/BasicTypes.h"
34 #include "llvm/IR/LegacyPassManager.h"
35 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
36 
37 namespace SVF
38 {
39 
43 class PTASCEV
44 {
45 
46 public:
47  PTASCEV():scev(nullptr), start(nullptr), step(nullptr),ptr(nullptr),inloop(false),tripcount(0) {}
48 
50  PTASCEV(const Value* p, const SCEV* s, ScalarEvolution* SE): scev(s),start(nullptr), step(nullptr), ptr(p), inloop(false), tripcount(0)
51  {
52  if(const SCEVAddRecExpr* ar = SVFUtil::dyn_cast<SCEVAddRecExpr>(s))
53  {
54  if (const SCEVConstant *startExpr = SVFUtil::dyn_cast<SCEVConstant>(ar->getStart()))
55  start = startExpr->getValue();
56  if (const SCEVConstant *stepExpr = SVFUtil::dyn_cast<SCEVConstant>(ar->getStepRecurrence(*SE)))
57  step = stepExpr->getValue();
58  tripcount = SE->getSmallConstantTripCount(const_cast<Loop*>(ar->getLoop()));
59  inloop = true;
60  }
61  }
63  PTASCEV(const PTASCEV& ptase): scev(ptase.scev), start(ptase.start), step(ptase.step), ptr(ptase.ptr), inloop(ptase.inloop),tripcount(ptase.tripcount)
64  {
65 
66  }
67 
69  virtual ~PTASCEV()
70  {
71  }
72 
73  const SCEV *scev;
74  const Value* start;
75  const Value* step;
76  const Value *ptr;
77  bool inloop;
78  unsigned tripcount;
79 
82  inline bool operator< (const PTASCEV& rhs) const
83  {
84  if(start!=rhs.start)
85  return start < rhs.start;
86  else if(step!=rhs.step)
87  return step < rhs.step;
88  else if(ptr!=rhs.ptr)
89  return ptr < rhs.ptr;
90  else if(tripcount!=rhs.tripcount)
91  return tripcount < rhs.tripcount;
92  else
93  return inloop < rhs.inloop;
94  }
96  inline PTASCEV& operator= (const PTASCEV& rhs)
97  {
98  if(*this!=rhs)
99  {
100  start = rhs.start;
101  step = rhs.step;
102  ptr = rhs.ptr;
103  tripcount = rhs.tripcount;
104  inloop = rhs.inloop;
105  }
106  return *this;
107  }
109  inline bool operator== (const PTASCEV& rhs) const
110  {
111  return (start == rhs.start && step == rhs.step && ptr == rhs.ptr && tripcount == rhs.tripcount && inloop == rhs.inloop);
112  }
114  inline bool operator!= (const PTASCEV& rhs) const
115  {
116  return !(*this==rhs);
117  }
118 };
119 
120 
122 public:
126 
129 
130  ~PTACFInfoBuilder();
131 
133  LoopInfo* getLoopInfo(const Function* f);
134 
136  PostDominatorTree* getPostDT(const Function* f);
137 
139  DominatorTree* getDT(const Function* f);
140 
141 private:
142  FunToLoopInfoMap funToLoopInfoMap;
143  FunToDTMap funToDTMap;
144  FunToPostDTMap funToPDTMap;
145 };
146 
147 
152 {
153 
154 private:
156 
157  void calculate(BasicBlock *, const DominanceFrontier &DF);
158 
159 public:
160  static char ID;
161 
163  DominanceFrontierBase(), DF(nullptr)
164  {
165  }
166 
168  {
169  }
170 
171  virtual void getAnalysisUsage(AnalysisUsage &AU) const
172  {
173  AU.setPreservesAll();
174  // AU.addRequired<DominanceFrontier>();
175  }
176 
177 // virtual bool runOnFunction(Function &m) {
178 // Frontiers.clear();
179 // DF = &getAnalysis<DominanceFrontier>();
180 // return false;
181 // }
182 
183  iterator getIDFSet(BasicBlock *B)
184  {
185  if (Frontiers.find(B) == Frontiers.end())
186  calculate(B, *DF);
187  return Frontiers.find(B);
188  }
189 };
190 
191 } // End namespace SVF
192 
193 #endif /* DATAFLOWUTIL_H_ */
FunToPostDTMap funToPDTMap
map a function to its post dominator tree
Definition: DataFlowUtil.h:144
Map< const Function *, LoopInfo * > FunToLoopInfoMap
map a function to its loop info
Definition: DataFlowUtil.h:125
llvm::ScalarEvolution ScalarEvolution
Definition: BasicTypes.h:186
llvm::AnalysisUsage AnalysisUsage
Definition: BasicTypes.h:96
llvm::BasicBlock BasicBlock
Definition: BasicTypes.h:77
virtual ~PTASCEV()
Destructor.
Definition: DataFlowUtil.h:69
llvm::LoopInfo LoopInfo
Definition: BasicTypes.h:89
const Value * ptr
Definition: DataFlowUtil.h:76
const Value * step
Definition: DataFlowUtil.h:75
llvm::SCEVAddRecExpr SCEVAddRecExpr
Definition: BasicTypes.h:187
FunToLoopInfoMap funToLoopInfoMap
map a function to its loop info
Definition: DataFlowUtil.h:142
PTASCEV(const PTASCEV &ptase)
Copy Constructor.
Definition: DataFlowUtil.h:63
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: SVFBasicTypes.h:98
llvm::DominanceFrontier DominanceFrontier
LLVM Dominators.
Definition: BasicTypes.h:192
PTASCEV & operator=(const PTASCEV &rhs)
Overloading operator=.
Definition: DataFlowUtil.h:96
llvm::PostDominatorTree PostDominatorTree
Definition: BasicTypes.h:194
llvm::Function Function
Definition: BasicTypes.h:76
llvm::DominanceFrontierBase< BasicBlock, false > DominanceFrontierBase
Definition: BasicTypes.h:196
Map< const Function *, DominatorTree * > FunToDTMap
map a function to its dominator tree
Definition: DataFlowUtil.h:123
llvm::DominatorTree DominatorTree
Definition: BasicTypes.h:193
const DominanceFrontier * DF
Definition: DataFlowUtil.h:155
llvm::SCEV SCEV
Definition: BasicTypes.h:189
PTASCEV(const Value *p, const SCEV *s, ScalarEvolution *SE)
Constructor.
Definition: DataFlowUtil.h:50
bool operator<(const PTASCEV &rhs) const
Definition: DataFlowUtil.h:82
llvm::SCEVConstant SCEVConstant
Definition: BasicTypes.h:188
bool operator!=(const PTASCEV &rhs) const
Overloading operator==.
Definition: DataFlowUtil.h:114
for isBitcode
Definition: ContextDDA.h:15
FunToDTMap funToDTMap
map a function to its dominator tree
Definition: DataFlowUtil.h:143
bool operator==(const PTASCEV &rhs) const
Overloading operator==.
Definition: DataFlowUtil.h:109
Map< const Function *, PostDominatorTree * > FunToPostDTMap
map a function to its post dominator tree
Definition: DataFlowUtil.h:124
iterator getIDFSet(BasicBlock *B)
Definition: DataFlowUtil.h:183
const SCEV * scev
Definition: DataFlowUtil.h:73
llvm::Value Value
Definition: BasicTypes.h:78
const Value * start
Definition: DataFlowUtil.h:74
unsigned tripcount
Definition: DataFlowUtil.h:78
virtual void getAnalysisUsage(AnalysisUsage &AU) const
Definition: DataFlowUtil.h:171