Skip to content

Commit f646713

Browse files
Copy Alignment for derived global strings from existing ones
1 parent 75dd66c commit f646713

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"
@@ -3320,8 +3321,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
33203321
// printf("%s", str"\n") --> puts(str)
33213322
if (OperandStr.back() == '\n') {
33223323
OperandStr = OperandStr.drop_back();
3323-
Value *GV = B.CreateGlobalString(OperandStr, "str");
3324-
return copyFlags(*CI, emitPutS(GV, B, TLI));
3324+
// Because we were able to derive OperandStr, we know it's safe to cast to
3325+
// GlobalVariable*.
3326+
GlobalVariable *OldStr =
3327+
dyn_cast<GlobalVariable>(getUnderlyingObject(CI->getArgOperand(1)));
3328+
GlobalVariable *NewStr = B.CreateGlobalString(
3329+
OperandStr, Twine(OldStr->getName(), ".clipped"));
3330+
NewStr->setAlignment(OldStr->getAlign());
3331+
return copyFlags(*CI, emitPutS(NewStr, B, TLI));
33253332
}
33263333
return nullptr;
33273334
}
@@ -3332,8 +3339,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
33323339
// Create a string literal with no \n on it. We expect the constant merge
33333340
// pass to be run after this pass, to merge duplicate strings.
33343341
FormatStr = FormatStr.drop_back();
3335-
Value *GV = B.CreateGlobalString(FormatStr, "str");
3336-
return copyFlags(*CI, emitPutS(GV, B, TLI));
3342+
// Because we were able to derive FormatStr, we know it's safe to cast to
3343+
// GlobalVariable*.
3344+
GlobalVariable *OldStr =
3345+
dyn_cast<GlobalVariable>(getUnderlyingObject(CI->getArgOperand(0)));
3346+
GlobalVariable *NewStr =
3347+
B.CreateGlobalString(FormatStr, Twine(OldStr->getName(), ".clipped"));
3348+
NewStr->setAlignment(OldStr->getAlign());
3349+
return copyFlags(*CI, emitPutS(NewStr, B, TLI));
33373350
}
33383351

33393352
// Optimize specific format strings.

0 commit comments

Comments
 (0)