[PATCH] D36878: Inst Combine GEP Flatten

Ashutosh Nema via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 18 09:23:37 PDT 2017


ashutosh.nema created this revision.

InstCombiner transform GEP accesses to source type, ideally it should keep the GEP to the actual/destination type.

Consider below test-case:
struct ABC {

  int A;
  int B[100];
  struct XYZ {
    int X;
    int Y[100];
  } OBJ;

};
void Setup(struct ABC *);
int foo(int offset) {

  struct ABC *Ptr = malloc(sizeof(struct ABC));
  Setup(Ptr);
  return Ptr->OBJ.X + Ptr->OBJ.Y[33];

}

Generated IR for the test-case:
define i32 @foo(i32 %offset) local_unnamed_addr #0 {
entry:

  %call = tail call i8* @malloc(i64 808)
  %0 = bitcast i8* %call to %struct.ABC*
  tail call void @Setup(%struct.ABC* %0) #3
  %OBJ = getelementptr inbounds i8, i8* %call, i64 404
  %X = bitcast i8* %OBJ to i32*
  %1 = load i32, i32* %X, align 4, !tbaa !2
  %arrayidx = getelementptr inbounds i8, i8* %call, i64 540
  %2 = bitcast i8* %arrayidx to i32*
  %3 = load i32, i32* %2, align 4, !tbaa !8
  %add = add nsw i32 %3, %1
  ret i32 %add

}

Instruction combiner transforms GEPs to i8 type, because of this the GEP offset looks weird and the actual type information is missing on GEP.

This change is to retain the GEP’s to the actual type.
For the above test it will now generate below IR:
; Function Attrs: nounwind uwtable
define i32 @foo(i32 %offset) local_unnamed_addr #0 {
entry:

  %call = tail call i8* @malloc(i64 808)
  %0 = bitcast i8* %call to %struct.ABC*
  tail call void @Setup(%struct.ABC* %0) #3
  %X = getelementptr inbounds %struct.ABC, %struct.ABC* %0, i64 0, i32 2, i32 0
  %1 = load i32, i32* %X, align 4, !tbaa !2
  %arrayidx = getelementptr inbounds %struct.ABC, %struct.ABC* %0, i64 0, i32 2, i32 1, i64 33
  %2 = load i32, i32* %arrayidx, align 4, !tbaa !8
  %add = add nsw i32 %2, %1
  ret i32 %add

}


Repository:
  rL LLVM

https://reviews.llvm.org/D36878

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/element-atomic-memcpy-to-loads.ll
  test/Transforms/InstCombine/gep-flatten.ll
  test/Transforms/InstCombine/getelementptr.ll
  test/Transforms/InstCombine/icmp.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36878.111687.patch
Type: text/x-patch
Size: 8862 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170818/88de98fd/attachment.bin>


More information about the llvm-commits mailing list