LLVM 23.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"
58#include <algorithm>
59#include <numeric>
60#include <utility>
61#include <vector>
62
63using namespace llvm;
64
65#define DEBUG_TYPE "mergeicmps"
66
67namespace llvm {
69} // namespace llvm
70namespace {
71
72// A BCE atom "Binary Compare Expression Atom" represents an integer load
73// that is a constant offset from a base value, e.g. `a` or `o.c` in the example
74// at the top.
75struct BCEAtom {
76 BCEAtom() = default;
77 BCEAtom(GetElementPtrInst *GEP, LoadInst *LoadI, int BaseId, APInt Offset)
78 : GEP(GEP), LoadI(LoadI), BaseId(BaseId), Offset(std::move(Offset)) {}
79
80 BCEAtom(const BCEAtom &) = delete;
81 BCEAtom &operator=(const BCEAtom &) = delete;
82
83 BCEAtom(BCEAtom &&that) = default;
84 BCEAtom &operator=(BCEAtom &&that) {
85 if (this == &that)
86 return *this;
87 GEP = that.GEP;
88 LoadI = that.LoadI;
89 BaseId = that.BaseId;
90 Offset = std::move(that.Offset);
91 return *this;
92 }
93
94 // We want to order BCEAtoms by (Base, Offset). However we cannot use
95 // the pointer values for Base because these are non-deterministic.
96 // To make sure that the sort order is stable, we first assign to each atom
97 // base value an index based on its order of appearance in the chain of
98 // comparisons. We call this index `BaseOrdering`. For example, for:
99 // b[3] == c[2] && a[1] == d[1] && b[4] == c[3]
100 // | block 1 | | block 2 | | block 3 |
101 // b gets assigned index 0 and a index 1, because b appears as LHS in block 1,
102 // which is before block 2.
103 // We then sort by (BaseOrdering[LHS.Base()], LHS.Offset), which is stable.
104 bool operator<(const BCEAtom &O) const {
105 return BaseId != O.BaseId ? BaseId < O.BaseId : Offset.slt(O.Offset);
106 }
107
108 GetElementPtrInst *GEP = nullptr;
109 LoadInst *LoadI = nullptr;
110 unsigned BaseId = 0;
111 APInt Offset;
112};
113
114// A class that assigns increasing ids to values in the order in which they are
115// seen. See comment in `BCEAtom::operator<()``.
116class BaseIdentifier {
117public:
118 // Returns the id for value `Base`, after assigning one if `Base` has not been
119 // seen before.
120 int getBaseId(const Value *Base) {
121 assert(Base && "invalid base");
122 const auto Insertion = BaseToIndex.try_emplace(Base, Order);
123 if (Insertion.second)
124 ++Order;
125 return Insertion.first->second;
126 }
127
128private:
129 unsigned Order = 1;
130 DenseMap<const Value*, int> BaseToIndex;
131};
132} // namespace
133
134// If this value is a load from a constant offset w.r.t. a base address, and
135// there are no other users of the load or address, returns the base address and
136// the offset.
137static BCEAtom visitICmpLoadOperand(Value *const Val, BaseIdentifier &BaseId) {
138 auto *const LoadI = dyn_cast<LoadInst>(Val);
139 if (!LoadI)
140 return {};
141 LLVM_DEBUG(dbgs() << "load\n");
142 if (LoadI->isUsedOutsideOfBlock(LoadI->getParent())) {
143 LLVM_DEBUG(dbgs() << "used outside of block\n");
144 return {};
145 }
146 // Do not optimize atomic loads to non-atomic memcmp
147 if (!LoadI->isSimple()) {
148 LLVM_DEBUG(dbgs() << "volatile or atomic\n");
149 return {};
150 }
151 Value *Addr = LoadI->getOperand(0);
152 if (Addr->getType()->getPointerAddressSpace() != 0) {
153 LLVM_DEBUG(dbgs() << "from non-zero AddressSpace\n");
154 return {};
155 }
156
157 // This pass only works correctly when all of the compared elements have
158 // byte-multiple sizes.
159 const auto &DL = LoadI->getDataLayout();
160 if (!DL.typeSizeEqualsStoreSize(LoadI->getType())) {
161 LLVM_DEBUG(dbgs() << "type size is not a byte multiple\n");
162 return {};
163 }
164
165 if (!isDereferenceablePointer(Addr, LoadI->getType(), 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 unconditionally dereferenceable.
169 return {};
170 }
171
172 APInt Offset = APInt(DL.getIndexTypeSizeInBits(Addr->getType()), 0);
173 Value *Base = Addr;
174 auto *GEP = dyn_cast<GetElementPtrInst>(Addr);
175 if (GEP) {
176 LLVM_DEBUG(dbgs() << "GEP\n");
177 if (GEP->isUsedOutsideOfBlock(LoadI->getParent())) {
178 LLVM_DEBUG(dbgs() << "used outside of block\n");
179 return {};
180 }
181 if (!GEP->accumulateConstantOffset(DL, Offset))
182 return {};
183 Base = GEP->getPointerOperand();
184 }
185 return BCEAtom(GEP, LoadI, BaseId.getBaseId(Base), Offset);
186}
187
188namespace {
189// A comparison between two BCE atoms, e.g. `a == o.a` in the example at the
190// top.
191// Note: the terminology is misleading: the comparison is symmetric, so there
192// is no real {l/r}hs. What we want though is to have the same base on the
193// left (resp. right), so that we can detect consecutive loads. To ensure this
194// we put the smallest atom on the left.
195struct BCECmp {
196 BCEAtom Lhs;
197 BCEAtom Rhs;
198 int SizeBits;
199 const ICmpInst *CmpI;
200
201 BCECmp(BCEAtom L, BCEAtom R, int SizeBits, const ICmpInst *CmpI)
202 : Lhs(std::move(L)), Rhs(std::move(R)), SizeBits(SizeBits), CmpI(CmpI) {
203 if (Rhs < Lhs) std::swap(Rhs, Lhs);
204 }
205};
206
207// A basic block with a comparison between two BCE atoms.
208// The block might do extra work besides the atom comparison, in which case
209// doesOtherWork() returns true. Under some conditions, the block can be
210// split into the atom comparison part and the "other work" part
211// (see canSplit()).
212class BCECmpBlock {
213 public:
214 typedef SmallDenseSet<const Instruction *, 8> InstructionSet;
215
216 BCECmpBlock(BCECmp Cmp, BasicBlock *BB, InstructionSet BlockInsts)
217 : BB(BB), BlockInsts(std::move(BlockInsts)), Cmp(std::move(Cmp)) {}
218
219 const BCEAtom &Lhs() const { return Cmp.Lhs; }
220 const BCEAtom &Rhs() const { return Cmp.Rhs; }
221 int SizeBits() const { return Cmp.SizeBits; }
222
223 // Returns true if the block does other works besides comparison.
224 bool doesOtherWork() const;
225
226 // Returns true if the non-BCE-cmp instructions can be separated from BCE-cmp
227 // instructions in the block.
228 bool canSplit(AliasAnalysis &AA) const;
229
230 // Return true if this all the relevant instructions in the BCE-cmp-block can
231 // be sunk below this instruction. By doing this, we know we can separate the
232 // BCE-cmp-block instructions from the non-BCE-cmp-block instructions in the
233 // block.
234 bool canSinkBCECmpInst(const Instruction *, AliasAnalysis &AA) const;
235
236 // We can separate the BCE-cmp-block instructions and the non-BCE-cmp-block
237 // instructions. Split the old block and move all non-BCE-cmp-insts into the
238 // new parent block.
239 void split(BasicBlock *NewParent, AliasAnalysis &AA) const;
240
241 // The basic block where this comparison happens.
242 BasicBlock *BB;
243 // Instructions relating to the BCECmp and branch.
244 InstructionSet BlockInsts;
245 // The block requires splitting.
246 bool RequireSplit = false;
247 // Original order of this block in the chain.
248 unsigned OrigOrder = 0;
249
250private:
251 BCECmp Cmp;
252};
253} // namespace
254
255bool BCECmpBlock::canSinkBCECmpInst(const Instruction *Inst,
256 AliasAnalysis &AA) const {
257 // If this instruction may clobber the loads and is in middle of the BCE cmp
258 // block instructions, then bail for now.
259 if (Inst->mayWriteToMemory()) {
260 auto MayClobber = [&](LoadInst *LI) {
261 // If a potentially clobbering instruction comes before the load,
262 // we can still safely sink the load.
263 return (Inst->getParent() != LI->getParent() || !Inst->comesBefore(LI)) &&
265 };
266 if (MayClobber(Cmp.Lhs.LoadI) || MayClobber(Cmp.Rhs.LoadI))
267 return false;
268 }
269 // Make sure this instruction does not use any of the BCE cmp block
270 // instructions as operand.
271 return llvm::none_of(Inst->operands(), [&](const Value *Op) {
272 const Instruction *OpI = dyn_cast<Instruction>(Op);
273 return OpI && BlockInsts.contains(OpI);
274 });
275}
276
277void BCECmpBlock::split(BasicBlock *NewParent, AliasAnalysis &AA) const {
278 llvm::SmallVector<Instruction *, 4> OtherInsts;
279 for (Instruction &Inst : *BB) {
280 if (BlockInsts.count(&Inst))
281 continue;
282 assert(canSinkBCECmpInst(&Inst, AA) && "Split unsplittable block");
283 // This is a non-BCE-cmp-block instruction. And it can be separated
284 // from the BCE-cmp-block instruction.
285 OtherInsts.push_back(&Inst);
286 }
287
288 // Do the actual spliting.
289 for (Instruction *Inst : reverse(OtherInsts))
290 Inst->moveBeforePreserving(*NewParent, NewParent->begin());
291}
292
293bool BCECmpBlock::canSplit(AliasAnalysis &AA) const {
294 for (Instruction &Inst : *BB) {
295 if (!BlockInsts.count(&Inst)) {
296 if (!canSinkBCECmpInst(&Inst, AA))
297 return false;
298 }
299 }
300 return true;
301}
302
303bool BCECmpBlock::doesOtherWork() const {
304 // TODO(courbet): Can we allow some other things ? This is very conservative.
305 // We might be able to get away with anything does not have any side
306 // effects outside of the basic block.
307 // Note: The GEPs and/or loads are not necessarily in the same block.
308 for (const Instruction &Inst : *BB) {
309 if (!BlockInsts.count(&Inst))
310 return true;
311 }
312 return false;
313}
314
315// Visit the given comparison. If this is a comparison between two valid
316// BCE atoms, returns the comparison.
317static std::optional<BCECmp>
318visitICmp(const ICmpInst *const CmpI,
319 const ICmpInst::Predicate ExpectedPredicate, BaseIdentifier &BaseId) {
320 // The comparison can only be used once:
321 // - For intermediate blocks, as a branch condition.
322 // - For the final block, as an incoming value for the Phi.
323 // If there are any other uses of the comparison, we cannot merge it with
324 // other comparisons as we would create an orphan use of the value.
325 if (!CmpI->hasOneUse()) {
326 LLVM_DEBUG(dbgs() << "cmp has several uses\n");
327 return std::nullopt;
328 }
329 if (CmpI->getPredicate() != ExpectedPredicate)
330 return std::nullopt;
331 LLVM_DEBUG(dbgs() << "cmp "
332 << (ExpectedPredicate == ICmpInst::ICMP_EQ ? "eq" : "ne")
333 << "\n");
334 auto Lhs = visitICmpLoadOperand(CmpI->getOperand(0), BaseId);
335 if (!Lhs.BaseId)
336 return std::nullopt;
337 auto Rhs = visitICmpLoadOperand(CmpI->getOperand(1), BaseId);
338 if (!Rhs.BaseId)
339 return std::nullopt;
340
341 const auto &DL = CmpI->getDataLayout();
342 return BCECmp(std::move(Lhs), std::move(Rhs),
343 DL.getTypeSizeInBits(CmpI->getOperand(0)->getType()), CmpI);
344}
345
346// Visit the given comparison block. If this is a comparison between two valid
347// BCE atoms, returns the comparison.
348static std::optional<BCECmpBlock>
350 const BasicBlock *const PhiBlock, BaseIdentifier &BaseId) {
351 if (Block->empty())
352 return std::nullopt;
353 auto *Term = Block->getTerminator();
354 Value *Cond;
355 ICmpInst::Predicate ExpectedPredicate;
356 if (isa<UncondBrInst>(Term)) {
357 // In this case, we expect an incoming value which is the result of the
358 // comparison. This is the last link in the chain of comparisons (note
359 // that this does not mean that this is the last incoming value, blocks
360 // can be reordered).
361 Cond = Val;
362 ExpectedPredicate = ICmpInst::ICMP_EQ;
363 } else if (auto *BranchI = dyn_cast<CondBrInst>(Term)) {
364 // In this case, we expect a constant incoming value (the comparison is
365 // chained).
366 const auto *const Const = cast<ConstantInt>(Val);
367 LLVM_DEBUG(dbgs() << "const\n");
368 if (!Const->isZero())
369 return std::nullopt;
370 LLVM_DEBUG(dbgs() << "false\n");
371 assert(BranchI->getNumSuccessors() == 2 && "expecting a cond branch");
372 BasicBlock *const FalseBlock = BranchI->getSuccessor(1);
373 Cond = BranchI->getCondition();
374 ExpectedPredicate =
375 FalseBlock == PhiBlock ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
376 } else
377 return std::nullopt;
378
379 auto *CmpI = dyn_cast<ICmpInst>(Cond);
380 if (!CmpI)
381 return std::nullopt;
382 LLVM_DEBUG(dbgs() << "icmp\n");
383
384 std::optional<BCECmp> Result = visitICmp(CmpI, ExpectedPredicate, BaseId);
385 if (!Result)
386 return std::nullopt;
387
388 BCECmpBlock::InstructionSet BlockInsts(
389 {Result->Lhs.LoadI, Result->Rhs.LoadI, Result->CmpI, Term});
390 if (Result->Lhs.GEP)
391 BlockInsts.insert(Result->Lhs.GEP);
392 if (Result->Rhs.GEP)
393 BlockInsts.insert(Result->Rhs.GEP);
394 return BCECmpBlock(std::move(*Result), Block, BlockInsts);
395}
396
397static inline void enqueueBlock(std::vector<BCECmpBlock> &Comparisons,
398 BCECmpBlock &&Comparison) {
399 LLVM_DEBUG(dbgs() << "Block '" << Comparison.BB->getName()
400 << "': Found cmp of " << Comparison.SizeBits()
401 << " bits between " << Comparison.Lhs().BaseId << " + "
402 << Comparison.Lhs().Offset << " and "
403 << Comparison.Rhs().BaseId << " + "
404 << Comparison.Rhs().Offset << "\n");
405 LLVM_DEBUG(dbgs() << "\n");
406 Comparison.OrigOrder = Comparisons.size();
407 Comparisons.push_back(std::move(Comparison));
408}
409
410namespace {
411// A chain of comparisons.
412class BCECmpChain {
413public:
414 using ContiguousBlocks = std::vector<BCECmpBlock>;
415
416 BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
417 AliasAnalysis &AA);
418
419 bool simplify(const TargetLibraryInfo &TLI, AliasAnalysis &AA,
420 DomTreeUpdater &DTU);
421
422 bool atLeastOneMerged() const {
423 return any_of(MergedBlocks_,
424 [](const auto &Blocks) { return Blocks.size() > 1; });
425 }
426
427private:
428 PHINode &Phi_;
429 // The list of all blocks in the chain, grouped by contiguity.
430 std::vector<ContiguousBlocks> MergedBlocks_;
431 // The original entry block (before sorting);
432 BasicBlock *EntryBlock_;
433};
434} // namespace
435
436static bool areContiguous(const BCECmpBlock &First, const BCECmpBlock &Second) {
437 return First.Lhs().BaseId == Second.Lhs().BaseId &&
438 First.Rhs().BaseId == Second.Rhs().BaseId &&
439 First.Lhs().Offset + First.SizeBits() / 8 == Second.Lhs().Offset &&
440 First.Rhs().Offset + First.SizeBits() / 8 == Second.Rhs().Offset;
441}
442
443static unsigned getMinOrigOrder(const BCECmpChain::ContiguousBlocks &Blocks) {
444 unsigned MinOrigOrder = std::numeric_limits<unsigned>::max();
445 for (const BCECmpBlock &Block : Blocks)
446 MinOrigOrder = std::min(MinOrigOrder, Block.OrigOrder);
447 return MinOrigOrder;
448}
449
450/// Given a chain of comparison blocks, groups the blocks into contiguous
451/// ranges that can be merged together into a single comparison.
452static std::vector<BCECmpChain::ContiguousBlocks>
453mergeBlocks(std::vector<BCECmpBlock> &&Blocks) {
454 std::vector<BCECmpChain::ContiguousBlocks> MergedBlocks;
455
456 // Sort to detect continuous offsets.
457 llvm::sort(Blocks,
458 [](const BCECmpBlock &LhsBlock, const BCECmpBlock &RhsBlock) {
459 return std::tie(LhsBlock.Lhs(), LhsBlock.Rhs()) <
460 std::tie(RhsBlock.Lhs(), RhsBlock.Rhs());
461 });
462
463 BCECmpChain::ContiguousBlocks *LastMergedBlock = nullptr;
464 for (BCECmpBlock &Block : Blocks) {
465 if (!LastMergedBlock || !areContiguous(LastMergedBlock->back(), Block)) {
466 MergedBlocks.emplace_back();
467 LastMergedBlock = &MergedBlocks.back();
468 } else {
469 LLVM_DEBUG(dbgs() << "Merging block " << Block.BB->getName() << " into "
470 << LastMergedBlock->back().BB->getName() << "\n");
471 }
472 LastMergedBlock->push_back(std::move(Block));
473 }
474
475 // While we allow reordering for merging, do not reorder unmerged comparisons.
476 // Doing so may introduce branch on poison.
477 llvm::sort(MergedBlocks, [](const BCECmpChain::ContiguousBlocks &LhsBlocks,
478 const BCECmpChain::ContiguousBlocks &RhsBlocks) {
479 return getMinOrigOrder(LhsBlocks) < getMinOrigOrder(RhsBlocks);
480 });
481
482 return MergedBlocks;
483}
484
485BCECmpChain::BCECmpChain(const std::vector<BasicBlock *> &Blocks, PHINode &Phi,
486 AliasAnalysis &AA)
487 : Phi_(Phi) {
488 assert(!Blocks.empty() && "a chain should have at least one block");
489 // Now look inside blocks to check for BCE comparisons.
490 std::vector<BCECmpBlock> Comparisons;
491 BaseIdentifier BaseId;
492 for (BasicBlock *const Block : Blocks) {
493 assert(Block && "invalid block");
494 if (Block->hasAddressTaken()) {
495 LLVM_DEBUG(dbgs() << "cannot merge blocks with blockaddress\n");
496 return;
497 }
498 std::optional<BCECmpBlock> Comparison = visitCmpBlock(
499 Phi.getIncomingValueForBlock(Block), Block, Phi.getParent(), BaseId);
500 if (!Comparison) {
501 LLVM_DEBUG(dbgs() << "chain with invalid BCECmpBlock, no merge.\n");
502 return;
503 }
504 if (Comparison->doesOtherWork()) {
505 LLVM_DEBUG(dbgs() << "block '" << Comparison->BB->getName()
506 << "' does extra work besides compare\n");
507 if (Comparisons.empty()) {
508 // This is the initial block in the chain, in case this block does other
509 // work, we can try to split the block and move the irrelevant
510 // instructions to the predecessor.
511 //
512 // If this is not the initial block in the chain, splitting it wont
513 // work.
514 //
515 // As once split, there will still be instructions before the BCE cmp
516 // instructions that do other work in program order, i.e. within the
517 // chain before sorting. Unless we can abort the chain at this point
518 // and start anew.
519 //
520 // NOTE: we only handle blocks a with single predecessor for now.
521 if (Comparison->canSplit(AA)) {
523 << "Split initial block '" << Comparison->BB->getName()
524 << "' that does extra work besides compare\n");
525 Comparison->RequireSplit = true;
526 enqueueBlock(Comparisons, std::move(*Comparison));
527 } else {
529 << "ignoring initial block '" << Comparison->BB->getName()
530 << "' that does extra work besides compare\n");
531 }
532 continue;
533 }
534 // TODO(courbet): Right now we abort the whole chain. We could be
535 // merging only the blocks that don't do other work and resume the
536 // chain from there. For example:
537 // if (a[0] == b[0]) { // bb1
538 // if (a[1] == b[1]) { // bb2
539 // some_value = 3; //bb3
540 // if (a[2] == b[2]) { //bb3
541 // do a ton of stuff //bb4
542 // }
543 // }
544 // }
545 //
546 // This is:
547 //
548 // bb1 --eq--> bb2 --eq--> bb3* -eq--> bb4 --+
549 // \ \ \ \
550 // ne ne ne \
551 // \ \ \ v
552 // +------------+-----------+----------> bb_phi
553 //
554 // We can only merge the first two comparisons, because bb3* does
555 // "other work" (setting some_value to 3).
556 // We could still merge bb1 and bb2 though.
557 return;
558 }
559 enqueueBlock(Comparisons, std::move(*Comparison));
560 }
561
562 // It is possible we have no suitable comparison to merge.
563 if (Comparisons.empty()) {
564 LLVM_DEBUG(dbgs() << "chain with no BCE basic blocks, no merge\n");
565 return;
566 }
567 EntryBlock_ = Comparisons[0].BB;
568 MergedBlocks_ = mergeBlocks(std::move(Comparisons));
569}
570
571namespace {
572
573// A class to compute the name of a set of merged basic blocks.
574// This is optimized for the common case of no block names.
575class MergedBlockName {
576 // Storage for the uncommon case of several named blocks.
577 SmallString<16> Scratch;
578
579public:
580 explicit MergedBlockName(ArrayRef<BCECmpBlock> Comparisons)
581 : Name(makeName(Comparisons)) {}
582 const StringRef Name;
583
584private:
585 StringRef makeName(ArrayRef<BCECmpBlock> Comparisons) {
586 assert(!Comparisons.empty() && "no basic block");
587 // Fast path: only one block, or no names at all.
588 if (Comparisons.size() == 1)
589 return Comparisons[0].BB->getName();
590 const int size = std::accumulate(Comparisons.begin(), Comparisons.end(), 0,
591 [](int i, const BCECmpBlock &Cmp) {
592 return i + Cmp.BB->getName().size();
593 });
594 if (size == 0)
595 return StringRef("", 0);
596
597 // Slow path: at least two blocks, at least one block with a name.
598 Scratch.clear();
599 // We'll have `size` bytes for name and `Comparisons.size() - 1` bytes for
600 // separators.
601 Scratch.reserve(size + Comparisons.size() - 1);
602 const auto append = [this](StringRef str) {
603 Scratch.append(str.begin(), str.end());
604 };
605 append(Comparisons[0].BB->getName());
606 for (int I = 1, E = Comparisons.size(); I < E; ++I) {
607 const BasicBlock *const BB = Comparisons[I].BB;
608 if (!BB->getName().empty()) {
609 append("+");
610 append(BB->getName());
611 }
612 }
613 return Scratch.str();
614 }
615};
616} // namespace
617
618/// Determine the branch weights for the resulting conditional branch, resulting
619/// after merging \p Comparisons.
620static std::optional<SmallVector<uint32_t, 2>>
622 assert(!Comparisons.empty());
624 return std::nullopt;
625 if (Comparisons.size() == 1) {
627 if (!extractBranchWeights(*Comparisons[0].BB->getTerminator(), Weights))
628 return std::nullopt;
629 return Weights;
630 }
631 // The probability to go to the phi block is the disjunction of the
632 // probability to go to the phi block from the individual Comparisons. We'll
633 // swap the weights because `getDisjunctionWeights` computes the disjunction
634 // for the "true" branch, then swap back.
635 SmallVector<uint64_t, 2> Weights{0, 1};
636 // At this point, Weights encodes "0-probability" for the "true" side.
637 for (const auto &C : Comparisons) {
639 if (!extractBranchWeights(*C.BB->getTerminator(), W))
640 return std::nullopt;
641
642 std::swap(W[0], W[1]);
643 Weights = getDisjunctionWeights(Weights, W);
644 }
645 std::swap(Weights[0], Weights[1]);
646 return fitWeights(Weights);
647}
648
649// Merges the given contiguous comparison blocks into one memcmp block.
651 BasicBlock *const InsertBefore,
652 BasicBlock *const NextCmpBlock,
653 PHINode &Phi, const TargetLibraryInfo &TLI,
655 assert(!Comparisons.empty() && "merging zero comparisons");
656 LLVMContext &Context = NextCmpBlock->getContext();
657 const BCECmpBlock &FirstCmp = Comparisons[0];
658
659 // Create a new cmp block before next cmp block.
660 BasicBlock *const BB =
661 BasicBlock::Create(Context, MergedBlockName(Comparisons).Name,
662 NextCmpBlock->getParent(), InsertBefore);
663 IRBuilder<> Builder(BB);
664 // Add the GEPs from the first BCECmpBlock.
665 Value *Lhs, *Rhs;
666 if (FirstCmp.Lhs().GEP)
667 Lhs = Builder.Insert(FirstCmp.Lhs().GEP->clone());
668 else
669 Lhs = FirstCmp.Lhs().LoadI->getPointerOperand();
670 if (FirstCmp.Rhs().GEP)
671 Rhs = Builder.Insert(FirstCmp.Rhs().GEP->clone());
672 else
673 Rhs = FirstCmp.Rhs().LoadI->getPointerOperand();
674
675 Value *IsEqual = nullptr;
676 LLVM_DEBUG(dbgs() << "Merging " << Comparisons.size() << " comparisons -> "
677 << BB->getName() << "\n");
678
679 // If there is one block that requires splitting, we do it now, i.e.
680 // just before we know we will collapse the chain. The instructions
681 // can be executed before any of the instructions in the chain.
682 const auto *ToSplit = llvm::find_if(
683 Comparisons, [](const BCECmpBlock &B) { return B.RequireSplit; });
684 if (ToSplit != Comparisons.end()) {
685 LLVM_DEBUG(dbgs() << "Splitting non_BCE work to header\n");
686 ToSplit->split(BB, AA);
687 }
688
689 if (Comparisons.size() == 1) {
690 LLVM_DEBUG(dbgs() << "Only one comparison, updating branches\n");
691 // Use clone to keep the metadata
692 Instruction *const LhsLoad = Builder.Insert(FirstCmp.Lhs().LoadI->clone());
693 Instruction *const RhsLoad = Builder.Insert(FirstCmp.Rhs().LoadI->clone());
694 LhsLoad->replaceUsesOfWith(LhsLoad->getOperand(0), Lhs);
695 RhsLoad->replaceUsesOfWith(RhsLoad->getOperand(0), Rhs);
696 // There are no blocks to merge, just do the comparison.
697 // If we condition on this IsEqual, we already have its probabilities.
698 IsEqual = Builder.CreateICmpEQ(LhsLoad, RhsLoad);
699 } else {
700 const unsigned TotalSizeBits = std::accumulate(
701 Comparisons.begin(), Comparisons.end(), 0u,
702 [](int Size, const BCECmpBlock &C) { return Size + C.SizeBits(); });
703
704 // memcmp expects a 'size_t' argument and returns 'int'.
705 unsigned SizeTBits = TLI.getSizeTSize(*Phi.getModule());
706 unsigned IntBits = TLI.getIntSize();
707
708 // Create memcmp() == 0.
709 const auto &DL = Phi.getDataLayout();
710 Value *const MemCmpCall = emitMemCmp(
711 Lhs, Rhs,
712 ConstantInt::get(Builder.getIntNTy(SizeTBits), TotalSizeBits / 8),
713 Builder, DL, &TLI);
714 IsEqual = Builder.CreateICmpEQ(
715 MemCmpCall, ConstantInt::get(Builder.getIntNTy(IntBits), 0));
716 }
717
718 BasicBlock *const PhiBB = Phi.getParent();
719 // Add a branch to the next basic block in the chain.
720 if (NextCmpBlock == PhiBB) {
721 // Continue to phi, passing it the comparison result.
722 Builder.CreateBr(PhiBB);
723 Phi.addIncoming(IsEqual, BB);
724 DTU.applyUpdates({{DominatorTree::Insert, BB, PhiBB}});
725 } else {
726 // Continue to next block if equal, exit to phi else.
727 auto *BI = Builder.CreateCondBr(IsEqual, NextCmpBlock, PhiBB);
728 if (auto BranchWeights = computeMergedBranchWeights(Comparisons))
729 setBranchWeights(*BI, BranchWeights.value(), /*IsExpected=*/false);
730 Phi.addIncoming(ConstantInt::getFalse(Context), BB);
731 DTU.applyUpdates({{DominatorTree::Insert, BB, NextCmpBlock},
732 {DominatorTree::Insert, BB, PhiBB}});
733 }
734 return BB;
735}
736
737bool BCECmpChain::simplify(const TargetLibraryInfo &TLI, AliasAnalysis &AA,
738 DomTreeUpdater &DTU) {
739 assert(atLeastOneMerged() && "simplifying trivial BCECmpChain");
740 LLVM_DEBUG(dbgs() << "Simplifying comparison chain starting at block "
741 << EntryBlock_->getName() << "\n");
742
743 // Effectively merge blocks. We go in the reverse direction from the phi block
744 // so that the next block is always available to branch to.
745 BasicBlock *InsertBefore = EntryBlock_;
746 BasicBlock *NextCmpBlock = Phi_.getParent();
747 for (const auto &Blocks : reverse(MergedBlocks_)) {
748 InsertBefore = NextCmpBlock = mergeComparisons(
749 Blocks, InsertBefore, NextCmpBlock, Phi_, TLI, AA, DTU);
750 }
751
752 // Replace the original cmp chain with the new cmp chain by pointing all
753 // predecessors of EntryBlock_ to NextCmpBlock instead. This makes all cmp
754 // blocks in the old chain unreachable.
755 while (!pred_empty(EntryBlock_)) {
756 BasicBlock* const Pred = *pred_begin(EntryBlock_);
757 LLVM_DEBUG(dbgs() << "Updating jump into old chain from " << Pred->getName()
758 << "\n");
759 Pred->getTerminator()->replaceUsesOfWith(EntryBlock_, NextCmpBlock);
760 DTU.applyUpdates({{DominatorTree::Delete, Pred, EntryBlock_},
761 {DominatorTree::Insert, Pred, NextCmpBlock}});
762 }
763
764 // If the old cmp chain was the function entry, we need to update the function
765 // entry.
766 const bool ChainEntryIsFnEntry = EntryBlock_->isEntryBlock();
767 if (ChainEntryIsFnEntry && DTU.hasDomTree()) {
768 LLVM_DEBUG(dbgs() << "Changing function entry from "
769 << EntryBlock_->getName() << " to "
770 << NextCmpBlock->getName() << "\n");
771 DTU.getDomTree().setNewRoot(NextCmpBlock);
772 DTU.applyUpdates({{DominatorTree::Delete, NextCmpBlock, EntryBlock_}});
773 }
774 EntryBlock_ = nullptr;
775
776 // Delete merged blocks. This also removes incoming values in phi.
777 SmallVector<BasicBlock *, 16> DeadBlocks;
778 for (const auto &Blocks : MergedBlocks_) {
779 for (const BCECmpBlock &Block : Blocks) {
780 LLVM_DEBUG(dbgs() << "Deleting merged block " << Block.BB->getName()
781 << "\n");
782 DeadBlocks.push_back(Block.BB);
783 }
784 }
785 DeleteDeadBlocks(DeadBlocks, &DTU);
786
787 MergedBlocks_.clear();
788 return true;
789}
790
791static std::vector<BasicBlock *>
792getOrderedBlocks(PHINode &Phi, BasicBlock *const LastBlock, int NumBlocks) {
793 // Walk up from the last block to find other blocks.
794 std::vector<BasicBlock *> Blocks(NumBlocks);
795 assert(LastBlock && "invalid last block");
796 BasicBlock *CurBlock = LastBlock;
797 for (int BlockIndex = NumBlocks - 1; BlockIndex > 0; --BlockIndex) {
798 if (CurBlock->hasAddressTaken()) {
799 // Somebody is jumping to the block through an address, all bets are
800 // off.
801 LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex
802 << " has its address taken\n");
803 return {};
804 }
805 Blocks[BlockIndex] = CurBlock;
806 auto *SinglePredecessor = CurBlock->getSinglePredecessor();
807 if (!SinglePredecessor) {
808 // The block has two or more predecessors.
809 LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex
810 << " has two or more predecessors\n");
811 return {};
812 }
813 if (Phi.getBasicBlockIndex(SinglePredecessor) < 0) {
814 // The block does not link back to the phi.
815 LLVM_DEBUG(dbgs() << "skip: block " << BlockIndex
816 << " does not link back to the phi\n");
817 return {};
818 }
819 CurBlock = SinglePredecessor;
820 }
821 Blocks[0] = CurBlock;
822 return Blocks;
823}
824
825static bool processPhi(PHINode &Phi, const TargetLibraryInfo &TLI,
827 LLVM_DEBUG(dbgs() << "processPhi()\n");
828 if (Phi.getNumIncomingValues() <= 1) {
829 LLVM_DEBUG(dbgs() << "skip: only one incoming value in phi\n");
830 return false;
831 }
832 // We are looking for something that has the following structure:
833 // bb1 --eq--> bb2 --eq--> bb3 --eq--> bb4 --+
834 // \ \ \ \
835 // ne ne ne \
836 // \ \ \ v
837 // +------------+-----------+----------> bb_phi
838 //
839 // - The last basic block (bb4 here) must branch unconditionally to bb_phi.
840 // It's the only block that contributes a non-constant value to the Phi.
841 // - All other blocks (b1, b2, b3) must have exactly two successors, one of
842 // them being the phi block.
843 // - All intermediate blocks (bb2, bb3) must have only one predecessor.
844 // - Blocks cannot do other work besides the comparison, see doesOtherWork()
845
846 // The blocks are not necessarily ordered in the phi, so we start from the
847 // last block and reconstruct the order.
848 BasicBlock *LastBlock = nullptr;
849 for (unsigned I = 0; I < Phi.getNumIncomingValues(); ++I) {
850 if (isa<ConstantInt>(Phi.getIncomingValue(I))) continue;
851 if (LastBlock) {
852 // There are several non-constant values.
853 LLVM_DEBUG(dbgs() << "skip: several non-constant values\n");
854 return false;
855 }
856 if (!isa<ICmpInst>(Phi.getIncomingValue(I)) ||
857 cast<ICmpInst>(Phi.getIncomingValue(I))->getParent() !=
858 Phi.getIncomingBlock(I)) {
859 // Non-constant incoming value is not from a cmp instruction or not
860 // produced by the last block. We could end up processing the value
861 // producing block more than once.
862 //
863 // This is an uncommon case, so we bail.
865 dbgs()
866 << "skip: non-constant value not from cmp or not from last block.\n");
867 return false;
868 }
869 LastBlock = Phi.getIncomingBlock(I);
870 }
871 if (!LastBlock) {
872 // There is no non-constant block.
873 LLVM_DEBUG(dbgs() << "skip: no non-constant block\n");
874 return false;
875 }
876 if (LastBlock->getSingleSuccessor() != Phi.getParent()) {
877 LLVM_DEBUG(dbgs() << "skip: last block non-phi successor\n");
878 return false;
879 }
880
881 const auto Blocks =
882 getOrderedBlocks(Phi, LastBlock, Phi.getNumIncomingValues());
883 if (Blocks.empty()) return false;
884 BCECmpChain CmpChain(Blocks, Phi, AA);
885
886 if (!CmpChain.atLeastOneMerged()) {
887 LLVM_DEBUG(dbgs() << "skip: nothing merged\n");
888 return false;
889 }
890
891 return CmpChain.simplify(TLI, AA, DTU);
892}
893
894static bool runImpl(Function &F, const TargetLibraryInfo &TLI,
896 DominatorTree *DT) {
897 LLVM_DEBUG(dbgs() << "MergeICmpsPass: " << F.getName() << "\n");
898
899 // We only try merging comparisons if the target wants to expand memcmp later.
900 // The rationale is to avoid turning small chains into memcmp calls.
901 if (!TTI.enableMemCmpExpansion(F.hasOptSize(), true))
902 return false;
903
904 // Make sure we can emit calls to memcmp().
905 if (!isLibFuncEmittable(F.getParent(), &TLI, LibFunc_memcmp))
906 return false;
907
908 DomTreeUpdater DTU(DT, /*PostDominatorTree*/ nullptr,
909 DomTreeUpdater::UpdateStrategy::Eager);
910
911 bool MadeChange = false;
912
913 for (BasicBlock &BB : llvm::drop_begin(F)) {
914 // A Phi operation is always first in a basic block.
915 if (auto *const Phi = dyn_cast<PHINode>(&*BB.begin()))
916 MadeChange |= processPhi(*Phi, TLI, AA, DTU);
917 }
918
919 return MadeChange;
920}
921
924 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
925 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
926 auto &AA = AM.getResult<AAManager>(F);
928 const bool MadeChanges = runImpl(F, TLI, TTI, AA, DT);
929 if (!MadeChanges)
930 return PreservedAnalyses::all();
933 return PA;
934}
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 runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
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)
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:119
This pass exposes codegen information to IR-level passes.
A manager for alias analyses.
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.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
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:687
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; assumes that the block is well-formed.
Definition BasicBlock.h:237
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ ICMP_NE
not equal
Definition InstrTypes.h:762
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Analysis pass which computes a DominatorTree.
Definition Dominators.h:278
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:159
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:2858
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.
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
Check if the string is empty.
Definition StringRef.h:141
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
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.
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:267
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
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:318
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:190
const ParentTy * getParent() const
Definition ilist_node.h:34
Abstract Attribute helper functions.
Definition Attributor.h:165
@ 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:467
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:315
@ Offset
Definition DWP.cpp:558
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
cl::opt< bool > ProfcheckDisableMetadataFixes
Definition LoopInfo.cpp:60
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:1668
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 bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, LibFunc TheLibFunc)
Check whether the library function is available on target and also that it in the current Module is a...
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:1745
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:407
bool isModSet(const ModRefInfo MRI)
Definition ModRef.h:49
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1635
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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:1752
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
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:250
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:1916
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:1771
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...
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.
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:876
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)