Skip to content

Commit 97f3c20

Browse files
Copy Attributes for derived global strings from existing ones
1 parent b48d7b1 commit 97f3c20

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

llvm/lib/IR/IRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ GlobalVariable *IRBuilderBase::CreateGlobalString(StringRef Str,
5252
*M, StrConstant->getType(), true, GlobalValue::PrivateLinkage,
5353
StrConstant, Name, nullptr, GlobalVariable::NotThreadLocal, AddressSpace);
5454
GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
55-
GV->setAlignment(M->getDataLayout().getPreferredAlign(GV));
55+
GV->setAlignment(Align(1));
5656
return GV;
5757
}
5858

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/AttributeMask.h"
2525
#include "llvm/IR/DataLayout.h"
2626
#include "llvm/IR/Function.h"
27+
#include "llvm/IR/GlobalVariable.h"
2728
#include "llvm/IR/IRBuilder.h"
2829
#include "llvm/IR/IntrinsicInst.h"
2930
#include "llvm/IR/Intrinsics.h"
@@ -3323,8 +3324,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
33233324
// printf("%s", str"\n") --> puts(str)
33243325
if (OperandStr.back() == '\n') {
33253326
OperandStr = OperandStr.drop_back();
3326-
Value *GV = B.CreateGlobalString(OperandStr, "str");
3327-
return copyFlags(*CI, emitPutS(GV, B, TLI));
3327+
// Because we were able to derive OperandStr, we know it's safe to cast to
3328+
// GlobalVariable*.
3329+
GlobalVariable *OldStr =
3330+
dyn_cast<GlobalVariable>(getUnderlyingObject(CI->getArgOperand(1)));
3331+
GlobalVariable *NewStr = B.CreateGlobalString(
3332+
OperandStr, Twine(OldStr->getName(), ".clipped"));
3333+
NewStr->copyAttributesFrom(OldStr);
3334+
return copyFlags(*CI, emitPutS(NewStr, B, TLI));
33283335
}
33293336
return nullptr;
33303337
}
@@ -3335,8 +3342,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
33353342
// Create a string literal with no \n on it. We expect the constant merge
33363343
// pass to be run after this pass, to merge duplicate strings.
33373344
FormatStr = FormatStr.drop_back();
3338-
Value *GV = B.CreateGlobalString(FormatStr, "str");
3339-
return copyFlags(*CI, emitPutS(GV, B, TLI));
3345+
// Because we were able to derive FormatStr, we know it's safe to cast to
3346+
// GlobalVariable*.
3347+
GlobalVariable *OldStr =
3348+
dyn_cast<GlobalVariable>(getUnderlyingObject(CI->getArgOperand(0)));
3349+
GlobalVariable *NewStr =
3350+
B.CreateGlobalString(FormatStr, Twine(OldStr->getName(), ".clipped"));
3351+
NewStr->copyAttributesFrom(OldStr);
3352+
return copyFlags(*CI, emitPutS(NewStr, B, TLI));
33403353
}
33413354

33423355
// Optimize specific format strings.

0 commit comments

Comments
 (0)