| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 1 | //===- MergeICmps.cpp - Optimize chains of integer comparisons ------------===// |
| 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 |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass turns chains of integer comparisons into memcmp (the memcmp is |
| 10 | // later typically inlined as a chain of efficient hardware comparisons). This |
| 11 | // typically benefits c++ member or nonmember operator==(). |
| 12 | // |
| Clement Courbet | cc004df | 2019-02-15 12:58:06 +0000 | [diff] [blame] | 13 | // The basic idea is to replace a longer chain of integer comparisons loaded |
| 14 | // from contiguous memory locations into a shorter chain of larger integer |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 15 | // comparisons. Benefits are double: |
| 16 | // - There are less jumps, and therefore less opportunities for mispredictions |
| 17 | // and I-cache misses. |
| 18 | // - Code size is smaller, both because jumps are removed and because the |
| 19 | // encoding of a 2*n byte compare is smaller than that of two n-byte |
| 20 | // compares. |
| Clement Courbet | cc004df | 2019-02-15 12:58:06 +0000 | [diff] [blame] | 21 | // |
| 22 | // Example: |
| 23 | // |
| 24 | // struct S { |
| 25 | // int a; |
| 26 | // char b; |
| 27 | // char c; |
| 28 | // uint16_t d; |
| 29 | // bool operator==(const S& o) const { |
| 30 | // return a == o.a && b == o.b && c == o.c && d == o.d; |
| 31 | // } |
| 32 | // }; |
| 33 | // |
| 34 | // Is optimized as : |
| 35 | // |
| 36 | // bool S::operator==(const S& o) const { |
| 37 | // return memcmp(this, &o, 8) == 0; |
| 38 | // } |
| 39 | // |
| 40 | // Which will later be expanded (ExpandMemCmp) as a single 8-bytes icmp. |
| 41 | // |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/DomTreeUpdater.h" |
| 45 | #include "llvm/Analysis/GlobalsModRef.h" |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/Loads.h" |
| 47 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 48 | #include "llvm/Analysis/TargetTransformInfo.h" |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 49 | #include "llvm/IR/Dominators.h" |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 50 | #include "llvm/IR/Function.h" |
| 51 | #include "llvm/IR/IRBuilder.h" |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 52 | #include "llvm/Pass.h" |
| 53 | #include "llvm/Transforms/Scalar.h" |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 54 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 56 | #include <algorithm> |
| 57 | #include <numeric> |
| 58 | #include <utility> |
| 59 | #include <vector> |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 60 | |
| 61 | using namespace llvm; |
| 62 | |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 63 | namespace { |
| 64 | |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 65 | #define DEBUG_TYPE "mergeicmps" |
| 66 | |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 67 | // Returns true if the instruction is a simple load or a simple store |
| 68 | static bool isSimpleLoadOrStore(const Instruction *I) { |
| 69 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 70 | return LI->isSimple(); |
| 71 | if (const StoreInst *SI = dyn_cast<StoreInst>(I)) |
| 72 | return SI->isSimple(); |
| 73 | return false; |
| 74 | } |
| 75 | |
| Clement Courbet | cc004df | 2019-02-15 12:58:06 +0000 | [diff] [blame] | 76 | // A BCE atom "Binary Compare Expression Atom" represents an integer load |
| 77 | // that is a constant offset from a base value, e.g. `a` or `o.c` in the example |
| 78 | // at the top. |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 79 | struct BCEAtom { |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 80 | BCEAtom() = default; |
| 81 | BCEAtom(GetElementPtrInst *GEP, LoadInst *LoadI, int BaseId, APInt Offset) |
| 82 | : GEP(GEP), LoadI(LoadI), BaseId(BaseId), Offset(Offset) {} |
| Clement Courbet | bc0c445 | 2017-09-01 11:51:23 +0000 | [diff] [blame] | 83 | |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 84 | // We want to order BCEAtoms by (Base, Offset). However we cannot use |
| 85 | // the pointer values for Base because these are non-deterministic. |
| 86 | // To make sure that the sort order is stable, we first assign to each atom |
| 87 | // base value an index based on its order of appearance in the chain of |
| 88 | // comparisons. We call this index `BaseOrdering`. For example, for: |
| 89 | // b[3] == c[2] && a[1] == d[1] && b[4] == c[3] |
| 90 | // | block 1 | | block 2 | | block 3 | |
| 91 | // b gets assigned index 0 and a index 1, because b appears as LHS in block 1, |
| 92 | // which is before block 2. |
| 93 | // We then sort by (BaseOrdering[LHS.Base()], LHS.Offset), which is stable. |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 94 | bool operator<(const BCEAtom &O) const { |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 95 | return BaseId != O.BaseId ? BaseId < O.BaseId : Offset.slt(O.Offset); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 98 | GetElementPtrInst *GEP = nullptr; |
| 99 | LoadInst *LoadI = nullptr; |
| 100 | unsigned BaseId = 0; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 101 | APInt Offset; |
| 102 | }; |
| 103 | |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 104 | // A class that assigns increasing ids to values in the order in which they are |
| 105 | // seen. See comment in `BCEAtom::operator<()``. |
| 106 | class BaseIdentifier { |
| 107 | public: |
| 108 | // Returns the id for value `Base`, after assigning one if `Base` has not been |
| 109 | // seen before. |
| 110 | int getBaseId(const Value *Base) { |
| 111 | assert(Base && "invalid base"); |
| 112 | const auto Insertion = BaseToIndex.try_emplace(Base, Order); |
| 113 | if (Insertion.second) |
| 114 | ++Order; |
| 115 | return Insertion.first->second; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | unsigned Order = 1; |
| 120 | DenseMap<const Value*, int> BaseToIndex; |
| 121 | }; |
| 122 | |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 123 | // If this value is a load from a constant offset w.r.t. a base address, and |
| Xin Tong | 256869d | 2018-02-28 12:09:53 +0000 | [diff] [blame] | 124 | // there are no other users of the load or address, returns the base address and |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 125 | // the offset. |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 126 | BCEAtom visitICmpLoadOperand(Value *const Val, BaseIdentifier &BaseId) { |
| 127 | auto *const LoadI = dyn_cast<LoadInst>(Val); |
| 128 | if (!LoadI) |
| 129 | return {}; |
| 130 | LLVM_DEBUG(dbgs() << "load\n"); |
| 131 | if (LoadI->isUsedOutsideOfBlock(LoadI->getParent())) { |
| 132 | LLVM_DEBUG(dbgs() << "used outside of block\n"); |
| 133 | return {}; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 134 | } |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 135 | // Do not optimize atomic loads to non-atomic memcmp |
| 136 | if (!LoadI->isSimple()) { |
| 137 | LLVM_DEBUG(dbgs() << "volatile or atomic\n"); |
| 138 | return {}; |
| 139 | } |
| 140 | Value *const Addr = LoadI->getOperand(0); |
| 141 | auto *const GEP = dyn_cast<GetElementPtrInst>(Addr); |
| 142 | if (!GEP) |
| 143 | return {}; |
| 144 | LLVM_DEBUG(dbgs() << "GEP\n"); |
| 145 | if (GEP->isUsedOutsideOfBlock(LoadI->getParent())) { |
| 146 | LLVM_DEBUG(dbgs() << "used outside of block\n"); |
| 147 | return {}; |
| 148 | } |
| 149 | const auto &DL = GEP->getModule()->getDataLayout(); |
| 150 | if (!isDereferenceablePointer(GEP, DL)) { |
| 151 | LLVM_DEBUG(dbgs() << "not dereferenceable\n"); |
| 152 | // We need to make sure that we can do comparison in any order, so we |
| 153 | // require memory to be unconditionnally dereferencable. |
| 154 | return {}; |
| 155 | } |
| 156 | APInt Offset = APInt(DL.getPointerTypeSizeInBits(GEP->getType()), 0); |
| 157 | if (!GEP->accumulateConstantOffset(DL, Offset)) |
| 158 | return {}; |
| 159 | return BCEAtom(GEP, LoadI, BaseId.getBaseId(GEP->getPointerOperand()), |
| 160 | Offset); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| Clement Courbet | cc004df | 2019-02-15 12:58:06 +0000 | [diff] [blame] | 163 | // A basic block with a comparison between two BCE atoms, e.g. `a == o.a` in the |
| 164 | // example at the top. |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 165 | // The block might do extra work besides the atom comparison, in which case |
| 166 | // doesOtherWork() returns true. Under some conditions, the block can be |
| 167 | // split into the atom comparison part and the "other work" part |
| 168 | // (see canSplit()). |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 169 | // Note: the terminology is misleading: the comparison is symmetric, so there |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 170 | // is no real {l/r}hs. What we want though is to have the same base on the |
| 171 | // left (resp. right), so that we can detect consecutive loads. To ensure this |
| 172 | // we put the smallest atom on the left. |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 173 | class BCECmpBlock { |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 174 | public: |
| 175 | BCECmpBlock() {} |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 176 | |
| 177 | BCECmpBlock(BCEAtom L, BCEAtom R, int SizeBits) |
| Clement Courbet | 8361a10 | 2019-05-21 14:24:46 +0000 | [diff] [blame] | 178 | : Lhs_(L), Rhs_(R), SizeBits_(SizeBits) { |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 179 | if (Rhs_ < Lhs_) std::swap(Rhs_, Lhs_); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 182 | bool IsValid() const { return Lhs_.BaseId != 0 && Rhs_.BaseId != 0; } |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 183 | |
| Hiroshi Inoue | d24ddcd | 2018-01-19 10:55:29 +0000 | [diff] [blame] | 184 | // Assert the block is consistent: If valid, it should also have |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 185 | // non-null members besides Lhs_ and Rhs_. |
| 186 | void AssertConsistent() const { |
| 187 | if (IsValid()) { |
| 188 | assert(BB); |
| 189 | assert(CmpI); |
| 190 | assert(BranchI); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | const BCEAtom &Lhs() const { return Lhs_; } |
| 195 | const BCEAtom &Rhs() const { return Rhs_; } |
| 196 | int SizeBits() const { return SizeBits_; } |
| 197 | |
| 198 | // Returns true if the block does other works besides comparison. |
| 199 | bool doesOtherWork() const; |
| 200 | |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 201 | // Returns true if the non-BCE-cmp instructions can be separated from BCE-cmp |
| 202 | // instructions in the block. |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 203 | bool canSplit(AliasAnalysis *AA) const; |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 204 | |
| 205 | // Return true if this all the relevant instructions in the BCE-cmp-block can |
| 206 | // be sunk below this instruction. By doing this, we know we can separate the |
| 207 | // BCE-cmp-block instructions from the non-BCE-cmp-block instructions in the |
| 208 | // block. |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 209 | bool canSinkBCECmpInst(const Instruction *, DenseSet<Instruction *> &, |
| 210 | AliasAnalysis *AA) const; |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 211 | |
| 212 | // We can separate the BCE-cmp-block instructions and the non-BCE-cmp-block |
| 213 | // instructions. Split the old block and move all non-BCE-cmp-insts into the |
| 214 | // new parent block. |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 215 | void split(BasicBlock *NewParent, AliasAnalysis *AA) const; |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 216 | |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 217 | // The basic block where this comparison happens. |
| 218 | BasicBlock *BB = nullptr; |
| 219 | // The ICMP for this comparison. |
| 220 | ICmpInst *CmpI = nullptr; |
| 221 | // The terminating branch. |
| 222 | BranchInst *BranchI = nullptr; |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 223 | // The block requires splitting. |
| 224 | bool RequireSplit = false; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 225 | |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 226 | private: |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 227 | BCEAtom Lhs_; |
| 228 | BCEAtom Rhs_; |
| 229 | int SizeBits_ = 0; |
| 230 | }; |
| 231 | |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 232 | bool BCECmpBlock::canSinkBCECmpInst(const Instruction *Inst, |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 233 | DenseSet<Instruction *> &BlockInsts, |
| 234 | AliasAnalysis *AA) const { |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 235 | // If this instruction has side effects and its in middle of the BCE cmp block |
| 236 | // instructions, then bail for now. |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 237 | if (Inst->mayHaveSideEffects()) { |
| 238 | // Bail if this is not a simple load or store |
| 239 | if (!isSimpleLoadOrStore(Inst)) |
| 240 | return false; |
| 241 | // Disallow stores that might alias the BCE operands |
| 242 | MemoryLocation LLoc = MemoryLocation::get(Lhs_.LoadI); |
| 243 | MemoryLocation RLoc = MemoryLocation::get(Rhs_.LoadI); |
| 244 | if (isModSet(AA->getModRefInfo(Inst, LLoc)) || |
| 245 | isModSet(AA->getModRefInfo(Inst, RLoc))) |
| 246 | return false; |
| 247 | } |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 248 | // Make sure this instruction does not use any of the BCE cmp block |
| 249 | // instructions as operand. |
| 250 | for (auto BI : BlockInsts) { |
| 251 | if (is_contained(Inst->operands(), BI)) |
| 252 | return false; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 257 | void BCECmpBlock::split(BasicBlock *NewParent, AliasAnalysis *AA) const { |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 258 | DenseSet<Instruction *> BlockInsts( |
| 259 | {Lhs_.GEP, Rhs_.GEP, Lhs_.LoadI, Rhs_.LoadI, CmpI, BranchI}); |
| 260 | llvm::SmallVector<Instruction *, 4> OtherInsts; |
| 261 | for (Instruction &Inst : *BB) { |
| 262 | if (BlockInsts.count(&Inst)) |
| 263 | continue; |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 264 | assert(canSinkBCECmpInst(&Inst, BlockInsts, AA) && |
| 265 | "Split unsplittable block"); |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 266 | // This is a non-BCE-cmp-block instruction. And it can be separated |
| 267 | // from the BCE-cmp-block instruction. |
| 268 | OtherInsts.push_back(&Inst); |
| 269 | } |
| 270 | |
| 271 | // Do the actual spliting. |
| 272 | for (Instruction *Inst : reverse(OtherInsts)) { |
| 273 | Inst->moveBefore(&*NewParent->begin()); |
| 274 | } |
| 275 | } |
| 276 | |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 277 | bool BCECmpBlock::canSplit(AliasAnalysis *AA) const { |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 278 | DenseSet<Instruction *> BlockInsts( |
| 279 | {Lhs_.GEP, Rhs_.GEP, Lhs_.LoadI, Rhs_.LoadI, CmpI, BranchI}); |
| 280 | for (Instruction &Inst : *BB) { |
| 281 | if (!BlockInsts.count(&Inst)) { |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 282 | if (!canSinkBCECmpInst(&Inst, BlockInsts, AA)) |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 283 | return false; |
| 284 | } |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 289 | bool BCECmpBlock::doesOtherWork() const { |
| 290 | AssertConsistent(); |
| Xin Tong | 8fd561f | 2018-03-06 02:24:02 +0000 | [diff] [blame] | 291 | // All the instructions we care about in the BCE cmp block. |
| 292 | DenseSet<Instruction *> BlockInsts( |
| 293 | {Lhs_.GEP, Rhs_.GEP, Lhs_.LoadI, Rhs_.LoadI, CmpI, BranchI}); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 294 | // TODO(courbet): Can we allow some other things ? This is very conservative. |
| Hiroshi Inoue | ae17900 | 2018-04-14 08:59:00 +0000 | [diff] [blame] | 295 | // We might be able to get away with anything does not have any side |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 296 | // effects outside of the basic block. |
| 297 | // Note: The GEPs and/or loads are not necessarily in the same block. |
| 298 | for (const Instruction &Inst : *BB) { |
| Xin Tong | 8fd561f | 2018-03-06 02:24:02 +0000 | [diff] [blame] | 299 | if (!BlockInsts.count(&Inst)) |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 300 | return true; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // Visit the given comparison. If this is a comparison between two valid |
| 306 | // BCE atoms, returns the comparison. |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 307 | BCECmpBlock visitICmp(const ICmpInst *const CmpI, |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 308 | const ICmpInst::Predicate ExpectedPredicate, |
| 309 | BaseIdentifier &BaseId) { |
| Clement Courbet | 9f0b317 | 2018-03-13 07:05:55 +0000 | [diff] [blame] | 310 | // The comparison can only be used once: |
| 311 | // - For intermediate blocks, as a branch condition. |
| 312 | // - For the final block, as an incoming value for the Phi. |
| 313 | // If there are any other uses of the comparison, we cannot merge it with |
| 314 | // other comparisons as we would create an orphan use of the value. |
| 315 | if (!CmpI->hasOneUse()) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 316 | LLVM_DEBUG(dbgs() << "cmp has several uses\n"); |
| Clement Courbet | 9f0b317 | 2018-03-13 07:05:55 +0000 | [diff] [blame] | 317 | return {}; |
| 318 | } |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 319 | if (CmpI->getPredicate() != ExpectedPredicate) |
| 320 | return {}; |
| 321 | LLVM_DEBUG(dbgs() << "cmp " |
| 322 | << (ExpectedPredicate == ICmpInst::ICMP_EQ ? "eq" : "ne") |
| 323 | << "\n"); |
| 324 | auto Lhs = visitICmpLoadOperand(CmpI->getOperand(0), BaseId); |
| 325 | if (!Lhs.BaseId) |
| 326 | return {}; |
| 327 | auto Rhs = visitICmpLoadOperand(CmpI->getOperand(1), BaseId); |
| 328 | if (!Rhs.BaseId) |
| 329 | return {}; |
| 330 | const auto &DL = CmpI->getModule()->getDataLayout(); |
| 331 | return BCECmpBlock(std::move(Lhs), std::move(Rhs), |
| 332 | DL.getTypeSizeInBits(CmpI->getOperand(0)->getType())); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | // Visit the given comparison block. If this is a comparison between two valid |
| 336 | // BCE atoms, returns the comparison. |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 337 | BCECmpBlock visitCmpBlock(Value *const Val, BasicBlock *const Block, |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 338 | const BasicBlock *const PhiBlock, |
| 339 | BaseIdentifier &BaseId) { |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 340 | if (Block->empty()) return {}; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 341 | auto *const BranchI = dyn_cast<BranchInst>(Block->getTerminator()); |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 342 | if (!BranchI) return {}; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 343 | LLVM_DEBUG(dbgs() << "branch\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 344 | if (BranchI->isUnconditional()) { |
| 345 | // In this case, we expect an incoming value which is the result of the |
| 346 | // comparison. This is the last link in the chain of comparisons (note |
| 347 | // that this does not mean that this is the last incoming value, blocks |
| 348 | // can be reordered). |
| 349 | auto *const CmpI = dyn_cast<ICmpInst>(Val); |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 350 | if (!CmpI) return {}; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 351 | LLVM_DEBUG(dbgs() << "icmp\n"); |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 352 | auto Result = visitICmp(CmpI, ICmpInst::ICMP_EQ, BaseId); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 353 | Result.CmpI = CmpI; |
| 354 | Result.BranchI = BranchI; |
| 355 | return Result; |
| 356 | } else { |
| 357 | // In this case, we expect a constant incoming value (the comparison is |
| 358 | // chained). |
| 359 | const auto *const Const = dyn_cast<ConstantInt>(Val); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 360 | LLVM_DEBUG(dbgs() << "const\n"); |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 361 | if (!Const->isZero()) return {}; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 362 | LLVM_DEBUG(dbgs() << "false\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 363 | auto *const CmpI = dyn_cast<ICmpInst>(BranchI->getCondition()); |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 364 | if (!CmpI) return {}; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 365 | LLVM_DEBUG(dbgs() << "icmp\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 366 | assert(BranchI->getNumSuccessors() == 2 && "expecting a cond branch"); |
| 367 | BasicBlock *const FalseBlock = BranchI->getSuccessor(1); |
| 368 | auto Result = visitICmp( |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 369 | CmpI, FalseBlock == PhiBlock ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE, |
| 370 | BaseId); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 371 | Result.CmpI = CmpI; |
| 372 | Result.BranchI = BranchI; |
| 373 | return Result; |
| 374 | } |
| 375 | return {}; |
| 376 | } |
| 377 | |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 378 | static inline void enqueueBlock(std::vector<BCECmpBlock> &Comparisons, |
| Clement Courbet | 8361a10 | 2019-05-21 14:24:46 +0000 | [diff] [blame] | 379 | BCECmpBlock &Comparison) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 380 | LLVM_DEBUG(dbgs() << "Block '" << Comparison.BB->getName() |
| 381 | << "': Found cmp of " << Comparison.SizeBits() |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 382 | << " bits between " << Comparison.Lhs().BaseId << " + " |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 383 | << Comparison.Lhs().Offset << " and " |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 384 | << Comparison.Rhs().BaseId << " + " |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 385 | << Comparison.Rhs().Offset << "\n"); |
| 386 | LLVM_DEBUG(dbgs() << "\n"); |
| Clement Courbet | 8361a10 | 2019-05-21 14:24:46 +0000 | [diff] [blame] | 387 | Comparisons.push_back(Comparison); |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 390 | // A chain of comparisons. |
| 391 | class BCECmpChain { |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 392 | public: |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 393 | BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi, |
| 394 | AliasAnalysis *AA); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 395 | |
| 396 | int size() const { return Comparisons_.size(); } |
| 397 | |
| 398 | #ifdef MERGEICMPS_DOT_ON |
| 399 | void dump() const; |
| 400 | #endif // MERGEICMPS_DOT_ON |
| 401 | |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 402 | bool simplify(const TargetLibraryInfo *const TLI, AliasAnalysis *AA, |
| 403 | DomTreeUpdater &DTU); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 404 | |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 405 | private: |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 406 | static bool IsContiguous(const BCECmpBlock &First, |
| 407 | const BCECmpBlock &Second) { |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 408 | return First.Lhs().BaseId == Second.Lhs().BaseId && |
| 409 | First.Rhs().BaseId == Second.Rhs().BaseId && |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 410 | First.Lhs().Offset + First.SizeBits() / 8 == Second.Lhs().Offset && |
| 411 | First.Rhs().Offset + First.SizeBits() / 8 == Second.Rhs().Offset; |
| 412 | } |
| 413 | |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 414 | PHINode &Phi_; |
| 415 | std::vector<BCECmpBlock> Comparisons_; |
| 416 | // The original entry block (before sorting); |
| 417 | BasicBlock *EntryBlock_; |
| 418 | }; |
| 419 | |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 420 | BCECmpChain::BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi, |
| 421 | AliasAnalysis *AA) |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 422 | : Phi_(Phi) { |
| Clement Courbet | c2109c8 | 2018-02-06 09:14:00 +0000 | [diff] [blame] | 423 | assert(!Blocks.empty() && "a chain should have at least one block"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 424 | // Now look inside blocks to check for BCE comparisons. |
| 425 | std::vector<BCECmpBlock> Comparisons; |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 426 | BaseIdentifier BaseId; |
| Clement Courbet | a7a1746 | 2018-02-06 12:25:33 +0000 | [diff] [blame] | 427 | for (size_t BlockIdx = 0; BlockIdx < Blocks.size(); ++BlockIdx) { |
| 428 | BasicBlock *const Block = Blocks[BlockIdx]; |
| Clement Courbet | c2109c8 | 2018-02-06 09:14:00 +0000 | [diff] [blame] | 429 | assert(Block && "invalid block"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 430 | BCECmpBlock Comparison = visitCmpBlock(Phi.getIncomingValueForBlock(Block), |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 431 | Block, Phi.getParent(), BaseId); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 432 | Comparison.BB = Block; |
| 433 | if (!Comparison.IsValid()) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 434 | LLVM_DEBUG(dbgs() << "chain with invalid BCECmpBlock, no merge.\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 435 | return; |
| 436 | } |
| 437 | if (Comparison.doesOtherWork()) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 438 | LLVM_DEBUG(dbgs() << "block '" << Comparison.BB->getName() |
| 439 | << "' does extra work besides compare\n"); |
| Xin Tong | 8345c0e | 2018-03-05 13:54:47 +0000 | [diff] [blame] | 440 | if (Comparisons.empty()) { |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 441 | // This is the initial block in the chain, in case this block does other |
| 442 | // work, we can try to split the block and move the irrelevant |
| 443 | // instructions to the predecessor. |
| 444 | // |
| 445 | // If this is not the initial block in the chain, splitting it wont |
| 446 | // work. |
| 447 | // |
| 448 | // As once split, there will still be instructions before the BCE cmp |
| 449 | // instructions that do other work in program order, i.e. within the |
| 450 | // chain before sorting. Unless we can abort the chain at this point |
| 451 | // and start anew. |
| 452 | // |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 453 | // NOTE: we only handle blocks a with single predecessor for now. |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 454 | if (Comparison.canSplit(AA)) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 455 | LLVM_DEBUG(dbgs() |
| 456 | << "Split initial block '" << Comparison.BB->getName() |
| 457 | << "' that does extra work besides compare\n"); |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 458 | Comparison.RequireSplit = true; |
| Clement Courbet | 8361a10 | 2019-05-21 14:24:46 +0000 | [diff] [blame] | 459 | enqueueBlock(Comparisons, Comparison); |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 460 | } else { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 461 | LLVM_DEBUG(dbgs() |
| 462 | << "ignoring initial block '" << Comparison.BB->getName() |
| 463 | << "' that does extra work besides compare\n"); |
| Xin Tong | 0efadbb | 2018-04-09 13:14:06 +0000 | [diff] [blame] | 464 | } |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 465 | continue; |
| 466 | } |
| 467 | // TODO(courbet): Right now we abort the whole chain. We could be |
| 468 | // merging only the blocks that don't do other work and resume the |
| 469 | // chain from there. For example: |
| 470 | // if (a[0] == b[0]) { // bb1 |
| 471 | // if (a[1] == b[1]) { // bb2 |
| 472 | // some_value = 3; //bb3 |
| 473 | // if (a[2] == b[2]) { //bb3 |
| 474 | // do a ton of stuff //bb4 |
| 475 | // } |
| 476 | // } |
| 477 | // } |
| 478 | // |
| 479 | // This is: |
| 480 | // |
| 481 | // bb1 --eq--> bb2 --eq--> bb3* -eq--> bb4 --+ |
| 482 | // \ \ \ \ |
| 483 | // ne ne ne \ |
| 484 | // \ \ \ v |
| 485 | // +------------+-----------+----------> bb_phi |
| 486 | // |
| 487 | // We can only merge the first two comparisons, because bb3* does |
| 488 | // "other work" (setting some_value to 3). |
| 489 | // We could still merge bb1 and bb2 though. |
| 490 | return; |
| 491 | } |
| Clement Courbet | 8361a10 | 2019-05-21 14:24:46 +0000 | [diff] [blame] | 492 | enqueueBlock(Comparisons, Comparison); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 493 | } |
| Xin Tong | 8345c0e | 2018-03-05 13:54:47 +0000 | [diff] [blame] | 494 | |
| 495 | // It is possible we have no suitable comparison to merge. |
| 496 | if (Comparisons.empty()) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 497 | LLVM_DEBUG(dbgs() << "chain with no BCE basic blocks, no merge\n"); |
| Xin Tong | 8345c0e | 2018-03-05 13:54:47 +0000 | [diff] [blame] | 498 | return; |
| 499 | } |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 500 | EntryBlock_ = Comparisons[0].BB; |
| 501 | Comparisons_ = std::move(Comparisons); |
| 502 | #ifdef MERGEICMPS_DOT_ON |
| 503 | errs() << "BEFORE REORDERING:\n\n"; |
| 504 | dump(); |
| 505 | #endif // MERGEICMPS_DOT_ON |
| 506 | // Reorder blocks by LHS. We can do that without changing the |
| 507 | // semantics because we are only accessing dereferencable memory. |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 508 | llvm::sort(Comparisons_, |
| 509 | [](const BCECmpBlock &LhsBlock, const BCECmpBlock &RhsBlock) { |
| Clement Courbet | 122c6e6 | 2019-05-21 17:58:42 +0000 | [diff] [blame^] | 510 | return std::tie(LhsBlock.Lhs(), LhsBlock.Rhs()) < |
| 511 | std::tie(RhsBlock.Lhs(), RhsBlock.Rhs()); |
| Clement Courbet | f7e84a2 | 2019-02-15 14:17:17 +0000 | [diff] [blame] | 512 | }); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 513 | #ifdef MERGEICMPS_DOT_ON |
| 514 | errs() << "AFTER REORDERING:\n\n"; |
| 515 | dump(); |
| 516 | #endif // MERGEICMPS_DOT_ON |
| 517 | } |
| 518 | |
| 519 | #ifdef MERGEICMPS_DOT_ON |
| 520 | void BCECmpChain::dump() const { |
| 521 | errs() << "digraph dag {\n"; |
| 522 | errs() << " graph [bgcolor=transparent];\n"; |
| 523 | errs() << " node [color=black,style=filled,fillcolor=lightyellow];\n"; |
| 524 | errs() << " edge [color=black];\n"; |
| 525 | for (size_t I = 0; I < Comparisons_.size(); ++I) { |
| 526 | const auto &Comparison = Comparisons_[I]; |
| 527 | errs() << " \"" << I << "\" [label=\"%" |
| 528 | << Comparison.Lhs().Base()->getName() << " + " |
| 529 | << Comparison.Lhs().Offset << " == %" |
| 530 | << Comparison.Rhs().Base()->getName() << " + " |
| 531 | << Comparison.Rhs().Offset << " (" << (Comparison.SizeBits() / 8) |
| 532 | << " bytes)\"];\n"; |
| 533 | const Value *const Val = Phi_.getIncomingValueForBlock(Comparison.BB); |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 534 | if (I > 0) errs() << " \"" << (I - 1) << "\" -> \"" << I << "\";\n"; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 535 | errs() << " \"" << I << "\" -> \"Phi\" [label=\"" << *Val << "\"];\n"; |
| 536 | } |
| 537 | errs() << " \"Phi\" [label=\"Phi\"];\n"; |
| 538 | errs() << "}\n\n"; |
| 539 | } |
| 540 | #endif // MERGEICMPS_DOT_ON |
| 541 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 542 | namespace { |
| 543 | |
| 544 | // A class to compute the name of a set of merged basic blocks. |
| 545 | // This is optimized for the common case of no block names. |
| 546 | class MergedBlockName { |
| 547 | // Storage for the uncommon case of several named blocks. |
| 548 | SmallString<16> Scratch; |
| 549 | |
| 550 | public: |
| 551 | explicit MergedBlockName(ArrayRef<BCECmpBlock> Comparisons) |
| 552 | : Name(makeName(Comparisons)) {} |
| 553 | const StringRef Name; |
| 554 | |
| 555 | private: |
| 556 | StringRef makeName(ArrayRef<BCECmpBlock> Comparisons) { |
| 557 | assert(!Comparisons.empty() && "no basic block"); |
| 558 | // Fast path: only one block, or no names at all. |
| 559 | if (Comparisons.size() == 1) |
| 560 | return Comparisons[0].BB->getName(); |
| 561 | const int size = std::accumulate(Comparisons.begin(), Comparisons.end(), 0, |
| 562 | [](int i, const BCECmpBlock &Cmp) { |
| 563 | return i + Cmp.BB->getName().size(); |
| 564 | }); |
| 565 | if (size == 0) |
| 566 | return StringRef("", 0); |
| 567 | |
| 568 | // Slow path: at least two blocks, at least one block with a name. |
| 569 | Scratch.clear(); |
| 570 | // We'll have `size` bytes for name and `Comparisons.size() - 1` bytes for |
| 571 | // separators. |
| 572 | Scratch.reserve(size + Comparisons.size() - 1); |
| 573 | const auto append = [this](StringRef str) { |
| 574 | Scratch.append(str.begin(), str.end()); |
| 575 | }; |
| 576 | append(Comparisons[0].BB->getName()); |
| 577 | for (int I = 1, E = Comparisons.size(); I < E; ++I) { |
| 578 | const BasicBlock *const BB = Comparisons[I].BB; |
| 579 | if (!BB->getName().empty()) { |
| 580 | append("+"); |
| 581 | append(BB->getName()); |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 582 | } |
| Clement Courbet | c4fdd71 | 2019-05-16 06:18:02 +0000 | [diff] [blame] | 583 | } |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 584 | return StringRef(Scratch); |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 585 | } |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 586 | }; |
| 587 | } // namespace |
| Clement Courbet | c4fdd71 | 2019-05-16 06:18:02 +0000 | [diff] [blame] | 588 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 589 | // Merges the given contiguous comparison blocks into one memcmp block. |
| 590 | static BasicBlock *mergeComparisons(ArrayRef<BCECmpBlock> Comparisons, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 591 | BasicBlock *const InsertBefore, |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 592 | BasicBlock *const NextCmpBlock, |
| 593 | PHINode &Phi, |
| 594 | const TargetLibraryInfo *const TLI, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 595 | AliasAnalysis *AA, DomTreeUpdater &DTU) { |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 596 | assert(!Comparisons.empty() && "merging zero comparisons"); |
| 597 | LLVMContext &Context = NextCmpBlock->getContext(); |
| 598 | const BCECmpBlock &FirstCmp = Comparisons[0]; |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 599 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 600 | // Create a new cmp block before next cmp block. |
| 601 | BasicBlock *const BB = |
| 602 | BasicBlock::Create(Context, MergedBlockName(Comparisons).Name, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 603 | NextCmpBlock->getParent(), InsertBefore); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 604 | IRBuilder<> Builder(BB); |
| 605 | // Add the GEPs from the first BCECmpBlock. |
| 606 | Value *const Lhs = Builder.Insert(FirstCmp.Lhs().GEP->clone()); |
| 607 | Value *const Rhs = Builder.Insert(FirstCmp.Rhs().GEP->clone()); |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 608 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 609 | Value *IsEqual = nullptr; |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 610 | LLVM_DEBUG(dbgs() << "Merging " << Comparisons.size() << " comparisons -> " |
| 611 | << BB->getName() << "\n"); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 612 | if (Comparisons.size() == 1) { |
| 613 | LLVM_DEBUG(dbgs() << "Only one comparison, updating branches\n"); |
| 614 | Value *const LhsLoad = |
| 615 | Builder.CreateLoad(FirstCmp.Lhs().LoadI->getType(), Lhs); |
| 616 | Value *const RhsLoad = |
| 617 | Builder.CreateLoad(FirstCmp.Rhs().LoadI->getType(), Rhs); |
| 618 | // There are no blocks to merge, just do the comparison. |
| 619 | IsEqual = Builder.CreateICmpEQ(LhsLoad, RhsLoad); |
| 620 | } else { |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 621 | // If there is one block that requires splitting, we do it now, i.e. |
| 622 | // just before we know we will collapse the chain. The instructions |
| 623 | // can be executed before any of the instructions in the chain. |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 624 | const auto ToSplit = |
| 625 | std::find_if(Comparisons.begin(), Comparisons.end(), |
| 626 | [](const BCECmpBlock &B) { return B.RequireSplit; }); |
| 627 | if (ToSplit != Comparisons.end()) { |
| 628 | LLVM_DEBUG(dbgs() << "Splitting non_BCE work to header\n"); |
| 629 | ToSplit->split(BB, AA); |
| 630 | } |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 631 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 632 | const unsigned TotalSizeBits = std::accumulate( |
| 633 | Comparisons.begin(), Comparisons.end(), 0u, |
| 634 | [](int Size, const BCECmpBlock &C) { return Size + C.SizeBits(); }); |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 635 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 636 | // Create memcmp() == 0. |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 637 | const auto &DL = Phi.getModule()->getDataLayout(); |
| 638 | Value *const MemCmpCall = emitMemCmp( |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 639 | Lhs, Rhs, |
| 640 | ConstantInt::get(DL.getIntPtrType(Context), TotalSizeBits / 8), Builder, |
| 641 | DL, TLI); |
| 642 | IsEqual = Builder.CreateICmpEQ( |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 643 | MemCmpCall, ConstantInt::get(Type::getInt32Ty(Context), 0)); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 644 | } |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 645 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 646 | BasicBlock *const PhiBB = Phi.getParent(); |
| 647 | // Add a branch to the next basic block in the chain. |
| 648 | if (NextCmpBlock == PhiBB) { |
| 649 | // Continue to phi, passing it the comparison result. |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 650 | Builder.CreateBr(PhiBB); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 651 | Phi.addIncoming(IsEqual, BB); |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 652 | DTU.applyUpdates({{DominatorTree::Insert, BB, PhiBB}}); |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 653 | } else { |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 654 | // Continue to next block if equal, exit to phi else. |
| 655 | Builder.CreateCondBr(IsEqual, NextCmpBlock, PhiBB); |
| 656 | Phi.addIncoming(ConstantInt::getFalse(Context), BB); |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 657 | DTU.applyUpdates({{DominatorTree::Insert, BB, NextCmpBlock}, |
| 658 | {DominatorTree::Insert, BB, PhiBB}}); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 659 | } |
| 660 | return BB; |
| 661 | } |
| 662 | |
| 663 | bool BCECmpChain::simplify(const TargetLibraryInfo *const TLI, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 664 | AliasAnalysis *AA, DomTreeUpdater &DTU) { |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 665 | assert(Comparisons_.size() >= 2 && "simplifying trivial BCECmpChain"); |
| 666 | // First pass to check if there is at least one merge. If not, we don't do |
| 667 | // anything and we keep analysis passes intact. |
| 668 | const auto AtLeastOneMerged = [this]() { |
| 669 | for (size_t I = 1; I < Comparisons_.size(); ++I) { |
| 670 | if (IsContiguous(Comparisons_[I - 1], Comparisons_[I])) |
| 671 | return true; |
| 672 | } |
| 673 | return false; |
| 674 | }; |
| 675 | if (!AtLeastOneMerged()) |
| 676 | return false; |
| 677 | |
| Clement Courbet | 90900fb | 2019-05-17 12:07:51 +0000 | [diff] [blame] | 678 | LLVM_DEBUG(dbgs() << "Simplifying comparison chain starting at block " |
| 679 | << EntryBlock_->getName() << "\n"); |
| 680 | |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 681 | // Effectively merge blocks. We go in the reverse direction from the phi block |
| 682 | // so that the next block is always available to branch to. |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 683 | const auto mergeRange = [this, TLI, AA, &DTU](int I, int Num, |
| 684 | BasicBlock *InsertBefore, |
| 685 | BasicBlock *Next) { |
| 686 | return mergeComparisons(makeArrayRef(Comparisons_).slice(I, Num), |
| 687 | InsertBefore, Next, Phi_, TLI, AA, DTU); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 688 | }; |
| 689 | int NumMerged = 1; |
| 690 | BasicBlock *NextCmpBlock = Phi_.getParent(); |
| 691 | for (int I = static_cast<int>(Comparisons_.size()) - 2; I >= 0; --I) { |
| 692 | if (IsContiguous(Comparisons_[I], Comparisons_[I + 1])) { |
| Clement Courbet | 90900fb | 2019-05-17 12:07:51 +0000 | [diff] [blame] | 693 | LLVM_DEBUG(dbgs() << "Merging block " << Comparisons_[I].BB->getName() |
| 694 | << " into " << Comparisons_[I + 1].BB->getName() |
| 695 | << "\n"); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 696 | ++NumMerged; |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 697 | } else { |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 698 | NextCmpBlock = mergeRange(I + 1, NumMerged, NextCmpBlock, NextCmpBlock); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 699 | NumMerged = 1; |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 702 | // Insert the entry block for the new chain before the old entry block. |
| 703 | // If the old entry block was the function entry, this ensures that the new |
| 704 | // entry can become the function entry. |
| 705 | NextCmpBlock = mergeRange(0, NumMerged, EntryBlock_, NextCmpBlock); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 706 | |
| 707 | // Replace the original cmp chain with the new cmp chain by pointing all |
| 708 | // predecessors of EntryBlock_ to NextCmpBlock instead. This makes all cmp |
| 709 | // blocks in the old chain unreachable. |
| 710 | while (!pred_empty(EntryBlock_)) { |
| 711 | BasicBlock* const Pred = *pred_begin(EntryBlock_); |
| Clement Courbet | 90900fb | 2019-05-17 12:07:51 +0000 | [diff] [blame] | 712 | LLVM_DEBUG(dbgs() << "Updating jump into old chain from " << Pred->getName() |
| 713 | << "\n"); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 714 | Pred->getTerminator()->replaceUsesOfWith(EntryBlock_, NextCmpBlock); |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 715 | DTU.applyUpdates({{DominatorTree::Delete, Pred, EntryBlock_}, |
| 716 | {DominatorTree::Insert, Pred, NextCmpBlock}}); |
| 717 | } |
| 718 | |
| 719 | // If the old cmp chain was the function entry, we need to update the function |
| 720 | // entry. |
| 721 | const bool ChainEntryIsFnEntry = |
| 722 | (EntryBlock_ == &EntryBlock_->getParent()->getEntryBlock()); |
| 723 | if (ChainEntryIsFnEntry && DTU.hasDomTree()) { |
| 724 | LLVM_DEBUG(dbgs() << "Changing function entry from " |
| 725 | << EntryBlock_->getName() << " to " |
| 726 | << NextCmpBlock->getName() << "\n"); |
| 727 | DTU.getDomTree().setNewRoot(NextCmpBlock); |
| 728 | DTU.applyUpdates({{DominatorTree::Delete, NextCmpBlock, EntryBlock_}}); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 729 | } |
| 730 | EntryBlock_ = nullptr; |
| 731 | |
| 732 | // Delete merged blocks. This also removes incoming values in phi. |
| 733 | SmallVector<BasicBlock *, 16> DeadBlocks; |
| 734 | for (auto &Cmp : Comparisons_) { |
| Clement Courbet | 90900fb | 2019-05-17 12:07:51 +0000 | [diff] [blame] | 735 | LLVM_DEBUG(dbgs() << "Deleting merged block " << Cmp.BB->getName() << "\n"); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 736 | DeadBlocks.push_back(Cmp.BB); |
| 737 | } |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 738 | DeleteDeadBlocks(DeadBlocks, &DTU); |
| Clement Courbet | 632dfdd | 2019-05-17 09:43:45 +0000 | [diff] [blame] | 739 | |
| 740 | Comparisons_.clear(); |
| 741 | return true; |
| Nico Weber | d764e7c | 2019-05-17 00:43:53 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 744 | std::vector<BasicBlock *> getOrderedBlocks(PHINode &Phi, |
| 745 | BasicBlock *const LastBlock, |
| 746 | int NumBlocks) { |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 747 | // Walk up from the last block to find other blocks. |
| 748 | std::vector<BasicBlock *> Blocks(NumBlocks); |
| Clement Courbet | c2109c8 | 2018-02-06 09:14:00 +0000 | [diff] [blame] | 749 | assert(LastBlock && "invalid last block"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 750 | BasicBlock *CurBlock = LastBlock; |
| 751 | for (int BlockIndex = NumBlocks - 1; BlockIndex > 0; --BlockIndex) { |
| 752 | if (CurBlock->hasAddressTaken()) { |
| 753 | // Somebody is jumping to the block through an address, all bets are |
| 754 | // off. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 755 | LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex |
| 756 | << " has its address taken\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 757 | return {}; |
| 758 | } |
| 759 | Blocks[BlockIndex] = CurBlock; |
| 760 | auto *SinglePredecessor = CurBlock->getSinglePredecessor(); |
| 761 | if (!SinglePredecessor) { |
| 762 | // The block has two or more predecessors. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 763 | LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex |
| 764 | << " has two or more predecessors\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 765 | return {}; |
| 766 | } |
| 767 | if (Phi.getBasicBlockIndex(SinglePredecessor) < 0) { |
| 768 | // The block does not link back to the phi. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 769 | LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex |
| 770 | << " does not link back to the phi\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 771 | return {}; |
| 772 | } |
| 773 | CurBlock = SinglePredecessor; |
| 774 | } |
| 775 | Blocks[0] = CurBlock; |
| 776 | return Blocks; |
| 777 | } |
| 778 | |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 779 | bool processPhi(PHINode &Phi, const TargetLibraryInfo *const TLI, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 780 | AliasAnalysis *AA, DomTreeUpdater &DTU) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 781 | LLVM_DEBUG(dbgs() << "processPhi()\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 782 | if (Phi.getNumIncomingValues() <= 1) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 783 | LLVM_DEBUG(dbgs() << "skip: only one incoming value in phi\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 784 | return false; |
| 785 | } |
| 786 | // We are looking for something that has the following structure: |
| 787 | // bb1 --eq--> bb2 --eq--> bb3 --eq--> bb4 --+ |
| 788 | // \ \ \ \ |
| 789 | // ne ne ne \ |
| 790 | // \ \ \ v |
| 791 | // +------------+-----------+----------> bb_phi |
| 792 | // |
| 793 | // - The last basic block (bb4 here) must branch unconditionally to bb_phi. |
| 794 | // It's the only block that contributes a non-constant value to the Phi. |
| 795 | // - All other blocks (b1, b2, b3) must have exactly two successors, one of |
| Hiroshi Inoue | d24ddcd | 2018-01-19 10:55:29 +0000 | [diff] [blame] | 796 | // them being the phi block. |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 797 | // - All intermediate blocks (bb2, bb3) must have only one predecessor. |
| 798 | // - Blocks cannot do other work besides the comparison, see doesOtherWork() |
| 799 | |
| 800 | // The blocks are not necessarily ordered in the phi, so we start from the |
| 801 | // last block and reconstruct the order. |
| 802 | BasicBlock *LastBlock = nullptr; |
| 803 | for (unsigned I = 0; I < Phi.getNumIncomingValues(); ++I) { |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 804 | if (isa<ConstantInt>(Phi.getIncomingValue(I))) continue; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 805 | if (LastBlock) { |
| 806 | // There are several non-constant values. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 807 | LLVM_DEBUG(dbgs() << "skip: several non-constant values\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 808 | return false; |
| 809 | } |
| Xin Tong | 8ba674e | 2018-02-28 12:08:00 +0000 | [diff] [blame] | 810 | if (!isa<ICmpInst>(Phi.getIncomingValue(I)) || |
| 811 | cast<ICmpInst>(Phi.getIncomingValue(I))->getParent() != |
| 812 | Phi.getIncomingBlock(I)) { |
| 813 | // Non-constant incoming value is not from a cmp instruction or not |
| 814 | // produced by the last block. We could end up processing the value |
| 815 | // producing block more than once. |
| 816 | // |
| 817 | // This is an uncommon case, so we bail. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 818 | LLVM_DEBUG( |
| Xin Tong | 8ba674e | 2018-02-28 12:08:00 +0000 | [diff] [blame] | 819 | dbgs() |
| 820 | << "skip: non-constant value not from cmp or not from last block.\n"); |
| 821 | return false; |
| 822 | } |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 823 | LastBlock = Phi.getIncomingBlock(I); |
| 824 | } |
| 825 | if (!LastBlock) { |
| 826 | // There is no non-constant block. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 827 | LLVM_DEBUG(dbgs() << "skip: no non-constant block\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 828 | return false; |
| 829 | } |
| 830 | if (LastBlock->getSingleSuccessor() != Phi.getParent()) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 831 | LLVM_DEBUG(dbgs() << "skip: last block non-phi successor\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 832 | return false; |
| 833 | } |
| 834 | |
| 835 | const auto Blocks = |
| 836 | getOrderedBlocks(Phi, LastBlock, Phi.getNumIncomingValues()); |
| Clement Courbet | 98eaa88 | 2017-10-04 15:13:52 +0000 | [diff] [blame] | 837 | if (Blocks.empty()) return false; |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 838 | BCECmpChain CmpChain(Blocks, Phi, AA); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 839 | |
| 840 | if (CmpChain.size() < 2) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 841 | LLVM_DEBUG(dbgs() << "skip: only one compare block\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 845 | return CmpChain.simplify(TLI, AA, DTU); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | class MergeICmps : public FunctionPass { |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 849 | public: |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 850 | static char ID; |
| 851 | |
| 852 | MergeICmps() : FunctionPass(ID) { |
| 853 | initializeMergeICmpsPass(*PassRegistry::getPassRegistry()); |
| 854 | } |
| 855 | |
| 856 | bool runOnFunction(Function &F) override { |
| 857 | if (skipFunction(F)) return false; |
| 858 | const auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 859 | const auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 860 | // MergeICmps does not need the DominatorTree, but we update it if it's |
| 861 | // already available. |
| 862 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 863 | DomTreeUpdater DTU(DTWP ? &DTWP->getDomTree() : nullptr, |
| 864 | /*PostDominatorTree*/ nullptr, |
| 865 | DomTreeUpdater::UpdateStrategy::Eager); |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 866 | AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 867 | auto PA = runImpl(F, &TLI, &TTI, AA, DTU); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 868 | return !PA.areAllPreserved(); |
| 869 | } |
| 870 | |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 871 | private: |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 872 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 873 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 874 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 875 | AU.addRequired<AAResultsWrapperPass>(); |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 876 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 877 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 880 | PreservedAnalyses runImpl(Function &F, const TargetLibraryInfo *TLI, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 881 | const TargetTransformInfo *TTI, AliasAnalysis *AA, |
| 882 | DomTreeUpdater &DTU); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 883 | }; |
| 884 | |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 885 | PreservedAnalyses MergeICmps::runImpl(Function &F, const TargetLibraryInfo *TLI, |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 886 | const TargetTransformInfo *TTI, |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 887 | AliasAnalysis *AA, DomTreeUpdater &DTU) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 888 | LLVM_DEBUG(dbgs() << "MergeICmpsPass: " << F.getName() << "\n"); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 889 | |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 890 | // We only try merging comparisons if the target wants to expand memcmp later. |
| 891 | // The rationale is to avoid turning small chains into memcmp calls. |
| Clement Courbet | b2c3eb8 | 2017-10-30 14:19:33 +0000 | [diff] [blame] | 892 | if (!TTI->enableMemCmpExpansion(true)) return PreservedAnalyses::all(); |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 893 | |
| Benjamin Kramer | a76b64f | 2018-05-19 12:51:59 +0000 | [diff] [blame] | 894 | // If we don't have memcmp avaiable we can't emit calls to it. |
| 895 | if (!TLI->has(LibFunc_memcmp)) |
| 896 | return PreservedAnalyses::all(); |
| 897 | |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 898 | bool MadeChange = false; |
| 899 | |
| 900 | for (auto BBIt = ++F.begin(); BBIt != F.end(); ++BBIt) { |
| 901 | // A Phi operation is always first in a basic block. |
| 902 | if (auto *const Phi = dyn_cast<PHINode>(&*BBIt->begin())) |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 903 | MadeChange |= processPhi(*Phi, TLI, AA, DTU); |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| Clement Courbet | a95d95d | 2019-05-21 11:02:23 +0000 | [diff] [blame] | 906 | if (!MadeChange) |
| 907 | return PreservedAnalyses::all(); |
| 908 | PreservedAnalyses PA; |
| 909 | PA.preserve<GlobalsAA>(); |
| 910 | PA.preserve<DominatorTreeAnalysis>(); |
| 911 | return PA; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 914 | } // namespace |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 915 | |
| Eugene Zelenko | 5c2aece | 2017-10-26 01:25:14 +0000 | [diff] [blame] | 916 | char MergeICmps::ID = 0; |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 917 | INITIALIZE_PASS_BEGIN(MergeICmps, "mergeicmps", |
| 918 | "Merge contiguous icmps into a memcmp", false, false) |
| 919 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| Clement Courbet | e2e8a5c | 2017-10-10 08:00:45 +0000 | [diff] [blame] | 920 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
| Christy Lee | e943748 | 2018-09-24 20:47:12 +0000 | [diff] [blame] | 921 | INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) |
| Clement Courbet | 65130e2 | 2017-09-01 10:56:34 +0000 | [diff] [blame] | 922 | INITIALIZE_PASS_END(MergeICmps, "mergeicmps", |
| 923 | "Merge contiguous icmps into a memcmp", false, false) |
| 924 | |
| 925 | Pass *llvm::createMergeICmpsPass() { return new MergeICmps(); } |