Page MenuHomePhabricator

[InstCombine] Stop folding inttoptr+bitcast if multiple uses
Needs RevisionPublic

Authored by ruiling on Mar 21 2021, 11:07 PM.

Details

Summary

Don't combine a inttoptr followed by a bitcast to another pointer type if
the intermediate pointer has multiple uses. Such combine is very unfriendly
to later passes like ScalarEvolutionAnalysis and LoadStoreVectorizer. For
example, this may change the IR from:

%p1 = inttoptr %addr to i32 *
%i  = load i32, i32 * %p1

==> %p2 = bitcast i32 * %p1 to float *

%p3 = getelementptr float, float * %p2, i64 1
%f  = load float, float * %p3

into:

%p1 = inttoptr %addr to i32 *

==> %p2 = inttoptr %addr to float *

%i  = load i32, i32 * %p1
%p3 = getelementptr float, float * %p2, i64 1
%f  = load float, float * %p3

This causes above mentioned passes fail to reason that the two pointers
are consecutive, thus fail to vectorize the two loads.

Diff Detail

Unit TestsFailed

TimeTest
60 msx64 debian > Clang.CodeGen::arm64_32-vaarg.c
Script: -- : 'RUN: at line 1'; /mnt/disks/ssd0/agent/llvm-project/build/bin/clang -cc1 -internal-isystem /mnt/disks/ssd0/agent/llvm-project/build/lib/clang/13.0.0/include -nostdsysteminc -triple arm64_32-apple-ios7.0 -target-abi darwinpcs -emit-llvm -o - -O1 -ffreestanding /mnt/disks/ssd0/agent/llvm-project/clang/test/CodeGen/arm64_32-vaarg.c | /mnt/disks/ssd0/agent/llvm-project/build/bin/FileCheck /mnt/disks/ssd0/agent/llvm-project/clang/test/CodeGen/arm64_32-vaarg.c
130 msx64 windows > Clang.CodeGen::arm64_32-vaarg.c
Script: -- : 'RUN: at line 1'; c:\ws\w16-1\llvm-project\premerge-checks\build\bin\clang.exe -cc1 -internal-isystem c:\ws\w16-1\llvm-project\premerge-checks\build\lib\clang\13.0.0\include -nostdsysteminc -triple arm64_32-apple-ios7.0 -target-abi darwinpcs -emit-llvm -o - -O1 -ffreestanding C:\ws\w16-1\llvm-project\premerge-checks\clang\test\CodeGen\arm64_32-vaarg.c | c:\ws\w16-1\llvm-project\premerge-checks\build\bin\filecheck.exe C:\ws\w16-1\llvm-project\premerge-checks\clang\test\CodeGen\arm64_32-vaarg.c

Event Timeline

ruiling created this revision.Mar 21 2021, 11:07 PM
ruiling requested review of this revision.Mar 21 2021, 11:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 21 2021, 11:07 PM
arsenm added inline comments.Mar 22 2021, 11:27 AM
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
279

Remove the spaces between the type and * throughout the comment

295–296

This feels like the wrong place. This is isEliminableCastPair, and the cast can be eliminated. Can the hasOneUse check go to some specific combine?

i agree that it makes sense, but this is only a partial solution.
I'm looking at the full solution, not sure what fallout it will have yet.

This revision now requires changes to proceed.Mar 22 2021, 3:18 PM
foad added a subscriber: foad.Apr 7 2021, 1:55 AM
Matt added a subscriber: Matt.May 12 2021, 8:41 AM