1//===- AssumeBundleBuilder.h - utils to build assume bundles ----*- C++ -*-===//
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 contain tools to preserve informations. They should be used before
10// performing a transformation that may move and delete instructions as those
11// transformation may destroy or worsen information that can be derived from the
12// IR.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_TRANSFORMS_UTILS_ASSUMEBUNDLEBUILDER_H
17#define LLVM_TRANSFORMS_UTILS_ASSUMEBUNDLEBUILDER_H
18
19#include "llvm/Analysis/AssumeBundleQueries.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/Compiler.h"
23
24namespace llvm {
25class AssumeInst;
26class Function;
27class Instruction;
28class AssumptionCache;
29class DominatorTree;
30
31LLVM_ABI extern cl::opt<bool> EnableKnowledgeRetention;
32
33/// Build a call to llvm.assume to preserve informations that can be derived
34/// from the given instruction.
35/// If no information derived from \p I, this call returns null.
36/// The returned instruction is not inserted anywhere.
37LLVM_ABI AssumeInst *buildAssumeFromInst(Instruction *I);
38
39/// Calls BuildAssumeFromInst and if the resulting llvm.assume is valid insert
40/// if before I. This is usually what need to be done to salvage the knowledge
41/// contained in the instruction I.
42/// The AssumptionCache must be provided if it is available or the cache may
43/// become silently be invalid.
44/// The DominatorTree can optionally be provided to enable cross-block
45/// reasoning.
46/// This returns if a change was made.
47LLVM_ABI bool salvageKnowledge(Instruction *I, AssumptionCache *AC = nullptr,
48 DominatorTree *DT = nullptr);
49
50/// Build and return a new assume created from the provided knowledge
51/// if the knowledge in the assume is fully redundant this will return nullptr
52LLVM_ABI AssumeInst *
53buildAssumeFromKnowledge(ArrayRef<RetainedKnowledge> Knowledge,
54 Instruction *CtxI, AssumptionCache *AC = nullptr,
55 DominatorTree *DT = nullptr);
56
57/// This pass attempts to minimize the number of assume without loosing any
58/// information.
59struct AssumeSimplifyPass : public PassInfoMixin<AssumeSimplifyPass> {
60 LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
61};
62
63/// This pass will try to build an llvm.assume for every instruction in the
64/// function. Its main purpose is testing.
65struct AssumeBuilderPass : public PassInfoMixin<AssumeBuilderPass> {
66 LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
67};
68
69/// canonicalize the RetainedKnowledge RK. it is assumed that RK is part of
70/// Assume. This will return an empty RetainedKnowledge if the knowledge is
71/// useless.
72LLVM_ABI RetainedKnowledge simplifyRetainedKnowledge(AssumeInst *Assume,
73 RetainedKnowledge RK,
74 AssumptionCache *AC,
75 DominatorTree *DT);
76
77} // namespace llvm
78
79#endif
80