This is an archive of the discontinued LLVM Phabricator instance.

Simplify gep (gep p, a), (b-a)
ClosedPublic

Authored by ranma42 on Dec 5 2016, 6:10 AM.

Details

Summary

This patch extends the patterns recognised by InstCombine to also optimise gep (gep p, a), (b-a) into gep p, b.
Some minor refactoring was done in order to make the various optimisation/shortcut cases share the same code structure.

The missing optimisation was found investigating why LLVM did not optimise away pointer computations in Rust slices: https://github.com/rust-lang/rust/pull/37921

Diff Detail

Repository
rL LLVM

Event Timeline

ranma42 updated this revision to Diff 80256.Dec 5 2016, 6:10 AM
ranma42 retitled this revision from to Simplify gep (gep p, a), (b-a).
ranma42 updated this object.
ranma42 added reviewers: majnemer, wmi.
ranma42 added a subscriber: llvm-commits.

Should I ping somebody to get started with the review?

Can we make this code a bit more general? Something like if (Value* Simplified = SimplifyAddInst(GO1, SO1, ...).

ranma42 updated this revision to Diff 81542.Dec 15 2016, 1:01 AM

The new patch extends the patterns recognised by InstCombine as suggested by Eli Friedman.
It can now optimise any gep (gep p, a), b which can be represented as gep p, a+b whenever the a+b operation is folded or simplified away by InstSimplify.
This strictly extends the patterns that are captured, because the previous code only simplified the addition when one operand was 0 or both were constant.

davide added a subscriber: davide.Dec 15 2016, 1:47 AM

The new patch extends the patterns recognised by InstCombine as suggested by Eli Friedman.
It can now optimise any gep (gep p, a), b which can be represented as gep p, a+b whenever the a+b operation is folded or simplified away by InstSimplify.
This strictly extends the patterns that are captured, because the previous code only simplified the addition when one operand was 0 or both were constant.

Is it possible to add a test to show the generalized behaviour?

The const+const pattern is already tested by test3 in test/Transforms/InstCombine/getelementptr.ll
Are there any other specific patterns that might be important to include in the patch?

I can write tests for more patterns, but I have no clue on how to check that any instcombine-able pattern will be folded away. Is it possible to do that? Any hints on how it could be achieved?

The const+const pattern is already tested by test3 in test/Transforms/InstCombine/getelementptr.ll
Are there any other specific patterns that might be important to include in the patch?

I can write tests for more patterns, but I have no clue on how to check that any instcombine-able pattern will be folded away. Is it possible to do that? Any hints on how it could be achieved?

Showing that any pattern will be folded may be hard, but if you can add some more patterns, that will be nice.

ranma42 updated this revision to Diff 81749.Dec 16 2016, 6:14 AM

Added tests for more add simplification cases.

davide added inline comments.Dec 16 2016, 11:53 AM
lib/Transforms/InstCombine/InstructionCombining.cpp
1566–1570 ↗(On Diff #81749)

You can fold the SimplifyAddInst inside the if, no?

davide added inline comments.Dec 16 2016, 12:07 PM
test/Transforms/InstCombine/getelementptr.ll
886–893 ↗(On Diff #81749)

My understanding of the transform is that the first gep will be removed right?
So, maybe add
CHECK-NOT: getelementptr i32, i32* %I, i64 %C
here and in the other tests?

I'm not really a fan of CHECK-NOT in most cases; tests using it have a tendency to bitrot over time. Better to use CHECK-NEXT to make sure there aren't any unexpected instructions in the optimized function.

I'm not really a fan of CHECK-NOT in most cases; tests using it have a tendency to bitrot over time. Better to use CHECK-NEXT to make sure there aren't any unexpected instructions in the optimized function.

Sure, CHECK-NEXT is probably better.

ranma42 added inline comments.Dec 16 2016, 4:57 PM
lib/Transforms/InstCombine/InstructionCombining.cpp
1566–1570 ↗(On Diff #81749)

As in

if (!(Sum = SimplifyAddInst(GO1, SO1, false, false, DL, &TLI, &DT, &AC)))

?
I was afraid that it was less readable, but I can definitely do that :)

davide added inline comments.Dec 16 2016, 5:11 PM
lib/Transforms/InstCombine/InstructionCombining.cpp
1566–1570 ↗(On Diff #81749)

up to you. The API is actually quite verbose already because it takes a lot of arguments. Anyway, I'm OK with this but please wait for others to comment.

ranma42 updated this revision to Diff 81951.Dec 19 2016, 8:01 AM

Use CHECK-NEXT as suggested

ranma42 marked an inline comment as done.Dec 19 2016, 8:06 AM
ranma42 added inline comments.Dec 23 2016, 2:13 PM
lib/Transforms/InstCombine/InstructionCombining.cpp
1566–1570 ↗(On Diff #81749)

I received no other comments on this. Should I proceed with your suggestion?

efriedma accepted this revision.Jan 18 2017, 1:51 PM

LGTM.

This revision is now accepted and ready to land.Jan 18 2017, 1:51 PM
davide accepted this revision.Jan 18 2017, 1:57 PM

lg. Do you need somebody to commit this on your behalf?

Awesome!

Yes, I need someone to commit the patch on my behalf.
Thank you for your suggestions and help :)

Awesome!

Yes, I need someone to commit the patch on my behalf.
Thank you for your suggestions and help :)

Committing this for you after a round of ninja check-all. Thanks!

This revision was automatically updated to reflect the committed changes.