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
25 template <typename T> class ArrayRef;
34 class TargetLibraryInfo;
37 /// If this constant is a constant offset from a global, return the global and
38 /// the constant. Because of constantexprs, this function is recursive.
39 bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
40 const DataLayout &DL);
42 /// ConstantFoldInstruction - Try to constant fold the specified instruction.
43 /// If successful, the constant result is returned, if not, null is returned.
44 /// Note that this fails if not all of the operands are constant. Otherwise,
45 /// this function can only fail when attempting to fold instructions like loads
46 /// and stores, which have no constant expression form.
47 Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
48 const TargetLibraryInfo *TLI = nullptr);
50 /// ConstantFoldConstant - Attempt to fold the constant using the
51 /// specified DataLayout.
52 /// If successful, the constant result is returned, if not, null is returned.
53 Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
54 const TargetLibraryInfo *TLI = nullptr);
56 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
57 /// specified operands. If successful, the constant result is returned, if not,
58 /// null is returned. Note that this function can fail when attempting to
59 /// fold instructions like loads and stores, which have no constant expression
62 Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
64 const TargetLibraryInfo *TLI = nullptr);
66 /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
67 /// instruction (icmp/fcmp) with the specified operands. If it fails, it
68 /// returns a constant expression of the specified operands.
71 ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
72 Constant *RHS, const DataLayout &DL,
73 const TargetLibraryInfo *TLI = nullptr);
75 /// \brief Attempt to constant fold a binary operation with the specified
76 /// operands. If it fails, it returns a constant expression of the specified
78 Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
79 Constant *RHS, const DataLayout &DL);
81 /// \brief Attempt to constant fold a cast with the specified operand. If it
82 /// fails, it returns a constant expression of the specified operand.
83 Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
84 const DataLayout &DL);
86 /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
87 /// instruction with the specified operands and indices. The constant result is
88 /// returned if successful; if not, null is returned.
89 Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
90 ArrayRef<unsigned> Idxs);
92 /// \brief Attempt to constant fold an extractvalue instruction with the
93 /// specified operands and indices. The constant result is returned if
94 /// successful; if not, null is returned.
95 Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
96 ArrayRef<unsigned> Idxs);
98 /// \brief Attempt to constant fold an extractelement instruction with the
99 /// specified operands and indices. The constant result is returned if
100 /// successful; if not, null is returned.
101 Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
103 /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
104 /// produce if it is constant and determinable. If this is not determinable,
106 Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
108 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
109 /// getelementptr constantexpr, return the constant value being addressed by the
110 /// constant expression, or null if something is funny and we can't decide.
111 Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
113 /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
114 /// indices (with an *implied* zero pointer index that is not in the list),
115 /// return the constant value being addressed by a virtual load, or null if
116 /// something is funny and we can't decide.
117 Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
118 ArrayRef<Constant *> Indices);
120 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
121 /// the specified function.
122 bool canConstantFoldCallTo(const Function *F);
124 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
125 /// with the specified arguments, returning null if unsuccessful.
126 Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
127 const TargetLibraryInfo *TLI = nullptr);
129 /// \brief Check whether the given call has no side-effects.
130 /// Specifically checks for math routimes which sometimes set errno.
131 bool isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI);