LLVM 22.0.0git
InstructionCombining.cpp
Go to the documentation of this file.
1//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
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// InstructionCombining - Combine instructions to form fewer, simple
10// instructions. This pass does not modify the CFG. This pass is where
11// algebraic simplification happens.
12//
13// This pass combines things like:
14// %Y = add i32 %X, 1
15// %Z = add i32 %Y, 1
16// into:
17// %Z = add i32 %X, 2
18//
19// This is a simple worklist driven algorithm.
20//
21// This pass guarantees that the following canonicalizations are performed on
22// the program:
23// 1. If a binary operator has a constant operand, it is moved to the RHS
24// 2. Bitwise operators with constant operands are always grouped so that
25// shifts are performed first, then or's, then and's, then xor's.
26// 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
27// 4. All cmp instructions on boolean values are replaced with logical ops
28// 5. add X, X is represented as (X*2) => (X << 1)
29// 6. Multiplies with a power-of-two constant argument are transformed into
30// shifts.
31// ... etc.
32//
33//===----------------------------------------------------------------------===//
34
35#include "InstCombineInternal.h"
36#include "llvm/ADT/APFloat.h"
37#include "llvm/ADT/APInt.h"
38#include "llvm/ADT/ArrayRef.h"
39#include "llvm/ADT/DenseMap.h"
42#include "llvm/ADT/Statistic.h"
47#include "llvm/Analysis/CFG.h"
62#include "llvm/IR/BasicBlock.h"
63#include "llvm/IR/CFG.h"
64#include "llvm/IR/Constant.h"
65#include "llvm/IR/Constants.h"
66#include "llvm/IR/DIBuilder.h"
67#include "llvm/IR/DataLayout.h"
68#include "llvm/IR/DebugInfo.h"
70#include "llvm/IR/Dominators.h"
72#include "llvm/IR/Function.h"
74#include "llvm/IR/IRBuilder.h"
75#include "llvm/IR/InstrTypes.h"
76#include "llvm/IR/Instruction.h"
79#include "llvm/IR/Intrinsics.h"
80#include "llvm/IR/Metadata.h"
81#include "llvm/IR/Operator.h"
82#include "llvm/IR/PassManager.h"
84#include "llvm/IR/Type.h"
85#include "llvm/IR/Use.h"
86#include "llvm/IR/User.h"
87#include "llvm/IR/Value.h"
88#include "llvm/IR/ValueHandle.h"
93#include "llvm/Support/Debug.h"
102#include <algorithm>
103#include <cassert>
104#include <cstdint>
105#include <memory>
106#include <optional>
107#include <string>
108#include <utility>
109
110#define DEBUG_TYPE "instcombine"
112#include <optional>
113
114using namespace llvm;
115using namespace llvm::PatternMatch;
116
117STATISTIC(NumWorklistIterations,
118 "Number of instruction combining iterations performed");
119STATISTIC(NumOneIteration, "Number of functions with one iteration");
120STATISTIC(NumTwoIterations, "Number of functions with two iterations");
121STATISTIC(NumThreeIterations, "Number of functions with three iterations");
122STATISTIC(NumFourOrMoreIterations,
123 "Number of functions with four or more iterations");
124
125STATISTIC(NumCombined , "Number of insts combined");
126STATISTIC(NumConstProp, "Number of constant folds");
127STATISTIC(NumDeadInst , "Number of dead inst eliminated");
128STATISTIC(NumSunkInst , "Number of instructions sunk");
129STATISTIC(NumExpand, "Number of expansions");
130STATISTIC(NumFactor , "Number of factorizations");
131STATISTIC(NumReassoc , "Number of reassociations");
132DEBUG_COUNTER(VisitCounter, "instcombine-visit",
133 "Controls which instructions are visited");
134
135static cl::opt<bool>
136EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"),
137 cl::init(true));
138
140 "instcombine-max-sink-users", cl::init(32),
141 cl::desc("Maximum number of undroppable users for instruction sinking"));
142
144MaxArraySize("instcombine-maxarray-size", cl::init(1024),
145 cl::desc("Maximum array size considered when doing a combine"));
146
147// FIXME: Remove this flag when it is no longer necessary to convert
148// llvm.dbg.declare to avoid inaccurate debug info. Setting this to false
149// increases variable availability at the cost of accuracy. Variables that
150// cannot be promoted by mem2reg or SROA will be described as living in memory
151// for their entire lifetime. However, passes like DSE and instcombine can
152// delete stores to the alloca, leading to misleading and inaccurate debug
153// information. This flag can be removed when those passes are fixed.
154static cl::opt<unsigned> ShouldLowerDbgDeclare("instcombine-lower-dbg-declare",
155 cl::Hidden, cl::init(true));
156
157std::optional<Instruction *>
159 // Handle target specific intrinsics
160 if (II.getCalledFunction()->isTargetIntrinsic()) {
161 return TTIForTargetIntrinsicsOnly.instCombineIntrinsic(*this, II);
162 }
163 return std::nullopt;
164}
165
167 IntrinsicInst &II, APInt DemandedMask, KnownBits &Known,
168 bool &KnownBitsComputed) {
169 // Handle target specific intrinsics
170 if (II.getCalledFunction()->isTargetIntrinsic()) {
171 return TTIForTargetIntrinsicsOnly.simplifyDemandedUseBitsIntrinsic(
172 *this, II, DemandedMask, Known, KnownBitsComputed);
173 }
174 return std::nullopt;
175}
176
178 IntrinsicInst &II, APInt DemandedElts, APInt &PoisonElts,
179 APInt &PoisonElts2, APInt &PoisonElts3,
180 std::function<void(Instruction *, unsigned, APInt, APInt &)>
181 SimplifyAndSetOp) {
182 // Handle target specific intrinsics
183 if (II.getCalledFunction()->isTargetIntrinsic()) {
184 return TTIForTargetIntrinsicsOnly.simplifyDemandedVectorEltsIntrinsic(
185 *this, II, DemandedElts, PoisonElts, PoisonElts2, PoisonElts3,
186 SimplifyAndSetOp);
187 }
188 return std::nullopt;
189}
190
191bool InstCombiner::isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const {
192 // Approved exception for TTI use: This queries a legality property of the
193 // target, not an profitability heuristic. Ideally this should be part of
194 // DataLayout instead.
195 return TTIForTargetIntrinsicsOnly.isValidAddrSpaceCast(FromAS, ToAS);
196}
197
198Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
199 if (!RewriteGEP)
201
203 auto *Inst = dyn_cast<Instruction>(GEP);
204 if (Inst)
206
207 Value *Offset = EmitGEPOffset(GEP);
208 // Rewrite non-trivial GEPs to avoid duplicating the offset arithmetic.
209 if (Inst && !GEP->hasAllConstantIndices() &&
210 !GEP->getSourceElementType()->isIntegerTy(8)) {
212 *Inst, Builder.CreateGEP(Builder.getInt8Ty(), GEP->getPointerOperand(),
213 Offset, "", GEP->getNoWrapFlags()));
215 }
216 return Offset;
217}
218
219Value *InstCombinerImpl::EmitGEPOffsets(ArrayRef<GEPOperator *> GEPs,
220 GEPNoWrapFlags NW, Type *IdxTy,
221 bool RewriteGEPs) {
222 auto Add = [&](Value *Sum, Value *Offset) -> Value * {
223 if (Sum)
224 return Builder.CreateAdd(Sum, Offset, "", NW.hasNoUnsignedWrap(),
225 NW.isInBounds());
226 else
227 return Offset;
228 };
229
230 Value *Sum = nullptr;
231 Value *OneUseSum = nullptr;
232 Value *OneUseBase = nullptr;
233 GEPNoWrapFlags OneUseFlags = GEPNoWrapFlags::all();
234 for (GEPOperator *GEP : reverse(GEPs)) {
235 Value *Offset;
236 {
237 // Expand the offset at the point of the previous GEP to enable rewriting.
238 // However, use the original insertion point for calculating Sum.
240 auto *Inst = dyn_cast<Instruction>(GEP);
241 if (RewriteGEPs && Inst)
243
245 if (Offset->getType() != IdxTy)
247 cast<VectorType>(IdxTy)->getElementCount(), Offset);
248 if (GEP->hasOneUse()) {
249 // Offsets of one-use GEPs will be merged into the next multi-use GEP.
250 OneUseSum = Add(OneUseSum, Offset);
251 OneUseFlags = OneUseFlags.intersectForOffsetAdd(GEP->getNoWrapFlags());
252 if (!OneUseBase)
253 OneUseBase = GEP->getPointerOperand();
254 continue;
255 }
256
257 if (OneUseSum)
258 Offset = Add(OneUseSum, Offset);
259
260 // Rewrite the GEP to reuse the computed offset. This also includes
261 // offsets from preceding one-use GEPs.
262 if (RewriteGEPs && Inst &&
263 !(GEP->getSourceElementType()->isIntegerTy(8) &&
264 GEP->getOperand(1) == Offset)) {
266 *Inst,
268 OneUseBase ? OneUseBase : GEP->getPointerOperand(), Offset, "",
269 OneUseFlags.intersectForOffsetAdd(GEP->getNoWrapFlags())));
271 }
272 }
273
274 Sum = Add(Sum, Offset);
275 OneUseSum = OneUseBase = nullptr;
276 OneUseFlags = GEPNoWrapFlags::all();
277 }
278 if (OneUseSum)
279 Sum = Add(Sum, OneUseSum);
280 if (!Sum)
281 return Constant::getNullValue(IdxTy);
282 return Sum;
283}
284
285/// Legal integers and common types are considered desirable. This is used to
286/// avoid creating instructions with types that may not be supported well by the
287/// the backend.
288/// NOTE: This treats i8, i16 and i32 specially because they are common
289/// types in frontend languages.
290bool InstCombinerImpl::isDesirableIntType(unsigned BitWidth) const {
291 switch (BitWidth) {
292 case 8:
293 case 16:
294 case 32:
295 return true;
296 default:
297 return DL.isLegalInteger(BitWidth);
298 }
299}
300
301/// Return true if it is desirable to convert an integer computation from a
302/// given bit width to a new bit width.
303/// We don't want to convert from a legal or desirable type (like i8) to an
304/// illegal type or from a smaller to a larger illegal type. A width of '1'
305/// is always treated as a desirable type because i1 is a fundamental type in
306/// IR, and there are many specialized optimizations for i1 types.
307/// Common/desirable widths are equally treated as legal to convert to, in
308/// order to open up more combining opportunities.
309bool InstCombinerImpl::shouldChangeType(unsigned FromWidth,
310 unsigned ToWidth) const {
311 bool FromLegal = FromWidth == 1 || DL.isLegalInteger(FromWidth);
312 bool ToLegal = ToWidth == 1 || DL.isLegalInteger(ToWidth);
313
314 // Convert to desirable widths even if they are not legal types.
315 // Only shrink types, to prevent infinite loops.
316 if (ToWidth < FromWidth && isDesirableIntType(ToWidth))
317 return true;
318
319 // If this is a legal or desiable integer from type, and the result would be
320 // an illegal type, don't do the transformation.
321 if ((FromLegal || isDesirableIntType(FromWidth)) && !ToLegal)
322 return false;
323
324 // Otherwise, if both are illegal, do not increase the size of the result. We
325 // do allow things like i160 -> i64, but not i64 -> i160.
326 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
327 return false;
328
329 return true;
330}
331
332/// Return true if it is desirable to convert a computation from 'From' to 'To'.
333/// We don't want to convert from a legal to an illegal type or from a smaller
334/// to a larger illegal type. i1 is always treated as a legal type because it is
335/// a fundamental type in IR, and there are many specialized optimizations for
336/// i1 types.
337bool InstCombinerImpl::shouldChangeType(Type *From, Type *To) const {
338 // TODO: This could be extended to allow vectors. Datalayout changes might be
339 // needed to properly support that.
340 if (!From->isIntegerTy() || !To->isIntegerTy())
341 return false;
342
343 unsigned FromWidth = From->getPrimitiveSizeInBits();
344 unsigned ToWidth = To->getPrimitiveSizeInBits();
345 return shouldChangeType(FromWidth, ToWidth);
346}
347
348// Return true, if No Signed Wrap should be maintained for I.
349// The No Signed Wrap flag can be kept if the operation "B (I.getOpcode) C",
350// where both B and C should be ConstantInts, results in a constant that does
351// not overflow. This function only handles the Add/Sub/Mul opcodes. For
352// all other opcodes, the function conservatively returns false.
354 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
355 if (!OBO || !OBO->hasNoSignedWrap())
356 return false;
357
358 const APInt *BVal, *CVal;
359 if (!match(B, m_APInt(BVal)) || !match(C, m_APInt(CVal)))
360 return false;
361
362 // We reason about Add/Sub/Mul Only.
363 bool Overflow = false;
364 switch (I.getOpcode()) {
365 case Instruction::Add:
366 (void)BVal->sadd_ov(*CVal, Overflow);
367 break;
368 case Instruction::Sub:
369 (void)BVal->ssub_ov(*CVal, Overflow);
370 break;
371 case Instruction::Mul:
372 (void)BVal->smul_ov(*CVal, Overflow);
373 break;
374 default:
375 // Conservatively return false for other opcodes.
376 return false;
377 }
378 return !Overflow;
379}
380
382 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
383 return OBO && OBO->hasNoUnsignedWrap();
384}
385
387 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
388 return OBO && OBO->hasNoSignedWrap();
389}
390
391/// Conservatively clears subclassOptionalData after a reassociation or
392/// commutation. We preserve fast-math flags when applicable as they can be
393/// preserved.
395 FPMathOperator *FPMO = dyn_cast<FPMathOperator>(&I);
396 if (!FPMO) {
397 I.clearSubclassOptionalData();
398 return;
399 }
400
401 FastMathFlags FMF = I.getFastMathFlags();
402 I.clearSubclassOptionalData();
403 I.setFastMathFlags(FMF);
404}
405
406/// Combine constant operands of associative operations either before or after a
407/// cast to eliminate one of the associative operations:
408/// (op (cast (op X, C2)), C1) --> (cast (op X, op (C1, C2)))
409/// (op (cast (op X, C2)), C1) --> (op (cast X), op (C1, C2))
411 InstCombinerImpl &IC) {
412 auto *Cast = dyn_cast<CastInst>(BinOp1->getOperand(0));
413 if (!Cast || !Cast->hasOneUse())
414 return false;
415
416 // TODO: Enhance logic for other casts and remove this check.
417 auto CastOpcode = Cast->getOpcode();
418 if (CastOpcode != Instruction::ZExt)
419 return false;
420
421 // TODO: Enhance logic for other BinOps and remove this check.
422 if (!BinOp1->isBitwiseLogicOp())
423 return false;
424
425 auto AssocOpcode = BinOp1->getOpcode();
426 auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
427 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
428 return false;
429
430 Constant *C1, *C2;
431 if (!match(BinOp1->getOperand(1), m_Constant(C1)) ||
432 !match(BinOp2->getOperand(1), m_Constant(C2)))
433 return false;
434
435 // TODO: This assumes a zext cast.
436 // Eg, if it was a trunc, we'd cast C1 to the source type because casting C2
437 // to the destination type might lose bits.
438
439 // Fold the constants together in the destination type:
440 // (op (cast (op X, C2)), C1) --> (op (cast X), FoldedC)
441 const DataLayout &DL = IC.getDataLayout();
442 Type *DestTy = C1->getType();
443 Constant *CastC2 = ConstantFoldCastOperand(CastOpcode, C2, DestTy, DL);
444 if (!CastC2)
445 return false;
446 Constant *FoldedC = ConstantFoldBinaryOpOperands(AssocOpcode, C1, CastC2, DL);
447 if (!FoldedC)
448 return false;
449
450 IC.replaceOperand(*Cast, 0, BinOp2->getOperand(0));
451 IC.replaceOperand(*BinOp1, 1, FoldedC);
453 Cast->dropPoisonGeneratingFlags();
454 return true;
455}
456
457// Simplifies IntToPtr/PtrToInt RoundTrip Cast.
458// inttoptr ( ptrtoint (x) ) --> x
459Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(Value *Val) {
460 auto *IntToPtr = dyn_cast<IntToPtrInst>(Val);
461 if (IntToPtr && DL.getTypeSizeInBits(IntToPtr->getDestTy()) ==
462 DL.getTypeSizeInBits(IntToPtr->getSrcTy())) {
463 auto *PtrToInt = dyn_cast<PtrToIntInst>(IntToPtr->getOperand(0));
464 Type *CastTy = IntToPtr->getDestTy();
465 if (PtrToInt &&
466 CastTy->getPointerAddressSpace() ==
467 PtrToInt->getSrcTy()->getPointerAddressSpace() &&
468 DL.getTypeSizeInBits(PtrToInt->getSrcTy()) ==
469 DL.getTypeSizeInBits(PtrToInt->getDestTy()))
470 return PtrToInt->getOperand(0);
471 }
472 return nullptr;
473}
474
475/// This performs a few simplifications for operators that are associative or
476/// commutative:
477///
478/// Commutative operators:
479///
480/// 1. Order operands such that they are listed from right (least complex) to
481/// left (most complex). This puts constants before unary operators before
482/// binary operators.
483///
484/// Associative operators:
485///
486/// 2. Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
487/// 3. Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
488///
489/// Associative and commutative operators:
490///
491/// 4. Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
492/// 5. Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
493/// 6. Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
494/// if C1 and C2 are constants.
496 Instruction::BinaryOps Opcode = I.getOpcode();
497 bool Changed = false;
498
499 do {
500 // Order operands such that they are listed from right (least complex) to
501 // left (most complex). This puts constants before unary operators before
502 // binary operators.
503 if (I.isCommutative() && getComplexity(I.getOperand(0)) <
504 getComplexity(I.getOperand(1)))
505 Changed = !I.swapOperands();
506
507 if (I.isCommutative()) {
508 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
509 replaceOperand(I, 0, Pair->first);
510 replaceOperand(I, 1, Pair->second);
511 Changed = true;
512 }
513 }
514
515 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(I.getOperand(0));
516 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(I.getOperand(1));
517
518 if (I.isAssociative()) {
519 // Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
520 if (Op0 && Op0->getOpcode() == Opcode) {
521 Value *A = Op0->getOperand(0);
522 Value *B = Op0->getOperand(1);
523 Value *C = I.getOperand(1);
524
525 // Does "B op C" simplify?
526 if (Value *V = simplifyBinOp(Opcode, B, C, SQ.getWithInstruction(&I))) {
527 // It simplifies to V. Form "A op V".
528 replaceOperand(I, 0, A);
529 replaceOperand(I, 1, V);
530 bool IsNUW = hasNoUnsignedWrap(I) && hasNoUnsignedWrap(*Op0);
531 bool IsNSW = maintainNoSignedWrap(I, B, C) && hasNoSignedWrap(*Op0);
532
533 // Conservatively clear all optional flags since they may not be
534 // preserved by the reassociation. Reset nsw/nuw based on the above
535 // analysis.
537
538 // Note: this is only valid because SimplifyBinOp doesn't look at
539 // the operands to Op0.
540 if (IsNUW)
541 I.setHasNoUnsignedWrap(true);
542
543 if (IsNSW)
544 I.setHasNoSignedWrap(true);
545
546 Changed = true;
547 ++NumReassoc;
548 continue;
549 }
550 }
551
552 // Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
553 if (Op1 && Op1->getOpcode() == Opcode) {
554 Value *A = I.getOperand(0);
555 Value *B = Op1->getOperand(0);
556 Value *C = Op1->getOperand(1);
557
558 // Does "A op B" simplify?
559 if (Value *V = simplifyBinOp(Opcode, A, B, SQ.getWithInstruction(&I))) {
560 // It simplifies to V. Form "V op C".
561 replaceOperand(I, 0, V);
562 replaceOperand(I, 1, C);
563 // Conservatively clear the optional flags, since they may not be
564 // preserved by the reassociation.
566 Changed = true;
567 ++NumReassoc;
568 continue;
569 }
570 }
571 }
572
573 if (I.isAssociative() && I.isCommutative()) {
574 if (simplifyAssocCastAssoc(&I, *this)) {
575 Changed = true;
576 ++NumReassoc;
577 continue;
578 }
579
580 // Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
581 if (Op0 && Op0->getOpcode() == Opcode) {
582 Value *A = Op0->getOperand(0);
583 Value *B = Op0->getOperand(1);
584 Value *C = I.getOperand(1);
585
586 // Does "C op A" simplify?
587 if (Value *V = simplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) {
588 // It simplifies to V. Form "V op B".
589 replaceOperand(I, 0, V);
590 replaceOperand(I, 1, B);
591 // Conservatively clear the optional flags, since they may not be
592 // preserved by the reassociation.
594 Changed = true;
595 ++NumReassoc;
596 continue;
597 }
598 }
599
600 // Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
601 if (Op1 && Op1->getOpcode() == Opcode) {
602 Value *A = I.getOperand(0);
603 Value *B = Op1->getOperand(0);
604 Value *C = Op1->getOperand(1);
605
606 // Does "C op A" simplify?
607 if (Value *V = simplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) {
608 // It simplifies to V. Form "B op V".
609 replaceOperand(I, 0, B);
610 replaceOperand(I, 1, V);
611 // Conservatively clear the optional flags, since they may not be
612 // preserved by the reassociation.
614 Changed = true;
615 ++NumReassoc;
616 continue;
617 }
618 }
619
620 // Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
621 // if C1 and C2 are constants.
622 Value *A, *B;
623 Constant *C1, *C2, *CRes;
624 if (Op0 && Op1 &&
625 Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
626 match(Op0, m_OneUse(m_BinOp(m_Value(A), m_Constant(C1)))) &&
627 match(Op1, m_OneUse(m_BinOp(m_Value(B), m_Constant(C2)))) &&
628 (CRes = ConstantFoldBinaryOpOperands(Opcode, C1, C2, DL))) {
629 bool IsNUW = hasNoUnsignedWrap(I) &&
630 hasNoUnsignedWrap(*Op0) &&
631 hasNoUnsignedWrap(*Op1);
632 BinaryOperator *NewBO = (IsNUW && Opcode == Instruction::Add) ?
633 BinaryOperator::CreateNUW(Opcode, A, B) :
634 BinaryOperator::Create(Opcode, A, B);
635
636 if (isa<FPMathOperator>(NewBO)) {
637 FastMathFlags Flags = I.getFastMathFlags() &
638 Op0->getFastMathFlags() &
639 Op1->getFastMathFlags();
640 NewBO->setFastMathFlags(Flags);
641 }
642 InsertNewInstWith(NewBO, I.getIterator());
643 NewBO->takeName(Op1);
644 replaceOperand(I, 0, NewBO);
645 replaceOperand(I, 1, CRes);
646 // Conservatively clear the optional flags, since they may not be
647 // preserved by the reassociation.
649 if (IsNUW)
650 I.setHasNoUnsignedWrap(true);
651
652 Changed = true;
653 continue;
654 }
655 }
656
657 // No further simplifications.
658 return Changed;
659 } while (true);
660}
661
662/// Return whether "X LOp (Y ROp Z)" is always equal to
663/// "(X LOp Y) ROp (X LOp Z)".
666 // X & (Y | Z) <--> (X & Y) | (X & Z)
667 // X & (Y ^ Z) <--> (X & Y) ^ (X & Z)
668 if (LOp == Instruction::And)
669 return ROp == Instruction::Or || ROp == Instruction::Xor;
670
671 // X | (Y & Z) <--> (X | Y) & (X | Z)
672 if (LOp == Instruction::Or)
673 return ROp == Instruction::And;
674
675 // X * (Y + Z) <--> (X * Y) + (X * Z)
676 // X * (Y - Z) <--> (X * Y) - (X * Z)
677 if (LOp == Instruction::Mul)
678 return ROp == Instruction::Add || ROp == Instruction::Sub;
679
680 return false;
681}
682
683/// Return whether "(X LOp Y) ROp Z" is always equal to
684/// "(X ROp Z) LOp (Y ROp Z)".
688 return leftDistributesOverRight(ROp, LOp);
689
690 // (X {&|^} Y) >> Z <--> (X >> Z) {&|^} (Y >> Z) for all shifts.
692
693 // TODO: It would be nice to handle division, aka "(X + Y)/Z = X/Z + Y/Z",
694 // but this requires knowing that the addition does not overflow and other
695 // such subtleties.
696}
697
698/// This function returns identity value for given opcode, which can be used to
699/// factor patterns like (X * 2) + X ==> (X * 2) + (X * 1) ==> X * (2 + 1).
701 if (isa<Constant>(V))
702 return nullptr;
703
704 return ConstantExpr::getBinOpIdentity(Opcode, V->getType());
705}
706
707/// This function predicates factorization using distributive laws. By default,
708/// it just returns the 'Op' inputs. But for special-cases like
709/// 'add(shl(X, 5), ...)', this function will have TopOpcode == Instruction::Add
710/// and Op = shl(X, 5). The 'shl' is treated as the more general 'mul X, 32' to
711/// allow more factorization opportunities.
714 Value *&LHS, Value *&RHS, BinaryOperator *OtherOp) {
715 assert(Op && "Expected a binary operator");
716 LHS = Op->getOperand(0);
717 RHS = Op->getOperand(1);
718 if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) {
719 Constant *C;
720 if (match(Op, m_Shl(m_Value(), m_ImmConstant(C)))) {
721 // X << C --> X * (1 << C)
723 Instruction::Shl, ConstantInt::get(Op->getType(), 1), C);
724 assert(RHS && "Constant folding of immediate constants failed");
725 return Instruction::Mul;
726 }
727 // TODO: We can add other conversions e.g. shr => div etc.
728 }
729 if (Instruction::isBitwiseLogicOp(TopOpcode)) {
730 if (OtherOp && OtherOp->getOpcode() == Instruction::AShr &&
732 // lshr nneg C, X --> ashr nneg C, X
733 return Instruction::AShr;
734 }
735 }
736 return Op->getOpcode();
737}
738
739/// This tries to simplify binary operations by factorizing out common terms
740/// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
743 Instruction::BinaryOps InnerOpcode, Value *A,
744 Value *B, Value *C, Value *D) {
745 assert(A && B && C && D && "All values must be provided");
746
747 Value *V = nullptr;
748 Value *RetVal = nullptr;
749 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
750 Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
751
752 // Does "X op' Y" always equal "Y op' X"?
753 bool InnerCommutative = Instruction::isCommutative(InnerOpcode);
754
755 // Does "X op' (Y op Z)" always equal "(X op' Y) op (X op' Z)"?
756 if (leftDistributesOverRight(InnerOpcode, TopLevelOpcode)) {
757 // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
758 // commutative case, "(A op' B) op (C op' A)"?
759 if (A == C || (InnerCommutative && A == D)) {
760 if (A != C)
761 std::swap(C, D);
762 // Consider forming "A op' (B op D)".
763 // If "B op D" simplifies then it can be formed with no cost.
764 V = simplifyBinOp(TopLevelOpcode, B, D, SQ.getWithInstruction(&I));
765
766 // If "B op D" doesn't simplify then only go on if one of the existing
767 // operations "A op' B" and "C op' D" will be zapped as no longer used.
768 if (!V && (LHS->hasOneUse() || RHS->hasOneUse()))
769 V = Builder.CreateBinOp(TopLevelOpcode, B, D, RHS->getName());
770 if (V)
771 RetVal = Builder.CreateBinOp(InnerOpcode, A, V);
772 }
773 }
774
775 // Does "(X op Y) op' Z" always equal "(X op' Z) op (Y op' Z)"?
776 if (!RetVal && rightDistributesOverLeft(TopLevelOpcode, InnerOpcode)) {
777 // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
778 // commutative case, "(A op' B) op (B op' D)"?
779 if (B == D || (InnerCommutative && B == C)) {
780 if (B != D)
781 std::swap(C, D);
782 // Consider forming "(A op C) op' B".
783 // If "A op C" simplifies then it can be formed with no cost.
784 V = simplifyBinOp(TopLevelOpcode, A, C, SQ.getWithInstruction(&I));
785
786 // If "A op C" doesn't simplify then only go on if one of the existing
787 // operations "A op' B" and "C op' D" will be zapped as no longer used.
788 if (!V && (LHS->hasOneUse() || RHS->hasOneUse()))
789 V = Builder.CreateBinOp(TopLevelOpcode, A, C, LHS->getName());
790 if (V)
791 RetVal = Builder.CreateBinOp(InnerOpcode, V, B);
792 }
793 }
794
795 if (!RetVal)
796 return nullptr;
797
798 ++NumFactor;
799 RetVal->takeName(&I);
800
801 // Try to add no-overflow flags to the final value.
802 if (isa<BinaryOperator>(RetVal)) {
803 bool HasNSW = false;
804 bool HasNUW = false;
805 if (isa<OverflowingBinaryOperator>(&I)) {
806 HasNSW = I.hasNoSignedWrap();
807 HasNUW = I.hasNoUnsignedWrap();
808 }
809 if (auto *LOBO = dyn_cast<OverflowingBinaryOperator>(LHS)) {
810 HasNSW &= LOBO->hasNoSignedWrap();
811 HasNUW &= LOBO->hasNoUnsignedWrap();
812 }
813
814 if (auto *ROBO = dyn_cast<OverflowingBinaryOperator>(RHS)) {
815 HasNSW &= ROBO->hasNoSignedWrap();
816 HasNUW &= ROBO->hasNoUnsignedWrap();
817 }
818
819 if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) {
820 // We can propagate 'nsw' if we know that
821 // %Y = mul nsw i16 %X, C
822 // %Z = add nsw i16 %Y, %X
823 // =>
824 // %Z = mul nsw i16 %X, C+1
825 //
826 // iff C+1 isn't INT_MIN
827 const APInt *CInt;
828 if (match(V, m_APInt(CInt)) && !CInt->isMinSignedValue())
829 cast<Instruction>(RetVal)->setHasNoSignedWrap(HasNSW);
830
831 // nuw can be propagated with any constant or nuw value.
832 cast<Instruction>(RetVal)->setHasNoUnsignedWrap(HasNUW);
833 }
834 }
835 return RetVal;
836}
837
838// If `I` has one Const operand and the other matches `(ctpop (not x))`,
839// replace `(ctpop (not x))` with `(sub nuw nsw BitWidth(x), (ctpop x))`.
840// This is only useful is the new subtract can fold so we only handle the
841// following cases:
842// 1) (add/sub/disjoint_or C, (ctpop (not x))
843// -> (add/sub/disjoint_or C', (ctpop x))
844// 1) (cmp pred C, (ctpop (not x))
845// -> (cmp pred C', (ctpop x))
847 unsigned Opc = I->getOpcode();
848 unsigned ConstIdx = 1;
849 switch (Opc) {
850 default:
851 return nullptr;
852 // (ctpop (not x)) <-> (sub nuw nsw BitWidth(x) - (ctpop x))
853 // We can fold the BitWidth(x) with add/sub/icmp as long the other operand
854 // is constant.
855 case Instruction::Sub:
856 ConstIdx = 0;
857 break;
858 case Instruction::ICmp:
859 // Signed predicates aren't correct in some edge cases like for i2 types, as
860 // well since (ctpop x) is known [0, log2(BitWidth(x))] almost all signed
861 // comparisons against it are simplfied to unsigned.
862 if (cast<ICmpInst>(I)->isSigned())
863 return nullptr;
864 break;
865 case Instruction::Or:
866 if (!match(I, m_DisjointOr(m_Value(), m_Value())))
867 return nullptr;
868 [[fallthrough]];
869 case Instruction::Add:
870 break;
871 }
872
873 Value *Op;
874 // Find ctpop.
875 if (!match(I->getOperand(1 - ConstIdx),
876 m_OneUse(m_Intrinsic<Intrinsic::ctpop>(m_Value(Op)))))
877 return nullptr;
878
879 Constant *C;
880 // Check other operand is ImmConstant.
881 if (!match(I->getOperand(ConstIdx), m_ImmConstant(C)))
882 return nullptr;
883
884 Type *Ty = Op->getType();
885 Constant *BitWidthC = ConstantInt::get(Ty, Ty->getScalarSizeInBits());
886 // Need extra check for icmp. Note if this check is true, it generally means
887 // the icmp will simplify to true/false.
888 if (Opc == Instruction::ICmp && !cast<ICmpInst>(I)->isEquality()) {
889 Constant *Cmp =
891 if (!Cmp || !Cmp->isZeroValue())
892 return nullptr;
893 }
894
895 // Check we can invert `(not x)` for free.
896 bool Consumes = false;
897 if (!isFreeToInvert(Op, Op->hasOneUse(), Consumes) || !Consumes)
898 return nullptr;
899 Value *NotOp = getFreelyInverted(Op, Op->hasOneUse(), &Builder);
900 assert(NotOp != nullptr &&
901 "Desync between isFreeToInvert and getFreelyInverted");
902
903 Value *CtpopOfNotOp = Builder.CreateIntrinsic(Ty, Intrinsic::ctpop, NotOp);
904
905 Value *R = nullptr;
906
907 // Do the transformation here to avoid potentially introducing an infinite
908 // loop.
909 switch (Opc) {
910 case Instruction::Sub:
911 R = Builder.CreateAdd(CtpopOfNotOp, ConstantExpr::getSub(C, BitWidthC));
912 break;
913 case Instruction::Or:
914 case Instruction::Add:
915 R = Builder.CreateSub(ConstantExpr::getAdd(C, BitWidthC), CtpopOfNotOp);
916 break;
917 case Instruction::ICmp:
918 R = Builder.CreateICmp(cast<ICmpInst>(I)->getSwappedPredicate(),
919 CtpopOfNotOp, ConstantExpr::getSub(BitWidthC, C));
920 break;
921 default:
922 llvm_unreachable("Unhandled Opcode");
923 }
924 assert(R != nullptr);
925 return replaceInstUsesWith(*I, R);
926}
927
928// (Binop1 (Binop2 (logic_shift X, C), C1), (logic_shift Y, C))
929// IFF
930// 1) the logic_shifts match
931// 2) either both binops are binops and one is `and` or
932// BinOp1 is `and`
933// (logic_shift (inv_logic_shift C1, C), C) == C1 or
934//
935// -> (logic_shift (Binop1 (Binop2 X, inv_logic_shift(C1, C)), Y), C)
936//
937// (Binop1 (Binop2 (logic_shift X, Amt), Mask), (logic_shift Y, Amt))
938// IFF
939// 1) the logic_shifts match
940// 2) BinOp1 == BinOp2 (if BinOp == `add`, then also requires `shl`).
941//
942// -> (BinOp (logic_shift (BinOp X, Y)), Mask)
943//
944// (Binop1 (Binop2 (arithmetic_shift X, Amt), Mask), (arithmetic_shift Y, Amt))
945// IFF
946// 1) Binop1 is bitwise logical operator `and`, `or` or `xor`
947// 2) Binop2 is `not`
948//
949// -> (arithmetic_shift Binop1((not X), Y), Amt)
950
952 const DataLayout &DL = I.getDataLayout();
953 auto IsValidBinOpc = [](unsigned Opc) {
954 switch (Opc) {
955 default:
956 return false;
957 case Instruction::And:
958 case Instruction::Or:
959 case Instruction::Xor:
960 case Instruction::Add:
961 // Skip Sub as we only match constant masks which will canonicalize to use
962 // add.
963 return true;
964 }
965 };
966
967 // Check if we can distribute binop arbitrarily. `add` + `lshr` has extra
968 // constraints.
969 auto IsCompletelyDistributable = [](unsigned BinOpc1, unsigned BinOpc2,
970 unsigned ShOpc) {
971 assert(ShOpc != Instruction::AShr);
972 return (BinOpc1 != Instruction::Add && BinOpc2 != Instruction::Add) ||
973 ShOpc == Instruction::Shl;
974 };
975
976 auto GetInvShift = [](unsigned ShOpc) {
977 assert(ShOpc != Instruction::AShr);
978 return ShOpc == Instruction::LShr ? Instruction::Shl : Instruction::LShr;
979 };
980
981 auto CanDistributeBinops = [&](unsigned BinOpc1, unsigned BinOpc2,
982 unsigned ShOpc, Constant *CMask,
983 Constant *CShift) {
984 // If the BinOp1 is `and` we don't need to check the mask.
985 if (BinOpc1 == Instruction::And)
986 return true;
987
988 // For all other possible transfers we need complete distributable
989 // binop/shift (anything but `add` + `lshr`).
990 if (!IsCompletelyDistributable(BinOpc1, BinOpc2, ShOpc))
991 return false;
992
993 // If BinOp2 is `and`, any mask works (this only really helps for non-splat
994 // vecs, otherwise the mask will be simplified and the following check will
995 // handle it).
996 if (BinOpc2 == Instruction::And)
997 return true;
998
999 // Otherwise, need mask that meets the below requirement.
1000 // (logic_shift (inv_logic_shift Mask, ShAmt), ShAmt) == Mask
1001 Constant *MaskInvShift =
1002 ConstantFoldBinaryOpOperands(GetInvShift(ShOpc), CMask, CShift, DL);
1003 return ConstantFoldBinaryOpOperands(ShOpc, MaskInvShift, CShift, DL) ==
1004 CMask;
1005 };
1006
1007 auto MatchBinOp = [&](unsigned ShOpnum) -> Instruction * {
1008 Constant *CMask, *CShift;
1009 Value *X, *Y, *ShiftedX, *Mask, *Shift;
1010 if (!match(I.getOperand(ShOpnum),
1011 m_OneUse(m_Shift(m_Value(Y), m_Value(Shift)))))
1012 return nullptr;
1013 if (!match(I.getOperand(1 - ShOpnum),
1015 m_OneUse(m_Shift(m_Value(X), m_Specific(Shift))),
1016 m_Value(ShiftedX)),
1017 m_Value(Mask))))
1018 return nullptr;
1019 // Make sure we are matching instruction shifts and not ConstantExpr
1020 auto *IY = dyn_cast<Instruction>(I.getOperand(ShOpnum));
1021 auto *IX = dyn_cast<Instruction>(ShiftedX);
1022 if (!IY || !IX)
1023 return nullptr;
1024
1025 // LHS and RHS need same shift opcode
1026 unsigned ShOpc = IY->getOpcode();
1027 if (ShOpc != IX->getOpcode())
1028 return nullptr;
1029
1030 // Make sure binop is real instruction and not ConstantExpr
1031 auto *BO2 = dyn_cast<Instruction>(I.getOperand(1 - ShOpnum));
1032 if (!BO2)
1033 return nullptr;
1034
1035 unsigned BinOpc = BO2->getOpcode();
1036 // Make sure we have valid binops.
1037 if (!IsValidBinOpc(I.getOpcode()) || !IsValidBinOpc(BinOpc))
1038 return nullptr;
1039
1040 if (ShOpc == Instruction::AShr) {
1041 if (Instruction::isBitwiseLogicOp(I.getOpcode()) &&
1042 BinOpc == Instruction::Xor && match(Mask, m_AllOnes())) {
1043 Value *NotX = Builder.CreateNot(X);
1044 Value *NewBinOp = Builder.CreateBinOp(I.getOpcode(), Y, NotX);
1046 static_cast<Instruction::BinaryOps>(ShOpc), NewBinOp, Shift);
1047 }
1048
1049 return nullptr;
1050 }
1051
1052 // If BinOp1 == BinOp2 and it's bitwise or shl with add, then just
1053 // distribute to drop the shift irrelevant of constants.
1054 if (BinOpc == I.getOpcode() &&
1055 IsCompletelyDistributable(I.getOpcode(), BinOpc, ShOpc)) {
1056 Value *NewBinOp2 = Builder.CreateBinOp(I.getOpcode(), X, Y);
1057 Value *NewBinOp1 = Builder.CreateBinOp(
1058 static_cast<Instruction::BinaryOps>(ShOpc), NewBinOp2, Shift);
1059 return BinaryOperator::Create(I.getOpcode(), NewBinOp1, Mask);
1060 }
1061
1062 // Otherwise we can only distribute by constant shifting the mask, so
1063 // ensure we have constants.
1064 if (!match(Shift, m_ImmConstant(CShift)))
1065 return nullptr;
1066 if (!match(Mask, m_ImmConstant(CMask)))
1067 return nullptr;
1068
1069 // Check if we can distribute the binops.
1070 if (!CanDistributeBinops(I.getOpcode(), BinOpc, ShOpc, CMask, CShift))
1071 return nullptr;
1072
1073 Constant *NewCMask =
1074 ConstantFoldBinaryOpOperands(GetInvShift(ShOpc), CMask, CShift, DL);
1075 Value *NewBinOp2 = Builder.CreateBinOp(
1076 static_cast<Instruction::BinaryOps>(BinOpc), X, NewCMask);
1077 Value *NewBinOp1 = Builder.CreateBinOp(I.getOpcode(), Y, NewBinOp2);
1078 return BinaryOperator::Create(static_cast<Instruction::BinaryOps>(ShOpc),
1079 NewBinOp1, CShift);
1080 };
1081
1082 if (Instruction *R = MatchBinOp(0))
1083 return R;
1084 return MatchBinOp(1);
1085}
1086
1087// (Binop (zext C), (select C, T, F))
1088// -> (select C, (binop 1, T), (binop 0, F))
1089//
1090// (Binop (sext C), (select C, T, F))
1091// -> (select C, (binop -1, T), (binop 0, F))
1092//
1093// Attempt to simplify binary operations into a select with folded args, when
1094// one operand of the binop is a select instruction and the other operand is a
1095// zext/sext extension, whose value is the select condition.
1098 // TODO: this simplification may be extended to any speculatable instruction,
1099 // not just binops, and would possibly be handled better in FoldOpIntoSelect.
1100 Instruction::BinaryOps Opc = I.getOpcode();
1101 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1102 Value *A, *CondVal, *TrueVal, *FalseVal;
1103 Value *CastOp;
1104
1105 auto MatchSelectAndCast = [&](Value *CastOp, Value *SelectOp) {
1106 return match(CastOp, m_ZExtOrSExt(m_Value(A))) &&
1107 A->getType()->getScalarSizeInBits() == 1 &&
1108 match(SelectOp, m_Select(m_Value(CondVal), m_Value(TrueVal),
1109 m_Value(FalseVal)));
1110 };
1111
1112 // Make sure one side of the binop is a select instruction, and the other is a
1113 // zero/sign extension operating on a i1.
1114 if (MatchSelectAndCast(LHS, RHS))
1115 CastOp = LHS;
1116 else if (MatchSelectAndCast(RHS, LHS))
1117 CastOp = RHS;
1118 else
1119 return nullptr;
1120
1121 auto NewFoldedConst = [&](bool IsTrueArm, Value *V) {
1122 bool IsCastOpRHS = (CastOp == RHS);
1123 bool IsZExt = isa<ZExtInst>(CastOp);
1124 Constant *C;
1125
1126 if (IsTrueArm) {
1127 C = Constant::getNullValue(V->getType());
1128 } else if (IsZExt) {
1129 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1130 C = Constant::getIntegerValue(V->getType(), APInt(BitWidth, 1));
1131 } else {
1132 C = Constant::getAllOnesValue(V->getType());
1133 }
1134
1135 return IsCastOpRHS ? Builder.CreateBinOp(Opc, V, C)
1136 : Builder.CreateBinOp(Opc, C, V);
1137 };
1138
1139 // If the value used in the zext/sext is the select condition, or the negated
1140 // of the select condition, the binop can be simplified.
1141 if (CondVal == A) {
1142 Value *NewTrueVal = NewFoldedConst(false, TrueVal);
1143 return SelectInst::Create(CondVal, NewTrueVal,
1144 NewFoldedConst(true, FalseVal));
1145 }
1146
1147 if (match(A, m_Not(m_Specific(CondVal)))) {
1148 Value *NewTrueVal = NewFoldedConst(true, TrueVal);
1149 return SelectInst::Create(CondVal, NewTrueVal,
1150 NewFoldedConst(false, FalseVal));
1151 }
1152
1153 return nullptr;
1154}
1155
1157 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1158 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
1159 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
1160 Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
1161 Value *A, *B, *C, *D;
1162 Instruction::BinaryOps LHSOpcode, RHSOpcode;
1163
1164 if (Op0)
1165 LHSOpcode = getBinOpsForFactorization(TopLevelOpcode, Op0, A, B, Op1);
1166 if (Op1)
1167 RHSOpcode = getBinOpsForFactorization(TopLevelOpcode, Op1, C, D, Op0);
1168
1169 // The instruction has the form "(A op' B) op (C op' D)". Try to factorize
1170 // a common term.
1171 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
1172 if (Value *V = tryFactorization(I, SQ, Builder, LHSOpcode, A, B, C, D))
1173 return V;
1174
1175 // The instruction has the form "(A op' B) op (C)". Try to factorize common
1176 // term.
1177 if (Op0)
1178 if (Value *Ident = getIdentityValue(LHSOpcode, RHS))
1179 if (Value *V =
1180 tryFactorization(I, SQ, Builder, LHSOpcode, A, B, RHS, Ident))
1181 return V;
1182
1183 // The instruction has the form "(B) op (C op' D)". Try to factorize common
1184 // term.
1185 if (Op1)
1186 if (Value *Ident = getIdentityValue(RHSOpcode, LHS))
1187 if (Value *V =
1188 tryFactorization(I, SQ, Builder, RHSOpcode, LHS, Ident, C, D))
1189 return V;
1190
1191 return nullptr;
1192}
1193
1194/// This tries to simplify binary operations which some other binary operation
1195/// distributes over either by factorizing out common terms
1196/// (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this results in
1197/// simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is a win).
1198/// Returns the simplified value, or null if it didn't simplify.
1200 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1201 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
1202 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
1203 Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
1204
1205 // Factorization.
1206 if (Value *R = tryFactorizationFolds(I))
1207 return R;
1208
1209 // Expansion.
1210 if (Op0 && rightDistributesOverLeft(Op0->getOpcode(), TopLevelOpcode)) {
1211 // The instruction has the form "(A op' B) op C". See if expanding it out
1212 // to "(A op C) op' (B op C)" results in simplifications.
1213 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
1214 Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
1215
1216 // Disable the use of undef because it's not safe to distribute undef.
1217 auto SQDistributive = SQ.getWithInstruction(&I).getWithoutUndef();
1218 Value *L = simplifyBinOp(TopLevelOpcode, A, C, SQDistributive);
1219 Value *R = simplifyBinOp(TopLevelOpcode, B, C, SQDistributive);
1220
1221 // Do "A op C" and "B op C" both simplify?
1222 if (L && R) {
1223 // They do! Return "L op' R".
1224 ++NumExpand;
1225 C = Builder.CreateBinOp(InnerOpcode, L, R);
1226 C->takeName(&I);
1227 return C;
1228 }
1229
1230 // Does "A op C" simplify to the identity value for the inner opcode?
1231 if (L && L == ConstantExpr::getBinOpIdentity(InnerOpcode, L->getType())) {
1232 // They do! Return "B op C".
1233 ++NumExpand;
1234 C = Builder.CreateBinOp(TopLevelOpcode, B, C);
1235 C->takeName(&I);
1236 return C;
1237 }
1238
1239 // Does "B op C" simplify to the identity value for the inner opcode?
1240 if (R && R == ConstantExpr::getBinOpIdentity(InnerOpcode, R->getType())) {
1241 // They do! Return "A op C".
1242 ++NumExpand;
1243 C = Builder.CreateBinOp(TopLevelOpcode, A, C);
1244 C->takeName(&I);
1245 return C;
1246 }
1247 }
1248
1249 if (Op1 && leftDistributesOverRight(TopLevelOpcode, Op1->getOpcode())) {
1250 // The instruction has the form "A op (B op' C)". See if expanding it out
1251 // to "(A op B) op' (A op C)" results in simplifications.
1252 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
1253 Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op'
1254
1255 // Disable the use of undef because it's not safe to distribute undef.
1256 auto SQDistributive = SQ.getWithInstruction(&I).getWithoutUndef();
1257 Value *L = simplifyBinOp(TopLevelOpcode, A, B, SQDistributive);
1258 Value *R = simplifyBinOp(TopLevelOpcode, A, C, SQDistributive);
1259
1260 // Do "A op B" and "A op C" both simplify?
1261 if (L && R) {
1262 // They do! Return "L op' R".
1263 ++NumExpand;
1264 A = Builder.CreateBinOp(InnerOpcode, L, R);
1265 A->takeName(&I);
1266 return A;
1267 }
1268
1269 // Does "A op B" simplify to the identity value for the inner opcode?
1270 if (L && L == ConstantExpr::getBinOpIdentity(InnerOpcode, L->getType())) {
1271 // They do! Return "A op C".
1272 ++NumExpand;
1273 A = Builder.CreateBinOp(TopLevelOpcode, A, C);
1274 A->takeName(&I);
1275 return A;
1276 }
1277
1278 // Does "A op C" simplify to the identity value for the inner opcode?
1279 if (R && R == ConstantExpr::getBinOpIdentity(InnerOpcode, R->getType())) {
1280 // They do! Return "A op B".
1281 ++NumExpand;
1282 A = Builder.CreateBinOp(TopLevelOpcode, A, B);
1283 A->takeName(&I);
1284 return A;
1285 }
1286 }
1287
1289}
1290
1291static std::optional<std::pair<Value *, Value *>>
1293 if (LHS->getParent() != RHS->getParent())
1294 return std::nullopt;
1295
1296 if (LHS->getNumIncomingValues() < 2)
1297 return std::nullopt;
1298
1299 if (!equal(LHS->blocks(), RHS->blocks()))
1300 return std::nullopt;
1301
1302 Value *L0 = LHS->getIncomingValue(0);
1303 Value *R0 = RHS->getIncomingValue(0);
1304
1305 for (unsigned I = 1, E = LHS->getNumIncomingValues(); I != E; ++I) {
1306 Value *L1 = LHS->getIncomingValue(I);
1307 Value *R1 = RHS->getIncomingValue(I);
1308
1309 if ((L0 == L1 && R0 == R1) || (L0 == R1 && R0 == L1))
1310 continue;
1311
1312 return std::nullopt;
1313 }
1314
1315 return std::optional(std::pair(L0, R0));
1316}
1317
1318std::optional<std::pair<Value *, Value *>>
1319InstCombinerImpl::matchSymmetricPair(Value *LHS, Value *RHS) {
1320 Instruction *LHSInst = dyn_cast<Instruction>(LHS);
1321 Instruction *RHSInst = dyn_cast<Instruction>(RHS);
1322 if (!LHSInst || !RHSInst || LHSInst->getOpcode() != RHSInst->getOpcode())
1323 return std::nullopt;
1324 switch (LHSInst->getOpcode()) {
1325 case Instruction::PHI:
1326 return matchSymmetricPhiNodesPair(cast<PHINode>(LHS), cast<PHINode>(RHS));
1327 case Instruction::Select: {
1328 Value *Cond = LHSInst->getOperand(0);
1329 Value *TrueVal = LHSInst->getOperand(1);
1330 Value *FalseVal = LHSInst->getOperand(2);
1331 if (Cond == RHSInst->getOperand(0) && TrueVal == RHSInst->getOperand(2) &&
1332 FalseVal == RHSInst->getOperand(1))
1333 return std::pair(TrueVal, FalseVal);
1334 return std::nullopt;
1335 }
1336 case Instruction::Call: {
1337 // Match min(a, b) and max(a, b)
1338 MinMaxIntrinsic *LHSMinMax = dyn_cast<MinMaxIntrinsic>(LHSInst);
1339 MinMaxIntrinsic *RHSMinMax = dyn_cast<MinMaxIntrinsic>(RHSInst);
1340 if (LHSMinMax && RHSMinMax &&
1341 LHSMinMax->getPredicate() ==
1343 ((LHSMinMax->getLHS() == RHSMinMax->getLHS() &&
1344 LHSMinMax->getRHS() == RHSMinMax->getRHS()) ||
1345 (LHSMinMax->getLHS() == RHSMinMax->getRHS() &&
1346 LHSMinMax->getRHS() == RHSMinMax->getLHS())))
1347 return std::pair(LHSMinMax->getLHS(), LHSMinMax->getRHS());
1348 return std::nullopt;
1349 }
1350 default:
1351 return std::nullopt;
1352 }
1353}
1354
1356 Value *LHS,
1357 Value *RHS) {
1358 Value *A, *B, *C, *D, *E, *F;
1359 bool LHSIsSelect = match(LHS, m_Select(m_Value(A), m_Value(B), m_Value(C)));
1360 bool RHSIsSelect = match(RHS, m_Select(m_Value(D), m_Value(E), m_Value(F)));
1361 if (!LHSIsSelect && !RHSIsSelect)
1362 return nullptr;
1363
1364 FastMathFlags FMF;
1366 if (isa<FPMathOperator>(&I)) {
1367 FMF = I.getFastMathFlags();
1369 }
1370
1371 Instruction::BinaryOps Opcode = I.getOpcode();
1373
1374 Value *Cond, *True = nullptr, *False = nullptr;
1375
1376 // Special-case for add/negate combination. Replace the zero in the negation
1377 // with the trailing add operand:
1378 // (Cond ? TVal : -N) + Z --> Cond ? True : (Z - N)
1379 // (Cond ? -N : FVal) + Z --> Cond ? (Z - N) : False
1380 auto foldAddNegate = [&](Value *TVal, Value *FVal, Value *Z) -> Value * {
1381 // We need an 'add' and exactly 1 arm of the select to have been simplified.
1382 if (Opcode != Instruction::Add || (!True && !False) || (True && False))
1383 return nullptr;
1384
1385 Value *N;
1386 if (True && match(FVal, m_Neg(m_Value(N)))) {
1387 Value *Sub = Builder.CreateSub(Z, N);
1388 return Builder.CreateSelect(Cond, True, Sub, I.getName());
1389 }
1390 if (False && match(TVal, m_Neg(m_Value(N)))) {
1391 Value *Sub = Builder.CreateSub(Z, N);
1392 return Builder.CreateSelect(Cond, Sub, False, I.getName());
1393 }
1394 return nullptr;
1395 };
1396
1397 if (LHSIsSelect && RHSIsSelect && A == D) {
1398 // (A ? B : C) op (A ? E : F) -> A ? (B op E) : (C op F)
1399 Cond = A;
1400 True = simplifyBinOp(Opcode, B, E, FMF, Q);
1401 False = simplifyBinOp(Opcode, C, F, FMF, Q);
1402
1403 if (LHS->hasOneUse() && RHS->hasOneUse()) {
1404 if (False && !True)
1405 True = Builder.CreateBinOp(Opcode, B, E);
1406 else if (True && !False)
1407 False = Builder.CreateBinOp(Opcode, C, F);
1408 }
1409 } else if (LHSIsSelect && LHS->hasOneUse()) {
1410 // (A ? B : C) op Y -> A ? (B op Y) : (C op Y)
1411 Cond = A;
1412 True = simplifyBinOp(Opcode, B, RHS, FMF, Q);
1413 False = simplifyBinOp(Opcode, C, RHS, FMF, Q);
1414 if (Value *NewSel = foldAddNegate(B, C, RHS))
1415 return NewSel;
1416 } else if (RHSIsSelect && RHS->hasOneUse()) {
1417 // X op (D ? E : F) -> D ? (X op E) : (X op F)
1418 Cond = D;
1419 True = simplifyBinOp(Opcode, LHS, E, FMF, Q);
1420 False = simplifyBinOp(Opcode, LHS, F, FMF, Q);
1421 if (Value *NewSel = foldAddNegate(E, F, LHS))
1422 return NewSel;
1423 }
1424
1425 if (!True || !False)
1426 return nullptr;
1427
1428 Value *SI = Builder.CreateSelect(Cond, True, False);
1429 SI->takeName(&I);
1430 return SI;
1431}
1432
1433/// Freely adapt every user of V as-if V was changed to !V.
1434/// WARNING: only if canFreelyInvertAllUsersOf() said this can be done.
1436 assert(!isa<Constant>(I) && "Shouldn't invert users of constant");
1437 for (User *U : make_early_inc_range(I->users())) {
1438 if (U == IgnoredUser)
1439 continue; // Don't consider this user.
1440 switch (cast<Instruction>(U)->getOpcode()) {
1441 case Instruction::Select: {
1442 auto *SI = cast<SelectInst>(U);
1443 SI->swapValues();
1444 SI->swapProfMetadata();
1445 break;
1446 }
1447 case Instruction::Br: {
1448 BranchInst *BI = cast<BranchInst>(U);
1449 BI->swapSuccessors(); // swaps prof metadata too
1450 if (BPI)
1452 break;
1453 }
1454 case Instruction::Xor:
1455 replaceInstUsesWith(cast<Instruction>(*U), I);
1456 // Add to worklist for DCE.
1457 addToWorklist(cast<Instruction>(U));
1458 break;
1459 default:
1460 llvm_unreachable("Got unexpected user - out of sync with "
1461 "canFreelyInvertAllUsersOf() ?");
1462 }
1463 }
1464
1465 // Update pre-existing debug value uses.
1466 SmallVector<DbgVariableRecord *, 4> DbgVariableRecords;
1467 llvm::findDbgValues(I, DbgVariableRecords);
1468
1469 for (DbgVariableRecord *DbgVal : DbgVariableRecords) {
1470 SmallVector<uint64_t, 1> Ops = {dwarf::DW_OP_not};
1471 for (unsigned Idx = 0, End = DbgVal->getNumVariableLocationOps();
1472 Idx != End; ++Idx)
1473 if (DbgVal->getVariableLocationOp(Idx) == I)
1474 DbgVal->setExpression(
1475 DIExpression::appendOpsToArg(DbgVal->getExpression(), Ops, Idx));
1476 }
1477}
1478
1479/// Given a 'sub' instruction, return the RHS of the instruction if the LHS is a
1480/// constant zero (which is the 'negate' form).
1481Value *InstCombinerImpl::dyn_castNegVal(Value *V) const {
1482 Value *NegV;
1483 if (match(V, m_Neg(m_Value(NegV))))
1484 return NegV;
1485
1486 // Constants can be considered to be negated values if they can be folded.
1487 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
1488 return ConstantExpr::getNeg(C);
1489
1490 if (ConstantDataVector *C = dyn_cast<ConstantDataVector>(V))
1491 if (C->getType()->getElementType()->isIntegerTy())
1492 return ConstantExpr::getNeg(C);
1493
1494 if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
1495 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1496 Constant *Elt = CV->getAggregateElement(i);
1497 if (!Elt)
1498 return nullptr;
1499
1500 if (isa<UndefValue>(Elt))
1501 continue;
1502
1503 if (!isa<ConstantInt>(Elt))
1504 return nullptr;
1505 }
1506 return ConstantExpr::getNeg(CV);
1507 }
1508
1509 // Negate integer vector splats.
1510 if (auto *CV = dyn_cast<Constant>(V))
1511 if (CV->getType()->isVectorTy() &&
1512 CV->getType()->getScalarType()->isIntegerTy() && CV->getSplatValue())
1513 return ConstantExpr::getNeg(CV);
1514
1515 return nullptr;
1516}
1517
1518// Try to fold:
1519// 1) (fp_binop ({s|u}itofp x), ({s|u}itofp y))
1520// -> ({s|u}itofp (int_binop x, y))
1521// 2) (fp_binop ({s|u}itofp x), FpC)
1522// -> ({s|u}itofp (int_binop x, (fpto{s|u}i FpC)))
1523//
1524// Assuming the sign of the cast for x/y is `OpsFromSigned`.
1525Instruction *InstCombinerImpl::foldFBinOpOfIntCastsFromSign(
1526 BinaryOperator &BO, bool OpsFromSigned, std::array<Value *, 2> IntOps,
1528
1529 Type *FPTy = BO.getType();
1530 Type *IntTy = IntOps[0]->getType();
1531
1532 unsigned IntSz = IntTy->getScalarSizeInBits();
1533 // This is the maximum number of inuse bits by the integer where the int -> fp
1534 // casts are exact.
1535 unsigned MaxRepresentableBits =
1537
1538 // Preserve known number of leading bits. This can allow us to trivial nsw/nuw
1539 // checks later on.
1540 unsigned NumUsedLeadingBits[2] = {IntSz, IntSz};
1541
1542 // NB: This only comes up if OpsFromSigned is true, so there is no need to
1543 // cache if between calls to `foldFBinOpOfIntCastsFromSign`.
1544 auto IsNonZero = [&](unsigned OpNo) -> bool {
1545 if (OpsKnown[OpNo].hasKnownBits() &&
1546 OpsKnown[OpNo].getKnownBits(SQ).isNonZero())
1547 return true;
1548 return isKnownNonZero(IntOps[OpNo], SQ);
1549 };
1550
1551 auto IsNonNeg = [&](unsigned OpNo) -> bool {
1552 // NB: This matches the impl in ValueTracking, we just try to use cached
1553 // knownbits here. If we ever start supporting WithCache for
1554 // `isKnownNonNegative`, change this to an explicit call.
1555 return OpsKnown[OpNo].getKnownBits(SQ).isNonNegative();
1556 };
1557
1558 // Check if we know for certain that ({s|u}itofp op) is exact.
1559 auto IsValidPromotion = [&](unsigned OpNo) -> bool {
1560 // Can we treat this operand as the desired sign?
1561 if (OpsFromSigned != isa<SIToFPInst>(BO.getOperand(OpNo)) &&
1562 !IsNonNeg(OpNo))
1563 return false;
1564
1565 // If fp precision >= bitwidth(op) then its exact.
1566 // NB: This is slightly conservative for `sitofp`. For signed conversion, we
1567 // can handle `MaxRepresentableBits == IntSz - 1` as the sign bit will be
1568 // handled specially. We can't, however, increase the bound arbitrarily for
1569 // `sitofp` as for larger sizes, it won't sign extend.
1570 if (MaxRepresentableBits < IntSz) {
1571 // Otherwise if its signed cast check that fp precisions >= bitwidth(op) -
1572 // numSignBits(op).
1573 // TODO: If we add support for `WithCache` in `ComputeNumSignBits`, change
1574 // `IntOps[OpNo]` arguments to `KnownOps[OpNo]`.
1575 if (OpsFromSigned)
1576 NumUsedLeadingBits[OpNo] = IntSz - ComputeNumSignBits(IntOps[OpNo]);
1577 // Finally for unsigned check that fp precision >= bitwidth(op) -
1578 // numLeadingZeros(op).
1579 else {
1580 NumUsedLeadingBits[OpNo] =
1581 IntSz - OpsKnown[OpNo].getKnownBits(SQ).countMinLeadingZeros();
1582 }
1583 }
1584 // NB: We could also check if op is known to be a power of 2 or zero (which
1585 // will always be representable). Its unlikely, however, that is we are
1586 // unable to bound op in any way we will be able to pass the overflow checks
1587 // later on.
1588
1589 if (MaxRepresentableBits < NumUsedLeadingBits[OpNo])
1590 return false;
1591 // Signed + Mul also requires that op is non-zero to avoid -0 cases.
1592 return !OpsFromSigned || BO.getOpcode() != Instruction::FMul ||
1593 IsNonZero(OpNo);
1594 };
1595
1596 // If we have a constant rhs, see if we can losslessly convert it to an int.
1597 if (Op1FpC != nullptr) {
1598 // Signed + Mul req non-zero
1599 if (OpsFromSigned && BO.getOpcode() == Instruction::FMul &&
1600 !match(Op1FpC, m_NonZeroFP()))
1601 return nullptr;
1602
1604 OpsFromSigned ? Instruction::FPToSI : Instruction::FPToUI, Op1FpC,
1605 IntTy, DL);
1606 if (Op1IntC == nullptr)
1607 return nullptr;
1608 if (ConstantFoldCastOperand(OpsFromSigned ? Instruction::SIToFP
1609 : Instruction::UIToFP,
1610 Op1IntC, FPTy, DL) != Op1FpC)
1611 return nullptr;
1612
1613 // First try to keep sign of cast the same.
1614 IntOps[1] = Op1IntC;
1615 }
1616
1617 // Ensure lhs/rhs integer types match.
1618 if (IntTy != IntOps[1]->getType())
1619 return nullptr;
1620
1621 if (Op1FpC == nullptr) {
1622 if (!IsValidPromotion(1))
1623 return nullptr;
1624 }
1625 if (!IsValidPromotion(0))
1626 return nullptr;
1627
1628 // Final we check if the integer version of the binop will not overflow.
1630 // Because of the precision check, we can often rule out overflows.
1631 bool NeedsOverflowCheck = true;
1632 // Try to conservatively rule out overflow based on the already done precision
1633 // checks.
1634 unsigned OverflowMaxOutputBits = OpsFromSigned ? 2 : 1;
1635 unsigned OverflowMaxCurBits =
1636 std::max(NumUsedLeadingBits[0], NumUsedLeadingBits[1]);
1637 bool OutputSigned = OpsFromSigned;
1638 switch (BO.getOpcode()) {
1639 case Instruction::FAdd:
1640 IntOpc = Instruction::Add;
1641 OverflowMaxOutputBits += OverflowMaxCurBits;
1642 break;
1643 case Instruction::FSub:
1644 IntOpc = Instruction::Sub;
1645 OverflowMaxOutputBits += OverflowMaxCurBits;
1646 break;
1647 case Instruction::FMul:
1648 IntOpc = Instruction::Mul;
1649 OverflowMaxOutputBits += OverflowMaxCurBits * 2;
1650 break;
1651 default:
1652 llvm_unreachable("Unsupported binop");
1653 }
1654 // The precision check may have already ruled out overflow.
1655 if (OverflowMaxOutputBits < IntSz) {
1656 NeedsOverflowCheck = false;
1657 // We can bound unsigned overflow from sub to in range signed value (this is
1658 // what allows us to avoid the overflow check for sub).
1659 if (IntOpc == Instruction::Sub)
1660 OutputSigned = true;
1661 }
1662
1663 // Precision check did not rule out overflow, so need to check.
1664 // TODO: If we add support for `WithCache` in `willNotOverflow`, change
1665 // `IntOps[...]` arguments to `KnownOps[...]`.
1666 if (NeedsOverflowCheck &&
1667 !willNotOverflow(IntOpc, IntOps[0], IntOps[1], BO, OutputSigned))
1668 return nullptr;
1669
1670 Value *IntBinOp = Builder.CreateBinOp(IntOpc, IntOps[0], IntOps[1]);
1671 if (auto *IntBO = dyn_cast<BinaryOperator>(IntBinOp)) {
1672 IntBO->setHasNoSignedWrap(OutputSigned);
1673 IntBO->setHasNoUnsignedWrap(!OutputSigned);
1674 }
1675 if (OutputSigned)
1676 return new SIToFPInst(IntBinOp, FPTy);
1677 return new UIToFPInst(IntBinOp, FPTy);
1678}
1679
1680// Try to fold:
1681// 1) (fp_binop ({s|u}itofp x), ({s|u}itofp y))
1682// -> ({s|u}itofp (int_binop x, y))
1683// 2) (fp_binop ({s|u}itofp x), FpC)
1684// -> ({s|u}itofp (int_binop x, (fpto{s|u}i FpC)))
1685Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) {
1686 std::array<Value *, 2> IntOps = {nullptr, nullptr};
1687 Constant *Op1FpC = nullptr;
1688 // Check for:
1689 // 1) (binop ({s|u}itofp x), ({s|u}itofp y))
1690 // 2) (binop ({s|u}itofp x), FpC)
1691 if (!match(BO.getOperand(0), m_SIToFP(m_Value(IntOps[0]))) &&
1692 !match(BO.getOperand(0), m_UIToFP(m_Value(IntOps[0]))))
1693 return nullptr;
1694
1695 if (!match(BO.getOperand(1), m_Constant(Op1FpC)) &&
1696 !match(BO.getOperand(1), m_SIToFP(m_Value(IntOps[1]))) &&
1697 !match(BO.getOperand(1), m_UIToFP(m_Value(IntOps[1]))))
1698 return nullptr;
1699
1700 // Cache KnownBits a bit to potentially save some analysis.
1701 SmallVector<WithCache<const Value *>, 2> OpsKnown = {IntOps[0], IntOps[1]};
1702
1703 // Try treating x/y as coming from both `uitofp` and `sitofp`. There are
1704 // different constraints depending on the sign of the cast.
1705 // NB: `(uitofp nneg X)` == `(sitofp nneg X)`.
1706 if (Instruction *R = foldFBinOpOfIntCastsFromSign(BO, /*OpsFromSigned=*/false,
1707 IntOps, Op1FpC, OpsKnown))
1708 return R;
1709 return foldFBinOpOfIntCastsFromSign(BO, /*OpsFromSigned=*/true, IntOps,
1710 Op1FpC, OpsKnown);
1711}
1712
1713/// A binop with a constant operand and a sign-extended boolean operand may be
1714/// converted into a select of constants by applying the binary operation to
1715/// the constant with the two possible values of the extended boolean (0 or -1).
1716Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
1717 // TODO: Handle non-commutative binop (constant is operand 0).
1718 // TODO: Handle zext.
1719 // TODO: Peek through 'not' of cast.
1720 Value *BO0 = BO.getOperand(0);
1721 Value *BO1 = BO.getOperand(1);
1722 Value *X;
1723 Constant *C;
1724 if (!match(BO0, m_SExt(m_Value(X))) || !match(BO1, m_ImmConstant(C)) ||
1725 !X->getType()->isIntOrIntVectorTy(1))
1726 return nullptr;
1727
1728 // bo (sext i1 X), C --> select X, (bo -1, C), (bo 0, C)
1731 Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
1732 Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
1733 return SelectInst::Create(X, TVal, FVal);
1734}
1735
1737 bool IsTrueArm) {
1739 for (Value *Op : I.operands()) {
1740 Value *V = nullptr;
1741 if (Op == SI) {
1742 V = IsTrueArm ? SI->getTrueValue() : SI->getFalseValue();
1743 } else if (match(SI->getCondition(),
1746 m_Specific(Op), m_Value(V))) &&
1748 // Pass
1749 } else {
1750 V = Op;
1751 }
1752 Ops.push_back(V);
1753 }
1754
1755 return simplifyInstructionWithOperands(&I, Ops, I.getDataLayout());
1756}
1757
1759 Value *NewOp, InstCombiner &IC) {
1760 Instruction *Clone = I.clone();
1761 Clone->replaceUsesOfWith(SI, NewOp);
1763 IC.InsertNewInstBefore(Clone, I.getIterator());
1764 return Clone;
1765}
1766
1768 bool FoldWithMultiUse) {
1769 // Don't modify shared select instructions unless set FoldWithMultiUse
1770 if (!SI->hasOneUse() && !FoldWithMultiUse)
1771 return nullptr;
1772
1773 Value *TV = SI->getTrueValue();
1774 Value *FV = SI->getFalseValue();
1775
1776 // Bool selects with constant operands can be folded to logical ops.
1777 if (SI->getType()->isIntOrIntVectorTy(1))
1778 return nullptr;
1779
1780 // Avoid breaking min/max reduction pattern,
1781 // which is necessary for vectorization later.
1782 if (isa<MinMaxIntrinsic>(&Op))
1783 for (Value *IntrinOp : Op.operands())
1784 if (auto *PN = dyn_cast<PHINode>(IntrinOp))
1785 for (Value *PhiOp : PN->operands())
1786 if (PhiOp == &Op)
1787 return nullptr;
1788
1789 // Test if a FCmpInst instruction is used exclusively by a select as
1790 // part of a minimum or maximum operation. If so, refrain from doing
1791 // any other folding. This helps out other analyses which understand
1792 // non-obfuscated minimum and maximum idioms. And in this case, at
1793 // least one of the comparison operands has at least one user besides
1794 // the compare (the select), which would often largely negate the
1795 // benefit of folding anyway.
1796 if (auto *CI = dyn_cast<FCmpInst>(SI->getCondition())) {
1797 if (CI->hasOneUse()) {
1798 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1799 if (((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1)) &&
1800 !CI->isCommutative())
1801 return nullptr;
1802 }
1803 }
1804
1805 // Make sure that one of the select arms folds successfully.
1806 Value *NewTV = simplifyOperationIntoSelectOperand(Op, SI, /*IsTrueArm=*/true);
1807 Value *NewFV =
1808 simplifyOperationIntoSelectOperand(Op, SI, /*IsTrueArm=*/false);
1809 if (!NewTV && !NewFV)
1810 return nullptr;
1811
1812 // Create an instruction for the arm that did not fold.
1813 if (!NewTV)
1814 NewTV = foldOperationIntoSelectOperand(Op, SI, TV, *this);
1815 if (!NewFV)
1816 NewFV = foldOperationIntoSelectOperand(Op, SI, FV, *this);
1817 return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
1818}
1819
1821 Value *InValue, BasicBlock *InBB,
1822 const DataLayout &DL,
1823 const SimplifyQuery SQ) {
1824 // NB: It is a precondition of this transform that the operands be
1825 // phi translatable!
1827 for (Value *Op : I.operands()) {
1828 if (Op == PN)
1829 Ops.push_back(InValue);
1830 else
1831 Ops.push_back(Op->DoPHITranslation(PN->getParent(), InBB));
1832 }
1833
1834 // Don't consider the simplification successful if we get back a constant
1835 // expression. That's just an instruction in hiding.
1836 // Also reject the case where we simplify back to the phi node. We wouldn't
1837 // be able to remove it in that case.
1839 &I, Ops, SQ.getWithInstruction(InBB->getTerminator()));
1840 if (NewVal && NewVal != PN && !match(NewVal, m_ConstantExpr()))
1841 return NewVal;
1842
1843 // Check if incoming PHI value can be replaced with constant
1844 // based on implied condition.
1845 BranchInst *TerminatorBI = dyn_cast<BranchInst>(InBB->getTerminator());
1846 const ICmpInst *ICmp = dyn_cast<ICmpInst>(&I);
1847 if (TerminatorBI && TerminatorBI->isConditional() &&
1848 TerminatorBI->getSuccessor(0) != TerminatorBI->getSuccessor(1) && ICmp) {
1849 bool LHSIsTrue = TerminatorBI->getSuccessor(0) == PN->getParent();
1850 std::optional<bool> ImpliedCond = isImpliedCondition(
1851 TerminatorBI->getCondition(), ICmp->getCmpPredicate(), Ops[0], Ops[1],
1852 DL, LHSIsTrue);
1853 if (ImpliedCond)
1854 return ConstantInt::getBool(I.getType(), ImpliedCond.value());
1855 }
1856
1857 return nullptr;
1858}
1859
1861 bool AllowMultipleUses) {
1862 unsigned NumPHIValues = PN->getNumIncomingValues();
1863 if (NumPHIValues == 0)
1864 return nullptr;
1865
1866 // We normally only transform phis with a single use. However, if a PHI has
1867 // multiple uses and they are all the same operation, we can fold *all* of the
1868 // uses into the PHI.
1869 bool OneUse = PN->hasOneUse();
1870 bool IdenticalUsers = false;
1871 if (!AllowMultipleUses && !OneUse) {
1872 // Walk the use list for the instruction, comparing them to I.
1873 for (User *U : PN->users()) {
1874 Instruction *UI = cast<Instruction>(U);
1875 if (UI != &I && !I.isIdenticalTo(UI))
1876 return nullptr;
1877 }
1878 // Otherwise, we can replace *all* users with the new PHI we form.
1879 IdenticalUsers = true;
1880 }
1881
1882 // Check that all operands are phi-translatable.
1883 for (Value *Op : I.operands()) {
1884 if (Op == PN)
1885 continue;
1886
1887 // Non-instructions never require phi-translation.
1888 auto *I = dyn_cast<Instruction>(Op);
1889 if (!I)
1890 continue;
1891
1892 // Phi-translate can handle phi nodes in the same block.
1893 if (isa<PHINode>(I))
1894 if (I->getParent() == PN->getParent())
1895 continue;
1896
1897 // Operand dominates the block, no phi-translation necessary.
1898 if (DT.dominates(I, PN->getParent()))
1899 continue;
1900
1901 // Not phi-translatable, bail out.
1902 return nullptr;
1903 }
1904
1905 // Check to see whether the instruction can be folded into each phi operand.
1906 // If there is one operand that does not fold, remember the BB it is in.
1907 SmallVector<Value *> NewPhiValues;
1908 SmallVector<unsigned int> OpsToMoveUseToIncomingBB;
1909 bool SeenNonSimplifiedInVal = false;
1910 for (unsigned i = 0; i != NumPHIValues; ++i) {
1911 Value *InVal = PN->getIncomingValue(i);
1912 BasicBlock *InBB = PN->getIncomingBlock(i);
1913
1914 if (auto *NewVal = simplifyInstructionWithPHI(I, PN, InVal, InBB, DL, SQ)) {
1915 NewPhiValues.push_back(NewVal);
1916 continue;
1917 }
1918
1919 // Handle some cases that can't be fully simplified, but where we know that
1920 // the two instructions will fold into one.
1921 auto WillFold = [&]() {
1922 if (!InVal->hasUseList() || !InVal->hasOneUser())
1923 return false;
1924
1925 // icmp of ucmp/scmp with constant will fold to icmp.
1926 const APInt *Ignored;
1927 if (isa<CmpIntrinsic>(InVal) &&
1928 match(&I, m_ICmp(m_Specific(PN), m_APInt(Ignored))))
1929 return true;
1930
1931 // icmp eq zext(bool), 0 will fold to !bool.
1932 if (isa<ZExtInst>(InVal) &&
1933 cast<ZExtInst>(InVal)->getSrcTy()->isIntOrIntVectorTy(1) &&
1934 match(&I,
1936 return true;
1937
1938 return false;
1939 };
1940
1941 if (WillFold()) {
1942 OpsToMoveUseToIncomingBB.push_back(i);
1943 NewPhiValues.push_back(nullptr);
1944 continue;
1945 }
1946
1947 if (!OneUse && !IdenticalUsers)
1948 return nullptr;
1949
1950 if (SeenNonSimplifiedInVal)
1951 return nullptr; // More than one non-simplified value.
1952 SeenNonSimplifiedInVal = true;
1953
1954 // If there is exactly one non-simplified value, we can insert a copy of the
1955 // operation in that block. However, if this is a critical edge, we would
1956 // be inserting the computation on some other paths (e.g. inside a loop).
1957 // Only do this if the pred block is unconditionally branching into the phi
1958 // block. Also, make sure that the pred block is not dead code.
1959 BranchInst *BI = dyn_cast<BranchInst>(InBB->getTerminator());
1960 if (!BI || !BI->isUnconditional() || !DT.isReachableFromEntry(InBB))
1961 return nullptr;
1962
1963 NewPhiValues.push_back(nullptr);
1964 OpsToMoveUseToIncomingBB.push_back(i);
1965
1966 // If the InVal is an invoke at the end of the pred block, then we can't
1967 // insert a computation after it without breaking the edge.
1968 if (isa<InvokeInst>(InVal))
1969 if (cast<Instruction>(InVal)->getParent() == InBB)
1970 return nullptr;
1971
1972 // Do not push the operation across a loop backedge. This could result in
1973 // an infinite combine loop, and is generally non-profitable (especially
1974 // if the operation was originally outside the loop).
1975 if (isBackEdge(InBB, PN->getParent()))
1976 return nullptr;
1977 }
1978
1979 // Clone the instruction that uses the phi node and move it into the incoming
1980 // BB because we know that the next iteration of InstCombine will simplify it.
1982 for (auto OpIndex : OpsToMoveUseToIncomingBB) {
1984 BasicBlock *OpBB = PN->getIncomingBlock(OpIndex);
1985
1986 Instruction *Clone = Clones.lookup(OpBB);
1987 if (!Clone) {
1988 Clone = I.clone();
1989 for (Use &U : Clone->operands()) {
1990 if (U == PN)
1991 U = Op;
1992 else
1993 U = U->DoPHITranslation(PN->getParent(), OpBB);
1994 }
1995 Clone = InsertNewInstBefore(Clone, OpBB->getTerminator()->getIterator());
1996 Clones.insert({OpBB, Clone});
1997 // We may have speculated the instruction.
1999 }
2000
2001 NewPhiValues[OpIndex] = Clone;
2002 }
2003
2004 // Okay, we can do the transformation: create the new PHI node.
2005 PHINode *NewPN = PHINode::Create(I.getType(), PN->getNumIncomingValues());
2006 InsertNewInstBefore(NewPN, PN->getIterator());
2007 NewPN->takeName(PN);
2008 NewPN->setDebugLoc(PN->getDebugLoc());
2009
2010 for (unsigned i = 0; i != NumPHIValues; ++i)
2011 NewPN->addIncoming(NewPhiValues[i], PN->getIncomingBlock(i));
2012
2013 if (IdenticalUsers) {
2014 // Collect and deduplicate users up-front to avoid iterator invalidation.
2016 for (User *U : PN->users()) {
2017 Instruction *User = cast<Instruction>(U);
2018 if (User == &I)
2019 continue;
2020 ToReplace.insert(User);
2021 }
2022 for (Instruction *I : ToReplace) {
2023 replaceInstUsesWith(*I, NewPN);
2025 }
2026 OneUse = true;
2027 }
2028
2029 if (OneUse) {
2030 replaceAllDbgUsesWith(*PN, *NewPN, *PN, DT);
2031 }
2032 return replaceInstUsesWith(I, NewPN);
2033}
2034
2036 if (!BO.isAssociative())
2037 return nullptr;
2038
2039 // Find the interleaved binary ops.
2040 auto Opc = BO.getOpcode();
2041 auto *BO0 = dyn_cast<BinaryOperator>(BO.getOperand(0));
2042 auto *BO1 = dyn_cast<BinaryOperator>(BO.getOperand(1));
2043 if (!BO0 || !BO1 || !BO0->hasNUses(2) || !BO1->hasNUses(2) ||
2044 BO0->getOpcode() != Opc || BO1->getOpcode() != Opc ||
2045 !BO0->isAssociative() || !BO1->isAssociative() ||
2046 BO0->getParent() != BO1->getParent())
2047 return nullptr;
2048
2049 assert(BO.isCommutative() && BO0->isCommutative() && BO1->isCommutative() &&
2050 "Expected commutative instructions!");
2051
2052 // Find the matching phis, forming the recurrences.
2053 PHINode *PN0, *PN1;
2054 Value *Start0, *Step0, *Start1, *Step1;
2055 if (!matchSimpleRecurrence(BO0, PN0, Start0, Step0) || !PN0->hasOneUse() ||
2056 !matchSimpleRecurrence(BO1, PN1, Start1, Step1) || !PN1->hasOneUse() ||
2057 PN0->getParent() != PN1->getParent())
2058 return nullptr;
2059
2060 assert(PN0->getNumIncomingValues() == 2 && PN1->getNumIncomingValues() == 2 &&
2061 "Expected PHIs with two incoming values!");
2062
2063 // Convert the start and step values to constants.
2064 auto *Init0 = dyn_cast<Constant>(Start0);
2065 auto *Init1 = dyn_cast<Constant>(Start1);
2066 auto *C0 = dyn_cast<Constant>(Step0);
2067 auto *C1 = dyn_cast<Constant>(Step1);
2068 if (!Init0 || !Init1 || !C0 || !C1)
2069 return nullptr;
2070
2071 // Fold the recurrence constants.
2072 auto *Init = ConstantFoldBinaryInstruction(Opc, Init0, Init1);
2073 auto *C = ConstantFoldBinaryInstruction(Opc, C0, C1);
2074 if (!Init || !C)
2075 return nullptr;
2076
2077 // Create the reduced PHI.
2078 auto *NewPN = PHINode::Create(PN0->getType(), PN0->getNumIncomingValues(),
2079 "reduced.phi");
2080
2081 // Create the new binary op.
2082 auto *NewBO = BinaryOperator::Create(Opc, NewPN, C);
2083 if (Opc == Instruction::FAdd || Opc == Instruction::FMul) {
2084 // Intersect FMF flags for FADD and FMUL.
2085 FastMathFlags Intersect = BO0->getFastMathFlags() &
2086 BO1->getFastMathFlags() & BO.getFastMathFlags();
2087 NewBO->setFastMathFlags(Intersect);
2088 } else {
2089 OverflowTracking Flags;
2090 Flags.AllKnownNonNegative = false;
2091 Flags.AllKnownNonZero = false;
2092 Flags.mergeFlags(*BO0);
2093 Flags.mergeFlags(*BO1);
2094 Flags.mergeFlags(BO);
2095 Flags.applyFlags(*NewBO);
2096 }
2097 NewBO->takeName(&BO);
2098
2099 for (unsigned I = 0, E = PN0->getNumIncomingValues(); I != E; ++I) {
2100 auto *V = PN0->getIncomingValue(I);
2101 auto *BB = PN0->getIncomingBlock(I);
2102 if (V == Init0) {
2103 assert(((PN1->getIncomingValue(0) == Init1 &&
2104 PN1->getIncomingBlock(0) == BB) ||
2105 (PN1->getIncomingValue(1) == Init1 &&
2106 PN1->getIncomingBlock(1) == BB)) &&
2107 "Invalid incoming block!");
2108 NewPN->addIncoming(Init, BB);
2109 } else if (V == BO0) {
2110 assert(((PN1->getIncomingValue(0) == BO1 &&
2111 PN1->getIncomingBlock(0) == BB) ||
2112 (PN1->getIncomingValue(1) == BO1 &&
2113 PN1->getIncomingBlock(1) == BB)) &&
2114 "Invalid incoming block!");
2115 NewPN->addIncoming(NewBO, BB);
2116 } else
2117 llvm_unreachable("Unexpected incoming value!");
2118 }
2119
2120 LLVM_DEBUG(dbgs() << " Combined " << *PN0 << "\n " << *BO0
2121 << "\n with " << *PN1 << "\n " << *BO1
2122 << '\n');
2123
2124 // Insert the new recurrence and remove the old (dead) ones.
2125 InsertNewInstWith(NewPN, PN0->getIterator());
2126 InsertNewInstWith(NewBO, BO0->getIterator());
2127
2134
2135 return replaceInstUsesWith(BO, NewBO);
2136}
2137
2139 // Attempt to fold binary operators whose operands are simple recurrences.
2140 if (auto *NewBO = foldBinopWithRecurrence(BO))
2141 return NewBO;
2142
2143 // TODO: This should be similar to the incoming values check in foldOpIntoPhi:
2144 // we are guarding against replicating the binop in >1 predecessor.
2145 // This could miss matching a phi with 2 constant incoming values.
2146 auto *Phi0 = dyn_cast<PHINode>(BO.getOperand(0));
2147 auto *Phi1 = dyn_cast<PHINode>(BO.getOperand(1));
2148 if (!Phi0 || !Phi1 || !Phi0->hasOneUse() || !Phi1->hasOneUse() ||
2149 Phi0->getNumOperands() != Phi1->getNumOperands())
2150 return nullptr;
2151
2152 // TODO: Remove the restriction for binop being in the same block as the phis.
2153 if (BO.getParent() != Phi0->getParent() ||
2154 BO.getParent() != Phi1->getParent())
2155 return nullptr;
2156
2157 // Fold if there is at least one specific constant value in phi0 or phi1's
2158 // incoming values that comes from the same block and this specific constant
2159 // value can be used to do optimization for specific binary operator.
2160 // For example:
2161 // %phi0 = phi i32 [0, %bb0], [%i, %bb1]
2162 // %phi1 = phi i32 [%j, %bb0], [0, %bb1]
2163 // %add = add i32 %phi0, %phi1
2164 // ==>
2165 // %add = phi i32 [%j, %bb0], [%i, %bb1]
2167 /*AllowRHSConstant*/ false);
2168 if (C) {
2169 SmallVector<Value *, 4> NewIncomingValues;
2170 auto CanFoldIncomingValuePair = [&](std::tuple<Use &, Use &> T) {
2171 auto &Phi0Use = std::get<0>(T);
2172 auto &Phi1Use = std::get<1>(T);
2173 if (Phi0->getIncomingBlock(Phi0Use) != Phi1->getIncomingBlock(Phi1Use))
2174 return false;
2175 Value *Phi0UseV = Phi0Use.get();
2176 Value *Phi1UseV = Phi1Use.get();
2177 if (Phi0UseV == C)
2178 NewIncomingValues.push_back(Phi1UseV);
2179 else if (Phi1UseV == C)
2180 NewIncomingValues.push_back(Phi0UseV);
2181 else
2182 return false;
2183 return true;
2184 };
2185
2186 if (all_of(zip(Phi0->operands(), Phi1->operands()),
2187 CanFoldIncomingValuePair)) {
2188 PHINode *NewPhi =
2189 PHINode::Create(Phi0->getType(), Phi0->getNumOperands());
2190 assert(NewIncomingValues.size() == Phi0->getNumOperands() &&
2191 "The number of collected incoming values should equal the number "
2192 "of the original PHINode operands!");
2193 for (unsigned I = 0; I < Phi0->getNumOperands(); I++)
2194 NewPhi->addIncoming(NewIncomingValues[I], Phi0->getIncomingBlock(I));
2195 return NewPhi;
2196 }
2197 }
2198
2199 if (Phi0->getNumOperands() != 2 || Phi1->getNumOperands() != 2)
2200 return nullptr;
2201
2202 // Match a pair of incoming constants for one of the predecessor blocks.
2203 BasicBlock *ConstBB, *OtherBB;
2204 Constant *C0, *C1;
2205 if (match(Phi0->getIncomingValue(0), m_ImmConstant(C0))) {
2206 ConstBB = Phi0->getIncomingBlock(0);
2207 OtherBB = Phi0->getIncomingBlock(1);
2208 } else if (match(Phi0->getIncomingValue(1), m_ImmConstant(C0))) {
2209 ConstBB = Phi0->getIncomingBlock(1);
2210 OtherBB = Phi0->getIncomingBlock(0);
2211 } else {
2212 return nullptr;
2213 }
2214 if (!match(Phi1->getIncomingValueForBlock(ConstBB), m_ImmConstant(C1)))
2215 return nullptr;
2216
2217 // The block that we are hoisting to must reach here unconditionally.
2218 // Otherwise, we could be speculatively executing an expensive or
2219 // non-speculative op.
2220 auto *PredBlockBranch = dyn_cast<BranchInst>(OtherBB->getTerminator());
2221 if (!PredBlockBranch || PredBlockBranch->isConditional() ||
2222 !DT.isReachableFromEntry(OtherBB))
2223 return nullptr;
2224
2225 // TODO: This check could be tightened to only apply to binops (div/rem) that
2226 // are not safe to speculatively execute. But that could allow hoisting
2227 // potentially expensive instructions (fdiv for example).
2228 for (auto BBIter = BO.getParent()->begin(); &*BBIter != &BO; ++BBIter)
2230 return nullptr;
2231
2232 // Fold constants for the predecessor block with constant incoming values.
2233 Constant *NewC = ConstantFoldBinaryOpOperands(BO.getOpcode(), C0, C1, DL);
2234 if (!NewC)
2235 return nullptr;
2236
2237 // Make a new binop in the predecessor block with the non-constant incoming
2238 // values.
2239 Builder.SetInsertPoint(PredBlockBranch);
2240 Value *NewBO = Builder.CreateBinOp(BO.getOpcode(),
2241 Phi0->getIncomingValueForBlock(OtherBB),
2242 Phi1->getIncomingValueForBlock(OtherBB));
2243 if (auto *NotFoldedNewBO = dyn_cast<BinaryOperator>(NewBO))
2244 NotFoldedNewBO->copyIRFlags(&BO);
2245
2246 // Replace the binop with a phi of the new values. The old phis are dead.
2247 PHINode *NewPhi = PHINode::Create(BO.getType(), 2);
2248 NewPhi->addIncoming(NewBO, OtherBB);
2249 NewPhi->addIncoming(NewC, ConstBB);
2250 return NewPhi;
2251}
2252
2254 if (!isa<Constant>(I.getOperand(1)))
2255 return nullptr;
2256
2257 if (auto *Sel = dyn_cast<SelectInst>(I.getOperand(0))) {
2258 if (Instruction *NewSel = FoldOpIntoSelect(I, Sel))
2259 return NewSel;
2260 } else if (auto *PN = dyn_cast<PHINode>(I.getOperand(0))) {
2261 if (Instruction *NewPhi = foldOpIntoPhi(I, PN))
2262 return NewPhi;
2263 }
2264 return nullptr;
2265}
2266
2268 // If this GEP has only 0 indices, it is the same pointer as
2269 // Src. If Src is not a trivial GEP too, don't combine
2270 // the indices.
2271 if (GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
2272 !Src.hasOneUse())
2273 return false;
2274 return true;
2275}
2276
2277/// Find a constant NewC that has property:
2278/// shuffle(NewC, ShMask) = C
2279/// Returns nullptr if such a constant does not exist e.g. ShMask=<0,0> C=<1,2>
2280///
2281/// A 1-to-1 mapping is not required. Example:
2282/// ShMask = <1,1,2,2> and C = <5,5,6,6> --> NewC = <poison,5,6,poison>
2284 VectorType *NewCTy) {
2285 if (isa<ScalableVectorType>(NewCTy)) {
2286 Constant *Splat = C->getSplatValue();
2287 if (!Splat)
2288 return nullptr;
2290 }
2291
2292 if (cast<FixedVectorType>(NewCTy)->getNumElements() >
2293 cast<FixedVectorType>(C->getType())->getNumElements())
2294 return nullptr;
2295
2296 unsigned NewCNumElts = cast<FixedVectorType>(NewCTy)->getNumElements();
2297 PoisonValue *PoisonScalar = PoisonValue::get(C->getType()->getScalarType());
2298 SmallVector<Constant *, 16> NewVecC(NewCNumElts, PoisonScalar);
2299 unsigned NumElts = cast<FixedVectorType>(C->getType())->getNumElements();
2300 for (unsigned I = 0; I < NumElts; ++I) {
2301 Constant *CElt = C->getAggregateElement(I);
2302 if (ShMask[I] >= 0) {
2303 assert(ShMask[I] < (int)NumElts && "Not expecting narrowing shuffle");
2304 Constant *NewCElt = NewVecC[ShMask[I]];
2305 // Bail out if:
2306 // 1. The constant vector contains a constant expression.
2307 // 2. The shuffle needs an element of the constant vector that can't
2308 // be mapped to a new constant vector.
2309 // 3. This is a widening shuffle that copies elements of V1 into the
2310 // extended elements (extending with poison is allowed).
2311 if (!CElt || (!isa<PoisonValue>(NewCElt) && NewCElt != CElt) ||
2312 I >= NewCNumElts)
2313 return nullptr;
2314 NewVecC[ShMask[I]] = CElt;
2315 }
2316 }
2317 return ConstantVector::get(NewVecC);
2318}
2319
2321 if (!isa<VectorType>(Inst.getType()))
2322 return nullptr;
2323
2324 BinaryOperator::BinaryOps Opcode = Inst.getOpcode();
2325 Value *LHS = Inst.getOperand(0), *RHS = Inst.getOperand(1);
2326 assert(cast<VectorType>(LHS->getType())->getElementCount() ==
2327 cast<VectorType>(Inst.getType())->getElementCount());
2328 assert(cast<VectorType>(RHS->getType())->getElementCount() ==
2329 cast<VectorType>(Inst.getType())->getElementCount());
2330
2331 // If both operands of the binop are vector concatenations, then perform the
2332 // narrow binop on each pair of the source operands followed by concatenation
2333 // of the results.
2334 Value *L0, *L1, *R0, *R1;
2335 ArrayRef<int> Mask;
2336 if (match(LHS, m_Shuffle(m_Value(L0), m_Value(L1), m_Mask(Mask))) &&
2337 match(RHS, m_Shuffle(m_Value(R0), m_Value(R1), m_SpecificMask(Mask))) &&
2338 LHS->hasOneUse() && RHS->hasOneUse() &&
2339 cast<ShuffleVectorInst>(LHS)->isConcat() &&
2340 cast<ShuffleVectorInst>(RHS)->isConcat()) {
2341 // This transform does not have the speculative execution constraint as
2342 // below because the shuffle is a concatenation. The new binops are
2343 // operating on exactly the same elements as the existing binop.
2344 // TODO: We could ease the mask requirement to allow different undef lanes,
2345 // but that requires an analysis of the binop-with-undef output value.
2346 Value *NewBO0 = Builder.CreateBinOp(Opcode, L0, R0);
2347 if (auto *BO = dyn_cast<BinaryOperator>(NewBO0))
2348 BO->copyIRFlags(&Inst);
2349 Value *NewBO1 = Builder.CreateBinOp(Opcode, L1, R1);
2350 if (auto *BO = dyn_cast<BinaryOperator>(NewBO1))
2351 BO->copyIRFlags(&Inst);
2352 return new ShuffleVectorInst(NewBO0, NewBO1, Mask);
2353 }
2354
2355 auto createBinOpReverse = [&](Value *X, Value *Y) {
2356 Value *V = Builder.CreateBinOp(Opcode, X, Y, Inst.getName());
2357 if (auto *BO = dyn_cast<BinaryOperator>(V))
2358 BO->copyIRFlags(&Inst);
2359 Module *M = Inst.getModule();
2361 M, Intrinsic::vector_reverse, V->getType());
2362 return CallInst::Create(F, V);
2363 };
2364
2365 // NOTE: Reverse shuffles don't require the speculative execution protection
2366 // below because they don't affect which lanes take part in the computation.
2367
2368 Value *V1, *V2;
2369 if (match(LHS, m_VecReverse(m_Value(V1)))) {
2370 // Op(rev(V1), rev(V2)) -> rev(Op(V1, V2))
2371 if (match(RHS, m_VecReverse(m_Value(V2))) &&
2372 (LHS->hasOneUse() || RHS->hasOneUse() ||
2373 (LHS == RHS && LHS->hasNUses(2))))
2374 return createBinOpReverse(V1, V2);
2375
2376 // Op(rev(V1), RHSSplat)) -> rev(Op(V1, RHSSplat))
2377 if (LHS->hasOneUse() && isSplatValue(RHS))
2378 return createBinOpReverse(V1, RHS);
2379 }
2380 // Op(LHSSplat, rev(V2)) -> rev(Op(LHSSplat, V2))
2381 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
2382 return createBinOpReverse(LHS, V2);
2383
2384 auto createBinOpVPReverse = [&](Value *X, Value *Y, Value *EVL) {
2385 Value *V = Builder.CreateBinOp(Opcode, X, Y, Inst.getName());
2386 if (auto *BO = dyn_cast<BinaryOperator>(V))
2387 BO->copyIRFlags(&Inst);
2388
2389 ElementCount EC = cast<VectorType>(V->getType())->getElementCount();
2390 Value *AllTrueMask = Builder.CreateVectorSplat(EC, Builder.getTrue());
2391 Module *M = Inst.getModule();
2393 M, Intrinsic::experimental_vp_reverse, V->getType());
2394 return CallInst::Create(F, {V, AllTrueMask, EVL});
2395 };
2396
2397 Value *EVL;
2398 if (match(LHS, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
2399 m_Value(V1), m_AllOnes(), m_Value(EVL)))) {
2400 // Op(rev(V1), rev(V2)) -> rev(Op(V1, V2))
2401 if (match(RHS, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
2402 m_Value(V2), m_AllOnes(), m_Specific(EVL))) &&
2403 (LHS->hasOneUse() || RHS->hasOneUse() ||
2404 (LHS == RHS && LHS->hasNUses(2))))
2405 return createBinOpVPReverse(V1, V2, EVL);
2406
2407 // Op(rev(V1), RHSSplat)) -> rev(Op(V1, RHSSplat))
2408 if (LHS->hasOneUse() && isSplatValue(RHS))
2409 return createBinOpVPReverse(V1, RHS, EVL);
2410 }
2411 // Op(LHSSplat, rev(V2)) -> rev(Op(LHSSplat, V2))
2412 else if (isSplatValue(LHS) &&
2413 match(RHS, m_Intrinsic<Intrinsic::experimental_vp_reverse>(
2414 m_Value(V2), m_AllOnes(), m_Value(EVL))))
2415 return createBinOpVPReverse(LHS, V2, EVL);
2416
2417 // It may not be safe to reorder shuffles and things like div, urem, etc.
2418 // because we may trap when executing those ops on unknown vector elements.
2419 // See PR20059.
2421 return nullptr;
2422
2423 auto createBinOpShuffle = [&](Value *X, Value *Y, ArrayRef<int> M) {
2424 Value *XY = Builder.CreateBinOp(Opcode, X, Y);
2425 if (auto *BO = dyn_cast<BinaryOperator>(XY))
2426 BO->copyIRFlags(&Inst);
2427 return new ShuffleVectorInst(XY, M);
2428 };
2429
2430 // If both arguments of the binary operation are shuffles that use the same
2431 // mask and shuffle within a single vector, move the shuffle after the binop.
2432 if (match(LHS, m_Shuffle(m_Value(V1), m_Poison(), m_Mask(Mask))) &&
2433 match(RHS, m_Shuffle(m_Value(V2), m_Poison(), m_SpecificMask(Mask))) &&
2434 V1->getType() == V2->getType() &&
2435 (LHS->hasOneUse() || RHS->hasOneUse() || LHS == RHS)) {
2436 // Op(shuffle(V1, Mask), shuffle(V2, Mask)) -> shuffle(Op(V1, V2), Mask)
2437 return createBinOpShuffle(V1, V2, Mask);
2438 }
2439
2440 // If both arguments of a commutative binop are select-shuffles that use the
2441 // same mask with commuted operands, the shuffles are unnecessary.
2442 if (Inst.isCommutative() &&
2443 match(LHS, m_Shuffle(m_Value(V1), m_Value(V2), m_Mask(Mask))) &&
2444 match(RHS,
2445 m_Shuffle(m_Specific(V2), m_Specific(V1), m_SpecificMask(Mask)))) {
2446 auto *LShuf = cast<ShuffleVectorInst>(LHS);
2447 auto *RShuf = cast<ShuffleVectorInst>(RHS);
2448 // TODO: Allow shuffles that contain undefs in the mask?
2449 // That is legal, but it reduces undef knowledge.
2450 // TODO: Allow arbitrary shuffles by shuffling after binop?
2451 // That might be legal, but we have to deal with poison.
2452 if (LShuf->isSelect() &&
2453 !is_contained(LShuf->getShuffleMask(), PoisonMaskElem) &&
2454 RShuf->isSelect() &&
2455 !is_contained(RShuf->getShuffleMask(), PoisonMaskElem)) {
2456 // Example:
2457 // LHS = shuffle V1, V2, <0, 5, 6, 3>
2458 // RHS = shuffle V2, V1, <0, 5, 6, 3>
2459 // LHS + RHS --> (V10+V20, V21+V11, V22+V12, V13+V23) --> V1 + V2
2460 Instruction *NewBO = BinaryOperator::Create(Opcode, V1, V2);
2461 NewBO->copyIRFlags(&Inst);
2462 return NewBO;
2463 }
2464 }
2465
2466 // If one argument is a shuffle within one vector and the other is a constant,
2467 // try moving the shuffle after the binary operation. This canonicalization
2468 // intends to move shuffles closer to other shuffles and binops closer to
2469 // other binops, so they can be folded. It may also enable demanded elements
2470 // transforms.
2471 Constant *C;
2473 m_Mask(Mask))),
2474 m_ImmConstant(C)))) {
2475 assert(Inst.getType()->getScalarType() == V1->getType()->getScalarType() &&
2476 "Shuffle should not change scalar type");
2477
2478 bool ConstOp1 = isa<Constant>(RHS);
2479 if (Constant *NewC =
2480 unshuffleConstant(Mask, C, cast<VectorType>(V1->getType()))) {
2481 // For fixed vectors, lanes of NewC not used by the shuffle will be poison
2482 // which will cause UB for div/rem. Mask them with a safe constant.
2483 if (isa<FixedVectorType>(V1->getType()) && Inst.isIntDivRem())
2484 NewC = getSafeVectorConstantForBinop(Opcode, NewC, ConstOp1);
2485
2486 // Op(shuffle(V1, Mask), C) -> shuffle(Op(V1, NewC), Mask)
2487 // Op(C, shuffle(V1, Mask)) -> shuffle(Op(NewC, V1), Mask)
2488 Value *NewLHS = ConstOp1 ? V1 : NewC;
2489 Value *NewRHS = ConstOp1 ? NewC : V1;
2490 return createBinOpShuffle(NewLHS, NewRHS, Mask);
2491 }
2492 }
2493
2494 // Try to reassociate to sink a splat shuffle after a binary operation.
2495 if (Inst.isAssociative() && Inst.isCommutative()) {
2496 // Canonicalize shuffle operand as LHS.
2497 if (isa<ShuffleVectorInst>(RHS))
2498 std::swap(LHS, RHS);
2499
2500 Value *X;
2501 ArrayRef<int> MaskC;
2502 int SplatIndex;
2503 Value *Y, *OtherOp;
2504 if (!match(LHS,
2505 m_OneUse(m_Shuffle(m_Value(X), m_Undef(), m_Mask(MaskC)))) ||
2506 !match(MaskC, m_SplatOrPoisonMask(SplatIndex)) ||
2507 X->getType() != Inst.getType() ||
2508 !match(RHS, m_OneUse(m_BinOp(Opcode, m_Value(Y), m_Value(OtherOp)))))
2509 return nullptr;
2510
2511 // FIXME: This may not be safe if the analysis allows undef elements. By
2512 // moving 'Y' before the splat shuffle, we are implicitly assuming
2513 // that it is not undef/poison at the splat index.
2514 if (isSplatValue(OtherOp, SplatIndex)) {
2515 std::swap(Y, OtherOp);
2516 } else if (!isSplatValue(Y, SplatIndex)) {
2517 return nullptr;
2518 }
2519
2520 // X and Y are splatted values, so perform the binary operation on those
2521 // values followed by a splat followed by the 2nd binary operation:
2522 // bo (splat X), (bo Y, OtherOp) --> bo (splat (bo X, Y)), OtherOp
2523 Value *NewBO = Builder.CreateBinOp(Opcode, X, Y);
2524 SmallVector<int, 8> NewMask(MaskC.size(), SplatIndex);
2525 Value *NewSplat = Builder.CreateShuffleVector(NewBO, NewMask);
2526 Instruction *R = BinaryOperator::Create(Opcode, NewSplat, OtherOp);
2527
2528 // Intersect FMF on both new binops. Other (poison-generating) flags are
2529 // dropped to be safe.
2530 if (isa<FPMathOperator>(R)) {
2531 R->copyFastMathFlags(&Inst);
2532 R->andIRFlags(RHS);
2533 }
2534 if (auto *NewInstBO = dyn_cast<BinaryOperator>(NewBO))
2535 NewInstBO->copyIRFlags(R);
2536 return R;
2537 }
2538
2539 return nullptr;
2540}
2541
2542/// Try to narrow the width of a binop if at least 1 operand is an extend of
2543/// of a value. This requires a potentially expensive known bits check to make
2544/// sure the narrow op does not overflow.
2545Instruction *InstCombinerImpl::narrowMathIfNoOverflow(BinaryOperator &BO) {
2546 // We need at least one extended operand.
2547 Value *Op0 = BO.getOperand(0), *Op1 = BO.getOperand(1);
2548
2549 // If this is a sub, we swap the operands since we always want an extension
2550 // on the RHS. The LHS can be an extension or a constant.
2551 if (BO.getOpcode() == Instruction::Sub)
2552 std::swap(Op0, Op1);
2553
2554 Value *X;
2555 bool IsSext = match(Op0, m_SExt(m_Value(X)));
2556 if (!IsSext && !match(Op0, m_ZExt(m_Value(X))))
2557 return nullptr;
2558
2559 // If both operands are the same extension from the same source type and we
2560 // can eliminate at least one (hasOneUse), this might work.
2561 CastInst::CastOps CastOpc = IsSext ? Instruction::SExt : Instruction::ZExt;
2562 Value *Y;
2563 if (!(match(Op1, m_ZExtOrSExt(m_Value(Y))) && X->getType() == Y->getType() &&
2564 cast<Operator>(Op1)->getOpcode() == CastOpc &&
2565 (Op0->hasOneUse() || Op1->hasOneUse()))) {
2566 // If that did not match, see if we have a suitable constant operand.
2567 // Truncating and extending must produce the same constant.
2568 Constant *WideC;
2569 if (!Op0->hasOneUse() || !match(Op1, m_Constant(WideC)))
2570 return nullptr;
2571 Constant *NarrowC = getLosslessTrunc(WideC, X->getType(), CastOpc);
2572 if (!NarrowC)
2573 return nullptr;
2574 Y = NarrowC;
2575 }
2576
2577 // Swap back now that we found our operands.
2578 if (BO.getOpcode() == Instruction::Sub)
2579 std::swap(X, Y);
2580
2581 // Both operands have narrow versions. Last step: the math must not overflow
2582 // in the narrow width.
2583 if (!willNotOverflow(BO.getOpcode(), X, Y, BO, IsSext))
2584 return nullptr;
2585
2586 // bo (ext X), (ext Y) --> ext (bo X, Y)
2587 // bo (ext X), C --> ext (bo X, C')
2588 Value *NarrowBO = Builder.CreateBinOp(BO.getOpcode(), X, Y, "narrow");
2589 if (auto *NewBinOp = dyn_cast<BinaryOperator>(NarrowBO)) {
2590 if (IsSext)
2591 NewBinOp->setHasNoSignedWrap();
2592 else
2593 NewBinOp->setHasNoUnsignedWrap();
2594 }
2595 return CastInst::Create(CastOpc, NarrowBO, BO.getType());
2596}
2597
2598/// Determine nowrap flags for (gep (gep p, x), y) to (gep p, (x + y))
2599/// transform.
2601 GEPOperator &GEP2) {
2603}
2604
2605/// Thread a GEP operation with constant indices through the constant true/false
2606/// arms of a select.
2608 InstCombiner::BuilderTy &Builder) {
2609 if (!GEP.hasAllConstantIndices())
2610 return nullptr;
2611
2612 Instruction *Sel;
2613 Value *Cond;
2614 Constant *TrueC, *FalseC;
2615 if (!match(GEP.getPointerOperand(), m_Instruction(Sel)) ||
2616 !match(Sel,
2617 m_Select(m_Value(Cond), m_Constant(TrueC), m_Constant(FalseC))))
2618 return nullptr;
2619
2620 // gep (select Cond, TrueC, FalseC), IndexC --> select Cond, TrueC', FalseC'
2621 // Propagate 'inbounds' and metadata from existing instructions.
2622 // Note: using IRBuilder to create the constants for efficiency.
2623 SmallVector<Value *, 4> IndexC(GEP.indices());
2624 GEPNoWrapFlags NW = GEP.getNoWrapFlags();
2625 Type *Ty = GEP.getSourceElementType();
2626 Value *NewTrueC = Builder.CreateGEP(Ty, TrueC, IndexC, "", NW);
2627 Value *NewFalseC = Builder.CreateGEP(Ty, FalseC, IndexC, "", NW);
2628 return SelectInst::Create(Cond, NewTrueC, NewFalseC, "", nullptr, Sel);
2629}
2630
2631// Canonicalization:
2632// gep T, (gep i8, base, C1), (Index + C2) into
2633// gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index
2635 GEPOperator *Src,
2636 InstCombinerImpl &IC) {
2637 if (GEP.getNumIndices() != 1)
2638 return nullptr;
2639 auto &DL = IC.getDataLayout();
2640 Value *Base;
2641 const APInt *C1;
2642 if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2643 return nullptr;
2644 Value *VarIndex;
2645 const APInt *C2;
2646 Type *PtrTy = Src->getType()->getScalarType();
2647 unsigned IndexSizeInBits = DL.getIndexTypeSizeInBits(PtrTy);
2648 if (!match(GEP.getOperand(1), m_AddLike(m_Value(VarIndex), m_APInt(C2))))
2649 return nullptr;
2650 if (C1->getBitWidth() != IndexSizeInBits ||
2651 C2->getBitWidth() != IndexSizeInBits)
2652 return nullptr;
2653 Type *BaseType = GEP.getSourceElementType();
2654 if (isa<ScalableVectorType>(BaseType))
2655 return nullptr;
2656 APInt TypeSize(IndexSizeInBits, DL.getTypeAllocSize(BaseType));
2657 APInt NewOffset = TypeSize * *C2 + *C1;
2658 if (NewOffset.isZero() ||
2659 (Src->hasOneUse() && GEP.getOperand(1)->hasOneUse())) {
2661 if (GEP.hasNoUnsignedWrap() &&
2662 cast<GEPOperator>(Src)->hasNoUnsignedWrap() &&
2663 match(GEP.getOperand(1), m_NUWAddLike(m_Value(), m_Value()))) {
2665 if (GEP.isInBounds() && cast<GEPOperator>(Src)->isInBounds())
2666 Flags |= GEPNoWrapFlags::inBounds();
2667 }
2668
2669 Value *GEPConst =
2670 IC.Builder.CreatePtrAdd(Base, IC.Builder.getInt(NewOffset), "", Flags);
2671 return GetElementPtrInst::Create(BaseType, GEPConst, VarIndex, Flags);
2672 }
2673
2674 return nullptr;
2675}
2676
2678 GEPOperator *Src) {
2679 // Combine Indices - If the source pointer to this getelementptr instruction
2680 // is a getelementptr instruction with matching element type, combine the
2681 // indices of the two getelementptr instructions into a single instruction.
2682 if (!shouldMergeGEPs(*cast<GEPOperator>(&GEP), *Src))
2683 return nullptr;
2684
2685 if (auto *I = canonicalizeGEPOfConstGEPI8(GEP, Src, *this))
2686 return I;
2687
2688 // For constant GEPs, use a more general offset-based folding approach.
2689 Type *PtrTy = Src->getType()->getScalarType();
2690 if (GEP.hasAllConstantIndices() &&
2691 (Src->hasOneUse() || Src->hasAllConstantIndices())) {
2692 // Split Src into a variable part and a constant suffix.
2694 Type *BaseType = GTI.getIndexedType();
2695 bool IsFirstType = true;
2696 unsigned NumVarIndices = 0;
2697 for (auto Pair : enumerate(Src->indices())) {
2698 if (!isa<ConstantInt>(Pair.value())) {
2699 BaseType = GTI.getIndexedType();
2700 IsFirstType = false;
2701 NumVarIndices = Pair.index() + 1;
2702 }
2703 ++GTI;
2704 }
2705
2706 // Determine the offset for the constant suffix of Src.
2708 if (NumVarIndices != Src->getNumIndices()) {
2709 // FIXME: getIndexedOffsetInType() does not handled scalable vectors.
2710 if (BaseType->isScalableTy())
2711 return nullptr;
2712
2713 SmallVector<Value *> ConstantIndices;
2714 if (!IsFirstType)
2715 ConstantIndices.push_back(
2717 append_range(ConstantIndices, drop_begin(Src->indices(), NumVarIndices));
2718 Offset += DL.getIndexedOffsetInType(BaseType, ConstantIndices);
2719 }
2720
2721 // Add the offset for GEP (which is fully constant).
2722 if (!GEP.accumulateConstantOffset(DL, Offset))
2723 return nullptr;
2724
2725 // Convert the total offset back into indices.
2726 SmallVector<APInt> ConstIndices =
2728 if (!Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero()))
2729 return nullptr;
2730
2731 GEPNoWrapFlags NW = getMergedGEPNoWrapFlags(*Src, *cast<GEPOperator>(&GEP));
2732 SmallVector<Value *> Indices(
2733 drop_end(Src->indices(), Src->getNumIndices() - NumVarIndices));
2734 for (const APInt &Idx : drop_begin(ConstIndices, !IsFirstType)) {
2735 Indices.push_back(ConstantInt::get(GEP.getContext(), Idx));
2736 // Even if the total offset is inbounds, we may end up representing it
2737 // by first performing a larger negative offset, and then a smaller
2738 // positive one. The large negative offset might go out of bounds. Only
2739 // preserve inbounds if all signs are the same.
2740 if (Idx.isNonNegative() != ConstIndices[0].isNonNegative())
2742 if (!Idx.isNonNegative())
2743 NW = NW.withoutNoUnsignedWrap();
2744 }
2745
2746 return replaceInstUsesWith(
2747 GEP, Builder.CreateGEP(Src->getSourceElementType(), Src->getOperand(0),
2748 Indices, "", NW));
2749 }
2750
2751 if (Src->getResultElementType() != GEP.getSourceElementType())
2752 return nullptr;
2753
2754 SmallVector<Value*, 8> Indices;
2755
2756 // Find out whether the last index in the source GEP is a sequential idx.
2757 bool EndsWithSequential = false;
2758 for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
2759 I != E; ++I)
2760 EndsWithSequential = I.isSequential();
2761
2762 // Can we combine the two pointer arithmetics offsets?
2763 if (EndsWithSequential) {
2764 // Replace: gep (gep %P, long B), long A, ...
2765 // With: T = long A+B; gep %P, T, ...
2766 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
2767 Value *GO1 = GEP.getOperand(1);
2768
2769 // If they aren't the same type, then the input hasn't been processed
2770 // by the loop above yet (which canonicalizes sequential index types to
2771 // intptr_t). Just avoid transforming this until the input has been
2772 // normalized.
2773 if (SO1->getType() != GO1->getType())
2774 return nullptr;
2775
2776 Value *Sum =
2777 simplifyAddInst(GO1, SO1, false, false, SQ.getWithInstruction(&GEP));
2778 // Only do the combine when we are sure the cost after the
2779 // merge is never more than that before the merge.
2780 if (Sum == nullptr)
2781 return nullptr;
2782
2783 Indices.append(Src->op_begin()+1, Src->op_end()-1);
2784 Indices.push_back(Sum);
2785 Indices.append(GEP.op_begin()+2, GEP.op_end());
2786 } else if (isa<Constant>(*GEP.idx_begin()) &&
2787 cast<Constant>(*GEP.idx_begin())->isNullValue() &&
2788 Src->getNumOperands() != 1) {
2789 // Otherwise we can do the fold if the first index of the GEP is a zero
2790 Indices.append(Src->op_begin()+1, Src->op_end());
2791 Indices.append(GEP.idx_begin()+1, GEP.idx_end());
2792 }
2793
2794 // Don't create GEPs with more than one variable index.
2795 unsigned NumVarIndices =
2796 count_if(Indices, [](Value *Idx) { return !isa<Constant>(Idx); });
2797 if (NumVarIndices > 1)
2798 return nullptr;
2799
2800 if (!Indices.empty())
2801 return replaceInstUsesWith(
2803 Src->getSourceElementType(), Src->getOperand(0), Indices, "",
2804 getMergedGEPNoWrapFlags(*Src, *cast<GEPOperator>(&GEP))));
2805
2806 return nullptr;
2807}
2808
2810 BuilderTy *Builder,
2811 bool &DoesConsume, unsigned Depth) {
2812 static Value *const NonNull = reinterpret_cast<Value *>(uintptr_t(1));
2813 // ~(~(X)) -> X.
2814 Value *A, *B;
2815 if (match(V, m_Not(m_Value(A)))) {
2816 DoesConsume = true;
2817 return A;
2818 }
2819
2820 Constant *C;
2821 // Constants can be considered to be not'ed values.
2822 if (match(V, m_ImmConstant(C)))
2823 return ConstantExpr::getNot(C);
2824
2826 return nullptr;
2827
2828 // The rest of the cases require that we invert all uses so don't bother
2829 // doing the analysis if we know we can't use the result.
2830 if (!WillInvertAllUses)
2831 return nullptr;
2832
2833 // Compares can be inverted if all of their uses are being modified to use
2834 // the ~V.
2835 if (auto *I = dyn_cast<CmpInst>(V)) {
2836 if (Builder != nullptr)
2837 return Builder->CreateCmp(I->getInversePredicate(), I->getOperand(0),
2838 I->getOperand(1));
2839 return NonNull;
2840 }
2841
2842 // If `V` is of the form `A + B` then `-1 - V` can be folded into
2843 // `(-1 - B) - A` if we are willing to invert all of the uses.
2844 if (match(V, m_Add(m_Value(A), m_Value(B)))) {
2845 if (auto *BV = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2846 DoesConsume, Depth))
2847 return Builder ? Builder->CreateSub(BV, A) : NonNull;
2848 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2849 DoesConsume, Depth))
2850 return Builder ? Builder->CreateSub(AV, B) : NonNull;
2851 return nullptr;
2852 }
2853
2854 // If `V` is of the form `A ^ ~B` then `~(A ^ ~B)` can be folded
2855 // into `A ^ B` if we are willing to invert all of the uses.
2856 if (match(V, m_Xor(m_Value(A), m_Value(B)))) {
2857 if (auto *BV = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2858 DoesConsume, Depth))
2859 return Builder ? Builder->CreateXor(A, BV) : NonNull;
2860 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2861 DoesConsume, Depth))
2862 return Builder ? Builder->CreateXor(AV, B) : NonNull;
2863 return nullptr;
2864 }
2865
2866 // If `V` is of the form `B - A` then `-1 - V` can be folded into
2867 // `A + (-1 - B)` if we are willing to invert all of the uses.
2868 if (match(V, m_Sub(m_Value(A), m_Value(B)))) {
2869 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2870 DoesConsume, Depth))
2871 return Builder ? Builder->CreateAdd(AV, B) : NonNull;
2872 return nullptr;
2873 }
2874
2875 // If `V` is of the form `(~A) s>> B` then `~((~A) s>> B)` can be folded
2876 // into `A s>> B` if we are willing to invert all of the uses.
2877 if (match(V, m_AShr(m_Value(A), m_Value(B)))) {
2878 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2879 DoesConsume, Depth))
2880 return Builder ? Builder->CreateAShr(AV, B) : NonNull;
2881 return nullptr;
2882 }
2883
2884 Value *Cond;
2885 // LogicOps are special in that we canonicalize them at the cost of an
2886 // instruction.
2887 bool IsSelect = match(V, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
2888 !shouldAvoidAbsorbingNotIntoSelect(*cast<SelectInst>(V));
2889 // Selects/min/max with invertible operands are freely invertible
2890 if (IsSelect || match(V, m_MaxOrMin(m_Value(A), m_Value(B)))) {
2891 bool LocalDoesConsume = DoesConsume;
2892 if (!getFreelyInvertedImpl(B, B->hasOneUse(), /*Builder*/ nullptr,
2893 LocalDoesConsume, Depth))
2894 return nullptr;
2895 if (Value *NotA = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2896 LocalDoesConsume, Depth)) {
2897 DoesConsume = LocalDoesConsume;
2898 if (Builder != nullptr) {
2899 Value *NotB = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2900 DoesConsume, Depth);
2901 assert(NotB != nullptr &&
2902 "Unable to build inverted value for known freely invertable op");
2903 if (auto *II = dyn_cast<IntrinsicInst>(V))
2905 getInverseMinMaxIntrinsic(II->getIntrinsicID()), NotA, NotB);
2906 return Builder->CreateSelect(Cond, NotA, NotB);
2907 }
2908 return NonNull;
2909 }
2910 }
2911
2912 if (PHINode *PN = dyn_cast<PHINode>(V)) {
2913 bool LocalDoesConsume = DoesConsume;
2915 for (Use &U : PN->operands()) {
2916 BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
2917 Value *NewIncomingVal = getFreelyInvertedImpl(
2918 U.get(), /*WillInvertAllUses=*/false,
2919 /*Builder=*/nullptr, LocalDoesConsume, MaxAnalysisRecursionDepth - 1);
2920 if (NewIncomingVal == nullptr)
2921 return nullptr;
2922 // Make sure that we can safely erase the original PHI node.
2923 if (NewIncomingVal == V)
2924 return nullptr;
2925 if (Builder != nullptr)
2926 IncomingValues.emplace_back(NewIncomingVal, IncomingBlock);
2927 }
2928
2929 DoesConsume = LocalDoesConsume;
2930 if (Builder != nullptr) {
2933 PHINode *NewPN =
2934 Builder->CreatePHI(PN->getType(), PN->getNumIncomingValues());
2935 for (auto [Val, Pred] : IncomingValues)
2936 NewPN->addIncoming(Val, Pred);
2937 return NewPN;
2938 }
2939 return NonNull;
2940 }
2941
2942 if (match(V, m_SExtLike(m_Value(A)))) {
2943 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2944 DoesConsume, Depth))
2945 return Builder ? Builder->CreateSExt(AV, V->getType()) : NonNull;
2946 return nullptr;
2947 }
2948
2949 if (match(V, m_Trunc(m_Value(A)))) {
2950 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2951 DoesConsume, Depth))
2952 return Builder ? Builder->CreateTrunc(AV, V->getType()) : NonNull;
2953 return nullptr;
2954 }
2955
2956 // De Morgan's Laws:
2957 // (~(A | B)) -> (~A & ~B)
2958 // (~(A & B)) -> (~A | ~B)
2959 auto TryInvertAndOrUsingDeMorgan = [&](Instruction::BinaryOps Opcode,
2960 bool IsLogical, Value *A,
2961 Value *B) -> Value * {
2962 bool LocalDoesConsume = DoesConsume;
2963 if (!getFreelyInvertedImpl(B, B->hasOneUse(), /*Builder=*/nullptr,
2964 LocalDoesConsume, Depth))
2965 return nullptr;
2966 if (auto *NotA = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2967 LocalDoesConsume, Depth)) {
2968 auto *NotB = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2969 LocalDoesConsume, Depth);
2970 DoesConsume = LocalDoesConsume;
2971 if (IsLogical)
2972 return Builder ? Builder->CreateLogicalOp(Opcode, NotA, NotB) : NonNull;
2973 return Builder ? Builder->CreateBinOp(Opcode, NotA, NotB) : NonNull;
2974 }
2975
2976 return nullptr;
2977 };
2978
2979 if (match(V, m_Or(m_Value(A), m_Value(B))))
2980 return TryInvertAndOrUsingDeMorgan(Instruction::And, /*IsLogical=*/false, A,
2981 B);
2982
2983 if (match(V, m_And(m_Value(A), m_Value(B))))
2984 return TryInvertAndOrUsingDeMorgan(Instruction::Or, /*IsLogical=*/false, A,
2985 B);
2986
2987 if (match(V, m_LogicalOr(m_Value(A), m_Value(B))))
2988 return TryInvertAndOrUsingDeMorgan(Instruction::And, /*IsLogical=*/true, A,
2989 B);
2990
2991 if (match(V, m_LogicalAnd(m_Value(A), m_Value(B))))
2992 return TryInvertAndOrUsingDeMorgan(Instruction::Or, /*IsLogical=*/true, A,
2993 B);
2994
2995 return nullptr;
2996}
2997
2998/// Return true if we should canonicalize the gep to an i8 ptradd.
3000 Value *PtrOp = GEP.getOperand(0);
3001 Type *GEPEltType = GEP.getSourceElementType();
3002 if (GEPEltType->isIntegerTy(8))
3003 return false;
3004
3005 // Canonicalize scalable GEPs to an explicit offset using the llvm.vscale
3006 // intrinsic. This has better support in BasicAA.
3007 if (GEPEltType->isScalableTy())
3008 return true;
3009
3010 // gep i32 p, mul(O, C) -> gep i8, p, mul(O, C*4) to fold the two multiplies
3011 // together.
3012 if (GEP.getNumIndices() == 1 &&
3013 match(GEP.getOperand(1),
3015 m_Shl(m_Value(), m_ConstantInt())))))
3016 return true;
3017
3018 // gep (gep %p, C1), %x, C2 is expanded so the two constants can
3019 // possibly be merged together.
3020 auto PtrOpGep = dyn_cast<GEPOperator>(PtrOp);
3021 return PtrOpGep && PtrOpGep->hasAllConstantIndices() &&
3022 any_of(GEP.indices(), [](Value *V) {
3023 const APInt *C;
3024 return match(V, m_APInt(C)) && !C->isZero();
3025 });
3026}
3027
3029 IRBuilderBase &Builder) {
3030 auto *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));
3031 if (!Op1)
3032 return nullptr;
3033
3034 // Don't fold a GEP into itself through a PHI node. This can only happen
3035 // through the back-edge of a loop. Folding a GEP into itself means that
3036 // the value of the previous iteration needs to be stored in the meantime,
3037 // thus requiring an additional register variable to be live, but not
3038 // actually achieving anything (the GEP still needs to be executed once per
3039 // loop iteration).
3040 if (Op1 == &GEP)
3041 return nullptr;
3042 GEPNoWrapFlags NW = Op1->getNoWrapFlags();
3043
3044 int DI = -1;
3045
3046 for (auto I = PN->op_begin()+1, E = PN->op_end(); I !=E; ++I) {
3047 auto *Op2 = dyn_cast<GetElementPtrInst>(*I);
3048 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands() ||
3049 Op1->getSourceElementType() != Op2->getSourceElementType())
3050 return nullptr;
3051
3052 // As for Op1 above, don't try to fold a GEP into itself.
3053 if (Op2 == &GEP)
3054 return nullptr;
3055
3056 // Keep track of the type as we walk the GEP.
3057 Type *CurTy = nullptr;
3058
3059 for (unsigned J = 0, F = Op1->getNumOperands(); J != F; ++J) {
3060 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
3061 return nullptr;
3062
3063 if (Op1->getOperand(J) != Op2->getOperand(J)) {
3064 if (DI == -1) {
3065 // We have not seen any differences yet in the GEPs feeding the
3066 // PHI yet, so we record this one if it is allowed to be a
3067 // variable.
3068
3069 // The first two arguments can vary for any GEP, the rest have to be
3070 // static for struct slots
3071 if (J > 1) {
3072 assert(CurTy && "No current type?");
3073 if (CurTy->isStructTy())
3074 return nullptr;
3075 }
3076
3077 DI = J;
3078 } else {
3079 // The GEP is different by more than one input. While this could be
3080 // extended to support GEPs that vary by more than one variable it
3081 // doesn't make sense since it greatly increases the complexity and
3082 // would result in an R+R+R addressing mode which no backend
3083 // directly supports and would need to be broken into several
3084 // simpler instructions anyway.
3085 return nullptr;
3086 }
3087 }
3088
3089 // Sink down a layer of the type for the next iteration.
3090 if (J > 0) {
3091 if (J == 1) {
3092 CurTy = Op1->getSourceElementType();
3093 } else {
3094 CurTy =
3095 GetElementPtrInst::getTypeAtIndex(CurTy, Op1->getOperand(J));
3096 }
3097 }
3098 }
3099
3100 NW &= Op2->getNoWrapFlags();
3101 }
3102
3103 // If not all GEPs are identical we'll have to create a new PHI node.
3104 // Check that the old PHI node has only one use so that it will get
3105 // removed.
3106 if (DI != -1 && !PN->hasOneUse())
3107 return nullptr;
3108
3109 auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
3110 NewGEP->setNoWrapFlags(NW);
3111
3112 if (DI == -1) {
3113 // All the GEPs feeding the PHI are identical. Clone one down into our
3114 // BB so that it can be merged with the current GEP.
3115 } else {
3116 // All the GEPs feeding the PHI differ at a single offset. Clone a GEP
3117 // into the current block so it can be merged, and create a new PHI to
3118 // set that index.
3119 PHINode *NewPN;
3120 {
3121 IRBuilderBase::InsertPointGuard Guard(Builder);
3122 Builder.SetInsertPoint(PN);
3123 NewPN = Builder.CreatePHI(Op1->getOperand(DI)->getType(),
3124 PN->getNumOperands());
3125 }
3126
3127 for (auto &I : PN->operands())
3128 NewPN->addIncoming(cast<GEPOperator>(I)->getOperand(DI),
3129 PN->getIncomingBlock(I));
3130
3131 NewGEP->setOperand(DI, NewPN);
3132 }
3133
3134 NewGEP->insertBefore(*GEP.getParent(), GEP.getParent()->getFirstInsertionPt());
3135 return NewGEP;
3136}
3137
3139 Value *PtrOp = GEP.getOperand(0);
3140 SmallVector<Value *, 8> Indices(GEP.indices());
3141 Type *GEPType = GEP.getType();
3142 Type *GEPEltType = GEP.getSourceElementType();
3143 if (Value *V =
3144 simplifyGEPInst(GEPEltType, PtrOp, Indices, GEP.getNoWrapFlags(),
3146 return replaceInstUsesWith(GEP, V);
3147
3148 // For vector geps, use the generic demanded vector support.
3149 // Skip if GEP return type is scalable. The number of elements is unknown at
3150 // compile-time.
3151 if (auto *GEPFVTy = dyn_cast<FixedVectorType>(GEPType)) {
3152 auto VWidth = GEPFVTy->getNumElements();
3153 APInt PoisonElts(VWidth, 0);
3154 APInt AllOnesEltMask(APInt::getAllOnes(VWidth));
3155 if (Value *V = SimplifyDemandedVectorElts(&GEP, AllOnesEltMask,
3156 PoisonElts)) {
3157 if (V != &GEP)
3158 return replaceInstUsesWith(GEP, V);
3159 return &GEP;
3160 }
3161 }
3162
3163 // Eliminate unneeded casts for indices, and replace indices which displace
3164 // by multiples of a zero size type with zero.
3165 bool MadeChange = false;
3166
3167 // Index width may not be the same width as pointer width.
3168 // Data layout chooses the right type based on supported integer types.
3169 Type *NewScalarIndexTy =
3170 DL.getIndexType(GEP.getPointerOperandType()->getScalarType());
3171
3173 for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end(); I != E;
3174 ++I, ++GTI) {
3175 // Skip indices into struct types.
3176 if (GTI.isStruct())
3177 continue;
3178
3179 Type *IndexTy = (*I)->getType();
3180 Type *NewIndexType =
3181 IndexTy->isVectorTy()
3182 ? VectorType::get(NewScalarIndexTy,
3183 cast<VectorType>(IndexTy)->getElementCount())
3184 : NewScalarIndexTy;
3185
3186 // If the element type has zero size then any index over it is equivalent
3187 // to an index of zero, so replace it with zero if it is not zero already.
3188 Type *EltTy = GTI.getIndexedType();
3189 if (EltTy->isSized() && DL.getTypeAllocSize(EltTy).isZero())
3190 if (!isa<Constant>(*I) || !match(I->get(), m_Zero())) {
3191 *I = Constant::getNullValue(NewIndexType);
3192 MadeChange = true;
3193 }
3194
3195 if (IndexTy != NewIndexType) {
3196 // If we are using a wider index than needed for this platform, shrink
3197 // it to what we need. If narrower, sign-extend it to what we need.
3198 // This explicit cast can make subsequent optimizations more obvious.
3199 if (IndexTy->getScalarSizeInBits() <
3200 NewIndexType->getScalarSizeInBits()) {
3201 if (GEP.hasNoUnsignedWrap() && GEP.hasNoUnsignedSignedWrap())
3202 *I = Builder.CreateZExt(*I, NewIndexType, "", /*IsNonNeg=*/true);
3203 else
3204 *I = Builder.CreateSExt(*I, NewIndexType);
3205 } else {
3206 *I = Builder.CreateTrunc(*I, NewIndexType, "", GEP.hasNoUnsignedWrap(),
3207 GEP.hasNoUnsignedSignedWrap());
3208 }
3209 MadeChange = true;
3210 }
3211 }
3212 if (MadeChange)
3213 return &GEP;
3214
3215 // Canonicalize constant GEPs to i8 type.
3216 if (!GEPEltType->isIntegerTy(8) && GEP.hasAllConstantIndices()) {
3218 if (GEP.accumulateConstantOffset(DL, Offset))
3219 return replaceInstUsesWith(
3221 GEP.getNoWrapFlags()));
3222 }
3223
3225 Value *Offset = EmitGEPOffset(cast<GEPOperator>(&GEP));
3226 Value *NewGEP =
3227 Builder.CreatePtrAdd(PtrOp, Offset, "", GEP.getNoWrapFlags());
3228 return replaceInstUsesWith(GEP, NewGEP);
3229 }
3230
3231 // Strip trailing zero indices.
3232 auto *LastIdx = dyn_cast<Constant>(Indices.back());
3233 if (LastIdx && LastIdx->isNullValue() && !LastIdx->getType()->isVectorTy()) {
3234 return replaceInstUsesWith(
3235 GEP, Builder.CreateGEP(GEP.getSourceElementType(), PtrOp,
3236 drop_end(Indices), "", GEP.getNoWrapFlags()));
3237 }
3238
3239 // Strip leading zero indices.
3240 auto *FirstIdx = dyn_cast<Constant>(Indices.front());
3241 if (FirstIdx && FirstIdx->isNullValue() &&
3242 !FirstIdx->getType()->isVectorTy()) {
3244 ++GTI;
3245 if (!GTI.isStruct())
3247 GEP.getPointerOperand(),
3248 drop_begin(Indices), "",
3249 GEP.getNoWrapFlags()));
3250 }
3251
3252 // Scalarize vector operands; prefer splat-of-gep.as canonical form.
3253 // Note that this looses information about undef lanes; we run it after
3254 // demanded bits to partially mitigate that loss.
3255 if (GEPType->isVectorTy() && llvm::any_of(GEP.operands(), [](Value *Op) {
3256 return Op->getType()->isVectorTy() && getSplatValue(Op);
3257 })) {
3258 SmallVector<Value *> NewOps;
3259 for (auto &Op : GEP.operands()) {
3260 if (Op->getType()->isVectorTy())
3261 if (Value *Scalar = getSplatValue(Op)) {
3262 NewOps.push_back(Scalar);
3263 continue;
3264 }
3265 NewOps.push_back(Op);
3266 }
3267
3268 Value *Res = Builder.CreateGEP(GEP.getSourceElementType(), NewOps[0],
3269 ArrayRef(NewOps).drop_front(), GEP.getName(),
3270 GEP.getNoWrapFlags());
3271 if (!Res->getType()->isVectorTy()) {
3272 ElementCount EC = cast<VectorType>(GEPType)->getElementCount();
3273 Res = Builder.CreateVectorSplat(EC, Res);
3274 }
3275 return replaceInstUsesWith(GEP, Res);
3276 }
3277
3278 bool SeenVarIndex = false;
3279 for (auto [IdxNum, Idx] : enumerate(Indices)) {
3280 if (isa<Constant>(Idx))
3281 continue;
3282
3283 if (!SeenVarIndex) {
3284 SeenVarIndex = true;
3285 continue;
3286 }
3287
3288 // GEP has multiple variable indices: Split it.
3289 ArrayRef<Value *> FrontIndices = ArrayRef(Indices).take_front(IdxNum);
3290 Value *FrontGEP =
3291 Builder.CreateGEP(GEPEltType, PtrOp, FrontIndices,
3292 GEP.getName() + ".split", GEP.getNoWrapFlags());
3293
3294 SmallVector<Value *> BackIndices;
3295 BackIndices.push_back(Constant::getNullValue(NewScalarIndexTy));
3296 append_range(BackIndices, drop_begin(Indices, IdxNum));
3298 GetElementPtrInst::getIndexedType(GEPEltType, FrontIndices), FrontGEP,
3299 BackIndices, GEP.getNoWrapFlags());
3300 }
3301
3302 // Check to see if the inputs to the PHI node are getelementptr instructions.
3303 if (auto *PN = dyn_cast<PHINode>(PtrOp)) {
3304 if (Value *NewPtrOp = foldGEPOfPhi(GEP, PN, Builder))
3305 return replaceOperand(GEP, 0, NewPtrOp);
3306 }
3307
3308 if (auto *Src = dyn_cast<GEPOperator>(PtrOp))
3309 if (Instruction *I = visitGEPOfGEP(GEP, Src))
3310 return I;
3311
3312 if (GEP.getNumIndices() == 1) {
3313 unsigned AS = GEP.getPointerAddressSpace();
3314 if (GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
3315 DL.getIndexSizeInBits(AS)) {
3316 uint64_t TyAllocSize = DL.getTypeAllocSize(GEPEltType).getFixedValue();
3317
3318 if (TyAllocSize == 1) {
3319 // Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) to (bitcast Y),
3320 // but only if the result pointer is only used as if it were an integer,
3321 // or both point to the same underlying object (otherwise provenance is
3322 // not necessarily retained).
3323 Value *X = GEP.getPointerOperand();
3324 Value *Y;
3325 if (match(GEP.getOperand(1),
3327 GEPType == Y->getType()) {
3328 bool HasSameUnderlyingObject =
3330 bool Changed = false;
3331 GEP.replaceUsesWithIf(Y, [&](Use &U) {
3332 bool ShouldReplace = HasSameUnderlyingObject ||
3333 isa<ICmpInst>(U.getUser()) ||
3334 isa<PtrToIntInst>(U.getUser());
3335 Changed |= ShouldReplace;
3336 return ShouldReplace;
3337 });
3338 return Changed ? &GEP : nullptr;
3339 }
3340 } else if (auto *ExactIns =
3341 dyn_cast<PossiblyExactOperator>(GEP.getOperand(1))) {
3342 // Canonicalize (gep T* X, V / sizeof(T)) to (gep i8* X, V)
3343 Value *V;
3344 if (ExactIns->isExact()) {
3345 if ((has_single_bit(TyAllocSize) &&
3346 match(GEP.getOperand(1),
3347 m_Shr(m_Value(V),
3348 m_SpecificInt(countr_zero(TyAllocSize))))) ||
3349 match(GEP.getOperand(1),
3350 m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize)))) {
3352 GEP.getPointerOperand(), V,
3353 GEP.getNoWrapFlags());
3354 }
3355 }
3356 if (ExactIns->isExact() && ExactIns->hasOneUse()) {
3357 // Try to canonicalize non-i8 element type to i8 if the index is an
3358 // exact instruction. If the index is an exact instruction (div/shr)
3359 // with a constant RHS, we can fold the non-i8 element scale into the
3360 // div/shr (similiar to the mul case, just inverted).
3361 const APInt *C;
3362 std::optional<APInt> NewC;
3363 if (has_single_bit(TyAllocSize) &&
3364 match(ExactIns, m_Shr(m_Value(V), m_APInt(C))) &&
3365 C->uge(countr_zero(TyAllocSize)))
3366 NewC = *C - countr_zero(TyAllocSize);
3367 else if (match(ExactIns, m_UDiv(m_Value(V), m_APInt(C)))) {
3368 APInt Quot;
3369 uint64_t Rem;
3370 APInt::udivrem(*C, TyAllocSize, Quot, Rem);
3371 if (Rem == 0)
3372 NewC = Quot;
3373 } else if (match(ExactIns, m_SDiv(m_Value(V), m_APInt(C)))) {
3374 APInt Quot;
3375 int64_t Rem;
3376 APInt::sdivrem(*C, TyAllocSize, Quot, Rem);
3377 // For sdiv we need to make sure we arent creating INT_MIN / -1.
3378 if (!Quot.isAllOnes() && Rem == 0)
3379 NewC = Quot;
3380 }
3381
3382 if (NewC.has_value()) {
3383 Value *NewOp = Builder.CreateBinOp(
3384 static_cast<Instruction::BinaryOps>(ExactIns->getOpcode()), V,
3385 ConstantInt::get(V->getType(), *NewC));
3386 cast<BinaryOperator>(NewOp)->setIsExact();
3388 GEP.getPointerOperand(), NewOp,
3389 GEP.getNoWrapFlags());
3390 }
3391 }
3392 }
3393 }
3394 }
3395 // We do not handle pointer-vector geps here.
3396 if (GEPType->isVectorTy())
3397 return nullptr;
3398
3399 if (!GEP.isInBounds()) {
3400 unsigned IdxWidth =
3402 APInt BasePtrOffset(IdxWidth, 0);
3403 Value *UnderlyingPtrOp =
3404 PtrOp->stripAndAccumulateInBoundsConstantOffsets(DL, BasePtrOffset);
3405 bool CanBeNull, CanBeFreed;
3406 uint64_t DerefBytes = UnderlyingPtrOp->getPointerDereferenceableBytes(
3407 DL, CanBeNull, CanBeFreed);
3408 if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
3409 if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
3410 BasePtrOffset.isNonNegative()) {
3411 APInt AllocSize(IdxWidth, DerefBytes);
3412 if (BasePtrOffset.ule(AllocSize)) {
3414 GEP.getSourceElementType(), PtrOp, Indices, GEP.getName());
3415 }
3416 }
3417 }
3418 }
3419
3420 // nusw + nneg -> nuw
3421 if (GEP.hasNoUnsignedSignedWrap() && !GEP.hasNoUnsignedWrap() &&
3422 all_of(GEP.indices(), [&](Value *Idx) {
3423 return isKnownNonNegative(Idx, SQ.getWithInstruction(&GEP));
3424 })) {
3425 GEP.setNoWrapFlags(GEP.getNoWrapFlags() | GEPNoWrapFlags::noUnsignedWrap());
3426 return &GEP;
3427 }
3428
3429 // These rewrites are trying to preserve inbounds/nuw attributes. So we want
3430 // to do this after having tried to derive "nuw" above.
3431 if (GEP.getNumIndices() == 1) {
3432 // Given (gep p, x+y) we want to determine the common nowrap flags for both
3433 // geps if transforming into (gep (gep p, x), y).
3434 auto GetPreservedNoWrapFlags = [&](bool AddIsNUW) {
3435 // We can preserve both "inbounds nuw", "nusw nuw" and "nuw" if we know
3436 // that x + y does not have unsigned wrap.
3437 if (GEP.hasNoUnsignedWrap() && AddIsNUW)
3438 return GEP.getNoWrapFlags();
3439 return GEPNoWrapFlags::none();
3440 };
3441
3442 // Try to replace ADD + GEP with GEP + GEP.
3443 Value *Idx1, *Idx2;
3444 if (match(GEP.getOperand(1),
3445 m_OneUse(m_AddLike(m_Value(Idx1), m_Value(Idx2))))) {
3446 // %idx = add i64 %idx1, %idx2
3447 // %gep = getelementptr i32, ptr %ptr, i64 %idx
3448 // as:
3449 // %newptr = getelementptr i32, ptr %ptr, i64 %idx1
3450 // %newgep = getelementptr i32, ptr %newptr, i64 %idx2
3451 bool NUW = match(GEP.getOperand(1), m_NUWAddLike(m_Value(), m_Value()));
3452 GEPNoWrapFlags NWFlags = GetPreservedNoWrapFlags(NUW);
3453 auto *NewPtr =
3454 Builder.CreateGEP(GEP.getSourceElementType(), GEP.getPointerOperand(),
3455 Idx1, "", NWFlags);
3456 return replaceInstUsesWith(GEP,
3457 Builder.CreateGEP(GEP.getSourceElementType(),
3458 NewPtr, Idx2, "", NWFlags));
3459 }
3460 ConstantInt *C;
3461 if (match(GEP.getOperand(1), m_OneUse(m_SExtLike(m_OneUse(m_NSWAddLike(
3462 m_Value(Idx1), m_ConstantInt(C))))))) {
3463 // %add = add nsw i32 %idx1, idx2
3464 // %sidx = sext i32 %add to i64
3465 // %gep = getelementptr i32, ptr %ptr, i64 %sidx
3466 // as:
3467 // %newptr = getelementptr i32, ptr %ptr, i32 %idx1
3468 // %newgep = getelementptr i32, ptr %newptr, i32 idx2
3469 bool NUW = match(GEP.getOperand(1),
3471 GEPNoWrapFlags NWFlags = GetPreservedNoWrapFlags(NUW);
3472 auto *NewPtr = Builder.CreateGEP(
3473 GEP.getSourceElementType(), GEP.getPointerOperand(),
3474 Builder.CreateSExt(Idx1, GEP.getOperand(1)->getType()), "", NWFlags);
3475 return replaceInstUsesWith(
3476 GEP,
3477 Builder.CreateGEP(GEP.getSourceElementType(), NewPtr,
3478 Builder.CreateSExt(C, GEP.getOperand(1)->getType()),
3479 "", NWFlags));
3480 }
3481 }
3482
3484 return R;
3485
3486 return nullptr;
3487}
3488
3490 Instruction *AI) {
3491 if (isa<ConstantPointerNull>(V))
3492 return true;
3493 if (auto *LI = dyn_cast<LoadInst>(V))
3494 return isa<GlobalVariable>(LI->getPointerOperand());
3495 // Two distinct allocations will never be equal.
3496 return isAllocLikeFn(V, &TLI) && V != AI;
3497}
3498
3499/// Given a call CB which uses an address UsedV, return true if we can prove the
3500/// call's only possible effect is storing to V.
3501static bool isRemovableWrite(CallBase &CB, Value *UsedV,
3502 const TargetLibraryInfo &TLI) {
3503 if (!CB.use_empty())
3504 // TODO: add recursion if returned attribute is present
3505 return false;
3506
3507 if (CB.isTerminator())
3508 // TODO: remove implementation restriction
3509 return false;
3510
3511 if (!CB.willReturn() || !CB.doesNotThrow())
3512 return false;
3513
3514 // If the only possible side effect of the call is writing to the alloca,
3515 // and the result isn't used, we can safely remove any reads implied by the
3516 // call including those which might read the alloca itself.
3517 std::optional<MemoryLocation> Dest = MemoryLocation::getForDest(&CB, TLI);
3518 return Dest && Dest->Ptr == UsedV;
3519}
3520
3521static std::optional<ModRefInfo>
3523 const TargetLibraryInfo &TLI, bool KnowInit) {
3525 const std::optional<StringRef> Family = getAllocationFamily(AI, &TLI);
3526 Worklist.push_back(AI);
3528
3529 do {
3530 Instruction *PI = Worklist.pop_back_val();
3531 for (User *U : PI->users()) {
3532 Instruction *I = cast<Instruction>(U);
3533 switch (I->getOpcode()) {
3534 default:
3535 // Give up the moment we see something we can't handle.
3536 return std::nullopt;
3537
3538 case Instruction::AddrSpaceCast:
3539 case Instruction::BitCast:
3540 case Instruction::GetElementPtr:
3541 Users.emplace_back(I);
3542 Worklist.push_back(I);
3543 continue;
3544
3545 case Instruction::ICmp: {
3546 ICmpInst *ICI = cast<ICmpInst>(I);
3547 // We can fold eq/ne comparisons with null to false/true, respectively.
3548 // We also fold comparisons in some conditions provided the alloc has
3549 // not escaped (see isNeverEqualToUnescapedAlloc).
3550 if (!ICI->isEquality())
3551 return std::nullopt;
3552 unsigned OtherIndex = (ICI->getOperand(0) == PI) ? 1 : 0;
3553 if (!isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex), TLI, AI))
3554 return std::nullopt;
3555
3556 // Do not fold compares to aligned_alloc calls, as they may have to
3557 // return null in case the required alignment cannot be satisfied,
3558 // unless we can prove that both alignment and size are valid.
3559 auto AlignmentAndSizeKnownValid = [](CallBase *CB) {
3560 // Check if alignment and size of a call to aligned_alloc is valid,
3561 // that is alignment is a power-of-2 and the size is a multiple of the
3562 // alignment.
3563 const APInt *Alignment;
3564 const APInt *Size;
3565 return match(CB->getArgOperand(0), m_APInt(Alignment)) &&
3566 match(CB->getArgOperand(1), m_APInt(Size)) &&
3567 Alignment->isPowerOf2() && Size->urem(*Alignment).isZero();
3568 };
3569 auto *CB = dyn_cast<CallBase>(AI);
3570 LibFunc TheLibFunc;
3571 if (CB && TLI.getLibFunc(*CB->getCalledFunction(), TheLibFunc) &&
3572 TLI.has(TheLibFunc) && TheLibFunc == LibFunc_aligned_alloc &&
3573 !AlignmentAndSizeKnownValid(CB))
3574 return std::nullopt;
3575 Users.emplace_back(I);
3576 continue;
3577 }
3578
3579 case Instruction::Call:
3580 // Ignore no-op and store intrinsics.
3581 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
3582 switch (II->getIntrinsicID()) {
3583 default:
3584 return std::nullopt;
3585
3586 case Intrinsic::memmove:
3587 case Intrinsic::memcpy:
3588 case Intrinsic::memset: {
3589 MemIntrinsic *MI = cast<MemIntrinsic>(II);
3590 if (MI->isVolatile())
3591 return std::nullopt;
3592 // Note: this could also be ModRef, but we can still interpret that
3593 // as just Mod in that case.
3594 ModRefInfo NewAccess =
3595 MI->getRawDest() == PI ? ModRefInfo::Mod : ModRefInfo::Ref;
3596 if ((Access & ~NewAccess) != ModRefInfo::NoModRef)
3597 return std::nullopt;
3598 Access |= NewAccess;
3599 [[fallthrough]];
3600 }
3601 case Intrinsic::assume:
3602 case Intrinsic::invariant_start:
3603 case Intrinsic::invariant_end:
3604 case Intrinsic::lifetime_start:
3605 case Intrinsic::lifetime_end:
3606 case Intrinsic::objectsize:
3607 Users.emplace_back(I);
3608 continue;
3609 case Intrinsic::launder_invariant_group:
3610 case Intrinsic::strip_invariant_group:
3611 Users.emplace_back(I);
3612 Worklist.push_back(I);
3613 continue;
3614 }
3615 }
3616
3617 if (Family && getFreedOperand(cast<CallBase>(I), &TLI) == PI &&
3618 getAllocationFamily(I, &TLI) == Family) {
3619 Users.emplace_back(I);
3620 continue;
3621 }
3622
3623 if (Family && getReallocatedOperand(cast<CallBase>(I)) == PI &&
3624 getAllocationFamily(I, &TLI) == Family) {
3625 Users.emplace_back(I);
3626 Worklist.push_back(I);
3627 continue;
3628 }
3629
3630 if (!isRefSet(Access) &&
3631 isRemovableWrite(*cast<CallBase>(I), PI, TLI)) {
3633 Users.emplace_back(I);
3634 continue;
3635 }
3636
3637 return std::nullopt;
3638
3639 case Instruction::Store: {
3640 StoreInst *SI = cast<StoreInst>(I);
3641 if (SI->isVolatile() || SI->getPointerOperand() != PI)
3642 return std::nullopt;
3643 if (isRefSet(Access))
3644 return std::nullopt;
3646 Users.emplace_back(I);
3647 continue;
3648 }
3649
3650 case Instruction::Load: {
3651 LoadInst *LI = cast<LoadInst>(I);
3652 if (LI->isVolatile() || LI->getPointerOperand() != PI)
3653 return std::nullopt;
3654 if (isModSet(Access))
3655 return std::nullopt;
3657 Users.emplace_back(I);
3658 continue;
3659 }
3660 }
3661 llvm_unreachable("missing a return?");
3662 }
3663 } while (!Worklist.empty());
3664
3666 return Access;
3667}
3668
3670 assert(isa<AllocaInst>(MI) || isRemovableAlloc(&cast<CallBase>(MI), &TLI));
3671
3672 // If we have a malloc call which is only used in any amount of comparisons to
3673 // null and free calls, delete the calls and replace the comparisons with true
3674 // or false as appropriate.
3675
3676 // This is based on the principle that we can substitute our own allocation
3677 // function (which will never return null) rather than knowledge of the
3678 // specific function being called. In some sense this can change the permitted
3679 // outputs of a program (when we convert a malloc to an alloca, the fact that
3680 // the allocation is now on the stack is potentially visible, for example),
3681 // but we believe in a permissible manner.
3683
3684 // If we are removing an alloca with a dbg.declare, insert dbg.value calls
3685 // before each store.
3687 std::unique_ptr<DIBuilder> DIB;
3688 if (isa<AllocaInst>(MI)) {
3689 findDbgUsers(&MI, DVRs);
3690 DIB.reset(new DIBuilder(*MI.getModule(), /*AllowUnresolved=*/false));
3691 }
3692
3693 // Determine what getInitialValueOfAllocation would return without actually
3694 // allocating the result.
3695 bool KnowInitUndef = false;
3696 bool KnowInitZero = false;
3697 Constant *Init =
3699 if (Init) {
3700 if (isa<UndefValue>(Init))
3701 KnowInitUndef = true;
3702 else if (Init->isNullValue())
3703 KnowInitZero = true;
3704 }
3705 // The various sanitizers don't actually return undef memory, but rather
3706 // memory initialized with special forms of runtime poison
3707 auto &F = *MI.getFunction();
3708 if (F.hasFnAttribute(Attribute::SanitizeMemory) ||
3709 F.hasFnAttribute(Attribute::SanitizeAddress))
3710 KnowInitUndef = false;
3711
3712 auto Removable =
3713 isAllocSiteRemovable(&MI, Users, TLI, KnowInitZero | KnowInitUndef);
3714 if (Removable) {
3715 for (WeakTrackingVH &User : Users) {
3716 // Lowering all @llvm.objectsize and MTI calls first because they may use
3717 // a bitcast/GEP of the alloca we are removing.
3718 if (!User)
3719 continue;
3720
3721 Instruction *I = cast<Instruction>(&*User);
3722
3723 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
3724 if (II->getIntrinsicID() == Intrinsic::objectsize) {
3725 SmallVector<Instruction *> InsertedInstructions;
3726 Value *Result = lowerObjectSizeCall(
3727 II, DL, &TLI, AA, /*MustSucceed=*/true, &InsertedInstructions);
3728 for (Instruction *Inserted : InsertedInstructions)
3729 Worklist.add(Inserted);
3730 replaceInstUsesWith(*I, Result);
3732 User = nullptr; // Skip examining in the next loop.
3733 continue;
3734 }
3735 if (auto *MTI = dyn_cast<MemTransferInst>(I)) {
3736 if (KnowInitZero && isRefSet(*Removable)) {
3739 auto *M = Builder.CreateMemSet(
3740 MTI->getRawDest(),
3741 ConstantInt::get(Type::getInt8Ty(MI.getContext()), 0),
3742 MTI->getLength(), MTI->getDestAlign());
3743 M->copyMetadata(*MTI);
3744 }
3745 }
3746 }
3747 }
3748 for (WeakTrackingVH &User : Users) {
3749 if (!User)
3750 continue;
3751
3752 Instruction *I = cast<Instruction>(&*User);
3753
3754 if (ICmpInst *C = dyn_cast<ICmpInst>(I)) {
3756 ConstantInt::get(Type::getInt1Ty(C->getContext()),
3757 C->isFalseWhenEqual()));
3758 } else if (auto *SI = dyn_cast<StoreInst>(I)) {
3759 for (auto *DVR : DVRs)
3760 if (DVR->isAddressOfVariable())
3761 ConvertDebugDeclareToDebugValue(DVR, SI, *DIB);
3762 } else {
3763 // Casts, GEP, or anything else: we're about to delete this instruction,
3764 // so it can not have any valid uses.
3765 Constant *Replace;
3766 if (isa<LoadInst>(I)) {
3767 assert(KnowInitZero || KnowInitUndef);
3768 Replace = KnowInitUndef ? UndefValue::get(I->getType())
3769 : Constant::getNullValue(I->getType());
3770 } else
3771 Replace = PoisonValue::get(I->getType());
3772 replaceInstUsesWith(*I, Replace);
3773 }
3775 }
3776
3777 if (InvokeInst *II = dyn_cast<InvokeInst>(&MI)) {
3778 // Replace invoke with a NOP intrinsic to maintain the original CFG
3779 Module *M = II->getModule();
3780 Function *F = Intrinsic::getOrInsertDeclaration(M, Intrinsic::donothing);
3781 auto *NewII = InvokeInst::Create(
3782 F, II->getNormalDest(), II->getUnwindDest(), {}, "", II->getParent());
3783 NewII->setDebugLoc(II->getDebugLoc());
3784 }
3785
3786 // Remove debug intrinsics which describe the value contained within the
3787 // alloca. In addition to removing dbg.{declare,addr} which simply point to
3788 // the alloca, remove dbg.value(<alloca>, ..., DW_OP_deref)'s as well, e.g.:
3789 //
3790 // ```
3791 // define void @foo(i32 %0) {
3792 // %a = alloca i32 ; Deleted.
3793 // store i32 %0, i32* %a
3794 // dbg.value(i32 %0, "arg0") ; Not deleted.
3795 // dbg.value(i32* %a, "arg0", DW_OP_deref) ; Deleted.
3796 // call void @trivially_inlinable_no_op(i32* %a)
3797 // ret void
3798 // }
3799 // ```
3800 //
3801 // This may not be required if we stop describing the contents of allocas
3802 // using dbg.value(<alloca>, ..., DW_OP_deref), but we currently do this in
3803 // the LowerDbgDeclare utility.
3804 //
3805 // If there is a dead store to `%a` in @trivially_inlinable_no_op, the
3806 // "arg0" dbg.value may be stale after the call. However, failing to remove
3807 // the DW_OP_deref dbg.value causes large gaps in location coverage.
3808 //
3809 // FIXME: the Assignment Tracking project has now likely made this
3810 // redundant (and it's sometimes harmful).
3811 for (auto *DVR : DVRs)
3812 if (DVR->isAddressOfVariable() || DVR->getExpression()->startsWithDeref())
3813 DVR->eraseFromParent();
3814
3815 return eraseInstFromFunction(MI);
3816 }
3817 return nullptr;
3818}
3819
3820/// Move the call to free before a NULL test.
3821///
3822/// Check if this free is accessed after its argument has been test
3823/// against NULL (property 0).
3824/// If yes, it is legal to move this call in its predecessor block.
3825///
3826/// The move is performed only if the block containing the call to free
3827/// will be removed, i.e.:
3828/// 1. it has only one predecessor P, and P has two successors
3829/// 2. it contains the call, noops, and an unconditional branch
3830/// 3. its successor is the same as its predecessor's successor
3831///
3832/// The profitability is out-of concern here and this function should
3833/// be called only if the caller knows this transformation would be
3834/// profitable (e.g., for code size).
3836 const DataLayout &DL) {
3837 Value *Op = FI.getArgOperand(0);
3838 BasicBlock *FreeInstrBB = FI.getParent();
3839 BasicBlock *PredBB = FreeInstrBB->getSinglePredecessor();
3840
3841 // Validate part of constraint #1: Only one predecessor
3842 // FIXME: We can extend the number of predecessor, but in that case, we
3843 // would duplicate the call to free in each predecessor and it may
3844 // not be profitable even for code size.
3845 if (!PredBB)
3846 return nullptr;
3847
3848 // Validate constraint #2: Does this block contains only the call to
3849 // free, noops, and an unconditional branch?
3850 BasicBlock *SuccBB;
3851 Instruction *FreeInstrBBTerminator = FreeInstrBB->getTerminator();
3852 if (!match(FreeInstrBBTerminator, m_UnconditionalBr(SuccBB)))
3853 return nullptr;
3854
3855 // If there are only 2 instructions in the block, at this point,
3856 // this is the call to free and unconditional.
3857 // If there are more than 2 instructions, check that they are noops
3858 // i.e., they won't hurt the performance of the generated code.
3859 if (FreeInstrBB->size() != 2) {
3860 for (const Instruction &Inst : FreeInstrBB->instructionsWithoutDebug()) {
3861 if (&Inst == &FI || &Inst == FreeInstrBBTerminator)
3862 continue;
3863 auto *Cast = dyn_cast<CastInst>(&Inst);
3864 if (!Cast || !Cast->isNoopCast(DL))
3865 return nullptr;
3866 }
3867 }
3868 // Validate the rest of constraint #1 by matching on the pred branch.
3869 Instruction *TI = PredBB->getTerminator();
3870 BasicBlock *TrueBB, *FalseBB;
3871 CmpPredicate Pred;
3872 if (!match(TI, m_Br(m_ICmp(Pred,
3874 m_Specific(Op->stripPointerCasts())),
3875 m_Zero()),
3876 TrueBB, FalseBB)))
3877 return nullptr;
3878 if (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
3879 return nullptr;
3880
3881 // Validate constraint #3: Ensure the null case just falls through.
3882 if (SuccBB != (Pred == ICmpInst::ICMP_EQ ? TrueBB : FalseBB))
3883 return nullptr;
3884 assert(FreeInstrBB == (Pred == ICmpInst::ICMP_EQ ? FalseBB : TrueBB) &&
3885 "Broken CFG: missing edge from predecessor to successor");
3886
3887 // At this point, we know that everything in FreeInstrBB can be moved
3888 // before TI.
3889 for (Instruction &Instr : llvm::make_early_inc_range(*FreeInstrBB)) {
3890 if (&Instr == FreeInstrBBTerminator)
3891 break;
3892 Instr.moveBeforePreserving(TI->getIterator());
3893 }
3894 assert(FreeInstrBB->size() == 1 &&
3895 "Only the branch instruction should remain");
3896
3897 // Now that we've moved the call to free before the NULL check, we have to
3898 // remove any attributes on its parameter that imply it's non-null, because
3899 // those attributes might have only been valid because of the NULL check, and
3900 // we can get miscompiles if we keep them. This is conservative if non-null is
3901 // also implied by something other than the NULL check, but it's guaranteed to
3902 // be correct, and the conservativeness won't matter in practice, since the
3903 // attributes are irrelevant for the call to free itself and the pointer
3904 // shouldn't be used after the call.
3905 AttributeList Attrs = FI.getAttributes();
3906 Attrs = Attrs.removeParamAttribute(FI.getContext(), 0, Attribute::NonNull);
3907 Attribute Dereferenceable = Attrs.getParamAttr(0, Attribute::Dereferenceable);
3908 if (Dereferenceable.isValid()) {
3909 uint64_t Bytes = Dereferenceable.getDereferenceableBytes();
3910 Attrs = Attrs.removeParamAttribute(FI.getContext(), 0,
3911 Attribute::Dereferenceable);
3912 Attrs = Attrs.addDereferenceableOrNullParamAttr(FI.getContext(), 0, Bytes);
3913 }
3914 FI.setAttributes(Attrs);
3915
3916 return &FI;
3917}
3918
3920 // free undef -> unreachable.
3921 if (isa<UndefValue>(Op)) {
3922 // Leave a marker since we can't modify the CFG here.
3924 return eraseInstFromFunction(FI);
3925 }
3926
3927 // If we have 'free null' delete the instruction. This can happen in stl code
3928 // when lots of inlining happens.
3929 if (isa<ConstantPointerNull>(Op))
3930 return eraseInstFromFunction(FI);
3931
3932 // If we had free(realloc(...)) with no intervening uses, then eliminate the
3933 // realloc() entirely.
3934 CallInst *CI = dyn_cast<CallInst>(Op);
3935 if (CI && CI->hasOneUse())
3936 if (Value *ReallocatedOp = getReallocatedOperand(CI))
3937 return eraseInstFromFunction(*replaceInstUsesWith(*CI, ReallocatedOp));
3938
3939 // If we optimize for code size, try to move the call to free before the null
3940 // test so that simplify cfg can remove the empty block and dead code
3941 // elimination the branch. I.e., helps to turn something like:
3942 // if (foo) free(foo);
3943 // into
3944 // free(foo);
3945 //
3946 // Note that we can only do this for 'free' and not for any flavor of
3947 // 'operator delete'; there is no 'operator delete' symbol for which we are
3948 // permitted to invent a call, even if we're passing in a null pointer.
3949 if (MinimizeSize) {
3950 LibFunc Func;
3951 if (TLI.getLibFunc(FI, Func) && TLI.has(Func) && Func == LibFunc_free)
3953 return I;
3954 }
3955
3956 return nullptr;
3957}
3958
3960 Value *RetVal = RI.getReturnValue();
3961 if (!RetVal)
3962 return nullptr;
3963
3964 Function *F = RI.getFunction();
3965 Type *RetTy = RetVal->getType();
3966 if (RetTy->isPointerTy()) {
3967 bool HasDereferenceable =
3968 F->getAttributes().getRetDereferenceableBytes() > 0;
3969 if (F->hasRetAttribute(Attribute::NonNull) ||
3970 (HasDereferenceable &&
3971 !NullPointerIsDefined(F, RetTy->getPointerAddressSpace()))) {
3972 if (Value *V = simplifyNonNullOperand(RetVal, HasDereferenceable))
3973 return replaceOperand(RI, 0, V);
3974 }
3975 }
3976
3978 return nullptr;
3979
3980 FPClassTest ReturnClass = F->getAttributes().getRetNoFPClass();
3981 if (ReturnClass == fcNone)
3982 return nullptr;
3983
3984 KnownFPClass KnownClass;
3985 Value *Simplified =
3986 SimplifyDemandedUseFPClass(RetVal, ~ReturnClass, KnownClass, &RI);
3987 if (!Simplified)
3988 return nullptr;
3989
3990 return ReturnInst::Create(RI.getContext(), Simplified);
3991}
3992
3993// WARNING: keep in sync with SimplifyCFGOpt::simplifyUnreachable()!
3995 // Try to remove the previous instruction if it must lead to unreachable.
3996 // This includes instructions like stores and "llvm.assume" that may not get
3997 // removed by simple dead code elimination.
3998 bool Changed = false;
3999 while (Instruction *Prev = I.getPrevNode()) {
4000 // While we theoretically can erase EH, that would result in a block that
4001 // used to start with an EH no longer starting with EH, which is invalid.
4002 // To make it valid, we'd need to fixup predecessors to no longer refer to
4003 // this block, but that changes CFG, which is not allowed in InstCombine.
4004 if (Prev->isEHPad())
4005 break; // Can not drop any more instructions. We're done here.
4006
4008 break; // Can not drop any more instructions. We're done here.
4009 // Otherwise, this instruction can be freely erased,
4010 // even if it is not side-effect free.
4011
4012 // A value may still have uses before we process it here (for example, in
4013 // another unreachable block), so convert those to poison.
4014 replaceInstUsesWith(*Prev, PoisonValue::get(Prev->getType()));
4015 eraseInstFromFunction(*Prev);
4016 Changed = true;
4017 }
4018 return Changed;
4019}
4020
4023 return nullptr;
4024}
4025
4027 assert(BI.isUnconditional() && "Only for unconditional branches.");
4028
4029 // If this store is the second-to-last instruction in the basic block
4030 // (excluding debug info) and if the block ends with
4031 // an unconditional branch, try to move the store to the successor block.
4032
4033 auto GetLastSinkableStore = [](BasicBlock::iterator BBI) {
4034 BasicBlock::iterator FirstInstr = BBI->getParent()->begin();
4035 do {
4036 if (BBI != FirstInstr)
4037 --BBI;
4038 } while (BBI != FirstInstr && BBI->isDebugOrPseudoInst());
4039
4040 return dyn_cast<StoreInst>(BBI);
4041 };
4042
4043 if (StoreInst *SI = GetLastSinkableStore(BasicBlock::iterator(BI)))
4044 if (mergeStoreIntoSuccessor(*SI))
4045 return &BI;
4046
4047 return nullptr;
4048}
4049
4052 if (!DeadEdges.insert({From, To}).second)
4053 return;
4054
4055 // Replace phi node operands in successor with poison.
4056 for (PHINode &PN : To->phis())
4057 for (Use &U : PN.incoming_values())
4058 if (PN.getIncomingBlock(U) == From && !isa<PoisonValue>(U)) {
4059 replaceUse(U, PoisonValue::get(PN.getType()));
4060 addToWorklist(&PN);
4061 MadeIRChange = true;
4062 }
4063
4064 Worklist.push_back(To);
4065}
4066
4067// Under the assumption that I is unreachable, remove it and following
4068// instructions. Changes are reported directly to MadeIRChange.
4071 BasicBlock *BB = I->getParent();
4072 for (Instruction &Inst : make_early_inc_range(
4073 make_range(std::next(BB->getTerminator()->getReverseIterator()),
4074 std::next(I->getReverseIterator())))) {
4075 if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
4076 replaceInstUsesWith(Inst, PoisonValue::get(Inst.getType()));
4077 MadeIRChange = true;
4078 }
4079 if (Inst.isEHPad() || Inst.getType()->isTokenTy())
4080 continue;
4081 // RemoveDIs: erase debug-info on this instruction manually.
4082 Inst.dropDbgRecords();
4084 MadeIRChange = true;
4085 }
4086
4087 SmallVector<Value *> Changed;
4088 if (handleUnreachableTerminator(BB->getTerminator(), Changed)) {
4089 MadeIRChange = true;
4090 for (Value *V : Changed)
4091 addToWorklist(cast<Instruction>(V));
4092 }
4093
4094 // Handle potentially dead successors.
4095 for (BasicBlock *Succ : successors(BB))
4096 addDeadEdge(BB, Succ, Worklist);
4097}
4098
4101 while (!Worklist.empty()) {
4102 BasicBlock *BB = Worklist.pop_back_val();
4103 if (!all_of(predecessors(BB), [&](BasicBlock *Pred) {
4104 return DeadEdges.contains({Pred, BB}) || DT.dominates(BB, Pred);
4105 }))
4106 continue;
4107
4109 }
4110}
4111
4113 BasicBlock *LiveSucc) {
4115 for (BasicBlock *Succ : successors(BB)) {
4116 // The live successor isn't dead.
4117 if (Succ == LiveSucc)
4118 continue;
4119
4120 addDeadEdge(BB, Succ, Worklist);
4121 }
4122
4124}
4125
4127 if (BI.isUnconditional())
4129
4130 // Change br (not X), label True, label False to: br X, label False, True
4131 Value *Cond = BI.getCondition();
4132 Value *X;
4133 if (match(Cond, m_Not(m_Value(X))) && !isa<Constant>(X)) {
4134 // Swap Destinations and condition...
4135 BI.swapSuccessors();
4136 if (BPI)
4138 return replaceOperand(BI, 0, X);
4139 }
4140
4141 // Canonicalize logical-and-with-invert as logical-or-with-invert.
4142 // This is done by inverting the condition and swapping successors:
4143 // br (X && !Y), T, F --> br !(X && !Y), F, T --> br (!X || Y), F, T
4144 Value *Y;
4145 if (isa<SelectInst>(Cond) &&
4146 match(Cond,
4148 Value *NotX = Builder.CreateNot(X, "not." + X->getName());
4149 Value *Or = Builder.CreateLogicalOr(NotX, Y);
4150 BI.swapSuccessors();
4151 if (BPI)
4153 return replaceOperand(BI, 0, Or);
4154 }
4155
4156 // If the condition is irrelevant, remove the use so that other
4157 // transforms on the condition become more effective.
4158 if (!isa<ConstantInt>(Cond) && BI.getSuccessor(0) == BI.getSuccessor(1))
4159 return replaceOperand(BI, 0, ConstantInt::getFalse(Cond->getType()));
4160
4161 // Canonicalize, for example, fcmp_one -> fcmp_oeq.
4162 CmpPredicate Pred;
4163 if (match(Cond, m_OneUse(m_FCmp(Pred, m_Value(), m_Value()))) &&
4164 !isCanonicalPredicate(Pred)) {
4165 // Swap destinations and condition.
4166 auto *Cmp = cast<CmpInst>(Cond);
4167 Cmp->setPredicate(CmpInst::getInversePredicate(Pred));
4168 BI.swapSuccessors();
4169 if (BPI)
4171 Worklist.push(Cmp);
4172 return &BI;
4173 }
4174
4175 if (isa<UndefValue>(Cond)) {
4176 handlePotentiallyDeadSuccessors(BI.getParent(), /*LiveSucc*/ nullptr);
4177 return nullptr;
4178 }
4179 if (auto *CI = dyn_cast<ConstantInt>(Cond)) {
4181 BI.getSuccessor(!CI->getZExtValue()));
4182 return nullptr;
4183 }
4184
4185 // Replace all dominated uses of the condition with true/false
4186 // Ignore constant expressions to avoid iterating over uses on other
4187 // functions.
4188 if (!isa<Constant>(Cond) && BI.getSuccessor(0) != BI.getSuccessor(1)) {
4189 for (auto &U : make_early_inc_range(Cond->uses())) {
4190 BasicBlockEdge Edge0(BI.getParent(), BI.getSuccessor(0));
4191 if (DT.dominates(Edge0, U)) {
4192 replaceUse(U, ConstantInt::getTrue(Cond->getType()));
4193 addToWorklist(cast<Instruction>(U.getUser()));
4194 continue;
4195 }
4196 BasicBlockEdge Edge1(BI.getParent(), BI.getSuccessor(1));
4197 if (DT.dominates(Edge1, U)) {
4198 replaceUse(U, ConstantInt::getFalse(Cond->getType()));
4199 addToWorklist(cast<Instruction>(U.getUser()));
4200 }
4201 }
4202 }
4203
4204 DC.registerBranch(&BI);
4205 return nullptr;
4206}
4207
4208// Replaces (switch (select cond, X, C)/(select cond, C, X)) with (switch X) if
4209// we can prove that both (switch C) and (switch X) go to the default when cond
4210// is false/true.
4213 bool IsTrueArm) {
4214 unsigned CstOpIdx = IsTrueArm ? 1 : 2;
4215 auto *C = dyn_cast<ConstantInt>(Select->getOperand(CstOpIdx));
4216 if (!C)
4217 return nullptr;
4218
4219 BasicBlock *CstBB = SI.findCaseValue(C)->getCaseSuccessor();
4220 if (CstBB != SI.getDefaultDest())
4221 return nullptr;
4222 Value *X = Select->getOperand(3 - CstOpIdx);
4223 CmpPredicate Pred;
4224 const APInt *RHSC;
4225 if (!match(Select->getCondition(),
4226 m_ICmp(Pred, m_Specific(X), m_APInt(RHSC))))
4227 return nullptr;
4228 if (IsTrueArm)
4229 Pred = ICmpInst::getInversePredicate(Pred);
4230
4231 // See whether we can replace the select with X
4233 for (auto Case : SI.cases())
4234 if (!CR.contains(Case.getCaseValue()->getValue()))
4235 return nullptr;
4236
4237 return X;
4238}
4239
4241 Value *Cond = SI.getCondition();
4242 Value *Op0;
4243 ConstantInt *AddRHS;
4244 if (match(Cond, m_Add(m_Value(Op0), m_ConstantInt(AddRHS)))) {
4245 // Change 'switch (X+4) case 1:' into 'switch (X) case -3'.
4246 for (auto Case : SI.cases()) {
4247 Constant *NewCase = ConstantExpr::getSub(Case.getCaseValue(), AddRHS);
4248 assert(isa<ConstantInt>(NewCase) &&
4249 "Result of expression should be constant");
4250 Case.setValue(cast<ConstantInt>(NewCase));
4251 }
4252 return replaceOperand(SI, 0, Op0);
4253 }
4254
4255 ConstantInt *SubLHS;
4256 if (match(Cond, m_Sub(m_ConstantInt(SubLHS), m_Value(Op0)))) {
4257 // Change 'switch (1-X) case 1:' into 'switch (X) case 0'.
4258 for (auto Case : SI.cases()) {
4259 Constant *NewCase = ConstantExpr::getSub(SubLHS, Case.getCaseValue());
4260 assert(isa<ConstantInt>(NewCase) &&
4261 "Result of expression should be constant");
4262 Case.setValue(cast<ConstantInt>(NewCase));
4263 }
4264 return replaceOperand(SI, 0, Op0);
4265 }
4266
4267 uint64_t ShiftAmt;
4268 if (match(Cond, m_Shl(m_Value(Op0), m_ConstantInt(ShiftAmt))) &&
4269 ShiftAmt < Op0->getType()->getScalarSizeInBits() &&
4270 all_of(SI.cases(), [&](const auto &Case) {
4271 return Case.getCaseValue()->getValue().countr_zero() >= ShiftAmt;
4272 })) {
4273 // Change 'switch (X << 2) case 4:' into 'switch (X) case 1:'.
4274 OverflowingBinaryOperator *Shl = cast<OverflowingBinaryOperator>(Cond);
4275 if (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap() ||
4276 Shl->hasOneUse()) {
4277 Value *NewCond = Op0;
4278 if (!Shl->hasNoUnsignedWrap() && !Shl->hasNoSignedWrap()) {
4279 // If the shift may wrap, we need to mask off the shifted bits.
4280 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
4281 NewCond = Builder.CreateAnd(
4282 Op0, APInt::getLowBitsSet(BitWidth, BitWidth - ShiftAmt));
4283 }
4284 for (auto Case : SI.cases()) {
4285 const APInt &CaseVal = Case.getCaseValue()->getValue();
4286 APInt ShiftedCase = Shl->hasNoSignedWrap() ? CaseVal.ashr(ShiftAmt)
4287 : CaseVal.lshr(ShiftAmt);
4288 Case.setValue(ConstantInt::get(SI.getContext(), ShiftedCase));
4289 }
4290 return replaceOperand(SI, 0, NewCond);
4291 }
4292 }
4293
4294 // Fold switch(zext/sext(X)) into switch(X) if possible.
4295 if (match(Cond, m_ZExtOrSExt(m_Value(Op0)))) {
4296 bool IsZExt = isa<ZExtInst>(Cond);
4297 Type *SrcTy = Op0->getType();
4298 unsigned NewWidth = SrcTy->getScalarSizeInBits();
4299
4300 if (all_of(SI.cases(), [&](const auto &Case) {
4301 const APInt &CaseVal = Case.getCaseValue()->getValue();
4302 return IsZExt ? CaseVal.isIntN(NewWidth)
4303 : CaseVal.isSignedIntN(NewWidth);
4304 })) {
4305 for (auto &Case : SI.cases()) {
4306 APInt TruncatedCase = Case.getCaseValue()->getValue().trunc(NewWidth);
4307 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
4308 }
4309 return replaceOperand(SI, 0, Op0);
4310 }
4311 }
4312
4313 // Fold switch(select cond, X, Y) into switch(X/Y) if possible
4314 if (auto *Select = dyn_cast<SelectInst>(Cond)) {
4315 if (Value *V =
4316 simplifySwitchOnSelectUsingRanges(SI, Select, /*IsTrueArm=*/true))
4317 return replaceOperand(SI, 0, V);
4318 if (Value *V =
4319 simplifySwitchOnSelectUsingRanges(SI, Select, /*IsTrueArm=*/false))
4320 return replaceOperand(SI, 0, V);
4321 }
4322
4323 KnownBits Known = computeKnownBits(Cond, &SI);
4324 unsigned LeadingKnownZeros = Known.countMinLeadingZeros();
4325 unsigned LeadingKnownOnes = Known.countMinLeadingOnes();
4326
4327 // Compute the number of leading bits we can ignore.
4328 // TODO: A better way to determine this would use ComputeNumSignBits().
4329 for (const auto &C : SI.cases()) {
4330 LeadingKnownZeros =
4331 std::min(LeadingKnownZeros, C.getCaseValue()->getValue().countl_zero());
4332 LeadingKnownOnes =
4333 std::min(LeadingKnownOnes, C.getCaseValue()->getValue().countl_one());
4334 }
4335
4336 unsigned NewWidth = Known.getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
4337
4338 // Shrink the condition operand if the new type is smaller than the old type.
4339 // But do not shrink to a non-standard type, because backend can't generate
4340 // good code for that yet.
4341 // TODO: We can make it aggressive again after fixing PR39569.
4342 if (NewWidth > 0 && NewWidth < Known.getBitWidth() &&
4343 shouldChangeType(Known.getBitWidth(), NewWidth)) {
4344 IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth);
4346 Value *NewCond = Builder.CreateTrunc(Cond, Ty, "trunc");
4347
4348 for (auto Case : SI.cases()) {
4349 APInt TruncatedCase = Case.getCaseValue()->getValue().trunc(NewWidth);
4350 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
4351 }
4352 return replaceOperand(SI, 0, NewCond);
4353 }
4354
4355 if (isa<UndefValue>(Cond)) {
4356 handlePotentiallyDeadSuccessors(SI.getParent(), /*LiveSucc*/ nullptr);
4357 return nullptr;
4358 }
4359 if (auto *CI = dyn_cast<ConstantInt>(Cond)) {
4360 handlePotentiallyDeadSuccessors(SI.getParent(),
4361 SI.findCaseValue(CI)->getCaseSuccessor());
4362 return nullptr;
4363 }
4364
4365 return nullptr;
4366}
4367
4369InstCombinerImpl::foldExtractOfOverflowIntrinsic(ExtractValueInst &EV) {
4370 auto *WO = dyn_cast<WithOverflowInst>(EV.getAggregateOperand());
4371 if (!WO)
4372 return nullptr;
4373
4374 Intrinsic::ID OvID = WO->getIntrinsicID();
4375 const APInt *C = nullptr;
4376 if (match(WO->getRHS(), m_APIntAllowPoison(C))) {
4377 if (*EV.idx_begin() == 0 && (OvID == Intrinsic::smul_with_overflow ||
4378 OvID == Intrinsic::umul_with_overflow)) {
4379 // extractvalue (any_mul_with_overflow X, -1), 0 --> -X
4380 if (C->isAllOnes())
4381 return BinaryOperator::CreateNeg(WO->getLHS());
4382 // extractvalue (any_mul_with_overflow X, 2^n), 0 --> X << n
4383 if (C->isPowerOf2()) {
4384 return BinaryOperator::CreateShl(
4385 WO->getLHS(),
4386 ConstantInt::get(WO->getLHS()->getType(), C->logBase2()));
4387 }
4388 }
4389 }
4390
4391 // We're extracting from an overflow intrinsic. See if we're the only user.
4392 // That allows us to simplify multiple result intrinsics to simpler things
4393 // that just get one value.
4394 if (!WO->hasOneUse())
4395 return nullptr;
4396
4397 // Check if we're grabbing only the result of a 'with overflow' intrinsic
4398 // and replace it with a traditional binary instruction.
4399 if (*EV.idx_begin() == 0) {
4400 Instruction::BinaryOps BinOp = WO->getBinaryOp();
4401 Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
4402 // Replace the old instruction's uses with poison.
4403 replaceInstUsesWith(*WO, PoisonValue::get(WO->getType()));
4405 return BinaryOperator::Create(BinOp, LHS, RHS);
4406 }
4407
4408 assert(*EV.idx_begin() == 1 && "Unexpected extract index for overflow inst");
4409
4410 // (usub LHS, RHS) overflows when LHS is unsigned-less-than RHS.
4411 if (OvID == Intrinsic::usub_with_overflow)
4412 return new ICmpInst(ICmpInst::ICMP_ULT, WO->getLHS(), WO->getRHS());
4413
4414 // smul with i1 types overflows when both sides are set: -1 * -1 == +1, but
4415 // +1 is not possible because we assume signed values.
4416 if (OvID == Intrinsic::smul_with_overflow &&
4417 WO->getLHS()->getType()->isIntOrIntVectorTy(1))
4418 return BinaryOperator::CreateAnd(WO->getLHS(), WO->getRHS());
4419
4420 // extractvalue (umul_with_overflow X, X), 1 -> X u> 2^(N/2)-1
4421 if (OvID == Intrinsic::umul_with_overflow && WO->getLHS() == WO->getRHS()) {
4422 unsigned BitWidth = WO->getLHS()->getType()->getScalarSizeInBits();
4423 // Only handle even bitwidths for performance reasons.
4424 if (BitWidth % 2 == 0)
4425 return new ICmpInst(
4426 ICmpInst::ICMP_UGT, WO->getLHS(),
4427 ConstantInt::get(WO->getLHS()->getType(),
4429 }
4430
4431 // If only the overflow result is used, and the right hand side is a
4432 // constant (or constant splat), we can remove the intrinsic by directly
4433 // checking for overflow.
4434 if (C) {
4435 // Compute the no-wrap range for LHS given RHS=C, then construct an
4436 // equivalent icmp, potentially using an offset.
4438 WO->getBinaryOp(), *C, WO->getNoWrapKind());
4439
4440 CmpInst::Predicate Pred;
4441 APInt NewRHSC, Offset;
4442 NWR.getEquivalentICmp(Pred, NewRHSC, Offset);
4443 auto *OpTy = WO->getRHS()->getType();
4444 auto *NewLHS = WO->getLHS();
4445 if (Offset != 0)
4446 NewLHS = Builder.CreateAdd(NewLHS, ConstantInt::get(OpTy, Offset));
4447 return new ICmpInst(ICmpInst::getInversePredicate(Pred), NewLHS,
4448 ConstantInt::get(OpTy, NewRHSC));
4449 }
4450
4451 return nullptr;
4452}
4453
4456 InstCombiner::BuilderTy &Builder) {
4457 // Helper to fold frexp of select to select of frexp.
4458
4459 if (!SelectInst->hasOneUse() || !FrexpCall->hasOneUse())
4460 return nullptr;
4462 Value *TrueVal = SelectInst->getTrueValue();
4463 Value *FalseVal = SelectInst->getFalseValue();
4464
4465 const APFloat *ConstVal = nullptr;
4466 Value *VarOp = nullptr;
4467 bool ConstIsTrue = false;
4468
4469 if (match(TrueVal, m_APFloat(ConstVal))) {
4470 VarOp = FalseVal;
4471 ConstIsTrue = true;
4472 } else if (match(FalseVal, m_APFloat(ConstVal))) {
4473 VarOp = TrueVal;
4474 ConstIsTrue = false;
4475 } else {
4476 return nullptr;
4477 }
4478
4479 Builder.SetInsertPoint(&EV);
4480
4481 CallInst *NewFrexp =
4482 Builder.CreateCall(FrexpCall->getCalledFunction(), {VarOp}, "frexp");
4483 NewFrexp->copyIRFlags(FrexpCall);
4484
4485 Value *NewEV = Builder.CreateExtractValue(NewFrexp, 0, "mantissa");
4486
4487 int Exp;
4488 APFloat Mantissa = frexp(*ConstVal, Exp, APFloat::rmNearestTiesToEven);
4489
4490 Constant *ConstantMantissa = ConstantFP::get(TrueVal->getType(), Mantissa);
4491
4492 Value *NewSel = Builder.CreateSelectFMF(
4493 Cond, ConstIsTrue ? ConstantMantissa : NewEV,
4494 ConstIsTrue ? NewEV : ConstantMantissa, SelectInst, "select.frexp");
4495 return NewSel;
4496}
4498 Value *Agg = EV.getAggregateOperand();
4499
4500 if (!EV.hasIndices())
4501 return replaceInstUsesWith(EV, Agg);
4502
4503 if (Value *V = simplifyExtractValueInst(Agg, EV.getIndices(),
4504 SQ.getWithInstruction(&EV)))
4505 return replaceInstUsesWith(EV, V);
4506
4507 Value *Cond, *TrueVal, *FalseVal;
4508 if (match(&EV, m_ExtractValue<0>(m_Intrinsic<Intrinsic::frexp>(m_Select(
4509 m_Value(Cond), m_Value(TrueVal), m_Value(FalseVal)))))) {
4510 auto *SelInst =
4511 cast<SelectInst>(cast<IntrinsicInst>(Agg)->getArgOperand(0));
4512 if (Value *Result =
4513 foldFrexpOfSelect(EV, cast<IntrinsicInst>(Agg), SelInst, Builder))
4514 return replaceInstUsesWith(EV, Result);
4515 }
4516 if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
4517 // We're extracting from an insertvalue instruction, compare the indices
4518 const unsigned *exti, *exte, *insi, *inse;
4519 for (exti = EV.idx_begin(), insi = IV->idx_begin(),
4520 exte = EV.idx_end(), inse = IV->idx_end();
4521 exti != exte && insi != inse;
4522 ++exti, ++insi) {
4523 if (*insi != *exti)
4524 // The insert and extract both reference distinctly different elements.
4525 // This means the extract is not influenced by the insert, and we can
4526 // replace the aggregate operand of the extract with the aggregate
4527 // operand of the insert. i.e., replace
4528 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
4529 // %E = extractvalue { i32, { i32 } } %I, 0
4530 // with
4531 // %E = extractvalue { i32, { i32 } } %A, 0
4532 return ExtractValueInst::Create(IV->getAggregateOperand(),
4533 EV.getIndices());
4534 }
4535 if (exti == exte && insi == inse)
4536 // Both iterators are at the end: Index lists are identical. Replace
4537 // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
4538 // %C = extractvalue { i32, { i32 } } %B, 1, 0
4539 // with "i32 42"
4540 return replaceInstUsesWith(EV, IV->getInsertedValueOperand());
4541 if (exti == exte) {
4542 // The extract list is a prefix of the insert list. i.e. replace
4543 // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
4544 // %E = extractvalue { i32, { i32 } } %I, 1
4545 // with
4546 // %X = extractvalue { i32, { i32 } } %A, 1
4547 // %E = insertvalue { i32 } %X, i32 42, 0
4548 // by switching the order of the insert and extract (though the
4549 // insertvalue should be left in, since it may have other uses).
4550 Value *NewEV = Builder.CreateExtractValue(IV->getAggregateOperand(),
4551 EV.getIndices());
4552 return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
4553 ArrayRef(insi, inse));
4554 }
4555 if (insi == inse)
4556 // The insert list is a prefix of the extract list
4557 // We can simply remove the common indices from the extract and make it
4558 // operate on the inserted value instead of the insertvalue result.
4559 // i.e., replace
4560 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
4561 // %E = extractvalue { i32, { i32 } } %I, 1, 0
4562 // with
4563 // %E extractvalue { i32 } { i32 42 }, 0
4564 return ExtractValueInst::Create(IV->getInsertedValueOperand(),
4565 ArrayRef(exti, exte));
4566 }
4567
4568 if (Instruction *R = foldExtractOfOverflowIntrinsic(EV))
4569 return R;
4570
4571 if (LoadInst *L = dyn_cast<LoadInst>(Agg)) {
4572 // Bail out if the aggregate contains scalable vector type
4573 if (auto *STy = dyn_cast<StructType>(Agg->getType());
4574 STy && STy->isScalableTy())
4575 return nullptr;
4576
4577 // If the (non-volatile) load only has one use, we can rewrite this to a
4578 // load from a GEP. This reduces the size of the load. If a load is used
4579 // only by extractvalue instructions then this either must have been
4580 // optimized before, or it is a struct with padding, in which case we
4581 // don't want to do the transformation as it loses padding knowledge.
4582 if (L->isSimple() && L->hasOneUse()) {
4583 // extractvalue has integer indices, getelementptr has Value*s. Convert.
4584 SmallVector<Value*, 4> Indices;
4585 // Prefix an i32 0 since we need the first element.
4586 Indices.push_back(Builder.getInt32(0));
4587 for (unsigned Idx : EV.indices())
4588 Indices.push_back(Builder.getInt32(Idx));
4589
4590 // We need to insert these at the location of the old load, not at that of
4591 // the extractvalue.
4593 Value *GEP = Builder.CreateInBoundsGEP(L->getType(),
4594 L->getPointerOperand(), Indices);
4596 // Whatever aliasing information we had for the orignal load must also
4597 // hold for the smaller load, so propagate the annotations.
4598 NL->setAAMetadata(L->getAAMetadata());
4599 // Returning the load directly will cause the main loop to insert it in
4600 // the wrong spot, so use replaceInstUsesWith().
4601 return replaceInstUsesWith(EV, NL);
4602 }
4603 }
4604
4605 if (auto *PN = dyn_cast<PHINode>(Agg))
4606 if (Instruction *Res = foldOpIntoPhi(EV, PN))
4607 return Res;
4608
4609 // Canonicalize extract (select Cond, TV, FV)
4610 // -> select cond, (extract TV), (extract FV)
4611 if (auto *SI = dyn_cast<SelectInst>(Agg))
4612 if (Instruction *R = FoldOpIntoSelect(EV, SI, /*FoldWithMultiUse=*/true))
4613 return R;
4614
4615 // We could simplify extracts from other values. Note that nested extracts may
4616 // already be simplified implicitly by the above: extract (extract (insert) )
4617 // will be translated into extract ( insert ( extract ) ) first and then just
4618 // the value inserted, if appropriate. Similarly for extracts from single-use
4619 // loads: extract (extract (load)) will be translated to extract (load (gep))
4620 // and if again single-use then via load (gep (gep)) to load (gep).
4621 // However, double extracts from e.g. function arguments or return values
4622 // aren't handled yet.
4623 return nullptr;
4624}
4625
4626/// Return 'true' if the given typeinfo will match anything.
4627static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo) {
4628 switch (Personality) {
4632 // The GCC C EH and Rust personality only exists to support cleanups, so
4633 // it's not clear what the semantics of catch clauses are.
4634 return false;
4636 return false;
4638 // While __gnat_all_others_value will match any Ada exception, it doesn't
4639 // match foreign exceptions (or didn't, before gcc-4.7).
4640 return false;
4651 return TypeInfo->isNullValue();
4652 }
4653 llvm_unreachable("invalid enum");
4654}
4655
4656static bool shorter_filter(const Value *LHS, const Value *RHS) {
4657 return
4658 cast<ArrayType>(LHS->getType())->getNumElements()
4659 <
4660 cast<ArrayType>(RHS->getType())->getNumElements();
4661}
4662
4664 // The logic here should be correct for any real-world personality function.
4665 // However if that turns out not to be true, the offending logic can always
4666 // be conditioned on the personality function, like the catch-all logic is.
4667 EHPersonality Personality =
4668 classifyEHPersonality(LI.getParent()->getParent()->getPersonalityFn());
4669
4670 // Simplify the list of clauses, eg by removing repeated catch clauses
4671 // (these are often created by inlining).
4672 bool MakeNewInstruction = false; // If true, recreate using the following:
4673 SmallVector<Constant *, 16> NewClauses; // - Clauses for the new instruction;
4674 bool CleanupFlag = LI.isCleanup(); // - The new instruction is a cleanup.
4675
4676 SmallPtrSet<Value *, 16> AlreadyCaught; // Typeinfos known caught already.
4677 for (unsigned i = 0, e = LI.getNumClauses(); i != e; ++i) {
4678 bool isLastClause = i + 1 == e;
4679 if (LI.isCatch(i)) {
4680 // A catch clause.
4681 Constant *CatchClause = LI.getClause(i);
4682 Constant *TypeInfo = CatchClause->stripPointerCasts();
4683
4684 // If we already saw this clause, there is no point in having a second
4685 // copy of it.
4686 if (AlreadyCaught.insert(TypeInfo).second) {
4687 // This catch clause was not already seen.
4688 NewClauses.push_back(CatchClause);
4689 } else {
4690 // Repeated catch clause - drop the redundant copy.
4691 MakeNewInstruction = true;
4692 }
4693
4694 // If this is a catch-all then there is no point in keeping any following
4695 // clauses or marking the landingpad as having a cleanup.
4696 if (isCatchAll(Personality, TypeInfo)) {
4697 if (!isLastClause)
4698 MakeNewInstruction = true;
4699 CleanupFlag = false;
4700 break;
4701 }
4702 } else {
4703 // A filter clause. If any of the filter elements were already caught
4704 // then they can be dropped from the filter. It is tempting to try to
4705 // exploit the filter further by saying that any typeinfo that does not
4706 // occur in the filter can't be caught later (and thus can be dropped).
4707 // However this would be wrong, since typeinfos can match without being
4708 // equal (for example if one represents a C++ class, and the other some
4709 // class derived from it).
4710 assert(LI.isFilter(i) && "Unsupported landingpad clause!");
4711 Constant *FilterClause = LI.getClause(i);
4712 ArrayType *FilterType = cast<ArrayType>(FilterClause->getType());
4713 unsigned NumTypeInfos = FilterType->getNumElements();
4714
4715 // An empty filter catches everything, so there is no point in keeping any
4716 // following clauses or marking the landingpad as having a cleanup. By
4717 // dealing with this case here the following code is made a bit simpler.
4718 if (!NumTypeInfos) {
4719 NewClauses.push_back(FilterClause);
4720 if (!isLastClause)
4721 MakeNewInstruction = true;
4722 CleanupFlag = false;
4723 break;
4724 }
4725
4726 bool MakeNewFilter = false; // If true, make a new filter.
4727 SmallVector<Constant *, 16> NewFilterElts; // New elements.
4728 if (isa<ConstantAggregateZero>(FilterClause)) {
4729 // Not an empty filter - it contains at least one null typeinfo.
4730 assert(NumTypeInfos > 0 && "Should have handled empty filter already!");
4731 Constant *TypeInfo =
4733 // If this typeinfo is a catch-all then the filter can never match.
4734 if (isCatchAll(Personality, TypeInfo)) {
4735 // Throw the filter away.
4736 MakeNewInstruction = true;
4737 continue;
4738 }
4739
4740 // There is no point in having multiple copies of this typeinfo, so
4741 // discard all but the first copy if there is more than one.
4742 NewFilterElts.push_back(TypeInfo);
4743 if (NumTypeInfos > 1)
4744 MakeNewFilter = true;
4745 } else {
4746 ConstantArray *Filter = cast<ConstantArray>(FilterClause);
4747 SmallPtrSet<Value *, 16> SeenInFilter; // For uniquing the elements.
4748 NewFilterElts.reserve(NumTypeInfos);
4749
4750 // Remove any filter elements that were already caught or that already
4751 // occurred in the filter. While there, see if any of the elements are
4752 // catch-alls. If so, the filter can be discarded.
4753 bool SawCatchAll = false;
4754 for (unsigned j = 0; j != NumTypeInfos; ++j) {
4755 Constant *Elt = Filter->getOperand(j);
4756 Constant *TypeInfo = Elt->stripPointerCasts();
4757 if (isCatchAll(Personality, TypeInfo)) {
4758 // This element is a catch-all. Bail out, noting this fact.
4759 SawCatchAll = true;
4760 break;
4761 }
4762
4763 // Even if we've seen a type in a catch clause, we don't want to
4764 // remove it from the filter. An unexpected type handler may be
4765 // set up for a call site which throws an exception of the same
4766 // type caught. In order for the exception thrown by the unexpected
4767 // handler to propagate correctly, the filter must be correctly
4768 // described for the call site.
4769 //
4770 // Example:
4771 //
4772 // void unexpected() { throw 1;}
4773 // void foo() throw (int) {
4774 // std::set_unexpected(unexpected);
4775 // try {
4776 // throw 2.0;
4777 // } catch (int i) {}
4778 // }
4779
4780 // There is no point in having multiple copies of the same typeinfo in
4781 // a filter, so only add it if we didn't already.
4782 if (SeenInFilter.insert(TypeInfo).second)
4783 NewFilterElts.push_back(cast<Constant>(Elt));
4784 }
4785 // A filter containing a catch-all cannot match anything by definition.
4786 if (SawCatchAll) {
4787 // Throw the filter away.
4788 MakeNewInstruction = true;
4789 continue;
4790 }
4791
4792 // If we dropped something from the filter, make a new one.
4793 if (NewFilterElts.size() < NumTypeInfos)
4794 MakeNewFilter = true;
4795 }
4796 if (MakeNewFilter) {
4797 FilterType = ArrayType::get(FilterType->getElementType(),
4798 NewFilterElts.size());
4799 FilterClause = ConstantArray::get(FilterType, NewFilterElts);
4800 MakeNewInstruction = true;
4801 }
4802
4803 NewClauses.push_back(FilterClause);
4804
4805 // If the new filter is empty then it will catch everything so there is
4806 // no point in keeping any following clauses or marking the landingpad
4807 // as having a cleanup. The case of the original filter being empty was
4808 // already handled above.
4809 if (MakeNewFilter && !NewFilterElts.size()) {
4810 assert(MakeNewInstruction && "New filter but not a new instruction!");
4811 CleanupFlag = false;
4812 break;
4813 }
4814 }
4815 }
4816
4817 // If several filters occur in a row then reorder them so that the shortest
4818 // filters come first (those with the smallest number of elements). This is
4819 // advantageous because shorter filters are more likely to match, speeding up
4820 // unwinding, but mostly because it increases the effectiveness of the other
4821 // filter optimizations below.
4822 for (unsigned i = 0, e = NewClauses.size(); i + 1 < e; ) {
4823 unsigned j;
4824 // Find the maximal 'j' s.t. the range [i, j) consists entirely of filters.
4825 for (j = i; j != e; ++j)
4826 if (!isa<ArrayType>(NewClauses[j]->getType()))
4827 break;
4828
4829 // Check whether the filters are already sorted by length. We need to know
4830 // if sorting them is actually going to do anything so that we only make a
4831 // new landingpad instruction if it does.
4832 for (unsigned k = i; k + 1 < j; ++k)
4833 if (shorter_filter(NewClauses[k+1], NewClauses[k])) {
4834 // Not sorted, so sort the filters now. Doing an unstable sort would be
4835 // correct too but reordering filters pointlessly might confuse users.
4836 std::stable_sort(NewClauses.begin() + i, NewClauses.begin() + j,
4838 MakeNewInstruction = true;
4839 break;
4840 }
4841
4842 // Look for the next batch of filters.
4843 i = j + 1;
4844 }
4845
4846 // If typeinfos matched if and only if equal, then the elements of a filter L
4847 // that occurs later than a filter F could be replaced by the intersection of
4848 // the elements of F and L. In reality two typeinfos can match without being
4849 // equal (for example if one represents a C++ class, and the other some class
4850 // derived from it) so it would be wrong to perform this transform in general.
4851 // However the transform is correct and useful if F is a subset of L. In that
4852 // case L can be replaced by F, and thus removed altogether since repeating a
4853 // filter is pointless. So here we look at all pairs of filters F and L where
4854 // L follows F in the list of clauses, and remove L if every element of F is
4855 // an element of L. This can occur when inlining C++ functions with exception
4856 // specifications.
4857 for (unsigned i = 0; i + 1 < NewClauses.size(); ++i) {
4858 // Examine each filter in turn.
4859 Value *Filter = NewClauses[i];
4860 ArrayType *FTy = dyn_cast<ArrayType>(Filter->getType());
4861 if (!FTy)
4862 // Not a filter - skip it.
4863 continue;
4864 unsigned FElts = FTy->getNumElements();
4865 // Examine each filter following this one. Doing this backwards means that
4866 // we don't have to worry about filters disappearing under us when removed.
4867 for (unsigned j = NewClauses.size() - 1; j != i; --j) {
4868 Value *LFilter = NewClauses[j];
4869 ArrayType *LTy = dyn_cast<ArrayType>(LFilter->getType());
4870 if (!LTy)
4871 // Not a filter - skip it.
4872 continue;
4873 // If Filter is a subset of LFilter, i.e. every element of Filter is also
4874 // an element of LFilter, then discard LFilter.
4875 SmallVectorImpl<Constant *>::iterator J = NewClauses.begin() + j;
4876 // If Filter is empty then it is a subset of LFilter.
4877 if (!FElts) {
4878 // Discard LFilter.
4879 NewClauses.erase(J);
4880 MakeNewInstruction = true;
4881 // Move on to the next filter.
4882 continue;
4883 }
4884 unsigned LElts = LTy->getNumElements();
4885 // If Filter is longer than LFilter then it cannot be a subset of it.
4886 if (FElts > LElts)
4887 // Move on to the next filter.
4888 continue;
4889 // At this point we know that LFilter has at least one element.
4890 if (isa<ConstantAggregateZero>(LFilter)) { // LFilter only contains zeros.
4891 // Filter is a subset of LFilter iff Filter contains only zeros (as we
4892 // already know that Filter is not longer than LFilter).
4893 if (isa<ConstantAggregateZero>(Filter)) {
4894 assert(FElts <= LElts && "Should have handled this case earlier!");
4895 // Discard LFilter.
4896 NewClauses.erase(J);
4897 MakeNewInstruction = true;
4898 }
4899 // Move on to the next filter.
4900 continue;
4901 }
4902 ConstantArray *LArray = cast<ConstantArray>(LFilter);
4903 if (isa<ConstantAggregateZero>(Filter)) { // Filter only contains zeros.
4904 // Since Filter is non-empty and contains only zeros, it is a subset of
4905 // LFilter iff LFilter contains a zero.
4906 assert(FElts > 0 && "Should have eliminated the empty filter earlier!");
4907 for (unsigned l = 0; l != LElts; ++l)
4908 if (LArray->getOperand(l)->isNullValue()) {
4909 // LFilter contains a zero - discard it.
4910 NewClauses.erase(J);
4911 MakeNewInstruction = true;
4912 break;
4913 }
4914 // Move on to the next filter.
4915 continue;
4916 }
4917 // At this point we know that both filters are ConstantArrays. Loop over
4918 // operands to see whether every element of Filter is also an element of
4919 // LFilter. Since filters tend to be short this is probably faster than
4920 // using a method that scales nicely.
4921 ConstantArray *FArray = cast<ConstantArray>(Filter);
4922 bool AllFound = true;
4923 for (unsigned f = 0; f != FElts; ++f) {
4924 Value *FTypeInfo = FArray->getOperand(f)->stripPointerCasts();
4925 AllFound = false;
4926 for (unsigned l = 0; l != LElts; ++l) {
4927 Value *LTypeInfo = LArray->getOperand(l)->stripPointerCasts();
4928 if (LTypeInfo == FTypeInfo) {
4929 AllFound = true;
4930 break;
4931 }
4932 }
4933 if (!AllFound)
4934 break;
4935 }
4936 if (AllFound) {
4937 // Discard LFilter.
4938 NewClauses.erase(J);
4939 MakeNewInstruction = true;
4940 }
4941 // Move on to the next filter.
4942 }
4943 }
4944
4945 // If we changed any of the clauses, replace the old landingpad instruction
4946 // with a new one.
4947 if (MakeNewInstruction) {
4949 NewClauses.size());
4950 for (Constant *C : NewClauses)
4951 NLI->addClause(C);
4952 // A landing pad with no clauses must have the cleanup flag set. It is
4953 // theoretically possible, though highly unlikely, that we eliminated all
4954 // clauses. If so, force the cleanup flag to true.
4955 if (NewClauses.empty())
4956 CleanupFlag = true;
4957 NLI->setCleanup(CleanupFlag);
4958 return NLI;
4959 }
4960
4961 // Even if none of the clauses changed, we may nonetheless have understood
4962 // that the cleanup flag is pointless. Clear it if so.
4963 if (LI.isCleanup() != CleanupFlag) {
4964 assert(!CleanupFlag && "Adding a cleanup, not removing one?!");
4965 LI.setCleanup(CleanupFlag);
4966 return &LI;
4967 }
4968
4969 return nullptr;
4970}
4971
4972Value *
4974 // Try to push freeze through instructions that propagate but don't produce
4975 // poison as far as possible. If an operand of freeze does not produce poison
4976 // then push the freeze through to the operands that are not guaranteed
4977 // non-poison. The actual transform is as follows.
4978 // Op1 = ... ; Op1 can be poison
4979 // Op0 = Inst(Op1, NonPoisonOps...)
4980 // ... = Freeze(Op0)
4981 // =>
4982 // Op1 = ...
4983 // Op1.fr = Freeze(Op1)
4984 // ... = Inst(Op1.fr, NonPoisonOps...)
4985
4986 auto CanPushFreeze = [](Value *V) {
4987 if (!isa<Instruction>(V) || isa<PHINode>(V))
4988 return false;
4989
4990 // We can't push the freeze through an instruction which can itself create
4991 // poison. If the only source of new poison is flags, we can simply
4992 // strip them (since we know the only use is the freeze and nothing can
4993 // benefit from them.)
4994 return !canCreateUndefOrPoison(cast<Operator>(V),
4995 /*ConsiderFlagsAndMetadata*/ false);
4996 };
4997
4998 // Pushing freezes up long instruction chains can be expensive. Instead,
4999 // we directly push the freeze all the way to the leaves. However, we leave
5000 // deduplication of freezes on the same value for freezeOtherUses().
5001 Use *OrigUse = &OrigFI.getOperandUse(0);
5004 Worklist.push_back(OrigUse);
5005 while (!Worklist.empty()) {
5006 auto *U = Worklist.pop_back_val();
5007 Value *V = U->get();
5008 if (!CanPushFreeze(V)) {
5009 // If we can't push through the original instruction, abort the transform.
5010 if (U == OrigUse)
5011 return nullptr;
5012
5013 auto *UserI = cast<Instruction>(U->getUser());
5014 Builder.SetInsertPoint(UserI);
5015 Value *Frozen = Builder.CreateFreeze(V, V->getName() + ".fr");
5016 U->set(Frozen);
5017 continue;
5018 }
5019
5020 auto *I = cast<Instruction>(V);
5021 if (!Visited.insert(I).second)
5022 continue;
5023
5024 // reverse() to emit freezes in a more natural order.
5025 for (Use &Op : reverse(I->operands())) {
5026 Value *OpV = Op.get();
5027 if (isa<MetadataAsValue>(OpV) || isGuaranteedNotToBeUndefOrPoison(OpV))
5028 continue;
5029 Worklist.push_back(&Op);
5030 }
5031
5032 I->dropPoisonGeneratingAnnotations();
5033 this->Worklist.add(I);
5034 }
5035
5036 return OrigUse->get();
5037}
5038
5040 PHINode *PN) {
5041 // Detect whether this is a recurrence with a start value and some number of
5042 // backedge values. We'll check whether we can push the freeze through the
5043 // backedge values (possibly dropping poison flags along the way) until we
5044 // reach the phi again. In that case, we can move the freeze to the start
5045 // value.
5046 Use *StartU = nullptr;
5048 for (Use &U : PN->incoming_values()) {
5049 if (DT.dominates(PN->getParent(), PN->getIncomingBlock(U))) {
5050 // Add backedge value to worklist.
5051 Worklist.push_back(U.get());
5052 continue;
5053 }
5054
5055 // Don't bother handling multiple start values.
5056 if (StartU)
5057 return nullptr;
5058 StartU = &U;
5059 }
5060
5061 if (!StartU || Worklist.empty())
5062 return nullptr; // Not a recurrence.
5063
5064 Value *StartV = StartU->get();
5065 BasicBlock *StartBB = PN->getIncomingBlock(*StartU);
5066 bool StartNeedsFreeze = !isGuaranteedNotToBeUndefOrPoison(StartV);
5067 // We can't insert freeze if the start value is the result of the
5068 // terminator (e.g. an invoke).
5069 if (StartNeedsFreeze && StartBB->getTerminator() == StartV)
5070 return nullptr;
5071
5074 while (!Worklist.empty()) {
5075 Value *V = Worklist.pop_back_val();
5076 if (!Visited.insert(V).second)
5077 continue;
5078
5079 if (Visited.size() > 32)
5080 return nullptr; // Limit the total number of values we inspect.
5081
5082 // Assume that PN is non-poison, because it will be after the transform.
5083 if (V == PN || isGuaranteedNotToBeUndefOrPoison(V))
5084 continue;
5085
5086 Instruction *I = dyn_cast<Instruction>(V);
5087 if (!I || canCreateUndefOrPoison(cast<Operator>(I),
5088 /*ConsiderFlagsAndMetadata*/ false))
5089 return nullptr;
5090
5091 DropFlags.push_back(I);
5092 append_range(Worklist, I->operands());
5093 }
5094
5095 for (Instruction *I : DropFlags)
5096 I->dropPoisonGeneratingAnnotations();
5097
5098 if (StartNeedsFreeze) {
5100 Value *FrozenStartV = Builder.CreateFreeze(StartV,
5101 StartV->getName() + ".fr");
5102 replaceUse(*StartU, FrozenStartV);
5103 }
5104 return replaceInstUsesWith(FI, PN);
5105}
5106
5108 Value *Op = FI.getOperand(0);
5109
5110 if (isa<Constant>(Op) || Op->hasOneUse())
5111 return false;
5112
5113 // Move the freeze directly after the definition of its operand, so that
5114 // it dominates the maximum number of uses. Note that it may not dominate
5115 // *all* uses if the operand is an invoke/callbr and the use is in a phi on
5116 // the normal/default destination. This is why the domination check in the
5117 // replacement below is still necessary.
5118 BasicBlock::iterator MoveBefore;
5119 if (isa<Argument>(Op)) {
5120 MoveBefore =
5122 } else {
5123 auto MoveBeforeOpt = cast<Instruction>(Op)->getInsertionPointAfterDef();
5124 if (!MoveBeforeOpt)
5125 return false;
5126 MoveBefore = *MoveBeforeOpt;
5127 }
5128
5129 // Re-point iterator to come after any debug-info records.
5130 MoveBefore.setHeadBit(false);
5131
5132 bool Changed = false;
5133 if (&FI != &*MoveBefore) {
5134 FI.moveBefore(*MoveBefore->getParent(), MoveBefore);
5135 Changed = true;
5136 }
5137
5138 Op->replaceUsesWithIf(&FI, [&](Use &U) -> bool {
5139 bool Dominates = DT.dominates(&FI, U);
5140 Changed |= Dominates;
5141 return Dominates;
5142 });
5143
5144 return Changed;
5145}
5146
5147// Check if any direct or bitcast user of this value is a shuffle instruction.
5149 for (auto *U : V->users()) {
5150 if (isa<ShuffleVectorInst>(U))
5151 return true;
5152 else if (match(U, m_BitCast(m_Specific(V))) && isUsedWithinShuffleVector(U))
5153 return true;
5154 }
5155 return false;
5156}
5157
5159 Value *Op0 = I.getOperand(0);
5160
5162 return replaceInstUsesWith(I, V);
5163
5164 // freeze (phi const, x) --> phi const, (freeze x)
5165 if (auto *PN = dyn_cast<PHINode>(Op0)) {
5166 if (Instruction *NV = foldOpIntoPhi(I, PN))
5167 return NV;
5168 if (Instruction *NV = foldFreezeIntoRecurrence(I, PN))
5169 return NV;
5170 }
5171
5173 return replaceInstUsesWith(I, NI);
5174
5175 // If I is freeze(undef), check its uses and fold it to a fixed constant.
5176 // - or: pick -1
5177 // - select's condition: if the true value is constant, choose it by making
5178 // the condition true.
5179 // - default: pick 0
5180 //
5181 // Note that this transform is intentionally done here rather than
5182 // via an analysis in InstSimplify or at individual user sites. That is
5183 // because we must produce the same value for all uses of the freeze -
5184 // it's the reason "freeze" exists!
5185 //
5186 // TODO: This could use getBinopAbsorber() / getBinopIdentity() to avoid
5187 // duplicating logic for binops at least.
5188 auto getUndefReplacement = [&](Type *Ty) {
5189 Value *BestValue = nullptr;
5190 Value *NullValue = Constant::getNullValue(Ty);
5191 for (const auto *U : I.users()) {
5192 Value *V = NullValue;
5193 if (match(U, m_Or(m_Value(), m_Value())))
5195 else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
5196 V = ConstantInt::getTrue(Ty);
5197 else if (match(U, m_c_Select(m_Specific(&I), m_Value(V)))) {
5199 V = NullValue;
5200 }
5201
5202 if (!BestValue)
5203 BestValue = V;
5204 else if (BestValue != V)
5205 BestValue = NullValue;
5206 }
5207 assert(BestValue && "Must have at least one use");
5208 return BestValue;
5209 };
5210
5211 if (match(Op0, m_Undef())) {
5212 // Don't fold freeze(undef/poison) if it's used as a vector operand in
5213 // a shuffle. This may improve codegen for shuffles that allow
5214 // unspecified inputs.
5216 return nullptr;
5217 return replaceInstUsesWith(I, getUndefReplacement(I.getType()));
5218 }
5219
5220 auto getFreezeVectorReplacement = [](Constant *C) -> Constant * {
5221 Type *Ty = C->getType();
5222 auto *VTy = dyn_cast<FixedVectorType>(Ty);
5223 if (!VTy)
5224 return nullptr;
5225 unsigned NumElts = VTy->getNumElements();
5226 Constant *BestValue = Constant::getNullValue(VTy->getScalarType());
5227 for (unsigned i = 0; i != NumElts; ++i) {
5228 Constant *EltC = C->getAggregateElement(i);
5229 if (EltC && !match(EltC, m_Undef())) {
5230 BestValue = EltC;
5231 break;
5232 }
5233 }
5234 return Constant::replaceUndefsWith(C, BestValue);
5235 };
5236
5237 Constant *C;
5238 if (match(Op0, m_Constant(C)) && C->containsUndefOrPoisonElement() &&
5239 !C->containsConstantExpression()) {
5240 if (Constant *Repl = getFreezeVectorReplacement(C))
5241 return replaceInstUsesWith(I, Repl);
5242 }
5243
5244 // Replace uses of Op with freeze(Op).
5245 if (freezeOtherUses(I))
5246 return &I;
5247
5248 return nullptr;
5249}
5250
5251/// Check for case where the call writes to an otherwise dead alloca. This
5252/// shows up for unused out-params in idiomatic C/C++ code. Note that this
5253/// helper *only* analyzes the write; doesn't check any other legality aspect.
5255 auto *CB = dyn_cast<CallBase>(I);
5256 if (!CB)
5257 // TODO: handle e.g. store to alloca here - only worth doing if we extend
5258 // to allow reload along used path as described below. Otherwise, this
5259 // is simply a store to a dead allocation which will be removed.
5260 return false;
5261 std::optional<MemoryLocation> Dest = MemoryLocation::getForDest(CB, TLI);
5262 if (!Dest)
5263 return false;
5264 auto *AI = dyn_cast<AllocaInst>(getUnderlyingObject(Dest->Ptr));
5265 if (!AI)
5266 // TODO: allow malloc?
5267 return false;
5268 // TODO: allow memory access dominated by move point? Note that since AI
5269 // could have a reference to itself captured by the call, we would need to
5270 // account for cycles in doing so.
5271 SmallVector<const User *> AllocaUsers;
5273 auto pushUsers = [&](const Instruction &I) {
5274 for (const User *U : I.users()) {
5275 if (Visited.insert(U).second)
5276 AllocaUsers.push_back(U);
5277 }
5278 };
5279 pushUsers(*AI);
5280 while (!AllocaUsers.empty()) {
5281 auto *UserI = cast<Instruction>(AllocaUsers.pop_back_val());
5282 if (isa<GetElementPtrInst>(UserI) || isa<AddrSpaceCastInst>(UserI)) {
5283 pushUsers(*UserI);
5284 continue;
5285 }
5286 if (UserI == CB)
5287 continue;
5288 // TODO: support lifetime.start/end here
5289 return false;
5290 }
5291 return true;
5292}
5293
5294/// Try to move the specified instruction from its current block into the
5295/// beginning of DestBlock, which can only happen if it's safe to move the
5296/// instruction past all of the instructions between it and the end of its
5297/// block.
5299 BasicBlock *DestBlock) {
5300 BasicBlock *SrcBlock = I->getParent();
5301
5302 // Cannot move control-flow-involving, volatile loads, vaarg, etc.
5303 if (isa<PHINode>(I) || I->isEHPad() || I->mayThrow() || !I->willReturn() ||
5304 I->isTerminator())
5305 return false;
5306
5307 // Do not sink static or dynamic alloca instructions. Static allocas must
5308 // remain in the entry block, and dynamic allocas must not be sunk in between
5309 // a stacksave / stackrestore pair, which would incorrectly shorten its
5310 // lifetime.
5311 if (isa<AllocaInst>(I))
5312 return false;
5313
5314 // Do not sink into catchswitch blocks.
5315 if (isa<CatchSwitchInst>(DestBlock->getTerminator()))
5316 return false;
5317
5318 // Do not sink convergent call instructions.
5319 if (auto *CI = dyn_cast<CallInst>(I)) {
5320 if (CI->isConvergent())
5321 return false;
5322 }
5323
5324 // Unless we can prove that the memory write isn't visibile except on the
5325 // path we're sinking to, we must bail.
5326 if (I->mayWriteToMemory()) {
5327 if (!SoleWriteToDeadLocal(I, TLI))
5328 return false;
5329 }
5330
5331 // We can only sink load instructions if there is nothing between the load and
5332 // the end of block that could change the value.
5333 if (I->mayReadFromMemory() &&
5334 !I->hasMetadata(LLVMContext::MD_invariant_load)) {
5335 // We don't want to do any sophisticated alias analysis, so we only check
5336 // the instructions after I in I's parent block if we try to sink to its
5337 // successor block.
5338 if (DestBlock->getUniquePredecessor() != I->getParent())
5339 return false;
5340 for (BasicBlock::iterator Scan = std::next(I->getIterator()),
5341 E = I->getParent()->end();
5342 Scan != E; ++Scan)
5343 if (Scan->mayWriteToMemory())
5344 return false;
5345 }
5346
5347 I->dropDroppableUses([&](const Use *U) {
5348 auto *I = dyn_cast<Instruction>(U->getUser());
5349 if (I && I->getParent() != DestBlock) {
5350 Worklist.add(I);
5351 return true;
5352 }
5353 return false;
5354 });
5355 /// FIXME: We could remove droppable uses that are not dominated by
5356 /// the new position.
5357
5358 BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt();
5359 I->moveBefore(*DestBlock, InsertPos);
5360 ++NumSunkInst;
5361
5362 // Also sink all related debug uses from the source basic block. Otherwise we
5363 // get debug use before the def. Attempt to salvage debug uses first, to
5364 // maximise the range variables have location for. If we cannot salvage, then
5365 // mark the location undef: we know it was supposed to receive a new location
5366 // here, but that computation has been sunk.
5367 SmallVector<DbgVariableRecord *, 2> DbgVariableRecords;
5368 findDbgUsers(I, DbgVariableRecords);
5369 if (!DbgVariableRecords.empty())
5370 tryToSinkInstructionDbgVariableRecords(I, InsertPos, SrcBlock, DestBlock,
5371 DbgVariableRecords);
5372
5373 // PS: there are numerous flaws with this behaviour, not least that right now
5374 // assignments can be re-ordered past other assignments to the same variable
5375 // if they use different Values. Creating more undef assignements can never be
5376 // undone. And salvaging all users outside of this block can un-necessarily
5377 // alter the lifetime of the live-value that the variable refers to.
5378 // Some of these things can be resolved by tolerating debug use-before-defs in
5379 // LLVM-IR, however it depends on the instruction-referencing CodeGen backend
5380 // being used for more architectures.
5381
5382 return true;
5383}
5384
5386 Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock,
5387 BasicBlock *DestBlock,
5388 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords) {
5389 // For all debug values in the destination block, the sunk instruction
5390 // will still be available, so they do not need to be dropped.
5391
5392 // Fetch all DbgVariableRecords not already in the destination.
5393 SmallVector<DbgVariableRecord *, 2> DbgVariableRecordsToSalvage;
5394 for (auto &DVR : DbgVariableRecords)
5395 if (DVR->getParent() != DestBlock)
5396 DbgVariableRecordsToSalvage.push_back(DVR);
5397
5398 // Fetch a second collection, of DbgVariableRecords in the source block that
5399 // we're going to sink.
5400 SmallVector<DbgVariableRecord *> DbgVariableRecordsToSink;
5401 for (DbgVariableRecord *DVR : DbgVariableRecordsToSalvage)
5402 if (DVR->getParent() == SrcBlock)
5403 DbgVariableRecordsToSink.push_back(DVR);
5404
5405 // Sort DbgVariableRecords according to their position in the block. This is a
5406 // partial order: DbgVariableRecords attached to different instructions will
5407 // be ordered by the instruction order, but DbgVariableRecords attached to the
5408 // same instruction won't have an order.
5409 auto Order = [](DbgVariableRecord *A, DbgVariableRecord *B) -> bool {
5410 return B->getInstruction()->comesBefore(A->getInstruction());
5411 };
5412 llvm::stable_sort(DbgVariableRecordsToSink, Order);
5413
5414 // If there are two assignments to the same variable attached to the same
5415 // instruction, the ordering between the two assignments is important. Scan
5416 // for this (rare) case and establish which is the last assignment.
5417 using InstVarPair = std::pair<const Instruction *, DebugVariable>;
5419 if (DbgVariableRecordsToSink.size() > 1) {
5421 // Count how many assignments to each variable there is per instruction.
5422 for (DbgVariableRecord *DVR : DbgVariableRecordsToSink) {
5423 DebugVariable DbgUserVariable =
5424 DebugVariable(DVR->getVariable(), DVR->getExpression(),
5425 DVR->getDebugLoc()->getInlinedAt());
5426 CountMap[std::make_pair(DVR->getInstruction(), DbgUserVariable)] += 1;
5427 }
5428
5429 // If there are any instructions with two assignments, add them to the
5430 // FilterOutMap to record that they need extra filtering.
5432 for (auto It : CountMap) {
5433 if (It.second > 1) {
5434 FilterOutMap[It.first] = nullptr;
5435 DupSet.insert(It.first.first);
5436 }
5437 }
5438
5439 // For all instruction/variable pairs needing extra filtering, find the
5440 // latest assignment.
5441 for (const Instruction *Inst : DupSet) {
5442 for (DbgVariableRecord &DVR :
5443 llvm::reverse(filterDbgVars(Inst->getDbgRecordRange()))) {
5444 DebugVariable DbgUserVariable =
5445 DebugVariable(DVR.getVariable(), DVR.getExpression(),
5446 DVR.getDebugLoc()->getInlinedAt());
5447 auto FilterIt =
5448 FilterOutMap.find(std::make_pair(Inst, DbgUserVariable));
5449 if (FilterIt == FilterOutMap.end())
5450 continue;
5451 if (FilterIt->second != nullptr)
5452 continue;
5453 FilterIt->second = &DVR;
5454 }
5455 }
5456 }
5457
5458 // Perform cloning of the DbgVariableRecords that we plan on sinking, filter
5459 // out any duplicate assignments identified above.
5461 SmallSet<DebugVariable, 4> SunkVariables;
5462 for (DbgVariableRecord *DVR : DbgVariableRecordsToSink) {
5464 continue;
5465
5466 DebugVariable DbgUserVariable =
5467 DebugVariable(DVR->getVariable(), DVR->getExpression(),
5468 DVR->getDebugLoc()->getInlinedAt());
5469
5470 // For any variable where there were multiple assignments in the same place,
5471 // ignore all but the last assignment.
5472 if (!FilterOutMap.empty()) {
5473 InstVarPair IVP = std::make_pair(DVR->getInstruction(), DbgUserVariable);
5474 auto It = FilterOutMap.find(IVP);
5475
5476 // Filter out.
5477 if (It != FilterOutMap.end() && It->second != DVR)
5478 continue;
5479 }
5480
5481 if (!SunkVariables.insert(DbgUserVariable).second)
5482 continue;
5483
5484 if (DVR->isDbgAssign())
5485 continue;
5486
5487 DVRClones.emplace_back(DVR->clone());
5488 LLVM_DEBUG(dbgs() << "CLONE: " << *DVRClones.back() << '\n');
5489 }
5490
5491 // Perform salvaging without the clones, then sink the clones.
5492 if (DVRClones.empty())
5493 return;
5494
5495 salvageDebugInfoForDbgValues(*I, DbgVariableRecordsToSalvage);
5496
5497 // The clones are in reverse order of original appearance. Assert that the
5498 // head bit is set on the iterator as we _should_ have received it via
5499 // getFirstInsertionPt. Inserting like this will reverse the clone order as
5500 // we'll repeatedly insert at the head, such as:
5501 // DVR-3 (third insertion goes here)
5502 // DVR-2 (second insertion goes here)
5503 // DVR-1 (first insertion goes here)
5504 // Any-Prior-DVRs
5505 // InsertPtInst
5506 assert(InsertPos.getHeadBit());
5507 for (DbgVariableRecord *DVRClone : DVRClones) {
5508 InsertPos->getParent()->insertDbgRecordBefore(DVRClone, InsertPos);
5509 LLVM_DEBUG(dbgs() << "SINK: " << *DVRClone << '\n');
5510 }
5511}
5512
5514 while (!Worklist.isEmpty()) {
5515 // Walk deferred instructions in reverse order, and push them to the
5516 // worklist, which means they'll end up popped from the worklist in-order.
5517 while (Instruction *I = Worklist.popDeferred()) {
5518 // Check to see if we can DCE the instruction. We do this already here to
5519 // reduce the number of uses and thus allow other folds to trigger.
5520 // Note that eraseInstFromFunction() may push additional instructions on
5521 // the deferred worklist, so this will DCE whole instruction chains.
5524 ++NumDeadInst;
5525 continue;
5526 }
5527
5528 Worklist.push(I);
5529 }
5530
5532 if (I == nullptr) continue; // skip null values.
5533
5534 // Check to see if we can DCE the instruction.
5537 ++NumDeadInst;
5538 continue;
5539 }
5540
5541 if (!DebugCounter::shouldExecute(VisitCounter))
5542 continue;
5543
5544 // See if we can trivially sink this instruction to its user if we can
5545 // prove that the successor is not executed more frequently than our block.
5546 // Return the UserBlock if successful.
5547 auto getOptionalSinkBlockForInst =
5548 [this](Instruction *I) -> std::optional<BasicBlock *> {
5549 if (!EnableCodeSinking)
5550 return std::nullopt;
5551
5552 BasicBlock *BB = I->getParent();
5553 BasicBlock *UserParent = nullptr;
5554 unsigned NumUsers = 0;
5555
5556 for (Use &U : I->uses()) {
5557 User *User = U.getUser();
5558 if (User->isDroppable())
5559 continue;
5560 if (NumUsers > MaxSinkNumUsers)
5561 return std::nullopt;
5562
5563 Instruction *UserInst = cast<Instruction>(User);
5564 // Special handling for Phi nodes - get the block the use occurs in.
5565 BasicBlock *UserBB = UserInst->getParent();
5566 if (PHINode *PN = dyn_cast<PHINode>(UserInst))
5567 UserBB = PN->getIncomingBlock(U);
5568 // Bail out if we have uses in different blocks. We don't do any
5569 // sophisticated analysis (i.e finding NearestCommonDominator of these
5570 // use blocks).
5571 if (UserParent && UserParent != UserBB)
5572 return std::nullopt;
5573 UserParent = UserBB;
5574
5575 // Make sure these checks are done only once, naturally we do the checks
5576 // the first time we get the userparent, this will save compile time.
5577 if (NumUsers == 0) {
5578 // Try sinking to another block. If that block is unreachable, then do
5579 // not bother. SimplifyCFG should handle it.
5580 if (UserParent == BB || !DT.isReachableFromEntry(UserParent))
5581 return std::nullopt;
5582
5583 auto *Term = UserParent->getTerminator();
5584 // See if the user is one of our successors that has only one
5585 // predecessor, so that we don't have to split the critical edge.
5586 // Another option where we can sink is a block that ends with a
5587 // terminator that does not pass control to other block (such as
5588 // return or unreachable or resume). In this case:
5589 // - I dominates the User (by SSA form);
5590 // - the User will be executed at most once.
5591 // So sinking I down to User is always profitable or neutral.
5592 if (UserParent->getUniquePredecessor() != BB && !succ_empty(Term))
5593 return std::nullopt;
5594
5595 assert(DT.dominates(BB, UserParent) && "Dominance relation broken?");
5596 }
5597
5598 NumUsers++;
5599 }
5600
5601 // No user or only has droppable users.
5602 if (!UserParent)
5603 return std::nullopt;
5604
5605 return UserParent;
5606 };
5607
5608 auto OptBB = getOptionalSinkBlockForInst(I);
5609 if (OptBB) {
5610 auto *UserParent = *OptBB;
5611 // Okay, the CFG is simple enough, try to sink this instruction.
5612 if (tryToSinkInstruction(I, UserParent)) {
5613 LLVM_DEBUG(dbgs() << "IC: Sink: " << *I << '\n');
5614 MadeIRChange = true;
5615 // We'll add uses of the sunk instruction below, but since
5616 // sinking can expose opportunities for it's *operands* add
5617 // them to the worklist
5618 for (Use &U : I->operands())
5619 if (Instruction *OpI = dyn_cast<Instruction>(U.get()))
5620 Worklist.push(OpI);
5621 }
5622 }
5623
5624 // Now that we have an instruction, try combining it to simplify it.
5627 I, {LLVMContext::MD_dbg, LLVMContext::MD_annotation});
5628
5629#ifndef NDEBUG
5630 std::string OrigI;
5631#endif
5632 LLVM_DEBUG(raw_string_ostream SS(OrigI); I->print(SS););
5633 LLVM_DEBUG(dbgs() << "IC: Visiting: " << OrigI << '\n');
5634
5635 if (Instruction *Result = visit(*I)) {
5636 ++NumCombined;
5637 // Should we replace the old instruction with a new one?
5638 if (Result != I) {
5639 LLVM_DEBUG(dbgs() << "IC: Old = " << *I << '\n'
5640 << " New = " << *Result << '\n');
5641
5642 // We copy the old instruction's DebugLoc to the new instruction, unless
5643 // InstCombine already assigned a DebugLoc to it, in which case we
5644 // should trust the more specifically selected DebugLoc.
5645 Result->setDebugLoc(Result->getDebugLoc().orElse(I->getDebugLoc()));
5646 // We also copy annotation metadata to the new instruction.
5647 Result->copyMetadata(*I, LLVMContext::MD_annotation);
5648 // Everything uses the new instruction now.
5649 I->replaceAllUsesWith(Result);
5650
5651 // Move the name to the new instruction first.
5652 Result->takeName(I);
5653
5654 // Insert the new instruction into the basic block...
5655 BasicBlock *InstParent = I->getParent();
5656 BasicBlock::iterator InsertPos = I->getIterator();
5657
5658 // Are we replace a PHI with something that isn't a PHI, or vice versa?
5659 if (isa<PHINode>(Result) != isa<PHINode>(I)) {
5660 // We need to fix up the insertion point.
5661 if (isa<PHINode>(I)) // PHI -> Non-PHI
5662 InsertPos = InstParent->getFirstInsertionPt();
5663 else // Non-PHI -> PHI
5664 InsertPos = InstParent->getFirstNonPHIIt();
5665 }
5666
5667 Result->insertInto(InstParent, InsertPos);
5668
5669 // Push the new instruction and any users onto the worklist.
5671 Worklist.push(Result);
5672
5674 } else {
5675 LLVM_DEBUG(dbgs() << "IC: Mod = " << OrigI << '\n'
5676 << " New = " << *I << '\n');
5677
5678 // If the instruction was modified, it's possible that it is now dead.
5679 // if so, remove it.
5682 } else {
5684 Worklist.push(I);
5685 }
5686 }
5687 MadeIRChange = true;
5688 }
5689 }
5690
5691 Worklist.zap();
5692 return MadeIRChange;
5693}
5694
5695// Track the scopes used by !alias.scope and !noalias. In a function, a
5696// @llvm.experimental.noalias.scope.decl is only useful if that scope is used
5697// by both sets. If not, the declaration of the scope can be safely omitted.
5698// The MDNode of the scope can be omitted as well for the instructions that are
5699// part of this function. We do not do that at this point, as this might become
5700// too time consuming to do.
5702 SmallPtrSet<const MDNode *, 8> UsedAliasScopesAndLists;
5703 SmallPtrSet<const MDNode *, 8> UsedNoAliasScopesAndLists;
5704
5705public:
5707 // This seems to be faster than checking 'mayReadOrWriteMemory()'.
5708 if (!I->hasMetadataOtherThanDebugLoc())
5709 return;
5710
5711 auto Track = [](Metadata *ScopeList, auto &Container) {
5712 const auto *MDScopeList = dyn_cast_or_null<MDNode>(ScopeList);
5713 if (!MDScopeList || !Container.insert(MDScopeList).second)
5714 return;
5715 for (const auto &MDOperand : MDScopeList->operands())
5716 if (auto *MDScope = dyn_cast<MDNode>(MDOperand))
5717 Container.insert(MDScope);
5718 };
5719
5720 Track(I->getMetadata(LLVMContext::MD_alias_scope), UsedAliasScopesAndLists);
5721 Track(I->getMetadata(LLVMContext::MD_noalias), UsedNoAliasScopesAndLists);
5722 }
5723
5725 NoAliasScopeDeclInst *Decl = dyn_cast<NoAliasScopeDeclInst>(Inst);
5726 if (!Decl)
5727 return false;
5728
5729 assert(Decl->use_empty() &&
5730 "llvm.experimental.noalias.scope.decl in use ?");
5731 const MDNode *MDSL = Decl->getScopeList();
5732 assert(MDSL->getNumOperands() == 1 &&
5733 "llvm.experimental.noalias.scope should refer to a single scope");
5734 auto &MDOperand = MDSL->getOperand(0);
5735 if (auto *MD = dyn_cast<MDNode>(MDOperand))
5736 return !UsedAliasScopesAndLists.contains(MD) ||
5737 !UsedNoAliasScopesAndLists.contains(MD);
5738
5739 // Not an MDNode ? throw away.
5740 return true;
5741 }
5742};
5743
5744/// Populate the IC worklist from a function, by walking it in reverse
5745/// post-order and adding all reachable code to the worklist.
5746///
5747/// This has a couple of tricks to make the code faster and more powerful. In
5748/// particular, we constant fold and DCE instructions as we go, to avoid adding
5749/// them to the worklist (this significantly speeds up instcombine on code where
5750/// many instructions are dead or constant). Additionally, if we find a branch
5751/// whose condition is a known constant, we only visit the reachable successors.
5753 bool MadeIRChange = false;
5755 SmallVector<Instruction *, 128> InstrsForInstructionWorklist;
5756 DenseMap<Constant *, Constant *> FoldedConstants;
5757 AliasScopeTracker SeenAliasScopes;
5758
5759 auto HandleOnlyLiveSuccessor = [&](BasicBlock *BB, BasicBlock *LiveSucc) {
5760 for (BasicBlock *Succ : successors(BB))
5761 if (Succ != LiveSucc && DeadEdges.insert({BB, Succ}).second)
5762 for (PHINode &PN : Succ->phis())
5763 for (Use &U : PN.incoming_values())
5764 if (PN.getIncomingBlock(U) == BB && !isa<PoisonValue>(U)) {
5765 U.set(PoisonValue::get(PN.getType()));
5766 MadeIRChange = true;
5767 }
5768 };
5769
5770 for (BasicBlock *BB : RPOT) {
5771 if (!BB->isEntryBlock() && all_of(predecessors(BB), [&](BasicBlock *Pred) {
5772 return DeadEdges.contains({Pred, BB}) || DT.dominates(BB, Pred);
5773 })) {
5774 HandleOnlyLiveSuccessor(BB, nullptr);
5775 continue;
5776 }
5777 LiveBlocks.insert(BB);
5778
5779 for (Instruction &Inst : llvm::make_early_inc_range(*BB)) {
5780 // ConstantProp instruction if trivially constant.
5781 if (!Inst.use_empty() &&
5782 (Inst.getNumOperands() == 0 || isa<Constant>(Inst.getOperand(0))))
5783 if (Constant *C = ConstantFoldInstruction(&Inst, DL, &TLI)) {
5784 LLVM_DEBUG(dbgs() << "IC: ConstFold to: " << *C << " from: " << Inst
5785 << '\n');
5786 Inst.replaceAllUsesWith(C);
5787 ++NumConstProp;
5788 if (isInstructionTriviallyDead(&Inst, &TLI))
5789 Inst.eraseFromParent();
5790 MadeIRChange = true;
5791 continue;
5792 }
5793
5794 // See if we can constant fold its operands.
5795 for (Use &U : Inst.operands()) {
5796 if (!isa<ConstantVector>(U) && !isa<ConstantExpr>(U))
5797 continue;
5798
5799 auto *C = cast<Constant>(U);
5800 Constant *&FoldRes = FoldedConstants[C];
5801 if (!FoldRes)
5802 FoldRes = ConstantFoldConstant(C, DL, &TLI);
5803
5804 if (FoldRes != C) {
5805 LLVM_DEBUG(dbgs() << "IC: ConstFold operand of: " << Inst
5806 << "\n Old = " << *C
5807 << "\n New = " << *FoldRes << '\n');
5808 U = FoldRes;
5809 MadeIRChange = true;
5810 }
5811 }
5812
5813 // Skip processing debug and pseudo intrinsics in InstCombine. Processing
5814 // these call instructions consumes non-trivial amount of time and
5815 // provides no value for the optimization.
5816 if (!Inst.isDebugOrPseudoInst()) {
5817 InstrsForInstructionWorklist.push_back(&Inst);
5818 SeenAliasScopes.analyse(&Inst);
5819 }
5820 }
5821
5822 // If this is a branch or switch on a constant, mark only the single
5823 // live successor. Otherwise assume all successors are live.
5824 Instruction *TI = BB->getTerminator();
5825 if (BranchInst *BI = dyn_cast<BranchInst>(TI); BI && BI->isConditional()) {
5826 if (isa<UndefValue>(BI->getCondition())) {
5827 // Branch on undef is UB.
5828 HandleOnlyLiveSuccessor(BB, nullptr);
5829 continue;
5830 }
5831 if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
5832 bool CondVal = Cond->getZExtValue();
5833 HandleOnlyLiveSuccessor(BB, BI->getSuccessor(!CondVal));
5834 continue;
5835 }
5836 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
5837 if (isa<UndefValue>(SI->getCondition())) {
5838 // Switch on undef is UB.
5839 HandleOnlyLiveSuccessor(BB, nullptr);
5840 continue;
5841 }
5842 if (auto *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
5843 HandleOnlyLiveSuccessor(BB,
5844 SI->findCaseValue(Cond)->getCaseSuccessor());
5845 continue;
5846 }
5847 }
5848 }
5849
5850 // Remove instructions inside unreachable blocks. This prevents the
5851 // instcombine code from having to deal with some bad special cases, and
5852 // reduces use counts of instructions.
5853 for (BasicBlock &BB : F) {
5854 if (LiveBlocks.count(&BB))
5855 continue;
5856
5857 unsigned NumDeadInstInBB;
5858 NumDeadInstInBB = removeAllNonTerminatorAndEHPadInstructions(&BB);
5859
5860 MadeIRChange |= NumDeadInstInBB != 0;
5861 NumDeadInst += NumDeadInstInBB;
5862 }
5863
5864 // Once we've found all of the instructions to add to instcombine's worklist,
5865 // add them in reverse order. This way instcombine will visit from the top
5866 // of the function down. This jives well with the way that it adds all uses
5867 // of instructions to the worklist after doing a transformation, thus avoiding
5868 // some N^2 behavior in pathological cases.
5869 Worklist.reserve(InstrsForInstructionWorklist.size());
5870 for (Instruction *Inst : reverse(InstrsForInstructionWorklist)) {
5871 // DCE instruction if trivially dead. As we iterate in reverse program
5872 // order here, we will clean up whole chains of dead instructions.
5873 if (isInstructionTriviallyDead(Inst, &TLI) ||
5874 SeenAliasScopes.isNoAliasScopeDeclDead(Inst)) {
5875 ++NumDeadInst;
5876 LLVM_DEBUG(dbgs() << "IC: DCE: " << *Inst << '\n');
5877 salvageDebugInfo(*Inst);
5878 Inst->eraseFromParent();
5879 MadeIRChange = true;
5880 continue;
5881 }
5882
5883 Worklist.push(Inst);
5884 }
5885
5886 return MadeIRChange;
5887}
5888
5890 // Collect backedges.
5892 for (BasicBlock *BB : RPOT) {
5893 Visited.insert(BB);
5894 for (BasicBlock *Succ : successors(BB))
5895 if (Visited.contains(Succ))
5896 BackEdges.insert({BB, Succ});
5897 }
5898 ComputedBackEdges = true;
5899}
5900
5906 const InstCombineOptions &Opts) {
5907 auto &DL = F.getDataLayout();
5908 bool VerifyFixpoint = Opts.VerifyFixpoint &&
5909 !F.hasFnAttribute("instcombine-no-verify-fixpoint");
5910
5911 /// Builder - This is an IRBuilder that automatically inserts new
5912 /// instructions into the worklist when they are created.
5914 F.getContext(), TargetFolder(DL),
5915 IRBuilderCallbackInserter([&Worklist, &AC](Instruction *I) {
5916 Worklist.add(I);
5917 if (auto *Assume = dyn_cast<AssumeInst>(I))
5918 AC.registerAssumption(Assume);
5919 }));
5920
5922
5923 // Lower dbg.declare intrinsics otherwise their value may be clobbered
5924 // by instcombiner.
5925 bool MadeIRChange = false;
5927 MadeIRChange = LowerDbgDeclare(F);
5928
5929 // Iterate while there is work to do.
5930 unsigned Iteration = 0;
5931 while (true) {
5932 if (Iteration >= Opts.MaxIterations && !VerifyFixpoint) {
5933 LLVM_DEBUG(dbgs() << "\n\n[IC] Iteration limit #" << Opts.MaxIterations
5934 << " on " << F.getName()
5935 << " reached; stopping without verifying fixpoint\n");
5936 break;
5937 }
5938
5939 ++Iteration;
5940 ++NumWorklistIterations;
5941 LLVM_DEBUG(dbgs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
5942 << F.getName() << "\n");
5943
5944 InstCombinerImpl IC(Worklist, Builder, F.hasMinSize(), AA, AC, TLI, TTI, DT,
5945 ORE, BFI, BPI, PSI, DL, RPOT);
5947 bool MadeChangeInThisIteration = IC.prepareWorklist(F);
5948 MadeChangeInThisIteration |= IC.run();
5949 if (!MadeChangeInThisIteration)
5950 break;
5951
5952 MadeIRChange = true;
5953 if (Iteration > Opts.MaxIterations) {
5955 "Instruction Combining on " + Twine(F.getName()) +
5956 " did not reach a fixpoint after " + Twine(Opts.MaxIterations) +
5957 " iterations. " +
5958 "Use 'instcombine<no-verify-fixpoint>' or function attribute "
5959 "'instcombine-no-verify-fixpoint' to suppress this error.");
5960 }
5961 }
5962
5963 if (Iteration == 1)
5964 ++NumOneIteration;
5965 else if (Iteration == 2)
5966 ++NumTwoIterations;
5967 else if (Iteration == 3)
5968 ++NumThreeIterations;
5969 else
5970 ++NumFourOrMoreIterations;
5971
5972 return MadeIRChange;
5973}
5974
5976
5978 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
5979 static_cast<PassInfoMixin<InstCombinePass> *>(this)->printPipeline(
5980 OS, MapClassName2PassName);
5981 OS << '<';
5982 OS << "max-iterations=" << Options.MaxIterations << ";";
5983 OS << (Options.VerifyFixpoint ? "" : "no-") << "verify-fixpoint";
5984 OS << '>';
5985}
5986
5987char InstCombinePass::ID = 0;
5988
5991 auto &LRT = AM.getResult<LastRunTrackingAnalysis>(F);
5992 // No changes since last InstCombine pass, exit early.
5993 if (LRT.shouldSkip(&ID))
5994 return PreservedAnalyses::all();
5995
5996 auto &AC = AM.getResult<AssumptionAnalysis>(F);
5997 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
5998 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
6000 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
6001
6002 auto *AA = &AM.getResult<AAManager>(F);
6003 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
6004 ProfileSummaryInfo *PSI =
6005 MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
6006 auto *BFI = (PSI && PSI->hasProfileSummary()) ?
6007 &AM.getResult<BlockFrequencyAnalysis>(F) : nullptr;
6009
6010 if (!combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, TTI, DT, ORE,
6011 BFI, BPI, PSI, Options)) {
6012 // No changes, all analyses are preserved.
6013 LRT.update(&ID, /*Changed=*/false);
6014 return PreservedAnalyses::all();
6015 }
6016
6017 // Mark all the analyses that instcombine updates as preserved.
6019 LRT.update(&ID, /*Changed=*/true);
6022 return PA;
6023}
6024
6026 AU.setPreservesCFG();
6039}
6040
6042 if (skipFunction(F))
6043 return false;
6044
6045 // Required analyses.
6046 auto AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
6047 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
6048 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
6049 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
6050 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
6051 auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
6052
6053 // Optional analyses.
6054 ProfileSummaryInfo *PSI =
6055 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
6056 BlockFrequencyInfo *BFI =
6057 (PSI && PSI->hasProfileSummary()) ?
6058 &getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() :
6059 nullptr;
6060 BranchProbabilityInfo *BPI = nullptr;
6061 if (auto *WrapperPass =
6062 getAnalysisIfAvailable<BranchProbabilityInfoWrapperPass>())
6063 BPI = &WrapperPass->getBPI();
6064
6065 return combineInstructionsOverFunction(F, Worklist, AA, AC, TLI, TTI, DT, ORE,
6066 BFI, BPI, PSI, InstCombineOptions());
6067}
6068
6070
6073}
6074
6076 "Combine redundant instructions", false, false)
6088
6089// Initialization Routines
6092}
6093
6095 return new InstructionCombiningPass();
6096}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static const Function * getParent(const Value *V)
This is the interface for LLVM's primary stateless and local alias analysis.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Resource Access
return RetTy
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
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
Definition: DebugCounter.h:194
This file defines the DenseMap class.
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool isSigned(unsigned int Opcode)
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
Hexagon Vector Combine
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
iv Induction Variable Users
Definition: IVUsers.cpp:48
static bool leftDistributesOverRight(Instruction::BinaryOps LOp, bool HasNUW, bool HasNSW, Intrinsic::ID ROp)
Return whether "X LOp (Y ROp Z)" is always equal to "(X LOp Y) ROp (X LOp Z)".
This file provides internal interfaces used to implement the InstCombine.
This file provides the primary interface to the instcombine pass.
static Value * simplifySwitchOnSelectUsingRanges(SwitchInst &SI, SelectInst *Select, bool IsTrueArm)
static bool isUsedWithinShuffleVector(Value *V)
static bool isNeverEqualToUnescapedAlloc(Value *V, const TargetLibraryInfo &TLI, Instruction *AI)
static bool shorter_filter(const Value *LHS, const Value *RHS)
static Instruction * foldSelectGEP(GetElementPtrInst &GEP, InstCombiner::BuilderTy &Builder)
Thread a GEP operation with constant indices through the constant true/false arms of a select.
static bool shouldMergeGEPs(GEPOperator &GEP, GEPOperator &Src)
static cl::opt< unsigned > MaxArraySize("instcombine-maxarray-size", cl::init(1024), cl::desc("Maximum array size considered when doing a combine"))
static cl::opt< unsigned > ShouldLowerDbgDeclare("instcombine-lower-dbg-declare", cl::Hidden, cl::init(true))
static bool hasNoSignedWrap(BinaryOperator &I)
static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1, InstCombinerImpl &IC)
Combine constant operands of associative operations either before or after a cast to eliminate one of...
static bool combineInstructionsOverFunction(Function &F, InstructionWorklist &Worklist, AliasAnalysis *AA, AssumptionCache &AC, TargetLibraryInfo &TLI, TargetTransformInfo &TTI, DominatorTree &DT, OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI, ProfileSummaryInfo *PSI, const InstCombineOptions &Opts)
static Value * simplifyInstructionWithPHI(Instruction &I, PHINode *PN, Value *InValue, BasicBlock *InBB, const DataLayout &DL, const SimplifyQuery SQ)
static bool shouldCanonicalizeGEPToPtrAdd(GetElementPtrInst &GEP)
Return true if we should canonicalize the gep to an i8 ptradd.
static void ClearSubclassDataAfterReassociation(BinaryOperator &I)
Conservatively clears subclassOptionalData after a reassociation or commutation.
static Value * getIdentityValue(Instruction::BinaryOps Opcode, Value *V)
This function returns identity value for given opcode, which can be used to factor patterns like (X *...
static Value * foldFrexpOfSelect(ExtractValueInst &EV, IntrinsicInst *FrexpCall, SelectInst *SelectInst, InstCombiner::BuilderTy &Builder)
static std::optional< std::pair< Value *, Value * > > matchSymmetricPhiNodesPair(PHINode *LHS, PHINode *RHS)
static Value * foldOperationIntoSelectOperand(Instruction &I, SelectInst *SI, Value *NewOp, InstCombiner &IC)
static Instruction * canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP, GEPOperator *Src, InstCombinerImpl &IC)
static Instruction * tryToMoveFreeBeforeNullTest(CallInst &FI, const DataLayout &DL)
Move the call to free before a NULL test.
static Value * simplifyOperationIntoSelectOperand(Instruction &I, SelectInst *SI, bool IsTrueArm)
static bool rightDistributesOverLeft(Instruction::BinaryOps LOp, Instruction::BinaryOps ROp)
Return whether "(X LOp Y) ROp Z" is always equal to "(X ROp Z) LOp (Y ROp Z)".
static Value * tryFactorization(BinaryOperator &I, const SimplifyQuery &SQ, InstCombiner::BuilderTy &Builder, Instruction::BinaryOps InnerOpcode, Value *A, Value *B, Value *C, Value *D)
This tries to simplify binary operations by factorizing out common terms (e.
static bool isRemovableWrite(CallBase &CB, Value *UsedV, const TargetLibraryInfo &TLI)
Given a call CB which uses an address UsedV, return true if we can prove the call's only possible eff...
static Instruction::BinaryOps getBinOpsForFactorization(Instruction::BinaryOps TopOpcode, BinaryOperator *Op, Value *&LHS, Value *&RHS, BinaryOperator *OtherOp)
This function predicates factorization using distributive laws.
static bool hasNoUnsignedWrap(BinaryOperator &I)
static bool SoleWriteToDeadLocal(Instruction *I, TargetLibraryInfo &TLI)
Check for case where the call writes to an otherwise dead alloca.
static cl::opt< unsigned > MaxSinkNumUsers("instcombine-max-sink-users", cl::init(32), cl::desc("Maximum number of undroppable users for instruction sinking"))
static Instruction * foldGEPOfPhi(GetElementPtrInst &GEP, PHINode *PN, IRBuilderBase &Builder)
static std::optional< ModRefInfo > isAllocSiteRemovable(Instruction *AI, SmallVectorImpl< WeakTrackingVH > &Users, const TargetLibraryInfo &TLI, bool KnowInit)
static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo)
Return 'true' if the given typeinfo will match anything.
static cl::opt< bool > EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"), cl::init(true))
static bool maintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C)
static GEPNoWrapFlags getMergedGEPNoWrapFlags(GEPOperator &GEP1, GEPOperator &GEP2)
Determine nowrap flags for (gep (gep p, x), y) to (gep p, (x + y)) transform.
static LVOptions Options
Definition: LVOptions.cpp:25
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
static bool IsSelect(MachineInstr &MI)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:39
const SmallVectorImpl< MachineOperand > & Cond
static unsigned getNumElements(Type *Ty)
unsigned OpIndex
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
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:167
#define LLVM_DEBUG(...)
Definition: Debug.h:119
static unsigned getScalarSizeInBits(Type *Ty)
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:247
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition: blake3_impl.h:83
bool isNoAliasScopeDeclDead(Instruction *Inst)
void analyse(Instruction *I)
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
A private abstract base class describing the concept of an individual alias analysis implementation.
Class for arbitrary precision integers.
Definition: APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
Definition: APInt.cpp:1758
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:423
static LLVM_ABI void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Definition: APInt.cpp:1890
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:936
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1928
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
LLVM_ABI APInt smul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1960
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:431
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:270
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition: ArrayRef.h:224
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
Class to represent array types.
Definition: DerivedTypes.h:398
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
uint64_t getNumElements() const
Definition: DerivedTypes.h:410
Type * getElementType() const
Definition: DerivedTypes.h:411
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM_ABI uint64_t getDereferenceableBytes() const
Returns the number of dereferenceable bytes from the dereferenceable attribute.
Definition: Attributes.cpp:447
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:223
Legacy wrapper pass to provide the BasicAAResult object.
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:528
LLVM_ABI 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:393
LLVM_ABI iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=true) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
Definition: BasicBlock.cpp:206
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:337
const Instruction & front() const
Definition: BasicBlock.h:482
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
Definition: BasicBlock.cpp:549
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:437
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:445
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
LLVM_ABI const_iterator getFirstNonPHIOrDbgOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a debug intrinsic,...
Definition: BasicBlock.cpp:406
size_t size() const
Definition: BasicBlock.h:480
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:233
static LLVM_ABI BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
Definition: InstrTypes.h:374
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateNUW(BinaryOps Opc, Value *V1, Value *V2, const Twine &Name="")
Definition: InstrTypes.h:294
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
LLVM_ABI void swapSuccessors()
Swap the successors of this branch instruction.
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
LLVM_ABI void swapSuccEdgesProbabilities(const BasicBlock *Src)
Swap outgoing edges probabilities for Src with branch terminator.
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1348
void setAttributes(AttributeList A)
Set the attributes for this call.
Definition: InstrTypes.h:1427
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: InstrTypes.h:1955
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
AttributeList getAttributes() const
Return the attributes for this call.
Definition: InstrTypes.h:1424
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:678
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:701
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:703
@ ICMP_EQ
equal
Definition: InstrTypes.h:699
@ ICMP_NE
not equal
Definition: InstrTypes.h:700
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:829
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:791
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:23
ConstantArray - Constant Array Declarations.
Definition: Constants.h:433
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1314
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:776
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2654
static LLVM_ABI Constant * getNot(Constant *C)
Definition: Constants.cpp:2641
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2647
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2694
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
Definition: Constants.cpp:2635
This is the shared class of boolean and integer constants.
Definition: Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:868
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:875
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:882
This class represents a range of values.
Definition: ConstantRange.h:47
LLVM_ABI bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static LLVM_ABI ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
Constant Vector Declarations.
Definition: Constants.h:517
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1474
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1423
This is an important base class in LLVM.
Definition: Constant.h:43
static LLVM_ABI Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Definition: Constants.cpp:403
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:784
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
const Constant * stripPointerCasts() const
Definition: Constant.h:219
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
LLVM_ABI SmallVector< APInt > getGEPIndicesForOffset(Type *&ElemTy, APInt &Offset) const
Get GEP indices to access Offset inside ElemTy.
Definition: DataLayout.cpp:971
bool isLegalInteger(uint64_t Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU.
Definition: DataLayout.h:220
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
Definition: DataLayout.cpp:753
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition: DataLayout.cpp:877
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:504
unsigned getIndexSizeInBits(unsigned AS) const
The size in bits of indices used for address calculation in getelementptr and for addresses in the gi...
Definition: DataLayout.h:398
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:674
LLVM_ABI int64_t getIndexedOffsetInType(Type *ElemTy, ArrayRef< Value * > Indices) const
Returns the offset from the beginning of the type for the specified indices.
Definition: DataLayout.cpp:892
Record of a variable value-assignment, aka a non instruction representation of the dbg....
static bool shouldExecute(unsigned CounterName)
Definition: DebugCounter.h:88
Identifies a unique instance of a variable.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:187
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:165
bool empty() const
Definition: DenseMap.h:107
iterator end()
Definition: DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:214
void registerBranch(BranchInst *BI)
Add a branch condition to the cache.
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:284
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:322
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:334
LLVM_ABI 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:135
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
iterator_range< idx_iterator > indices() const
idx_iterator idx_end() const
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
idx_iterator idx_begin() const
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:188
const BasicBlock & getEntryBlock() const
Definition: Function.h:807
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags inBounds()
GEPNoWrapFlags withoutNoUnsignedSignedWrap() const
static GEPNoWrapFlags all()
static GEPNoWrapFlags noUnsignedWrap()
bool hasNoUnsignedWrap() const
bool isInBounds() const
GEPNoWrapFlags intersectForOffsetAdd(GEPNoWrapFlags Other) const
Given (gep (gep p, x), y), determine the nowrap flags for (gep p, x+y).
GEPNoWrapFlags withoutNoUnsignedWrap() const
static GEPNoWrapFlags none()
GEPNoWrapFlags getNoWrapFlags() const
Definition: Operator.h:425
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
static LLVM_ABI Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition: Instructions.h:973
static LLVM_ABI Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:997
Legacy wrapper pass to provide the GlobalsAAResult object.
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getCmpPredicate() const
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
Value * CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1737
LLVM_ABI Value * CreateSelectFMF(Value *C, Value *True, Value *False, FMFSource FMFSource, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1010
LLVM_ABI Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1115
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2618
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:502
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1005
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2094
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2637
Value * CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:2036
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:345
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1931
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", GEPNoWrapFlags NW=GEPNoWrapFlags::none())
Definition: IRBuilder.h:1923
void CollectMetadataToCopy(Instruction *Src, ArrayRef< unsigned > MetadataKinds)
Collect metadata with IDs MetadataKinds from Src which should be added to all created instructions.
Definition: IRBuilder.h:262
LLVM_ABI Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
Definition: IRBuilder.cpp:823
LLVM_ABI 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:834
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:522
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2463
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2494
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1805
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1420
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1847
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, const AAMDNodes &AAInfo=AAMDNodes())
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:630
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="", bool IsNonNeg=false)
Definition: IRBuilder.h:2082
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2593
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1551
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1403
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2508
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="", bool IsNUW=false, bool IsNSW=false)
Definition: IRBuilder.h:2068
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1708
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:207
Value * CreateAShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1532
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1599
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2439
Value * CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name="")
Definition: IRBuilder.h:1731
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
Definition: IRBuilder.h:552
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Definition: IRBuilder.h:538
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
Definition: IRBuilder.h:75
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI InstCombinePass(InstCombineOptions Opts={})
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I)
Tries to simplify binops of select and cast of the select condition.
Instruction * foldBinOpIntoSelectOrPhi(BinaryOperator &I)
This is a convenience wrapper function for the above two functions.
bool SimplifyAssociativeOrCommutative(BinaryOperator &I)
Performs a few simplifications for operators which are associative or commutative.
Instruction * visitGEPOfGEP(GetElementPtrInst &GEP, GEPOperator *Src)
Value * foldUsingDistributiveLaws(BinaryOperator &I)
Tries to simplify binary operations which some other binary operation distributes over.
Instruction * foldBinOpShiftWithShift(BinaryOperator &I)
Instruction * visitUnreachableInst(UnreachableInst &I)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, bool AllowMultipleUses=false)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
void handleUnreachableFrom(Instruction *I, SmallVectorImpl< BasicBlock * > &Worklist)
Value * SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &PoisonElts, unsigned Depth=0, bool AllowMultipleUsers=false) override
The specified value produces a vector with any number of elements.
Instruction * visitFreeze(FreezeInst &I)
void handlePotentiallyDeadBlocks(SmallVectorImpl< BasicBlock * > &Worklist)
bool prepareWorklist(Function &F)
Perform early cleanup and prepare the InstCombine worklist.
Instruction * visitFree(CallInst &FI, Value *FreedOp)
Instruction * visitExtractValueInst(ExtractValueInst &EV)
void handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc)
Instruction * visitUnconditionalBranchInst(BranchInst &BI)
Instruction * foldBinopWithRecurrence(BinaryOperator &BO)
Try to fold binary operators whose operands are simple interleaved recurrences to a single recurrence...
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * visitLandingPadInst(LandingPadInst &LI)
Instruction * visitReturnInst(ReturnInst &RI)
Instruction * visitSwitchInst(SwitchInst &SI)
Instruction * foldBinopWithPhiOperands(BinaryOperator &BO)
For a binary operator with 2 phi operands, try to hoist the binary operation before the phi.
Constant * getLosslessTrunc(Constant *C, Type *TruncTy, unsigned ExtOp)
bool mergeStoreIntoSuccessor(StoreInst &SI)
Try to transform: if () { *P = v1; } else { *P = v2 } or: *P = v1; if () { *P = v2; } into a phi node...
Instruction * tryFoldInstWithCtpopWithNot(Instruction *I)
void CreateNonTerminatorUnreachable(Instruction *InsertAt)
Create and insert the idiom we use to indicate a block is unreachable without having to rewrite the C...
Value * pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI)
bool run()
Run the combiner over the entire worklist until it is empty.
Instruction * foldVectorBinop(BinaryOperator &Inst)
Canonicalize the position of binops relative to shufflevector.
bool removeInstructionsBeforeUnreachable(Instruction &I)
Value * SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS, Value *RHS)
void tryToSinkInstructionDbgVariableRecords(Instruction *I, BasicBlock::iterator InsertPos, BasicBlock *SrcBlock, BasicBlock *DestBlock, SmallVectorImpl< DbgVariableRecord * > &DPUsers)
void addDeadEdge(BasicBlock *From, BasicBlock *To, SmallVectorImpl< BasicBlock * > &Worklist)
Constant * unshuffleConstant(ArrayRef< int > ShMask, Constant *C, VectorType *NewCTy)
Find a constant NewC that has property: shuffle(NewC, ShMask) = C Returns nullptr if such a constant ...
Instruction * visitAllocSite(Instruction &FI)
Instruction * visitGetElementPtrInst(GetElementPtrInst &GEP)
Instruction * visitBranchInst(BranchInst &BI)
Value * tryFactorizationFolds(BinaryOperator &I)
This tries to simplify binary operations by factorizing out common terms (e.
Instruction * foldFreezeIntoRecurrence(FreezeInst &I, PHINode *PN)
Value * SimplifyDemandedUseFPClass(Value *V, FPClassTest DemandedMask, KnownFPClass &Known, Instruction *CxtI, unsigned Depth=0)
Attempts to replace V with a simpler value based on the demanded floating-point classes.
bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock)
Try to move the specified instruction from its current block into the beginning of DestBlock,...
bool freezeOtherUses(FreezeInst &FI)
void freelyInvertAllUsersOf(Value *V, Value *IgnoredUser=nullptr)
Freely adapt every user of V as-if V was changed to !V.
The core instruction combiner logic.
Definition: InstCombiner.h:48
SimplifyQuery SQ
Definition: InstCombiner.h:77
const DataLayout & getDataLayout() const
Definition: InstCombiner.h:337
bool isFreeToInvert(Value *V, bool WillInvertAllUses, bool &DoesConsume)
Return true if the specified value is free to invert (apply ~ to).
Definition: InstCombiner.h:228
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
Definition: InstCombiner.h:143
TargetLibraryInfo & TLI
Definition: InstCombiner.h:74
unsigned ComputeNumSignBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
Definition: InstCombiner.h:456
Instruction * InsertNewInstBefore(Instruction *New, BasicBlock::iterator Old)
Inserts an instruction New before instruction Old.
Definition: InstCombiner.h:368
AAResults * AA
Definition: InstCombiner.h:70
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
Definition: InstCombiner.h:388
uint64_t MaxArraySizeForCombine
Maximum size of array considered when transforming.
Definition: InstCombiner.h:56
static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI)
Definition: InstCombiner.h:187
void replaceUse(Use &U, Value *NewValue)
Replace use and add the previously used value to the worklist.
Definition: InstCombiner.h:420
static bool isCanonicalPredicate(CmpPredicate Pred)
Predicate canonicalization reduces the number of patterns that need to be matched by other transforms...
Definition: InstCombiner.h:160
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Definition: InstCombiner.h:65
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
Definition: InstCombiner.h:377
BranchProbabilityInfo * BPI
Definition: InstCombiner.h:80
ReversePostOrderTraversal< BasicBlock * > & RPOT
Definition: InstCombiner.h:84
const DataLayout & DL
Definition: InstCombiner.h:76
DomConditionCache DC
Definition: InstCombiner.h:82
const bool MinimizeSize
Definition: InstCombiner.h:68
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
Definition: InstCombiner.h:433
std::optional< Instruction * > targetInstCombineIntrinsic(IntrinsicInst &II)
AssumptionCache & AC
Definition: InstCombiner.h:73
void addToWorklist(Instruction *I)
Definition: InstCombiner.h:332
Value * getFreelyInvertedImpl(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume, unsigned Depth)
Return nonnull value if V is free to invert under the condition of WillInvertAllUses.
SmallDenseSet< std::pair< const BasicBlock *, const BasicBlock * >, 8 > BackEdges
Backedges, used to avoid pushing instructions across backedges in cases where this may result in infi...
Definition: InstCombiner.h:97
std::optional< Value * > targetSimplifyDemandedVectorEltsIntrinsic(IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, APInt &UndefElts2, APInt &UndefElts3, std::function< void(Instruction *, unsigned, APInt, APInt &)> SimplifyAndSetOp)
Instruction * replaceOperand(Instruction &I, unsigned OpNum, Value *V)
Replace operand of instruction and add old operand to the worklist.
Definition: InstCombiner.h:412
DominatorTree & DT
Definition: InstCombiner.h:75
static Constant * getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant)
Some binary operators require special handling to avoid poison and undefined behavior.
Definition: InstCombiner.h:280
SmallDenseSet< std::pair< BasicBlock *, BasicBlock * >, 8 > DeadEdges
Edges that are known to never be taken.
Definition: InstCombiner.h:89
std::optional< Value * > targetSimplifyDemandedUseBitsIntrinsic(IntrinsicInst &II, APInt DemandedMask, KnownBits &Known, bool &KnownBitsComputed)
BuilderTy & Builder
Definition: InstCombiner.h:61
bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const
Value * getFreelyInverted(Value *V, bool WillInvertAllUses, BuilderTy *Builder, bool &DoesConsume)
Definition: InstCombiner.h:209
bool isBackEdge(const BasicBlock *From, const BasicBlock *To)
Definition: InstCombiner.h:358
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:87
The legacy pass manager's instcombine pass.
Definition: InstCombine.h:68
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void add(Instruction *I)
Add instruction to the worklist.
void push(Instruction *I)
Push the instruction onto the worklist stack.
void zap()
Check that the worklist is empty and nuke the backing store for the map.
LLVM_ABI void dropUBImplyingAttrsAndMetadata(ArrayRef< unsigned > Keep={})
Drop any attributes or metadata that can cause immediate undefined behavior.
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
Definition: Instruction.h:366
LLVM_ABI void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:513
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:78
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1804
LLVM_ABI bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:82
bool isTerminator() const
Definition: Instruction.h:315
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
LLVM_ABI bool willReturn() const LLVM_READONLY
Return true if the instruction will return (unwinding is considered as a form of returning control fl...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:312
bool isBitwiseLogicOp() const
Return true if this is and/or/xor.
Definition: Instruction.h:371
bool isShift() const
Definition: Instruction.h:320
LLVM_ABI void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:510
bool isIntDivRem() const
Definition: Instruction.h:318
Class to represent integer types.
Definition: DerivedTypes.h:42
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:319
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
Invoke instruction.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
The landingpad instruction holds all of the information necessary to generate correct exception handl...
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
static LLVM_ABI LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
LLVM_ABI void addClause(Constant *ClauseVal)
Add a catch or filter clause to the landing pad.
bool isCatch(unsigned Idx) const
Return 'true' if the clause and index Idx is a catch clause.
bool isFilter(unsigned Idx) const
Return 'true' if the clause and index Idx is a filter clause.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
A function/module analysis which provides an empty LastRunTrackingInfo.
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
An instruction for reading from memory.
Definition: Instructions.h:180
Value * getPointerOperand()
Definition: Instructions.h:259
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:209
Metadata node.
Definition: Metadata.h:1077
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1445
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1451
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:899
This is the common base class for memset/memcpy/memmove.
static LLVM_ABI MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
Root of the metadata hierarchy.
Definition: Metadata.h:63
This class represents min/max intrinsics.
Value * getLHS() const
Value * getRHS() const
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
MDNode * getScopeList() const
OptimizationRemarkEmitter legacy analysis pass.
The optimization diagnostic interface.
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:716
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:78
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
Definition: Operator.h:111
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Definition: Operator.h:105
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
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...
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
In order to facilitate speculative execution, many instructions do not invoke immediate undefined beh...
Definition: Constants.h:1468
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
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 & preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition: Analysis.h:132
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
bool hasProfileSummary() const
Returns true if profile summary is available.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:44
Return a value (possibly void), from a function.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
This class represents a cast from signed integer to floating point.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:168
This instruction constructs a fixed permutation of two input vectors.
size_type size() const
Definition: SmallPtrSet.h:99
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
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:401
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:476
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:356
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:182
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void reserve(size_type N)
Definition: SmallVector.h:664
iterator erase(const_iterator CI)
Definition: SmallVector.h:738
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
typename SuperClass::iterator iterator
Definition: SmallVector.h:578
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
An instruction for storing to memory.
Definition: Instructions.h:296
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Multiway switch.
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:35
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool has(LibFunc F) const
Tests whether a library function is available.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI std::optional< Instruction * > instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const
Targets can implement their own combinations for target-specific intrinsics.
LLVM_ABI std::optional< Value * > simplifyDemandedVectorEltsIntrinsic(InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, APInt &UndefElts2, APInt &UndefElts3, std::function< void(Instruction *, unsigned, APInt, APInt &)> SimplifyAndSetOp) const
Can be used to implement target-specific instruction combining.
LLVM_ABI std::optional< Value * > simplifyDemandedUseBitsIntrinsic(InstCombiner &IC, IntrinsicInst &II, APInt DemandedMask, KnownBits &Known, bool &KnownBitsComputed) const
Can be used to implement target-specific instruction combining.
LLVM_ABI bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const
Query the target whether the specified address space cast from FromAS to ToAS is valid.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
bool isStructTy() const
True if this is an instance of StructType.
Definition: Type.h:261
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:311
LLVM_ABI const fltSemantics & getFltSemantics() const
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:240
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
This class represents a cast unsigned integer to floating point.
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1866
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
op_range operands()
Definition: User.h:292
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:21
op_iterator op_begin()
Definition: User.h:284
const Use & getOperandUse(unsigned i) const
Definition: User.h:245
Value * getOperand(unsigned i) const
Definition: User.h:232
unsigned getNumOperands() const
Definition: User.h:254
op_iterator op_end()
Definition: User.h:286
LLVM_ABI bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition: User.cpp:115
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition: Value.h:759
LLVM_ABI bool hasOneUser() const
Return true if there is exactly one user of this value.
Definition: Value.cpp:166
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:439
iterator_range< user_iterator > users()
Definition: Value.h:426
bool hasUseList() const
Check if this Value has a use-list.
Definition: Value.h:344
LLVM_ABI bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition: Value.cpp:150
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:701
bool use_empty() const
Definition: Value.h:346
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1101
LLVM_ABI 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:881
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:396
Base class of all SIMD vector types.
Definition: DerivedTypes.h:430
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:695
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:205
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:203
constexpr bool isZero() const
Definition: TypeSize.h:157
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition: ilist_node.h:34
reverse_self_iterator getReverseIterator()
Definition: ilist_node.h:137
self_iterator getIterator()
Definition: ilist_node.h:134
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI bool isNoFPClassCompatibleType(Type *Ty)
Returns true if this is a type legal for the 'nofpclass' attribute.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
Definition: Intrinsics.cpp:751
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
class_match< PoisonValue > m_Poison()
Match an arbitrary poison constant.
Definition: PatternMatch.h:160
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
br_match m_UnconditionalBr(BasicBlock *&Succ)
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)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:862
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
DisjointOr_match< LHS, RHS > m_DisjointOr(const LHS &L, const RHS &R)
constantexpr_match m_ConstantExpr()
Match a constant expression or a constant that contains a constant expression.
Definition: PatternMatch.h:186
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
apint_match m_APIntAllowPoison(const APInt *&Res)
Match APInt while allowing poison in splat vector constants.
Definition: PatternMatch.h:305
NNegZExt_match< OpTy > m_NNegZExt(const OpTy &Op)
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
ThreeOps_match< decltype(m_Value()), LHS, RHS, Instruction::Select, true > m_c_Select(const LHS &L, const RHS &R)
Match Select(C, LHS, RHS) or Select(C, RHS, LHS)
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.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:931
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
CastInst_match< OpTy, UIToFPInst > m_UIToFP(const OpTy &Op)
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(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
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
CastInst_match< OpTy, SIToFPInst > m_SIToFP(const OpTy &Op)
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)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
cstfp_pred_ty< is_non_zero_fp > m_NonZeroFP()
Match a floating-point non-zero.
Definition: PatternMatch.h:793
m_Intrinsic_Ty< Opnd0 >::Ty m_VecReverse(const Opnd0 &Op0)
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
match_combine_or< match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > >, match_combine_or< MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty >, MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > > > m_MaxOrMin(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
@ FalseVal
Definition: TGLexer.h:59
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:338
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition: DWP.cpp:477
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition: STLExtras.h:860
void stable_sort(R &&Range)
Definition: STLExtras.h:2077
LLVM_ABI void initializeInstructionCombiningPassPass(PassRegistry &)
LLVM_ABI unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB)
Remove all instructions from a basic block other than its terminator and any present EH pad instructi...
Definition: Local.cpp:2485
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1744
LLVM_ABI Value * simplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef< Value * > Indices, GEPNoWrapFlags NW, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
LLVM_ABI Constant * getInitialValueOfAllocation(const Value *V, const TargetLibraryInfo *TLI, Type *Ty)
If this is a call to an allocation function that initializes memory to a fixed value,...
bool succ_empty(const Instruction *I)
Definition: CFG.h:256
LLVM_ABI Value * simplifyFreezeInst(Value *Op, const SimplifyQuery &Q)
Given an operand for a Freeze, see if we can fold the result.
LLVM_ABI FunctionPass * createInstructionCombiningPass()
LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the dbg.values describing a value.
Definition: DebugInfo.cpp:124
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2491
LLVM_ABI void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
Definition: Utils.cpp:1723
auto successors(const MachineBasicBlock *BB)
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
LLVM_ABI bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI)
Return true if this is a call to an allocation function that does not have side effects that we are r...
LLVM_ABI std::optional< StringRef > getAllocationFamily(const Value *I, const TargetLibraryInfo *TLI)
If a function is part of an allocation family (e.g.
LLVM_ABI Value * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI Value * simplifyInstructionWithOperands(Instruction *I, ArrayRef< Value * > NewOps, const SimplifyQuery &Q)
Like simplifyInstruction but the operands of I are replaced with NewOps.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
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:663
gep_type_iterator gep_type_end(const User *GEP)
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI Value * getReallocatedOperand(const CallBase *CB)
If this is a call to a realloc function, return the reallocated operand.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition: APFloat.h:1555
LLVM_ABI bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc,...
LLVM_ABI bool handleUnreachableTerminator(Instruction *I, SmallVectorImpl< Value * > &PoisonedValues)
If a terminator in an unreachable basic block has an operand of type Instruction, transform it into p...
Definition: Local.cpp:2468
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:157
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
LLVM_ABI Value * simplifyAddInst(Value *LHS, Value *RHS, bool IsNSW, bool IsNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
LLVM_ABI Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
constexpr bool has_single_bit(T Value) noexcept
Definition: bit.h:147
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:402
LLVM_ABI bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
LLVM_ABI Value * emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Definition: Local.cpp:22
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:47
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:49
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI bool LowerDbgDeclare(Function &F)
Lowers dbg.declare records into appropriate set of dbg.value records.
Definition: Local.cpp:1795
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1172
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI void ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, StoreInst *SI, DIBuilder &Builder)
Inserts a dbg.value record before a store to an alloca'd value that has an associated dbg....
Definition: Local.cpp:1662
LLVM_ABI void salvageDebugInfoForDbgValues(Instruction &I, ArrayRef< DbgVariableRecord * > DPInsns)
Implementation of salvageDebugInfo, applying only to instructions in Insns, rather than all debug use...
Definition: Local.cpp:2037
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI Value * simplifyExtractValueInst(Value *Agg, ArrayRef< unsigned > Idxs, const SimplifyQuery &Q)
Given operands for an ExtractValueInst, fold the result or return null.
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
LLVM_ABI bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
Definition: Local.cpp:2414
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
auto drop_end(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the last N elements excluded.
Definition: STLExtras.h:345
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition: ModRef.h:28
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
LLVM_ABI Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
DWARFExpression::Operation Op
bool isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I, bool IgnoreUBImplyingAttrs=true)
Don't use information from its non-constant operands.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
LLVM_ABI Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)
If this if a call to a free function, return the freed operand.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1980
gep_type_iterator gep_type_begin(const User *GEP)
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition: STLExtras.h:2107
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI void initializeInstCombine(PassRegistry &)
Initialize all passes linked into the InstCombine library.
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.
Definition: DebugInfo.cpp:129
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
bool isRefSet(const ModRefInfo MRI)
Definition: ModRef.h:52
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition: Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:853
#define N
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:304
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:324
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition: KnownBits.h:244
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:44
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:241
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
SimplifyQuery getWithInstruction(const Instruction *I) const
SimplifyQuery getWithoutUndef() const