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