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"
|
2020-08-09 04:21:08 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-06-24 22:12:11 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2020-08-09 04:21:08 +08:00
|
|
|
#include <iterator>
|
|
|
|
#include <vector>
|
2019-09-11 06:10:00 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2019-08-09 06:16:33 +08:00
|
|
|
|
2020-08-09 04:21:08 +08:00
|
|
|
/// Removes all the Defined Functions
|
2019-08-09 06:16:33 +08:00
|
|
|
/// that aren't inside any of the desired Chunks.
|
|
|
|
static void extractFunctionsFromModule(const std::vector<Chunk> &ChunksToKeep,
|
|
|
|
Module *Program) {
|
2020-07-08 17:05:15 +08:00
|
|
|
Oracle O(ChunksToKeep);
|
|
|
|
|
2020-08-09 04:21:08 +08:00
|
|
|
// Record all out-of-chunk functions.
|
|
|
|
std::vector<std::reference_wrapper<Function>> FuncsToRemove;
|
|
|
|
copy_if(Program->functions(), std::back_inserter(FuncsToRemove),
|
2020-10-02 01:44:59 +08:00
|
|
|
[&O](Function &F) {
|
|
|
|
// Intrinsics don't have function bodies that are useful to
|
|
|
|
// reduce. Additionally, intrinsics may have additional operand
|
2021-01-03 00:29:01 +08:00
|
|
|
// constraints. But, do drop intrinsics that are not referenced.
|
|
|
|
return (!F.isIntrinsic() || F.use_empty()) && !O.shouldKeep();
|
2020-10-02 01:44:59 +08:00
|
|
|
});
|
2019-08-09 06:16:33 +08:00
|
|
|
|
2020-08-09 04:21:08 +08:00
|
|
|
// Then, drop body of each of them. We want to batch this and do nothing else
|
|
|
|
// here so that minimal number of remaining exteranal uses will remain.
|
|
|
|
for (Function &F : FuncsToRemove)
|
|
|
|
F.dropAllReferences();
|
2019-08-15 06:22:37 +08:00
|
|
|
|
2020-08-09 04:21:08 +08:00
|
|
|
// And finally, we can actually delete them.
|
|
|
|
for (Function &F : FuncsToRemove) {
|
|
|
|
// Replace all *still* remaining uses with undef.
|
|
|
|
F.replaceAllUsesWith(UndefValue::get(F.getType()));
|
|
|
|
// And finally, fully drop it.
|
|
|
|
F.eraseFromParent();
|
|
|
|
}
|
2019-08-09 06:16:33 +08:00
|
|
|
}
|
|
|
|
|
2021-01-03 00:29:01 +08:00
|
|
|
/// Counts the amount of functions and prints their
|
2019-08-09 06:16:33 +08:00
|
|
|
/// respective name & index
|
2019-09-19 06:30:25 +08:00
|
|
|
static int 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-09-19 06:30:25 +08:00
|
|
|
int FunctionCount = 0;
|
2020-10-02 01:44:59 +08:00
|
|
|
for (auto &F : *Program) {
|
2021-01-03 00:29:01 +08:00
|
|
|
if (F.isIntrinsic() && !F.use_empty())
|
2020-10-02 01:44:59 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
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-09-19 06:30:25 +08:00
|
|
|
int 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
|
|
|
}
|