| 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 |
|
| 18 |
using namespace llvm; |
| 19 |
|
| 20 |
/// Filter out cases where deleting the instruction will likely cause the |
| 21 |
/// user/def of the instruction to fail the verifier. |
| 22 |
// |
| 23 |
// TODO: Technically the verifier only enforces preallocated token usage and |
| 24 |
// there is a none token. |
| 25 |
static bool shouldAlwaysKeep(const Instruction &I) { |
| 26 |
return I.isEHPad() || I.getType()->isTokenTy() || I.isSwiftError(); |
| 27 |
} |
| 28 |
|
| 29 |
/// Removes out-of-chunk arguments from functions, and modifies their calls |
| 30 |
/// accordingly. It also removes allocations of out-of-chunk arguments. |
| 31 |
static void extractInstrFromModule(Oracle &O, ReducerWorkItem &WorkItem) { |
| 32 |
Module &Program = WorkItem.getModule(); |
| 33 |
std::vector<Instruction *> InitInstToKeep; |
| 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 |
InitInstToKeep.push_back(BB.getTerminator()); |
| 40 |
for (auto &Inst : make_range(BB.begin(), std::prev(BB.end()))) { |
| 41 |
if (shouldAlwaysKeep(Inst) || O.shouldKeep()) |
| 42 |
InitInstToKeep.push_back(&Inst); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// We create a vector first, then convert it to a set, so that we don't have |
| 47 |
// to pay the cost of rebalancing the set frequently if the order we insert |
| 48 |
// the elements doesn't match the order they should appear inside the set. |
| 49 |
std::set<Instruction *> InstToKeep(InitInstToKeep.begin(), |
| 50 |
InitInstToKeep.end()); |
| 51 |
|
| 52 |
std::vector<Instruction *> InstToDelete; |
| 53 |
for (auto &F : Program) |
| 54 |
for (auto &BB : F) |
| 55 |
for (auto &Inst : BB) |
| 56 |
if (!InstToKeep.count(&Inst)) { |
| 57 |
Inst.replaceAllUsesWith(getDefaultValue(Inst.getType())); |
| 58 |
InstToDelete.push_back(&Inst); |
| 59 |
} |
| 60 |
|
| 61 |
for (auto &I : InstToDelete) |
| 62 |
I->eraseFromParent(); |
| 63 |
} |
| 64 |
|
| 65 |
void llvm::reduceInstructionsDeltaPass(TestRunner &Test) { |
| 66 |
runDeltaPass(Test, extractInstrFromModule, "Reducing Instructions"); |
| 67 |
} |