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

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.

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

Hi Shankha,

//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.

names are of no interest. They are only there to help developers
see where the IR may have come from in the original source code,
and cannot be relied upon to exist.

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.

You can determine if it is a constant expression by
   isa<ConstantExpr>(CallInst.getOperand(1))

If it is a constant expression, you determine if it is a cast by
   cast<ConstantExpr>(CallInst.getOperand(1))->isCast()

These can be combined into
   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(CallInst.getOperand(1)))
     if (CE->isCast())
        ...

More generally, When I use Instruction iterator on BasicBlock, I get
the RHS of instruction
but not the LHS.

There is no LHS, the apparent LHS in the human readable IR is only there to
make it easier for humans to read.

How do I get name of nameless LHS registers(%1, %2).

They don't have a name, but you don't need one: just use the instruction
itself.

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).

Probably getArgOperand doesn't exist in the version of LLVM you are using:
it was only introduced recently. In older versions you probably want
CI->getOperand(1) since operand 0 is the callee IIRC.

That doesn't return the ptrtoint constant expression. The CI instruction
has shows two operands. The first operand is the function name.

The first operand is the function (not the function name).

The

second operand
prints NULL.

Try CI->getOperand(1)->dump()

Ciao,

Duncan.