11591693c7b415e9869157c711fe11263c95d74eDavid Li/*
21591693c7b415e9869157c711fe11263c95d74eDavid Li * Copyright © 2010 Intel Corporation
31591693c7b415e9869157c711fe11263c95d74eDavid Li *
41591693c7b415e9869157c711fe11263c95d74eDavid Li * Permission is hereby granted, free of charge, to any person obtaining a
51591693c7b415e9869157c711fe11263c95d74eDavid Li * copy of this software and associated documentation files (the "Software"),
61591693c7b415e9869157c711fe11263c95d74eDavid Li * to deal in the Software without restriction, including without limitation
71591693c7b415e9869157c711fe11263c95d74eDavid Li * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81591693c7b415e9869157c711fe11263c95d74eDavid Li * and/or sell copies of the Software, and to permit persons to whom the
91591693c7b415e9869157c711fe11263c95d74eDavid Li * Software is furnished to do so, subject to the following conditions:
101591693c7b415e9869157c711fe11263c95d74eDavid Li *
111591693c7b415e9869157c711fe11263c95d74eDavid Li * The above copyright notice and this permission notice (including the next
121591693c7b415e9869157c711fe11263c95d74eDavid Li * paragraph) shall be included in all copies or substantial portions of the
131591693c7b415e9869157c711fe11263c95d74eDavid Li * Software.
141591693c7b415e9869157c711fe11263c95d74eDavid Li *
151591693c7b415e9869157c711fe11263c95d74eDavid Li * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161591693c7b415e9869157c711fe11263c95d74eDavid Li * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171591693c7b415e9869157c711fe11263c95d74eDavid Li * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181591693c7b415e9869157c711fe11263c95d74eDavid Li * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191591693c7b415e9869157c711fe11263c95d74eDavid Li * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201591693c7b415e9869157c711fe11263c95d74eDavid Li * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211591693c7b415e9869157c711fe11263c95d74eDavid Li * DEALINGS IN THE SOFTWARE.
221591693c7b415e9869157c711fe11263c95d74eDavid Li */
231591693c7b415e9869157c711fe11263c95d74eDavid Li
241591693c7b415e9869157c711fe11263c95d74eDavid Li/**
251591693c7b415e9869157c711fe11263c95d74eDavid Li * \file lower_noise.cpp
261591693c7b415e9869157c711fe11263c95d74eDavid Li * IR lower pass to remove noise opcodes.
271591693c7b415e9869157c711fe11263c95d74eDavid Li *
281591693c7b415e9869157c711fe11263c95d74eDavid Li * \author Ian Romanick <ian.d.romanick@intel.com>
291591693c7b415e9869157c711fe11263c95d74eDavid Li */
301591693c7b415e9869157c711fe11263c95d74eDavid Li
311591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ir.h"
321591693c7b415e9869157c711fe11263c95d74eDavid Li#include "ir_rvalue_visitor.h"
331591693c7b415e9869157c711fe11263c95d74eDavid Li
341591693c7b415e9869157c711fe11263c95d74eDavid Liclass lower_noise_visitor : public ir_rvalue_visitor {
351591693c7b415e9869157c711fe11263c95d74eDavid Lipublic:
361591693c7b415e9869157c711fe11263c95d74eDavid Li   lower_noise_visitor() : progress(false)
371591693c7b415e9869157c711fe11263c95d74eDavid Li   {
381591693c7b415e9869157c711fe11263c95d74eDavid Li      /* empty */
391591693c7b415e9869157c711fe11263c95d74eDavid Li   }
401591693c7b415e9869157c711fe11263c95d74eDavid Li
411591693c7b415e9869157c711fe11263c95d74eDavid Li   void handle_rvalue(ir_rvalue **rvalue)
421591693c7b415e9869157c711fe11263c95d74eDavid Li   {
431591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!*rvalue)
441591693c7b415e9869157c711fe11263c95d74eDavid Li	 return;
451591693c7b415e9869157c711fe11263c95d74eDavid Li
461591693c7b415e9869157c711fe11263c95d74eDavid Li      ir_expression *expr = (*rvalue)->as_expression();
471591693c7b415e9869157c711fe11263c95d74eDavid Li      if (!expr)
481591693c7b415e9869157c711fe11263c95d74eDavid Li	 return;
491591693c7b415e9869157c711fe11263c95d74eDavid Li
501591693c7b415e9869157c711fe11263c95d74eDavid Li      /* In the future, ir_unop_noise may be replaced by a call to a function
511591693c7b415e9869157c711fe11263c95d74eDavid Li       * that implements noise.  No hardware has a noise instruction.
521591693c7b415e9869157c711fe11263c95d74eDavid Li       */
531591693c7b415e9869157c711fe11263c95d74eDavid Li      if (expr->operation == ir_unop_noise) {
54d50d9a90a0df4d706421850e17c0fbd85bf710eeDavid Li	 *rvalue = ir_constant::zero(hieralloc_parent(expr), expr->type);
551591693c7b415e9869157c711fe11263c95d74eDavid Li	 this->progress = true;
561591693c7b415e9869157c711fe11263c95d74eDavid Li      }
571591693c7b415e9869157c711fe11263c95d74eDavid Li   }
581591693c7b415e9869157c711fe11263c95d74eDavid Li
591591693c7b415e9869157c711fe11263c95d74eDavid Li   bool progress;
601591693c7b415e9869157c711fe11263c95d74eDavid Li};
611591693c7b415e9869157c711fe11263c95d74eDavid Li
621591693c7b415e9869157c711fe11263c95d74eDavid Li
631591693c7b415e9869157c711fe11263c95d74eDavid Libool
641591693c7b415e9869157c711fe11263c95d74eDavid Lilower_noise(exec_list *instructions)
651591693c7b415e9869157c711fe11263c95d74eDavid Li{
661591693c7b415e9869157c711fe11263c95d74eDavid Li   lower_noise_visitor v;
671591693c7b415e9869157c711fe11263c95d74eDavid Li
681591693c7b415e9869157c711fe11263c95d74eDavid Li   visit_list_elements(&v, instructions);
691591693c7b415e9869157c711fe11263c95d74eDavid Li
701591693c7b415e9869157c711fe11263c95d74eDavid Li   return v.progress;
711591693c7b415e9869157c711fe11263c95d74eDavid Li}
72