55 lines · cpp
1//===- ReduceFunctions.cpp - Specialized Delta Pass -----------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements a function which calls the Generic Delta pass in order10// to reduce functions (and any instruction that calls it) in the provided11// Module.12//13//===----------------------------------------------------------------------===//14 15#include "ReduceFunctions.h"16#include "Utils.h"17#include "llvm/Transforms/Utils/Cloning.h"18#include "llvm/Transforms/Utils/ModuleUtils.h"19 20using namespace llvm;21 22/// Removes all the Defined Functions23/// that aren't inside any of the desired Chunks.24void llvm::reduceFunctionsDeltaPass(Oracle &O, ReducerWorkItem &WorkItem) {25 Module &Program = WorkItem.getModule();26 27 // Record all out-of-chunk functions.28 SmallPtrSet<Constant *, 8> FuncsToRemove;29 for (Function &F : Program.functions()) {30 // Intrinsics don't have function bodies that are useful to31 // reduce. Additionally, intrinsics may have additional operand32 // constraints. But, do drop intrinsics that are not referenced.33 if ((!F.isIntrinsic() || F.use_empty()) && !hasAliasUse(F) &&34 !O.shouldKeep())35 FuncsToRemove.insert(&F);36 }37 38 removeFromUsedLists(Program, [&FuncsToRemove](Constant *C) {39 return FuncsToRemove.count(C);40 });41 42 // Then, drop body of each of them. We want to batch this and do nothing else43 // here so that minimal number of remaining external uses will remain.44 for (Constant *F : FuncsToRemove)45 F->dropAllReferences();46 47 // And finally, we can actually delete them.48 for (Constant *F : FuncsToRemove) {49 // Replace all *still* remaining uses with the default value.50 F->replaceAllUsesWith(getDefaultValue(F->getType()));51 // And finally, fully drop it.52 cast<Function>(F)->eraseFromParent();53 }54}55