Skip to content

Commit a70dee7

Browse files
committed
[PPCMergeStringPool] Avoid replacing constant with instruction
String pool merging currently, for a reason that's not entirely clear to me, tries to create GEP instructions instead of GEP constant expressions when replacing constant references. It only uses constant expressions in cases where this is required. However, it does not catch all cases where such a requirement exists. For example, the landingpad catch clause has to be a constant. Fix this by always using the constant expression variant, which also makes the implementation simpler. Additionally, there are some edge cases where even replacement with a constant GEP is not legal. The one I am aware of is the llvm.eh.typeid.for intrinsic, so add a special case to forbid replacements for it. Fixes #88844.
1 parent a71565d commit a70dee7

File tree

4 files changed

+71
-45
lines changed

4 files changed

+71
-45
lines changed

llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2424
#include "llvm/IR/Constants.h"
2525
#include "llvm/IR/Instructions.h"
26+
#include "llvm/IR/IntrinsicInst.h"
2627
#include "llvm/IR/Module.h"
2728
#include "llvm/IR/ValueSymbolTable.h"
2829
#include "llvm/Pass.h"
@@ -117,9 +118,16 @@ class PPCMergeStringPool : public ModulePass {
117118
// sure that they can be replaced.
118119
static bool hasReplaceableUsers(GlobalVariable &GV) {
119120
for (User *CurrentUser : GV.users()) {
120-
// Instruction users are always valid.
121-
if (isa<Instruction>(CurrentUser))
121+
if (isa<Instruction>(CurrentUser)) {
122+
if (auto *II = dyn_cast<IntrinsicInst>(CurrentUser)) {
123+
// Some intrinsics require a plain global.
124+
if (II->getIntrinsicID() == Intrinsic::eh_typeid_for)
125+
return false;
126+
}
127+
128+
// Other instruction users are always valid.
122129
continue;
130+
}
123131

124132
// We cannot replace GlobalValue users because they are not just nodes
125133
// in IR. To replace a user like this we would need to create a new
@@ -330,38 +338,13 @@ void PPCMergeStringPool::replaceUsesWithGEP(GlobalVariable *GlobalToReplace,
330338
if (isa<GlobalValue>(CurrentUser))
331339
continue;
332340

333-
if (!UserInstruction) {
334-
// User is a constant type.
335-
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
336-
PooledStructType, GPool, Indices);
337-
UserConstant->handleOperandChange(GlobalToReplace, ConstGEP);
338-
continue;
339-
}
340-
341-
if (PHINode *UserPHI = dyn_cast<PHINode>(UserInstruction)) {
342-
// GEP instructions cannot be added before PHI nodes.
343-
// With getInBoundsGetElementPtr we create the GEP and then replace it
344-
// inline into the PHI.
345-
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
346-
PooledStructType, GPool, Indices);
347-
UserPHI->replaceUsesOfWith(GlobalToReplace, ConstGEP);
348-
continue;
349-
}
350-
// The user is a valid instruction that is not a PHINode.
351-
GetElementPtrInst *GEPInst =
352-
GetElementPtrInst::Create(PooledStructType, GPool, Indices);
353-
GEPInst->insertBefore(UserInstruction);
354-
355-
LLVM_DEBUG(dbgs() << "Inserting GEP before:\n");
356-
LLVM_DEBUG(UserInstruction->dump());
357-
341+
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
342+
PooledStructType, GPool, Indices);
358343
LLVM_DEBUG(dbgs() << "Replacing this global:\n");
359344
LLVM_DEBUG(GlobalToReplace->dump());
360345
LLVM_DEBUG(dbgs() << "with this:\n");
361-
LLVM_DEBUG(GEPInst->dump());
362-
363-
// After the GEP is inserted the GV can be replaced.
364-
CurrentUser->replaceUsesOfWith(GlobalToReplace, GEPInst);
346+
LLVM_DEBUG(ConstGEP->dump());
347+
GlobalToReplace->replaceAllUsesWith(ConstGEP);
365348
}
366349
}
367350

llvm/test/CodeGen/PowerPC/merge-string-used-by-metadata.mir

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414

1515
define noundef ptr @func1(ptr noundef nonnull align 8 dereferenceable(8) %this) #0 !dbg !6 {
1616
; 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
17+
; CHECK: ret ptr getelementptr inbounds ({ [7 x i8], [7 x i8] }, ptr @__ModuleStringPool, i32 0, i32 1), !dbg !14
1918
entry:
2019
ret ptr @const.2, !dbg !14
2120
}
2221

