[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / lib / Transforms / Scalar / CorrelatedValuePropagation.cpp
blob66335ed6ce5cb11dbd5beca4b89e70009acddb8c
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
14 #include "llvm/ADT/DepthFirstIterator.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/DomTreeUpdater.h"
19 #include "llvm/Analysis/GlobalsModRef.h"
20 #include "llvm/Analysis/InstructionSimplify.h"
21 #include "llvm/Analysis/LazyValueInfo.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/CFG.h"
25 #include "llvm/IR/CallSite.h"
26 #include "llvm/IR/Constant.h"
27 #include "llvm/IR/ConstantRange.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/IRBuilder.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/IR/PassManager.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/Pass.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/Scalar.h"
46 #include "llvm/Transforms/Utils/Local.h"
47 #include <cassert>
48 #include <utility>
50 using namespace llvm;
52 #define DEBUG_TYPE "correlated-value-propagation"
54 STATISTIC(NumPhis, "Number of phis propagated");
55 STATISTIC(NumPhiCommon, "Number of phis deleted via common incoming value");
56 STATISTIC(NumSelects, "Number of selects propagated");
57 STATISTIC(NumMemAccess, "Number of memory access targets propagated");
58 STATISTIC(NumCmps, "Number of comparisons propagated");
59 STATISTIC(NumReturns, "Number of return values propagated");
60 STATISTIC(NumDeadCases, "Number of switch cases removed");
61 STATISTIC(NumSDivs, "Number of sdiv converted to udiv");
62 STATISTIC(NumUDivs, "Number of udivs whose width was decreased");
63 STATISTIC(NumAShrs, "Number of ashr converted to lshr");
64 STATISTIC(NumSRems, "Number of srem converted to urem");
65 STATISTIC(NumOverflows, "Number of overflow checks removed");
66 STATISTIC(NumSaturating,
67 "Number of saturating arithmetics converted to normal arithmetics");
69 static cl::opt<bool> DontAddNoWrapFlags("cvp-dont-add-nowrap-flags", cl::init(false));
71 namespace {
73 class CorrelatedValuePropagation : public FunctionPass {
74 public:
75 static char ID;
77 CorrelatedValuePropagation(): FunctionPass(ID) {
78 initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
81 bool runOnFunction(Function &F) override;
83 void getAnalysisUsage(AnalysisUsage &AU) const override {
84 AU.addRequired<DominatorTreeWrapperPass>();
85 AU.addRequired<LazyValueInfoWrapperPass>();
86 AU.addPreserved<GlobalsAAWrapperPass>();
87 AU.addPreserved<DominatorTreeWrapperPass>();
88 AU.addPreserved<LazyValueInfoWrapperPass>();
92 } // end anonymous namespace
94 char CorrelatedValuePropagation::ID = 0;
96 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
97 "Value Propagation", false, false)
98 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
99 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
100 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
101 "Value Propagation", false, false)
103 // Public interface to the Value Propagation pass
104 Pass *llvm::createCorrelatedValuePropagationPass() {
105 return new CorrelatedValuePropagation();
108 static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
109 if (S->getType()->isVectorTy()) return false;
110 if (isa<Constant>(S->getOperand(0))) return false;
112 Constant *C = LVI->getConstant(S->getCondition(), S->getParent(), S);
113 if (!C) return false;
115 ConstantInt *CI = dyn_cast<ConstantInt>(C);
116 if (!CI) return false;
118 Value *ReplaceWith = S->getTrueValue();
119 Value *Other = S->getFalseValue();
120 if (!CI->isOne()) std::swap(ReplaceWith, Other);
121 if (ReplaceWith == S) ReplaceWith = UndefValue::get(S->getType());
123 S->replaceAllUsesWith(ReplaceWith);
124 S->eraseFromParent();
126 ++NumSelects;
128 return true;
131 /// Try to simplify a phi with constant incoming values that match the edge
132 /// values of a non-constant value on all other edges:
133 /// bb0:
134 /// %isnull = icmp eq i8* %x, null
135 /// br i1 %isnull, label %bb2, label %bb1
136 /// bb1:
137 /// br label %bb2
138 /// bb2:
139 /// %r = phi i8* [ %x, %bb1 ], [ null, %bb0 ]
140 /// -->
141 /// %r = %x
142 static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI,
143 DominatorTree *DT) {
144 // Collect incoming constants and initialize possible common value.
145 SmallVector<std::pair<Constant *, unsigned>, 4> IncomingConstants;
146 Value *CommonValue = nullptr;
147 for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
148 Value *Incoming = P->getIncomingValue(i);
149 if (auto *IncomingConstant = dyn_cast<Constant>(Incoming)) {
150 IncomingConstants.push_back(std::make_pair(IncomingConstant, i));
151 } else if (!CommonValue) {
152 // The potential common value is initialized to the first non-constant.
153 CommonValue = Incoming;
154 } else if (Incoming != CommonValue) {
155 // There can be only one non-constant common value.
156 return false;
160 if (!CommonValue || IncomingConstants.empty())
161 return false;
163 // The common value must be valid in all incoming blocks.
164 BasicBlock *ToBB = P->getParent();
165 if (auto *CommonInst = dyn_cast<Instruction>(CommonValue))
166 if (!DT->dominates(CommonInst, ToBB))
167 return false;
169 // We have a phi with exactly 1 variable incoming value and 1 or more constant
170 // incoming values. See if all constant incoming values can be mapped back to
171 // the same incoming variable value.
172 for (auto &IncomingConstant : IncomingConstants) {
173 Constant *C = IncomingConstant.first;
174 BasicBlock *IncomingBB = P->getIncomingBlock(IncomingConstant.second);
175 if (C != LVI->getConstantOnEdge(CommonValue, IncomingBB, ToBB, P))
176 return false;
179 // All constant incoming values map to the same variable along the incoming
180 // edges of the phi. The phi is unnecessary.
181 P->replaceAllUsesWith(CommonValue);
182 P->eraseFromParent();
183 ++NumPhiCommon;
184 return true;
187 static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT,
188 const SimplifyQuery &SQ) {
189 bool Changed = false;
191 BasicBlock *BB = P->getParent();
192 for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
193 Value *Incoming = P->getIncomingValue(i);
194 if (isa<Constant>(Incoming)) continue;
196 Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);
198 // Look if the incoming value is a select with a scalar condition for which
199 // LVI can tells us the value. In that case replace the incoming value with
200 // the appropriate value of the select. This often allows us to remove the
201 // select later.
202 if (!V) {
203 SelectInst *SI = dyn_cast<SelectInst>(Incoming);
204 if (!SI) continue;
206 Value *Condition = SI->getCondition();
207 if (!Condition->getType()->isVectorTy()) {
208 if (Constant *C = LVI->getConstantOnEdge(
209 Condition, P->getIncomingBlock(i), BB, P)) {
210 if (C->isOneValue()) {
211 V = SI->getTrueValue();
212 } else if (C->isZeroValue()) {
213 V = SI->getFalseValue();
215 // Once LVI learns to handle vector types, we could also add support
216 // for vector type constants that are not all zeroes or all ones.
220 // Look if the select has a constant but LVI tells us that the incoming
221 // value can never be that constant. In that case replace the incoming
222 // value with the other value of the select. This often allows us to
223 // remove the select later.
224 if (!V) {
225 Constant *C = dyn_cast<Constant>(SI->getFalseValue());
226 if (!C) continue;
228 if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
229 P->getIncomingBlock(i), BB, P) !=
230 LazyValueInfo::False)
231 continue;
232 V = SI->getTrueValue();
235 LLVM_DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n');
238 P->setIncomingValue(i, V);
239 Changed = true;
242 if (Value *V = SimplifyInstruction(P, SQ)) {
243 P->replaceAllUsesWith(V);
244 P->eraseFromParent();
245 Changed = true;
248 if (!Changed)
249 Changed = simplifyCommonValuePhi(P, LVI, DT);
251 if (Changed)
252 ++NumPhis;
254 return Changed;
257 static bool processMemAccess(Instruction *I, LazyValueInfo *LVI) {
258 Value *Pointer = nullptr;
259 if (LoadInst *L = dyn_cast<LoadInst>(I))
260 Pointer = L->getPointerOperand();
261 else
262 Pointer = cast<StoreInst>(I)->getPointerOperand();
264 if (isa<Constant>(Pointer)) return false;
266 Constant *C = LVI->getConstant(Pointer, I->getParent(), I);
267 if (!C) return false;
269 ++NumMemAccess;
270 I->replaceUsesOfWith(Pointer, C);
271 return true;
274 /// See if LazyValueInfo's ability to exploit edge conditions or range
275 /// information is sufficient to prove this comparison. Even for local
276 /// conditions, this can sometimes prove conditions instcombine can't by
277 /// exploiting range information.
278 static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
279 Value *Op0 = Cmp->getOperand(0);
280 auto *C = dyn_cast<Constant>(Cmp->getOperand(1));
281 if (!C)
282 return false;
284 // As a policy choice, we choose not to waste compile time on anything where
285 // the comparison is testing local values. While LVI can sometimes reason
286 // about such cases, it's not its primary purpose. We do make sure to do
287 // the block local query for uses from terminator instructions, but that's
288 // handled in the code for each terminator.
289 auto *I = dyn_cast<Instruction>(Op0);
290 if (I && I->getParent() == Cmp->getParent())
291 return false;
293 LazyValueInfo::Tristate Result =
294 LVI->getPredicateAt(Cmp->getPredicate(), Op0, C, Cmp);
295 if (Result == LazyValueInfo::Unknown)
296 return false;
298 ++NumCmps;
299 Constant *TorF = ConstantInt::get(Type::getInt1Ty(Cmp->getContext()), Result);
300 Cmp->replaceAllUsesWith(TorF);
301 Cmp->eraseFromParent();
302 return true;
305 /// Simplify a switch instruction by removing cases which can never fire. If the
306 /// uselessness of a case could be determined locally then constant propagation
307 /// would already have figured it out. Instead, walk the predecessors and
308 /// statically evaluate cases based on information available on that edge. Cases
309 /// that cannot fire no matter what the incoming edge can safely be removed. If
310 /// a case fires on every incoming edge then the entire switch can be removed
311 /// and replaced with a branch to the case destination.
312 static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
313 DominatorTree *DT) {
314 DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
315 Value *Cond = I->getCondition();
316 BasicBlock *BB = I->getParent();
318 // If the condition was defined in same block as the switch then LazyValueInfo
319 // currently won't say anything useful about it, though in theory it could.
320 if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->getParent() == BB)
321 return false;
323 // If the switch is unreachable then trying to improve it is a waste of time.
324 pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
325 if (PB == PE) return false;
327 // Analyse each switch case in turn.
328 bool Changed = false;
329 DenseMap<BasicBlock*, int> SuccessorsCount;
330 for (auto *Succ : successors(BB))
331 SuccessorsCount[Succ]++;
333 { // Scope for SwitchInstProfUpdateWrapper. It must not live during
334 // ConstantFoldTerminator() as the underlying SwitchInst can be changed.
335 SwitchInstProfUpdateWrapper SI(*I);
337 for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
338 ConstantInt *Case = CI->getCaseValue();
340 // Check to see if the switch condition is equal to/not equal to the case
341 // value on every incoming edge, equal/not equal being the same each time.
342 LazyValueInfo::Tristate State = LazyValueInfo::Unknown;
343 for (pred_iterator PI = PB; PI != PE; ++PI) {
344 // Is the switch condition equal to the case value?
345 LazyValueInfo::Tristate Value = LVI->getPredicateOnEdge(CmpInst::ICMP_EQ,
346 Cond, Case, *PI,
347 BB, SI);
348 // Give up on this case if nothing is known.
349 if (Value == LazyValueInfo::Unknown) {
350 State = LazyValueInfo::Unknown;
351 break;
354 // If this was the first edge to be visited, record that all other edges
355 // need to give the same result.
356 if (PI == PB) {
357 State = Value;
358 continue;
361 // If this case is known to fire for some edges and known not to fire for
362 // others then there is nothing we can do - give up.
363 if (Value != State) {
364 State = LazyValueInfo::Unknown;
365 break;
369 if (State == LazyValueInfo::False) {
370 // This case never fires - remove it.
371 BasicBlock *Succ = CI->getCaseSuccessor();
372 Succ->removePredecessor(BB);
373 CI = SI.removeCase(CI);
374 CE = SI->case_end();
376 // The condition can be modified by removePredecessor's PHI simplification
377 // logic.
378 Cond = SI->getCondition();
380 ++NumDeadCases;
381 Changed = true;
382 if (--SuccessorsCount[Succ] == 0)
383 DTU.applyUpdatesPermissive({{DominatorTree::Delete, BB, Succ}});
384 continue;
386 if (State == LazyValueInfo::True) {
387 // This case always fires. Arrange for the switch to be turned into an
388 // unconditional branch by replacing the switch condition with the case
389 // value.
390 SI->setCondition(Case);
391 NumDeadCases += SI->getNumCases();
392 Changed = true;
393 break;
396 // Increment the case iterator since we didn't delete it.
397 ++CI;
401 if (Changed)
402 // If the switch has been simplified to the point where it can be replaced
403 // by a branch then do so now.
404 ConstantFoldTerminator(BB, /*DeleteDeadConditions = */ false,
405 /*TLI = */ nullptr, &DTU);
406 return Changed;
409 // See if we can prove that the given binary op intrinsic will not overflow.
410 static bool willNotOverflow(BinaryOpIntrinsic *BO, LazyValueInfo *LVI) {
411 ConstantRange LRange = LVI->getConstantRange(
412 BO->getLHS(), BO->getParent(), BO);
413 ConstantRange RRange = LVI->getConstantRange(
414 BO->getRHS(), BO->getParent(), BO);
415 ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
416 BO->getBinaryOp(), RRange, BO->getNoWrapKind());
417 return NWRegion.contains(LRange);
420 // Rewrite this with.overflow intrinsic as non-overflowing.
421 static void processOverflowIntrinsic(WithOverflowInst *WO) {
422 IRBuilder<> B(WO);
423 Value *NewOp = B.CreateBinOp(
424 WO->getBinaryOp(), WO->getLHS(), WO->getRHS(), WO->getName());
425 // Constant-folding could have happened.
426 if (auto *Inst = dyn_cast<Instruction>(NewOp)) {
427 if (WO->isSigned())
428 Inst->setHasNoSignedWrap();
429 else
430 Inst->setHasNoUnsignedWrap();
433 StructType *ST = cast<StructType>(WO->getType());
434 Constant *Struct = ConstantStruct::get(ST,
435 { UndefValue::get(ST->getElementType(0)),
436 ConstantInt::getFalse(ST->getElementType(1)) });
437 Value *NewI = B.CreateInsertValue(Struct, NewOp, 0);
438 WO->replaceAllUsesWith(NewI);
439 WO->eraseFromParent();
440 ++NumOverflows;
443 static void processSaturatingInst(SaturatingInst *SI) {
444 BinaryOperator *BinOp = BinaryOperator::Create(
445 SI->getBinaryOp(), SI->getLHS(), SI->getRHS(), SI->getName(), SI);
446 BinOp->setDebugLoc(SI->getDebugLoc());
447 if (SI->isSigned())
448 BinOp->setHasNoSignedWrap();
449 else
450 BinOp->setHasNoUnsignedWrap();
452 SI->replaceAllUsesWith(BinOp);
453 SI->eraseFromParent();
454 ++NumSaturating;
457 /// Infer nonnull attributes for the arguments at the specified callsite.
458 static bool processCallSite(CallSite CS, LazyValueInfo *LVI) {
459 SmallVector<unsigned, 4> ArgNos;
460 unsigned ArgNo = 0;
462 if (auto *WO = dyn_cast<WithOverflowInst>(CS.getInstruction())) {
463 if (WO->getLHS()->getType()->isIntegerTy() && willNotOverflow(WO, LVI)) {
464 processOverflowIntrinsic(WO);
465 return true;
469 if (auto *SI = dyn_cast<SaturatingInst>(CS.getInstruction())) {
470 if (SI->getType()->isIntegerTy() && willNotOverflow(SI, LVI)) {
471 processSaturatingInst(SI);
472 return true;
476 // Deopt bundle operands are intended to capture state with minimal
477 // perturbance of the code otherwise. If we can find a constant value for
478 // any such operand and remove a use of the original value, that's
479 // desireable since it may allow further optimization of that value (e.g. via
480 // single use rules in instcombine). Since deopt uses tend to,
481 // idiomatically, appear along rare conditional paths, it's reasonable likely
482 // we may have a conditional fact with which LVI can fold.
483 if (auto DeoptBundle = CS.getOperandBundle(LLVMContext::OB_deopt)) {
484 bool Progress = false;
485 for (const Use &ConstU : DeoptBundle->Inputs) {
486 Use &U = const_cast<Use&>(ConstU);
487 Value *V = U.get();
488 if (V->getType()->isVectorTy()) continue;
489 if (isa<Constant>(V)) continue;
491 Constant *C = LVI->getConstant(V, CS.getParent(), CS.getInstruction());
492 if (!C) continue;
493 U.set(C);
494 Progress = true;
496 if (Progress)
497 return true;
500 for (Value *V : CS.args()) {
501 PointerType *Type = dyn_cast<PointerType>(V->getType());
502 // Try to mark pointer typed parameters as non-null. We skip the
503 // relatively expensive analysis for constants which are obviously either
504 // null or non-null to start with.
505 if (Type && !CS.paramHasAttr(ArgNo, Attribute::NonNull) &&
506 !isa<Constant>(V) &&
507 LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
508 ConstantPointerNull::get(Type),
509 CS.getInstruction()) == LazyValueInfo::False)
510 ArgNos.push_back(ArgNo);
511 ArgNo++;
514 assert(ArgNo == CS.arg_size() && "sanity check");
516 if (ArgNos.empty())
517 return false;
519 AttributeList AS = CS.getAttributes();
520 LLVMContext &Ctx = CS.getInstruction()->getContext();
521 AS = AS.addParamAttribute(Ctx, ArgNos,
522 Attribute::get(Ctx, Attribute::NonNull));
523 CS.setAttributes(AS);
525 return true;
528 static bool hasPositiveOperands(BinaryOperator *SDI, LazyValueInfo *LVI) {
529 Constant *Zero = ConstantInt::get(SDI->getType(), 0);
530 for (Value *O : SDI->operands()) {
531 auto Result = LVI->getPredicateAt(ICmpInst::ICMP_SGE, O, Zero, SDI);
532 if (Result != LazyValueInfo::True)
533 return false;
535 return true;
538 /// Try to shrink a udiv/urem's width down to the smallest power of two that's
539 /// sufficient to contain its operands.
540 static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
541 assert(Instr->getOpcode() == Instruction::UDiv ||
542 Instr->getOpcode() == Instruction::URem);
543 if (Instr->getType()->isVectorTy())
544 return false;
546 // Find the smallest power of two bitwidth that's sufficient to hold Instr's
547 // operands.
548 auto OrigWidth = Instr->getType()->getIntegerBitWidth();
549 ConstantRange OperandRange(OrigWidth, /*isFullSet=*/false);
550 for (Value *Operand : Instr->operands()) {
551 OperandRange = OperandRange.unionWith(
552 LVI->getConstantRange(Operand, Instr->getParent()));
554 // Don't shrink below 8 bits wide.
555 unsigned NewWidth = std::max<unsigned>(
556 PowerOf2Ceil(OperandRange.getUnsignedMax().getActiveBits()), 8);
557 // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
558 // two.
559 if (NewWidth >= OrigWidth)
560 return false;
562 ++NumUDivs;
563 IRBuilder<> B{Instr};
564 auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
565 auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
566 Instr->getName() + ".lhs.trunc");
567 auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
568 Instr->getName() + ".rhs.trunc");
569 auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
570 auto *Zext = B.CreateZExt(BO, Instr->getType(), Instr->getName() + ".zext");
571 if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
572 if (BinOp->getOpcode() == Instruction::UDiv)
573 BinOp->setIsExact(Instr->isExact());
575 Instr->replaceAllUsesWith(Zext);
576 Instr->eraseFromParent();
577 return true;
580 static bool processSRem(BinaryOperator *SDI, LazyValueInfo *LVI) {
581 if (SDI->getType()->isVectorTy() || !hasPositiveOperands(SDI, LVI))
582 return false;
584 ++NumSRems;
585 auto *BO = BinaryOperator::CreateURem(SDI->getOperand(0), SDI->getOperand(1),
586 SDI->getName(), SDI);
587 BO->setDebugLoc(SDI->getDebugLoc());
588 SDI->replaceAllUsesWith(BO);
589 SDI->eraseFromParent();
591 // Try to process our new urem.
592 processUDivOrURem(BO, LVI);
594 return true;
597 /// See if LazyValueInfo's ability to exploit edge conditions or range
598 /// information is sufficient to prove the both operands of this SDiv are
599 /// positive. If this is the case, replace the SDiv with a UDiv. Even for local
600 /// conditions, this can sometimes prove conditions instcombine can't by
601 /// exploiting range information.
602 static bool processSDiv(BinaryOperator *SDI, LazyValueInfo *LVI) {
603 if (SDI->getType()->isVectorTy() || !hasPositiveOperands(SDI, LVI))
604 return false;
606 ++NumSDivs;
607 auto *BO = BinaryOperator::CreateUDiv(SDI->getOperand(0), SDI->getOperand(1),
608 SDI->getName(), SDI);
609 BO->setDebugLoc(SDI->getDebugLoc());
610 BO->setIsExact(SDI->isExact());
611 SDI->replaceAllUsesWith(BO);
612 SDI->eraseFromParent();
614 // Try to simplify our new udiv.
615 processUDivOrURem(BO, LVI);
617 return true;
620 static bool processAShr(BinaryOperator *SDI, LazyValueInfo *LVI) {
621 if (SDI->getType()->isVectorTy())
622 return false;
624 Constant *Zero = ConstantInt::get(SDI->getType(), 0);
625 if (LVI->getPredicateAt(ICmpInst::ICMP_SGE, SDI->getOperand(0), Zero, SDI) !=
626 LazyValueInfo::True)
627 return false;
629 ++NumAShrs;
630 auto *BO = BinaryOperator::CreateLShr(SDI->getOperand(0), SDI->getOperand(1),
631 SDI->getName(), SDI);
632 BO->setDebugLoc(SDI->getDebugLoc());
633 BO->setIsExact(SDI->isExact());
634 SDI->replaceAllUsesWith(BO);
635 SDI->eraseFromParent();
637 return true;
640 static bool processBinOp(BinaryOperator *BinOp, LazyValueInfo *LVI) {
641 using OBO = OverflowingBinaryOperator;
643 if (DontAddNoWrapFlags)
644 return false;
646 if (BinOp->getType()->isVectorTy())
647 return false;
649 bool NSW = BinOp->hasNoSignedWrap();
650 bool NUW = BinOp->hasNoUnsignedWrap();
651 if (NSW && NUW)
652 return false;
654 BasicBlock *BB = BinOp->getParent();
656 Value *LHS = BinOp->getOperand(0);
657 Value *RHS = BinOp->getOperand(1);
659 ConstantRange LRange = LVI->getConstantRange(LHS, BB, BinOp);
660 ConstantRange RRange = LVI->getConstantRange(RHS, BB, BinOp);
662 bool Changed = false;
663 if (!NUW) {
664 ConstantRange NUWRange = ConstantRange::makeGuaranteedNoWrapRegion(
665 BinOp->getOpcode(), RRange, OBO::NoUnsignedWrap);
666 bool NewNUW = NUWRange.contains(LRange);
667 BinOp->setHasNoUnsignedWrap(NewNUW);
668 Changed |= NewNUW;
670 if (!NSW) {
671 ConstantRange NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(
672 BinOp->getOpcode(), RRange, OBO::NoSignedWrap);
673 bool NewNSW = NSWRange.contains(LRange);
674 BinOp->setHasNoSignedWrap(NewNSW);
675 Changed |= NewNSW;
678 return Changed;
681 static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
682 if (Constant *C = LVI->getConstant(V, At->getParent(), At))
683 return C;
685 // TODO: The following really should be sunk inside LVI's core algorithm, or
686 // at least the outer shims around such.
687 auto *C = dyn_cast<CmpInst>(V);
688 if (!C) return nullptr;
690 Value *Op0 = C->getOperand(0);
691 Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
692 if (!Op1) return nullptr;
694 LazyValueInfo::Tristate Result =
695 LVI->getPredicateAt(C->getPredicate(), Op0, Op1, At);
696 if (Result == LazyValueInfo::Unknown)
697 return nullptr;
699 return (Result == LazyValueInfo::True) ?
700 ConstantInt::getTrue(C->getContext()) :
701 ConstantInt::getFalse(C->getContext());
704 static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT,
705 const SimplifyQuery &SQ) {
706 bool FnChanged = false;
707 // Visiting in a pre-order depth-first traversal causes us to simplify early
708 // blocks before querying later blocks (which require us to analyze early
709 // blocks). Eagerly simplifying shallow blocks means there is strictly less
710 // work to do for deep blocks. This also means we don't visit unreachable
711 // blocks.
712 for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
713 bool BBChanged = false;
714 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
715 Instruction *II = &*BI++;
716 switch (II->getOpcode()) {
717 case Instruction::Select:
718 BBChanged |= processSelect(cast<SelectInst>(II), LVI);
719 break;
720 case Instruction::PHI:
721 BBChanged |= processPHI(cast<PHINode>(II), LVI, DT, SQ);
722 break;
723 case Instruction::ICmp:
724 case Instruction::FCmp:
725 BBChanged |= processCmp(cast<CmpInst>(II), LVI);
726 break;
727 case Instruction::Load:
728 case Instruction::Store:
729 BBChanged |= processMemAccess(II, LVI);
730 break;
731 case Instruction::Call:
732 case Instruction::Invoke:
733 BBChanged |= processCallSite(CallSite(II), LVI);
734 break;
735 case Instruction::SRem:
736 BBChanged |= processSRem(cast<BinaryOperator>(II), LVI);
737 break;
738 case Instruction::SDiv:
739 BBChanged |= processSDiv(cast<BinaryOperator>(II), LVI);
740 break;
741 case Instruction::UDiv:
742 case Instruction::URem:
743 BBChanged |= processUDivOrURem(cast<BinaryOperator>(II), LVI);
744 break;
745 case Instruction::AShr:
746 BBChanged |= processAShr(cast<BinaryOperator>(II), LVI);
747 break;
748 case Instruction::Add:
749 case Instruction::Sub:
750 BBChanged |= processBinOp(cast<BinaryOperator>(II), LVI);
751 break;
755 Instruction *Term = BB->getTerminator();
756 switch (Term->getOpcode()) {
757 case Instruction::Switch:
758 BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI, DT);
759 break;
760 case Instruction::Ret: {
761 auto *RI = cast<ReturnInst>(Term);
762 // Try to determine the return value if we can. This is mainly here to
763 // simplify the writing of unit tests, but also helps to enable IPO by
764 // constant folding the return values of callees.
765 auto *RetVal = RI->getReturnValue();
766 if (!RetVal) break; // handle "ret void"
767 if (isa<Constant>(RetVal)) break; // nothing to do
768 if (auto *C = getConstantAt(RetVal, RI, LVI)) {
769 ++NumReturns;
770 RI->replaceUsesOfWith(RetVal, C);
771 BBChanged = true;
776 FnChanged |= BBChanged;
779 return FnChanged;
782 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
783 if (skipFunction(F))
784 return false;
786 LazyValueInfo *LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
787 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
789 return runImpl(F, LVI, DT, getBestSimplifyQuery(*this, F));
792 PreservedAnalyses
793 CorrelatedValuePropagationPass::run(Function &F, FunctionAnalysisManager &AM) {
794 LazyValueInfo *LVI = &AM.getResult<LazyValueAnalysis>(F);
795 DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
797 bool Changed = runImpl(F, LVI, DT, getBestSimplifyQuery(AM, F));
799 if (!Changed)
800 return PreservedAnalyses::all();
801 PreservedAnalyses PA;
802 PA.preserve<GlobalsAA>();
803 PA.preserve<DominatorTreeAnalysis>();
804 PA.preserve<LazyValueAnalysis>();
805 return PA;