From 041ea8726d34b6f4dfed3caf22ab292685df1fde Mon Sep 17 00:00:00 2001 From: Alessandro Di Federico Date: Mon, 13 Jun 2016 13:52:51 +0200 Subject: [PATCH] Introduce `predecessors` and `successors` --- codegenerator.cpp | 2 +- ir-helpers.h | 11 +++++++++++ jumptargetmanager.cpp | 12 ++++-------- osra.cpp | 12 +++++------- set.cpp | 2 +- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/codegenerator.cpp b/codegenerator.cpp index 04d9d1fb8..6ec6b32da 100644 --- a/codegenerator.cpp +++ b/codegenerator.cpp @@ -604,7 +604,7 @@ static void purgeDeadBlocks(Function *F) { // Skip the first basic block for (BasicBlock &BB : make_range(++F->begin(), F->end())) - if (pred_begin(&BB) == pred_end(&BB)) + if (pred_empty(&BB)) Kill.push_back(&BB); } while (!Kill.empty()); diff --git a/ir-helpers.h b/ir-helpers.h index adac67738..1da7e9862 100644 --- a/ir-helpers.h +++ b/ir-helpers.h @@ -3,6 +3,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Analysis/ConstantFolding.h" +#include "llvm/Analysis/Interval.h" #include "llvm/IR/CFG.h" #include "llvm/IR/Constants.h" #include "llvm/IR/Instruction.h" @@ -80,4 +81,14 @@ static inline uint64_t getExtValue(llvm::Constant *C, return getZExtValue(C, DL); } +static inline llvm::iterator_range +predecessors(llvm::Interval *BB) { + return make_range(pred_begin(BB), pred_end(BB)); +} + +static inline llvm::iterator_range +successors(llvm::Interval *BB) { + return make_range(succ_begin(BB), succ_end(BB)); +} + #endif // _IRHELPERS_H diff --git a/jumptargetmanager.cpp b/jumptargetmanager.cpp index a906f8a8f..11ed64f59 100644 --- a/jumptargetmanager.cpp +++ b/jumptargetmanager.cpp @@ -505,16 +505,14 @@ JumpTargetManager::getPC(Instruction *TheInstruction) const { // If we haven't find a newpc call yet, continue exploration backward if (NewPCCall == nullptr) { // If one of the predecessors is the dispatcher, don't explore any further - auto Predecessors = make_range(pred_begin(BB), pred_end(BB)); - for (BasicBlock *Predecessor : Predecessors) { + for (BasicBlock *Predecessor : predecessors(BB)) { // Assert we didn't reach the almighty dispatcher assert(!(NewPCCall == nullptr && Predecessor == Dispatcher)); if (Predecessor == Dispatcher) continue; } - Predecessors = make_range(pred_begin(BB), pred_end(BB)); - for (BasicBlock *Predecessor : Predecessors) { + for (BasicBlock *Predecessor : predecessors(BB)) { // Ignore already visited or empty BBs if (!Predecessor->empty() && Visited.find(Predecessor) == Visited.end()) { @@ -589,8 +587,7 @@ void JumpTargetManager::handleSumJump(Instruction *SumJump) { } // Inspect and enqueue successors - auto Successors = make_range(succ_begin(BB), succ_end(BB)); - for (BasicBlock *Successor : Successors) + for (BasicBlock *Successor : successors(BB)) if (Visited.find(Successor) == Visited.end()) WorkList.push(Successor); @@ -841,8 +838,7 @@ void JumpTargetManager::unvisit(BasicBlock *BB) { Visited.erase(Current); - auto Successors = make_range(succ_begin(Current), succ_end(Current)); - for (BasicBlock *Successor : Successors) { + for (BasicBlock *Successor : successors(BB)) { if (Visited.find(Successor) != Visited.end() && !Successor->empty()) { auto *Call = dyn_cast(&*Successor->begin()); diff --git a/osra.cpp b/osra.cpp index 8984a1571..a31f0ceac 100644 --- a/osra.cpp +++ b/osra.cpp @@ -965,10 +965,8 @@ bool OSRAPass::runOnFunction(Function &F) { // Propagate the new constraints to the successors (except for the // dispatcher) - auto Successors = make_range(succ_begin(Entry.Target), - succ_end(Entry.Target)); if (Entry.Constraints.size() != 0) - for (BasicBlock *Successor : Successors) + for (BasicBlock *Successor : successors(Entry.Target)) if (BlockBlackList.find(Successor) == BlockBlackList.end()) ConstraintsWL.push_back(WLEntry(Successor, Entry.Target, @@ -1140,7 +1138,7 @@ bool OSRAPass::runOnFunction(Function &F) { // If we didn't stop, enqueue all the non-blacklisted successors for // exploration if (!Stop) - for (auto *Successor : make_range(succ_begin(BB), succ_end(BB))) + for (auto *Successor : successors(BB)) if (!BlockBlackList.count(Successor) && !Successor->empty() && !Visited.count(Successor)) @@ -1258,7 +1256,7 @@ bool OSRAPass::runOnFunction(Function &F) { // If we didn't stop, enqueue all the non-blacklisted successors for // exploration if (!Stop) - for (auto *Successor : make_range(succ_begin(BB), succ_end(BB))) + for (auto *Successor : successors(BB)) if (BlockBlackList.find(Successor) == BlockBlackList.end() && !Successor->empty() && Visited.find(Successor) == Visited.end()) @@ -1362,9 +1360,9 @@ BoundedValue &OSRAPass::BVMap::summarize(BasicBlock *Target, BVOVector->Summary = BVOVector->Components[0].second; unsigned PredecessorsCount = 0; - for (auto *Predecessor : make_range(pred_begin(Target), pred_end(Target))) + for (auto *Predecessor : predecessors(Target)) if (BlockBlackList->find(Predecessor) == BlockBlackList->end() - && pred_begin(Predecessor) != pred_end(Predecessor)) + && !pred_empty(Predecessor)) PredecessorsCount++; // Do we have a constraint for each predecessor? diff --git a/set.cpp b/set.cpp index 37659dc47..20efa4ddc 100644 --- a/set.cpp +++ b/set.cpp @@ -270,7 +270,7 @@ void SET::enqueueStores(LoadInst *Start) { // If we haven't find a store, proceed recursively in the predecessors if (!Found && Depth < MaxDepth) - for (BasicBlock *Predecessor : make_range(pred_begin(BB), pred_end(BB))) + for (BasicBlock *Predecessor : predecessors(BB)) if (Predecessor != JTM->dispatcher() && !Predecessor->empty()) ToExplore.push(make_pair(&*Predecessor->rbegin(), Depth + 1)); }