[llvm] [Mips] Add MipsPat `add $gp, (select $cond, tglobaladdr, tglobaladdr)` to parse MipsISD::GPRel TargetGlobalAddress (PR #165531)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 29 02:38:00 PDT 2025
https://github.com/yingopq created https://github.com/llvm/llvm-project/pull/165531
When SelectionDAG process visitSELECT, would fold select(cond, binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
Therefore, the original MipsPat `add GPR64:$gp, (MipsGPRel tglobaladdr:$in)` is no longer available.
>From 09cb0d61f05012026c0b5b5e9bf9606aa1f2ae7f Mon Sep 17 00:00:00 2001
From: Ying Huang <ying.huang at oss.cipunited.com>
Date: Wed, 29 Oct 2025 05:21:46 -0400
Subject: [PATCH] [Mips] Add MipsPat `add $gp, (select $cond, tglobaladdr,
tglobaladdr)` to parse MipsISD::GPRel TargetGlobalAddress
When SelectionDAG process visitSELECT, would fold select(cond,
binop(x, y), binop(x, z)) to binop(x, select(cond, y, z)).
Therefore, the original MipsPat `add GPR64:$gp, (MipsGPRel
tglobaladdr:$in)` is no longer available.
---
llvm/lib/Target/Mips/MipsCondMov.td | 3 +++
.../CodeGen/Mips/llvm-ir/select-globaladdr.ll | 22 +++++++++++++++++++
2 files changed, 25 insertions(+)
create mode 100644 llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll
diff --git a/llvm/lib/Target/Mips/MipsCondMov.td b/llvm/lib/Target/Mips/MipsCondMov.td
index e9e09a188bf5b..947a52bddebb4 100644
--- a/llvm/lib/Target/Mips/MipsCondMov.td
+++ b/llvm/lib/Target/Mips/MipsCondMov.td
@@ -196,6 +196,9 @@ let AdditionalPredicates = [NotInMicroMips] in {
}
// Instantiation of conditional move patterns.
+ def : MipsPat<(add GPR64:$gp, (select GPR32:$cond, (MipsGPRel tglobaladdr:$T), (MipsGPRel tglobaladdr:$F))),
+ (MOVN_I_I64 (i64 (DADDiu GPR64:$gp, tglobaladdr:$T)), GPR32:$cond, (i64 (DADDiu GPR64:$gp, tglobaladdr:$F)))>, ISA_MIPS3, ABI_N64;
+
defm : MovzPats0<GPR32, GPR32, MOVZ_I_I, SLT, SLTu, SLTi, SLTiu>,
INSN_MIPS4_32_NOT_32R6_64R6;
defm : MovzPats1<GPR32, GPR32, MOVZ_I_I, XOR>, INSN_MIPS4_32_NOT_32R6_64R6;
diff --git a/llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll b/llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll
new file mode 100644
index 0000000000000..54c272a832721
--- /dev/null
+++ b/llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll
@@ -0,0 +1,22 @@
+; RUN: llc < %s -mattr +noabicalls -mgpopt | FileCheck %s -check-prefixes=MIPS64
+
+target datalayout = "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "mips64-unknown-linux-muslabi64"
+
+ at .str = external constant [6 x i8]
+ at .str.1 = external constant [6 x i8]
+
+define ptr @tst_select_ptr_ptr(i1 %tobool.not) {
+entry:
+; MIPS64-LABEL: tst_select_ptr_ptr:
+; MIPS64: # %bb.0: # %entry
+; MIPS64: sll $1, $4, 0
+; MIPS64: andi $1, $1, 1
+; MIPS64: daddiu $2, $gp, %gp_rel(.str)
+; MIPS64: daddiu $3, $gp, %gp_rel(.str.1)
+; MIPS64: jr $ra
+; MIPS64: movn $2, $3, $1
+
+ %cond = select i1 %tobool.not, ptr @.str.1, ptr @.str
+ ret ptr %cond
+}
More information about the llvm-commits
mailing list