shankha
2010-Jul-14 18:41 UTC
[LLVMdev] Figuring out the parameters of the Call Instruction
Hi, I am trying to figure out how to read arguments of a call instruction. I had few questions based on that I have the following C Code 1 #include <stdio.h> 2 3 struct my_struct 4 { 5 int a; 6 int b; 7 }; 8 9 struct my_struct abc; 10 void p_ptr ( unsigned long j) 11 { 12 printf ( "%lx \n", j ); 13 } 14 15 void struct_ptr ( struct my_struct * s_ptr ) 16 { 17 printf ( "%p \n", s_ptr ); 18 } 19 20 int 21 main () 22 { 23 struct my_struct stack_abc; 24 p_ptr ((unsigned long) &abc); 25 struct_ptr ( &abc ); 26 p_ptr ((unsigned long) &stack_abc); 27 struct_ptr ( &stack_abc ); 28 return 0; 29 } 24 p_ptr ((unsigned long) &abc); call void @p_ptr(i64 ptrtoint (%struct.my_struct* @abc to i64)) nounwind, !dbg !31 Q.1 At line no 24 I try to read the address of global variable abc. The address is type casted from struct * to int * for which ptrtoint. I read the operands of the call instruction and there descriptions. I do not see among the fields of the call instruction the ptrtoint instruction. How can I reach the ptrtoint instruction from the call instruction ? What fields I need to access. 26 p_ptr ((unsigned long) &stack_abc); %stack_abc1 = ptrtoint %struct.my_struct* %stack_abc to i64, !dbg !33 ; <i64> [#uses=1] call void @p_ptr(i64 %stack_abc1) nounwind, !dbg !33 Q.2 stack_abc1 is a alias for the operation performed by the ptrtoint instruction. Which fields from the instruction class will help me read if I may say the "output" of the instruction ? Thanks Shankha Banerjee
Duncan Sands
2010-Jul-15 07:07 UTC
[LLVMdev] Figuring out the parameters of the Call Instruction
Hi Shankha,> 24 p_ptr ((unsigned long)&abc); > call void @p_ptr(i64 ptrtoint (%struct.my_struct* @abc to i64)) > nounwind, !dbg !31 > > Q.1 At line no 24 I try to read the address of global variable abc. > The address is type casted > from struct * to int * for which ptrtoint.I guess you mean "is type casted from struct * to unsigned long". I read the> operands of the call instruction and there > descriptions. I do not see among the fields of the call > instruction the ptrtoint instruction.In your case, this is not the ptrtoint instruction, it is the ptrtoint constant expression. Constants are written in the assembler at the point where they are used, as you see in your example.> How can I reach the ptrtoint instruction from the call > instruction ? What fields I need to access.It is the first argument to the call instruction CI, thus you can get it with CI->getArgOperand(0).> 26 p_ptr ((unsigned long)&stack_abc); > %stack_abc1 = ptrtoint %struct.my_struct* %stack_abc to i64, !dbg > !33 ;<i64> [#uses=1] > call void @p_ptr(i64 %stack_abc1) nounwind, !dbg !33 > > Q.2 stack_abc1 is a alias for the operation performed by the > ptrtoint instruction. > Which fields from the instruction class will help me read if I may say > the "output" of the instruction ?I didn't understand the question, sorry. Ciao, Duncan.
shankha
2010-Jul-15 17:27 UTC
[LLVMdev] Figuring out the parameters of the Call Instruction
Hi Duncan, Thanks for pointing out my mistake. I will reword my questions. //C code int var1; //global int a, b; foo(a, b); bar(c); generates following //LLVM IR %1 = load a; %2 = load b; call foo(%1, %2) call bar(@var1) CallInst.getOperand(1).getNameStr() on foo, returns null, but on bar returns var1. Similarly, for call void @p_ptr(i64 ptrtoint (%struct.my_struct* @abc to i64)) nounwind, !dbg !31 CallInst.getOperand(1).getNameStr(), return abc and type returns i64, How do I figure the operand is a const expression and a cast operator was used to generate it. More generally, When I use Instruction iterator on BasicBlock, I get the RHS of instruction but not the LHS. How do I get name of nameless LHS registers(%1, %2). C Code : p_ptr ((unsigned long) &abc); LLVM IR : call void @p_ptr(i64 ptrtoint (%struct.my_struct* @abc to i64)) nounwind, !dbg !52 I think by the call CI->getArgOperand(0) you mean CI->getOperand(0). That doesn't return the ptrtoint constant expression. The CI instruction has shows two operands. The first operand is the function name. The second operand prints NULL. thank you, Shankha Banerjee
Possibly Parallel Threads
- [LLVMdev] Figuring out the parameters of the Call Instruction
- [LLVMdev] Figuring out the parameters of the Call Instruction
- [LLVMdev] Alias analysis issue with structs on PPC
- [LLVMdev] Alias analysis issue with structs on PPC
- [LLVMdev] Alias analysis issue with structs on PPC