blob: ef6e66b2b88ea63ccef17d68452f0baefb7ed15b [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
22namespace llvm {
23class APInt;
24template <typename T> class ArrayRef;
Andrew Walbran16937d02019-10-22 13:54:20 +010025class CallBase;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026class Constant;
27class ConstantExpr;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020028class DSOLocalEquivalent;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010029class DataLayout;
30class Function;
31class GlobalValue;
32class Instruction;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010033class TargetLibraryInfo;
34class 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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020038/// If the global is part of a dso_local_equivalent constant, return it through
39/// `Equiv` if it is provided.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020041 const DataLayout &DL,
42 DSOLocalEquivalent **DSOEquiv = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043
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.
49Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
50 const TargetLibraryInfo *TLI = nullptr);
51
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020052/// 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.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055Constant *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///
64Constant *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///
72Constant *
73ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
74 Constant *RHS, const DataLayout &DL,
75 const TargetLibraryInfo *TLI = nullptr);
76
Andrew Walbran3d2c1972020-04-07 12:24:26 +010077/// 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.
80Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
81 const DataLayout &DL);
82
Andrew Scullcdfcccc2018-10-05 20:58:37 +010083/// Attempt to constant fold a binary operation with the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010084/// operands. If it fails, it returns a constant expression of the specified
85/// operands.
86Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
87 Constant *RHS, const DataLayout &DL);
88
Andrew Scullcdfcccc2018-10-05 20:58:37 +010089/// Attempt to constant fold a select instruction with the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010090/// operands. The constant result is returned if successful; if not, null is
91/// returned.
92Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
93 Constant *V2);
94
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095/// Attempt to constant fold a cast with the specified operand. If it
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096/// fails, it returns a constant expression of the specified operand.
97Constant *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.
103Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
104 ArrayRef<unsigned> Idxs);
105
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100106/// Attempt to constant fold an extractvalue instruction with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107/// specified operands and indices. The constant result is returned if
108/// successful; if not, null is returned.
109Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
110 ArrayRef<unsigned> Idxs);
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112/// Attempt to constant fold an insertelement instruction with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113/// specified operands and indices. The constant result is returned if
114/// successful; if not, null is returned.
115Constant *ConstantFoldInsertElementInstruction(Constant *Val,
116 Constant *Elt,
117 Constant *Idx);
118
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100119/// Attempt to constant fold an extractelement instruction with the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120/// specified operands and indices. The constant result is returned if
121/// successful; if not, null is returned.
122Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
123
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100124/// Attempt to constant fold a shufflevector instruction with the
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200125/// 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.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200129 ArrayRef<int> Mask);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100130
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.
134Constant *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.
139Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
140
141/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
142/// indices (with an *implied* zero pointer index that is not in the list),
143/// return the constant value being addressed by a virtual load, or null if
144/// something is funny and we can't decide.
145Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
146 ArrayRef<Constant *> Indices);
147
148/// canConstantFoldCallTo - Return true if its even possible to fold a call to
149/// the specified function.
Andrew Walbran16937d02019-10-22 13:54:20 +0100150bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151
152/// ConstantFoldCall - Attempt to constant fold a call to the specified function
153/// with the specified arguments, returning null if unsuccessful.
Andrew Walbran16937d02019-10-22 13:54:20 +0100154Constant *ConstantFoldCall(const CallBase *Call, Function *F,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100155 ArrayRef<Constant *> Operands,
156 const TargetLibraryInfo *TLI = nullptr);
157
158/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
159/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
160/// integer and vice versa if their sizes are equal.
161Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
162 const DataLayout &DL);
163
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100164/// Check whether the given call has no side-effects.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100165/// Specifically checks for math routimes which sometimes set errno.
Andrew Walbran16937d02019-10-22 13:54:20 +0100166bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167}
168
169#endif