1//===- ReduceInstructions.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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a function which calls the Generic Delta pass in order
10// to reduce uninteresting Instructions from defined functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ReduceInstructions.h"
15#include "Utils.h"
16
17using namespace llvm;
18
19/// Filter out cases where deleting the instruction will likely cause the
20/// user/def of the instruction to fail the verifier.
21//
22// TODO: Technically the verifier only enforces preallocated token usage and
23// there is a none token.
24static bool shouldAlwaysKeep(const Instruction &I) {
25 return I.isEHPad() || I.getType()->isTokenTy() || I.isSwiftError();
26}
27
28/// Removes out-of-chunk arguments from functions, and modifies their calls
29/// accordingly. It also removes allocations of out-of-chunk arguments.
30void llvm::reduceInstructionsDeltaPass(Oracle &O, ReducerWorkItem &WorkItem) {
31 Module &Program = WorkItem.getModule();
32
33 for (auto &F : Program) {
34 for (auto &BB : F) {
35 // Removing the terminator would make the block invalid. Only iterate over
36 // instructions before the terminator.
37 for (auto &Inst :
38 make_early_inc_range(Range: make_range(x: BB.begin(), y: std::prev(x: BB.end())))) {
39 if (!shouldAlwaysKeep(I: Inst) && !O.shouldKeep()) {
40 Inst.replaceAllUsesWith(V: getDefaultValue(T: Inst.getType()));
41 Inst.eraseFromParent();
42 }
43 }
44 }
45 }
46}
47