Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 1 | //===- AggressiveInstCombine.cpp ------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the aggressive expression pattern combiner classes. |
| 10 | // Currently, it handles expression patterns for: |
| 11 | // * Truncate instruction |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" |
| 16 | #include "AggressiveInstCombineInternal.h" |
George Burgess IV | 3482871 | 2018-07-10 22:48:13 +0000 | [diff] [blame] | 17 | #include "llvm-c/Initialization.h" |
Benjamin Kramer | 837f93a | 2018-09-05 11:41:12 +0000 | [diff] [blame] | 18 | #include "llvm-c/Transforms/AggressiveInstCombine.h" |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
| 20 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
| 21 | #include "llvm/Analysis/GlobalsModRef.h" |
| 22 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 23 | #include "llvm/IR/DataLayout.h" |
Amjad Aboud | d895bff | 2018-01-31 10:41:31 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Dominators.h" |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 25 | #include "llvm/IR/IRBuilder.h" |
David Blaikie | ba47dd1 | 2018-04-24 15:40:07 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LegacyPassManager.h" |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 27 | #include "llvm/IR/PatternMatch.h" |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" |
George Burgess IV | 3482871 | 2018-07-10 22:48:13 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 31 | using namespace PatternMatch; |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 32 | |
| 33 | #define DEBUG_TYPE "aggressive-instcombine" |
| 34 | |
| 35 | namespace { |
| 36 | /// Contains expression pattern combiner logic. |
| 37 | /// This class provides both the logic to combine expression patterns and |
| 38 | /// combine them. It differs from InstCombiner class in that each pattern |
| 39 | /// combiner runs only once as opposed to InstCombine's multi-iteration, |
| 40 | /// which allows pattern combiner to have higher complexity than the O(1) |
| 41 | /// required by the instruction combiner. |
| 42 | class AggressiveInstCombinerLegacyPass : public FunctionPass { |
| 43 | public: |
| 44 | static char ID; // Pass identification, replacement for typeid |
| 45 | |
| 46 | AggressiveInstCombinerLegacyPass() : FunctionPass(ID) { |
| 47 | initializeAggressiveInstCombinerLegacyPassPass( |
| 48 | *PassRegistry::getPassRegistry()); |
| 49 | } |
| 50 | |
| 51 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 52 | |
| 53 | /// Run all expression pattern optimizations on the given /p F function. |
| 54 | /// |
| 55 | /// \param F function to optimize. |
| 56 | /// \returns true if the IR is changed. |
| 57 | bool runOnFunction(Function &F) override; |
| 58 | }; |
| 59 | } // namespace |
| 60 | |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 61 | /// Match a pattern for a bitwise rotate operation that partially guards |
| 62 | /// against undefined behavior by branching around the rotation when the shift |
| 63 | /// amount is 0. |
| 64 | static bool foldGuardedRotateToFunnelShift(Instruction &I) { |
| 65 | if (I.getOpcode() != Instruction::PHI || I.getNumOperands() != 2) |
| 66 | return false; |
| 67 | |
| 68 | // As with the one-use checks below, this is not strictly necessary, but we |
| 69 | // are being cautious to avoid potential perf regressions on targets that |
| 70 | // do not actually have a rotate instruction (where the funnel shift would be |
| 71 | // expanded back into math/shift/logic ops). |
| 72 | if (!isPowerOf2_32(I.getType()->getScalarSizeInBits())) |
| 73 | return false; |
| 74 | |
| 75 | // Match V to funnel shift left/right and capture the source operand and |
| 76 | // shift amount in X and Y. |
| 77 | auto matchRotate = [](Value *V, Value *&X, Value *&Y) { |
| 78 | Value *L0, *L1, *R0, *R1; |
| 79 | unsigned Width = V->getType()->getScalarSizeInBits(); |
| 80 | auto Sub = m_Sub(m_SpecificInt(Width), m_Value(R1)); |
| 81 | |
| 82 | // rotate_left(X, Y) == (X << Y) | (X >> (Width - Y)) |
Pawel Bylica | 119aa8f | 2019-01-02 19:51:46 +0000 | [diff] [blame] | 83 | auto RotL = m_OneUse( |
| 84 | m_c_Or(m_Shl(m_Value(L0), m_Value(L1)), m_LShr(m_Value(R0), Sub))); |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 85 | if (RotL.match(V) && L0 == R0 && L1 == R1) { |
| 86 | X = L0; |
| 87 | Y = L1; |
| 88 | return Intrinsic::fshl; |
| 89 | } |
| 90 | |
| 91 | // rotate_right(X, Y) == (X >> Y) | (X << (Width - Y)) |
Pawel Bylica | 119aa8f | 2019-01-02 19:51:46 +0000 | [diff] [blame] | 92 | auto RotR = m_OneUse( |
| 93 | m_c_Or(m_LShr(m_Value(L0), m_Value(L1)), m_Shl(m_Value(R0), Sub))); |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 94 | if (RotR.match(V) && L0 == R0 && L1 == R1) { |
| 95 | X = L0; |
| 96 | Y = L1; |
| 97 | return Intrinsic::fshr; |
| 98 | } |
| 99 | |
| 100 | return Intrinsic::not_intrinsic; |
| 101 | }; |
| 102 | |
| 103 | // One phi operand must be a rotate operation, and the other phi operand must |
| 104 | // be the source value of that rotate operation: |
| 105 | // phi [ rotate(RotSrc, RotAmt), RotBB ], [ RotSrc, GuardBB ] |
| 106 | PHINode &Phi = cast<PHINode>(I); |
| 107 | Value *P0 = Phi.getOperand(0), *P1 = Phi.getOperand(1); |
| 108 | Value *RotSrc, *RotAmt; |
| 109 | Intrinsic::ID IID = matchRotate(P0, RotSrc, RotAmt); |
| 110 | if (IID == Intrinsic::not_intrinsic || RotSrc != P1) { |
| 111 | IID = matchRotate(P1, RotSrc, RotAmt); |
| 112 | if (IID == Intrinsic::not_intrinsic || RotSrc != P0) |
| 113 | return false; |
| 114 | assert((IID == Intrinsic::fshl || IID == Intrinsic::fshr) && |
| 115 | "Pattern must match funnel shift left or right"); |
| 116 | } |
| 117 | |
| 118 | // The incoming block with our source operand must be the "guard" block. |
| 119 | // That must contain a cmp+branch to avoid the rotate when the shift amount |
| 120 | // is equal to 0. The other incoming block is the block with the rotate. |
| 121 | BasicBlock *GuardBB = Phi.getIncomingBlock(RotSrc == P1); |
| 122 | BasicBlock *RotBB = Phi.getIncomingBlock(RotSrc != P1); |
| 123 | Instruction *TermI = GuardBB->getTerminator(); |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 124 | ICmpInst::Predicate Pred; |
Florian Hahn | 5c3bc3c | 2019-09-25 15:05:08 +0000 | [diff] [blame] | 125 | BasicBlock *PhiBB = Phi.getParent(); |
| 126 | if (!match(TermI, m_Br(m_ICmp(Pred, m_Specific(RotAmt), m_ZeroInt()), |
| 127 | m_SpecificBB(PhiBB), m_SpecificBB(RotBB)))) |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 128 | return false; |
| 129 | |
Florian Hahn | 5c3bc3c | 2019-09-25 15:05:08 +0000 | [diff] [blame] | 130 | if (Pred != CmpInst::ICMP_EQ) |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 131 | return false; |
| 132 | |
| 133 | // We matched a variation of this IR pattern: |
| 134 | // GuardBB: |
| 135 | // %cmp = icmp eq i32 %RotAmt, 0 |
| 136 | // br i1 %cmp, label %PhiBB, label %RotBB |
| 137 | // RotBB: |
| 138 | // %sub = sub i32 32, %RotAmt |
| 139 | // %shr = lshr i32 %X, %sub |
| 140 | // %shl = shl i32 %X, %RotAmt |
| 141 | // %rot = or i32 %shr, %shl |
| 142 | // br label %PhiBB |
| 143 | // PhiBB: |
| 144 | // %cond = phi i32 [ %rot, %RotBB ], [ %X, %GuardBB ] |
| 145 | // --> |
| 146 | // llvm.fshl.i32(i32 %X, i32 %RotAmt) |
| 147 | IRBuilder<> Builder(PhiBB, PhiBB->getFirstInsertionPt()); |
| 148 | Function *F = Intrinsic::getDeclaration(Phi.getModule(), IID, Phi.getType()); |
| 149 | Phi.replaceAllUsesWith(Builder.CreateCall(F, {RotSrc, RotSrc, RotAmt})); |
| 150 | return true; |
| 151 | } |
| 152 | |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 153 | /// This is used by foldAnyOrAllBitsSet() to capture a source value (Root) and |
| 154 | /// the bit indexes (Mask) needed by a masked compare. If we're matching a chain |
| 155 | /// of 'and' ops, then we also need to capture the fact that we saw an |
| 156 | /// "and X, 1", so that's an extra return value for that case. |
| 157 | struct MaskOps { |
| 158 | Value *Root; |
| 159 | APInt Mask; |
| 160 | bool MatchAndChain; |
| 161 | bool FoundAnd1; |
| 162 | |
Pawel Bylica | 119aa8f | 2019-01-02 19:51:46 +0000 | [diff] [blame] | 163 | MaskOps(unsigned BitWidth, bool MatchAnds) |
| 164 | : Root(nullptr), Mask(APInt::getNullValue(BitWidth)), |
| 165 | MatchAndChain(MatchAnds), FoundAnd1(false) {} |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /// This is a recursive helper for foldAnyOrAllBitsSet() that walks through a |
| 169 | /// chain of 'and' or 'or' instructions looking for shift ops of a common source |
| 170 | /// value. Examples: |
| 171 | /// or (or (or X, (X >> 3)), (X >> 5)), (X >> 8) |
| 172 | /// returns { X, 0x129 } |
| 173 | /// and (and (X >> 1), 1), (X >> 4) |
| 174 | /// returns { X, 0x12 } |
| 175 | static bool matchAndOrChain(Value *V, MaskOps &MOps) { |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 176 | Value *Op0, *Op1; |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 177 | if (MOps.MatchAndChain) { |
| 178 | // Recurse through a chain of 'and' operands. This requires an extra check |
| 179 | // vs. the 'or' matcher: we must find an "and X, 1" instruction somewhere |
| 180 | // in the chain to know that all of the high bits are cleared. |
| 181 | if (match(V, m_And(m_Value(Op0), m_One()))) { |
| 182 | MOps.FoundAnd1 = true; |
| 183 | return matchAndOrChain(Op0, MOps); |
| 184 | } |
| 185 | if (match(V, m_And(m_Value(Op0), m_Value(Op1)))) |
| 186 | return matchAndOrChain(Op0, MOps) && matchAndOrChain(Op1, MOps); |
| 187 | } else { |
| 188 | // Recurse through a chain of 'or' operands. |
| 189 | if (match(V, m_Or(m_Value(Op0), m_Value(Op1)))) |
| 190 | return matchAndOrChain(Op0, MOps) && matchAndOrChain(Op1, MOps); |
| 191 | } |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 192 | |
| 193 | // We need a shift-right or a bare value representing a compare of bit 0 of |
| 194 | // the original source operand. |
| 195 | Value *Candidate; |
| 196 | uint64_t BitIndex = 0; |
| 197 | if (!match(V, m_LShr(m_Value(Candidate), m_ConstantInt(BitIndex)))) |
| 198 | Candidate = V; |
| 199 | |
| 200 | // Initialize result source operand. |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 201 | if (!MOps.Root) |
| 202 | MOps.Root = Candidate; |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 203 | |
Sanjay Patel | bf55e6d | 2018-05-14 13:43:32 +0000 | [diff] [blame] | 204 | // The shift constant is out-of-range? This code hasn't been simplified. |
| 205 | if (BitIndex >= MOps.Mask.getBitWidth()) |
| 206 | return false; |
| 207 | |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 208 | // Fill in the mask bit derived from the shift constant. |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 209 | MOps.Mask.setBit(BitIndex); |
| 210 | return MOps.Root == Candidate; |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 213 | /// Match patterns that correspond to "any-bits-set" and "all-bits-set". |
| 214 | /// These will include a chain of 'or' or 'and'-shifted bits from a |
| 215 | /// common source value: |
| 216 | /// and (or (lshr X, C), ...), 1 --> (X & CMask) != 0 |
| 217 | /// and (and (lshr X, C), ...), 1 --> (X & CMask) == CMask |
| 218 | /// Note: "any-bits-clear" and "all-bits-clear" are variations of these patterns |
| 219 | /// that differ only with a final 'not' of the result. We expect that final |
| 220 | /// 'not' to be folded with the compare that we create here (invert predicate). |
| 221 | static bool foldAnyOrAllBitsSet(Instruction &I) { |
| 222 | // The 'any-bits-set' ('or' chain) pattern is simpler to match because the |
| 223 | // final "and X, 1" instruction must be the final op in the sequence. |
| 224 | bool MatchAllBitsSet; |
| 225 | if (match(&I, m_c_And(m_OneUse(m_And(m_Value(), m_Value())), m_Value()))) |
| 226 | MatchAllBitsSet = true; |
| 227 | else if (match(&I, m_And(m_OneUse(m_Or(m_Value(), m_Value())), m_One()))) |
| 228 | MatchAllBitsSet = false; |
| 229 | else |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 230 | return false; |
| 231 | |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 232 | MaskOps MOps(I.getType()->getScalarSizeInBits(), MatchAllBitsSet); |
| 233 | if (MatchAllBitsSet) { |
| 234 | if (!matchAndOrChain(cast<BinaryOperator>(&I), MOps) || !MOps.FoundAnd1) |
| 235 | return false; |
| 236 | } else { |
| 237 | if (!matchAndOrChain(cast<BinaryOperator>(&I)->getOperand(0), MOps)) |
| 238 | return false; |
| 239 | } |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 240 | |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 241 | // The pattern was found. Create a masked compare that replaces all of the |
| 242 | // shift and logic ops. |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 243 | IRBuilder<> Builder(&I); |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 244 | Constant *Mask = ConstantInt::get(I.getType(), MOps.Mask); |
| 245 | Value *And = Builder.CreateAnd(MOps.Root, Mask); |
Pawel Bylica | 119aa8f | 2019-01-02 19:51:46 +0000 | [diff] [blame] | 246 | Value *Cmp = MatchAllBitsSet ? Builder.CreateICmpEQ(And, Mask) |
| 247 | : Builder.CreateIsNotNull(And); |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 248 | Value *Zext = Builder.CreateZExt(Cmp, I.getType()); |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 249 | I.replaceAllUsesWith(Zext); |
| 250 | return true; |
| 251 | } |
| 252 | |
Chen Zheng | c17c586 | 2019-10-11 05:13:56 +0000 | [diff] [blame] | 253 | // Try to recognize below function as popcount intrinsic. |
| 254 | // This is the "best" algorithm from |
| 255 | // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel |
| 256 | // Also used in TargetLowering::expandCTPOP(). |
| 257 | // |
| 258 | // int popcount(unsigned int i) { |
| 259 | // i = i - ((i >> 1) & 0x55555555); |
| 260 | // i = (i & 0x33333333) + ((i >> 2) & 0x33333333); |
| 261 | // i = ((i + (i >> 4)) & 0x0F0F0F0F); |
| 262 | // return (i * 0x01010101) >> 24; |
| 263 | // } |
| 264 | static bool tryToRecognizePopCount(Instruction &I) { |
| 265 | if (I.getOpcode() != Instruction::LShr) |
| 266 | return false; |
| 267 | |
| 268 | Type *Ty = I.getType(); |
| 269 | if (!Ty->isIntOrIntVectorTy()) |
| 270 | return false; |
| 271 | |
| 272 | unsigned Len = Ty->getScalarSizeInBits(); |
| 273 | // FIXME: fix Len == 8 and other irregular type lengths. |
| 274 | if (!(Len <= 128 && Len > 8 && Len % 8 == 0)) |
| 275 | return false; |
| 276 | |
| 277 | APInt Mask55 = APInt::getSplat(Len, APInt(8, 0x55)); |
| 278 | APInt Mask33 = APInt::getSplat(Len, APInt(8, 0x33)); |
| 279 | APInt Mask0F = APInt::getSplat(Len, APInt(8, 0x0F)); |
| 280 | APInt Mask01 = APInt::getSplat(Len, APInt(8, 0x01)); |
| 281 | APInt MaskShift = APInt(Len, Len - 8); |
| 282 | |
| 283 | Value *Op0 = I.getOperand(0); |
| 284 | Value *Op1 = I.getOperand(1); |
| 285 | Value *MulOp0; |
| 286 | // Matching "(i * 0x01010101...) >> 24". |
| 287 | if ((match(Op0, m_Mul(m_Value(MulOp0), m_SpecificInt(Mask01)))) && |
| 288 | match(Op1, m_SpecificInt(MaskShift))) { |
| 289 | Value *ShiftOp0; |
| 290 | // Matching "((i + (i >> 4)) & 0x0F0F0F0F...)". |
| 291 | if (match(MulOp0, m_And(m_c_Add(m_LShr(m_Value(ShiftOp0), m_SpecificInt(4)), |
| 292 | m_Deferred(ShiftOp0)), |
| 293 | m_SpecificInt(Mask0F)))) { |
| 294 | Value *AndOp0; |
| 295 | // Matching "(i & 0x33333333...) + ((i >> 2) & 0x33333333...)". |
| 296 | if (match(ShiftOp0, |
| 297 | m_c_Add(m_And(m_Value(AndOp0), m_SpecificInt(Mask33)), |
| 298 | m_And(m_LShr(m_Deferred(AndOp0), m_SpecificInt(2)), |
| 299 | m_SpecificInt(Mask33))))) { |
| 300 | Value *Root, *SubOp1; |
| 301 | // Matching "i - ((i >> 1) & 0x55555555...)". |
| 302 | if (match(AndOp0, m_Sub(m_Value(Root), m_Value(SubOp1))) && |
| 303 | match(SubOp1, m_And(m_LShr(m_Specific(Root), m_SpecificInt(1)), |
| 304 | m_SpecificInt(Mask55)))) { |
| 305 | LLVM_DEBUG(dbgs() << "Recognized popcount intrinsic\n"); |
| 306 | IRBuilder<> Builder(&I); |
| 307 | Function *Func = Intrinsic::getDeclaration( |
| 308 | I.getModule(), Intrinsic::ctpop, I.getType()); |
| 309 | I.replaceAllUsesWith(Builder.CreateCall(Func, {Root})); |
| 310 | return true; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 319 | /// This is the entry point for folds that could be implemented in regular |
| 320 | /// InstCombine, but they are separated because they are not expected to |
| 321 | /// occur frequently and/or have more than a constant-length pattern match. |
| 322 | static bool foldUnusualPatterns(Function &F, DominatorTree &DT) { |
| 323 | bool MadeChange = false; |
| 324 | for (BasicBlock &BB : F) { |
| 325 | // Ignore unreachable basic blocks. |
| 326 | if (!DT.isReachableFromEntry(&BB)) |
| 327 | continue; |
| 328 | // Do not delete instructions under here and invalidate the iterator. |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 329 | // Walk the block backwards for efficiency. We're matching a chain of |
| 330 | // use->defs, so we're more likely to succeed by starting from the bottom. |
| 331 | // Also, we want to avoid matching partial patterns. |
| 332 | // TODO: It would be more efficient if we removed dead instructions |
| 333 | // iteratively in this loop rather than waiting until the end. |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 334 | for (Instruction &I : make_range(BB.rbegin(), BB.rend())) { |
Sanjay Patel | ac3951a | 2018-05-09 23:08:15 +0000 | [diff] [blame] | 335 | MadeChange |= foldAnyOrAllBitsSet(I); |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 336 | MadeChange |= foldGuardedRotateToFunnelShift(I); |
Chen Zheng | c17c586 | 2019-10-11 05:13:56 +0000 | [diff] [blame] | 337 | MadeChange |= tryToRecognizePopCount(I); |
Sanjay Patel | 200885e | 2018-12-17 21:14:51 +0000 | [diff] [blame] | 338 | } |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // We're done with transforms, so remove dead instructions. |
| 342 | if (MadeChange) |
| 343 | for (BasicBlock &BB : F) |
| 344 | SimplifyInstructionsInBlock(&BB); |
| 345 | |
| 346 | return MadeChange; |
| 347 | } |
| 348 | |
| 349 | /// This is the entry point for all transforms. Pass manager differences are |
| 350 | /// handled in the callers of this function. |
| 351 | static bool runImpl(Function &F, TargetLibraryInfo &TLI, DominatorTree &DT) { |
| 352 | bool MadeChange = false; |
| 353 | const DataLayout &DL = F.getParent()->getDataLayout(); |
| 354 | TruncInstCombine TIC(TLI, DL, DT); |
| 355 | MadeChange |= TIC.run(F); |
| 356 | MadeChange |= foldUnusualPatterns(F, DT); |
| 357 | return MadeChange; |
| 358 | } |
| 359 | |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 360 | void AggressiveInstCombinerLegacyPass::getAnalysisUsage( |
| 361 | AnalysisUsage &AU) const { |
| 362 | AU.setPreservesCFG(); |
Amjad Aboud | d895bff | 2018-01-31 10:41:31 +0000 | [diff] [blame] | 363 | AU.addRequired<DominatorTreeWrapperPass>(); |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 364 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| 365 | AU.addPreserved<AAResultsWrapperPass>(); |
| 366 | AU.addPreserved<BasicAAWrapperPass>(); |
Amjad Aboud | d895bff | 2018-01-31 10:41:31 +0000 | [diff] [blame] | 367 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 368 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 369 | } |
| 370 | |
| 371 | bool AggressiveInstCombinerLegacyPass::runOnFunction(Function &F) { |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 372 | auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 373 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 374 | return runImpl(F, TLI, DT); |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | PreservedAnalyses AggressiveInstCombinePass::run(Function &F, |
| 378 | FunctionAnalysisManager &AM) { |
| 379 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(F); |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 380 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
| 381 | if (!runImpl(F, TLI, DT)) { |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 382 | // No changes, all analyses are preserved. |
| 383 | return PreservedAnalyses::all(); |
Sanjay Patel | d2025a2 | 2018-05-01 21:02:09 +0000 | [diff] [blame] | 384 | } |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 385 | // Mark all the analyses that instcombine updates as preserved. |
| 386 | PreservedAnalyses PA; |
| 387 | PA.preserveSet<CFGAnalyses>(); |
| 388 | PA.preserve<AAManager>(); |
| 389 | PA.preserve<GlobalsAA>(); |
| 390 | return PA; |
| 391 | } |
| 392 | |
| 393 | char AggressiveInstCombinerLegacyPass::ID = 0; |
| 394 | INITIALIZE_PASS_BEGIN(AggressiveInstCombinerLegacyPass, |
| 395 | "aggressive-instcombine", |
| 396 | "Combine pattern based expressions", false, false) |
Amjad Aboud | d895bff | 2018-01-31 10:41:31 +0000 | [diff] [blame] | 397 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 398 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 399 | INITIALIZE_PASS_END(AggressiveInstCombinerLegacyPass, "aggressive-instcombine", |
| 400 | "Combine pattern based expressions", false, false) |
| 401 | |
Craig Topper | d4eb207 | 2018-04-24 00:05:21 +0000 | [diff] [blame] | 402 | // Initialization Routines |
| 403 | void llvm::initializeAggressiveInstCombine(PassRegistry &Registry) { |
| 404 | initializeAggressiveInstCombinerLegacyPassPass(Registry); |
| 405 | } |
| 406 | |
Craig Topper | 1bcb258 | 2018-04-24 00:39:29 +0000 | [diff] [blame] | 407 | void LLVMInitializeAggressiveInstCombiner(LLVMPassRegistryRef R) { |
| 408 | initializeAggressiveInstCombinerLegacyPassPass(*unwrap(R)); |
| 409 | } |
| 410 | |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 411 | FunctionPass *llvm::createAggressiveInstCombinerPass() { |
| 412 | return new AggressiveInstCombinerLegacyPass(); |
| 413 | } |
David Blaikie | ba47dd1 | 2018-04-24 15:40:07 +0000 | [diff] [blame] | 414 | |
| 415 | void LLVMAddAggressiveInstCombinerPass(LLVMPassManagerRef PM) { |
| 416 | unwrap(PM)->add(createAggressiveInstCombinerPass()); |
| 417 | } |