|
15 | 15 | #include "bolt/Core/ParallelUtilities.h" |
16 | 16 | #include "llvm/ADT/SmallVector.h" |
17 | 17 | #include "llvm/Support/CommandLine.h" |
| 18 | +#include "llvm/Support/FormatVariadic.h" |
18 | 19 | #include "llvm/Support/ThreadPool.h" |
19 | 20 | #include "llvm/Support/Timer.h" |
20 | 21 | #include <atomic> |
@@ -42,8 +43,41 @@ TimeICF("time-icf", |
42 | 43 | cl::ReallyHidden, |
43 | 44 | cl::ZeroOrMore, |
44 | 45 | cl::cat(BoltOptCategory)); |
| 46 | + |
| 47 | +cl::opt<bolt::IdenticalCodeFolding::ICFLevel, false, |
| 48 | + DeprecatedICFNumericOptionParser> |
| 49 | + ICF("icf", cl::desc("fold functions with identical code"), |
| 50 | + cl::init(bolt::IdenticalCodeFolding::ICFLevel::None), |
| 51 | + cl::values(clEnumValN(bolt::IdenticalCodeFolding::ICFLevel::All, "all", |
| 52 | + "Enable identical code folding"), |
| 53 | + clEnumValN(bolt::IdenticalCodeFolding::ICFLevel::All, "1", |
| 54 | + "Enable identical code folding"), |
| 55 | + clEnumValN(bolt::IdenticalCodeFolding::ICFLevel::All, "", |
| 56 | + "Enable identical code folding"), |
| 57 | + clEnumValN(bolt::IdenticalCodeFolding::ICFLevel::None, |
| 58 | + "none", |
| 59 | + "Disable identical code folding (default)"), |
| 60 | + clEnumValN(bolt::IdenticalCodeFolding::ICFLevel::None, "0", |
| 61 | + "Disable identical code folding (default)"), |
| 62 | + clEnumValN(bolt::IdenticalCodeFolding::ICFLevel::Safe, |
| 63 | + "safe", "Enable safe identical code folding")), |
| 64 | + cl::ZeroOrMore, cl::ValueOptional, cl::cat(BoltOptCategory)); |
45 | 65 | } // namespace opts |
46 | 66 |
|
| 67 | +bool IdenticalCodeFolding::shouldOptimize(const BinaryFunction &BF) const { |
| 68 | + if (BF.hasUnknownControlFlow()) |
| 69 | + return false; |
| 70 | + if (BF.isFolded()) |
| 71 | + return false; |
| 72 | + if (BF.hasSDTMarker()) |
| 73 | + return false; |
| 74 | + if (BF.isPseudo()) |
| 75 | + return false; |
| 76 | + if (opts::ICF == ICFLevel::Safe && BF.hasAddressTaken()) |
| 77 | + return false; |
| 78 | + return BinaryFunctionPass::shouldOptimize(BF); |
| 79 | +} |
| 80 | + |
47 | 81 | /// Compare two jump tables in 2 functions. The function relies on consistent |
48 | 82 | /// ordering of basic blocks in both binary functions (e.g. DFS). |
49 | 83 | static bool equalJumpTables(const JumpTable &JumpTableA, |
@@ -340,6 +374,74 @@ typedef std::unordered_map<BinaryFunction *, std::vector<BinaryFunction *>, |
340 | 374 |
|
341 | 375 | namespace llvm { |
342 | 376 | namespace bolt { |
| 377 | +void IdenticalCodeFolding::initVTableReferences(const BinaryContext &BC) { |
| 378 | + for (const auto &[Address, Data] : BC.getBinaryData()) { |
| 379 | + // Filter out all symbols that are not vtables. |
| 380 | + if (!Data->getName().starts_with("_ZTV")) |
| 381 | + continue; |
| 382 | + for (uint64_t I = Address, End = I + Data->getSize(); I < End; I += 8) |
| 383 | + setAddressUsedInVTable(I); |
| 384 | + } |
| 385 | +} |
| 386 | + |
| 387 | +void IdenticalCodeFolding::analyzeDataRelocations(BinaryContext &BC) { |
| 388 | + initVTableReferences(BC); |
| 389 | + // For static relocations there should be a symbol for function references. |
| 390 | + for (const BinarySection &Sec : BC.sections()) { |
| 391 | + if (!Sec.hasSectionRef() || !Sec.isData()) |
| 392 | + continue; |
| 393 | + for (const auto &Rel : Sec.relocations()) { |
| 394 | + const uint64_t RelAddr = Rel.Offset + Sec.getAddress(); |
| 395 | + if (isAddressInVTable(RelAddr)) |
| 396 | + continue; |
| 397 | + if (BinaryFunction *BF = BC.getFunctionForSymbol(Rel.Symbol)) |
| 398 | + BF->setHasAddressTaken(true); |
| 399 | + } |
| 400 | + // For dynamic relocations there are two cases: |
| 401 | + // 1: No symbol and only addend. |
| 402 | + // 2: There is a symbol, but it does not references a function in a binary. |
| 403 | + for (const auto &Rel : Sec.dynamicRelocations()) { |
| 404 | + const uint64_t RelAddr = Rel.Offset + Sec.getAddress(); |
| 405 | + if (isAddressInVTable(RelAddr)) |
| 406 | + continue; |
| 407 | + if (BinaryFunction *BF = BC.getBinaryFunctionAtAddress(Rel.Addend)) |
| 408 | + BF->setHasAddressTaken(true); |
| 409 | + } |
| 410 | + } |
| 411 | +} |
| 412 | + |
| 413 | +void IdenticalCodeFolding::analyzeFunctions(BinaryContext &BC) { |
| 414 | + ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) { |
| 415 | + for (const BinaryBasicBlock &BB : BF) |
| 416 | + for (const MCInst &Inst : BB) |
| 417 | + if (!(BC.MIB->isCall(Inst) || BC.MIB->isBranch(Inst))) |
| 418 | + BF.analyzeInstructionForFuncReference(Inst); |
| 419 | + }; |
| 420 | + ParallelUtilities::PredicateTy SkipFunc = |
| 421 | + [&](const BinaryFunction &BF) -> bool { return !BF.hasCFG(); }; |
| 422 | + ParallelUtilities::runOnEachFunction( |
| 423 | + BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, |
| 424 | + SkipFunc, "markUnsafe"); |
| 425 | + |
| 426 | + LLVM_DEBUG({ |
| 427 | + for (const auto &BFIter : BC.getBinaryFunctions()) { |
| 428 | + if (!BFIter.second.hasAddressTaken()) |
| 429 | + continue; |
| 430 | + dbgs() << "BOLT-DEBUG: skipping function with reference taken " |
| 431 | + << BFIter.second.getOneName() << '\n'; |
| 432 | + } |
| 433 | + }); |
| 434 | +} |
| 435 | + |
| 436 | +void IdenticalCodeFolding::markFunctionsUnsafeToFold(BinaryContext &BC) { |
| 437 | + NamedRegionTimer MarkFunctionsUnsafeToFoldTimer( |
| 438 | + "markFunctionsUnsafeToFold", "markFunctionsUnsafeToFold", "ICF breakdown", |
| 439 | + "ICF breakdown", opts::TimeICF); |
| 440 | + if (!BC.isX86()) |
| 441 | + BC.outs() << "BOLT-WARNING: safe ICF is only supported for x86\n"; |
| 442 | + analyzeDataRelocations(BC); |
| 443 | + analyzeFunctions(BC); |
| 444 | +} |
343 | 445 |
|
344 | 446 | Error IdenticalCodeFolding::runOnFunctions(BinaryContext &BC) { |
345 | 447 | const size_t OriginalFunctionCount = BC.getBinaryFunctions().size(); |
@@ -385,7 +487,7 @@ Error IdenticalCodeFolding::runOnFunctions(BinaryContext &BC) { |
385 | 487 | "ICF breakdown", opts::TimeICF); |
386 | 488 | for (auto &BFI : BC.getBinaryFunctions()) { |
387 | 489 | BinaryFunction &BF = BFI.second; |
388 | | - if (!this->shouldOptimize(BF)) |
| 490 | + if (!shouldOptimize(BF)) |
389 | 491 | continue; |
390 | 492 | CongruentBuckets[&BF].emplace(&BF); |
391 | 493 | } |
@@ -475,7 +577,8 @@ Error IdenticalCodeFolding::runOnFunctions(BinaryContext &BC) { |
475 | 577 |
|
476 | 578 | LLVM_DEBUG(SinglePass.stopTimer()); |
477 | 579 | }; |
478 | | - |
| 580 | + if (opts::ICF == ICFLevel::Safe) |
| 581 | + markFunctionsUnsafeToFold(BC); |
479 | 582 | hashFunctions(); |
480 | 583 | createCongruentBuckets(); |
481 | 584 |
|
|
0 commit comments