xref: /llvm-project/llvm/tools/llvm-reduce/deltas/ReduceInstructions.cpp (revision f0fa2d7c292853b79b5bcd16be97940859a800ec)
16f3f19a3SMatt Arsenault //===- ReduceInstructions.cpp - Specialized Delta Pass ---------------------===//
2798fe477SDavid Blaikie //
3798fe477SDavid Blaikie // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4798fe477SDavid Blaikie // See https://llvm.org/LICENSE.txt for license information.
5798fe477SDavid Blaikie // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6798fe477SDavid Blaikie //
7798fe477SDavid Blaikie //===----------------------------------------------------------------------===//
8798fe477SDavid Blaikie //
9798fe477SDavid Blaikie // This file implements a function which calls the Generic Delta pass in order
106f3f19a3SMatt Arsenault // to reduce uninteresting Instructions from defined functions.
11798fe477SDavid Blaikie //
12798fe477SDavid Blaikie //===----------------------------------------------------------------------===//
13798fe477SDavid Blaikie 
14798fe477SDavid Blaikie #include "ReduceInstructions.h"
152962f9dfSJohn Regehr #include "Utils.h"
16a5bbc6efSBill Wendling #include "llvm/IR/Constants.h"
17*f0fa2d7cSElliot Goodrich #include <set>
18798fe477SDavid Blaikie 
19798fe477SDavid Blaikie using namespace llvm;
20798fe477SDavid Blaikie 
21f4cb935cSMatt Arsenault /// Filter out cases where deleting the instruction will likely cause the
22f4cb935cSMatt Arsenault /// user/def of the instruction to fail the verifier.
23f4cb935cSMatt Arsenault //
24f4cb935cSMatt Arsenault // TODO: Technically the verifier only enforces preallocated token usage and
25f4cb935cSMatt Arsenault // there is a none token.
shouldAlwaysKeep(const Instruction & I)26f4cb935cSMatt Arsenault static bool shouldAlwaysKeep(const Instruction &I) {
27f4cb935cSMatt Arsenault   return I.isEHPad() || I.getType()->isTokenTy() || I.isSwiftError();
28f4cb935cSMatt Arsenault }
29f4cb935cSMatt Arsenault 
30798fe477SDavid Blaikie /// Removes out-of-chunk arguments from functions, and modifies their calls
31798fe477SDavid Blaikie /// accordingly. It also removes allocations of out-of-chunk arguments.
extractInstrFromModule(Oracle & O,ReducerWorkItem & WorkItem)3223cc36e4SMatt Arsenault static void extractInstrFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
3323cc36e4SMatt Arsenault   Module &Program = WorkItem.getModule();
342f161736SDwight Guth   std::vector<Instruction *> InitInstToKeep;
35798fe477SDavid Blaikie 
3677bc3ba3SArthur Eubanks   for (auto &F : Program)
3751a82828SFlorian Hahn     for (auto &BB : F) {
3851a82828SFlorian Hahn       // Removing the terminator would make the block invalid. Only iterate over
3951a82828SFlorian Hahn       // instructions before the terminator.
402f161736SDwight Guth       InitInstToKeep.push_back(BB.getTerminator());
41f4cb935cSMatt Arsenault       for (auto &Inst : make_range(BB.begin(), std::prev(BB.end()))) {
42f4cb935cSMatt Arsenault         if (shouldAlwaysKeep(Inst) || O.shouldKeep())
432f161736SDwight Guth           InitInstToKeep.push_back(&Inst);
4451a82828SFlorian Hahn       }
45f4cb935cSMatt Arsenault     }
46798fe477SDavid Blaikie 
472f161736SDwight Guth   // We create a vector first, then convert it to a set, so that we don't have
482f161736SDwight Guth   // to pay the cost of rebalancing the set frequently if the order we insert
492f161736SDwight Guth   // the elements doesn't match the order they should appear inside the set.
502f161736SDwight Guth   std::set<Instruction *> InstToKeep(InitInstToKeep.begin(),
512f161736SDwight Guth                                      InitInstToKeep.end());
522f161736SDwight Guth 
53798fe477SDavid Blaikie   std::vector<Instruction *> InstToDelete;
5477bc3ba3SArthur Eubanks   for (auto &F : Program)
55798fe477SDavid Blaikie     for (auto &BB : F)
56798fe477SDavid Blaikie       for (auto &Inst : BB)
57798fe477SDavid Blaikie         if (!InstToKeep.count(&Inst)) {
582962f9dfSJohn Regehr           Inst.replaceAllUsesWith(getDefaultValue(Inst.getType()));
59798fe477SDavid Blaikie           InstToDelete.push_back(&Inst);
60798fe477SDavid Blaikie         }
61798fe477SDavid Blaikie 
62798fe477SDavid Blaikie   for (auto &I : InstToDelete)
63798fe477SDavid Blaikie     I->eraseFromParent();
64798fe477SDavid Blaikie }
65798fe477SDavid Blaikie 
reduceInstructionsDeltaPass(TestRunner & Test)66798fe477SDavid Blaikie void llvm::reduceInstructionsDeltaPass(TestRunner &Test) {
672592ccdeSArthur Eubanks   runDeltaPass(Test, extractInstrFromModule, "Reducing Instructions");
68798fe477SDavid Blaikie }
69