Skip to content

Commit 1184a9c

Browse files
nikictstellar
authored andcommitted
[PPCMergeStringPool] Avoid replacing constant with instruction (#88846)
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. (cherry picked from commit 3a3aeb8)
1 parent f1491c7 commit 1184a9c

File tree

5 files changed

+87
-63
lines changed

5 files changed

+87
-63
lines changed

llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp

+18-39
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"
@@ -116,9 +117,20 @@ class PPCMergeStringPool : public ModulePass {
116117
// sure that they can be replaced.
117118
static bool hasReplaceableUsers(GlobalVariable &GV) {
118119
for (User *CurrentUser : GV.users()) {
119-
// Instruction users are always valid.
120-
if (isa<Instruction>(CurrentUser))
120+
if (auto *I = dyn_cast<Instruction>(CurrentUser)) {
121+
// Do not merge globals in exception pads.
122+
if (I->isEHPad())
123+
return false;
124+
125+
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
126+
// Some intrinsics require a plain global.
127+
if (II->getIntrinsicID() == Intrinsic::eh_typeid_for)
128+
return false;
129+
}
130+
131+
// Other instruction users are always valid.
121132
continue;
133+
}
122134

123135
// We cannot replace GlobalValue users because they are not just nodes
124136
// in IR. To replace a user like this we would need to create a new
@@ -302,14 +314,6 @@ void PPCMergeStringPool::replaceUsesWithGEP(GlobalVariable *GlobalToReplace,
302314
Users.push_back(CurrentUser);
303315

304316
for (User *CurrentUser : Users) {
305-
Instruction *UserInstruction = dyn_cast<Instruction>(CurrentUser);
306-
Constant *UserConstant = dyn_cast<Constant>(CurrentUser);
307-
308-
// At this point we expect that the user is either an instruction or a
309-
// constant.
310-
assert((UserConstant || UserInstruction) &&
311-
"Expected the user to be an instruction or a constant.");
312-
313317
// The user was not found so it must have been replaced earlier.
314318
if (!userHasOperand(CurrentUser, GlobalToReplace))
315319
continue;
@@ -318,38 +322,13 @@ void PPCMergeStringPool::replaceUsesWithGEP(GlobalVariable *GlobalToReplace,
318322
if (isa<GlobalValue>(CurrentUser))
319323
continue;
320324

321-
if (!UserInstruction) {
322-
// User is a constant type.
323-
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
324-
PooledStructType, GPool, Indices);
325-
UserConstant->handleOperandChange(GlobalToReplace, ConstGEP);
326-
continue;
327-
}
328-
329-
if (PHINode *UserPHI = dyn_cast<PHINode>(UserInstruction)) {
330-
// GEP instructions cannot be added before PHI nodes.
331-
// With getInBoundsGetElementPtr we create the GEP and then replace it
332-
// inline into the PHI.
333-
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
334-
PooledStructType, GPool, Indices);
335-
UserPHI->replaceUsesOfWith(GlobalToReplace, ConstGEP);
336-
continue;
337-
}
338-
// The user is a valid instruction that is not a PHINode.
339-
GetElementPtrInst *GEPInst =
340-
GetElementPtrInst::Create(PooledStructType, GPool, Indices);
341-
GEPInst->insertBefore(UserInstruction);
342-
343-
LLVM_DEBUG(dbgs() << "Inserting GEP before:\n");
344-
LLVM_DEBUG(UserInstruction->dump());
345-
325+
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr(
326+
PooledStructType, GPool, Indices);
346327
LLVM_DEBUG(dbgs() << "Replacing this global:\n");
347328
LLVM_DEBUG(GlobalToReplace->dump());
348329
LLVM_DEBUG(dbgs() << "with this:\n");
349-
LLVM_DEBUG(GEPInst->dump());
350-
351-
// After the GEP is inserted the GV can be replaced.
352-
CurrentUser->replaceUsesOfWith(GlobalToReplace, GEPInst);
330+
LLVM_DEBUG(ConstGEP->dump());
331+
GlobalToReplace->replaceAllUsesWith(ConstGEP);
353332
}
354333
}
355334

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-large.ll

+7-7
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,16 @@ define dso_local signext i32 @array0() local_unnamed_addr #0 {
319319
; AIX32-NEXT: mflr r0
320320
; AIX32-NEXT: stwu r1, -96(r1)
321321
; AIX32-NEXT: lis r6, 0
322-
; AIX32-NEXT: lwz r4, L..C0(r2) # @__ModuleStringPool
323-
; AIX32-NEXT: li r5, 12
322+
; AIX32-NEXT: lwz r5, L..C0(r2) # @__ModuleStringPool
323+
; AIX32-NEXT: li r4, 12
324324
; AIX32-NEXT: addi r3, r1, 64
325325
; AIX32-NEXT: stw r0, 104(r1)
326326
; AIX32-NEXT: ori r7, r6, 35596
327-
; AIX32-NEXT: rlwimi r5, r3, 0, 30, 27
328-
; AIX32-NEXT: lxvw4x vs0, r4, r7
329-
; AIX32-NEXT: stxvw4x vs0, 0, r5
330-
; AIX32-NEXT: ori r5, r6, 35584
331-
; AIX32-NEXT: lxvw4x vs0, r4, r5
327+
; AIX32-NEXT: rlwimi r4, r3, 0, 30, 27
328+
; AIX32-NEXT: lxvw4x vs0, r5, r7
329+
; AIX32-NEXT: stxvw4x vs0, 0, r4
330+
; AIX32-NEXT: ori r4, r6, 35584
331+
; AIX32-NEXT: lxvw4x vs0, r5, r4
332332
; AIX32-NEXT: stxvw4x vs0, 0, r3
333333
; AIX32-NEXT: bl .calleeInt[PR]
334334
; AIX32-NEXT: nop

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

+8-10
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

llvm/test/CodeGen/PowerPC/mergeable-string-pool.ll

+7-7
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,16 @@ define dso_local signext i32 @array1() local_unnamed_addr #0 {
398398
; AIX32: # %bb.0: # %entry
399399
; AIX32-NEXT: mflr r0
400400
; AIX32-NEXT: stwu r1, -96(r1)
401-
; AIX32-NEXT: lwz r4, L..C0(r2) # @__ModuleStringPool
401+
; AIX32-NEXT: lwz r5, L..C0(r2) # @__ModuleStringPool
402402
; AIX32-NEXT: li r6, 372
403-
; AIX32-NEXT: li r5, 12
403+
; AIX32-NEXT: li r4, 12
404404
; AIX32-NEXT: addi r3, r1, 64
405405
; AIX32-NEXT: stw r0, 104(r1)
406-
; AIX32-NEXT: rlwimi r5, r3, 0, 30, 27
407-
; AIX32-NEXT: lxvw4x vs0, r4, r6
408-
; AIX32-NEXT: stxvw4x vs0, 0, r5
409-
; AIX32-NEXT: li r5, 360
410-
; AIX32-NEXT: lxvw4x vs0, r4, r5
406+
; AIX32-NEXT: rlwimi r4, r3, 0, 30, 27
407+
; AIX32-NEXT: lxvw4x vs0, r5, r6
408+
; AIX32-NEXT: stxvw4x vs0, 0, r4
409+
; AIX32-NEXT: li r4, 360
410+
; AIX32-NEXT: lxvw4x vs0, r5, r4
411411
; AIX32-NEXT: stxvw4x vs0, 0, r3
412412
; AIX32-NEXT: bl .calleeInt[PR]
413413
; AIX32-NEXT: nop

0 commit comments

Comments
 (0)