Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 1 | //===- BlockExtractor.cpp - Extracts blocks into their own functions ------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass extracts the specified basic blocks from the module into their |
| 10 | // own functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | #include "llvm/ADT/Statistic.h" |
| 16 | #include "llvm/IR/Instructions.h" |
| 17 | #include "llvm/IR/Module.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 18 | #include "llvm/InitializePasses.h" |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Transforms/IPO.h" |
| 24 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 25 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 26 | |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | #define DEBUG_TYPE "block-extractor" |
| 30 | |
| 31 | STATISTIC(NumExtracted, "Number of basic blocks extracted"); |
| 32 | |
| 33 | static cl::opt<std::string> BlockExtractorFile( |
| 34 | "extract-blocks-file", cl::value_desc("filename"), |
| 35 | cl::desc("A file containing list of basic blocks to extract"), cl::Hidden); |
| 36 | |
| 37 | cl::opt<bool> BlockExtractorEraseFuncs("extract-blocks-erase-funcs", |
| 38 | cl::desc("Erase the existing functions"), |
| 39 | cl::Hidden); |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | class BlockExtractor : public ModulePass { |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 42 | SmallVector<SmallVector<BasicBlock *, 16>, 4> GroupsOfBlocks; |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 43 | bool EraseFunctions; |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 44 | /// Map a function name to groups of blocks. |
| 45 | SmallVector<std::pair<std::string, SmallVector<std::string, 4>>, 4> |
| 46 | BlocksByName; |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 47 | |
Quentin Colombet | 31ce274 | 2019-04-29 16:14:02 +0000 | [diff] [blame] | 48 | void init(const SmallVectorImpl<SmallVector<BasicBlock *, 16>> |
| 49 | &GroupsOfBlocksToExtract) { |
| 50 | for (const SmallVectorImpl<BasicBlock *> &GroupOfBlocks : |
| 51 | GroupsOfBlocksToExtract) { |
| 52 | SmallVector<BasicBlock *, 16> NewGroup; |
| 53 | NewGroup.append(GroupOfBlocks.begin(), GroupOfBlocks.end()); |
| 54 | GroupsOfBlocks.emplace_back(NewGroup); |
| 55 | } |
| 56 | if (!BlockExtractorFile.empty()) |
| 57 | loadFile(); |
| 58 | } |
| 59 | |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 60 | public: |
| 61 | static char ID; |
| 62 | BlockExtractor(const SmallVectorImpl<BasicBlock *> &BlocksToExtract, |
| 63 | bool EraseFunctions) |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 64 | : ModulePass(ID), EraseFunctions(EraseFunctions) { |
| 65 | // We want one group per element of the input list. |
Quentin Colombet | 31ce274 | 2019-04-29 16:14:02 +0000 | [diff] [blame] | 66 | SmallVector<SmallVector<BasicBlock *, 16>, 4> MassagedGroupsOfBlocks; |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 67 | for (BasicBlock *BB : BlocksToExtract) { |
| 68 | SmallVector<BasicBlock *, 16> NewGroup; |
| 69 | NewGroup.push_back(BB); |
Quentin Colombet | 31ce274 | 2019-04-29 16:14:02 +0000 | [diff] [blame] | 70 | MassagedGroupsOfBlocks.push_back(NewGroup); |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 71 | } |
Quentin Colombet | 31ce274 | 2019-04-29 16:14:02 +0000 | [diff] [blame] | 72 | init(MassagedGroupsOfBlocks); |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 73 | } |
Quentin Colombet | 31ce274 | 2019-04-29 16:14:02 +0000 | [diff] [blame] | 74 | |
| 75 | BlockExtractor(const SmallVectorImpl<SmallVector<BasicBlock *, 16>> |
| 76 | &GroupsOfBlocksToExtract, |
| 77 | bool EraseFunctions) |
| 78 | : ModulePass(ID), EraseFunctions(EraseFunctions) { |
| 79 | init(GroupsOfBlocksToExtract); |
| 80 | } |
| 81 | |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 82 | BlockExtractor() : BlockExtractor(SmallVector<BasicBlock *, 0>(), false) {} |
| 83 | bool runOnModule(Module &M) override; |
| 84 | |
| 85 | private: |
| 86 | void loadFile(); |
| 87 | void splitLandingPadPreds(Function &F); |
| 88 | }; |
| 89 | } // end anonymous namespace |
| 90 | |
| 91 | char BlockExtractor::ID = 0; |
| 92 | INITIALIZE_PASS(BlockExtractor, "extract-blocks", |
| 93 | "Extract basic blocks from module", false, false) |
| 94 | |
| 95 | ModulePass *llvm::createBlockExtractorPass() { return new BlockExtractor(); } |
| 96 | ModulePass *llvm::createBlockExtractorPass( |
| 97 | const SmallVectorImpl<BasicBlock *> &BlocksToExtract, bool EraseFunctions) { |
| 98 | return new BlockExtractor(BlocksToExtract, EraseFunctions); |
| 99 | } |
Quentin Colombet | 31ce274 | 2019-04-29 16:14:02 +0000 | [diff] [blame] | 100 | ModulePass *llvm::createBlockExtractorPass( |
| 101 | const SmallVectorImpl<SmallVector<BasicBlock *, 16>> |
| 102 | &GroupsOfBlocksToExtract, |
| 103 | bool EraseFunctions) { |
| 104 | return new BlockExtractor(GroupsOfBlocksToExtract, EraseFunctions); |
| 105 | } |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 106 | |
| 107 | /// Gets all of the blocks specified in the input file. |
| 108 | void BlockExtractor::loadFile() { |
| 109 | auto ErrOrBuf = MemoryBuffer::getFile(BlockExtractorFile); |
Volkan Keles | ebf34ea | 2018-01-23 22:24:34 +0000 | [diff] [blame] | 110 | if (ErrOrBuf.getError()) |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 111 | report_fatal_error("BlockExtractor couldn't load the file."); |
| 112 | // Read the file. |
| 113 | auto &Buf = *ErrOrBuf; |
| 114 | SmallVector<StringRef, 16> Lines; |
| 115 | Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1, |
| 116 | /*KeepEmpty=*/false); |
| 117 | for (const auto &Line : Lines) { |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 118 | SmallVector<StringRef, 4> LineSplit; |
| 119 | Line.split(LineSplit, ' ', /*MaxSplit=*/-1, |
| 120 | /*KeepEmpty=*/false); |
| 121 | if (LineSplit.empty()) |
| 122 | continue; |
Jinsong Ji | cda334b | 2019-08-20 14:46:02 +0000 | [diff] [blame] | 123 | if (LineSplit.size()!=2) |
| 124 | report_fatal_error("Invalid line format, expecting lines like: 'funcname bb1[;bb2..]'"); |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 125 | SmallVector<StringRef, 4> BBNames; |
Quentin Colombet | ae2cbb3 | 2019-04-29 16:14:00 +0000 | [diff] [blame] | 126 | LineSplit[1].split(BBNames, ';', /*MaxSplit=*/-1, |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 127 | /*KeepEmpty=*/false); |
| 128 | if (BBNames.empty()) |
| 129 | report_fatal_error("Missing bbs name"); |
| 130 | BlocksByName.push_back({LineSplit[0], {BBNames.begin(), BBNames.end()}}); |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Extracts the landing pads to make sure all of them have only one |
| 135 | /// predecessor. |
| 136 | void BlockExtractor::splitLandingPadPreds(Function &F) { |
| 137 | for (BasicBlock &BB : F) { |
| 138 | for (Instruction &I : BB) { |
| 139 | if (!isa<InvokeInst>(&I)) |
| 140 | continue; |
| 141 | InvokeInst *II = cast<InvokeInst>(&I); |
| 142 | BasicBlock *Parent = II->getParent(); |
| 143 | BasicBlock *LPad = II->getUnwindDest(); |
| 144 | |
| 145 | // Look through the landing pad's predecessors. If one of them ends in an |
| 146 | // 'invoke', then we want to split the landing pad. |
| 147 | bool Split = false; |
| 148 | for (auto PredBB : predecessors(LPad)) { |
| 149 | if (PredBB->isLandingPad() && PredBB != Parent && |
| 150 | isa<InvokeInst>(Parent->getTerminator())) { |
| 151 | Split = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (!Split) |
| 157 | continue; |
| 158 | |
| 159 | SmallVector<BasicBlock *, 2> NewBBs; |
| 160 | SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", NewBBs); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | bool BlockExtractor::runOnModule(Module &M) { |
| 166 | |
| 167 | bool Changed = false; |
| 168 | |
| 169 | // Get all the functions. |
| 170 | SmallVector<Function *, 4> Functions; |
| 171 | for (Function &F : M) { |
| 172 | splitLandingPadPreds(F); |
| 173 | Functions.push_back(&F); |
| 174 | } |
| 175 | |
| 176 | // Get all the blocks specified in the input file. |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 177 | unsigned NextGroupIdx = GroupsOfBlocks.size(); |
| 178 | GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size()); |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 179 | for (const auto &BInfo : BlocksByName) { |
| 180 | Function *F = M.getFunction(BInfo.first); |
| 181 | if (!F) |
| 182 | report_fatal_error("Invalid function name specified in the input file"); |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 183 | for (const auto &BBInfo : BInfo.second) { |
| 184 | auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) { |
| 185 | return BB.getName().equals(BBInfo); |
| 186 | }); |
| 187 | if (Res == F->end()) |
| 188 | report_fatal_error("Invalid block name specified in the input file"); |
| 189 | GroupsOfBlocks[NextGroupIdx].push_back(&*Res); |
| 190 | } |
| 191 | ++NextGroupIdx; |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 194 | // Extract each group of basic blocks. |
| 195 | for (auto &BBs : GroupsOfBlocks) { |
| 196 | SmallVector<BasicBlock *, 32> BlocksToExtractVec; |
| 197 | for (BasicBlock *BB : BBs) { |
| 198 | // Check if the module contains BB. |
| 199 | if (BB->getParent()->getParent() != &M) |
| 200 | report_fatal_error("Invalid basic block"); |
| 201 | LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting " |
| 202 | << BB->getParent()->getName() << ":" << BB->getName() |
| 203 | << "\n"); |
| 204 | BlocksToExtractVec.push_back(BB); |
| 205 | if (const InvokeInst *II = dyn_cast<InvokeInst>(BB->getTerminator())) |
| 206 | BlocksToExtractVec.push_back(II->getUnwindDest()); |
| 207 | ++NumExtracted; |
| 208 | Changed = true; |
| 209 | } |
Vedant Kumar | 9852699 | 2019-10-08 17:17:51 +0000 | [diff] [blame] | 210 | CodeExtractorAnalysisCache CEAC(*BBs[0]->getParent()); |
| 211 | Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(CEAC); |
Quentin Colombet | ea3364b | 2019-04-18 18:28:30 +0000 | [diff] [blame] | 212 | if (F) |
| 213 | LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName() |
| 214 | << "' in: " << F->getName() << '\n'); |
| 215 | else |
| 216 | LLVM_DEBUG(dbgs() << "Failed to extract for group '" |
| 217 | << (*BBs.begin())->getName() << "'\n"); |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Erase the functions. |
| 221 | if (EraseFunctions || BlockExtractorEraseFuncs) { |
| 222 | for (Function *F : Functions) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 223 | LLVM_DEBUG(dbgs() << "BlockExtractor: Trying to delete " << F->getName() |
| 224 | << "\n"); |
Volkan Keles | 4ecdb44 | 2018-03-12 22:28:18 +0000 | [diff] [blame] | 225 | F->deleteBody(); |
Volkan Keles | dc40be7 | 2018-01-23 21:51:34 +0000 | [diff] [blame] | 226 | } |
| 227 | // Set linkage as ExternalLinkage to avoid erasing unreachable functions. |
| 228 | for (Function &F : M) |
| 229 | F.setLinkage(GlobalValue::ExternalLinkage); |
| 230 | Changed = true; |
| 231 | } |
| 232 | |
| 233 | return Changed; |
| 234 | } |