[llvm] [BOLT] Add support for safe-icf (PR #116275)
Amir Ayupov via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 13 12:40:51 PST 2024
================
@@ -340,6 +354,77 @@ typedef std::unordered_map<BinaryFunction *, std::vector<BinaryFunction *>,
namespace llvm {
namespace bolt {
+void IdenticalCodeFolding::initVTableReferences(const BinaryContext &BC) {
+ initVtable();
+ for (const auto &[Address, Data] : BC.getBinaryData()) {
+ // Filter out all symbols that are not vtables.
+ if (!Data->getName().starts_with("_ZTV"))
+ continue;
+ for (uint64_t I = Address, End = I + Data->getSize(); I < End; I += 8)
+ setAddressUsedInVTable(I);
+ }
+}
+void IdenticalCodeFolding::analyzeDataRelocations(BinaryContext &BC) {
+ initVTableReferences(BC);
+ for (const BinarySection &Sec : BC.sections()) {
+ if (!Sec.hasSectionRef() || !Sec.isData())
+ continue;
+ for (const auto &Rel : Sec.relocations()) {
+ const uint64_t RelAddr = Rel.Offset + Sec.getAddress();
+ if (isAddressInVTable(RelAddr))
+ continue;
+ BinaryFunction *BF = BC.getFunctionForSymbol(Rel.Symbol);
+ if (!BF)
+ continue;
+ BF->setHasAddressTaken(true);
+ }
+ for (const auto &Rel : Sec.dynamicRelocations()) {
+ const uint64_t RelAddr = Rel.Offset + Sec.getAddress();
+ if (isAddressInVTable(RelAddr))
+ continue;
+ BinaryFunction *BF =
+ BC.getBinaryFunctionContainingAddress(Rel.Addend,
+ /*CheckPastEnd*/ false,
+ /*UseMaxSize*/ true);
+ if (!BF)
+ continue;
+ BF->setHasAddressTaken(true);
+ }
+ }
+}
+void IdenticalCodeFolding::analyzeFunctions(BinaryContext &BC) {
+ ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
+ for (const BinaryBasicBlock *BB : BF.getLayout().blocks())
+ for (const MCInst &Inst : *BB)
+ if (!(BC.MIB->isCall(Inst) || BC.MIB->isBranch(Inst)))
+ BF.processInstructionsForFuncReferences(BC, Inst);
+ };
+ ParallelUtilities::PredicateTy SkipFunc =
+ [&](const BinaryFunction &BF) -> bool {
+ return BF.getState() != BinaryFunction::State::CFG;
----------------
aaupov wrote:
```suggestion
return !BF.hasCFG();
```
https://github.com/llvm/llvm-project/pull/116275
More information about the llvm-commits
mailing list