[llvm] [SelectionDAG] Preserve COPY chain for callbr outputs (PR #219792)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 03:52:38 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-selectiondag

Author: Oscar Priego (Opriego)

<details>
<summary>Changes</summary>

`CopyValueToVirtualRegister()` may replace `ANY_EXTEND` with a preferred
extension for exported values. For `callbr` results, this can eliminate the
COPY chain that `visitCallBrLandingPad()` later follows to recover the
`INLINEASM_BR` output.

Keep `ANY_EXTEND` for `CallBrInst` exports so the COPY chain remains
available for provenance recovery. Other exported values continue to use
`PreferredExtendType` as before.

The required ABI extension is still emitted at the consumer.

Add RISC-V regression coverage for i8, i16, and i32 sign-extended callbr
outputs used from an indirect destination.

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


2 Files Affected:

- (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (+3-1) 
- (added) llvm/test/CodeGen/RISCV/callbr-asm-outputs-indirect-isel.ll (+72) 


``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 5a930ef7981c7..22717e874f7b4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -11757,7 +11757,9 @@ void SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V,
                    std::nullopt); // This is not an ABI copy.
   SDValue Chain = DAG.getEntryNode();
 
-  if (ExtendType == ISD::ANY_EXTEND) {
+  // Callbr exports must preserve the COPY chain needed by
+  // visitCallBrLandingPad() to recover the INLINEASM_BR output.
+  if (ExtendType == ISD::ANY_EXTEND && !isa<CallBrInst>(V)) {
     auto PreferredExtendIt = FuncInfo.PreferredExtendType.find(V);
     if (PreferredExtendIt != FuncInfo.PreferredExtendType.end())
       ExtendType = PreferredExtendIt->second;
diff --git a/llvm/test/CodeGen/RISCV/callbr-asm-outputs-indirect-isel.ll b/llvm/test/CodeGen/RISCV/callbr-asm-outputs-indirect-isel.ll
new file mode 100644
index 0000000000000..81498e3c03a8e
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/callbr-asm-outputs-indirect-isel.ll
@@ -0,0 +1,72 @@
+; RUN: llc -mtriple=riscv64-unknown-linux-gnu -stop-after=finalize-isel \
+; RUN:   -o - %s | FileCheck %s
+
+declare void @sink_i8(i8 signext)
+declare void @sink_i16(i16 signext)
+declare void @sink_i32(i32 signext)
+
+define i8 @callbr_i8_signext(i1 %cond) {
+; CHECK-LABEL: name: callbr_i8_signext
+; CHECK:       INLINEASM_BR
+; CHECK-NEXT:  {{.*}}:gpr = COPY
+entry:
+  %result = callbr i8 asm "", "=&r,!i"()
+            to label %normal [label %indirect]
+
+normal:
+  ret i8 0
+
+indirect:
+  br i1 %cond, label %use, label %exit
+
+use:
+  call void @sink_i8(i8 signext %result)
+  br label %exit
+
+exit:
+  ret i8 0
+}
+
+define i16 @callbr_i16_signext(i1 %cond) {
+; CHECK-LABEL: name: callbr_i16_signext
+; CHECK:       INLINEASM_BR
+; CHECK-NEXT:  {{.*}}:gpr = COPY
+entry:
+  %result = callbr i16 asm "", "=&r,!i"()
+            to label %normal [label %indirect]
+
+normal:
+  ret i16 0
+
+indirect:
+  br i1 %cond, label %use, label %exit
+
+use:
+  call void @sink_i16(i16 signext %result)
+  br label %exit
+
+exit:
+  ret i16 0
+}
+
+define i32 @callbr_i32_signext(i1 %cond) {
+; CHECK-LABEL: name: callbr_i32_signext
+; CHECK:       INLINEASM_BR
+; CHECK-NEXT:  {{.*}}:gpr = COPY
+entry:
+  %result = callbr i32 asm "", "=&r,!i"()
+            to label %normal [label %indirect]
+
+normal:
+  ret i32 0
+
+indirect:
+  br i1 %cond, label %use, label %exit
+
+use:
+  call void @sink_i32(i32 signext %result)
+  br label %exit
+
+exit:
+  ret i32 0
+}

``````````

</details>


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


More information about the llvm-commits mailing list