global variable uses

Hi,

I’m trying to change all uses of a global variable so they’ll use a different global variable.

For instance, I have:

@a = global [100 x [100 x i64]] zeroinitializer, align 16

@b = global [100 x [100 x i64]] zeroinitializer, align 16

And I want to change the use

%arrayidx = getelementptr inbounds [100 x [100 x i64]]* @a, i32 0, i64 %3

to

%arrayidx = getelementptr inbounds [100 x [100 x i64]]* @b, i32 0, i64 %3

But, when I’m iterating the uses with the use_iterator of the Global variable, I get the Global variable’s definition/allocation as the use(s) and not the “real” uses (the GEP).

Why? Isn’t the meaning of use here is where @a is used?

The relevant code (var is a GlobalVariable *):

var->dump(); // prints: @a = global [100 x [100 x i64]] zeroinitializer, align 16

for (GlobalVariable::use_iterator U = var->use_begin(); U != var->use_end(); U++) {

Constant * CE = dyn_cast(*U); // this is the only cast that works here

CE->dump(); // prints: @a = global [100 x [100 x i64]] zeroinitializer, align 16

}

Thanks for your help,

Tehila.

Hi Tehila,

Why? Isn't the meaning of use here is where @a is used?

Each element you're iterating through is an llvm::Use, which records
both what is being used and the user. Possibly confusingly, what you
get when you simply dereference it is the thing being used. You should
either call U->getUser() or iterate using user_begin/user_end instead.

Though there's actually a function Value::ReplaceAllUsesWith which
might mean you don't need the loop at all, depending on how complex
your change is.

Cheers.

Tim.

Thanks a lot for the explanation, Tim!
Tehila.