llvm.org GIT mirror llvm / 630d85a
Use value ranges to fold ext(trunc) in SCEV when possible. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124062 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 9 years ago
2 changed file(s) with 68 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
897897 void *IP = 0;
898898 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
899899
900 // zext(trunc(x)) --> zext(x) or x or trunc(x)
901 if (const SCEVTruncateExpr *ST = dyn_cast(Op)) {
902 // It's possible the bits taken off by the truncate were all zero bits. If
903 // so, we should be able to simplify this further.
904 const SCEV *X = ST->getOperand();
905 ConstantRange CR = getUnsignedRange(X);
906 unsigned OrigBits = CR.getBitWidth();
907 unsigned TruncBits = getTypeSizeInBits(ST->getType());
908 unsigned NewBits = getTypeSizeInBits(Ty);
909 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
910 CR.zextOrTrunc(NewBits))) {
911 if (NewBits > OrigBits) return getZeroExtendExpr(X, Ty);
912 if (NewBits < OrigBits) return getTruncateExpr(X, Ty);
913 return X;
914 }
915 }
916
900917 // If the input value is a chrec scev, and we can prove that the value
901918 // did not overflow the old, smaller, value, we can zero extend all of the
902919 // operands (often constants). This allows analysis of something like
10371054 // If the input value is provably positive, build a zext instead.
10381055 if (isKnownNonNegative(Op))
10391056 return getZeroExtendExpr(Op, Ty);
1057
1058 // sext(trunc(x)) --> sext(x) or x or trunc(x)
1059 if (const SCEVTruncateExpr *ST = dyn_cast(Op)) {
1060 // It's possible the bits taken off by the truncate were all sign bits. If
1061 // so, we should be able to simplify this further.
1062 const SCEV *X = ST->getOperand();
1063 ConstantRange CR = getSignedRange(X);
1064 unsigned OrigBits = CR.getBitWidth();
1065 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1066 unsigned NewBits = getTypeSizeInBits(Ty);
1067 if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1068 CR.sextOrTrunc(NewBits))) {
1069 if (NewBits > OrigBits) return getSignExtendExpr(X, Ty);
1070 if (NewBits < OrigBits) return getTruncateExpr(X, Ty);
1071 return X;
1072 }
1073 }
10401074
10411075 // If the input value is a chrec scev, and we can prove that the value
10421076 // did not overflow the old, smaller, value, we can sign extend all of the
2121 ; CHECK: (3 * %x)
2222 ret i8 %C
2323 }
24
25 define void @test4(i32 %x) {
26 entry:
27 %0 = icmp sge i32 %x, 0
28 br i1 %0, label %loop, label %exit
29 loop:
30 %A = phi i32 [0, %entry], [%I, %loop]
31 %rand = icmp sgt i32 %A, 10
32 %Z = select i1 %rand, i32 %A, i32 10
33 %B = trunc i32 %Z to i16
34 %C = sext i16 %B to i30
35 ; CHECK: %C =
36 ; CHECK-NEXT: (trunc i32 (10 smax {0,+,1}<%loop>) to i30)
37 %D = sext i16 %B to i32
38 ; CHECK: %D =
39 ; CHECK-NEXT: (10 smax {0,+,1}<%loop>)
40 %E = sext i16 %B to i34
41 ; CHECK: %E =
42 ; CHECK-NEXT: (zext i32 (10 smax {0,+,1}<%loop>) to i34)
43 %F = zext i16 %B to i30
44 ; CHECK: %F =
45 ; CHECK-NEXT: (trunc i32 (10 smax {0,+,1}<%loop>) to i30
46 %G = zext i16 %B to i32
47 ; CHECK: %G =
48 ; CHECK-NEXT: (10 smax {0,+,1}<%loop>)
49 %H = zext i16 %B to i34
50 ; CHECK: %H =
51 ; CHECK-NEXT: (zext i32 (10 smax {0,+,1}<%loop>) to i34)
52 %I = add i32 %A, 1
53 %1 = icmp ne i32 %A, 20
54 br i1 %1, label %loop, label %exit
55 exit:
56 ret void
57 }