1 //===-- ConstantFolding.h - Fold instructions into constants ----*- 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 declares routines for folding instructions into constants when all
10 // operands are constants, for example "sub i32 1, 0" -> "1".
11 //
12 // Also, to supplement the basic VMCore ConstantExpr simplifications,
13 // this file declares some additional folding routines that can make use of
14 // DataLayout information. These functions cannot go in VMCore due to library
15 // dependency issues.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
20 #define LLVM_ANALYSIS_CONSTANTFOLDING_H
21 
22 #include <stdint.h>
23 
24 namespace llvm {
25 class APInt;
26 template <typename T> class ArrayRef;
27 class CallBase;
28 class Constant;
29 class DSOLocalEquivalent;
30 class DataLayout;
31 class Function;
32 class GlobalValue;
33 class GlobalVariable;
34 class Instruction;
35 class TargetLibraryInfo;
36 class Type;
37 
38 /// If this constant is a constant offset from a global, return the global and
39 /// the constant. Because of constantexprs, this function is recursive.
40 /// If the global is part of a dso_local_equivalent constant, return it through
41 /// `Equiv` if it is provided.
42 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
43                                 const DataLayout &DL,
44                                 DSOLocalEquivalent **DSOEquiv = nullptr);
45 
46 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
47 /// If successful, the constant result is returned, if not, null is returned.
48 /// Note that this fails if not all of the operands are constant.  Otherwise,
49 /// this function can only fail when attempting to fold instructions like loads
50 /// and stores, which have no constant expression form.
51 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
52                                   const TargetLibraryInfo *TLI = nullptr);
53 
54 /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
55 /// This function always returns a non-null constant: Either the folding result,
56 /// or the original constant if further folding is not possible.
57 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
58                                const TargetLibraryInfo *TLI = nullptr);
59 
60 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
61 /// specified operands.  If successful, the constant result is returned, if not,
62 /// null is returned.  Note that this function can fail when attempting to
63 /// fold instructions like loads and stores, which have no constant expression
64 /// form.
65 ///
66 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
67                                    const DataLayout &DL,
68                                    const TargetLibraryInfo *TLI = nullptr);
69 
70 /// Attempt to constant fold a compare instruction (icmp/fcmp) with the
71 /// specified operands.  If it fails, it returns a constant expression of the
72 /// specified operands.
73 /// Denormal inputs may be flushed based on the denormal handling mode.
74 Constant *ConstantFoldCompareInstOperands(
75     unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
76     const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
77 
78 /// Attempt to constant fold a unary operation with the specified
79 /// operand. If it fails, it returns a constant expression of the specified
80 /// operands.
81 Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
82                                      const DataLayout &DL);
83 
84 /// Attempt to constant fold a binary operation with the specified
85 /// operands.  If it fails, it returns a constant expression of the specified
86 /// operands.
87 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
88                                        Constant *RHS, const DataLayout &DL);
89 
90 /// Attempt to constant fold a floating point binary operation with the
91 /// specified operands, applying the denormal handling mod to the operands.  If
92 /// it fails, it returns a constant expression of the specified operands.
93 Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,
94                                      Constant *RHS, const DataLayout &DL,
95                                      const Instruction *I);
96 
97 /// Attempt to flush float point constant according to denormal mode set in the
98 /// instruction's parent function attributes. If so, return a zero with the
99 /// correct sign, otherwise return the original constant. Inputs and outputs to
100 /// floating point instructions can have their mode set separately, so the
101 /// direction is also needed.
102 Constant *FlushFPConstant(Constant *Operand, const Instruction *I,
103                           bool IsOutput);
104 
105 /// Attempt to constant fold a select instruction with the specified
106 /// operands. The constant result is returned if successful; if not, null is
107 /// returned.
108 Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
109                                         Constant *V2);
110 
111 /// Attempt to constant fold a cast with the specified operand.  If it
112 /// fails, it returns a constant expression of the specified operand.
113 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
114                                   const DataLayout &DL);
115 
116 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
117 /// instruction with the specified operands and indices.  The constant result is
118 /// returned if successful; if not, null is returned.
119 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
120                                              ArrayRef<unsigned> Idxs);
121 
122 /// Attempt to constant fold an extractvalue instruction with the
123 /// specified operands and indices.  The constant result is returned if
124 /// successful; if not, null is returned.
125 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
126                                               ArrayRef<unsigned> Idxs);
127 
128 /// Attempt to constant fold an insertelement instruction with the
129 /// specified operands and indices.  The constant result is returned if
130 /// successful; if not, null is returned.
131 Constant *ConstantFoldInsertElementInstruction(Constant *Val,
132                                                Constant *Elt,
133                                                Constant *Idx);
134 
135 /// Attempt to constant fold an extractelement instruction with the
136 /// specified operands and indices.  The constant result is returned if
137 /// successful; if not, null is returned.
138 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
139 
140 /// Attempt to constant fold a shufflevector instruction with the
141 /// specified operands and mask.  See class ShuffleVectorInst for a description
142 /// of the mask representation. The constant result is returned if successful;
143 /// if not, null is returned.
144 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
145                                                ArrayRef<int> Mask);
146 
147 /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
148 /// the end of C are accessed, they are assumed to be poison.
149 Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
150                                     const DataLayout &DL);
151 
152 /// Extract value of C reinterpreted as Ty. Same as previous API with zero
153 /// offset.
154 Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty,
155                                     const DataLayout &DL);
156 
157 /// Return the value that a load from C with offset Offset would produce if it
158 /// is constant and determinable. If this is not determinable, return null.
159 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
160                                        const DataLayout &DL);
161 
162 /// Return the value that a load from C would produce if it is constant and
163 /// determinable. If this is not determinable, return null.
164 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
165                                        const DataLayout &DL);
166 
167 /// If C is a uniform value where all bits are the same (either all zero, all
168 /// ones, all undef or all poison), return the corresponding uniform value in
169 /// the new type. If the value is not uniform or the result cannot be
170 /// represented, return null.
171 Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty);
172 
173 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
174 /// the specified function.
175 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
176 
177 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
178 /// with the specified arguments, returning null if unsuccessful.
179 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
180                            ArrayRef<Constant *> Operands,
181                            const TargetLibraryInfo *TLI = nullptr);
182 
183 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
184 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
185 /// integer and vice versa if their sizes are equal.
186 Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
187                                          const DataLayout &DL);
188 
189 /// Check whether the given call has no side-effects.
190 /// Specifically checks for math routimes which sometimes set errno.
191 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
192 
193 Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
194 }
195 
196 #endif
197