101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2013 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
2101e04c3fSmrg * DEALINGS IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
2401e04c3fSmrg#include "ir.h"
2501e04c3fSmrg
2601e04c3fSmrg/**
2701e04c3fSmrg * Helper for checking equality when one instruction might be NULL, since you
2801e04c3fSmrg * can't access a's vtable in that case.
2901e04c3fSmrg */
3001e04c3fSmrgstatic bool
3101e04c3fSmrgpossibly_null_equals(const ir_instruction *a, const ir_instruction *b,
3201e04c3fSmrg                     enum ir_node_type ignore)
3301e04c3fSmrg{
3401e04c3fSmrg   if (!a || !b)
3501e04c3fSmrg      return !a && !b;
3601e04c3fSmrg
3701e04c3fSmrg   return a->equals(b, ignore);
3801e04c3fSmrg}
3901e04c3fSmrg
4001e04c3fSmrg/**
4101e04c3fSmrg * The base equality function: Return not equal for anything we don't know
4201e04c3fSmrg * about.
4301e04c3fSmrg */
4401e04c3fSmrgbool
4501e04c3fSmrgir_instruction::equals(const ir_instruction *, enum ir_node_type) const
4601e04c3fSmrg{
4701e04c3fSmrg   return false;
4801e04c3fSmrg}
4901e04c3fSmrg
5001e04c3fSmrgbool
5101e04c3fSmrgir_constant::equals(const ir_instruction *ir, enum ir_node_type) const
5201e04c3fSmrg{
5301e04c3fSmrg   const ir_constant *other = ir->as_constant();
5401e04c3fSmrg   if (!other)
5501e04c3fSmrg      return false;
5601e04c3fSmrg
5701e04c3fSmrg   if (type != other->type)
5801e04c3fSmrg      return false;
5901e04c3fSmrg
6001e04c3fSmrg   for (unsigned i = 0; i < type->components(); i++) {
6101e04c3fSmrg      if (type->is_double()) {
6201e04c3fSmrg         if (value.d[i] != other->value.d[i])
6301e04c3fSmrg            return false;
6401e04c3fSmrg      } else {
6501e04c3fSmrg         if (value.u[i] != other->value.u[i])
6601e04c3fSmrg            return false;
6701e04c3fSmrg      }
6801e04c3fSmrg   }
6901e04c3fSmrg
7001e04c3fSmrg   return true;
7101e04c3fSmrg}
7201e04c3fSmrg
7301e04c3fSmrgbool
7401e04c3fSmrgir_dereference_variable::equals(const ir_instruction *ir,
7501e04c3fSmrg                                enum ir_node_type) const
7601e04c3fSmrg{
7701e04c3fSmrg   const ir_dereference_variable *other = ir->as_dereference_variable();
7801e04c3fSmrg   if (!other)
7901e04c3fSmrg      return false;
8001e04c3fSmrg
8101e04c3fSmrg   return var == other->var;
8201e04c3fSmrg}
8301e04c3fSmrg
8401e04c3fSmrgbool
8501e04c3fSmrgir_dereference_array::equals(const ir_instruction *ir,
8601e04c3fSmrg                             enum ir_node_type ignore) const
8701e04c3fSmrg{
8801e04c3fSmrg   const ir_dereference_array *other = ir->as_dereference_array();
8901e04c3fSmrg   if (!other)
9001e04c3fSmrg      return false;
9101e04c3fSmrg
9201e04c3fSmrg   if (type != other->type)
9301e04c3fSmrg      return false;
9401e04c3fSmrg
9501e04c3fSmrg   if (!array->equals(other->array, ignore))
9601e04c3fSmrg      return false;
9701e04c3fSmrg
9801e04c3fSmrg   if (!array_index->equals(other->array_index, ignore))
9901e04c3fSmrg      return false;
10001e04c3fSmrg
10101e04c3fSmrg   return true;
10201e04c3fSmrg}
10301e04c3fSmrg
10401e04c3fSmrgbool
10501e04c3fSmrgir_swizzle::equals(const ir_instruction *ir,
10601e04c3fSmrg                   enum ir_node_type ignore) const
10701e04c3fSmrg{
10801e04c3fSmrg   const ir_swizzle *other = ir->as_swizzle();
10901e04c3fSmrg   if (!other)
11001e04c3fSmrg      return false;
11101e04c3fSmrg
11201e04c3fSmrg   if (type != other->type)
11301e04c3fSmrg      return false;
11401e04c3fSmrg
11501e04c3fSmrg   if (ignore != ir_type_swizzle) {
11601e04c3fSmrg      if (mask.x != other->mask.x ||
11701e04c3fSmrg          mask.y != other->mask.y ||
11801e04c3fSmrg          mask.z != other->mask.z ||
11901e04c3fSmrg          mask.w != other->mask.w) {
12001e04c3fSmrg         return false;
12101e04c3fSmrg      }
12201e04c3fSmrg   }
12301e04c3fSmrg
12401e04c3fSmrg   return val->equals(other->val, ignore);
12501e04c3fSmrg}
12601e04c3fSmrg
12701e04c3fSmrgbool
12801e04c3fSmrgir_texture::equals(const ir_instruction *ir, enum ir_node_type ignore) const
12901e04c3fSmrg{
13001e04c3fSmrg   const ir_texture *other = ir->as_texture();
13101e04c3fSmrg   if (!other)
13201e04c3fSmrg      return false;
13301e04c3fSmrg
13401e04c3fSmrg   if (type != other->type)
13501e04c3fSmrg      return false;
13601e04c3fSmrg
13701e04c3fSmrg   if (op != other->op)
13801e04c3fSmrg      return false;
13901e04c3fSmrg
14001e04c3fSmrg   if (!possibly_null_equals(coordinate, other->coordinate, ignore))
14101e04c3fSmrg      return false;
14201e04c3fSmrg
14301e04c3fSmrg   if (!possibly_null_equals(projector, other->projector, ignore))
14401e04c3fSmrg      return false;
14501e04c3fSmrg
14601e04c3fSmrg   if (!possibly_null_equals(shadow_comparator, other->shadow_comparator, ignore))
14701e04c3fSmrg      return false;
14801e04c3fSmrg
14901e04c3fSmrg   if (!possibly_null_equals(offset, other->offset, ignore))
15001e04c3fSmrg      return false;
15101e04c3fSmrg
15201e04c3fSmrg   if (!sampler->equals(other->sampler, ignore))
15301e04c3fSmrg      return false;
15401e04c3fSmrg
15501e04c3fSmrg   switch (op) {
15601e04c3fSmrg   case ir_tex:
15701e04c3fSmrg   case ir_lod:
15801e04c3fSmrg   case ir_query_levels:
15901e04c3fSmrg   case ir_texture_samples:
16001e04c3fSmrg   case ir_samples_identical:
16101e04c3fSmrg      break;
16201e04c3fSmrg   case ir_txb:
16301e04c3fSmrg      if (!lod_info.bias->equals(other->lod_info.bias, ignore))
16401e04c3fSmrg         return false;
16501e04c3fSmrg      break;
16601e04c3fSmrg   case ir_txl:
16701e04c3fSmrg   case ir_txf:
16801e04c3fSmrg   case ir_txs:
16901e04c3fSmrg      if (!lod_info.lod->equals(other->lod_info.lod, ignore))
17001e04c3fSmrg         return false;
17101e04c3fSmrg      break;
17201e04c3fSmrg   case ir_txd:
17301e04c3fSmrg      if (!lod_info.grad.dPdx->equals(other->lod_info.grad.dPdx, ignore) ||
17401e04c3fSmrg          !lod_info.grad.dPdy->equals(other->lod_info.grad.dPdy, ignore))
17501e04c3fSmrg         return false;
17601e04c3fSmrg      break;
17701e04c3fSmrg   case ir_txf_ms:
17801e04c3fSmrg      if (!lod_info.sample_index->equals(other->lod_info.sample_index, ignore))
17901e04c3fSmrg         return false;
18001e04c3fSmrg      break;
18101e04c3fSmrg   case ir_tg4:
18201e04c3fSmrg      if (!lod_info.component->equals(other->lod_info.component, ignore))
18301e04c3fSmrg         return false;
18401e04c3fSmrg      break;
18501e04c3fSmrg   default:
18601e04c3fSmrg      assert(!"Unrecognized texture op");
18701e04c3fSmrg   }
18801e04c3fSmrg
18901e04c3fSmrg   return true;
19001e04c3fSmrg}
19101e04c3fSmrg
19201e04c3fSmrgbool
19301e04c3fSmrgir_expression::equals(const ir_instruction *ir, enum ir_node_type ignore) const
19401e04c3fSmrg{
19501e04c3fSmrg   const ir_expression *other = ir->as_expression();
19601e04c3fSmrg   if (!other)
19701e04c3fSmrg      return false;
19801e04c3fSmrg
19901e04c3fSmrg   if (type != other->type)
20001e04c3fSmrg      return false;
20101e04c3fSmrg
20201e04c3fSmrg   if (operation != other->operation)
20301e04c3fSmrg      return false;
20401e04c3fSmrg
20501e04c3fSmrg   for (unsigned i = 0; i < num_operands; i++) {
20601e04c3fSmrg      if (!operands[i]->equals(other->operands[i], ignore))
20701e04c3fSmrg         return false;
20801e04c3fSmrg   }
20901e04c3fSmrg
21001e04c3fSmrg   return true;
21101e04c3fSmrg}
212