[llvm] r238452 - [InstCombine] Fold IntToPtr and PtrToInt into preceding loads.

Quentin Colombet via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 11 14:35:15 PST 2016


Re-applied as Committed revision 260612.
> On Feb 9, 2016, at 11:34 AM, Quentin Colombet <qcolombet at apple.com> wrote:
> 
> Actually, that was the only problem.
> A clean build of clang and LTO with a hacky fix fixes the stage2 build even with that patch in.
> 
> Anyhow, I think we need to wait for this PR to be fixed before reapplying this patch.
> Note, the reason why it was not failing before that patch is because the basic alias analysis was able to correctly answer in the cases we were interested, because it found the underlying object through the bitcasts. Now, with the ptrtoint or inttoptr in the way, the basic alias analysis failed to find a definite answer and relied on TBAA to give the right answer. The TBAA information were wrong all the time (the involved files are regexec.c and Regex.cpp).
> 
> Cheers,
> -Quentin
> 
>> On Feb 9, 2016, at 9:59 AM, Quentin Colombet <qcolombet at apple.com <mailto:qcolombet at apple.com>> wrote:
>> 
>> Part of the problem is https://llvm.org/bugs/show_bug.cgi?id=26550 <https://llvm.org/bugs/show_bug.cgi?id=26550>.
>> It seems there are other problems as well, I am still investigating.
>> 
>> Cheers,
>> Q.
>>> On Feb 3, 2016, at 10:33 AM, Quentin Colombet <qcolombet at apple.com <mailto:qcolombet at apple.com>> wrote:
>>> 
>>> For the record, I think it has to do with type based alias analysis.
>>> 
>>> Q.
>>>> On Feb 3, 2016, at 10:15 AM, Quentin Colombet via llvm-commits <llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>> wrote:
>>>> 
>>>> +llvm-commit
>>>>> On Feb 3, 2016, at 10:14 AM, Quentin Colombet <qcolombet at apple.com <mailto:qcolombet at apple.com>> wrote:
>>>>> 
>>>>> Hi David,
>>>>> 
>>>>> Sorry to bring that commit up, but I’ve reverted it in r259674.
>>>>> According to git bisect, this is the root cause of a miscompile in Regex.
>>>>> I’ve put the detailed on how to reproduce in the commit message of r259674.
>>>>> 
>>>>> I am working on reducing the test case (which right now is a bootstrap, then build llvm-extract with lto).
>>>>> 
>>>>> I am guessing that the bug is not actually in this commit but exposes more opportunities to another optimization to go wrong.
>>>>> 
>>>>> Anyhow, I’ve reverted it, because we have some internal bots that hit this now.
>>>>> 
>>>>> Thanks,
>>>>> -Quentin
>>>>> 
>>>>>> On May 28, 2015, at 11:52 AM, David Majnemer <david.majnemer at gmail.com <mailto:david.majnemer at gmail.com>> wrote:
>>>>>> 
>>>>>> Patch by Philip Pfaffe!
>>>>>> 
>>>>>> On Thu, May 28, 2015 at 11:39 AM, David Majnemer <david.majnemer at gmail.com <mailto:david.majnemer at gmail.com>> wrote:
>>>>>> Author: majnemer
>>>>>> Date: Thu May 28 13:39:17 2015
>>>>>> New Revision: 238452
>>>>>> 
>>>>>> URL: http://llvm.org/viewvc/llvm-project?rev=238452&view=rev <https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D238452-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=olNjq2h6jOqNEfp-gkn0gkdSmC_Cq2PIHP67QCufkSY&s=sY9wqRVuCtMcwCgMuzhFlmNBzaeMHGzxurg-FS2sE_w&e=>
>>>>>> Log:
>>>>>> [InstCombine] Fold IntToPtr and PtrToInt into preceding loads.
>>>>>> 
>>>>>> Currently we only fold a BitCast into a Load when the BitCast is its
>>>>>> only user.
>>>>>> 
>>>>>> Do the same for any no-op cast.
>>>>>> 
>>>>>> Differential Revision: http://reviews.llvm.org/D9152 <https://urldefense.proofpoint.com/v2/url?u=http-3A__reviews.llvm.org_D9152&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=olNjq2h6jOqNEfp-gkn0gkdSmC_Cq2PIHP67QCufkSY&s=0ErPMlQC5TI1p-lxa0ZTYmfyih7fQVsAo9kgeBsBiLo&e=>
>>>>>> 
>>>>>> Added:
>>>>>>     llvm/trunk/test/Transforms/InstCombine/load-bitcast32.ll
>>>>>>     llvm/trunk/test/Transforms/InstCombine/load-bitcast64.ll
>>>>>> Modified:
>>>>>>     llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
>>>>>> 
>>>>>> Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
>>>>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp?rev=238452&r1=238451&r2=238452&view=diff <https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_lib_Transforms_InstCombine_InstCombineLoadStoreAlloca.cpp-3Frev-3D238452-26r1-3D238451-26r2-3D238452-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=olNjq2h6jOqNEfp-gkn0gkdSmC_Cq2PIHP67QCufkSY&s=QM30wzxtVasww-Km95KLdBoCNDt_25E5FeFDPrMWQp4&e=>
>>>>>> ==============================================================================
>>>>>> --- llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp (original)
>>>>>> +++ llvm/trunk/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp Thu May 28 13:39:17 2015
>>>>>> @@ -483,12 +483,17 @@ static Instruction *combineLoadToOperati
>>>>>>    }
>>>>>> 
>>>>>>    // Fold away bit casts of the loaded value by loading the desired type.
>>>>>> +  // We can do this for BitCastInsts as well as casts from and to pointer types,
>>>>>> +  // as long as those are noops (i.e., the source or dest type have the same
>>>>>> +  // bitwidth as the target's pointers).
>>>>>>    if (LI.hasOneUse())
>>>>>> -    if (auto *BC = dyn_cast<BitCastInst>(LI.user_back())) {
>>>>>> -      LoadInst *NewLoad = combineLoadToNewType(IC, LI, BC->getDestTy());
>>>>>> -      BC->replaceAllUsesWith(NewLoad);
>>>>>> -      IC.EraseInstFromFunction(*BC);
>>>>>> -      return &LI;
>>>>>> +    if (auto* CI = dyn_cast<CastInst>(LI.user_back())) {
>>>>>> +      if (CI->isNoopCast(DL)) {
>>>>>> +        LoadInst *NewLoad = combineLoadToNewType(IC, LI, CI->getDestTy());
>>>>>> +        CI->replaceAllUsesWith(NewLoad);
>>>>>> +        IC.EraseInstFromFunction(*CI);
>>>>>> +        return &LI;
>>>>>> +      }
>>>>>>      }
>>>>>> 
>>>>>>    // FIXME: We should also canonicalize loads of vectors when their elements are
>>>>>> 
>>>>>> Added: llvm/trunk/test/Transforms/InstCombine/load-bitcast32.ll
>>>>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/load-bitcast32.ll?rev=238452&view=auto <https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_test_Transforms_InstCombine_load-2Dbitcast32.ll-3Frev-3D238452-26view-3Dauto&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=olNjq2h6jOqNEfp-gkn0gkdSmC_Cq2PIHP67QCufkSY&s=NrPn-jA9V0ieNNsVOi8wYa31xU_WEeNqb6Qk8C4cnJA&e=>
>>>>>> ==============================================================================
>>>>>> --- llvm/trunk/test/Transforms/InstCombine/load-bitcast32.ll (added)
>>>>>> +++ llvm/trunk/test/Transforms/InstCombine/load-bitcast32.ll Thu May 28 13:39:17 2015
>>>>>> @@ -0,0 +1,79 @@
>>>>>> +; RUN: opt -instcombine -S < %s | FileCheck %s
>>>>>> +
>>>>>> +target datalayout = "p:32:32:32"
>>>>>> +
>>>>>> +
>>>>>> +define i64* @test1(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test1(
>>>>>> +; CHECK: load i64, i64*
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i64*
>>>>>> +  %b = load i64, i64* %a
>>>>>> +  %c = inttoptr i64 %b to i64*
>>>>>> +
>>>>>> +  ret i64* %c
>>>>>> +}
>>>>>> +
>>>>>> +define i32* @test2(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test2(
>>>>>> +; CHECK: load i32*, i32**
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32*
>>>>>> +  %b = load i32, i32* %a
>>>>>> +  %c = inttoptr i32 %b to i32*
>>>>>> +
>>>>>> +  ret i32* %c
>>>>>> +}
>>>>>> +
>>>>>> +define i64* @test3(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test3(
>>>>>> +; CHECK: load i64*, i64**
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32*
>>>>>> +  %b = load i32, i32* %a
>>>>>> +  %c = inttoptr i32 %b to i64*
>>>>>> +
>>>>>> +  ret i64* %c
>>>>>> +}
>>>>>> +
>>>>>> +define i64 @test4(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test4(
>>>>>> +; CHECK: load i32, i32*
>>>>>> +; CHECK: zext
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i64**
>>>>>> +  %b = load i64*, i64** %a
>>>>>> +  %c = ptrtoint i64* %b to i64
>>>>>> +
>>>>>> +  ret i64 %c
>>>>>> +}
>>>>>> +
>>>>>> +define i32 @test5(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test5(
>>>>>> +; CHECK: load i32, i32*
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32**
>>>>>> +  %b = load i32*, i32** %a
>>>>>> +  %c = ptrtoint i32* %b to i32
>>>>>> +
>>>>>> +  ret i32 %c
>>>>>> +}
>>>>>> +
>>>>>> +define i64 @test6(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test6(
>>>>>> +; CHECK: load i32, i32*
>>>>>> +; CHECK: zext
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32**
>>>>>> +  %b = load i32*, i32** %a
>>>>>> +  %c = ptrtoint i32* %b to i64
>>>>>> +
>>>>>> +  ret i64 %c
>>>>>> +}
>>>>>> +
>>>>>> 
>>>>>> Added: llvm/trunk/test/Transforms/InstCombine/load-bitcast64.ll
>>>>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/load-bitcast64.ll?rev=238452&view=auto <https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_test_Transforms_InstCombine_load-2Dbitcast64.ll-3Frev-3D238452-26view-3Dauto&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=olNjq2h6jOqNEfp-gkn0gkdSmC_Cq2PIHP67QCufkSY&s=Ct7qAC1oyddLnff8X5WXL8bZlU4kgWSD4P7ZT45sFt8&e=>
>>>>>> ==============================================================================
>>>>>> --- llvm/trunk/test/Transforms/InstCombine/load-bitcast64.ll (added)
>>>>>> +++ llvm/trunk/test/Transforms/InstCombine/load-bitcast64.ll Thu May 28 13:39:17 2015
>>>>>> @@ -0,0 +1,78 @@
>>>>>> +; RUN: opt -instcombine -S < %s | FileCheck %s
>>>>>> +
>>>>>> +target datalayout = "p:64:64:64"
>>>>>> +
>>>>>> +
>>>>>> +define i64* @test1(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test1(
>>>>>> +; CHECK: load i64*, i64**
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i64*
>>>>>> +  %b = load i64, i64* %a
>>>>>> +  %c = inttoptr i64 %b to i64*
>>>>>> +
>>>>>> +  ret i64* %c
>>>>>> +}
>>>>>> +
>>>>>> +define i32* @test2(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test2(
>>>>>> +; CHECK: load i32, i32*
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32*
>>>>>> +  %b = load i32, i32* %a
>>>>>> +  %c = inttoptr i32 %b to i32*
>>>>>> +
>>>>>> +  ret i32* %c
>>>>>> +}
>>>>>> +
>>>>>> +define i64* @test3(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test3(
>>>>>> +; CHECK: load i32, i32*
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32*
>>>>>> +  %b = load i32, i32* %a
>>>>>> +  %c = inttoptr i32 %b to i64*
>>>>>> +
>>>>>> +  ret i64* %c
>>>>>> +}
>>>>>> +
>>>>>> +define i64 @test4(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test4(
>>>>>> +; CHECK: load i64, i64*
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i64**
>>>>>> +  %b = load i64*, i64** %a
>>>>>> +  %c = ptrtoint i64* %b to i64
>>>>>> +
>>>>>> +  ret i64 %c
>>>>>> +}
>>>>>> +
>>>>>> +define i32 @test5(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test5(
>>>>>> +; CHECK: load i64, i64*
>>>>>> +; CHECK: trunc
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32**
>>>>>> +  %b = load i32*, i32** %a
>>>>>> +  %c = ptrtoint i32* %b to i32
>>>>>> +
>>>>>> +  ret i32 %c
>>>>>> +}
>>>>>> +
>>>>>> +define i64 @test6(i8* %x) {
>>>>>> +entry:
>>>>>> +; CHECK-LABEL: @test6(
>>>>>> +; CHECK: load i64, i64*
>>>>>> +; CHECK: ret
>>>>>> +  %a = bitcast i8* %x to i32**
>>>>>> +  %b = load i32*, i32** %a
>>>>>> +  %c = ptrtoint i32* %b to i64
>>>>>> +
>>>>>> +  ret i64 %c
>>>>>> +}
>>>>>> +
>>>>>> 
>>>>>> 
>>>>>> _______________________________________________
>>>>>> llvm-commits mailing list
>>>>>> llvm-commits at cs.uiuc.edu <mailto:llvm-commits at cs.uiuc.edu>
>>>>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits <http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>
>>>>>> 
>>>>>> _______________________________________________
>>>>>> llvm-commits mailing list
>>>>>> llvm-commits at cs.uiuc.edu <mailto:llvm-commits at cs.uiuc.edu>
>>>>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits <http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>
>>>>> 
>>>> 
>>>> _______________________________________________
>>>> llvm-commits mailing list
>>>> llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>
>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits>
>>> 
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160211/18cba5b4/attachment.html>


More information about the llvm-commits mailing list