1//==-- ConstantFold.h - DL-independent Constant Folding Interface -*- 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 DataLayout-independent constant folding interface.
10// When possible, the DataLayout-aware constant folding interface in
11// Analysis/ConstantFolding.h should be preferred.
12//
13// These interfaces are used by the ConstantExpr::get* methods to automatically
14// fold constants when possible.
15//
16// These operators may return a null object if they don't know how to perform
17// the specified operation on the specified constant types.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_IR_CONSTANTFOLD_H
22#define LLVM_IR_CONSTANTFOLD_H
23
24#include "llvm/IR/InstrTypes.h"
25#include "llvm/Support/Compiler.h"
26#include <optional>
27
28namespace llvm {
29template <typename T> class ArrayRef;
30class Value;
31class Constant;
32class Type;
33
34// Constant fold various types of instruction...
35LLVM_ABI Constant *
36ConstantFoldCastInstruction(unsigned opcode, ///< The opcode of the cast
37 Constant *V, ///< The source constant
38 Type *DestTy ///< The destination type
39);
40
41/// Attempt to constant fold a select instruction with the specified
42/// operands. The constant result is returned if successful; if not, null is
43/// returned.
44LLVM_ABI Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
45 Constant *V2);
46
47/// Attempt to constant fold an extractelement instruction with the
48/// specified operands and indices. The constant result is returned if
49/// successful; if not, null is returned.
50LLVM_ABI Constant *ConstantFoldExtractElementInstruction(Constant *Val,
51 Constant *Idx);
52
53/// Attempt to constant fold an insertelement instruction with the
54/// specified operands and indices. The constant result is returned if
55/// successful; if not, null is returned.
56LLVM_ABI Constant *ConstantFoldInsertElementInstruction(Constant *Val,
57 Constant *Elt,
58 Constant *Idx);
59
60/// Attempt to constant fold a shufflevector instruction with the
61/// specified operands and mask. See class ShuffleVectorInst for a description
62/// of the mask representation. The constant result is returned if successful;
63/// if not, null is returned.
64LLVM_ABI Constant *ConstantFoldShuffleVectorInstruction(Constant *V1,
65 Constant *V2,
66 ArrayRef<int> Mask);
67
68/// Attempt to constant fold an extractvalue instruction with the
69/// specified operands and indices. The constant result is returned if
70/// successful; if not, null is returned.
71LLVM_ABI Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
72 ArrayRef<unsigned> Idxs);
73
74/// Attempt to constant fold an insertvalue instruction with the specified
75/// operands and indices. The constant result is returned if successful; if
76/// not, null is returned.
77LLVM_ABI Constant *ConstantFoldInsertValueInstruction(Constant *Agg,
78 Constant *Val,
79 ArrayRef<unsigned> Idxs);
80LLVM_ABI Constant *ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V);
81LLVM_ABI Constant *ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1,
82 Constant *V2);
83LLVM_ABI Constant *ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
84 Constant *C1, Constant *C2);
85LLVM_ABI Constant *
86ConstantFoldGetElementPtr(Type *Ty, Constant *C,
87 std::optional<ConstantRange> InRange,
88 ArrayRef<Value *> Idxs);
89} // namespace llvm
90
91#endif
92