LLVM 20.0.0git
AggressiveInstCombine.cpp
Go to the documentation of this file.
1//===- AggressiveInstCombine.cpp ------------------------------------------===//
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 file implements the aggressive expression pattern combiner classes.
10// Currently, it handles expression patterns for:
11// * Truncate instruction
12//
13//===----------------------------------------------------------------------===//
14
17#include "llvm/ADT/Statistic.h"
27#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/Dominators.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/IRBuilder.h"
35
36using namespace llvm;
37using namespace PatternMatch;
38
39#define DEBUG_TYPE "aggressive-instcombine"
40
41STATISTIC(NumAnyOrAllBitsSet, "Number of any/all-bits-set patterns folded");
42STATISTIC(NumGuardedRotates,
43 "Number of guarded rotates transformed into funnel shifts");
44STATISTIC(NumGuardedFunnelShifts,
45 "Number of guarded funnel shifts transformed into funnel shifts");
46STATISTIC(NumPopCountRecognized, "Number of popcount idioms recognized");
47
49 "aggressive-instcombine-max-scan-instrs", cl::init(64), cl::Hidden,
50 cl::desc("Max number of instructions to scan for aggressive instcombine."));
51
53 "strncmp-inline-threshold", cl::init(3), cl::Hidden,
54 cl::desc("The maximum length of a constant string for a builtin string cmp "
55 "call eligible for inlining. The default value is 3."));
56
58 MemChrInlineThreshold("memchr-inline-threshold", cl::init(3), cl::Hidden,
59 cl::desc("The maximum length of a constant string to "
60 "inline a memchr call."));
61
62/// Match a pattern for a bitwise funnel/rotate operation that partially guards
63/// against undefined behavior by branching around the funnel-shift/rotation
64/// when the shift amount is 0.
66 if (I.getOpcode() != Instruction::PHI || I.getNumOperands() != 2)
67 return false;
68
69 // As with the one-use checks below, this is not strictly necessary, but we
70 // are being cautious to avoid potential perf regressions on targets that
71 // do not actually have a funnel/rotate instruction (where the funnel shift
72 // would be expanded back into math/shift/logic ops).
73 if (!isPowerOf2_32(I.getType()->getScalarSizeInBits()))
74 return false;
75
76 // Match V to funnel shift left/right and capture the source operands and
77 // shift amount.
78 auto matchFunnelShift = [](Value *V, Value *&ShVal0, Value *&ShVal1,
79 Value *&ShAmt) {
80 unsigned Width = V->getType()->getScalarSizeInBits();
81
82 // fshl(ShVal0, ShVal1, ShAmt)
83 // == (ShVal0 << ShAmt) | (ShVal1 >> (Width -ShAmt))
84 if (match(V, m_OneUse(m_c_Or(
85 m_Shl(m_Value(ShVal0), m_Value(ShAmt)),
86 m_LShr(m_Value(ShVal1),
87 m_Sub(m_SpecificInt(Width), m_Deferred(ShAmt))))))) {
88 return Intrinsic::fshl;
89 }
90
91 // fshr(ShVal0, ShVal1, ShAmt)
92 // == (ShVal0 >> ShAmt) | (ShVal1 << (Width - ShAmt))
93 if (match(V,
95 m_Value(ShAmt))),
96 m_LShr(m_Value(ShVal1), m_Deferred(ShAmt)))))) {
97 return Intrinsic::fshr;
98 }
99
101 };
102
103 // One phi operand must be a funnel/rotate operation, and the other phi
104 // operand must be the source value of that funnel/rotate operation:
105 // phi [ rotate(RotSrc, ShAmt), FunnelBB ], [ RotSrc, GuardBB ]
106 // phi [ fshl(ShVal0, ShVal1, ShAmt), FunnelBB ], [ ShVal0, GuardBB ]
107 // phi [ fshr(ShVal0, ShVal1, ShAmt), FunnelBB ], [ ShVal1, GuardBB ]
108 PHINode &Phi = cast<PHINode>(I);
109 unsigned FunnelOp = 0, GuardOp = 1;
110 Value *P0 = Phi.getOperand(0), *P1 = Phi.getOperand(1);
111 Value *ShVal0, *ShVal1, *ShAmt;
112 Intrinsic::ID IID = matchFunnelShift(P0, ShVal0, ShVal1, ShAmt);
113 if (IID == Intrinsic::not_intrinsic ||
114 (IID == Intrinsic::fshl && ShVal0 != P1) ||
115 (IID == Intrinsic::fshr && ShVal1 != P1)) {
116 IID = matchFunnelShift(P1, ShVal0, ShVal1, ShAmt);
117 if (IID == Intrinsic::not_intrinsic ||
118 (IID == Intrinsic::fshl && ShVal0 != P0) ||
119 (IID == Intrinsic::fshr && ShVal1 != P0))
120 return false;
121 assert((IID == Intrinsic::fshl || IID == Intrinsic::fshr) &&
122 "Pattern must match funnel shift left or right");
123 std::swap(FunnelOp, GuardOp);
124 }
125
126 // The incoming block with our source operand must be the "guard" block.
127 // That must contain a cmp+branch to avoid the funnel/rotate when the shift
128 // amount is equal to 0. The other incoming block is the block with the
129 // funnel/rotate.
130 BasicBlock *GuardBB = Phi.getIncomingBlock(GuardOp);
131 BasicBlock *FunnelBB = Phi.getIncomingBlock(FunnelOp);
132 Instruction *TermI = GuardBB->getTerminator();
133
134 // Ensure that the shift values dominate each block.
135 if (!DT.dominates(ShVal0, TermI) || !DT.dominates(ShVal1, TermI))
136 return false;
137
138 BasicBlock *PhiBB = Phi.getParent();
140 m_ZeroInt()),
141 m_SpecificBB(PhiBB), m_SpecificBB(FunnelBB))))
142 return false;
143
144 IRBuilder<> Builder(PhiBB, PhiBB->getFirstInsertionPt());
145
146 if (ShVal0 == ShVal1)
147 ++NumGuardedRotates;
148 else
149 ++NumGuardedFunnelShifts;
150
151 // If this is not a rotate then the select was blocking poison from the
152 // 'shift-by-zero' non-TVal, but a funnel shift won't - so freeze it.
153 bool IsFshl = IID == Intrinsic::fshl;
154 if (ShVal0 != ShVal1) {
155 if (IsFshl && !llvm::isGuaranteedNotToBePoison(ShVal1))
156 ShVal1 = Builder.CreateFreeze(ShVal1);
157 else if (!IsFshl && !llvm::isGuaranteedNotToBePoison(ShVal0))
158 ShVal0 = Builder.CreateFreeze(ShVal0);
159 }
160
161 // We matched a variation of this IR pattern:
162 // GuardBB:
163 // %cmp = icmp eq i32 %ShAmt, 0
164 // br i1 %cmp, label %PhiBB, label %FunnelBB
165 // FunnelBB:
166 // %sub = sub i32 32, %ShAmt
167 // %shr = lshr i32 %ShVal1, %sub
168 // %shl = shl i32 %ShVal0, %ShAmt
169 // %fsh = or i32 %shr, %shl
170 // br label %PhiBB
171 // PhiBB:
172 // %cond = phi i32 [ %fsh, %FunnelBB ], [ %ShVal0, %GuardBB ]
173 // -->
174 // llvm.fshl.i32(i32 %ShVal0, i32 %ShVal1, i32 %ShAmt)
175 Phi.replaceAllUsesWith(
176 Builder.CreateIntrinsic(IID, Phi.getType(), {ShVal0, ShVal1, ShAmt}));
177 return true;
178}
179
180/// This is used by foldAnyOrAllBitsSet() to capture a source value (Root) and
181/// the bit indexes (Mask) needed by a masked compare. If we're matching a chain
182/// of 'and' ops, then we also need to capture the fact that we saw an
183/// "and X, 1", so that's an extra return value for that case.
184namespace {
185struct MaskOps {
186 Value *Root = nullptr;
187 APInt Mask;
188 bool MatchAndChain;
189 bool FoundAnd1 = false;
190
191 MaskOps(unsigned BitWidth, bool MatchAnds)
192 : Mask(APInt::getZero(BitWidth)), MatchAndChain(MatchAnds) {}
193};
194} // namespace
195
196/// This is a recursive helper for foldAnyOrAllBitsSet() that walks through a
197/// chain of 'and' or 'or' instructions looking for shift ops of a common source
198/// value. Examples:
199/// or (or (or X, (X >> 3)), (X >> 5)), (X >> 8)
200/// returns { X, 0x129 }
201/// and (and (X >> 1), 1), (X >> 4)
202/// returns { X, 0x12 }
203static bool matchAndOrChain(Value *V, MaskOps &MOps) {
204 Value *Op0, *Op1;
205 if (MOps.MatchAndChain) {
206 // Recurse through a chain of 'and' operands. This requires an extra check
207 // vs. the 'or' matcher: we must find an "and X, 1" instruction somewhere
208 // in the chain to know that all of the high bits are cleared.
209 if (match(V, m_And(m_Value(Op0), m_One()))) {
210 MOps.FoundAnd1 = true;
211 return matchAndOrChain(Op0, MOps);
212 }
213 if (match(V, m_And(m_Value(Op0), m_Value(Op1))))
214 return matchAndOrChain(Op0, MOps) && matchAndOrChain(Op1, MOps);
215 } else {
216 // Recurse through a chain of 'or' operands.
217 if (match(V, m_Or(m_Value(Op0), m_Value(Op1))))
218 return matchAndOrChain(Op0, MOps) && matchAndOrChain(Op1, MOps);
219 }
220
221 // We need a shift-right or a bare value representing a compare of bit 0 of
222 // the original source operand.
223 Value *Candidate;
224 const APInt *BitIndex = nullptr;
225 if (!match(V, m_LShr(m_Value(Candidate), m_APInt(BitIndex))))
226 Candidate = V;
227
228 // Initialize result source operand.
229 if (!MOps.Root)
230 MOps.Root = Candidate;
231
232 // The shift constant is out-of-range? This code hasn't been simplified.
233 if (BitIndex && BitIndex->uge(MOps.Mask.getBitWidth()))
234 return false;
235
236 // Fill in the mask bit derived from the shift constant.
237 MOps.Mask.setBit(BitIndex ? BitIndex->getZExtValue() : 0);
238 return MOps.Root == Candidate;
239}
240
241/// Match patterns that correspond to "any-bits-set" and "all-bits-set".
242/// These will include a chain of 'or' or 'and'-shifted bits from a
243/// common source value:
244/// and (or (lshr X, C), ...), 1 --> (X & CMask) != 0
245/// and (and (lshr X, C), ...), 1 --> (X & CMask) == CMask
246/// Note: "any-bits-clear" and "all-bits-clear" are variations of these patterns
247/// that differ only with a final 'not' of the result. We expect that final
248/// 'not' to be folded with the compare that we create here (invert predicate).
250 // The 'any-bits-set' ('or' chain) pattern is simpler to match because the
251 // final "and X, 1" instruction must be the final op in the sequence.
252 bool MatchAllBitsSet;
254 MatchAllBitsSet = true;
255 else if (match(&I, m_And(m_OneUse(m_Or(m_Value(), m_Value())), m_One())))
256 MatchAllBitsSet = false;
257 else
258 return false;
259
260 MaskOps MOps(I.getType()->getScalarSizeInBits(), MatchAllBitsSet);
261 if (MatchAllBitsSet) {
262 if (!matchAndOrChain(cast<BinaryOperator>(&I), MOps) || !MOps.FoundAnd1)
263 return false;
264 } else {
265 if (!matchAndOrChain(cast<BinaryOperator>(&I)->getOperand(0), MOps))
266 return false;
267 }
268
269 // The pattern was found. Create a masked compare that replaces all of the
270 // shift and logic ops.
271 IRBuilder<> Builder(&I);
272 Constant *Mask = ConstantInt::get(I.getType(), MOps.Mask);
273 Value *And = Builder.CreateAnd(MOps.Root, Mask);
274 Value *Cmp = MatchAllBitsSet ? Builder.CreateICmpEQ(And, Mask)
275 : Builder.CreateIsNotNull(And);
276 Value *Zext = Builder.CreateZExt(Cmp, I.getType());
277 I.replaceAllUsesWith(Zext);
278 ++NumAnyOrAllBitsSet;
279 return true;
280}
281
282// Try to recognize below function as popcount intrinsic.
283// This is the "best" algorithm from
284// http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
285// Also used in TargetLowering::expandCTPOP().
286//
287// int popcount(unsigned int i) {
288// i = i - ((i >> 1) & 0x55555555);
289// i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
290// i = ((i + (i >> 4)) & 0x0F0F0F0F);
291// return (i * 0x01010101) >> 24;
292// }
294 if (I.getOpcode() != Instruction::LShr)
295 return false;
296
297 Type *Ty = I.getType();
298 if (!Ty->isIntOrIntVectorTy())
299 return false;
300
301 unsigned Len = Ty->getScalarSizeInBits();
302 // FIXME: fix Len == 8 and other irregular type lengths.
303 if (!(Len <= 128 && Len > 8 && Len % 8 == 0))
304 return false;
305
306 APInt Mask55 = APInt::getSplat(Len, APInt(8, 0x55));
307 APInt Mask33 = APInt::getSplat(Len, APInt(8, 0x33));
308 APInt Mask0F = APInt::getSplat(Len, APInt(8, 0x0F));
309 APInt Mask01 = APInt::getSplat(Len, APInt(8, 0x01));
310 APInt MaskShift = APInt(Len, Len - 8);
311
312 Value *Op0 = I.getOperand(0);
313 Value *Op1 = I.getOperand(1);
314 Value *MulOp0;
315 // Matching "(i * 0x01010101...) >> 24".
316 if ((match(Op0, m_Mul(m_Value(MulOp0), m_SpecificInt(Mask01)))) &&
318 Value *ShiftOp0;
319 // Matching "((i + (i >> 4)) & 0x0F0F0F0F...)".
320 if (match(MulOp0, m_And(m_c_Add(m_LShr(m_Value(ShiftOp0), m_SpecificInt(4)),
321 m_Deferred(ShiftOp0)),
322 m_SpecificInt(Mask0F)))) {
323 Value *AndOp0;
324 // Matching "(i & 0x33333333...) + ((i >> 2) & 0x33333333...)".
325 if (match(ShiftOp0,
326 m_c_Add(m_And(m_Value(AndOp0), m_SpecificInt(Mask33)),
328 m_SpecificInt(Mask33))))) {
329 Value *Root, *SubOp1;
330 // Matching "i - ((i >> 1) & 0x55555555...)".
331 if (match(AndOp0, m_Sub(m_Value(Root), m_Value(SubOp1))) &&
332 match(SubOp1, m_And(m_LShr(m_Specific(Root), m_SpecificInt(1)),
333 m_SpecificInt(Mask55)))) {
334 LLVM_DEBUG(dbgs() << "Recognized popcount intrinsic\n");
335 IRBuilder<> Builder(&I);
336 I.replaceAllUsesWith(
337 Builder.CreateIntrinsic(Intrinsic::ctpop, I.getType(), {Root}));
338 ++NumPopCountRecognized;
339 return true;
340 }
341 }
342 }
343 }
344
345 return false;
346}
347
348/// Fold smin(smax(fptosi(x), C1), C2) to llvm.fptosi.sat(x), providing C1 and
349/// C2 saturate the value of the fp conversion. The transform is not reversable
350/// as the fptosi.sat is more defined than the input - all values produce a
351/// valid value for the fptosi.sat, where as some produce poison for original
352/// that were out of range of the integer conversion. The reversed pattern may
353/// use fmax and fmin instead. As we cannot directly reverse the transform, and
354/// it is not always profitable, we make it conditional on the cost being
355/// reported as lower by TTI.
357 // Look for min(max(fptosi, converting to fptosi_sat.
358 Value *In;
359 const APInt *MinC, *MaxC;
361 m_APInt(MinC))),
362 m_APInt(MaxC))) &&
364 m_APInt(MaxC))),
365 m_APInt(MinC))))
366 return false;
367
368 // Check that the constants clamp a saturate.
369 if (!(*MinC + 1).isPowerOf2() || -*MaxC != *MinC + 1)
370 return false;
371
372 Type *IntTy = I.getType();
373 Type *FpTy = In->getType();
374 Type *SatTy =
375 IntegerType::get(IntTy->getContext(), (*MinC + 1).exactLogBase2() + 1);
376 if (auto *VecTy = dyn_cast<VectorType>(IntTy))
377 SatTy = VectorType::get(SatTy, VecTy->getElementCount());
378
379 // Get the cost of the intrinsic, and check that against the cost of
380 // fptosi+smin+smax
382 IntrinsicCostAttributes(Intrinsic::fptosi_sat, SatTy, {In}, {FpTy}),
384 SatCost += TTI.getCastInstrCost(Instruction::SExt, IntTy, SatTy,
387
389 Instruction::FPToSI, IntTy, FpTy, TTI::CastContextHint::None,
391 MinMaxCost += TTI.getIntrinsicInstrCost(
392 IntrinsicCostAttributes(Intrinsic::smin, IntTy, {IntTy}),
394 MinMaxCost += TTI.getIntrinsicInstrCost(
395 IntrinsicCostAttributes(Intrinsic::smax, IntTy, {IntTy}),
397
398 if (SatCost >= MinMaxCost)
399 return false;
400
401 IRBuilder<> Builder(&I);
402 Value *Sat =
403 Builder.CreateIntrinsic(Intrinsic::fptosi_sat, {SatTy, FpTy}, In);
404 I.replaceAllUsesWith(Builder.CreateSExt(Sat, IntTy));
405 return true;
406}
407
408/// Try to replace a mathlib call to sqrt with the LLVM intrinsic. This avoids
409/// pessimistic codegen that has to account for setting errno and can enable
410/// vectorization.
413 DominatorTree &DT) {
414 // If (1) this is a sqrt libcall, (2) we can assume that NAN is not created
415 // (because NNAN or the operand arg must not be less than -0.0) and (2) we
416 // would not end up lowering to a libcall anyway (which could change the value
417 // of errno), then:
418 // (1) errno won't be set.
419 // (2) it is safe to convert this to an intrinsic call.
420 Type *Ty = Call->getType();
421 Value *Arg = Call->getArgOperand(0);
422 if (TTI.haveFastSqrt(Ty) &&
423 (Call->hasNoNaNs() ||
425 Arg, 0,
426 SimplifyQuery(Call->getDataLayout(), &TLI, &DT, &AC, Call)))) {
427 IRBuilder<> Builder(Call);
428 Value *NewSqrt =
429 Builder.CreateIntrinsic(Intrinsic::sqrt, Ty, Arg, Call, "sqrt");
430 Call->replaceAllUsesWith(NewSqrt);
431
432 // Explicitly erase the old call because a call with side effects is not
433 // trivially dead.
434 Call->eraseFromParent();
435 return true;
436 }
437
438 return false;
439}
440
441// Check if this array of constants represents a cttz table.
442// Iterate over the elements from \p Table by trying to find/match all
443// the numbers from 0 to \p InputBits that should represent cttz results.
444static bool isCTTZTable(const ConstantDataArray &Table, uint64_t Mul,
445 uint64_t Shift, uint64_t InputBits) {
446 unsigned Length = Table.getNumElements();
447 if (Length < InputBits || Length > InputBits * 2)
448 return false;
449
450 APInt Mask = APInt::getBitsSetFrom(InputBits, Shift);
451 unsigned Matched = 0;
452
453 for (unsigned i = 0; i < Length; i++) {
454 uint64_t Element = Table.getElementAsInteger(i);
455 if (Element >= InputBits)
456 continue;
457
458 // Check if \p Element matches a concrete answer. It could fail for some
459 // elements that are never accessed, so we keep iterating over each element
460 // from the table. The number of matched elements should be equal to the
461 // number of potential right answers which is \p InputBits actually.
462 if ((((Mul << Element) & Mask.getZExtValue()) >> Shift) == i)
463 Matched++;
464 }
465
466 return Matched == InputBits;
467}
468
469// Try to recognize table-based ctz implementation.
470// E.g., an example in C (for more cases please see the llvm/tests):
471// int f(unsigned x) {
472// static const char table[32] =
473// {0, 1, 28, 2, 29, 14, 24, 3, 30,
474// 22, 20, 15, 25, 17, 4, 8, 31, 27,
475// 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
476// return table[((unsigned)((x & -x) * 0x077CB531U)) >> 27];
477// }
478// this can be lowered to `cttz` instruction.
479// There is also a special case when the element is 0.
480//
481// Here are some examples or LLVM IR for a 64-bit target:
482//
483// CASE 1:
484// %sub = sub i32 0, %x
485// %and = and i32 %sub, %x
486// %mul = mul i32 %and, 125613361
487// %shr = lshr i32 %mul, 27
488// %idxprom = zext i32 %shr to i64
489// %arrayidx = getelementptr inbounds [32 x i8], [32 x i8]* @ctz1.table, i64 0,
490// i64 %idxprom
491// %0 = load i8, i8* %arrayidx, align 1, !tbaa !8
492//
493// CASE 2:
494// %sub = sub i32 0, %x
495// %and = and i32 %sub, %x
496// %mul = mul i32 %and, 72416175
497// %shr = lshr i32 %mul, 26
498// %idxprom = zext i32 %shr to i64
499// %arrayidx = getelementptr inbounds [64 x i16], [64 x i16]* @ctz2.table,
500// i64 0, i64 %idxprom
501// %0 = load i16, i16* %arrayidx, align 2, !tbaa !8
502//
503// CASE 3:
504// %sub = sub i32 0, %x
505// %and = and i32 %sub, %x
506// %mul = mul i32 %and, 81224991
507// %shr = lshr i32 %mul, 27
508// %idxprom = zext i32 %shr to i64
509// %arrayidx = getelementptr inbounds [32 x i32], [32 x i32]* @ctz3.table,
510// i64 0, i64 %idxprom
511// %0 = load i32, i32* %arrayidx, align 4, !tbaa !8
512//
513// CASE 4:
514// %sub = sub i64 0, %x
515// %and = and i64 %sub, %x
516// %mul = mul i64 %and, 283881067100198605
517// %shr = lshr i64 %mul, 58
518// %arrayidx = getelementptr inbounds [64 x i8], [64 x i8]* @table, i64 0,
519// i64 %shr
520// %0 = load i8, i8* %arrayidx, align 1, !tbaa !8
521//
522// All this can be lowered to @llvm.cttz.i32/64 intrinsic.
524 LoadInst *LI = dyn_cast<LoadInst>(&I);
525 if (!LI)
526 return false;
527
528 Type *AccessType = LI->getType();
529 if (!AccessType->isIntegerTy())
530 return false;
531
532 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getPointerOperand());
533 if (!GEP || !GEP->isInBounds() || GEP->getNumIndices() != 2)
534 return false;
535
536 if (!GEP->getSourceElementType()->isArrayTy())
537 return false;
538
539 uint64_t ArraySize = GEP->getSourceElementType()->getArrayNumElements();
540 if (ArraySize != 32 && ArraySize != 64)
541 return false;
542
543 GlobalVariable *GVTable = dyn_cast<GlobalVariable>(GEP->getPointerOperand());
544 if (!GVTable || !GVTable->hasInitializer() || !GVTable->isConstant())
545 return false;
546
547 ConstantDataArray *ConstData =
548 dyn_cast<ConstantDataArray>(GVTable->getInitializer());
549 if (!ConstData)
550 return false;
551
552 if (!match(GEP->idx_begin()->get(), m_ZeroInt()))
553 return false;
554
555 Value *Idx2 = std::next(GEP->idx_begin())->get();
556 Value *X1;
557 uint64_t MulConst, ShiftConst;
558 // FIXME: 64-bit targets have `i64` type for the GEP index, so this match will
559 // probably fail for other (e.g. 32-bit) targets.
560 if (!match(Idx2, m_ZExtOrSelf(
562 m_ConstantInt(MulConst)),
563 m_ConstantInt(ShiftConst)))))
564 return false;
565
566 unsigned InputBits = X1->getType()->getScalarSizeInBits();
567 if (InputBits != 32 && InputBits != 64)
568 return false;
569
570 // Shift should extract top 5..7 bits.
571 if (InputBits - Log2_32(InputBits) != ShiftConst &&
572 InputBits - Log2_32(InputBits) - 1 != ShiftConst)
573 return false;
574
575 if (!isCTTZTable(*ConstData, MulConst, ShiftConst, InputBits))
576 return false;
577
578 auto ZeroTableElem = ConstData->getElementAsInteger(0);
579 bool DefinedForZero = ZeroTableElem == InputBits;
580
581 IRBuilder<> B(LI);
582 ConstantInt *BoolConst = B.getInt1(!DefinedForZero);
583 Type *XType = X1->getType();
584 auto Cttz = B.CreateIntrinsic(Intrinsic::cttz, {XType}, {X1, BoolConst});
585 Value *ZExtOrTrunc = nullptr;
586
587 if (DefinedForZero) {
588 ZExtOrTrunc = B.CreateZExtOrTrunc(Cttz, AccessType);
589 } else {
590 // If the value in elem 0 isn't the same as InputBits, we still want to
591 // produce the value from the table.
592 auto Cmp = B.CreateICmpEQ(X1, ConstantInt::get(XType, 0));
593 auto Select =
594 B.CreateSelect(Cmp, ConstantInt::get(XType, ZeroTableElem), Cttz);
595
596 // NOTE: If the table[0] is 0, but the cttz(0) is defined by the Target
597 // it should be handled as: `cttz(x) & (typeSize - 1)`.
598
599 ZExtOrTrunc = B.CreateZExtOrTrunc(Select, AccessType);
600 }
601
602 LI->replaceAllUsesWith(ZExtOrTrunc);
603
604 return true;
605}
606
607/// This is used by foldLoadsRecursive() to capture a Root Load node which is
608/// of type or(load, load) and recursively build the wide load. Also capture the
609/// shift amount, zero extend type and loadSize.
610struct LoadOps {
611 LoadInst *Root = nullptr;
613 bool FoundRoot = false;
615 const APInt *Shift = nullptr;
618};
619
620// Identify and Merge consecutive loads recursively which is of the form
621// (ZExt(L1) << shift1) | (ZExt(L2) << shift2) -> ZExt(L3) << shift1
622// (ZExt(L1) << shift1) | ZExt(L2) -> ZExt(L3)
623static bool foldLoadsRecursive(Value *V, LoadOps &LOps, const DataLayout &DL,
624 AliasAnalysis &AA) {
625 const APInt *ShAmt2 = nullptr;
626 Value *X;
627 Instruction *L1, *L2;
628
629 // Go to the last node with loads.
630 if (match(V, m_OneUse(m_c_Or(
631 m_Value(X),
633 m_APInt(ShAmt2)))))) ||
636 if (!foldLoadsRecursive(X, LOps, DL, AA) && LOps.FoundRoot)
637 // Avoid Partial chain merge.
638 return false;
639 } else
640 return false;
641
642 // Check if the pattern has loads
643 LoadInst *LI1 = LOps.Root;
644 const APInt *ShAmt1 = LOps.Shift;
645 if (LOps.FoundRoot == false &&
648 m_APInt(ShAmt1)))))) {
649 LI1 = dyn_cast<LoadInst>(L1);
650 }
651 LoadInst *LI2 = dyn_cast<LoadInst>(L2);
652
653 // Check if loads are same, atomic, volatile and having same address space.
654 if (LI1 == LI2 || !LI1 || !LI2 || !LI1->isSimple() || !LI2->isSimple() ||
656 return false;
657
658 // Check if Loads come from same BB.
659 if (LI1->getParent() != LI2->getParent())
660 return false;
661
662 // Find the data layout
663 bool IsBigEndian = DL.isBigEndian();
664
665 // Check if loads are consecutive and same size.
666 Value *Load1Ptr = LI1->getPointerOperand();
667 APInt Offset1(DL.getIndexTypeSizeInBits(Load1Ptr->getType()), 0);
668 Load1Ptr =
669 Load1Ptr->stripAndAccumulateConstantOffsets(DL, Offset1,
670 /* AllowNonInbounds */ true);
671
672 Value *Load2Ptr = LI2->getPointerOperand();
673 APInt Offset2(DL.getIndexTypeSizeInBits(Load2Ptr->getType()), 0);
674 Load2Ptr =
675 Load2Ptr->stripAndAccumulateConstantOffsets(DL, Offset2,
676 /* AllowNonInbounds */ true);
677
678 // Verify if both loads have same base pointers and load sizes are same.
679 uint64_t LoadSize1 = LI1->getType()->getPrimitiveSizeInBits();
680 uint64_t LoadSize2 = LI2->getType()->getPrimitiveSizeInBits();
681 if (Load1Ptr != Load2Ptr || LoadSize1 != LoadSize2)
682 return false;
683
684 // Support Loadsizes greater or equal to 8bits and only power of 2.
685 if (LoadSize1 < 8 || !isPowerOf2_64(LoadSize1))
686 return false;
687
688 // Alias Analysis to check for stores b/w the loads.
689 LoadInst *Start = LOps.FoundRoot ? LOps.RootInsert : LI1, *End = LI2;
690 MemoryLocation Loc;
691 if (!Start->comesBefore(End)) {
692 std::swap(Start, End);
694 if (LOps.FoundRoot)
695 Loc = Loc.getWithNewSize(LOps.LoadSize);
696 } else
698 unsigned NumScanned = 0;
699 for (Instruction &Inst :
700 make_range(Start->getIterator(), End->getIterator())) {
701 if (Inst.mayWriteToMemory() && isModSet(AA.getModRefInfo(&Inst, Loc)))
702 return false;
703
704 // Ignore debug info so that's not counted against MaxInstrsToScan.
705 // Otherwise debug info could affect codegen.
706 if (!isa<DbgInfoIntrinsic>(Inst) && ++NumScanned > MaxInstrsToScan)
707 return false;
708 }
709
710 // Make sure Load with lower Offset is at LI1
711 bool Reverse = false;
712 if (Offset2.slt(Offset1)) {
713 std::swap(LI1, LI2);
714 std::swap(ShAmt1, ShAmt2);
715 std::swap(Offset1, Offset2);
716 std::swap(Load1Ptr, Load2Ptr);
717 std::swap(LoadSize1, LoadSize2);
718 Reverse = true;
719 }
720
721 // Big endian swap the shifts
722 if (IsBigEndian)
723 std::swap(ShAmt1, ShAmt2);
724
725 // Find Shifts values.
726 uint64_t Shift1 = 0, Shift2 = 0;
727 if (ShAmt1)
728 Shift1 = ShAmt1->getZExtValue();
729 if (ShAmt2)
730 Shift2 = ShAmt2->getZExtValue();
731
732 // First load is always LI1. This is where we put the new load.
733 // Use the merged load size available from LI1 for forward loads.
734 if (LOps.FoundRoot) {
735 if (!Reverse)
736 LoadSize1 = LOps.LoadSize;
737 else
738 LoadSize2 = LOps.LoadSize;
739 }
740
741 // Verify if shift amount and load index aligns and verifies that loads
742 // are consecutive.
743 uint64_t ShiftDiff = IsBigEndian ? LoadSize2 : LoadSize1;
744 uint64_t PrevSize =
745 DL.getTypeStoreSize(IntegerType::get(LI1->getContext(), LoadSize1));
746 if ((Shift2 - Shift1) != ShiftDiff || (Offset2 - Offset1) != PrevSize)
747 return false;
748
749 // Update LOps
750 AAMDNodes AATags1 = LOps.AATags;
751 AAMDNodes AATags2 = LI2->getAAMetadata();
752 if (LOps.FoundRoot == false) {
753 LOps.FoundRoot = true;
754 AATags1 = LI1->getAAMetadata();
755 }
756 LOps.LoadSize = LoadSize1 + LoadSize2;
757 LOps.RootInsert = Start;
758
759 // Concatenate the AATags of the Merged Loads.
760 LOps.AATags = AATags1.concat(AATags2);
761
762 LOps.Root = LI1;
763 LOps.Shift = ShAmt1;
764 LOps.ZextType = X->getType();
765 return true;
766}
767
768// For a given BB instruction, evaluate all loads in the chain that form a
769// pattern which suggests that the loads can be combined. The one and only use
770// of the loads is to form a wider load.
773 const DominatorTree &DT) {
774 // Only consider load chains of scalar values.
775 if (isa<VectorType>(I.getType()))
776 return false;
777
778 LoadOps LOps;
779 if (!foldLoadsRecursive(&I, LOps, DL, AA) || !LOps.FoundRoot)
780 return false;
781
782 IRBuilder<> Builder(&I);
783 LoadInst *NewLoad = nullptr, *LI1 = LOps.Root;
784
785 IntegerType *WiderType = IntegerType::get(I.getContext(), LOps.LoadSize);
786 // TTI based checks if we want to proceed with wider load
787 bool Allowed = TTI.isTypeLegal(WiderType);
788 if (!Allowed)
789 return false;
790
791 unsigned AS = LI1->getPointerAddressSpace();
792 unsigned Fast = 0;
793 Allowed = TTI.allowsMisalignedMemoryAccesses(I.getContext(), LOps.LoadSize,
794 AS, LI1->getAlign(), &Fast);
795 if (!Allowed || !Fast)
796 return false;
797
798 // Get the Index and Ptr for the new GEP.
799 Value *Load1Ptr = LI1->getPointerOperand();
800 Builder.SetInsertPoint(LOps.RootInsert);
801 if (!DT.dominates(Load1Ptr, LOps.RootInsert)) {
802 APInt Offset1(DL.getIndexTypeSizeInBits(Load1Ptr->getType()), 0);
803 Load1Ptr = Load1Ptr->stripAndAccumulateConstantOffsets(
804 DL, Offset1, /* AllowNonInbounds */ true);
805 Load1Ptr = Builder.CreatePtrAdd(Load1Ptr, Builder.getInt(Offset1));
806 }
807 // Generate wider load.
808 NewLoad = Builder.CreateAlignedLoad(WiderType, Load1Ptr, LI1->getAlign(),
809 LI1->isVolatile(), "");
810 NewLoad->takeName(LI1);
811 // Set the New Load AATags Metadata.
812 if (LOps.AATags)
813 NewLoad->setAAMetadata(LOps.AATags);
814
815 Value *NewOp = NewLoad;
816 // Check if zero extend needed.
817 if (LOps.ZextType)
818 NewOp = Builder.CreateZExt(NewOp, LOps.ZextType);
819
820 // Check if shift needed. We need to shift with the amount of load1
821 // shift if not zero.
822 if (LOps.Shift)
823 NewOp = Builder.CreateShl(NewOp, ConstantInt::get(I.getContext(), *LOps.Shift));
824 I.replaceAllUsesWith(NewOp);
825
826 return true;
827}
828
829// Calculate GEP Stride and accumulated const ModOffset. Return Stride and
830// ModOffset
831static std::pair<APInt, APInt>
833 unsigned BW = DL.getIndexTypeSizeInBits(PtrOp->getType());
834 std::optional<APInt> Stride;
835 APInt ModOffset(BW, 0);
836 // Return a minimum gep stride, greatest common divisor of consective gep
837 // index scales(c.f. Bézout's identity).
838 while (auto *GEP = dyn_cast<GEPOperator>(PtrOp)) {
840 if (!GEP->collectOffset(DL, BW, VarOffsets, ModOffset))
841 break;
842
843 for (auto [V, Scale] : VarOffsets) {
844 // Only keep a power of two factor for non-inbounds
845 if (!GEP->isInBounds())
846 Scale = APInt::getOneBitSet(Scale.getBitWidth(), Scale.countr_zero());
847
848 if (!Stride)
849 Stride = Scale;
850 else
851 Stride = APIntOps::GreatestCommonDivisor(*Stride, Scale);
852 }
853
854 PtrOp = GEP->getPointerOperand();
855 }
856
857 // Check whether pointer arrives back at Global Variable via at least one GEP.
858 // Even if it doesn't, we can check by alignment.
859 if (!isa<GlobalVariable>(PtrOp) || !Stride)
860 return {APInt(BW, 1), APInt(BW, 0)};
861
862 // In consideration of signed GEP indices, non-negligible offset become
863 // remainder of division by minimum GEP stride.
864 ModOffset = ModOffset.srem(*Stride);
865 if (ModOffset.isNegative())
866 ModOffset += *Stride;
867
868 return {*Stride, ModOffset};
869}
870
871/// If C is a constant patterned array and all valid loaded results for given
872/// alignment are same to a constant, return that constant.
874 auto *LI = dyn_cast<LoadInst>(&I);
875 if (!LI || LI->isVolatile())
876 return false;
877
878 // We can only fold the load if it is from a constant global with definitive
879 // initializer. Skip expensive logic if this is not the case.
880 auto *PtrOp = LI->getPointerOperand();
881 auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(PtrOp));
882 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
883 return false;
884
885 // Bail for large initializers in excess of 4K to avoid too many scans.
886 Constant *C = GV->getInitializer();
887 uint64_t GVSize = DL.getTypeAllocSize(C->getType());
888 if (!GVSize || 4096 < GVSize)
889 return false;
890
891 Type *LoadTy = LI->getType();
892 unsigned BW = DL.getIndexTypeSizeInBits(PtrOp->getType());
893 auto [Stride, ConstOffset] = getStrideAndModOffsetOfGEP(PtrOp, DL);
894
895 // Any possible offset could be multiple of GEP stride. And any valid
896 // offset is multiple of load alignment, so checking only multiples of bigger
897 // one is sufficient to say results' equality.
898 if (auto LA = LI->getAlign();
899 LA <= GV->getAlign().valueOrOne() && Stride.getZExtValue() < LA.value()) {
900 ConstOffset = APInt(BW, 0);
901 Stride = APInt(BW, LA.value());
902 }
903
904 Constant *Ca = ConstantFoldLoadFromConst(C, LoadTy, ConstOffset, DL);
905 if (!Ca)
906 return false;
907
908 unsigned E = GVSize - DL.getTypeStoreSize(LoadTy);
909 for (; ConstOffset.getZExtValue() <= E; ConstOffset += Stride)
910 if (Ca != ConstantFoldLoadFromConst(C, LoadTy, ConstOffset, DL))
911 return false;
912
913 I.replaceAllUsesWith(Ca);
914
915 return true;
916}
917
918namespace {
919class StrNCmpInliner {
920public:
921 StrNCmpInliner(CallInst *CI, LibFunc Func, DomTreeUpdater *DTU,
922 const DataLayout &DL)
923 : CI(CI), Func(Func), DTU(DTU), DL(DL) {}
924
925 bool optimizeStrNCmp();
926
927private:
928 void inlineCompare(Value *LHS, StringRef RHS, uint64_t N, bool Swapped);
929
930 CallInst *CI;
932 DomTreeUpdater *DTU;
933 const DataLayout &DL;
934};
935
936} // namespace
937
938/// First we normalize calls to strncmp/strcmp to the form of
939/// compare(s1, s2, N), which means comparing first N bytes of s1 and s2
940/// (without considering '\0').
941///
942/// Examples:
943///
944/// \code
945/// strncmp(s, "a", 3) -> compare(s, "a", 2)
946/// strncmp(s, "abc", 3) -> compare(s, "abc", 3)
947/// strncmp(s, "a\0b", 3) -> compare(s, "a\0b", 2)
948/// strcmp(s, "a") -> compare(s, "a", 2)
949///
950/// char s2[] = {'a'}
951/// strncmp(s, s2, 3) -> compare(s, s2, 3)
952///
953/// char s2[] = {'a', 'b', 'c', 'd'}
954/// strncmp(s, s2, 3) -> compare(s, s2, 3)
955/// \endcode
956///
957/// We only handle cases where N and exactly one of s1 and s2 are constant.
958/// Cases that s1 and s2 are both constant are already handled by the
959/// instcombine pass.
960///
961/// We do not handle cases where N > StrNCmpInlineThreshold.
962///
963/// We also do not handles cases where N < 2, which are already
964/// handled by the instcombine pass.
965///
966bool StrNCmpInliner::optimizeStrNCmp() {
968 return false;
969
971 return false;
972
973 Value *Str1P = CI->getArgOperand(0);
974 Value *Str2P = CI->getArgOperand(1);
975 // Should be handled elsewhere.
976 if (Str1P == Str2P)
977 return false;
978
979 StringRef Str1, Str2;
980 bool HasStr1 = getConstantStringInfo(Str1P, Str1, /*TrimAtNul=*/false);
981 bool HasStr2 = getConstantStringInfo(Str2P, Str2, /*TrimAtNul=*/false);
982 if (HasStr1 == HasStr2)
983 return false;
984
985 // Note that '\0' and characters after it are not trimmed.
986 StringRef Str = HasStr1 ? Str1 : Str2;
987 Value *StrP = HasStr1 ? Str2P : Str1P;
988
989 size_t Idx = Str.find('\0');
991 if (Func == LibFunc_strncmp) {
992 if (auto *ConstInt = dyn_cast<ConstantInt>(CI->getArgOperand(2)))
993 N = std::min(N, ConstInt->getZExtValue());
994 else
995 return false;
996 }
997 // Now N means how many bytes we need to compare at most.
998 if (N > Str.size() || N < 2 || N > StrNCmpInlineThreshold)
999 return false;
1000
1001 // Cases where StrP has two or more dereferenceable bytes might be better
1002 // optimized elsewhere.
1003 bool CanBeNull = false, CanBeFreed = false;
1004 if (StrP->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed) > 1)
1005 return false;
1006 inlineCompare(StrP, Str, N, HasStr1);
1007 return true;
1008}
1009
1010/// Convert
1011///
1012/// \code
1013/// ret = compare(s1, s2, N)
1014/// \endcode
1015///
1016/// into
1017///
1018/// \code
1019/// ret = (int)s1[0] - (int)s2[0]
1020/// if (ret != 0)
1021/// goto NE
1022/// ...
1023/// ret = (int)s1[N-2] - (int)s2[N-2]
1024/// if (ret != 0)
1025/// goto NE
1026/// ret = (int)s1[N-1] - (int)s2[N-1]
1027/// NE:
1028/// \endcode
1029///
1030/// CFG before and after the transformation:
1031///
1032/// (before)
1033/// BBCI
1034///
1035/// (after)
1036/// BBCI -> BBSubs[0] (sub,icmp) --NE-> BBNE -> BBTail
1037/// | ^
1038/// E |
1039/// | |
1040/// BBSubs[1] (sub,icmp) --NE-----+
1041/// ... |
1042/// BBSubs[N-1] (sub) ---------+
1043///
1044void StrNCmpInliner::inlineCompare(Value *LHS, StringRef RHS, uint64_t N,
1045 bool Swapped) {
1046 auto &Ctx = CI->getContext();
1047 IRBuilder<> B(Ctx);
1048 // We want these instructions to be recognized as inlined instructions for the
1049 // compare call, but we don't have a source location for the definition of
1050 // that function, since we're generating that code now. Because the generated
1051 // code is a viable point for a memory access error, we make the pragmatic
1052 // choice here to directly use CI's location so that we have useful
1053 // attribution for the generated code.
1054 B.SetCurrentDebugLocation(CI->getDebugLoc());
1055
1056 BasicBlock *BBCI = CI->getParent();
1057 BasicBlock *BBTail =
1058 SplitBlock(BBCI, CI, DTU, nullptr, nullptr, BBCI->getName() + ".tail");
1059
1061 for (uint64_t I = 0; I < N; ++I)
1062 BBSubs.push_back(
1063 BasicBlock::Create(Ctx, "sub_" + Twine(I), BBCI->getParent(), BBTail));
1064 BasicBlock *BBNE = BasicBlock::Create(Ctx, "ne", BBCI->getParent(), BBTail);
1065
1066 cast<BranchInst>(BBCI->getTerminator())->setSuccessor(0, BBSubs[0]);
1067
1068 B.SetInsertPoint(BBNE);
1069 PHINode *Phi = B.CreatePHI(CI->getType(), N);
1070 B.CreateBr(BBTail);
1071
1072 Value *Base = LHS;
1073 for (uint64_t i = 0; i < N; ++i) {
1074 B.SetInsertPoint(BBSubs[i]);
1075 Value *VL =
1076 B.CreateZExt(B.CreateLoad(B.getInt8Ty(),
1077 B.CreateInBoundsPtrAdd(Base, B.getInt64(i))),
1078 CI->getType());
1079 Value *VR =
1080 ConstantInt::get(CI->getType(), static_cast<unsigned char>(RHS[i]));
1081 Value *Sub = Swapped ? B.CreateSub(VR, VL) : B.CreateSub(VL, VR);
1082 if (i < N - 1)
1083 B.CreateCondBr(B.CreateICmpNE(Sub, ConstantInt::get(CI->getType(), 0)),
1084 BBNE, BBSubs[i + 1]);
1085 else
1086 B.CreateBr(BBNE);
1087
1088 Phi->addIncoming(Sub, BBSubs[i]);
1089 }
1090
1091 CI->replaceAllUsesWith(Phi);
1092 CI->eraseFromParent();
1093
1094 if (DTU) {
1096 Updates.push_back({DominatorTree::Insert, BBCI, BBSubs[0]});
1097 for (uint64_t i = 0; i < N; ++i) {
1098 if (i < N - 1)
1099 Updates.push_back({DominatorTree::Insert, BBSubs[i], BBSubs[i + 1]});
1100 Updates.push_back({DominatorTree::Insert, BBSubs[i], BBNE});
1101 }
1102 Updates.push_back({DominatorTree::Insert, BBNE, BBTail});
1103 Updates.push_back({DominatorTree::Delete, BBCI, BBTail});
1104 DTU->applyUpdates(Updates);
1105 }
1106}
1107
1108/// Convert memchr with a small constant string into a switch
1109static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU,
1110 const DataLayout &DL) {
1111 if (isa<Constant>(Call->getArgOperand(1)))
1112 return false;
1113
1114 StringRef Str;
1115 Value *Base = Call->getArgOperand(0);
1116 if (!getConstantStringInfo(Base, Str, /*TrimAtNul=*/false))
1117 return false;
1118
1119 uint64_t N = Str.size();
1120 if (auto *ConstInt = dyn_cast<ConstantInt>(Call->getArgOperand(2))) {
1121 uint64_t Val = ConstInt->getZExtValue();
1122 // Ignore the case that n is larger than the size of string.
1123 if (Val > N)
1124 return false;
1125 N = Val;
1126 } else
1127 return false;
1128
1130 return false;
1131
1132 BasicBlock *BB = Call->getParent();
1133 BasicBlock *BBNext = SplitBlock(BB, Call, DTU);
1134 IRBuilder<> IRB(BB);
1135 IntegerType *ByteTy = IRB.getInt8Ty();
1137 SwitchInst *SI = IRB.CreateSwitch(
1138 IRB.CreateTrunc(Call->getArgOperand(1), ByteTy), BBNext, N);
1139 Type *IndexTy = DL.getIndexType(Call->getType());
1141
1142 BasicBlock *BBSuccess = BasicBlock::Create(
1143 Call->getContext(), "memchr.success", BB->getParent(), BBNext);
1144 IRB.SetInsertPoint(BBSuccess);
1145 PHINode *IndexPHI = IRB.CreatePHI(IndexTy, N, "memchr.idx");
1146 Value *FirstOccursLocation = IRB.CreateInBoundsPtrAdd(Base, IndexPHI);
1147 IRB.CreateBr(BBNext);
1148 if (DTU)
1149 Updates.push_back({DominatorTree::Insert, BBSuccess, BBNext});
1150
1152 for (uint64_t I = 0; I < N; ++I) {
1153 ConstantInt *CaseVal = ConstantInt::get(ByteTy, Str[I]);
1154 if (!Cases.insert(CaseVal).second)
1155 continue;
1156
1157 BasicBlock *BBCase = BasicBlock::Create(Call->getContext(), "memchr.case",
1158 BB->getParent(), BBSuccess);
1159 SI->addCase(CaseVal, BBCase);
1160 IRB.SetInsertPoint(BBCase);
1161 IndexPHI->addIncoming(ConstantInt::get(IndexTy, I), BBCase);
1162 IRB.CreateBr(BBSuccess);
1163 if (DTU) {
1164 Updates.push_back({DominatorTree::Insert, BB, BBCase});
1165 Updates.push_back({DominatorTree::Insert, BBCase, BBSuccess});
1166 }
1167 }
1168
1169 PHINode *PHI =
1170 PHINode::Create(Call->getType(), 2, Call->getName(), BBNext->begin());
1171 PHI->addIncoming(Constant::getNullValue(Call->getType()), BB);
1172 PHI->addIncoming(FirstOccursLocation, BBSuccess);
1173
1174 Call->replaceAllUsesWith(PHI);
1175 Call->eraseFromParent();
1176
1177 if (DTU)
1178 DTU->applyUpdates(Updates);
1179
1180 return true;
1181}
1182
1185 DominatorTree &DT, const DataLayout &DL,
1186 bool &MadeCFGChange) {
1187
1188 auto *CI = dyn_cast<CallInst>(&I);
1189 if (!CI || CI->isNoBuiltin())
1190 return false;
1191
1192 Function *CalledFunc = CI->getCalledFunction();
1193 if (!CalledFunc)
1194 return false;
1195
1196 LibFunc LF;
1197 if (!TLI.getLibFunc(*CalledFunc, LF) ||
1198 !isLibFuncEmittable(CI->getModule(), &TLI, LF))
1199 return false;
1200
1201 DomTreeUpdater DTU(&DT, DomTreeUpdater::UpdateStrategy::Lazy);
1202
1203 switch (LF) {
1204 case LibFunc_sqrt:
1205 case LibFunc_sqrtf:
1206 case LibFunc_sqrtl:
1207 return foldSqrt(CI, LF, TTI, TLI, AC, DT);
1208 case LibFunc_strcmp:
1209 case LibFunc_strncmp:
1210 if (StrNCmpInliner(CI, LF, &DTU, DL).optimizeStrNCmp()) {
1211 MadeCFGChange = true;
1212 return true;
1213 }
1214 break;
1215 case LibFunc_memchr:
1216 if (foldMemChr(CI, &DTU, DL)) {
1217 MadeCFGChange = true;
1218 return true;
1219 }
1220 break;
1221 default:;
1222 }
1223 return false;
1224}
1225
1226/// This is the entry point for folds that could be implemented in regular
1227/// InstCombine, but they are separated because they are not expected to
1228/// occur frequently and/or have more than a constant-length pattern match.
1232 AssumptionCache &AC, bool &MadeCFGChange) {
1233 bool MadeChange = false;
1234 for (BasicBlock &BB : F) {
1235 // Ignore unreachable basic blocks.
1236 if (!DT.isReachableFromEntry(&BB))
1237 continue;
1238
1239 const DataLayout &DL = F.getDataLayout();
1240
1241 // Walk the block backwards for efficiency. We're matching a chain of
1242 // use->defs, so we're more likely to succeed by starting from the bottom.
1243 // Also, we want to avoid matching partial patterns.
1244 // TODO: It would be more efficient if we removed dead instructions
1245 // iteratively in this loop rather than waiting until the end.
1247 MadeChange |= foldAnyOrAllBitsSet(I);
1248 MadeChange |= foldGuardedFunnelShift(I, DT);
1249 MadeChange |= tryToRecognizePopCount(I);
1250 MadeChange |= tryToFPToSat(I, TTI);
1251 MadeChange |= tryToRecognizeTableBasedCttz(I);
1252 MadeChange |= foldConsecutiveLoads(I, DL, TTI, AA, DT);
1253 MadeChange |= foldPatternedLoads(I, DL);
1254 // NOTE: This function introduces erasing of the instruction `I`, so it
1255 // needs to be called at the end of this sequence, otherwise we may make
1256 // bugs.
1257 MadeChange |= foldLibCalls(I, TTI, TLI, AC, DT, DL, MadeCFGChange);
1258 }
1259 }
1260
1261 // We're done with transforms, so remove dead instructions.
1262 if (MadeChange)
1263 for (BasicBlock &BB : F)
1265
1266 return MadeChange;
1267}
1268
1269/// This is the entry point for all transforms. Pass manager differences are
1270/// handled in the callers of this function.
1273 AliasAnalysis &AA, bool &MadeCFGChange) {
1274 bool MadeChange = false;
1275 const DataLayout &DL = F.getDataLayout();
1276 TruncInstCombine TIC(AC, TLI, DL, DT);
1277 MadeChange |= TIC.run(F);
1278 MadeChange |= foldUnusualPatterns(F, DT, TTI, TLI, AA, AC, MadeCFGChange);
1279 return MadeChange;
1280}
1281
1284 auto &AC = AM.getResult<AssumptionAnalysis>(F);
1285 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
1286 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
1287 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
1288 auto &AA = AM.getResult<AAManager>(F);
1289 bool MadeCFGChange = false;
1290 if (!runImpl(F, AC, TTI, TLI, DT, AA, MadeCFGChange)) {
1291 // No changes, all analyses are preserved.
1292 return PreservedAnalyses::all();
1293 }
1294 // Mark all the analyses that instcombine updates as preserved.
1296 if (MadeCFGChange)
1298 else
1300 return PA;
1301}
AMDGPU Register Bank Select
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool tryToRecognizePopCount(Instruction &I)
static bool foldSqrt(CallInst *Call, LibFunc Func, TargetTransformInfo &TTI, TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree &DT)
Try to replace a mathlib call to sqrt with the LLVM intrinsic.
static bool foldAnyOrAllBitsSet(Instruction &I)
Match patterns that correspond to "any-bits-set" and "all-bits-set".
static cl::opt< unsigned > MemChrInlineThreshold("memchr-inline-threshold", cl::init(3), cl::Hidden, cl::desc("The maximum length of a constant string to " "inline a memchr call."))
static bool tryToFPToSat(Instruction &I, TargetTransformInfo &TTI)
Fold smin(smax(fptosi(x), C1), C2) to llvm.fptosi.sat(x), providing C1 and C2 saturate the value of t...
static cl::opt< unsigned > StrNCmpInlineThreshold("strncmp-inline-threshold", cl::init(3), cl::Hidden, cl::desc("The maximum length of a constant string for a builtin string cmp " "call eligible for inlining. The default value is 3."))
static bool matchAndOrChain(Value *V, MaskOps &MOps)
This is a recursive helper for foldAnyOrAllBitsSet() that walks through a chain of 'and' or 'or' inst...
static bool foldMemChr(CallInst *Call, DomTreeUpdater *DTU, const DataLayout &DL)
Convert memchr with a small constant string into a switch.
static bool foldConsecutiveLoads(Instruction &I, const DataLayout &DL, TargetTransformInfo &TTI, AliasAnalysis &AA, const DominatorTree &DT)
static bool runImpl(Function &F, AssumptionCache &AC, TargetTransformInfo &TTI, TargetLibraryInfo &TLI, DominatorTree &DT, AliasAnalysis &AA, bool &MadeCFGChange)
This is the entry point for all transforms.
static bool tryToRecognizeTableBasedCttz(Instruction &I)
static bool foldGuardedFunnelShift(Instruction &I, const DominatorTree &DT)
Match a pattern for a bitwise funnel/rotate operation that partially guards against undefined behavio...
static cl::opt< unsigned > MaxInstrsToScan("aggressive-instcombine-max-scan-instrs", cl::init(64), cl::Hidden, cl::desc("Max number of instructions to scan for aggressive instcombine."))
static bool foldLoadsRecursive(Value *V, LoadOps &LOps, const DataLayout &DL, AliasAnalysis &AA)
static std::pair< APInt, APInt > getStrideAndModOffsetOfGEP(Value *PtrOp, const DataLayout &DL)
static bool isCTTZTable(const ConstantDataArray &Table, uint64_t Mul, uint64_t Shift, uint64_t InputBits)
static bool foldPatternedLoads(Instruction &I, const DataLayout &DL)
If C is a constant patterned array and all valid loaded results for given alignment are same to a con...
static bool foldLibCalls(Instruction &I, TargetTransformInfo &TTI, TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree &DT, const DataLayout &DL, bool &MadeCFGChange)
static bool foldUnusualPatterns(Function &F, DominatorTree &DT, TargetTransformInfo &TTI, TargetLibraryInfo &TLI, AliasAnalysis &AA, AssumptionCache &AC, bool &MadeCFGChange)
This is the entry point for folds that could be implemented in regular InstCombine,...
AggressiveInstCombiner - Combine expression patterns to form expressions with fewer,...
This is the interface for LLVM's primary stateless and local alias analysis.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(...)
Definition: Debug.h:106
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool runImpl(Function &F, const TargetLowering &TLI)
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
static MaybeAlign getAlign(Value *Ptr)
Definition: IRBuilder.cpp:500
static Instruction * matchFunnelShift(Instruction &Or, InstCombinerImpl &IC)
Match UB-safe variants of the funnel shift intrinsic.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const MCExpr * MaskShift(const MCExpr *Val, uint32_t Mask, uint32_t Shift, MCContext &Ctx)
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:166
This pass exposes codegen information to IR-level passes.
Value * LHS
BinaryOperator * Mul
A manager for alias analyses.
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
Class for arbitrary precision integers.
Definition: APInt.h:78
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:624
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1710
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1130
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:416
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:212
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
This class represents a function call, abstracting a target machine's calling convention.
@ ICMP_EQ
equal
Definition: InstrTypes.h:694
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:696
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:3114
unsigned getNumElements() const
Return the number of elements in the array or vector.
Definition: Constants.cpp:2857
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:933
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasInitializer() const
Definitions have initializers, declarations don't.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1814
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2044
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2573
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1986
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:890
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2434
SwitchInst * CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases=10, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a switch instruction with the specified value, default dest, and with a hint for the number of...
Definition: IRBuilder.h:1186
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2269
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1458
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2032
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1517
Value * CreateIsNotNull(Value *Arg, const Twine &Name="")
Return a boolean value testing if Arg != 0.
Definition: IRBuilder.h:2587
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2018
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
Definition: IRBuilder.h:1157
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:199
Value * CreateInBoundsPtrAdd(Value *Ptr, Value *Offset, const Twine &Name="")
Definition: IRBuilder.h:1991
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:535
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:521
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2704
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1764
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:94
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1750
Class to represent integer types.
Definition: DerivedTypes.h:42
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:311
An instruction for reading from memory.
Definition: Instructions.h:176
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:261
Value * getPointerOperand()
Definition: Instructions.h:255
bool isSimple() const
Definition: Instructions.h:247
Representation for a specific memory location.
MemoryLocation getWithNewSize(LocationSize NewSize) const
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
static constexpr size_t npos
Definition: StringRef.h:53
Multiway switch.
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind) const
InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, TTI::CastContextHint CCH, TTI::TargetCostKind CostKind=TTI::TCK_SizeAndLatency, const Instruction *I=nullptr) const
@ TCK_RecipThroughput
Reciprocal throughput.
bool isTypeLegal(Type *Ty) const
Return true if this type is legal.
bool allowsMisalignedMemoryAccesses(LLVMContext &Context, unsigned BitWidth, unsigned AddressSpace=0, Align Alignment=Align(1), unsigned *Fast=nullptr) const
Determine if the target supports unaligned memory accesses.
bool haveFastSqrt(Type *Ty) const
Return true if the hardware has a fast square-root instruction.
@ None
The cast is not used with a load/store of any kind.
bool run(Function &F)
Perform TruncInst pattern optimization on given function.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:243
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:237
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr) const
Accumulate the constant offset this value has compared to a base pointer.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull, bool &CanBeFreed) const
Returns the number of bytes known to be dereferenceable for the pointer value.
Definition: Value.cpp:852
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
const ParentTy * getParent() const
Definition: ilist_node.h:32
#define UINT64_MAX
Definition: DataTypes.h:77
APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
Definition: APInt.cpp:771
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:125
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:41
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:982
match_combine_or< CastInst_match< OpTy, ZExtInst >, OpTy > m_ZExtOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:826
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:885
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:903
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
specific_bbval m_SpecificBB(BasicBlock *BB)
Match a specific basic block value.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
NodeAddr< PhiNode * > Phi
Definition: RDFGraph.h:390
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Length
Definition: DWP.cpp:480
bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
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:737
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:296
bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, LibFunc TheLibFunc)
Check whether the library function is available on target and also that it in the current Module is a...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:340
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:291
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
@ And
Bitwise or logical AND of integers.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
bool cannotBeOrderedLessThanZero(const Value *V, unsigned Depth, const SimplifyQuery &SQ)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
This is used by foldLoadsRecursive() to capture a Root Load node which is of type or(load,...
const APInt * Shift
LoadInst * RootInsert
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:760
AAMDNodes concat(const AAMDNodes &Other) const
Determine the best AAMDNodes after concatenating two different locations together.
A MapVector that performs no allocations if smaller than a certain size.
Definition: MapVector.h:254