ExtractFunction.cpp revision 276479
1185573Srwatson//===- ExtractFunction.cpp - Extract a function from Program --------------===//
2185573Srwatson//
3155131Srwatson//                     The LLVM Compiler Infrastructure
4155131Srwatson//
5155131Srwatson// This file is distributed under the University of Illinois Open Source
6155131Srwatson// License. See LICENSE.TXT for details.
7155131Srwatson//
8155131Srwatson//===----------------------------------------------------------------------===//
9155131Srwatson//
10155131Srwatson// This file implements several methods that are used to extract functions,
11155131Srwatson// loops, or portions of a module from the rest of the module.
12155131Srwatson//
13155131Srwatson//===----------------------------------------------------------------------===//
14185573Srwatson
15155131Srwatson#include "BugDriver.h"
16155131Srwatson#include "llvm/IR/Constants.h"
17155131Srwatson#include "llvm/IR/DataLayout.h"
18155131Srwatson#include "llvm/IR/DerivedTypes.h"
19155131Srwatson#include "llvm/IR/LLVMContext.h"
20155131Srwatson#include "llvm/IR/Module.h"
21155131Srwatson#include "llvm/IR/Verifier.h"
22155131Srwatson#include "llvm/Pass.h"
23155131Srwatson#include "llvm/PassManager.h"
24155131Srwatson#include "llvm/Support/CommandLine.h"
25155131Srwatson#include "llvm/Support/Debug.h"
26155131Srwatson#include "llvm/Support/FileUtilities.h"
27155131Srwatson#include "llvm/Support/Path.h"
28155131Srwatson#include "llvm/Support/Signals.h"
29155131Srwatson#include "llvm/Support/ToolOutputFile.h"
30186647Srwatson#include "llvm/Transforms/IPO.h"
31155131Srwatson#include "llvm/Transforms/Scalar.h"
32155131Srwatson#include "llvm/Transforms/Utils/Cloning.h"
33155131Srwatson#include "llvm/Transforms/Utils/CodeExtractor.h"
34156283Srwatson#include <set>
35156283Srwatsonusing namespace llvm;
36156283Srwatson
37155131Srwatson#define DEBUG_TYPE "bugpoint"
38156283Srwatson
39156283Srwatsonnamespace llvm {
40156283Srwatson  bool DisableSimplifyCFG = false;
41155131Srwatson  extern cl::opt<std::string> OutputPrefix;
42155131Srwatson} // End llvm namespace
43155131Srwatson
44186647Srwatsonnamespace {
45155131Srwatson  cl::opt<bool>
46186647Srwatson  NoDCE ("disable-dce",
47155131Srwatson         cl::desc("Do not use the -dce pass to reduce testcases"));
48155131Srwatson  cl::opt<bool, true>
49155131Srwatson  NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
50155131Srwatson         cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
51186647Srwatson
52155131Srwatson  Function* globalInitUsesExternalBA(GlobalVariable* GV) {
53186647Srwatson    if (!GV->hasInitializer())
54155131Srwatson      return nullptr;
55155131Srwatson
56155131Srwatson    Constant *I = GV->getInitializer();
57155131Srwatson
58155131Srwatson    // walk the values used by the initializer
59155131Srwatson    // (and recurse into things like ConstantExpr)
60155131Srwatson    std::vector<Constant*> Todo;
61155131Srwatson    std::set<Constant*> Done;
62155131Srwatson    Todo.push_back(I);
63155131Srwatson
64155131Srwatson    while (!Todo.empty()) {
65155131Srwatson      Constant* V = Todo.back();
66155131Srwatson      Todo.pop_back();
67155131Srwatson      Done.insert(V);
68155131Srwatson
69155131Srwatson      if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) {
70155131Srwatson        Function *F = BA->getFunction();
71155131Srwatson        if (F->isDeclaration())
72155131Srwatson          return F;
73155131Srwatson      }
74155131Srwatson
75155131Srwatson      for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) {
76155131Srwatson        Constant *C = dyn_cast<Constant>(*i);
77155131Srwatson        if (C && !isa<GlobalValue>(C) && !Done.count(C))
78155131Srwatson          Todo.push_back(C);
79155131Srwatson      }
80155131Srwatson    }
81155131Srwatson    return nullptr;
82155131Srwatson  }
83155131Srwatson}  // end anonymous namespace
84155131Srwatson
85155131Srwatson/// deleteInstructionFromProgram - This method clones the current Program and
86155131Srwatson/// deletes the specified instruction from the cloned module.  It then runs a
87155131Srwatson/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
88155131Srwatson/// depends on the value.  The modified module is then returned.
89155131Srwatson///
90155131SrwatsonModule *BugDriver::deleteInstructionFromProgram(const Instruction *I,
91155131Srwatson                                                unsigned Simplification) {
92155131Srwatson  // FIXME, use vmap?
93155131Srwatson  Module *Clone = CloneModule(Program);
94155131Srwatson
95155131Srwatson  const BasicBlock *PBB = I->getParent();
96155131Srwatson  const Function *PF = PBB->getParent();
97155131Srwatson
98155131Srwatson  Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn
99155131Srwatson  std::advance(RFI, std::distance(PF->getParent()->begin(),
100155131Srwatson                                  Module::const_iterator(PF)));
101155131Srwatson
102155131Srwatson  Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
103155131Srwatson  std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
104155131Srwatson
105155131Srwatson  BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
106155131Srwatson  std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
107155131Srwatson  Instruction *TheInst = RI;              // Got the corresponding instruction!
108155131Srwatson
109155131Srwatson  // If this instruction produces a value, replace any users with null values
110155131Srwatson  if (!TheInst->getType()->isVoidTy())
111155131Srwatson    TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
112155131Srwatson
113155131Srwatson  // Remove the instruction from the program.
114155131Srwatson  TheInst->getParent()->getInstList().erase(TheInst);
115155131Srwatson
116155131Srwatson  // Spiff up the output a little bit.
117155131Srwatson  std::vector<std::string> Passes;
118155131Srwatson
119155131Srwatson  /// Can we get rid of the -disable-* options?
120155131Srwatson  if (Simplification > 1 && !NoDCE)
121155131Srwatson    Passes.push_back("dce");
122155131Srwatson  if (Simplification && !DisableSimplifyCFG)
123155131Srwatson    Passes.push_back("simplifycfg");      // Delete dead control flow
124155131Srwatson
125155131Srwatson  Passes.push_back("verify");
126155131Srwatson  Module *New = runPassesOn(Clone, Passes);
127155131Srwatson  delete Clone;
128155131Srwatson  if (!New) {
129155131Srwatson    errs() << "Instruction removal failed.  Sorry. :(  Please report a bug!\n";
130155131Srwatson    exit(1);
131155131Srwatson  }
132155131Srwatson  return New;
133155131Srwatson}
134155131Srwatson
135155131Srwatson/// performFinalCleanups - This method clones the current Program and performs
136155131Srwatson/// a series of cleanups intended to get rid of extra cruft on the module
137155131Srwatson/// before handing it to the user.
138155131Srwatson///
139155131SrwatsonModule *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
140155131Srwatson  // Make all functions external, so GlobalDCE doesn't delete them...
141155131Srwatson  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
142155131Srwatson    I->setLinkage(GlobalValue::ExternalLinkage);
143155131Srwatson
144155131Srwatson  std::vector<std::string> CleanupPasses;
145155131Srwatson  CleanupPasses.push_back("globaldce");
146155131Srwatson
147155131Srwatson  if (MayModifySemantics)
148155131Srwatson    CleanupPasses.push_back("deadarghaX0r");
149155131Srwatson  else
150155131Srwatson    CleanupPasses.push_back("deadargelim");
151155131Srwatson
152155131Srwatson  Module *New = runPassesOn(M, CleanupPasses);
153155131Srwatson  if (!New) {
154155131Srwatson    errs() << "Final cleanups failed.  Sorry. :(  Please report a bug!\n";
155155131Srwatson    return M;
156155131Srwatson  }
157155131Srwatson  delete M;
158155131Srwatson  return New;
159155131Srwatson}
160155131Srwatson
161155131Srwatson
162155131Srwatson/// ExtractLoop - Given a module, extract up to one loop from it into a new
163155131Srwatson/// function.  This returns null if there are no extractable loops in the
164155131Srwatson/// program or if the loop extractor crashes.
165155131SrwatsonModule *BugDriver::ExtractLoop(Module *M) {
166155131Srwatson  std::vector<std::string> LoopExtractPasses;
167155131Srwatson  LoopExtractPasses.push_back("loop-extract-single");
168155131Srwatson
169186647Srwatson  Module *NewM = runPassesOn(M, LoopExtractPasses);
170155131Srwatson  if (!NewM) {
171186647Srwatson    outs() << "*** Loop extraction failed: ";
172155131Srwatson    EmitProgressBitcode(M, "loopextraction", true);
173155131Srwatson    outs() << "*** Sorry. :(  Please report a bug!\n";
174155131Srwatson    return nullptr;
175186647Srwatson  }
176155131Srwatson
177186647Srwatson  // Check to see if we created any new functions.  If not, no loops were
178155131Srwatson  // extracted and we should return null.  Limit the number of loops we extract
179155131Srwatson  // to avoid taking forever.
180155131Srwatson  static unsigned NumExtracted = 32;
181155131Srwatson  if (M->size() == NewM->size() || --NumExtracted == 0) {
182155131Srwatson    delete NewM;
183155131Srwatson    return nullptr;
184155131Srwatson  } else {
185186647Srwatson    assert(M->size() < NewM->size() && "Loop extract removed functions?");
186155131Srwatson    Module::iterator MI = NewM->begin();
187186647Srwatson    for (unsigned i = 0, e = M->size(); i != e; ++i)
188155131Srwatson      ++MI;
189155131Srwatson  }
190155131Srwatson
191155131Srwatson  return NewM;
192155131Srwatson}
193155131Srwatson
194155131Srwatson
195155131Srwatson// DeleteFunctionBody - "Remove" the function by deleting all of its basic
196155131Srwatson// blocks, making it external.
197155131Srwatson//
198155131Srwatsonvoid llvm::DeleteFunctionBody(Function *F) {
199186647Srwatson  // delete the body of the function...
200155131Srwatson  F->deleteBody();
201186647Srwatson  assert(F->isDeclaration() && "This didn't make the function external!");
202155131Srwatson}
203155131Srwatson
204155131Srwatson/// GetTorInit - Given a list of entries for static ctors/dtors, return them
205155131Srwatson/// as a constant array.
206155131Srwatsonstatic Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
207155131Srwatson  assert(!TorList.empty() && "Don't create empty tor list!");
208186647Srwatson  std::vector<Constant*> ArrayElts;
209155131Srwatson  Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext());
210186647Srwatson
211155131Srwatson  StructType *STy =
212155131Srwatson    StructType::get(Int32Ty, TorList[0].first->getType(), NULL);
213155131Srwatson  for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
214155131Srwatson    Constant *Elts[] = {
215      ConstantInt::get(Int32Ty, TorList[i].second),
216      TorList[i].first
217    };
218    ArrayElts.push_back(ConstantStruct::get(STy, Elts));
219  }
220  return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
221                                           ArrayElts.size()),
222                            ArrayElts);
223}
224
225/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and
226/// M1 has all of the global variables.  If M2 contains any functions that are
227/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and
228/// prune appropriate entries out of M1s list.
229static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2,
230                                ValueToValueMapTy &VMap) {
231  GlobalVariable *GV = M1->getNamedGlobal(GlobalName);
232  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() ||
233      !GV->use_empty()) return;
234
235  std::vector<std::pair<Function*, int> > M1Tors, M2Tors;
236  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
237  if (!InitList) return;
238
239  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
240    if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
241      if (CS->getNumOperands() != 2) return;  // Not array of 2-element structs.
242
243      if (CS->getOperand(1)->isNullValue())
244        break;  // Found a null terminator, stop here.
245
246      ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
247      int Priority = CI ? CI->getSExtValue() : 0;
248
249      Constant *FP = CS->getOperand(1);
250      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
251        if (CE->isCast())
252          FP = CE->getOperand(0);
253      if (Function *F = dyn_cast<Function>(FP)) {
254        if (!F->isDeclaration())
255          M1Tors.push_back(std::make_pair(F, Priority));
256        else {
257          // Map to M2's version of the function.
258          F = cast<Function>(VMap[F]);
259          M2Tors.push_back(std::make_pair(F, Priority));
260        }
261      }
262    }
263  }
264
265  GV->eraseFromParent();
266  if (!M1Tors.empty()) {
267    Constant *M1Init = GetTorInit(M1Tors);
268    new GlobalVariable(*M1, M1Init->getType(), false,
269                       GlobalValue::AppendingLinkage,
270                       M1Init, GlobalName);
271  }
272
273  GV = M2->getNamedGlobal(GlobalName);
274  assert(GV && "Not a clone of M1?");
275  assert(GV->use_empty() && "llvm.ctors shouldn't have uses!");
276
277  GV->eraseFromParent();
278  if (!M2Tors.empty()) {
279    Constant *M2Init = GetTorInit(M2Tors);
280    new GlobalVariable(*M2, M2Init->getType(), false,
281                       GlobalValue::AppendingLinkage,
282                       M2Init, GlobalName);
283  }
284}
285
286
287/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
288/// module, split the functions OUT of the specified module, and place them in
289/// the new module.
290Module *
291llvm::SplitFunctionsOutOfModule(Module *M,
292                                const std::vector<Function*> &F,
293                                ValueToValueMapTy &VMap) {
294  // Make sure functions & globals are all external so that linkage
295  // between the two modules will work.
296  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
297    I->setLinkage(GlobalValue::ExternalLinkage);
298  for (Module::global_iterator I = M->global_begin(), E = M->global_end();
299       I != E; ++I) {
300    if (I->hasName() && I->getName()[0] == '\01')
301      I->setName(I->getName().substr(1));
302    I->setLinkage(GlobalValue::ExternalLinkage);
303  }
304
305  ValueToValueMapTy NewVMap;
306  Module *New = CloneModule(M, NewVMap);
307
308  // Remove the Test functions from the Safe module
309  std::set<Function *> TestFunctions;
310  for (unsigned i = 0, e = F.size(); i != e; ++i) {
311    Function *TNOF = cast<Function>(VMap[F[i]]);
312    DEBUG(errs() << "Removing function ");
313    DEBUG(TNOF->printAsOperand(errs(), false));
314    DEBUG(errs() << "\n");
315    TestFunctions.insert(cast<Function>(NewVMap[TNOF]));
316    DeleteFunctionBody(TNOF);       // Function is now external in this module!
317  }
318
319
320  // Remove the Safe functions from the Test module
321  for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I)
322    if (!TestFunctions.count(I))
323      DeleteFunctionBody(I);
324
325
326  // Try to split the global initializers evenly
327  for (Module::global_iterator I = M->global_begin(), E = M->global_end();
328       I != E; ++I) {
329    GlobalVariable *GV = cast<GlobalVariable>(NewVMap[I]);
330    if (Function *TestFn = globalInitUsesExternalBA(I)) {
331      if (Function *SafeFn = globalInitUsesExternalBA(GV)) {
332        errs() << "*** Error: when reducing functions, encountered "
333                  "the global '";
334        GV->printAsOperand(errs(), false);
335        errs() << "' with an initializer that references blockaddresses "
336                  "from safe function '" << SafeFn->getName()
337               << "' and from test function '" << TestFn->getName() << "'.\n";
338        exit(1);
339      }
340      I->setInitializer(nullptr);  // Delete the initializer to make it external
341    } else {
342      // If we keep it in the safe module, then delete it in the test module
343      GV->setInitializer(nullptr);
344    }
345  }
346
347  // Make sure that there is a global ctor/dtor array in both halves of the
348  // module if they both have static ctor/dtor functions.
349  SplitStaticCtorDtor("llvm.global_ctors", M, New, NewVMap);
350  SplitStaticCtorDtor("llvm.global_dtors", M, New, NewVMap);
351
352  return New;
353}
354
355//===----------------------------------------------------------------------===//
356// Basic Block Extraction Code
357//===----------------------------------------------------------------------===//
358
359/// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
360/// into their own functions.  The only detail is that M is actually a module
361/// cloned from the one the BBs are in, so some mapping needs to be performed.
362/// If this operation fails for some reason (ie the implementation is buggy),
363/// this function should return null, otherwise it returns a new Module.
364Module *BugDriver::ExtractMappedBlocksFromModule(const
365                                                 std::vector<BasicBlock*> &BBs,
366                                                 Module *M) {
367  SmallString<128> Filename;
368  int FD;
369  std::error_code EC = sys::fs::createUniqueFile(
370      OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
371  if (EC) {
372    outs() << "*** Basic Block extraction failed!\n";
373    errs() << "Error creating temporary file: " << EC.message() << "\n";
374    EmitProgressBitcode(M, "basicblockextractfail", true);
375    return nullptr;
376  }
377  sys::RemoveFileOnSignal(Filename);
378
379  tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD);
380  for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
381       I != E; ++I) {
382    BasicBlock *BB = *I;
383    // If the BB doesn't have a name, give it one so we have something to key
384    // off of.
385    if (!BB->hasName()) BB->setName("tmpbb");
386    BlocksToNotExtractFile.os() << BB->getParent()->getName() << " "
387                                << BB->getName() << "\n";
388  }
389  BlocksToNotExtractFile.os().close();
390  if (BlocksToNotExtractFile.os().has_error()) {
391    errs() << "Error writing list of blocks to not extract\n";
392    EmitProgressBitcode(M, "basicblockextractfail", true);
393    BlocksToNotExtractFile.os().clear_error();
394    return nullptr;
395  }
396  BlocksToNotExtractFile.keep();
397
398  std::string uniqueFN = "--extract-blocks-file=";
399  uniqueFN += Filename.str();
400  const char *ExtraArg = uniqueFN.c_str();
401
402  std::vector<std::string> PI;
403  PI.push_back("extract-blocks");
404  Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
405
406  sys::fs::remove(Filename.c_str());
407
408  if (!Ret) {
409    outs() << "*** Basic Block extraction failed, please report a bug!\n";
410    EmitProgressBitcode(M, "basicblockextractfail", true);
411  }
412  return Ret;
413}
414