2322
define noundef ptr @func2(ptr noundef nonnull align 8 dereferenceable(8) %this) #0 {
2423
; 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
24+
; CHECK: ret ptr @__ModuleStringPool
2725
entry:
2826
ret ptr @const.1
2927
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc -mtriple=ppc64le-unknown-linux-gnu < %s | FileCheck %s
3+
4+
@id = private unnamed_addr constant [4 x i8] c"@id\00", align 1
5+
@id2 = private unnamed_addr constant [5 x i8] c"@id2\00", align 1
6+
7+
; Higher-aligned dummy to make sure it is first in the string pool.
8+
@dummy = private unnamed_addr constant [1 x i32] [i32 42], align 4
9+
10+
define ptr @test1() personality ptr @__gnu_objc_personality_v0 {
11+
; CHECK-LABEL: test1:
12+
; CHECK: # %bb.0:
13+
; CHECK-NEXT: mflr 0
14+
; CHECK-NEXT: stdu 1, -32(1)
15+
; CHECK-NEXT: std 0, 48(1)
16+
; CHECK-NEXT: .cfi_def_cfa_offset 32
17+
; CHECK-NEXT: .cfi_offset lr, 16
18+
; CHECK-NEXT: addis 3, 2, .L__ModuleStringPool@toc@ha
19+
; CHECK-NEXT: addi 3, 3, .L__ModuleStringPool@toc@l
20+
; CHECK-NEXT: bl foo
21+
; CHECK-NEXT: nop
22+
invoke void @foo(ptr @dummy)
23+
to label %cont unwind label %unwind
24+
25+
cont:
26+
unreachable
27+
28+
unwind:
29+
%lp = landingpad { ptr, i32 }
30+
catch ptr @id
31+
resume { ptr, i32 } %lp
32+
}
33+
34+
define i32 @test2() personality ptr @__gnu_objc_personality_v0 {
35+
; CHECK-LABEL: test2:
36+
; CHECK: # %bb.0:
37+
; CHECK-NEXT: li 3, 1
38+
; CHECK-NEXT: blr
39+
%id = tail call i32 @llvm.eh.typeid.for(ptr @id2)
40+
ret i32 %id
41+
}
42+
43+
declare i32 @__gnu_objc_personality_v0(...)
44+
45+
declare i32 @llvm.eh.typeid.for(ptr)
46+
47+
declare void @foo()

llvm/test/CodeGen/PowerPC/mergeable-string-pool-pass-only.mir

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
ret i32 %call
3636

3737
; CHECK-LABEL: test1
38-
; CHECK: %0 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 6
39-
; CHECK: tail call signext i32 @calleeStr
38+
; CHECK: %call = tail call signext i32 @calleeStr(ptr noundef nonnull getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 6))
4039
}
4140

4241
define dso_local signext i32 @test2() local_unnamed_addr #0 {
@@ -49,7 +48,7 @@
4948
ret i32 %call
5049

5150
; CHECK-LABEL: test2
52-
; CHECK: %0 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 2
51+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %A, ptr noundef nonnull align 4 dereferenceable(24) getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 2), i64 24, i1 false)
5352
; CHECK: call signext i32 @calleeInt
5453
}
5554

@@ -62,7 +61,7 @@
6261
call void @llvm.lifetime.end.p0(i64 28, ptr nonnull %A) #0
6362
ret i32 %call
6463
; CHECK-LABEL: test3
65-
; CHECK: %0 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 4
64+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(28) %A, ptr noundef nonnull align 4 dereferenceable(28) getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 4), i64 28, i1 false)
6665
; CHECK: call signext i32 @calleeFloat
6766
}
6867

@@ -75,7 +74,7 @@
7574
call void @llvm.lifetime.end.p0(i64 56, ptr nonnull %A) #0
7675
ret i32 %call
7776
; CHECK-LABEL: test4
78-
; CHECK: %0 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 0
77+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(56) %A, ptr noundef nonnull align 8 dereferenceable(56) @__ModuleStringPool, i64 56, i1 false)
7978
; CHECK: call signext i32 @calleeDouble
8079
}
8180

@@ -102,11 +101,10 @@
102101
call void @llvm.lifetime.end.p0(i64 24, ptr nonnull %B) #0
103102
ret i32 %add7
104103
; CHECK-LABEL: test5
105-
; CHECK: %0 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 3
106-
; CHECK: %1 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 5
107-
; CHECK: %2 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 1
108-
; CHECK: %3 = getelementptr { [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 7
109-
; CHECK: call signext i32 @calleeStr
104+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(24) %B, ptr noundef nonnull align 4 dereferenceable(24) getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 3), i64 24, i1 false)
105+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(28) %C, ptr noundef nonnull align 4 dereferenceable(28) getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 5), i64 28, i1 false)
106+
; CHECK: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(56) %D, ptr noundef nonnull align 8 dereferenceable(56) getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 1), i64 56, i1 false)
107+
; CHECK: call signext i32 @calleeStr(ptr noundef nonnull getelementptr inbounds ({ [7 x double], [7 x double], [6 x i32], [6 x i32], [7 x float], [7 x float], [8 x i8], [16 x i8] }, ptr @__ModuleStringPool, i32 0, i32 7))
110108
; CHECK: call signext i32 @calleeInt
111109
; CHECK: call signext i32 @calleeFloat
112110
; CHECK: call signext i32 @calleeDouble

0 commit comments

Comments
 (0)