ConstantFolding.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares routines for folding instructions into constants when all
  15. // operands are constants, for example "sub i32 1, 0" -> "1".
  16. //
  17. // Also, to supplement the basic VMCore ConstantExpr simplifications,
  18. // this file declares some additional folding routines that can make use of
  19. // DataLayout information. These functions cannot go in VMCore due to library
  20. // dependency issues.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
  24. #define LLVM_ANALYSIS_CONSTANTFOLDING_H
  25. #include <stdint.h>
  26. namespace llvm {
  27. class APInt;
  28. template <typename T> class ArrayRef;
  29. class CallBase;
  30. class Constant;
  31. class DSOLocalEquivalent;
  32. class DataLayout;
  33. class Function;
  34. class GlobalValue;
  35. class GlobalVariable;
  36. class Instruction;
  37. class TargetLibraryInfo;
  38. class Type;
  39. /// If this constant is a constant offset from a global, return the global and
  40. /// the constant. Because of constantexprs, this function is recursive.
  41. /// If the global is part of a dso_local_equivalent constant, return it through
  42. /// `Equiv` if it is provided.
  43. bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
  44. const DataLayout &DL,
  45. DSOLocalEquivalent **DSOEquiv = nullptr);
  46. /// ConstantFoldInstruction - Try to constant fold the specified instruction.
  47. /// If successful, the constant result is returned, if not, null is returned.
  48. /// Note that this fails if not all of the operands are constant. Otherwise,
  49. /// this function can only fail when attempting to fold instructions like loads
  50. /// and stores, which have no constant expression form.
  51. Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
  52. const TargetLibraryInfo *TLI = nullptr);
  53. /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
  54. /// This function always returns a non-null constant: Either the folding result,
  55. /// or the original constant if further folding is not possible.
  56. Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
  57. const TargetLibraryInfo *TLI = nullptr);
  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. ///
  64. Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
  65. const DataLayout &DL,
  66. const TargetLibraryInfo *TLI = nullptr);
  67. /// Attempt to constant fold a compare instruction (icmp/fcmp) with the
  68. /// specified operands. If it fails, it returns a constant expression of the
  69. /// specified operands.
  70. /// Denormal inputs may be flushed based on the denormal handling mode.
  71. Constant *ConstantFoldCompareInstOperands(
  72. unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL,
  73. const TargetLibraryInfo *TLI = nullptr, const Instruction *I = nullptr);
  74. /// Attempt to constant fold a unary operation with the specified
  75. /// operand. If it fails, it returns a constant expression of the specified
  76. /// operands.
  77. Constant *ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
  78. const DataLayout &DL);
  79. /// Attempt to constant fold a binary operation with the specified
  80. /// operands. If it fails, it returns a constant expression of the specified
  81. /// operands.
  82. Constant *ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
  83. Constant *RHS, const DataLayout &DL);
  84. /// Attempt to constant fold a floating point binary operation with the
  85. /// specified operands, applying the denormal handling mod to the operands. If
  86. /// it fails, it returns a constant expression of the specified operands.
  87. Constant *ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,
  88. Constant *RHS, const DataLayout &DL,
  89. const Instruction *I);
  90. /// Attempt to flush float point constant according to denormal mode set in the
  91. /// instruction's parent function attributes. If so, return a zero with the
  92. /// correct sign, otherwise return the original constant. Inputs and outputs to
  93. /// floating point instructions can have their mode set separately, so the
  94. /// direction is also needed.
  95. Constant *FlushFPConstant(Constant *Operand, const Instruction *I,
  96. bool IsOutput);
  97. /// Attempt to constant fold a select instruction with the specified
  98. /// operands. The constant result is returned if successful; if not, null is
  99. /// returned.
  100. Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
  101. Constant *V2);
  102. /// Attempt to constant fold a cast with the specified operand. If it
  103. /// fails, it returns a constant expression of the specified operand.
  104. Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
  105. const DataLayout &DL);
  106. /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
  107. /// instruction with the specified operands and indices. The constant result is
  108. /// returned if successful; if not, null is returned.
  109. Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
  110. ArrayRef<unsigned> Idxs);
  111. /// Attempt to constant fold an extractvalue instruction with the
  112. /// specified operands and indices. The constant result is returned if
  113. /// successful; if not, null is returned.
  114. Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
  115. ArrayRef<unsigned> Idxs);
  116. /// Attempt to constant fold an insertelement instruction with the
  117. /// specified operands and indices. The constant result is returned if
  118. /// successful; if not, null is returned.
  119. Constant *ConstantFoldInsertElementInstruction(Constant *Val,
  120. Constant *Elt,
  121. Constant *Idx);
  122. /// Attempt to constant fold an extractelement instruction with the
  123. /// specified operands and indices. The constant result is returned if
  124. /// successful; if not, null is returned.
  125. Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
  126. /// Attempt to constant fold a shufflevector instruction with the
  127. /// specified operands and mask. See class ShuffleVectorInst for a description
  128. /// of the mask representation. The constant result is returned if successful;
  129. /// if not, null is returned.
  130. Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
  131. ArrayRef<int> Mask);
  132. /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
  133. /// the end of C are accessed, they are assumed to be poison.
  134. Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
  135. const DataLayout &DL);
  136. /// Extract value of C reinterpreted as Ty. Same as previous API with zero
  137. /// offset.
  138. Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty,
  139. const DataLayout &DL);
  140. /// Return the value that a load from C with offset Offset would produce if it
  141. /// is constant and determinable. If this is not determinable, return null.
  142. Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
  143. const DataLayout &DL);
  144. /// Return the value that a load from C would produce if it is constant and
  145. /// determinable. If this is not determinable, return null.
  146. Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
  147. const DataLayout &DL);
  148. /// If C is a uniform value where all bits are the same (either all zero, all
  149. /// ones, all undef or all poison), return the corresponding uniform value in
  150. /// the new type. If the value is not uniform or the result cannot be
  151. /// represented, return null.
  152. Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty);
  153. /// canConstantFoldCallTo - Return true if its even possible to fold a call to
  154. /// the specified function.
  155. bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
  156. /// ConstantFoldCall - Attempt to constant fold a call to the specified function
  157. /// with the specified arguments, returning null if unsuccessful.
  158. Constant *ConstantFoldCall(const CallBase *Call, Function *F,
  159. ArrayRef<Constant *> Operands,
  160. const TargetLibraryInfo *TLI = nullptr);
  161. /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
  162. /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
  163. /// integer and vice versa if their sizes are equal.
  164. Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
  165. const DataLayout &DL);
  166. /// Check whether the given call has no side-effects.
  167. /// Specifically checks for math routimes which sometimes set errno.
  168. bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
  169. Constant *ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset);
  170. }
  171. #endif
  172. #ifdef __GNUC__
  173. #pragma GCC diagnostic pop
  174. #endif