[llvm] [flang] [clang] [InstCombine] Canonicalize constant GEPs to i8 source element type (PR #68882)

Yingwei Zheng via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 21 11:51:56 PST 2023


dtcxzyw wrote:

> @nikic Could you please have a look at [dtcxzyw/llvm-opt-benchmark#17](https://github.com/dtcxzyw/llvm-opt-benchmark/pull/17)? One regression:
> 
> ```
> diff --git a/bench/brotli/optimized/compound_dictionary.c.ll b/bench/brotli/optimized/compound_dictionary.c.ll
> index 21fd37fd..b9894810 100644
> --- a/bench/brotli/optimized/compound_dictionary.c.ll
> +++ b/bench/brotli/optimized/compound_dictionary.c.ll
> @@ -3,9 +3,6 @@ source_filename = "bench/brotli/original/compound_dictionary.c.ll"
>  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
>  target triple = "x86_64-unknown-linux-gnu"
>  
> -%struct.PreparedDictionary = type { i32, i32, i32, i32, i32, i32 }
> -%struct.CompoundDictionary = type { i64, i64, [16 x ptr], [16 x ptr], [16 x i64], i64, [16 x ptr] }
> -
>  ; Function Attrs: nounwind uwtable
>  define hidden ptr @CreatePreparedDictionary(ptr noundef %m, ptr noundef %source, i64 noundef %source_size) local_unnamed_addr #0 {
>  entry:
> @@ -168,25 +165,29 @@ cond.true119.i:                                   ; preds = %for.end106.i
>  
>  cond.end123.i:                                    ; preds = %cond.true119.i, %for.end106.i
>    %cond124.i = phi ptr [ %call121.i, %cond.true119.i ], [ null, %for.end106.i ]
> -  %arrayidx125.i = getelementptr inbounds %struct.PreparedDictionary, ptr %cond124.i, i64 1
> +  %arrayidx125.i = getelementptr inbounds i8, ptr %cond124.i, i64 24
>    %arrayidx127.i = getelementptr inbounds i32, ptr %arrayidx125.i, i64 %idxprom.i
>    %arrayidx129.i = getelementptr inbounds i16, ptr %arrayidx127.i, i64 %idxprom26.i
>    %arrayidx131.i = getelementptr inbounds i32, ptr %arrayidx129.i, i64 %conv113.i
>    store i32 -558043677, ptr %cond124.i, align 4
> -  %num_items.i = getelementptr inbounds %struct.PreparedDictionary, ptr %cond124.i, i64 0, i32 1
> +  %num_items.i = getelementptr inbounds i8, ptr %cond124.i, i64 4
>    store i32 %add100.i, ptr %num_items.i, align 4
>    %conv132.i = trunc i64 %source_size to i32
> -  %source_size133.i = getelementptr inbounds %struct.PreparedDictionary, ptr %cond124.i, i64 0, i32 2
> +  %source_size133.i = getelementptr inbounds i8, ptr %cond124.i, i64 8
>    store i32 %conv132.i, ptr %source_size133.i, align 4
> -  %hash_bits134.i = getelementptr inbounds %struct.PreparedDictionary, ptr %cond124.i, i64 0, i32 3
> +  %hash_bits134.i = getelementptr inbounds i8, ptr %cond124.i, i64 12
>    store i32 40, ptr %hash_bits134.i, align 4
> -  %bucket_bits135.i = getelementptr inbounds %struct.PreparedDictionary, ptr %cond124.i, i64 0, i32 4
> +  %bucket_bits135.i = getelementptr inbounds i8, ptr %cond124.i, i64 16
>    store i32 %bucket_bits.0.lcssa, ptr %bucket_bits135.i, align 4
> -  %slot_bits136.i = getelementptr inbounds %struct.PreparedDictionary, ptr %cond124.i, i64 0, i32 5
> +  %slot_bits136.i = getelementptr inbounds i8, ptr %cond124.i, i64 20
>    store i32 %slot_bits.0.lcssa, ptr %slot_bits136.i, align 4
>    store ptr %source, ptr %arrayidx131.i, align 1
>    br label %for.body140.i
>  
> +for.cond151.preheader.i:                          ; preds = %for.body140.i
> +  %invariant.gep.i = getelementptr i8, ptr %arrayidx129.i, i64 -4
> +  br label %for.body154.i
> +
>  for.body140.i:                                    ; preds = %for.body140.i, %cond.end123.i
>    %indvars.iv145.i = phi i64 [ 0, %cond.end123.i ], [ %indvars.iv.next146.i, %for.body140.i ]
>    %total_items.1139.i = phi i32 [ 0, %cond.end123.i ], [ %add145.i, %for.body140.i ]
> @@ -198,10 +199,10 @@ for.body140.i:                                    ; preds = %for.body140.i, %con
>    store i32 0, ptr %arrayidx144.i, align 4
>    %indvars.iv.next146.i = add nuw nsw i64 %indvars.iv145.i, 1
>    %exitcond150.not.i = icmp eq i64 %indvars.iv.next146.i, %idxprom.i
> -  br i1 %exitcond150.not.i, label %for.body154.i, label %for.body140.i, !llvm.loop !9
> +  br i1 %exitcond150.not.i, label %for.cond151.preheader.i, label %for.body140.i, !llvm.loop !9
>  
> -for.body154.i:                                    ; preds = %for.body140.i, %for.inc204.i
> -  %indvars.iv152.i = phi i64 [ %indvars.iv.next153.i, %for.inc204.i ], [ 0, %for.body140.i ]
> +for.body154.i:                                    ; preds = %for.inc204.i, %for.cond151.preheader.i
> +  %indvars.iv152.i = phi i64 [ 0, %for.cond151.preheader.i ], [ %indvars.iv.next153.i, %for.inc204.i ]
>    %5 = trunc i64 %indvars.iv152.i to i32
>    %and155.i = and i32 %sub3.i, %5
>    %arrayidx158.i = getelementptr inbounds i16, ptr %arrayidx25.i, i64 %indvars.iv152.i
> @@ -243,7 +244,7 @@ for.body194.i:                                    ; preds = %for.body194.i, %if.
>    %pos.0.in140.i = phi ptr [ %arrayidx189.i, %if.end177.i ], [ %arrayidx198.i, %for.body194.i ]
>    %pos.0.i = load i32, ptr %pos.0.in140.i, align 4
>    %inc195.i = add nuw nsw i64 %cursor.0142.i, 1
> -  %arrayidx196.i = getelementptr i32, ptr %arrayidx129.i, i64 %cursor.0142.i
> +  %arrayidx196.i = getelementptr inbounds i32, ptr %arrayidx129.i, i64 %cursor.0142.i
>    store i32 %pos.0.i, ptr %arrayidx196.i, align 4
>    %idxprom197.i = zext i32 %pos.0.i to i64
>    %arrayidx198.i = getelementptr inbounds i32, ptr %arrayidx29.i, i64 %idxprom197.i
> @@ -252,9 +253,9 @@ for.body194.i:                                    ; preds = %for.body194.i, %if.
>    br i1 %exitcond151.not.i, label %for.end201.i, label %for.body194.i, !llvm.loop !10
>  
>  for.end201.i:                                     ; preds = %for.body194.i
> -  %arrayidx196.i.le = getelementptr i32, ptr %arrayidx129.i, i64 %cursor.0142.i
> +  %gep.i = getelementptr i32, ptr %invariant.gep.i, i64 %inc195.i
>    %or.i = or i32 %pos.0.i, -2147483648
> -  store i32 %or.i, ptr %arrayidx196.i.le, align 4
> +  store i32 %or.i, ptr %gep.i, align 4
>    br label %for.inc204.i
>  
>  for.inc204.i:                                     ; preds = %for.end201.i, %if.then174.i
> ```
> 
> Alive2: https://alive2.llvm.org/ce/z/JfN5sB
> 
> I will post a patch later.

Should be fixed by #76177. I don't think it needs to block this patch.


https://github.com/llvm/llvm-project/pull/68882


More information about the cfe-commits mailing list