1 //====- TargetFolder.h - Constant 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 TargetFolder class, a helper for IRBuilder.
10 // It provides IRBuilder with a set of methods for creating constants with
11 // target dependent folding, in addition to the same target-independent
12 // folding that the ConstantFolder class provides.  For general constant
13 // creation and folding, use ConstantExpr and the routines in
14 // llvm/Analysis/ConstantFolding.h.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #ifndef LLVM_ANALYSIS_TARGETFOLDER_H
19 #define LLVM_ANALYSIS_TARGETFOLDER_H
20 
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/Analysis/ConstantFolding.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/IRBuilderFolder.h"
25 #include "llvm/IR/Operator.h"
26 
27 namespace llvm {
28 
29 class Constant;
30 class DataLayout;
31 class Type;
32 
33 /// TargetFolder - Create constants with target dependent folding.
34 class TargetFolder final : public IRBuilderFolder {
35   const DataLayout &DL;
36 
37   /// Fold - Fold the constant using target specific information.
38   Constant *Fold(Constant *C) const {
39     return ConstantFoldConstant(C, DL);
40   }
41 
42   virtual void anchor();
43 
44 public:
45   explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
46 
47   //===--------------------------------------------------------------------===//
48   // Value-based folders.
49   //
50   // Return an existing value or a constant if the operation can be simplified.
51   // Otherwise return nullptr.
52   //===--------------------------------------------------------------------===//
53 
54   Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
55                    Value *RHS) const override {
56     auto *LC = dyn_cast<Constant>(LHS);
57     auto *RC = dyn_cast<Constant>(RHS);
58     if (LC && RC)
59       return Fold(ConstantExpr::get(Opc, LC, RC));
60     return nullptr;
61   }
62 
63   Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
64                         bool IsExact) const override {
65     auto *LC = dyn_cast<Constant>(LHS);
66     auto *RC = dyn_cast<Constant>(RHS);
67     if (LC && RC)
68       return Fold(ConstantExpr::get(
69           Opc, LC, RC, IsExact ? PossiblyExactOperator::IsExact : 0));
70     return nullptr;
71   }
72 
73   Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
74                          bool HasNUW, bool HasNSW) const override {
75     auto *LC = dyn_cast<Constant>(LHS);
76     auto *RC = dyn_cast<Constant>(RHS);
77     if (LC && RC) {
78       unsigned Flags = 0;
79       if (HasNUW)
80         Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
81       if (HasNSW)
82         Flags |= OverflowingBinaryOperator::NoSignedWrap;
83       return Fold(ConstantExpr::get(Opc, LC, RC, Flags));
84     }
85     return nullptr;
86   }
87 
88   Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
89                       FastMathFlags FMF) const override {
90     return FoldBinOp(Opc, LHS, RHS);
91   }
92   Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
93     auto *LC = dyn_cast<Constant>(LHS);
94     auto *RC = dyn_cast<Constant>(RHS);
95     if (LC && RC)
96       return ConstantExpr::getCompare(P, LC, RC);
97     return nullptr;
98   }
99 
100   Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
101                  bool IsInBounds = false) const override {
102     if (auto *PC = dyn_cast<Constant>(Ptr)) {
103       // Every index must be constant.
104       if (any_of(IdxList, [](Value *V) { return !isa<Constant>(V); }))
105         return nullptr;
106       if (IsInBounds)
107         return Fold(ConstantExpr::getInBoundsGetElementPtr(Ty, PC, IdxList));
108       else
109         return Fold(ConstantExpr::getGetElementPtr(Ty, PC, IdxList));
110     }
111     return nullptr;
112   }
113 
114   Value *FoldSelect(Value *C, Value *True, Value *False) const override {
115     auto *CC = dyn_cast<Constant>(C);
116     auto *TC = dyn_cast<Constant>(True);
117     auto *FC = dyn_cast<Constant>(False);
118     if (CC && TC && FC)
119       return Fold(ConstantExpr::getSelect(CC, TC, FC));
120 
121     return nullptr;
122   }
123 
124   Value *FoldExtractValue(Value *Agg,
125                           ArrayRef<unsigned> IdxList) const override {
126     if (auto *CAgg = dyn_cast<Constant>(Agg))
127       return ConstantFoldExtractValueInstruction(CAgg, IdxList);
128     return nullptr;
129   };
130 
131   Value *FoldInsertValue(Value *Agg, Value *Val,
132                          ArrayRef<unsigned> IdxList) const override {
133     auto *CAgg = dyn_cast<Constant>(Agg);
134     auto *CVal = dyn_cast<Constant>(Val);
135     if (CAgg && CVal)
136       return ConstantFoldInsertValueInstruction(CAgg, CVal, IdxList);
137     return nullptr;
138   }
139 
140   Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
141     auto *CVec = dyn_cast<Constant>(Vec);
142     auto *CIdx = dyn_cast<Constant>(Idx);
143     if (CVec && CIdx)
144       return Fold(ConstantExpr::getExtractElement(CVec, CIdx));
145     return nullptr;
146   }
147 
148   Value *FoldInsertElement(Value *Vec, Value *NewElt,
149                            Value *Idx) const override {
150     auto *CVec = dyn_cast<Constant>(Vec);
151     auto *CNewElt = dyn_cast<Constant>(NewElt);
152     auto *CIdx = dyn_cast<Constant>(Idx);
153     if (CVec && CNewElt && CIdx)
154       return Fold(ConstantExpr::getInsertElement(CVec, CNewElt, CIdx));
155     return nullptr;
156   }
157 
158   Value *FoldShuffleVector(Value *V1, Value *V2,
159                            ArrayRef<int> Mask) const override {
160     auto *C1 = dyn_cast<Constant>(V1);
161     auto *C2 = dyn_cast<Constant>(V2);
162     if (C1 && C2)
163       return Fold(ConstantExpr::getShuffleVector(C1, C2, Mask));
164     return nullptr;
165   }
166 
167   //===--------------------------------------------------------------------===//
168   // Unary Operators
169   //===--------------------------------------------------------------------===//
170 
171   Constant *CreateFNeg(Constant *C) const override {
172     return Fold(ConstantExpr::getFNeg(C));
173   }
174 
175   Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override {
176     return Fold(ConstantExpr::get(Opc, C));
177   }
178 
179   //===--------------------------------------------------------------------===//
180   // Cast/Conversion Operators
181   //===--------------------------------------------------------------------===//
182 
183   Constant *CreateCast(Instruction::CastOps Op, Constant *C,
184                        Type *DestTy) const override {
185     if (C->getType() == DestTy)
186       return C; // avoid calling Fold
187     return Fold(ConstantExpr::getCast(Op, C, DestTy));
188   }
189   Constant *CreateIntCast(Constant *C, Type *DestTy,
190                           bool isSigned) const override {
191     if (C->getType() == DestTy)
192       return C; // avoid calling Fold
193     return Fold(ConstantExpr::getIntegerCast(C, DestTy, isSigned));
194   }
195   Constant *CreatePointerCast(Constant *C, Type *DestTy) const override {
196     if (C->getType() == DestTy)
197       return C; // avoid calling Fold
198     return Fold(ConstantExpr::getPointerCast(C, DestTy));
199   }
200   Constant *CreateFPCast(Constant *C, Type *DestTy) const override {
201     if (C->getType() == DestTy)
202       return C; // avoid calling Fold
203     return Fold(ConstantExpr::getFPCast(C, DestTy));
204   }
205   Constant *CreateBitCast(Constant *C, Type *DestTy) const override {
206     return CreateCast(Instruction::BitCast, C, DestTy);
207   }
208   Constant *CreateIntToPtr(Constant *C, Type *DestTy) const override {
209     return CreateCast(Instruction::IntToPtr, C, DestTy);
210   }
211   Constant *CreatePtrToInt(Constant *C, Type *DestTy) const override {
212     return CreateCast(Instruction::PtrToInt, C, DestTy);
213   }
214   Constant *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
215     if (C->getType() == DestTy)
216       return C; // avoid calling Fold
217     return Fold(ConstantExpr::getZExtOrBitCast(C, DestTy));
218   }
219   Constant *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
220     if (C->getType() == DestTy)
221       return C; // avoid calling Fold
222     return Fold(ConstantExpr::getSExtOrBitCast(C, DestTy));
223   }
224   Constant *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
225     if (C->getType() == DestTy)
226       return C; // avoid calling Fold
227     return Fold(ConstantExpr::getTruncOrBitCast(C, DestTy));
228   }
229 
230   Constant *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
231                                                 Type *DestTy) const override {
232     if (C->getType() == DestTy)
233       return C; // avoid calling Fold
234     return Fold(ConstantExpr::getPointerBitCastOrAddrSpaceCast(C, DestTy));
235   }
236 
237   //===--------------------------------------------------------------------===//
238   // Compare Instructions
239   //===--------------------------------------------------------------------===//
240 
241   Constant *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
242                        Constant *RHS) const override {
243     return Fold(ConstantExpr::getCompare(P, LHS, RHS));
244   }
245 };
246 
247 }
248 
249 #endif
250