Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:43

0001 //===- PredicateInfo.h - Build PredicateInfo ----------------------*-C++-*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 ///  This file implements the PredicateInfo analysis, which creates an Extended
0011 /// SSA form for operations used in branch comparisons and llvm.assume
0012 /// comparisons.
0013 ///
0014 /// Copies of these operations are inserted into the true/false edge (and after
0015 /// assumes), and information attached to the copies.  All uses of the original
0016 /// operation in blocks dominated by the true/false edge (and assume), are
0017 /// replaced with uses of the copies.  This enables passes to easily and sparsely
0018 /// propagate condition based info into the operations that may be affected.
0019 ///
0020 /// Example:
0021 /// %cmp = icmp eq i32 %x, 50
0022 /// br i1 %cmp, label %true, label %false
0023 /// true:
0024 /// ret i32 %x
0025 /// false:
0026 /// ret i32 1
0027 ///
0028 /// will become
0029 ///
0030 /// %cmp = icmp eq i32, %x, 50
0031 /// br i1 %cmp, label %true, label %false
0032 /// true:
0033 /// %x.0 = call \@llvm.ssa_copy.i32(i32 %x)
0034 /// ret i32 %x.0
0035 /// false:
0036 /// ret i32 1
0037 ///
0038 /// Using getPredicateInfoFor on x.0 will give you the comparison it is
0039 /// dominated by (the icmp), and that you are located in the true edge of that
0040 /// comparison, which tells you x.0 is 50.
0041 ///
0042 /// In order to reduce the number of copies inserted, predicateinfo is only
0043 /// inserted where it would actually be live.  This means if there are no uses of
0044 /// an operation dominated by the branch edges, or by an assume, the associated
0045 /// predicate info is never inserted.
0046 ///
0047 ///
0048 //===----------------------------------------------------------------------===//
0049 
0050 #ifndef LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
0051 #define LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
0052 
0053 #include "llvm/ADT/DenseMap.h"
0054 #include "llvm/ADT/SmallSet.h"
0055 #include "llvm/ADT/ilist.h"
0056 #include "llvm/ADT/ilist_node.h"
0057 #include "llvm/IR/Instructions.h"
0058 #include "llvm/IR/PassManager.h"
0059 #include "llvm/IR/ValueHandle.h"
0060 
0061 namespace llvm {
0062 
0063 class AssumptionCache;
0064 class DominatorTree;
0065 class Function;
0066 class Value;
0067 class IntrinsicInst;
0068 class raw_ostream;
0069 
0070 enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
0071 
0072 /// Constraint for a predicate of the form "cmp Pred Op, OtherOp", where Op
0073 /// is the value the constraint applies to (the ssa.copy result).
0074 struct PredicateConstraint {
0075   CmpInst::Predicate Predicate;
0076   Value *OtherOp;
0077 };
0078 
0079 // Base class for all predicate information we provide.
0080 // All of our predicate information has at least a comparison.
0081 class PredicateBase : public ilist_node<PredicateBase> {
0082 public:
0083   PredicateType Type;
0084   // The original operand before we renamed it.
0085   // This can be use by passes, when destroying predicateinfo, to know
0086   // whether they can just drop the intrinsic, or have to merge metadata.
0087   Value *OriginalOp;
0088   // The renamed operand in the condition used for this predicate. For nested
0089   // predicates, this is different to OriginalOp which refers to the initial
0090   // operand.
0091   Value *RenamedOp;
0092   // The condition associated with this predicate.
0093   Value *Condition;
0094 
0095   PredicateBase(const PredicateBase &) = delete;
0096   PredicateBase &operator=(const PredicateBase &) = delete;
0097   PredicateBase() = delete;
0098   virtual ~PredicateBase() = default;
0099   static bool classof(const PredicateBase *PB) {
0100     return PB->Type == PT_Assume || PB->Type == PT_Branch ||
0101            PB->Type == PT_Switch;
0102   }
0103 
0104   /// Fetch condition in the form of PredicateConstraint, if possible.
0105   std::optional<PredicateConstraint> getConstraint() const;
0106 
0107 protected:
0108   PredicateBase(PredicateType PT, Value *Op, Value *Condition)
0109       : Type(PT), OriginalOp(Op), Condition(Condition) {}
0110 };
0111 
0112 // Provides predicate information for assumes.  Since assumes are always true,
0113 // we simply provide the assume instruction, so you can tell your relative
0114 // position to it.
0115 class PredicateAssume : public PredicateBase {
0116 public:
0117   IntrinsicInst *AssumeInst;
0118   PredicateAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
0119       : PredicateBase(PT_Assume, Op, Condition), AssumeInst(AssumeInst) {}
0120   PredicateAssume() = delete;
0121   static bool classof(const PredicateBase *PB) {
0122     return PB->Type == PT_Assume;
0123   }
0124 };
0125 
0126 // Mixin class for edge predicates.  The FROM block is the block where the
0127 // predicate originates, and the TO block is the block where the predicate is
0128 // valid.
0129 class PredicateWithEdge : public PredicateBase {
0130 public:
0131   BasicBlock *From;
0132   BasicBlock *To;
0133   PredicateWithEdge() = delete;
0134   static bool classof(const PredicateBase *PB) {
0135     return PB->Type == PT_Branch || PB->Type == PT_Switch;
0136   }
0137 
0138 protected:
0139   PredicateWithEdge(PredicateType PType, Value *Op, BasicBlock *From,
0140                     BasicBlock *To, Value *Cond)
0141       : PredicateBase(PType, Op, Cond), From(From), To(To) {}
0142 };
0143 
0144 // Provides predicate information for branches.
0145 class PredicateBranch : public PredicateWithEdge {
0146 public:
0147   // If true, SplitBB is the true successor, otherwise it's the false successor.
0148   bool TrueEdge;
0149   PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB,
0150                   Value *Condition, bool TakenEdge)
0151       : PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition),
0152         TrueEdge(TakenEdge) {}
0153   PredicateBranch() = delete;
0154   static bool classof(const PredicateBase *PB) {
0155     return PB->Type == PT_Branch;
0156   }
0157 };
0158 
0159 class PredicateSwitch : public PredicateWithEdge {
0160 public:
0161   Value *CaseValue;
0162   // This is the switch instruction.
0163   SwitchInst *Switch;
0164   PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB,
0165                   Value *CaseValue, SwitchInst *SI)
0166       : PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB,
0167                           SI->getCondition()),
0168         CaseValue(CaseValue), Switch(SI) {}
0169   PredicateSwitch() = delete;
0170   static bool classof(const PredicateBase *PB) {
0171     return PB->Type == PT_Switch;
0172   }
0173 };
0174 
0175 /// Encapsulates PredicateInfo, including all data associated with memory
0176 /// accesses.
0177 class PredicateInfo {
0178 public:
0179   PredicateInfo(Function &, DominatorTree &, AssumptionCache &);
0180   ~PredicateInfo();
0181 
0182   void verifyPredicateInfo() const;
0183 
0184   void dump() const;
0185   void print(raw_ostream &) const;
0186 
0187   const PredicateBase *getPredicateInfoFor(const Value *V) const {
0188     return PredicateMap.lookup(V);
0189   }
0190 
0191 protected:
0192   // Used by PredicateInfo annotater, dumpers, and wrapper pass.
0193   friend class PredicateInfoAnnotatedWriter;
0194   friend class PredicateInfoBuilder;
0195 
0196 private:
0197   Function &F;
0198 
0199   // This owns the all the predicate infos in the function, placed or not.
0200   iplist<PredicateBase> AllInfos;
0201 
0202   // This maps from copy operands to Predicate Info. Note that it does not own
0203   // the Predicate Info, they belong to the ValueInfo structs in the ValueInfos
0204   // vector.
0205   DenseMap<const Value *, const PredicateBase *> PredicateMap;
0206   // The set of ssa_copy declarations we created with our custom mangling.
0207   SmallSet<AssertingVH<Function>, 20> CreatedDeclarations;
0208 };
0209 
0210 /// Printer pass for \c PredicateInfo.
0211 class PredicateInfoPrinterPass
0212     : public PassInfoMixin<PredicateInfoPrinterPass> {
0213   raw_ostream &OS;
0214 
0215 public:
0216   explicit PredicateInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
0217   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0218   static bool isRequired() { return true; }
0219 };
0220 
0221 /// Verifier pass for \c PredicateInfo.
0222 struct PredicateInfoVerifierPass : PassInfoMixin<PredicateInfoVerifierPass> {
0223   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0224   static bool isRequired() { return true; }
0225 };
0226 
0227 } // end namespace llvm
0228 
0229 #endif // LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H