]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
LoongArch: Avoid non-returning indirect jumps through $ra [PR110136]
authorLulu Cheng <chenglulu@loongson.cn>
Wed, 7 Jun 2023 02:21:58 +0000 (10:21 +0800)
committerLulu Cheng <chenglulu@loongson.cn>
Thu, 15 Jun 2023 08:23:42 +0000 (16:23 +0800)
Micro-architecture unconditionally treats a "jr $ra" as "return from subroutine",
hence doing "jr $ra" would interfere with both subroutine return prediction and
the more general indirect branch prediction.

Therefore, a problem like PR110136 can cause a significant increase in branch error
prediction rate and affect performance. The same problem exists with "indirect_jump".

gcc/ChangeLog:

PR target/110136
* config/loongarch/loongarch.md: Modify the register constraints for template
"jumptable" and "indirect_jump" from "r" to "e".

Co-authored-by: Andrew Pinski <apinski@marvell.com>
(cherry picked from commit 5430c86e71927492399129f3df80824c6c334ddf)

gcc/config/loongarch/loongarch.md

index 8f8412fba84ee15ce7619f85cee443e4f1a6d1ea..a64142c7a780b1c2b0047f594dc72128ede10d3c 100644 (file)
 }
   [(set_attr "type" "branch")])
 
+;; Micro-architecture unconditionally treats a "jr $ra" as "return from subroutine",
+;; non-returning indirect jumps through $ra would interfere with both subroutine
+;; return prediction and the more general indirect branch prediction.
+
 (define_expand "indirect_jump"
   [(set (pc) (match_operand 0 "register_operand"))]
   ""
 })
 
 (define_insn "@indirect_jump<mode>"
-  [(set (pc) (match_operand:P 0 "register_operand" "r"))]
+  [(set (pc) (match_operand:P 0 "register_operand" "e"))]
   ""
   "jr\t%0"
   [(set_attr "type" "jump")
 
 (define_insn "@tablejump<mode>"
   [(set (pc)
-       (match_operand:P 0 "register_operand" "r"))
+       (match_operand:P 0 "register_operand" "e"))
    (use (label_ref (match_operand 1 "" "")))]
   ""
   "jr\t%0"