llvm2alive: optimize cleanup of created constexprs
MODIFY
llvm_util/llvm2alive.cpp
llvm_util/llvm2alive.cpp
@@ -176,8 +176,8 @@ public:
out(&get_outs()) {}
~llvm2alive_() {
+ reset_state();
for (auto &inst : i_constexprs) {
- remove_value_name(*inst); // otherwise value_names maintain freed pointers
inst->deleteValue();
}
}

MODIFY
llvm_util/utils.cpp
llvm_util/utils.cpp
@@ -24,7 +24,7 @@ namespace {
// cache Value*'s names
unordered_map<const llvm::Value*, string> value_names;
-unsigned value_id_counter; // for %0, %1, etc..
+unsigned value_id_counter = 0; // for %0, %1, etc..
vector<unique_ptr<IntType>> int_types;
vector<unique_ptr<PtrType>> ptr_types;
@@ -101,10 +101,6 @@ string value_name(const llvm::Value &v) {
: '%' + to_string(value_id_counter++);
}
-void remove_value_name(const llvm::Value &v) {
- value_names.erase(&v);
-}
-
Type& get_int_type(unsigned bits) {
if (bits >= int_types.size())
int_types.resize(bits + 1);
@@ -439,11 +435,14 @@ void set_outs(ostream &os) {
out = &os;
}
-void reset_state(Function &f) {
- current_fn = &f;
+void reset_state() {
value_cache.clear();
value_names.clear();
value_id_counter = 0;
}
+void reset_state(Function &f) {
+ current_fn = &f;
+}
+
}

MODIFY
llvm_util/utils.h
llvm_util/utils.h
@@ -32,7 +32,6 @@ IR::FastMathFlags parse_fmath(llvm::Instruction &i);
IR::BasicBlock& getBB(const llvm::BasicBlock *bb);
std::string value_name(const llvm::Value &v);
-void remove_value_name(const llvm::Value &v);
IR::Type& get_int_type(unsigned bits);
IR::Type* llvm_type2alive(const llvm::Type *ty);
@@ -54,5 +53,6 @@ void init_llvm_utils(std::ostream &os, const llvm::DataLayout &DL);
std::ostream& get_outs();
void set_outs(std::ostream &os);
+void reset_state();
void reset_state(IR::Function &f);
}

llvm2alive: optimize cleanup of created constexprs