Debian Codegraph @

llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp

raw
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 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18
19 using namespace llvm;
20
21 /// Filter out cases where deleting the instruction will likely cause the
22 /// user/def of the instruction to fail the verifier.
23 //
24 // TODO: Technically the verifier only enforces preallocated token usage and
25 // there is a none token.
26 static bool shouldAlwaysKeep(const Instruction &I) {
27 return I.isEHPad() || I.getType()->isTokenTy() || I.isSwiftError();
28 }
29
30 /// Removes out-of-chunk arguments from functions, and modifies their calls
31 /// accordingly. It also removes allocations of out-of-chunk arguments.
32 void llvm::reduceInstructionsDeltaPass(Oracle &O, ReducerWorkItem &WorkItem) {
33 Module &Program = WorkItem.getModule();
34
35 for (auto &F : Program) {
36 for (auto &BB : F) {
37 // Removing the terminator would make the block invalid. Only iterate over
38 // instructions before the terminator.
39 for (auto &Inst :
40 make_early_inc_range(make_range(BB.begin(), std::prev(BB.end())))) {
41 if (!shouldAlwaysKeep(Inst) && !O.shouldKeep()) {
42 Inst.replaceAllUsesWith(isa<AllocaInst>(Inst)
43 ? PoisonValue::get(Inst.getType())
44 : getDefaultValue(Inst.getType()));
45 Inst.eraseFromParent();
46 }
47 }
48 }
49 }
50 }