CACAO
LoopSimplificationPass.cpp
Go to the documentation of this file.
1 /* src/vm/jit/compiler2/LoopSimplificationPass.cpp - LoopSimplificationPass
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 
30 
31 #include "toolbox/logging.hpp"
32 
33 #define DEBUG_NAME "compiler2/LoopSimplificationPass"
34 
35 namespace cacao {
36 namespace jit {
37 namespace compiler2 {
38 #if 0
39 ///////////////////////////////////////////////////////////////////////////////
40 // LoopSimplificationPass
41 ///////////////////////////////////////////////////////////////////////////////
42 
43 void LoopSimplificationPass::check_loop(Loop *loop) const {
44  // of the we have a shared loop header
45  #if 0
46  assert(LT);
47  BeginInst* header = loop->get_header();
48  if (LT->get_Loop(header) != loop) {
49  LOG("Loop with shared header:" << header << nl);
50  /*
51  * We have a BeginInst which is the header of more than one loop.
52  * We create a new header block for the _outer_ loop (stored in the
53  * variable loop). All edges from outside the loop and the backedge
54  * of the current loop will be redirected to the new header block.
55  */
56  alloc::set<BeginInst*>::type outer_edges;
57 
58  for (BeginInst::PredecessorListTy::const_iterator i = header->pred_begin(),
59  e = header->pred_end(); i != e; ++i) {
60  BeginInst *pred = *i;
61  assert(pred);
62  Loop *pred_loop = LT->get_Loop(pred);
63 
64  if (!LT->is_inner_loop(pred_loop,loop)) {
65  // edge from outside -> replace target
66  // NOTE: We can not handle this edges now because that would mess up our
67  // iterator. Therefor we store them in this list.
68  outer_edges.insert(pred);
69  }
70  }
71  // create new loop header
72  BeginInst* new_header = new BeginInst();
73  // create jump to the old header
74  EndInst* new_exit = new GOTOInst(new_header,header);
75  for (alloc::set<BeginInst*>::type::iterator i = outer_edges.begin(), e = outer_edges.end() ;
76  i != e ; ++i) {
77  EndInst *end = (*i)->get_EndInst();
78  assert(end && "Can not replace a successor of a BeginInst without an EndInst!");
79  end->replace_succ(header, new_header);
80  }
81 
82  // update loop information
83  // Loop
84  loop->header = new_header;
85  // LoopTree
86  LT->loop_map[new_header] = loop;
87 
88  // XXX we have to do something with the phi instructions
89  /*
90  * The phi instruction of the original header have again tow kinds
91  * of operands. Those from the inner loop and all others.
92  */
93  for (Instruction::DepListTy::const_iterator i = header->dep_begin(),
94  e = header->dep_end(); i != e; ++i) {
95  //
96  Instruction *I = *i;
97  PHIInst *phi = I->to_PHIInst();
98  if (phi) {
99  LOG("PHI: " << phi << nl);
100  }
101  }
102 
103  // install new instructions
104  M->add_bb(new_header);
105  M->add_Instruction(new_exit);
106 
107  }
108  for (Loop::loop_iterator i = loop->loop_begin(), e = loop->loop_end();
109  i != e; ++i) {
110  check_loop(*i);
111  }
112  #endif
113 }
114 
115 bool LoopSimplificationPass::run(JITData &JD) {
116 
117  M = JD.get_Method();
118  assert(M);
119  LT = get_Pass<LoopPass>();
120  assert(LT);
121 
122  for (Loop::loop_iterator i = LT->loop_begin(), e = LT->loop_end();
123  i != e; ++i) {
124  check_loop(*i);
125  }
126 
127  #ifndef NDEBUG
128  LOG("Verify LoopSimplificationPass result" << nl);
129  // verify the result
130  alloc::list<Loop*>::type loops(LT->loop_begin(), LT->loop_end());
131  while(!loops.empty()) {
132  Loop *loop = loops.front();
133  loops.pop_front();
134  assert(LT->get_Loop(loop->get_header()) == loop);
135  for (Loop::loop_iterator i = loop->loop_begin(), e = loop->loop_end();
136  i != e; ++i) {
137  loops.push_back(*i);
138  }
139  }
140  #endif
141 
142  return true;
143 }
144 
145 PassUsage& LoopSimplificationPass::get_PassUsage(PassUsage &PU) const {
146  PU.add_requires<LoopPass>();
147  PU.add_modifies<LoopPass>();
148  return PU;
149 }
150 // the address of this variable is used to identify the pass
151 char LoopSimplificationPass::ID = 0;
152 
153 // register pass
154 static PassRegistry<LoopSimplificationPass> X("LoopSimplificationPass");
155 #endif
156 } // end namespace compiler2
157 } // end namespace jit
158 } // end namespace cacao
159 
160 
161 /*
162  * These are local overrides for various environment variables in Emacs.
163  * Please do not remove this and leave it at the end of the file, where
164  * Emacs will automagically detect them.
165  * ---------------------------------------------------------------------
166  * Local variables:
167  * mode: c++
168  * indent-tabs-mode: t
169  * c-basic-offset: 4
170  * tab-width: 4
171  * End:
172  * vim:noexpandtab:sw=4:ts=4:
173  */
174 
MachineLoop * loop
LoopPassBase< BeginInst > LoopPass
Definition: LoopPass.hpp:35
LoopSetTy::iterator loop_iterator
Definition: LoopBase.hpp:48
void add_Instruction(Instruction *I)
Add instructions to a Method.
Definition: MethodC2.cpp:82
MIIterator i
LoopBase< BeginInst > Loop
Definition: Loop.hpp:35
MIIterator e
#define LOG(STMT)
Analogous to DEBUG.
Definition: logging.hpp:91
#define I(value)
Definition: codegen.c:279
void add_bb(BeginInst *bi)
Add a BeginInst.
Definition: MethodC2.cpp:97
const Method & M
static PassRegistry< BasicBlockSchedulingPass > X("BasicBlockSchedulingPass")
Nl nl
Definition: OStream.cpp:56