Skip to content

[PowerPC] Do not string pool globals that are part of llvm used. #66848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ static bool hasReplaceableUsers(GlobalVariable &GV) {
// valid candidates to be merged into the string pool. Valid candidates will
// be added to MergeableStrings.
void PPCMergeStringPool::collectCandidateConstants(Module &M) {
SmallVector<GlobalValue *, 4> UsedV;
collectUsedGlobalVariables(M, UsedV, /*CompilerUsed=*/false);
SmallVector<GlobalValue *, 4> UsedVCompiler;
collectUsedGlobalVariables(M, UsedVCompiler, /*CompilerUsed=*/true);
// Combine all of the Global Variables marked as used into a SmallPtrSet for
// faster lookup inside the loop.
SmallPtrSet<GlobalValue *, 8> AllUsedGlobals;
AllUsedGlobals.insert(UsedV.begin(), UsedV.end());
AllUsedGlobals.insert(UsedVCompiler.begin(), UsedVCompiler.end());

for (GlobalVariable &Global : M.globals()) {
LLVM_DEBUG(dbgs() << "Looking at global:");
LLVM_DEBUG(Global.dump());
Expand Down Expand Up @@ -171,6 +181,10 @@ void PPCMergeStringPool::collectCandidateConstants(Module &M) {
if (!ConstData)
continue;

// Do not pool globals that are part of llvm.used or llvm.compiler.end.
if (AllUsedGlobals.contains(&Global))
continue;

if (!hasReplaceableUsers(Global))
continue;

Expand Down
57 changes: 57 additions & 0 deletions llvm/test/CodeGen/PowerPC/aix-xcoff-used-with-stringpool.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
;; Test that the string pooling pass does not pool globals that are
;; in llvm.used or in llvm.compiler.used.

; RUN: llc -verify-machineinstrs -mcpu=pwr8 -mtriple powerpc-ibm-aix-xcoff -data-sections=false < %s | \
; RUN: FileCheck %s

; RUN: llc -verify-machineinstrs -mcpu=pwr8 -mtriple powerpc64-ibm-aix-xcoff -data-sections=false < %s | \
; RUN: FileCheck %s

@keep_this = internal constant [5 x i8] c"keep1", align 1
@keep_this2 = internal constant [5 x i8] c"keep2", align 1
@.str.1 = private unnamed_addr constant [12 x i8] c"str1_STRING\00", align 1
@.str.2 = private unnamed_addr constant [12 x i8] c"str2_STRING\00", align 1
@.str.3 = private unnamed_addr constant [12 x i8] c"str3_STRING\00", align 1
@llvm.used = appending global [1 x ptr] [ptr @keep_this], section "llvm.metadata"
@llvm.compiler.used = appending global [1 x ptr] [ptr @keep_this2], section "llvm.metadata"

declare signext i32 @callee(ptr noundef)

define dso_local signext i32 @keep1() {
entry:
%call = tail call signext i32 @callee(ptr noundef nonnull @keep_this)
ret i32 %call
}

define dso_local signext i32 @keep2() {
entry:
%call = tail call signext i32 @callee(ptr noundef nonnull @keep_this2)
ret i32 %call
}

define dso_local signext i32 @str1() {
entry:
%call = tail call signext i32 @callee(ptr noundef nonnull @.str.1)
ret i32 %call
}

define dso_local signext i32 @str2() {
entry:
%call = tail call signext i32 @callee(ptr noundef nonnull @.str.2)
ret i32 %call
}

define dso_local signext i32 @str3() {
entry:
%call = tail call signext i32 @callee(ptr noundef nonnull @.str.3)
ret i32 %call
}

; CHECK: .lglobl keep_this
; CHECK: keep_this:
; CHECK: .lglobl keep_this2
; CHECK: keep_this2:
; CHECK: L..__ModuleStringPool:
; CHECK: .string "str1_STRING"
; CHECK: .string "str2_STRING"
; CHECK: .string "str3_STRING"