[v3,0/3] LoongArch: TLS type transition instruction removal and old LE relax

Message ID 20240301061751.3061364-1-cailulu@loongson.cn
Headers
Series LoongArch: TLS type transition instruction removal and old LE relax |

Message

Lulu Cai March 1, 2024, 6:17 a.m. UTC
  This modification mainly changes the timing of type transition,
adds relaxation to the old LE instruction sequence.

We strictly distinguish between type transition and relaxation.
Type transition is from one type to another, while relaxation
is the removal of instructions under the same TLS type. Detailed
instructions are as follows:

1. For type transition, only the normal code model of DESC/IE
does type transition, and each relocation is accompanied by a
R_LARCH_RELAX. Neither abs nor extreme will do type transition,
and no R_LARCH_RELAX will be generated.
The extra instructions when DESC transitions to other TLS types
will be deleted during the type transition.

Type Transition Delete Instructions:
pcalau12i $a0,%desc_pc_hi20(sym)      =>  pcalau12i $a0,%ie_pc_hi20(sym)
addi.d    $a0,$a0,%desc_pc_lo12(sym)  =>  ld.d  $a0,$a0,%ie_pc_lo12(sym)
ld.d      $ra,$a0,%desc_ld(sym)       =>  (deleted)
jirl      $ra,$ra,%desc_call(sym)     =>  (deleted)

2. Implemented relaxation for the old LE instruction sequence.
The first two instructions of LE's 32-bit and 64-bit models
use the same relocations and cannot be distinguished based on
relocations. Therefore, for LE's instruction sequence, any code
model will try to relaxation.

LE of 32-bit relaxation:
lu12i.w $a0,%le_hi20(sym)       =>  (deleted)
ori     $a0,$a0,%le_lo12(sym)   =>  ori $a0,$zero,%le_lo12(sym)

LE of 64-bit relaxation:
lu12i.w $a0,$le_hi20(sym)       =>  (deleted)
ori     $a0,%le_lo12(sym)       =>  ori $a0,$zero,%le_lo12(sym)
lu32i.d $a0,%le64_lo20(sym)     =>  (deleted)
lu52i.d $a0,$a0,le64_hi12(sym)  =>  (deleted)

3. Some function names have been adjusted to facilitate understanding,
parameters have been adjusted, and unused macros have been deleted.

----
V1 -> V2:
* Distinguished the different behavior of type transitions when using
  --relax/--no-relax.
  If --no-relax is used, the nop in DESC->IE/LE will not be deleted.
  such as DESC->LE:
  pcalau12i $a0,%desc_pc_hi20(sym)      =>  lu12i.w $a0,%le_hi20(sym)
  addi.d    $a0,$a0,%desc_pc_lo12(sym)  =>  ori   $a0,$a0,%le_lo12(sym)
  ld.d      $ra,$a0,%desc_ld(sym)       =>  nop
  jirl      $ra,$ra,%desc_call(sym)     =>  nop

  If --relax is used, the nop in DESC->IE/LE will be deleted. And it
  will try to relaxation the LE instruction sequence after the tls type
  transition.
* Added a check to see if the current relocation is the last for the
  section to prevent excessive reads.
* Added new test cases for testing --relax and --no-relax.

----
V2 -> V3:
* Add dtpoff calculation function to prevent ld from crashing if tls_sec
  does not exist.
* The gas test case and ld test case have been updated, and test cases
  for different code models have been added to the ld test case.

