2019-08-09 06:16:33 +08:00
|
|
|
//===- ReduceFunctions.cpp - Specialized Delta Pass -----------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a function which calls the Generic Delta pass in order
|
|
|
|
// to reduce functions (and any instruction that calls it) in the provided
|
|
|
|
// Module.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ReduceFunctions.h"
|
2019-09-11 06:10:00 +08:00
|
|
|
#include "Delta.h"
|
2019-09-12 04:29:22 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2019-09-11 06:31:35 +08:00
|
|
|
#include <set>
|
2019-09-11 06:10:00 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2019-08-09 06:16:33 +08:00
|
|
|
|
|
|
|
/// Removes all the Defined Functions (as well as their calls)
|
|
|
|
/// that aren't inside any of the desired Chunks.
|
|
|
|
static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep,
|
|
|
|
Module *Program) {
|
|
|
|
// Get functions inside desired chunks
|
|
|
|
std::set<Function *> FuncsToKeep;
|
|
|
|
unsigned I = 0, FunctionCount = 0;
|
|
|
|
for (auto &F : *Program)
|
2019-08-15 06:22:37 +08:00
|
|
|
if (I < ChunksToKeep.size()) {
|
2019-08-09 06:16:33 +08:00
|
|
|
if (ChunksToKeep[I].contains(++FunctionCount))
|
|
|
|
FuncsToKeep.insert(&F);
|
|
|
|
if (FunctionCount == ChunksToKeep[I].end)
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete out-of-chunk functions, and replace their calls with undef
|
|
|
|
std::vector<Function *> FuncsToRemove;
|
2019-09-12 04:29:22 +08:00
|
|
|
SetVector<CallInst *> CallsToRemove;
|
2019-08-09 06:16:33 +08:00
|
|
|
for (auto &F : *Program)
|
2019-08-15 06:22:37 +08:00
|
|
|
if (!FuncsToKeep.count(&F)) {
|
|
|
|
for (auto U : F.users())
|
|
|
|
if (auto *Call = dyn_cast<CallInst>(U)) {
|
|
|
|
Call->replaceAllUsesWith(UndefValue::get(Call->getType()));
|
2019-09-12 04:29:22 +08:00
|
|
|
CallsToRemove.insert(Call);
|
2019-08-15 06:22:37 +08:00
|
|
|
}
|
2019-08-09 06:16:33 +08:00
|
|
|
F.replaceAllUsesWith(UndefValue::get(F.getType()));
|
|
|
|
FuncsToRemove.push_back(&F);
|
|
|
|
}
|
|
|
|
|
2019-08-15 06:22:37 +08:00
|
|
|
for (auto *C : CallsToRemove)
|
|
|
|
C->eraseFromParent();
|
|
|
|
|
2019-08-09 06:16:33 +08:00
|
|
|
for (auto *F : FuncsToRemove)
|
|
|
|
F->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Counts the amount of non-declaration functions and prints their
|
|
|
|
/// respective name & index
|
2019-08-15 06:22:37 +08:00
|
|
|
static unsigned countFunctions(Module *Program) {
|
2019-08-09 06:16:33 +08:00
|
|
|
// TODO: Silence index with --quiet flag
|
2019-08-16 06:39:43 +08:00
|
|
|
errs() << "----------------------------\n";
|
|
|
|
errs() << "Function Index Reference:\n";
|
2019-08-09 06:16:33 +08:00
|
|
|
unsigned FunctionCount = 0;
|
|
|
|
for (auto &F : *Program)
|
2019-08-16 06:39:43 +08:00
|
|
|
errs() << "\t" << ++FunctionCount << ": " << F.getName() << "\n";
|
2019-08-09 06:16:33 +08:00
|
|
|
|
2019-08-16 06:39:43 +08:00
|
|
|
errs() << "----------------------------\n";
|
2019-08-09 06:16:33 +08:00
|
|
|
return FunctionCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::reduceFunctionsDeltaPass(TestRunner &Test) {
|
2019-08-16 06:39:43 +08:00
|
|
|
errs() << "*** Reducing Functions...\n";
|
2019-08-15 06:22:37 +08:00
|
|
|
unsigned Functions = countFunctions(Test.getProgram());
|
2019-08-09 06:16:33 +08:00
|
|
|
runDeltaPass(Test, Functions, extractFunctionsFromModule);
|
2019-08-16 06:39:43 +08:00
|
|
|
errs() << "----------------------------\n";
|
2019-09-11 06:10:00 +08:00
|
|
|
}
|