Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Sun Jul 20 2025 13:23:21 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp