ClearBlue
CBAccessPath.h
1 /*
2  * This file defines a full access path used in cb-check
3  *
4  * Created on: May 26, 2016
5  * Author: Zhou Jinguo
6  */
7 
8 #ifndef IR_SEG_CBAccessPath_H
9 #define IR_SEG_CBAccessPath_H
10 
11 #include <llvm/IR/Value.h>
12 
13 #include "Platform/Arch/CBTypes.h"
14 #include "Utils/ADT/kvec.h"
15 
16 using namespace llvm;
17 
18 // This class defines the access path for SEG Nodes
19 // Currently used for pseudo-inputs and pseudo outputs of function
20 // Note: Offsets are stored in a reverse way for better efficiency
21 // For example arg2->4->8->0 is abstracted as base_ptr=arg2, offsets=[0,8,4]
22 class CBAccessPath {
23 private:
24  kvec<pp_offset_t> offsets;
25  Value *base_ptr;
26 
27  bool is_base_ptr_from_return;
28 
29 public:
30  CBAccessPath() {
31  base_ptr = nullptr;
32  is_base_ptr_from_return = false;
33  }
34  ~CBAccessPath() {}
35 
36  // Adding one level to the Access Path
37  // e.g. arg->8 => base_ptr->offset->8 where arg is base_ptr->offset
38  void add_level(Value *base_ptr, pp_offset_t offset,
39  bool is_from_return = false) {
40  this->base_ptr = base_ptr;
41  offsets.push_back(offset);
42  this->is_base_ptr_from_return = is_from_return;
43  }
44 
45  // Adding one level to the Access Path
46  // e.g. arg->8 => base_ptr->(8-offset) where arg points to base_ptr->offset
47  void reset_cur_level(Value *base_ptr, pp_offset_t offset,
48  bool is_from_return = false) {
49  if (!offsets.empty()) {
50  this->base_ptr = base_ptr;
51  offsets.back() -= offset;
52  this->is_base_ptr_from_return = is_from_return;
53  }
54  }
55 
56  // reset the Assess Path as a direct access to base_ptr
57  void reset(Value *base_ptr, bool is_from_return = false) {
58  offsets.clear();
59  this->base_ptr = base_ptr;
60  this->is_base_ptr_from_return = is_from_return;
61  }
62 
63  // reset the Assess Path as to the argument access path
64  void reset(const CBAccessPath &ap) {
65  offsets.clear();
66  offsets.push_back(ap.offsets);
67  this->base_ptr = ap.base_ptr;
68  this->is_base_ptr_from_return = ap.is_base_ptr_from_return;
69  }
70 
71  int get_depth() const { return offsets.size(); }
72 
73  pp_offset_t get_offset(int idx) const {
74  // The offset is added in a reversed way, i.e. arg->0->4->8 is stored as
75  // [8,4,0] This is because that push_back() is much faster than push_frount
76  return offsets[get_depth() - idx - 1];
77  }
78 
79  Value *get_base_ptr() const { return base_ptr; }
80 
81  // return true if the ap roots from function return
82  bool is_from_return() const { return is_base_ptr_from_return; }
83 };
84 
85 #endif /* IR_SEG_CBAccessPath_H */
CBAccessPath
Definition: CBAccessPath.h:22