101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg *
2301e04c3fSmrg * Authors:
2401e04c3fSmrg *    Jason Ekstrand (jason@jlekstrand.net)
2501e04c3fSmrg */
2601e04c3fSmrg
2701e04c3fSmrg#include "nir.h"
2801e04c3fSmrg
2901e04c3fSmrg/*
3001e04c3fSmrg * Handles management of the metadata.
3101e04c3fSmrg */
3201e04c3fSmrg
3301e04c3fSmrgvoid
3401e04c3fSmrgnir_metadata_require(nir_function_impl *impl, nir_metadata required, ...)
3501e04c3fSmrg{
3601e04c3fSmrg#define NEEDS_UPDATE(X) ((required & ~impl->valid_metadata) & (X))
3701e04c3fSmrg
3801e04c3fSmrg   if (NEEDS_UPDATE(nir_metadata_block_index))
3901e04c3fSmrg      nir_index_blocks(impl);
407ec681f3Smrg   if (NEEDS_UPDATE(nir_metadata_instr_index))
417ec681f3Smrg      nir_index_instrs(impl);
4201e04c3fSmrg   if (NEEDS_UPDATE(nir_metadata_dominance))
4301e04c3fSmrg      nir_calc_dominance_impl(impl);
4401e04c3fSmrg   if (NEEDS_UPDATE(nir_metadata_live_ssa_defs))
4501e04c3fSmrg      nir_live_ssa_defs_impl(impl);
4601e04c3fSmrg   if (NEEDS_UPDATE(nir_metadata_loop_analysis)) {
4701e04c3fSmrg      va_list ap;
4801e04c3fSmrg      va_start(ap, required);
4901e04c3fSmrg      nir_loop_analyze_impl(impl, va_arg(ap, nir_variable_mode));
5001e04c3fSmrg      va_end(ap);
5101e04c3fSmrg   }
5201e04c3fSmrg
5301e04c3fSmrg#undef NEEDS_UPDATE
5401e04c3fSmrg
5501e04c3fSmrg   impl->valid_metadata |= required;
5601e04c3fSmrg}
5701e04c3fSmrg
5801e04c3fSmrgvoid
5901e04c3fSmrgnir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved)
6001e04c3fSmrg{
6101e04c3fSmrg   impl->valid_metadata &= preserved;
6201e04c3fSmrg}
6301e04c3fSmrg
647ec681f3Smrgvoid
657ec681f3Smrgnir_shader_preserve_all_metadata(nir_shader *shader)
667ec681f3Smrg{
677ec681f3Smrg   nir_foreach_function(function, shader) {
687ec681f3Smrg      if (function->impl)
697ec681f3Smrg         nir_metadata_preserve(function->impl, nir_metadata_all);
707ec681f3Smrg   }
717ec681f3Smrg}
727ec681f3Smrg
7301e04c3fSmrg#ifndef NDEBUG
7401e04c3fSmrg/**
7501e04c3fSmrg * Make sure passes properly invalidate metadata (part 1).
7601e04c3fSmrg *
7701e04c3fSmrg * Call this before running a pass to set a bogus metadata flag, which will
7801e04c3fSmrg * only be preserved if the pass forgets to call nir_metadata_preserve().
7901e04c3fSmrg */
8001e04c3fSmrgvoid
8101e04c3fSmrgnir_metadata_set_validation_flag(nir_shader *shader)
8201e04c3fSmrg{
8301e04c3fSmrg   nir_foreach_function(function, shader) {
8401e04c3fSmrg      if (function->impl) {
8501e04c3fSmrg         function->impl->valid_metadata |= nir_metadata_not_properly_reset;
8601e04c3fSmrg      }
8701e04c3fSmrg   }
8801e04c3fSmrg}
8901e04c3fSmrg
9001e04c3fSmrg/**
9101e04c3fSmrg * Make sure passes properly invalidate metadata (part 2).
9201e04c3fSmrg *
9301e04c3fSmrg * Call this after a pass makes progress to verify that the bogus metadata set by
9401e04c3fSmrg * the earlier function was properly thrown away.  Note that passes may not call
9501e04c3fSmrg * nir_metadata_preserve() if they don't actually make any changes at all.
9601e04c3fSmrg */
9701e04c3fSmrgvoid
9801e04c3fSmrgnir_metadata_check_validation_flag(nir_shader *shader)
9901e04c3fSmrg{
10001e04c3fSmrg   nir_foreach_function(function, shader) {
10101e04c3fSmrg      if (function->impl) {
10201e04c3fSmrg         assert(!(function->impl->valid_metadata &
10301e04c3fSmrg                  nir_metadata_not_properly_reset));
10401e04c3fSmrg      }
10501e04c3fSmrg   }
10601e04c3fSmrg}
10701e04c3fSmrg#endif
108