[InstSimplify] fold extracting from std::pair (1/2)
authorHiroshi Inoue <inouehrs@jp.ibm.com>
Wed, 1 Aug 2018 04:40:32 +0000 (04:40 +0000)
committerHiroshi Inoue <inouehrs@jp.ibm.com>
Wed, 1 Aug 2018 04:40:32 +0000 (04:40 +0000)
commit02f79eae06895d16d5b1d2e8803d17997437d73f
tree778c06988a59c074bf3edf8854557cb402bc2e5f
parent2089e4c8f11aeff994c14a85b9de246538c8fd8d
[InstSimplify] fold extracting from std::pair (1/2)

This patch intends to enable jump threading when a method whose return type is std::pair<int, bool> or std::pair<bool, int> is inlined.
For example, jump threading does not happen for the if statement in func.

std::pair<int, bool> callee(int v) {
  int a = dummy(v);
  if (a) return std::make_pair(dummy(v), true);
  else return std::make_pair(v, v < 0);
}

int func(int v) {
  std::pair<int, bool> rc = callee(v);
  if (rc.second) {
    // do something
  }

SROA executed before the method inlining replaces std::pair by i64 without splitting in both callee and func since at this point no access to the individual fields is seen to SROA.
After inlining, jump threading fails to identify that the incoming value is a constant due to additional instructions (like or, and, trunc).

This series of patch add patterns in InstructionSimplify to fold extraction of members of std::pair. To help jump threading, actually we need to optimize the code sequence spanning multiple BBs.
These patches does not handle phi by itself, but these additional patterns help NewGVN pass, which calls instsimplify to check opportunities for simplifying instructions over phi, apply phi-of-ops optimization to result in successful jump threading.
SimplifyDemandedBits in InstCombine, can do more general optimization but this patch aims to provide opportunities for other optimizers by supporting a simple but common case in InstSimplify.

This first patch in the series handles code sequences that merges two values using shl and or and then extracts one value using lshr.

Differential Revision: https://reviews.llvm.org/D48828

llvm-svn: 338485
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/shift.ll