Use iterator misunderstanding?


I try to follow the use of the value yielded by a malloc call. Here follows a concrete example of what I expect and what I get.

define void @main() #0 {
%ptr = alloca i8*, align 8
%call = call noalias i8* @malloc(i64 10) #2
store i8* %call, i8** %ptr, align 8
%0 = load i8*, i8** %ptr, align 8
call void @free(i8* %0) #2
ret void

It is a simple program that allocates and frees a pointer. Now I intercepted the call instruction to malloc and iterated over its uses. I expected to have 2 results:

%call = call noalias i8* @malloc(i64 10) #2

store i8* %call, i8** %ptr, align 8

But I only get the first one. Is it normal? What should I do to get both? Here the piece of code I used to iterate:

CallInst *CI = …The call to malloc…
errs() << " Value use:\n";
for(Value::use_iterator i = CI->use_begin(), e = CI->use_end(); i != e; ++i) {
Use *use_i = &*i;
if(Instruction *I = dyn_cast(use_i)) {
errs() << " instruction: " <<
*I << “\n”;

And I get this output:

Value use:
instruction: %call = call noalias i8* @malloc(i64 10) #2

Thanks for your time and help,


Hello there, Pierre,

From your example code, there are two things we need to define:

  1. This instruction is a definition (which can be used by others):
    %call = call noalias i8* @malloc(i64 10) #2
  2. This instruction is a user of that definition showed above:
    store i8* %call, i8** %ptr, align 8