xref: /llvm-project/llvm/lib/Transforms/Scalar/MergeICmps.cpp (revision 6603fc0e7be3e182ec120de8de7f9b4bb5cb905b)
1 //===- MergeICmps.cpp - Optimize chains of integer comparisons ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass turns chains of integer comparisons into memcmp (the memcmp is
11 // later typically inlined as a chain of efficient hardware comparisons). This
12 // typically benefits c++ member or nonmember operator==().
13 //
14 // The basic idea is to replace a larger chain of integer comparisons loaded
15 // from contiguous memory locations into a smaller chain of such integer
16 // comparisons. Benefits are double:
17 //  - There are less jumps, and therefore less opportunities for mispredictions
18 //    and I-cache misses.
19 //  - Code size is smaller, both because jumps are removed and because the
20 //    encoding of a 2*n byte compare is smaller than that of two n-byte
21 //    compares.
22 
23 //===----------------------------------------------------------------------===//
24 
25 #include <algorithm>
26 #include <numeric>
27 #include <utility>
28 #include <vector>
29 #include "llvm/ADT/APSInt.h"
30 #include "llvm/Analysis/Loads.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/IntrinsicInst.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Transforms/Scalar.h"
36 #include "llvm/Transforms/Utils/BuildLibCalls.h"
37 
38 using namespace llvm;
39 
40 namespace {
41 
42 #define DEBUG_TYPE "mergeicmps"
43 
44 #define MERGEICMPS_DOT_ON
45 
46 // A BCE atom.
47 struct BCEAtom {
48   BCEAtom() : GEP(nullptr), LoadI(nullptr), Offset() {}
49 
50   const Value *Base() const { return GEP ? GEP->getPointerOperand() : nullptr; }
51 
52   bool operator<(const BCEAtom &O) const {
53     return Base() == O.Base() ? Offset.slt(O.Offset) : Base() < O.Base();
54   }
55 
56   GetElementPtrInst *GEP;
57   LoadInst *LoadI;
58   APInt Offset;
59 };
60 
61 // If this value is a load from a constant offset w.r.t. a base address, and
62 // there are no othe rusers of the load or address, returns the base address and
63 // the offset.
64 BCEAtom visitICmpLoadOperand(Value *const Val) {
65   BCEAtom Result;
66   if (auto *const LoadI = dyn_cast<LoadInst>(Val)) {
67     DEBUG(dbgs() << "load\n");
68     if (LoadI->isUsedOutsideOfBlock(LoadI->getParent())) {
69       DEBUG(dbgs() << "used outside of block\n");
70       return {};
71     }
72     if (LoadI->isVolatile()) {
73       DEBUG(dbgs() << "volatile\n");
74       return {};
75     }
76     Value *const Addr = LoadI->getOperand(0);
77     if (auto *const GEP = dyn_cast<GetElementPtrInst>(Addr)) {
78       DEBUG(dbgs() << "GEP\n");
79       if (LoadI->isUsedOutsideOfBlock(LoadI->getParent())) {
80         DEBUG(dbgs() << "used outside of block\n");
81         return {};
82       }
83       const auto &DL = GEP->getModule()->getDataLayout();
84       if (!isDereferenceablePointer(GEP, DL)) {
85         DEBUG(dbgs() << "not dereferenceable\n");
86         // We need to make sure that we can do comparison in any order, so we
87         // require memory to be unconditionnally dereferencable.
88         return {};
89       }
90       Result.Offset = APInt(DL.getPointerTypeSizeInBits(GEP->getType()), 0);
91       if (GEP->accumulateConstantOffset(DL, Result.Offset)) {
92         Result.GEP = GEP;
93         Result.LoadI = LoadI;
94       }
95     }
96   }
97   return Result;
98 }
99 
100 // A basic block with a comparison between two BCE atoms.
101 // Note: the terminology is misleading: the comparison is symmetric, so there
102 // is no real {l/r}hs. To break the symmetry, we use the smallest atom as Lhs.
103 class BCECmpBlock {
104  public:
105   BCECmpBlock() {}
106 
107   BCECmpBlock(BCEAtom L, BCEAtom R, int SizeBits)
108       : Lhs_(L), Rhs_(R), SizeBits_(SizeBits) {
109     if (Rhs_ < Lhs_) std::swap(Rhs_, Lhs_);
110   }
111 
112   bool IsValid() const {
113     return Lhs_.Base() != nullptr && Rhs_.Base() != nullptr;
114   }
115 
116   // Assert the the block is consistent: If valid, it should also have
117   // non-null members besides Lhs_ and Rhs_.
118   void AssertConsistent() const {
119     if (IsValid()) {
120       assert(BB);
121       assert(CmpI);
122       assert(BranchI);
123     }
124   }
125 
126   const BCEAtom &Lhs() const { return Lhs_; }
127   const BCEAtom &Rhs() const { return Rhs_; }
128   int SizeBits() const { return SizeBits_; }
129 
130   // Returns true if the block does other works besides comparison.
131   bool doesOtherWork() const;
132 
133   // The basic block where this comparison happens.
134   BasicBlock *BB = nullptr;
135   // The ICMP for this comparison.
136   ICmpInst *CmpI = nullptr;
137   // The terminating branch.
138   BranchInst *BranchI = nullptr;
139 
140  private:
141   BCEAtom Lhs_;
142   BCEAtom Rhs_;
143   int SizeBits_ = 0;
144 };
145 
146 bool BCECmpBlock::doesOtherWork() const {
147   AssertConsistent();
148   // TODO(courbet): Can we allow some other things ? This is very conservative.
149   // We might be able to get away with anything does does not have any side
150   // effects outside of the basic block.
151   // Note: The GEPs and/or loads are not necessarily in the same block.
152   for (const Instruction &Inst : *BB) {
153     if (const auto *const GEP = dyn_cast<GetElementPtrInst>(&Inst)) {
154       if (!(Lhs_.GEP == GEP || Rhs_.GEP == GEP)) return true;
155     } else if (const auto *const L = dyn_cast<LoadInst>(&Inst)) {
156       if (!(Lhs_.LoadI == L || Rhs_.LoadI == L)) return true;
157     } else if (const auto *const C = dyn_cast<ICmpInst>(&Inst)) {
158       if (C != CmpI) return true;
159     } else if (const auto *const Br = dyn_cast<BranchInst>(&Inst)) {
160       if (Br != BranchI) return true;
161     } else {
162       return true;
163     }
164   }
165   return false;
166 }
167 
168 // Visit the given comparison. If this is a comparison between two valid
169 // BCE atoms, returns the comparison.
170 BCECmpBlock visitICmp(const ICmpInst *const CmpI,
171                       const ICmpInst::Predicate ExpectedPredicate) {
172   if (CmpI->getPredicate() == ExpectedPredicate) {
173     DEBUG(dbgs() << "cmp "
174                  << (ExpectedPredicate == ICmpInst::ICMP_EQ ? "eq" : "ne")
175                  << "\n");
176     auto Lhs = visitICmpLoadOperand(CmpI->getOperand(0));
177     if (!Lhs.Base()) return {};
178     auto Rhs = visitICmpLoadOperand(CmpI->getOperand(1));
179     if (!Rhs.Base()) return {};
180     return BCECmpBlock(std::move(Lhs), std::move(Rhs),
181                        CmpI->getOperand(0)->getType()->getScalarSizeInBits());
182   }
183   return {};
184 }
185 
186 // Visit the given comparison block. If this is a comparison between two valid
187 // BCE atoms, returns the comparison.
188 BCECmpBlock visitCmpBlock(Value *const Val, BasicBlock *const Block,
189                           const BasicBlock *const PhiBlock) {
190   if (Block->empty()) return {};
191   auto *const BranchI = dyn_cast<BranchInst>(Block->getTerminator());
192   if (!BranchI) return {};
193   DEBUG(dbgs() << "branch\n");
194   if (BranchI->isUnconditional()) {
195     // In this case, we expect an incoming value which is the result of the
196     // comparison. This is the last link in the chain of comparisons (note
197     // that this does not mean that this is the last incoming value, blocks
198     // can be reordered).
199     auto *const CmpI = dyn_cast<ICmpInst>(Val);
200     if (!CmpI) return {};
201     DEBUG(dbgs() << "icmp\n");
202     auto Result = visitICmp(CmpI, ICmpInst::ICMP_EQ);
203     Result.CmpI = CmpI;
204     Result.BranchI = BranchI;
205     return Result;
206   } else {
207     // In this case, we expect a constant incoming value (the comparison is
208     // chained).
209     const auto *const Const = dyn_cast<ConstantInt>(Val);
210     DEBUG(dbgs() << "const\n");
211     if (!Const->isZero()) return {};
212     DEBUG(dbgs() << "false\n");
213     auto *const CmpI = dyn_cast<ICmpInst>(BranchI->getCondition());
214     if (!CmpI) return {};
215     DEBUG(dbgs() << "icmp\n");
216     assert(BranchI->getNumSuccessors() == 2 && "expecting a cond branch");
217     BasicBlock *const FalseBlock = BranchI->getSuccessor(1);
218     auto Result = visitICmp(
219         CmpI, FalseBlock == PhiBlock ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE);
220     Result.CmpI = CmpI;
221     Result.BranchI = BranchI;
222     return Result;
223   }
224   return {};
225 }
226 
227 // A chain of comparisons.
228 class BCECmpChain {
229  public:
230   BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi);
231 
232   int size() const { return Comparisons_.size(); }
233 
234 #ifdef MERGEICMPS_DOT_ON
235   void dump() const;
236 #endif  // MERGEICMPS_DOT_ON
237 
238   bool simplify(const TargetLibraryInfo *const TLI);
239 
240  private:
241   static bool IsContiguous(const BCECmpBlock &First,
242                            const BCECmpBlock &Second) {
243     return First.Lhs().Base() == Second.Lhs().Base() &&
244            First.Rhs().Base() == Second.Rhs().Base() &&
245            First.Lhs().Offset + First.SizeBits() / 8 == Second.Lhs().Offset &&
246            First.Rhs().Offset + First.SizeBits() / 8 == Second.Rhs().Offset;
247   }
248 
249   // Merges the given comparison blocks into one memcmp block and update
250   // branches. Comparisons are assumed to be continguous. If NextBBInChain is
251   // null, the merged block will link to the phi block.
252   static void mergeComparisons(ArrayRef<BCECmpBlock> Comparisons,
253                                BasicBlock *const NextBBInChain, PHINode &Phi,
254                                const TargetLibraryInfo *const TLI);
255 
256   PHINode &Phi_;
257   std::vector<BCECmpBlock> Comparisons_;
258   // The original entry block (before sorting);
259   BasicBlock *EntryBlock_;
260 };
261 
262 BCECmpChain::BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi)
263     : Phi_(Phi) {
264   // Now look inside blocks to check for BCE comparisons.
265   std::vector<BCECmpBlock> Comparisons;
266   for (BasicBlock *Block : Blocks) {
267     BCECmpBlock Comparison = visitCmpBlock(Phi.getIncomingValueForBlock(Block),
268                                            Block, Phi.getParent());
269     Comparison.BB = Block;
270     if (!Comparison.IsValid()) {
271       DEBUG(dbgs() << "skip: not a valid BCECmpBlock\n");
272       return;
273     }
274     if (Comparison.doesOtherWork()) {
275       DEBUG(dbgs() << "block does extra work besides compare\n");
276       if (Comparisons.empty()) {  // First block.
277         // TODO(courbet): The first block can do other things, and we should
278         // split them apart in a separate block before the comparison chain.
279         // Right now we just discard it and make the chain shorter.
280         DEBUG(dbgs()
281               << "ignoring first block that does extra work besides compare\n");
282         continue;
283       }
284       // TODO(courbet): Right now we abort the whole chain. We could be
285       // merging only the blocks that don't do other work and resume the
286       // chain from there. For example:
287       //  if (a[0] == b[0]) {  // bb1
288       //    if (a[1] == b[1]) {  // bb2
289       //      some_value = 3; //bb3
290       //      if (a[2] == b[2]) { //bb3
291       //        do a ton of stuff  //bb4
292       //      }
293       //    }
294       //  }
295       //
296       // This is:
297       //
298       // bb1 --eq--> bb2 --eq--> bb3* -eq--> bb4 --+
299       //  \            \           \               \
300       //   ne           ne          ne              \
301       //    \            \           \               v
302       //     +------------+-----------+----------> bb_phi
303       //
304       // We can only merge the first two comparisons, because bb3* does
305       // "other work" (setting some_value to 3).
306       // We could still merge bb1 and bb2 though.
307       return;
308     }
309     DEBUG(dbgs() << "*Found cmp of " << Comparison.SizeBits()
310                  << " bits between " << Comparison.Lhs().Base() << " + "
311                  << Comparison.Lhs().Offset << " and "
312                  << Comparison.Rhs().Base() << " + " << Comparison.Rhs().Offset
313                  << "\n");
314     DEBUG(dbgs() << "\n");
315     Comparisons.push_back(Comparison);
316   }
317   EntryBlock_ = Comparisons[0].BB;
318   Comparisons_ = std::move(Comparisons);
319 #ifdef MERGEICMPS_DOT_ON
320   errs() << "BEFORE REORDERING:\n\n";
321   dump();
322 #endif  // MERGEICMPS_DOT_ON
323   // Reorder blocks by LHS. We can do that without changing the
324   // semantics because we are only accessing dereferencable memory.
325   std::sort(Comparisons_.begin(), Comparisons_.end(),
326             [](const BCECmpBlock &a, const BCECmpBlock &b) {
327               return a.Lhs() < b.Lhs();
328             });
329 #ifdef MERGEICMPS_DOT_ON
330   errs() << "AFTER REORDERING:\n\n";
331   dump();
332 #endif  // MERGEICMPS_DOT_ON
333 }
334 
335 #ifdef MERGEICMPS_DOT_ON
336 void BCECmpChain::dump() const {
337   errs() << "digraph dag {\n";
338   errs() << " graph [bgcolor=transparent];\n";
339   errs() << " node [color=black,style=filled,fillcolor=lightyellow];\n";
340   errs() << " edge [color=black];\n";
341   for (size_t I = 0; I < Comparisons_.size(); ++I) {
342     const auto &Comparison = Comparisons_[I];
343     errs() << " \"" << I << "\" [label=\"%"
344            << Comparison.Lhs().Base()->getName() << " + "
345            << Comparison.Lhs().Offset << " == %"
346            << Comparison.Rhs().Base()->getName() << " + "
347            << Comparison.Rhs().Offset << " (" << (Comparison.SizeBits() / 8)
348            << " bytes)\"];\n";
349     const Value *const Val = Phi_.getIncomingValueForBlock(Comparison.BB);
350     if (I > 0) errs() << " \"" << (I - 1) << "\" -> \"" << I << "\";\n";
351     errs() << " \"" << I << "\" -> \"Phi\" [label=\"" << *Val << "\"];\n";
352   }
353   errs() << " \"Phi\" [label=\"Phi\"];\n";
354   errs() << "}\n\n";
355 }
356 #endif  // MERGEICMPS_DOT_ON
357 
358 bool BCECmpChain::simplify(const TargetLibraryInfo *const TLI) {
359   // First pass to check if there is at least one merge. If not, we don't do
360   // anything and we keep analysis passes intact.
361   {
362     bool AtLeastOneMerged = false;
363     for (size_t I = 1; I < Comparisons_.size(); ++I) {
364       if (IsContiguous(Comparisons_[I - 1], Comparisons_[I])) {
365         AtLeastOneMerged = true;
366         break;
367       }
368     }
369     if (!AtLeastOneMerged) return false;
370   }
371 
372   // Remove phi references to comparison blocks, they will be rebuilt as we
373   // merge the blocks.
374   for (const auto &Comparison : Comparisons_) {
375     Phi_.removeIncomingValue(Comparison.BB, false);
376   }
377 
378   // Point the predecessors of the chain to the first comparison block (which is
379   // the new entry point).
380   if (EntryBlock_ != Comparisons_[0].BB)
381     EntryBlock_->replaceAllUsesWith(Comparisons_[0].BB);
382 
383   // Effectively merge blocks.
384   int NumMerged = 1;
385   for (size_t I = 1; I < Comparisons_.size(); ++I) {
386     if (IsContiguous(Comparisons_[I - 1], Comparisons_[I])) {
387       ++NumMerged;
388     } else {
389       // Merge all previous comparisons and start a new merge block.
390       mergeComparisons(
391           makeArrayRef(Comparisons_).slice(I - NumMerged, NumMerged),
392           Comparisons_[I].BB, Phi_, TLI);
393       NumMerged = 1;
394     }
395   }
396   mergeComparisons(makeArrayRef(Comparisons_)
397                        .slice(Comparisons_.size() - NumMerged, NumMerged),
398                    nullptr, Phi_, TLI);
399 
400   return true;
401 }
402 
403 void BCECmpChain::mergeComparisons(ArrayRef<BCECmpBlock> Comparisons,
404                                    BasicBlock *const NextBBInChain,
405                                    PHINode &Phi,
406                                    const TargetLibraryInfo *const TLI) {
407   assert(!Comparisons.empty());
408   const auto &FirstComparison = *Comparisons.begin();
409   BasicBlock *const BB = FirstComparison.BB;
410   LLVMContext &Context = BB->getContext();
411 
412   if (Comparisons.size() >= 2) {
413     DEBUG(dbgs() << "Merging " << Comparisons.size() << " comparisons\n");
414     const auto TotalSize =
415         std::accumulate(Comparisons.begin(), Comparisons.end(), 0,
416                         [](int Size, const BCECmpBlock &C) {
417                           return Size + C.SizeBits();
418                         }) /
419         8;
420 
421     // Incoming edges do not need to be updated, and both GEPs are already
422     // computing the right address, we just need to:
423     //   - replace the two loads and the icmp with the memcmp
424     //   - update the branch
425     //   - update the incoming values in the phi.
426     FirstComparison.BranchI->eraseFromParent();
427     FirstComparison.CmpI->eraseFromParent();
428     FirstComparison.Lhs().LoadI->eraseFromParent();
429     FirstComparison.Rhs().LoadI->eraseFromParent();
430 
431     IRBuilder<> Builder(BB);
432     const auto &DL = Phi.getModule()->getDataLayout();
433     Value *const MemCmpCall =
434         emitMemCmp(FirstComparison.Lhs().GEP, FirstComparison.Rhs().GEP,
435                    ConstantInt::get(DL.getIntPtrType(Context), TotalSize),
436                    Builder, DL, TLI);
437     Value *const MemCmpIsZero = Builder.CreateICmpEQ(
438         MemCmpCall, ConstantInt::get(Type::getInt32Ty(Context), 0));
439 
440     // Add a branch to the next basic block in the chain.
441     if (NextBBInChain) {
442       Builder.CreateCondBr(MemCmpIsZero, NextBBInChain, Phi.getParent());
443       Phi.addIncoming(ConstantInt::getFalse(Context), BB);
444     } else {
445       Builder.CreateBr(Phi.getParent());
446       Phi.addIncoming(MemCmpIsZero, BB);
447     }
448 
449     // Delete merged blocks.
450     for (size_t I = 1; I < Comparisons.size(); ++I) {
451       BasicBlock *CBB = Comparisons[I].BB;
452       CBB->replaceAllUsesWith(BB);
453       CBB->eraseFromParent();
454     }
455   } else {
456     assert(Comparisons.size() == 1);
457     // There are no blocks to merge, but we still need to update the branches.
458     DEBUG(dbgs() << "Only one comparison, updating branches\n");
459     if (NextBBInChain) {
460       if (FirstComparison.BranchI->isConditional()) {
461         DEBUG(dbgs() << "conditional -> conditional\n");
462         // Just update the "true" target, the "false" target should already be
463         // the phi block.
464         assert(FirstComparison.BranchI->getSuccessor(1) == Phi.getParent());
465         FirstComparison.BranchI->setSuccessor(0, NextBBInChain);
466         Phi.addIncoming(ConstantInt::getFalse(Context), BB);
467       } else {
468         DEBUG(dbgs() << "unconditional -> conditional\n");
469         // Replace the unconditional branch by a conditional one.
470         FirstComparison.BranchI->eraseFromParent();
471         IRBuilder<> Builder(BB);
472         Builder.CreateCondBr(FirstComparison.CmpI, NextBBInChain,
473                              Phi.getParent());
474         Phi.addIncoming(FirstComparison.CmpI, BB);
475       }
476     } else {
477       if (FirstComparison.BranchI->isConditional()) {
478         DEBUG(dbgs() << "conditional -> unconditional\n");
479         // Replace the conditional branch by an unconditional one.
480         FirstComparison.BranchI->eraseFromParent();
481         IRBuilder<> Builder(BB);
482         Builder.CreateBr(Phi.getParent());
483         Phi.addIncoming(FirstComparison.CmpI, BB);
484       } else {
485         DEBUG(dbgs() << "unconditional -> unconditional\n");
486         Phi.addIncoming(FirstComparison.CmpI, BB);
487       }
488     }
489   }
490 }
491 
492 std::vector<BasicBlock *> getOrderedBlocks(PHINode &Phi,
493                                            BasicBlock *const LastBlock,
494                                            int NumBlocks) {
495   // Walk up from the last block to find other blocks.
496   std::vector<BasicBlock *> Blocks(NumBlocks);
497   BasicBlock *CurBlock = LastBlock;
498   for (int BlockIndex = NumBlocks - 1; BlockIndex > 0; --BlockIndex) {
499     if (CurBlock->hasAddressTaken()) {
500       // Somebody is jumping to the block through an address, all bets are
501       // off.
502       DEBUG(dbgs() << "skip: block " << BlockIndex
503                    << " has its address taken\n");
504       return {};
505     }
506     Blocks[BlockIndex] = CurBlock;
507     auto *SinglePredecessor = CurBlock->getSinglePredecessor();
508     if (!SinglePredecessor) {
509       // The block has two or more predecessors.
510       DEBUG(dbgs() << "skip: block " << BlockIndex
511                    << " has two or more predecessors\n");
512       return {};
513     }
514     if (Phi.getBasicBlockIndex(SinglePredecessor) < 0) {
515       // The block does not link back to the phi.
516       DEBUG(dbgs() << "skip: block " << BlockIndex
517                    << " does not link back to the phi\n");
518       return {};
519     }
520     CurBlock = SinglePredecessor;
521   }
522   Blocks[0] = CurBlock;
523   return Blocks;
524 }
525 
526 bool processPhi(PHINode &Phi, const TargetLibraryInfo *const TLI) {
527   DEBUG(dbgs() << "processPhi()\n");
528   if (Phi.getNumIncomingValues() <= 1) {
529     DEBUG(dbgs() << "skip: only one incoming value in phi\n");
530     return false;
531   }
532   // We are looking for something that has the following structure:
533   //   bb1 --eq--> bb2 --eq--> bb3 --eq--> bb4 --+
534   //     \            \           \               \
535   //      ne           ne          ne              \
536   //       \            \           \               v
537   //        +------------+-----------+----------> bb_phi
538   //
539   //  - The last basic block (bb4 here) must branch unconditionally to bb_phi.
540   //    It's the only block that contributes a non-constant value to the Phi.
541   //  - All other blocks (b1, b2, b3) must have exactly two successors, one of
542   //    them being the the phi block.
543   //  - All intermediate blocks (bb2, bb3) must have only one predecessor.
544   //  - Blocks cannot do other work besides the comparison, see doesOtherWork()
545 
546   // The blocks are not necessarily ordered in the phi, so we start from the
547   // last block and reconstruct the order.
548   BasicBlock *LastBlock = nullptr;
549   for (unsigned I = 0; I < Phi.getNumIncomingValues(); ++I) {
550     if (isa<ConstantInt>(Phi.getIncomingValue(I))) continue;
551     if (LastBlock) {
552       // There are several non-constant values.
553       DEBUG(dbgs() << "skip: several non-constant values\n");
554       return false;
555     }
556     LastBlock = Phi.getIncomingBlock(I);
557   }
558   if (!LastBlock) {
559     // There is no non-constant block.
560     DEBUG(dbgs() << "skip: no non-constant block\n");
561     return false;
562   }
563   if (LastBlock->getSingleSuccessor() != Phi.getParent()) {
564     DEBUG(dbgs() << "skip: last block non-phi successor\n");
565     return false;
566   }
567 
568   const auto Blocks =
569       getOrderedBlocks(Phi, LastBlock, Phi.getNumIncomingValues());
570   if (Blocks.empty()) return false;
571   BCECmpChain CmpChain(Blocks, Phi);
572 
573   if (CmpChain.size() < 2) {
574     DEBUG(dbgs() << "skip: only one compare block\n");
575     return false;
576   }
577 
578   return CmpChain.simplify(TLI);
579 }
580 
581 class MergeICmps : public FunctionPass {
582  public:
583   static char ID;
584 
585   MergeICmps() : FunctionPass(ID) {
586     initializeMergeICmpsPass(*PassRegistry::getPassRegistry());
587   }
588 
589   bool runOnFunction(Function &F) override {
590     if (skipFunction(F)) return false;
591     const auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
592     auto PA = runImpl(F, &TLI);
593     return !PA.areAllPreserved();
594   }
595 
596  private:
597   void getAnalysisUsage(AnalysisUsage &AU) const override {
598     AU.addRequired<TargetLibraryInfoWrapperPass>();
599   }
600 
601   PreservedAnalyses runImpl(Function &F, const TargetLibraryInfo *TLI);
602 };
603 
604 PreservedAnalyses MergeICmps::runImpl(Function &F,
605                                       const TargetLibraryInfo *TLI) {
606   DEBUG(dbgs() << "MergeICmpsPass: " << F.getName() << "\n");
607 
608   bool MadeChange = false;
609 
610   for (auto BBIt = ++F.begin(); BBIt != F.end(); ++BBIt) {
611     // A Phi operation is always first in a basic block.
612     if (auto *const Phi = dyn_cast<PHINode>(&*BBIt->begin()))
613       MadeChange |= processPhi(*Phi, TLI);
614   }
615 
616   if (MadeChange) return PreservedAnalyses::none();
617   return PreservedAnalyses::all();
618 }
619 
620 }  // namespace
621 
622 char MergeICmps::ID = 0;
623 INITIALIZE_PASS_BEGIN(MergeICmps, "mergeicmps",
624                       "Merge contiguous icmps into a memcmp", false, false)
625 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
626 INITIALIZE_PASS_END(MergeICmps, "mergeicmps",
627                     "Merge contiguous icmps into a memcmp", false, false)
628 
629 Pass *llvm::createMergeICmpsPass() { return new MergeICmps(); }
630