Pierre Gagelin via llvm-dev
2016-Jun-30 09:05 UTC
[llvm-dev] Use iterator misunderstanding?
Hi, 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 { entry: %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 and 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<Instruction>(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, Pierre -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160630/bfee128e/attachment.html>
Cristianno Martins via llvm-dev
2016-Jun-30 18:50 UTC
[llvm-dev] Use iterator misunderstanding?
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 What you are doing when you cast a Use of an instruction is that you are going through all the uses of an Instruction, which ends up being the same instruction from 1. As an example, if you had these three instructions in your code: [...] *%call* = call noalias i8* @malloc(i64 10) #2 [...] store i8* *%call*, i8** %ptr, align 8 [...] %arrayidx = getelementptr inbounds i8, i8* *%call*, i32 %add.i [...] And you iterate through all the uses, you would get: Instruction: *%call* = call noalias i8* @malloc(i64 10) #2 Instruction: *%call* = call noalias i8* @malloc(i64 10) #2 As a result. This is because the same %call, defined by this instruction, is being used on each of the instructions above (the store and the gep). Now, if you want to get the *user* of an instruction (which would be the store and the gep instructions above), you would have to explicitly ask for the user with: Instruction *I = dyn_cast<Instruction>(*use_i->getUser()*) Which results in: Instruction: store i8* *%call*, i8** %ptr, align 8 Instruction: %arrayidx = getelementptr inbounds i8, i8* *%call*, i32 %add.i However, keep in mind this line will get you all the instructions that *use* that %call value, and not the %call definition itself. In other words, you will not get "*%call* = call noalias i8* @malloc(i64 10) #2" from this example code of yours, but only the store. I hope this helps. -- Cristianno Martins PhD Student of Computer Science University of Campinas cmartins at ic.unicamp.br <cristiannomartins at hotmail.com> On Thu, Jun 30, 2016 at 6:05 AM, Pierre Gagelin via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > 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 { > entry: > %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 > and > 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<Instruction>(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, > Pierre > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160630/738f4847/attachment-0001.html>