[getelementptr] how delete a subscript index for IR getelementptr

hi,
Now I get the following IR getelementptr

  • %gep = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* %p, i64 1, i64 2
    and I want to delete the last subscript index, i.e, how to get the following simalar IR base the above expressions
  • %gep = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* %p, i64 1

You can’t generally mutate instructions in-place in LLVM, partly because it can change the type and lead to inconsistent IR.

In this case for example the first GEP has type i32* but the second has type <vscale x 4 x i32>*.

So instead you’d use an IRBuilder to create the new GEP instruction you want in the same location, then maybe bitcast its output back to i32* and call replaceAllUsesWith to make everything use this new bitcasted GEP. Then you could erase the old one from the block (or maybe even leave it there for a dead code elimination cleanup pass to take care of).

I may not expression my question clear, and thanks very much for your attention.

I only want to delete the last subscript index and get the same expression getelementptr <vscale x 4 x i32>, <vscale x 4 x i32> %p, i64 1* for the following two IRs for example, so then I will easy to check they have the same base as they may use the same pointer.
And I don’t need use them to update exist IR sequences.

%gep1 = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* %p, i64 1, i64 2
%gep2 = getelementptr <vscale x 4 x i32>, <vscale x 4 x i32>* %p, i64 1, i64 3