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