InstructionSimplify.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- InstructionSimplify.h - Fold instrs into simpler forms --*- 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 declares routines for folding instructions into simpler forms
  15. // that do not require creating new instructions. This does constant folding
  16. // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
  17. // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
  18. // ("and i32 %x, %x" -> "%x"). If the simplification is also an instruction
  19. // then it dominates the original instruction.
  20. //
  21. // These routines implicitly resolve undef uses. The easiest way to be safe when
  22. // using these routines to obtain simplified values for existing instructions is
  23. // to always replace all uses of the instructions with the resulting simplified
  24. // values. This will prevent other code from seeing the same undef uses and
  25. // resolving them to different values.
  26. //
  27. // These routines are designed to tolerate moderately incomplete IR, such as
  28. // instructions that are not connected to basic blocks yet. However, they do
  29. // require that all the IR that they encounter be valid. In particular, they
  30. // require that all non-constant values be defined in the same function, and the
  31. // same call context of that function (and not split between caller and callee
  32. // contexts of a directly recursive call, for example).
  33. //
  34. // Additionally, these routines can't simplify to the instructions that are not
  35. // def-reachable, meaning we can't just scan the basic block for instructions
  36. // to simplify to.
  37. //
  38. //===----------------------------------------------------------------------===//
  39. #ifndef LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
  40. #define LLVM_ANALYSIS_INSTRUCTIONSIMPLIFY_H
  41. #include "llvm/IR/Instruction.h"
  42. #include "llvm/IR/Operator.h"
  43. namespace llvm {
  44. template <typename T, typename... TArgs> class AnalysisManager;
  45. template <class T> class ArrayRef;
  46. class AssumptionCache;
  47. class BinaryOperator;
  48. class CallBase;
  49. class DataLayout;
  50. class DominatorTree;
  51. class Function;
  52. struct LoopStandardAnalysisResults;
  53. class MDNode;
  54. class OptimizationRemarkEmitter;
  55. class Pass;
  56. template <class T, unsigned n> class SmallSetVector;
  57. class TargetLibraryInfo;
  58. class Type;
  59. class Value;
  60. /// InstrInfoQuery provides an interface to query additional information for
  61. /// instructions like metadata or keywords like nsw, which provides conservative
  62. /// results if the users specified it is safe to use.
  63. struct InstrInfoQuery {
  64. InstrInfoQuery(bool UMD) : UseInstrInfo(UMD) {}
  65. InstrInfoQuery() : UseInstrInfo(true) {}
  66. bool UseInstrInfo = true;
  67. MDNode *getMetadata(const Instruction *I, unsigned KindID) const {
  68. if (UseInstrInfo)
  69. return I->getMetadata(KindID);
  70. return nullptr;
  71. }
  72. template <class InstT> bool hasNoUnsignedWrap(const InstT *Op) const {
  73. if (UseInstrInfo)
  74. return Op->hasNoUnsignedWrap();
  75. return false;
  76. }
  77. template <class InstT> bool hasNoSignedWrap(const InstT *Op) const {
  78. if (UseInstrInfo)
  79. return Op->hasNoSignedWrap();
  80. return false;
  81. }
  82. bool isExact(const BinaryOperator *Op) const {
  83. if (UseInstrInfo && isa<PossiblyExactOperator>(Op))
  84. return cast<PossiblyExactOperator>(Op)->isExact();
  85. return false;
  86. }
  87. };
  88. struct SimplifyQuery {
  89. const DataLayout &DL;
  90. const TargetLibraryInfo *TLI = nullptr;
  91. const DominatorTree *DT = nullptr;
  92. AssumptionCache *AC = nullptr;
  93. const Instruction *CxtI = nullptr;
  94. // Wrapper to query additional information for instructions like metadata or
  95. // keywords like nsw, which provides conservative results if those cannot
  96. // be safely used.
  97. const InstrInfoQuery IIQ;
  98. /// Controls whether simplifications are allowed to constrain the range of
  99. /// possible values for uses of undef. If it is false, simplifications are not
  100. /// allowed to assume a particular value for a use of undef for example.
  101. bool CanUseUndef = true;
  102. SimplifyQuery(const DataLayout &DL, const Instruction *CXTI = nullptr)
  103. : DL(DL), CxtI(CXTI) {}
  104. SimplifyQuery(const DataLayout &DL, const TargetLibraryInfo *TLI,
  105. const DominatorTree *DT = nullptr,
  106. AssumptionCache *AC = nullptr,
  107. const Instruction *CXTI = nullptr, bool UseInstrInfo = true,
  108. bool CanUseUndef = true)
  109. : DL(DL), TLI(TLI), DT(DT), AC(AC), CxtI(CXTI), IIQ(UseInstrInfo),
  110. CanUseUndef(CanUseUndef) {}
  111. SimplifyQuery getWithInstruction(Instruction *I) const {
  112. SimplifyQuery Copy(*this);
  113. Copy.CxtI = I;
  114. return Copy;
  115. }
  116. SimplifyQuery getWithoutUndef() const {
  117. SimplifyQuery Copy(*this);
  118. Copy.CanUseUndef = false;
  119. return Copy;
  120. }
  121. /// If CanUseUndef is true, returns whether \p V is undef.
  122. /// Otherwise always return false.
  123. bool isUndefValue(Value *V) const {
  124. if (!CanUseUndef)
  125. return false;
  126. return isa<UndefValue>(V);
  127. }
  128. };
  129. // NOTE: the explicit multiple argument versions of these functions are
  130. // deprecated.
  131. // Please use the SimplifyQuery versions in new code.
  132. /// Given operand for an FNeg, fold the result or return null.
  133. Value *SimplifyFNegInst(Value *Op, FastMathFlags FMF,
  134. const SimplifyQuery &Q);
  135. /// Given operands for an Add, fold the result or return null.
  136. Value *SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
  137. const SimplifyQuery &Q);
  138. /// Given operands for a Sub, fold the result or return null.
  139. Value *SimplifySubInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW,
  140. const SimplifyQuery &Q);
  141. /// Given operands for an FAdd, fold the result or return null.
  142. Value *SimplifyFAddInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  143. const SimplifyQuery &Q);
  144. /// Given operands for an FSub, fold the result or return null.
  145. Value *SimplifyFSubInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  146. const SimplifyQuery &Q);
  147. /// Given operands for an FMul, fold the result or return null.
  148. Value *SimplifyFMulInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  149. const SimplifyQuery &Q);
  150. /// Given operands for the multiplication of a FMA, fold the result or return
  151. /// null. In contrast to SimplifyFMulInst, this function will not perform
  152. /// simplifications whose unrounded results differ when rounded to the argument
  153. /// type.
  154. Value *SimplifyFMAFMul(Value *LHS, Value *RHS, FastMathFlags FMF,
  155. const SimplifyQuery &Q);
  156. /// Given operands for a Mul, fold the result or return null.
  157. Value *SimplifyMulInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  158. /// Given operands for an SDiv, fold the result or return null.
  159. Value *SimplifySDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  160. /// Given operands for a UDiv, fold the result or return null.
  161. Value *SimplifyUDivInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  162. /// Given operands for an FDiv, fold the result or return null.
  163. Value *SimplifyFDivInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  164. const SimplifyQuery &Q);
  165. /// Given operands for an SRem, fold the result or return null.
  166. Value *SimplifySRemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  167. /// Given operands for a URem, fold the result or return null.
  168. Value *SimplifyURemInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  169. /// Given operands for an FRem, fold the result or return null.
  170. Value *SimplifyFRemInst(Value *LHS, Value *RHS, FastMathFlags FMF,
  171. const SimplifyQuery &Q);
  172. /// Given operands for a Shl, fold the result or return null.
  173. Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  174. const SimplifyQuery &Q);
  175. /// Given operands for a LShr, fold the result or return null.
  176. Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
  177. const SimplifyQuery &Q);
  178. /// Given operands for a AShr, fold the result or return nulll.
  179. Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
  180. const SimplifyQuery &Q);
  181. /// Given operands for an And, fold the result or return null.
  182. Value *SimplifyAndInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  183. /// Given operands for an Or, fold the result or return null.
  184. Value *SimplifyOrInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  185. /// Given operands for an Xor, fold the result or return null.
  186. Value *SimplifyXorInst(Value *LHS, Value *RHS, const SimplifyQuery &Q);
  187. /// Given operands for an ICmpInst, fold the result or return null.
  188. Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  189. const SimplifyQuery &Q);
  190. /// Given operands for an FCmpInst, fold the result or return null.
  191. Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  192. FastMathFlags FMF, const SimplifyQuery &Q);
  193. /// Given operands for a SelectInst, fold the result or return null.
  194. Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
  195. const SimplifyQuery &Q);
  196. /// Given operands for a GetElementPtrInst, fold the result or return null.
  197. Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
  198. const SimplifyQuery &Q);
  199. /// Given operands for an InsertValueInst, fold the result or return null.
  200. Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
  201. const SimplifyQuery &Q);
  202. /// Given operands for an InsertElement, fold the result or return null.
  203. Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
  204. const SimplifyQuery &Q);
  205. /// Given operands for an ExtractValueInst, fold the result or return null.
  206. Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
  207. const SimplifyQuery &Q);
  208. /// Given operands for an ExtractElementInst, fold the result or return null.
  209. Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
  210. const SimplifyQuery &Q);
  211. /// Given operands for a CastInst, fold the result or return null.
  212. Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
  213. const SimplifyQuery &Q);
  214. /// Given operands for a ShuffleVectorInst, fold the result or return null.
  215. /// See class ShuffleVectorInst for a description of the mask representation.
  216. Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1, ArrayRef<int> Mask,
  217. Type *RetTy, const SimplifyQuery &Q);
  218. //=== Helper functions for higher up the class hierarchy.
  219. /// Given operands for a CmpInst, fold the result or return null.
  220. Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  221. const SimplifyQuery &Q);
  222. /// Given operand for a UnaryOperator, fold the result or return null.
  223. Value *SimplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q);
  224. /// Given operand for a UnaryOperator, fold the result or return null.
  225. /// Try to use FastMathFlags when folding the result.
  226. Value *SimplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
  227. const SimplifyQuery &Q);
  228. /// Given operands for a BinaryOperator, fold the result or return null.
  229. Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  230. const SimplifyQuery &Q);
  231. /// Given operands for a BinaryOperator, fold the result or return null.
  232. /// Try to use FastMathFlags when folding the result.
  233. Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  234. FastMathFlags FMF, const SimplifyQuery &Q);
  235. /// Given a callsite, fold the result or return null.
  236. Value *SimplifyCall(CallBase *Call, const SimplifyQuery &Q);
  237. /// Given an operand for a Freeze, see if we can fold the result.
  238. /// If not, this returns null.
  239. Value *SimplifyFreezeInst(Value *Op, const SimplifyQuery &Q);
  240. /// See if we can compute a simplified version of this instruction. If not,
  241. /// return null.
  242. Value *SimplifyInstruction(Instruction *I, const SimplifyQuery &Q,
  243. OptimizationRemarkEmitter *ORE = nullptr);
  244. /// See if V simplifies when its operand Op is replaced with RepOp. If not,
  245. /// return null.
  246. /// AllowRefinement specifies whether the simplification can be a refinement,
  247. /// or whether it needs to be strictly identical.
  248. Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
  249. const SimplifyQuery &Q, bool AllowRefinement);
  250. /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
  251. ///
  252. /// This first performs a normal RAUW of I with SimpleV. It then recursively
  253. /// attempts to simplify those users updated by the operation. The 'I'
  254. /// instruction must not be equal to the simplified value 'SimpleV'.
  255. /// If UnsimplifiedUsers is provided, instructions that could not be simplified
  256. /// are added to it.
  257. ///
  258. /// The function returns true if any simplifications were performed.
  259. bool replaceAndRecursivelySimplify(
  260. Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI = nullptr,
  261. const DominatorTree *DT = nullptr, AssumptionCache *AC = nullptr,
  262. SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr);
  263. // These helper functions return a SimplifyQuery structure that contains as
  264. // many of the optional analysis we use as are currently valid. This is the
  265. // strongly preferred way of constructing SimplifyQuery in passes.
  266. const SimplifyQuery getBestSimplifyQuery(Pass &, Function &);
  267. template <class T, class... TArgs>
  268. const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &,
  269. Function &);
  270. const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &,
  271. const DataLayout &);
  272. } // end namespace llvm
  273. #endif
  274. #ifdef __GNUC__
  275. #pragma GCC diagnostic pop
  276. #endif