; Pin: ISD::BRIND (computed-goto / IR `indirectbr`) lowers via the
; __indirTarget DP $00B8 slot.  Without LowerBRIND, llc aborts with
; "Cannot select: brind ... t<N>: i32 = REG_SEQUENCE ..." because the
; default legalizer has no pattern for `brind` of an i32 (Wide32)
; pointer under p:32:16.
;
; LowerBRIND extracts sub_lo of the i32 target, stores it to $00B8
; (the shared __indirTarget slot — see libgcc.s), and emits a
; BRINDpseudo that AsmPrinter expands to `jmp ($00B8)`.  The store
; lands at $00:00B8 regardless of DBR because the pattern routes to
; either `sta $b8` (DBR-relative, bank-0-pinned at runtime via DP=0)
; or the [E0],Y indirect-long form (bank-explicit) — both reach the
; same byte.
;
; RUN: llc -mtriple=w65816 -O2 < %s | FileCheck %s

target datalayout = "e-m:e-p:32:16-i16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S8"

; Minimal computed-goto repro: `goto *labels[sel]` over 3 block
; addresses.  Prior to LowerBRIND this crashed during isel.
define i16 @test_cgoto(i16 %sel) {
; CHECK-LABEL: test_cgoto:
; CHECK:       jmp	(0xb8)
entry:
  %cmp = icmp ult i16 %sel, 3
  br i1 %cmp, label %dispatch, label %fail

dispatch:
  %idx = zext i16 %sel to i32
  %addr = getelementptr inbounds [3 x ptr], ptr @labels, i32 0, i32 %idx
  %tgt = load ptr, ptr %addr
  indirectbr ptr %tgt, [label %L0, label %L1, label %L2]

L0:
  ret i16 10
L1:
  ret i16 20
L2:
  ret i16 30
fail:
  ret i16 0
}

@labels = internal constant [3 x ptr] [
  ptr blockaddress(@test_cgoto, %L0),
  ptr blockaddress(@test_cgoto, %L1),
  ptr blockaddress(@test_cgoto, %L2)
]
