LLVM 22.0.0git
MergeICmps.cpp
Go to the documentation of this file.
1//===- MergeICmps.cpp - Optimize chains of integer comparisons ------------===//
2//
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
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//
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
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.
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//
42//===----------------------------------------------------------------------===//
43
48#include "llvm/Analysis/Loads.h"
51#include "llvm/IR/Dominators.h"
52#include "llvm/IR/Function.h"
53#include "llvm/IR/IRBuilder.h"
54#include "llvm/IR/Instruction.h"
57#include "llvm/Pass.h"
61#include <algorithm>
62#include <numeric>
63#include <utility>
64#include <vector>
65
66using namespace llvm;
67
68#define DEBUG_TYPE "mergeicmps"
69
70namespace llvm {
72} // namespace llvm
73namespace {
74
75// A BCE atom "Binary Compare Expression Atom" represents an integer load
76// that is a constant offset from a base value, e.g. `a` or `o.c` in the example
77// at the top.
78struct BCEAtom {
79 BCEAtom() = default;
80 BCEAtom(GetElementPtrInst *GEP, LoadInst *LoadI, int BaseId, APInt Offset)
81 : GEP(GEP), LoadI(LoadI), BaseId(BaseId), Offset(std::move(Offset)) {}
82
83 BCEAtom(const BCEAtom &) = delete;
84 BCEAtom &operator=(const BCEAtom &) = delete;
85
86 BCEAtom(BCEAtom &&that) = default;
87 BCEAtom &operator=(BCEAtom &&that) {
88 if (this == &that)
89 return *this;
90 GEP = that.GEP;
91 LoadI = that.LoadI;
92 BaseId = that.BaseId;
93 Offset = std::move(that.Offset);
94 return *this;
95 }
96
97 // We want to order BCEAtoms by (Base, Offset). However we cannot use
98 // the pointer values for Base because these are non-deterministic.
99 // To make sure that the sort order is stable, we first assign to each atom
100 // base value an index based on its order of appearance in the chain of
101 // comparisons. We call this index `BaseOrdering`. For example, for:
102 // b[3] == c[2] && a[1] == d[1] && b[4] == c[3]
103 // | block 1 | | block 2 | | block 3 |
104 // b gets assigned index 0 and a index 1, because b appears as LHS in block 1,
105 // which is before block 2.
106 // We then sort by (BaseOrdering[LHS.Base()], LHS.Offset), which is stable.
107 bool operator<(const BCEAtom &O) const {
108 return BaseId != O.BaseId ? BaseId < O.BaseId : Offset.slt(O.Offset);
109 }
110
111 GetElementPtrInst *GEP = nullptr;
112 LoadInst *LoadI = nullptr;
113 unsigned BaseId = 0;
114 APInt Offset;
115};
116
117// A class that assigns increasing ids to values in the order in which they are
118// seen. See comment in `BCEAtom::operator<()``.
119class BaseIdentifier {
120public:
121 // Returns the id for value `Base`, after assigning one if `Base` has not been
122 // seen before.
123 int getBaseId(const Value *Base) {
124 assert(Base && "invalid base");
125 const auto Insertion = BaseToIndex.try_emplace(Base, Order);
126 if (Insertion.second)
127 ++Order;
128 return Insertion.first->second;
129 }
130
131private:
132 unsigned Order = 1;
133 DenseMap<const Value*, int> BaseToIndex;
134};
135} // namespace
136
137// If this value is a load from a constant offset w.r.t. a base address, and
138// there are no other users of the load or address, returns the base address and
139// the offset.
140static BCEAtom visitICmpLoadOperand(Value *const Val, BaseIdentifier &BaseId) {
141 auto *const LoadI = dyn_cast<LoadInst>(Val);
142 if (!LoadI)
143 return {};
144 LLVM_DEBUG(dbgs() << "load\n");
145 if (LoadI->isUsedOutsideOfBlock(LoadI->getParent())) {
146 LLVM_DEBUG(dbgs() << "used outside of block\n");
147 return {};
148 }
149 // Do not optimize atomic loads to non-atomic memcmp
150 if (!LoadI->isSimple()) {
151 LLVM_DEBUG(dbgs() << "volatile or atomic\n");
152 return {};
153 }
154 Value *Addr = LoadI->getOperand(0);
155 if (Addr->getType()->getPointerAddressSpace() != 0) {
156 LLVM_DEBUG(dbgs() << "from non-zero AddressSpace\n");
157 return {};
158 }
159 const auto &DL = LoadI->getDataLayout();
160 if (!isDereferenceablePointer(Addr, LoadI->getType(), DL)) {
161 LLVM_DEBUG(dbgs() << "not dereferenceable\n");
162 // We need to make sure that we can do comparison in any order, so we
163 // require memory to be unconditionally dereferenceable.
164 return {};
165 }
166
167 APInt Offset = APInt(DL.getIndexTypeSizeInBits(Addr->getType()), 0);
168 Value *Base = Addr;
169 auto *GEP = dyn_cast<GetElementPtrInst>(Addr);
170 if (GEP) {
171 LLVM_DEBUG(dbgs() << "GEP\n");
172 if (GEP->isUsedOutsideOfBlock(LoadI->getParent())) {
173 LLVM_DEBUG(dbgs() << "used outside of block\n");
174 return {};
175 }
176 if (!GEP->accumulateConstantOffset(DL, Offset))
177 return {};
178 Base = GEP->getPointerOperand();
179 }
180 return BCEAtom(GEP, LoadI, BaseId.getBaseId(Base), Offset);
181}
182
183namespace {
184// A comparison between two BCE atoms, e.g. `a == o.a` in the example at the
185// top.
186// Note: the terminology is misleading: the comparison is symmetric, so there
187// is no real {l/r}hs. What we want though is to have the same base on the
188// left (resp. right), so that we can detect consecutive loads. To ensure this
189// we put the smallest atom on the left.
190struct BCECmp {
191 BCEAtom Lhs;
192 BCEAtom Rhs;
193 int SizeBits;
194 const ICmpInst *CmpI;
195
196 BCECmp(BCEAtom L, BCEAtom R, int SizeBits, const ICmpInst *CmpI)
197 : Lhs(std::move(L)), Rhs(std::move(R)), SizeBits(SizeBits), CmpI(CmpI) {
198 if (Rhs < Lhs) std::swap(Rhs, Lhs);
199 }
200};
201
202// A basic block with a comparison between two BCE atoms.
203// The block might do extra work besides the atom comparison, in which case
204// doesOtherWork() returns true. Under some conditions, the block can be
205// split into the atom comparison part and the "other work" part
206// (see canSplit()).
207class BCECmpBlock {
208 public:
209 typedef SmallDenseSet<const Instruction *, 8> InstructionSet;
210
211 BCECmpBlock(BCECmp Cmp, BasicBlock *BB, InstructionSet BlockInsts)
212 : BB(BB), BlockInsts(std::move(BlockInsts)), Cmp(std::move(Cmp)) {}
213
214 const BCEAtom &Lhs() const { return Cmp.Lhs; }
215 const BCEAtom &Rhs() const { return Cmp.Rhs; }
216 int SizeBits() const { return Cmp.SizeBits; }
217
218 // Returns true if the block does other works besides comparison.
219 bool doesOtherWork() const;
220
221 // Returns true if the non-BCE-cmp instructions can be separated from BCE-cmp
222 // instructions in the block.
223 bool canSplit(AliasAnalysis &AA) const;
224
225 // Return true if this all the relevant instructions in the BCE-cmp-block can
226 // be sunk below this instruction. By doing this, we know we can separate the
227 // BCE-cmp-block instructions from the non-BCE-cmp-block instructions in the
228 // block.
229 bool canSinkBCECmpInst(const Instruction *, AliasAnalysis &AA) const;
230
231 // We can separate the BCE-cmp-block instructions and the non-BCE-cmp-block
232 // instructions. Split the old block and move all non-BCE-cmp-insts into the
233 // new parent block.
234 void split(BasicBlock *NewParent, AliasAnalysis &AA) const;
235
236 // The basic block where this comparison happens.
237 BasicBlock *BB;
238 // Instructions relating to the BCECmp and branch.
239 InstructionSet BlockInsts;
240 // The block requires splitting.
241 bool RequireSplit = false;
242 // Original order of this block in the chain.
243 unsigned OrigOrder = 0;
244
245private:
246 BCECmp Cmp;
247};
248} // namespace
249
250bool BCECmpBlock::canSinkBCECmpInst(const Instruction *Inst,
251 AliasAnalysis &AA) const {
252 // If this instruction may clobber the loads and is in middle of the BCE cmp
253 // block instructions, then bail for now.
254 if (Inst->mayWriteToMemory()) {
255 auto MayClobber = [&](LoadInst *LI) {
256 // If a potentially clobbering instruction comes before the load,
257 // we can still safely sink the load.
258 return (Inst->getParent() != LI->getParent() || !Inst->comesBefore(LI)) &&
260 };
261 if (MayClobber(Cmp.Lhs.LoadI) || MayClobber(Cmp.Rhs.LoadI))
262 return false;
263 }
264 // Make sure this instruction does not use any of the BCE cmp block
265 // instructions as operand.
266 return llvm::none_of(Inst->operands(), [&](const Value *Op) {
267 const Instruction *OpI = dyn_cast<Instruction>(Op);
268 return OpI && BlockInsts.contains(OpI);
269 });
270}
271
272void BCECmpBlock::split(BasicBlock *NewParent, AliasAnalysis &AA) const {
273 llvm::SmallVector<Instruction *, 4> OtherInsts;
274 for (Instruction &Inst : *BB) {
275 if (BlockInsts.count(&Inst))
276 continue;
277 assert(canSinkBCECmpInst(&Inst, AA) && "Split unsplittable block");
278 // This is a non-BCE-cmp-block instruction. And it can be separated
279 // from the BCE-cmp-block instruction.
280 OtherInsts.push_back(&Inst);
281 }
282
283 // Do the actual spliting.
284 for (Instruction *Inst : reverse(OtherInsts))
285 Inst->moveBeforePreserving(*NewParent, NewParent->begin());
286}
287
288bool BCECmpBlock::canSplit(AliasAnalysis &AA) const {
289 for (Instruction &Inst : *BB) {
290 if (!BlockInsts.count(&Inst)) {
291 if (!canSinkBCECmpInst(&Inst, AA))
292 return false;
293 }
294 }
295 return true;
296}
297
298bool BCECmpBlock::doesOtherWork() const {
299 // TODO(courbet): Can we allow some other things ? This is very conservative.
300 // We might be able to get away with anything does not have any side
301 // effects outside of the basic block.
302 // Note: The GEPs and/or loads are not necessarily in the same block.
303 for (const Instruction &Inst : *BB) {
304 if (!BlockInsts.count(&Inst))
305 return true;
306 }
307 return false;
308}
309
310// Visit the given comparison. If this is a comparison between two valid
311// BCE atoms, returns the comparison.
312static std::optional<BCECmp>
313visitICmp(const ICmpInst *const CmpI,
314 const ICmpInst::Predicate ExpectedPredicate, BaseIdentifier &BaseId) {
315 // The comparison can only be used once:
316 // - For intermediate blocks, as a branch condition.
317 // - For the final block, as an incoming value for the Phi.
318 // If there are any other uses of the comparison, we cannot merge it with
319 // other comparisons as we would create an orphan use of the value.
320 if (!CmpI->hasOneUse()) {
321 LLVM_DEBUG(dbgs() << "cmp has several uses\n");
322 return std::nullopt;
323 }
324 if (CmpI->getPredicate() != ExpectedPredicate)
325 return std::nullopt;
326 LLVM_DEBUG(dbgs() << "cmp "
327 << (ExpectedPredicate == ICmpInst::ICMP_EQ ? "eq" : "ne")
328 << "\n");
329 auto Lhs = visitICmpLoadOperand(CmpI->getOperand(0), BaseId);
330 if (!Lhs.BaseId)
331 return std::nullopt;
332 auto Rhs = visitICmpLoadOperand(CmpI->getOperand(1), BaseId);
333 if (!Rhs.BaseId)
334 return std::nullopt;
335 const auto &DL = CmpI->getDataLayout();
336 return BCECmp(std::move(Lhs), std::move(Rhs),
337 DL.getTypeSizeInBits(CmpI->getOperand(0)->getType()), CmpI);
338}
339
340// Visit the given comparison block. If this is a comparison between two valid
341// BCE atoms, returns the comparison.
342static std::optional<BCECmpBlock>
344 const BasicBlock *const PhiBlock, BaseIdentifier &BaseId) {
345 if (Block->empty())
346 return std::nullopt;
347 auto *const BranchI = dyn_cast<BranchInst>(Block->getTerminator());
348 if (!BranchI)
349 return std::nullopt;
350 LLVM_DEBUG(dbgs() << "branch\n");
351 Value *Cond;
352 ICmpInst::Predicate ExpectedPredicate;
353 if (BranchI->isUnconditional()) {
354 // In this case, we expect an incoming value which is the result of the
355 // comparison. This is the last link in the chain of comparisons (note
356 // that this does not mean that this is the last incoming value, blocks
357 // can be reordered).
358 Cond = Val;
359 ExpectedPredicate = ICmpInst::ICMP_EQ;
360 } else {
361 // In this case, we expect a constant incoming value (the comparison is
362 // chained).
363 const auto *const Const = cast<ConstantInt>(Val);
364 LLVM_DEBUG(dbgs() << "const\n");
365 if (!Const->isZero())
366 return std::nullopt;
367 LLVM_DEBUG(dbgs() << "false\n");
368 assert(BranchI->getNumSuccessors() == 2 && "expecting a cond branch");
369 BasicBlock *const FalseBlock = BranchI->getSuccessor(1);
370 Cond = BranchI->getCondition();
371 ExpectedPredicate =
372 FalseBlock == PhiBlock ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
373 }
374
375 auto *CmpI = dyn_cast<ICmpInst>(Cond);
376 if (!CmpI)
377 return std::nullopt;
378 LLVM_DEBUG(dbgs() << "icmp\n");
379
380 std::optional<BCECmp> Result = visitICmp(CmpI, ExpectedPredicate, BaseId);
381 if (!Result)
382 return std::nullopt;
383
384 BCECmpBlock::InstructionSet BlockInsts(
385 {Result->Lhs.LoadI, Result->Rhs.LoadI, Result->CmpI, BranchI});
386 if (Result->Lhs.GEP)
387 BlockInsts.insert(Result->Lhs.GEP);
388 if (Result->Rhs.GEP)
389 BlockInsts.insert(Result->Rhs.GEP);
390 return BCECmpBlock(std::move(*Result), Block, BlockInsts);
391}
392
393static inline void enqueueBlock(std::vector<BCECmpBlock> &Comparisons,
394 BCECmpBlock &&Comparison) {
395 LLVM_DEBUG(dbgs() << "Block '" << Comparison.BB->getName()
396 << "': Found cmp of " << Comparison.SizeBits()
397 << " bits between " << Comparison.Lhs().BaseId << " + "
398 << Comparison.Lhs().Offset << " and "
399 << Comparison.Rhs().BaseId << " + "
400 << Comparison.Rhs().Offset << "\n");
401 LLVM_DEBUG(dbgs() << "\n");
402 Comparison.OrigOrder = Comparisons.size();
403 Comparisons.push_back(std::move(Comparison));
404}
405
406namespace {
407// A chain of comparisons.
408class BCECmpChain {
409public:
410 using ContiguousBlocks = std::vector<BCECmpBlock>;
411
412 BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
413 AliasAnalysis &AA);
414
415 bool simplify(const TargetLibraryInfo &TLI, AliasAnalysis &AA,
416 DomTreeUpdater &DTU);
417
418 bool atLeastOneMerged() const {
419 return any_of(MergedBlocks_,
420 [](const auto &Blocks) { return Blocks.size() > 1; });
421 }
422
423private:
424 PHINode &Phi_;
425 // The list of all blocks in the chain, grouped by contiguity.
426 std::vector<ContiguousBlocks> MergedBlocks_;
427 // The original entry block (before sorting);
428 BasicBlock *EntryBlock_;
429};
430} // namespace
431
432static bool areContiguous(const BCECmpBlock &First, const BCECmpBlock &Second) {
433 return First.Lhs().BaseId == Second.Lhs().BaseId &&
434 First.Rhs().BaseId == Second.Rhs().BaseId &&
435 First.Lhs().Offset + First.SizeBits() / 8 == Second.Lhs().Offset &&
436 First.Rhs().Offset + First.SizeBits() / 8 == Second.Rhs().Offset;
437}
438
439static unsigned getMinOrigOrder(const BCECmpChain::ContiguousBlocks &Blocks) {
440 unsigned MinOrigOrder = std::numeric_limits<unsigned>::max();
441 for (const BCECmpBlock &Block : Blocks)
442 MinOrigOrder = std::min(MinOrigOrder, Block.OrigOrder);
443 return MinOrigOrder;
444}
445
446/// Given a chain of comparison blocks, groups the blocks into contiguous
447/// ranges that can be merged together into a single comparison.
448static std::vector<BCECmpChain::ContiguousBlocks>
449mergeBlocks(std::vector<BCECmpBlock> &&Blocks) {
450 std::vector<BCECmpChain::ContiguousBlocks> MergedBlocks;
451
452 // Sort to detect continuous offsets.
453 llvm::sort(Blocks,
454 [](const BCECmpBlock &LhsBlock, const BCECmpBlock &RhsBlock) {
455 return std::tie(LhsBlock.Lhs(), LhsBlock.Rhs()) <
456 std::tie(RhsBlock.Lhs(), RhsBlock.Rhs());
457 });
458
459 BCECmpChain::ContiguousBlocks *LastMergedBlock = nullptr;
460 for (BCECmpBlock &Block : Blocks) {
461 if (!LastMergedBlock || !areContiguous(LastMergedBlock->back(), Block)) {
462 MergedBlocks.emplace_back();
463 LastMergedBlock = &MergedBlocks.back();
464 } else {
465 LLVM_DEBUG(dbgs() << "Merging block " << Block.BB->getName() << " into "
466 << LastMergedBlock->back().BB->getName() << "\n");
467 }
468 LastMergedBlock->push_back(std::move(Block));
469 }
470
471 // While we allow reordering for merging, do not reorder unmerged comparisons.
472 // Doing so may introduce branch on poison.
473 llvm::sort(MergedBlocks, [](const BCECmpChain::ContiguousBlocks &LhsBlocks,
474 const BCECmpChain::ContiguousBlocks &RhsBlocks) {
475 return getMinOrigOrder(LhsBlocks) < getMinOrigOrder(RhsBlocks);
476 });
477
478 return MergedBlocks;
479}
480
481BCECmpChain::BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
482 AliasAnalysis &AA)
483 : Phi_(Phi) {
484 assert(!Blocks.empty() && "a chain should have at least one block");
485 // Now look inside blocks to check for BCE comparisons.
486 std::vector<BCECmpBlock> Comparisons;
487 BaseIdentifier BaseId;
488 for (BasicBlock *const Block : Blocks) {
489 assert(Block && "invalid block");
490 if (Block->hasAddressTaken()) {
491 LLVM_DEBUG(dbgs() << "cannot merge blocks with blockaddress\n");
492 return;
493 }
494 std::optional<BCECmpBlock> Comparison = visitCmpBlock(
495 Phi.getIncomingValueForBlock(Block), Block, Phi.getParent(), BaseId);
496 if (!Comparison) {
497 LLVM_DEBUG(dbgs() << "chain with invalid BCECmpBlock, no merge.\n");
498 return;
499 }
500 if (Comparison->doesOtherWork()) {
501 LLVM_DEBUG(dbgs() << "block '" << Comparison->BB->getName()
502 << "' does extra work besides compare\n");
503 if (Comparisons.empty()) {
504 // This is the initial block in the chain, in case this block does other
505 // work, we can try to split the block and move the irrelevant
506 // instructions to the predecessor.
507 //
508 // If this is not the initial block in the chain, splitting it wont
509 // work.
510 //
511 // As once split, there will still be instructions before the BCE cmp
512 // instructions that do other work in program order, i.e. within the
513 // chain before sorting. Unless we can abort the chain at this point
514 // and start anew.
515 //
516 // NOTE: we only handle blocks a with single predecessor for now.
517 if (Comparison->canSplit(AA)) {
519 << "Split initial block '" << Comparison->BB->getName()
520 << "' that does extra work besides compare\n");
521 Comparison->RequireSplit = true;
522 enqueueBlock(Comparisons, std::move(*Comparison));
523 } else {
525 << "ignoring initial block '" << Comparison->BB->getName()
526 << "' that does extra work besides compare\n");
527 }
528 continue;
529 }
530 // TODO(courbet): Right now we abort the whole chain. We could be
531 // merging only the blocks that don't do other work and resume the
532 // chain from there. For example:
533 // if (a[0] == b[0]) { // bb1
534 // if (a[1] == b[1]) { // bb2
535 // some_value = 3; //bb3
536 // if (a[2] == b[2]) { //bb3
537 // do a ton of stuff //bb4
538 // }
539 // }
540 // }
541 //
542 // This is:
543 //
544 // bb1 --eq--> bb2 --eq--> bb3* -eq--> bb4 --+
545 // \ \ \ \
546 // ne ne ne \
547 // \ \ \ v
548 // +------------+-----------+----------> bb_phi
549 //
550 // We can only merge the first two comparisons, because bb3* does
551 // "other work" (setting some_value to 3).
552 // We could still merge bb1 and bb2 though.
553 return;
554 }
555 enqueueBlock(Comparisons, std::move(*Comparison));
556 }
557
558 // It is possible we have no suitable comparison to merge.
559 if (Comparisons.empty()) {
560 LLVM_DEBUG(dbgs() << "chain with no BCE basic blocks, no merge\n");
561 return;
562 }
563 EntryBlock_ = Comparisons[0].BB;
564 MergedBlocks_ = mergeBlocks(std::move(Comparisons));
565}
566
567namespace {
568
569// A class to compute the name of a set of merged basic blocks.
570// This is optimized for the common case of no block names.
571class MergedBlockName {
572 // Storage for the uncommon case of several named blocks.
573 SmallString<16> Scratch;
574
575public:
576 explicit MergedBlockName(ArrayRef<BCECmpBlock> Comparisons)
577 : Name(makeName(Comparisons)) {}
578 const StringRef Name;
579
580private:
581 StringRef makeName(ArrayRef<BCECmpBlock> Comparisons) {
582 assert(!Comparisons.empty() && "no basic block");
583 // Fast path: only one block, or no names at all.
584 if (Comparisons.size() == 1)
585 return Comparisons[0].BB->getName();
586 const int size = std::accumulate(Comparisons.begin(), Comparisons.end(), 0,
587 [](int i, const BCECmpBlock &Cmp) {
588 return i + Cmp.BB->getName().size();
589 });
590 if (size == 0)
591 return StringRef("", 0);
592
593 // Slow path: at least two blocks, at least one block with a name.
594 Scratch.clear();
595 // We'll have `size` bytes for name and `Comparisons.size() - 1` bytes for
596 // separators.
597 Scratch.reserve(size + Comparisons.size() - 1);
598 const auto append = [this](StringRef str) {
599 Scratch.append(str.begin(), str.end());
600 };
601 append(Comparisons[0].BB->getName());
602 for (int I = 1, E = Comparisons.size(); I < E; ++I) {
603 const BasicBlock *const BB = Comparisons[I].BB;
604 if (!BB->getName().empty()) {
605 append("+");
606 append(BB->getName());
607 }
608 }
609 return Scratch.str();
610 }
611};
612} // namespace
613
614/// Determine the branch weights for the resulting conditional branch, resulting
615/// after merging \p Comparisons.
616static std::optional<SmallVector<uint32_t, 2>>
618 assert(!Comparisons.empty());
620 return std::nullopt;
621 if (Comparisons.size() == 1) {
623 if (!extractBranchWeights(*Comparisons[0].BB->getTerminator(), Weights))
624 return std::nullopt;
625 return Weights;
626 }
627 // The probability to go to the phi block is the disjunction of the
628 // probability to go to the phi block from the individual Comparisons. We'll
629 // swap the weights because `getDisjunctionWeights` computes the disjunction
630 // for the "true" branch, then swap back.
631 SmallVector<uint64_t, 2> Weights{0, 1};
632 // At this point, Weights encodes "0-probability" for the "true" side.
633 for (const auto &C : Comparisons) {
635 if (!extractBranchWeights(*C.BB->getTerminator(), W))
636 return std::nullopt;
637
638 std::swap(W[0], W[1]);
639 Weights = getDisjunctionWeights(Weights, W);
640 }
641 std::swap(Weights[0], Weights[1]);
642 return fitWeights(Weights);
643}
644
645// Merges the given contiguous comparison blocks into one memcmp block.
647 BasicBlock *const InsertBefore,
648 BasicBlock *const NextCmpBlock,
649 PHINode &Phi, const TargetLibraryInfo &TLI,
651 assert(!Comparisons.empty() && "merging zero comparisons");
652 LLVMContext &Context = NextCmpBlock->getContext();
653 const BCECmpBlock &FirstCmp = Comparisons[0];
654
655 // Create a new cmp block before next cmp block.
656 BasicBlock *const BB =
657 BasicBlock::Create(Context, MergedBlockName(Comparisons).Name,
658 NextCmpBlock->getParent(), InsertBefore);
659 IRBuilder<> Builder(BB);
660 // Add the GEPs from the first BCECmpBlock.
661 Value *Lhs, *Rhs;
662 if (FirstCmp.Lhs().GEP)
663 Lhs = Builder.Insert(FirstCmp.Lhs().GEP->clone());
664 else
665 Lhs = FirstCmp.Lhs().LoadI->getPointerOperand();
666 if (FirstCmp.Rhs().GEP)
667 Rhs = Builder.Insert(FirstCmp.Rhs().GEP->clone());
668 else
669 Rhs = FirstCmp.Rhs().LoadI->getPointerOperand();
670
671 Value *IsEqual = nullptr;
672 LLVM_DEBUG(dbgs() << "Merging " << Comparisons.size() << " comparisons -> "
673 << BB->getName() << "\n");
674
675 // If there is one block that requires splitting, we do it now, i.e.
676 // just before we know we will collapse the chain. The instructions
677 // can be executed before any of the instructions in the chain.
678 const auto *ToSplit = llvm::find_if(
679 Comparisons, [](const BCECmpBlock &B) { return B.RequireSplit; });
680 if (ToSplit != Comparisons.end()) {
681 LLVM_DEBUG(dbgs() << "Splitting non_BCE work to header\n");
682 ToSplit->split(BB, AA);
683 }
684
685 if (Comparisons.size() == 1) {
686 LLVM_DEBUG(dbgs() << "Only one comparison, updating branches\n");
687 // Use clone to keep the metadata
688 Instruction *const LhsLoad = Builder.Insert(FirstCmp.Lhs().LoadI->clone());
689 Instruction *const RhsLoad = Builder.Insert(FirstCmp.Rhs().LoadI->clone());
690 LhsLoad->replaceUsesOfWith(LhsLoad->getOperand(0), Lhs);
691 RhsLoad->replaceUsesOfWith(RhsLoad->getOperand(0), Rhs);
692 // There are no blocks to merge, just do the comparison.
693 // If we condition on this IsEqual, we already have its probabilities.
694 IsEqual = Builder.CreateICmpEQ(LhsLoad, RhsLoad);
695 } else {
696 const unsigned TotalSizeBits = std::accumulate(
697 Comparisons.begin(), Comparisons.end(), 0u,
698 [](int Size, const BCECmpBlock &C) { return Size + C.SizeBits(); });
699
700 // memcmp expects a 'size_t' argument and returns 'int'.
701 unsigned SizeTBits = TLI.getSizeTSize(*Phi.getModule());
702 unsigned IntBits = TLI.getIntSize();
703
704 // Create memcmp() == 0.
705 const auto &DL = Phi.getDataLayout();
706 Value *const MemCmpCall = emitMemCmp(
707 Lhs, Rhs,
708 ConstantInt::get(Builder.getIntNTy(SizeTBits), TotalSizeBits / 8),
709 Builder, DL, &TLI);
710 IsEqual = Builder.CreateICmpEQ(
711 MemCmpCall, ConstantInt::get(Builder.getIntNTy(IntBits), 0));
712 }
713
714 BasicBlock *const PhiBB = Phi.getParent();
715 // Add a branch to the next basic block in the chain.
716 if (NextCmpBlock == PhiBB) {
717 // Continue to phi, passing it the comparison result.
718 Builder.CreateBr(PhiBB);
719 Phi.addIncoming(IsEqual, BB);
720 DTU.applyUpdates({{DominatorTree::Insert, BB, PhiBB}});
721 } else {
722 // Continue to next block if equal, exit to phi else.
723 auto *BI = Builder.CreateCondBr(IsEqual, NextCmpBlock, PhiBB);
724 if (auto BranchWeights = computeMergedBranchWeights(Comparisons))
725 setBranchWeights(*BI, BranchWeights.value(), /*IsExpected=*/false);
726 Phi.addIncoming(ConstantInt::getFalse(Context), BB);
727 DTU.applyUpdates({{DominatorTree::Insert, BB, NextCmpBlock},
728 {DominatorTree::Insert, BB, PhiBB}});
729 }
730 return BB;
731}
732
733bool BCECmpChain::simplify(const TargetLibraryInfo &TLI, AliasAnalysis &AA,
734 DomTreeUpdater &DTU) {
735 assert(atLeastOneMerged() && "simplifying trivial BCECmpChain");
736 LLVM_DEBUG(dbgs() << "Simplifying comparison chain starting at block "
737 << EntryBlock_->getName() << "\n");
738
739 // Effectively merge blocks. We go in the reverse direction from the phi block
740 // so that the next block is always available to branch to.
741 BasicBlock *InsertBefore = EntryBlock_;
742 BasicBlock *NextCmpBlock = Phi_.getParent();
743 for (const auto &Blocks : reverse(MergedBlocks_)) {
744 InsertBefore = NextCmpBlock = mergeComparisons(
745 Blocks, InsertBefore, NextCmpBlock, Phi_, TLI, AA, DTU);
746 }
747
748 // Replace the original cmp chain with the new cmp chain by pointing all
749 // predecessors of EntryBlock_ to NextCmpBlock instead. This makes all cmp
750 // blocks in the old chain unreachable.
751 while (!pred_empty(EntryBlock_)) {
752 BasicBlock* const Pred = *pred_begin(EntryBlock_);
753 LLVM_DEBUG(dbgs() << "Updating jump into old chain from " << Pred->getName()
754 << "\n");
755 Pred->getTerminator()->replaceUsesOfWith(EntryBlock_, NextCmpBlock);
756 DTU.applyUpdates({{DominatorTree::Delete, Pred, EntryBlock_},
757 {DominatorTree::Insert, Pred, NextCmpBlock}});
758 }
759
760 // If the old cmp chain was the function entry, we need to update the function
761 // entry.
762 const bool ChainEntryIsFnEntry = EntryBlock_->isEntryBlock();
763 if (ChainEntryIsFnEntry && DTU.hasDomTree()) {
764 LLVM_DEBUG(dbgs() << "Changing function entry from "
765 << EntryBlock_->getName() << " to "
766 << NextCmpBlock->getName() << "\n");
767 DTU.getDomTree().setNewRoot(NextCmpBlock);
768 DTU.applyUpdates({{DominatorTree::Delete, NextCmpBlock, EntryBlock_}});
769 }
770 EntryBlock_ = nullptr;
771
772 // Delete merged blocks. This also removes incoming values in phi.
773 SmallVector<BasicBlock *, 16> DeadBlocks;
774 for (const auto &Blocks : MergedBlocks_) {
775 for (const BCECmpBlock &Block : Blocks) {
776 LLVM_DEBUG(dbgs() << "Deleting merged block " << Block.BB->getName()
777 << "\n");
778 DeadBlocks.push_back(Block.BB);
779 }
780 }
781 DeleteDeadBlocks(DeadBlocks, &DTU);
782
783 MergedBlocks_.clear();
784 return true;
785}
786
787static std::vector<BasicBlock *>
788getOrderedBlocks(PHINode &Phi, BasicBlock *const LastBlock, int NumBlocks) {
789 // Walk up from the last block to find other blocks.
790 std::vector<BasicBlock *> Blocks(NumBlocks);
791 assert(LastBlock && "invalid last block");
792 BasicBlock *CurBlock = LastBlock;
793 for (int BlockIndex = NumBlocks - 1; BlockIndex > 0; --BlockIndex) {
794 if (CurBlock->hasAddressTaken()) {
795 // Somebody is jumping to the block through an address, all bets are
796 // off.
797 LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex
798 << " has its address taken\n");
799 return {};
800 }
801 Blocks[BlockIndex] = CurBlock;
802 auto *SinglePredecessor = CurBlock->getSinglePredecessor();
803 if (!SinglePredecessor) {
804 // The block has two or more predecessors.
805 LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex
806 << " has two or more predecessors\n");
807 return {};
808 }
809 if (Phi.getBasicBlockIndex(SinglePredecessor) < 0) {
810 // The block does not link back to the phi.
811 LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex
812 << " does not link back to the phi\n");
813 return {};
814 }
815 CurBlock = SinglePredecessor;
816 }
817 Blocks[0] = CurBlock;
818 return Blocks;
819}
820
821static bool processPhi(PHINode &Phi, const TargetLibraryInfo &TLI,
823 LLVM_DEBUG(dbgs() << "processPhi()\n");
824 if (Phi.getNumIncomingValues() <= 1) {
825 LLVM_DEBUG(dbgs() << "skip: only one incoming value in phi\n");
826 return false;
827 }
828 // We are looking for something that has the following structure:
829 // bb1 --eq--> bb2 --eq--> bb3 --eq--> bb4 --+
830 // \ \ \ \
831 // ne ne ne \
832 // \ \ \ v
833 // +------------+-----------+----------> bb_phi
834 //
835 // - The last basic block (bb4 here) must branch unconditionally to bb_phi.
836 // It's the only block that contributes a non-constant value to the Phi.
837 // - All other blocks (b1, b2, b3) must have exactly two successors, one of
838 // them being the phi block.
839 // - All intermediate blocks (bb2, bb3) must have only one predecessor.
840 // - Blocks cannot do other work besides the comparison, see doesOtherWork()
841
842 // The blocks are not necessarily ordered in the phi, so we start from the
843 // last block and reconstruct the order.
844 BasicBlock *LastBlock = nullptr;
845 for (unsigned I = 0; I < Phi.getNumIncomingValues(); ++I) {
846 if (isa<ConstantInt>(Phi.getIncomingValue(I))) continue;
847 if (LastBlock) {
848 // There are several non-constant values.
849 LLVM_DEBUG(dbgs() << "skip: several non-constant values\n");
850 return false;
851 }
852 if (!isa<ICmpInst>(Phi.getIncomingValue(I)) ||
853 cast<ICmpInst>(Phi.getIncomingValue(I))->getParent() !=
854 Phi.getIncomingBlock(I)) {
855 // Non-constant incoming value is not from a cmp instruction or not
856 // produced by the last block. We could end up processing the value
857 // producing block more than once.
858 //
859 // This is an uncommon case, so we bail.
861 dbgs()
862 << "skip: non-constant value not from cmp or not from last block.\n");
863 return false;
864 }
865 LastBlock = Phi.getIncomingBlock(I);
866 }
867 if (!LastBlock) {
868 // There is no non-constant block.
869 LLVM_DEBUG(dbgs() << "skip: no non-constant block\n");
870 return false;
871 }
872 if (LastBlock->getSingleSuccessor() != Phi.getParent()) {
873 LLVM_DEBUG(dbgs() << "skip: last block non-phi successor\n");
874 return false;
875 }
876
877 const auto Blocks =
878 getOrderedBlocks(Phi, LastBlock, Phi.getNumIncomingValues());
879 if (Blocks.empty()) return false;
880 BCECmpChain CmpChain(Blocks, Phi, AA);
881
882 if (!CmpChain.atLeastOneMerged()) {
883 LLVM_DEBUG(dbgs() << "skip: nothing merged\n");
884 return false;
885 }
886
887 return CmpChain.simplify(TLI, AA, DTU);
888}
889
890static bool runImpl(Function &F, const TargetLibraryInfo &TLI,
892 DominatorTree *DT) {
893 LLVM_DEBUG(dbgs() << "MergeICmpsLegacyPass: " << F.getName() << "\n");
894
895 // We only try merging comparisons if the target wants to expand memcmp later.
896 // The rationale is to avoid turning small chains into memcmp calls.
897 if (!TTI.enableMemCmpExpansion(F.hasOptSize(), true))
898 return false;
899
900 // If we don't have memcmp avaiable we can't emit calls to it.
901 if (!TLI.has(LibFunc_memcmp))
902 return false;
903
904 DomTreeUpdater DTU(DT, /*PostDominatorTree*/ nullptr,
905 DomTreeUpdater::UpdateStrategy::Eager);
906
907 bool MadeChange = false;
908
909 for (BasicBlock &BB : llvm::drop_begin(F)) {
910 // A Phi operation is always first in a basic block.
911 if (auto *const Phi = dyn_cast<PHINode>(&*BB.begin()))
912 MadeChange |= processPhi(*Phi, TLI, AA, DTU);
913 }
914
915 return MadeChange;
916}
917
918namespace {
919class MergeICmpsLegacyPass : public FunctionPass {
920public:
921 static char ID;
922
923 MergeICmpsLegacyPass() : FunctionPass(ID) {
925 }
926
927 bool runOnFunction(Function &F) override {
928 if (skipFunction(F)) return false;
929 const auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
930 const auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
931 // MergeICmps does not need the DominatorTree, but we update it if it's
932 // already available.
933 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
934 auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
935 return runImpl(F, TLI, TTI, AA, DTWP ? &DTWP->getDomTree() : nullptr);
936 }
937
938 private:
939 void getAnalysisUsage(AnalysisUsage &AU) const override {
940 AU.addRequired<TargetLibraryInfoWrapperPass>();
941 AU.addRequired<TargetTransformInfoWrapperPass>();
942 AU.addRequired<AAResultsWrapperPass>();
943 AU.addPreserved<GlobalsAAWrapperPass>();
944 AU.addPreserved<DominatorTreeWrapperPass>();
945 }
946};
947
948} // namespace
949
950char MergeICmpsLegacyPass::ID = 0;
951INITIALIZE_PASS_BEGIN(MergeICmpsLegacyPass, "mergeicmps",
952 "Merge contiguous icmps into a memcmp", false, false)
956INITIALIZE_PASS_END(MergeICmpsLegacyPass, "mergeicmps",
957 "Merge contiguous icmps into a memcmp", false, false)
958
959Pass *llvm::createMergeICmpsLegacyPass() { return new MergeICmpsLegacyPass(); }
960
963 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
964 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
965 auto &AA = AM.getResult<AAManager>(F);
967 const bool MadeChanges = runImpl(F, TLI, TTI, AA, DT);
968 if (!MadeChanges)
969 return PreservedAnalyses::all();
972 return PA;
973}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
static bool runImpl(Function &F, const TargetLowering &TLI, AssumptionCache *AC)
Definition ExpandFp.cpp:993
This is the interface for a simple mod/ref and alias analysis over globals.
hexagon bit simplify
Hexagon Common GEP
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static void enqueueBlock(std::vector< BCECmpBlock > &Comparisons, BCECmpBlock &&Comparison)
static std::vector< BCECmpChain::ContiguousBlocks > mergeBlocks(std::vector< BCECmpBlock > &&Blocks)
Given a chain of comparison blocks, groups the blocks into contiguous ranges that can be merged toget...
static std::optional< SmallVector< uint32_t, 2 > > computeMergedBranchWeights(ArrayRef< BCECmpBlock > Comparisons)
Determine the branch weights for the resulting conditional branch, resulting after merging Comparison...
static std::optional< BCECmpBlock > visitCmpBlock(Value *const Val, BasicBlock *const Block, const BasicBlock *const PhiBlock, BaseIdentifier &BaseId)
static bool areContiguous(const BCECmpBlock &First, const BCECmpBlock &Second)
static std::vector< BasicBlock * > getOrderedBlocks(PHINode &Phi, BasicBlock *const LastBlock, int NumBlocks)
static unsigned getMinOrigOrder(const BCECmpChain::ContiguousBlocks &Blocks)
static BCEAtom visitICmpLoadOperand(Value *const Val, BaseIdentifier &BaseId)
static std::optional< BCECmp > visitICmp(const ICmpInst *const CmpI, const ICmpInst::Predicate ExpectedPredicate, BaseIdentifier &BaseId)
static BasicBlock * mergeComparisons(ArrayRef< BCECmpBlock > Comparisons, BasicBlock *const InsertBefore, BasicBlock *const NextCmpBlock, PHINode &Phi, const TargetLibraryInfo &TLI, AliasAnalysis &AA, DomTreeUpdater &DTU)
static bool processPhi(PHINode &Phi, const TargetLibraryInfo &TLI, AliasAnalysis &AA, DomTreeUpdater &DTU)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallString class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
This pass exposes codegen information to IR-level passes.
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
Class for arbitrary precision integers.
Definition APInt.h:78
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:690
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_NE
not equal
Definition InstrTypes.h:698
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Analysis pass which computes a DominatorTree.
Definition Dominators.h:283
DomTreeNodeBase< NodeT > * setNewRoot(NodeT *BB)
Add a new node to the forward dominator tree and make it a new root.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
DomTreeT & getDomTree()
Flush DomTree updates and return DomTree.
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
bool hasDomTree() const
Returns true if it holds a DomTreeT.
This instruction compares its operands according to the predicate given to the constructor.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
LLVM_ABI void moveBeforePreserving(InstListType::iterator MovePos)
Perform a moveBefore operation, while signalling that the caller intends to preserve the original ord...
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
unsigned getSizeTSize(const Module &M) const
Returns the size of the size_t type in bits.
unsigned getIntSize() const
Get size of a C-level int or unsigned int, in bits.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
op_range operands()
Definition User.h:292
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:24
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition DenseSet.h:180
const ParentTy * getParent() const
Definition ilist_node.h:34
Abstract Attribute helper functions.
Definition Attributor.h:165
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:456
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
LLVM_ABI Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
LLVM_ABI SmallVector< uint32_t > fitWeights(ArrayRef< uint64_t > Weights)
Push the weights right to fit in uint32_t.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
LLVM_ABI Pass * createMergeICmpsLegacyPass()
TargetTransformInfo TTI
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition Loads.cpp:249
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1758
SmallVector< uint64_t, 2 > getDisjunctionWeights(const SmallVector< T1, 2 > &B1, const SmallVector< T2, 2 > &B2)
Get the branch weights of a branch conditioned on b1 || b2, where b1 and b2 are 2 booleans that are t...
cl::opt< bool > ProfcheckDisableMetadataFixes("profcheck-disable-metadata-fixes", cl::Hidden, cl::init(false), cl::desc("Disable metadata propagation fixes discovered through Issue #147390"))
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:119
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void DeleteDeadBlocks(ArrayRef< BasicBlock * > BBs, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified blocks from BB.
LLVM_ABI void initializeMergeICmpsLegacyPassPass(PassRegistry &)
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)