On Mon, 3 Mar 2014, Richard Biener wrote:
That's a bit much of ad-hoc pattern-matching ... wouldn't be
p = malloc (n);
memset (p, 0, n);
transform better suited to the strlen opt pass? After all that tracks
what 'string' is associated with a SSA name pointer through
arbitrary satements using a lattice.
Too early, it needs to run later than ldist, or there won't be any
memset to match in the std::vector case. Would you consider moving or
duplicating either strlen or ldist so they are run in the order I need?
The same probably applies to calloc(); memset (, 0,);
Oh, you mean the length doesn't have to match for calloc? That's true, I
completely missed that.
though here you
could even match points-to info (after all even only clearing a portion of
the calloc()ed memory is dead code). If points-to conservatively computes
that the memset pointer only points to null or the memory tag the
calloc return value points to then you can discard it without further
checking ...
I'll look into it (and DSE). Note that the calloc case is just an
afterthought, what I really care about is replacing malloc.
+ /* Finally, make sure the memory is not used before stmt2. */
+ ao_ref ref;
+ ao_ref_init_from_ptr_and_size (&ref, ptr1, size);
+ tree vdef = gimple_vuse (stmt2);
+ if (vdef == NULL)
+ return false;
+ while (true)
+ {
+ gimple cur = SSA_NAME_DEF_STMT (vdef);
+ if (cur == stmt1) break;
+ if (stmt_may_clobber_ref_p_1 (cur, &ref))
+ return false;
+ vdef = gimple_vuse (cur);
+ }
We have walk_aliased_vdefs() for this.
As explained in the PR, walk_aliased_vdefs misses the call to malloc (it
doesn't clobber the memory pointed to by p). You then suggested:
"Exact pattern matching of the CFG involved might be the easiest, plus
manually implementing walk_aliased_vdefs by simply walking the use-def
chain of the virtual operands from the memset operation to the malloc and
checking stmt_may_clobber_ref_p_1 on the ao_ref_init_from_ptr_and_size
ref."
That said, please try to integrate this kind of transforms with
the strlen opt pass (even if it requires making its lattice more generic).
Assuming the passes have a chance of being reordered, I'll try to
understand how strlen works.
Thanks for the comments,
--
Marc Glisse