attributes: dont allocate std::strings unnecessarily
MODIFY
CMakeLists.txt
CMakeLists.txt
@@ -75,6 +75,7 @@ include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_BINARY_DIR})
set(IR_SRCS
+ ir/attrs.cpp
ir/constant.cpp
ir/function.cpp
ir/globals.cpp

ADD
ir/attrs.cpp
+// Copyright (c) 2018-present The Alive2 Authors.
+// Distributed under the MIT license that can be found in the LICENSE file.
+
+#include "ir/attrs.h"
+
+using namespace std;
+
+namespace IR {
+ostream& operator<<(ostream &os, const ParamAttrs &attr) {
+ if (attr.has(ParamAttrs::NonNull))
+ os << "nonnull ";
+ if (attr.has(ParamAttrs::ByVal))
+ os << "byval ";
+ if (attr.has(ParamAttrs::NoCapture))
+ os << "nocapture ";
+ if (attr.has(ParamAttrs::ReadOnly))
+ os << "readonly ";
+ if (attr.has(ParamAttrs::ReadNone))
+ os << "readnone ";
+ return os;
+}
+
+
+ostream& operator<<(ostream &os, const FnAttrs &attr) {
+ if (attr.has(FnAttrs::NoRead))
+ os << " noread";
+ if (attr.has(FnAttrs::NoWrite))
+ os << " nowrite";
+ if (attr.has(FnAttrs::ArgMemOnly))
+ os << " argmemonly";
+ if (attr.has(FnAttrs::NNaN))
+ os << " NNaN";
+ if (attr.has(FnAttrs::NoReturn))
+ os << " noreturn";
+ return os;
+}
+
+}

MODIFY
ir/attrs.h
ir/attrs.h
// Copyright (c) 2018-present The Alive2 Authors.
// Distributed under the MIT license that can be found in the LICENSE file.
-#include <string>
+#include <ostream>
namespace IR {
@@ -16,23 +16,10 @@ public:
ParamAttrs(unsigned bits = None) : bits(bits) {}
- std::string str() const {
- std::string ret;
- if (has(ParamAttrs::NonNull))
- ret += "nonnull ";
- if (has(ParamAttrs::ByVal))
- ret += "byval ";
- if (has(ParamAttrs::NoCapture))
- ret += "nocapture ";
- if (has(ParamAttrs::ReadOnly))
- ret += "readonly ";
- if (has(ParamAttrs::ReadNone))
- ret += "readnone ";
- return ret;
- }
-
bool has(Attribute a) const { return (bits & a) != 0; }
- void set(Attribute a) { bits = bits | (unsigned)a; }
+ void set(Attribute a) { bits |= (unsigned)a; }
+
+ friend std::ostream& operator<<(std::ostream &os, const ParamAttrs &attr);
};
@@ -45,23 +32,10 @@ public:
FnAttrs(unsigned bits = None) : bits(bits) {}
- std::string str() const {
- std::string ret;
- if (bits & NoRead)
- ret += " noread";
- if (bits & NoWrite)
- ret += " nowrite";
- if (bits & ArgMemOnly)
- ret += " argmemonly";
- if (bits & NNaN)
- ret += " NNaN";
- if (bits & NoReturn)
- ret += " noreturn";
- return ret;
- }
-
bool has(Attribute a) const { return (bits & a) != 0; }
- void set(Attribute a) { bits = bits | (unsigned)a; }
+ void set(Attribute a) { bits |= (unsigned)a; }
+
+ friend std::ostream& operator<<(std::ostream &os, const FnAttrs &attr);
};
}

MODIFY
ir/instr.cpp
ir/instr.cpp
@@ -1299,10 +1299,10 @@ void FnCall::print(ostream &os) const {
if (!first)
os << ", ";
- os << attrs.str() << *arg;
+ os << attrs << *arg;
first = false;
}
- os << ')' << attrs.str();
+ os << ')' << attrs;
if (!valid)
os << "\t; WARNING: unknown known function";

MODIFY
ir/value.cpp
ir/value.cpp
#include "smt/expr.h"
#include "util/compiler.h"
#include "util/config.h"
+#include <sstream>
using namespace smt;
using namespace std;
@@ -156,8 +157,14 @@ void AggregateValue::print(std::ostream &os) const {
}
+static string attr_str(const ParamAttrs &attr) {
+ stringstream ss;
+ ss << attr;
+ return ss.str();
+}
+
Input::Input(Type &type, string &&name, ParamAttrs &&attributes)
- : Value(type, attributes.str() + name), smt_name(move(name)),
+ : Value(type, attr_str(attributes) + name), smt_name(move(name)),
attrs(move(attributes)) {}
void Input::copySMTName(const Input &other) {

attributes: dont allocate std::strings unnecessarily