xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Analysis/InstSimplifyFolder.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===- InstSimplifyFolder.h - InstSimplify folding helper --------*- 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 // This file defines the InstSimplifyFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for folding operations to
11 // existing values using InstructionSimplify. At the moment, only a subset of
12 // the implementation uses InstructionSimplify. The rest of the implementation
13 // only folds constants.
14 //
15 // The folder also applies target-specific constant folding.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
20 #define LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
21 
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/Analysis/InstructionSimplify.h"
24 #include "llvm/Analysis/TargetFolder.h"
25 #include "llvm/IR/IRBuilderFolder.h"
26 #include "llvm/IR/Instruction.h"
27 
28 namespace llvm {
29 class Constant;
30 
31 /// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
32 /// values. Also applies target-specific constant folding when not using
33 /// InstructionSimplify.
34 class InstSimplifyFolder final : public IRBuilderFolder {
35   TargetFolder ConstFolder;
36   SimplifyQuery SQ;
37 
38   virtual void anchor();
39 
40 public:
InstSimplifyFolder(const DataLayout & DL)41   InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
42 
43   //===--------------------------------------------------------------------===//
44   // Value-based folders.
45   //
46   // Return an existing value or a constant if the operation can be simplified.
47   // Otherwise return nullptr.
48   //===--------------------------------------------------------------------===//
49 
FoldBinOp(Instruction::BinaryOps Opc,Value * LHS,Value * RHS)50   Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
51                    Value *RHS) const override {
52     return simplifyBinOp(Opc, LHS, RHS, SQ);
53   }
54 
FoldExactBinOp(Instruction::BinaryOps Opc,Value * LHS,Value * RHS,bool IsExact)55   Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
56                         bool IsExact) const override {
57     return simplifyBinOp(Opc, LHS, RHS, SQ);
58   }
59 
FoldNoWrapBinOp(Instruction::BinaryOps Opc,Value * LHS,Value * RHS,bool HasNUW,bool HasNSW)60   Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
61                          bool HasNUW, bool HasNSW) const override {
62     return simplifyBinOp(Opc, LHS, RHS, SQ);
63   }
64 
FoldBinOpFMF(Instruction::BinaryOps Opc,Value * LHS,Value * RHS,FastMathFlags FMF)65   Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
66                       FastMathFlags FMF) const override {
67     return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
68   }
69 
FoldUnOpFMF(Instruction::UnaryOps Opc,Value * V,FastMathFlags FMF)70   Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
71                       FastMathFlags FMF) const override {
72     return simplifyUnOp(Opc, V, FMF, SQ);
73   }
74 
FoldCmp(CmpInst::Predicate P,Value * LHS,Value * RHS)75   Value *FoldCmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
76     return simplifyCmpInst(P, LHS, RHS, SQ);
77   }
78 
FoldGEP(Type * Ty,Value * Ptr,ArrayRef<Value * > IdxList,GEPNoWrapFlags NW)79   Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
80                  GEPNoWrapFlags NW) const override {
81     return simplifyGEPInst(Ty, Ptr, IdxList, NW, SQ);
82   }
83 
FoldSelect(Value * C,Value * True,Value * False)84   Value *FoldSelect(Value *C, Value *True, Value *False) const override {
85     return simplifySelectInst(C, True, False, SQ);
86   }
87 
FoldExtractValue(Value * Agg,ArrayRef<unsigned> IdxList)88   Value *FoldExtractValue(Value *Agg,
89                           ArrayRef<unsigned> IdxList) const override {
90     return simplifyExtractValueInst(Agg, IdxList, SQ);
91   };
92 
FoldInsertValue(Value * Agg,Value * Val,ArrayRef<unsigned> IdxList)93   Value *FoldInsertValue(Value *Agg, Value *Val,
94                          ArrayRef<unsigned> IdxList) const override {
95     return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
96   }
97 
FoldExtractElement(Value * Vec,Value * Idx)98   Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
99     return simplifyExtractElementInst(Vec, Idx, SQ);
100   }
101 
FoldInsertElement(Value * Vec,Value * NewElt,Value * Idx)102   Value *FoldInsertElement(Value *Vec, Value *NewElt,
103                            Value *Idx) const override {
104     return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
105   }
106 
FoldShuffleVector(Value * V1,Value * V2,ArrayRef<int> Mask)107   Value *FoldShuffleVector(Value *V1, Value *V2,
108                            ArrayRef<int> Mask) const override {
109     Type *RetTy = VectorType::get(
110         cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
111         isa<ScalableVectorType>(V1->getType()));
112     return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
113   }
114 
FoldCast(Instruction::CastOps Op,Value * V,Type * DestTy)115   Value *FoldCast(Instruction::CastOps Op, Value *V,
116                   Type *DestTy) const override {
117     return simplifyCastInst(Op, V, DestTy, SQ);
118   }
119 
FoldBinaryIntrinsic(Intrinsic::ID ID,Value * LHS,Value * RHS,Type * Ty,Instruction * FMFSource)120   Value *FoldBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, Type *Ty,
121                              Instruction *FMFSource) const override {
122     return simplifyBinaryIntrinsic(ID, Ty, LHS, RHS, SQ,
123                                    dyn_cast_if_present<CallBase>(FMFSource));
124   }
125 
126   //===--------------------------------------------------------------------===//
127   // Cast/Conversion Operators
128   //===--------------------------------------------------------------------===//
129 
CreatePointerCast(Constant * C,Type * DestTy)130   Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
131     if (C->getType() == DestTy)
132       return C; // avoid calling Fold
133     return ConstFolder.CreatePointerCast(C, DestTy);
134   }
135 
CreatePointerBitCastOrAddrSpaceCast(Constant * C,Type * DestTy)136   Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
137                                              Type *DestTy) const override {
138     if (C->getType() == DestTy)
139       return C; // avoid calling Fold
140     return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
141   }
142 };
143 
144 } // end namespace llvm
145 
146 #endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
147