This is an archive of the discontinued LLVM Phabricator instance.

[InstrSimplify] add testcases for fold sdiv if two operands are negatived and non-overflow
ClosedPublic

Authored by shchenz on Jul 16 2018, 2:03 AM.

Details

Summary

We add folding support for add instruction in D49216.

we should also fold for sdiv instruction if sdiv's two operands are negatived leverage new added interface isKnownNegation() in D49216. for example:

define i32 @negated_operand(i32 %x) {
  %negx = sub nsw i32 0, %x
  %div = sdiv i32 %negx, %x
  ret i32 %div
}

can be folded to

define i32 @negated_operand(i32 %x) {
  ret i32 -1
}

Note: we must ensure sdiv two operands are non-overflow. Otherwise, the optimization is not right.
Result from Alive:
testing transform:

%sub = sub i8 0, %x
%div = sdiv i8 %x, %sub

=>

%div = i8 -1

testing result:
ERROR: Mismatch in values of i8 %div
Example:
%x i8 = 0x80 (128, -128)
%sub i8 = 0x80 (128, -128)
Source value: 0x01 (1)
Target value: 0xFF (255, -1)

Diff Detail

Repository
rL LLVM

Event Timeline

shchenz created this revision.Jul 16 2018, 2:03 AM
shchenz edited the summary of this revision. (Show Details)

sub instruction to shift instruction for negation operands transformation is already done is trunk. Maybe we need to do some code refactor to let subsimplify function also call isKnownNegation().

spatel accepted this revision.EditedJul 16 2018, 8:02 AM

LGTM. I think you can add testcases like this without pre-commit review. If there are questions about whether we have the proper test coverage, we can always answer that in the patch with the code change.

This revision is now accepted and ready to land.Jul 16 2018, 8:02 AM
This revision was automatically updated to reflect the committed changes.
spatel added inline comments.Jul 16 2018, 9:02 AM
llvm/trunk/test/Transforms/InstCombine/sdiv.ll
1

Oops - I did not notice that we were doing an 'instsimplify' here. This file should be moved to test/Transforms/InstSimplify and the RUN line should be changed to only use -instsimplify.