diff options
author | Alon Zakai <azakai@google.com> | 2019-04-26 16:59:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-26 16:59:41 -0700 |
commit | db9124f1de0478dcac525009b6f1589b44a7edd8 (patch) | |
tree | fa26395a0f6cca53cf5cb6e10189f989c5bfa847 /src/cfg/liveness-traversal.h | |
parent | 87636dccd404a340d75acb1d96301581343f29ca (diff) | |
download | binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.tar.gz binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.tar.bz2 binaryen-db9124f1de0478dcac525009b6f1589b44a7edd8.zip |
Apply format changes from #2048 (#2059)
Mass change to apply clang-format to everything. We are applying this in a PR by me so the (git) blame is all mine ;) but @aheejin did all the work to get clang-format set up and all the manual work to tidy up some things to make the output nicer in #2048
Diffstat (limited to 'src/cfg/liveness-traversal.h')
-rw-r--r-- | src/cfg/liveness-traversal.h | 104 |
1 files changed, 65 insertions, 39 deletions
diff --git a/src/cfg/liveness-traversal.h b/src/cfg/liveness-traversal.h index edbe52fd6..b26898460 100644 --- a/src/cfg/liveness-traversal.h +++ b/src/cfg/liveness-traversal.h @@ -21,12 +21,12 @@ #ifndef liveness_traversal_h #define liveness_traversal_h +#include "cfg-traversal.h" +#include "ir/utils.h" #include "support/sorted_vector.h" -#include "wasm.h" #include "wasm-builder.h" #include "wasm-traversal.h" -#include "cfg-traversal.h" -#include "ir/utils.h" +#include "wasm.h" namespace wasm { @@ -41,20 +41,19 @@ typedef SortedVector LocalSet; // "other" which can be used for other purposes, to mark // their position in a block struct LivenessAction { - enum What { - Get = 0, - Set = 1, - Other = 2 - }; + enum What { Get = 0, Set = 1, Other = 2 }; What what; - Index index; // the local index read or written + Index index; // the local index read or written Expression** origin; // the origin bool effective; // whether a store is actually effective, i.e., may be read - LivenessAction(What what, Index index, Expression** origin) : what(what), index(index), origin(origin), effective(false) { + LivenessAction(What what, Index index, Expression** origin) + : what(what), index(index), origin(origin), effective(false) { assert(what != Other); - if (what == Get) assert((*origin)->is<GetLocal>()); - if (what == Set) assert((*origin)->is<SetLocal>()); + if (what == Get) + assert((*origin)->is<GetLocal>()); + if (what == Set) + assert((*origin)->is<SetLocal>()); } LivenessAction(Expression** origin) : what(Other), origin(origin) {} @@ -80,15 +79,18 @@ struct LivenessAction { // information about liveness in a basic block struct Liveness { - LocalSet start, end; // live locals at the start and end + LocalSet start, end; // live locals at the start and end std::vector<LivenessAction> actions; // actions occurring in this block #if LIVENESS_DEBUG void dump(Function* func) { - if (actions.empty()) return; + if (actions.empty()) + return; std::cout << " actions:\n"; for (auto& action : actions) { - std::cout << " " << (action.isGet() ? "get" : (action.isSet() ? "set" : "other")) << " " << func->getLocalName(action.index) << "\n"; + std::cout << " " + << (action.isGet() ? "get" : (action.isSet() ? "set" : "other")) + << " " << func->getLocalName(action.index) << "\n"; } } #endif // LIVENESS_DEBUG @@ -96,28 +98,35 @@ struct Liveness { template<typename SubType, typename VisitorType> struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { - typedef typename CFGWalker<SubType, VisitorType, Liveness>::BasicBlock BasicBlock; + typedef + typename CFGWalker<SubType, VisitorType, Liveness>::BasicBlock BasicBlock; Index numLocals; std::unordered_set<BasicBlock*> liveBlocks; - std::vector<uint8_t> copies; // canonicalized - accesses should check (low, high) TODO: use a map for high N, as this tends to be sparse? or don't look at copies at all for big N? - std::vector<Index> totalCopies; // total # of copies for each local, with all others + // canonicalized - accesses should check (low, high) + // TODO: use a map for high N, as this tends to be sparse? or don't look at + // copies at all for big N? + std::vector<uint8_t> copies; + // total # of copies for each local, with all others + std::vector<Index> totalCopies; // cfg traversal work static void doVisitGetLocal(SubType* self, Expression** currp) { auto* curr = (*currp)->cast<GetLocal>(); - // if in unreachable code, ignore + // if in unreachable code, ignore if (!self->currBasicBlock) { *currp = Builder(*self->getModule()).replaceWithIdenticalType(curr); return; } - self->currBasicBlock->contents.actions.emplace_back(LivenessAction::Get, curr->index, currp); + self->currBasicBlock->contents.actions.emplace_back( + LivenessAction::Get, curr->index, currp); } static void doVisitSetLocal(SubType* self, Expression** currp) { auto* curr = (*currp)->cast<SetLocal>(); - // if in unreachable code, we don't need the tee (but might need the value, if it has side effects) + // if in unreachable code, we don't need the tee (but might need the value, + // if it has side effects) if (!self->currBasicBlock) { if (curr->isTee()) { *currp = curr->value; @@ -126,10 +135,12 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { } return; } - self->currBasicBlock->contents.actions.emplace_back(LivenessAction::Set, curr->index, currp); + self->currBasicBlock->contents.actions.emplace_back( + LivenessAction::Set, curr->index, currp); // if this is a copy, note it if (auto* get = self->getCopy(curr)) { - // add 2 units, so that backedge prioritization can decide ties, but not much more + // add 2 units, so that backedge prioritization can decide ties, but not + // much more self->addCopy(curr->index, get->index); self->addCopy(curr->index, get->index); } @@ -137,16 +148,19 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { // A simple copy is a set of a get. A more interesting copy // is a set of an if with a value, where one side a get. - // That can happen when we create an if value in simplify-locals. TODO: recurse into - // nested ifs, and block return values? Those cases are trickier, need to - // count to see if worth it. + // That can happen when we create an if value in simplify-locals. TODO: + // recurse into nested ifs, and block return values? Those cases are trickier, + // need to count to see if worth it. // TODO: an if can have two copies GetLocal* getCopy(SetLocal* set) { - if (auto* get = set->value->dynCast<GetLocal>()) return get; + if (auto* get = set->value->dynCast<GetLocal>()) + return get; if (auto* iff = set->value->dynCast<If>()) { - if (auto* get = iff->ifTrue->dynCast<GetLocal>()) return get; + if (auto* get = iff->ifTrue->dynCast<GetLocal>()) + return get; if (iff->ifFalse) { - if (auto* get = iff->ifFalse->dynCast<GetLocal>()) return get; + if (auto* get = iff->ifFalse->dynCast<GetLocal>()) + return get; } } return nullptr; @@ -162,7 +176,8 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { std::fill(totalCopies.begin(), totalCopies.end(), 0); // create the CFG by walking the IR CFGWalker<SubType, VisitorType, Liveness>::doWalkFunction(func); - // ignore links to dead blocks, so they don't confuse us and we can see their stores are all ineffective + // ignore links to dead blocks, so they don't confuse us and we can see + // their stores are all ineffective liveBlocks = CFGWalker<SubType, VisitorType, Liveness>::findLiveBlocks(); CFGWalker<SubType, VisitorType, Liveness>::unlinkDeadBlocks(liveBlocks); // flow liveness across blocks @@ -173,24 +188,30 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { // keep working while stuff is flowing std::unordered_set<BasicBlock*> queue; for (auto& curr : CFGWalker<SubType, VisitorType, Liveness>::basicBlocks) { - if (liveBlocks.count(curr.get()) == 0) continue; // ignore dead blocks + if (liveBlocks.count(curr.get()) == 0) + continue; // ignore dead blocks queue.insert(curr.get()); - // do the first scan through the block, starting with nothing live at the end, and updating the liveness at the start + // do the first scan through the block, starting with nothing live at the + // end, and updating the liveness at the start scanLivenessThroughActions(curr->contents.actions, curr->contents.start); } - // at every point in time, we assume we already noted interferences between things already known alive at the end, and scanned back through the block using that + // at every point in time, we assume we already noted interferences between + // things already known alive at the end, and scanned back through the block + // using that while (queue.size() > 0) { auto iter = queue.begin(); auto* curr = *iter; queue.erase(iter); LocalSet live; - if (!mergeStartsAndCheckChange(curr->out, curr->contents.end, live)) continue; + if (!mergeStartsAndCheckChange(curr->out, curr->contents.end, live)) + continue; assert(curr->contents.end.size() < live.size()); curr->contents.end = live; scanLivenessThroughActions(curr->contents.actions, live); // liveness is now calculated at the start. if something // changed, all predecessor blocks need recomputation - if (curr->contents.start == live) continue; + if (curr->contents.start == live) + continue; assert(curr->contents.start.size() < live.size()); curr->contents.start = live; for (auto* in : curr->in) { @@ -200,9 +221,13 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { } // merge starts of a list of blocks. return - // whether anything changed vs an old state (which indicates further processing is necessary). - bool mergeStartsAndCheckChange(std::vector<BasicBlock*>& blocks, LocalSet& old, LocalSet& ret) { - if (blocks.size() == 0) return false; + // whether anything changed vs an old state (which indicates further + // processing is necessary). + bool mergeStartsAndCheckChange(std::vector<BasicBlock*>& blocks, + LocalSet& old, + LocalSet& ret) { + if (blocks.size() == 0) + return false; ret = blocks[0]->contents.start; if (blocks.size() > 1) { // more than one, so we must merge @@ -213,7 +238,8 @@ struct LivenessWalker : public CFGWalker<SubType, VisitorType, Liveness> { return old != ret; } - void scanLivenessThroughActions(std::vector<LivenessAction>& actions, LocalSet& live) { + void scanLivenessThroughActions(std::vector<LivenessAction>& actions, + LocalSet& live) { // move towards the front for (int i = int(actions.size()) - 1; i >= 0; i--) { auto& action = actions[i]; |