From 9b1f31a61524c0c12119eedf87923652cf72e9c7 Mon Sep 17 00:00:00 2001 From: Friedrich Vock Date: Fri, 20 Dec 2024 18:00:32 +0100 Subject: [PATCH] nir: Add nir_instr_is_before helper is_instr_between works similarly but requires special-casing w.r.t the first instruction in a block for the same effect. Reviewed-by: Alyssa Rosenzweig Part-of: --- src/compiler/nir/nir.c | 18 ++++++++++++++++++ src/compiler/nir/nir.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/compiler/nir/nir.c b/src/compiler/nir/nir.c index be23a142ddb..8f9ec619779 100644 --- a/src/compiler/nir/nir.c +++ b/src/compiler/nir/nir.c @@ -1602,6 +1602,24 @@ nir_def_rewrite_uses_src(nir_def *def, nir_src new_src) nir_def_rewrite_uses(def, new_src.ssa); } +bool +nir_instr_is_before(nir_instr *first, nir_instr *second) +{ + if (first->block != second->block) + return false; + + /* Search backwards looking for "first" */ + while (second != nir_block_first_instr(second->block)) { + second = nir_instr_prev(second); + assert(second); + + if (first == second) + return true; + } + + return false; +} + static bool is_instr_between(nir_instr *start, nir_instr *end, nir_instr *between) { diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index e258faa42f4..d1eb34cdc68 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -5098,6 +5098,9 @@ void nir_instr_clear_src(nir_instr *instr, nir_src *src); void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src); +/** Returns true if first comes before second in a block. */ +bool nir_instr_is_before(nir_instr *first, nir_instr *second); + void nir_def_init(nir_instr *instr, nir_def *def, unsigned num_components, unsigned bit_size); static inline void