Skip to content

Commit e278c67

Browse files
authored
Add support to meger strings used by metadata (#77364)
Currently if the merged string is used by metadata, its metadata uses are not replaced if the string is merged. This is to add code support for the metadata use replacement.
1 parent afa52de commit e278c67

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ bool PPCMergeStringPool::mergeModuleStringPool(Module &M) {
268268
// before every use in order to compute this offset.
269269
replaceUsesWithGEP(GV, PooledGlobal, ElementIndex);
270270

271+
// Replace all the uses by metadata.
272+
if (GV->isUsedByMetadata()) {
273+
Constant *Indices[2] = {
274+
ConstantInt::get(Type::getInt32Ty(*Context), 0),
275+
ConstantInt::get(Type::getInt32Ty(*Context), ElementIndex)};
276+
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
277+
PooledStructType, PooledGlobal, Indices);
278+
ValueAsMetadata::handleRAUW(GV, ConstGEP);
279+
}
280+
assert(!GV->isUsedByMetadata() && "Should be no metadata use anymore");
281+
271282
// This GV has no more uses so we can erase it.
272283
if (GV->use_empty())
273284
GV->eraseFromParent();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# RUN: llc -run-pass=ppc-merge-strings -mcpu=pwr8 -mtriple powerpc64le-unknown-linux-gnu \
2+
# RUN: -verify-machineinstrs -o - %s | FileCheck %s
3+
# RUN: llc -run-pass=ppc-merge-strings -mcpu=pwr8 -mtriple powerpc64-ibm-aix-xcoff \
4+
# RUN: -verify-machineinstrs -o - %s | FileCheck %s
5+
6+
--- |
7+
; Constants list.
8+
@const.1 = internal constant [7 x i8] c"const1\00", align 1
9+
@const.2 = internal constant [7 x i8] c"const2\00", align 1
10+
11+
;;
12+
;; Start of test code.
13+
;;
14+
15+
define noundef ptr @func1(ptr noundef nonnull align 8 dereferenceable(8) %this) #0 !dbg !6 {
16+
; CHECK-LABEL: func1
17+
; CHECK: %0 = getelementptr { [7 x i8], [7 x i8] }, ptr @__ModuleStringPool, i32 0, i32 1
18+
; CHECK-NEXT: ret ptr %0, !dbg !14
19+
entry:
20+
ret ptr @const.2, !dbg !14
21+
}
22+
23+
define noundef ptr @func2(ptr noundef nonnull align 8 dereferenceable(8) %this) #0 {
24+
; CHECK-LABEL: func2
25+
; CHECK: %0 = getelementptr { [7 x i8], [7 x i8] }, ptr @__ModuleStringPool, i32 0, i32 0
26+
; CHECK-NEXT: ret ptr %0
27+
entry:
28+
ret ptr @const.1
29+
}
30+
31+
attributes #0 = { noinline nounwind "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="pwr8" "target-features"="+altivec,+bpermd,+crbits,+crypto,+direct-move,+extdiv,+htm,+isa-v206-instructions,+isa-v207-instructions,+power8-vector,+quadword-atomics,+vsx,-aix-small-local-exec-tls,-isa-v30-instructions,-power9-vector,-privileged,-rop-protect,-spe" }
32+
33+
!llvm.dbg.cu = !{!0}
34+
!llvm.module.flags = !{!3, !4}
35+
!llvm.ident = !{!5}
36+
37+
; CHECK: !10 = !DITemplateValueParameter(name: "ID", type: !11, value: ptr getelementptr inbounds ({ [7 x i8], [7 x i8] }, ptr @__ModuleStringPool, i32 0, i32 1))
38+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
39+
!1 = !DIFile(filename: "constant-pointers.cpp", directory: "/tmp/dbginfo")
40+
!2 = !{}
41+
!3 = !{i32 7, !"Dwarf Version", i32 3}
42+
!4 = !{i32 2, !"Debug Info Version", i32 3}
43+
!5 = !{!"clang version 17.0.0"}
44+
!6 = distinct !DISubprogram(name: "getId", linkageName: "func1", scope: !1, file: !1, line: 2, type: !7, scopeLine: 2, virtualIndex: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, templateParams: !9, retainedNodes: !2)
45+
!7 = !DISubroutineType(types: !8)
46+
!8 = !{null}
47+
!9 = !{!10}
48+
!10 = !DITemplateValueParameter(name: "ID", type: !11, value: ptr @const.2)
49+
!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64)
50+
!12 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !13)
51+
!13 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_unsigned_char)
52+
!14 = !DILocation(line: 3, scope: !6)
53+
54+
...

0 commit comments

Comments
 (0)