blob: 354b5579236ec4dcaa5254941d01897723e1d23c [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares routines for folding instructions into constants when all
11// operands are constants, for example "sub i32 1, 0" -> "1".
12//
13// Also, to supplement the basic VMCore ConstantExpr simplifications,
14// this file declares some additional folding routines that can make use of
15// DataLayout information. These functions cannot go in VMCore due to library
16// dependency issues.
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
21#define LLVM_ANALYSIS_CONSTANTFOLDING_H
22
23namespace llvm {
24class APInt;
25template <typename T> class ArrayRef;
26class CallSite;
27class Constant;
28class ConstantExpr;
29class ConstantVector;
30class DataLayout;
31class Function;
32class GlobalValue;
33class Instruction;
34class ImmutableCallSite;
35class TargetLibraryInfo;
36class 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.
40bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
41 const DataLayout &DL);
42
43/// ConstantFoldInstruction - Try to constant fold the specified instruction.
44/// If successful, the constant result is returned, if not, null is returned.
45/// Note that this fails if not all of the operands are constant. Otherwise,
46/// this function can only fail when attempting to fold instructions like loads
47/// and stores, which have no constant expression form.
48Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
49 const TargetLibraryInfo *TLI = nullptr);
50
51/// ConstantFoldConstant - Attempt to fold the constant using the
52/// specified DataLayout.
53/// If successful, the constant result is returned, if not, null is returned.
54Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
55 const TargetLibraryInfo *TLI = nullptr);
56
57/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
58/// specified operands. If successful, the constant result is returned, if not,
59/// null is returned. Note that this function can fail when attempting to
60/// fold instructions like loads and stores, which have no constant expression
61/// form.
62///
63Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
64 const DataLayout &DL,
65 const TargetLibraryInfo *TLI = nullptr);
66
67/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
68/// instruction (icmp/fcmp) with the specified operands. If it fails, it
69/// returns a constant expression of the specified operands.
70///
71Constant *
72ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
73 Constant *RHS, const DataLayout &DL,
74 const TargetLibraryInfo *TLI = nullptr);
75
76/// \brief Attempt to constant fold a binary operation with the specified
77/// operands. If it fails, it returns a constant expression of the specified
78/// operands.
79Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
80 Constant *RHS, const DataLayout &DL);
81
82/// \brief Attempt to constant fold a select instruction with the specified
83/// operands. The constant result is returned if successful; if not, null is
84/// returned.
85Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
86 Constant *V2);
87
88/// \brief Attempt to constant fold a cast with the specified operand. If it
89/// fails, it returns a constant expression of the specified operand.
90Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
91 const DataLayout &DL);
92
93/// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
94/// instruction with the specified operands and indices. The constant result is
95/// returned if successful; if not, null is returned.
96Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
97 ArrayRef<unsigned> Idxs);
98
99/// \brief Attempt to constant fold an extractvalue instruction with the
100/// specified operands and indices. The constant result is returned if
101/// successful; if not, null is returned.
102Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
103 ArrayRef<unsigned> Idxs);
104
105/// \brief Attempt to constant fold an insertelement instruction with the
106/// specified operands and indices. The constant result is returned if
107/// successful; if not, null is returned.
108Constant *ConstantFoldInsertElementInstruction(Constant *Val,
109 Constant *Elt,
110 Constant *Idx);
111
112/// \brief Attempt to constant fold an extractelement instruction with the
113/// specified operands and indices. The constant result is returned if
114/// successful; if not, null is returned.
115Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
116
117/// \brief Attempt to constant fold a shufflevector instruction with the
118/// specified operands and indices. The constant result is returned if
119/// successful; if not, null is returned.
120Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
121 Constant *Mask);
122
123/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
124/// produce if it is constant and determinable. If this is not determinable,
125/// return null.
126Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
127
128/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
129/// getelementptr constantexpr, return the constant value being addressed by the
130/// constant expression, or null if something is funny and we can't decide.
131Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
132
133/// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
134/// indices (with an *implied* zero pointer index that is not in the list),
135/// return the constant value being addressed by a virtual load, or null if
136/// something is funny and we can't decide.
137Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
138 ArrayRef<Constant *> Indices);
139
140/// canConstantFoldCallTo - Return true if its even possible to fold a call to
141/// the specified function.
142bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F);
143
144/// ConstantFoldCall - Attempt to constant fold a call to the specified function
145/// with the specified arguments, returning null if unsuccessful.
146Constant *ConstantFoldCall(ImmutableCallSite CS, Function *F,
147 ArrayRef<Constant *> Operands,
148 const TargetLibraryInfo *TLI = nullptr);
149
150/// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
151/// returning null if unsuccessful. Can cast pointer to pointer or pointer to
152/// integer and vice versa if their sizes are equal.
153Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
154 const DataLayout &DL);
155
156/// \brief Check whether the given call has no side-effects.
157/// Specifically checks for math routimes which sometimes set errno.
158bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI);
159}
160
161#endif