@@ -1273,35 +1273,50 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
1273
1273
}
1274
1274
}
1275
1275
1276
- // If the memory object has non-trivial type, then removing the deallocation
1277
- // will drop any releases. Check that there is nothing preventing removal.
1276
+ // If our memory is trivially typed, we can just remove it without needing to
1277
+ // consider if the stored value needs to be destroyed. So at this point,
1278
+ // delete the memory!
1279
+ if (MemoryType.isTrivial (Module)) {
1280
+ LLVM_DEBUG (llvm::dbgs () << " *** Removing autogenerated trivial allocation: "
1281
+ << *TheMemory);
1282
+
1283
+ // If it is safe to remove, do it. Recursively remove all instructions
1284
+ // hanging off the allocation instruction, then return success. Let the
1285
+ // caller remove the allocation itself to avoid iterator invalidation.
1286
+ eraseUsesOfInstruction (TheMemory);
1287
+
1288
+ return true ;
1289
+ }
1290
+
1291
+ // Otherwise removing the deallocation will drop any releases. Check that
1292
+ // there is nothing preventing removal.
1278
1293
llvm::SmallVector<unsigned , 8 > DestroyAddrIndices;
1279
1294
llvm::SmallVector<AvailableValue, 32 > AvailableValueList;
1280
1295
llvm::SmallVector<unsigned , 8 > AvailableValueStartOffsets;
1281
1296
1282
- if (!MemoryType.isTrivial (Module)) {
1283
- for (auto P : llvm::enumerate (Releases)) {
1284
- auto *R = P.value ();
1285
- if (R == nullptr )
1286
- continue ;
1287
-
1288
- // We stash all of the destroy_addr that we see.
1289
- if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
1290
- AvailableValueStartOffsets.push_back (AvailableValueList.size ());
1291
- // Make sure we can actually promote this destroy addr. If we can not,
1292
- // then we must bail. In order to not gather available values twice, we
1293
- // gather the available values here that we will use to promote the
1294
- // values.
1295
- if (!canPromoteDestroyAddr (DAI, AvailableValueList))
1296
- return false ;
1297
- DestroyAddrIndices.push_back (P.index ());
1298
- continue ;
1299
- }
1297
+ for (auto P : llvm::enumerate (Releases)) {
1298
+ auto *R = P.value ();
1299
+ if (R == nullptr )
1300
+ continue ;
1300
1301
1301
- LLVM_DEBUG (llvm::dbgs () << " *** Failed to remove autogenerated alloc: "
1302
- " kept alive by release: " << *R);
1303
- return false ;
1302
+ // We stash all of the destroy_addr that we see.
1303
+ if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
1304
+ AvailableValueStartOffsets.push_back (AvailableValueList.size ());
1305
+ // Make sure we can actually promote this destroy addr. If we can not,
1306
+ // then we must bail. In order to not gather available values twice, we
1307
+ // gather the available values here that we will use to promote the
1308
+ // values.
1309
+ if (!canPromoteDestroyAddr (DAI, AvailableValueList))
1310
+ return false ;
1311
+ DestroyAddrIndices.push_back (P.index ());
1312
+ continue ;
1304
1313
}
1314
+
1315
+ LLVM_DEBUG (llvm::dbgs ()
1316
+ << " *** Failed to remove autogenerated non-trivial alloc: "
1317
+ " kept alive by release: "
1318
+ << *R);
1319
+ return false ;
1305
1320
}
1306
1321
1307
1322
// If we reached this point, we can promote all of our destroy_addr.
@@ -1324,7 +1339,7 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
1324
1339
Releases[DestroyAddrIndex] = nullptr ;
1325
1340
}
1326
1341
1327
- LLVM_DEBUG (llvm::dbgs () << " *** Removing autogenerated alloc_stack : "
1342
+ LLVM_DEBUG (llvm::dbgs () << " *** Removing autogenerated non-trivial alloc : "
1328
1343
<< *TheMemory);
1329
1344
1330
1345
// If it is safe to remove, do it. Recursively remove all instructions
0 commit comments