LLVM 19.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"
41#include "llvm/ADT/Statistic.h"
46#include "llvm/Analysis/CFG.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"
69#include "llvm/IR/Dominators.h"
71#include "llvm/IR/Function.h"
73#include "llvm/IR/IRBuilder.h"
74#include "llvm/IR/InstrTypes.h"
75#include "llvm/IR/Instruction.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"
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"
92#include "llvm/Support/Debug.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"
110#include <optional>
111
112using namespace llvm;
113using namespace llvm::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
133static cl::opt<bool>
134EnableCodeSinking("instcombine-code-sinking", cl::desc("Enable code sinking"),
135 cl::init(true));
136
138 "instcombine-max-sink-users", cl::init(32),
139 cl::desc("Maximum number of undroppable users for instruction sinking"));
140
142MaxArraySize("instcombine-maxarray-size", cl::init(1024),
143 cl::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.
152static cl::opt<unsigned> ShouldLowerDbgDeclare("instcombine-lower-dbg-declare",
153 cl::Hidden, cl::init(true));
154
155std::optional<Instruction *>
157 // Handle target specific intrinsics
158 if (II.getCalledFunction()->isTargetIntrinsic()) {
159 return TTI.instCombineIntrinsic(*this, II);
160 }
161 return std::nullopt;
162}
163
165 IntrinsicInst &II, APInt DemandedMask, KnownBits &Known,
166 bool &KnownBitsComputed) {
167 // Handle target specific intrinsics
168 if (II.getCalledFunction()->isTargetIntrinsic()) {
169 return TTI.simplifyDemandedUseBitsIntrinsic(*this, II, DemandedMask, Known,
170 KnownBitsComputed);
171 }
172 return std::nullopt;
173}
174
176 IntrinsicInst &II, APInt DemandedElts, APInt &PoisonElts,
177 APInt &PoisonElts2, APInt &PoisonElts3,
178 std::function<void(Instruction *, unsigned, APInt, APInt &)>
179 SimplifyAndSetOp) {
180 // Handle target specific intrinsics
181 if (II.getCalledFunction()->isTargetIntrinsic()) {
183 *this, II, DemandedElts, PoisonElts, PoisonElts2, PoisonElts3,
184 SimplifyAndSetOp);
185 }
186 return std::nullopt;
187}
188
189bool InstCombiner::isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const {
190 return TTI.isValidAddrSpaceCast(FromAS, ToAS);
191}
192
193Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
194 if (!RewriteGEP)
196
198 auto *Inst = dyn_cast<Instruction>(GEP);
199 if (Inst)
201
202 Value *Offset = EmitGEPOffset(GEP);
203 // If a non-trivial GEP has other uses, rewrite it to avoid duplicating
204 // the offset arithmetic.
205 if (Inst && !GEP->hasOneUse() && !GEP->hasAllConstantIndices() &&
206 !GEP->getSourceElementType()->isIntegerTy(8)) {
208 *Inst, Builder.CreateGEP(Builder.getInt8Ty(), GEP->getPointerOperand(),
209 Offset, "", GEP->getNoWrapFlags()));
211 }
212 return Offset;
213}
214
215/// Legal integers and common types are considered desirable. This is used to
216/// avoid creating instructions with types that may not be supported well by the
217/// the backend.
218/// NOTE: This treats i8, i16 and i32 specially because they are common
219/// types in frontend languages.
220bool InstCombinerImpl::isDesirableIntType(unsigned BitWidth) const {
221 switch (BitWidth) {
222 case 8:
223 case 16:
224 case 32:
225 return true;
226 default:
227 return DL.isLegalInteger(BitWidth);
228 }
229}
230
231/// Return true if it is desirable to convert an integer computation from a
232/// given bit width to a new bit width.
233/// We don't want to convert from a legal or desirable type (like i8) to an
234/// illegal type or from a smaller to a larger illegal type. A width of '1'
235/// is always treated as a desirable type because i1 is a fundamental type in
236/// IR, and there are many specialized optimizations for i1 types.
237/// Common/desirable widths are equally treated as legal to convert to, in
238/// order to open up more combining opportunities.
239bool InstCombinerImpl::shouldChangeType(unsigned FromWidth,
240 unsigned ToWidth) const {
241 bool FromLegal = FromWidth == 1 || DL.isLegalInteger(FromWidth);
242 bool ToLegal = ToWidth == 1 || DL.isLegalInteger(ToWidth);
243
244 // Convert to desirable widths even if they are not legal types.
245 // Only shrink types, to prevent infinite loops.
246 if (ToWidth < FromWidth && isDesirableIntType(ToWidth))
247 return true;
248
249 // If this is a legal or desiable integer from type, and the result would be
250 // an illegal type, don't do the transformation.
251 if ((FromLegal || isDesirableIntType(FromWidth)) && !ToLegal)
252 return false;
253
254 // Otherwise, if both are illegal, do not increase the size of the result. We
255 // do allow things like i160 -> i64, but not i64 -> i160.
256 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
257 return false;
258
259 return true;
260}
261
262/// Return true if it is desirable to convert a computation from 'From' to 'To'.
263/// We don't want to convert from a legal to an illegal type or from a smaller
264/// to a larger illegal type. i1 is always treated as a legal type because it is
265/// a fundamental type in IR, and there are many specialized optimizations for
266/// i1 types.
267bool InstCombinerImpl::shouldChangeType(Type *From, Type *To) const {
268 // TODO: This could be extended to allow vectors. Datalayout changes might be
269 // needed to properly support that.
270 if (!From->isIntegerTy() || !To->isIntegerTy())
271 return false;
272
273 unsigned FromWidth = From->getPrimitiveSizeInBits();
274 unsigned ToWidth = To->getPrimitiveSizeInBits();
275 return shouldChangeType(FromWidth, ToWidth);
276}
277
278// Return true, if No Signed Wrap should be maintained for I.
279// The No Signed Wrap flag can be kept if the operation "B (I.getOpcode) C",
280// where both B and C should be ConstantInts, results in a constant that does
281// not overflow. This function only handles the Add and Sub opcodes. For
282// all other opcodes, the function conservatively returns false.
284 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
285 if (!OBO || !OBO->hasNoSignedWrap())
286 return false;
287
288 // We reason about Add and Sub Only.
289 Instruction::BinaryOps Opcode = I.getOpcode();
290 if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
291 return false;
292
293 const APInt *BVal, *CVal;
294 if (!match(B, m_APInt(BVal)) || !match(C, m_APInt(CVal)))
295 return false;
296
297 bool Overflow = false;
298 if (Opcode == Instruction::Add)
299 (void)BVal->sadd_ov(*CVal, Overflow);
300 else
301 (void)BVal->ssub_ov(*CVal, Overflow);
302
303 return !Overflow;
304}
305
307 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
308 return OBO && OBO->hasNoUnsignedWrap();
309}
310
312 auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
313 return OBO && OBO->hasNoSignedWrap();
314}
315
316/// Conservatively clears subclassOptionalData after a reassociation or
317/// commutation. We preserve fast-math flags when applicable as they can be
318/// preserved.
320 FPMathOperator *FPMO = dyn_cast<FPMathOperator>(&I);
321 if (!FPMO) {
322 I.clearSubclassOptionalData();
323 return;
324 }
325
326 FastMathFlags FMF = I.getFastMathFlags();
327 I.clearSubclassOptionalData();
328 I.setFastMathFlags(FMF);
329}
330
331/// Combine constant operands of associative operations either before or after a
332/// cast to eliminate one of the associative operations:
333/// (op (cast (op X, C2)), C1) --> (cast (op X, op (C1, C2)))
334/// (op (cast (op X, C2)), C1) --> (op (cast X), op (C1, C2))
336 InstCombinerImpl &IC) {
337 auto *Cast = dyn_cast<CastInst>(BinOp1->getOperand(0));
338 if (!Cast || !Cast->hasOneUse())
339 return false;
340
341 // TODO: Enhance logic for other casts and remove this check.
342 auto CastOpcode = Cast->getOpcode();
343 if (CastOpcode != Instruction::ZExt)
344 return false;
345
346 // TODO: Enhance logic for other BinOps and remove this check.
347 if (!BinOp1->isBitwiseLogicOp())
348 return false;
349
350 auto AssocOpcode = BinOp1->getOpcode();
351 auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
352 if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
353 return false;
354
355 Constant *C1, *C2;
356 if (!match(BinOp1->getOperand(1), m_Constant(C1)) ||
357 !match(BinOp2->getOperand(1), m_Constant(C2)))
358 return false;
359
360 // TODO: This assumes a zext cast.
361 // Eg, if it was a trunc, we'd cast C1 to the source type because casting C2
362 // to the destination type might lose bits.
363
364 // Fold the constants together in the destination type:
365 // (op (cast (op X, C2)), C1) --> (op (cast X), FoldedC)
366 const DataLayout &DL = IC.getDataLayout();
367 Type *DestTy = C1->getType();
368 Constant *CastC2 = ConstantFoldCastOperand(CastOpcode, C2, DestTy, DL);
369 if (!CastC2)
370 return false;
371 Constant *FoldedC = ConstantFoldBinaryOpOperands(AssocOpcode, C1, CastC2, DL);
372 if (!FoldedC)
373 return false;
374
375 IC.replaceOperand(*Cast, 0, BinOp2->getOperand(0));
376 IC.replaceOperand(*BinOp1, 1, FoldedC);
378 Cast->dropPoisonGeneratingFlags();
379 return true;
380}
381
382// Simplifies IntToPtr/PtrToInt RoundTrip Cast.
383// inttoptr ( ptrtoint (x) ) --> x
384Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(Value *Val) {
385 auto *IntToPtr = dyn_cast<IntToPtrInst>(Val);
386 if (IntToPtr && DL.getTypeSizeInBits(IntToPtr->getDestTy()) ==
387 DL.getTypeSizeInBits(IntToPtr->getSrcTy())) {
388 auto *PtrToInt = dyn_cast<PtrToIntInst>(IntToPtr->getOperand(0));
389 Type *CastTy = IntToPtr->getDestTy();
390 if (PtrToInt &&
391 CastTy->getPointerAddressSpace() ==
392 PtrToInt->getSrcTy()->getPointerAddressSpace() &&
393 DL.getTypeSizeInBits(PtrToInt->getSrcTy()) ==
394 DL.getTypeSizeInBits(PtrToInt->getDestTy()))
395 return PtrToInt->getOperand(0);
396 }
397 return nullptr;
398}
399
400/// This performs a few simplifications for operators that are associative or
401/// commutative:
402///
403/// Commutative operators:
404///
405/// 1. Order operands such that they are listed from right (least complex) to
406/// left (most complex). This puts constants before unary operators before
407/// binary operators.
408///
409/// Associative operators:
410///
411/// 2. Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
412/// 3. Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
413///
414/// Associative and commutative operators:
415///
416/// 4. Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
417/// 5. Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
418/// 6. Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
419/// if C1 and C2 are constants.
421 Instruction::BinaryOps Opcode = I.getOpcode();
422 bool Changed = false;
423
424 do {
425 // Order operands such that they are listed from right (least complex) to
426 // left (most complex). This puts constants before unary operators before
427 // binary operators.
428 if (I.isCommutative() && getComplexity(I.getOperand(0)) <
429 getComplexity(I.getOperand(1)))
430 Changed = !I.swapOperands();
431
432 if (I.isCommutative()) {
433 if (auto Pair = matchSymmetricPair(I.getOperand(0), I.getOperand(1))) {
434 replaceOperand(I, 0, Pair->first);
435 replaceOperand(I, 1, Pair->second);
436 Changed = true;
437 }
438 }
439
440 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(I.getOperand(0));
441 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(I.getOperand(1));
442
443 if (I.isAssociative()) {
444 // Transform: "(A op B) op C" ==> "A op (B op C)" if "B op C" simplifies.
445 if (Op0 && Op0->getOpcode() == Opcode) {
446 Value *A = Op0->getOperand(0);
447 Value *B = Op0->getOperand(1);
448 Value *C = I.getOperand(1);
449
450 // Does "B op C" simplify?
451 if (Value *V = simplifyBinOp(Opcode, B, C, SQ.getWithInstruction(&I))) {
452 // It simplifies to V. Form "A op V".
453 replaceOperand(I, 0, A);
454 replaceOperand(I, 1, V);
455 bool IsNUW = hasNoUnsignedWrap(I) && hasNoUnsignedWrap(*Op0);
456 bool IsNSW = maintainNoSignedWrap(I, B, C) && hasNoSignedWrap(*Op0);
457
458 // Conservatively clear all optional flags since they may not be
459 // preserved by the reassociation. Reset nsw/nuw based on the above
460 // analysis.
462
463 // Note: this is only valid because SimplifyBinOp doesn't look at
464 // the operands to Op0.
465 if (IsNUW)
466 I.setHasNoUnsignedWrap(true);
467
468 if (IsNSW)
469 I.setHasNoSignedWrap(true);
470
471 Changed = true;
472 ++NumReassoc;
473 continue;
474 }
475 }
476
477 // Transform: "A op (B op C)" ==> "(A op B) op C" if "A op B" simplifies.
478 if (Op1 && Op1->getOpcode() == Opcode) {
479 Value *A = I.getOperand(0);
480 Value *B = Op1->getOperand(0);
481 Value *C = Op1->getOperand(1);
482
483 // Does "A op B" simplify?
484 if (Value *V = simplifyBinOp(Opcode, A, B, SQ.getWithInstruction(&I))) {
485 // It simplifies to V. Form "V op C".
486 replaceOperand(I, 0, V);
487 replaceOperand(I, 1, C);
488 // Conservatively clear the optional flags, since they may not be
489 // preserved by the reassociation.
491 Changed = true;
492 ++NumReassoc;
493 continue;
494 }
495 }
496 }
497
498 if (I.isAssociative() && I.isCommutative()) {
499 if (simplifyAssocCastAssoc(&I, *this)) {
500 Changed = true;
501 ++NumReassoc;
502 continue;
503 }
504
505 // Transform: "(A op B) op C" ==> "(C op A) op B" if "C op A" simplifies.
506 if (Op0 && Op0->getOpcode() == Opcode) {
507 Value *A = Op0->getOperand(0);
508 Value *B = Op0->getOperand(1);
509 Value *C = I.getOperand(1);
510
511 // Does "C op A" simplify?
512 if (Value *V = simplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) {
513 // It simplifies to V. Form "V op B".
514 replaceOperand(I, 0, V);
515 replaceOperand(I, 1, B);
516 // Conservatively clear the optional flags, since they may not be
517 // preserved by the reassociation.
519 Changed = true;
520 ++NumReassoc;
521 continue;
522 }
523 }
524
525 // Transform: "A op (B op C)" ==> "B op (C op A)" if "C op A" simplifies.
526 if (Op1 && Op1->getOpcode() == Opcode) {
527 Value *A = I.getOperand(0);
528 Value *B = Op1->getOperand(0);
529 Value *C = Op1->getOperand(1);
530
531 // Does "C op A" simplify?
532 if (Value *V = simplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) {
533 // It simplifies to V. Form "B op V".
534 replaceOperand(I, 0, B);
535 replaceOperand(I, 1, V);
536 // Conservatively clear the optional flags, since they may not be
537 // preserved by the reassociation.
539 Changed = true;
540 ++NumReassoc;
541 continue;
542 }
543 }
544
545 // Transform: "(A op C1) op (B op C2)" ==> "(A op B) op (C1 op C2)"
546 // if C1 and C2 are constants.
547 Value *A, *B;
548 Constant *C1, *C2, *CRes;
549 if (Op0 && Op1 &&
550 Op0->getOpcode() == Opcode && Op1->getOpcode() == Opcode &&
551 match(Op0, m_OneUse(m_BinOp(m_Value(A), m_Constant(C1)))) &&
552 match(Op1, m_OneUse(m_BinOp(m_Value(B), m_Constant(C2)))) &&
553 (CRes = ConstantFoldBinaryOpOperands(Opcode, C1, C2, DL))) {
554 bool IsNUW = hasNoUnsignedWrap(I) &&
555 hasNoUnsignedWrap(*Op0) &&
556 hasNoUnsignedWrap(*Op1);
557 BinaryOperator *NewBO = (IsNUW && Opcode == Instruction::Add) ?
558 BinaryOperator::CreateNUW(Opcode, A, B) :
559 BinaryOperator::Create(Opcode, A, B);
560
561 if (isa<FPMathOperator>(NewBO)) {
562 FastMathFlags Flags = I.getFastMathFlags() &
563 Op0->getFastMathFlags() &
564 Op1->getFastMathFlags();
565 NewBO->setFastMathFlags(Flags);
566 }
567 InsertNewInstWith(NewBO, I.getIterator());
568 NewBO->takeName(Op1);
569 replaceOperand(I, 0, NewBO);
570 replaceOperand(I, 1, CRes);
571 // Conservatively clear the optional flags, since they may not be
572 // preserved by the reassociation.
574 if (IsNUW)
575 I.setHasNoUnsignedWrap(true);
576
577 Changed = true;
578 continue;
579 }
580 }
581
582 // No further simplifications.
583 return Changed;
584 } while (true);
585}
586
587/// Return whether "X LOp (Y ROp Z)" is always equal to
588/// "(X LOp Y) ROp (X LOp Z)".
591 // X & (Y | Z) <--> (X & Y) | (X & Z)
592 // X & (Y ^ Z) <--> (X & Y) ^ (X & Z)
593 if (LOp == Instruction::And)
594 return ROp == Instruction::Or || ROp == Instruction::Xor;
595
596 // X | (Y & Z) <--> (X | Y) & (X | Z)
597 if (LOp == Instruction::Or)
598 return ROp == Instruction::And;
599
600 // X * (Y + Z) <--> (X * Y) + (X * Z)
601 // X * (Y - Z) <--> (X * Y) - (X * Z)
602 if (LOp == Instruction::Mul)
603 return ROp == Instruction::Add || ROp == Instruction::Sub;
604
605 return false;
606}
607
608/// Return whether "(X LOp Y) ROp Z" is always equal to
609/// "(X ROp Z) LOp (Y ROp Z)".
613 return leftDistributesOverRight(ROp, LOp);
614
615 // (X {&|^} Y) >> Z <--> (X >> Z) {&|^} (Y >> Z) for all shifts.
617
618 // TODO: It would be nice to handle division, aka "(X + Y)/Z = X/Z + Y/Z",
619 // but this requires knowing that the addition does not overflow and other
620 // such subtleties.
621}
622
623/// This function returns identity value for given opcode, which can be used to
624/// factor patterns like (X * 2) + X ==> (X * 2) + (X * 1) ==> X * (2 + 1).
626 if (isa<Constant>(V))
627 return nullptr;
628
629 return ConstantExpr::getBinOpIdentity(Opcode, V->getType());
630}
631
632/// This function predicates factorization using distributive laws. By default,
633/// it just returns the 'Op' inputs. But for special-cases like
634/// 'add(shl(X, 5), ...)', this function will have TopOpcode == Instruction::Add
635/// and Op = shl(X, 5). The 'shl' is treated as the more general 'mul X, 32' to
636/// allow more factorization opportunities.
639 Value *&LHS, Value *&RHS, BinaryOperator *OtherOp) {
640 assert(Op && "Expected a binary operator");
641 LHS = Op->getOperand(0);
642 RHS = Op->getOperand(1);
643 if (TopOpcode == Instruction::Add || TopOpcode == Instruction::Sub) {
644 Constant *C;
645 if (match(Op, m_Shl(m_Value(), m_ImmConstant(C)))) {
646 // X << C --> X * (1 << C)
648 Instruction::Shl, ConstantInt::get(Op->getType(), 1), C);
649 assert(RHS && "Constant folding of immediate constants failed");
650 return Instruction::Mul;
651 }
652 // TODO: We can add other conversions e.g. shr => div etc.
653 }
654 if (Instruction::isBitwiseLogicOp(TopOpcode)) {
655 if (OtherOp && OtherOp->getOpcode() == Instruction::AShr &&
657 // lshr nneg C, X --> ashr nneg C, X
658 return Instruction::AShr;
659 }
660 }
661 return Op->getOpcode();
662}
663
664/// This tries to simplify binary operations by factorizing out common terms
665/// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
668 Instruction::BinaryOps InnerOpcode, Value *A,
669 Value *B, Value *C, Value *D) {
670 assert(A && B && C && D && "All values must be provided");
671
672 Value *V = nullptr;
673 Value *RetVal = nullptr;
674 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
675 Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
676
677 // Does "X op' Y" always equal "Y op' X"?
678 bool InnerCommutative = Instruction::isCommutative(InnerOpcode);
679
680 // Does "X op' (Y op Z)" always equal "(X op' Y) op (X op' Z)"?
681 if (leftDistributesOverRight(InnerOpcode, TopLevelOpcode)) {
682 // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
683 // commutative case, "(A op' B) op (C op' A)"?
684 if (A == C || (InnerCommutative && A == D)) {
685 if (A != C)
686 std::swap(C, D);
687 // Consider forming "A op' (B op D)".
688 // If "B op D" simplifies then it can be formed with no cost.
689 V = simplifyBinOp(TopLevelOpcode, B, D, SQ.getWithInstruction(&I));
690
691 // If "B op D" doesn't simplify then only go on if one of the existing
692 // operations "A op' B" and "C op' D" will be zapped as no longer used.
693 if (!V && (LHS->hasOneUse() || RHS->hasOneUse()))
694 V = Builder.CreateBinOp(TopLevelOpcode, B, D, RHS->getName());
695 if (V)
696 RetVal = Builder.CreateBinOp(InnerOpcode, A, V);
697 }
698 }
699
700 // Does "(X op Y) op' Z" always equal "(X op' Z) op (Y op' Z)"?
701 if (!RetVal && rightDistributesOverLeft(TopLevelOpcode, InnerOpcode)) {
702 // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
703 // commutative case, "(A op' B) op (B op' D)"?
704 if (B == D || (InnerCommutative && B == C)) {
705 if (B != D)
706 std::swap(C, D);
707 // Consider forming "(A op C) op' B".
708 // If "A op C" simplifies then it can be formed with no cost.
709 V = simplifyBinOp(TopLevelOpcode, A, C, SQ.getWithInstruction(&I));
710
711 // If "A op C" doesn't simplify then only go on if one of the existing
712 // operations "A op' B" and "C op' D" will be zapped as no longer used.
713 if (!V && (LHS->hasOneUse() || RHS->hasOneUse()))
714 V = Builder.CreateBinOp(TopLevelOpcode, A, C, LHS->getName());
715 if (V)
716 RetVal = Builder.CreateBinOp(InnerOpcode, V, B);
717 }
718 }
719
720 if (!RetVal)
721 return nullptr;
722
723 ++NumFactor;
724 RetVal->takeName(&I);
725
726 // Try to add no-overflow flags to the final value.
727 if (isa<OverflowingBinaryOperator>(RetVal)) {
728 bool HasNSW = false;
729 bool HasNUW = false;
730 if (isa<OverflowingBinaryOperator>(&I)) {
731 HasNSW = I.hasNoSignedWrap();
732 HasNUW = I.hasNoUnsignedWrap();
733 }
734 if (auto *LOBO = dyn_cast<OverflowingBinaryOperator>(LHS)) {
735 HasNSW &= LOBO->hasNoSignedWrap();
736 HasNUW &= LOBO->hasNoUnsignedWrap();
737 }
738
739 if (auto *ROBO = dyn_cast<OverflowingBinaryOperator>(RHS)) {
740 HasNSW &= ROBO->hasNoSignedWrap();
741 HasNUW &= ROBO->hasNoUnsignedWrap();
742 }
743
744 if (TopLevelOpcode == Instruction::Add && InnerOpcode == Instruction::Mul) {
745 // We can propagate 'nsw' if we know that
746 // %Y = mul nsw i16 %X, C
747 // %Z = add nsw i16 %Y, %X
748 // =>
749 // %Z = mul nsw i16 %X, C+1
750 //
751 // iff C+1 isn't INT_MIN
752 const APInt *CInt;
753 if (match(V, m_APInt(CInt)) && !CInt->isMinSignedValue())
754 cast<Instruction>(RetVal)->setHasNoSignedWrap(HasNSW);
755
756 // nuw can be propagated with any constant or nuw value.
757 cast<Instruction>(RetVal)->setHasNoUnsignedWrap(HasNUW);
758 }
759 }
760 return RetVal;
761}
762
763// If `I` has one Const operand and the other matches `(ctpop (not x))`,
764// replace `(ctpop (not x))` with `(sub nuw nsw BitWidth(x), (ctpop x))`.
765// This is only useful is the new subtract can fold so we only handle the
766// following cases:
767// 1) (add/sub/disjoint_or C, (ctpop (not x))
768// -> (add/sub/disjoint_or C', (ctpop x))
769// 1) (cmp pred C, (ctpop (not x))
770// -> (cmp pred C', (ctpop x))
772 unsigned Opc = I->getOpcode();
773 unsigned ConstIdx = 1;
774 switch (Opc) {
775 default:
776 return nullptr;
777 // (ctpop (not x)) <-> (sub nuw nsw BitWidth(x) - (ctpop x))
778 // We can fold the BitWidth(x) with add/sub/icmp as long the other operand
779 // is constant.
780 case Instruction::Sub:
781 ConstIdx = 0;
782 break;
783 case Instruction::ICmp:
784 // Signed predicates aren't correct in some edge cases like for i2 types, as
785 // well since (ctpop x) is known [0, log2(BitWidth(x))] almost all signed
786 // comparisons against it are simplfied to unsigned.
787 if (cast<ICmpInst>(I)->isSigned())
788 return nullptr;
789 break;
790 case Instruction::Or:
791 if (!match(I, m_DisjointOr(m_Value(), m_Value())))
792 return nullptr;
793 [[fallthrough]];
794 case Instruction::Add:
795 break;
796 }
797
798 Value *Op;
799 // Find ctpop.
800 if (!match(I->getOperand(1 - ConstIdx),
801 m_OneUse(m_Intrinsic<Intrinsic::ctpop>(m_Value(Op)))))
802 return nullptr;
803
804 Constant *C;
805 // Check other operand is ImmConstant.
806 if (!match(I->getOperand(ConstIdx), m_ImmConstant(C)))
807 return nullptr;
808
809 Type *Ty = Op->getType();
810 Constant *BitWidthC = ConstantInt::get(Ty, Ty->getScalarSizeInBits());
811 // Need extra check for icmp. Note if this check is true, it generally means
812 // the icmp will simplify to true/false.
813 if (Opc == Instruction::ICmp && !cast<ICmpInst>(I)->isEquality()) {
814 Constant *Cmp =
816 if (!Cmp || !Cmp->isZeroValue())
817 return nullptr;
818 }
819
820 // Check we can invert `(not x)` for free.
821 bool Consumes = false;
822 if (!isFreeToInvert(Op, Op->hasOneUse(), Consumes) || !Consumes)
823 return nullptr;
824 Value *NotOp = getFreelyInverted(Op, Op->hasOneUse(), &Builder);
825 assert(NotOp != nullptr &&
826 "Desync between isFreeToInvert and getFreelyInverted");
827
828 Value *CtpopOfNotOp = Builder.CreateIntrinsic(Ty, Intrinsic::ctpop, NotOp);
829
830 Value *R = nullptr;
831
832 // Do the transformation here to avoid potentially introducing an infinite
833 // loop.
834 switch (Opc) {
835 case Instruction::Sub:
836 R = Builder.CreateAdd(CtpopOfNotOp, ConstantExpr::getSub(C, BitWidthC));
837 break;
838 case Instruction::Or:
839 case Instruction::Add:
840 R = Builder.CreateSub(ConstantExpr::getAdd(C, BitWidthC), CtpopOfNotOp);
841 break;
842 case Instruction::ICmp:
843 R = Builder.CreateICmp(cast<ICmpInst>(I)->getSwappedPredicate(),
844 CtpopOfNotOp, ConstantExpr::getSub(BitWidthC, C));
845 break;
846 default:
847 llvm_unreachable("Unhandled Opcode");
848 }
849 assert(R != nullptr);
850 return replaceInstUsesWith(*I, R);
851}
852
853// (Binop1 (Binop2 (logic_shift X, C), C1), (logic_shift Y, C))
854// IFF
855// 1) the logic_shifts match
856// 2) either both binops are binops and one is `and` or
857// BinOp1 is `and`
858// (logic_shift (inv_logic_shift C1, C), C) == C1 or
859//
860// -> (logic_shift (Binop1 (Binop2 X, inv_logic_shift(C1, C)), Y), C)
861//
862// (Binop1 (Binop2 (logic_shift X, Amt), Mask), (logic_shift Y, Amt))
863// IFF
864// 1) the logic_shifts match
865// 2) BinOp1 == BinOp2 (if BinOp == `add`, then also requires `shl`).
866//
867// -> (BinOp (logic_shift (BinOp X, Y)), Mask)
868//
869// (Binop1 (Binop2 (arithmetic_shift X, Amt), Mask), (arithmetic_shift Y, Amt))
870// IFF
871// 1) Binop1 is bitwise logical operator `and`, `or` or `xor`
872// 2) Binop2 is `not`
873//
874// -> (arithmetic_shift Binop1((not X), Y), Amt)
875
877 const DataLayout &DL = I.getModule()->getDataLayout();
878 auto IsValidBinOpc = [](unsigned Opc) {
879 switch (Opc) {
880 default:
881 return false;
882 case Instruction::And:
883 case Instruction::Or:
884 case Instruction::Xor:
885 case Instruction::Add:
886 // Skip Sub as we only match constant masks which will canonicalize to use
887 // add.
888 return true;
889 }
890 };
891
892 // Check if we can distribute binop arbitrarily. `add` + `lshr` has extra
893 // constraints.
894 auto IsCompletelyDistributable = [](unsigned BinOpc1, unsigned BinOpc2,
895 unsigned ShOpc) {
896 assert(ShOpc != Instruction::AShr);
897 return (BinOpc1 != Instruction::Add && BinOpc2 != Instruction::Add) ||
898 ShOpc == Instruction::Shl;
899 };
900
901 auto GetInvShift = [](unsigned ShOpc) {
902 assert(ShOpc != Instruction::AShr);
903 return ShOpc == Instruction::LShr ? Instruction::Shl : Instruction::LShr;
904 };
905
906 auto CanDistributeBinops = [&](unsigned BinOpc1, unsigned BinOpc2,
907 unsigned ShOpc, Constant *CMask,
908 Constant *CShift) {
909 // If the BinOp1 is `and` we don't need to check the mask.
910 if (BinOpc1 == Instruction::And)
911 return true;
912
913 // For all other possible transfers we need complete distributable
914 // binop/shift (anything but `add` + `lshr`).
915 if (!IsCompletelyDistributable(BinOpc1, BinOpc2, ShOpc))
916 return false;
917
918 // If BinOp2 is `and`, any mask works (this only really helps for non-splat
919 // vecs, otherwise the mask will be simplified and the following check will
920 // handle it).
921 if (BinOpc2 == Instruction::And)
922 return true;
923
924 // Otherwise, need mask that meets the below requirement.
925 // (logic_shift (inv_logic_shift Mask, ShAmt), ShAmt) == Mask
926 Constant *MaskInvShift =
927 ConstantFoldBinaryOpOperands(GetInvShift(ShOpc), CMask, CShift, DL);
928 return ConstantFoldBinaryOpOperands(ShOpc, MaskInvShift, CShift, DL) ==
929 CMask;
930 };
931
932 auto MatchBinOp = [&](unsigned ShOpnum) -> Instruction * {
933 Constant *CMask, *CShift;
934 Value *X, *Y, *ShiftedX, *Mask, *Shift;
935 if (!match(I.getOperand(ShOpnum),
936 m_OneUse(m_Shift(m_Value(Y), m_Value(Shift)))))
937 return nullptr;
938 if (!match(I.getOperand(1 - ShOpnum),
939 m_BinOp(m_Value(ShiftedX), m_Value(Mask))))
940 return nullptr;
941
942 if (!match(ShiftedX, m_OneUse(m_Shift(m_Value(X), m_Specific(Shift)))))
943 return nullptr;
944
945 // Make sure we are matching instruction shifts and not ConstantExpr
946 auto *IY = dyn_cast<Instruction>(I.getOperand(ShOpnum));
947 auto *IX = dyn_cast<Instruction>(ShiftedX);
948 if (!IY || !IX)
949 return nullptr;
950
951 // LHS and RHS need same shift opcode
952 unsigned ShOpc = IY->getOpcode();
953 if (ShOpc != IX->getOpcode())
954 return nullptr;
955
956 // Make sure binop is real instruction and not ConstantExpr
957 auto *BO2 = dyn_cast<Instruction>(I.getOperand(1 - ShOpnum));
958 if (!BO2)
959 return nullptr;
960
961 unsigned BinOpc = BO2->getOpcode();
962 // Make sure we have valid binops.
963 if (!IsValidBinOpc(I.getOpcode()) || !IsValidBinOpc(BinOpc))
964 return nullptr;
965
966 if (ShOpc == Instruction::AShr) {
967 if (Instruction::isBitwiseLogicOp(I.getOpcode()) &&
968 BinOpc == Instruction::Xor && match(Mask, m_AllOnes())) {
969 Value *NotX = Builder.CreateNot(X);
970 Value *NewBinOp = Builder.CreateBinOp(I.getOpcode(), Y, NotX);
972 static_cast<Instruction::BinaryOps>(ShOpc), NewBinOp, Shift);
973 }
974
975 return nullptr;
976 }
977
978 // If BinOp1 == BinOp2 and it's bitwise or shl with add, then just
979 // distribute to drop the shift irrelevant of constants.
980 if (BinOpc == I.getOpcode() &&
981 IsCompletelyDistributable(I.getOpcode(), BinOpc, ShOpc)) {
982 Value *NewBinOp2 = Builder.CreateBinOp(I.getOpcode(), X, Y);
983 Value *NewBinOp1 = Builder.CreateBinOp(
984 static_cast<Instruction::BinaryOps>(ShOpc), NewBinOp2, Shift);
985 return BinaryOperator::Create(I.getOpcode(), NewBinOp1, Mask);
986 }
987
988 // Otherwise we can only distribute by constant shifting the mask, so
989 // ensure we have constants.
990 if (!match(Shift, m_ImmConstant(CShift)))
991 return nullptr;
992 if (!match(Mask, m_ImmConstant(CMask)))
993 return nullptr;
994
995 // Check if we can distribute the binops.
996 if (!CanDistributeBinops(I.getOpcode(), BinOpc, ShOpc, CMask, CShift))
997 return nullptr;
998
999 Constant *NewCMask =
1000 ConstantFoldBinaryOpOperands(GetInvShift(ShOpc), CMask, CShift, DL);
1001 Value *NewBinOp2 = Builder.CreateBinOp(
1002 static_cast<Instruction::BinaryOps>(BinOpc), X, NewCMask);
1003 Value *NewBinOp1 = Builder.CreateBinOp(I.getOpcode(), Y, NewBinOp2);
1004 return BinaryOperator::Create(static_cast<Instruction::BinaryOps>(ShOpc),
1005 NewBinOp1, CShift);
1006 };
1007
1008 if (Instruction *R = MatchBinOp(0))
1009 return R;
1010 return MatchBinOp(1);
1011}
1012
1013// (Binop (zext C), (select C, T, F))
1014// -> (select C, (binop 1, T), (binop 0, F))
1015//
1016// (Binop (sext C), (select C, T, F))
1017// -> (select C, (binop -1, T), (binop 0, F))
1018//
1019// Attempt to simplify binary operations into a select with folded args, when
1020// one operand of the binop is a select instruction and the other operand is a
1021// zext/sext extension, whose value is the select condition.
1024 // TODO: this simplification may be extended to any speculatable instruction,
1025 // not just binops, and would possibly be handled better in FoldOpIntoSelect.
1026 Instruction::BinaryOps Opc = I.getOpcode();
1027 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1028 Value *A, *CondVal, *TrueVal, *FalseVal;
1029 Value *CastOp;
1030
1031 auto MatchSelectAndCast = [&](Value *CastOp, Value *SelectOp) {
1032 return match(CastOp, m_ZExtOrSExt(m_Value(A))) &&
1033 A->getType()->getScalarSizeInBits() == 1 &&
1034 match(SelectOp, m_Select(m_Value(CondVal), m_Value(TrueVal),
1035 m_Value(FalseVal)));
1036 };
1037
1038 // Make sure one side of the binop is a select instruction, and the other is a
1039 // zero/sign extension operating on a i1.
1040 if (MatchSelectAndCast(LHS, RHS))
1041 CastOp = LHS;
1042 else if (MatchSelectAndCast(RHS, LHS))
1043 CastOp = RHS;
1044 else
1045 return nullptr;
1046
1047 auto NewFoldedConst = [&](bool IsTrueArm, Value *V) {
1048 bool IsCastOpRHS = (CastOp == RHS);
1049 bool IsZExt = isa<ZExtInst>(CastOp);
1050 Constant *C;
1051
1052 if (IsTrueArm) {
1053 C = Constant::getNullValue(V->getType());
1054 } else if (IsZExt) {
1055 unsigned BitWidth = V->getType()->getScalarSizeInBits();
1056 C = Constant::getIntegerValue(V->getType(), APInt(BitWidth, 1));
1057 } else {
1058 C = Constant::getAllOnesValue(V->getType());
1059 }
1060
1061 return IsCastOpRHS ? Builder.CreateBinOp(Opc, V, C)
1062 : Builder.CreateBinOp(Opc, C, V);
1063 };
1064
1065 // If the value used in the zext/sext is the select condition, or the negated
1066 // of the select condition, the binop can be simplified.
1067 if (CondVal == A) {
1068 Value *NewTrueVal = NewFoldedConst(false, TrueVal);
1069 return SelectInst::Create(CondVal, NewTrueVal,
1070 NewFoldedConst(true, FalseVal));
1071 }
1072
1073 if (match(A, m_Not(m_Specific(CondVal)))) {
1074 Value *NewTrueVal = NewFoldedConst(true, TrueVal);
1075 return SelectInst::Create(CondVal, NewTrueVal,
1076 NewFoldedConst(false, FalseVal));
1077 }
1078
1079 return nullptr;
1080}
1081
1083 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1084 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
1085 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
1086 Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
1087 Value *A, *B, *C, *D;
1088 Instruction::BinaryOps LHSOpcode, RHSOpcode;
1089
1090 if (Op0)
1091 LHSOpcode = getBinOpsForFactorization(TopLevelOpcode, Op0, A, B, Op1);
1092 if (Op1)
1093 RHSOpcode = getBinOpsForFactorization(TopLevelOpcode, Op1, C, D, Op0);
1094
1095 // The instruction has the form "(A op' B) op (C op' D)". Try to factorize
1096 // a common term.
1097 if (Op0 && Op1 && LHSOpcode == RHSOpcode)
1098 if (Value *V = tryFactorization(I, SQ, Builder, LHSOpcode, A, B, C, D))
1099 return V;
1100
1101 // The instruction has the form "(A op' B) op (C)". Try to factorize common
1102 // term.
1103 if (Op0)
1104 if (Value *Ident = getIdentityValue(LHSOpcode, RHS))
1105 if (Value *V =
1106 tryFactorization(I, SQ, Builder, LHSOpcode, A, B, RHS, Ident))
1107 return V;
1108
1109 // The instruction has the form "(B) op (C op' D)". Try to factorize common
1110 // term.
1111 if (Op1)
1112 if (Value *Ident = getIdentityValue(RHSOpcode, LHS))
1113 if (Value *V =
1114 tryFactorization(I, SQ, Builder, RHSOpcode, LHS, Ident, C, D))
1115 return V;
1116
1117 return nullptr;
1118}
1119
1120/// This tries to simplify binary operations which some other binary operation
1121/// distributes over either by factorizing out common terms
1122/// (eg "(A*B)+(A*C)" -> "A*(B+C)") or expanding out if this results in
1123/// simplifications (eg: "A & (B | C) -> (A&B) | (A&C)" if this is a win).
1124/// Returns the simplified value, or null if it didn't simplify.
1126 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1127 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
1128 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
1129 Instruction::BinaryOps TopLevelOpcode = I.getOpcode();
1130
1131 // Factorization.
1132 if (Value *R = tryFactorizationFolds(I))
1133 return R;
1134
1135 // Expansion.
1136 if (Op0 && rightDistributesOverLeft(Op0->getOpcode(), TopLevelOpcode)) {
1137 // The instruction has the form "(A op' B) op C". See if expanding it out
1138 // to "(A op C) op' (B op C)" results in simplifications.
1139 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
1140 Instruction::BinaryOps InnerOpcode = Op0->getOpcode(); // op'
1141
1142 // Disable the use of undef because it's not safe to distribute undef.
1143 auto SQDistributive = SQ.getWithInstruction(&I).getWithoutUndef();
1144 Value *L = simplifyBinOp(TopLevelOpcode, A, C, SQDistributive);
1145 Value *R = simplifyBinOp(TopLevelOpcode, B, C, SQDistributive);
1146
1147 // Do "A op C" and "B op C" both simplify?
1148 if (L && R) {
1149 // They do! Return "L op' R".
1150 ++NumExpand;
1151 C = Builder.CreateBinOp(InnerOpcode, L, R);
1152 C->takeName(&I);
1153 return C;
1154 }
1155
1156 // Does "A op C" simplify to the identity value for the inner opcode?
1157 if (L && L == ConstantExpr::getBinOpIdentity(InnerOpcode, L->getType())) {
1158 // They do! Return "B op C".
1159 ++NumExpand;
1160 C = Builder.CreateBinOp(TopLevelOpcode, B, C);
1161 C->takeName(&I);
1162 return C;
1163 }
1164
1165 // Does "B op C" simplify to the identity value for the inner opcode?
1166 if (R && R == ConstantExpr::getBinOpIdentity(InnerOpcode, R->getType())) {
1167 // They do! Return "A op C".
1168 ++NumExpand;
1169 C = Builder.CreateBinOp(TopLevelOpcode, A, C);
1170 C->takeName(&I);
1171 return C;
1172 }
1173 }
1174
1175 if (Op1 && leftDistributesOverRight(TopLevelOpcode, Op1->getOpcode())) {
1176 // The instruction has the form "A op (B op' C)". See if expanding it out
1177 // to "(A op B) op' (A op C)" results in simplifications.
1178 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
1179 Instruction::BinaryOps InnerOpcode = Op1->getOpcode(); // op'
1180
1181 // Disable the use of undef because it's not safe to distribute undef.
1182 auto SQDistributive = SQ.getWithInstruction(&I).getWithoutUndef();
1183 Value *L = simplifyBinOp(TopLevelOpcode, A, B, SQDistributive);
1184 Value *R = simplifyBinOp(TopLevelOpcode, A, C, SQDistributive);
1185
1186 // Do "A op B" and "A op C" both simplify?
1187 if (L && R) {
1188 // They do! Return "L op' R".
1189 ++NumExpand;
1190 A = Builder.CreateBinOp(InnerOpcode, L, R);
1191 A->takeName(&I);
1192 return A;
1193 }
1194
1195 // Does "A op B" simplify to the identity value for the inner opcode?
1196 if (L && L == ConstantExpr::getBinOpIdentity(InnerOpcode, L->getType())) {
1197 // They do! Return "A op C".
1198 ++NumExpand;
1199 A = Builder.CreateBinOp(TopLevelOpcode, A, C);
1200 A->takeName(&I);
1201 return A;
1202 }
1203
1204 // Does "A op C" simplify to the identity value for the inner opcode?
1205 if (R && R == ConstantExpr::getBinOpIdentity(InnerOpcode, R->getType())) {
1206 // They do! Return "A op B".
1207 ++NumExpand;
1208 A = Builder.CreateBinOp(TopLevelOpcode, A, B);
1209 A->takeName(&I);
1210 return A;
1211 }
1212 }
1213
1215}
1216
1217static std::optional<std::pair<Value *, Value *>>
1219 if (LHS->getParent() != RHS->getParent())
1220 return std::nullopt;
1221
1222 if (LHS->getNumIncomingValues() < 2)
1223 return std::nullopt;
1224
1225 if (!equal(LHS->blocks(), RHS->blocks()))
1226 return std::nullopt;
1227
1228 Value *L0 = LHS->getIncomingValue(0);
1229 Value *R0 = RHS->getIncomingValue(0);
1230
1231 for (unsigned I = 1, E = LHS->getNumIncomingValues(); I != E; ++I) {
1232 Value *L1 = LHS->getIncomingValue(I);
1233 Value *R1 = RHS->getIncomingValue(I);
1234
1235 if ((L0 == L1 && R0 == R1) || (L0 == R1 && R0 == L1))
1236 continue;
1237
1238 return std::nullopt;
1239 }
1240
1241 return std::optional(std::pair(L0, R0));
1242}
1243
1244std::optional<std::pair<Value *, Value *>>
1245InstCombinerImpl::matchSymmetricPair(Value *LHS, Value *RHS) {
1246 Instruction *LHSInst = dyn_cast<Instruction>(LHS);
1247 Instruction *RHSInst = dyn_cast<Instruction>(RHS);
1248 if (!LHSInst || !RHSInst || LHSInst->getOpcode() != RHSInst->getOpcode())
1249 return std::nullopt;
1250 switch (LHSInst->getOpcode()) {
1251 case Instruction::PHI:
1252 return matchSymmetricPhiNodesPair(cast<PHINode>(LHS), cast<PHINode>(RHS));
1253 case Instruction::Select: {
1254 Value *Cond = LHSInst->getOperand(0);
1255 Value *TrueVal = LHSInst->getOperand(1);
1256 Value *FalseVal = LHSInst->getOperand(2);
1257 if (Cond == RHSInst->getOperand(0) && TrueVal == RHSInst->getOperand(2) &&
1258 FalseVal == RHSInst->getOperand(1))
1259 return std::pair(TrueVal, FalseVal);
1260 return std::nullopt;
1261 }
1262 case Instruction::Call: {
1263 // Match min(a, b) and max(a, b)
1264 MinMaxIntrinsic *LHSMinMax = dyn_cast<MinMaxIntrinsic>(LHSInst);
1265 MinMaxIntrinsic *RHSMinMax = dyn_cast<MinMaxIntrinsic>(RHSInst);
1266 if (LHSMinMax && RHSMinMax &&
1267 LHSMinMax->getPredicate() ==
1269 ((LHSMinMax->getLHS() == RHSMinMax->getLHS() &&
1270 LHSMinMax->getRHS() == RHSMinMax->getRHS()) ||
1271 (LHSMinMax->getLHS() == RHSMinMax->getRHS() &&
1272 LHSMinMax->getRHS() == RHSMinMax->getLHS())))
1273 return std::pair(LHSMinMax->getLHS(), LHSMinMax->getRHS());
1274 return std::nullopt;
1275 }
1276 default:
1277 return std::nullopt;
1278 }
1279}
1280
1282 Value *LHS,
1283 Value *RHS) {
1284 Value *A, *B, *C, *D, *E, *F;
1285 bool LHSIsSelect = match(LHS, m_Select(m_Value(A), m_Value(B), m_Value(C)));
1286 bool RHSIsSelect = match(RHS, m_Select(m_Value(D), m_Value(E), m_Value(F)));
1287 if (!LHSIsSelect && !RHSIsSelect)
1288 return nullptr;
1289
1290 FastMathFlags FMF;
1292 if (isa<FPMathOperator>(&I)) {
1293 FMF = I.getFastMathFlags();
1295 }
1296
1297 Instruction::BinaryOps Opcode = I.getOpcode();
1299
1300 Value *Cond, *True = nullptr, *False = nullptr;
1301
1302 // Special-case for add/negate combination. Replace the zero in the negation
1303 // with the trailing add operand:
1304 // (Cond ? TVal : -N) + Z --> Cond ? True : (Z - N)
1305 // (Cond ? -N : FVal) + Z --> Cond ? (Z - N) : False
1306 auto foldAddNegate = [&](Value *TVal, Value *FVal, Value *Z) -> Value * {
1307 // We need an 'add' and exactly 1 arm of the select to have been simplified.
1308 if (Opcode != Instruction::Add || (!True && !False) || (True && False))
1309 return nullptr;
1310
1311 Value *N;
1312 if (True && match(FVal, m_Neg(m_Value(N)))) {
1313 Value *Sub = Builder.CreateSub(Z, N);
1314 return Builder.CreateSelect(Cond, True, Sub, I.getName());
1315 }
1316 if (False && match(TVal, m_Neg(m_Value(N)))) {
1317 Value *Sub = Builder.CreateSub(Z, N);
1318 return Builder.CreateSelect(Cond, Sub, False, I.getName());
1319 }
1320 return nullptr;
1321 };
1322
1323 if (LHSIsSelect && RHSIsSelect && A == D) {
1324 // (A ? B : C) op (A ? E : F) -> A ? (B op E) : (C op F)
1325 Cond = A;
1326 True = simplifyBinOp(Opcode, B, E, FMF, Q);
1327 False = simplifyBinOp(Opcode, C, F, FMF, Q);
1328
1329 if (LHS->hasOneUse() && RHS->hasOneUse()) {
1330 if (False && !True)
1331 True = Builder.CreateBinOp(Opcode, B, E);
1332 else if (True && !False)
1333 False = Builder.CreateBinOp(Opcode, C, F);
1334 }
1335 } else if (LHSIsSelect && LHS->hasOneUse()) {
1336 // (A ? B : C) op Y -> A ? (B op Y) : (C op Y)
1337 Cond = A;
1338 True = simplifyBinOp(Opcode, B, RHS, FMF, Q);
1339 False = simplifyBinOp(Opcode, C, RHS, FMF, Q);
1340 if (Value *NewSel = foldAddNegate(B, C, RHS))
1341 return NewSel;
1342 } else if (RHSIsSelect && RHS->hasOneUse()) {
1343 // X op (D ? E : F) -> D ? (X op E) : (X op F)
1344 Cond = D;
1345 True = simplifyBinOp(Opcode, LHS, E, FMF, Q);
1346 False = simplifyBinOp(Opcode, LHS, F, FMF, Q);
1347 if (Value *NewSel = foldAddNegate(E, F, LHS))
1348 return NewSel;
1349 }
1350
1351 if (!True || !False)
1352 return nullptr;
1353
1354 Value *SI = Builder.CreateSelect(Cond, True, False);
1355 SI->takeName(&I);
1356 return SI;
1357}
1358
1359/// Freely adapt every user of V as-if V was changed to !V.
1360/// WARNING: only if canFreelyInvertAllUsersOf() said this can be done.
1362 assert(!isa<Constant>(I) && "Shouldn't invert users of constant");
1363 for (User *U : make_early_inc_range(I->users())) {
1364 if (U == IgnoredUser)
1365 continue; // Don't consider this user.
1366 switch (cast<Instruction>(U)->getOpcode()) {
1367 case Instruction::Select: {
1368 auto *SI = cast<SelectInst>(U);
1369 SI->swapValues();
1370 SI->swapProfMetadata();
1371 break;
1372 }
1373 case Instruction::Br: {
1374 BranchInst *BI = cast<BranchInst>(U);
1375 BI->swapSuccessors(); // swaps prof metadata too
1376 if (BPI)
1378 break;
1379 }
1380 case Instruction::Xor:
1381 replaceInstUsesWith(cast<Instruction>(*U), I);
1382 // Add to worklist for DCE.
1383 addToWorklist(cast<Instruction>(U));
1384 break;
1385 default:
1386 llvm_unreachable("Got unexpected user - out of sync with "
1387 "canFreelyInvertAllUsersOf() ?");
1388 }
1389 }
1390}
1391
1392/// Given a 'sub' instruction, return the RHS of the instruction if the LHS is a
1393/// constant zero (which is the 'negate' form).
1394Value *InstCombinerImpl::dyn_castNegVal(Value *V) const {
1395 Value *NegV;
1396 if (match(V, m_Neg(m_Value(NegV))))
1397 return NegV;
1398
1399 // Constants can be considered to be negated values if they can be folded.
1400 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
1401 return ConstantExpr::getNeg(C);
1402
1403 if (ConstantDataVector *C = dyn_cast<ConstantDataVector>(V))
1404 if (C->getType()->getElementType()->isIntegerTy())
1405 return ConstantExpr::getNeg(C);
1406
1407 if (ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
1408 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1409 Constant *Elt = CV->getAggregateElement(i);
1410 if (!Elt)
1411 return nullptr;
1412
1413 if (isa<UndefValue>(Elt))
1414 continue;
1415
1416 if (!isa<ConstantInt>(Elt))
1417 return nullptr;
1418 }
1419 return ConstantExpr::getNeg(CV);
1420 }
1421
1422 // Negate integer vector splats.
1423 if (auto *CV = dyn_cast<Constant>(V))
1424 if (CV->getType()->isVectorTy() &&
1425 CV->getType()->getScalarType()->isIntegerTy() && CV->getSplatValue())
1426 return ConstantExpr::getNeg(CV);
1427
1428 return nullptr;
1429}
1430
1431// Try to fold:
1432// 1) (fp_binop ({s|u}itofp x), ({s|u}itofp y))
1433// -> ({s|u}itofp (int_binop x, y))
1434// 2) (fp_binop ({s|u}itofp x), FpC)
1435// -> ({s|u}itofp (int_binop x, (fpto{s|u}i FpC)))
1436//
1437// Assuming the sign of the cast for x/y is `OpsFromSigned`.
1438Instruction *InstCombinerImpl::foldFBinOpOfIntCastsFromSign(
1439 BinaryOperator &BO, bool OpsFromSigned, std::array<Value *, 2> IntOps,
1441
1442 Type *FPTy = BO.getType();
1443 Type *IntTy = IntOps[0]->getType();
1444
1445 unsigned IntSz = IntTy->getScalarSizeInBits();
1446 // This is the maximum number of inuse bits by the integer where the int -> fp
1447 // casts are exact.
1448 unsigned MaxRepresentableBits =
1450
1451 // Preserve known number of leading bits. This can allow us to trivial nsw/nuw
1452 // checks later on.
1453 unsigned NumUsedLeadingBits[2] = {IntSz, IntSz};
1454
1455 // NB: This only comes up if OpsFromSigned is true, so there is no need to
1456 // cache if between calls to `foldFBinOpOfIntCastsFromSign`.
1457 auto IsNonZero = [&](unsigned OpNo) -> bool {
1458 if (OpsKnown[OpNo].hasKnownBits() &&
1459 OpsKnown[OpNo].getKnownBits(SQ).isNonZero())
1460 return true;
1461 return isKnownNonZero(IntOps[OpNo], SQ);
1462 };
1463
1464 auto IsNonNeg = [&](unsigned OpNo) -> bool {
1465 // NB: This matches the impl in ValueTracking, we just try to use cached
1466 // knownbits here. If we ever start supporting WithCache for
1467 // `isKnownNonNegative`, change this to an explicit call.
1468 return OpsKnown[OpNo].getKnownBits(SQ).isNonNegative();
1469 };
1470
1471 // Check if we know for certain that ({s|u}itofp op) is exact.
1472 auto IsValidPromotion = [&](unsigned OpNo) -> bool {
1473 // Can we treat this operand as the desired sign?
1474 if (OpsFromSigned != isa<SIToFPInst>(BO.getOperand(OpNo)) &&
1475 !IsNonNeg(OpNo))
1476 return false;
1477
1478 // If fp precision >= bitwidth(op) then its exact.
1479 // NB: This is slightly conservative for `sitofp`. For signed conversion, we
1480 // can handle `MaxRepresentableBits == IntSz - 1` as the sign bit will be
1481 // handled specially. We can't, however, increase the bound arbitrarily for
1482 // `sitofp` as for larger sizes, it won't sign extend.
1483 if (MaxRepresentableBits < IntSz) {
1484 // Otherwise if its signed cast check that fp precisions >= bitwidth(op) -
1485 // numSignBits(op).
1486 // TODO: If we add support for `WithCache` in `ComputeNumSignBits`, change
1487 // `IntOps[OpNo]` arguments to `KnownOps[OpNo]`.
1488 if (OpsFromSigned)
1489 NumUsedLeadingBits[OpNo] = IntSz - ComputeNumSignBits(IntOps[OpNo]);
1490 // Finally for unsigned check that fp precision >= bitwidth(op) -
1491 // numLeadingZeros(op).
1492 else {
1493 NumUsedLeadingBits[OpNo] =
1494 IntSz - OpsKnown[OpNo].getKnownBits(SQ).countMinLeadingZeros();
1495 }
1496 }
1497 // NB: We could also check if op is known to be a power of 2 or zero (which
1498 // will always be representable). Its unlikely, however, that is we are
1499 // unable to bound op in any way we will be able to pass the overflow checks
1500 // later on.
1501
1502 if (MaxRepresentableBits < NumUsedLeadingBits[OpNo])
1503 return false;
1504 // Signed + Mul also requires that op is non-zero to avoid -0 cases.
1505 return !OpsFromSigned || BO.getOpcode() != Instruction::FMul ||
1506 IsNonZero(OpNo);
1507 };
1508
1509 // If we have a constant rhs, see if we can losslessly convert it to an int.
1510 if (Op1FpC != nullptr) {
1511 // Signed + Mul req non-zero
1512 if (OpsFromSigned && BO.getOpcode() == Instruction::FMul &&
1513 !match(Op1FpC, m_NonZeroFP()))
1514 return nullptr;
1515
1517 OpsFromSigned ? Instruction::FPToSI : Instruction::FPToUI, Op1FpC,
1518 IntTy, DL);
1519 if (Op1IntC == nullptr)
1520 return nullptr;
1521 if (ConstantFoldCastOperand(OpsFromSigned ? Instruction::SIToFP
1522 : Instruction::UIToFP,
1523 Op1IntC, FPTy, DL) != Op1FpC)
1524 return nullptr;
1525
1526 // First try to keep sign of cast the same.
1527 IntOps[1] = Op1IntC;
1528 }
1529
1530 // Ensure lhs/rhs integer types match.
1531 if (IntTy != IntOps[1]->getType())
1532 return nullptr;
1533
1534 if (Op1FpC == nullptr) {
1535 if (!IsValidPromotion(1))
1536 return nullptr;
1537 }
1538 if (!IsValidPromotion(0))
1539 return nullptr;
1540
1541 // Final we check if the integer version of the binop will not overflow.
1543 // Because of the precision check, we can often rule out overflows.
1544 bool NeedsOverflowCheck = true;
1545 // Try to conservatively rule out overflow based on the already done precision
1546 // checks.
1547 unsigned OverflowMaxOutputBits = OpsFromSigned ? 2 : 1;
1548 unsigned OverflowMaxCurBits =
1549 std::max(NumUsedLeadingBits[0], NumUsedLeadingBits[1]);
1550 bool OutputSigned = OpsFromSigned;
1551 switch (BO.getOpcode()) {
1552 case Instruction::FAdd:
1553 IntOpc = Instruction::Add;
1554 OverflowMaxOutputBits += OverflowMaxCurBits;
1555 break;
1556 case Instruction::FSub:
1557 IntOpc = Instruction::Sub;
1558 OverflowMaxOutputBits += OverflowMaxCurBits;
1559 break;
1560 case Instruction::FMul:
1561 IntOpc = Instruction::Mul;
1562 OverflowMaxOutputBits += OverflowMaxCurBits * 2;
1563 break;
1564 default:
1565 llvm_unreachable("Unsupported binop");
1566 }
1567 // The precision check may have already ruled out overflow.
1568 if (OverflowMaxOutputBits < IntSz) {
1569 NeedsOverflowCheck = false;
1570 // We can bound unsigned overflow from sub to in range signed value (this is
1571 // what allows us to avoid the overflow check for sub).
1572 if (IntOpc == Instruction::Sub)
1573 OutputSigned = true;
1574 }
1575
1576 // Precision check did not rule out overflow, so need to check.
1577 // TODO: If we add support for `WithCache` in `willNotOverflow`, change
1578 // `IntOps[...]` arguments to `KnownOps[...]`.
1579 if (NeedsOverflowCheck &&
1580 !willNotOverflow(IntOpc, IntOps[0], IntOps[1], BO, OutputSigned))
1581 return nullptr;
1582
1583 Value *IntBinOp = Builder.CreateBinOp(IntOpc, IntOps[0], IntOps[1]);
1584 if (auto *IntBO = dyn_cast<BinaryOperator>(IntBinOp)) {
1585 IntBO->setHasNoSignedWrap(OutputSigned);
1586 IntBO->setHasNoUnsignedWrap(!OutputSigned);
1587 }
1588 if (OutputSigned)
1589 return new SIToFPInst(IntBinOp, FPTy);
1590 return new UIToFPInst(IntBinOp, FPTy);
1591}
1592
1593// Try to fold:
1594// 1) (fp_binop ({s|u}itofp x), ({s|u}itofp y))
1595// -> ({s|u}itofp (int_binop x, y))
1596// 2) (fp_binop ({s|u}itofp x), FpC)
1597// -> ({s|u}itofp (int_binop x, (fpto{s|u}i FpC)))
1598Instruction *InstCombinerImpl::foldFBinOpOfIntCasts(BinaryOperator &BO) {
1599 std::array<Value *, 2> IntOps = {nullptr, nullptr};
1600 Constant *Op1FpC = nullptr;
1601 // Check for:
1602 // 1) (binop ({s|u}itofp x), ({s|u}itofp y))
1603 // 2) (binop ({s|u}itofp x), FpC)
1604 if (!match(BO.getOperand(0), m_SIToFP(m_Value(IntOps[0]))) &&
1605 !match(BO.getOperand(0), m_UIToFP(m_Value(IntOps[0]))))
1606 return nullptr;
1607
1608 if (!match(BO.getOperand(1), m_Constant(Op1FpC)) &&
1609 !match(BO.getOperand(1), m_SIToFP(m_Value(IntOps[1]))) &&
1610 !match(BO.getOperand(1), m_UIToFP(m_Value(IntOps[1]))))
1611 return nullptr;
1612
1613 // Cache KnownBits a bit to potentially save some analysis.
1614 SmallVector<WithCache<const Value *>, 2> OpsKnown = {IntOps[0], IntOps[1]};
1615
1616 // Try treating x/y as coming from both `uitofp` and `sitofp`. There are
1617 // different constraints depending on the sign of the cast.
1618 // NB: `(uitofp nneg X)` == `(sitofp nneg X)`.
1619 if (Instruction *R = foldFBinOpOfIntCastsFromSign(BO, /*OpsFromSigned=*/false,
1620 IntOps, Op1FpC, OpsKnown))
1621 return R;
1622 return foldFBinOpOfIntCastsFromSign(BO, /*OpsFromSigned=*/true, IntOps,
1623 Op1FpC, OpsKnown);
1624}
1625
1626/// A binop with a constant operand and a sign-extended boolean operand may be
1627/// converted into a select of constants by applying the binary operation to
1628/// the constant with the two possible values of the extended boolean (0 or -1).
1629Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
1630 // TODO: Handle non-commutative binop (constant is operand 0).
1631 // TODO: Handle zext.
1632 // TODO: Peek through 'not' of cast.
1633 Value *BO0 = BO.getOperand(0);
1634 Value *BO1 = BO.getOperand(1);
1635 Value *X;
1636 Constant *C;
1637 if (!match(BO0, m_SExt(m_Value(X))) || !match(BO1, m_ImmConstant(C)) ||
1638 !X->getType()->isIntOrIntVectorTy(1))
1639 return nullptr;
1640
1641 // bo (sext i1 X), C --> select X, (bo -1, C), (bo 0, C)
1644 Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
1645 Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
1646 return SelectInst::Create(X, TVal, FVal);
1647}
1648
1650 SelectInst *SI,
1651 bool IsTrueArm) {
1652 SmallVector<Constant *> ConstOps;
1653 for (Value *Op : I.operands()) {
1654 CmpInst::Predicate Pred;
1655 Constant *C = nullptr;
1656 if (Op == SI) {
1657 C = dyn_cast<Constant>(IsTrueArm ? SI->getTrueValue()
1658 : SI->getFalseValue());
1659 } else if (match(SI->getCondition(),
1660 m_ICmp(Pred, m_Specific(Op), m_Constant(C))) &&
1661 Pred == (IsTrueArm ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
1663 // Pass
1664 } else {
1665 C = dyn_cast<Constant>(Op);
1666 }
1667 if (C == nullptr)
1668 return nullptr;
1669
1670 ConstOps.push_back(C);
1671 }
1672
1673 return ConstantFoldInstOperands(&I, ConstOps, I.getModule()->getDataLayout());
1674}
1675
1677 Value *NewOp, InstCombiner &IC) {
1678 Instruction *Clone = I.clone();
1679 Clone->replaceUsesOfWith(SI, NewOp);
1681 IC.InsertNewInstBefore(Clone, SI->getIterator());
1682 return Clone;
1683}
1684
1686 bool FoldWithMultiUse) {
1687 // Don't modify shared select instructions unless set FoldWithMultiUse
1688 if (!SI->hasOneUse() && !FoldWithMultiUse)
1689 return nullptr;
1690
1691 Value *TV = SI->getTrueValue();
1692 Value *FV = SI->getFalseValue();
1693 if (!(isa<Constant>(TV) || isa<Constant>(FV)))
1694 return nullptr;
1695
1696 // Bool selects with constant operands can be folded to logical ops.
1697 if (SI->getType()->isIntOrIntVectorTy(1))
1698 return nullptr;
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.
1707 if (auto *CI = dyn_cast<FCmpInst>(SI->getCondition())) {
1708 if (CI->hasOneUse()) {
1709 Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
1710 if ((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1))
1711 return nullptr;
1712 }
1713 }
1714
1715 // Make sure that one of the select arms constant folds successfully.
1716 Value *NewTV = constantFoldOperationIntoSelectOperand(Op, SI, /*IsTrueArm*/ true);
1717 Value *NewFV = constantFoldOperationIntoSelectOperand(Op, SI, /*IsTrueArm*/ false);
1718 if (!NewTV && !NewFV)
1719 return nullptr;
1720
1721 // Create an instruction for the arm that did not fold.
1722 if (!NewTV)
1723 NewTV = foldOperationIntoSelectOperand(Op, SI, TV, *this);
1724 if (!NewFV)
1725 NewFV = foldOperationIntoSelectOperand(Op, SI, FV, *this);
1726 return SelectInst::Create(SI->getCondition(), NewTV, NewFV, "", nullptr, SI);
1727}
1728
1730 Value *InValue, BasicBlock *InBB,
1731 const DataLayout &DL,
1732 const SimplifyQuery SQ) {
1733 // NB: It is a precondition of this transform that the operands be
1734 // phi translatable! This is usually trivially satisfied by limiting it
1735 // to constant ops, and for selects we do a more sophisticated check.
1737 for (Value *Op : I.operands()) {
1738 if (Op == PN)
1739 Ops.push_back(InValue);
1740 else
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.
1749 &I, Ops, SQ.getWithInstruction(InBB->getTerminator()));
1750 if (NewVal && NewVal != PN && !match(NewVal, m_ConstantExpr()))
1751 return NewVal;
1752
1753 // Check if incoming PHI value can be replaced with constant
1754 // based on implied condition.
1755 BranchInst *TerminatorBI = dyn_cast<BranchInst>(InBB->getTerminator());
1756 const ICmpInst *ICmp = dyn_cast<ICmpInst>(&I);
1757 if (TerminatorBI && TerminatorBI->isConditional() &&
1758 TerminatorBI->getSuccessor(0) != TerminatorBI->getSuccessor(1) && ICmp) {
1759 bool LHSIsTrue = TerminatorBI->getSuccessor(0) == PN->getParent();
1760 std::optional<bool> ImpliedCond =
1761 isImpliedCondition(TerminatorBI->getCondition(), ICmp->getPredicate(),
1762 Ops[0], Ops[1], DL, LHSIsTrue);
1763 if (ImpliedCond)
1764 return ConstantInt::getBool(I.getType(), ImpliedCond.value());
1765 }
1766
1767 return nullptr;
1768}
1769
1771 unsigned NumPHIValues = PN->getNumIncomingValues();
1772 if (NumPHIValues == 0)
1773 return nullptr;
1774
1775 // We normally only transform phis with a single use. However, if a PHI has
1776 // multiple uses and they are all the same operation, we can fold *all* of the
1777 // uses into the PHI.
1778 if (!PN->hasOneUse()) {
1779 // Walk the use list for the instruction, comparing them to I.
1780 for (User *U : PN->users()) {
1781 Instruction *UI = cast<Instruction>(U);
1782 if (UI != &I && !I.isIdenticalTo(UI))
1783 return nullptr;
1784 }
1785 // Otherwise, we can replace *all* users with the new PHI we form.
1786 }
1787
1788 // Check to see whether the instruction can be folded into each phi operand.
1789 // If there is one operand that does not fold, remember the BB it is in.
1790 // If there is more than one or if *it* is a PHI, bail out.
1791 SmallVector<Value *> NewPhiValues;
1792 BasicBlock *NonSimplifiedBB = nullptr;
1793 Value *NonSimplifiedInVal = nullptr;
1794 for (unsigned i = 0; i != NumPHIValues; ++i) {
1795 Value *InVal = PN->getIncomingValue(i);
1796 BasicBlock *InBB = PN->getIncomingBlock(i);
1797
1798 if (auto *NewVal = simplifyInstructionWithPHI(I, PN, InVal, InBB, DL, SQ)) {
1799 NewPhiValues.push_back(NewVal);
1800 continue;
1801 }
1802
1803 if (NonSimplifiedBB) return nullptr; // More than one non-simplified value.
1804
1805 NonSimplifiedBB = InBB;
1806 NonSimplifiedInVal = InVal;
1807 NewPhiValues.push_back(nullptr);
1808
1809 // If the InVal is an invoke at the end of the pred block, then we can't
1810 // insert a computation after it without breaking the edge.
1811 if (isa<InvokeInst>(InVal))
1812 if (cast<Instruction>(InVal)->getParent() == NonSimplifiedBB)
1813 return nullptr;
1814
1815 // If the incoming non-constant value is reachable from the phis block,
1816 // we'll push the operation across a loop backedge. This could result in
1817 // an infinite combine loop, and is generally non-profitable (especially
1818 // if the operation was originally outside the loop).
1819 if (isPotentiallyReachable(PN->getParent(), NonSimplifiedBB, nullptr, &DT,
1820 LI))
1821 return nullptr;
1822 }
1823
1824 // If there is exactly one non-simplified value, we can insert a copy of the
1825 // operation in that block. However, if this is a critical edge, we would be
1826 // inserting the computation on some other paths (e.g. inside a loop). Only
1827 // do this if the pred block is unconditionally branching into the phi block.
1828 // Also, make sure that the pred block is not dead code.
1829 if (NonSimplifiedBB != nullptr) {
1830 BranchInst *BI = dyn_cast<BranchInst>(NonSimplifiedBB->getTerminator());
1831 if (!BI || !BI->isUnconditional() ||
1832 !DT.isReachableFromEntry(NonSimplifiedBB))
1833 return nullptr;
1834 }
1835
1836 // Okay, we can do the transformation: create the new PHI node.
1837 PHINode *NewPN = PHINode::Create(I.getType(), PN->getNumIncomingValues());
1838 InsertNewInstBefore(NewPN, PN->getIterator());
1839 NewPN->takeName(PN);
1840 NewPN->setDebugLoc(PN->getDebugLoc());
1841
1842 // If we are going to have to insert a new computation, do so right before the
1843 // predecessor's terminator.
1844 Instruction *Clone = nullptr;
1845 if (NonSimplifiedBB) {
1846 Clone = I.clone();
1847 for (Use &U : Clone->operands()) {
1848 if (U == PN)
1849 U = NonSimplifiedInVal;
1850 else
1851 U = U->DoPHITranslation(PN->getParent(), NonSimplifiedBB);
1852 }
1853 InsertNewInstBefore(Clone, NonSimplifiedBB->getTerminator()->getIterator());
1854 }
1855
1856 for (unsigned i = 0; i != NumPHIValues; ++i) {
1857 if (NewPhiValues[i])
1858 NewPN->addIncoming(NewPhiValues[i], PN->getIncomingBlock(i));
1859 else
1860 NewPN->addIncoming(Clone, PN->getIncomingBlock(i));
1861 }
1862
1863 for (User *U : make_early_inc_range(PN->users())) {
1864 Instruction *User = cast<Instruction>(U);
1865 if (User == &I) continue;
1866 replaceInstUsesWith(*User, NewPN);
1868 }
1869
1870 replaceAllDbgUsesWith(const_cast<PHINode &>(*PN),
1871 const_cast<PHINode &>(*NewPN),
1872 const_cast<PHINode &>(*PN), DT);
1873 return replaceInstUsesWith(I, NewPN);
1874}
1875
1877 // TODO: This should be similar to the incoming values check in foldOpIntoPhi:
1878 // we are guarding against replicating the binop in >1 predecessor.
1879 // This could miss matching a phi with 2 constant incoming values.
1880 auto *Phi0 = dyn_cast<PHINode>(BO.getOperand(0));
1881 auto *Phi1 = dyn_cast<PHINode>(BO.getOperand(1));
1882 if (!Phi0 || !Phi1 || !Phi0->hasOneUse() || !Phi1->hasOneUse() ||
1883 Phi0->getNumOperands() != Phi1->getNumOperands())
1884 return nullptr;
1885
1886 // TODO: Remove the restriction for binop being in the same block as the phis.
1887 if (BO.getParent() != Phi0->getParent() ||
1888 BO.getParent() != Phi1->getParent())
1889 return nullptr;
1890
1891 // Fold if there is at least one specific constant value in phi0 or phi1's
1892 // incoming values that comes from the same block and this specific constant
1893 // value can be used to do optimization for specific binary operator.
1894 // For example:
1895 // %phi0 = phi i32 [0, %bb0], [%i, %bb1]
1896 // %phi1 = phi i32 [%j, %bb0], [0, %bb1]
1897 // %add = add i32 %phi0, %phi1
1898 // ==>
1899 // %add = phi i32 [%j, %bb0], [%i, %bb1]
1901 /*AllowRHSConstant*/ false);
1902 if (C) {
1903 SmallVector<Value *, 4> NewIncomingValues;
1904 auto CanFoldIncomingValuePair = [&](std::tuple<Use &, Use &> T) {
1905 auto &Phi0Use = std::get<0>(T);
1906 auto &Phi1Use = std::get<1>(T);
1907 if (Phi0->getIncomingBlock(Phi0Use) != Phi1->getIncomingBlock(Phi1Use))
1908 return false;
1909 Value *Phi0UseV = Phi0Use.get();
1910 Value *Phi1UseV = Phi1Use.get();
1911 if (Phi0UseV == C)
1912 NewIncomingValues.push_back(Phi1UseV);
1913 else if (Phi1UseV == C)
1914 NewIncomingValues.push_back(Phi0UseV);
1915 else
1916 return false;
1917 return true;
1918 };
1919
1920 if (all_of(zip(Phi0->operands(), Phi1->operands()),
1921 CanFoldIncomingValuePair)) {
1922 PHINode *NewPhi =
1923 PHINode::Create(Phi0->getType(), Phi0->getNumOperands());
1924 assert(NewIncomingValues.size() == Phi0->getNumOperands() &&
1925 "The number of collected incoming values should equal the number "
1926 "of the original PHINode operands!");
1927 for (unsigned I = 0; I < Phi0->getNumOperands(); I++)
1928 NewPhi->addIncoming(NewIncomingValues[I], Phi0->getIncomingBlock(I));
1929 return NewPhi;
1930 }
1931 }
1932
1933 if (Phi0->getNumOperands() != 2 || Phi1->getNumOperands() != 2)
1934 return nullptr;
1935
1936 // Match a pair of incoming constants for one of the predecessor blocks.
1937 BasicBlock *ConstBB, *OtherBB;
1938 Constant *C0, *C1;
1939 if (match(Phi0->getIncomingValue(0), m_ImmConstant(C0))) {
1940 ConstBB = Phi0->getIncomingBlock(0);
1941 OtherBB = Phi0->getIncomingBlock(1);
1942 } else if (match(Phi0->getIncomingValue(1), m_ImmConstant(C0))) {
1943 ConstBB = Phi0->getIncomingBlock(1);
1944 OtherBB = Phi0->getIncomingBlock(0);
1945 } else {
1946 return nullptr;
1947 }
1948 if (!match(Phi1->getIncomingValueForBlock(ConstBB), m_ImmConstant(C1)))
1949 return nullptr;
1950
1951 // The block that we are hoisting to must reach here unconditionally.
1952 // Otherwise, we could be speculatively executing an expensive or
1953 // non-speculative op.
1954 auto *PredBlockBranch = dyn_cast<BranchInst>(OtherBB->getTerminator());
1955 if (!PredBlockBranch || PredBlockBranch->isConditional() ||
1956 !DT.isReachableFromEntry(OtherBB))
1957 return nullptr;
1958
1959 // TODO: This check could be tightened to only apply to binops (div/rem) that
1960 // are not safe to speculatively execute. But that could allow hoisting
1961 // potentially expensive instructions (fdiv for example).
1962 for (auto BBIter = BO.getParent()->begin(); &*BBIter != &BO; ++BBIter)
1964 return nullptr;
1965
1966 // Fold constants for the predecessor block with constant incoming values.
1967 Constant *NewC = ConstantFoldBinaryOpOperands(BO.getOpcode(), C0, C1, DL);
1968 if (!NewC)
1969 return nullptr;
1970
1971 // Make a new binop in the predecessor block with the non-constant incoming
1972 // values.
1973 Builder.SetInsertPoint(PredBlockBranch);
1974 Value *NewBO = Builder.CreateBinOp(BO.getOpcode(),
1975 Phi0->getIncomingValueForBlock(OtherBB),
1976 Phi1->getIncomingValueForBlock(OtherBB));
1977 if (auto *NotFoldedNewBO = dyn_cast<BinaryOperator>(NewBO))
1978 NotFoldedNewBO->copyIRFlags(&BO);
1979
1980 // Replace the binop with a phi of the new values. The old phis are dead.
1981 PHINode *NewPhi = PHINode::Create(BO.getType(), 2);
1982 NewPhi->addIncoming(NewBO, OtherBB);
1983 NewPhi->addIncoming(NewC, ConstBB);
1984 return NewPhi;
1985}
1986
1988 if (!isa<Constant>(I.getOperand(1)))
1989 return nullptr;
1990
1991 if (auto *Sel = dyn_cast<SelectInst>(I.getOperand(0))) {
1992 if (Instruction *NewSel = FoldOpIntoSelect(I, Sel))
1993 return NewSel;
1994 } else if (auto *PN = dyn_cast<PHINode>(I.getOperand(0))) {
1995 if (Instruction *NewPhi = foldOpIntoPhi(I, PN))
1996 return NewPhi;
1997 }
1998 return nullptr;
1999}
2000
2002 // If this GEP has only 0 indices, it is the same pointer as
2003 // Src. If Src is not a trivial GEP too, don't combine
2004 // the indices.
2005 if (GEP.hasAllZeroIndices() && !Src.hasAllZeroIndices() &&
2006 !Src.hasOneUse())
2007 return false;
2008 return true;
2009}
2010
2012 if (!isa<VectorType>(Inst.getType()))
2013 return nullptr;
2014
2015 BinaryOperator::BinaryOps Opcode = Inst.getOpcode();
2016 Value *LHS = Inst.getOperand(0), *RHS = Inst.getOperand(1);
2017 assert(cast<VectorType>(LHS->getType())->getElementCount() ==
2018 cast<VectorType>(Inst.getType())->getElementCount());
2019 assert(cast<VectorType>(RHS->getType())->getElementCount() ==
2020 cast<VectorType>(Inst.getType())->getElementCount());
2021
2022 // If both operands of the binop are vector concatenations, then perform the
2023 // narrow binop on each pair of the source operands followed by concatenation
2024 // of the results.
2025 Value *L0, *L1, *R0, *R1;
2026 ArrayRef<int> Mask;
2027 if (match(LHS, m_Shuffle(m_Value(L0), m_Value(L1), m_Mask(Mask))) &&
2028 match(RHS, m_Shuffle(m_Value(R0), m_Value(R1), m_SpecificMask(Mask))) &&
2029 LHS->hasOneUse() && RHS->hasOneUse() &&
2030 cast<ShuffleVectorInst>(LHS)->isConcat() &&
2031 cast<ShuffleVectorInst>(RHS)->isConcat()) {
2032 // This transform does not have the speculative execution constraint as
2033 // below because the shuffle is a concatenation. The new binops are
2034 // operating on exactly the same elements as the existing binop.
2035 // TODO: We could ease the mask requirement to allow different undef lanes,
2036 // but that requires an analysis of the binop-with-undef output value.
2037 Value *NewBO0 = Builder.CreateBinOp(Opcode, L0, R0);
2038 if (auto *BO = dyn_cast<BinaryOperator>(NewBO0))
2039 BO->copyIRFlags(&Inst);
2040 Value *NewBO1 = Builder.CreateBinOp(Opcode, L1, R1);
2041 if (auto *BO = dyn_cast<BinaryOperator>(NewBO1))
2042 BO->copyIRFlags(&Inst);
2043 return new ShuffleVectorInst(NewBO0, NewBO1, Mask);
2044 }
2045
2046 auto createBinOpReverse = [&](Value *X, Value *Y) {
2047 Value *V = Builder.CreateBinOp(Opcode, X, Y, Inst.getName());
2048 if (auto *BO = dyn_cast<BinaryOperator>(V))
2049 BO->copyIRFlags(&Inst);
2050 Module *M = Inst.getModule();
2051 Function *F =
2052 Intrinsic::getDeclaration(M, Intrinsic::vector_reverse, V->getType());
2053 return CallInst::Create(F, V);
2054 };
2055
2056 // NOTE: Reverse shuffles don't require the speculative execution protection
2057 // below because they don't affect which lanes take part in the computation.
2058
2059 Value *V1, *V2;
2060 if (match(LHS, m_VecReverse(m_Value(V1)))) {
2061 // Op(rev(V1), rev(V2)) -> rev(Op(V1, V2))
2062 if (match(RHS, m_VecReverse(m_Value(V2))) &&
2063 (LHS->hasOneUse() || RHS->hasOneUse() ||
2064 (LHS == RHS && LHS->hasNUses(2))))
2065 return createBinOpReverse(V1, V2);
2066
2067 // Op(rev(V1), RHSSplat)) -> rev(Op(V1, RHSSplat))
2068 if (LHS->hasOneUse() && isSplatValue(RHS))
2069 return createBinOpReverse(V1, RHS);
2070 }
2071 // Op(LHSSplat, rev(V2)) -> rev(Op(LHSSplat, V2))
2072 else if (isSplatValue(LHS) && match(RHS, m_OneUse(m_VecReverse(m_Value(V2)))))
2073 return createBinOpReverse(LHS, V2);
2074
2075 // It may not be safe to reorder shuffles and things like div, urem, etc.
2076 // because we may trap when executing those ops on unknown vector elements.
2077 // See PR20059.
2078 if (!isSafeToSpeculativelyExecute(&Inst))
2079 return nullptr;
2080
2081 auto createBinOpShuffle = [&](Value *X, Value *Y, ArrayRef<int> M) {
2082 Value *XY = Builder.CreateBinOp(Opcode, X, Y);
2083 if (auto *BO = dyn_cast<BinaryOperator>(XY))
2084 BO->copyIRFlags(&Inst);
2085 return new ShuffleVectorInst(XY, M);
2086 };
2087
2088 // If both arguments of the binary operation are shuffles that use the same
2089 // mask and shuffle within a single vector, move the shuffle after the binop.
2090 if (match(LHS, m_Shuffle(m_Value(V1), m_Poison(), m_Mask(Mask))) &&
2091 match(RHS, m_Shuffle(m_Value(V2), m_Poison(), m_SpecificMask(Mask))) &&
2092 V1->getType() == V2->getType() &&
2093 (LHS->hasOneUse() || RHS->hasOneUse() || LHS == RHS)) {
2094 // Op(shuffle(V1, Mask), shuffle(V2, Mask)) -> shuffle(Op(V1, V2), Mask)
2095 return createBinOpShuffle(V1, V2, Mask);
2096 }
2097
2098 // If both arguments of a commutative binop are select-shuffles that use the
2099 // same mask with commuted operands, the shuffles are unnecessary.
2100 if (Inst.isCommutative() &&
2101 match(LHS, m_Shuffle(m_Value(V1), m_Value(V2), m_Mask(Mask))) &&
2102 match(RHS,
2103 m_Shuffle(m_Specific(V2), m_Specific(V1), m_SpecificMask(Mask)))) {
2104 auto *LShuf = cast<ShuffleVectorInst>(LHS);
2105 auto *RShuf = cast<ShuffleVectorInst>(RHS);
2106 // TODO: Allow shuffles that contain undefs in the mask?
2107 // That is legal, but it reduces undef knowledge.
2108 // TODO: Allow arbitrary shuffles by shuffling after binop?
2109 // That might be legal, but we have to deal with poison.
2110 if (LShuf->isSelect() &&
2111 !is_contained(LShuf->getShuffleMask(), PoisonMaskElem) &&
2112 RShuf->isSelect() &&
2113 !is_contained(RShuf->getShuffleMask(), PoisonMaskElem)) {
2114 // Example:
2115 // LHS = shuffle V1, V2, <0, 5, 6, 3>
2116 // RHS = shuffle V2, V1, <0, 5, 6, 3>
2117 // LHS + RHS --> (V10+V20, V21+V11, V22+V12, V13+V23) --> V1 + V2
2118 Instruction *NewBO = BinaryOperator::Create(Opcode, V1, V2);
2119 NewBO->copyIRFlags(&Inst);
2120 return NewBO;
2121 }
2122 }
2123
2124 // If one argument is a shuffle within one vector and the other is a constant,
2125 // try moving the shuffle after the binary operation. This canonicalization
2126 // intends to move shuffles closer to other shuffles and binops closer to
2127 // other binops, so they can be folded. It may also enable demanded elements
2128 // transforms.
2129 Constant *C;
2130 auto *InstVTy = dyn_cast<FixedVectorType>(Inst.getType());
2131 if (InstVTy &&
2133 m_Mask(Mask))),
2134 m_ImmConstant(C))) &&
2135 cast<FixedVectorType>(V1->getType())->getNumElements() <=
2136 InstVTy->getNumElements()) {
2137 assert(InstVTy->getScalarType() == V1->getType()->getScalarType() &&
2138 "Shuffle should not change scalar type");
2139
2140 // Find constant NewC that has property:
2141 // shuffle(NewC, ShMask) = C
2142 // If such constant does not exist (example: ShMask=<0,0> and C=<1,2>)
2143 // reorder is not possible. A 1-to-1 mapping is not required. Example:
2144 // ShMask = <1,1,2,2> and C = <5,5,6,6> --> NewC = <undef,5,6,undef>
2145 bool ConstOp1 = isa<Constant>(RHS);
2146 ArrayRef<int> ShMask = Mask;
2147 unsigned SrcVecNumElts =
2148 cast<FixedVectorType>(V1->getType())->getNumElements();
2149 PoisonValue *PoisonScalar = PoisonValue::get(C->getType()->getScalarType());
2150 SmallVector<Constant *, 16> NewVecC(SrcVecNumElts, PoisonScalar);
2151 bool MayChange = true;
2152 unsigned NumElts = InstVTy->getNumElements();
2153 for (unsigned I = 0; I < NumElts; ++I) {
2154 Constant *CElt = C->getAggregateElement(I);
2155 if (ShMask[I] >= 0) {
2156 assert(ShMask[I] < (int)NumElts && "Not expecting narrowing shuffle");
2157 Constant *NewCElt = NewVecC[ShMask[I]];
2158 // Bail out if:
2159 // 1. The constant vector contains a constant expression.
2160 // 2. The shuffle needs an element of the constant vector that can't
2161 // be mapped to a new constant vector.
2162 // 3. This is a widening shuffle that copies elements of V1 into the
2163 // extended elements (extending with poison is allowed).
2164 if (!CElt || (!isa<PoisonValue>(NewCElt) && NewCElt != CElt) ||
2165 I >= SrcVecNumElts) {
2166 MayChange = false;
2167 break;
2168 }
2169 NewVecC[ShMask[I]] = CElt;
2170 }
2171 // If this is a widening shuffle, we must be able to extend with poison
2172 // elements. If the original binop does not produce a poison in the high
2173 // lanes, then this transform is not safe.
2174 // Similarly for poison lanes due to the shuffle mask, we can only
2175 // transform binops that preserve poison.
2176 // TODO: We could shuffle those non-poison constant values into the
2177 // result by using a constant vector (rather than an poison vector)
2178 // as operand 1 of the new binop, but that might be too aggressive
2179 // for target-independent shuffle creation.
2180 if (I >= SrcVecNumElts || ShMask[I] < 0) {
2181 Constant *MaybePoison =
2182 ConstOp1
2183 ? ConstantFoldBinaryOpOperands(Opcode, PoisonScalar, CElt, DL)
2184 : ConstantFoldBinaryOpOperands(Opcode, CElt, PoisonScalar, DL);
2185 if (!MaybePoison || !isa<PoisonValue>(MaybePoison)) {
2186 MayChange = false;
2187 break;
2188 }
2189 }
2190 }
2191 if (MayChange) {
2192 Constant *NewC = ConstantVector::get(NewVecC);
2193 // It may not be safe to execute a binop on a vector with poison elements
2194 // because the entire instruction can be folded to undef or create poison
2195 // that did not exist in the original code.
2196 // TODO: The shift case should not be necessary.
2197 if (Inst.isIntDivRem() || (Inst.isShift() && ConstOp1))
2198 NewC = getSafeVectorConstantForBinop(Opcode, NewC, ConstOp1);
2199
2200 // Op(shuffle(V1, Mask), C) -> shuffle(Op(V1, NewC), Mask)
2201 // Op(C, shuffle(V1, Mask)) -> shuffle(Op(NewC, V1), Mask)
2202 Value *NewLHS = ConstOp1 ? V1 : NewC;
2203 Value *NewRHS = ConstOp1 ? NewC : V1;
2204 return createBinOpShuffle(NewLHS, NewRHS, Mask);
2205 }
2206 }
2207
2208 // Try to reassociate to sink a splat shuffle after a binary operation.
2209 if (Inst.isAssociative() && Inst.isCommutative()) {
2210 // Canonicalize shuffle operand as LHS.
2211 if (isa<ShuffleVectorInst>(RHS))
2212 std::swap(LHS, RHS);
2213
2214 Value *X;
2215 ArrayRef<int> MaskC;
2216 int SplatIndex;
2217 Value *Y, *OtherOp;
2218 if (!match(LHS,
2219 m_OneUse(m_Shuffle(m_Value(X), m_Undef(), m_Mask(MaskC)))) ||
2220 !match(MaskC, m_SplatOrPoisonMask(SplatIndex)) ||
2221 X->getType() != Inst.getType() ||
2222 !match(RHS, m_OneUse(m_BinOp(Opcode, m_Value(Y), m_Value(OtherOp)))))
2223 return nullptr;
2224
2225 // FIXME: This may not be safe if the analysis allows undef elements. By
2226 // moving 'Y' before the splat shuffle, we are implicitly assuming
2227 // that it is not undef/poison at the splat index.
2228 if (isSplatValue(OtherOp, SplatIndex)) {
2229 std::swap(Y, OtherOp);
2230 } else if (!isSplatValue(Y, SplatIndex)) {
2231 return nullptr;
2232 }
2233
2234 // X and Y are splatted values, so perform the binary operation on those
2235 // values followed by a splat followed by the 2nd binary operation:
2236 // bo (splat X), (bo Y, OtherOp) --> bo (splat (bo X, Y)), OtherOp
2237 Value *NewBO = Builder.CreateBinOp(Opcode, X, Y);
2238 SmallVector<int, 8> NewMask(MaskC.size(), SplatIndex);
2239 Value *NewSplat = Builder.CreateShuffleVector(NewBO, NewMask);
2240 Instruction *R = BinaryOperator::Create(Opcode, NewSplat, OtherOp);
2241
2242 // Intersect FMF on both new binops. Other (poison-generating) flags are
2243 // dropped to be safe.
2244 if (isa<FPMathOperator>(R)) {
2245 R->copyFastMathFlags(&Inst);
2246 R->andIRFlags(RHS);
2247 }
2248 if (auto *NewInstBO = dyn_cast<BinaryOperator>(NewBO))
2249 NewInstBO->copyIRFlags(R);
2250 return R;
2251 }
2252
2253 return nullptr;
2254}
2255
2256/// Try to narrow the width of a binop if at least 1 operand is an extend of
2257/// of a value. This requires a potentially expensive known bits check to make
2258/// sure the narrow op does not overflow.
2259Instruction *InstCombinerImpl::narrowMathIfNoOverflow(BinaryOperator &BO) {
2260 // We need at least one extended operand.
2261 Value *Op0 = BO.getOperand(0), *Op1 = BO.getOperand(1);
2262
2263 // If this is a sub, we swap the operands since we always want an extension
2264 // on the RHS. The LHS can be an extension or a constant.
2265 if (BO.getOpcode() == Instruction::Sub)
2266 std::swap(Op0, Op1);
2267
2268 Value *X;
2269 bool IsSext = match(Op0, m_SExt(m_Value(X)));
2270 if (!IsSext && !match(Op0, m_ZExt(m_Value(X))))
2271 return nullptr;
2272
2273 // If both operands are the same extension from the same source type and we
2274 // can eliminate at least one (hasOneUse), this might work.
2275 CastInst::CastOps CastOpc = IsSext ? Instruction::SExt : Instruction::ZExt;
2276 Value *Y;
2277 if (!(match(Op1, m_ZExtOrSExt(m_Value(Y))) && X->getType() == Y->getType() &&
2278 cast<Operator>(Op1)->getOpcode() == CastOpc &&
2279 (Op0->hasOneUse() || Op1->hasOneUse()))) {
2280 // If that did not match, see if we have a suitable constant operand.
2281 // Truncating and extending must produce the same constant.
2282 Constant *WideC;
2283 if (!Op0->hasOneUse() || !match(Op1, m_Constant(WideC)))
2284 return nullptr;
2285 Constant *NarrowC = getLosslessTrunc(WideC, X->getType(), CastOpc);
2286 if (!NarrowC)
2287 return nullptr;
2288 Y = NarrowC;
2289 }
2290
2291 // Swap back now that we found our operands.
2292 if (BO.getOpcode() == Instruction::Sub)
2293 std::swap(X, Y);
2294
2295 // Both operands have narrow versions. Last step: the math must not overflow
2296 // in the narrow width.
2297 if (!willNotOverflow(BO.getOpcode(), X, Y, BO, IsSext))
2298 return nullptr;
2299
2300 // bo (ext X), (ext Y) --> ext (bo X, Y)
2301 // bo (ext X), C --> ext (bo X, C')
2302 Value *NarrowBO = Builder.CreateBinOp(BO.getOpcode(), X, Y, "narrow");
2303 if (auto *NewBinOp = dyn_cast<BinaryOperator>(NarrowBO)) {
2304 if (IsSext)
2305 NewBinOp->setHasNoSignedWrap();
2306 else
2307 NewBinOp->setHasNoUnsignedWrap();
2308 }
2309 return CastInst::Create(CastOpc, NarrowBO, BO.getType());
2310}
2311
2313 return GEP1.isInBounds() && GEP2.isInBounds();
2314}
2315
2316/// Thread a GEP operation with constant indices through the constant true/false
2317/// arms of a select.
2319 InstCombiner::BuilderTy &Builder) {
2320 if (!GEP.hasAllConstantIndices())
2321 return nullptr;
2322
2323 Instruction *Sel;
2324 Value *Cond;
2325 Constant *TrueC, *FalseC;
2326 if (!match(GEP.getPointerOperand(), m_Instruction(Sel)) ||
2327 !match(Sel,
2328 m_Select(m_Value(Cond), m_Constant(TrueC), m_Constant(FalseC))))
2329 return nullptr;
2330
2331 // gep (select Cond, TrueC, FalseC), IndexC --> select Cond, TrueC', FalseC'
2332 // Propagate 'inbounds' and metadata from existing instructions.
2333 // Note: using IRBuilder to create the constants for efficiency.
2334 SmallVector<Value *, 4> IndexC(GEP.indices());
2335 GEPNoWrapFlags NW = GEP.getNoWrapFlags();
2336 Type *Ty = GEP.getSourceElementType();
2337 Value *NewTrueC = Builder.CreateGEP(Ty, TrueC, IndexC, "", NW);
2338 Value *NewFalseC = Builder.CreateGEP(Ty, FalseC, IndexC, "", NW);
2339 return SelectInst::Create(Cond, NewTrueC, NewFalseC, "", nullptr, Sel);
2340}
2341
2342// Canonicalization:
2343// gep T, (gep i8, base, C1), (Index + C2) into
2344// gep T, (gep i8, base, C1 + C2 * sizeof(T)), Index
2346 GEPOperator *Src,
2347 InstCombinerImpl &IC) {
2348 if (GEP.getNumIndices() != 1)
2349 return nullptr;
2350 auto &DL = IC.getDataLayout();
2351 Value *Base;
2352 const APInt *C1;
2353 if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1))))
2354 return nullptr;
2355 Value *VarIndex;
2356 const APInt *C2;
2357 Type *PtrTy = Src->getType()->getScalarType();
2358 unsigned IndexSizeInBits = DL.getIndexTypeSizeInBits(PtrTy);
2359 if (!match(GEP.getOperand(1), m_AddLike(m_Value(VarIndex), m_APInt(C2))))
2360 return nullptr;
2361 if (C1->getBitWidth() != IndexSizeInBits ||
2362 C2->getBitWidth() != IndexSizeInBits)
2363 return nullptr;
2364 Type *BaseType = GEP.getSourceElementType();
2365 if (isa<ScalableVectorType>(BaseType))
2366 return nullptr;
2367 APInt TypeSize(IndexSizeInBits, DL.getTypeAllocSize(BaseType));
2368 APInt NewOffset = TypeSize * *C2 + *C1;
2369 if (NewOffset.isZero() ||
2370 (Src->hasOneUse() && GEP.getOperand(1)->hasOneUse())) {
2371 Value *GEPConst =
2372 IC.Builder.CreatePtrAdd(Base, IC.Builder.getInt(NewOffset));
2373 return GetElementPtrInst::Create(BaseType, GEPConst, VarIndex);
2374 }
2375
2376 return nullptr;
2377}
2378
2380 GEPOperator *Src) {
2381 // Combine Indices - If the source pointer to this getelementptr instruction
2382 // is a getelementptr instruction with matching element type, combine the
2383 // indices of the two getelementptr instructions into a single instruction.
2384 if (!shouldMergeGEPs(*cast<GEPOperator>(&GEP), *Src))
2385 return nullptr;
2386
2387 if (auto *I = canonicalizeGEPOfConstGEPI8(GEP, Src, *this))
2388 return I;
2389
2390 // For constant GEPs, use a more general offset-based folding approach.
2391 Type *PtrTy = Src->getType()->getScalarType();
2392 if (GEP.hasAllConstantIndices() &&
2393 (Src->hasOneUse() || Src->hasAllConstantIndices())) {
2394 // Split Src into a variable part and a constant suffix.
2396 Type *BaseType = GTI.getIndexedType();
2397 bool IsFirstType = true;
2398 unsigned NumVarIndices = 0;
2399 for (auto Pair : enumerate(Src->indices())) {
2400 if (!isa<ConstantInt>(Pair.value())) {
2401 BaseType = GTI.getIndexedType();
2402 IsFirstType = false;
2403 NumVarIndices = Pair.index() + 1;
2404 }
2405 ++GTI;
2406 }
2407
2408 // Determine the offset for the constant suffix of Src.
2410 if (NumVarIndices != Src->getNumIndices()) {
2411 // FIXME: getIndexedOffsetInType() does not handled scalable vectors.
2412 if (BaseType->isScalableTy())
2413 return nullptr;
2414
2415 SmallVector<Value *> ConstantIndices;
2416 if (!IsFirstType)
2417 ConstantIndices.push_back(
2419 append_range(ConstantIndices, drop_begin(Src->indices(), NumVarIndices));
2420 Offset += DL.getIndexedOffsetInType(BaseType, ConstantIndices);
2421 }
2422
2423 // Add the offset for GEP (which is fully constant).
2424 if (!GEP.accumulateConstantOffset(DL, Offset))
2425 return nullptr;
2426
2427 APInt OffsetOld = Offset;
2428 // Convert the total offset back into indices.
2429 SmallVector<APInt> ConstIndices =
2431 if (!Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero())) {
2432 // If both GEP are constant-indexed, and cannot be merged in either way,
2433 // convert them to a GEP of i8.
2434 if (Src->hasAllConstantIndices())
2435 return replaceInstUsesWith(
2437 Builder.getInt8Ty(), Src->getOperand(0),
2438 Builder.getInt(OffsetOld), "",
2439 isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP))));
2440 return nullptr;
2441 }
2442
2443 bool IsInBounds = isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP));
2444 SmallVector<Value *> Indices;
2445 append_range(Indices, drop_end(Src->indices(),
2446 Src->getNumIndices() - NumVarIndices));
2447 for (const APInt &Idx : drop_begin(ConstIndices, !IsFirstType)) {
2448 Indices.push_back(ConstantInt::get(GEP.getContext(), Idx));
2449 // Even if the total offset is inbounds, we may end up representing it
2450 // by first performing a larger negative offset, and then a smaller
2451 // positive one. The large negative offset might go out of bounds. Only
2452 // preserve inbounds if all signs are the same.
2453 IsInBounds &= Idx.isNonNegative() == ConstIndices[0].isNonNegative();
2454 }
2455
2456 return replaceInstUsesWith(
2457 GEP, Builder.CreateGEP(Src->getSourceElementType(), Src->getOperand(0),
2458 Indices, "", IsInBounds));
2459 }
2460
2461 if (Src->getResultElementType() != GEP.getSourceElementType())
2462 return nullptr;
2463
2464 SmallVector<Value*, 8> Indices;
2465
2466 // Find out whether the last index in the source GEP is a sequential idx.
2467 bool EndsWithSequential = false;
2468 for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
2469 I != E; ++I)
2470 EndsWithSequential = I.isSequential();
2471
2472 // Can we combine the two pointer arithmetics offsets?
2473 if (EndsWithSequential) {
2474 // Replace: gep (gep %P, long B), long A, ...
2475 // With: T = long A+B; gep %P, T, ...
2476 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
2477 Value *GO1 = GEP.getOperand(1);
2478
2479 // If they aren't the same type, then the input hasn't been processed
2480 // by the loop above yet (which canonicalizes sequential index types to
2481 // intptr_t). Just avoid transforming this until the input has been
2482 // normalized.
2483 if (SO1->getType() != GO1->getType())
2484 return nullptr;
2485
2486 Value *Sum =
2487 simplifyAddInst(GO1, SO1, false, false, SQ.getWithInstruction(&GEP));
2488 // Only do the combine when we are sure the cost after the
2489 // merge is never more than that before the merge.
2490 if (Sum == nullptr)
2491 return nullptr;
2492
2493 // Update the GEP in place if possible.
2494 if (Src->getNumOperands() == 2) {
2495 GEP.setIsInBounds(isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP)));
2496 replaceOperand(GEP, 0, Src->getOperand(0));
2497 replaceOperand(GEP, 1, Sum);
2498 return &GEP;
2499 }
2500 Indices.append(Src->op_begin()+1, Src->op_end()-1);
2501 Indices.push_back(Sum);
2502 Indices.append(GEP.op_begin()+2, GEP.op_end());
2503 } else if (isa<Constant>(*GEP.idx_begin()) &&
2504 cast<Constant>(*GEP.idx_begin())->isNullValue() &&
2505 Src->getNumOperands() != 1) {
2506 // Otherwise we can do the fold if the first index of the GEP is a zero
2507 Indices.append(Src->op_begin()+1, Src->op_end());
2508 Indices.append(GEP.idx_begin()+1, GEP.idx_end());
2509 }
2510
2511 if (!Indices.empty())
2512 return replaceInstUsesWith(
2514 Src->getSourceElementType(), Src->getOperand(0), Indices, "",
2515 isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP))));
2516
2517 return nullptr;
2518}
2519
2521 BuilderTy *Builder,
2522 bool &DoesConsume, unsigned Depth) {
2523 static Value *const NonNull = reinterpret_cast<Value *>(uintptr_t(1));
2524 // ~(~(X)) -> X.
2525 Value *A, *B;
2526 if (match(V, m_Not(m_Value(A)))) {
2527 DoesConsume = true;
2528 return A;
2529 }
2530
2531 Constant *C;
2532 // Constants can be considered to be not'ed values.
2533 if (match(V, m_ImmConstant(C)))
2534 return ConstantExpr::getNot(C);
2535
2537 return nullptr;
2538
2539 // The rest of the cases require that we invert all uses so don't bother
2540 // doing the analysis if we know we can't use the result.
2541 if (!WillInvertAllUses)
2542 return nullptr;
2543
2544 // Compares can be inverted if all of their uses are being modified to use
2545 // the ~V.
2546 if (auto *I = dyn_cast<CmpInst>(V)) {
2547 if (Builder != nullptr)
2548 return Builder->CreateCmp(I->getInversePredicate(), I->getOperand(0),
2549 I->getOperand(1));
2550 return NonNull;
2551 }
2552
2553 // If `V` is of the form `A + B` then `-1 - V` can be folded into
2554 // `(-1 - B) - A` if we are willing to invert all of the uses.
2555 if (match(V, m_Add(m_Value(A), m_Value(B)))) {
2556 if (auto *BV = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2557 DoesConsume, Depth))
2558 return Builder ? Builder->CreateSub(BV, A) : NonNull;
2559 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2560 DoesConsume, Depth))
2561 return Builder ? Builder->CreateSub(AV, B) : NonNull;
2562 return nullptr;
2563 }
2564
2565 // If `V` is of the form `A ^ ~B` then `~(A ^ ~B)` can be folded
2566 // into `A ^ B` if we are willing to invert all of the uses.
2567 if (match(V, m_Xor(m_Value(A), m_Value(B)))) {
2568 if (auto *BV = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2569 DoesConsume, Depth))
2570 return Builder ? Builder->CreateXor(A, BV) : NonNull;
2571 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2572 DoesConsume, Depth))
2573 return Builder ? Builder->CreateXor(AV, B) : NonNull;
2574 return nullptr;
2575 }
2576
2577 // If `V` is of the form `B - A` then `-1 - V` can be folded into
2578 // `A + (-1 - B)` if we are willing to invert all of the uses.
2579 if (match(V, m_Sub(m_Value(A), m_Value(B)))) {
2580 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2581 DoesConsume, Depth))
2582 return Builder ? Builder->CreateAdd(AV, B) : NonNull;
2583 return nullptr;
2584 }
2585
2586 // If `V` is of the form `(~A) s>> B` then `~((~A) s>> B)` can be folded
2587 // into `A s>> B` if we are willing to invert all of the uses.
2588 if (match(V, m_AShr(m_Value(A), m_Value(B)))) {
2589 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2590 DoesConsume, Depth))
2591 return Builder ? Builder->CreateAShr(AV, B) : NonNull;
2592 return nullptr;
2593 }
2594
2595 Value *Cond;
2596 // LogicOps are special in that we canonicalize them at the cost of an
2597 // instruction.
2598 bool IsSelect = match(V, m_Select(m_Value(Cond), m_Value(A), m_Value(B))) &&
2599 !shouldAvoidAbsorbingNotIntoSelect(*cast<SelectInst>(V));
2600 // Selects/min/max with invertible operands are freely invertible
2601 if (IsSelect || match(V, m_MaxOrMin(m_Value(A), m_Value(B)))) {
2602 bool LocalDoesConsume = DoesConsume;
2603 if (!getFreelyInvertedImpl(B, B->hasOneUse(), /*Builder*/ nullptr,
2604 LocalDoesConsume, Depth))
2605 return nullptr;
2606 if (Value *NotA = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2607 LocalDoesConsume, Depth)) {
2608 DoesConsume = LocalDoesConsume;
2609 if (Builder != nullptr) {
2610 Value *NotB = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2611 DoesConsume, Depth);
2612 assert(NotB != nullptr &&
2613 "Unable to build inverted value for known freely invertable op");
2614 if (auto *II = dyn_cast<IntrinsicInst>(V))
2616 getInverseMinMaxIntrinsic(II->getIntrinsicID()), NotA, NotB);
2617 return Builder->CreateSelect(Cond, NotA, NotB);
2618 }
2619 return NonNull;
2620 }
2621 }
2622
2623 if (PHINode *PN = dyn_cast<PHINode>(V)) {
2624 bool LocalDoesConsume = DoesConsume;
2626 for (Use &U : PN->operands()) {
2627 BasicBlock *IncomingBlock = PN->getIncomingBlock(U);
2628 Value *NewIncomingVal = getFreelyInvertedImpl(
2629 U.get(), /*WillInvertAllUses=*/false,
2630 /*Builder=*/nullptr, LocalDoesConsume, MaxAnalysisRecursionDepth - 1);
2631 if (NewIncomingVal == nullptr)
2632 return nullptr;
2633 // Make sure that we can safely erase the original PHI node.
2634 if (NewIncomingVal == V)
2635 return nullptr;
2636 if (Builder != nullptr)
2637 IncomingValues.emplace_back(NewIncomingVal, IncomingBlock);
2638 }
2639
2640 DoesConsume = LocalDoesConsume;
2641 if (Builder != nullptr) {
2644 PHINode *NewPN =
2645 Builder->CreatePHI(PN->getType(), PN->getNumIncomingValues());
2646 for (auto [Val, Pred] : IncomingValues)
2647 NewPN->addIncoming(Val, Pred);
2648 return NewPN;
2649 }
2650 return NonNull;
2651 }
2652
2653 if (match(V, m_SExtLike(m_Value(A)))) {
2654 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2655 DoesConsume, Depth))
2656 return Builder ? Builder->CreateSExt(AV, V->getType()) : NonNull;
2657 return nullptr;
2658 }
2659
2660 if (match(V, m_Trunc(m_Value(A)))) {
2661 if (auto *AV = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2662 DoesConsume, Depth))
2663 return Builder ? Builder->CreateTrunc(AV, V->getType()) : NonNull;
2664 return nullptr;
2665 }
2666
2667 // De Morgan's Laws:
2668 // (~(A | B)) -> (~A & ~B)
2669 // (~(A & B)) -> (~A | ~B)
2670 auto TryInvertAndOrUsingDeMorgan = [&](Instruction::BinaryOps Opcode,
2671 bool IsLogical, Value *A,
2672 Value *B) -> Value * {
2673 bool LocalDoesConsume = DoesConsume;
2674 if (!getFreelyInvertedImpl(B, B->hasOneUse(), /*Builder=*/nullptr,
2675 LocalDoesConsume, Depth))
2676 return nullptr;
2677 if (auto *NotA = getFreelyInvertedImpl(A, A->hasOneUse(), Builder,
2678 LocalDoesConsume, Depth)) {
2679 auto *NotB = getFreelyInvertedImpl(B, B->hasOneUse(), Builder,
2680 LocalDoesConsume, Depth);
2681 DoesConsume = LocalDoesConsume;
2682 if (IsLogical)
2683 return Builder ? Builder->CreateLogicalOp(Opcode, NotA, NotB) : NonNull;
2684 return Builder ? Builder->CreateBinOp(Opcode, NotA, NotB) : NonNull;
2685 }
2686
2687 return nullptr;
2688 };
2689
2690 if (match(V, m_Or(m_Value(A), m_Value(B))))
2691 return TryInvertAndOrUsingDeMorgan(Instruction::And, /*IsLogical=*/false, A,
2692 B);
2693
2694 if (match(V, m_And(m_Value(A), m_Value(B))))
2695 return TryInvertAndOrUsingDeMorgan(Instruction::Or, /*IsLogical=*/false, A,
2696 B);
2697
2698 if (match(V, m_LogicalOr(m_Value(A), m_Value(B))))
2699 return TryInvertAndOrUsingDeMorgan(Instruction::And, /*IsLogical=*/true, A,
2700 B);
2701
2702 if (match(V, m_LogicalAnd(m_Value(A), m_Value(B))))
2703 return TryInvertAndOrUsingDeMorgan(Instruction::Or, /*IsLogical=*/true, A,
2704 B);
2705
2706 return nullptr;
2707}
2708
2710 Value *PtrOp = GEP.getOperand(0);
2711 SmallVector<Value *, 8> Indices(GEP.indices());
2712 Type *GEPType = GEP.getType();
2713 Type *GEPEltType = GEP.getSourceElementType();
2714 if (Value *V =
2715 simplifyGEPInst(GEPEltType, PtrOp, Indices, GEP.getNoWrapFlags(),
2717 return replaceInstUsesWith(GEP, V);
2718
2719 // For vector geps, use the generic demanded vector support.
2720 // Skip if GEP return type is scalable. The number of elements is unknown at
2721 // compile-time.
2722 if (auto *GEPFVTy = dyn_cast<FixedVectorType>(GEPType)) {
2723 auto VWidth = GEPFVTy->getNumElements();
2724 APInt PoisonElts(VWidth, 0);
2725 APInt AllOnesEltMask(APInt::getAllOnes(VWidth));
2726 if (Value *V = SimplifyDemandedVectorElts(&GEP, AllOnesEltMask,
2727 PoisonElts)) {
2728 if (V != &GEP)
2729 return replaceInstUsesWith(GEP, V);
2730 return &GEP;
2731 }
2732
2733 // TODO: 1) Scalarize splat operands, 2) scalarize entire instruction if
2734 // possible (decide on canonical form for pointer broadcast), 3) exploit
2735 // undef elements to decrease demanded bits
2736 }
2737
2738 // Eliminate unneeded casts for indices, and replace indices which displace
2739 // by multiples of a zero size type with zero.
2740 bool MadeChange = false;
2741
2742 // Index width may not be the same width as pointer width.
2743 // Data layout chooses the right type based on supported integer types.
2744 Type *NewScalarIndexTy =
2745 DL.getIndexType(GEP.getPointerOperandType()->getScalarType());
2746
2748 for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end(); I != E;
2749 ++I, ++GTI) {
2750 // Skip indices into struct types.
2751 if (GTI.isStruct())
2752 continue;
2753
2754 Type *IndexTy = (*I)->getType();
2755 Type *NewIndexType =
2756 IndexTy->isVectorTy()
2757 ? VectorType::get(NewScalarIndexTy,
2758 cast<VectorType>(IndexTy)->getElementCount())
2759 : NewScalarIndexTy;
2760
2761 // If the element type has zero size then any index over it is equivalent
2762 // to an index of zero, so replace it with zero if it is not zero already.
2763 Type *EltTy = GTI.getIndexedType();
2764 if (EltTy->isSized() && DL.getTypeAllocSize(EltTy).isZero())
2765 if (!isa<Constant>(*I) || !match(I->get(), m_Zero())) {
2766 *I = Constant::getNullValue(NewIndexType);
2767 MadeChange = true;
2768 }
2769
2770 if (IndexTy != NewIndexType) {
2771 // If we are using a wider index than needed for this platform, shrink
2772 // it to what we need. If narrower, sign-extend it to what we need.
2773 // This explicit cast can make subsequent optimizations more obvious.
2774 *I = Builder.CreateIntCast(*I, NewIndexType, true);
2775 MadeChange = true;
2776 }
2777 }
2778 if (MadeChange)
2779 return &GEP;
2780
2781 // Canonicalize constant GEPs to i8 type.
2782 if (!GEPEltType->isIntegerTy(8) && GEP.hasAllConstantIndices()) {
2784 if (GEP.accumulateConstantOffset(DL, Offset))
2785 return replaceInstUsesWith(
2787 GEP.getNoWrapFlags()));
2788 }
2789
2790 // Canonicalize scalable GEPs to an explicit offset using the llvm.vscale
2791 // intrinsic. This has better support in BasicAA.
2792 if (GEPEltType->isScalableTy()) {
2793 Value *Offset = EmitGEPOffset(cast<GEPOperator>(&GEP));
2794 return replaceInstUsesWith(
2795 GEP, Builder.CreatePtrAdd(PtrOp, Offset, "", GEP.isInBounds()));
2796 }
2797
2798 // Check to see if the inputs to the PHI node are getelementptr instructions.
2799 if (auto *PN = dyn_cast<PHINode>(PtrOp)) {
2800 auto *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));
2801 if (!Op1)
2802 return nullptr;
2803
2804 // Don't fold a GEP into itself through a PHI node. This can only happen
2805 // through the back-edge of a loop. Folding a GEP into itself means that
2806 // the value of the previous iteration needs to be stored in the meantime,
2807 // thus requiring an additional register variable to be live, but not
2808 // actually achieving anything (the GEP still needs to be executed once per
2809 // loop iteration).
2810 if (Op1 == &GEP)
2811 return nullptr;
2812
2813 int DI = -1;
2814
2815 for (auto I = PN->op_begin()+1, E = PN->op_end(); I !=E; ++I) {
2816 auto *Op2 = dyn_cast<GetElementPtrInst>(*I);
2817 if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands() ||
2818 Op1->getSourceElementType() != Op2->getSourceElementType())
2819 return nullptr;
2820
2821 // As for Op1 above, don't try to fold a GEP into itself.
2822 if (Op2 == &GEP)
2823 return nullptr;
2824
2825 // Keep track of the type as we walk the GEP.
2826 Type *CurTy = nullptr;
2827
2828 for (unsigned J = 0, F = Op1->getNumOperands(); J != F; ++J) {
2829 if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())
2830 return nullptr;
2831
2832 if (Op1->getOperand(J) != Op2->getOperand(J)) {
2833 if (DI == -1) {
2834 // We have not seen any differences yet in the GEPs feeding the
2835 // PHI yet, so we record this one if it is allowed to be a
2836 // variable.
2837
2838 // The first two arguments can vary for any GEP, the rest have to be
2839 // static for struct slots
2840 if (J > 1) {
2841 assert(CurTy && "No current type?");
2842 if (CurTy->isStructTy())
2843 return nullptr;
2844 }
2845
2846 DI = J;
2847 } else {
2848 // The GEP is different by more than one input. While this could be
2849 // extended to support GEPs that vary by more than one variable it
2850 // doesn't make sense since it greatly increases the complexity and
2851 // would result in an R+R+R addressing mode which no backend
2852 // directly supports and would need to be broken into several
2853 // simpler instructions anyway.
2854 return nullptr;
2855 }
2856 }
2857
2858 // Sink down a layer of the type for the next iteration.
2859 if (J > 0) {
2860 if (J == 1) {
2861 CurTy = Op1->getSourceElementType();
2862 } else {
2863 CurTy =
2864 GetElementPtrInst::getTypeAtIndex(CurTy, Op1->getOperand(J));
2865 }
2866 }
2867 }
2868 }
2869
2870 // If not all GEPs are identical we'll have to create a new PHI node.
2871 // Check that the old PHI node has only one use so that it will get
2872 // removed.
2873 if (DI != -1 && !PN->hasOneUse())
2874 return nullptr;
2875
2876 auto *NewGEP = cast<GetElementPtrInst>(Op1->clone());
2877 if (DI == -1) {
2878 // All the GEPs feeding the PHI are identical. Clone one down into our
2879 // BB so that it can be merged with the current GEP.
2880 } else {
2881 // All the GEPs feeding the PHI differ at a single offset. Clone a GEP
2882 // into the current block so it can be merged, and create a new PHI to
2883 // set that index.
2884 PHINode *NewPN;
2885 {
2888 NewPN = Builder.CreatePHI(Op1->getOperand(DI)->getType(),
2889 PN->getNumOperands());
2890 }
2891
2892 for (auto &I : PN->operands())
2893 NewPN->addIncoming(cast<GEPOperator>(I)->getOperand(DI),
2894 PN->getIncomingBlock(I));
2895
2896 NewGEP->setOperand(DI, NewPN);
2897 }
2898
2899 NewGEP->insertBefore(*GEP.getParent(), GEP.getParent()->getFirstInsertionPt());
2900 return replaceOperand(GEP, 0, NewGEP);
2901 }
2902
2903 if (auto *Src = dyn_cast<GEPOperator>(PtrOp))
2904 if (Instruction *I = visitGEPOfGEP(GEP, Src))
2905 return I;
2906
2907 if (GEP.getNumIndices() == 1) {
2908 unsigned AS = GEP.getPointerAddressSpace();
2909 if (GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
2910 DL.getIndexSizeInBits(AS)) {
2911 uint64_t TyAllocSize = DL.getTypeAllocSize(GEPEltType).getFixedValue();
2912
2913 if (TyAllocSize == 1) {
2914 // Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) to (bitcast Y),
2915 // but only if the result pointer is only used as if it were an integer,
2916 // or both point to the same underlying object (otherwise provenance is
2917 // not necessarily retained).
2918 Value *X = GEP.getPointerOperand();
2919 Value *Y;
2920 if (match(GEP.getOperand(1),
2922 GEPType == Y->getType()) {
2923 bool HasSameUnderlyingObject =
2925 bool Changed = false;
2926 GEP.replaceUsesWithIf(Y, [&](Use &U) {
2927 bool ShouldReplace = HasSameUnderlyingObject ||
2928 isa<ICmpInst>(U.getUser()) ||
2929 isa<PtrToIntInst>(U.getUser());
2930 Changed |= ShouldReplace;
2931 return ShouldReplace;
2932 });
2933 return Changed ? &GEP : nullptr;
2934 }
2935 } else {
2936 // Canonicalize (gep T* X, V / sizeof(T)) to (gep i8* X, V)
2937 Value *V;
2938 if ((has_single_bit(TyAllocSize) &&
2939 match(GEP.getOperand(1),
2941 m_SpecificInt(countr_zero(TyAllocSize)))))) ||
2942 match(GEP.getOperand(1),
2943 m_Exact(m_IDiv(m_Value(V), m_SpecificInt(TyAllocSize))))) {
2945 Builder.getInt8Ty(), GEP.getPointerOperand(), V);
2946 NewGEP->setIsInBounds(GEP.isInBounds());
2947 return NewGEP;
2948 }
2949 }
2950 }
2951 }
2952 // We do not handle pointer-vector geps here.
2953 if (GEPType->isVectorTy())
2954 return nullptr;
2955
2956 if (GEP.getNumIndices() == 1) {
2957 // We can only preserve inbounds if the original gep is inbounds, the add
2958 // is nsw, and the add operands are non-negative.
2959 auto CanPreserveInBounds = [&](bool AddIsNSW, Value *Idx1, Value *Idx2) {
2961 return GEP.isInBounds() && AddIsNSW && isKnownNonNegative(Idx1, Q) &&
2962 isKnownNonNegative(Idx2, Q);
2963 };
2964
2965 // Try to replace ADD + GEP with GEP + GEP.
2966 Value *Idx1, *Idx2;
2967 if (match(GEP.getOperand(1),
2968 m_OneUse(m_Add(m_Value(Idx1), m_Value(Idx2))))) {
2969 // %idx = add i64 %idx1, %idx2
2970 // %gep = getelementptr i32, ptr %ptr, i64 %idx
2971 // as:
2972 // %newptr = getelementptr i32, ptr %ptr, i64 %idx1
2973 // %newgep = getelementptr i32, ptr %newptr, i64 %idx2
2974 bool IsInBounds = CanPreserveInBounds(
2975 cast<OverflowingBinaryOperator>(GEP.getOperand(1))->hasNoSignedWrap(),
2976 Idx1, Idx2);
2977 auto *NewPtr =
2978 Builder.CreateGEP(GEP.getSourceElementType(), GEP.getPointerOperand(),
2979 Idx1, "", IsInBounds);
2980 return replaceInstUsesWith(
2981 GEP, Builder.CreateGEP(GEP.getSourceElementType(), NewPtr, Idx2, "",
2982 IsInBounds));
2983 }
2984 ConstantInt *C;
2985 if (match(GEP.getOperand(1), m_OneUse(m_SExtLike(m_OneUse(m_NSWAdd(
2986 m_Value(Idx1), m_ConstantInt(C))))))) {
2987 // %add = add nsw i32 %idx1, idx2
2988 // %sidx = sext i32 %add to i64
2989 // %gep = getelementptr i32, ptr %ptr, i64 %sidx
2990 // as:
2991 // %newptr = getelementptr i32, ptr %ptr, i32 %idx1
2992 // %newgep = getelementptr i32, ptr %newptr, i32 idx2
2993 bool IsInBounds = CanPreserveInBounds(
2994 /*IsNSW=*/true, Idx1, C);
2995 auto *NewPtr = Builder.CreateGEP(
2996 GEP.getSourceElementType(), GEP.getPointerOperand(),
2997 Builder.CreateSExt(Idx1, GEP.getOperand(1)->getType()), "",
2998 IsInBounds);
2999 return replaceInstUsesWith(
3000 GEP,
3001 Builder.CreateGEP(GEP.getSourceElementType(), NewPtr,
3002 Builder.CreateSExt(C, GEP.getOperand(1)->getType()),
3003 "", IsInBounds));
3004 }
3005 }
3006
3007 if (!GEP.isInBounds()) {
3008 unsigned IdxWidth =
3010 APInt BasePtrOffset(IdxWidth, 0);
3011 Value *UnderlyingPtrOp =
3013 BasePtrOffset);
3014 bool CanBeNull, CanBeFreed;
3015 uint64_t DerefBytes = UnderlyingPtrOp->getPointerDereferenceableBytes(
3016 DL, CanBeNull, CanBeFreed);
3017 if (!CanBeNull && !CanBeFreed && DerefBytes != 0) {
3018 if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
3019 BasePtrOffset.isNonNegative()) {
3020 APInt AllocSize(IdxWidth, DerefBytes);
3021 if (BasePtrOffset.ule(AllocSize)) {
3023 GEP.getSourceElementType(), PtrOp, Indices, GEP.getName());
3024 }
3025 }
3026 }
3027 }
3028
3030 return R;
3031
3032 return nullptr;
3033}
3034
3036 Instruction *AI) {
3037 if (isa<ConstantPointerNull>(V))
3038 return true;
3039 if (auto *LI = dyn_cast<LoadInst>(V))
3040 return isa<GlobalVariable>(LI->getPointerOperand());
3041 // Two distinct allocations will never be equal.
3042 return isAllocLikeFn(V, &TLI) && V != AI;
3043}
3044
3045/// Given a call CB which uses an address UsedV, return true if we can prove the
3046/// call's only possible effect is storing to V.
3047static bool isRemovableWrite(CallBase &CB, Value *UsedV,
3048 const TargetLibraryInfo &TLI) {
3049 if (!CB.use_empty())
3050 // TODO: add recursion if returned attribute is present
3051 return false;
3052
3053 if (CB.isTerminator())
3054 // TODO: remove implementation restriction
3055 return false;
3056
3057 if (!CB.willReturn() || !CB.doesNotThrow())
3058 return false;
3059
3060 // If the only possible side effect of the call is writing to the alloca,
3061 // and the result isn't used, we can safely remove any reads implied by the
3062 // call including those which might read the alloca itself.
3063 std::optional<MemoryLocation> Dest = MemoryLocation::getForDest(&CB, TLI);
3064 return Dest && Dest->Ptr == UsedV;
3065}
3066
3069 const TargetLibraryInfo &TLI) {
3071 const std::optional<StringRef> Family = getAllocationFamily(AI, &TLI);
3072 Worklist.push_back(AI);
3073
3074 do {
3075 Instruction *PI = Worklist.pop_back_val();
3076 for (User *U : PI->users()) {
3077 Instruction *I = cast<Instruction>(U);
3078 switch (I->getOpcode()) {
3079 default:
3080 // Give up the moment we see something we can't handle.
3081 return false;
3082
3083 case Instruction::AddrSpaceCast:
3084 case Instruction::BitCast:
3085 case Instruction::GetElementPtr:
3086 Users.emplace_back(I);
3087 Worklist.push_back(I);
3088 continue;
3089
3090 case Instruction::ICmp: {
3091 ICmpInst *ICI = cast<ICmpInst>(I);
3092 // We can fold eq/ne comparisons with null to false/true, respectively.
3093 // We also fold comparisons in some conditions provided the alloc has
3094 // not escaped (see isNeverEqualToUnescapedAlloc).
3095 if (!ICI->isEquality())
3096 return false;
3097 unsigned OtherIndex = (ICI->getOperand(0) == PI) ? 1 : 0;
3098 if (!isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex), TLI, AI))
3099 return false;
3100
3101 // Do not fold compares to aligned_alloc calls, as they may have to
3102 // return null in case the required alignment cannot be satisfied,
3103 // unless we can prove that both alignment and size are valid.
3104 auto AlignmentAndSizeKnownValid = [](CallBase *CB) {
3105 // Check if alignment and size of a call to aligned_alloc is valid,
3106 // that is alignment is a power-of-2 and the size is a multiple of the
3107 // alignment.
3108 const APInt *Alignment;
3109 const APInt *Size;
3110 return match(CB->getArgOperand(0), m_APInt(Alignment)) &&
3111 match(CB->getArgOperand(1), m_APInt(Size)) &&
3112 Alignment->isPowerOf2() && Size->urem(*Alignment).isZero();
3113 };
3114 auto *CB = dyn_cast<CallBase>(AI);
3115 LibFunc TheLibFunc;
3116 if (CB && TLI.getLibFunc(*CB->getCalledFunction(), TheLibFunc) &&
3117 TLI.has(TheLibFunc) && TheLibFunc == LibFunc_aligned_alloc &&
3118 !AlignmentAndSizeKnownValid(CB))
3119 return false;
3120 Users.emplace_back(I);
3121 continue;
3122 }
3123
3124 case Instruction::Call:
3125 // Ignore no-op and store intrinsics.
3126 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
3127 switch (II->getIntrinsicID()) {
3128 default:
3129 return false;
3130
3131 case Intrinsic::memmove:
3132 case Intrinsic::memcpy:
3133 case Intrinsic::memset: {
3134 MemIntrinsic *MI = cast<MemIntrinsic>(II);
3135 if (MI->isVolatile() || MI->getRawDest() != PI)
3136 return false;
3137 [[fallthrough]];
3138 }
3139 case Intrinsic::assume:
3140 case Intrinsic::invariant_start:
3141 case Intrinsic::invariant_end:
3142 case Intrinsic::lifetime_start:
3143 case Intrinsic::lifetime_end:
3144 case Intrinsic::objectsize:
3145 Users.emplace_back(I);
3146 continue;
3147 case Intrinsic::launder_invariant_group:
3148 case Intrinsic::strip_invariant_group:
3149 Users.emplace_back(I);
3150 Worklist.push_back(I);
3151 continue;
3152 }
3153 }
3154
3155 if (isRemovableWrite(*cast<CallBase>(I), PI, TLI)) {
3156 Users.emplace_back(I);
3157 continue;
3158 }
3159
3160 if (getFreedOperand(cast<CallBase>(I), &TLI) == PI &&
3161 getAllocationFamily(I, &TLI) == Family) {
3162 assert(Family);
3163 Users.emplace_back(I);
3164 continue;
3165 }
3166
3167 if (getReallocatedOperand(cast<CallBase>(I)) == PI &&
3168 getAllocationFamily(I, &TLI) == Family) {
3169 assert(Family);
3170 Users.emplace_back(I);
3171 Worklist.push_back(I);
3172 continue;
3173 }
3174
3175 return false;
3176
3177 case Instruction::Store: {
3178 StoreInst *SI = cast<StoreInst>(I);
3179 if (SI->isVolatile() || SI->getPointerOperand() != PI)
3180 return false;
3181 Users.emplace_back(I);
3182 continue;
3183 }
3184 }
3185 llvm_unreachable("missing a return?");
3186 }
3187 } while (!Worklist.empty());
3188 return true;
3189}
3190
3192 assert(isa<AllocaInst>(MI) || isRemovableAlloc(&cast<CallBase>(MI), &TLI));
3193
3194 // If we have a malloc call which is only used in any amount of comparisons to
3195 // null and free calls, delete the calls and replace the comparisons with true
3196 // or false as appropriate.
3197
3198 // This is based on the principle that we can substitute our own allocation
3199 // function (which will never return null) rather than knowledge of the
3200 // specific function being called. In some sense this can change the permitted
3201 // outputs of a program (when we convert a malloc to an alloca, the fact that
3202 // the allocation is now on the stack is potentially visible, for example),
3203 // but we believe in a permissible manner.
3205
3206 // If we are removing an alloca with a dbg.declare, insert dbg.value calls
3207 // before each store.
3210 std::unique_ptr<DIBuilder> DIB;
3211 if (isa<AllocaInst>(MI)) {
3212 findDbgUsers(DVIs, &MI, &DVRs);
3213 DIB.reset(new DIBuilder(*MI.getModule(), /*AllowUnresolved=*/false));
3214 }
3215
3216 if (isAllocSiteRemovable(&MI, Users, TLI)) {
3217 for (unsigned i = 0, e = Users.size(); i != e; ++i) {
3218 // Lowering all @llvm.objectsize calls first because they may
3219 // use a bitcast/GEP of the alloca we are removing.
3220 if (!Users[i])
3221 continue;
3222
3223 Instruction *I = cast<Instruction>(&*Users[i]);
3224
3225 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
3226 if (II->getIntrinsicID() == Intrinsic::objectsize) {
3227 SmallVector<Instruction *> InsertedInstructions;
3228 Value *Result = lowerObjectSizeCall(
3229 II, DL, &TLI, AA, /*MustSucceed=*/true, &InsertedInstructions);
3230 for (Instruction *Inserted : InsertedInstructions)
3231 Worklist.add(Inserted);
3232 replaceInstUsesWith(*I, Result);
3234 Users[i] = nullptr; // Skip examining in the next loop.
3235 }
3236 }
3237 }
3238 for (unsigned i = 0, e = Users.size(); i != e; ++i) {
3239 if (!Users[i])
3240 continue;
3241
3242 Instruction *I = cast<Instruction>(&*Users[i]);
3243
3244 if (ICmpInst *C = dyn_cast<ICmpInst>(I)) {
3246 ConstantInt::get(Type::getInt1Ty(C->getContext()),
3247 C->isFalseWhenEqual()));
3248 } else if (auto *SI = dyn_cast<StoreInst>(I)) {
3249 for (auto *DVI : DVIs)
3250 if (DVI->isAddressOfVariable())
3251 ConvertDebugDeclareToDebugValue(DVI, SI, *DIB);
3252 for (auto *DVR : DVRs)
3253 if (DVR->isAddressOfVariable())
3254 ConvertDebugDeclareToDebugValue(DVR, SI, *DIB);
3255 } else {
3256 // Casts, GEP, or anything else: we're about to delete this instruction,
3257 // so it can not have any valid uses.
3258 replaceInstUsesWith(*I, PoisonValue::get(I->getType()));
3259 }
3261 }
3262
3263 if (InvokeInst *II = dyn_cast<InvokeInst>(&MI)) {
3264 // Replace invoke with a NOP intrinsic to maintain the original CFG
3265 Module *M = II->getModule();
3266 Function *F = Intrinsic::getDeclaration(M, Intrinsic::donothing);
3267 InvokeInst::Create(F, II->getNormalDest(), II->getUnwindDest(),
3268 std::nullopt, "", II->getParent());
3269 }
3270
3271 // Remove debug intrinsics which describe the value contained within the
3272 // alloca. In addition to removing dbg.{declare,addr} which simply point to
3273 // the alloca, remove dbg.value(<alloca>, ..., DW_OP_deref)'s as well, e.g.:
3274 //
3275 // ```
3276 // define void @foo(i32 %0) {
3277 // %a = alloca i32 ; Deleted.
3278 // store i32 %0, i32* %a
3279 // dbg.value(i32 %0, "arg0") ; Not deleted.
3280 // dbg.value(i32* %a, "arg0", DW_OP_deref) ; Deleted.
3281 // call void @trivially_inlinable_no_op(i32* %a)
3282 // ret void
3283 // }
3284 // ```
3285 //
3286 // This may not be required if we stop describing the contents of allocas
3287 // using dbg.value(<alloca>, ..., DW_OP_deref), but we currently do this in
3288 // the LowerDbgDeclare utility.
3289 //
3290 // If there is a dead store to `%a` in @trivially_inlinable_no_op, the
3291 // "arg0" dbg.value may be stale after the call. However, failing to remove
3292 // the DW_OP_deref dbg.value causes large gaps in location coverage.
3293 //
3294 // FIXME: the Assignment Tracking project has now likely made this
3295 // redundant (and it's sometimes harmful).
3296 for (auto *DVI : DVIs)
3297 if (DVI->isAddressOfVariable() || DVI->getExpression()->startsWithDeref())
3298 DVI->eraseFromParent();
3299 for (auto *DVR : DVRs)
3300 if (DVR->isAddressOfVariable() || DVR->getExpression()->startsWithDeref())
3301 DVR->eraseFromParent();
3302
3303 return eraseInstFromFunction(MI);
3304 }
3305 return nullptr;
3306}
3307
3308/// Move the call to free before a NULL test.
3309///
3310/// Check if this free is accessed after its argument has been test
3311/// against NULL (property 0).
3312/// If yes, it is legal to move this call in its predecessor block.
3313///
3314/// The move is performed only if the block containing the call to free
3315/// will be removed, i.e.:
3316/// 1. it has only one predecessor P, and P has two successors
3317/// 2. it contains the call, noops, and an unconditional branch
3318/// 3. its successor is the same as its predecessor's successor
3319///
3320/// The profitability is out-of concern here and this function should
3321/// be called only if the caller knows this transformation would be
3322/// profitable (e.g., for code size).
3324 const DataLayout &DL) {
3325 Value *Op = FI.getArgOperand(0);
3326 BasicBlock *FreeInstrBB = FI.getParent();
3327 BasicBlock *PredBB = FreeInstrBB->getSinglePredecessor();
3328
3329 // Validate part of constraint #1: Only one predecessor
3330 // FIXME: We can extend the number of predecessor, but in that case, we
3331 // would duplicate the call to free in each predecessor and it may
3332 // not be profitable even for code size.
3333 if (!PredBB)
3334 return nullptr;
3335
3336 // Validate constraint #2: Does this block contains only the call to
3337 // free, noops, and an unconditional branch?
3338 BasicBlock *SuccBB;
3339 Instruction *FreeInstrBBTerminator = FreeInstrBB->getTerminator();
3340 if (!match(FreeInstrBBTerminator, m_UnconditionalBr(SuccBB)))
3341 return nullptr;
3342
3343 // If there are only 2 instructions in the block, at this point,
3344 // this is the call to free and unconditional.
3345 // If there are more than 2 instructions, check that they are noops
3346 // i.e., they won't hurt the performance of the generated code.
3347 if (FreeInstrBB->size() != 2) {
3348 for (const Instruction &Inst : FreeInstrBB->instructionsWithoutDebug()) {
3349 if (&Inst == &FI || &Inst == FreeInstrBBTerminator)
3350 continue;
3351 auto *Cast = dyn_cast<CastInst>(&Inst);
3352 if (!Cast || !Cast->isNoopCast(DL))
3353 return nullptr;
3354 }
3355 }
3356 // Validate the rest of constraint #1 by matching on the pred branch.
3357 Instruction *TI = PredBB->getTerminator();
3358 BasicBlock *TrueBB, *FalseBB;
3360 if (!match(TI, m_Br(m_ICmp(Pred,
3362 m_Specific(Op->stripPointerCasts())),
3363 m_Zero()),
3364 TrueBB, FalseBB)))
3365 return nullptr;
3366 if (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE)
3367 return nullptr;
3368
3369 // Validate constraint #3: Ensure the null case just falls through.
3370 if (SuccBB != (Pred == ICmpInst::ICMP_EQ ? TrueBB : FalseBB))
3371 return nullptr;
3372 assert(FreeInstrBB == (Pred == ICmpInst::ICMP_EQ ? FalseBB : TrueBB) &&
3373 "Broken CFG: missing edge from predecessor to successor");
3374
3375 // At this point, we know that everything in FreeInstrBB can be moved
3376 // before TI.
3377 for (Instruction &Instr : llvm::make_early_inc_range(*FreeInstrBB)) {
3378 if (&Instr == FreeInstrBBTerminator)
3379 break;
3380 Instr.moveBeforePreserving(TI);
3381 }
3382 assert(FreeInstrBB->size() == 1 &&
3383 "Only the branch instruction should remain");
3384
3385 // Now that we've moved the call to free before the NULL check, we have to
3386 // remove any attributes on its parameter that imply it's non-null, because
3387 // those attributes might have only been valid because of the NULL check, and
3388 // we can get miscompiles if we keep them. This is conservative if non-null is
3389 // also implied by something other than the NULL check, but it's guaranteed to
3390 // be correct, and the conservativeness won't matter in practice, since the
3391 // attributes are irrelevant for the call to free itself and the pointer
3392 // shouldn't be used after the call.
3393 AttributeList Attrs = FI.getAttributes();
3394 Attrs = Attrs.removeParamAttribute(FI.getContext(), 0, Attribute::NonNull);
3395 Attribute Dereferenceable = Attrs.getParamAttr(0, Attribute::Dereferenceable);
3396 if (Dereferenceable.isValid()) {
3397 uint64_t Bytes = Dereferenceable.getDereferenceableBytes();
3398 Attrs = Attrs.removeParamAttribute(FI.getContext(), 0,
3399 Attribute::Dereferenceable);
3400 Attrs = Attrs.addDereferenceableOrNullParamAttr(FI.getContext(), 0, Bytes);
3401 }
3402 FI.setAttributes(Attrs);
3403
3404 return &FI;
3405}
3406
3408 // free undef -> unreachable.
3409 if (isa<UndefValue>(Op)) {
3410 // Leave a marker since we can't modify the CFG here.
3412 return eraseInstFromFunction(FI);
3413 }
3414
3415 // If we have 'free null' delete the instruction. This can happen in stl code
3416 // when lots of inlining happens.
3417 if (isa<ConstantPointerNull>(Op))
3418 return eraseInstFromFunction(FI);
3419
3420 // If we had free(realloc(...)) with no intervening uses, then eliminate the
3421 // realloc() entirely.
3422 CallInst *CI = dyn_cast<CallInst>(Op);
3423 if (CI && CI->hasOneUse())
3424 if (Value *ReallocatedOp = getReallocatedOperand(CI))
3425 return eraseInstFromFunction(*replaceInstUsesWith(*CI, ReallocatedOp));
3426
3427 // If we optimize for code size, try to move the call to free before the null
3428 // test so that simplify cfg can remove the empty block and dead code
3429 // elimination the branch. I.e., helps to turn something like:
3430 // if (foo) free(foo);
3431 // into
3432 // free(foo);
3433 //
3434 // Note that we can only do this for 'free' and not for any flavor of
3435 // 'operator delete'; there is no 'operator delete' symbol for which we are
3436 // permitted to invent a call, even if we're passing in a null pointer.
3437 if (MinimizeSize) {
3438 LibFunc Func;
3439 if (TLI.getLibFunc(FI, Func) && TLI.has(Func) && Func == LibFunc_free)
3441 return I;
3442 }
3443
3444 return nullptr;
3445}
3446
3448 Value *RetVal = RI.getReturnValue();
3449 if (!RetVal || !AttributeFuncs::isNoFPClassCompatibleType(RetVal->getType()))
3450 return nullptr;
3451
3452 Function *F = RI.getFunction();
3453 FPClassTest ReturnClass = F->getAttributes().getRetNoFPClass();
3454 if (ReturnClass == fcNone)
3455 return nullptr;
3456
3457 KnownFPClass KnownClass;
3458 Value *Simplified =
3459 SimplifyDemandedUseFPClass(RetVal, ~ReturnClass, KnownClass, 0, &RI);
3460 if (!Simplified)
3461 return nullptr;
3462
3463 return ReturnInst::Create(RI.getContext(), Simplified);
3464}
3465
3466// WARNING: keep in sync with SimplifyCFGOpt::simplifyUnreachable()!
3468 // Try to remove the previous instruction if it must lead to unreachable.
3469 // This includes instructions like stores and "llvm.assume" that may not get
3470 // removed by simple dead code elimination.
3471 bool Changed = false;
3472 while (Instruction *Prev = I.getPrevNonDebugInstruction()) {
3473 // While we theoretically can erase EH, that would result in a block that
3474 // used to start with an EH no longer starting with EH, which is invalid.
3475 // To make it valid, we'd need to fixup predecessors to no longer refer to
3476 // this block, but that changes CFG, which is not allowed in InstCombine.
3477 if (Prev->isEHPad())
3478 break; // Can not drop any more instructions. We're done here.
3479
3481 break; // Can not drop any more instructions. We're done here.
3482 // Otherwise, this instruction can be freely erased,
3483 // even if it is not side-effect free.
3484
3485 // A value may still have uses before we process it here (for example, in
3486 // another unreachable block), so convert those to poison.
3487 replaceInstUsesWith(*Prev, PoisonValue::get(Prev->getType()));
3488 eraseInstFromFunction(*Prev);
3489 Changed = true;
3490 }
3491 return Changed;
3492}
3493
3496 return nullptr;
3497}
3498
3500 assert(BI.isUnconditional() && "Only for unconditional branches.");
3501
3502 // If this store is the second-to-last instruction in the basic block
3503 // (excluding debug info and bitcasts of pointers) and if the block ends with
3504 // an unconditional branch, try to move the store to the successor block.
3505
3506 auto GetLastSinkableStore = [](BasicBlock::iterator BBI) {
3507 auto IsNoopInstrForStoreMerging = [](BasicBlock::iterator BBI) {
3508 return BBI->isDebugOrPseudoInst() ||
3509 (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy());
3510 };
3511
3512 BasicBlock::iterator FirstInstr = BBI->getParent()->begin();
3513 do {
3514 if (BBI != FirstInstr)
3515 --BBI;
3516 } while (BBI != FirstInstr && IsNoopInstrForStoreMerging(BBI));
3517
3518 return dyn_cast<StoreInst>(BBI);
3519 };
3520
3521 if (StoreInst *SI = GetLastSinkableStore(BasicBlock::iterator(BI)))
3522 if (mergeStoreIntoSuccessor(*SI))
3523 return &BI;
3524
3525 return nullptr;
3526}
3527
3530 if (!DeadEdges.insert({From, To}).second)
3531 return;
3532
3533 // Replace phi node operands in successor with poison.
3534 for (PHINode &PN : To->phis())
3535 for (Use &U : PN.incoming_values())
3536 if (PN.getIncomingBlock(U) == From && !isa<PoisonValue>(U)) {
3537 replaceUse(U, PoisonValue::get(PN.getType()));
3538 addToWorklist(&PN);
3539 MadeIRChange = true;
3540 }
3541
3542 Worklist.push_back(To);
3543}
3544
3545// Under the assumption that I is unreachable, remove it and following
3546// instructions. Changes are reported directly to MadeIRChange.
3549 BasicBlock *BB = I->getParent();
3550 for (Instruction &Inst : make_early_inc_range(
3551 make_range(std::next(BB->getTerminator()->getReverseIterator()),
3552 std::next(I->getReverseIterator())))) {
3553 if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
3554 replaceInstUsesWith(Inst, PoisonValue::get(Inst.getType()));
3555 MadeIRChange = true;
3556 }
3557 if (Inst.isEHPad() || Inst.getType()->isTokenTy())
3558 continue;
3559 // RemoveDIs: erase debug-info on this instruction manually.
3560 Inst.dropDbgRecords();
3562 MadeIRChange = true;
3563 }
3564
3565 SmallVector<Value *> Changed;
3566 if (handleUnreachableTerminator(BB->getTerminator(), Changed)) {
3567 MadeIRChange = true;
3568 for (Value *V : Changed)
3569 addToWorklist(cast<Instruction>(V));
3570 }
3571
3572 // Handle potentially dead successors.
3573 for (BasicBlock *Succ : successors(BB))
3574 addDeadEdge(BB, Succ, Worklist);
3575}
3576
3579 while (!Worklist.empty()) {
3580 BasicBlock *BB = Worklist.pop_back_val();
3581 if (!all_of(predecessors(BB), [&](BasicBlock *Pred) {
3582 return DeadEdges.contains({Pred, BB}) || DT.dominates(BB, Pred);
3583 }))
3584 continue;
3585
3587 }
3588}
3589
3591 BasicBlock *LiveSucc) {
3593 for (BasicBlock *Succ : successors(BB)) {
3594 // The live successor isn't dead.
3595 if (Succ == LiveSucc)
3596 continue;
3597
3598 addDeadEdge(BB, Succ, Worklist);
3599 }
3600
3602}
3603
3605 if (BI.isUnconditional())
3607
3608 // Change br (not X), label True, label False to: br X, label False, True
3609 Value *Cond = BI.getCondition();
3610 Value *X;
3611 if (match(Cond, m_Not(m_Value(X))) && !isa<Constant>(X)) {
3612 // Swap Destinations and condition...
3613 BI.swapSuccessors();
3614 if (BPI)
3616 return replaceOperand(BI, 0, X);
3617 }
3618
3619 // Canonicalize logical-and-with-invert as logical-or-with-invert.
3620 // This is done by inverting the condition and swapping successors:
3621 // br (X && !Y), T, F --> br !(X && !Y), F, T --> br (!X || Y), F, T
3622 Value *Y;
3623 if (isa<SelectInst>(Cond) &&
3624 match(Cond,
3626 Value *NotX = Builder.CreateNot(X, "not." + X->getName());
3627 Value *Or = Builder.CreateLogicalOr(NotX, Y);
3628 BI.swapSuccessors();
3629 if (BPI)
3631 return replaceOperand(BI, 0, Or);
3632 }
3633
3634 // If the condition is irrelevant, remove the use so that other
3635 // transforms on the condition become more effective.
3636 if (!isa<ConstantInt>(Cond) && BI.getSuccessor(0) == BI.getSuccessor(1))
3637 return replaceOperand(BI, 0, ConstantInt::getFalse(Cond->getType()));
3638
3639 // Canonicalize, for example, fcmp_one -> fcmp_oeq.
3640 CmpInst::Predicate Pred;
3641 if (match(Cond, m_OneUse(m_FCmp(Pred, m_Value(), m_Value()))) &&
3642 !isCanonicalPredicate(Pred)) {
3643 // Swap destinations and condition.
3644 auto *Cmp = cast<CmpInst>(Cond);
3645 Cmp->setPredicate(CmpInst::getInversePredicate(Pred));
3646 BI.swapSuccessors();
3647 if (BPI)
3649 Worklist.push(Cmp);
3650 return &BI;
3651 }
3652
3653 if (isa<UndefValue>(Cond)) {
3654 handlePotentiallyDeadSuccessors(BI.getParent(), /*LiveSucc*/ nullptr);
3655 return nullptr;
3656 }
3657 if (auto *CI = dyn_cast<ConstantInt>(Cond)) {
3659 BI.getSuccessor(!CI->getZExtValue()));
3660 return nullptr;
3661 }
3662
3663 DC.registerBranch(&BI);
3664 return nullptr;
3665}
3666
3667// Replaces (switch (select cond, X, C)/(select cond, C, X)) with (switch X) if
3668// we can prove that both (switch C) and (switch X) go to the default when cond
3669// is false/true.
3672 bool IsTrueArm) {
3673 unsigned CstOpIdx = IsTrueArm ? 1 : 2;
3674 auto *C = dyn_cast<ConstantInt>(Select->getOperand(CstOpIdx));
3675 if (!C)
3676 return nullptr;
3677
3678 BasicBlock *CstBB = SI.findCaseValue(C)->getCaseSuccessor();
3679 if (CstBB != SI.getDefaultDest())
3680 return nullptr;
3681 Value *X = Select->getOperand(3 - CstOpIdx);
3683 const APInt *RHSC;
3684 if (!match(Select->getCondition(),
3685 m_ICmp(Pred, m_Specific(X), m_APInt(RHSC))))
3686 return nullptr;
3687 if (IsTrueArm)
3688 Pred = ICmpInst::getInversePredicate(Pred);
3689
3690 // See whether we can replace the select with X
3692 for (auto Case : SI.cases())
3693 if (!CR.contains(Case.getCaseValue()->getValue()))
3694 return nullptr;
3695
3696 return X;
3697}
3698
3700 Value *Cond = SI.getCondition();
3701 Value *Op0;
3702 ConstantInt *AddRHS;
3703 if (match(Cond, m_Add(m_Value(Op0), m_ConstantInt(AddRHS)))) {
3704 // Change 'switch (X+4) case 1:' into 'switch (X) case -3'.
3705 for (auto Case : SI.cases()) {
3706 Constant *NewCase = ConstantExpr::getSub(Case.getCaseValue(), AddRHS);
3707 assert(isa<ConstantInt>(NewCase) &&
3708 "Result of expression should be constant");
3709 Case.setValue(cast<ConstantInt>(NewCase));
3710 }
3711 return replaceOperand(SI, 0, Op0);
3712 }
3713
3714 ConstantInt *SubLHS;
3715 if (match(Cond, m_Sub(m_ConstantInt(SubLHS), m_Value(Op0)))) {
3716 // Change 'switch (1-X) case 1:' into 'switch (X) case 0'.
3717 for (auto Case : SI.cases()) {
3718 Constant *NewCase = ConstantExpr::getSub(SubLHS, Case.getCaseValue());
3719 assert(isa<ConstantInt>(NewCase) &&
3720 "Result of expression should be constant");
3721 Case.setValue(cast<ConstantInt>(NewCase));
3722 }
3723 return replaceOperand(SI, 0, Op0);
3724 }
3725
3726 uint64_t ShiftAmt;
3727 if (match(Cond, m_Shl(m_Value(Op0), m_ConstantInt(ShiftAmt))) &&
3728 ShiftAmt < Op0->getType()->getScalarSizeInBits() &&
3729 all_of(SI.cases(), [&](const auto &Case) {
3730 return Case.getCaseValue()->getValue().countr_zero() >= ShiftAmt;
3731 })) {
3732 // Change 'switch (X << 2) case 4:' into 'switch (X) case 1:'.
3733 OverflowingBinaryOperator *Shl = cast<OverflowingBinaryOperator>(Cond);
3734 if (Shl->hasNoUnsignedWrap() || Shl->hasNoSignedWrap() ||
3735 Shl->hasOneUse()) {
3736 Value *NewCond = Op0;
3737 if (!Shl->hasNoUnsignedWrap() && !Shl->hasNoSignedWrap()) {
3738 // If the shift may wrap, we need to mask off the shifted bits.
3739 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
3740 NewCond = Builder.CreateAnd(
3741 Op0, APInt::getLowBitsSet(BitWidth, BitWidth - ShiftAmt));
3742 }
3743 for (auto Case : SI.cases()) {
3744 const APInt &CaseVal = Case.getCaseValue()->getValue();
3745 APInt ShiftedCase = Shl->hasNoSignedWrap() ? CaseVal.ashr(ShiftAmt)
3746 : CaseVal.lshr(ShiftAmt);
3747 Case.setValue(ConstantInt::get(SI.getContext(), ShiftedCase));
3748 }
3749 return replaceOperand(SI, 0, NewCond);
3750 }
3751 }
3752
3753 // Fold switch(zext/sext(X)) into switch(X) if possible.
3754 if (match(Cond, m_ZExtOrSExt(m_Value(Op0)))) {
3755 bool IsZExt = isa<ZExtInst>(Cond);
3756 Type *SrcTy = Op0->getType();
3757 unsigned NewWidth = SrcTy->getScalarSizeInBits();
3758
3759 if (all_of(SI.cases(), [&](const auto &Case) {
3760 const APInt &CaseVal = Case.getCaseValue()->getValue();
3761 return IsZExt ? CaseVal.isIntN(NewWidth)
3762 : CaseVal.isSignedIntN(NewWidth);
3763 })) {
3764 for (auto &Case : SI.cases()) {
3765 APInt TruncatedCase = Case.getCaseValue()->getValue().trunc(NewWidth);
3766 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
3767 }
3768 return replaceOperand(SI, 0, Op0);
3769 }
3770 }
3771
3772 // Fold switch(select cond, X, Y) into switch(X/Y) if possible
3773 if (auto *Select = dyn_cast<SelectInst>(Cond)) {
3774 if (Value *V =
3775 simplifySwitchOnSelectUsingRanges(SI, Select, /*IsTrueArm=*/true))
3776 return replaceOperand(SI, 0, V);
3777 if (Value *V =
3778 simplifySwitchOnSelectUsingRanges(SI, Select, /*IsTrueArm=*/false))
3779 return replaceOperand(SI, 0, V);
3780 }
3781
3782 KnownBits Known = computeKnownBits(Cond, 0, &SI);
3783 unsigned LeadingKnownZeros = Known.countMinLeadingZeros();
3784 unsigned LeadingKnownOnes = Known.countMinLeadingOnes();
3785
3786 // Compute the number of leading bits we can ignore.
3787 // TODO: A better way to determine this would use ComputeNumSignBits().
3788 for (const auto &C : SI.cases()) {
3789 LeadingKnownZeros =
3790 std::min(LeadingKnownZeros, C.getCaseValue()->getValue().countl_zero());
3791 LeadingKnownOnes =
3792 std::min(LeadingKnownOnes, C.getCaseValue()->getValue().countl_one());
3793 }
3794
3795 unsigned NewWidth = Known.getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes);
3796
3797 // Shrink the condition operand if the new type is smaller than the old type.
3798 // But do not shrink to a non-standard type, because backend can't generate
3799 // good code for that yet.
3800 // TODO: We can make it aggressive again after fixing PR39569.
3801 if (NewWidth > 0 && NewWidth < Known.getBitWidth() &&
3802 shouldChangeType(Known.getBitWidth(), NewWidth)) {
3803 IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth);
3805 Value *NewCond = Builder.CreateTrunc(Cond, Ty, "trunc");
3806
3807 for (auto Case : SI.cases()) {
3808 APInt TruncatedCase = Case.getCaseValue()->getValue().trunc(NewWidth);
3809 Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
3810 }
3811 return replaceOperand(SI, 0, NewCond);
3812 }
3813
3814 if (isa<UndefValue>(Cond)) {
3815 handlePotentiallyDeadSuccessors(SI.getParent(), /*LiveSucc*/ nullptr);
3816 return nullptr;
3817 }
3818 if (auto *CI = dyn_cast<ConstantInt>(Cond)) {
3819 handlePotentiallyDeadSuccessors(SI.getParent(),
3820 SI.findCaseValue(CI)->getCaseSuccessor());
3821 return nullptr;
3822 }
3823
3824 return nullptr;
3825}
3826
3828InstCombinerImpl::foldExtractOfOverflowIntrinsic(ExtractValueInst &EV) {
3829 auto *WO = dyn_cast<WithOverflowInst>(EV.getAggregateOperand());
3830 if (!WO)
3831 return nullptr;
3832
3833 Intrinsic::ID OvID = WO->getIntrinsicID();
3834 const APInt *C = nullptr;
3835 if (match(WO->getRHS(), m_APIntAllowPoison(C))) {
3836 if (*EV.idx_begin() == 0 && (OvID == Intrinsic::smul_with_overflow ||
3837 OvID == Intrinsic::umul_with_overflow)) {
3838 // extractvalue (any_mul_with_overflow X, -1), 0 --> -X
3839 if (C->isAllOnes())
3840 return BinaryOperator::CreateNeg(WO->getLHS());
3841 // extractvalue (any_mul_with_overflow X, 2^n), 0 --> X << n
3842 if (C->isPowerOf2()) {
3843 return BinaryOperator::CreateShl(
3844 WO->getLHS(),
3845 ConstantInt::get(WO->getLHS()->getType(), C->logBase2()));
3846 }
3847 }
3848 }
3849
3850 // We're extracting from an overflow intrinsic. See if we're the only user.
3851 // That allows us to simplify multiple result intrinsics to simpler things
3852 // that just get one value.
3853 if (!WO->hasOneUse())
3854 return nullptr;
3855
3856 // Check if we're grabbing only the result of a 'with overflow' intrinsic
3857 // and replace it with a traditional binary instruction.
3858 if (*EV.idx_begin() == 0) {
3859 Instruction::BinaryOps BinOp = WO->getBinaryOp();
3860 Value *LHS = WO->getLHS(), *RHS = WO->getRHS();
3861 // Replace the old instruction's uses with poison.
3862 replaceInstUsesWith(*WO, PoisonValue::get(WO->getType()));
3864 return BinaryOperator::Create(BinOp, LHS, RHS);
3865 }
3866
3867 assert(*EV.idx_begin() == 1 && "Unexpected extract index for overflow inst");
3868
3869 // (usub LHS, RHS) overflows when LHS is unsigned-less-than RHS.
3870 if (OvID == Intrinsic::usub_with_overflow)
3871 return new ICmpInst(ICmpInst::ICMP_ULT, WO->getLHS(), WO->getRHS());
3872
3873 // smul with i1 types overflows when both sides are set: -1 * -1 == +1, but
3874 // +1 is not possible because we assume signed values.
3875 if (OvID == Intrinsic::smul_with_overflow &&
3876 WO->getLHS()->getType()->isIntOrIntVectorTy(1))
3877 return BinaryOperator::CreateAnd(WO->getLHS(), WO->getRHS());
3878
3879 // extractvalue (umul_with_overflow X, X), 1 -> X u> 2^(N/2)-1
3880 if (OvID == Intrinsic::umul_with_overflow && WO->getLHS() == WO->getRHS()) {
3881 unsigned BitWidth = WO->getLHS()->getType()->getScalarSizeInBits();
3882 // Only handle even bitwidths for performance reasons.
3883 if (BitWidth % 2 == 0)
3884 return new ICmpInst(
3885 ICmpInst::ICMP_UGT, WO->getLHS(),
3886 ConstantInt::get(WO->getLHS()->getType(),
3888 }
3889
3890 // If only the overflow result is used, and the right hand side is a
3891 // constant (or constant splat), we can remove the intrinsic by directly
3892 // checking for overflow.
3893 if (C) {
3894 // Compute the no-wrap range for LHS given RHS=C, then construct an
3895 // equivalent icmp, potentially using an offset.
3897 WO->getBinaryOp(), *C, WO->getNoWrapKind());
3898
3899 CmpInst::Predicate Pred;
3900 APInt NewRHSC, Offset;
3901 NWR.getEquivalentICmp(Pred, NewRHSC, Offset);
3902 auto *OpTy = WO->getRHS()->getType();
3903 auto *NewLHS = WO->getLHS();
3904 if (Offset != 0)
3905 NewLHS = Builder.CreateAdd(NewLHS, ConstantInt::get(OpTy, Offset));
3906 return new ICmpInst(ICmpInst::getInversePredicate(Pred), NewLHS,
3907 ConstantInt::get(OpTy, NewRHSC));
3908 }
3909
3910 return nullptr;
3911}
3912
3914 Value *Agg = EV.getAggregateOperand();
3915
3916 if (!EV.hasIndices())
3917 return replaceInstUsesWith(EV, Agg);
3918
3919 if (Value *V = simplifyExtractValueInst(Agg, EV.getIndices(),
3920 SQ.getWithInstruction(&EV)))
3921 return replaceInstUsesWith(EV, V);
3922
3923 if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
3924 // We're extracting from an insertvalue instruction, compare the indices
3925 const unsigned *exti, *exte, *insi, *inse;
3926 for (exti = EV.idx_begin(), insi = IV->idx_begin(),
3927 exte = EV.idx_end(), inse = IV->idx_end();
3928 exti != exte && insi != inse;
3929 ++exti, ++insi) {
3930 if (*insi != *exti)
3931 // The insert and extract both reference distinctly different elements.
3932 // This means the extract is not influenced by the insert, and we can
3933 // replace the aggregate operand of the extract with the aggregate
3934 // operand of the insert. i.e., replace
3935 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
3936 // %E = extractvalue { i32, { i32 } } %I, 0
3937 // with
3938 // %E = extractvalue { i32, { i32 } } %A, 0
3939 return ExtractValueInst::Create(IV->getAggregateOperand(),
3940 EV.getIndices());
3941 }
3942 if (exti == exte && insi == inse)
3943 // Both iterators are at the end: Index lists are identical. Replace
3944 // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
3945 // %C = extractvalue { i32, { i32 } } %B, 1, 0
3946 // with "i32 42"
3947 return replaceInstUsesWith(EV, IV->getInsertedValueOperand());
3948 if (exti == exte) {
3949 // The extract list is a prefix of the insert list. i.e. replace
3950 // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
3951 // %E = extractvalue { i32, { i32 } } %I, 1
3952 // with
3953 // %X = extractvalue { i32, { i32 } } %A, 1
3954 // %E = insertvalue { i32 } %X, i32 42, 0
3955 // by switching the order of the insert and extract (though the
3956 // insertvalue should be left in, since it may have other uses).
3957 Value *NewEV = Builder.CreateExtractValue(IV->getAggregateOperand(),
3958 EV.getIndices());
3959 return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
3960 ArrayRef(insi, inse));
3961 }
3962 if (insi == inse)
3963 // The insert list is a prefix of the extract list
3964 // We can simply remove the common indices from the extract and make it
3965 // operate on the inserted value instead of the insertvalue result.
3966 // i.e., replace
3967 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
3968 // %E = extractvalue { i32, { i32 } } %I, 1, 0
3969 // with
3970 // %E extractvalue { i32 } { i32 42 }, 0
3971 return ExtractValueInst::Create(IV->getInsertedValueOperand(),
3972 ArrayRef(exti, exte));
3973 }
3974
3975 if (Instruction *R = foldExtractOfOverflowIntrinsic(EV))
3976 return R;
3977
3978 if (LoadInst *L = dyn_cast<LoadInst>(Agg)) {
3979 // Bail out if the aggregate contains scalable vector type
3980 if (auto *STy = dyn_cast<StructType>(Agg->getType());
3981 STy && STy->containsScalableVectorType())
3982 return nullptr;
3983
3984 // If the (non-volatile) load only has one use, we can rewrite this to a
3985 // load from a GEP. This reduces the size of the load. If a load is used
3986 // only by extractvalue instructions then this either must have been
3987 // optimized before, or it is a struct with padding, in which case we
3988 // don't want to do the transformation as it loses padding knowledge.
3989 if (L->isSimple() && L->hasOneUse()) {
3990 // extractvalue has integer indices, getelementptr has Value*s. Convert.
3991 SmallVector<Value*, 4> Indices;
3992 // Prefix an i32 0 since we need the first element.
3993 Indices.push_back(Builder.getInt32(0));
3994 for (unsigned Idx : EV.indices())
3995 Indices.push_back(Builder.getInt32(Idx));
3996
3997 // We need to insert these at the location of the old load, not at that of
3998 // the extractvalue.
4000 Value *GEP = Builder.CreateInBoundsGEP(L->getType(),
4001 L->getPointerOperand(), Indices);
4003 // Whatever aliasing information we had for the orignal load must also
4004 // hold for the smaller load, so propagate the annotations.
4005 NL->setAAMetadata(L->getAAMetadata());
4006 // Returning the load directly will cause the main loop to insert it in
4007 // the wrong spot, so use replaceInstUsesWith().
4008 return replaceInstUsesWith(EV, NL);
4009 }
4010 }
4011
4012 if (auto *PN = dyn_cast<PHINode>(Agg))
4013 if (Instruction *Res = foldOpIntoPhi(EV, PN))
4014 return Res;
4015
4016 // Canonicalize extract (select Cond, TV, FV)
4017 // -> select cond, (extract TV), (extract FV)
4018 if (auto *SI = dyn_cast<SelectInst>(Agg))
4019 if (Instruction *R = FoldOpIntoSelect(EV, SI, /*FoldWithMultiUse=*/true))
4020 return R;
4021
4022 // We could simplify extracts from other values. Note that nested extracts may
4023 // already be simplified implicitly by the above: extract (extract (insert) )
4024 // will be translated into extract ( insert ( extract ) ) first and then just
4025 // the value inserted, if appropriate. Similarly for extracts from single-use
4026 // loads: extract (extract (load)) will be translated to extract (load (gep))
4027 // and if again single-use then via load (gep (gep)) to load (gep).
4028 // However, double extracts from e.g. function arguments or return values
4029 // aren't handled yet.
4030 return nullptr;
4031}
4032
4033/// Return 'true' if the given typeinfo will match anything.
4034static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo) {
4035 switch (Personality) {
4039 // The GCC C EH and Rust personality only exists to support cleanups, so
4040 // it's not clear what the semantics of catch clauses are.
4041 return false;
4043 return false;
4045 // While __gnat_all_others_value will match any Ada exception, it doesn't
4046 // match foreign exceptions (or didn't, before gcc-4.7).
4047 return false;
4058 return TypeInfo->isNullValue();
4059 }
4060 llvm_unreachable("invalid enum");
4061}
4062
4063static bool shorter_filter(const Value *LHS, const Value *RHS) {
4064 return
4065 cast<ArrayType>(LHS->getType())->getNumElements()
4066 <
4067 cast<ArrayType>(RHS->getType())->getNumElements();
4068}
4069
4071 // The logic here should be correct for any real-world personality function.
4072 // However if that turns out not to be true, the offending logic can always
4073 // be conditioned on the personality function, like the catch-all logic is.
4074 EHPersonality Personality =
4075 classifyEHPersonality(LI.getParent()->getParent()->getPersonalityFn());
4076
4077 // Simplify the list of clauses, eg by removing repeated catch clauses
4078 // (these are often created by inlining).
4079 bool MakeNewInstruction = false; // If true, recreate using the following:
4080 SmallVector<Constant *, 16> NewClauses; // - Clauses for the new instruction;
4081 bool CleanupFlag = LI.isCleanup(); // - The new instruction is a cleanup.
4082
4083 SmallPtrSet<Value *, 16> AlreadyCaught; // Typeinfos known caught already.
4084 for (unsigned i = 0, e = LI.getNumClauses(); i != e; ++i) {
4085 bool isLastClause = i + 1 == e;
4086 if (LI.isCatch(i)) {
4087 // A catch clause.
4088 Constant *CatchClause = LI.getClause(i);
4089 Constant *TypeInfo = CatchClause->stripPointerCasts();
4090
4091 // If we already saw this clause, there is no point in having a second
4092 // copy of it.
4093 if (AlreadyCaught.insert(TypeInfo).second) {
4094 // This catch clause was not already seen.
4095 NewClauses.push_back(CatchClause);
4096 } else {
4097 // Repeated catch clause - drop the redundant copy.
4098 MakeNewInstruction = true;
4099 }
4100
4101 // If this is a catch-all then there is no point in keeping any following
4102 // clauses or marking the landingpad as having a cleanup.
4103 if (isCatchAll(Personality, TypeInfo)) {
4104 if (!isLastClause)
4105 MakeNewInstruction = true;
4106 CleanupFlag = false;
4107 break;
4108 }
4109 } else {
4110 // A filter clause. If any of the filter elements were already caught
4111 // then they can be dropped from the filter. It is tempting to try to
4112 // exploit the filter further by saying that any typeinfo that does not
4113 // occur in the filter can't be caught later (and thus can be dropped).
4114 // However this would be wrong, since typeinfos can match without being
4115 // equal (for example if one represents a C++ class, and the other some
4116 // class derived from it).
4117 assert(LI.isFilter(i) && "Unsupported landingpad clause!");
4118 Constant *FilterClause = LI.getClause(i);
4119 ArrayType *FilterType = cast<ArrayType>(FilterClause->getType());
4120 unsigned NumTypeInfos = FilterType->getNumElements();
4121
4122 // An empty filter catches everything, so there is no point in keeping any
4123 // following clauses or marking the landingpad as having a cleanup. By
4124 // dealing with this case here the following code is made a bit simpler.
4125 if (!NumTypeInfos) {
4126 NewClauses.push_back(FilterClause);
4127 if (!isLastClause)
4128 MakeNewInstruction = true;
4129 CleanupFlag = false;
4130 break;
4131 }
4132
4133 bool MakeNewFilter = false; // If true, make a new filter.
4134 SmallVector<Constant *, 16> NewFilterElts; // New elements.
4135 if (isa<ConstantAggregateZero>(FilterClause)) {
4136 // Not an empty filter - it contains at least one null typeinfo.
4137 assert(NumTypeInfos > 0 && "Should have handled empty filter already!");
4138 Constant *TypeInfo =
4140 // If this typeinfo is a catch-all then the filter can never match.
4141 if (isCatchAll(Personality, TypeInfo)) {
4142 // Throw the filter away.
4143 MakeNewInstruction = true;
4144 continue;
4145 }
4146
4147 // There is no point in having multiple copies of this typeinfo, so
4148 // discard all but the first copy if there is more than one.
4149 NewFilterElts.push_back(TypeInfo);
4150 if (NumTypeInfos > 1)
4151 MakeNewFilter = true;
4152 } else {
4153 ConstantArray *Filter = cast<ConstantArray>(FilterClause);
4154 SmallPtrSet<Value *, 16> SeenInFilter; // For uniquing the elements.
4155 NewFilterElts.reserve(NumTypeInfos);
4156
4157 // Remove any filter elements that were already caught or that already
4158 // occurred in the filter. While there, see if any of the elements are
4159 // catch-alls. If so, the filter can be discarded.
4160 bool SawCatchAll = false;
4161 for (unsigned j = 0; j != NumTypeInfos; ++j) {
4162 Constant *Elt = Filter->getOperand(j);
4163 Constant *TypeInfo = Elt->stripPointerCasts();
4164 if (isCatchAll(Personality, TypeInfo)) {
4165 // This element is a catch-all. Bail out, noting this fact.
4166 SawCatchAll = true;
4167 break;
4168 }
4169
4170 // Even if we've seen a type in a catch clause, we don't want to
4171 // remove it from the filter. An unexpected type handler may be
4172 // set up for a call site which throws an exception of the same
4173 // type caught. In order for the exception thrown by the unexpected
4174 // handler to propagate correctly, the filter must be correctly
4175 // described for the call site.
4176 //
4177 // Example:
4178 //
4179 // void unexpected() { throw 1;}
4180 // void foo() throw (int) {
4181 // std::set_unexpected(unexpected);
4182 // try {
4183 // throw 2.0;
4184 // } catch (int i) {}
4185 // }
4186
4187 // There is no point in having multiple copies of the same typeinfo in
4188 // a filter, so only add it if we didn't already.
4189 if (SeenInFilter.insert(TypeInfo).second)
4190 NewFilterElts.push_back(cast<Constant>(Elt));
4191 }
4192 // A filter containing a catch-all cannot match anything by definition.
4193 if (SawCatchAll) {
4194 // Throw the filter away.
4195 MakeNewInstruction = true;
4196 continue;
4197 }
4198
4199 // If we dropped something from the filter, make a new one.
4200 if (NewFilterElts.size() < NumTypeInfos)
4201 MakeNewFilter = true;
4202 }
4203 if (MakeNewFilter) {
4204 FilterType = ArrayType::get(FilterType->getElementType(),
4205 NewFilterElts.size());
4206 FilterClause = ConstantArray::get(FilterType, NewFilterElts);
4207 MakeNewInstruction = true;
4208 }
4209
4210 NewClauses.push_back(FilterClause);
4211
4212 // If the new filter is empty then it will catch everything so there is
4213 // no point in keeping any following clauses or marking the landingpad
4214 // as having a cleanup. The case of the original filter being empty was
4215 // already handled above.
4216 if (MakeNewFilter && !NewFilterElts.size()) {
4217 assert(MakeNewInstruction && "New filter but not a new instruction!");
4218 CleanupFlag = false;
4219 break;
4220 }
4221 }
4222 }
4223
4224 // If several filters occur in a row then reorder them so that the shortest
4225 // filters come first (those with the smallest number of elements). This is
4226 // advantageous because shorter filters are more likely to match, speeding up
4227 // unwinding, but mostly because it increases the effectiveness of the other
4228 // filter optimizations below.
4229 for (unsigned i = 0, e = NewClauses.size(); i + 1 < e; ) {
4230 unsigned j;
4231 // Find the maximal 'j' s.t. the range [i, j) consists entirely of filters.
4232 for (j = i; j != e; ++j)
4233 if (!isa<ArrayType>(NewClauses[j]->getType()))
4234 break;
4235
4236 // Check whether the filters are already sorted by length. We need to know
4237 // if sorting them is actually going to do anything so that we only make a
4238 // new landingpad instruction if it does.
4239 for (unsigned k = i; k + 1 < j; ++k)
4240 if (shorter_filter(NewClauses[k+1], NewClauses[k])) {
4241 // Not sorted, so sort the filters now. Doing an unstable sort would be
4242 // correct too but reordering filters pointlessly might confuse users.
4243 std::stable_sort(NewClauses.begin() + i, NewClauses.begin() + j,
4245 MakeNewInstruction = true;
4246 break;
4247 }
4248
4249 // Look for the next batch of filters.
4250 i = j + 1;
4251 }
4252
4253 // If typeinfos matched if and only if equal, then the elements of a filter L
4254 // that occurs later than a filter F could be replaced by the intersection of
4255 // the elements of F and L. In reality two typeinfos can match without being
4256 // equal (for example if one represents a C++ class, and the other some class
4257 // derived from it) so it would be wrong to perform this transform in general.
4258 // However the transform is correct and useful if F is a subset of L. In that
4259 // case L can be replaced by F, and thus removed altogether since repeating a
4260 // filter is pointless. So here we look at all pairs of filters F and L where
4261 // L follows F in the list of clauses, and remove L if every element of F is
4262 // an element of L. This can occur when inlining C++ functions with exception
4263 // specifications.
4264 for (unsigned i = 0; i + 1 < NewClauses.size(); ++i) {
4265 // Examine each filter in turn.
4266 Value *Filter = NewClauses[i];
4267 ArrayType *FTy = dyn_cast<ArrayType>(Filter->getType());
4268 if (!FTy)
4269 // Not a filter - skip it.
4270 continue;
4271 unsigned FElts = FTy->getNumElements();
4272 // Examine each filter following this one. Doing this backwards means that
4273 // we don't have to worry about filters disappearing under us when removed.
4274 for (unsigned j = NewClauses.size() - 1; j != i; --j) {
4275 Value *LFilter = NewClauses[j];
4276 ArrayType *LTy = dyn_cast<ArrayType>(LFilter->getType());
4277 if (!LTy)
4278 // Not a filter - skip it.
4279 continue;
4280 // If Filter is a subset of LFilter, i.e. every element of Filter is also
4281 // an element of LFilter, then discard LFilter.
4282 SmallVectorImpl<Constant *>::iterator J = NewClauses.begin() + j;
4283 // If Filter is empty then it is a subset of LFilter.
4284 if (!FElts) {
4285 // Discard LFilter.
4286 NewClauses.erase(J);
4287 MakeNewInstruction = true;
4288 // Move on to the next filter.
4289 continue;
4290 }
4291 unsigned LElts = LTy->getNumElements();
4292 // If Filter is longer than LFilter then it cannot be a subset of it.
4293 if (FElts > LElts)
4294 // Move on to the next filter.
4295 continue;
4296 // At this point we know that LFilter has at least one element.
4297 if (isa<ConstantAggregateZero>(LFilter)) { // LFilter only contains zeros.
4298 // Filter is a subset of LFilter iff Filter contains only zeros (as we
4299 // already know that Filter is not longer than LFilter).
4300 if (isa<ConstantAggregateZero>(Filter)) {
4301 assert(FElts <= LElts && "Should have handled this case earlier!");
4302 // Discard LFilter.
4303 NewClauses.erase(J);
4304 MakeNewInstruction = true;
4305 }
4306 // Move on to the next filter.
4307 continue;
4308 }
4309 ConstantArray *LArray = cast<ConstantArray>(LFilter);
4310 if (isa<ConstantAggregateZero>(Filter)) { // Filter only contains zeros.
4311 // Since Filter is non-empty and contains only zeros, it is a subset of
4312 // LFilter iff LFilter contains a zero.
4313 assert(FElts > 0 && "Should have eliminated the empty filter earlier!");
4314 for (unsigned l = 0; l != LElts; ++l)
4315 if (LArray->getOperand(l)->isNullValue()) {
4316 // LFilter contains a zero - discard it.
4317 NewClauses.erase(J);
4318 MakeNewInstruction = true;
4319 break;
4320 }
4321 // Move on to the next filter.
4322 continue;
4323 }
4324 // At this point we know that both filters are ConstantArrays. Loop over
4325 // operands to see whether every element of Filter is also an element of
4326 // LFilter. Since filters tend to be short this is probably faster than
4327 // using a method that scales nicely.
4328 ConstantArray *FArray = cast<ConstantArray>(Filter);
4329 bool AllFound = true;
4330 for (unsigned f = 0; f != FElts; ++f) {
4331 Value *FTypeInfo = FArray->getOperand(f)->stripPointerCasts();
4332 AllFound = false;
4333 for (unsigned l = 0; l != LElts; ++l) {
4334 Value *LTypeInfo = LArray->getOperand(l)->stripPointerCasts();
4335 if (LTypeInfo == FTypeInfo) {
4336 AllFound = true;
4337 break;
4338 }
4339 }
4340 if (!AllFound)
4341 break;
4342 }
4343 if (AllFound) {
4344 // Discard LFilter.
4345 NewClauses.erase(J);
4346 MakeNewInstruction = true;
4347 }
4348 // Move on to the next filter.
4349 }
4350 }
4351
4352 // If we changed any of the clauses, replace the old landingpad instruction
4353 // with a new one.
4354 if (MakeNewInstruction) {
4355 LandingPadInst *NLI = LandingPadInst::Create(LI.getType(),
4356 NewClauses.size());
4357 for (unsigned i = 0, e = NewClauses.size(); i != e; ++i)
4358 NLI->addClause(NewClauses[i]);
4359 // A landing pad with no clauses must have the cleanup flag set. It is
4360 // theoretically possible, though highly unlikely, that we eliminated all
4361 // clauses. If so, force the cleanup flag to true.
4362 if (NewClauses.empty())
4363 CleanupFlag = true;
4364 NLI->setCleanup(CleanupFlag);
4365 return NLI;
4366 }
4367
4368 // Even if none of the clauses changed, we may nonetheless have understood
4369 // that the cleanup flag is pointless. Clear it if so.
4370 if (LI.isCleanup() != CleanupFlag) {
4371 assert(!CleanupFlag && "Adding a cleanup, not removing one?!");
4372 LI.setCleanup(CleanupFlag);
4373 return &LI;
4374 }
4375
4376 return nullptr;
4377}
4378
4379Value *
4381 // Try to push freeze through instructions that propagate but don't produce
4382 // poison as far as possible. If an operand of freeze follows three
4383 // conditions 1) one-use, 2) does not produce poison, and 3) has all but one
4384 // guaranteed-non-poison operands then push the freeze through to the one
4385 // operand that is not guaranteed non-poison. The actual transform is as
4386 // follows.
4387 // Op1 = ... ; Op1 can be posion
4388 // Op0 = Inst(Op1, NonPoisonOps...) ; Op0 has only one use and only have
4389 // ; single guaranteed-non-poison operands
4390 // ... = Freeze(Op0)
4391 // =>
4392 // Op1 = ...
4393 // Op1.fr = Freeze(Op1)
4394 // ... = Inst(Op1.fr, NonPoisonOps...)
4395 auto *OrigOp = OrigFI.getOperand(0);
4396 auto *OrigOpInst = dyn_cast<Instruction>(OrigOp);
4397
4398 // While we could change the other users of OrigOp to use freeze(OrigOp), that
4399 // potentially reduces their optimization potential, so let's only do this iff
4400 // the OrigOp is only used by the freeze.
4401 if (!OrigOpInst || !OrigOpInst->hasOneUse() || isa<PHINode>(OrigOp))
4402 return nullptr;
4403
4404 // We can't push the freeze through an instruction which can itself create
4405 // poison. If the only source of new poison is flags, we can simply
4406 // strip them (since we know the only use is the freeze and nothing can
4407 // benefit from them.)
4408 if (canCreateUndefOrPoison(cast<Operator>(OrigOp),
4409 /*ConsiderFlagsAndMetadata*/ false))
4410 return nullptr;
4411
4412 // If operand is guaranteed not to be poison, there is no need to add freeze
4413 // to the operand. So we first find the operand that is not guaranteed to be
4414 // poison.
4415 Use *MaybePoisonOperand = nullptr;
4416 for (Use &U : OrigOpInst->operands()) {
4417 if (isa<MetadataAsValue>(U.get()) ||
4419 continue;
4420 if (!MaybePoisonOperand)
4421 MaybePoisonOperand = &U;
4422 else
4423 return nullptr;
4424 }
4425
4426 OrigOpInst->dropPoisonGeneratingAnnotations();
4427
4428 // If all operands are guaranteed to be non-poison, we can drop freeze.
4429 if (!MaybePoisonOperand)
4430 return OrigOp;
4431
4432 Builder.SetInsertPoint(OrigOpInst);
4433 auto *FrozenMaybePoisonOperand = Builder.CreateFreeze(
4434 MaybePoisonOperand->get(), MaybePoisonOperand->get()->getName() + ".fr");
4435
4436 replaceUse(*MaybePoisonOperand, FrozenMaybePoisonOperand);
4437 return OrigOp;
4438}
4439
4441 PHINode *PN) {
4442 // Detect whether this is a recurrence with a start value and some number of
4443 // backedge values. We'll check whether we can push the freeze through the
4444 // backedge values (possibly dropping poison flags along the way) until we
4445 // reach the phi again. In that case, we can move the freeze to the start
4446 // value.
4447 Use *StartU = nullptr;
4449 for (Use &U : PN->incoming_values()) {
4450 if (DT.dominates(PN->getParent(), PN->getIncomingBlock(U))) {
4451 // Add backedge value to worklist.
4452 Worklist.push_back(U.get());
4453 continue;
4454 }
4455
4456 // Don't bother handling multiple start values.
4457 if (StartU)
4458 return nullptr;
4459 StartU = &U;
4460 }
4461
4462 if (!StartU || Worklist.empty())
4463 return nullptr; // Not a recurrence.
4464
4465 Value *StartV = StartU->get();
4466 BasicBlock *StartBB = PN->getIncomingBlock(*StartU);
4467 bool StartNeedsFreeze = !isGuaranteedNotToBeUndefOrPoison(StartV);
4468 // We can't insert freeze if the start value is the result of the
4469 // terminator (e.g. an invoke).
4470 if (StartNeedsFreeze && StartBB->getTerminator() == StartV)
4471 return nullptr;
4472
4475 while (!Worklist.empty()) {
4476 Value *V = Worklist.pop_back_val();
4477 if (!Visited.insert(V).second)
4478 continue;
4479
4480 if (Visited.size() > 32)
4481 return nullptr; // Limit the total number of values we inspect.
4482
4483 // Assume that PN is non-poison, because it will be after the transform.
4484 if (V == PN || isGuaranteedNotToBeUndefOrPoison(V))
4485 continue;
4486
4487 Instruction *I = dyn_cast<Instruction>(V);
4488 if (!I || canCreateUndefOrPoison(cast<Operator>(I),
4489 /*ConsiderFlagsAndMetadata*/ false))
4490 return nullptr;
4491
4492 DropFlags.push_back(I);
4493 append_range(Worklist, I->operands());
4494 }
4495
4496 for (Instruction *I : DropFlags)
4497 I->dropPoisonGeneratingAnnotations();
4498
4499 if (StartNeedsFreeze) {
4501 Value *FrozenStartV = Builder.CreateFreeze(StartV,
4502 StartV->getName() + ".fr");
4503 replaceUse(*StartU, FrozenStartV);
4504 }
4505 return replaceInstUsesWith(FI, PN);
4506}
4507
4509 Value *Op = FI.getOperand(0);
4510
4511 if (isa<Constant>(Op) || Op->hasOneUse())
4512 return false;
4513
4514 // Move the freeze directly after the definition of its operand, so that
4515 // it dominates the maximum number of uses. Note that it may not dominate
4516 // *all* uses if the operand is an invoke/callbr and the use is in a phi on
4517 // the normal/default destination. This is why the domination check in the
4518 // replacement below is still necessary.
4519 BasicBlock::iterator MoveBefore;
4520 if (isa<Argument>(Op)) {
4521 MoveBefore =
4523 } else {
4524 auto MoveBeforeOpt = cast<Instruction>(Op)->getInsertionPointAfterDef();
4525 if (!MoveBeforeOpt)
4526 return false;
4527 MoveBefore = *MoveBeforeOpt;
4528 }
4529
4530 // Don't move to the position of a debug intrinsic.
4531 if (isa<DbgInfoIntrinsic>(MoveBefore))
4532 MoveBefore = MoveBefore->getNextNonDebugInstruction()->getIterator();
4533 // Re-point iterator to come after any debug-info records, if we're
4534 // running in "RemoveDIs" mode
4535 MoveBefore.setHeadBit(false);
4536
4537 bool Changed = false;
4538 if (&FI != &*MoveBefore) {
4539 FI.moveBefore(*MoveBefore->getParent(), MoveBefore);
4540 Changed = true;
4541 }
4542
4543 Op->replaceUsesWithIf(&FI, [&](Use &U) -> bool {
4544 bool Dominates = DT.dominates(&FI, U);
4545 Changed |= Dominates;
4546 return Dominates;
4547 });
4548
4549 return Changed;
4550}
4551
4552// Check if any direct or bitcast user of this value is a shuffle instruction.
4554 for (auto *U : V->users()) {
4555 if (isa<ShuffleVectorInst>(U))
4556 return true;
4557 else if (match(U, m_BitCast(m_Specific(V))) && isUsedWithinShuffleVector(U))
4558 return true;
4559 }
4560 return false;
4561}
4562
4564 Value *Op0 = I.getOperand(0);
4565
4567 return replaceInstUsesWith(I, V);
4568
4569 // freeze (phi const, x) --> phi const, (freeze x)
4570 if (auto *PN = dyn_cast<PHINode>(Op0)) {
4571 if (Instruction *NV = foldOpIntoPhi(I, PN))
4572 return NV;
4573 if (Instruction *NV = foldFreezeIntoRecurrence(I, PN))
4574 return NV;
4575 }
4576
4578 return replaceInstUsesWith(I, NI);
4579
4580 // If I is freeze(undef), check its uses and fold it to a fixed constant.
4581 // - or: pick -1
4582 // - select's condition: if the true value is constant, choose it by making
4583 // the condition true.
4584 // - default: pick 0
4585 //
4586 // Note that this transform is intentionally done here rather than
4587 // via an analysis in InstSimplify or at individual user sites. That is
4588 // because we must produce the same value for all uses of the freeze -
4589 // it's the reason "freeze" exists!
4590 //
4591 // TODO: This could use getBinopAbsorber() / getBinopIdentity() to avoid
4592 // duplicating logic for binops at least.
4593 auto getUndefReplacement = [&I](Type *Ty) {
4594 Constant *BestValue = nullptr;
4595 Constant *NullValue = Constant::getNullValue(Ty);
4596 for (const auto *U : I.users()) {
4597 Constant *C = NullValue;
4598 if (match(U, m_Or(m_Value(), m_Value())))
4600 else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
4601 C = ConstantInt::getTrue(Ty);
4602
4603 if (!BestValue)
4604 BestValue = C;
4605 else if (BestValue != C)
4606 BestValue = NullValue;
4607 }
4608 a