ConstantFolding.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
  120. /// produce if it is constant and determinable. If this is not determinable,
  121. /// return null.
  122. Constant *ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL);
  123. /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
  124. /// getelementptr constantexpr, return the constant value being addressed by the
  125. /// constant expression, or null if something is funny and we can't decide.
  126. Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
  127. /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
  128. /// indices (with an *implied* zero pointer index that is not in the list),
  129. /// return the constant value being addressed by a virtual load, or null if
  130. /// something is funny and we can't decide.
  131. Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
  132. ArrayRef<Constant *> Indices);
  133. /// canConstantFoldCallTo - Return true if its even possible to fold a call to
  134. /// the specified function.
  135. bool canConstantFoldCallTo(const CallBase *Call, const Function *F);
  136. /// ConstantFoldCall - Attempt to constant fold a call to the specified function
  137. /// with the specified arguments, returning null if unsuccessful.
  138. Constant *ConstantFoldCall(const CallBase *Call, Function *F,
  139. ArrayRef<Constant *> Operands,
  140. const TargetLibraryInfo *TLI = nullptr);
  141. /// ConstantFoldLoadThroughBitcast - try to cast constant to destination type
  142. /// returning null if unsuccessful. Can cast pointer to pointer or pointer to
  143. /// integer and vice versa if their sizes are equal.
  144. Constant *ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy,
  145. const DataLayout &DL);
  146. /// Check whether the given call has no side-effects.
  147. /// Specifically checks for math routimes which sometimes set errno.
  148. bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI);
  149. }
  150. #endif
  151. #ifdef __GNUC__
  152. #pragma GCC diagnostic pop
  153. #endif