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 "llvm/Analysis/TargetTransformInfo.h" |
16 | #include "llvm/IR/Constants.h" |
17 | #include "llvm/IR/Instructions.h" |
18 | #include "llvm/Transforms/Utils/Local.h" |
19 | |
20 | using namespace llvm; |
21 | |
22 | static void reduceUsingSimplifyCFG(Oracle &O, ReducerWorkItem &WorkItem) { |
23 | Module &Program = WorkItem.getModule(); |
24 | SmallVector<BasicBlock *, 16> ToSimplify; |
25 | for (auto &F : Program) |
26 | for (auto &BB : F) |
27 | if (!O.shouldKeep()) |
28 | ToSimplify.push_back(Elt: &BB); |
29 | TargetTransformInfo TTI(Program.getDataLayout()); |
30 | for (auto *BB : ToSimplify) |
31 | simplifyCFG(BB, TTI); |
32 | } |
33 | |
34 | void llvm::reduceUsingSimplifyCFGDeltaPass(TestRunner &Test) { |
35 | runDeltaPass(Test, ExtractChunksFromModule: reduceUsingSimplifyCFG, Message: "Reducing using SimplifyCFG" ); |
36 | } |
37 | static void reduceConditionals(Oracle &O, ReducerWorkItem &WorkItem, |
38 | bool Direction) { |
39 | Module &M = WorkItem.getModule(); |
40 | SmallVector<BasicBlock *, 16> ToSimplify; |
41 | |
42 | for (auto &F : M) { |
43 | for (auto &BB : F) { |
44 | auto *BR = dyn_cast<BranchInst>(Val: BB.getTerminator()); |
45 | if (!BR || !BR->isConditional() || O.shouldKeep()) |
46 | continue; |
47 | |
48 | if (Direction) |
49 | BR->setCondition(ConstantInt::getTrue(Context&: BR->getContext())); |
50 | else |
51 | BR->setCondition(ConstantInt::getFalse(Context&: BR->getContext())); |
52 | |
53 | ToSimplify.push_back(Elt: &BB); |
54 | } |
55 | } |
56 | |
57 | TargetTransformInfo TTI(M.getDataLayout()); |
58 | for (auto *BB : ToSimplify) |
59 | simplifyCFG(BB, TTI); |
60 | } |
61 | |
62 | void llvm::reduceConditionalsTrueDeltaPass(TestRunner &Test) { |
63 | runDeltaPass( |
64 | Test, |
65 | ExtractChunksFromModule: [](Oracle &O, ReducerWorkItem &WorkItem) { |
66 | reduceConditionals(O, WorkItem, Direction: true); |
67 | }, |
68 | Message: "Reducing conditional branches to true" ); |
69 | } |
70 | |
71 | void llvm::reduceConditionalsFalseDeltaPass(TestRunner &Test) { |
72 | runDeltaPass( |
73 | Test, |
74 | ExtractChunksFromModule: [](Oracle &O, ReducerWorkItem &WorkItem) { |
75 | reduceConditionals(O, WorkItem, Direction: false); |
76 | }, |
77 | Message: "Reducing conditional branches to false" ); |
78 | } |
79 | |