IRBuilderFolder.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- IRBuilderFolder.h - Const folder interface for IRBuilder -*- 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 defines for constant folding interface used by IRBuilder.
  15. // It is implemented by ConstantFolder (default), TargetFolder and NoFoler.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_IR_IRBUILDERFOLDER_H
  19. #define LLVM_IR_IRBUILDERFOLDER_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/IR/InstrTypes.h"
  22. #include "llvm/IR/Instruction.h"
  23. namespace llvm {
  24. /// IRBuilderFolder - Interface for constant folding in IRBuilder.
  25. class IRBuilderFolder {
  26. public:
  27. virtual ~IRBuilderFolder();
  28. //===--------------------------------------------------------------------===//
  29. // Value-based folders.
  30. //
  31. // Return an existing value or a constant if the operation can be simplified.
  32. // Otherwise return nullptr.
  33. //===--------------------------------------------------------------------===//
  34. virtual Value *FoldAdd(Value *LHS, Value *RHS, bool HasNUW = false,
  35. bool HasNSW = false) const = 0;
  36. virtual Value *FoldAnd(Value *LHS, Value *RHS) const = 0;
  37. virtual Value *FoldOr(Value *LHS, Value *RHS) const = 0;
  38. virtual Value *FoldICmp(CmpInst::Predicate P, Value *LHS,
  39. Value *RHS) const = 0;
  40. virtual Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  41. bool IsInBounds = false) const = 0;
  42. virtual Value *FoldSelect(Value *C, Value *True, Value *False) const = 0;
  43. //===--------------------------------------------------------------------===//
  44. // Binary Operators
  45. //===--------------------------------------------------------------------===//
  46. virtual Value *CreateFAdd(Constant *LHS, Constant *RHS) const = 0;
  47. virtual Value *CreateSub(Constant *LHS, Constant *RHS,
  48. bool HasNUW = false, bool HasNSW = false) const = 0;
  49. virtual Value *CreateFSub(Constant *LHS, Constant *RHS) const = 0;
  50. virtual Value *CreateMul(Constant *LHS, Constant *RHS,
  51. bool HasNUW = false, bool HasNSW = false) const = 0;
  52. virtual Value *CreateFMul(Constant *LHS, Constant *RHS) const = 0;
  53. virtual Value *CreateUDiv(Constant *LHS, Constant *RHS,
  54. bool isExact = false) const = 0;
  55. virtual Value *CreateSDiv(Constant *LHS, Constant *RHS,
  56. bool isExact = false) const = 0;
  57. virtual Value *CreateFDiv(Constant *LHS, Constant *RHS) const = 0;
  58. virtual Value *CreateURem(Constant *LHS, Constant *RHS) const = 0;
  59. virtual Value *CreateSRem(Constant *LHS, Constant *RHS) const = 0;
  60. virtual Value *CreateFRem(Constant *LHS, Constant *RHS) const = 0;
  61. virtual Value *CreateShl(Constant *LHS, Constant *RHS,
  62. bool HasNUW = false, bool HasNSW = false) const = 0;
  63. virtual Value *CreateLShr(Constant *LHS, Constant *RHS,
  64. bool isExact = false) const = 0;
  65. virtual Value *CreateAShr(Constant *LHS, Constant *RHS,
  66. bool isExact = false) const = 0;
  67. virtual Value *CreateXor(Constant *LHS, Constant *RHS) const = 0;
  68. virtual Value *CreateBinOp(Instruction::BinaryOps Opc,
  69. Constant *LHS, Constant *RHS) const = 0;
  70. //===--------------------------------------------------------------------===//
  71. // Unary Operators
  72. //===--------------------------------------------------------------------===//
  73. virtual Value *CreateNeg(Constant *C,
  74. bool HasNUW = false, bool HasNSW = false) const = 0;
  75. virtual Value *CreateFNeg(Constant *C) const = 0;
  76. virtual Value *CreateNot(Constant *C) const = 0;
  77. virtual Value *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const = 0;
  78. //===--------------------------------------------------------------------===//
  79. // Cast/Conversion Operators
  80. //===--------------------------------------------------------------------===//
  81. virtual Value *CreateCast(Instruction::CastOps Op, Constant *C,
  82. Type *DestTy) const = 0;
  83. virtual Value *CreatePointerCast(Constant *C, Type *DestTy) const = 0;
  84. virtual Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
  85. Type *DestTy) const = 0;
  86. virtual Value *CreateIntCast(Constant *C, Type *DestTy,
  87. bool isSigned) const = 0;
  88. virtual Value *CreateFPCast(Constant *C, Type *DestTy) const = 0;
  89. virtual Value *CreateBitCast(Constant *C, Type *DestTy) const = 0;
  90. virtual Value *CreateIntToPtr(Constant *C, Type *DestTy) const = 0;
  91. virtual Value *CreatePtrToInt(Constant *C, Type *DestTy) const = 0;
  92. virtual Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const = 0;
  93. virtual Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const = 0;
  94. virtual Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const = 0;
  95. //===--------------------------------------------------------------------===//
  96. // Compare Instructions
  97. //===--------------------------------------------------------------------===//
  98. virtual Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
  99. Constant *RHS) const = 0;
  100. //===--------------------------------------------------------------------===//
  101. // Other Instructions
  102. //===--------------------------------------------------------------------===//
  103. virtual Value *CreateExtractElement(Constant *Vec, Constant *Idx) const = 0;
  104. virtual Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
  105. Constant *Idx) const = 0;
  106. virtual Value *CreateShuffleVector(Constant *V1, Constant *V2,
  107. ArrayRef<int> Mask) const = 0;
  108. virtual Value *CreateExtractValue(Constant *Agg,
  109. ArrayRef<unsigned> IdxList) const = 0;
  110. virtual Value *CreateInsertValue(Constant *Agg, Constant *Val,
  111. ArrayRef<unsigned> IdxList) const = 0;
  112. };
  113. } // end namespace llvm
  114. #endif // LLVM_IR_IRBUILDERFOLDER_H
  115. #ifdef __GNUC__
  116. #pragma GCC diagnostic pop
  117. #endif