|
| 1 | +//===- bolt/Passes/ReorderSection.cpp - Reordering of section data --------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements DiscoverNoReturnPass class. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "bolt/Passes/DiscoverNoReturnPass.h" |
| 14 | + |
| 15 | +using namespace llvm; |
| 16 | + |
| 17 | +namespace opts { |
| 18 | +extern cl::OptionCategory BoltCategory; |
| 19 | + |
| 20 | +static cl::opt<bool> DiscoverNoReturnAnalysis( |
| 21 | + "discover-no-return", |
| 22 | + cl::desc("analyze the binary and mark no-return functions"), cl::init(true), |
| 23 | + cl::cat(BoltCategory), cl::ReallyHidden); |
| 24 | +} // namespace opts |
| 25 | + |
| 26 | +namespace llvm { |
| 27 | +namespace bolt { |
| 28 | + |
| 29 | +Error DiscoverNoReturnPass::runOnFunctions(BinaryContext &BC) { |
| 30 | + bool Changed; |
| 31 | + do { |
| 32 | + Changed = false; |
| 33 | + for (auto &BFI : BC.getBinaryFunctions()) { |
| 34 | + auto &Func = BFI.second; |
| 35 | + bool PrevStat = BC.hasPathToNoReturn(&Func); |
| 36 | + bool CurStat = traverseFromFunction(&Func, BC); |
| 37 | + Changed |= (PrevStat != CurStat); |
| 38 | + } |
| 39 | + } while (Changed); |
| 40 | + |
| 41 | + return Error::success(); |
| 42 | +} |
| 43 | + |
| 44 | +bool DiscoverNoReturnPass::traverseFromFunction(BinaryFunction *Func, |
| 45 | + BinaryContext &BC) { |
| 46 | + // The Function cached before, so return its value |
| 47 | + if (BC.cachedInNoReturnMap(Func)) |
| 48 | + return BC.hasPathToNoReturn(Func); |
| 49 | + |
| 50 | + Visited[Func] = true; |
| 51 | + bool Result = true; |
| 52 | + bool hasCalls = 0; |
| 53 | + bool hasReturns = 0; |
| 54 | + for (auto &BB : *Func) { |
| 55 | + if (!BB.getNumCalls()) |
| 56 | + continue; |
| 57 | + for (auto &Inst : BB) { |
| 58 | + if (BC.MIB->isCall(Inst)) { |
| 59 | + hasCalls = true; |
| 60 | + const MCSymbol *TargetSymbol = BC.MIB->getTargetSymbol(Inst); |
| 61 | + BinaryFunction *TargetFunction = BC.SymbolToFunctionMap[TargetSymbol]; |
| 62 | + if (!Visited.count(TargetFunction)) |
| 63 | + Result &= traverseFromFunction(TargetFunction, BC); |
| 64 | + } |
| 65 | + hasReturns |= BC.MIB->isReturn(Inst); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // This functions is represented as a leaf in the call graph and doesn't |
| 70 | + // have a no-return attribute. |
| 71 | + if (!hasCalls && hasReturns) |
| 72 | + Result = false; |
| 73 | + |
| 74 | + // If the function doens't have a return instruction then it's a |
| 75 | + // no-return function. |
| 76 | + if (!hasReturns) |
| 77 | + Result = true; |
| 78 | + |
| 79 | + BC.setHasPathToNoReturn(Func, Result); |
| 80 | + return Result; |
| 81 | +} |
| 82 | + |
| 83 | +} // end namespace bolt |
| 84 | +} // end namespace llvm |
0 commit comments