@@ -48,7 +48,7 @@ struct Node {
4848 Set, // a register, defined by a SetLocal
4949 Const, // a constant value
5050 Phi, // a phi from converging control flow
51- Cond, // a condition on a block path (blockpc)
51+ Cond, // a condition on a block path (pc or blockpc)
5252 Block, // a source of phis
5353 Bad // something we can't handle and should ignore
5454 } type;
@@ -434,6 +434,10 @@ struct Trace : public Visitor<Trace> {
434434 auto * node = builder.setNodeMap [set];
435435 // Pull in all the dependencies, starting from the value itself.
436436 add (node);
437+ // Also pull in conditions based on the location of this node: e.g.
438+ // if it is inside an if's true branch, we can add a path-condition
439+ // for that.
440+ addPath (set);
437441 // If nothing bad showed up, still mark it as bad if it's trivial
438442 // and worthless.
439443 if (!bad) {
@@ -489,6 +493,39 @@ struct Trace : public Visitor<Trace> {
489493 return node;
490494 }
491495
496+ void addPath (Expression* curr) {
497+ // We track curr and parent, which are always in the state of parent
498+ // being the parent of curr.
499+ auto * parent = builder.parentMap .at (set);
500+ while (parent) {
501+ auto iter = builder.expressionBlockMap .find (parent);
502+ if (iter != builder.expressionBlockMap .end ()) {
503+ // Given the block, add a proper path-condition
504+ Node* node = iter.second ;
505+ addPathTo (parent, curr, node);
506+ }
507+ curr = parent;
508+ parent = builder.parentMap .at (parent);
509+ }
510+ }
511+
512+ // curr is a child of parent, and parent has a Block which we are
513+ // give as 'node'. Add a path condition for reaching the child.
514+ void addPathTo (Expression* parent, Expression* curr, Node* node) {
515+ if (auto * iff = parent->dynCast <If>()) {
516+ if (curr == iff->ifTrue ) {
517+ ..
518+ need to mark the Condition as pc and not a blockpc
519+ } else if (curr == iff->ifFalse ) {
520+ ..
521+ } else {
522+ WASM_UNREACHABLE ();
523+ }
524+ } else {
525+ WASM_UNREACHABLE ();
526+ }
527+ }
528+
492529 bool isBad () {
493530 return bad;
494531 }
0 commit comments