This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Simplify pointer difference subtractions (GEP-GEP) where GEPs have other uses and one non-constant index
ClosedPublic

Authored by hjyamauchi on Jul 17 2017, 12:24 PM.

Details

Summary

Pointer difference simplifications currently happen only if input GEPs don't have other uses or their indexes are all constants, to avoid duplicating indexing arithmetic.

This patch enables cases with exactly one non-constant index among input GEPs to happen where there is no duplicated arithmetic or code size increase even if input GEPs have other uses.

For example, this patch allows "(&A[42][i]-&A[42][0])" --> "i", which didn't happen previously, if the input GEP(s) have other uses.

Diff Detail

Event Timeline

hjyamauchi created this revision.Jul 17 2017, 12:24 PM
sanjoy accepted this revision.Jul 17 2017, 2:44 PM

lgtm with minor nits

include/llvm/IR/Operator.h
475

I'd call this countNonConstantIndices.

477

You can use llvm::count_if here.

lib/Transforms/InstCombine/InstCombineAddSub.cpp
1490

Please add a full sentence here, or remove it.

This revision is now accepted and ready to land.Jul 17 2017, 2:44 PM
mcrosier added inline comments.
lib/Transforms/InstCombine/InstCombineAddSub.cpp
1468–1469

!GEP1 is the preferred condition style and you don't need the extra curly brackets.

if (!GEP1)
  return nullptr;
1473

Similarly, !GEP2 is preferred.

Addressed the comments.

hjyamauchi marked 3 inline comments as done.

Addressed one more comment.

hjyamauchi marked an inline comment as done.Jul 18 2017, 2:30 PM
hjyamauchi added inline comments.
include/llvm/IR/Operator.h
477

I used std::count_if instead as llvm::count_if seems to use the standard begin/end instead of the idx_begin/idx_end required here.

Now using llvm::count_if + make_range.

include/llvm/IR/Operator.h
477

Now using llvm::count_if + make_range.

hjyamauchi closed this revision.Jul 27 2017, 11:27 AM