ConstantFolding.h 5.5 KB

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