LLVM 23.0.0git
PredicateInfo.h
Go to the documentation of this file.
1//===- PredicateInfo.h - Build PredicateInfo ----------------------*-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/// \file
10/// This file implements the PredicateInfo analysis, which creates an Extended
11/// SSA form for operations used in branch comparisons and llvm.assume
12/// comparisons.
13///
14/// Copies of these operations are inserted into the true/false edge (and after
15/// assumes), and information attached to the copies. All uses of the original
16/// operation in blocks dominated by the true/false edge (and assume), are
17/// replaced with uses of the copies. This enables passes to easily and sparsely
18/// propagate condition based info into the operations that may be affected.
19///
20/// Example:
21/// %cmp = icmp eq i32 %x, 50
22/// br i1 %cmp, label %true, label %false
23/// true:
24/// ret i32 %x
25/// false:
26/// ret i32 1
27///
28/// will become
29///
30/// %cmp = icmp eq i32, %x, 50
31/// br i1 %cmp, label %true, label %false
32/// true:
33/// %x.0 = bitcast i32 %x to %x
34/// ret i32 %x.0
35/// false:
36/// ret i32 1
37///
38/// Using getPredicateInfoFor on x.0 will give you the comparison it is
39/// dominated by (the icmp), and that you are located in the true edge of that
40/// comparison, which tells you x.0 is 50.
41///
42/// In order to reduce the number of copies inserted, predicateinfo is only
43/// inserted where it would actually be live. This means if there are no uses of
44/// an operation dominated by the branch edges, or by an assume, the associated
45/// predicate info is never inserted.
46///
47///
48//===----------------------------------------------------------------------===//
49
50#ifndef LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
51#define LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
52
53#include "llvm/ADT/DenseMap.h"
54#include "llvm/ADT/SmallSet.h"
56#include "llvm/IR/PassManager.h"
57#include "llvm/IR/ValueHandle.h"
60
61namespace llvm {
62
63class AssumptionCache;
64class DominatorTree;
65class Function;
66class Value;
67class IntrinsicInst;
68class raw_ostream;
69
76
77/// Constraint for a predicate of the form "cmp Pred Op, OtherOp", where Op
78/// is the value the constraint applies to (the bitcast result).
83
84// Base class for all predicate information we provide.
85// All of our predicate information has at least a comparison.
87public:
89 // The original operand before we renamed it.
90 // This can be use by passes, when destroying predicateinfo, to know
91 // whether they can just drop the intrinsic, or have to merge metadata.
93 // The renamed operand in the condition used for this predicate. For nested
94 // predicates, this is different to OriginalOp which refers to the initial
95 // operand.
97 // The condition associated with this predicate.
99
100 PredicateBase(const PredicateBase &) = delete;
102 PredicateBase() = delete;
103 static bool classof(const PredicateBase *PB) {
104 return PB->Type == PT_BundleAssume || PB->Type == PT_ConditionAssume ||
105 PB->Type == PT_Branch || PB->Type == PT_Switch;
106 }
107
108 /// Fetch condition in the form of PredicateConstraint, if possible.
109 LLVM_ABI std::optional<PredicateConstraint> getConstraint() const;
110
111protected:
114};
115
116// Provides predicate information for assumes. Since assumes are always true,
117// we simply provide the assume instruction, so you can tell your relative
118// position to it.
120public:
122
123 static bool classof(const PredicateBase *PB) {
124 return PB->Type == PT_ConditionAssume || PB->Type == PT_BundleAssume;
125 }
126
127protected:
131};
132
145
156
157// Mixin class for edge predicates. The FROM block is the block where the
158// predicate originates, and the TO block is the block where the predicate is
159// valid.
161public:
165 static bool classof(const PredicateBase *PB) {
166 return PB->Type == PT_Branch || PB->Type == PT_Switch;
167 }
168
169protected:
173};
174
175// Provides predicate information for branches.
177public:
178 // If true, SplitBB is the true successor, otherwise it's the false successor.
181 Value *Condition, bool TakenEdge)
182 : PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition),
183 TrueEdge(TakenEdge) {}
184 PredicateBranch() = delete;
185 static bool classof(const PredicateBase *PB) {
186 return PB->Type == PT_Branch;
187 }
188};
189
191public:
193 // This is the switch instruction.
197 : PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB,
198 SI->getCondition()),
200 PredicateSwitch() = delete;
201 static bool classof(const PredicateBase *PB) {
202 return PB->Type == PT_Switch;
203 }
204};
205
206/// Encapsulates PredicateInfo, including all data associated with memory
207/// accesses.
209public:
212
213 LLVM_ABI void verifyPredicateInfo() const;
214
215 LLVM_ABI void dump() const;
216 LLVM_ABI void print(raw_ostream &) const;
217
218 const PredicateBase *getPredicateInfoFor(const Value *V) const {
219 return PredicateMap.lookup(V);
220 }
221
222protected:
223 // Used by PredicateInfo annotater, dumpers, and wrapper pass.
226
227private:
228 Function &F;
229
230 // This maps from copy operands to Predicate Info. Note that it does not own
231 // the Predicate Info, they belong to the ValueInfo structs in the ValueInfos
232 // vector.
234};
235
236/// Printer pass for \c PredicateInfo.
238 : public PassInfoMixin<PredicateInfoPrinterPass> {
239 raw_ostream &OS;
240
241public:
242 explicit PredicateInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
244 static bool isRequired() { return true; }
245};
246
247/// Verifier pass for \c PredicateInfo.
248struct PredicateInfoVerifierPass : PassInfoMixin<PredicateInfoVerifierPass> {
250 static bool isRequired() { return true; }
251};
252
253} // end namespace llvm
254
255#endif // LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
static Value * getCondition(Instruction *I)
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallSet class.
A cache of @llvm.assume calls within a function.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:122
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
A wrapper class for inspecting calls to intrinsic functions.
IntrinsicInst * AssumeInst
PredicateAssume(PredicateType PT, Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
static bool classof(const PredicateBase *PB)
PredicateType Type
PredicateBase(const PredicateBase &)=delete
PredicateBase & operator=(const PredicateBase &)=delete
LLVM_ABI std::optional< PredicateConstraint > getConstraint() const
Fetch condition in the form of PredicateConstraint, if possible.
PredicateBase(PredicateType PT, Value *Op, Value *Condition)
static bool classof(const PredicateBase *PB)
static bool classof(const PredicateBase *PB)
PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB, Value *Condition, bool TakenEdge)
Attribute::AttrKind AttrKind
static bool classof(const PredicateBase *PB)
PredicateBundleAssume(Value *Op, IntrinsicInst *AssumeInst, Attribute::AttrKind AttrKind)
PredicateConditionAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
static bool classof(const PredicateBase *PB)
PredicateInfoPrinterPass(raw_ostream &OS)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
friend class PredicateInfoAnnotatedWriter
LLVM_ABI void verifyPredicateInfo() const
friend class PredicateInfoBuilder
LLVM_ABI void print(raw_ostream &) const
LLVM_ABI PredicateInfo(Function &, DominatorTree &, AssumptionCache &, BumpPtrAllocator &)
LLVM_ABI void dump() const
const PredicateBase * getPredicateInfoFor(const Value *V) const
PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB, Value *CaseValue, SwitchInst *SI)
static bool classof(const PredicateBase *PB)
PredicateWithEdge(PredicateType PType, Value *Op, BasicBlock *From, BasicBlock *To, Value *Cond)
static bool classof(const PredicateBase *PB)
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Multiway switch.
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ PT_ConditionAssume
@ PT_Switch
@ PT_BundleAssume
@ PT_Branch
DWARFExpression::Operation Op
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
Constraint for a predicate of the form "cmp Pred Op, OtherOp", where Op is the value the constraint a...
CmpInst::Predicate Predicate
Verifier pass for PredicateInfo.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)