llvm.org GIT mirror llvm / 614fef6
Make SCEV's brute force analysis stronger in two ways. Firstly, we should be able to constant fold load instructions where the argument is a constant. Second, we should be able to watch multiple PHI nodes through the loop; this patch only supports PHIs in loop headers, more can be done here. With this patch, we now constant evaluate: static const int arr[] = {1, 2, 3, 4, 5}; int test() { int sum = 0; for (int i = 0; i < 5; ++i) sum += arr[i]; return sum; } git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142731 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 8 years ago
2 changed file(s) with 179 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
46574657 /// specified type, assuming that all operands were constants.
46584658 static bool CanConstantFold(const Instruction *I) {
46594659 if (isa(I) || isa(I) ||
4660 isa(I) || isa(I) || isa(I))
4660 isa(I) || isa(I) || isa(I) ||
4661 isa(I))
46614662 return true;
46624663
46634664 if (const CallInst *CI = dyn_cast(I))
47504751 const TargetData *TD) {
47514752 // Convenient constant check, but redundant for recursive calls.
47524753 if (Constant *C = dyn_cast(V)) return C;
4753
4754 Instruction *I = cast(V);
4754 Instruction *I = dyn_cast(V);
4755 if (!I) return 0;
4756
47554757 if (Constant *C = Vals.lookup(I)) return C;
47564758
4757 assert(!isa(I) && "loop header phis should be mapped to constant");
4758 assert(canConstantEvolve(I, L) && "cannot evaluate expression in this loop");
4759 (void)L;
4759 // An instruction inside the loop depends on a value outside the loop that we
4760 // weren't given a mapping for, or a value such as a call inside the loop.
4761 if (!canConstantEvolve(I, L)) return 0;
4762
4763 // An unmapped PHI can be due to a branch or another loop inside this loop,
4764 // or due to this not being the initial iteration through a loop where we
4765 // couldn't compute the evolution of this particular PHI last time.
4766 if (isa(I)) return 0;
47604767
47614768 std::vector Operands(I->getNumOperands());
47624769
47734780 Operands[i] = C;
47744781 }
47754782
4776 if (const CmpInst *CI = dyn_cast(I))
4783 if (CmpInst *CI = dyn_cast(I))
47774784 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
47784785 Operands[1], TD);
4786 if (LoadInst *LI = dyn_cast(I)) {
4787 if (!LI->isVolatile())
4788 return ConstantFoldLoadFromConstPtr(Operands[0], TD);
4789 }
47794790 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Operands, TD);
47804791 }
47814792
47974808
47984809 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
47994810
4800 // FIXME: Nick's fix for PR11034 will seed constants for multiple header phis.
48014811 DenseMap CurrentIterVals;
4812 BasicBlock *Header = L->getHeader();
4813 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
48024814
48034815 // Since the loop is canonicalized, the PHI node must have two entries. One
48044816 // entry must be a constant (coming in from outside of the loop), and the
48054817 // second must be derived from the same PHI.
48064818 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
4807 Constant *StartCST =
4808 dyn_cast(PN->getIncomingValue(!SecondIsBackedge));
4809 if (StartCST == 0)
4810 return RetVal = 0; // Must be a constant.
4811 CurrentIterVals[PN] = StartCST;
4819 PHINode *PHI = 0;
4820 for (BasicBlock::iterator I = Header->begin();
4821 (PHI = dyn_cast(I)); ++I) {
4822 Constant *StartCST =
4823 dyn_cast(PHI->getIncomingValue(!SecondIsBackedge));
4824 if (StartCST == 0) continue;
4825 CurrentIterVals[PHI] = StartCST;
4826 }
4827 if (!CurrentIterVals.count(PN))
4828 return RetVal = 0;
48124829
48134830 Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
4814 if (getConstantEvolvingPHI(BEValue, L) != PN &&
4815 !isa(BEValue))
4816 return RetVal = 0; // Not derived from same PHI.
48174831
48184832 // Execute the loop symbolically to determine the exit value.
48194833 if (BEs.getActiveBits() >= 32)
48254839 if (IterationNum == NumIterations)
48264840 return RetVal = CurrentIterVals[PN]; // Got exit value!
48274841
4828 // Compute the value of the PHI node for the next iteration.
4842 // Compute the value of the PHIs for the next iteration.
48294843 // EvaluateExpression adds non-phi values to the CurrentIterVals map.
4844 DenseMap NextIterVals;
48304845 Constant *NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
48314846 if (NextPHI == CurrentIterVals[PN])
48324847 return RetVal = NextPHI; // Stopped evolving!
48334848 if (NextPHI == 0)
48344849 return 0; // Couldn't evaluate!
4835 DenseMap NextIterVals;
48364850 NextIterVals[PN] = NextPHI;
4851
4852 // Also evaluate the other PHI nodes. However, we don't get to stop if we
4853 // cease to be able to evaluate one of them or if they stop evolving,
4854 // because that doesn't necessarily prevent us from computing PN.
4855 for (DenseMap::const_iterator
4856 I = CurrentIterVals.begin(), E = CurrentIterVals.end(); I != E; ++I){
4857 PHINode *PHI = dyn_cast(I->first);
4858 if (!PHI || PHI == PN) continue;
4859 Constant *&NextPHI = NextIterVals[PHI];
4860 if (NextPHI) continue; // Already computed!
4861
4862 Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4863 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD);
4864 }
48374865 CurrentIterVals.swap(NextIterVals);
48384866 }
48394867 }
48434871 /// try to evaluate a few iterations of the loop until we get the exit
48444872 /// condition gets a value of ExitWhen (true or false). If we cannot
48454873 /// evaluate the trip count of the loop, return getCouldNotCompute().
4846 const SCEV * ScalarEvolution::ComputeExitCountExhaustively(const Loop *L,
4847 Value *Cond,
4848 bool ExitWhen) {
4874 const SCEV *ScalarEvolution::ComputeExitCountExhaustively(const Loop *L,
4875 Value *Cond,
4876 bool ExitWhen) {
48494877 PHINode *PN = getConstantEvolvingPHI(Cond, L);
48504878 if (PN == 0) return getCouldNotCompute();
48514879
49184946 const SCEV *C = computeSCEVAtScope(V, L);
49194947 ValuesAtScopes[V][L] = C;
49204948 return C;
4949 }
4950
4951 /// This builds up a Constant using the ConstantExpr interface. That way, we
4952 /// will return Constants for objects which aren't represented by a
4953 /// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
4954 /// Returns NULL if the SCEV isn't representable as a Constant.
4955 static Constant *BuildConstantFromSCEV(const SCEV *V) {
4956 switch (V->getSCEVType()) {
4957 default: // TODO: smax, umax.
4958 case scCouldNotCompute:
4959 case scAddRecExpr:
4960 break;
4961 case scConstant:
4962 return cast(V)->getValue();
4963 case scUnknown:
4964 return dyn_cast(cast(V)->getValue());
4965 case scSignExtend: {
4966 const SCEVSignExtendExpr *SS = cast(V);
4967 if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
4968 return ConstantExpr::getSExt(CastOp, SS->getType());
4969 break;
4970 }
4971 case scZeroExtend: {
4972 const SCEVZeroExtendExpr *SZ = cast(V);
4973 if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
4974 return ConstantExpr::getZExt(CastOp, SZ->getType());
4975 break;
4976 }
4977 case scTruncate: {
4978 const SCEVTruncateExpr *ST = cast(V);
4979 if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
4980 return ConstantExpr::getTrunc(CastOp, ST->getType());
4981 break;
4982 }
4983 case scAddExpr: {
4984 const SCEVAddExpr *SA = cast(V);
4985 if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
4986 if (C->getType()->isPointerTy())
4987 C = ConstantExpr::getBitCast(C, Type::getInt8PtrTy(C->getContext()));
4988 for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
4989 Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
4990 if (!C2) return 0;
4991
4992 // First pointer!
4993 if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
4994 std::swap(C, C2);
4995 // The offsets have been converted to bytes. We can add bytes to an
4996 // i8* by GEP with the byte count in the first index.
4997 C = ConstantExpr::getBitCast(C,Type::getInt8PtrTy(C->getContext()));
4998 }
4999
5000 // Don't bother trying to sum two pointers. We probably can't
5001 // statically compute a load that results from it anyway.
5002 if (C2->getType()->isPointerTy())
5003 return 0;
5004
5005 if (C->getType()->isPointerTy()) {
5006 if (cast(C->getType())->getElementType()->isStructTy())
5007 C2 = ConstantExpr::getIntegerCast(
5008 C2, Type::getInt32Ty(C->getContext()), true);
5009 C = ConstantExpr::getGetElementPtr(C, C2);
5010 } else
5011 C = ConstantExpr::getAdd(C, C2);
5012 }
5013 return C;
5014 }
5015 break;
5016 }
5017 case scMulExpr: {
5018 const SCEVMulExpr *SM = cast(V);
5019 if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
5020 // Don't bother with pointers at all.
5021 if (C->getType()->isPointerTy()) return 0;
5022 for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
5023 Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
5024 if (!C2 || C2->getType()->isPointerTy()) return 0;
5025 C = ConstantExpr::getMul(C, C2);
5026 }
5027 return C;
5028 }
5029 break;
5030 }
5031 case scUDivExpr: {
5032 const SCEVUDivExpr *SU = cast(V);
5033 if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
5034 if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
5035 if (LHS->getType() == RHS->getType())
5036 return ConstantExpr::getUDiv(LHS, RHS);
5037 break;
5038 }
5039 }
5040 return 0;
49215041 }
49225042
49235043 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
49725092 const SCEV *OpV = getSCEVAtScope(OrigV, L);
49735093 MadeImprovement |= OrigV != OpV;
49745094
4975 Constant *C = 0;
4976 if (const SCEVConstant *SC = dyn_cast(OpV))
4977 C = SC->getValue();
4978 if (const SCEVUnknown *SU = dyn_cast(OpV))
4979 C = dyn_cast(SU->getValue());
5095 Constant *C = BuildConstantFromSCEV(OpV);
49805096 if (!C) return V;
49815097 if (C->getType() != Op->getType())
49825098 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
49925108 if (const CmpInst *CI = dyn_cast(I))
49935109 C = ConstantFoldCompareInstOperands(CI->getPredicate(),
49945110 Operands[0], Operands[1], TD);
4995 else
5111 else if (const LoadInst *LI = dyn_cast(I)) {
5112 if (!LI->isVolatile())
5113 C = ConstantFoldLoadFromConstPtr(Operands[0], TD);
5114 } else
49965115 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
49975116 Operands, TD);
49985117 if (!C) return V;
0 ; RUN: opt -analyze -scalar-evolution < %s 2>&1 | FileCheck %s
1 ; PR11034
2
3 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32"
4 target triple = "i386-pc-linux-gnu"
5
6 @arr1 = internal unnamed_addr constant [50 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 32, i32 33, i32 34, i32 35, i32 36, i32 37, i32 38, i32 39, i32 40, i32 41, i32 42, i32 43, i32 44, i32 45, i32 46, i32 47, i32 48, i32 49, i32 50], align 4
7 @arr2 = internal unnamed_addr constant [50 x i32] [i32 49, i32 48, i32 47, i32 46, i32 45, i32 44, i32 43, i32 42, i32 41, i32 40, i32 39, i32 38, i32 37, i32 36, i32 35, i32 34, i32 33, i32 32, i32 31, i32 30, i32 29, i32 28, i32 27, i32 26, i32 25, i32 24, i32 23, i32 22, i32 21, i32 20, i32 19, i32 18, i32 17, i32 16, i32 15, i32 14, i32 13, i32 12, i32 11, i32 10, i32 9, i32 8, i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0], align 4
8
9 define i32 @test1() nounwind readnone {
10 ; CHECK: test1
11 entry:
12 br label %for.body
13
14 for.body: ; preds = %entry, %for.body
15 %sum.04 = phi i32 [ 0, %entry ], [ %add2, %for.body ]
16 ; CHECK: --> %sum.04{{ *}}Exits: 2450
17 %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
18 %arrayidx = getelementptr inbounds [50 x i32]* @arr1, i32 0, i32 %i.03
19 %0 = load i32* %arrayidx, align 4
20 ; CHECK: --> %0{{ *}}Exits: 50
21 %arrayidx1 = getelementptr inbounds [50 x i32]* @arr2, i32 0, i32 %i.03
22 %1 = load i32* %arrayidx1, align 4
23 ; CHECK: --> %1{{ *}}Exits: 0
24 %add = add i32 %0, %sum.04
25 %add2 = add i32 %add, %1
26 %inc = add nsw i32 %i.03, 1
27 %cmp = icmp eq i32 %inc, 50
28 br i1 %cmp, label %for.end, label %for.body
29
30 for.end: ; preds = %for.body
31 ret i32 %add2
32 }