NoFolder.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- NoFolder.h - Constant 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 NoFolder class, a helper for IRBuilder. It provides
  15. // IRBuilder with a set of methods for creating unfolded constants. This is
  16. // useful for learners trying to understand how LLVM IR works, and who don't
  17. // want details to be hidden by the constant folder. For general constant
  18. // creation and folding, use ConstantExpr and the routines in
  19. // llvm/Analysis/ConstantFolding.h.
  20. //
  21. // Note: since it is not actually possible to create unfolded constants, this
  22. // class returns instructions rather than constants.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_IR_NOFOLDER_H
  26. #define LLVM_IR_NOFOLDER_H
  27. #include "llvm/ADT/ArrayRef.h"
  28. #include "llvm/IR/Constants.h"
  29. #include "llvm/IR/FMF.h"
  30. #include "llvm/IR/IRBuilderFolder.h"
  31. #include "llvm/IR/InstrTypes.h"
  32. #include "llvm/IR/Instruction.h"
  33. #include "llvm/IR/Instructions.h"
  34. namespace llvm {
  35. /// NoFolder - Create "constants" (actually, instructions) with no folding.
  36. class NoFolder final : public IRBuilderFolder {
  37. virtual void anchor();
  38. public:
  39. explicit NoFolder() = default;
  40. //===--------------------------------------------------------------------===//
  41. // Value-based folders.
  42. //
  43. // Return an existing value or a constant if the operation can be simplified.
  44. // Otherwise return nullptr.
  45. //===--------------------------------------------------------------------===//
  46. Value *FoldBinOp(Instruction::BinaryOps Opc, Value *LHS,
  47. Value *RHS) const override {
  48. return nullptr;
  49. }
  50. Value *FoldExactBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
  51. bool IsExact) const override {
  52. return nullptr;
  53. }
  54. Value *FoldNoWrapBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
  55. bool HasNUW, bool HasNSW) const override {
  56. return nullptr;
  57. }
  58. Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
  59. FastMathFlags FMF) const override {
  60. return nullptr;
  61. }
  62. Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
  63. FastMathFlags FMF) const override {
  64. return nullptr;
  65. }
  66. Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
  67. return nullptr;
  68. }
  69. Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
  70. bool IsInBounds = false) const override {
  71. return nullptr;
  72. }
  73. Value *FoldSelect(Value *C, Value *True, Value *False) const override {
  74. return nullptr;
  75. }
  76. Value *FoldExtractValue(Value *Agg,
  77. ArrayRef<unsigned> IdxList) const override {
  78. return nullptr;
  79. }
  80. Value *FoldInsertValue(Value *Agg, Value *Val,
  81. ArrayRef<unsigned> IdxList) const override {
  82. return nullptr;
  83. }
  84. Value *FoldExtractElement(Value *Vec, Value *Idx) const override {
  85. return nullptr;
  86. }
  87. Value *FoldInsertElement(Value *Vec, Value *NewElt,
  88. Value *Idx) const override {
  89. return nullptr;
  90. }
  91. Value *FoldShuffleVector(Value *V1, Value *V2,
  92. ArrayRef<int> Mask) const override {
  93. return nullptr;
  94. }
  95. //===--------------------------------------------------------------------===//
  96. // Cast/Conversion Operators
  97. //===--------------------------------------------------------------------===//
  98. Instruction *CreateCast(Instruction::CastOps Op, Constant *C,
  99. Type *DestTy) const override {
  100. return CastInst::Create(Op, C, DestTy);
  101. }
  102. Instruction *CreatePointerCast(Constant *C, Type *DestTy) const override {
  103. return CastInst::CreatePointerCast(C, DestTy);
  104. }
  105. Instruction *CreatePointerBitCastOrAddrSpaceCast(
  106. Constant *C, Type *DestTy) const override {
  107. return CastInst::CreatePointerBitCastOrAddrSpaceCast(C, DestTy);
  108. }
  109. Instruction *CreateIntCast(Constant *C, Type *DestTy,
  110. bool isSigned) const override {
  111. return CastInst::CreateIntegerCast(C, DestTy, isSigned);
  112. }
  113. Instruction *CreateFPCast(Constant *C, Type *DestTy) const override {
  114. return CastInst::CreateFPCast(C, DestTy);
  115. }
  116. Instruction *CreateBitCast(Constant *C, Type *DestTy) const override {
  117. return CreateCast(Instruction::BitCast, C, DestTy);
  118. }
  119. Instruction *CreateIntToPtr(Constant *C, Type *DestTy) const override {
  120. return CreateCast(Instruction::IntToPtr, C, DestTy);
  121. }
  122. Instruction *CreatePtrToInt(Constant *C, Type *DestTy) const override {
  123. return CreateCast(Instruction::PtrToInt, C, DestTy);
  124. }
  125. Instruction *CreateZExtOrBitCast(Constant *C, Type *DestTy) const override {
  126. return CastInst::CreateZExtOrBitCast(C, DestTy);
  127. }
  128. Instruction *CreateSExtOrBitCast(Constant *C, Type *DestTy) const override {
  129. return CastInst::CreateSExtOrBitCast(C, DestTy);
  130. }
  131. Instruction *CreateTruncOrBitCast(Constant *C, Type *DestTy) const override {
  132. return CastInst::CreateTruncOrBitCast(C, DestTy);
  133. }
  134. //===--------------------------------------------------------------------===//
  135. // Compare Instructions
  136. //===--------------------------------------------------------------------===//
  137. Instruction *CreateFCmp(CmpInst::Predicate P,
  138. Constant *LHS, Constant *RHS) const override {
  139. return new FCmpInst(P, LHS, RHS);
  140. }
  141. };
  142. } // end namespace llvm
  143. #endif // LLVM_IR_NOFOLDER_H
  144. #ifdef __GNUC__
  145. #pragma GCC diagnostic pop
  146. #endif