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 namespace llvm {
23 class APInt;
24 template <typename T> class ArrayRef;
25 class CallBase;
26 class Constant;
27 class ConstantExpr;
28 class DSOLocalEquivalent;
29 class DataLayout;
30 class Function;
31 class GlobalValue;
32 class Instruction;
33 class TargetLibraryInfo;
34 class Type;
35 
36 /// If this constant is a constant offset from a global, return the global and
37 /// the constant. Because of constantexprs, this function is recursive.
38 /// If the global is part of a dso_local_equivalent constant, return it through
39 /// `Equiv` if it is provided.
40 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
41                                 const DataLayout &DL,
42                                 DSOLocalEquivalent **DSOEquiv = nullptr);
43 
44 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
45 /// If successful, the constant result is returned, if not, null is returned.
46 /// Note that this fails if not all of the operands are constant.  Otherwise,
47 /// this function can only fail when attempting to fold instructions like loads
48 /// and stores, which have no constant expression form.
49 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
50                                   const TargetLibraryInfo *TLI = nullptr);
51 
52 /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
53 /// This function always returns a non-null constant: Either the folding result,
54 /// or the original constant if further folding is not possible.
55 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
56                                const TargetLibraryInfo *TLI = nullptr);
57 
58 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
59 /// specified operands.  If successful, the constant result is returned, if not,
60 /// null is returned.  Note that this function can fail when attempting to
61 /// fold instructions like loads and stores, which have no constant expression
62 /// form.
63 ///
64 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
65                                    const DataLayout &DL,
66                                    const TargetLibraryInfo *TLI = nullptr);
67 
68 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
69 /// instruction (icmp/fcmp) with the specified operands.  If it fails, it
70 /// returns a constant expression of the specified operands.
71 ///
72 Constant *
73 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
74                                 Constant *RHS, const DataLayout &DL,
75                                 const TargetLibraryInfo *TLI = nullptr);
76 
77 /// Attempt to constant fold a unary operation with the specified
78 /// operand. If it fails, it returns a constant expression of the specified
79 /// operands.
80 Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
81                                      const DataLayout &DL);
82 
83 /// Attempt to constant fold a binary operation with the specified
84 /// operands.  If it fails, it returns a constant expression of the specified
85 /// operands.
86 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
87                                        Constant *RHS, const DataLayout &DL);
88 
89 /// Attempt to constant fold a select instruction with the specified
90 /// operands. The constant result is returned if successful; if not, null is
91 /// returned.
92 Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
93                                         Constant *V2);
94 
95 /// Attempt to constant fold a cast with the specified operand.  If it
96 /// fails, it returns a constant expression of the specified operand.
97 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
98                                   const DataLayout &DL);
99 
100 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
101 /// instruction with the specified operands and indices.  The constant result is
102 /// returned if successful; if not, null is returned.
103 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
104                                              ArrayRef<unsigned> Idxs);
105 
106 /// Attempt to constant fold an extractvalue instruction with the
107 /// specified operands and indices.  The constant result is returned if
108 /// successful; if not, null is returned.
109 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
110                                               ArrayRef<unsigned> Idxs);
111 
112 /// Attempt to constant fold an insertelement instruction with the
113 /// specified operands and indices.  The constant result is returned if
114 /// successful; if not, null is returned.
115 Constant *ConstantFoldInsertElementInstruction(Constant *Val,
116                                                Constant *Elt,
117                                                Constant *Idx);
118 
119 /// Attempt to constant fold an extractelement instruction with the
120 /// specified operands and indices.  The constant result is returned if
121 /// successful; if not, null is returned.
122 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
123 
124 /// Attempt to constant fold a shufflevector instruction with the
125 /// specified operands and mask.  See class ShuffleVectorInst for a description
126 /// of the mask representation. The constant result is returned if successful;
127 /// if not, null is returned.
128 Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
129                                                ArrayRef<int> Mask);
130 
131 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
132 /// produce if it is constant and determinable.  If this is not determinable,
133 /// return null.
134 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
135 
136 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
137 /// getelementptr constantexpr, return the constant value being addressed by the
138 /// constant expression, or null if something is funny and we can't decide.
139 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE,
140                                                  Type *Ty,
141                                                  const DataLayout &DL);
142 
143 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
144 /// indices (with an *implied* zero pointer index that is not in the list),
145 /// return the constant value being addressed by a virtual load, or null if
146 /// something is funny and we can't decide.
147 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
148                                             ArrayRef<Constant *> Indices);
149 
150 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
151 /// the specified function.
152 bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
153 
154 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
155 /// with the specified arguments, returning null if unsuccessful.
156 Constant *ConstantFoldCall(const CallBase *Call, Function *F,
157                            ArrayRef<Constant *> Operands,
158                            const TargetLibraryInfo *TLI = nullptr);
159 
160 /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
161 /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
162 /// integer and vice versa if their sizes are equal.
163 Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
164                                          const DataLayout &DL);
165 
166 /// Check whether the given call has no side-effects.
167 /// Specifically checks for math routimes which sometimes set errno.
168 bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
169 }
170 
171 #endif
172