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