@@ -1156,59 +1156,12 @@ Value *LibCallSimplifier::optimizeMemMove(CallInst *CI, IRBuilderBase &B) {
1156
1156
return CI->getArgOperand (0 );
1157
1157
}
1158
1158
1159
- // / Fold memset[_chk](malloc(n), 0, n) --> calloc(1, n).
1160
- Value *LibCallSimplifier::foldMallocMemset (CallInst *Memset, IRBuilderBase &B) {
1161
- // This has to be a memset of zeros (bzero).
1162
- auto *FillValue = dyn_cast<ConstantInt>(Memset->getArgOperand (1 ));
1163
- if (!FillValue || FillValue->getZExtValue () != 0 )
1164
- return nullptr ;
1165
-
1166
- // TODO: We should handle the case where the malloc has more than one use.
1167
- // This is necessary to optimize common patterns such as when the result of
1168
- // the malloc is checked against null or when a memset intrinsic is used in
1169
- // place of a memset library call.
1170
- auto *Malloc = dyn_cast<CallInst>(Memset->getArgOperand (0 ));
1171
- if (!Malloc || !Malloc->hasOneUse ())
1172
- return nullptr ;
1173
-
1174
- // Is the inner call really malloc()?
1175
- Function *InnerCallee = Malloc->getCalledFunction ();
1176
- if (!InnerCallee)
1177
- return nullptr ;
1178
-
1179
- LibFunc Func;
1180
- if (!TLI->getLibFunc (*InnerCallee, Func) || !TLI->has (Func) ||
1181
- Func != LibFunc_malloc)
1182
- return nullptr ;
1183
-
1184
- // The memset must cover the same number of bytes that are malloc'd.
1185
- if (Memset->getArgOperand (2 ) != Malloc->getArgOperand (0 ))
1186
- return nullptr ;
1187
-
1188
- // Replace the malloc with a calloc. We need the data layout to know what the
1189
- // actual size of a 'size_t' parameter is.
1190
- B.SetInsertPoint (Malloc->getParent (), ++Malloc->getIterator ());
1191
- const DataLayout &DL = Malloc->getModule ()->getDataLayout ();
1192
- IntegerType *SizeType = DL.getIntPtrType (B.GetInsertBlock ()->getContext ());
1193
- if (Value *Calloc = emitCalloc (ConstantInt::get (SizeType, 1 ),
1194
- Malloc->getArgOperand (0 ),
1195
- Malloc->getAttributes (), B, *TLI)) {
1196
- substituteInParent (Malloc, Calloc);
1197
- return Calloc;
1198
- }
1199
-
1200
- return nullptr ;
1201
- }
1202
-
1203
1159
Value *LibCallSimplifier::optimizeMemSet (CallInst *CI, IRBuilderBase &B) {
1204
1160
Value *Size = CI->getArgOperand (2 );
1205
1161
annotateNonNullAndDereferenceable (CI, 0 , Size, DL);
1206
1162
if (isa<IntrinsicInst>(CI))
1207
1163
return nullptr ;
1208
1164
1209
- if (auto *Calloc = foldMallocMemset (CI, B))
1210
- return Calloc;
1211
-
1212
1165
// memset(p, v, n) -> llvm.memset(align 1 p, v, n)
1213
1166
Value *Val = B.CreateIntCast (CI->getArgOperand (1 ), B.getInt8Ty (), false );
1214
1167
CallInst *NewCI = B.CreateMemSet (CI->getArgOperand (0 ), Val, Size, Align (1 ));
@@ -3066,7 +3019,6 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI, IRBuilderBase &Builder) {
3066
3019
return optimizeLog (CI, Builder);
3067
3020
case Intrinsic::sqrt:
3068
3021
return optimizeSqrt (CI, Builder);
3069
- // TODO: Use foldMallocMemset() with memset intrinsic.
3070
3022
case Intrinsic::memset:
3071
3023
return optimizeMemSet (CI, Builder);
3072
3024
case Intrinsic::memcpy:
@@ -3289,8 +3241,6 @@ Value *FortifiedLibCallSimplifier::optimizeMemMoveChk(CallInst *CI,
3289
3241
3290
3242
Value *FortifiedLibCallSimplifier::optimizeMemSetChk (CallInst *CI,
3291
3243
IRBuilderBase &B) {
3292
- // TODO: Try foldMallocMemset() here.
3293
-
3294
3244
if (isFortifiedCallFoldable (CI, 3 , 2 )) {
3295
3245
Value *Val = B.CreateIntCast (CI->getArgOperand (1 ), B.getInt8Ty (), false );
3296
3246
CallInst *NewCI = B.CreateMemSet (CI->getArgOperand (0 ), Val,
0 commit comments