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