#ifndef LOOPUNROLL_HPP #define LOOPUNROLL_HPP #include "BasicBlock.h" #include "Module.h" #include "PassManager.hpp" #include "Type.h" #include #include #include #include using std::cout; using std::endl; using std::map; using std::string; using std::to_string; using std::vector; namespace Graph { using Edge = std::pair; using BackEdgeList = vector; using SimpleLoop = vector; struct BackEdgeSearcher { BackEdgeSearcher(BasicBlock *entry) { dfsrun(entry); } void dfsrun(BasicBlock *bb); vector path; map vis; BackEdgeList edges; }; } // namespace Graph namespace Analysis{ struct LoopAnalysis { LoopAnalysis(const Graph::SimpleLoop &); LoopAnalysis() = delete; enum { INT, FLOAT, UNDEF} Type; union { int v; float fv; } initial, delta, threshold; };} /* This is a class to unroll simple loops: * - strict structure: * --a->b->c--- * ^-----+ * - if the loop has constant times, unroll it. */ class LoopUnroll : public Pass { public: LoopUnroll(Module *_m) : Pass(_m) { for (auto &f : m_->get_functions()) if (f.get_name() == "neg_idx_except") { neg_func = &f; break; } if (neg_func == nullptr) assert(false && "find function neg_idx_except first!"); } LoopUnroll() = delete; void run() override; static string str(const Graph::Edge &edge) { return "(" + edge.first->get_name() + ", " + edge.second->get_name() + ")"; } private: Function *neg_func; Graph::BackEdgeList detect_back(Function *); vector check_sloops(const Graph::BackEdgeList &) const; }; #endif