]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Scalar / CorrelatedValuePropagation.cpp
1 //===- CorrelatedValuePropagation.cpp - Propagate CFG-derived info --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Correlated Value Propagation pass.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
15 #include "llvm/ADT/DepthFirstIterator.h"
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.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/DomTreeUpdater.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/IRBuilder.h"
33 #include "llvm/IR/InstrTypes.h"
34 #include "llvm/IR/Instruction.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/Operator.h"
38 #include "llvm/IR/PassManager.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/IR/Value.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CommandLine.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Transforms/Scalar.h"
47 #include "llvm/Transforms/Utils/Local.h"
48 #include <cassert>
49 #include <utility>
50
51 using namespace llvm;
52
53 #define DEBUG_TYPE "correlated-value-propagation"
54
55 STATISTIC(NumPhis,      "Number of phis propagated");
56 STATISTIC(NumPhiCommon, "Number of phis deleted via common incoming value");
57 STATISTIC(NumSelects,   "Number of selects propagated");
58 STATISTIC(NumMemAccess, "Number of memory access targets propagated");
59 STATISTIC(NumCmps,      "Number of comparisons propagated");
60 STATISTIC(NumReturns,   "Number of return values propagated");
61 STATISTIC(NumDeadCases, "Number of switch cases removed");
62 STATISTIC(NumSDivs,     "Number of sdiv converted to udiv");
63 STATISTIC(NumUDivs,     "Number of udivs whose width was decreased");
64 STATISTIC(NumAShrs,     "Number of ashr converted to lshr");
65 STATISTIC(NumSRems,     "Number of srem converted to urem");
66 STATISTIC(NumOverflows, "Number of overflow checks removed");
67
68 static cl::opt<bool> DontProcessAdds("cvp-dont-process-adds", cl::init(true));
69
70 namespace {
71
72   class CorrelatedValuePropagation : public FunctionPass {
73   public:
74     static char ID;
75
76     CorrelatedValuePropagation(): FunctionPass(ID) {
77      initializeCorrelatedValuePropagationPass(*PassRegistry::getPassRegistry());
78     }
79
80     bool runOnFunction(Function &F) override;
81
82     void getAnalysisUsage(AnalysisUsage &AU) const override {
83       AU.addRequired<DominatorTreeWrapperPass>();
84       AU.addRequired<LazyValueInfoWrapperPass>();
85       AU.addPreserved<GlobalsAAWrapperPass>();
86       AU.addPreserved<DominatorTreeWrapperPass>();
87     }
88   };
89
90 } // end anonymous namespace
91
92 char CorrelatedValuePropagation::ID = 0;
93
94 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
95                 "Value Propagation", false, false)
96 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
97 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
98 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
99                 "Value Propagation", false, false)
100
101 // Public interface to the Value Propagation pass
102 Pass *llvm::createCorrelatedValuePropagationPass() {
103   return new CorrelatedValuePropagation();
104 }
105
106 static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
107   if (S->getType()->isVectorTy()) return false;
108   if (isa<Constant>(S->getOperand(0))) return false;
109
110   Constant *C = LVI->getConstant(S->getCondition(), S->getParent(), S);
111   if (!C) return false;
112
113   ConstantInt *CI = dyn_cast<ConstantInt>(C);
114   if (!CI) return false;
115
116   Value *ReplaceWith = S->getTrueValue();
117   Value *Other = S->getFalseValue();
118   if (!CI->isOne()) std::swap(ReplaceWith, Other);
119   if (ReplaceWith == S) ReplaceWith = UndefValue::get(S->getType());
120
121   S->replaceAllUsesWith(ReplaceWith);
122   S->eraseFromParent();
123
124   ++NumSelects;
125
126   return true;
127 }
128
129 /// Try to simplify a phi with constant incoming values that match the edge
130 /// values of a non-constant value on all other edges:
131 /// bb0:
132 ///   %isnull = icmp eq i8* %x, null
133 ///   br i1 %isnull, label %bb2, label %bb1
134 /// bb1:
135 ///   br label %bb2
136 /// bb2:
137 ///   %r = phi i8* [ %x, %bb1 ], [ null, %bb0 ]
138 /// -->
139 ///   %r = %x
140 static bool simplifyCommonValuePhi(PHINode *P, LazyValueInfo *LVI,
141                                    DominatorTree *DT) {
142   // Collect incoming constants and initialize possible common value.
143   SmallVector<std::pair<Constant *, unsigned>, 4> IncomingConstants;
144   Value *CommonValue = nullptr;
145   for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) {
146     Value *Incoming = P->getIncomingValue(i);
147     if (auto *IncomingConstant = dyn_cast<Constant>(Incoming)) {
148       IncomingConstants.push_back(std::make_pair(IncomingConstant, i));
149     } else if (!CommonValue) {
150       // The potential common value is initialized to the first non-constant.
151       CommonValue = Incoming;
152     } else if (Incoming != CommonValue) {
153       // There can be only one non-constant common value.
154       return false;
155     }
156   }
157
158   if (!CommonValue || IncomingConstants.empty())
159     return false;
160
161   // The common value must be valid in all incoming blocks.
162   BasicBlock *ToBB = P->getParent();
163   if (auto *CommonInst = dyn_cast<Instruction>(CommonValue))
164     if (!DT->dominates(CommonInst, ToBB))
165       return false;
166
167   // We have a phi with exactly 1 variable incoming value and 1 or more constant
168   // incoming values. See if all constant incoming values can be mapped back to
169   // the same incoming variable value.
170   for (auto &IncomingConstant : IncomingConstants) {
171     Constant *C = IncomingConstant.first;
172     BasicBlock *IncomingBB = P->getIncomingBlock(IncomingConstant.second);
173     if (C != LVI->getConstantOnEdge(CommonValue, IncomingBB, ToBB, P))
174       return false;
175   }
176
177   // All constant incoming values map to the same variable along the incoming
178   // edges of the phi. The phi is unnecessary.
179   P->replaceAllUsesWith(CommonValue);
180   P->eraseFromParent();
181   ++NumPhiCommon;
182   return true;
183 }
184
185 static bool processPHI(PHINode *P, LazyValueInfo *LVI, DominatorTree *DT,
186                        const SimplifyQuery &SQ) {
187   bool Changed = false;
188
189   BasicBlock *BB = P->getParent();
190   for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
191     Value *Incoming = P->getIncomingValue(i);
192     if (isa<Constant>(Incoming)) continue;
193
194     Value *V = LVI->getConstantOnEdge(Incoming, P->getIncomingBlock(i), BB, P);
195
196     // Look if the incoming value is a select with a scalar condition for which
197     // LVI can tells us the value. In that case replace the incoming value with
198     // the appropriate value of the select. This often allows us to remove the
199     // select later.
200     if (!V) {
201       SelectInst *SI = dyn_cast<SelectInst>(Incoming);
202       if (!SI) continue;
203
204       Value *Condition = SI->getCondition();
205       if (!Condition->getType()->isVectorTy()) {
206         if (Constant *C = LVI->getConstantOnEdge(
207                 Condition, P->getIncomingBlock(i), BB, P)) {
208           if (C->isOneValue()) {
209             V = SI->getTrueValue();
210           } else if (C->isZeroValue()) {
211             V = SI->getFalseValue();
212           }
213           // Once LVI learns to handle vector types, we could also add support
214           // for vector type constants that are not all zeroes or all ones.
215         }
216       }
217
218       // Look if the select has a constant but LVI tells us that the incoming
219       // value can never be that constant. In that case replace the incoming
220       // value with the other value of the select. This often allows us to
221       // remove the select later.
222       if (!V) {
223         Constant *C = dyn_cast<Constant>(SI->getFalseValue());
224         if (!C) continue;
225
226         if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C,
227               P->getIncomingBlock(i), BB, P) !=
228             LazyValueInfo::False)
229           continue;
230         V = SI->getTrueValue();
231       }
232
233       LLVM_DEBUG(dbgs() << "CVP: Threading PHI over " << *SI << '\n');
234     }
235
236     P->setIncomingValue(i, V);
237     Changed = true;
238   }
239
240   if (Value *V = SimplifyInstruction(P, SQ)) {
241     P->replaceAllUsesWith(V);
242     P->eraseFromParent();
243     Changed = true;
244   }
245
246   if (!Changed)
247     Changed = simplifyCommonValuePhi(P, LVI, DT);
248
249   if (Changed)
250     ++NumPhis;
251
252   return Changed;
253 }
254
255 static bool processMemAccess(Instruction *I, LazyValueInfo *LVI) {
256   Value *Pointer = nullptr;
257   if (LoadInst *L = dyn_cast<LoadInst>(I))
258     Pointer = L->getPointerOperand();
259   else
260     Pointer = cast<StoreInst>(I)->getPointerOperand();
261
262   if (isa<Constant>(Pointer)) return false;
263
264   Constant *C = LVI->getConstant(Pointer, I->getParent(), I);
265   if (!C) return false;
266
267   ++NumMemAccess;
268   I->replaceUsesOfWith(Pointer, C);
269   return true;
270 }
271
272 /// See if LazyValueInfo's ability to exploit edge conditions or range
273 /// information is sufficient to prove this comparison. Even for local
274 /// conditions, this can sometimes prove conditions instcombine can't by
275 /// exploiting range information.
276 static bool processCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
277   Value *Op0 = Cmp->getOperand(0);
278   auto *C = dyn_cast<Constant>(Cmp->getOperand(1));
279   if (!C)
280     return false;
281
282   // As a policy choice, we choose not to waste compile time on anything where
283   // the comparison is testing local values.  While LVI can sometimes reason
284   // about such cases, it's not its primary purpose.  We do make sure to do
285   // the block local query for uses from terminator instructions, but that's
286   // handled in the code for each terminator.
287   auto *I = dyn_cast<Instruction>(Op0);
288   if (I && I->getParent() == Cmp->getParent())
289     return false;
290
291   LazyValueInfo::Tristate Result =
292       LVI->getPredicateAt(Cmp->getPredicate(), Op0, C, Cmp);
293   if (Result == LazyValueInfo::Unknown)
294     return false;
295
296   ++NumCmps;
297   Constant *TorF = ConstantInt::get(Type::getInt1Ty(Cmp->getContext()), Result);
298   Cmp->replaceAllUsesWith(TorF);
299   Cmp->eraseFromParent();
300   return true;
301 }
302
303 /// Simplify a switch instruction by removing cases which can never fire. If the
304 /// uselessness of a case could be determined locally then constant propagation
305 /// would already have figured it out. Instead, walk the predecessors and
306 /// statically evaluate cases based on information available on that edge. Cases
307 /// that cannot fire no matter what the incoming edge can safely be removed. If
308 /// a case fires on every incoming edge then the entire switch can be removed
309 /// and replaced with a branch to the case destination.
310 static bool processSwitch(SwitchInst *SI, LazyValueInfo *LVI,
311                           DominatorTree *DT) {
312   DomTreeUpdater DTU(*DT, DomTreeUpdater::UpdateStrategy::Lazy);
313   Value *Cond = SI->getCondition();
314   BasicBlock *BB = SI->getParent();
315
316   // If the condition was defined in same block as the switch then LazyValueInfo
317   // currently won't say anything useful about it, though in theory it could.
318   if (isa<Instruction>(Cond) && cast<Instruction>(Cond)->getParent() == BB)
319     return false;
320
321   // If the switch is unreachable then trying to improve it is a waste of time.
322   pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
323   if (PB == PE) return false;
324
325   // Analyse each switch case in turn.
326   bool Changed = false;
327   DenseMap<BasicBlock*, int> SuccessorsCount;
328   for (auto *Succ : successors(BB))
329     SuccessorsCount[Succ]++;
330
331   for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
332     ConstantInt *Case = CI->getCaseValue();
333
334     // Check to see if the switch condition is equal to/not equal to the case
335     // value on every incoming edge, equal/not equal being the same each time.
336     LazyValueInfo::Tristate State = LazyValueInfo::Unknown;
337     for (pred_iterator PI = PB; PI != PE; ++PI) {
338       // Is the switch condition equal to the case value?
339       LazyValueInfo::Tristate Value = LVI->getPredicateOnEdge(CmpInst::ICMP_EQ,
340                                                               Cond, Case, *PI,
341                                                               BB, SI);
342       // Give up on this case if nothing is known.
343       if (Value == LazyValueInfo::Unknown) {
344         State = LazyValueInfo::Unknown;
345         break;
346       }
347
348       // If this was the first edge to be visited, record that all other edges
349       // need to give the same result.
350       if (PI == PB) {
351         State = Value;
352         continue;
353       }
354
355       // If this case is known to fire for some edges and known not to fire for
356       // others then there is nothing we can do - give up.
357       if (Value != State) {
358         State = LazyValueInfo::Unknown;
359         break;
360       }
361     }
362
363     if (State == LazyValueInfo::False) {
364       // This case never fires - remove it.
365       BasicBlock *Succ = CI->getCaseSuccessor();
366       Succ->removePredecessor(BB);
367       CI = SI->removeCase(CI);
368       CE = SI->case_end();
369
370       // The condition can be modified by removePredecessor's PHI simplification
371       // logic.
372       Cond = SI->getCondition();
373
374       ++NumDeadCases;
375       Changed = true;
376       if (--SuccessorsCount[Succ] == 0)
377         DTU.deleteEdge(BB, Succ);
378       continue;
379     }
380     if (State == LazyValueInfo::True) {
381       // This case always fires.  Arrange for the switch to be turned into an
382       // unconditional branch by replacing the switch condition with the case
383       // value.
384       SI->setCondition(Case);
385       NumDeadCases += SI->getNumCases();
386       Changed = true;
387       break;
388     }
389
390     // Increment the case iterator since we didn't delete it.
391     ++CI;
392   }
393
394   if (Changed)
395     // If the switch has been simplified to the point where it can be replaced
396     // by a branch then do so now.
397     ConstantFoldTerminator(BB, /*DeleteDeadConditions = */ false,
398                            /*TLI = */ nullptr, &DTU);
399   return Changed;
400 }
401
402 // See if we can prove that the given overflow intrinsic will not overflow.
403 static bool willNotOverflow(IntrinsicInst *II, LazyValueInfo *LVI) {
404   using OBO = OverflowingBinaryOperator;
405   auto NoWrap = [&] (Instruction::BinaryOps BinOp, unsigned NoWrapKind) {
406     Value *RHS = II->getOperand(1);
407     ConstantRange RRange = LVI->getConstantRange(RHS, II->getParent(), II);
408     ConstantRange NWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
409         BinOp, RRange, NoWrapKind);
410     // As an optimization, do not compute LRange if we do not need it.
411     if (NWRegion.isEmptySet())
412       return false;
413     Value *LHS = II->getOperand(0);
414     ConstantRange LRange = LVI->getConstantRange(LHS, II->getParent(), II);
415     return NWRegion.contains(LRange);
416   };
417   switch (II->getIntrinsicID()) {
418   default:
419     break;
420   case Intrinsic::uadd_with_overflow:
421     return NoWrap(Instruction::Add, OBO::NoUnsignedWrap);
422   case Intrinsic::sadd_with_overflow:
423     return NoWrap(Instruction::Add, OBO::NoSignedWrap);
424   case Intrinsic::usub_with_overflow:
425     return NoWrap(Instruction::Sub, OBO::NoUnsignedWrap);
426   case Intrinsic::ssub_with_overflow:
427     return NoWrap(Instruction::Sub, OBO::NoSignedWrap);
428   }
429   return false;
430 }
431
432 static void processOverflowIntrinsic(IntrinsicInst *II) {
433   IRBuilder<> B(II);
434   Value *NewOp = nullptr;
435   switch (II->getIntrinsicID()) {
436   default:
437     llvm_unreachable("Unexpected instruction.");
438   case Intrinsic::uadd_with_overflow:
439   case Intrinsic::sadd_with_overflow:
440     NewOp = B.CreateAdd(II->getOperand(0), II->getOperand(1), II->getName());
441     break;
442   case Intrinsic::usub_with_overflow:
443   case Intrinsic::ssub_with_overflow:
444     NewOp = B.CreateSub(II->getOperand(0), II->getOperand(1), II->getName());
445     break;
446   }
447   ++NumOverflows;
448   Value *NewI = B.CreateInsertValue(UndefValue::get(II->getType()), NewOp, 0);
449   NewI = B.CreateInsertValue(NewI, ConstantInt::getFalse(II->getContext()), 1);
450   II->replaceAllUsesWith(NewI);
451   II->eraseFromParent();
452 }
453
454 /// Infer nonnull attributes for the arguments at the specified callsite.
455 static bool processCallSite(CallSite CS, LazyValueInfo *LVI) {
456   SmallVector<unsigned, 4> ArgNos;
457   unsigned ArgNo = 0;
458
459   if (auto *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
460     if (willNotOverflow(II, LVI)) {
461       processOverflowIntrinsic(II);
462       return true;
463     }
464   }
465
466   for (Value *V : CS.args()) {
467     PointerType *Type = dyn_cast<PointerType>(V->getType());
468     // Try to mark pointer typed parameters as non-null.  We skip the
469     // relatively expensive analysis for constants which are obviously either
470     // null or non-null to start with.
471     if (Type && !CS.paramHasAttr(ArgNo, Attribute::NonNull) &&
472         !isa<Constant>(V) &&
473         LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
474                             ConstantPointerNull::get(Type),
475                             CS.getInstruction()) == LazyValueInfo::False)
476       ArgNos.push_back(ArgNo);
477     ArgNo++;
478   }
479
480   assert(ArgNo == CS.arg_size() && "sanity check");
481
482   if (ArgNos.empty())
483     return false;
484
485   AttributeList AS = CS.getAttributes();
486   LLVMContext &Ctx = CS.getInstruction()->getContext();
487   AS = AS.addParamAttribute(Ctx, ArgNos,
488                             Attribute::get(Ctx, Attribute::NonNull));
489   CS.setAttributes(AS);
490
491   return true;
492 }
493
494 static bool hasPositiveOperands(BinaryOperator *SDI, LazyValueInfo *LVI) {
495   Constant *Zero = ConstantInt::get(SDI->getType(), 0);
496   for (Value *O : SDI->operands()) {
497     auto Result = LVI->getPredicateAt(ICmpInst::ICMP_SGE, O, Zero, SDI);
498     if (Result != LazyValueInfo::True)
499       return false;
500   }
501   return true;
502 }
503
504 /// Try to shrink a udiv/urem's width down to the smallest power of two that's
505 /// sufficient to contain its operands.
506 static bool processUDivOrURem(BinaryOperator *Instr, LazyValueInfo *LVI) {
507   assert(Instr->getOpcode() == Instruction::UDiv ||
508          Instr->getOpcode() == Instruction::URem);
509   if (Instr->getType()->isVectorTy())
510     return false;
511
512   // Find the smallest power of two bitwidth that's sufficient to hold Instr's
513   // operands.
514   auto OrigWidth = Instr->getType()->getIntegerBitWidth();
515   ConstantRange OperandRange(OrigWidth, /*isFullset=*/false);
516   for (Value *Operand : Instr->operands()) {
517     OperandRange = OperandRange.unionWith(
518         LVI->getConstantRange(Operand, Instr->getParent()));
519   }
520   // Don't shrink below 8 bits wide.
521   unsigned NewWidth = std::max<unsigned>(
522       PowerOf2Ceil(OperandRange.getUnsignedMax().getActiveBits()), 8);
523   // NewWidth might be greater than OrigWidth if OrigWidth is not a power of
524   // two.
525   if (NewWidth >= OrigWidth)
526     return false;
527
528   ++NumUDivs;
529   IRBuilder<> B{Instr};
530   auto *TruncTy = Type::getIntNTy(Instr->getContext(), NewWidth);
531   auto *LHS = B.CreateTruncOrBitCast(Instr->getOperand(0), TruncTy,
532                                      Instr->getName() + ".lhs.trunc");
533   auto *RHS = B.CreateTruncOrBitCast(Instr->getOperand(1), TruncTy,
534                                      Instr->getName() + ".rhs.trunc");
535   auto *BO = B.CreateBinOp(Instr->getOpcode(), LHS, RHS, Instr->getName());
536   auto *Zext = B.CreateZExt(BO, Instr->getType(), Instr->getName() + ".zext");
537   if (auto *BinOp = dyn_cast<BinaryOperator>(BO))
538     if (BinOp->getOpcode() == Instruction::UDiv)
539       BinOp->setIsExact(Instr->isExact());
540
541   Instr->replaceAllUsesWith(Zext);
542   Instr->eraseFromParent();
543   return true;
544 }
545
546 static bool processSRem(BinaryOperator *SDI, LazyValueInfo *LVI) {
547   if (SDI->getType()->isVectorTy() || !hasPositiveOperands(SDI, LVI))
548     return false;
549
550   ++NumSRems;
551   auto *BO = BinaryOperator::CreateURem(SDI->getOperand(0), SDI->getOperand(1),
552                                         SDI->getName(), SDI);
553   BO->setDebugLoc(SDI->getDebugLoc());
554   SDI->replaceAllUsesWith(BO);
555   SDI->eraseFromParent();
556
557   // Try to process our new urem.
558   processUDivOrURem(BO, LVI);
559
560   return true;
561 }
562
563 /// See if LazyValueInfo's ability to exploit edge conditions or range
564 /// information is sufficient to prove the both operands of this SDiv are
565 /// positive.  If this is the case, replace the SDiv with a UDiv. Even for local
566 /// conditions, this can sometimes prove conditions instcombine can't by
567 /// exploiting range information.
568 static bool processSDiv(BinaryOperator *SDI, LazyValueInfo *LVI) {
569   if (SDI->getType()->isVectorTy() || !hasPositiveOperands(SDI, LVI))
570     return false;
571
572   ++NumSDivs;
573   auto *BO = BinaryOperator::CreateUDiv(SDI->getOperand(0), SDI->getOperand(1),
574                                         SDI->getName(), SDI);
575   BO->setDebugLoc(SDI->getDebugLoc());
576   BO->setIsExact(SDI->isExact());
577   SDI->replaceAllUsesWith(BO);
578   SDI->eraseFromParent();
579
580   // Try to simplify our new udiv.
581   processUDivOrURem(BO, LVI);
582
583   return true;
584 }
585
586 static bool processAShr(BinaryOperator *SDI, LazyValueInfo *LVI) {
587   if (SDI->getType()->isVectorTy())
588     return false;
589
590   Constant *Zero = ConstantInt::get(SDI->getType(), 0);
591   if (LVI->getPredicateAt(ICmpInst::ICMP_SGE, SDI->getOperand(0), Zero, SDI) !=
592       LazyValueInfo::True)
593     return false;
594
595   ++NumAShrs;
596   auto *BO = BinaryOperator::CreateLShr(SDI->getOperand(0), SDI->getOperand(1),
597                                         SDI->getName(), SDI);
598   BO->setDebugLoc(SDI->getDebugLoc());
599   BO->setIsExact(SDI->isExact());
600   SDI->replaceAllUsesWith(BO);
601   SDI->eraseFromParent();
602
603   return true;
604 }
605
606 static bool processAdd(BinaryOperator *AddOp, LazyValueInfo *LVI) {
607   using OBO = OverflowingBinaryOperator;
608
609   if (DontProcessAdds)
610     return false;
611
612   if (AddOp->getType()->isVectorTy())
613     return false;
614
615   bool NSW = AddOp->hasNoSignedWrap();
616   bool NUW = AddOp->hasNoUnsignedWrap();
617   if (NSW && NUW)
618     return false;
619
620   BasicBlock *BB = AddOp->getParent();
621
622   Value *LHS = AddOp->getOperand(0);
623   Value *RHS = AddOp->getOperand(1);
624
625   ConstantRange LRange = LVI->getConstantRange(LHS, BB, AddOp);
626
627   // Initialize RRange only if we need it. If we know that guaranteed no wrap
628   // range for the given LHS range is empty don't spend time calculating the
629   // range for the RHS.
630   Optional<ConstantRange> RRange;
631   auto LazyRRange = [&] () {
632       if (!RRange)
633         RRange = LVI->getConstantRange(RHS, BB, AddOp);
634       return RRange.getValue();
635   };
636
637   bool Changed = false;
638   if (!NUW) {
639     ConstantRange NUWRange = ConstantRange::makeGuaranteedNoWrapRegion(
640         BinaryOperator::Add, LRange, OBO::NoUnsignedWrap);
641     if (!NUWRange.isEmptySet()) {
642       bool NewNUW = NUWRange.contains(LazyRRange());
643       AddOp->setHasNoUnsignedWrap(NewNUW);
644       Changed |= NewNUW;
645     }
646   }
647   if (!NSW) {
648     ConstantRange NSWRange = ConstantRange::makeGuaranteedNoWrapRegion(
649         BinaryOperator::Add, LRange, OBO::NoSignedWrap);
650     if (!NSWRange.isEmptySet()) {
651       bool NewNSW = NSWRange.contains(LazyRRange());
652       AddOp->setHasNoSignedWrap(NewNSW);
653       Changed |= NewNSW;
654     }
655   }
656
657   return Changed;
658 }
659
660 static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
661   if (Constant *C = LVI->getConstant(V, At->getParent(), At))
662     return C;
663
664   // TODO: The following really should be sunk inside LVI's core algorithm, or
665   // at least the outer shims around such.
666   auto *C = dyn_cast<CmpInst>(V);
667   if (!C) return nullptr;
668
669   Value *Op0 = C->getOperand(0);
670   Constant *Op1 = dyn_cast<Constant>(C->getOperand(1));
671   if (!Op1) return nullptr;
672
673   LazyValueInfo::Tristate Result =
674     LVI->getPredicateAt(C->getPredicate(), Op0, Op1, At);
675   if (Result == LazyValueInfo::Unknown)
676     return nullptr;
677
678   return (Result == LazyValueInfo::True) ?
679     ConstantInt::getTrue(C->getContext()) :
680     ConstantInt::getFalse(C->getContext());
681 }
682
683 static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT,
684                     const SimplifyQuery &SQ) {
685   bool FnChanged = false;
686   // Visiting in a pre-order depth-first traversal causes us to simplify early
687   // blocks before querying later blocks (which require us to analyze early
688   // blocks).  Eagerly simplifying shallow blocks means there is strictly less
689   // work to do for deep blocks.  This also means we don't visit unreachable
690   // blocks.
691   for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
692     bool BBChanged = false;
693     for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
694       Instruction *II = &*BI++;
695       switch (II->getOpcode()) {
696       case Instruction::Select:
697         BBChanged |= processSelect(cast<SelectInst>(II), LVI);
698         break;
699       case Instruction::PHI:
700         BBChanged |= processPHI(cast<PHINode>(II), LVI, DT, SQ);
701         break;
702       case Instruction::ICmp:
703       case Instruction::FCmp:
704         BBChanged |= processCmp(cast<CmpInst>(II), LVI);
705         break;
706       case Instruction::Load:
707       case Instruction::Store:
708         BBChanged |= processMemAccess(II, LVI);
709         break;
710       case Instruction::Call:
711       case Instruction::Invoke:
712         BBChanged |= processCallSite(CallSite(II), LVI);
713         break;
714       case Instruction::SRem:
715         BBChanged |= processSRem(cast<BinaryOperator>(II), LVI);
716         break;
717       case Instruction::SDiv:
718         BBChanged |= processSDiv(cast<BinaryOperator>(II), LVI);
719         break;
720       case Instruction::UDiv:
721       case Instruction::URem:
722         BBChanged |= processUDivOrURem(cast<BinaryOperator>(II), LVI);
723         break;
724       case Instruction::AShr:
725         BBChanged |= processAShr(cast<BinaryOperator>(II), LVI);
726         break;
727       case Instruction::Add:
728         BBChanged |= processAdd(cast<BinaryOperator>(II), LVI);
729         break;
730       }
731     }
732
733     Instruction *Term = BB->getTerminator();
734     switch (Term->getOpcode()) {
735     case Instruction::Switch:
736       BBChanged |= processSwitch(cast<SwitchInst>(Term), LVI, DT);
737       break;
738     case Instruction::Ret: {
739       auto *RI = cast<ReturnInst>(Term);
740       // Try to determine the return value if we can.  This is mainly here to
741       // simplify the writing of unit tests, but also helps to enable IPO by
742       // constant folding the return values of callees.
743       auto *RetVal = RI->getReturnValue();
744       if (!RetVal) break; // handle "ret void"
745       if (isa<Constant>(RetVal)) break; // nothing to do
746       if (auto *C = getConstantAt(RetVal, RI, LVI)) {
747         ++NumReturns;
748         RI->replaceUsesOfWith(RetVal, C);
749         BBChanged = true;
750       }
751     }
752     }
753
754     FnChanged |= BBChanged;
755   }
756
757   return FnChanged;
758 }
759
760 bool CorrelatedValuePropagation::runOnFunction(Function &F) {
761   if (skipFunction(F))
762     return false;
763
764   LazyValueInfo *LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
765   DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
766
767   return runImpl(F, LVI, DT, getBestSimplifyQuery(*this, F));
768 }
769
770 PreservedAnalyses
771 CorrelatedValuePropagationPass::run(Function &F, FunctionAnalysisManager &AM) {
772   LazyValueInfo *LVI = &AM.getResult<LazyValueAnalysis>(F);
773   DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
774
775   bool Changed = runImpl(F, LVI, DT, getBestSimplifyQuery(AM, F));
776
777   if (!Changed)
778     return PreservedAnalyses::all();
779   PreservedAnalyses PA;
780   PA.preserve<GlobalsAA>();
781   PA.preserve<DominatorTreeAnalysis>();
782   return PA;
783 }