Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- InstSimplifyFolder.h - InstSimplify folding helper --------*- 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 defines the InstSimplifyFolder class, a helper for IRBuilder.
0010 // It provides IRBuilder with a set of methods for folding operations to
0011 // existing values using InstructionSimplify. At the moment, only a subset of
0012 // the implementation uses InstructionSimplify. The rest of the implementation
0013 // only folds constants.
0014 //
0015 // The folder also applies target-specific constant folding.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
0020 #define LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
0021 
0022 #include "llvm/ADT/ArrayRef.h"
0023 #include "llvm/Analysis/InstructionSimplify.h"
0024 #include "llvm/Analysis/TargetFolder.h"
0025 #include "llvm/IR/CmpPredicate.h"
0026 #include "llvm/IR/IRBuilderFolder.h"
0027 #include "llvm/IR/Instruction.h"
0028 
0029 namespace llvm {
0030 class Constant;
0031 
0032 /// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
0033 /// values. Also applies target-specific constant folding when not using
0034 /// InstructionSimplify.
0035 class InstSimplifyFolder final : public IRBuilderFolder {
0036   TargetFolder ConstFolder;
0037   SimplifyQuery SQ;
0038 
0039   virtual void anchor();
0040 
0041 public:
0042   explicit InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
0043 
0044   //===--------------------------------------------------------------------===//
0045   // Value-based folders.
0046   //
0047   // Return an existing value or a constant if the operation can be simplified.
0048   // Otherwise return nullptr.
0049   //===--------------------------------------------------------------------===//
0050 
0051   Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
0052                    Value *RHS) const override {
0053     return simplifyBinOp(Opc, LHS, RHS, SQ);
0054   }
0055 
0056   Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
0057                         bool IsExact) const override {
0058     return simplifyBinOp(Opc, LHS, RHS, SQ);
0059   }
0060 
0061   Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
0062                          bool HasNUW, bool HasNSW) const override {
0063     return simplifyBinOp(Opc, LHS, RHS, SQ);
0064   }
0065 
0066   Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
0067                       FastMathFlags FMF) const override {
0068     return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
0069   }
0070 
0071   Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
0072                       FastMathFlags FMF) const override {
0073     return simplifyUnOp(Opc, V, FMF, SQ);
0074   }
0075 
0076   Value *FoldCmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
0077     return simplifyCmpInst(P, LHS, RHS, SQ);
0078   }
0079 
0080   Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
0081                  GEPNoWrapFlags NW) const override {
0082     return simplifyGEPInst(Ty, Ptr, IdxList, NW, SQ);
0083   }
0084 
0085   Value *FoldSelect(Value *C, Value *True, Value *False) const override {
0086     return simplifySelectInst(C, True, False, SQ);
0087   }
0088 
0089   Value *FoldExtractValue(Value *Agg,
0090                           ArrayRef<unsigned> IdxList) const override {
0091     return simplifyExtractValueInst(Agg, IdxList, SQ);
0092   };
0093 
0094   Value *FoldInsertValue(Value *Agg, Value *Val,
0095                          ArrayRef<unsigned> IdxList) const override {
0096     return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
0097   }
0098 
0099   Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
0100     return simplifyExtractElementInst(Vec, Idx, SQ);
0101   }
0102 
0103   Value *FoldInsertElement(Value *Vec, Value *NewElt,
0104                            Value *Idx) const override {
0105     return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
0106   }
0107 
0108   Value *FoldShuffleVector(Value *V1, Value *V2,
0109                            ArrayRef<int> Mask) const override {
0110     Type *RetTy = VectorType::get(
0111         cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
0112         isa<ScalableVectorType>(V1->getType()));
0113     return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
0114   }
0115 
0116   Value *FoldCast(Instruction::CastOps Op, Value *V,
0117                   Type *DestTy) const override {
0118     return simplifyCastInst(Op, V, DestTy, SQ);
0119   }
0120 
0121   Value *FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty,
0122                              Instruction *FMFSource) const override {
0123     return simplifyBinaryIntrinsic(ID, Ty, LHS, RHS, SQ,
0124                                    dyn_cast_if_present<CallBase>(FMFSource));
0125   }
0126 
0127   //===--------------------------------------------------------------------===//
0128   // Cast/Conversion Operators
0129   //===--------------------------------------------------------------------===//
0130 
0131   Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
0132     if (C->getType() == DestTy)
0133       return C; // avoid calling Fold
0134     return ConstFolder.CreatePointerCast(C, DestTy);
0135   }
0136 
0137   Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
0138                                              Type *DestTy) const override {
0139     if (C->getType() == DestTy)
0140       return C; // avoid calling Fold
0141     return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
0142   }
0143 };
0144 
0145 } // end namespace llvm
0146 
0147 #endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H