Nearly CC
An educational compiler skeleton
live_mregs.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_MREGS_H
22 #define LIVE_MREGS_H
23 
24 #include <string>
25 #include "instruction.h"
26 #include "lowlevel_defuse.h"
27 #include "lowlevel_formatter.h"
28 #include "dataflow.h"
29 
33 
37 public:
38  // There are only 16 mregs
39  static const unsigned MAX_MREGS = 16;
40 
42  typedef std::bitset<MAX_MREGS> FactType;
43 
46  FactType get_top_fact() const { return FactType(); }
47 
49  FactType combine_facts(const FactType &left, const FactType &right) const {
50  return left | right;
51  }
52 
58  void model_instruction(Instruction *ins, FactType &fact) const {
59  // Model an instruction (backwards). If the instruction is a def,
60  // the assigned-to mreg is killed. Every mreg used in the instruction,
61  // the mreg becomes alive (or is kept alive.)
62 
63  if (LowLevel::is_def(ins)) {
64  std::vector<MachineReg> defs = LowLevel::get_def_mregs(ins);
65  for (auto i = defs.begin(); i != defs.end(); ++i)
66  fact.reset(unsigned(*i));
67  }
68 
69  std::vector<MachineReg> uses = LowLevel::get_use_mregs(ins);
70  for (auto i = uses.begin(); i != uses.end(); ++i)
71  fact.set(unsigned(*i));
72  }
73 
78  std::string fact_to_string(const FactType &fact) const {
79  LowLevelFormatter ll_formatter;
80 
81  std::string s("{");
82  for (unsigned i = 0; i < MAX_MREGS; i++) {
83  if (fact.test(i)) {
84  if (s != "{") { s += ","; }
85  // Machine registers are shown using their 64-bit name
86  Operand mreg_operand(Operand::MREG64, i);
87  s += ll_formatter.format_operand(mreg_operand);
88  }
89  }
90  s += "}";
91  return s;
92  }
93 };
94 
98 
99 #endif // LIVE_MREGS_H
Base class for backward analyses.
Definition: dataflow.h:139
Instruction object type.
Definition: instruction.h:31
Dataflow analysis to determine which machine registers contain live values.
Definition: live_mregs.h:36
std::bitset< MAX_MREGS > FactType
Fact type is bitset of machine register numbers.
Definition: live_mregs.h:42
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_mregs.h:78
FactType get_top_fact() const
The "top" fact is an unknown value that combines nondestructively with known facts.
Definition: live_mregs.h:46
void model_instruction(Instruction *ins, FactType &fact) const
Model an instruction.
Definition: live_mregs.h:58
FactType combine_facts(const FactType &left, const FactType &right) const
Combine live sets. For this analysis, we use union.
Definition: live_mregs.h:49
Implementation of Formatter for low-level code.
Definition: lowlevel_formatter.h:27
virtual std::string format_operand(const Operand &operand) const
Convert a low-level Operand to a formatted string.
Definition: lowlevel_formatter.cpp:71
Definition: operand.h:31
Support for global (procedure-scope) dataflow analysis.
Dataflow< LiveMregsAnalysis > LiveMregs
Convenient typedef for the type of a dataflow object for executing LiveMregsAnalysis on a ControlFlow...
Definition: live_mregs.h:97