ExtractFunction.cpp revision 280031
1249259Sdim//===- ExtractFunction.cpp - Extract a function from Program --------------===// 2249259Sdim// 3249259Sdim// The LLVM Compiler Infrastructure 4249259Sdim// 5249259Sdim// This file is distributed under the University of Illinois Open Source 6249259Sdim// License. See LICENSE.TXT for details. 7249259Sdim// 8249259Sdim//===----------------------------------------------------------------------===// 9249259Sdim// 10249259Sdim// This file implements several methods that are used to extract functions, 11249259Sdim// loops, or portions of a module from the rest of the module. 12249259Sdim// 13249259Sdim//===----------------------------------------------------------------------===// 14249259Sdim 15249259Sdim#include "BugDriver.h" 16249259Sdim#include "llvm/IR/Constants.h" 17249259Sdim#include "llvm/IR/DataLayout.h" 18249259Sdim#include "llvm/IR/DerivedTypes.h" 19249259Sdim#include "llvm/IR/LLVMContext.h" 20249259Sdim#include "llvm/IR/Module.h" 21249259Sdim#include "llvm/IR/Verifier.h" 22249259Sdim#include "llvm/Pass.h" 23249259Sdim#include "llvm/PassManager.h" 24249259Sdim#include "llvm/Support/CommandLine.h" 25249259Sdim#include "llvm/Support/Debug.h" 26249259Sdim#include "llvm/Support/FileUtilities.h" 27249259Sdim#include "llvm/Support/Path.h" 28263508Sdim#include "llvm/Support/Signals.h" 29263508Sdim#include "llvm/Support/ToolOutputFile.h" 30263508Sdim#include "llvm/Transforms/IPO.h" 31263508Sdim#include "llvm/Transforms/Scalar.h" 32249259Sdim#include "llvm/Transforms/Utils/Cloning.h" 33263508Sdim#include "llvm/Transforms/Utils/CodeExtractor.h" 34249259Sdim#include <set> 35249259Sdimusing namespace llvm; 36249259Sdim 37249259Sdim#define DEBUG_TYPE "bugpoint" 38249259Sdim 39249259Sdimnamespace llvm { 40263508Sdim bool DisableSimplifyCFG = false; 41263508Sdim extern cl::opt<std::string> OutputPrefix; 42263508Sdim} // End llvm namespace 43263508Sdim 44263508Sdimnamespace { 45263508Sdim cl::opt<bool> 46263508Sdim NoDCE ("disable-dce", 47249259Sdim cl::desc("Do not use the -dce pass to reduce testcases")); 48249259Sdim cl::opt<bool, true> 49249259Sdim NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), 50249259Sdim cl::desc("Do not use the -simplifycfg pass to reduce testcases")); 51249259Sdim 52249259Sdim Function* globalInitUsesExternalBA(GlobalVariable* GV) { 53249259Sdim if (!GV->hasInitializer()) 54249259Sdim return nullptr; 55249259Sdim 56263508Sdim Constant *I = GV->getInitializer(); 57263508Sdim 58263508Sdim // walk the values used by the initializer 59249259Sdim // (and recurse into things like ConstantExpr) 60263508Sdim std::vector<Constant*> Todo; 61263508Sdim std::set<Constant*> Done; 62249259Sdim Todo.push_back(I); 63263508Sdim 64249259Sdim while (!Todo.empty()) { 65249259Sdim Constant* V = Todo.back(); 66249259Sdim Todo.pop_back(); 67249259Sdim Done.insert(V); 68249259Sdim 69249259Sdim if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) { 70263508Sdim Function *F = BA->getFunction(); 71263508Sdim if (F->isDeclaration()) 72263508Sdim return F; 73263508Sdim } 74263508Sdim 75263508Sdim for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) { 76263508Sdim Constant *C = dyn_cast<Constant>(*i); 77263508Sdim if (C && !isa<GlobalValue>(C) && !Done.count(C)) 78249259Sdim Todo.push_back(C); 79249259Sdim } 80249259Sdim } 81249259Sdim return nullptr; 82 } 83} // end anonymous namespace 84 85std::unique_ptr<Module> 86BugDriver::deleteInstructionFromProgram(const Instruction *I, 87 unsigned Simplification) { 88 // FIXME, use vmap? 89 Module *Clone = CloneModule(Program); 90 91 const BasicBlock *PBB = I->getParent(); 92 const Function *PF = PBB->getParent(); 93 94 Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn 95 std::advance(RFI, std::distance(PF->getParent()->begin(), 96 Module::const_iterator(PF))); 97 98 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB 99 std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); 100 101 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst 102 std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); 103 Instruction *TheInst = RI; // Got the corresponding instruction! 104 105 // If this instruction produces a value, replace any users with null values 106 if (!TheInst->getType()->isVoidTy()) 107 TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); 108 109 // Remove the instruction from the program. 110 TheInst->getParent()->getInstList().erase(TheInst); 111 112 // Spiff up the output a little bit. 113 std::vector<std::string> Passes; 114 115 /// Can we get rid of the -disable-* options? 116 if (Simplification > 1 && !NoDCE) 117 Passes.push_back("dce"); 118 if (Simplification && !DisableSimplifyCFG) 119 Passes.push_back("simplifycfg"); // Delete dead control flow 120 121 Passes.push_back("verify"); 122 std::unique_ptr<Module> New = runPassesOn(Clone, Passes); 123 delete Clone; 124 if (!New) { 125 errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n"; 126 exit(1); 127 } 128 return New; 129} 130 131std::unique_ptr<Module> 132BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { 133 // Make all functions external, so GlobalDCE doesn't delete them... 134 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 135 I->setLinkage(GlobalValue::ExternalLinkage); 136 137 std::vector<std::string> CleanupPasses; 138 CleanupPasses.push_back("globaldce"); 139 140 if (MayModifySemantics) 141 CleanupPasses.push_back("deadarghaX0r"); 142 else 143 CleanupPasses.push_back("deadargelim"); 144 145 std::unique_ptr<Module> New = runPassesOn(M, CleanupPasses); 146 if (!New) { 147 errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; 148 return nullptr; 149 } 150 delete M; 151 return New; 152} 153 154std::unique_ptr<Module> BugDriver::extractLoop(Module *M) { 155 std::vector<std::string> LoopExtractPasses; 156 LoopExtractPasses.push_back("loop-extract-single"); 157 158 std::unique_ptr<Module> NewM = runPassesOn(M, LoopExtractPasses); 159 if (!NewM) { 160 outs() << "*** Loop extraction failed: "; 161 EmitProgressBitcode(M, "loopextraction", true); 162 outs() << "*** Sorry. :( Please report a bug!\n"; 163 return nullptr; 164 } 165 166 // Check to see if we created any new functions. If not, no loops were 167 // extracted and we should return null. Limit the number of loops we extract 168 // to avoid taking forever. 169 static unsigned NumExtracted = 32; 170 if (M->size() == NewM->size() || --NumExtracted == 0) { 171 return nullptr; 172 } else { 173 assert(M->size() < NewM->size() && "Loop extract removed functions?"); 174 Module::iterator MI = NewM->begin(); 175 for (unsigned i = 0, e = M->size(); i != e; ++i) 176 ++MI; 177 } 178 179 return NewM; 180} 181 182 183// DeleteFunctionBody - "Remove" the function by deleting all of its basic 184// blocks, making it external. 185// 186void llvm::DeleteFunctionBody(Function *F) { 187 // delete the body of the function... 188 F->deleteBody(); 189 assert(F->isDeclaration() && "This didn't make the function external!"); 190} 191 192/// GetTorInit - Given a list of entries for static ctors/dtors, return them 193/// as a constant array. 194static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { 195 assert(!TorList.empty() && "Don't create empty tor list!"); 196 std::vector<Constant*> ArrayElts; 197 Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext()); 198 199 StructType *STy = 200 StructType::get(Int32Ty, TorList[0].first->getType(), NULL); 201 for (unsigned i = 0, e = TorList.size(); i != e; ++i) { 202 Constant *Elts[] = { 203 ConstantInt::get(Int32Ty, TorList[i].second), 204 TorList[i].first 205 }; 206 ArrayElts.push_back(ConstantStruct::get(STy, Elts)); 207 } 208 return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(), 209 ArrayElts.size()), 210 ArrayElts); 211} 212 213/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and 214/// M1 has all of the global variables. If M2 contains any functions that are 215/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and 216/// prune appropriate entries out of M1s list. 217static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, 218 ValueToValueMapTy &VMap) { 219 GlobalVariable *GV = M1->getNamedGlobal(GlobalName); 220 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() || 221 !GV->use_empty()) return; 222 223 std::vector<std::pair<Function*, int> > M1Tors, M2Tors; 224 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 225 if (!InitList) return; 226 227 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 228 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 229 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 230 231 if (CS->getOperand(1)->isNullValue()) 232 break; // Found a null terminator, stop here. 233 234 ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); 235 int Priority = CI ? CI->getSExtValue() : 0; 236 237 Constant *FP = CS->getOperand(1); 238 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 239 if (CE->isCast()) 240 FP = CE->getOperand(0); 241 if (Function *F = dyn_cast<Function>(FP)) { 242 if (!F->isDeclaration()) 243 M1Tors.push_back(std::make_pair(F, Priority)); 244 else { 245 // Map to M2's version of the function. 246 F = cast<Function>(VMap[F]); 247 M2Tors.push_back(std::make_pair(F, Priority)); 248 } 249 } 250 } 251 } 252 253 GV->eraseFromParent(); 254 if (!M1Tors.empty()) { 255 Constant *M1Init = GetTorInit(M1Tors); 256 new GlobalVariable(*M1, M1Init->getType(), false, 257 GlobalValue::AppendingLinkage, 258 M1Init, GlobalName); 259 } 260 261 GV = M2->getNamedGlobal(GlobalName); 262 assert(GV && "Not a clone of M1?"); 263 assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); 264 265 GV->eraseFromParent(); 266 if (!M2Tors.empty()) { 267 Constant *M2Init = GetTorInit(M2Tors); 268 new GlobalVariable(*M2, M2Init->getType(), false, 269 GlobalValue::AppendingLinkage, 270 M2Init, GlobalName); 271 } 272} 273 274 275/// SplitFunctionsOutOfModule - Given a module and a list of functions in the 276/// module, split the functions OUT of the specified module, and place them in 277/// the new module. 278Module * 279llvm::SplitFunctionsOutOfModule(Module *M, 280 const std::vector<Function*> &F, 281 ValueToValueMapTy &VMap) { 282 // Make sure functions & globals are all external so that linkage 283 // between the two modules will work. 284 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 285 I->setLinkage(GlobalValue::ExternalLinkage); 286 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 287 I != E; ++I) { 288 if (I->hasName() && I->getName()[0] == '\01') 289 I->setName(I->getName().substr(1)); 290 I->setLinkage(GlobalValue::ExternalLinkage); 291 } 292 293 ValueToValueMapTy NewVMap; 294 Module *New = CloneModule(M, NewVMap); 295 296 // Remove the Test functions from the Safe module 297 std::set<Function *> TestFunctions; 298 for (unsigned i = 0, e = F.size(); i != e; ++i) { 299 Function *TNOF = cast<Function>(VMap[F[i]]); 300 DEBUG(errs() << "Removing function "); 301 DEBUG(TNOF->printAsOperand(errs(), false)); 302 DEBUG(errs() << "\n"); 303 TestFunctions.insert(cast<Function>(NewVMap[TNOF])); 304 DeleteFunctionBody(TNOF); // Function is now external in this module! 305 } 306 307 308 // Remove the Safe functions from the Test module 309 for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) 310 if (!TestFunctions.count(I)) 311 DeleteFunctionBody(I); 312 313 314 // Try to split the global initializers evenly 315 for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 316 I != E; ++I) { 317 GlobalVariable *GV = cast<GlobalVariable>(NewVMap[I]); 318 if (Function *TestFn = globalInitUsesExternalBA(I)) { 319 if (Function *SafeFn = globalInitUsesExternalBA(GV)) { 320 errs() << "*** Error: when reducing functions, encountered " 321 "the global '"; 322 GV->printAsOperand(errs(), false); 323 errs() << "' with an initializer that references blockaddresses " 324 "from safe function '" << SafeFn->getName() 325 << "' and from test function '" << TestFn->getName() << "'.\n"; 326 exit(1); 327 } 328 I->setInitializer(nullptr); // Delete the initializer to make it external 329 } else { 330 // If we keep it in the safe module, then delete it in the test module 331 GV->setInitializer(nullptr); 332 } 333 } 334 335 // Make sure that there is a global ctor/dtor array in both halves of the 336 // module if they both have static ctor/dtor functions. 337 SplitStaticCtorDtor("llvm.global_ctors", M, New, NewVMap); 338 SplitStaticCtorDtor("llvm.global_dtors", M, New, NewVMap); 339 340 return New; 341} 342 343//===----------------------------------------------------------------------===// 344// Basic Block Extraction Code 345//===----------------------------------------------------------------------===// 346 347std::unique_ptr<Module> 348BugDriver::extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs, 349 Module *M) { 350 SmallString<128> Filename; 351 int FD; 352 std::error_code EC = sys::fs::createUniqueFile( 353 OutputPrefix + "-extractblocks%%%%%%%", FD, Filename); 354 if (EC) { 355 outs() << "*** Basic Block extraction failed!\n"; 356 errs() << "Error creating temporary file: " << EC.message() << "\n"; 357 EmitProgressBitcode(M, "basicblockextractfail", true); 358 return nullptr; 359 } 360 sys::RemoveFileOnSignal(Filename); 361 362 tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD); 363 for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); 364 I != E; ++I) { 365 BasicBlock *BB = *I; 366 // If the BB doesn't have a name, give it one so we have something to key 367 // off of. 368 if (!BB->hasName()) BB->setName("tmpbb"); 369 BlocksToNotExtractFile.os() << BB->getParent()->getName() << " " 370 << BB->getName() << "\n"; 371 } 372 BlocksToNotExtractFile.os().close(); 373 if (BlocksToNotExtractFile.os().has_error()) { 374 errs() << "Error writing list of blocks to not extract\n"; 375 EmitProgressBitcode(M, "basicblockextractfail", true); 376 BlocksToNotExtractFile.os().clear_error(); 377 return nullptr; 378 } 379 BlocksToNotExtractFile.keep(); 380 381 std::string uniqueFN = "--extract-blocks-file="; 382 uniqueFN += Filename.str(); 383 const char *ExtraArg = uniqueFN.c_str(); 384 385 std::vector<std::string> PI; 386 PI.push_back("extract-blocks"); 387 std::unique_ptr<Module> Ret = runPassesOn(M, PI, false, 1, &ExtraArg); 388 389 sys::fs::remove(Filename.c_str()); 390 391 if (!Ret) { 392 outs() << "*** Basic Block extraction failed, please report a bug!\n"; 393 EmitProgressBitcode(M, "basicblockextractfail", true); 394 } 395 return Ret; 396} 397