File: value_number_table.h

package info (click to toggle)
spirv-tools 2023.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,608 kB
  • sloc: cpp: 408,882; javascript: 5,890; python: 2,847; ansic: 403; sh: 385; ruby: 88; makefile: 17; lisp: 9
file content (91 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download | duplicates (40)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SOURCE_OPT_VALUE_NUMBER_TABLE_H_
#define SOURCE_OPT_VALUE_NUMBER_TABLE_H_

#include <cstdint>
#include <unordered_map>

#include "source/opt/instruction.h"

namespace spvtools {
namespace opt {

class IRContext;

// Returns true if the two instructions compute the same value.  Used by the
// value number table to compare two instructions.
class ComputeSameValue {
 public:
  bool operator()(const Instruction& lhs, const Instruction& rhs) const;
};

// The hash function used in the value number table.
class ValueTableHash {
 public:
  std::size_t operator()(const Instruction& inst) const;
};

// This class implements the value number analysis.  It is using a hash-based
// approach to value numbering.  It is essentially doing dominator-tree value
// numbering described in
//
//   Preston Briggs, Keith D. Cooper, and L. Taylor Simpson. 1997. Value
//   numbering. Softw. Pract. Exper. 27, 6 (June 1997), 701-724.
//   https://www.cs.rice.edu/~keith/Promo/CRPC-TR94517.pdf.gz
//
// The main difference is that because we do not perform redundancy elimination
// as we build the value number table, we do not have to deal with cleaning up
// the scope.
class ValueNumberTable {
 public:
  ValueNumberTable(IRContext* ctx) : context_(ctx), next_value_number_(1) {
    BuildDominatorTreeValueNumberTable();
  }

  // Returns the value number of the value computed by |inst|.  |inst| must have
  // a result id that will hold the computed value.  If no value number has been
  // assigned to the result id, then the return value is 0.
  uint32_t GetValueNumber(Instruction* inst) const;

  // Returns the value number of the value contain in |id|.  Returns 0 if it
  // has not been assigned a value number.
  uint32_t GetValueNumber(uint32_t id) const;

  IRContext* context() const { return context_; }

 private:
  // Assigns a value number to every result id in the module.
  void BuildDominatorTreeValueNumberTable();

  // Returns the new value number.
  uint32_t TakeNextValueNumber() { return next_value_number_++; }

  // Assigns a new value number to the result of |inst| if it does not already
  // have one.  Return the value number for |inst|.  |inst| must have a result
  // id.
  uint32_t AssignValueNumber(Instruction* inst);

  std::unordered_map<Instruction, uint32_t, ValueTableHash, ComputeSameValue>
      instruction_to_value_;
  std::unordered_map<uint32_t, uint32_t> id_to_value_;
  IRContext* context_;
  uint32_t next_value_number_;
};

}  // namespace opt
}  // namespace spvtools

#endif  // SOURCE_OPT_VALUE_NUMBER_TABLE_H_