InstCombineInternal.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //===- InstCombineInternal.h - InstCombine pass internals -------*- 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. /// \file
  10. ///
  11. /// This file provides internal interfaces used to implement the InstCombine.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
  15. #define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
  16. #include "llvm/Analysis/AliasAnalysis.h"
  17. #include "llvm/Analysis/AssumptionCache.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. #include "llvm/Analysis/TargetFolder.h"
  20. #include "llvm/Analysis/ValueTracking.h"
  21. #include "llvm/IR/Dominators.h"
  22. #include "llvm/IR/IRBuilder.h"
  23. #include "llvm/IR/InstVisitor.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Operator.h"
  26. #include "llvm/IR/PatternMatch.h"
  27. #include "llvm/Pass.h"
  28. #include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
  29. #define DEBUG_TYPE "instcombine"
  30. namespace llvm {
  31. class CallSite;
  32. class DataLayout;
  33. class DominatorTree;
  34. class TargetLibraryInfo;
  35. class DbgDeclareInst;
  36. class MemIntrinsic;
  37. class MemSetInst;
  38. /// \brief Assign a complexity or rank value to LLVM Values.
  39. ///
  40. /// This routine maps IR values to various complexity ranks:
  41. /// 0 -> undef
  42. /// 1 -> Constants
  43. /// 2 -> Other non-instructions
  44. /// 3 -> Arguments
  45. /// 3 -> Unary operations
  46. /// 4 -> Other instructions
  47. static inline unsigned getComplexity(Value *V) {
  48. if (isa<Instruction>(V)) {
  49. if (BinaryOperator::isNeg(V) || BinaryOperator::isFNeg(V) ||
  50. BinaryOperator::isNot(V))
  51. return 3;
  52. return 4;
  53. }
  54. if (isa<Argument>(V))
  55. return 3;
  56. return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
  57. }
  58. /// \brief Add one to a Constant
  59. static inline Constant *AddOne(Constant *C) {
  60. return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
  61. }
  62. /// \brief Subtract one from a Constant
  63. static inline Constant *SubOne(Constant *C) {
  64. return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
  65. }
  66. /// \brief Return true if the specified value is free to invert (apply ~ to).
  67. /// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
  68. /// is true, work under the assumption that the caller intends to remove all
  69. /// uses of V and only keep uses of ~V.
  70. ///
  71. static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
  72. // ~(~(X)) -> X.
  73. if (BinaryOperator::isNot(V))
  74. return true;
  75. // Constants can be considered to be not'ed values.
  76. if (isa<ConstantInt>(V))
  77. return true;
  78. // Compares can be inverted if all of their uses are being modified to use the
  79. // ~V.
  80. if (isa<CmpInst>(V))
  81. return WillInvertAllUses;
  82. // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
  83. // - Constant) - A` if we are willing to invert all of the uses.
  84. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
  85. if (BO->getOpcode() == Instruction::Add ||
  86. BO->getOpcode() == Instruction::Sub)
  87. if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
  88. return WillInvertAllUses;
  89. return false;
  90. }
  91. /// \brief Specific patterns of overflow check idioms that we match.
  92. enum OverflowCheckFlavor {
  93. OCF_UNSIGNED_ADD,
  94. OCF_SIGNED_ADD,
  95. OCF_UNSIGNED_SUB,
  96. OCF_SIGNED_SUB,
  97. OCF_UNSIGNED_MUL,
  98. OCF_SIGNED_MUL,
  99. OCF_INVALID
  100. };
  101. /// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
  102. /// intrinsic.
  103. static inline OverflowCheckFlavor
  104. IntrinsicIDToOverflowCheckFlavor(unsigned ID) {
  105. switch (ID) {
  106. default:
  107. return OCF_INVALID;
  108. case Intrinsic::uadd_with_overflow:
  109. return OCF_UNSIGNED_ADD;
  110. case Intrinsic::sadd_with_overflow:
  111. return OCF_SIGNED_ADD;
  112. case Intrinsic::usub_with_overflow:
  113. return OCF_UNSIGNED_SUB;
  114. case Intrinsic::ssub_with_overflow:
  115. return OCF_SIGNED_SUB;
  116. case Intrinsic::umul_with_overflow:
  117. return OCF_UNSIGNED_MUL;
  118. case Intrinsic::smul_with_overflow:
  119. return OCF_SIGNED_MUL;
  120. }
  121. }
  122. /// \brief An IRBuilder inserter that adds new instructions to the instcombine
  123. /// worklist.
  124. class LLVM_LIBRARY_VISIBILITY InstCombineIRInserter
  125. : public IRBuilderDefaultInserter<true> {
  126. InstCombineWorklist &Worklist;
  127. AssumptionCache *AC;
  128. public:
  129. InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
  130. : Worklist(WL), AC(AC) {}
  131. void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB,
  132. BasicBlock::iterator InsertPt) const {
  133. IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
  134. Worklist.Add(I);
  135. using namespace llvm::PatternMatch;
  136. if (match(I, m_Intrinsic<Intrinsic::assume>()))
  137. AC->registerAssumption(cast<CallInst>(I));
  138. }
  139. };
  140. /// \brief The core instruction combiner logic.
  141. ///
  142. /// This class provides both the logic to recursively visit instructions and
  143. /// combine them, as well as the pass infrastructure for running this as part
  144. /// of the LLVM pass pipeline.
  145. class LLVM_LIBRARY_VISIBILITY InstCombiner
  146. : public InstVisitor<InstCombiner, Instruction *> {
  147. // FIXME: These members shouldn't be public.
  148. public:
  149. /// \brief A worklist of the instructions that need to be simplified.
  150. InstCombineWorklist &Worklist;
  151. /// \brief An IRBuilder that automatically inserts new instructions into the
  152. /// worklist.
  153. typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy;
  154. BuilderTy *Builder;
  155. private:
  156. // Mode in which we are running the combiner.
  157. const bool MinimizeSize;
  158. AliasAnalysis *AA;
  159. // Required analyses.
  160. // FIXME: These can never be null and should be references.
  161. AssumptionCache *AC;
  162. TargetLibraryInfo *TLI;
  163. DominatorTree *DT;
  164. const DataLayout &DL;
  165. // Optional analyses. When non-null, these can both be used to do better
  166. // combining and will be updated to reflect any changes.
  167. LoopInfo *LI;
  168. bool MadeIRChange;
  169. public:
  170. InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder,
  171. bool MinimizeSize, AliasAnalysis *AA,
  172. AssumptionCache *AC, TargetLibraryInfo *TLI,
  173. DominatorTree *DT, const DataLayout &DL, LoopInfo *LI)
  174. : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
  175. AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {}
  176. /// \brief Run the combiner over the entire worklist until it is empty.
  177. ///
  178. /// \returns true if the IR is changed.
  179. bool run();
  180. AssumptionCache *getAssumptionCache() const { return AC; }
  181. const DataLayout &getDataLayout() const { return DL; }
  182. DominatorTree *getDominatorTree() const { return DT; }
  183. LoopInfo *getLoopInfo() const { return LI; }
  184. TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
  185. // Visitation implementation - Implement instruction combining for different
  186. // instruction types. The semantics are as follows:
  187. // Return Value:
  188. // null - No change was made
  189. // I - Change was made, I is still valid, I may be dead though
  190. // otherwise - Change was made, replace I with returned instruction
  191. //
  192. Instruction *visitAdd(BinaryOperator &I);
  193. Instruction *visitFAdd(BinaryOperator &I);
  194. Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
  195. Instruction *visitSub(BinaryOperator &I);
  196. Instruction *visitFSub(BinaryOperator &I);
  197. Instruction *visitMul(BinaryOperator &I);
  198. Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
  199. Instruction *InsertBefore);
  200. Instruction *visitFMul(BinaryOperator &I);
  201. Instruction *visitURem(BinaryOperator &I);
  202. Instruction *visitSRem(BinaryOperator &I);
  203. Instruction *visitFRem(BinaryOperator &I);
  204. bool SimplifyDivRemOfSelect(BinaryOperator &I);
  205. Instruction *commonRemTransforms(BinaryOperator &I);
  206. Instruction *commonIRemTransforms(BinaryOperator &I);
  207. Instruction *commonDivTransforms(BinaryOperator &I);
  208. Instruction *commonIDivTransforms(BinaryOperator &I);
  209. Instruction *visitUDiv(BinaryOperator &I);
  210. Instruction *visitSDiv(BinaryOperator &I);
  211. Instruction *visitFDiv(BinaryOperator &I);
  212. Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
  213. Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
  214. Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
  215. Instruction *visitAnd(BinaryOperator &I);
  216. Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
  217. Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
  218. Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
  219. Value *B, Value *C);
  220. Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
  221. Value *B, Value *C);
  222. Instruction *visitOr(BinaryOperator &I);
  223. Instruction *visitXor(BinaryOperator &I);
  224. Instruction *visitShl(BinaryOperator &I);
  225. Instruction *visitAShr(BinaryOperator &I);
  226. Instruction *visitLShr(BinaryOperator &I);
  227. Instruction *commonShiftTransforms(BinaryOperator &I);
  228. Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
  229. Constant *RHSC);
  230. Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
  231. GlobalVariable *GV, CmpInst &ICI,
  232. ConstantInt *AndCst = nullptr);
  233. Instruction *visitFCmpInst(FCmpInst &I);
  234. Instruction *visitICmpInst(ICmpInst &I);
  235. Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
  236. Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
  237. ConstantInt *RHS);
  238. Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
  239. ConstantInt *DivRHS);
  240. Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
  241. ConstantInt *DivRHS);
  242. Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
  243. ConstantInt *CI1, ConstantInt *CI2);
  244. Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
  245. ConstantInt *CI1, ConstantInt *CI2);
  246. Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
  247. ICmpInst::Predicate Pred);
  248. Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
  249. ICmpInst::Predicate Cond, Instruction &I);
  250. Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
  251. BinaryOperator &I);
  252. Instruction *commonCastTransforms(CastInst &CI);
  253. Instruction *commonPointerCastTransforms(CastInst &CI);
  254. Instruction *visitTrunc(TruncInst &CI);
  255. Instruction *visitZExt(ZExtInst &CI);
  256. Instruction *visitSExt(SExtInst &CI);
  257. Instruction *visitFPTrunc(FPTruncInst &CI);
  258. Instruction *visitFPExt(CastInst &CI);
  259. Instruction *visitFPToUI(FPToUIInst &FI);
  260. Instruction *visitFPToSI(FPToSIInst &FI);
  261. Instruction *visitUIToFP(CastInst &CI);
  262. Instruction *visitSIToFP(CastInst &CI);
  263. Instruction *visitPtrToInt(PtrToIntInst &CI);
  264. Instruction *visitIntToPtr(IntToPtrInst &CI);
  265. Instruction *visitBitCast(BitCastInst &CI);
  266. Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
  267. Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
  268. Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
  269. Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
  270. Value *A, Value *B, Instruction &Outer,
  271. SelectPatternFlavor SPF2, Value *C);
  272. Instruction *FoldItoFPtoI(Instruction &FI);
  273. Instruction *visitSelectInst(SelectInst &SI);
  274. Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
  275. Instruction *visitCallInst(CallInst &CI);
  276. Instruction *visitInvokeInst(InvokeInst &II);
  277. Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
  278. Instruction *visitPHINode(PHINode &PN);
  279. Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
  280. Instruction *visitAllocaInst(AllocaInst &AI);
  281. Instruction *visitAllocSite(Instruction &FI);
  282. Instruction *visitFree(CallInst &FI);
  283. Instruction *visitLoadInst(LoadInst &LI);
  284. Instruction *visitStoreInst(StoreInst &SI);
  285. Instruction *visitBranchInst(BranchInst &BI);
  286. Instruction *visitSwitchInst(SwitchInst &SI);
  287. Instruction *visitReturnInst(ReturnInst &RI);
  288. Instruction *visitInsertValueInst(InsertValueInst &IV);
  289. Instruction *visitInsertElementInst(InsertElementInst &IE);
  290. Instruction *visitExtractElementInst(ExtractElementInst &EI);
  291. Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
  292. Instruction *visitExtractValueInst(ExtractValueInst &EV);
  293. Instruction *visitLandingPadInst(LandingPadInst &LI);
  294. // visitInstruction - Specify what to return for unhandled instructions...
  295. Instruction *visitInstruction(Instruction &I) { return nullptr; }
  296. // True when DB dominates all uses of DI execpt UI.
  297. // UI must be in the same block as DI.
  298. // The routine checks that the DI parent and DB are different.
  299. bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
  300. const BasicBlock *DB) const;
  301. // Replace select with select operand SIOpd in SI-ICmp sequence when possible
  302. bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
  303. const unsigned SIOpd);
  304. private:
  305. bool ShouldChangeType(Type *From, Type *To) const;
  306. Value *dyn_castNegVal(Value *V) const;
  307. Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
  308. Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
  309. SmallVectorImpl<Value *> &NewIndices);
  310. Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
  311. /// \brief Classify whether a cast is worth optimizing.
  312. ///
  313. /// Returns true if the cast from "V to Ty" actually results in any code
  314. /// being generated and is interesting to optimize out. If the cast can be
  315. /// eliminated by some other simple transformation, we prefer to do the
  316. /// simplification first.
  317. bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
  318. Type *Ty);
  319. /// \brief Try to optimize a sequence of instructions checking if an operation
  320. /// on LHS and RHS overflows.
  321. ///
  322. /// If a simplification is possible, stores the simplified result of the
  323. /// operation in OperationResult and result of the overflow check in
  324. /// OverflowResult, and return true. If no simplification is possible,
  325. /// returns false.
  326. bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
  327. Instruction &CtxI, Value *&OperationResult,
  328. Constant *&OverflowResult);
  329. Instruction *visitCallSite(CallSite CS);
  330. Instruction *tryOptimizeCall(CallInst *CI);
  331. bool transformConstExprCastCall(CallSite CS);
  332. Instruction *transformCallThroughTrampoline(CallSite CS,
  333. IntrinsicInst *Tramp);
  334. Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
  335. bool DoXform = true);
  336. Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
  337. bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI);
  338. bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
  339. bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
  340. bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI);
  341. Value *EmitGEPOffset(User *GEP);
  342. Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
  343. Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
  344. public:
  345. /// \brief Inserts an instruction \p New before instruction \p Old
  346. ///
  347. /// Also adds the new instruction to the worklist and returns \p New so that
  348. /// it is suitable for use as the return from the visitation patterns.
  349. Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
  350. assert(New && !New->getParent() &&
  351. "New instruction already inserted into a basic block!");
  352. BasicBlock *BB = Old.getParent();
  353. BB->getInstList().insert(&Old, New); // Insert inst
  354. Worklist.Add(New);
  355. return New;
  356. }
  357. /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
  358. Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
  359. New->setDebugLoc(Old.getDebugLoc());
  360. return InsertNewInstBefore(New, Old);
  361. }
  362. /// \brief A combiner-aware RAUW-like routine.
  363. ///
  364. /// This method is to be used when an instruction is found to be dead,
  365. /// replacable with another preexisting expression. Here we add all uses of
  366. /// I to the worklist, replace all uses of I with the new value, then return
  367. /// I, so that the inst combiner will know that I was modified.
  368. Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
  369. // If there are no uses to replace, then we return nullptr to indicate that
  370. // no changes were made to the program.
  371. if (I.use_empty()) return nullptr;
  372. Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
  373. // If we are replacing the instruction with itself, this must be in a
  374. // segment of unreachable code, so just clobber the instruction.
  375. if (&I == V)
  376. V = UndefValue::get(I.getType());
  377. DEBUG(dbgs() << "IC: Replacing " << I << "\n"
  378. << " with " << *V << '\n');
  379. I.replaceAllUsesWith(V);
  380. return &I;
  381. }
  382. /// Creates a result tuple for an overflow intrinsic \p II with a given
  383. /// \p Result and a constant \p Overflow value.
  384. Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
  385. Constant *Overflow) {
  386. Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
  387. StructType *ST = cast<StructType>(II->getType());
  388. Constant *Struct = ConstantStruct::get(ST, V);
  389. return InsertValueInst::Create(Struct, Result, 0);
  390. }
  391. /// \brief Combiner aware instruction erasure.
  392. ///
  393. /// When dealing with an instruction that has side effects or produces a void
  394. /// value, we can't rely on DCE to delete the instruction. Instead, visit
  395. /// methods should return the value returned by this function.
  396. Instruction *EraseInstFromFunction(Instruction &I) {
  397. DEBUG(dbgs() << "IC: ERASE " << I << '\n');
  398. assert(I.use_empty() && "Cannot erase instruction that is used!");
  399. // Make sure that we reprocess all operands now that we reduced their
  400. // use counts.
  401. if (I.getNumOperands() < 8) {
  402. for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
  403. if (Instruction *Op = dyn_cast<Instruction>(*i))
  404. Worklist.Add(Op);
  405. }
  406. Worklist.Remove(&I);
  407. I.eraseFromParent();
  408. MadeIRChange = true;
  409. return nullptr; // Don't do anything with FI
  410. }
  411. void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
  412. unsigned Depth, Instruction *CxtI) const {
  413. return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
  414. DT);
  415. }
  416. bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
  417. Instruction *CxtI = nullptr) const {
  418. return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
  419. }
  420. unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
  421. Instruction *CxtI = nullptr) const {
  422. return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
  423. }
  424. void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
  425. unsigned Depth = 0, Instruction *CxtI = nullptr) const {
  426. return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
  427. DT);
  428. }
  429. OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS,
  430. const Instruction *CxtI) {
  431. return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
  432. }
  433. OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS,
  434. const Instruction *CxtI) {
  435. return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
  436. }
  437. private:
  438. /// \brief Performs a few simplifications for operators which are associative
  439. /// or commutative.
  440. bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
  441. /// \brief Tries to simplify binary operations which some other binary
  442. /// operation distributes over.
  443. ///
  444. /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
  445. /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
  446. /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
  447. /// value, or null if it didn't simplify.
  448. Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
  449. /// \brief Attempts to replace V with a simpler value based on the demanded
  450. /// bits.
  451. Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
  452. APInt &KnownOne, unsigned Depth,
  453. Instruction *CxtI);
  454. bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
  455. APInt &KnownOne, unsigned Depth = 0);
  456. /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
  457. /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
  458. Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
  459. APInt DemandedMask, APInt &KnownZero,
  460. APInt &KnownOne);
  461. /// \brief Tries to simplify operands to an integer instruction based on its
  462. /// demanded bits.
  463. bool SimplifyDemandedInstructionBits(Instruction &Inst);
  464. Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
  465. APInt &UndefElts, unsigned Depth = 0);
  466. Value *SimplifyVectorOp(BinaryOperator &Inst);
  467. Value *SimplifyBSwap(BinaryOperator &Inst);
  468. // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
  469. // which has a PHI node as operand #0, see if we can fold the instruction
  470. // into the PHI (which is only possible if all operands to the PHI are
  471. // constants).
  472. //
  473. Instruction *FoldOpIntoPhi(Instruction &I);
  474. /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
  475. /// its operands.
  476. Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
  477. Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
  478. Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
  479. Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
  480. Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
  481. ConstantInt *AndRHS, BinaryOperator &TheAnd);
  482. Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
  483. bool isSub, Instruction &I);
  484. Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
  485. bool Inside);
  486. Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
  487. Instruction *MatchBSwap(BinaryOperator &I);
  488. bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
  489. Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
  490. Instruction *SimplifyMemSet(MemSetInst *MI);
  491. Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
  492. /// \brief Returns a value X such that Val = X * Scale, or null if none.
  493. ///
  494. /// If the multiplication is known not to overflow then NoSignedWrap is set.
  495. Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
  496. };
  497. } // end namespace llvm.
  498. #undef DEBUG_TYPE
  499. #endif