CACAO
SourceStateAttachmentPass.cpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/SourceStateAttachmentPass.cpp - SourceStateAttachmentPass
2 
3  Copyright (C) 2013
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #include <algorithm>
26 
40 
41 #include "toolbox/logging.hpp"
42 
43 // define name for debugging (see logging.hpp)
44 #define DEBUG_NAME "compiler2/SourceStateAttachment"
45 
46 namespace cacao {
47 namespace jit {
48 namespace compiler2 {
49 
50 namespace {
51 
52 SourceStateInst *get_associated_source_state(Instruction *I) {
53  for (auto i = I->rdep_begin(); i != I->rdep_end(); i++) {
54  SourceStateInst *source_state = (*i)->to_SourceStateInst();
55  if (source_state != NULL) {
56  return source_state;
57  }
58  }
59  return NULL;
60 }
61 
62 } // end anonymous namespace
63 
65  SourceStateInst *latest_source_state) {
66 
67  assert(begin);
68 
69  // This is a CFG merge, therefore it has its own SourceStateInst.
70  if (begin->pred_size() > 1) {
71  latest_source_state = get_associated_source_state(begin);
72  }
73 
74  assert(latest_source_state);
75 
76  for (auto i = IS->inst_begin(begin); i != IS->inst_end(begin); i++) {
77  Instruction *I = *i;
78 
79  // Attach the SourceStateInst in case the current Instruction needs one.
82  LOG("Attach " << latest_source_state << " to " << sink->to_Instruction() << nl);
83  sink->set_source_state(latest_source_state);
84  }
85 
86  // Keep track of the latest applicable SourceStateInst.
87  if (I->has_side_effects()) {
88  latest_source_state = get_associated_source_state(I);
89  assert(latest_source_state);
90  }
91  }
92 
93  return latest_source_state;
94 }
95 
97  M = JD.get_Method();
98  IS = get_Pass<ListSchedulingPass>();
99 
100  // The blocks that have not yet been processed.
101  alloc::queue<BeginInst*>::type blocks_to_process;
102 
103  // For each block in blocks_to_process this holds the corresponding
104  // SourceStateInst that is applicable at the entry of the block.
105  alloc::queue<SourceStateInst*>::type source_state_at_block_entry;
106 
107  // The blocks that have already been processed.
109 
110  BeginInst *init_bb = M->get_init_bb();
111  SourceStateInst *init_source_state = get_associated_source_state(init_bb);
112 
113  blocks_to_process.push(init_bb);
114  source_state_at_block_entry.push(init_source_state);
115 
116  while (!blocks_to_process.empty()) {
117  BeginInst *begin = blocks_to_process.front();
118  blocks_to_process.pop();
119  SourceStateInst *latest_source_state = source_state_at_block_entry.front();
120  source_state_at_block_entry.pop();
121 
122  // Ignore blocks that have already been processed.
123  if (processed_blocks.count(begin) > 0) {
124  continue;
125  }
126 
127  latest_source_state = process_block(begin, latest_source_state);
128  processed_blocks.insert(begin);
129 
130  EndInst *end = begin->get_EndInst();
131  for (auto i = end->succ_begin(); i != end->succ_end(); i++) {
132  BeginInst *successor = (*i).get();
133  blocks_to_process.push(successor);
134  source_state_at_block_entry.push(latest_source_state);
135  }
136  }
137 
138  return true;
139 }
140 
141 // pass usage
146  return PU;
147 }
148 
149 // register pass
150 static PassRegistry<SourceStateAttachmentPass> X("SourceStateAttachmentPass");
151 
152 } // end namespace compiler2
153 } // end namespace jit
154 } // end namespace cacao
155 
156 
157 /*
158  * These are local overrides for various environment variables in Emacs.
159  * Please do not remove this and leave it at the end of the file, where
160  * Emacs will automagically detect them.
161  * ---------------------------------------------------------------------
162  * Local variables:
163  * mode: c++
164  * indent-tabs-mode: t
165  * c-basic-offset: 4
166  * tab-width: 4
167  * End:
168  * vim:noexpandtab:sw=4:ts=4:
169  */
void set_source_state(SourceStateInst *source_state)
Set the SourceStateInst that corresponds to this Instruction.
This Instruction marks the start of a basic block.
This Instruction marks the end of a basic block.
virtual bool has_side_effects() const
True the instruction has side effects.
virtual SourceStateAwareInst * to_SourceStateAwareInst()
virtual PassUsage & get_PassUsage(PassUsage &PU) const
Set the requirements for the pass.
SourceStateInst * process_block(BeginInst *begin, SourceStateInst *latest_source_state)
std::unordered_set< Key, Hash, KeyEqual, Allocator< Key > > type
void add_schedule_before()
Schedule before PassName.
Definition: PassUsage.hpp:127
const_inst_iterator inst_begin(const BeginInst *BI) const
Base type of instructions that can be mapped to a SourceStateInst.
SuccessorListTy::const_iterator succ_end() const
Provides a mapping from HIR values to baseline IR variables.
Stores the interdependencies of a pass.
Definition: PassUsage.hpp:55
Instruction super class.
Definition: Instruction.hpp:75
MIIterator i
SuccessorListTy::const_iterator succ_begin() const
#define LOG(STMT)
Analogous to DEBUG.
Definition: logging.hpp:91
#define I(value)
Definition: codegen.c:279
std::queue< T, Container > type
Definition: queue.hpp:38
virtual Instruction * to_Instruction()=0
Convert this SourceStateAwareInst to an Instruction.
const_inst_iterator inst_end(const BeginInst *BI) const
static PassRegistry< BasicBlockSchedulingPass > X("BasicBlockSchedulingPass")
Nl nl
Definition: OStream.cpp:56
BeginInst * get_init_bb() const
Definition: MethodC2.hpp:139
std::size_t pred_size() const
void add_requires()
PassName is required.
Definition: PassUsage.hpp:78