24
24
#include " llvm/IR/AttributeMask.h"
25
25
#include " llvm/IR/DataLayout.h"
26
26
#include " llvm/IR/Function.h"
27
+ #include " llvm/IR/GlobalVariable.h"
27
28
#include " llvm/IR/IRBuilder.h"
28
29
#include " llvm/IR/IntrinsicInst.h"
29
30
#include " llvm/IR/Intrinsics.h"
@@ -3320,8 +3321,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3320
3321
// printf("%s", str"\n") --> puts(str)
3321
3322
if (OperandStr.back () == ' \n ' ) {
3322
3323
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));
3325
3332
}
3326
3333
return nullptr ;
3327
3334
}
@@ -3332,8 +3339,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3332
3339
// Create a string literal with no \n on it. We expect the constant merge
3333
3340
// pass to be run after this pass, to merge duplicate strings.
3334
3341
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));
3337
3350
}
3338
3351
3339
3352
// Optimize specific format strings.
0 commit comments