111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// Copyright (c) 2016 Google Inc.
211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// Permission is hereby granted, free of charge, to any person obtaining a
411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// copy of this software and/or associated documentation files (the
511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// "Materials"), to deal in the Materials without restriction, including
611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// without limitation the rights to use, copy, modify, merge, publish,
711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// distribute, sublicense, and/or sell copies of the Materials, and to
811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// permit persons to whom the Materials are furnished to do so, subject to
911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// the following conditions:
1011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
1111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// The above copyright notice and this permission notice shall be included
1211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// in all copies or substantial portions of the Materials.
1311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
1411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
1511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
1611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
1711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//    https://www.khronos.org/registry/
1811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert//
1911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
2211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
2311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
2611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
2711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#ifndef LIBSPIRV_OPT_CONSTRUCTS_H_
2811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#define LIBSPIRV_OPT_CONSTRUCTS_H_
2911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
3011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include <functional>
3111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include <vector>
3211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include <utility>
3311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
3411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include "basic_block.h"
3511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#include "instruction.h"
3611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
3711cd02dfb91661c65134cac258cf5924270e9d2Dan Albertnamespace spvtools {
3811cd02dfb91661c65134cac258cf5924270e9d2Dan Albertnamespace ir {
3911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
4011cd02dfb91661c65134cac258cf5924270e9d2Dan Albertclass Module;
4111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
4211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert// A SPIR-V function.
4311cd02dfb91661c65134cac258cf5924270e9d2Dan Albertclass Function {
4411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert public:
4511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  Function(Instruction&& def_inst)
4611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert      : module_(nullptr),
4711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        def_inst_(std::move(def_inst)),
4811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        end_inst_(SpvOpFunctionEnd) {}
4911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
5011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  // Sets the enclosing module for this function.
5111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  void SetParent(Module* module) { module_ = module; }
5211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  // Appends a parameter to this function.
5311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  void AddParameter(Instruction&& p) { params_.push_back(std::move(p)); }
5411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  // Appends a basic block to this function.
5511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  void AddBasicBlock(BasicBlock&& b) { blocks_.push_back(std::move(b)); }
5611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
5711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  const std::vector<BasicBlock>& basic_blocks() const { return blocks_; }
5811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  std::vector<BasicBlock>& basic_blocks() { return blocks_; }
5911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
6011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  // Runs the given function |f| on each instruction in this basic block.
6111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  void ForEachInst(const std::function<void(Instruction*)>& f);
6211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
6311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  // Pushes the binary segments for this instruction into the back of *|binary|.
6411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  // If |skip_nop| is true and this is a OpNop, do nothing.
6511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  void ToBinary(std::vector<uint32_t>* binary, bool skip_nop) const;
6611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
6711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert private:
6811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  Module* module_;        // The enclosing module.
6911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  Instruction def_inst_;  // The instruction definining this function.
7011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  std::vector<Instruction> params_;  // All parameters to this function.
7111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  std::vector<BasicBlock> blocks_;   // All basic blocks inside this function.
7211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert  Instruction end_inst_;             // The OpFunctionEnd instruction.
7311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert};
7411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
7511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert}  // namespace ir
7611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert}  // namespace spvtools
7711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
7811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert#endif  // LIBSPIRV_OPT_CONSTRUCTS_H_
79