[llvm-dev] The GEP formats when generating IR.

周书林 via llvm-dev llvm-dev at lists.llvm.org
Sun May 30 00:13:30 PDT 2021


Hi everyone,

I am learning llvm IR from official documents, especially the GEP
instruction.

Now I have a problem about the GEP instructions generated by llvm.

As the example in
https://llvm.org/docs/LangRef.html#getelementptr-instruction say, for the
function "*foo" *in the following code example:

struct RT {
  char A;
  int B[10][20];
  char C;};struct ST {
  int X;
  double Y;
  struct RT Z;};
int *foo(struct ST *s) {
  return &s[1].Z.B[5][13];}

the IR could be:

define i32* @foo(%struct.ST* %s) nounwind uwtable readnone optsize ssp {entry:
  %arrayidx = getelementptr inbounds %struct.ST, %struct.ST* %s, i64
1, i32 2, i32 1, i64 5, i64 13
  ret i32* %arrayidx}

or

define i32* @foo(%struct.ST* %s) {
  %t1 = getelementptr %struct.ST, %struct.ST* %s, i32 1
        ; yields %struct.ST*:%t1
  %t2 = getelementptr %struct.ST, %struct.ST* %t1, i32 0, i32 2
        ; yields %struct.RT*:%t2
  %t3 = getelementptr %struct.RT, %struct.RT* %t2, i32 0, i32 1
        ; yields [10 x [20 x i32]]*:%t3
  %t4 = getelementptr [10 x [20 x i32]], [10 x [20 x i32]]* %t3, i32
0, i32 5  ; yields [20 x i32]*:%t4
  %t5 = getelementptr [20 x i32], [20 x i32]* %t4, i32 0, i32 13
        ; yields i32*:%t5
  ret i32* %t5}

I wonder when the llvm will generate the former one, and when it will
generate the later one?

Thank you very much!

Sincerely,
Shulin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20210530/ca870f30/attachment.html>


More information about the llvm-dev mailing list