LLVM 19.0.0git
CorrelatedValuePropagation.cpp
Go to the documentation of this file.
1//===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Correlated Value Propagation pass.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/ADT/Statistic.h"
22#include "llvm/IR/Attributes.h"
23#include "llvm/IR/BasicBlock.h"
24#include "llvm/IR/CFG.h"
25#include "llvm/IR/Constant.h"
27#include "llvm/IR/Constants.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/IRBuilder.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
35#include "llvm/IR/Operator.h"
36#include "llvm/IR/PassManager.h"
37#include "llvm/IR/Type.h"
38#include "llvm/IR/Value.h"
42#include <cassert>
43#include <optional>
44#include <utility>
45
46using namespace llvm;
47
48#define DEBUG_TYPE "correlated-value-propagation"
49
50STATISTIC(NumPhis, "Number of phis propagated");
51STATISTIC(NumPhiCommon, "Number of phis deleted via common incoming value");
52STATISTIC(NumSelects, "Number of selects propagated");
53STATISTIC(NumCmps, "Number of comparisons propagated");
54STATISTIC(NumReturns, "Number of return values propagated");
55STATISTIC(NumDeadCases, "Number of switch cases removed");
56STATISTIC(NumSDivSRemsNarrowed,
57 "Number of sdivs/srems whose width was decreased");
58STATISTIC(NumSDivs, "Number of sdiv converted to udiv");
59STATISTIC(NumUDivURemsNarrowed,
60 "Number of udivs/urems whose width was decreased");
61STATISTIC(NumAShrsConverted, "Number of ashr converted to lshr");
62STATISTIC(NumAShrsRemoved, "Number of ashr removed");
63STATISTIC(NumSRems, "Number of srem converted to urem");
64STATISTIC(NumSExt, "Number of sext converted to zext");
65STATISTIC(NumSIToFP, "Number of sitofp converted to uitofp");
66STATISTIC(NumSICmps, "Number of signed icmp preds simplified to unsigned");
67STATISTIC(NumAnd, "Number of ands removed");
68STATISTIC(NumNW, "Number of no-wrap deductions");
69STATISTIC(NumNSW, "Number of no-signed-wrap deductions");
70STATISTIC(NumNUW, "Number of no-unsigned-wrap deductions");
71STATISTIC(NumAddNW, "Number of no-wrap deductions for add");
72STATISTIC(NumAddNSW, "Number of no-signed-wrap deductions for add");
73STATISTIC(NumAddNUW, "Number of no-unsigned-wrap deductions for add");
74STATISTIC(NumSubNW, "Number of no-wrap deductions for sub");
75STATISTIC(NumSubNSW, "Number of no-signed-wrap deductions for sub");
76STATISTIC(NumSubNUW, "Number of no-unsigned-wrap deductions for sub");
77STATISTIC(NumMulNW, "Number of no-wrap deductions for mul");
78STATISTIC(NumMulNSW, "Number of no-signed-wrap deductions for mul");
79STATISTIC(NumMulNUW, "Number of no-unsigned-wrap deductions for mul");
80STATISTIC(NumShlNW, "Number of no-wrap deductions for shl");
81STATISTIC(NumShlNSW, "Number of no-signed-wrap deductions for shl");
82STATISTIC(NumShlNUW, "Number of no-unsigned-wrap deductions for shl");
83STATISTIC(NumAbs, "Number of llvm.abs intrinsics removed");
84STATISTIC(NumOverflows, "Number of overflow checks removed");
85STATISTIC(NumSaturating,
86 "Number of saturating arithmetics converted to normal arithmetics");
87STATISTIC(NumNonNull, "Number of function pointer arguments marked non-null");
88STATISTIC(NumMinMax, "Number of llvm.[us]{min,max} intrinsics removed");
89STATISTIC(NumSMinMax,
90 "Number of llvm.s{min,max} intrinsics simplified to unsigned");
91STATISTIC(NumUDivURemsNarrowedExpanded,
92 "Number of bound udiv's/urem's expanded");
93STATISTIC(NumNNeg, "Number of zext/uitofp non-negative deductions");
94
96 if (Constant *C = LVI->getConstant(V, At))
97 return C;
98
99 // TODO: The following really should be sunk inside LVI's core algorithm, or
100 // at least the outer shims around such.
101 auto *C = dyn_cast<CmpInst>(V);
102 if (!C)
103 return nullptr;
104
105 Value *Op0 = C->getOperand(0);
106 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
107 if (!Op1)
108 return nullptr;
109
111 C->getPredicate(), Op0, Op1, At, /*UseBlockValue=*/false);
112 if (Result == LazyValueInfo::Unknown)
113 return nullptr;
114
115 return (Result == LazyValueInfo::True)
116 ? ConstantInt::getTrue(C->getContext())
117 : ConstantInt::getFalse(C->getContext());
118}
119
121 if (S->getType()->isVectorTy() || isa<Constant>(S->getCondition()))
122 return false;
123
124 bool Changed = false;
125 for (Use &U : make_early_inc_range(S->uses())) {
126 auto *I = cast<Instruction>(U.getUser());
127 Constant *C;
128 if (auto *PN = dyn_cast<PHINode>(I))
129 C = LVI->getConstantOnEdge(S->getCondition(), PN->getIncomingBlock(U),
130 I->getParent(), I);
131 else
132 C = getConstantAt(S->getCondition(), I, LVI);
133
134 auto *CI = dyn_cast_or_null<ConstantInt>(C);
135 if (!CI)
136 continue;
137
138 U.set(CI->isOne() ? S->getTrueValue() : S->getFalseValue());
139 Changed = true;
140 ++NumSelects;
141 }
142
143 if (Changed && S->use_empty())
144 S->eraseFromParent();
145
146 return Changed;
147}
148
149/// Try to simplify a phi with constant incoming values that match the edge
150/// values of a non-constant value on all other edges:
151/// bb0:
152/// %isnull = icmp eq i8* %x, null
153/// br i1 %isnull, label %bb2, label %bb1
154/// bb1:
155/// br label %bb2
156/// bb2:
157/// %r = phi i8* [ %x, %bb1 ], [ null, %bb0 ]
158/// -->
159/// %r = %x
161 DominatorTree *DT) {
162 // Collect incoming constants and initialize possible common value.
164 Value *CommonValue = nullptr;
165 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
166 Value *Incoming = P->getIncomingValue(i);
167 if (auto *IncomingConstant = dyn_cast<Constant>(Incoming)) {
168 IncomingConstants.push_back(std::make_pair(IncomingConstant, i));
169 } else if (!CommonValue) {
170 // The potential common value is initialized to the first non-constant.
171 CommonValue = Incoming;
172 } else if (Incoming != CommonValue) {
173 // There can be only one non-constant common value.
174 return false;
175 }
176 }
177
178 if (!CommonValue || IncomingConstants.empty())
179 return false;
180
181 // The common value must be valid in all incoming blocks.
182 BasicBlock *ToBB = P->getParent();
183 if (auto *CommonInst = dyn_cast<Instruction>(CommonValue))
184 if (!DT->dominates(CommonInst, ToBB))
185 return false;
186
187 // We have a phi with exactly 1 variable incoming value and 1 or more constant
188 // incoming values. See if all constant incoming values can be mapped back to
189 // the same incoming variable value.
190 for (auto &IncomingConstant : IncomingConstants) {
191 Constant *C = IncomingConstant.first;
192 BasicBlock *IncomingBB = P->getIncomingBlock(IncomingConstant.second);
193 if (C != LVI->getConstantOnEdge(CommonValue, IncomingBB, ToBB, P))
194 return false;
195 }
196
197 // LVI only guarantees that the value matches a certain constant if the value
198 // is not poison. Make sure we don't replace a well-defined value with poison.
199 // This is usually satisfied due to a prior branch on the value.
200 if (!isGuaranteedNotToBePoison(CommonValue, nullptr, P, DT))
201 return false;
202
203 // All constant incoming values map to the same variable along the incoming
204 // edges of the phi. The phi is unnecessary.
205 P->replaceAllUsesWith(CommonValue);
206 P->eraseFromParent();
207 ++NumPhiCommon;
208 return true;
209}
210
213 Instruction *CxtI) {
214 if (Constant *C = LVI->getConstantOnEdge(Incoming, From, To, CxtI))
215 return C;
216
217 // Look if the incoming value is a select with a scalar condition for which
218 // LVI can tells us the value. In that case replace the incoming value with
219 // the appropriate value of the select. This often allows us to remove the
220 // select later.
221 auto *SI = dyn_cast<SelectInst>(Incoming);
222 if (!SI)
223 return nullptr;
224
225 // Once LVI learns to handle vector types, we could also add support
226 // for vector type constants that are not all zeroes or all ones.
227 Value *Condition = SI->getCondition();
228 if (!Condition->getType()->isVectorTy()) {
229 if (Constant *C = LVI->getConstantOnEdge(Condition, From, To, CxtI)) {
230 if (C->isOneValue())
231 return SI->getTrueValue();
232 if (C->isZeroValue())
233 return SI->getFalseValue();
234 }
235 }
236
237 // Look if the select has a constant but LVI tells us that the incoming
238 // value can never be that constant. In that case replace the incoming
239 // value with the other value of the select. This often allows us to
240 // remove the select later.
241
242 // The "false" case
243 if (auto *C = dyn_cast<Constant>(SI->getFalseValue()))
244 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
246 return SI->getTrueValue();
247
248 // The "true" case,
249 // similar to the select "false" case, but try the select "true" value
250 if (auto *C = dyn_cast<Constant>(SI->getTrueValue()))
251 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
253 return SI->getFalseValue();
254
255 return nullptr;
256}
257
259 const SimplifyQuery &SQ) {
260 bool Changed = false;
261
262 BasicBlock *BB = P->getParent();
263 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
264 Value *Incoming = P->getIncomingValue(i);
265 if (isa<Constant>(Incoming)) continue;
266
267 Value *V = getValueOnEdge(LVI, Incoming, P->getIncomingBlock(i), BB, P);
268 if (V) {
269 P->setIncomingValue(i, V);
270 Changed = true;
271 }
272 }
273
274 if (Value *V = simplifyInstruction(P, SQ)) {
275 P->replaceAllUsesWith(V);
276 P->eraseFromParent();
277 Changed = true;
278 }
279
280 if (!Changed)
281 Changed = simplifyCommonValuePhi(P, LVI, DT);
282
283 if (Changed)
284 ++NumPhis;
285
286 return Changed;
287}
288
289static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
290 // Only for signed relational comparisons of scalar integers.
291 if (Cmp->getType()->isVectorTy() ||
292 !Cmp->getOperand(0)->getType()->isIntegerTy())
293 return false;
294
295 if (!Cmp->isSigned())
296 return false;
297
298 ICmpInst::Predicate UnsignedPred =
300 Cmp->getPredicate(),
301 LVI->getConstantRangeAtUse(Cmp->getOperandUse(0),
302 /*UndefAllowed*/ true),
303 LVI->getConstantRangeAtUse(Cmp->getOperandUse(1),
304 /*UndefAllowed*/ true));
305
306 if (UnsignedPred == ICmpInst::Predicate::BAD_ICMP_PREDICATE)
307 return false;
308
309 ++NumSICmps;
310 Cmp->setPredicate(UnsignedPred);
311
312 return true;
313}
314
315/// See if LazyValueInfo's ability to exploit edge conditions or range
316/// information is sufficient to prove this comparison. Even for local
317/// conditions, this can sometimes prove conditions instcombine can't by
318/// exploiting range information.
319static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
320 Value *Op0 = Cmp->getOperand(0);
321 Value *Op1 = Cmp->getOperand(1);
323 LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
324 /*UseBlockValue=*/true);
325 if (Result == LazyValueInfo::Unknown)
326 return false;
327
328 ++NumCmps;
329 Constant *TorF =
330 ConstantInt::get(CmpInst::makeCmpResultType(Op0->getType()), Result);
331 Cmp->replaceAllUsesWith(TorF);
332 Cmp->eraseFromParent();
333 return true;
334}
335
336static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
337 if (constantFoldCmp(Cmp, LVI))
338 return true;
339
340 if (auto *ICmp = dyn_cast<ICmpInst>(Cmp))
341 if (processICmp(ICmp, LVI))
342 return true;
343
344 return false;
345}
346
347/// Simplify a switch instruction by removing cases which can never fire. If the
348/// uselessness of a case could be determined locally then constant propagation
349/// would already have figured it out. Instead, walk the predecessors and
350/// statically evaluate cases based on information available on that edge. Cases
351/// that cannot fire no matter what the incoming edge can safely be removed. If
352/// a case fires on every incoming edge then the entire switch can be removed
353/// and replaced with a branch to the case destination.
355 DominatorTree *DT) {
356 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
357 Value *Cond = I->getCondition();
358 BasicBlock *BB = I->getParent();
359
360 // Analyse each switch case in turn.
361 bool Changed = false;
362 DenseMap<BasicBlock*, int> SuccessorsCount;
363 for (auto *Succ : successors(BB))
364 SuccessorsCount[Succ]++;
365
366 { // Scope for SwitchInstProfUpdateWrapper. It must not live during
367 // ConstantFoldTerminator() as the underlying SwitchInst can be changed.
369 unsigned ReachableCaseCount = 0;
370
371 for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
372 ConstantInt *Case = CI->getCaseValue();
375 /* UseBlockValue */ true);
376
377 if (State == LazyValueInfo::False) {
378 // This case never fires - remove it.
379 BasicBlock *Succ = CI->getCaseSuccessor();
380 Succ->removePredecessor(BB);
381 CI = SI.removeCase(CI);
382 CE = SI->case_end();
383
384 // The condition can be modified by removePredecessor's PHI simplification
385 // logic.
386 Cond = SI->getCondition();
387
388 ++NumDeadCases;
389 Changed = true;
390 if (--SuccessorsCount[Succ] == 0)
391 DTU.applyUpdatesPermissive({{DominatorTree::Delete, BB, Succ}});
392 continue;
393 }
394 if (State == LazyValueInfo::True) {
395 // This case always fires. Arrange for the switch to be turned into an
396 // unconditional branch by replacing the switch condition with the case
397 // value.
398 SI->setCondition(Case);
399 NumDeadCases += SI->getNumCases();
400 Changed = true;
401 break;
402 }
403
404 // Increment the case iterator since we didn't delete it.
405 ++CI;
406 ++ReachableCaseCount;
407 }
408
409 BasicBlock *DefaultDest = SI->getDefaultDest();
410 if (ReachableCaseCount > 1 &&
411 !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg())) {
412 ConstantRange CR = LVI->getConstantRangeAtUse(I->getOperandUse(0),
413 /*UndefAllowed*/ false);
414 // The default dest is unreachable if all cases are covered.
415 if (!CR.isSizeLargerThan(ReachableCaseCount)) {
416 BasicBlock *NewUnreachableBB =
417 BasicBlock::Create(BB->getContext(), "default.unreachable",
418 BB->getParent(), DefaultDest);
419 new UnreachableInst(BB->getContext(), NewUnreachableBB);
420
421 DefaultDest->removePredecessor(BB);
422 SI->setDefaultDest(NewUnreachableBB);
423
424 if (SuccessorsCount[DefaultDest] == 1)
425 DTU.applyUpdates({{DominatorTree::Delete, BB, DefaultDest}});
426 DTU.applyUpdates({{DominatorTree::Insert, BB, NewUnreachableBB}});
427
428 ++NumDeadCases;
429 Changed = true;
430 }
431 }
432 }
433
434 if (Changed)
435 // If the switch has been simplified to the point where it can be replaced
436 // by a branch then do so now.
437 ConstantFoldTerminator(BB, /*DeleteDeadConditions = */ false,
438 /*TLI = */ nullptr, &DTU);
439 return Changed;
440}
441
442// See if we can prove that the given binary op intrinsic will not overflow.
444 ConstantRange LRange =
445 LVI->getConstantRangeAtUse(BO->getOperandUse(0), /*UndefAllowed*/ false);
446 ConstantRange RRange =
447 LVI->getConstantRangeAtUse(BO->getOperandUse(1), /*UndefAllowed*/ false);
449 BO->getBinaryOp(), RRange, BO->getNoWrapKind());
450 return NWRegion.contains(LRange);
451}
452
454 bool NewNSW, bool NewNUW) {
455 Statistic *OpcNW, *OpcNSW, *OpcNUW;
456 switch (Opcode) {
457 case Instruction::Add:
458 OpcNW = &NumAddNW;
459 OpcNSW = &NumAddNSW;
460 OpcNUW = &NumAddNUW;
461 break;
462 case Instruction::Sub:
463 OpcNW = &NumSubNW;
464 OpcNSW = &NumSubNSW;
465 OpcNUW = &NumSubNUW;
466 break;
467 case Instruction::Mul:
468 OpcNW = &NumMulNW;
469 OpcNSW = &NumMulNSW;
470 OpcNUW = &NumMulNUW;
471 break;
472 case Instruction::Shl:
473 OpcNW = &NumShlNW;
474 OpcNSW = &NumShlNSW;
475 OpcNUW = &NumShlNUW;
476 break;
477 default:
478 llvm_unreachable("Will not be called with other binops");
479 }
480
481 auto *Inst = dyn_cast<Instruction>(V);
482 if (NewNSW) {
483 ++NumNW;
484 ++*OpcNW;
485 ++NumNSW;
486 ++*OpcNSW;
487 if (Inst)
488 Inst->setHasNoSignedWrap();
489 }
490 if (NewNUW) {
491 ++NumNW;
492 ++*OpcNW;
493 ++NumNUW;
494 ++*OpcNUW;
495 if (Inst)
496 Inst->setHasNoUnsignedWrap();
497 }
498}
499
500static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI);
501
502// See if @llvm.abs argument is alays positive/negative, and simplify.
503// Notably, INT_MIN can belong to either range, regardless of the NSW,
504// because it is negation-invariant.
506 Value *X = II->getArgOperand(0);
507 Type *Ty = X->getType();
508 if (!Ty->isIntegerTy())
509 return false;
510
511 bool IsIntMinPoison = cast<ConstantInt>(II->getArgOperand(1))->isOne();
514 II->getOperandUse(0), /*UndefAllowed*/ IsIntMinPoison);
515
516 // Is X in [0, IntMin]? NOTE: INT_MIN is fine!
517 if (Range.icmp(CmpInst::ICMP_ULE, IntMin)) {
518 ++NumAbs;
519 II->replaceAllUsesWith(X);
520 II->eraseFromParent();
521 return true;
522 }
523
524 // Is X in [IntMin, 0]? NOTE: INT_MIN is fine!
527 Value *NegX = B.CreateNeg(X, II->getName(),
528 /*HasNSW=*/IsIntMinPoison);
529 ++NumAbs;
530 II->replaceAllUsesWith(NegX);
531 II->eraseFromParent();
532
533 // See if we can infer some no-wrap flags.
534 if (auto *BO = dyn_cast<BinaryOperator>(NegX))
535 processBinOp(BO, LVI);
536
537 return true;
538 }
539
540 // Argument's range crosses zero.
541 // Can we at least tell that the argument is never INT_MIN?
542 if (!IsIntMinPoison && !Range.contains(IntMin)) {
543 ++NumNSW;
544 ++NumSubNSW;
545 II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
546 return true;
547 }
548 return false;
549}
550
551// See if this min/max intrinsic always picks it's one specific operand.
552// If not, check whether we can canonicalize signed minmax into unsigned version
556 /*UndefAllowed*/ false);
558 /*UndefAllowed*/ false);
559 if (LHS_CR.icmp(Pred, RHS_CR)) {
560 ++NumMinMax;
561 MM->replaceAllUsesWith(MM->getLHS());
562 MM->eraseFromParent();
563 return true;
564 }
565 if (RHS_CR.icmp(Pred, LHS_CR)) {
566 ++NumMinMax;
567 MM->replaceAllUsesWith(MM->getRHS());
568 MM->eraseFromParent();
569 return true;
570 }
571
572 if (MM->isSigned() &&
574 RHS_CR)) {
575 ++NumSMinMax;
576 IRBuilder<> B(MM);
577 MM->replaceAllUsesWith(B.CreateBinaryIntrinsic(
578 MM->getIntrinsicID() == Intrinsic::smin ? Intrinsic::umin
579 : Intrinsic::umax,
580 MM->getLHS(), MM->getRHS()));
581 MM->eraseFromParent();
582 return true;
583 }
584
585 return false;
586}
587
588// Rewrite this with.overflow intrinsic as non-overflowing.
590 IRBuilder<> B(WO);
591 Instruction::BinaryOps Opcode = WO->getBinaryOp();
592 bool NSW = WO->isSigned();
593 bool NUW = !WO->isSigned();
594
595 Value *NewOp =
596 B.CreateBinOp(Opcode, WO->getLHS(), WO->getRHS(), WO->getName());
597 setDeducedOverflowingFlags(NewOp, Opcode, NSW, NUW);
598
599 StructType *ST = cast<StructType>(WO->getType());
601 { PoisonValue::get(ST->getElementType(0)),
602 ConstantInt::getFalse(ST->getElementType(1)) });
603 Value *NewI = B.CreateInsertValue(Struct, NewOp, 0);
604 WO->replaceAllUsesWith(NewI);
605 WO->eraseFromParent();
606 ++NumOverflows;
607
608 // See if we can infer the other no-wrap too.
609 if (auto *BO = dyn_cast<BinaryOperator>(NewOp))
610 processBinOp(BO, LVI);
611
612 return true;
613}
614
616 Instruction::BinaryOps Opcode = SI->getBinaryOp();
617 bool NSW = SI->isSigned();
618 bool NUW = !SI->isSigned();
620 Opcode, SI->getLHS(), SI->getRHS(), SI->getName(), SI->getIterator());
621 BinOp->setDebugLoc(SI->getDebugLoc());
622 setDeducedOverflowingFlags(BinOp, Opcode, NSW, NUW);
623
624 SI->replaceAllUsesWith(BinOp);
625 SI->eraseFromParent();
626 ++NumSaturating;
627
628 // See if we can infer the other no-wrap too.
629 if (auto *BO = dyn_cast<BinaryOperator>(BinOp))
630 processBinOp(BO, LVI);
631
632 return true;
633}
634
635/// Infer nonnull attributes for the arguments at the specified callsite.
636static bool processCallSite(CallBase &CB, LazyValueInfo *LVI) {
637
638 if (CB.getIntrinsicID() == Intrinsic::abs) {
639 return processAbsIntrinsic(&cast<IntrinsicInst>(CB), LVI);
640 }
641
642 if (auto *MM = dyn_cast<MinMaxIntrinsic>(&CB)) {
643 return processMinMaxIntrinsic(MM, LVI);
644 }
645
646 if (auto *WO = dyn_cast<WithOverflowInst>(&CB)) {
647 if (WO->getLHS()->getType()->isIntegerTy() && willNotOverflow(WO, LVI)) {
648 return processOverflowIntrinsic(WO, LVI);
649 }
650 }
651
652 if (auto *SI = dyn_cast<SaturatingInst>(&CB)) {
653 if (SI->getType()->isIntegerTy() && willNotOverflow(SI, LVI)) {
654 return processSaturatingInst(SI, LVI);
655 }
656 }
657
658 bool Changed = false;
659
660 // Deopt bundle operands are intended to capture state with minimal
661 // perturbance of the code otherwise. If we can find a constant value for
662 // any such operand and remove a use of the original value, that's
663 // desireable since it may allow further optimization of that value (e.g. via
664 // single use rules in instcombine). Since deopt uses tend to,
665 // idiomatically, appear along rare conditional paths, it's reasonable likely
666 // we may have a conditional fact with which LVI can fold.
667 if (auto DeoptBundle = CB.getOperandBundle(LLVMContext::OB_deopt)) {
668 for (const Use &ConstU : DeoptBundle->Inputs) {
669 Use &U = const_cast<Use&>(ConstU);
670 Value *V = U.get();
671 if (V->getType()->isVectorTy()) continue;
672 if (isa<Constant>(V)) continue;
673
674 Constant *C = LVI->getConstant(V, &CB);
675 if (!C) continue;
676 U.set(C);
677 Changed = true;
678 }
679 }
680
682 unsigned ArgNo = 0;
683
684 for (Value *V : CB.args()) {
685 PointerType *Type = dyn_cast<PointerType>(V->getType());
686 // Try to mark pointer typed parameters as non-null. We skip the
687 // relatively expensive analysis for constants which are obviously either
688 // null or non-null to start with.
689 if (Type && !CB.paramHasAttr(ArgNo, Attribute::NonNull) &&
690 !isa<Constant>(V) &&
691 LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
693 /*UseBlockValue=*/false) == LazyValueInfo::False)
694 ArgNos.push_back(ArgNo);
695 ArgNo++;
696 }
697
698 assert(ArgNo == CB.arg_size() && "Call arguments not processed correctly.");
699
700 if (ArgNos.empty())
701 return Changed;
702
703 NumNonNull += ArgNos.size();
705 LLVMContext &Ctx = CB.getContext();
706 AS = AS.addParamAttribute(Ctx, ArgNos,
707 Attribute::get(Ctx, Attribute::NonNull));
708 CB.setAttributes(AS);
709
710 return true;
711}
712
714
715static Domain getDomain(const ConstantRange &CR) {
716 if (CR.isAllNonNegative())
717 return Domain::NonNegative;
718 if (CR.icmp(ICmpInst::ICMP_SLE, APInt::getZero(CR.getBitWidth())))
719 return Domain::NonPositive;
720 return Domain::Unknown;
721}
722
723/// Try to shrink a sdiv/srem's width down to the smallest power of two that's
724/// sufficient to contain its operands.
725static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR,
726 const ConstantRange &RCR) {
727 assert(Instr->getOpcode() == Instruction::SDiv ||
728 Instr->getOpcode() == Instruction::SRem);
729 assert(!Instr->getType()->isVectorTy());
730
731 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
732 // operands.
733 unsigned OrigWidth = Instr->getType()->getIntegerBitWidth();
734
735 // What is the smallest bit width that can accommodate the entire value ranges
736 // of both of the operands?
737 unsigned MinSignedBits =
738 std::max(LCR.getMinSignedBits(), RCR.getMinSignedBits());
739
740 // sdiv/srem is UB if divisor is -1 and divident is INT_MIN, so unless we can
741 // prove that such a combination is impossible, we need to bump the bitwidth.
742 if (RCR.contains(APInt::getAllOnes(OrigWidth)) &&
743 LCR.contains(APInt::getSignedMinValue(MinSignedBits).sext(OrigWidth)))
744 ++MinSignedBits;
745
746 // Don't shrink below 8 bits wide.
747 unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MinSignedBits), 8);
748
749 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
750 // two.
751 if (NewWidth >= OrigWidth)
752 return false;
753
754 ++NumSDivSRemsNarrowed;
755 IRBuilder<> B{Instr};
756 auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
757 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
758 Instr->getName() + ".lhs.trunc");
759 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
760 Instr->getName() + ".rhs.trunc");
761 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
762 auto *Sext = B.CreateSExt(BO, Instr->getType(), Instr->getName() + ".sext");
763 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
764 if (BinOp->getOpcode() == Instruction::SDiv)
765 BinOp->setIsExact(Instr->isExact());
766
767 Instr->replaceAllUsesWith(Sext);
768 Instr->eraseFromParent();
769 return true;
770}
771
772static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
773 const ConstantRange &YCR) {
774 Type *Ty = Instr->getType();
775 assert(Instr->getOpcode() == Instruction::UDiv ||
776 Instr->getOpcode() == Instruction::URem);
777 assert(!Ty->isVectorTy());
778 bool IsRem = Instr->getOpcode() == Instruction::URem;
779
780 Value *X = Instr->getOperand(0);
781 Value *Y = Instr->getOperand(1);
782
783 // X u/ Y -> 0 iff X u< Y
784 // X u% Y -> X iff X u< Y
785 if (XCR.icmp(ICmpInst::ICMP_ULT, YCR)) {
786 Instr->replaceAllUsesWith(IsRem ? X : Constant::getNullValue(Ty));
787 Instr->eraseFromParent();
788 ++NumUDivURemsNarrowedExpanded;
789 return true;
790 }
791
792 // Given
793 // R = X u% Y
794 // We can represent the modulo operation as a loop/self-recursion:
795 // urem_rec(X, Y):
796 // Z = X - Y
797 // if X u< Y
798 // ret X
799 // else
800 // ret urem_rec(Z, Y)
801 // which isn't better, but if we only need a single iteration
802 // to compute the answer, this becomes quite good:
803 // R = X < Y ? X : X - Y iff X u< 2*Y (w/ unsigned saturation)
804 // Now, we do not care about all full multiples of Y in X, they do not change
805 // the answer, thus we could rewrite the expression as:
806 // X* = X - (Y * |_ X / Y _|)
807 // R = X* % Y
808 // so we don't need the *first* iteration to return, we just need to
809 // know *which* iteration will always return, so we could also rewrite it as:
810 // X* = X - (Y * |_ X / Y _|)
811 // R = X* % Y iff X* u< 2*Y (w/ unsigned saturation)
812 // but that does not seem profitable here.
813
814 // Even if we don't know X's range, the divisor may be so large, X can't ever
815 // be 2x larger than that. I.e. if divisor is always negative.
816 if (!XCR.icmp(ICmpInst::ICMP_ULT,
817 YCR.umul_sat(APInt(YCR.getBitWidth(), 2))) &&
818 !YCR.isAllNegative())
819 return false;
820
821 IRBuilder<> B(Instr);
822 Value *ExpandedOp;
823 if (XCR.icmp(ICmpInst::ICMP_UGE, YCR)) {
824 // If X is between Y and 2*Y the result is known.
825 if (IsRem)
826 ExpandedOp = B.CreateNUWSub(X, Y);
827 else
828 ExpandedOp = ConstantInt::get(Instr->getType(), 1);
829 } else if (IsRem) {
830 // NOTE: this transformation introduces two uses of X,
831 // but it may be undef so we must freeze it first.
832 Value *FrozenX = X;
834 FrozenX = B.CreateFreeze(X, X->getName() + ".frozen");
835 Value *FrozenY = Y;
837 FrozenY = B.CreateFreeze(Y, Y->getName() + ".frozen");
838 auto *AdjX = B.CreateNUWSub(FrozenX, FrozenY, Instr->getName() + ".urem");
839 auto *Cmp = B.CreateICmp(ICmpInst::ICMP_ULT, FrozenX, FrozenY,
840 Instr->getName() + ".cmp");
841 ExpandedOp = B.CreateSelect(Cmp, FrozenX, AdjX);
842 } else {
843 auto *Cmp =
844 B.CreateICmp(ICmpInst::ICMP_UGE, X, Y, Instr->getName() + ".cmp");
845 ExpandedOp = B.CreateZExt(Cmp, Ty, Instr->getName() + ".udiv");
846 }
847 ExpandedOp->takeName(Instr);
848 Instr->replaceAllUsesWith(ExpandedOp);
849 Instr->eraseFromParent();
850 ++NumUDivURemsNarrowedExpanded;
851 return true;
852}
853
854/// Try to shrink a udiv/urem's width down to the smallest power of two that's
855/// sufficient to contain its operands.
856static bool narrowUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
857 const ConstantRange &YCR) {
858 assert(Instr->getOpcode() == Instruction::UDiv ||
859 Instr->getOpcode() == Instruction::URem);
860 assert(!Instr->getType()->isVectorTy());
861
862 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
863 // operands.
864
865 // What is the smallest bit width that can accommodate the entire value ranges
866 // of both of the operands?
867 unsigned MaxActiveBits = std::max(XCR.getActiveBits(), YCR.getActiveBits());
868 // Don't shrink below 8 bits wide.
869 unsigned NewWidth = std::max<unsigned>(PowerOf2Ceil(MaxActiveBits), 8);
870
871 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
872 // two.
873 if (NewWidth >= Instr->getType()->getIntegerBitWidth())
874 return false;
875
876 ++NumUDivURemsNarrowed;
877 IRBuilder<> B{Instr};
878 auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
879 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
880 Instr->getName() + ".lhs.trunc");
881 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
882 Instr->getName() + ".rhs.trunc");
883 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
884 auto *Zext = B.CreateZExt(BO, Instr->getType(), Instr->getName() + ".zext");
885 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
886 if (BinOp->getOpcode() == Instruction::UDiv)
887 BinOp->setIsExact(Instr->isExact());
888
889 Instr->replaceAllUsesWith(Zext);
890 Instr->eraseFromParent();
891 return true;
892}
893
895 assert(Instr->getOpcode() == Instruction::UDiv ||
896 Instr->getOpcode() == Instruction::URem);
897 if (Instr->getType()->isVectorTy())
898 return false;
899
900 ConstantRange XCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(0),
901 /*UndefAllowed*/ false);
902 // Allow undef for RHS, as we can assume it is division by zero UB.
903 ConstantRange YCR = LVI->getConstantRangeAtUse(Instr->getOperandUse(1),
904 /*UndefAllowed*/ true);
905 if (expandUDivOrURem(Instr, XCR, YCR))
906 return true;
907
908 return narrowUDivOrURem(Instr, XCR, YCR);
909}
910
911static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR,
912 const ConstantRange &RCR, LazyValueInfo *LVI) {
913 assert(SDI->getOpcode() == Instruction::SRem);
914 assert(!SDI->getType()->isVectorTy());
915
916 if (LCR.abs().icmp(CmpInst::ICMP_ULT, RCR.abs())) {
917 SDI->replaceAllUsesWith(SDI->getOperand(0));
918 SDI->eraseFromParent();
919 return true;
920 }
921
922 struct Operand {
923 Value *V;
924 Domain D;
925 };
926 std::array<Operand, 2> Ops = {{{SDI->getOperand(0), getDomain(LCR)},
927 {SDI->getOperand(1), getDomain(RCR)}}};
928 if (Ops[0].D == Domain::Unknown || Ops[1].D == Domain::Unknown)
929 return false;
930
931 // We know domains of both of the operands!
932 ++NumSRems;
933
934 // We need operands to be non-negative, so negate each one that isn't.
935 for (Operand &Op : Ops) {
936 if (Op.D == Domain::NonNegative)
937 continue;
938 auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
939 SDI->getIterator());
940 BO->setDebugLoc(SDI->getDebugLoc());
941 Op.V = BO;
942 }
943
944 auto *URem = BinaryOperator::CreateURem(Ops[0].V, Ops[1].V, SDI->getName(),
945 SDI->getIterator());
946 URem->setDebugLoc(SDI->getDebugLoc());
947
948 auto *Res = URem;
949
950 // If the divident was non-positive, we need to negate the result.
951 if (Ops[0].D == Domain::NonPositive) {
952 Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg",
953 SDI->getIterator());
954 Res->setDebugLoc(SDI->getDebugLoc());
955 }
956
957 SDI->replaceAllUsesWith(Res);
958 SDI->eraseFromParent();
959
960 // Try to simplify our new urem.
961 processUDivOrURem(URem, LVI);
962
963 return true;
964}
965
966/// See if LazyValueInfo's ability to exploit edge conditions or range
967/// information is sufficient to prove the signs of both operands of this SDiv.
968/// If this is the case, replace the SDiv with a UDiv. Even for local
969/// conditions, this can sometimes prove conditions instcombine can't by
970/// exploiting range information.
971static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR,
972 const ConstantRange &RCR, LazyValueInfo *LVI) {
973 assert(SDI->getOpcode() == Instruction::SDiv);
974 assert(!SDI->getType()->isVectorTy());
975
976 // Check whether the division folds to a constant.
977 ConstantRange DivCR = LCR.sdiv(RCR);
978 if (const APInt *Elem = DivCR.getSingleElement()) {
979 SDI->replaceAllUsesWith(ConstantInt::get(SDI->getType(), *Elem));
980 SDI->eraseFromParent();
981 return true;
982 }
983
984 struct Operand {
985 Value *V;
986 Domain D;
987 };
988 std::array<Operand, 2> Ops = {{{SDI->getOperand(0), getDomain(LCR)},
989 {SDI->getOperand(1), getDomain(RCR)}}};
990 if (Ops[0].D == Domain::Unknown || Ops[1].D == Domain::Unknown)
991 return false;
992
993 // We know domains of both of the operands!
994 ++NumSDivs;
995
996 // We need operands to be non-negative, so negate each one that isn't.
997 for (Operand &Op : Ops) {
998 if (Op.D == Domain::NonNegative)
999 continue;
1000 auto *BO = BinaryOperator::CreateNeg(Op.V, Op.V->getName() + ".nonneg",
1001 SDI->getIterator());
1002 BO->setDebugLoc(SDI->getDebugLoc());
1003 Op.V = BO;
1004 }
1005
1006 auto *UDiv = BinaryOperator::CreateUDiv(Ops[0].V, Ops[1].V, SDI->getName(),
1007 SDI->getIterator());
1008 UDiv->setDebugLoc(SDI->getDebugLoc());
1009 UDiv->setIsExact(SDI->isExact());
1010
1011 auto *Res = UDiv;
1012
1013 // If the operands had two different domains, we need to negate the result.
1014 if (Ops[0].D != Ops[1].D) {
1015 Res = BinaryOperator::CreateNeg(Res, Res->getName() + ".neg",
1016 SDI->getIterator());
1017 Res->setDebugLoc(SDI->getDebugLoc());
1018 }
1019
1020 SDI->replaceAllUsesWith(Res);
1021 SDI->eraseFromParent();
1022
1023 // Try to simplify our new udiv.
1024 processUDivOrURem(UDiv, LVI);
1025
1026 return true;
1027}
1028
1030 assert(Instr->getOpcode() == Instruction::SDiv ||
1031 Instr->getOpcode() == Instruction::SRem);
1032 if (Instr->getType()->isVectorTy())
1033 return false;
1034
1035 ConstantRange LCR =
1036 LVI->getConstantRangeAtUse(Instr->getOperandUse(0), /*AllowUndef*/ false);
1037 // Allow undef for RHS, as we can assume it is division by zero UB.
1038 ConstantRange RCR =
1039 LVI->getConstantRangeAtUse(Instr->getOperandUse(1), /*AlloweUndef*/ true);
1040 if (Instr->getOpcode() == Instruction::SDiv)
1041 if (processSDiv(Instr, LCR, RCR, LVI))
1042 return true;
1043
1044 if (Instr->getOpcode() == Instruction::SRem) {
1045 if (processSRem(Instr, LCR, RCR, LVI))
1046 return true;
1047 }
1048
1049 return narrowSDivOrSRem(Instr, LCR, RCR);
1050}
1051
1053 if (SDI->getType()->isVectorTy())
1054 return false;
1055
1056 ConstantRange LRange =
1057 LVI->getConstantRangeAtUse(SDI->getOperandUse(0), /*UndefAllowed*/ false);
1058 unsigned OrigWidth = SDI->getType()->getIntegerBitWidth();
1059 ConstantRange NegOneOrZero =
1060 ConstantRange(APInt(OrigWidth, (uint64_t)-1, true), APInt(OrigWidth, 1));
1061 if (NegOneOrZero.contains(LRange)) {
1062 // ashr of -1 or 0 never changes the value, so drop the whole instruction
1063 ++NumAShrsRemoved;
1064 SDI->replaceAllUsesWith(SDI->getOperand(0));
1065 SDI->eraseFromParent();
1066 return true;
1067 }
1068
1069 if (!LRange.isAllNonNegative())
1070 return false;
1071
1072 ++NumAShrsConverted;
1073 auto *BO = BinaryOperator::CreateLShr(SDI->getOperand(0), SDI->getOperand(1),
1074 "", SDI->getIterator());
1075 BO->takeName(SDI);
1076 BO->setDebugLoc(SDI->getDebugLoc());
1077 BO->setIsExact(SDI->isExact());
1078 SDI->replaceAllUsesWith(BO);
1079 SDI->eraseFromParent();
1080
1081 return true;
1082}
1083
1084static bool processSExt(SExtInst *SDI, LazyValueInfo *LVI) {
1085 if (SDI->getType()->isVectorTy())
1086 return false;
1087
1088 const Use &Base = SDI->getOperandUse(0);
1089 if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1091 return false;
1092
1093 ++NumSExt;
1094 auto *ZExt = CastInst::CreateZExtOrBitCast(Base, SDI->getType(), "",
1095 SDI->getIterator());
1096 ZExt->takeName(SDI);
1097 ZExt->setDebugLoc(SDI->getDebugLoc());
1098 ZExt->setNonNeg();
1099 SDI->replaceAllUsesWith(ZExt);
1100 SDI->eraseFromParent();
1101
1102 return true;
1103}
1104
1106 if (I->getType()->isVectorTy())
1107 return false;
1108
1109 if (I->hasNonNeg())
1110 return false;
1111
1112 const Use &Base = I->getOperandUse(0);
1113 if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1115 return false;
1116
1117 ++NumNNeg;
1118 I->setNonNeg();
1119
1120 return true;
1121}
1122
1123static bool processZExt(ZExtInst *ZExt, LazyValueInfo *LVI) {
1124 return processPossibleNonNeg(cast<PossiblyNonNegInst>(ZExt), LVI);
1125}
1126
1127static bool processUIToFP(UIToFPInst *UIToFP, LazyValueInfo *LVI) {
1128 return processPossibleNonNeg(cast<PossiblyNonNegInst>(UIToFP), LVI);
1129}
1130
1131static bool processSIToFP(SIToFPInst *SIToFP, LazyValueInfo *LVI) {
1132 if (SIToFP->getType()->isVectorTy())
1133 return false;
1134
1135 const Use &Base = SIToFP->getOperandUse(0);
1136 if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1138 return false;
1139
1140 ++NumSIToFP;
1141 auto *UIToFP = CastInst::Create(Instruction::UIToFP, Base, SIToFP->getType(),
1142 "", SIToFP->getIterator());
1143 UIToFP->takeName(SIToFP);
1144 UIToFP->setDebugLoc(SIToFP->getDebugLoc());
1145 UIToFP->setNonNeg();
1146 SIToFP->replaceAllUsesWith(UIToFP);
1147 SIToFP->eraseFromParent();
1148
1149 return true;
1150}
1151
1152static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI) {
1153 using OBO = OverflowingBinaryOperator;
1154
1155 if (BinOp->getType()->isVectorTy())
1156 return false;
1157
1158 bool NSW = BinOp->hasNoSignedWrap();
1159 bool NUW = BinOp->hasNoUnsignedWrap();
1160 if (NSW && NUW)
1161 return false;
1162
1163 Instruction::BinaryOps Opcode = BinOp->getOpcode();
1164 ConstantRange LRange = LVI->getConstantRangeAtUse(BinOp->getOperandUse(0),
1165 /*UndefAllowed=*/false);
1166 ConstantRange RRange = LVI->getConstantRangeAtUse(BinOp->getOperandUse(1),
1167 /*UndefAllowed=*/false);
1168
1169 bool Changed = false;
1170 bool NewNUW = false, NewNSW = false;
1171 if (!NUW) {
1173 Opcode, RRange, OBO::NoUnsignedWrap);
1174 NewNUW = NUWRange.contains(LRange);
1175 Changed |= NewNUW;
1176 }
1177 if (!NSW) {
1179 Opcode, RRange, OBO::NoSignedWrap);
1180 NewNSW = NSWRange.contains(LRange);
1181 Changed |= NewNSW;
1182 }
1183
1184 setDeducedOverflowingFlags(BinOp, Opcode, NewNSW, NewNUW);
1185
1186 return Changed;
1187}
1188
1189static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI) {
1190 if (BinOp->getType()->isVectorTy())
1191 return false;
1192
1193 // Pattern match (and lhs, C) where C includes a superset of bits which might
1194 // be set in lhs. This is a common truncation idiom created by instcombine.
1195 const Use &LHS = BinOp->getOperandUse(0);
1196 ConstantInt *RHS = dyn_cast<ConstantInt>(BinOp->getOperand(1));
1197 if (!RHS || !RHS->getValue().isMask())
1198 return false;
1199
1200 // We can only replace the AND with LHS based on range info if the range does
1201 // not include undef.
1202 ConstantRange LRange =
1203 LVI->getConstantRangeAtUse(LHS, /*UndefAllowed=*/false);
1204 if (!LRange.getUnsignedMax().ule(RHS->getValue()))
1205 return false;
1206
1207 BinOp->replaceAllUsesWith(LHS);
1208 BinOp->eraseFromParent();
1209 NumAnd++;
1210 return true;
1211}
1212
1214 const SimplifyQuery &SQ) {
1215 bool FnChanged = false;
1216 // Visiting in a pre-order depth-first traversal causes us to simplify early
1217 // blocks before querying later blocks (which require us to analyze early
1218 // blocks). Eagerly simplifying shallow blocks means there is strictly less
1219 // work to do for deep blocks. This also means we don't visit unreachable
1220 // blocks.
1221 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
1222 bool BBChanged = false;
1224 switch (II.getOpcode()) {
1225 case Instruction::Select:
1226 BBChanged |= processSelect(cast<SelectInst>(&II), LVI);
1227 break;
1228 case Instruction::PHI:
1229 BBChanged |= processPHI(cast<PHINode>(&II), LVI, DT, SQ);
1230 break;
1231 case Instruction::ICmp:
1232 case Instruction::FCmp:
1233 BBChanged |= processCmp(cast<CmpInst>(&II), LVI);
1234 break;
1235 case Instruction::Call:
1236 case Instruction::Invoke:
1237 BBChanged |= processCallSite(cast<CallBase>(II), LVI);
1238 break;
1239 case Instruction::SRem:
1240 case Instruction::SDiv:
1241 BBChanged |= processSDivOrSRem(cast<BinaryOperator>(&II), LVI);
1242 break;
1243 case Instruction::UDiv:
1244 case Instruction::URem:
1245 BBChanged |= processUDivOrURem(cast<BinaryOperator>(&II), LVI);
1246 break;
1247 case Instruction::AShr:
1248 BBChanged |= processAShr(cast<BinaryOperator>(&II), LVI);
1249 break;
1250 case Instruction::SExt:
1251 BBChanged |= processSExt(cast<SExtInst>(&II), LVI);
1252 break;
1253 case Instruction::ZExt:
1254 BBChanged |= processZExt(cast<ZExtInst>(&II), LVI);
1255 break;
1256 case Instruction::UIToFP:
1257 BBChanged |= processUIToFP(cast<UIToFPInst>(&II), LVI);
1258 break;
1259 case Instruction::SIToFP:
1260 BBChanged |= processSIToFP(cast<SIToFPInst>(&II), LVI);
1261 break;
1262 case Instruction::Add:
1263 case Instruction::Sub:
1264 case Instruction::Mul:
1265 case Instruction::Shl:
1266 BBChanged |= processBinOp(cast<BinaryOperator>(&II), LVI);
1267 break;
1268 case Instruction::And:
1269 BBChanged |= processAnd(cast<BinaryOperator>(&II), LVI);
1270 break;
1271 }
1272 }
1273
1274 Instruction *Term = BB->getTerminator();
1275 switch (Term->getOpcode()) {
1276 case Instruction::Switch:
1277 BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI, DT);
1278 break;
1279 case Instruction::Ret: {
1280 auto *RI = cast<ReturnInst>(Term);
1281 // Try to determine the return value if we can. This is mainly here to
1282 // simplify the writing of unit tests, but also helps to enable IPO by
1283 // constant folding the return values of callees.
1284 auto *RetVal = RI->getReturnValue();
1285 if (!RetVal) break; // handle "ret void"
1286 if (isa<Constant>(RetVal)) break; // nothing to do
1287 if (auto *C = getConstantAt(RetVal, RI, LVI)) {
1288 ++NumReturns;
1289 RI->replaceUsesOfWith(RetVal, C);
1290 BBChanged = true;
1291 }
1292 }
1293 }
1294
1295 FnChanged |= BBChanged;
1296 }
1297
1298 return FnChanged;
1299}
1300
1305
1306 bool Changed = runImpl(F, LVI, DT, getBestSimplifyQuery(AM, F));
1307
1309 if (!Changed) {
1311 } else {
1312#if defined(EXPENSIVE_CHECKS)
1313 assert(DT->verify(DominatorTree::VerificationLevel::Full));
1314#else
1315 assert(DT->verify(DominatorTree::VerificationLevel::Fast));
1316#endif // EXPENSIVE_CHECKS
1317
1320 }
1321
1322 // Keeping LVI alive is expensive, both because it uses a lot of memory, and
1323 // because invalidating values in LVI is expensive. While CVP does preserve
1324 // LVI, we know that passes after JumpThreading+CVP will not need the result
1325 // of this analysis, so we forcefully discard it early.
1327 return PA;
1328}
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI)
static bool processAnd(BinaryOperator *BinOp, LazyValueInfo *LVI)
static bool processOverflowIntrinsic(WithOverflowInst *WO, LazyValueInfo *LVI)
static bool processSDivOrSRem(BinaryOperator *Instr, LazyValueInfo *LVI)
static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR, const ConstantRange &YCR)
static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI)
See if LazyValueInfo's ability to exploit edge conditions or range information is sufficient to prove...
static bool processSaturatingInst(SaturatingInst *SI, LazyValueInfo *LVI)
static Value * getValueOnEdge(LazyValueInfo *LVI, Value *Incoming, BasicBlock *From, BasicBlock *To, Instruction *CxtI)
static bool narrowUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR, const ConstantRange &YCR)
Try to shrink a udiv/urem's width down to the smallest power of two that's sufficient to contain its ...
static bool willNotOverflow(BinaryOpIntrinsic *BO, LazyValueInfo *LVI)
static bool processSelect(SelectInst *S, LazyValueInfo *LVI)
static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT, const SimplifyQuery &SQ)
static void setDeducedOverflowingFlags(Value *V, Instruction::BinaryOps Opcode, bool NewNSW, bool NewNUW)
static bool processMinMaxIntrinsic(MinMaxIntrinsic *MM, LazyValueInfo *LVI)
static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI)
static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT)
Try to simplify a phi with constant incoming values that match the edge values of a non-constant valu...
static bool processSRem(BinaryOperator *SDI, const ConstantRange &LCR, const ConstantRange &RCR, LazyValueInfo *LVI)
static Domain getDomain(const ConstantRange &CR)
static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT, const SimplifyQuery &SQ)
static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI)
@ NonNegative
@ NonPositive
static bool processSDiv(BinaryOperator *SDI, const ConstantRange &LCR, const ConstantRange &RCR, LazyValueInfo *LVI)
See if LazyValueInfo's ability to exploit edge conditions or range information is sufficient to prove...
static bool processAShr(BinaryOperator *SDI, LazyValueInfo *LVI)
static bool processZExt(ZExtInst *ZExt, LazyValueInfo *LVI)
static bool narrowSDivOrSRem(BinaryOperator *Instr, const ConstantRange &LCR, const ConstantRange &RCR)
Try to shrink a sdiv/srem's width down to the smallest power of two that's sufficient to contain its ...
static bool processSIToFP(SIToFPInst *SIToFP, LazyValueInfo *LVI)
static bool processSExt(SExtInst *SDI, LazyValueInfo *LVI)
static bool processPossibleNonNeg(PossiblyNonNegInst *I, LazyValueInfo *LVI)
static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI, DominatorTree *DT)
Simplify a switch instruction by removing cases which can never fire.
static bool processUIToFP(UIToFPInst *UIToFP, LazyValueInfo *LVI)
static Constant * getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI)
static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI)
static bool processAbsIntrinsic(IntrinsicInst *II, LazyValueInfo *LVI)
static bool processCallSite(CallBase &CB, LazyValueInfo *LVI)
Infer nonnull attributes for the arguments at the specified callsite.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static bool runImpl(Function &F, const TargetLowering &TLI)
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
This header defines various interfaces for pass management in LLVM.
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
@ Struct
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:77
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:213
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:198
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
Definition: APInt.h:340
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1129
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:179
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo, Attribute::AttrKind Kind) const
Add an argument attribute to the list.
Definition: Attributes.h:606
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:94
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:202
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:209
const Instruction * getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...
Definition: BasicBlock.cpp:384
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:168
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition: BasicBlock.cpp:514
This class represents an intrinsic that is based on a binary operation.
Value * getRHS() const
unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
bool isSigned() const
Whether the intrinsic is signed or unsigned.
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Value * getLHS() const
static BinaryOperator * CreateNeg(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Helper functions to construct and inspect unary operations (NEG and NOT) via binary operators SUB and...
BinaryOps getOpcode() const
Definition: InstrTypes.h:442
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
std::optional< OperandBundleUse > getOperandBundle(StringRef Name) const
Return an operand bundle by name, if present.
Definition: InstrTypes.h:2143
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1546
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1401
unsigned arg_size() const
Definition: InstrTypes.h:1408
AttributeList getAttributes() const
Return the parameter attributes for this call.
Definition: InstrTypes.h:1542
static CastInst * CreateZExtOrBitCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt or BitCast cast instruction.
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:747
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1104
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:782
@ ICMP_EQ
equal
Definition: InstrTypes.h:778
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:783
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition: InstrTypes.h:953
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:850
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:857
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1762
This class represents a range of values.
Definition: ConstantRange.h:47
unsigned getActiveBits() const
Compute the maximal number of active bits needed to represent every value in this range.
ConstantRange umul_sat(const ConstantRange &Other) const
Perform an unsigned saturating multiplication of two constant ranges.
static CmpInst::Predicate getEquivalentPredWithFlippedSignedness(CmpInst::Predicate Pred, const ConstantRange &CR1, const ConstantRange &CR2)
If the comparison between constant ranges this and Other is insensitive to the signedness of the comp...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
bool isAllNegative() const
Return true if all values in this range are negative.
bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
bool isSizeLargerThan(uint64_t MaxSize) const
Compare set size of this range with Value.
ConstantRange abs(bool IntMinIsPoison=false) const
Calculate absolute value range.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
ConstantRange sdiv(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a signed division of a value in th...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
static bool areInsensitiveToSignednessOfICmpPredicate(const ConstantRange &CR1, const ConstantRange &CR2)
Return true iff CR1 ult CR2 is equivalent to CR1 slt CR2.
static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Produce the largest range containing all X such that "X BinOp Y" is guaranteed not to wrap (overflow)...
unsigned getMinSignedBits() const
Compute the maximal number of bits needed to represent every value in this signed range.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1357
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
This class represents an Operation in the Expression.
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
void applyUpdatesPermissive(ArrayRef< typename DomTreeT::UpdateType > Updates)
Submit updates to all available trees.
void applyUpdates(ArrayRef< typename DomTreeT::UpdateType > Updates)
Submit updates to all available trees.
This instruction compares its operands according to the predicate given to the constructor.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2664
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:476
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:473
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:55
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Analysis to compute lazy value information.
This pass computes, caches, and vends lazy value constraint information.
Definition: LazyValueInfo.h:33
ConstantRange getConstantRangeAtUse(const Use &U, bool UndefAllowed)
Return the ConstantRange constraint that is known to hold for the value at a specific use-site.
Tristate
This is used to return true/false/dunno results.
Definition: LazyValueInfo.h:63
Constant * getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value is known to be a constant on the specified edge.
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value comparison with a constant is known to be true or false on the ...
Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C, Instruction *CxtI, bool UseBlockValue)
Determine whether the specified value comparison with a constant is known to be true or false at the ...
Constant * getConstant(Value *V, Instruction *CxtI)
Determine whether the specified value is known to be a constant at the specified instruction.
This class represents min/max intrinsics.
Value * getLHS() const
Value * getRHS() const
static ICmpInst::Predicate getPredicate(Intrinsic::ID ID)
Returns the comparison predicate underlying the intrinsic.
static bool isSigned(Intrinsic::ID ID)
Whether the intrinsic is signed or unsigned.
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:77
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1814
Instruction that can have a nneg flag (zext/uitofp).
Definition: InstrTypes.h:722
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void abandon()
Mark an analysis as abandoned.
Definition: Analysis.h:164
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
This class represents a sign extension of integer types.
This class represents a cast from signed integer to floating point.
Represents a saturating add/sub intrinsic.
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
Class to represent struct types.
Definition: DerivedTypes.h:216
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
Multiway switch.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
This class represents a cast unsigned integer to floating point.
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
const Use & getOperandUse(unsigned i) const
Definition: User.h:182
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
bool use_empty() const
Definition: Value.h:344
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
iterator_range< use_iterator > uses()
Definition: Value.h:376
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
void takeName(Value *V)
Transfer the name from V to this value.
Definition: Value.cpp:383
Represents an op.with.overflow intrinsic.
This class represents zero extension of integer types.
self_iterator getIterator()
Definition: ilist_node.h:132
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Definition: Local.cpp:130
auto successors(const MachineBasicBlock *BB)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:372
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
iterator_range< df_iterator< T > > depth_first(const T &G)
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
const SimplifyQuery getBestSimplifyQuery(Pass &, Function &)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...