Skip to content

Commit f77d38f

Browse files
author
kroening
committed
use empty_d to make irept::read() branch-free
git-svn-id: svn+ssh://svn.cprover.org/srv/svn/cbmc/trunk@1051 6afb6bc1-c8e4-404c-8f48-9ae832c5b171
1 parent d536812 commit f77d38f

File tree

2 files changed

+30
-55
lines changed

2 files changed

+30
-55
lines changed

src/util/irep.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Author: Daniel Kroening, [email protected]
1616
irept nil_rep_storage;
1717

1818
#ifdef SHARING
19-
const irept::dt empty_d;
19+
irept::dt irept::empty_d;
2020
#endif
2121

2222
/*******************************************************************\
@@ -57,7 +57,7 @@ void irept::detatch()
5757
std::cout << "DETATCH1: " << data << std::endl;
5858
#endif
5959

60-
if(data==NULL)
60+
if(data==&empty_d)
6161
{
6262
data=new dt;
6363

@@ -80,7 +80,6 @@ void irept::detatch()
8080

8181
assert(data->ref_count==1);
8282

83-
8483
#ifdef IREP_DEBUG
8584
std::cout << "DETATCH2: " << data << std::endl;
8685
#endif
@@ -89,32 +88,6 @@ void irept::detatch()
8988

9089
/*******************************************************************\
9190
92-
Function: irept::read
93-
94-
Inputs:
95-
96-
Outputs:
97-
98-
Purpose:
99-
100-
\*******************************************************************/
101-
102-
#ifdef SHARING
103-
const irept::dt &irept::read() const
104-
{
105-
#ifdef IREP_DEBUG
106-
std::cout << "READ: " << data << std::endl;
107-
#endif
108-
109-
if(data==NULL)
110-
return empty_d;
111-
112-
return *data;
113-
}
114-
#endif
115-
116-
/*******************************************************************\
117-
11891
Function: irept::remove_ref
11992
12093
Inputs:
@@ -130,7 +103,7 @@ Function: irept::remove_ref
130103
#ifdef SHARING
131104
void irept::remove_ref(dt *old_data)
132105
{
133-
if(old_data==NULL) return;
106+
if(old_data==&empty_d) return;
134107

135108
assert(old_data->ref_count!=0);
136109

@@ -174,7 +147,7 @@ void irept::clear()
174147
{
175148
#ifdef SHARING
176149
remove_ref(data);
177-
data=NULL;
150+
data=&empty_d;
178151
#else
179152
data.clear();
180153
#endif

src/util/irep.h

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,24 @@ class irept
6161
{
6262
public:
6363
typedef std::vector<irept> subt;
64-
//typedef std::list<irept> subt;
65-
6664
typedef std::map<irep_namet, irept> named_subt;
6765

68-
bool is_nil() const { return id()==ID_nil; }
69-
bool is_not_nil() const { return id()!=ID_nil; }
66+
inline bool is_nil() const { return id()==ID_nil; }
67+
inline bool is_not_nil() const { return id()!=ID_nil; }
7068

71-
inline explicit irept(const irep_idt &_id):data(NULL)
69+
inline explicit irept(const irep_idt &_id):data(&empty_d)
7270
{
7371
id(_id);
7472
}
7573

7674
#ifdef SHARING
77-
inline irept():data(NULL)
75+
inline irept():data(&empty_d)
7876
{
7977
}
8078

8179
inline irept(const irept &irep):data(irep.data)
8280
{
83-
if(data!=NULL)
81+
if(data!=&empty_d)
8482
{
8583
assert(data->ref_count!=0);
8684
data->ref_count++;
@@ -99,21 +97,21 @@ class irept
9997
// Ordering is very important here!
10098
// Consider self-assignment, which may destroy 'irep'
10199
dt *irep_data=irep.data;
102-
if(irep_data!=NULL) irep_data->ref_count++;
100+
if(irep_data!=&empty_d) irep_data->ref_count++;
103101

104102
remove_ref(data); // this may kill 'irep'
105103
data=irep_data;
106104

107105
return *this;
108106
}
109107

110-
~irept()
108+
inline ~irept()
111109
{
112110
remove_ref(data);
113111
data=NULL;
114112
}
115113
#else
116-
irept()
114+
inline irept()
117115
{
118116
}
119117
#endif
@@ -135,7 +133,7 @@ class irept
135133
const irept &find(const irep_namet &name) const;
136134
irept &add(const irep_namet &name);
137135

138-
const std::string &get_string(const irep_namet &name) const
136+
inline const std::string &get_string(const irep_namet &name) const
139137
{
140138
#ifdef USE_DSTRING
141139
return get(name).as_string();
@@ -166,7 +164,7 @@ class irept
166164

167165
std::string to_string() const;
168166

169-
void swap(irept &irep)
167+
inline void swap(irept &irep)
170168
{
171169
std::swap(irep.data, data);
172170
}
@@ -178,14 +176,14 @@ class irept
178176

179177
void clear();
180178

181-
void make_nil() { clear(); id(ID_nil); }
179+
inline void make_nil() { clear(); id(ID_nil); }
182180

183-
subt &get_sub() { return write().sub; } // DANGEROUS
184-
const subt &get_sub() const { return read().sub; }
185-
named_subt &get_named_sub() { return write().named_sub; } // DANGEROUS
186-
const named_subt &get_named_sub() const { return read().named_sub; }
187-
named_subt &get_comments() { return write().comments; } // DANGEROUS
188-
const named_subt &get_comments() const { return read().comments; }
181+
inline subt &get_sub() { return write().sub; } // DANGEROUS
182+
inline const subt &get_sub() const { return read().sub; }
183+
inline named_subt &get_named_sub() { return write().named_sub; } // DANGEROUS
184+
inline const named_subt &get_named_sub() const { return read().named_sub; }
185+
inline named_subt &get_comments() { return write().comments; } // DANGEROUS
186+
inline const named_subt &get_comments() const { return read().comments; }
189187

190188
size_t hash() const;
191189
size_t full_hash() const;
@@ -195,7 +193,7 @@ class irept
195193
std::string pretty(unsigned indent=0, unsigned max_indent=0) const;
196194

197195
protected:
198-
static bool is_comment(const irep_namet &name)
196+
inline static bool is_comment(const irep_namet &name)
199197
{ return !name.empty() && name[0]=='#'; }
200198

201199
public:
@@ -250,12 +248,16 @@ class irept
250248
protected:
251249
#ifdef SHARING
252250
dt *data;
251+
static dt empty_d;
253252

254253
void remove_ref(dt *old_data);
255254
void detatch();
256255

257256
public:
258-
const dt &read() const;
257+
inline const dt &read() const
258+
{
259+
return *data;
260+
}
259261

260262
inline dt &write()
261263
{
@@ -299,17 +301,17 @@ extern inline const std::string &name2string(const irep_namet &n)
299301

300302
struct irep_hash
301303
{
302-
size_t operator()(const irept &irep) const { return irep.hash(); }
304+
inline size_t operator()(const irept &irep) const { return irep.hash(); }
303305
};
304306

305307
struct irep_full_hash
306308
{
307-
size_t operator()(const irept &irep) const { return irep.full_hash(); }
309+
inline size_t operator()(const irept &irep) const { return irep.full_hash(); }
308310
};
309311

310312
struct irep_full_eq
311313
{
312-
bool operator()(const irept &i1, const irept &i2) const
314+
inline bool operator()(const irept &i1, const irept &i2) const
313315
{
314316
return full_eq(i1, i2);
315317
}

0 commit comments

Comments
 (0)