LLVM 24.0.0git
ExpandMemCmp.cpp
Go to the documentation of this file.
1//===--- ExpandMemCmp.cpp - Expand memcmp() to load/stores ----------------===//
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 tries to expand memcmp() calls into optimally-sized loads and
10// compares for the target.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/Statistic.h"
23#include "llvm/IR/Dominators.h"
24#include "llvm/IR/IRBuilder.h"
31#include <optional>
32
33using namespace llvm;
34using namespace llvm::PatternMatch;
35
36#define DEBUG_TYPE "expand-memcmp"
37
38STATISTIC(NumMemCmpCalls, "Number of memcmp calls");
39STATISTIC(NumMemCmpNotConstant, "Number of memcmp calls without constant size");
40STATISTIC(NumMemCmpGreaterThanMax,
41 "Number of memcmp calls with size greater than max size");
42STATISTIC(NumMemCmpInlined, "Number of inlined memcmp calls");
43
45 "memcmp-num-loads-per-block", cl::Hidden, cl::init(1),
46 cl::desc("The number of loads per basic block for inline expansion of "
47 "memcmp."));
48
50 "max-loads-per-memcmp", cl::Hidden,
51 cl::desc("Set maximum number of loads used in expanded memcmp"));
52
54 "max-loads-per-memcmp-opt-size", cl::Hidden,
55 cl::desc("Set maximum number of loads used in expanded memcmp for -Os/Oz"));
56
57namespace {
58
59// Return the known alignment of the pointer argument \p ArgNo of \p CI,
60// combining the alignment of the underlying pointer value with any align
61// attribute on the call site itself.
62static Align getMemCmpArgAlignment(const CallInst *CI, unsigned ArgNo,
63 const DataLayout &DL) {
65 if (MaybeAlign ParamAlign = CI->getParamAlign(ArgNo))
66 A = std::max(A, *ParamAlign);
67 return A;
68}
69
70// This class provides helper functions to expand a memcmp library call into an
71// inline expansion.
72class MemCmpExpansion {
73 struct LoadPair {
74 Value *Lhs = nullptr;
75 Value *Rhs = nullptr;
76 };
77
78 struct ResultBlock {
79 BasicBlock *BB = nullptr;
80 PHINode *PhiSrc1 = nullptr;
81 PHINode *PhiSrc2 = nullptr;
82
83 ResultBlock() = default;
84 };
85
86 CallInst *const CI = nullptr;
87 ResultBlock ResBlock;
88 const uint64_t Size;
89 unsigned MaxLoadSize = 0;
90 const uint64_t NumLoadsPerBlock;
91 const unsigned MaxBytesPerBlock;
92 unsigned MaxBlockSize = 0;
93 std::vector<BasicBlock *> LoadCmpBlocks;
94 BasicBlock *EndBlock = nullptr;
95 PHINode *PhiRes = nullptr;
96 const bool IsUsedForZeroCmp;
97 const DataLayout &DL;
98 const TargetTransformInfo &TTI;
99 // The known common alignment of the two source pointers.
100 const Align CommonAlign;
101 DomTreeUpdater *DTU = nullptr;
102 IRBuilder<> Builder;
103 // Represents the decomposition in blocks of the expansion. For example,
104 // comparing 33 bytes on X86+sse can be done with 2x16-byte loads and
105 // 1x1-byte load, which would be represented as [{16, 0}, {16, 16}, {1, 32}.
106 struct LoadEntry {
107 LoadEntry(unsigned LoadSize, uint64_t Offset)
108 : LoadSize(LoadSize), Offset(Offset) {
109 }
110
111 // The size of the load for this block, in bytes.
112 unsigned LoadSize;
113 // The offset of this load from the base pointer, in bytes.
114 uint64_t Offset;
115 };
116 using LoadEntryVector = SmallVector<LoadEntry, 8>;
117 LoadEntryVector LoadSequence;
118
119 void createLoadCmpBlocks();
120 void createResultBlock();
121 void setupResultBlockPHINodes();
122 void setupEndBlockPHINodes();
123 Value *getCompareLoadPairs(unsigned BlockIndex, unsigned &LoadIndex);
124 LoadPair getPackedLoadPair(unsigned BlockIndex, unsigned &LoadIndex);
125 void emitLoadCompareBlock(unsigned BlockIndex, unsigned &LoadIndex);
126 void emitLoadCompareBlockMultipleLoads(unsigned BlockIndex,
127 unsigned &LoadIndex);
128 void emitLoadCompareByteBlock(unsigned BlockIndex, unsigned OffsetBytes);
129 void emitMemCmpResultBlock();
130 Value *getMemCmpExpansionZeroCase();
131 Value *getMemCmpEqZeroOneBlock();
132 Value *getMemCmpOneBlock();
133 Value *getMemCmpOneBlockMultipleLoads();
134 Value *getMemCmpResult(const LoadPair &Loads);
135 LoadPair getLoadPair(Type *LoadSizeType, Type *BSwapSizeType,
136 Type *CmpSizeType, unsigned OffsetBytes);
137
138 // Return true if a load of `LoadSize` bytes at `Offset` from the base
139 // pointers is accessible on the target: either it is naturally aligned given
140 // the known common base alignment, or the target allows a misaligned access
141 // of that width.
142 bool isAccessAllowed(unsigned LoadSize, uint64_t Offset) const;
143
144 static LoadEntryVector
145 computeGreedyLoadSequence(uint64_t Size, llvm::ArrayRef<unsigned> LoadSizes,
146 unsigned MaxNumLoads);
147 LoadEntryVector computeOverlappingLoadSequence(uint64_t Size,
148 unsigned MaxLoadSize,
149 unsigned MaxNumLoads) const;
150
151 void optimiseLoadSequence(
152 LoadEntryVector &LoadSequence,
153 const TargetTransformInfo::MemCmpExpansionOptions &Options,
154 bool IsUsedForZeroCmp) const;
155
156public:
157 MemCmpExpansion(CallInst *CI, uint64_t Size,
158 const TargetTransformInfo::MemCmpExpansionOptions &Options,
159 const bool IsUsedForZeroCmp, const DataLayout &TheDataLayout,
160 DomTreeUpdater *DTU, const TargetTransformInfo &TTI,
161 Align CommonAlign, unsigned MaxBytesPerBlock);
162
163 unsigned getNumBlocks();
164 unsigned getNumLoadsInBlock(unsigned LoadIndex) const;
165 unsigned getNumBytesInBlock(unsigned LoadIndex, unsigned NumLoads) const;
166 uint64_t getNumLoads() const { return LoadSequence.size(); }
167
168 Value *getMemCmpExpansion();
169};
170
171// Return true if a load of `LoadSize` bytes at `Offset` from the base pointers
172// is accessible on the target: either it is naturally aligned given the known
173// common base alignment, or the target allows a misaligned access of that
174// width. We query whether the access is *allowed*, not whether it is *fast*,
175// matching the historical behavior of forming unaligned loads whenever the
176// target permits them.
177static bool isAccessAllowed(const CallInst *CI, const TargetTransformInfo &TTI,
178 Align CommonAlign, unsigned LoadSize,
180 // The access is naturally aligned when the known alignment is at least the
181 // load width. LoadSize is not necessarily a power of two here: some targets
182 // like RISC-V add non-power-of-two load sizes for vector memcmp, so compare
183 // against the raw width rather than constructing an Align, which would
184 // require a power of two.
185 Align AccessAlign = commonAlignment(CommonAlign, Offset);
186 if (AccessAlign.value() >= LoadSize)
187 return true;
188 unsigned AS = CI->getArgOperand(0)->getType()->getPointerAddressSpace();
189 return TTI.allowsMisalignedMemoryAccesses(CI->getContext(), LoadSize * 8, AS,
190 AccessAlign);
191}
192
193// Return true if a load of `LoadSize` bytes at `Offset` from the base pointers
194// is accessible on the target given the known common base alignment. This gates
195// the (power-of-two) overlapping loads; tail expansions are always legalized by
196// the backend and skip this check.
197bool MemCmpExpansion::isAccessAllowed(unsigned LoadSize,
198 uint64_t Offset) const {
199 return ::isAccessAllowed(CI, TTI, CommonAlign, LoadSize, Offset);
200}
201
202MemCmpExpansion::LoadEntryVector
203MemCmpExpansion::computeGreedyLoadSequence(uint64_t Size,
204 llvm::ArrayRef<unsigned> LoadSizes,
205 const unsigned MaxNumLoads) {
206 LoadEntryVector LoadSequence;
207 uint64_t Offset = 0;
208 while (Size && !LoadSizes.empty()) {
209 const unsigned LoadSize = LoadSizes.front();
210 const uint64_t NumLoadsForThisSize = Size / LoadSize;
211 if (LoadSequence.size() + NumLoadsForThisSize > MaxNumLoads) {
212 // Do not expand if the total number of loads is larger than what the
213 // target allows. Note that it's important that we exit before completing
214 // the expansion to avoid using a ton of memory to store the expansion for
215 // large sizes.
216 return {};
217 }
218 if (NumLoadsForThisSize > 0) {
219 for (uint64_t I = 0; I < NumLoadsForThisSize; ++I) {
220 LoadSequence.push_back({LoadSize, Offset});
221 Offset += LoadSize;
222 }
223 Size = Size % LoadSize;
224 }
225 LoadSizes = LoadSizes.drop_front();
226 }
227 return LoadSequence;
228}
229
230MemCmpExpansion::LoadEntryVector
231MemCmpExpansion::computeOverlappingLoadSequence(
232 uint64_t Size, const unsigned MaxLoadSize,
233 const unsigned MaxNumLoads) const {
234 // These are already handled by the greedy approach.
235 if (Size < 2 || MaxLoadSize < 2)
236 return {};
237
238 // We try to do as many non-overlapping loads as possible starting from the
239 // beginning.
240 const uint64_t NumNonOverlappingLoads = Size / MaxLoadSize;
241 assert(NumNonOverlappingLoads && "there must be at least one load");
242 // There remain 0 to (MaxLoadSize - 1) bytes to load, this will be done with
243 // an overlapping load.
244 Size = Size - NumNonOverlappingLoads * MaxLoadSize;
245 // Bail if we do not need an overloapping store, this is already handled by
246 // the greedy approach.
247 if (Size == 0)
248 return {};
249 // Bail if the number of loads (non-overlapping + potential overlapping one)
250 // is larger than the max allowed.
251 if ((NumNonOverlappingLoads + 1) > MaxNumLoads)
252 return {};
253
254 // Add non-overlapping loads.
255 LoadEntryVector LoadSequence;
256 uint64_t Offset = 0;
257 for (uint64_t I = 0; I < NumNonOverlappingLoads; ++I) {
258 LoadSequence.push_back({MaxLoadSize, Offset});
259 Offset += MaxLoadSize;
260 }
261
262 // Add the last overlapping load. Its offset is not a multiple of the load
263 // size, so it may be misaligned; bail if the target cannot access it.
264 assert(Size > 0 && Size < MaxLoadSize && "broken invariant");
265 uint64_t OverlapOffset = Offset - (MaxLoadSize - Size);
266 if (!isAccessAllowed(MaxLoadSize, OverlapOffset))
267 return {};
268
269 LoadSequence.push_back({MaxLoadSize, OverlapOffset});
270 return LoadSequence;
271}
272
273void MemCmpExpansion::optimiseLoadSequence(
274 LoadEntryVector &LoadSequence,
275 const TargetTransformInfo::MemCmpExpansionOptions &Options,
276 bool IsUsedForZeroCmp) const {
277 // This part of code attempts to optimize the LoadSequence by merging allowed
278 // subsequences into single loads of allowed sizes from
279 // `MemCmpExpansionOptions::AllowedTailExpansions`. If it is for zero
280 // comparison or if no allowed tail expansions are specified, we exit early.
281 if (IsUsedForZeroCmp || Options.AllowedTailExpansions.empty())
282 return;
283
284 while (LoadSequence.size() >= 2) {
285 auto Last = LoadSequence[LoadSequence.size() - 1];
286 auto PreLast = LoadSequence[LoadSequence.size() - 2];
287
288 // Exit the loop if the two sequences are not contiguous
289 if (PreLast.Offset + PreLast.LoadSize != Last.Offset)
290 break;
291
292 auto LoadSize = Last.LoadSize + PreLast.LoadSize;
293 if (find(Options.AllowedTailExpansions, LoadSize) ==
294 Options.AllowedTailExpansions.end())
295 break;
296
297 // A merged load wider than MaxLoadSize can only be emitted when it is the
298 // sole load (getMemCmpOneBlock); in a multi-block expansion
299 // emitLoadCompareBlock requires every load to fit in MaxLoadSize (the
300 // result-block phis are sized to it). The per-call-site alignment filter
301 // can shrink MaxLoadSize, so stop merging when the result would still be
302 // multi-block and the merged load exceeds it.
303 if (LoadSize > MaxLoadSize && LoadSequence.size() > 2)
304 break;
305
306 // Remove the last two sequences and replace with the combined sequence
307 LoadSequence.pop_back();
308 LoadSequence.pop_back();
309 LoadSequence.emplace_back(LoadSize, PreLast.Offset);
310 }
311}
312
313// Initialize the basic block structure required for expansion of memcmp call
314// with given maximum load size and memcmp size parameter.
315// This structure includes:
316// 1. A list of load compare blocks - LoadCmpBlocks.
317// 2. An EndBlock, split from original instruction point, which is the block to
318// return from.
319// 3. ResultBlock, block to branch to for early exit when a
320// LoadCmpBlock finds a difference.
321MemCmpExpansion::MemCmpExpansion(
322 CallInst *const CI, uint64_t Size,
323 const TargetTransformInfo::MemCmpExpansionOptions &Options,
324 const bool IsUsedForZeroCmp, const DataLayout &TheDataLayout,
325 DomTreeUpdater *DTU, const TargetTransformInfo &TTI, Align CommonAlign,
326 unsigned MaxBytesPerBlock)
327 : CI(CI), Size(Size), NumLoadsPerBlock(Options.NumLoadsPerBlock),
328 MaxBytesPerBlock(MaxBytesPerBlock), IsUsedForZeroCmp(IsUsedForZeroCmp),
329 DL(TheDataLayout), TTI(TTI), CommonAlign(CommonAlign), DTU(DTU),
330 Builder(CI) {
331 assert(Size > 0 && "zero blocks");
332 assert(NumLoadsPerBlock > 0 && "zero loads per block");
333 // Scale the max size down if the target can load more bytes than we need.
334 llvm::ArrayRef<unsigned> LoadSizes(Options.LoadSizes);
335 while (!LoadSizes.empty() && LoadSizes.front() > Size) {
336 LoadSizes = LoadSizes.drop_front();
337 }
338 assert(!LoadSizes.empty() && "cannot load Size bytes");
339 MaxLoadSize = LoadSizes.front();
340 // Compute the decomposition.
341 LoadSequence =
342 computeGreedyLoadSequence(Size, LoadSizes, Options.MaxNumLoads);
343 assert(LoadSequence.size() <= Options.MaxNumLoads && "broken invariant");
344 // If we allow overlapping loads and the load sequence is not already optimal,
345 // use overlapping loads.
346 if (Options.AllowOverlappingLoads &&
347 (LoadSequence.empty() || LoadSequence.size() > 2)) {
348 auto OverlappingLoads =
349 computeOverlappingLoadSequence(Size, MaxLoadSize, Options.MaxNumLoads);
350 if (!OverlappingLoads.empty() &&
351 (LoadSequence.empty() ||
352 OverlappingLoads.size() < LoadSequence.size())) {
353 LoadSequence = OverlappingLoads;
354 }
355 }
356 assert(LoadSequence.size() <= Options.MaxNumLoads && "broken invariant");
357 optimiseLoadSequence(LoadSequence, Options, IsUsedForZeroCmp);
358
359 unsigned LoadIndex = 0;
360 while (LoadIndex < getNumLoads()) {
361 unsigned NumLoads = getNumLoadsInBlock(LoadIndex);
362 MaxBlockSize =
363 std::max(MaxBlockSize, getNumBytesInBlock(LoadIndex, NumLoads));
364 LoadIndex += NumLoads;
365 }
366 if (MaxBlockSize)
367 MaxBlockSize = PowerOf2Ceil(MaxBlockSize);
368}
369
370unsigned MemCmpExpansion::getNumLoadsInBlock(unsigned LoadIndex) const {
371 if (IsUsedForZeroCmp)
372 return std::min<uint64_t>(getNumLoads() - LoadIndex, NumLoadsPerBlock);
373
374 unsigned NumLoads = 0;
375 unsigned NumBytes = 0;
376 while (LoadIndex + NumLoads < getNumLoads() && NumLoads < NumLoadsPerBlock &&
377 NumBytes + LoadSequence[LoadIndex + NumLoads].LoadSize <=
378 MaxBytesPerBlock) {
379 NumBytes += LoadSequence[LoadIndex + NumLoads].LoadSize;
380 ++NumLoads;
381 }
382 assert(NumLoads && "at least one load must fit in a block");
383 return NumLoads;
384}
385
386unsigned MemCmpExpansion::getNumBytesInBlock(unsigned LoadIndex,
387 unsigned NumLoads) const {
388 unsigned NumBytes = 0;
389 for (unsigned I = 0; I != NumLoads; ++I)
390 NumBytes += LoadSequence[LoadIndex + I].LoadSize;
391 return NumBytes;
392}
393
394unsigned MemCmpExpansion::getNumBlocks() {
395 unsigned NumBlocks = 0;
396 for (unsigned LoadIndex = 0; LoadIndex < getNumLoads(); ++NumBlocks)
397 LoadIndex += getNumLoadsInBlock(LoadIndex);
398 return NumBlocks;
399}
400
401void MemCmpExpansion::createLoadCmpBlocks() {
402 for (unsigned i = 0; i < getNumBlocks(); i++) {
403 BasicBlock *BB = BasicBlock::Create(CI->getContext(), "loadbb",
404 EndBlock->getParent(), EndBlock);
405 LoadCmpBlocks.push_back(BB);
406 }
407}
408
409void MemCmpExpansion::createResultBlock() {
410 ResBlock.BB = BasicBlock::Create(CI->getContext(), "res_block",
411 EndBlock->getParent(), EndBlock);
412}
413
414MemCmpExpansion::LoadPair MemCmpExpansion::getLoadPair(Type *LoadSizeType,
415 Type *BSwapSizeType,
416 Type *CmpSizeType,
417 unsigned OffsetBytes) {
418 // Get the memory source at offset `OffsetBytes`.
419 Value *LhsSource = CI->getArgOperand(0);
420 Value *RhsSource = CI->getArgOperand(1);
421 Align LhsAlign = getMemCmpArgAlignment(CI, 0, DL);
422 Align RhsAlign = getMemCmpArgAlignment(CI, 1, DL);
423 if (OffsetBytes > 0) {
424 auto *ByteType = Type::getInt8Ty(CI->getContext());
425 LhsSource = Builder.CreateConstGEP1_64(ByteType, LhsSource, OffsetBytes);
426 RhsSource = Builder.CreateConstGEP1_64(ByteType, RhsSource, OffsetBytes);
427 LhsAlign = commonAlignment(LhsAlign, OffsetBytes);
428 RhsAlign = commonAlignment(RhsAlign, OffsetBytes);
429 }
430
431 // Create a constant or a load from the source.
432 Value *Lhs = nullptr;
433 if (auto *C = dyn_cast<Constant>(LhsSource))
434 Lhs = ConstantFoldLoadFromConstPtr(C, LoadSizeType, DL);
435 if (!Lhs)
436 Lhs = Builder.CreateAlignedLoad(LoadSizeType, LhsSource, LhsAlign);
437
438 Value *Rhs = nullptr;
439 if (auto *C = dyn_cast<Constant>(RhsSource))
440 Rhs = ConstantFoldLoadFromConstPtr(C, LoadSizeType, DL);
441 if (!Rhs)
442 Rhs = Builder.CreateAlignedLoad(LoadSizeType, RhsSource, RhsAlign);
443
444 // Zero extend if Byte Swap intrinsic has different type
445 if (BSwapSizeType && LoadSizeType != BSwapSizeType) {
446 Lhs = Builder.CreateZExt(Lhs, BSwapSizeType);
447 Rhs = Builder.CreateZExt(Rhs, BSwapSizeType);
448 }
449
450 // Swap bytes if required.
451 if (BSwapSizeType) {
453 CI->getModule(), Intrinsic::bswap, BSwapSizeType);
454 Lhs = Builder.CreateCall(Bswap, Lhs);
455 Rhs = Builder.CreateCall(Bswap, Rhs);
456 }
457
458 // Zero extend if required.
459 if (CmpSizeType != nullptr && CmpSizeType != Lhs->getType()) {
460 Lhs = Builder.CreateZExt(Lhs, CmpSizeType);
461 Rhs = Builder.CreateZExt(Rhs, CmpSizeType);
462 }
463 return {Lhs, Rhs};
464}
465
466// This function creates the IR instructions for loading and comparing 1 byte.
467// It loads 1 byte from each source of the memcmp parameters with the given
468// GEPIndex. It then subtracts the two loaded values and adds this result to the
469// final phi node for selecting the memcmp result.
470void MemCmpExpansion::emitLoadCompareByteBlock(unsigned BlockIndex,
471 unsigned OffsetBytes) {
472 BasicBlock *BB = LoadCmpBlocks[BlockIndex];
473 Builder.SetInsertPoint(BB);
474 const LoadPair Loads =
475 getLoadPair(Type::getInt8Ty(CI->getContext()), nullptr,
476 Type::getInt32Ty(CI->getContext()), OffsetBytes);
477 Value *Diff = Builder.CreateSub(Loads.Lhs, Loads.Rhs);
478
479 PhiRes->addIncoming(Diff, BB);
480
481 if (BlockIndex < (LoadCmpBlocks.size() - 1)) {
482 // Early exit branch if difference found to EndBlock. Otherwise, continue to
483 // next LoadCmpBlock,
484 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_NE, Diff,
485 ConstantInt::get(Diff->getType(), 0));
486 Builder.CreateCondBr(Cmp, EndBlock, LoadCmpBlocks[BlockIndex + 1]);
487 if (DTU)
488 DTU->applyUpdates(
489 {{DominatorTree::Insert, BB, EndBlock},
490 {DominatorTree::Insert, BB, LoadCmpBlocks[BlockIndex + 1]}});
491 } else {
492 // The last block has an unconditional branch to EndBlock.
493 Builder.CreateBr(EndBlock);
494 if (DTU)
495 DTU->applyUpdates({{DominatorTree::Insert, BB, EndBlock}});
496 }
497}
498
499/// Generate an equality comparison for one or more pairs of loaded values.
500/// This is used in the case where the memcmp() call is compared equal or not
501/// equal to zero.
502Value *MemCmpExpansion::getCompareLoadPairs(unsigned BlockIndex,
503 unsigned &LoadIndex) {
504 assert(LoadIndex < getNumLoads() &&
505 "getCompareLoadPairs() called with no remaining loads");
506 std::vector<Value *> XorList, OrList;
507 Value *Diff = nullptr;
508
509 const unsigned NumLoads = getNumLoadsInBlock(LoadIndex);
510
511 // For a single-block expansion, start inserting before the memcmp call.
512 if (LoadCmpBlocks.empty())
513 Builder.SetInsertPoint(CI);
514 else
515 Builder.SetInsertPoint(LoadCmpBlocks[BlockIndex]);
516
517 Value *Cmp = nullptr;
518 // If we have multiple loads per block, we need to generate a composite
519 // comparison using xor+or. The type for the combinations is the largest load
520 // type.
521 IntegerType *const MaxLoadType =
522 NumLoads == 1 ? nullptr
523 : IntegerType::get(CI->getContext(), MaxLoadSize * 8);
524
525 for (unsigned i = 0; i < NumLoads; ++i, ++LoadIndex) {
526 const LoadEntry &CurLoadEntry = LoadSequence[LoadIndex];
527 const LoadPair Loads = getLoadPair(
528 IntegerType::get(CI->getContext(), CurLoadEntry.LoadSize * 8), nullptr,
529 MaxLoadType, CurLoadEntry.Offset);
530
531 if (NumLoads != 1) {
532 // If we have multiple loads per block, we need to generate a composite
533 // comparison using xor+or.
534 Diff = Builder.CreateXor(Loads.Lhs, Loads.Rhs);
535 Diff = Builder.CreateZExt(Diff, MaxLoadType);
536 XorList.push_back(Diff);
537 } else {
538 // If there's only one load per block, we just compare the loaded values.
539 Cmp = Builder.CreateICmpNE(Loads.Lhs, Loads.Rhs);
540 }
541 }
542
543 auto pairWiseOr = [&](std::vector<Value *> &InList) -> std::vector<Value *> {
544 std::vector<Value *> OutList;
545 for (unsigned i = 0; i < InList.size() - 1; i = i + 2) {
546 Value *Or = Builder.CreateOr(InList[i], InList[i + 1]);
547 OutList.push_back(Or);
548 }
549 if (InList.size() % 2 != 0)
550 OutList.push_back(InList.back());
551 return OutList;
552 };
553
554 if (!Cmp) {
555 // Pairwise OR the XOR results.
556 OrList = pairWiseOr(XorList);
557
558 // Pairwise OR the OR results until one result left.
559 while (OrList.size() != 1) {
560 OrList = pairWiseOr(OrList);
561 }
562
563 assert(Diff && "Failed to find comparison diff");
564 Cmp = Builder.CreateICmpNE(OrList[0], ConstantInt::get(Diff->getType(), 0));
565 }
566
567 return Cmp;
568}
569
570MemCmpExpansion::LoadPair
571MemCmpExpansion::getPackedLoadPair(unsigned BlockIndex, unsigned &LoadIndex) {
572 assert(LoadIndex < getNumLoads() &&
573 "getPackedLoadPair() called with no remaining loads");
574 if (LoadCmpBlocks.empty())
575 Builder.SetInsertPoint(CI);
576 else
577 Builder.SetInsertPoint(LoadCmpBlocks[BlockIndex]);
578
579 const unsigned NumLoads = getNumLoadsInBlock(LoadIndex);
580 const unsigned NumBytes = getNumBytesInBlock(LoadIndex, NumLoads);
581 // Pack the loads so that the byte at the lowest address occupies the most
582 // significant bits. An unsigned comparison of the packed values therefore
583 // has the same lexicographic ordering as memcmp.
584 auto *BlockType = IntegerType::get(CI->getContext(), MaxBlockSize * 8);
585 Value *PackedLhs = ConstantInt::get(BlockType, 0);
586 Value *PackedRhs = ConstantInt::get(BlockType, 0);
587 unsigned RemainingBytes = NumBytes;
588
589 for (unsigned I = 0; I != NumLoads; ++I, ++LoadIndex) {
590 const LoadEntry &Entry = LoadSequence[LoadIndex];
591 auto *LoadType = IntegerType::get(CI->getContext(), Entry.LoadSize * 8);
592 auto *BSwapType = DL.isLittleEndian() && Entry.LoadSize != 1
594 PowerOf2Ceil(Entry.LoadSize * 8))
595 : nullptr;
596 LoadPair Loads = getLoadPair(LoadType, BSwapType, BlockType, Entry.Offset);
597
598 if (BSwapType && BSwapType->getIntegerBitWidth() != Entry.LoadSize * 8) {
599 unsigned Padding = BSwapType->getIntegerBitWidth() - Entry.LoadSize * 8;
600 Loads.Lhs = Builder.CreateLShr(Loads.Lhs, Padding);
601 Loads.Rhs = Builder.CreateLShr(Loads.Rhs, Padding);
602 }
603
604 RemainingBytes -= Entry.LoadSize;
605 unsigned Shift = RemainingBytes * 8;
606 PackedLhs =
607 Builder.CreateOr(PackedLhs, Builder.CreateShl(Loads.Lhs, Shift));
608 PackedRhs =
609 Builder.CreateOr(PackedRhs, Builder.CreateShl(Loads.Rhs, Shift));
610 }
611
612 return {PackedLhs, PackedRhs};
613}
614
615void MemCmpExpansion::emitLoadCompareBlockMultipleLoads(unsigned BlockIndex,
616 unsigned &LoadIndex) {
617 Value *Cmp = getCompareLoadPairs(BlockIndex, LoadIndex);
618
619 BasicBlock *NextBB = (BlockIndex == (LoadCmpBlocks.size() - 1))
620 ? EndBlock
621 : LoadCmpBlocks[BlockIndex + 1];
622 // Early exit branch if difference found to ResultBlock. Otherwise,
623 // continue to next LoadCmpBlock or EndBlock.
624 BasicBlock *BB = Builder.GetInsertBlock();
625 CondBrInst *CmpBr = Builder.CreateCondBr(Cmp, ResBlock.BB, NextBB);
627 CI->getFunction());
628 if (DTU)
629 DTU->applyUpdates({{DominatorTree::Insert, BB, ResBlock.BB},
630 {DominatorTree::Insert, BB, NextBB}});
631
632 // Add a phi edge for the last LoadCmpBlock to Endblock with a value of 0
633 // since early exit to ResultBlock was not taken (no difference was found in
634 // any of the bytes).
635 if (BlockIndex == LoadCmpBlocks.size() - 1) {
636 Value *Zero = ConstantInt::get(Type::getInt32Ty(CI->getContext()), 0);
637 PhiRes->addIncoming(Zero, LoadCmpBlocks[BlockIndex]);
638 }
639}
640
641// This function creates the IR intructions for loading and comparing using the
642// given LoadSize. It loads the number of bytes specified by LoadSize from each
643// source of the memcmp parameters. It then does a subtract to see if there was
644// a difference in the loaded values. If a difference is found, it branches
645// with an early exit to the ResultBlock for calculating which source was
646// larger. Otherwise, it falls through to the either the next LoadCmpBlock or
647// the EndBlock if this is the last LoadCmpBlock. Loading 1 byte is handled with
648// a special case through emitLoadCompareByteBlock. The special handling can
649// simply subtract the loaded values and add it to the result phi node.
650void MemCmpExpansion::emitLoadCompareBlock(unsigned BlockIndex,
651 unsigned &LoadIndex) {
652 const unsigned NumLoads = getNumLoadsInBlock(LoadIndex);
653 if (NumLoads == 1 && LoadSequence[LoadIndex].LoadSize == 1) {
654 MemCmpExpansion::emitLoadCompareByteBlock(BlockIndex,
655 LoadSequence[LoadIndex].Offset);
656 ++LoadIndex;
657 return;
658 }
659
660 LoadPair Loads;
661 if (NumLoads == 1) {
662 const LoadEntry &Entry = LoadSequence[LoadIndex++];
663 auto *LoadType = IntegerType::get(CI->getContext(), Entry.LoadSize * 8);
664 auto *BSwapType = DL.isLittleEndian()
666 PowerOf2Ceil(Entry.LoadSize * 8))
667 : nullptr;
668 auto *CmpType = IntegerType::get(CI->getContext(), MaxBlockSize * 8);
669 Builder.SetInsertPoint(LoadCmpBlocks[BlockIndex]);
670 Loads = getLoadPair(LoadType, BSwapType, CmpType, Entry.Offset);
671 } else {
672 Loads = getPackedLoadPair(BlockIndex, LoadIndex);
673 }
674
675 // Add the loaded values to the phi nodes for calculating memcmp result only
676 // if result is not used in a zero equality.
677 if (!IsUsedForZeroCmp) {
678 ResBlock.PhiSrc1->addIncoming(Loads.Lhs, LoadCmpBlocks[BlockIndex]);
679 ResBlock.PhiSrc2->addIncoming(Loads.Rhs, LoadCmpBlocks[BlockIndex]);
680 }
681
682 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_EQ, Loads.Lhs, Loads.Rhs);
683 BasicBlock *NextBB = (BlockIndex == (LoadCmpBlocks.size() - 1))
684 ? EndBlock
685 : LoadCmpBlocks[BlockIndex + 1];
686 // Early exit branch if difference found to ResultBlock. Otherwise, continue
687 // to next LoadCmpBlock or EndBlock.
688 BasicBlock *BB = Builder.GetInsertBlock();
689 CondBrInst *CmpBr = Builder.CreateCondBr(Cmp, NextBB, ResBlock.BB);
691 CI->getFunction());
692 if (DTU)
693 DTU->applyUpdates({{DominatorTree::Insert, BB, NextBB},
694 {DominatorTree::Insert, BB, ResBlock.BB}});
695
696 // Add a phi edge for the last LoadCmpBlock to Endblock with a value of 0
697 // since early exit to ResultBlock was not taken (no difference was found in
698 // any of the bytes).
699 if (BlockIndex == LoadCmpBlocks.size() - 1) {
700 Value *Zero = ConstantInt::get(Type::getInt32Ty(CI->getContext()), 0);
701 PhiRes->addIncoming(Zero, LoadCmpBlocks[BlockIndex]);
702 }
703}
704
705// This function populates the ResultBlock with a sequence to calculate the
706// memcmp result. It compares the two loaded source values and returns -1 if
707// src1 < src2 and 1 if src1 > src2.
708void MemCmpExpansion::emitMemCmpResultBlock() {
709 // Special case: if memcmp result is used in a zero equality, result does not
710 // need to be calculated and can simply return 1.
711 if (IsUsedForZeroCmp) {
712 BasicBlock::iterator InsertPt = ResBlock.BB->getFirstInsertionPt();
713 Builder.SetInsertPoint(ResBlock.BB, InsertPt);
714 Value *Res = ConstantInt::get(Type::getInt32Ty(CI->getContext()), 1);
715 PhiRes->addIncoming(Res, ResBlock.BB);
716 Builder.CreateBr(EndBlock);
717 if (DTU)
718 DTU->applyUpdates({{DominatorTree::Insert, ResBlock.BB, EndBlock}});
719 return;
720 }
721 BasicBlock::iterator InsertPt = ResBlock.BB->getFirstInsertionPt();
722 Builder.SetInsertPoint(ResBlock.BB, InsertPt);
723
724 Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_ULT, ResBlock.PhiSrc1,
725 ResBlock.PhiSrc2);
726
727 Value *Res =
728 Builder.CreateSelect(Cmp, Constant::getAllOnesValue(Builder.getInt32Ty()),
729 ConstantInt::get(Builder.getInt32Ty(), 1));
731 DEBUG_TYPE, CI->getFunction());
732
733 PhiRes->addIncoming(Res, ResBlock.BB);
734 Builder.CreateBr(EndBlock);
735 if (DTU)
736 DTU->applyUpdates({{DominatorTree::Insert, ResBlock.BB, EndBlock}});
737}
738
739void MemCmpExpansion::setupResultBlockPHINodes() {
740 Type *MaxLoadType = IntegerType::get(CI->getContext(), MaxBlockSize * 8);
741 Builder.SetInsertPoint(ResBlock.BB);
742 ResBlock.PhiSrc1 = Builder.CreatePHI(MaxLoadType, getNumBlocks(), "phi.src1");
743 ResBlock.PhiSrc2 = Builder.CreatePHI(MaxLoadType, getNumBlocks(), "phi.src2");
744}
745
746void MemCmpExpansion::setupEndBlockPHINodes() {
747 Builder.SetInsertPoint(EndBlock, EndBlock->begin());
748 PhiRes = Builder.CreatePHI(Type::getInt32Ty(CI->getContext()), 2, "phi.res");
749}
750
751Value *MemCmpExpansion::getMemCmpExpansionZeroCase() {
752 unsigned LoadIndex = 0;
753 // This loop populates each of the LoadCmpBlocks with the IR sequence to
754 // handle multiple loads per block.
755 for (unsigned I = 0; I < getNumBlocks(); ++I) {
756 emitLoadCompareBlockMultipleLoads(I, LoadIndex);
757 }
758
759 emitMemCmpResultBlock();
760 return PhiRes;
761}
762
763/// A memcmp expansion that compares equality with 0 and only has one block of
764/// load and compare can bypass the compare, branch, and phi IR that is required
765/// in the general case.
766Value *MemCmpExpansion::getMemCmpEqZeroOneBlock() {
767 unsigned LoadIndex = 0;
768 Value *Cmp = getCompareLoadPairs(0, LoadIndex);
769 assert(LoadIndex == getNumLoads() && "some entries were not consumed");
770 return Builder.CreateZExt(Cmp, Type::getInt32Ty(CI->getContext()));
771}
772
773/// A memcmp expansion that only has one block of load and compare can bypass
774/// the compare, branch, and phi IR that is required in the general case.
775/// This function also analyses users of memcmp, and if there is only one user
776/// from which we can conclude that only 2 out of 3 memcmp outcomes really
777/// matter, then it generates more efficient code with only one comparison.
778Value *MemCmpExpansion::getMemCmpOneBlock() {
779 bool NeedsBSwap = DL.isLittleEndian() && Size != 1;
780 Type *LoadSizeType = IntegerType::get(CI->getContext(), Size * 8);
781 Type *BSwapSizeType =
782 NeedsBSwap ? IntegerType::get(CI->getContext(), PowerOf2Ceil(Size * 8))
783 : nullptr;
784 Type *MaxLoadType =
786 std::max(MaxLoadSize, (unsigned)PowerOf2Ceil(Size)) * 8);
787
788 // The i8 and i16 cases don't need compares. We zext the loaded values and
789 // subtract them to get the suitable negative, zero, or positive i32 result.
790 if (Size == 1 || Size == 2) {
791 const LoadPair Loads = getLoadPair(LoadSizeType, BSwapSizeType,
792 Builder.getInt32Ty(), /*Offset*/ 0);
793 return Builder.CreateSub(Loads.Lhs, Loads.Rhs);
794 }
795
796 const LoadPair Loads = getLoadPair(LoadSizeType, BSwapSizeType, MaxLoadType,
797 /*Offset*/ 0);
798
799 return getMemCmpResult(Loads);
800}
801
802Value *MemCmpExpansion::getMemCmpOneBlockMultipleLoads() {
803 unsigned LoadIndex = 0;
804 LoadPair Loads = getPackedLoadPair(/*BlockIndex=*/0, LoadIndex);
805 assert(LoadIndex == getNumLoads() && "some entries were not consumed");
806 return getMemCmpResult(Loads);
807}
808
809Value *MemCmpExpansion::getMemCmpResult(const LoadPair &Loads) {
810 // If a user of memcmp cares only about two outcomes, for example:
811 // bool result = memcmp(a, b, NBYTES) > 0;
812 // We can generate more optimal code with a smaller number of operations
813 if (CI->hasOneUser()) {
814 auto *UI = cast<Instruction>(*CI->user_begin());
815 CmpPredicate Pred = ICmpInst::Predicate::BAD_ICMP_PREDICATE;
816 bool NeedsZExt = false;
817 // This is a special case because instead of checking if the result is less
818 // than zero:
819 // bool result = memcmp(a, b, NBYTES) < 0;
820 // Compiler is clever enough to generate the following code:
821 // bool result = memcmp(a, b, NBYTES) >> 31;
822 if (match(UI,
823 m_LShr(m_Value(),
824 m_SpecificInt(CI->getType()->getIntegerBitWidth() - 1)))) {
825 Pred = ICmpInst::ICMP_SLT;
826 NeedsZExt = true;
827 } else if (match(UI, m_SpecificICmp(ICmpInst::ICMP_SGT, m_Specific(CI),
828 m_AllOnes()))) {
829 // Adjust predicate as if it compared with 0.
830 Pred = ICmpInst::ICMP_SGE;
831 } else if (match(UI, m_SpecificICmp(ICmpInst::ICMP_SLT, m_Specific(CI),
832 m_One()))) {
833 // Adjust predicate as if it compared with 0.
834 Pred = ICmpInst::ICMP_SLE;
835 } else {
836 // In case of a successful match this call will set `Pred` variable
837 match(UI, m_ICmp(Pred, m_Specific(CI), m_Zero()));
838 }
839 // Generate new code and remove the original memcmp call and the user
840 if (ICmpInst::isSigned(Pred)) {
842 Loads.Lhs, Loads.Rhs);
843 auto *Result = NeedsZExt ? Builder.CreateZExt(Cmp, UI->getType()) : Cmp;
844 UI->replaceAllUsesWith(Result);
845 UI->eraseFromParent();
846 CI->eraseFromParent();
847 return nullptr;
848 }
849 }
850
851 // The result of memcmp is negative, zero, or positive.
852 return Builder.CreateIntrinsic(Builder.getInt32Ty(), Intrinsic::ucmp,
853 {Loads.Lhs, Loads.Rhs});
854}
855
856// This function expands the memcmp call into an inline expansion and returns
857// the memcmp result. Returns nullptr if the memcmp is already replaced.
858Value *MemCmpExpansion::getMemCmpExpansion() {
859 // Create the basic block framework for a multi-block expansion.
860 if (getNumBlocks() != 1) {
861 BasicBlock *StartBlock = CI->getParent();
862 EndBlock = SplitBlock(StartBlock, CI, DTU, /*LI=*/nullptr,
863 /*MSSAU=*/nullptr, "endblock");
864 setupEndBlockPHINodes();
865 createResultBlock();
866
867 // If return value of memcmp is not used in a zero equality, we need to
868 // calculate which source was larger. The calculation requires the
869 // two loaded source values of each load compare block.
870 // These will be saved in the phi nodes created by setupResultBlockPHINodes.
871 if (!IsUsedForZeroCmp) setupResultBlockPHINodes();
872
873 // Create the number of required load compare basic blocks.
874 createLoadCmpBlocks();
875
876 // Update the terminator added by SplitBlock to branch to the first
877 // LoadCmpBlock.
878 StartBlock->getTerminator()->setSuccessor(0, LoadCmpBlocks[0]);
879 if (DTU)
880 DTU->applyUpdates({{DominatorTree::Insert, StartBlock, LoadCmpBlocks[0]},
881 {DominatorTree::Delete, StartBlock, EndBlock}});
882 }
883
885
886 if (IsUsedForZeroCmp)
887 return getNumBlocks() == 1 ? getMemCmpEqZeroOneBlock()
888 : getMemCmpExpansionZeroCase();
889
890 if (getNumBlocks() == 1)
891 return getNumLoads() == 1 ? getMemCmpOneBlock()
892 : getMemCmpOneBlockMultipleLoads();
893
894 unsigned LoadIndex = 0;
895 for (unsigned I = 0; I < getNumBlocks(); ++I) {
896 emitLoadCompareBlock(I, LoadIndex);
897 }
898
899 emitMemCmpResultBlock();
900 return PhiRes;
901}
902
903// This function checks to see if an expansion of memcmp can be generated.
904// It checks for constant compare size that is less than the max inline size.
905// If an expansion cannot occur, returns false to leave as a library call.
906// Otherwise, the library call is replaced with a new IR instruction sequence.
907/// We want to transform:
908/// %call = call signext i32 @memcmp(i8* %0, i8* %1, i64 15)
909/// To:
910/// loadbb:
911/// %0 = bitcast i32* %buffer2 to i8*
912/// %1 = bitcast i32* %buffer1 to i8*
913/// %2 = bitcast i8* %1 to i64*
914/// %3 = bitcast i8* %0 to i64*
915/// %4 = load i64, i64* %2
916/// %5 = load i64, i64* %3
917/// %6 = call i64 @llvm.bswap.i64(i64 %4)
918/// %7 = call i64 @llvm.bswap.i64(i64 %5)
919/// %8 = sub i64 %6, %7
920/// %9 = icmp ne i64 %8, 0
921/// br i1 %9, label %res_block, label %loadbb1
922/// res_block: ; preds = %loadbb2,
923/// %loadbb1, %loadbb
924/// %phi.src1 = phi i64 [ %6, %loadbb ], [ %22, %loadbb1 ], [ %36, %loadbb2 ]
925/// %phi.src2 = phi i64 [ %7, %loadbb ], [ %23, %loadbb1 ], [ %37, %loadbb2 ]
926/// %10 = icmp ult i64 %phi.src1, %phi.src2
927/// %11 = select i1 %10, i32 -1, i32 1
928/// br label %endblock
929/// loadbb1: ; preds = %loadbb
930/// %12 = bitcast i32* %buffer2 to i8*
931/// %13 = bitcast i32* %buffer1 to i8*
932/// %14 = bitcast i8* %13 to i32*
933/// %15 = bitcast i8* %12 to i32*
934/// %16 = getelementptr i32, i32* %14, i32 2
935/// %17 = getelementptr i32, i32* %15, i32 2
936/// %18 = load i32, i32* %16
937/// %19 = load i32, i32* %17
938/// %20 = call i32 @llvm.bswap.i32(i32 %18)
939/// %21 = call i32 @llvm.bswap.i32(i32 %19)
940/// %22 = zext i32 %20 to i64
941/// %23 = zext i32 %21 to i64
942/// %24 = sub i64 %22, %23
943/// %25 = icmp ne i64 %24, 0
944/// br i1 %25, label %res_block, label %loadbb2
945/// loadbb2: ; preds = %loadbb1
946/// %26 = bitcast i32* %buffer2 to i8*
947/// %27 = bitcast i32* %buffer1 to i8*
948/// %28 = bitcast i8* %27 to i16*
949/// %29 = bitcast i8* %26 to i16*
950/// %30 = getelementptr i16, i16* %28, i16 6
951/// %31 = getelementptr i16, i16* %29, i16 6
952/// %32 = load i16, i16* %30
953/// %33 = load i16, i16* %31
954/// %34 = call i16 @llvm.bswap.i16(i16 %32)
955/// %35 = call i16 @llvm.bswap.i16(i16 %33)
956/// %36 = zext i16 %34 to i64
957/// %37 = zext i16 %35 to i64
958/// %38 = sub i64 %36, %37
959/// %39 = icmp ne i64 %38, 0
960/// br i1 %39, label %res_block, label %loadbb3
961/// loadbb3: ; preds = %loadbb2
962/// %40 = bitcast i32* %buffer2 to i8*
963/// %41 = bitcast i32* %buffer1 to i8*
964/// %42 = getelementptr i8, i8* %41, i8 14
965/// %43 = getelementptr i8, i8* %40, i8 14
966/// %44 = load i8, i8* %42
967/// %45 = load i8, i8* %43
968/// %46 = zext i8 %44 to i32
969/// %47 = zext i8 %45 to i32
970/// %48 = sub i32 %46, %47
971/// br label %endblock
972/// endblock: ; preds = %res_block,
973/// %loadbb3
974/// %phi.res = phi i32 [ %48, %loadbb3 ], [ %11, %res_block ]
975/// ret i32 %phi.res
976static bool expandMemCmp(CallInst *CI, const TargetTransformInfo *TTI,
977 const DataLayout *DL, ProfileSummaryInfo *PSI,
978 BlockFrequencyInfo *BFI, DomTreeUpdater *DTU,
979 const bool IsBCmp) {
980 NumMemCmpCalls++;
981
982 // Early exit from expansion if -Oz.
983 if (CI->getFunction()->hasMinSize())
984 return false;
985
986 // Early exit from expansion if size is not a constant.
987 ConstantInt *SizeCast = dyn_cast<ConstantInt>(CI->getArgOperand(2));
988 if (!SizeCast) {
989 NumMemCmpNotConstant++;
990 return false;
991 }
992 const uint64_t SizeVal = SizeCast->getZExtValue();
993
994 if (SizeVal == 0) {
995 return false;
996 }
997 // TTI call to check if target would like to expand memcmp. Also, get the
998 // available load sizes.
999 const bool IsUsedForZeroCmp =
1001 bool OptForSize = llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
1002 auto Options = TTI->enableMemCmpExpansion(OptForSize,
1003 IsUsedForZeroCmp);
1004 if (!Options) return false;
1005
1006 if (MemCmpNumLoadsPerBlock.getNumOccurrences())
1008
1009 if (OptForSize &&
1010 MaxLoadsPerMemcmpOptSize.getNumOccurrences())
1011 Options.MaxNumLoads = MaxLoadsPerMemcmpOptSize;
1012
1013 if (!OptForSize && MaxLoadsPerMemcmp.getNumOccurrences())
1014 Options.MaxNumLoads = MaxLoadsPerMemcmp;
1015
1016 // Keep only the load sizes the target can access at the base alignment:
1017 // either the access is naturally aligned, or the target allows a misaligned
1018 // access of that width. This lets strict-alignment targets expand compares
1019 // whose pointers happen to be sufficiently aligned, while still falling back
1020 // to the libcall when no load size fits. Because the greedy load sequence
1021 // only places a load of size S at an offset that is a multiple of S, a size
1022 // kept here is always accessible in that sequence; overlapping loads and
1023 // merged tail expansions are checked separately against their actual offsets
1024 // in MemCmpExpansion.
1025 const Align CommonAlign = std::min(getMemCmpArgAlignment(CI, 0, *DL),
1026 getMemCmpArgAlignment(CI, 1, *DL));
1027 // Remember the target's preferred load width before filtering inaccessible
1028 // sizes. It is also the maximum width of the value formed by packing the
1029 // surviving loads in one ordering-compare block.
1030 const unsigned MaxBytesPerBlock = Options.LoadSizes.front();
1031 llvm::erase_if(Options.LoadSizes, [&](unsigned LoadSize) {
1032 return !isAccessAllowed(CI, *TTI, CommonAlign, LoadSize, /*Offset=*/0);
1033 });
1034 // If the filter removed every load size, bail out to the libcall: the
1035 // MemCmpExpansion constructor asserts that at least one load size remains.
1036 // In practice all in-tree targets include a byte load size, which is
1037 // accessible at any alignment and therefore always survives the filter.
1038 if (Options.LoadSizes.empty())
1039 return false;
1040
1041 MemCmpExpansion Expansion(CI, SizeVal, Options, IsUsedForZeroCmp, *DL, DTU,
1042 *TTI, CommonAlign, MaxBytesPerBlock);
1043
1044 // Don't expand if this will require more loads than desired by the target.
1045 if (Expansion.getNumLoads() == 0) {
1046 NumMemCmpGreaterThanMax++;
1047 return false;
1048 }
1049
1050 NumMemCmpInlined++;
1051
1052 if (Value *Res = Expansion.getMemCmpExpansion()) {
1053 // Replace call with result of expansion and erase call.
1054 CI->replaceAllUsesWith(Res);
1055 CI->eraseFromParent();
1056 }
1057
1058 return true;
1059}
1060
1061static PreservedAnalyses runImpl(Function &F, const TargetLibraryInfo *TLI,
1062 const TargetTransformInfo *TTI,
1063 ProfileSummaryInfo *PSI,
1064 BlockFrequencyInfo *BFI, DominatorTree *DT) {
1065 std::optional<DomTreeUpdater> DTU;
1066 if (DT)
1067 DTU.emplace(DT, DomTreeUpdater::UpdateStrategy::Lazy);
1068
1069 const DataLayout& DL = F.getDataLayout();
1071 for (Instruction &I : instructions(F)) {
1072 if (auto *CI = dyn_cast<CallInst>(&I)) {
1073 LibFunc Func = TLI->getLibFunc(*CI);
1074 if (Func == LibFunc_memcmp || Func == LibFunc_bcmp)
1075 MemCmpCalls.push_back({CI, Func});
1076 }
1077 }
1078
1079 bool MadeChanges = false;
1080 for (const auto &[CI, Func] : MemCmpCalls) {
1081 if (expandMemCmp(CI, TTI, &DL, PSI, BFI, DTU ? &*DTU : nullptr,
1082 Func == LibFunc_bcmp))
1083 MadeChanges = true;
1084 }
1085
1086 if (MadeChanges)
1087 for (BasicBlock &BB : F)
1089 if (!MadeChanges)
1090 return PreservedAnalyses::all();
1091 PreservedAnalyses PA;
1092 PA.preserve<DominatorTreeAnalysis>();
1093 return PA;
1094}
1095
1096} // namespace
1097
1100 // Don't expand memcmp in sanitized functions — sanitizers intercept memcmp
1101 // calls to check for memory errors, and expanding would bypass that.
1102 if (F.hasFnAttribute(Attribute::SanitizeAddress) ||
1103 F.hasFnAttribute(Attribute::SanitizeMemory) ||
1104 F.hasFnAttribute(Attribute::SanitizeThread) ||
1105 F.hasFnAttribute(Attribute::SanitizeHWAddress))
1106 return PreservedAnalyses::all();
1107
1108 const auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
1109 const auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
1110 auto *PSI = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F)
1111 .getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
1112 BlockFrequencyInfo *BFI = (PSI && PSI->hasProfileSummary())
1113 ? &FAM.getResult<BlockFrequencyAnalysis>(F)
1114 : nullptr;
1115 auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F);
1116
1117 return runImpl(F, &TLI, &TTI, PSI, BFI, DT);
1118}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
DXIL Intrinsic Expansion
static cl::opt< unsigned > MemCmpNumLoadsPerBlock("memcmp-num-loads-per-block", cl::Hidden, cl::init(1), cl::desc("The number of loads per basic block for inline expansion of " "memcmp."))
static cl::opt< unsigned > MaxLoadsPerMemcmpOptSize("max-loads-per-memcmp-opt-size", cl::Hidden, cl::desc("Set maximum number of loads used in expanded memcmp for -Os/Oz"))
static cl::opt< unsigned > MaxLoadsPerMemcmp("max-loads-per-memcmp", cl::Hidden, cl::desc("Set maximum number of loads used in expanded memcmp"))
#define DEBUG_TYPE
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
This file contains the declarations for profiling metadata utility functions.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This pass exposes codegen information to IR-level passes.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:446
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
MaybeAlign getParamAlign(unsigned ArgNo) const
Extract the alignment for a call or parameter (0=unknown).
Value * getArgOperand(unsigned i) const
This class represents a function call, abstracting a target machine's calling convention.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:696
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
Value * CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition IRBuilder.h:2065
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition IRBuilder.h:1944
CondBrInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition IRBuilder.h:1226
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
Definition IRBuilder.h:221
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition IRBuilder.h:1542
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition IRBuilder.h:534
BasicBlock * GetInsertBlock() const
Definition IRBuilder.h:175
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2396
UncondBrInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition IRBuilder.h:1220
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition IRBuilder.h:2557
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1449
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1521
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition IRBuilder.h:2131
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2571
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:181
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:1632
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition IRBuilder.h:2502
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="", bool IsDisjoint=false)
Definition IRBuilder.h:1602
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:348
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
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)
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
LibFunc getLibFunc(StringRef funcName) const
Searches for a particular function name.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI MemCmpExpansionOptions enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const
LLVM_ABI unsigned getIntegerBitWidth() const
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
user_iterator user_begin()
Definition Value.h:402
LLVM_ABI bool hasOneUser() const
Return true if there is exactly one user of this value.
Definition Value.cpp:163
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
LLVM_ABI Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition Value.cpp:1002
const ParentTy * getParent() const
Definition ilist_node.h:34
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ Entry
Definition COFF.h:862
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
AllOnesConstantMatch m_AllOnes()
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
auto m_Value()
Match an arbitrary value and ignore it.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BlockType
Used as immediate MachineOperands for block signatures.
initializer< Ty > init(const Ty &Val)
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1765
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
LLVM_ABI bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...
Definition Local.cpp:715
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:380
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
TargetTransformInfo TTI
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ Or
Bitwise or logical OR of integers.
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2192
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, APInt Offset, const DataLayout &DL)
Return the value that a load from C with offset Offset would produce if it is constant and determinab...
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106