1 //===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares routines for folding instructions into constants when all
11 // operands are constants, for example "sub i32 1, 0" -> "1".
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
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
21 #define LLVM_ANALYSIS_CONSTANTFOLDING_H
28 class TargetLibraryInfo;
34 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
35 /// If successful, the constant result is returned, if not, null is returned.
36 /// Note that this fails if not all of the operands are constant. Otherwise,
37 /// this function can only fail when attempting to fold instructions like loads
38 /// and stores, which have no constant expression form.
39 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
40 const TargetLibraryInfo *TLI = nullptr);
42 /// ConstantFoldConstantExpression - Attempt to fold the constant expression
43 /// using the specified DataLayout. If successful, the constant result is
44 /// result is returned, if not, null is returned.
46 ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL,
47 const TargetLibraryInfo *TLI = nullptr);
49 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
50 /// specified operands. If successful, the constant result is returned, if not,
51 /// null is returned. Note that this function can fail when attempting to
52 /// fold instructions like loads and stores, which have no constant expression
55 Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
56 ArrayRef<Constant *> Ops,
58 const TargetLibraryInfo *TLI = nullptr);
60 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
61 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
62 /// returns a constant expression of the specified operands.
65 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
66 Constant *RHS, const DataLayout &DL,
67 const TargetLibraryInfo *TLI = nullptr);
69 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
70 /// instruction with the specified operands and indices. The constant result is
71 /// returned if successful; if not, null is returned.
72 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
73 ArrayRef<unsigned> Idxs);
75 /// \brief Attempt to constant fold an extractvalue instruction with the
76 /// specified operands and indices. The constant result is returned if
77 /// successful; if not, null is returned.
78 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
79 ArrayRef<unsigned> Idxs);
81 /// \brief Attempt to constant fold an extractelement instruction with the
82 /// specified operands and indices. The constant result is returned if
83 /// successful; if not, null is returned.
84 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
86 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
87 /// produce if it is constant and determinable. If this is not determinable,
89 Constant *ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout &DL);
91 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
92 /// getelementptr constantexpr, return the constant value being addressed by the
93 /// constant expression, or null if something is funny and we can't decide.
94 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
96 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
97 /// indices (with an *implied* zero pointer index that is not in the list),
98 /// return the constant value being addressed by a virtual load, or null if
99 /// something is funny and we can't decide.
100 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
101 ArrayRef<Constant*> Indices);
103 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
104 /// the specified function.
105 bool canConstantFoldCallTo(const Function *F);
107 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
108 /// with the specified arguments, returning null if unsuccessful.
109 Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
110 const TargetLibraryInfo *TLI = nullptr);