Lulu Cai (3):
  LoongArch: Delete extra instructions when TLS type transition
  LoongArch: Add dtpoff calculation function
  LoongArch: Fix some test cases for TLS transition and relax

 bfd/elfnn-loongarch.c                         | 426 +++++++++++-------
 gas/config/tc-loongarch.c                     |  31 +-
 gas/testsuite/gas/loongarch/macro_op.d        |   4 +
 gas/testsuite/gas/loongarch/macro_op_32.d     |   4 +
 .../gas/loongarch/macro_op_extreme_abs.d      |   4 +-
 .../gas/loongarch/macro_op_extreme_pc.d       |   2 +
 .../relax-cfi-fde-DW_CFA_advance_loc.d        |  16 +-
 .../relax-cfi-fde-DW_CFA_advance_loc.s        |   8 +
 gas/testsuite/gas/loongarch/reloc.d           |   8 +
 gas/testsuite/gas/loongarch/tlsdesc_32.d      |  27 --
 gas/testsuite/gas/loongarch/tlsdesc_32.s      |  12 -
 gas/testsuite/gas/loongarch/tlsdesc_64.d      |   2 +
 ld/testsuite/ld-loongarch-elf/desc-ie.d       |  14 +-
 ld/testsuite/ld-loongarch-elf/desc-ie.s       |  13 +-
 .../ld-loongarch-elf/desc-le-norelax.d        |  15 +
 .../ld-loongarch-elf/desc-le-norelax.s        |  11 +
 ld/testsuite/ld-loongarch-elf/desc-le-relax.d |  13 +
 ld/testsuite/ld-loongarch-elf/desc-le-relax.s |  14 +
 ld/testsuite/ld-loongarch-elf/desc-le.d       |  15 -
 ld/testsuite/ld-loongarch-elf/desc-le.s       |  14 -
 ld/testsuite/ld-loongarch-elf/ie-le-norelax.d |  13 +
 .../{ie-le.s => ie-le-norelax.s}              |   4 +-
 ld/testsuite/ld-loongarch-elf/ie-le-relax.d   |  13 +
 ld/testsuite/ld-loongarch-elf/ie-le-relax.s   |  13 +
 ld/testsuite/ld-loongarch-elf/ie-le.d         |  13 -
 .../ld-loongarch-elf/ld-loongarch-elf.exp     |  11 +-
 ld/testsuite/ld-loongarch-elf/macro_op.d      |   4 +
 ld/testsuite/ld-loongarch-elf/relax.exp       |   6 +-
 .../ld-loongarch-elf/tls-le-norelax.d         |  18 +
 .../{tls-le.s => tls-le-norelax.s}            |   4 +
 ld/testsuite/ld-loongarch-elf/tls-le-relax.d  |  13 +
 ld/testsuite/ld-loongarch-elf/tls-le-relax.s  |  22 +
 ld/testsuite/ld-loongarch-elf/tls-le.d        |  14 -
 ld/testsuite/ld-loongarch-elf/tlsdesc-dso.d   |  86 ++--
 ld/testsuite/ld-loongarch-elf/tlsdesc-dso.s   |   9 +
 ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d   |  23 +
 ld/testsuite/ld-loongarch-elf/tlsdesc_abs.s   |   7 +
 .../ld-loongarch-elf/tlsdesc_extreme.d        |  25 +
 .../ld-loongarch-elf/tlsdesc_extreme.s        |   7 +
 39 files changed, 620 insertions(+), 338 deletions(-)
 delete mode 100644 gas/testsuite/gas/loongarch/tlsdesc_32.d
 delete mode 100644 gas/testsuite/gas/loongarch/tlsdesc_32.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/desc-le-norelax.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/desc-le-norelax.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/desc-le-relax.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/desc-le-relax.s
 delete mode 100644 ld/testsuite/ld-loongarch-elf/desc-le.d
 delete mode 100644 ld/testsuite/ld-loongarch-elf/desc-le.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/ie-le-norelax.d
 rename ld/testsuite/ld-loongarch-elf/{ie-le.s => ie-le-norelax.s} (63%)
 create mode 100644 ld/testsuite/ld-loongarch-elf/ie-le-relax.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/ie-le-relax.s
 delete mode 100644 ld/testsuite/ld-loongarch-elf/ie-le.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/tls-le-norelax.d
 rename ld/testsuite/ld-loongarch-elf/{tls-le.s => tls-le-norelax.s} (70%)
 create mode 100644 ld/testsuite/ld-loongarch-elf/tls-le-relax.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/tls-le-relax.s
 delete mode 100644 ld/testsuite/ld-loongarch-elf/tls-le.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/tlsdesc_abs.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/tlsdesc_abs.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/tlsdesc_extreme.s