{
for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
generate(*i, mode);
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
+ unsigned n = 0;
+ for(list<Stage>::iterator i=module->stages.begin(); (i!=module->stages.end() && n<10000); ++n)
{
- if(optimize(*i))
+ OptimizeResult result = optimize(*i);
+ if(result==REDO_PREVIOUS)
i = module->stages.begin();
else
++i;
LegacyConverter().apply(stage, features);
}
-bool Compiler::optimize(Stage &stage)
+Compiler::OptimizeResult Compiler::optimize(Stage &stage)
{
ConstantConditionEliminator().apply(stage);
/* Removing variables or functions may cause things from the previous stage
to become unused. */
- bool result = UnusedVariableRemover().apply(stage);
- result |= UnusedFunctionRemover().apply(stage);
+ bool any_removed = UnusedVariableRemover().apply(stage);
+ any_removed |= UnusedFunctionRemover().apply(stage);
- return result;
+ return any_removed ? REDO_PREVIOUS : NEXT_STAGE;
}
void Compiler::finalize(Stage &stage, Mode mode)
};
private:
+ enum OptimizeResult
+ {
+ NEXT_STAGE,
+ REDO_PREVIOUS
+ };
+
Features features;
Module *module;
std::vector<std::string> imported_names;
variables. */
void generate(Stage &, Mode);
- /** Applies optimizations to a stage. The return value indicates if the
- preceding stage should be processed again. */
- bool optimize(Stage &);
+ /** Applies optimizations to a stage. The return value indicates which
+ stage should be optimized next. */
+ OptimizeResult optimize(Stage &);
/** Performs final adjustments on a stage after compilation. */
void finalize(Stage &, Mode);