[llvm] [RISCV] Use addi rather than addiw for immediates materialised by lui+addi(w) pairs when possible (PR #141663)

Alex Bradbury via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 2 13:55:41 PDT 2025


asb wrote:

> > I've edited the PR description and written up the extra testing + analysis there. It turns out this is actually results in a meaningful reduction of dynamic instcount for 519.lbm_r (~4%).
> 
> Is that from the FoldMemOffset pass?

Yes exactly, that's when they start to diverge. I think the diff for the entry basic block is telling:

```diff
   %19:gpr = COPY $x11
   %18:gpr = COPY $x10
   %21:gpr = LUI 1048185
-  %22:gpr = ADDIW %21:gpr, 1520
-  %0:gpr = ADD %19:gpr, killed %22:gpr
+  %0:gpr = ADD %19:gpr, %21:gpr
   %23:gpr = LUI 391
-  %24:gpr = ADDIW %23:gpr, -1560
-  %1:gpr = ADD %19:gpr, killed %24:gpr
-  %25:gpr = ADDIW %21:gpr, 1824
-  %2:gpr = ADD %19:gpr, killed %25:gpr
-  %26:gpr = ADDIW %23:gpr, -1256
-  %3:gpr = ADD %19:gpr, killed %26:gpr
+  %1:gpr = ADD %19:gpr, %23:gpr
+  %2:gpr = ADD %19:gpr, %21:gpr
+  %3:gpr = ADD %19:gpr, %23:gpr
   %27:gpr = LUI 1048181
-  %28:gpr = ADDIW killed %27:gpr, 2032
-  %4:gpr = ADD %19:gpr, killed %28:gpr
+  %4:gpr = ADD %19:gpr, %27:gpr
   %29:gpr = LUI 387
-  %30:gpr = ADDIW killed %29:gpr, -1048
-  %5:gpr = ADD %19:gpr, killed %30:gpr
+  %5:gpr = ADD %19:gpr, %29:gpr
   %31:gpr = LUI 1048189
-  %32:gpr = ADDIW killed %31:gpr, 1248
-  %6:gpr = ADD %19:gpr, killed %32:gpr
+  %6:gpr = ADD %19:gpr, %31:gpr
   %33:gpr = LUI 395
-  %34:gpr = ADDIW killed %33:gpr, -1832
-  %7:gpr = ADD %19:gpr, killed %34:gpr
+  %7:gpr = ADD %19:gpr, %33:gpr
   %35:gpr = ADDI $x0, -2010
   %8:gpr = SH3ADD killed %35:gpr, %19:gpr
   %36:gpr = ADDI $x0, -1971
   %9:gpr = SH3ADD killed %36:gpr, %19:gpr
-  %37:gpr = ADDIW %21:gpr, 1584
-  %10:gpr = ADD %19:gpr, killed %37:gpr
-  %38:gpr = ADDIW %23:gpr, -1496
-  %11:gpr = ADD %19:gpr, killed %38:gpr
+  %10:gpr = ADD %19:gpr, %21:gpr
+  %11:gpr = ADD %19:gpr, %23:gpr
   %39:gpr = ADDI $x0, 2001
   %12:gpr = SH3ADD killed %39:gpr, %19:gpr
   %40:gpr = ADDI $x0, -1998
```
(where of course, the number of required regs is reduced as some of the offsets previously stored to regs are now folded directly into memory accesses later in the function).

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


More information about the llvm-commits mailing list