ConstantFolding.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //===-- ConstantFolding.h - Fold instructions into constants ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file declares routines for folding instructions into constants when all
  11. // operands are constants, for example "sub i32 1, 0" -> "1".
  12. //
  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
  16. // dependency issues.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ANALYSIS_CONSTANTFOLDING_H
  20. #define LLVM_ANALYSIS_CONSTANTFOLDING_H
  21. #include "dxc/Support/WinAdapter.h" // HLSL Change
  22. namespace llvm {
  23. class Constant;
  24. class ConstantFP;
  25. class ConstantExpr;
  26. class Instruction;
  27. class DataLayout;
  28. class TargetLibraryInfo;
  29. class Function;
  30. class Type;
  31. template<typename T>
  32. class ArrayRef;
  33. /// ConstantFoldInstruction - Try to constant fold the specified instruction.
  34. /// If successful, the constant result is returned, if not, null is returned.
  35. /// Note that this fails if not all of the operands are constant. Otherwise,
  36. /// this function can only fail when attempting to fold instructions like loads
  37. /// and stores, which have no constant expression form.
  38. Constant *ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
  39. const TargetLibraryInfo *TLI = nullptr);
  40. /// ConstantFoldConstantExpression - Attempt to fold the constant expression
  41. /// using the specified DataLayout. If successful, the constant result is
  42. /// result is returned, if not, null is returned.
  43. Constant *
  44. ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL,
  45. const TargetLibraryInfo *TLI = nullptr);
  46. /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
  47. /// specified operands. If successful, the constant result is returned, if not,
  48. /// null is returned. Note that this function can fail when attempting to
  49. /// fold instructions like loads and stores, which have no constant expression
  50. /// form.
  51. ///
  52. Constant *ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
  53. ArrayRef<Constant *> Ops,
  54. const DataLayout &DL,
  55. const TargetLibraryInfo *TLI = nullptr);
  56. /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
  57. /// instruction (icmp/fcmp) with the specified operands. If it fails, it
  58. /// returns a constant expression of the specified operands.
  59. ///
  60. Constant *
  61. ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS,
  62. Constant *RHS, const DataLayout &DL,
  63. const TargetLibraryInfo *TLI = nullptr);
  64. /// ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue
  65. /// instruction with the specified operands and indices. The constant result is
  66. /// returned if successful; if not, null is returned.
  67. Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
  68. ArrayRef<unsigned> Idxs);
  69. /// \brief Attempt to constant fold an extractvalue instruction with the
  70. /// specified operands and indices. The constant result is returned if
  71. /// successful; if not, null is returned.
  72. Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
  73. ArrayRef<unsigned> Idxs);
  74. /// \brief Attempt to constant fold an extractelement instruction with the
  75. /// specified operands and indices. The constant result is returned if
  76. /// successful; if not, null is returned.
  77. Constant *ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx);
  78. /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
  79. /// produce if it is constant and determinable. If this is not determinable,
  80. /// return null.
  81. Constant *ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout &DL);
  82. /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
  83. /// getelementptr constantexpr, return the constant value being addressed by the
  84. /// constant expression, or null if something is funny and we can't decide.
  85. Constant *ConstantFoldLoadThroughGEPConstantExpr(Constant *C, ConstantExpr *CE);
  86. /// ConstantFoldLoadThroughGEPIndices - Given a constant and getelementptr
  87. /// indices (with an *implied* zero pointer index that is not in the list),
  88. /// return the constant value being addressed by a virtual load, or null if
  89. /// something is funny and we can't decide.
  90. Constant *ConstantFoldLoadThroughGEPIndices(Constant *C,
  91. ArrayRef<Constant*> Indices);
  92. /// canConstantFoldCallTo - Return true if its even possible to fold a call to
  93. /// the specified function.
  94. bool canConstantFoldCallTo(const Function *F);
  95. /// ConstantFoldCall - Attempt to constant fold a call to the specified function
  96. /// with the specified arguments, returning null if unsuccessful.
  97. Constant *ConstantFoldCall(Function *F, ArrayRef<Constant *> Operands,
  98. const TargetLibraryInfo *TLI = nullptr);
  99. /// HLSL Change - make these functions external so we can call them from
  100. /// DxilConstantFolding.cpp.
  101. Constant *ConstantFoldFP(double(__cdecl *NativeFP)(double), double V, Type *Ty);
  102. double getValueAsDouble(ConstantFP *Op);
  103. }
  104. #endif