| 14 | } |
| 15 | |
| 16 | void RDSpace::build(const std::vector<Function *> &Funcs) { |
| 17 | // 1. Check Input Validity |
| 18 | for (auto *F : Funcs) { |
| 19 | if (!F) |
| 20 | throw std::invalid_argument("Function should not be null"); |
| 21 | if (!F->getParent()) |
| 22 | throw std::invalid_argument("Function should be defined in a module"); |
| 23 | } |
| 24 | |
| 25 | // 2. Initialize Member Variables that should be rebuilt whenever build calls |
| 26 | EntryFuncs = Funcs; |
| 27 | LinkMap.clear(); |
| 28 | CallerMap.clear(); |
| 29 | |
| 30 | // 3. Connect given Functions using Link Map |
| 31 | for (size_t S = 1, E = EntryFuncs.size(); S < E; ++S) |
| 32 | LinkMap.insert(std::make_pair(EntryFuncs[S], EntryFuncs[S - 1])); |
| 33 | |
| 34 | // 4. Build search space for every visitable function from given functions |
| 35 | std::queue<Function *> Queue; |
| 36 | std::set<Function *> VisitedFuncs; |
| 37 | for (auto *F : EntryFuncs) |
| 38 | Queue.push(F); |
| 39 | while (Queue.size() > 0) { |
| 40 | auto *Next = Queue.front(); |
| 41 | assert(Next && "Unexpected Program State"); |
| 42 | |
| 43 | Queue.pop(); |
| 44 | if (VisitedFuncs.find(Next) != VisitedFuncs.end()) |
| 45 | continue; |
| 46 | |
| 47 | VisitedFuncs.insert(Next); |
| 48 | auto DiscoveredFuncs = buildCallerMap(*Next); |
| 49 | for (auto *F : DiscoveredFuncs) |
| 50 | Queue.push(F); |
| 51 | if (BuiltFuncs.find(Next) != BuiltFuncs.end()) |
| 52 | continue; |
| 53 | |
| 54 | build(*Next); |
| 55 | BuiltFuncs.insert(Next); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | std::set<Instruction *> RDSpace::next(Instruction &I) { |
| 60 | auto *F = I.getFunction(); |
no test coverage detected