Static Value-Flow Analysis
CSC.cpp
Go to the documentation of this file.
1 //===- CSC.cpp -- Cycle Stride Calculation algorithm---------------------------------------//
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 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  * CSC.cpp
25  *
26  * Created on: 09, Feb, 2019
27  * Author: Yuxiang Lei
28  */
29 
30 #include "WPA/CSC.h"
31 
32 using namespace SVF;
33 using namespace SVFUtil;
34 
35 
39 void CSC::clear()
40 {
41  _I = 0;
42  _D.clear();
43 }
44 
45 
49 void CSC::find(NodeStack& candidates)
50 {
51  clear();
52 
53  NodeStack revCandidates;
54  while (!candidates.empty())
55  {
56  NodeID nId = candidates.top();
57  revCandidates.push(nId);
58  candidates.pop();
59 
60  if (_scc->subNodes(nId).count()>1 && !isVisited(nId)) // node is actually in a cycle
61  visit(nId, 0);
62  }
63 
64  while (!revCandidates.empty())
65  {
66  NodeID nId = revCandidates.top();
67  candidates.push(nId);
68  revCandidates.pop();
69  }
70 }
71 
72 
76 void CSC::visit(NodeID nodeId, s32_t _w)
77 {
78 // pwcReps[nodeId] = _scc->repNode(nodeId);
79  setVisited(nodeId);
80 
81  _I += _w;
82  _D[nodeId] = _I;
83  _S.push(nodeId);
84 
85  ConstraintNode* node = _consG->getConstraintNode(nodeId);
86  for (ConstraintNode::const_iterator eit = node->directOutEdgeBegin(); eit != node->directOutEdgeEnd(); ++eit)
87  {
88  s32_t offset;
89  if (NormalGepCGEdge* gepCGEdge = SVFUtil::dyn_cast<NormalGepCGEdge>(*eit))
90  offset = gepCGEdge->getConstantFieldIdx();
91  else
92  offset = 0;
93  NodeID dstId = (*eit)->getDstID();
94  if (_scc->repNode(nodeId) == _scc->repNode(dstId) && !isVisited(dstId))
95  visit(dstId, offset);
96  }
97 
98  NodeStack _revS;
99  NodeSet _C;
100 
101  while (!_S.empty())
102  {
103  NodeID backNodeId = _S.top();
104  _S.pop();
105  _revS.push(backNodeId);
106  ConstraintNode* backNode = _consG->getConstraintNode(backNodeId);
107  if (_consG->hasEdge(node, backNode, ConstraintEdge::NormalGep))
108  {
109  NormalGepCGEdge* normalGep = SVFUtil::dyn_cast<NormalGepCGEdge>(_consG->getEdge(node, backNode, ConstraintEdge::NormalGep));
110  s32_t _w = normalGep->getConstantFieldIdx();
111  s32_t _l = _D[nodeId] +_w - _D[backNodeId];
112  backNode->strides.set(_l);
113  for (auto cNodeId : _C)
114  _consG->getConstraintNode(cNodeId)->strides.set(_l);
115  }
116  else if (_consG->hasEdge(node, backNode, ConstraintEdge::VariantGep) ||
117  _consG->hasEdge(node, backNode, ConstraintEdge::Copy))
118  {
119  s32_t _l = _D[nodeId] - _D[backNodeId];
120  backNode->strides.set(_l);
121  for (auto cNodeId : _C)
122  _consG->getConstraintNode(cNodeId)->strides.set(_l);
123  }
124  _C.insert(backNodeId);
125  }
126 
127  while (!_revS.empty())
128  {
129  NodeID backedId = _revS.top();
130  _S.push(backedId);
131  _revS.pop();
132  }
133 
134  _S.pop(); // after checking all the edges of the top node of _S, remove the node
135 }
buffer offset
Definition: cJSON.cpp:1113
void find(NodeStack &candidates)
Definition: CSC.cpp:49
void visit(NodeID nodeId, s32_t _w)
Definition: CSC.cpp:76
void clear()
Definition: CSC.cpp:39
NodeBS strides
For stride-based field representation.
Definition: ConsGNode.h:71
iterator directOutEdgeEnd()
Definition: ConsG.cpp:674
ConstraintEdge::ConstraintEdgeSetTy::const_iterator const_iterator
Definition: ConsGNode.h:45
iterator directOutEdgeBegin()
Iterators.
Definition: ConsG.cpp:666
APOffset getConstantFieldIdx() const
Get location set of the gep edge.
Definition: ConsGEdge.h:309
void set(unsigned Idx)
for isBitcode
Definition: BasicTypes.h:68
std::stack< NodeID > NodeStack
Definition: GeneralType.h:118
Set< NodeID > NodeSet
Definition: GeneralType.h:113
u32_t NodeID
Definition: GeneralType.h:55
signed s32_t
Definition: GeneralType.h:47