-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[PPCMergeStringPool] Avoid replacing constant with instruction #88846
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" | ||
#include "llvm/IR/Constants.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/IntrinsicInst.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/ValueSymbolTable.h" | ||
#include "llvm/Pass.h" | ||
|
@@ -117,9 +118,20 @@ class PPCMergeStringPool : public ModulePass { | |
// sure that they can be replaced. | ||
static bool hasReplaceableUsers(GlobalVariable &GV) { | ||
for (User *CurrentUser : GV.users()) { | ||
// Instruction users are always valid. | ||
if (isa<Instruction>(CurrentUser)) | ||
if (auto *I = dyn_cast<Instruction>(CurrentUser)) { | ||
// Do not merge globals in exception pads. | ||
if (I->isEHPad()) | ||
return false; | ||
|
||
if (auto *II = dyn_cast<IntrinsicInst>(I)) { | ||
// Some intrinsics require a plain global. | ||
if (II->getIntrinsicID() == Intrinsic::eh_typeid_for) | ||
return false; | ||
} | ||
|
||
// Other instruction users are always valid. | ||
continue; | ||
} | ||
|
||
// We cannot replace GlobalValue users because they are not just nodes | ||
// in IR. To replace a user like this we would need to create a new | ||
|
@@ -314,14 +326,6 @@ void PPCMergeStringPool::replaceUsesWithGEP(GlobalVariable *GlobalToReplace, | |
Users.push_back(CurrentUser); | ||
|
||
for (User *CurrentUser : Users) { | ||
Instruction *UserInstruction = dyn_cast<Instruction>(CurrentUser); | ||
Constant *UserConstant = dyn_cast<Constant>(CurrentUser); | ||
|
||
// At this point we expect that the user is either an instruction or a | ||
// constant. | ||
assert((UserConstant || UserInstruction) && | ||
"Expected the user to be an instruction or a constant."); | ||
|
||
// The user was not found so it must have been replaced earlier. | ||
if (!userHasOperand(CurrentUser, GlobalToReplace)) | ||
continue; | ||
|
@@ -330,38 +334,13 @@ void PPCMergeStringPool::replaceUsesWithGEP(GlobalVariable *GlobalToReplace, | |
if (isa<GlobalValue>(CurrentUser)) | ||
continue; | ||
|
||
if (!UserInstruction) { | ||
// User is a constant type. | ||
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr( | ||
PooledStructType, GPool, Indices); | ||
UserConstant->handleOperandChange(GlobalToReplace, ConstGEP); | ||
continue; | ||
} | ||
|
||
if (PHINode *UserPHI = dyn_cast<PHINode>(UserInstruction)) { | ||
// GEP instructions cannot be added before PHI nodes. | ||
// With getInBoundsGetElementPtr we create the GEP and then replace it | ||
// inline into the PHI. | ||
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr( | ||
PooledStructType, GPool, Indices); | ||
UserPHI->replaceUsesOfWith(GlobalToReplace, ConstGEP); | ||
continue; | ||
} | ||
// The user is a valid instruction that is not a PHINode. | ||
GetElementPtrInst *GEPInst = | ||
GetElementPtrInst::Create(PooledStructType, GPool, Indices); | ||
GEPInst->insertBefore(UserInstruction); | ||
|
||
LLVM_DEBUG(dbgs() << "Inserting GEP before:\n"); | ||
LLVM_DEBUG(UserInstruction->dump()); | ||
|
||
Constant *ConstGEP = ConstantExpr::getInBoundsGetElementPtr( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks reasonable to me. |
||
PooledStructType, GPool, Indices); | ||
chenzheng1030 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LLVM_DEBUG(dbgs() << "Replacing this global:\n"); | ||
LLVM_DEBUG(GlobalToReplace->dump()); | ||
LLVM_DEBUG(dbgs() << "with this:\n"); | ||
LLVM_DEBUG(GEPInst->dump()); | ||
|
||
// After the GEP is inserted the GV can be replaced. | ||
CurrentUser->replaceUsesOfWith(GlobalToReplace, GEPInst); | ||
LLVM_DEBUG(ConstGEP->dump()); | ||
GlobalToReplace->replaceAllUsesWith(ConstGEP); | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
llvm/test/CodeGen/PowerPC/mergeable-string-pool-exceptions.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4 | ||
; RUN: llc -mtriple=ppc64le-unknown-linux-gnu < %s | FileCheck %s | ||
|
||
@id = private unnamed_addr constant [4 x i8] c"@id\00", align 1 | ||
@id2 = private unnamed_addr constant [5 x i8] c"@id2\00", align 1 | ||
|
||
; Higher-aligned dummy to make sure it is first in the string pool. | ||
@dummy = private unnamed_addr constant [1 x i32] [i32 42], align 4 | ||
|
||
define ptr @test1() personality ptr @__gnu_objc_personality_v0 { | ||
; CHECK-LABEL: test1: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: mflr 0 | ||
; CHECK-NEXT: stdu 1, -32(1) | ||
; CHECK-NEXT: std 0, 48(1) | ||
; CHECK-NEXT: .cfi_def_cfa_offset 32 | ||
; CHECK-NEXT: .cfi_offset lr, 16 | ||
; CHECK-NEXT: addis 3, 2, .Ldummy@toc@ha | ||
; CHECK-NEXT: addi 3, 3, .Ldummy@toc@l | ||
; CHECK-NEXT: bl foo | ||
; CHECK-NEXT: nop | ||
invoke void @foo(ptr @dummy) | ||
to label %cont unwind label %unwind | ||
|
||
cont: | ||
unreachable | ||
|
||
unwind: | ||
%lp = landingpad { ptr, i32 } | ||
catch ptr @id | ||
resume { ptr, i32 } %lp | ||
} | ||
|
||
define i32 @test2() personality ptr @__gnu_objc_personality_v0 { | ||
; CHECK-LABEL: test2: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: li 3, 1 | ||
; CHECK-NEXT: blr | ||
%id = tail call i32 @llvm.eh.typeid.for(ptr @id2) | ||
ret i32 %id | ||
} | ||
|
||
declare i32 @__gnu_objc_personality_v0(...) | ||
|
||
declare i32 @llvm.eh.typeid.for(ptr) | ||
|
||
declare void @foo() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From
llvm-project/llvm/lib/CodeGen/GlobalMerge.cpp
Lines 601 to 610 in 853344d
GlobalVariable
used in eh related instructions are not allowed to replace. Not sure why we don't have similar logic here. @stefanp-ibm do you know why? Thanks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, maybe adding this check is a better fix. I assume it will also cover the eh.typeid.for issue if this intrinsic is only used if the global is also mentioned in a landingpad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and added the EHPad check while also keeping the explicit eh.typeid.for check. Better safe than sorry...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough.