This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0)) (PR50183)
ClosedPublic

Authored by RKSimon on Jul 13 2021, 9:16 AM.

Details

Summary

As discussed on PR50183, we already fold:

define <4 x i32>* @select0a(<4 x i32>* %a0, i64 %a1, i1 %a2, i64 %a3) {
  %gep0 = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %a1
  %gep1 = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %a3
  %sel = select i1 %a2, <4 x i32>* %gep0, <4 x i32>* %gep1
  ret <4 x i32> %sel
}
-->
define <4 x i32>* @select1a(<4 x i32>* %a0, i64 %a1, i1 %a2, i64 %a3) {
  %sel = select i1 %a2, i64 %a1, i64 %a3
  %gep = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %sel
  ret <4 x i32> %gep
}

This patch adds basic handling for the 'fallthrough' cases where the gep idx == 0 has been folded away to the base address:

define <4 x i32>* @select0(<4 x i32>* %a0, i64 %a1, i1 %a2) {
  %gep = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %a1
  %sel = select i1 %a2, <4 x i32>* %a0, <4 x i32>* %gep
  ret <4 x i32>* %sel
}
-->
define <4 x i32>* @select1(<4 x i32>* %a0, i64 %a1, i1 %a2) {
  %sel = select i1 %a2, i64 0, i64 %a1
  %gep = getelementptr inbounds <4 x i32>, <4 x i32>* %a0, i64 %sel
  ret <4 x i32>* %gep
}

Diff Detail

Event Timeline

RKSimon created this revision.Jul 13 2021, 9:16 AM
RKSimon requested review of this revision.Jul 13 2021, 9:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2021, 9:16 AM
reames accepted this revision.Jul 13 2021, 9:53 AM

LGTM w/minor comment

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2942

Please use early return to reduce nesting.

This revision is now accepted and ready to land.Jul 13 2021, 9:53 AM
This revision was landed with ongoing or failed builds.Jul 14 2021, 3:14 AM
This revision was automatically updated to reflect the committed changes.