On 9/29/21 4:43 AM, Richard Biener wrote:
On Tue, Sep 28, 2021 at 10:39 PM Andrew MacLeod <amacl...@redhat.com> wrote:
On 9/28/21 7:50 AM, Richard Biener wrote:
On Wed, Sep 15, 2021 at 10:46 AM Martin Liška <mli...@suse.cz> wrote:
    /* Unswitch single LOOP.  NUM is number of unswitchings done; we do not 
allow
@@ -269,6 +311,7 @@ tree_unswitch_single_loop (class loop *loop, int num)
      class loop *nloop;
      unsigned i, found;
      tree cond = NULL_TREE;
+  edge cond_edge = NULL;
      gimple *stmt;
      bool changed = false;
      HOST_WIDE_INT iterations;
@@ -311,11 +354,12 @@ tree_unswitch_single_loop (class loop *loop, int num)
      bbs = get_loop_body (loop);
      found = loop->num_nodes;

+  gimple_ranger ranger;
ISTR constructing/destructing ranger has a non-negligible overhead -
is it possible
to keep it live for a longer time (note we're heavily modifying the CFG)?

There is some overhead.. right now we determine all the imports and
exports for each block ahead of time, but thats about it. We can make
adjustments for true on demand clients like this so that even that
doesnt happen. we only do that so we know ahead of time which ssa-names
are never used in outgoing edges, and never even have to check those.
Thats mostly an optimization for heavy users like EVRP.  If you want, I
can make that an option  so there virtually no overhead

More importantly, the longer it remains alive, the more "reuse" of
ranges you will get..   If there is not a pattern of using variables
from earlier in the program it wouldnt really matter much.

In Theory, modifying the IL should be fine, it happens already in
places, but its not extensively tested under those conditions yet.
Note it's modifying the CFG as well.
bah, thats what I meant.   as long as the IL is changed and CFG updated to match, it should in theory work.  And as long as existing SSA_NAMEs dont have their meaning changes..  ie reusing an SSA_NAME to have a  different definition is likely to cause problems without telling ranger that an SSA_NAME is now different.

My issue is that the current place is one construction per loop
(and even when we do _not_ end up doing anything), so if the
ranger use isn't O(loop-size) (not to mention nest depth...) then
we'll quickly run into complexity issues for functions with a lot of
loops.

It should, again in theory, be possible to simple call enable_ranger(cfun) at the beginning of the pass,  and then just use "get_range_query(cfun)"  throughout.. and call disable_ranger at the end.  or if you want to keep using the gimple_ranger pointer:

gimple_ranger *ranger = enable_ranger (cfun);

disable_ranger(cfun)


I *think* it will work thru the CFG changes.. but one would have to try it and see if the same results happen.  If they don't loop me in because it might be something simple.   we may need to force a request for a range of the stmts which are changed under some circumstances.  Nothing occurs to me off the top of my head that would be needed.



Yes and no  :-)  I use to do that, but now that we allow uninitialized
values to be treated as UNDEFINED,  it may also mean that its
uninitialized on that edge.

Evaluating
if (c_3 == 0)       when we know c_3 = [1,1]

What you suggest is fundamentally what ranger does... It evaluates what
the full set of possible ranges are on the edge you ask about, then
intersects it with the known range of c_3.  .   If the condition cannot
ever be true,and is thus unexecutable,  the result will be UNDEFINED .
ie above,  c_3 would have to have a range of [0,0] on the true edge, and
its real range is [1,1].. intersecting the 2 values results in UNDEFINED...

So it can mean the edge is unexecutable.   It can also mean the value is
actually undefined.. if this was a use-before-def case, the range of c_3
in the block would be UNDEFINED.  and c_3 will be UNDEFINED on BOTH
edges due ot the intersection.  the UNDEFINED state is viral.

I guess you can argue you can arbitrarily choose an edge to process in
this case, but if you want to avoid that situation completely, I think
you could also check that cond is not UNDEFINED  in the stmt first..
then if you get UNDEFINED on and edge you are 100% sure its
unexectuable.. ie

+
+         if (ranger.range_of_expr (r, cond, stmt) && !r.undefined_p ())
+           {
+             if (ranger.range_on_edge (r, edge_true, cond) && r.undefined_p ())

Note the call to range_of_expr () will do the supported_type check
anyway and return false if it isnt supported.
So my question was probably more like if there's a way to evaluate
a condition with ranger to either true, false or unknown that looks less "odd"?

well, you can directly try to fold the conditional stmt and see what the result is.   You can simply pass a range_query in which is used to resolve the operands on the stmt.   So where 'stmt' is :    if (c_3 == 0)

  if (fold_range (r, stmt, ranger) && r.singleton_p ())

will return [0,1] if we don't know which edge is taken, or [0,0] if the false edge is known to be taken, or [1,1] if the true edge is known to be taken...

Or, if we switch to the enable_ranger() mechanism for the entire pass:

  if (fold_range (r, stmt, get_range_query (cfun)) && r.singleton_p ())

btw this is virtually the same as

  if (get_range_query (cfun)->range_of_stmt (r, stmt) && r.singleton_p ())



In principle it would be asking for the range of the c_3 == 0 expression
which is embedded in the GIMPLE_COND.  I really don't like the other
proposed options as they look like error-prone (wrt wrong-code).

yes, so its exactly like that


Andrew


Reply via email to