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"
@@ -3323,8 +3324,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3323
3324
// printf("%s", str"\n") --> puts(str)
3324
3325
if (OperandStr.back () == ' \n ' ) {
3325
3326
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));
3328
3335
}
3329
3336
return nullptr ;
3330
3337
}
@@ -3335,8 +3342,14 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilderBase &B) {
3335
3342
// Create a string literal with no \n on it. We expect the constant merge
3336
3343
// pass to be run after this pass, to merge duplicate strings.
3337
3344
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));
3340
3353
}
3341
3354
3342
3355
// Optimize specific format strings.
0 commit comments