Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- 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 // This file declares routines for folding instructions into simpler forms
0010 // that do not require creating new instructions.  This does constant folding
0011 // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
0012 // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
0013 // ("and i32 %x, %x" -> "%x").  If the simplification is also an instruction
0014 // then it dominates the original instruction.
0015 //
0016 // These routines implicitly resolve undef uses. The easiest way to be safe when
0017 // using these routines to obtain simplified values for existing instructions is
0018 // to always replace all uses of the instructions with the resulting simplified
0019 // values. This will prevent other code from seeing the same undef uses and
0020 // resolving them to different values.
0021 //
0022 // They require that all the IR that they encounter be valid and inserted into a
0023 // parent function.
0024 //
0025 // Additionally, these routines can't simplify to the instructions that are not
0026 // def-reachable, meaning we can't just scan the basic block for instructions
0027 // to simplify to.
0028 //
0029 //===----------------------------------------------------------------------===//
0030 
0031 #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
0032 #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
0033 
0034 #include "llvm/Analysis/SimplifyQuery.h"
0035 #include "llvm/IR/FPEnv.h"
0036 
0037 namespace llvm {
0038 
0039 template <typename T, typename... TArgs> class AnalysisManager;
0040 template <class T> class ArrayRef;
0041 class AssumptionCache;
0042 class CallBase;
0043 class DataLayout;
0044 class DominatorTree;
0045 class Function;
0046 class Instruction;
0047 class CmpPredicate;
0048 class LoadInst;
0049 struct LoopStandardAnalysisResults;
0050 class Pass;
0051 template <class T, unsigned n> class SmallSetVector;
0052 class TargetLibraryInfo;
0053 class Type;
0054 class Value;
0055 
0056 // NOTE: the explicit multiple argument versions of these functions are
0057 // deprecated.
0058 // Please use the SimplifyQuery versions in new code.
0059 
0060 /// Given operands for an Add, fold the result or return null.
0061 Value *simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
0062                        const SimplifyQuery &Q);
0063 
0064 /// Given operands for a Sub, fold the result or return null.
0065 Value *simplifySubInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
0066                        const SimplifyQuery &Q);
0067 
0068 /// Given operands for a Mul, fold the result or return null.
0069 Value *simplifyMulInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW,
0070                        const SimplifyQuery &Q);
0071 
0072 /// Given operands for an SDiv, fold the result or return null.
0073 Value *simplifySDivInst(Value *LHS, Value *RHS, bool IsExact,
0074                         const SimplifyQuery &Q);
0075 
0076 /// Given operands for a UDiv, fold the result or return null.
0077 Value *simplifyUDivInst(Value *LHS, Value *RHS, bool IsExact,
0078                         const SimplifyQuery &Q);
0079 
0080 /// Given operands for an SRem, fold the result or return null.
0081 Value *simplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
0082 
0083 /// Given operands for a URem, fold the result or return null.
0084 Value *simplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
0085 
0086 /// Given operand for an FNeg, fold the result or return null.
0087 Value *simplifyFNegInst(Value *Op, FastMathFlags FMF, const SimplifyQuery &Q);
0088 
0089 
0090 /// Given operands for an FAdd, fold the result or return null.
0091 Value *
0092 simplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
0093                  const SimplifyQuery &Q,
0094                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
0095                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
0096 
0097 /// Given operands for an FSub, fold the result or return null.
0098 Value *
0099 simplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
0100                  const SimplifyQuery &Q,
0101                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
0102                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
0103 
0104 /// Given operands for an FMul, fold the result or return null.
0105 Value *
0106 simplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
0107                  const SimplifyQuery &Q,
0108                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
0109                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
0110 
0111 /// Given operands for the multiplication of a FMA, fold the result or return
0112 /// null. In contrast to simplifyFMulInst, this function will not perform
0113 /// simplifications whose unrounded results differ when rounded to the argument
0114 /// type.
0115 Value *simplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF,
0116                        const SimplifyQuery &Q,
0117                        fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
0118                        RoundingMode Rounding = RoundingMode::NearestTiesToEven);
0119 
0120 /// Given operands for an FDiv, fold the result or return null.
0121 Value *
0122 simplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
0123                  const SimplifyQuery &Q,
0124                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
0125                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
0126 
0127 /// Given operands for an FRem, fold the result or return null.
0128 Value *
0129 simplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
0130                  const SimplifyQuery &Q,
0131                  fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
0132                  RoundingMode Rounding = RoundingMode::NearestTiesToEven);
0133 
0134 /// Given operands for a Shl, fold the result or return null.
0135 Value *simplifyShlInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
0136                        const SimplifyQuery &Q);
0137 
0138 /// Given operands for a LShr, fold the result or return null.
0139 Value *simplifyLShrInst(Value *Op0, Value *Op1, bool IsExact,
0140                         const SimplifyQuery &Q);
0141 
0142 /// Given operands for a AShr, fold the result or return nulll.
0143 Value *simplifyAShrInst(Value *Op0, Value *Op1, bool IsExact,
0144                         const SimplifyQuery &Q);
0145 
0146 /// Given operands for an And, fold the result or return null.
0147 Value *simplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
0148 
0149 /// Given operands for an Or, fold the result or return null.
0150 Value *simplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
0151 
0152 /// Given operands for an Xor, fold the result or return null.
0153 Value *simplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
0154 
0155 /// Given operands for an ICmpInst, fold the result or return null.
0156 Value *simplifyICmpInst(CmpPredicate Pred, Value *LHS, Value *RHS,
0157                         const SimplifyQuery &Q);
0158 
0159 /// Given operands for an FCmpInst, fold the result or return null.
0160 Value *simplifyFCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
0161                         FastMathFlags FMF, const SimplifyQuery &Q);
0162 
0163 /// Given operands for a SelectInst, fold the result or return null.
0164 Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
0165                           const SimplifyQuery &Q);
0166 
0167 /// Given operands for a GetElementPtrInst, fold the result or return null.
0168 Value *simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
0169                        GEPNoWrapFlags NW, const SimplifyQuery &Q);
0170 
0171 /// Given operands for an InsertValueInst, fold the result or return null.
0172 Value *simplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
0173                                const SimplifyQuery &Q);
0174 
0175 /// Given operands for an InsertElement, fold the result or return null.
0176 Value *simplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
0177                                  const SimplifyQuery &Q);
0178 
0179 /// Given operands for an ExtractValueInst, fold the result or return null.
0180 Value *simplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
0181                                 const SimplifyQuery &Q);
0182 
0183 /// Given operands for an ExtractElementInst, fold the result or return null.
0184 Value *simplifyExtractElementInst(Value *Vec, Value *Idx,
0185                                   const SimplifyQuery &Q);
0186 
0187 /// Given operands for a CastInst, fold the result or return null.
0188 Value *simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
0189                         const SimplifyQuery &Q);
0190 
0191 /// Given operands for a BinaryIntrinsic, fold the result or return null.
0192 Value *simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType, Value *Op0,
0193                                Value *Op1, const SimplifyQuery &Q,
0194                                const CallBase *Call);
0195 
0196 /// Given operands for a ShuffleVectorInst, fold the result or return null.
0197 /// See class ShuffleVectorInst for a description of the mask representation.
0198 Value *simplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef<int> Mask,
0199                                  Type *RetTy, const SimplifyQuery &Q);
0200 
0201 //=== Helper functions for higher up the class hierarchy.
0202 
0203 /// Given operands for a CmpInst, fold the result or return null.
0204 Value *simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS,
0205                        const SimplifyQuery &Q);
0206 
0207 /// Given operand for a UnaryOperator, fold the result or return null.
0208 Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q);
0209 
0210 /// Given operand for a UnaryOperator, fold the result or return null.
0211 /// Try to use FastMathFlags when folding the result.
0212 Value *simplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
0213                     const SimplifyQuery &Q);
0214 
0215 /// Given operands for a BinaryOperator, fold the result or return null.
0216 Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
0217                      const SimplifyQuery &Q);
0218 
0219 /// Given operands for a BinaryOperator, fold the result or return null.
0220 /// Try to use FastMathFlags when folding the result.
0221 Value *simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, FastMathFlags FMF,
0222                      const SimplifyQuery &Q);
0223 
0224 /// Given a callsite, callee, and arguments, fold the result or return null.
0225 Value *simplifyCall(CallBase *Call, Value *Callee, ArrayRef<Value *> Args,
0226                     const SimplifyQuery &Q);
0227 
0228 /// Given a constrained FP intrinsic call, tries to compute its simplified
0229 /// version. Returns a simplified result or null.
0230 ///
0231 /// This function provides an additional contract: it guarantees that if
0232 /// simplification succeeds that the intrinsic is side effect free. As a result,
0233 /// successful simplification can be used to delete the intrinsic not just
0234 /// replace its result.
0235 Value *simplifyConstrainedFPCall(CallBase *Call, const SimplifyQuery &Q);
0236 
0237 /// Given an operand for a Freeze, see if we can fold the result.
0238 /// If not, this returns null.
0239 Value *simplifyFreezeInst(Value *Op, const SimplifyQuery &Q);
0240 
0241 /// Given a load instruction and its pointer operand, fold the result or return
0242 /// null.
0243 Value *simplifyLoadInst(LoadInst *LI, Value *PtrOp, const SimplifyQuery &Q);
0244 
0245 /// See if we can compute a simplified version of this instruction. If not,
0246 /// return null.
0247 Value *simplifyInstruction(Instruction *I, const SimplifyQuery &Q);
0248 
0249 /// Like \p simplifyInstruction but the operands of \p I are replaced with
0250 /// \p NewOps. Returns a simplified value, or null if none was found.
0251 Value *
0252 simplifyInstructionWithOperands(Instruction *I, ArrayRef<Value *> NewOps,
0253                                 const SimplifyQuery &Q);
0254 
0255 /// See if V simplifies when its operand Op is replaced with RepOp. If not,
0256 /// return null.
0257 /// AllowRefinement specifies whether the simplification can be a refinement
0258 /// (e.g. 0 instead of poison), or whether it needs to be strictly identical.
0259 /// Op and RepOp can be assumed to not be poison when determining refinement.
0260 ///
0261 /// If DropFlags is passed, then the replacement result is only valid if
0262 /// poison-generating flags/metadata on those instructions are dropped. This
0263 /// is only useful in conjunction with AllowRefinement=false.
0264 Value *
0265 simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
0266                        const SimplifyQuery &Q, bool AllowRefinement,
0267                        SmallVectorImpl<Instruction *> *DropFlags = nullptr);
0268 
0269 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
0270 ///
0271 /// This first performs a normal RAUW of I with SimpleV. It then recursively
0272 /// attempts to simplify those users updated by the operation. The 'I'
0273 /// instruction must not be equal to the simplified value 'SimpleV'.
0274 /// If UnsimplifiedUsers is provided, instructions that could not be simplified
0275 /// are added to it.
0276 ///
0277 /// The function returns true if any simplifications were performed.
0278 bool replaceAndRecursivelySimplify(
0279     Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
0280     const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
0281     SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
0282 
0283 // These helper functions return a SimplifyQuery structure that contains as
0284 // many of the optional analysis we use as are currently valid.  This is the
0285 // strongly preferred way of constructing SimplifyQuery in passes.
0286 const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
0287 template <class T, class... TArgs>
0288 const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
0289                                          Function &);
0290 const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
0291                                          const DataLayout &);
0292 } // end namespace llvm
0293 
0294 #endif