| OLD | NEW |
| 1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// | 1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file contains the implementation of the scalar evolution analysis | 10 // This file contains the implementation of the scalar evolution analysis |
| (...skipping 3919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3930 /// getSmallConstantTripCount - Returns the maximum trip count of this loop as a | 3930 /// getSmallConstantTripCount - Returns the maximum trip count of this loop as a |
| 3931 /// normal unsigned value. Returns 0 if the trip count is unknown or not | 3931 /// normal unsigned value. Returns 0 if the trip count is unknown or not |
| 3932 /// constant. Will also return 0 if the maximum trip count is very large (>= | 3932 /// constant. Will also return 0 if the maximum trip count is very large (>= |
| 3933 /// 2^32). | 3933 /// 2^32). |
| 3934 /// | 3934 /// |
| 3935 /// This "trip count" assumes that control exits via ExitingBlock. More | 3935 /// This "trip count" assumes that control exits via ExitingBlock. More |
| 3936 /// precisely, it is the number of times that control may reach ExitingBlock | 3936 /// precisely, it is the number of times that control may reach ExitingBlock |
| 3937 /// before taking the branch. For loops with multiple exits, it may not be the | 3937 /// before taking the branch. For loops with multiple exits, it may not be the |
| 3938 /// number times that the loop header executes because the loop may exit | 3938 /// number times that the loop header executes because the loop may exit |
| 3939 /// prematurely via another branch. | 3939 /// prematurely via another branch. |
| 3940 /// | |
| 3941 /// FIXME: We conservatively call getBackedgeTakenCount(L) instead of | |
| 3942 /// getExitCount(L, ExitingBlock) to compute a safe trip count considering all | |
| 3943 /// loop exits. getExitCount() may return an exact count for this branch | |
| 3944 /// assuming no-signed-wrap. The number of well-defined iterations may actually | |
| 3945 /// be higher than this trip count if this exit test is skipped and the loop | |
| 3946 /// exits via a different branch. Ideally, getExitCount() would know whether it | |
| 3947 /// depends on a NSW assumption, and we would only fall back to a conservative | |
| 3948 /// trip count in that case. | |
| 3949 unsigned ScalarEvolution:: | 3940 unsigned ScalarEvolution:: |
| 3950 getSmallConstantTripCount(Loop *L, BasicBlock */*ExitingBlock*/) { | 3941 getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock) { |
| 3951 const SCEVConstant *ExitCount = | 3942 const SCEVConstant *ExitCount = |
| 3952 dyn_cast<SCEVConstant>(getBackedgeTakenCount(L)); | 3943 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock)); |
| 3953 if (!ExitCount) | 3944 if (!ExitCount) |
| 3954 return 0; | 3945 return 0; |
| 3955 | 3946 |
| 3956 ConstantInt *ExitConst = ExitCount->getValue(); | 3947 ConstantInt *ExitConst = ExitCount->getValue(); |
| 3957 | 3948 |
| 3958 // Guard against huge trip counts. | 3949 // Guard against huge trip counts. |
| 3959 if (ExitConst->getValue().getActiveBits() > 32) | 3950 if (ExitConst->getValue().getActiveBits() > 32) |
| 3960 return 0; | 3951 return 0; |
| 3961 | 3952 |
| 3962 // In case of integer overflow, this returns 0, which is correct. | 3953 // In case of integer overflow, this returns 0, which is correct. |
| 3963 return ((unsigned)ExitConst->getZExtValue()) + 1; | 3954 return ((unsigned)ExitConst->getZExtValue()) + 1; |
| 3964 } | 3955 } |
| 3965 | 3956 |
| 3966 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the | 3957 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the |
| 3967 /// trip count of this loop as a normal unsigned value, if possible. This | 3958 /// trip count of this loop as a normal unsigned value, if possible. This |
| 3968 /// means that the actual trip count is always a multiple of the returned | 3959 /// means that the actual trip count is always a multiple of the returned |
| 3969 /// value (don't forget the trip count could very well be zero as well!). | 3960 /// value (don't forget the trip count could very well be zero as well!). |
| 3970 /// | 3961 /// |
| 3971 /// Returns 1 if the trip count is unknown or not guaranteed to be the | 3962 /// Returns 1 if the trip count is unknown or not guaranteed to be the |
| 3972 /// multiple of a constant (which is also the case if the trip count is simply | 3963 /// multiple of a constant (which is also the case if the trip count is simply |
| 3973 /// constant, use getSmallConstantTripCount for that case), Will also return 1 | 3964 /// constant, use getSmallConstantTripCount for that case), Will also return 1 |
| 3974 /// if the trip count is very large (>= 2^32). | 3965 /// if the trip count is very large (>= 2^32). |
| 3975 /// | 3966 /// |
| 3976 /// As explained in the comments for getSmallConstantTripCount, this assumes | 3967 /// As explained in the comments for getSmallConstantTripCount, this assumes |
| 3977 /// that control exits the loop via ExitingBlock. | 3968 /// that control exits the loop via ExitingBlock. |
| 3978 unsigned ScalarEvolution:: | 3969 unsigned ScalarEvolution:: |
| 3979 getSmallConstantTripMultiple(Loop *L, BasicBlock */*ExitingBlock*/) { | 3970 getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock) { |
| 3980 const SCEV *ExitCount = getBackedgeTakenCount(L); | 3971 const SCEV *ExitCount = getExitCount(L, ExitingBlock); |
| 3981 if (ExitCount == getCouldNotCompute()) | 3972 if (ExitCount == getCouldNotCompute()) |
| 3982 return 1; | 3973 return 1; |
| 3983 | 3974 |
| 3984 // Get the trip count from the BE count by adding 1. | 3975 // Get the trip count from the BE count by adding 1. |
| 3985 const SCEV *TCMul = getAddExpr(ExitCount, | 3976 const SCEV *TCMul = getAddExpr(ExitCount, |
| 3986 getConstant(ExitCount->getType(), 1)); | 3977 getConstant(ExitCount->getType(), 1)); |
| 3987 // FIXME: SCEV distributes multiplication as V1*C1 + V2*C1. We could attempt | 3978 // FIXME: SCEV distributes multiplication as V1*C1 + V2*C1. We could attempt |
| 3988 // to factor simple cases. | 3979 // to factor simple cases. |
| 3989 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(TCMul)) | 3980 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(TCMul)) |
| 3990 TCMul = Mul->getOperand(0); | 3981 TCMul = Mul->getOperand(0); |
| 3991 | 3982 |
| 3992 const SCEVConstant *MulC = dyn_cast<SCEVConstant>(TCMul); | 3983 const SCEVConstant *MulC = dyn_cast<SCEVConstant>(TCMul); |
| 3993 if (!MulC) | 3984 if (!MulC) |
| 3994 return 1; | 3985 return 1; |
| 3995 | 3986 |
| 3996 ConstantInt *Result = MulC->getValue(); | 3987 ConstantInt *Result = MulC->getValue(); |
| 3997 | 3988 |
| 3998 // Guard against huge trip counts (this requires checking | 3989 // Guard against huge trip counts (this requires checking |
| 3999 // for zero to handle the case where the trip count == -1 and the | 3990 // for zero to handle the case where the trip count == -1 and the |
| 4000 // addition wraps). | 3991 // addition wraps). |
| 4001 if (!Result || Result->getValue().getActiveBits() > 32 || | 3992 if (!Result || Result->getValue().getActiveBits() > 32 || |
| 4002 Result->getValue().getActiveBits() == 0) | 3993 Result->getValue().getActiveBits() == 0) |
| 4003 return 1; | 3994 return 1; |
| 4004 | 3995 |
| 4005 return (unsigned)Result->getZExtValue(); | 3996 return (unsigned)Result->getZExtValue(); |
| 4006 } | 3997 } |
| 4007 | 3998 |
| 4008 // getExitCount - Get the expression for the number of loop iterations for which | 3999 // getExitCount - Get the expression for the number of loop iterations for which |
| 4009 // this loop is guaranteed not to exit via ExitingBlock. Otherwise return | 4000 // this loop is guaranteed not to exit via ExitintBlock. Otherwise return |
| 4010 // SCEVCouldNotCompute. | 4001 // SCEVCouldNotCompute. |
| 4011 const SCEV *ScalarEvolution::getExitCount(Loop *L, BasicBlock *ExitingBlock) { | 4002 const SCEV *ScalarEvolution::getExitCount(Loop *L, BasicBlock *ExitingBlock) { |
| 4012 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); | 4003 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); |
| 4013 } | 4004 } |
| 4014 | 4005 |
| 4015 /// getBackedgeTakenCount - If the specified loop has a predictable | 4006 /// getBackedgeTakenCount - If the specified loop has a predictable |
| 4016 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute | 4007 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute |
| 4017 /// object. The backedge-taken count is the number of times the loop header | 4008 /// object. The backedge-taken count is the number of times the loop header |
| 4018 /// will be branched to from within the loop. This is one less than the | 4009 /// will be branched to from within the loop. This is one less than the |
| 4019 /// trip count of the loop, since it doesn't count the first iteration, | 4010 /// trip count of the loop, since it doesn't count the first iteration, |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4384 } | 4375 } |
| 4385 BB = Pred; | 4376 BB = Pred; |
| 4386 } | 4377 } |
| 4387 if (!Ok) | 4378 if (!Ok) |
| 4388 return getCouldNotCompute(); | 4379 return getCouldNotCompute(); |
| 4389 } | 4380 } |
| 4390 | 4381 |
| 4391 // Proceed to the next level to examine the exit condition expression. | 4382 // Proceed to the next level to examine the exit condition expression. |
| 4392 return ComputeExitLimitFromCond(L, ExitBr->getCondition(), | 4383 return ComputeExitLimitFromCond(L, ExitBr->getCondition(), |
| 4393 ExitBr->getSuccessor(0), | 4384 ExitBr->getSuccessor(0), |
| 4394 ExitBr->getSuccessor(1), | 4385 ExitBr->getSuccessor(1)); |
| 4395 /*IsSubExpr=*/false); | |
| 4396 } | 4386 } |
| 4397 | 4387 |
| 4398 /// ComputeExitLimitFromCond - Compute the number of times the | 4388 /// ComputeExitLimitFromCond - Compute the number of times the |
| 4399 /// backedge of the specified loop will execute if its exit condition | 4389 /// backedge of the specified loop will execute if its exit condition |
| 4400 /// were a conditional branch of ExitCond, TBB, and FBB. | 4390 /// were a conditional branch of ExitCond, TBB, and FBB. |
| 4401 /// | |
| 4402 /// @param IsSubExpr is true if ExitCond does not directly control the exit | |
| 4403 /// branch. In this case, we cannot assume that the loop only exits when the | |
| 4404 /// condition is true and cannot infer that failing to meet the condition prior | |
| 4405 /// to integer wraparound results in undefined behavior. | |
| 4406 ScalarEvolution::ExitLimit | 4391 ScalarEvolution::ExitLimit |
| 4407 ScalarEvolution::ComputeExitLimitFromCond(const Loop *L, | 4392 ScalarEvolution::ComputeExitLimitFromCond(const Loop *L, |
| 4408 Value *ExitCond, | 4393 Value *ExitCond, |
| 4409 BasicBlock *TBB, | 4394 BasicBlock *TBB, |
| 4410 BasicBlock *FBB, | 4395 BasicBlock *FBB) { |
| 4411 bool IsSubExpr) { | |
| 4412 // Check if the controlling expression for this loop is an And or Or. | 4396 // Check if the controlling expression for this loop is an And or Or. |
| 4413 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { | 4397 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { |
| 4414 if (BO->getOpcode() == Instruction::And) { | 4398 if (BO->getOpcode() == Instruction::And) { |
| 4415 // Recurse on the operands of the and. | 4399 // Recurse on the operands of the and. |
| 4416 bool EitherMayExit = L->contains(TBB); | 4400 ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB); |
| 4417 ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB, | 4401 ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB); |
| 4418 IsSubExpr || EitherMayExit); | |
| 4419 ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB, | |
| 4420 IsSubExpr || EitherMayExit); | |
| 4421 const SCEV *BECount = getCouldNotCompute(); | 4402 const SCEV *BECount = getCouldNotCompute(); |
| 4422 const SCEV *MaxBECount = getCouldNotCompute(); | 4403 const SCEV *MaxBECount = getCouldNotCompute(); |
| 4423 if (EitherMayExit) { | 4404 if (L->contains(TBB)) { |
| 4424 // Both conditions must be true for the loop to continue executing. | 4405 // Both conditions must be true for the loop to continue executing. |
| 4425 // Choose the less conservative count. | 4406 // Choose the less conservative count. |
| 4426 if (EL0.Exact == getCouldNotCompute() || | 4407 if (EL0.Exact == getCouldNotCompute() || |
| 4427 EL1.Exact == getCouldNotCompute()) | 4408 EL1.Exact == getCouldNotCompute()) |
| 4428 BECount = getCouldNotCompute(); | 4409 BECount = getCouldNotCompute(); |
| 4429 else | 4410 else |
| 4430 BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact); | 4411 BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact); |
| 4431 if (EL0.Max == getCouldNotCompute()) | 4412 if (EL0.Max == getCouldNotCompute()) |
| 4432 MaxBECount = EL1.Max; | 4413 MaxBECount = EL1.Max; |
| 4433 else if (EL1.Max == getCouldNotCompute()) | 4414 else if (EL1.Max == getCouldNotCompute()) |
| 4434 MaxBECount = EL0.Max; | 4415 MaxBECount = EL0.Max; |
| 4435 else | 4416 else |
| 4436 MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max); | 4417 MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max); |
| 4437 } else { | 4418 } else { |
| 4438 // Both conditions must be true at the same time for the loop to exit. | 4419 // Both conditions must be true at the same time for the loop to exit. |
| 4439 // For now, be conservative. | 4420 // For now, be conservative. |
| 4440 assert(L->contains(FBB) && "Loop block has no successor in loop!"); | 4421 assert(L->contains(FBB) && "Loop block has no successor in loop!"); |
| 4441 if (EL0.Max == EL1.Max) | 4422 if (EL0.Max == EL1.Max) |
| 4442 MaxBECount = EL0.Max; | 4423 MaxBECount = EL0.Max; |
| 4443 if (EL0.Exact == EL1.Exact) | 4424 if (EL0.Exact == EL1.Exact) |
| 4444 BECount = EL0.Exact; | 4425 BECount = EL0.Exact; |
| 4445 } | 4426 } |
| 4446 | 4427 |
| 4447 return ExitLimit(BECount, MaxBECount); | 4428 return ExitLimit(BECount, MaxBECount); |
| 4448 } | 4429 } |
| 4449 if (BO->getOpcode() == Instruction::Or) { | 4430 if (BO->getOpcode() == Instruction::Or) { |
| 4450 // Recurse on the operands of the or. | 4431 // Recurse on the operands of the or. |
| 4451 bool EitherMayExit = L->contains(FBB); | 4432 ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB); |
| 4452 ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB, | 4433 ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB); |
| 4453 IsSubExpr || EitherMayExit); | |
| 4454 ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB, | |
| 4455 IsSubExpr || EitherMayExit); | |
| 4456 const SCEV *BECount = getCouldNotCompute(); | 4434 const SCEV *BECount = getCouldNotCompute(); |
| 4457 const SCEV *MaxBECount = getCouldNotCompute(); | 4435 const SCEV *MaxBECount = getCouldNotCompute(); |
| 4458 if (EitherMayExit) { | 4436 if (L->contains(FBB)) { |
| 4459 // Both conditions must be false for the loop to continue executing. | 4437 // Both conditions must be false for the loop to continue executing. |
| 4460 // Choose the less conservative count. | 4438 // Choose the less conservative count. |
| 4461 if (EL0.Exact == getCouldNotCompute() || | 4439 if (EL0.Exact == getCouldNotCompute() || |
| 4462 EL1.Exact == getCouldNotCompute()) | 4440 EL1.Exact == getCouldNotCompute()) |
| 4463 BECount = getCouldNotCompute(); | 4441 BECount = getCouldNotCompute(); |
| 4464 else | 4442 else |
| 4465 BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact); | 4443 BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact); |
| 4466 if (EL0.Max == getCouldNotCompute()) | 4444 if (EL0.Max == getCouldNotCompute()) |
| 4467 MaxBECount = EL1.Max; | 4445 MaxBECount = EL1.Max; |
| 4468 else if (EL1.Max == getCouldNotCompute()) | 4446 else if (EL1.Max == getCouldNotCompute()) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 4479 BECount = EL0.Exact; | 4457 BECount = EL0.Exact; |
| 4480 } | 4458 } |
| 4481 | 4459 |
| 4482 return ExitLimit(BECount, MaxBECount); | 4460 return ExitLimit(BECount, MaxBECount); |
| 4483 } | 4461 } |
| 4484 } | 4462 } |
| 4485 | 4463 |
| 4486 // With an icmp, it may be feasible to compute an exact backedge-taken count. | 4464 // With an icmp, it may be feasible to compute an exact backedge-taken count. |
| 4487 // Proceed to the next level to examine the icmp. | 4465 // Proceed to the next level to examine the icmp. |
| 4488 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) | 4466 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) |
| 4489 return ComputeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, IsSubExpr); | 4467 return ComputeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB); |
| 4490 | 4468 |
| 4491 // Check for a constant condition. These are normally stripped out by | 4469 // Check for a constant condition. These are normally stripped out by |
| 4492 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to | 4470 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to |
| 4493 // preserve the CFG and is temporarily leaving constant conditions | 4471 // preserve the CFG and is temporarily leaving constant conditions |
| 4494 // in place. | 4472 // in place. |
| 4495 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { | 4473 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { |
| 4496 if (L->contains(FBB) == !CI->getZExtValue()) | 4474 if (L->contains(FBB) == !CI->getZExtValue()) |
| 4497 // The backedge is always taken. | 4475 // The backedge is always taken. |
| 4498 return getCouldNotCompute(); | 4476 return getCouldNotCompute(); |
| 4499 else | 4477 else |
| 4500 // The backedge is never taken. | 4478 // The backedge is never taken. |
| 4501 return getConstant(CI->getType(), 0); | 4479 return getConstant(CI->getType(), 0); |
| 4502 } | 4480 } |
| 4503 | 4481 |
| 4504 // If it's not an integer or pointer comparison then compute it the hard way. | 4482 // If it's not an integer or pointer comparison then compute it the hard way. |
| 4505 return ComputeExitCountExhaustively(L, ExitCond, !L->contains(TBB)); | 4483 return ComputeExitCountExhaustively(L, ExitCond, !L->contains(TBB)); |
| 4506 } | 4484 } |
| 4507 | 4485 |
| 4508 /// ComputeExitLimitFromICmp - Compute the number of times the | 4486 /// ComputeExitLimitFromICmp - Compute the number of times the |
| 4509 /// backedge of the specified loop will execute if its exit condition | 4487 /// backedge of the specified loop will execute if its exit condition |
| 4510 /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. | 4488 /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. |
| 4511 ScalarEvolution::ExitLimit | 4489 ScalarEvolution::ExitLimit |
| 4512 ScalarEvolution::ComputeExitLimitFromICmp(const Loop *L, | 4490 ScalarEvolution::ComputeExitLimitFromICmp(const Loop *L, |
| 4513 ICmpInst *ExitCond, | 4491 ICmpInst *ExitCond, |
| 4514 BasicBlock *TBB, | 4492 BasicBlock *TBB, |
| 4515 BasicBlock *FBB, | 4493 BasicBlock *FBB) { |
| 4516 bool IsSubExpr) { | |
| 4517 | 4494 |
| 4518 // If the condition was exit on true, convert the condition to exit on false | 4495 // If the condition was exit on true, convert the condition to exit on false |
| 4519 ICmpInst::Predicate Cond; | 4496 ICmpInst::Predicate Cond; |
| 4520 if (!L->contains(FBB)) | 4497 if (!L->contains(FBB)) |
| 4521 Cond = ExitCond->getPredicate(); | 4498 Cond = ExitCond->getPredicate(); |
| 4522 else | 4499 else |
| 4523 Cond = ExitCond->getInversePredicate(); | 4500 Cond = ExitCond->getInversePredicate(); |
| 4524 | 4501 |
| 4525 // Handle common loops like: for (X = "string"; *X; ++X) | 4502 // Handle common loops like: for (X = "string"; *X; ++X) |
| 4526 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) | 4503 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4558 ConstantRange CompRange( | 4535 ConstantRange CompRange( |
| 4559 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); | 4536 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); |
| 4560 | 4537 |
| 4561 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); | 4538 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); |
| 4562 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; | 4539 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; |
| 4563 } | 4540 } |
| 4564 | 4541 |
| 4565 switch (Cond) { | 4542 switch (Cond) { |
| 4566 case ICmpInst::ICMP_NE: { // while (X != Y) | 4543 case ICmpInst::ICMP_NE: { // while (X != Y) |
| 4567 // Convert to: while (X-Y != 0) | 4544 // Convert to: while (X-Y != 0) |
| 4568 ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L, IsSubExpr); | 4545 ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L); |
| 4569 if (EL.hasAnyInfo()) return EL; | 4546 if (EL.hasAnyInfo()) return EL; |
| 4570 break; | 4547 break; |
| 4571 } | 4548 } |
| 4572 case ICmpInst::ICMP_EQ: { // while (X == Y) | 4549 case ICmpInst::ICMP_EQ: { // while (X == Y) |
| 4573 // Convert to: while (X-Y == 0) | 4550 // Convert to: while (X-Y == 0) |
| 4574 ExitLimit EL = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); | 4551 ExitLimit EL = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); |
| 4575 if (EL.hasAnyInfo()) return EL; | 4552 if (EL.hasAnyInfo()) return EL; |
| 4576 break; | 4553 break; |
| 4577 } | 4554 } |
| 4578 case ICmpInst::ICMP_SLT: { | 4555 case ICmpInst::ICMP_SLT: { |
| 4579 ExitLimit EL = HowManyLessThans(LHS, RHS, L, true, IsSubExpr); | 4556 ExitLimit EL = HowManyLessThans(LHS, RHS, L, true); |
| 4580 if (EL.hasAnyInfo()) return EL; | 4557 if (EL.hasAnyInfo()) return EL; |
| 4581 break; | 4558 break; |
| 4582 } | 4559 } |
| 4583 case ICmpInst::ICMP_SGT: { | 4560 case ICmpInst::ICMP_SGT: { |
| 4584 ExitLimit EL = HowManyLessThans(getNotSCEV(LHS), | 4561 ExitLimit EL = HowManyLessThans(getNotSCEV(LHS), |
| 4585 getNotSCEV(RHS), L, true, IsSubExpr); | 4562 getNotSCEV(RHS), L, true); |
| 4586 if (EL.hasAnyInfo()) return EL; | 4563 if (EL.hasAnyInfo()) return EL; |
| 4587 break; | 4564 break; |
| 4588 } | 4565 } |
| 4589 case ICmpInst::ICMP_ULT: { | 4566 case ICmpInst::ICMP_ULT: { |
| 4590 ExitLimit EL = HowManyLessThans(LHS, RHS, L, false, IsSubExpr); | 4567 ExitLimit EL = HowManyLessThans(LHS, RHS, L, false); |
| 4591 if (EL.hasAnyInfo()) return EL; | 4568 if (EL.hasAnyInfo()) return EL; |
| 4592 break; | 4569 break; |
| 4593 } | 4570 } |
| 4594 case ICmpInst::ICMP_UGT: { | 4571 case ICmpInst::ICMP_UGT: { |
| 4595 ExitLimit EL = HowManyLessThans(getNotSCEV(LHS), | 4572 ExitLimit EL = HowManyLessThans(getNotSCEV(LHS), |
| 4596 getNotSCEV(RHS), L, false, IsSubExpr); | 4573 getNotSCEV(RHS), L, false); |
| 4597 if (EL.hasAnyInfo()) return EL; | 4574 if (EL.hasAnyInfo()) return EL; |
| 4598 break; | 4575 break; |
| 4599 } | 4576 } |
| 4600 default: | 4577 default: |
| 4601 #if 0 | 4578 #if 0 |
| 4602 dbgs() << "ComputeBackedgeTakenCount "; | 4579 dbgs() << "ComputeBackedgeTakenCount "; |
| 4603 if (ExitCond->getOperand(0)->getType()->isUnsigned()) | 4580 if (ExitCond->getOperand(0)->getType()->isUnsigned()) |
| 4604 dbgs() << "[unsigned] "; | 4581 dbgs() << "[unsigned] "; |
| 4605 dbgs() << *LHS << " " | 4582 dbgs() << *LHS << " " |
| 4606 << Instruction::getOpcodeName(Instruction::ICmp) | 4583 << Instruction::getOpcodeName(Instruction::ICmp) |
| (...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5455 } | 5432 } |
| 5456 | 5433 |
| 5457 /// HowFarToZero - Return the number of times a backedge comparing the specified | 5434 /// HowFarToZero - Return the number of times a backedge comparing the specified |
| 5458 /// value to zero will execute. If not computable, return CouldNotCompute. | 5435 /// value to zero will execute. If not computable, return CouldNotCompute. |
| 5459 /// | 5436 /// |
| 5460 /// This is only used for loops with a "x != y" exit test. The exit condition is | 5437 /// This is only used for loops with a "x != y" exit test. The exit condition is |
| 5461 /// now expressed as a single expression, V = x-y. So the exit test is | 5438 /// now expressed as a single expression, V = x-y. So the exit test is |
| 5462 /// effectively V != 0. We know and take advantage of the fact that this | 5439 /// effectively V != 0. We know and take advantage of the fact that this |
| 5463 /// expression only being used in a comparison by zero context. | 5440 /// expression only being used in a comparison by zero context. |
| 5464 ScalarEvolution::ExitLimit | 5441 ScalarEvolution::ExitLimit |
| 5465 ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L, bool IsSubExpr) { | 5442 ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { |
| 5466 // If the value is a constant | 5443 // If the value is a constant |
| 5467 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { | 5444 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { |
| 5468 // If the value is already zero, the branch will execute zero times. | 5445 // If the value is already zero, the branch will execute zero times. |
| 5469 if (C->getValue()->isZero()) return C; | 5446 if (C->getValue()->isZero()) return C; |
| 5470 return getCouldNotCompute(); // Otherwise it will loop infinitely. | 5447 return getCouldNotCompute(); // Otherwise it will loop infinitely. |
| 5471 } | 5448 } |
| 5472 | 5449 |
| 5473 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); | 5450 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); |
| 5474 if (!AddRec || AddRec->getLoop() != L) | 5451 if (!AddRec || AddRec->getLoop() != L) |
| 5475 return getCouldNotCompute(); | 5452 return getCouldNotCompute(); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5553 MaxBECount = CR.getUnsignedMax().isMinValue() | 5530 MaxBECount = CR.getUnsignedMax().isMinValue() |
| 5554 ? getConstant(APInt::getMinValue(CR.getBitWidth())) | 5531 ? getConstant(APInt::getMinValue(CR.getBitWidth())) |
| 5555 : getConstant(APInt::getMaxValue(CR.getBitWidth())); | 5532 : getConstant(APInt::getMaxValue(CR.getBitWidth())); |
| 5556 else | 5533 else |
| 5557 MaxBECount = getConstant(CountDown ? CR.getUnsignedMax() | 5534 MaxBECount = getConstant(CountDown ? CR.getUnsignedMax() |
| 5558 : -CR.getUnsignedMin()); | 5535 : -CR.getUnsignedMin()); |
| 5559 return ExitLimit(Distance, MaxBECount); | 5536 return ExitLimit(Distance, MaxBECount); |
| 5560 } | 5537 } |
| 5561 | 5538 |
| 5562 // If the recurrence is known not to wraparound, unsigned divide computes the | 5539 // If the recurrence is known not to wraparound, unsigned divide computes the |
| 5563 // back edge count. (Ideally we would have an "isexact" bit for udiv). We know | 5540 // back edge count. We know that the value will either become zero (and thus |
| 5564 // that the value will either become zero (and thus the loop terminates), that | 5541 // the loop terminates), that the loop will terminate through some other exit |
| 5565 // the loop will terminate through some other exit condition first, or that | 5542 // condition first, or that the loop has undefined behavior. This means |
| 5566 // the loop has undefined behavior. This means we can't "miss" the exit | 5543 // we can't "miss" the exit value, even with nonunit stride. |
| 5567 // value, even with nonunit stride. | |
| 5568 // | 5544 // |
| 5569 // This is only valid for expressions that directly compute the loop exit. It | 5545 // FIXME: Prove that loops always exhibits *acceptable* undefined |
| 5570 // is invalid for subexpressions in which the loop may exit through this | 5546 // behavior. Loops must exhibit defined behavior until a wrapped value is |
| 5571 // branch even if this subexpression is false. In that case, the trip count | 5547 // actually used. So the trip count computed by udiv could be smaller than the |
| 5572 // computed by this udiv could be smaller than the number of well-defined | 5548 // number of well-defined iterations. |
| 5573 // iterations. | 5549 if (AddRec->getNoWrapFlags(SCEV::FlagNW)) { |
| 5574 if (!IsSubExpr && AddRec->getNoWrapFlags(SCEV::FlagNW)) | 5550 // FIXME: We really want an "isexact" bit for udiv. |
| 5575 return getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); | 5551 return getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); |
| 5576 | 5552 } |
| 5577 // Then, try to solve the above equation provided that Start is constant. | 5553 // Then, try to solve the above equation provided that Start is constant. |
| 5578 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) | 5554 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) |
| 5579 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), | 5555 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), |
| 5580 -StartC->getValue()->getValue(), | 5556 -StartC->getValue()->getValue(), |
| 5581 *this); | 5557 *this); |
| 5582 return getCouldNotCompute(); | 5558 return getCouldNotCompute(); |
| 5583 } | 5559 } |
| 5584 | 5560 |
| 5585 /// HowFarToNonZero - Return the number of times a backedge checking the | 5561 /// HowFarToNonZero - Return the number of times a backedge checking the |
| 5586 /// specified value for nonzero will execute. If not computable, return | 5562 /// specified value for nonzero will execute. If not computable, return |
| (...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6332 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) | 6308 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) |
| 6333 return getCouldNotCompute(); | 6309 return getCouldNotCompute(); |
| 6334 } | 6310 } |
| 6335 | 6311 |
| 6336 return getUDivExpr(Add, Step); | 6312 return getUDivExpr(Add, Step); |
| 6337 } | 6313 } |
| 6338 | 6314 |
| 6339 /// HowManyLessThans - Return the number of times a backedge containing the | 6315 /// HowManyLessThans - Return the number of times a backedge containing the |
| 6340 /// specified less-than comparison will execute. If not computable, return | 6316 /// specified less-than comparison will execute. If not computable, return |
| 6341 /// CouldNotCompute. | 6317 /// CouldNotCompute. |
| 6342 /// | |
| 6343 /// @param IsSubExpr is true when the LHS < RHS condition does not directly | |
| 6344 /// control the branch. In this case, we can only compute an iteration count for | |
| 6345 /// a subexpression that cannot overflow before evaluating true. | |
| 6346 ScalarEvolution::ExitLimit | 6318 ScalarEvolution::ExitLimit |
| 6347 ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, | 6319 ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, |
| 6348 const Loop *L, bool isSigned, | 6320 const Loop *L, bool isSigned) { |
| 6349 bool IsSubExpr) { | |
| 6350 // Only handle: "ADDREC < LoopInvariant". | 6321 // Only handle: "ADDREC < LoopInvariant". |
| 6351 if (!isLoopInvariant(RHS, L)) return getCouldNotCompute(); | 6322 if (!isLoopInvariant(RHS, L)) return getCouldNotCompute(); |
| 6352 | 6323 |
| 6353 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); | 6324 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); |
| 6354 if (!AddRec || AddRec->getLoop() != L) | 6325 if (!AddRec || AddRec->getLoop() != L) |
| 6355 return getCouldNotCompute(); | 6326 return getCouldNotCompute(); |
| 6356 | 6327 |
| 6357 // Check to see if we have a flag which makes analysis easy. | 6328 // Check to see if we have a flag which makes analysis easy. |
| 6358 bool NoWrap = false; | 6329 bool NoWrap = isSigned ? |
| 6359 if (!IsSubExpr) { | 6330 AddRec->getNoWrapFlags((SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNW)) : |
| 6360 NoWrap = AddRec->getNoWrapFlags( | 6331 AddRec->getNoWrapFlags((SCEV::NoWrapFlags)(SCEV::FlagNUW | SCEV::FlagNW)); |
| 6361 (SCEV::NoWrapFlags)(((isSigned ? SCEV::FlagNSW : SCEV::FlagNUW)) | 6332 |
| 6362 | SCEV::FlagNW)); | |
| 6363 } | |
| 6364 if (AddRec->isAffine()) { | 6333 if (AddRec->isAffine()) { |
| 6365 unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); | 6334 unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); |
| 6366 const SCEV *Step = AddRec->getStepRecurrence(*this); | 6335 const SCEV *Step = AddRec->getStepRecurrence(*this); |
| 6367 | 6336 |
| 6368 if (Step->isZero()) | 6337 if (Step->isZero()) |
| 6369 return getCouldNotCompute(); | 6338 return getCouldNotCompute(); |
| 6370 if (Step->isOne()) { | 6339 if (Step->isOne()) { |
| 6371 // With unit stride, the iteration never steps past the limit value. | 6340 // With unit stride, the iteration never steps past the limit value. |
| 6372 } else if (isKnownPositive(Step)) { | 6341 } else if (isKnownPositive(Step)) { |
| 6373 // Test whether a positive iteration can step past the limit | 6342 // Test whether a positive iteration can step past the limit |
| (...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7079 dbgs() << "SCEVValidator: SCEV for loop '" | 7048 dbgs() << "SCEVValidator: SCEV for loop '" |
| 7080 << OldI->first->getHeader()->getName() | 7049 << OldI->first->getHeader()->getName() |
| 7081 << "' changed from '" << OldI->second | 7050 << "' changed from '" << OldI->second |
| 7082 << "' to '" << NewI->second << "'!\n"; | 7051 << "' to '" << NewI->second << "'!\n"; |
| 7083 std::abort(); | 7052 std::abort(); |
| 7084 } | 7053 } |
| 7085 } | 7054 } |
| 7086 | 7055 |
| 7087 // TODO: Verify more things. | 7056 // TODO: Verify more things. |
| 7088 } | 7057 } |
| OLD | NEW |