This is an archive of the discontinued LLVM Phabricator instance.

[IfCvt][ARM] Optimise diamond if-conversion for code size
ClosedPublic

Authored by ostannard on Sep 9 2019, 5:43 AM.

Details

Summary

Currently, the heuristics the if-conversion pass uses for diamond if-conversion
are based on execution time, with no consideration for code size. This adds a
new set of heuristics to be used when optimising for code size.

This is mostly target-independent, because the if-conversion pass can
see the code size of the instructions which it is removing. For thumb,
there are a few passes (insertion of IT instructions, selection of
narrow branches, and selection of CBZ instructions) which are run after
if conversion and affect these heuristics, so I've added target hooks to
better predict the code-size effect of a proposed if-conversion.

Diff Detail

Event Timeline

ostannard created this revision.Sep 9 2019, 5:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2019, 5:43 AM
efriedma added inline comments.Sep 9 2019, 6:48 PM
llvm/lib/CodeGen/IfConversion.cpp
289

Maybe minsize? I don't think we want to predicate some ridiculous number of instructions to save a few bytes at -Os.

306

Is this actually true, if the terminator isn't analyzable? We can form a "diamond" where TrueBB and FalseBB have an arbitrary terminator, like an INLINEASM_BR, as long as it's the same.

334

Are you adding CommonBytes for common instructions before the predicated ones, somewhere?

997

This looks weird; why are we constructing TrueBBICalc/FalseBBICalc like this? What does this change do?

ostannard marked 4 inline comments as done.Sep 10 2019, 3:30 AM
ostannard added inline comments.
llvm/lib/CodeGen/IfConversion.cpp
306

Good point, non-analyzable terminators should be treated as common instructions, not branches which can be completely removed.

334

I didn't do that because I haven't been able to find an example where there are common instructions at the start of the blocks, I'd expect them to always be hoisted up into the common block. That said, the code change is small, and can be tested by MIR, so I'll add this.

997

These are temporary BBInfo objects which will be passed into MeetIfcvtSizeLimit, I think they differ from the actual BBInfos in that they ignore the common instructions, which would prevent if-conversion if there wasn't a matching instruction in the other block.

I need to also pass through the IsBrAnalyzable value so that MeetIfcvtSizeLimit can treat non-analyzable branches differently to analyzable ones (only the latter can be removed completely).

ostannard marked an inline comment as done.Sep 10 2019, 3:52 AM
ostannard added inline comments.
llvm/lib/CodeGen/IfConversion.cpp
306

I think this also needs to take into account whether we are doing forked-diamond if conversion, in which case even analyzable branches will merged rather then removed.

ostannard updated this revision to Diff 219513.Sep 10 2019, 3:59 AM
  • Limit to minsize (-Oz)
  • Count common instructions at the start of the true/false blocks
  • Non-analyzable branches and branches in forked diamonds will be merged, not removed
This revision is now accepted and ready to land.Oct 9 2019, 11:50 AM
This revision was automatically updated to reflect the committed changes.