Skip to content

Conversation

@yingopq
Copy link
Contributor

@yingopq yingopq commented Oct 29, 2025

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.

Fix #142060.

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.
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2025

@llvm/pr-subscribers-backend-mips

Author: None (yingopq)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/165531.diff

2 Files Affected:

  • (modified) llvm/lib/Target/Mips/MipsCondMov.td (+3)
  • (added) llvm/test/CodeGen/Mips/llvm-ir/select-globaladdr.ll (+22)
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"
+
+@.str = external constant [6 x i8]
+@.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
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Mips] Cannot select: t28: i64 = MipsISD::GPRel TargetGlobalAddress:i64<ptr @.str.5> 0 [TF=3] when building musl libc.a with -mno-abicalls

2 participants