GlobalVariable initializer using from beyond the grave

For LLDB I’m writing a dumb module pass that removes all global variables, by running the following code:

bool erased = true;

while (erased)
erased = false;

for (Module::global_iterator gi = llvm_module.global_begin(), ge = llvm_module.global_end();
gi != ge;
GlobalVariable *global_var = dyn_cast(gi);

if (global_var->use_empty())
log->Printf(“Did remove %s”,
erased = true;

It’s not super efficient and it falls over in the face of cycles, but that’s not what I’m running into. Rather, Constants inside the initializers for global variables that I successfully removed are still showing up as uses for global variables that I haven’t yet removed. E.g., I couldn’t erase:

@"\01L_OBJC_SELECTOR_REFERENCES_" = internal global i8* getelementptr inbounds ([20 x i8]* inttoptr (i64 4295929920 to [20 x i8]*), i32 0, i32 0), section “__DATA, __objc_selrefs, literal_pointers, no_dead_strip”

because the initializer for

@llvm.used = appending global [2 x i8*] [i8* getelementptr inbounds ([20 x i8]* inttoptr (i64 4295929920 to [20 x i8]), i32 0, i32 0), i8 bitcast (i8** @"\01L_OBJC_SELECTOR_REFERENCES_" to i8*)]

which I had previously erased, was still claimed as a use.

Is there some way I need to purge the GlobalVariable beyond just calling eraseFromParent()? Do I need to rebuild the use graph or something?


You're looking for Constant::removeDeadConstantUsers()