| 1 | //===- ReduceUsingSimplifyCFG.h - 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 call SimplifyCFG on individual basic blocks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ReduceUsingSimplifyCFG.h" |
| 15 | #include "Utils.h" |
| 16 | #include "llvm/Analysis/TargetTransformInfo.h" |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Instructions.h" |
| 19 | #include "llvm/Transforms/Utils/Local.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | void llvm::reduceUsingSimplifyCFGDeltaPass(Oracle &O, |
| 24 | ReducerWorkItem &WorkItem) { |
| 25 | Module &Program = WorkItem.getModule(); |
| 26 | SmallVector<BasicBlock *, 16> ToSimplify; |
| 27 | for (auto &F : Program) |
| 28 | for (auto &BB : F) |
| 29 | if (!O.shouldKeep()) |
| 30 | ToSimplify.push_back(Elt: &BB); |
| 31 | TargetTransformInfo TTI(Program.getDataLayout()); |
| 32 | for (auto *BB : ToSimplify) |
| 33 | simplifyCFG(BB, TTI); |
| 34 | } |
| 35 | |
| 36 | static void reduceConditionals(Oracle &O, ReducerWorkItem &WorkItem, |
| 37 | bool Direction) { |
| 38 | Module &M = WorkItem.getModule(); |
| 39 | |
| 40 | LLVMContext &Ctx = M.getContext(); |
| 41 | ConstantInt *ConstValToSet = |
| 42 | Direction ? ConstantInt::getTrue(Context&: Ctx) : ConstantInt::getFalse(Context&: Ctx); |
| 43 | |
| 44 | for (Function &F : M) { |
| 45 | if (F.isDeclaration()) |
| 46 | continue; |
| 47 | |
| 48 | SmallVector<BasicBlock *, 16> ToSimplify; |
| 49 | |
| 50 | for (auto &BB : F) { |
| 51 | auto *BR = dyn_cast<BranchInst>(Val: BB.getTerminator()); |
| 52 | if (!BR || !BR->isConditional() || BR->getCondition() == ConstValToSet || |
| 53 | O.shouldKeep()) |
| 54 | continue; |
| 55 | |
| 56 | BR->setCondition(ConstValToSet); |
| 57 | ToSimplify.push_back(Elt: &BB); |
| 58 | } |
| 59 | |
| 60 | if (!ToSimplify.empty()) { |
| 61 | // TODO: Should probably leave MergeBlockIntoPredecessor for a separate |
| 62 | // reduction |
| 63 | simpleSimplifyCFG(F, BBs: ToSimplify); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void llvm::reduceConditionalsTrueDeltaPass(Oracle &O, |
| 69 | ReducerWorkItem &WorkItem) { |
| 70 | reduceConditionals(O, WorkItem, Direction: true); |
| 71 | } |
| 72 | |
| 73 | void llvm::reduceConditionalsFalseDeltaPass(Oracle &O, |
| 74 | ReducerWorkItem &WorkItem) { |
| 75 | reduceConditionals(O, WorkItem, Direction: false); |
| 76 | } |
| 77 | |