aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/tools/llvm-reduce/deltas/ReduceUsingSimplifyCFG.cpp
blob: c49fcb9855d416eebc183c1953cbb97d5e8da39e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//===- ReduceUsingSimplifyCFG.h - Specialized Delta Pass ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a function which calls the Generic Delta pass in order
// to call SimplifyCFG on individual basic blocks.
//
//===----------------------------------------------------------------------===//

#include "ReduceUsingSimplifyCFG.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Transforms/Utils/Local.h"

using namespace llvm;

static void reduceUsingSimplifyCFG(Oracle &O, ReducerWorkItem &WorkItem) {
  Module &Program = WorkItem.getModule();
  SmallVector<BasicBlock *, 16> ToSimplify;
  for (auto &F : Program)
    for (auto &BB : F)
      if (!O.shouldKeep())
        ToSimplify.push_back(&BB);
  TargetTransformInfo TTI(Program.getDataLayout());
  for (auto *BB : ToSimplify)
    simplifyCFG(BB, TTI);
}

void llvm::reduceUsingSimplifyCFGDeltaPass(TestRunner &Test) {
  runDeltaPass(Test, reduceUsingSimplifyCFG, "Reducing using SimplifyCFG");
}
static void reduceConditionals(Oracle &O, ReducerWorkItem &WorkItem,
                               bool Direction) {
  Module &M = WorkItem.getModule();
  SmallVector<BasicBlock *, 16> ToSimplify;

  for (auto &F : M) {
    for (auto &BB : F) {
      auto *BR = dyn_cast<BranchInst>(BB.getTerminator());
      if (!BR || !BR->isConditional() || O.shouldKeep())
        continue;

      if (Direction)
        BR->setCondition(ConstantInt::getTrue(BR->getContext()));
      else
        BR->setCondition(ConstantInt::getFalse(BR->getContext()));

      ToSimplify.push_back(&BB);
    }
  }

  TargetTransformInfo TTI(M.getDataLayout());
  for (auto *BB : ToSimplify)
    simplifyCFG(BB, TTI);
}

void llvm::reduceConditionalsTrueDeltaPass(TestRunner &Test) {
  runDeltaPass(
      Test,
      [](Oracle &O, ReducerWorkItem &WorkItem) {
        reduceConditionals(O, WorkItem, true);
      },
      "Reducing conditional branches to true");
}

void llvm::reduceConditionalsFalseDeltaPass(TestRunner &Test) {
  runDeltaPass(
      Test,
      [](Oracle &O, ReducerWorkItem &WorkItem) {
        reduceConditionals(O, WorkItem, false);
      },
      "Reducing conditional branches to false");
}