Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- ConstantFolding.h - Fold instructions into constants ----*- 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 constants when all
0010 // operands are constants, for example "sub i32 1, 0" -> "1".
0011 //
0012 // Also, to supplement the basic VMCore ConstantExpr simplifications,
0013 // this file declares some additional folding routines that can make use of
0014 // DataLayout information. These functions cannot go in VMCore due to library
0015 // dependency issues.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
0020 #define LLVM_ANALYSIS_CONSTANTFOLDING_H
0021 
0022 #include <stdint.h>
0023 
0024 namespace llvm {
0025 
0026 namespace Intrinsic {
0027 using ID = unsigned;
0028 }
0029 
0030 class APInt;
0031 template <typename T> class ArrayRef;
0032 class CallBase;
0033 class Constant;
0034 class DSOLocalEquivalent;
0035 class DataLayout;
0036 class Function;
0037 class GlobalValue;
0038 class GlobalVariable;
0039 class Instruction;
0040 class TargetLibraryInfo;
0041 class Type;
0042 
0043 /// If this constant is a constant offset from a global, return the global and
0044 /// the constant. Because of constantexprs, this function is recursive.
0045 /// If the global is part of a dso_local_equivalent constant, return it through
0046 /// `Equiv` if it is provided.
0047 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
0048                                 const DataLayout &DL,
0049                                 DSOLocalEquivalent **DSOEquiv = nullptr);
0050 
0051 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
0052 /// If successful, the constant result is returned, if not, null is returned.
0053 /// Note that this fails if not all of the operands are constant.  Otherwise,
0054 /// this function can only fail when attempting to fold instructions like loads
0055 /// and stores, which have no constant expression form.
0056 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
0057                                   const TargetLibraryInfo *TLI = nullptr);
0058 
0059 /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
0060 /// This function always returns a non-null constant: Either the folding result,
0061 /// or the original constant if further folding is not possible.
0062 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
0063                                const TargetLibraryInfo *TLI = nullptr);
0064 
0065 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
0066 /// specified operands.  If successful, the constant result is returned, if not,
0067 /// null is returned.  Note that this function can fail when attempting to
0068 /// fold instructions like loads and stores, which have no constant expression
0069 /// form.
0070 ///
0071 /// In some cases, constant folding may return one value chosen from a set of
0072 /// multiple legal return values. For example, the exact bit pattern of NaN
0073 /// results is not guaranteed. Using such a result is usually only valid if
0074 /// all uses of the original operation are replaced by the constant-folded
0075 /// result. The \p AllowNonDeterministic parameter controls whether this is
0076 /// allowed.
0077 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
0078                                    const DataLayout &DL,
0079                                    const TargetLibraryInfo *TLI = nullptr,
0080                                    bool AllowNonDeterministic = true);
0081 
0082 /// Attempt to constant fold a compare instruction (icmp/fcmp) with the
0083 /// specified operands. Returns null or a constant expression of the specified
0084 /// operands on failure.
0085 /// Denormal inputs may be flushed based on the denormal handling mode.
0086 Constant *ConstantFoldCompareInstOperands(
0087     unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
0088     const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
0089 
0090 /// Attempt to constant fold a unary operation with the specified operand.
0091 /// Returns null on failure.
0092 Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
0093                                      const DataLayout &DL);
0094 
0095 /// Attempt to constant fold a binary operation with the specified operands.
0096 /// Returns null or a constant expression of the specified operands on failure.
0097 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
0098                                        Constant *RHS, const DataLayout &DL);
0099 
0100 /// Attempt to constant fold a floating point binary operation with the
0101 /// specified operands, applying the denormal handling mod to the operands.
0102 /// Returns null or a constant expression of the specified operands on failure.
0103 Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,
0104                                      Constant *RHS, const DataLayout &DL,
0105                                      const Instruction *I,
0106                                      bool AllowNonDeterministic = true);
0107 
0108 /// Attempt to flush float point constant according to denormal mode set in the
0109 /// instruction's parent function attributes. If so, return a zero with the
0110 /// correct sign, otherwise return the original constant. Inputs and outputs to
0111 /// floating point instructions can have their mode set separately, so the
0112 /// direction is also needed.
0113 ///
0114 /// If the calling function's "denormal-fp-math" input mode is "dynamic" for the
0115 /// floating-point type, returns nullptr for denormal inputs.
0116 Constant *FlushFPConstant(Constant *Operand, const Instruction *I,
0117                           bool IsOutput);
0118 
0119 /// Attempt to constant fold a select instruction with the specified
0120 /// operands. The constant result is returned if successful; if not, null is
0121 /// returned.
0122 Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
0123                                         Constant *V2);
0124 
0125 /// Attempt to constant fold a cast with the specified operand.  If it
0126 /// fails, it returns a constant expression of the specified operand.
0127 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
0128                                   const DataLayout &DL);
0129 
0130 /// Constant fold a zext, sext or trunc, depending on IsSigned and whether the
0131 /// DestTy is wider or narrower than C. Returns nullptr on failure.
0132 Constant *ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned,
0133                                   const DataLayout &DL);
0134 
0135 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
0136 /// instruction with the specified operands and indices.  The constant result is
0137 /// returned if successful; if not, null is returned.
0138 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
0139                                              ArrayRef<unsigned> Idxs);
0140 
0141 /// Attempt to constant fold an extractvalue instruction with the
0142 /// specified operands and indices.  The constant result is returned if
0143 /// successful; if not, null is returned.
0144 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
0145                                               ArrayRef<unsigned> Idxs);
0146 
0147 /// Attempt to constant fold an insertelement instruction with the
0148 /// specified operands and indices.  The constant result is returned if
0149 /// successful; if not, null is returned.
0150 Constant *ConstantFoldInsertElementInstruction(Constant *Val,
0151                                                Constant *Elt,
0152                                                Constant *Idx);
0153 
0154 /// Attempt to constant fold an extractelement instruction with the
0155 /// specified operands and indices.  The constant result is returned if
0156 /// successful; if not, null is returned.
0157 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
0158 
0159 /// Attempt to constant fold a shufflevector instruction with the
0160 /// specified operands and mask.  See class ShuffleVectorInst for a description
0161 /// of the mask representation. The constant result is returned if successful;
0162 /// if not, null is returned.
0163 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
0164                                                ArrayRef<int> Mask);
0165 
0166 /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
0167 /// the end of C are accessed, they are assumed to be poison.
0168 Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
0169                                     const DataLayout &DL);
0170 
0171 /// Extract value of C reinterpreted as Ty. Same as previous API with zero
0172 /// offset.
0173 Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty,
0174                                     const DataLayout &DL);
0175 
0176 /// Return the value that a load from C with offset Offset would produce if it
0177 /// is constant and determinable. If this is not determinable, return null.
0178 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
0179                                        const DataLayout &DL);
0180 
0181 /// Return the value that a load from C would produce if it is constant and
0182 /// determinable. If this is not determinable, return null.
0183 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
0184                                        const DataLayout &DL);
0185 
0186 /// If C is a uniform value where all bits are the same (either all zero, all
0187 /// ones, all undef or all poison), return the corresponding uniform value in
0188 /// the new type. If the value is not uniform or the result cannot be
0189 /// represented, return null.
0190 Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty,
0191                                            const DataLayout &DL);
0192 
0193 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
0194 /// the specified function.
0195 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
0196 
0197 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
0198 /// with the specified arguments, returning null if unsuccessful.
0199 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
0200                            ArrayRef<Constant *> Operands,
0201                            const TargetLibraryInfo *TLI = nullptr,
0202                            bool AllowNonDeterministic = true);
0203 
0204 Constant *ConstantFoldBinaryIntrinsic(Intrinsic::ID ID, Constant *LHS,
0205                                       Constant *RHS, Type *Ty,
0206                                       Instruction *FMFSource);
0207 
0208 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
0209 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
0210 /// integer and vice versa if their sizes are equal.
0211 Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
0212                                          const DataLayout &DL);
0213 
0214 /// Check whether the given call has no side-effects.
0215 /// Specifically checks for math routimes which sometimes set errno.
0216 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
0217 
0218 Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
0219 }
0220 
0221 #endif