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 The core instruction combiner logic. |
| 139 | /// |
| 140 | /// This class provides both the logic to recursively visit instructions and |
| 141 | /// combine them, as well as the pass infrastructure for running this as part |
| 142 | /// of the LLVM pass pipeline. |
Duncan Sands | 6c5e435 | 2010-05-11 20:16:09 +0000 | [diff] [blame] | 143 | class LLVM_LIBRARY_VISIBILITY InstCombiner |
Chandler Carruth | 1edb9d6 | 2015-01-20 22:44:35 +0000 | [diff] [blame] | 144 | : public InstVisitor<InstCombiner, Instruction *> { |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 145 | // FIXME: These members shouldn't be public. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 146 | public: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 147 | /// \brief A worklist of the instructions that need to be simplified. |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 148 | InstCombineWorklist &Worklist; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 149 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 150 | /// \brief An IRBuilder that automatically inserts new instructions into the |
| 151 | /// worklist. |
Justin Bogner | 19dd0da | 2016-08-04 23:41:01 +0000 | [diff] [blame] | 152 | typedef IRBuilder<TargetFolder, IRBuilderCallbackInserter> BuilderTy; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 153 | BuilderTy *Builder; |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 154 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 155 | private: |
| 156 | // Mode in which we are running the combiner. |
| 157 | const bool MinimizeSize; |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 158 | /// Enable combines that trigger rarely but are costly in compiletime. |
| 159 | const bool ExpensiveCombines; |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 160 | |
Bjorn Steinbrink | 8350534 | 2015-07-10 06:55:49 +0000 | [diff] [blame] | 161 | AliasAnalysis *AA; |
| 162 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 163 | // Required analyses. |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 164 | AssumptionCache &AC; |
| 165 | TargetLibraryInfo &TLI; |
| 166 | DominatorTree &DT; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 167 | const DataLayout &DL; |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 168 | |
| 169 | // Optional analyses. When non-null, these can both be used to do better |
| 170 | // combining and will be updated to reflect any changes. |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 171 | LoopInfo *LI; |
| 172 | |
| 173 | bool MadeIRChange; |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 174 | |
| 175 | public: |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 176 | InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder, |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 177 | bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA, |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 178 | AssumptionCache &AC, TargetLibraryInfo &TLI, |
| 179 | DominatorTree &DT, const DataLayout &DL, LoopInfo *LI) |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 180 | : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize), |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 181 | ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT), |
| 182 | DL(DL), LI(LI), MadeIRChange(false) {} |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 183 | |
Chandler Carruth | df5747a | 2015-01-21 11:38:17 +0000 | [diff] [blame] | 184 | /// \brief Run the combiner over the entire worklist until it is empty. |
| 185 | /// |
| 186 | /// \returns true if the IR is changed. |
| 187 | bool run(); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 188 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 189 | AssumptionCache &getAssumptionCache() const { return AC; } |
Hal Finkel | 74c2f35 | 2014-09-07 12:44:26 +0000 | [diff] [blame] | 190 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 191 | const DataLayout &getDataLayout() const { return DL; } |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 192 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 193 | DominatorTree &getDominatorTree() const { return DT; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 194 | |
Chandler Carruth | 5175b9a | 2015-01-20 08:35:24 +0000 | [diff] [blame] | 195 | LoopInfo *getLoopInfo() const { return LI; } |
| 196 | |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 197 | TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; } |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 199 | // Visitation implementation - Implement instruction combining for different |
| 200 | // instruction types. The semantics are as follows: |
| 201 | // Return Value: |
| 202 | // null - No change was made |
| 203 | // I - Change was made, I is still valid, I may be dead though |
| 204 | // otherwise - Change was made, replace I with returned instruction |
| 205 | // |
| 206 | Instruction *visitAdd(BinaryOperator &I); |
| 207 | Instruction *visitFAdd(BinaryOperator &I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 208 | Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 209 | Instruction *visitSub(BinaryOperator &I); |
| 210 | Instruction *visitFSub(BinaryOperator &I); |
| 211 | Instruction *visitMul(BinaryOperator &I); |
Benjamin Kramer | 76b15d0 | 2014-01-19 13:36:27 +0000 | [diff] [blame] | 212 | Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C, |
Shuxin Yang | df0e61e | 2013-01-07 21:39:23 +0000 | [diff] [blame] | 213 | Instruction *InsertBefore); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 214 | Instruction *visitFMul(BinaryOperator &I); |
| 215 | Instruction *visitURem(BinaryOperator &I); |
| 216 | Instruction *visitSRem(BinaryOperator &I); |
| 217 | Instruction *visitFRem(BinaryOperator &I); |
| 218 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
| 219 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 220 | Instruction *commonIRemTransforms(BinaryOperator &I); |
| 221 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 222 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 223 | Instruction *visitUDiv(BinaryOperator &I); |
| 224 | Instruction *visitSDiv(BinaryOperator &I); |
Frits van Bommel | 2a55951 | 2011-01-29 17:50:27 +0000 | [diff] [blame] | 225 | Instruction *visitFDiv(BinaryOperator &I); |
Erik Eckstein | d181752 | 2014-12-03 10:39:15 +0000 | [diff] [blame] | 226 | Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 227 | Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS); |
| 228 | Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 229 | Instruction *visitAnd(BinaryOperator &I); |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 230 | Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI); |
Chris Lattner | 067459c | 2010-03-05 08:46:26 +0000 | [diff] [blame] | 231 | Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 232 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A, |
| 233 | Value *B, Value *C); |
David Majnemer | 5d1aeba | 2014-08-21 05:14:48 +0000 | [diff] [blame] | 234 | Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A, |
| 235 | Value *B, Value *C); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 236 | Instruction *visitOr(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 237 | Instruction *visitXor(BinaryOperator &I); |
| 238 | Instruction *visitShl(BinaryOperator &I); |
| 239 | Instruction *visitAShr(BinaryOperator &I); |
| 240 | Instruction *visitLShr(BinaryOperator &I); |
| 241 | Instruction *commonShiftTransforms(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 242 | Instruction *visitFCmpInst(FCmpInst &I); |
| 243 | Instruction *visitICmpInst(ICmpInst &I); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 244 | Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1, |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 245 | BinaryOperator &I); |
| 246 | Instruction *commonCastTransforms(CastInst &CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 247 | Instruction *commonPointerCastTransforms(CastInst &CI); |
| 248 | Instruction *visitTrunc(TruncInst &CI); |
| 249 | Instruction *visitZExt(ZExtInst &CI); |
| 250 | Instruction *visitSExt(SExtInst &CI); |
| 251 | Instruction *visitFPTrunc(FPTruncInst &CI); |
| 252 | Instruction *visitFPExt(CastInst &CI); |
| 253 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 254 | Instruction *visitFPToSI(FPToSIInst &FI); |
| 255 | Instruction *visitUIToFP(CastInst &CI); |
| 256 | Instruction *visitSIToFP(CastInst &CI); |
| 257 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
| 258 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
| 259 | Instruction *visitBitCast(BitCastInst &CI); |
Matt Arsenault | a9e95ab | 2013-11-15 05:45:08 +0000 | [diff] [blame] | 260 | Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI); |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 261 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI); |
| 262 | Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 263 | Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, |
| 264 | Value *A, Value *B, Instruction &Outer, |
| 265 | SelectPatternFlavor SPF2, Value *C); |
Mehdi Amini | b9a0fa4 | 2015-02-16 21:47:54 +0000 | [diff] [blame] | 266 | Instruction *FoldItoFPtoI(Instruction &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 267 | Instruction *visitSelectInst(SelectInst &SI); |
| 268 | Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
| 269 | Instruction *visitCallInst(CallInst &CI); |
| 270 | Instruction *visitInvokeInst(InvokeInst &II); |
| 271 | |
| 272 | Instruction *SliceUpIllegalIntegerPHI(PHINode &PN); |
| 273 | Instruction *visitPHINode(PHINode &PN); |
| 274 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 275 | Instruction *visitAllocaInst(AllocaInst &AI); |
Nuno Lopes | 95cc4f3 | 2012-07-09 18:38:20 +0000 | [diff] [blame] | 276 | Instruction *visitAllocSite(Instruction &FI); |
Gabor Greif | 75f6943 | 2010-06-24 12:21:15 +0000 | [diff] [blame] | 277 | Instruction *visitFree(CallInst &FI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 278 | Instruction *visitLoadInst(LoadInst &LI); |
| 279 | Instruction *visitStoreInst(StoreInst &SI); |
| 280 | Instruction *visitBranchInst(BranchInst &BI); |
| 281 | Instruction *visitSwitchInst(SwitchInst &SI); |
Hal Finkel | 93873cc1 | 2014-09-07 21:28:34 +0000 | [diff] [blame] | 282 | Instruction *visitReturnInst(ReturnInst &RI); |
Michael Zolotukhin | 7d6293a | 2014-05-07 14:30:18 +0000 | [diff] [blame] | 283 | Instruction *visitInsertValueInst(InsertValueInst &IV); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 284 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
| 285 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
| 286 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
| 287 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
Duncan Sands | 5c05579 | 2011-09-30 13:12:16 +0000 | [diff] [blame] | 288 | Instruction *visitLandingPadInst(LandingPadInst &LI); |
Arnaud A. de Grandmaison | 333ef38 | 2016-05-10 09:24:49 +0000 | [diff] [blame] | 289 | Instruction *visitVAStartInst(VAStartInst &I); |
| 290 | Instruction *visitVACopyInst(VACopyInst &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 291 | |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 292 | /// Specify what to return for unhandled instructions. |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 293 | Instruction *visitInstruction(Instruction &I) { return nullptr; } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 294 | |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 295 | /// True when DB dominates all uses of DI except UI. |
| 296 | /// UI must be in the same block as DI. |
| 297 | /// The routine checks that the DI parent and DB are different. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 298 | bool dominatesAllUses(const Instruction *DI, const Instruction *UI, |
| 299 | const BasicBlock *DB) const; |
| 300 | |
Sanjay Patel | 5352331 | 2016-09-12 14:25:46 +0000 | [diff] [blame] | 301 | /// Try to replace select with select operand SIOpd in SI-ICmp sequence. |
Gerolf Hoflehner | ec6217c | 2014-11-21 23:36:44 +0000 | [diff] [blame] | 302 | bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp, |
| 303 | const unsigned SIOpd); |
| 304 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 305 | private: |
Sanjay Patel | 55dcd40 | 2015-09-21 16:09:37 +0000 | [diff] [blame] | 306 | bool ShouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 307 | bool ShouldChangeType(Type *From, Type *To) const; |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 308 | Value *dyn_castNegVal(Value *V) const; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 309 | Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const; |
David Blaikie | 87ca1b6 | 2015-03-27 20:56:11 +0000 | [diff] [blame] | 310 | Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 311 | SmallVectorImpl<Value *> &NewIndices); |
Chris Lattner | 2b295a0 | 2010-01-04 07:53:58 +0000 | [diff] [blame] | 312 | Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 313 | |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 314 | /// Classify whether a cast is worth optimizing. |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 315 | /// |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 316 | /// This is a helper to decide whether the simplification of |
| 317 | /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed. |
| 318 | /// |
| 319 | /// \param CI The cast we are interested in. |
| 320 | /// |
| 321 | /// \return true if this cast actually results in any code being generated and |
| 322 | /// if it cannot already be eliminated by some other transformation. |
| 323 | bool shouldOptimizeCast(CastInst *CI); |
Chris Lattner | 2188e40 | 2010-01-04 07:37:31 +0000 | [diff] [blame] | 324 | |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 325 | /// \brief Try to optimize a sequence of instructions checking if an operation |
| 326 | /// on LHS and RHS overflows. |
| 327 | /// |
Sanjoy Das | 6f5dca7 | 2015-08-28 19:09:31 +0000 | [diff] [blame] | 328 | /// If this overflow check is done via one of the overflow check intrinsics, |
| 329 | /// then CtxI has to be the call instruction calling that intrinsic. If this |
| 330 | /// overflow check is done by arithmetic followed by a compare, then CtxI has |
| 331 | /// to be the arithmetic instruction. |
| 332 | /// |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 333 | /// If a simplification is possible, stores the simplified result of the |
| 334 | /// operation in OperationResult and result of the overflow check in |
| 335 | /// OverflowResult, and return true. If no simplification is possible, |
| 336 | /// returns false. |
| 337 | bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS, |
| 338 | Instruction &CtxI, Value *&OperationResult, |
| 339 | Constant *&OverflowResult); |
| 340 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 341 | Instruction *visitCallSite(CallSite CS); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 342 | Instruction *tryOptimizeCall(CallInst *CI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 343 | bool transformConstExprCastCall(CallSite CS); |
Duncan Sands | a098436 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 344 | Instruction *transformCallThroughTrampoline(CallSite CS, |
| 345 | IntrinsicInst *Tramp); |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 346 | |
| 347 | /// Transform (zext icmp) to bitwise / integer operations in order to |
| 348 | /// eliminate it. |
| 349 | /// |
| 350 | /// \param ICI The icmp of the (zext icmp) pair we are interested in. |
| 351 | /// \parem CI The zext of the (zext icmp) pair we are interested in. |
| 352 | /// \param DoTransform Pass false to just test whether the given (zext icmp) |
| 353 | /// would be transformed. Pass true to actually perform the transformation. |
| 354 | /// |
| 355 | /// \return null if the transformation cannot be performed. If the |
| 356 | /// transformation can be performed the new instruction that replaces the |
| 357 | /// (zext icmp) pair will be returned (if \p DoTransform is false the |
| 358 | /// unmodified \p ICI will be returned in this case). |
| 359 | Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI, |
| 360 | bool DoTransform = true); |
| 361 | |
Benjamin Kramer | 398b8c5 | 2011-04-01 20:09:03 +0000 | [diff] [blame] | 362 | Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 363 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI); |
| 364 | bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI); |
| 365 | bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI); |
| 366 | bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI); |
Nuno Lopes | a2f6cec | 2012-05-22 17:19:09 +0000 | [diff] [blame] | 367 | Value *EmitGEPOffset(User *GEP); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 368 | Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 369 | Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask); |
Sanjay Patel | 40e7ba0 | 2016-02-23 16:36:07 +0000 | [diff] [blame] | 370 | Instruction *foldCastedBitwiseLogic(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 371 | |
Tobias Grosser | 8ef834c | 2016-07-19 09:06:08 +0000 | [diff] [blame] | 372 | /// Determine if a pair of casts can be replaced by a single cast. |
| 373 | /// |
| 374 | /// \param CI1 The first of a pair of casts. |
| 375 | /// \param CI2 The second of a pair of casts. |
| 376 | /// |
| 377 | /// \return 0 if the cast pair cannot be eliminated, otherwise returns an |
| 378 | /// Instruction::CastOps value for a cast that can replace the pair, casting |
| 379 | /// CI1->getSrcTy() to CI2->getDstTy(). |
| 380 | /// |
| 381 | /// \see CastInst::isEliminableCastPair |
| 382 | Instruction::CastOps isEliminableCastPair(const CastInst *CI1, |
| 383 | const CastInst *CI2); |
| 384 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 385 | public: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 386 | /// \brief Inserts an instruction \p New before instruction \p Old |
| 387 | /// |
| 388 | /// Also adds the new instruction to the worklist and returns \p New so that |
| 389 | /// it is suitable for use as the return from the visitation patterns. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 390 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 391 | assert(New && !New->getParent() && |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 392 | "New instruction already inserted into a basic block!"); |
| 393 | BasicBlock *BB = Old.getParent(); |
Duncan P. N. Exon Smith | 9f8aaf2 | 2015-10-13 16:59:33 +0000 | [diff] [blame] | 394 | BB->getInstList().insert(Old.getIterator(), New); // Insert inst |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 395 | Worklist.Add(New); |
| 396 | return New; |
| 397 | } |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 398 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 399 | /// \brief Same as InsertNewInstBefore, but also sets the debug loc. |
Eli Friedman | 6efb64e | 2011-05-19 01:20:42 +0000 | [diff] [blame] | 400 | Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) { |
| 401 | New->setDebugLoc(Old.getDebugLoc()); |
| 402 | return InsertNewInstBefore(New, Old); |
| 403 | } |
| 404 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 405 | /// \brief A combiner-aware RAUW-like routine. |
| 406 | /// |
| 407 | /// This method is to be used when an instruction is found to be dead, |
Sanjay Patel | f2ea8a2 | 2016-01-06 00:23:12 +0000 | [diff] [blame] | 408 | /// replaceable with another preexisting expression. Here we add all uses of |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 409 | /// I to the worklist, replace all uses of I with the new value, then return |
| 410 | /// I, so that the inst combiner will know that I was modified. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 411 | Instruction *replaceInstUsesWith(Instruction &I, Value *V) { |
Owen Anderson | 51b75b8c | 2015-03-10 05:13:47 +0000 | [diff] [blame] | 412 | // If there are no uses to replace, then we return nullptr to indicate that |
| 413 | // no changes were made to the program. |
| 414 | if (I.use_empty()) return nullptr; |
| 415 | |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 416 | Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 418 | // If we are replacing the instruction with itself, this must be in a |
| 419 | // segment of unreachable code, so just clobber the instruction. |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 420 | if (&I == V) |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 421 | V = UndefValue::get(I.getType()); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 422 | |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 423 | DEBUG(dbgs() << "IC: Replacing " << I << "\n" |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 424 | << " with " << *V << '\n'); |
Frits van Bommel | d14d991 | 2011-03-27 23:32:31 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 426 | I.replaceAllUsesWith(V); |
| 427 | return &I; |
| 428 | } |
| 429 | |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 430 | /// 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] | 431 | /// \p Result and a constant \p Overflow value. |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 432 | Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result, |
Sanjoy Das | b098447 | 2015-04-08 04:27:22 +0000 | [diff] [blame] | 433 | Constant *Overflow) { |
| 434 | Constant *V[] = {UndefValue::get(Result->getType()), Overflow}; |
Erik Eckstein | 096ff7d | 2014-12-11 08:02:30 +0000 | [diff] [blame] | 435 | StructType *ST = cast<StructType>(II->getType()); |
| 436 | Constant *Struct = ConstantStruct::get(ST, V); |
| 437 | return InsertValueInst::Create(Struct, Result, 0); |
| 438 | } |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 439 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 440 | /// \brief Combiner aware instruction erasure. |
| 441 | /// |
| 442 | /// When dealing with an instruction that has side effects or produces a void |
| 443 | /// value, we can't rely on DCE to delete the instruction. Instead, visit |
| 444 | /// methods should return the value returned by this function. |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 445 | Instruction *eraseInstFromFunction(Instruction &I) { |
Matt Arsenault | e6db760 | 2013-09-05 19:48:28 +0000 | [diff] [blame] | 446 | DEBUG(dbgs() << "IC: ERASE " << I << '\n'); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 447 | |
| 448 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 449 | // Make sure that we reprocess all operands now that we reduced their |
| 450 | // use counts. |
| 451 | if (I.getNumOperands() < 8) { |
Sanjay Patel | 8af7fbc3 | 2016-01-31 16:34:48 +0000 | [diff] [blame] | 452 | for (Use &Operand : I.operands()) |
| 453 | if (auto *Inst = dyn_cast<Instruction>(Operand)) |
| 454 | Worklist.Add(Inst); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 455 | } |
| 456 | Worklist.Remove(&I); |
| 457 | I.eraseFromParent(); |
| 458 | MadeIRChange = true; |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 459 | return nullptr; // Don't do anything with FI |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 460 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 461 | |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 462 | void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 463 | unsigned Depth, Instruction *CxtI) const { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 464 | return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI, |
| 465 | &DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 466 | } |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 467 | |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 468 | bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 469 | Instruction *CxtI = nullptr) const { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 470 | return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 471 | } |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 472 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0, |
| 473 | Instruction *CxtI = nullptr) const { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 474 | return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 475 | } |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 476 | void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, |
| 477 | unsigned Depth = 0, Instruction *CxtI = nullptr) const { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 478 | return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, &AC, CxtI, |
| 479 | &DT); |
David Majnemer | 54c2ca2 | 2014-12-26 09:10:14 +0000 | [diff] [blame] | 480 | } |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 481 | OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, |
| 482 | const Instruction *CxtI) { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 483 | return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT); |
David Majnemer | 491331a | 2015-01-02 07:29:43 +0000 | [diff] [blame] | 484 | } |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 485 | OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, |
| 486 | const Instruction *CxtI) { |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 487 | return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT); |
David Majnemer | 5310c1e | 2015-01-07 00:39:50 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 489 | |
| 490 | private: |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 491 | /// \brief Performs a few simplifications for operators which are associative |
| 492 | /// or commutative. |
Duncan Sands | 641baf1 | 2010-11-13 15:10:37 +0000 | [diff] [blame] | 493 | bool SimplifyAssociativeOrCommutative(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 494 | |
Chandler Carruth | b3d03df | 2015-01-20 21:10:35 +0000 | [diff] [blame] | 495 | /// \brief Tries to simplify binary operations which some other binary |
| 496 | /// operation distributes over. |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 497 | /// |
| 498 | /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)" |
| 499 | /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A |
| 500 | /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified |
| 501 | /// value, or null if it didn't simplify. |
Duncan Sands | fbb9ac3 | 2010-12-22 13:36:08 +0000 | [diff] [blame] | 502 | Value *SimplifyUsingDistributiveLaws(BinaryOperator &I); |
Duncan Sands | adc7771f | 2010-11-23 14:23:47 +0000 | [diff] [blame] | 503 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 504 | /// \brief Attempts to replace V with a simpler value based on the demanded |
| 505 | /// bits. |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 506 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 507 | APInt &KnownOne, unsigned Depth, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 508 | Instruction *CxtI); |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 509 | bool SimplifyDemandedBits(Use &U, const APInt &DemandedMask, APInt &KnownZero, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 510 | APInt &KnownOne, unsigned Depth = 0); |
Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 511 | /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded |
| 512 | /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence. |
| 513 | Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl, |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 514 | const APInt &DemandedMask, APInt &KnownZero, |
Shuxin Yang | 63e999e | 2012-12-04 00:04:54 +0000 | [diff] [blame] | 515 | APInt &KnownOne); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 516 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 517 | /// \brief Tries to simplify operands to an integer instruction based on its |
| 518 | /// demanded bits. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 519 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 521 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
Chandler Carruth | d70cc60 | 2014-05-07 17:36:59 +0000 | [diff] [blame] | 522 | APInt &UndefElts, unsigned Depth = 0); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 523 | |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 524 | Value *SimplifyVectorOp(BinaryOperator &Inst); |
Simon Pilgrim | be24ab3 | 2014-12-04 09:44:01 +0000 | [diff] [blame] | 525 | Value *SimplifyBSwap(BinaryOperator &Inst); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 527 | // FoldOpIntoPhi - Given a binary operator, cast instruction, or select |
| 528 | // which has a PHI node as operand #0, see if we can fold the instruction |
| 529 | // into the PHI (which is only possible if all operands to the PHI are |
| 530 | // constants). |
| 531 | // |
Chris Lattner | ea7131a | 2011-01-16 05:14:26 +0000 | [diff] [blame] | 532 | Instruction *FoldOpIntoPhi(Instruction &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 533 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 534 | /// \brief Try to rotate an operation below a PHI node, using PHI nodes for |
| 535 | /// its operands. |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 536 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 537 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 538 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 539 | Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN); |
Sanjay Patel | 9533407 | 2015-09-27 20:34:31 +0000 | [diff] [blame] | 540 | Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 541 | |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 542 | Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 543 | ICmpInst::Predicate Cond, Instruction &I); |
Pete Cooper | 980a935 | 2016-08-12 17:13:28 +0000 | [diff] [blame] | 544 | Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca, |
| 545 | const Value *Other); |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 546 | Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, |
| 547 | GlobalVariable *GV, CmpInst &ICI, |
| 548 | ConstantInt *AndCst = nullptr); |
| 549 | Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI, |
| 550 | Constant *RHSC); |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 551 | Instruction *foldICmpCstShrConst(ICmpInst &I, Value *Op, Value *A, |
| 552 | ConstantInt *CI1, ConstantInt *CI2); |
| 553 | Instruction *foldICmpCstShlConst(ICmpInst &I, Value *Op, Value *A, |
| 554 | ConstantInt *CI1, ConstantInt *CI2); |
| 555 | Instruction *foldICmpAddOpConst(Instruction &ICI, Value *X, ConstantInt *CI, |
| 556 | ICmpInst::Predicate Pred); |
| 557 | Instruction *foldICmpWithCastAndCast(ICmpInst &ICI); |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 558 | |
Sanjay Patel | 3151dec | 2016-09-12 15:24:31 +0000 | [diff] [blame^] | 559 | Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp); |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 560 | Instruction *foldICmpInstWithConstant(ICmpInst &Cmp); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 561 | |
Sanjay Patel | c9196c4 | 2016-08-22 21:24:29 +0000 | [diff] [blame] | 562 | Instruction *foldICmpTruncConstant(ICmpInst &Cmp, Instruction *Trunc, |
| 563 | const APInt *C); |
| 564 | Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And, |
| 565 | const APInt *C); |
| 566 | Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor, |
| 567 | const APInt *C); |
| 568 | Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or, |
| 569 | const APInt *C); |
| 570 | Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul, |
| 571 | const APInt *C); |
| 572 | Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl, |
| 573 | const APInt *C); |
| 574 | Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr, |
| 575 | const APInt *C); |
| 576 | Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv, |
| 577 | const APInt *C); |
| 578 | Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div, |
| 579 | const APInt *C); |
| 580 | Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub, |
| 581 | const APInt *C); |
| 582 | Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add, |
| 583 | const APInt *C); |
Sanjay Patel | d3c7bb28 | 2016-08-26 16:42:33 +0000 | [diff] [blame] | 584 | Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And, |
Sanjay Patel | 14e0e18 | 2016-08-26 18:28:46 +0000 | [diff] [blame] | 585 | const APInt *C1); |
| 586 | Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And, |
Sanjay Patel | 9b40f98 | 2016-09-07 22:33:03 +0000 | [diff] [blame] | 587 | const APInt *C1, const APInt *C2); |
Sanjay Patel | a3f4f08 | 2016-08-16 17:54:36 +0000 | [diff] [blame] | 588 | |
Sanjay Patel | f58f68c | 2016-09-10 15:03:44 +0000 | [diff] [blame] | 589 | Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp, |
| 590 | BinaryOperator *BO, |
| 591 | const APInt *C); |
| 592 | Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, const APInt *C); |
Sanjay Patel | 4339506 | 2016-07-21 18:07:40 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 594 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 595 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
Jakub Staszak | 190db2f | 2013-01-14 23:16:36 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 597 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
| 598 | bool isSub, Instruction &I); |
Sanjay Patel | 85d7974 | 2016-08-31 19:49:56 +0000 | [diff] [blame] | 599 | Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi, |
| 600 | bool isSigned, bool Inside); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 601 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI); |
Chad Rosier | a00df49 | 2016-05-25 16:22:14 +0000 | [diff] [blame] | 602 | Instruction *MatchBSwap(BinaryOperator &I); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 603 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
Pete Cooper | 67cf9a7 | 2015-11-19 05:56:52 +0000 | [diff] [blame] | 604 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 605 | Instruction *SimplifyMemSet(MemSetInst *MI); |
| 606 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 607 | Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned); |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 608 | |
Chandler Carruth | 3a62216 | 2015-01-20 19:27:58 +0000 | [diff] [blame] | 609 | /// \brief Returns a value X such that Val = X * Scale, or null if none. |
| 610 | /// |
| 611 | /// If the multiplication is known not to overflow then NoSignedWrap is set. |
Duncan Sands | 533c8ae | 2012-10-23 08:28:26 +0000 | [diff] [blame] | 612 | Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap); |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 613 | }; |
| 614 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 615 | } // end namespace llvm. |
| 616 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 617 | #undef DEBUG_TYPE |
| 618 | |
Chris Lattner | 35522b7 | 2010-01-04 07:12:23 +0000 | [diff] [blame] | 619 | #endif |