[InstCombine] fold zexts and constants into a phi (PR24766)
authorSanjay Patel <spatel@rotateright.com>
Sun, 27 Sep 2015 20:34:31 +0000 (20:34 +0000)
committerSanjay Patel <spatel@rotateright.com>
Sun, 27 Sep 2015 20:34:31 +0000 (20:34 +0000)
commitfa2392de5f49336bcc93bc2f45f7ab3c3b5b54f0
treed4392751c02e4a9ea08be9527be276bd5531c612
parent8a43b3fad98540d6a25e85eccee11ae2d82733f3
[InstCombine] fold zexts and constants into a phi (PR24766)

This is one step towards solving PR24766:
https://llvm.org/bugs/show_bug.cgi?id=24766

We were not producing the same IR for these two C functions because the store
to the temp bool causes extra zexts:

#include <stdbool.h>

bool switchy(char x1, char x2, char condition) {
   bool conditionMet = false;
   switch (condition) {
   case 0: conditionMet = (x1 == x2); break;
   case 1: conditionMet = (x1 <= x2); break;
   }
   return conditionMet;
}

bool switchy2(char x1, char x2, char condition) {
   switch (condition) {
   case 0: return (x1 == x2);
   case 1: return (x1 <= x2);
   }
  return false;
}

As noted in the code comments, this test case manages to avoid the more general existing
phi optimizations where there are only 2 phi inputs or where there are no constant phi
args mixed in with the casts ops. It seems like a corner case, but if we don't catch it,
then I don't think we can get SimplifyCFG to further optimize towards the canonical form
for this function shown in the bug report.

Differential Revision: http://reviews.llvm.org/D12866

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@248689 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineInternal.h
lib/Transforms/InstCombine/InstCombinePHI.cpp
test/Transforms/InstCombine/phi.ll