[InstCombine] Resubmit the combine of A->B->A BitCast and fix for pr27996
authorGuozhi Wei <carrot@google.com>
Tue, 25 Oct 2016 20:43:42 +0000 (20:43 +0000)
committerGuozhi Wei <carrot@google.com>
Tue, 25 Oct 2016 20:43:42 +0000 (20:43 +0000)
commitae541f6a71ebffc96411fc6ce7437ad1cd03181c
treea39b9b89758a8da7c59c13ef8c86ce239b71c252
parentb3f709e10f37225ae65c1d48c4623f6abc2cac1e
[InstCombine] Resubmit the combine of A->B->A BitCast and fix for pr27996

The original patch of the A->B->A BitCast optimization was reverted by r274094 because it may cause infinite loop inside compiler https://llvm.org/bugs/show_bug.cgi?id=27996.

The problem is with following code

xB = load (type B);
xA = load (type A);
+yA = (A)xB; B -> A
+zAn = PHI[yA, xA]; PHI
+zBn = (B)zAn; // A -> B
store zAn;
store zBn;

optimizeBitCastFromPhi generates

+zBn = (B)zAn; // A -> B

and expects it will be combined with the following store instruction to another

store zAn

Unfortunately before combineStoreToValueType is called on the store instruction, optimizeBitCastFromPhi is called on the new BitCast again, and this pattern repeats indefinitely.

optimizeBitCastFromPhi only generates BitCast for load/store instructions, only the BitCast before store can cause the reexecution of optimizeBitCastFromPhi, and BitCast before store can easily be handled by InstCombineLoadStoreAlloca.cpp. So the solution to the problem is if all users of a CI are store instructions, we should not do optimizeBitCastFromPhi on it. Then optimizeBitCastFromPhi will not be called on the new BitCast instructions.

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

llvm-svn: 285116
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/pr25342.ll [new file with mode: 0644]
llvm/test/Transforms/InstCombine/pr27703.ll [new file with mode: 0644]
llvm/test/Transforms/InstCombine/pr27996.ll [new file with mode: 0644]