[PATCH] D137212: [InstCombine] Simplify chain of GEP with constant indices

William Junda Huang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 17:03:56 PDT 2022


huangjd created this revision.
huangjd added reviewers: davidxl, Carrot, nikic, spatel, reames.
Herald added a subscriber: hiraditya.
Herald added a project: All.
huangjd requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Simplify chain of GEP in the form of `GEP (GEP ... (GEP (GEP p C1 <https://reviews.llvm.org/C1>) X1)
... Xn) C2` where `C1` and `C2` are constants by splitting `GEP p C1`
and breaking its user depedency, allowing visitGEPofGEP to reassociate
the cloned inner GEP to outer GEP and perform constant index merging.

Example code that is optimized

  struct S {
      char padding[123];
      int x;
  };
  
  int* P(S* s) {
      return (int*)((char*)&s->x + s->x - offsetof(S, x));
  }

originally generates

  P(S*):                                # @P(S*)
          movslq  124(%rdi), %rax
          addq    %rdi, %rax
          addq    $124, %rax
          addq    $-124, %rax
          retq

Now generates

  P(S*):                                # @P(S*)
  	movslq	124(%rdi), %rax
  	addq	%rdi, %rax
  	retq


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137212

Files:
  llvm/lib/Transforms/InstCombine/InstCombineInternal.h
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/gep-merge-constant-indices.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137212.472459.patch
Type: text/x-patch
Size: 7016 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221102/1d8f776c/attachment.bin>


More information about the llvm-commits mailing list