[llvm] [InstCombine] Extend folding of aggregate construction to cases when source aggregates are partially available (PR #100828)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 00:16:03 PDT 2024


https://github.com/dtcxzyw requested changes to this pull request.

Another reproducer causing instcombine to hang:
```
bin/opt -passes=instcombine reduced.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-pc-linux-gnu"

define { i64, ptr } @minmax_by({ i64, ptr } %0, i1 %1, i1 %2, { i64, ptr } %3) {
  %5 = extractvalue { i64, ptr } %0, 0
  %6 = extractvalue { i64, ptr } %0, 1
  br label %7

7:                                                ; preds = %14, %4
  %.sroa.01.0 = phi i64 [ %5, %4 ], [ %.sroa.01.1, %14 ]
  %.sroa.3.0 = phi ptr [ %6, %4 ], [ %.sroa.3.1, %14 ]
  br i1 %1, label %8, label %15

8:                                                ; preds = %7
  br i1 %2, label %9, label %12

9:                                                ; preds = %8
  %10 = extractvalue { i64, ptr } %3, 0
  %11 = extractvalue { i64, ptr } %3, 1
  br label %14

12:                                               ; preds = %8
  %13 = getelementptr { i64, ptr }, ptr null, i32 0, i32 0
  br label %14

14:                                               ; preds = %12, %9
  %.sroa.01.1 = phi i64 [ %10, %9 ], [ %.sroa.01.0, %12 ]
  %.sroa.3.1 = phi ptr [ %11, %9 ], [ %.sroa.3.0, %12 ]
  br label %7

15:                                               ; preds = %7
  %.fca.0.insert = insertvalue { i64, ptr } zeroinitializer, i64 %.sroa.01.0, 0
  %.fca.1.insert = insertvalue { i64, ptr } %.fca.0.insert, ptr %.sroa.3.0, 1
  ret { i64, ptr } %.fca.1.insert
}
```

BTW, I don't think this transform is beneficial since it blocks some select-related optimizations:
Before this patch:
```
define hidden { i64, i64 } @"_ZN4core6result19Result$LT$T$C$E$GT$7map_err17h84015741f1851455E.llvm.9397651224479784633"(i64 noundef %0, i64 %1) unnamed_addr #0 {
   %3 = insertvalue { i64, i64 } poison, i64 %0, 0
   %4 = insertvalue { i64, i64 } %3, i64 %1, 1
   ret { i64, i64 } %4
 }
```
After this patch:
```
define hidden { i64, i64 } @"_ZN4core6result19Result$LT$T$C$E$GT$7map_err17h84015741f1851455E.llvm.9397651224479784633"(i64 noundef %0, i64 %1) unnamed_addr #0 {
   %3 = icmp eq i64 %0, -9223372036854775807
   %4 = insertvalue { i64, i64 } poison, i64 %0, 0
   %5 = insertvalue { i64, i64 } %4, i64 %1, 1
   %.merged = select i1 %3, { i64, i64 } { i64 -9223372036854775807, i64 undef }, { i64, i64 } %5
   ret { i64, i64 } %.merged
 }
```

Can you provide some performance data to demonstrate the usefulness of this patch?

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


More information about the llvm-commits mailing list