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