uniformize handling of globals initializers such that they are all sorted
MODIFY
ir/value.cpp
ir/value.cpp
@@ -96,10 +96,6 @@ StateValue GlobalVariable::toSMT(State &s) const {
&glbvar_bid);
s.addGlobalVarBid(getName(), glbvar_bid);
}
- if (initval) {
- assert(isconst);
- s.getMemory().store(ptrval, s[*initval], initval->getType(), align);
- }
return { move(ptrval), true };
}

MODIFY
ir/value.h
ir/value.h
@@ -82,20 +82,14 @@ class GlobalVariable final : public Value {
// The size of this global variable (in bytes)
uint64_t allocsize;
unsigned align;
- // Initial value stored at this global variable.
- // This is initialized only if the global is constant.
- // If it has no initial value, this is nullptr.
- Value *initval;
- // initval can be null even if isconst is true if this is an external const.
bool isconst;
public:
GlobalVariable(Type &type, std::string &&name, uint64_t allocsize,
- unsigned align, Value *initval, bool isconst) :
+ unsigned align, bool isconst) :
Value(type, std::move(name)), allocsize(allocsize), align(align),
- initval(initval), isconst(isconst) {}
+ isconst(isconst) {}
uint64_t size() const { return allocsize; }
bool isConst() const { return isconst; }
- const Value* initVal() const { return initval; }
void print(std::ostream &os) const override;
StateValue toSMT(State &s) const override;
};

MODIFY
llvm_util/llvm2alive.cpp
llvm_util/llvm2alive.cpp
@@ -713,20 +713,19 @@ public:
auto GV = dynamic_cast<GlobalVariable *>(&Fn.getConstant(i));
if (!GV)
continue;
-
auto gv = M->getGlobalVariable(GV->getName().substr(1), true);
- if (!gv->hasInitializer())
+ if (!gv->isConstant() || !gv->hasInitializer())
continue;
- else if (auto CE = dyn_cast<llvm::ConstantExpr>(gv->getInitializer())) {
- auto storedval = convert_constexpr(CE);
+
+ auto storedval = get_operand(gv->getInitializer());
auto globalvar = get_operand(gv);
if (!storedval || !globalvar)
return {};
+
// Alignment is already enforced by Memory::alloc.
stores.emplace(gv->getName(),
make_unique<Store>(*globalvar, *storedval, 1));
}
- }
for (auto &itm : stores)
// Insert stores in lexicographical order of global var's names

MODIFY
llvm_util/utils.cpp
llvm_util/utils.cpp
@@ -212,22 +212,15 @@ Value* get_operand(llvm::Value *v,
// TODO: Global variable of opaque type is not supported.
return nullptr;
}
- Value *initval = nullptr;
- if (gv->hasInitializer() && gv->isConstant()) {
- auto initializer = gv->getInitializer();
- if (!isa<llvm::ConstantExpr>(initializer))
- if (!(initval = get_operand(initializer, constexpr_conv)))
- return nullptr;
- }
+
int size = DL->getTypeAllocSize(gv->getValueType());
int align = gv->getAlignment();
auto name = "@" + gv->getName().str();
auto val = make_unique<GlobalVariable>(*ty, move(name), size, align,
- initval, gv->isConstant());
- auto ret = val.get();
- gvar = ret;
+ gv->isConstant());
+ gvar = val.get();
current_fn->addConstant(move(val));
- return ret;
+ return gvar;
}
if (auto cnst = dyn_cast<llvm::ConstantAggregate>(v)) {

DELETE
tests/alive-tv/memory/glb-4.src.ll
-@x = global i8 0
-@y = global i8 0
-
-define i8 @f() {
- %v = load i8, i8* @x
- ret i8 %v
-}

DELETE
tests/alive-tv/memory/glb-4.tgt.ll
-@x = global i8 0
-@y = global i8 0
-
-define i8 @f() {
- %w = load i8, i8* @y
- %v = load i8, i8* @x
- ret i8 %v
-}

MODIFY
tests/alive-tv/memory/glb-disjoint.src.ll
tests/alive-tv/memory/glb-disjoint.src.ll
; target: 64 bits ptr addr
target datalayout = "p:64:64:64"
-@x = global i32 0
-@y = global i64 0
+@x = external global i32
+@y = external global i64
define i1 @disj() {
%ix = ptrtoint i32* @x to i64

MODIFY
tests/alive-tv/memory/glb-disjoint.tgt.ll
tests/alive-tv/memory/glb-disjoint.tgt.ll
; target: 64 bits ptr addr
target datalayout = "p:64:64:64"
-@x = global i32 0
-@y = global i64 0
+@x = external global i32
+@y = external global i64
define i1 @disj() {
ret i1 true

MODIFY
tools/transform.cpp
tools/transform.cpp
@@ -417,18 +417,6 @@ static void calculateAndInitConstants(Transform &t) {
}
}
- if (!nullptr_is_used) {
- for (auto gvs : { &globals_src , &globals_tgt }) {
- for (auto gv : *gvs) {
- if (auto init = gv->initVal()) {
- if ((nullptr_is_used |= has_nullptr(init)))
- goto exit_gv_loop;
- }
- }
- }
- }
-exit_gv_loop:
-
// Include null block
num_nonlocals = num_globals + num_ptrinputs + num_max_nonlocals_inst + 1;

uniformize handling of globals initializers such that they are all sorted