ConstantFolding.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace llvm {
  26. class APInt;
  27. template <typename T> class ArrayRef;
  28. class CallBase;
  29. class Constant;
  30. class ConstantExpr;
  31. class DSOLocalEquivalent;
  32. class DataLayout;
  33. class Function;
  34. class GlobalValue;
  35. class Instruction;
  36. class TargetLibraryInfo;
  37. class Type;
  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.
  40. /// If the global is part of a dso_local_equivalent constant, return it through
  41. /// `Equiv` if it is provided.
  42. bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, APInt &Offset,
  43. const DataLayout &DL,
  44. DSOLocalEquivalent **DSOEquiv = nullptr);
  45. /// ConstantFoldInstruction - Try to constant fold the specified instruction.
  46. /// If successful, the constant result is returned, if not, null is returned.
  47. /// Note that this fails if not all of the operands are constant. Otherwise,
  48. /// this function can only fail when attempting to fold instructions like loads
  49. /// and stores, which have no constant expression form.
  50. Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
  51. const TargetLibraryInfo *TLI = nullptr);
  52. /// ConstantFoldConstant - Fold the constant using the specified DataLayout.
  53. /// This function always returns a non-null constant: Either the folding result,
  54. /// or the original constant if further folding is not possible.
  55. Constant *ConstantFoldConstant(const Constant *C, const DataLayout &DL,
  56. const TargetLibraryInfo *TLI = nullptr);
  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. ///
  63. Constant *ConstantFoldInstOperands(Instruction *I, ArrayRef<Constant *> Ops,
  64. const DataLayout &DL,
  65. 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.
  69. ///
  70. Constant *
  71. ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
  72. Constant *RHS, const DataLayout &DL,
  73. const TargetLibraryInfo *TLI = 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 select instruction with the specified
  85. /// operands. The constant result is returned if successful; if not, null is
  86. /// returned.
  87. Constant *ConstantFoldSelectInstruction(Constant *Cond, Constant *V1,
  88. Constant *V2);
  89. /// Attempt to constant fold a cast with the specified operand. If it
  90. /// fails, it returns a constant expression of the specified operand.
  91. Constant *ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy,
  92. const DataLayout &DL);
  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.
  96. Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
  97. ArrayRef<unsigned> Idxs);
  98. /// Attempt to constant fold an extractvalue instruction with the
  99. /// specified operands and indices. The constant result is returned if
  100. /// successful; if not, null is returned.
  101. Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
  102. ArrayRef<unsigned> Idxs);
  103. /// Attempt to constant fold an insertelement instruction with the
  104. /// specified operands and indices. The constant result is returned if
  105. /// successful; if not, null is returned.
  106. Constant *ConstantFoldInsertElementInstruction(Constant *Val,
  107. Constant *Elt,
  108. Constant *Idx);
  109. /// Attempt to constant fold an extractelement instruction with the
  110. /// specified operands and indices. The constant result is returned if
  111. /// successful; if not, null is returned.
  112. Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
  113. /// Attempt to constant fold a shufflevector instruction with the
  114. /// specified operands and mask. See class ShuffleVectorInst for a description
  115. /// of the mask representation. The constant result is returned if successful;
  116. /// if not, null is returned.
  117. Constant *ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2,
  118. ArrayRef<int> Mask);
  119. /// Extract value of C at the given Offset reinterpreted as Ty. If bits past
  120. /// the end of C are accessed, they are assumed to be poison.
  121. Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset,
  122. const DataLayout &DL);
  123. /// Extract value of C reinterpreted as Ty. Same as previous API with zero
  124. /// offset.
  125. Constant *ConstantFoldLoadFromConst(Constant *C, Type *Ty,
  126. const DataLayout &DL);
  127. /// Return the value that a load from C with offset Offset would produce if it
  128. /// is constant and determinable. If this is not determinable, return null.
  129. Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset,
  130. const DataLayout &DL);
  131. /// Return the value that a load from C would produce if it is constant and
  132. /// determinable. If this is not determinable, return null.
  133. Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
  134. const DataLayout &DL);
  135. /// If C is a uniform value where all bits are the same (either all zero, all
  136. /// ones, all undef or all poison), return the corresponding uniform value in
  137. /// the new type. If the value is not uniform or the result cannot be
  138. /// represented, return null.
  139. Constant *ConstantFoldLoadFromUniformValue(Constant *C, Type *Ty);
  140. /// canConstantFoldCallTo - Return true if its even possible to fold a call to
  141. /// the specified function.
  142. bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
  143. /// ConstantFoldCall - Attempt to constant fold a call to the specified function
  144. /// with the specified arguments, returning null if unsuccessful.
  145. Constant *ConstantFoldCall(const CallBase *Call, Function *F,
  146. ArrayRef<Constant *> Operands,
  147. const TargetLibraryInfo *TLI = nullptr);
  148. /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
  149. /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
  150. /// integer and vice versa if their sizes are equal.
  151. Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
  152. const DataLayout &DL);
  153. /// Check whether the given call has no side-effects.
  154. /// Specifically checks for math routimes which sometimes set errno.
  155. bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
  156. }
  157. #endif
  158. #ifdef __GNUC__
  159. #pragma GCC diagnostic pop
  160. #endif