Nearly CC
An educational compiler skeleton
live_vregs.h
Go to the documentation of this file.
1 // Copyright (c) 2021-2023, David H. Hovemeyer <david.hovemeyer@gmail.com>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 // OTHER DEALINGS IN THE SOFTWARE.
20 
21 #ifndef LIVE_VREGS_H
22 #define LIVE_VREGS_H
23 
24 #include <string>
25 #include "instruction.h"
26 #include "highlevel_defuse.h"
27 #include "dataflow.h"
28 
32 
36 public:
37  // We assume that there are never more than this many vregs used
38  static const unsigned MAX_VREGS = 256;
39 
41  typedef std::bitset<MAX_VREGS> FactType;
42 
45  FactType get_top_fact() const { return FactType(); }
46 
48  FactType combine_facts(const FactType &left, const FactType &right) const {
49  return left | right;
50  }
51 
57  void model_instruction(Instruction *ins, FactType &fact) const {
58  // Model an instruction (backwards). If the instruction is a def,
59  // the assigned-to vreg is killed. Every vreg used in the instruction,
60  // the vreg becomes alive (or is kept alive.)
61 
62  if (HighLevel::is_def(ins)) {
63  Operand operand = ins->get_operand(0);
64  assert(operand.has_base_reg());
65  fact.reset(operand.get_base_reg());
66  }
67 
68  for (unsigned i = 0; i < ins->get_num_operands(); i++) {
69  if (HighLevel::is_use(ins, i)) {
70  Operand operand = ins->get_operand(i);
71 
72  assert(operand.has_base_reg());
73  fact.set(operand.get_base_reg());
74 
75  if (operand.has_index_reg()) {
76  fact.set(operand.get_index_reg());
77  }
78  }
79  }
80  }
81 
86  std::string fact_to_string(const FactType &fact) const {
87  std::string s("{");
88  for (unsigned i = 0; i < MAX_VREGS; i++) {
89  if (fact.test(i)) {
90  if (s != "{") { s += ","; }
91  s += std::to_string(i);
92  }
93  }
94  s += "}";
95  return s;
96  }
97 };
98 
102 
103 #endif // LIVE_VREGS_H
Base class for backward analyses.
Definition: dataflow.h:139
An instance of Dataflow performs a dataflow analysis on the basic blocks of a control flow graph and ...
Definition: dataflow.h:233
Instruction object type.
Definition: instruction.h:31
const Operand & get_operand(unsigned index) const
Get the specified Operand.
Definition: instruction.cpp:54
unsigned get_num_operands() const
Get the number of operands.
Definition: instruction.cpp:50
Dataflow analysis to determine which virtual registers (in high-level code) contain live values.
Definition: live_vregs.h:35
void model_instruction(Instruction *ins, FactType &fact) const
Model an instruction.
Definition: live_vregs.h:57
std::string fact_to_string(const FactType &fact) const
Convert a dataflow fact to a string (for printing the CFG annotated with dataflow facts)
Definition: live_vregs.h:86
std::bitset< MAX_VREGS > FactType
Fact type is a bitset of live virtual register numbers.
Definition: live_vregs.h:41
FactType combine_facts(const FactType &left, const FactType &right) const
Combine live sets. For this analysis, we use union.
Definition: live_vregs.h:48
FactType get_top_fact() const
The "top" fact is an unknown value that combines nondestructively with known facts.
Definition: live_vregs.h:45
Definition: operand.h:31
Support for global (procedure-scope) dataflow analysis.
Dataflow< LiveVregsAnalysis > LiveVregs
Convenient typedef for the type of a dataflow object for executing LiveVregsAnalysis on a ControlFlow...
Definition: live_vregs.h:101