This is an archive of the discontinued LLVM Phabricator instance.

InstCombine: Fold ldexp(ldexp(x, a), b) -> ldexp(x, a + b)
ClosedPublic

Authored by arsenm on Jul 5 2023, 4:47 AM.

Details

Summary

The problem here is overflow or underflow which would have occurred in
the inner operation, which the exponent offsetting avoids. We can do
this if we know the two exponents are in the same direction, or
reassoc flags allow unsafe reassociates.

Diff Detail

Event Timeline

arsenm created this revision.Jul 5 2023, 4:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 5 2023, 4:47 AM
arsenm requested review of this revision.Jul 5 2023, 4:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 5 2023, 4:47 AM
Herald added a subscriber: wdng. · View Herald Transcript
foad accepted this revision.Jul 5 2023, 5:32 AM

LGTM.

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2381–2383

Additionally it would be safe if both exponents are known to be <= 0, but I guess we don't have a helper function for that.

This revision is now accepted and ready to land.Jul 5 2023, 5:32 AM
arsenm added inline comments.Jul 5 2023, 5:47 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2381–2383

Right we totally lack FP range tracking

foad added inline comments.Jul 5 2023, 6:01 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2381–2383

I just mean for the integer exponents, you're checking if they're both < 0, but that could be relaxed to both <= 0.

arsenm added inline comments.Jul 5 2023, 8:29 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2381–2383

This checks that they are both positive or both negative so I’m not sure what you mean

foad added inline comments.Jul 5 2023, 8:35 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2381–2383

Your code optimizes if they are both >= 0 or both < 0.
You could optimize if they are both >= 0 or both <= 0. This might optimize more cases.

arsenm closed this revision.Jul 7 2023, 5:15 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
2381–2383

added todo