InstSimplifyFolder.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InstSimplifyFolder.h - InstSimplify folding helper --------*- 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 the InstSimplifyFolder class, a helper for IRBuilder.
  15. // It provides IRBuilder with a set of methods for folding operations to
  16. // existing values using InstructionSimplify. At the moment, only a subset of
  17. // the implementation uses InstructionSimplify. The rest of the implementation
  18. // only folds constants.
  19. //
  20. // The folder also applies target-specific constant folding.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
  24. #define LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
  25. #include "llvm/ADT/ArrayRef.h"
  26. #include "llvm/Analysis/InstructionSimplify.h"
  27. #include "llvm/Analysis/TargetFolder.h"
  28. #include "llvm/IR/IRBuilderFolder.h"
  29. #include "llvm/IR/Instruction.h"
  30. namespace llvm {
  31. class Constant;
  32. /// InstSimplifyFolder - Use InstructionSimplify to fold operations to existing
  33. /// values. Also applies target-specific constant folding when not using
  34. /// InstructionSimplify.
  35. class InstSimplifyFolder final : public IRBuilderFolder {
  36. TargetFolder ConstFolder;
  37. SimplifyQuery SQ;
  38. virtual void anchor();
  39. public:
  40. InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
  41. //===--------------------------------------------------------------------===//
  42. // Value-based folders.
  43. //
  44. // Return an existing value or a constant if the operation can be simplified.
  45. // Otherwise return nullptr.
  46. //===--------------------------------------------------------------------===//
  47. Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
  48. Value *RHS) const override {
  49. return simplifyBinOp(Opc, LHS, RHS, SQ);
  50. }
  51. Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
  52. bool IsExact) const override {
  53. return simplifyBinOp(Opc, LHS, RHS, SQ);
  54. }
  55. Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
  56. bool HasNUW, bool HasNSW) const override {
  57. return simplifyBinOp(Opc, LHS, RHS, SQ);
  58. }
  59. Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
  60. FastMathFlags FMF) const override {
  61. return simplifyBinOp(Opc, LHS, RHS, FMF, SQ);
  62. }
  63. Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
  64. FastMathFlags FMF) const override {
  65. return simplifyUnOp(Opc, V, FMF, SQ);
  66. }
  67. Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
  68. return simplifyICmpInst(P, LHS, RHS, SQ);
  69. }
  70. Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  71. bool IsInBounds = false) const override {
  72. return simplifyGEPInst(Ty, Ptr, IdxList, IsInBounds, SQ);
  73. }
  74. Value *FoldSelect(Value *C, Value *True, Value *False) const override {
  75. return simplifySelectInst(C, True, False, SQ);
  76. }
  77. Value *FoldExtractValue(Value *Agg,
  78. ArrayRef<unsigned> IdxList) const override {
  79. return simplifyExtractValueInst(Agg, IdxList, SQ);
  80. };
  81. Value *FoldInsertValue(Value *Agg, Value *Val,
  82. ArrayRef<unsigned> IdxList) const override {
  83. return simplifyInsertValueInst(Agg, Val, IdxList, SQ);
  84. }
  85. Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
  86. return simplifyExtractElementInst(Vec, Idx, SQ);
  87. }
  88. Value *FoldInsertElement(Value *Vec, Value *NewElt,
  89. Value *Idx) const override {
  90. return simplifyInsertElementInst(Vec, NewElt, Idx, SQ);
  91. }
  92. Value *FoldShuffleVector(Value *V1, Value *V2,
  93. ArrayRef<int> Mask) const override {
  94. Type *RetTy = VectorType::get(
  95. cast<VectorType>(V1->getType())->getElementType(), Mask.size(),
  96. isa<ScalableVectorType>(V1->getType()));
  97. return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
  98. }
  99. //===--------------------------------------------------------------------===//
  100. // Cast/Conversion Operators
  101. //===--------------------------------------------------------------------===//
  102. Value *CreateCast(Instruction::CastOps Op, Constant *C,
  103. Type *DestTy) const override {
  104. if (C->getType() == DestTy)
  105. return C; // avoid calling Fold
  106. return ConstFolder.CreateCast(Op, C, DestTy);
  107. }
  108. Value *CreateIntCast(Constant *C, Type *DestTy,
  109. bool isSigned) const override {
  110. if (C->getType() == DestTy)
  111. return C; // avoid calling Fold
  112. return ConstFolder.CreateIntCast(C, DestTy, isSigned);
  113. }
  114. Value *CreatePointerCast(Constant *C, Type *DestTy) const override {
  115. if (C->getType() == DestTy)
  116. return C; // avoid calling Fold
  117. return ConstFolder.CreatePointerCast(C, DestTy);
  118. }
  119. Value *CreateFPCast(Constant *C, Type *DestTy) const override {
  120. if (C->getType() == DestTy)
  121. return C; // avoid calling Fold
  122. return ConstFolder.CreateFPCast(C, DestTy);
  123. }
  124. Value *CreateBitCast(Constant *C, Type *DestTy) const override {
  125. return ConstFolder.CreateBitCast(C, DestTy);
  126. }
  127. Value *CreateIntToPtr(Constant *C, Type *DestTy) const override {
  128. return ConstFolder.CreateIntToPtr(C, DestTy);
  129. }
  130. Value *CreatePtrToInt(Constant *C, Type *DestTy) const override {
  131. return ConstFolder.CreatePtrToInt(C, DestTy);
  132. }
  133. Value *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
  134. if (C->getType() == DestTy)
  135. return C; // avoid calling Fold
  136. return ConstFolder.CreateZExtOrBitCast(C, DestTy);
  137. }
  138. Value *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
  139. if (C->getType() == DestTy)
  140. return C; // avoid calling Fold
  141. return ConstFolder.CreateSExtOrBitCast(C, DestTy);
  142. }
  143. Value *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
  144. if (C->getType() == DestTy)
  145. return C; // avoid calling Fold
  146. return ConstFolder.CreateTruncOrBitCast(C, DestTy);
  147. }
  148. Value *CreatePointerBitCastOrAddrSpaceCast(Constant *C,
  149. Type *DestTy) const override {
  150. if (C->getType() == DestTy)
  151. return C; // avoid calling Fold
  152. return ConstFolder.CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
  153. }
  154. //===--------------------------------------------------------------------===//
  155. // Compare Instructions
  156. //===--------------------------------------------------------------------===//
  157. Value *CreateFCmp(CmpInst::Predicate P, Constant *LHS,
  158. Constant *RHS) const override {
  159. return ConstFolder.CreateFCmp(P, LHS, RHS);
  160. }
  161. };
  162. } // end namespace llvm
  163. #endif // LLVM_ANALYSIS_INSTSIMPLIFYFOLDER_H
  164. #ifdef __GNUC__
  165. #pragma GCC diagnostic pop
  166. #endif