Commit 7aa4079
committed
auto merge of #15998 : luqmana/rust/nmnnbd, r=thestinger
LLVM recently added a new attribute, dereferenceable: http://reviews.llvm.org/D4449
>This patch adds a dereferencable attribute. In some sense, this is a companion to the nonnull attribute, but specifies that the pointer is known to be dereferencable in the same sense as a pointer generated by alloca is known to be dereferencable.
With rust, everywhere that we previously marked `nonnull` we can actually mark as `dereferenceable` (which implies nonnull) since we know the size. That is, except for one case: when generating calls for TyVisitor. It seems like we haven't substituted the self type (so we have `ty_param`) and just treat it as an opaque pointer so I just left that bit as nonnull.
With this, LLVM can for example hoist a load out of a loop where it previously couldn't:
```Rust
pub fn baz(c: &uint, n: uint) -> uint {
let mut res = 0;
for i in range(0, n) {
if i > 0 {
res += *c * i;
}
}
res
}
```
Before:
```llvm
define i64 @baz(i64* noalias nocapture nonnull readonly, i64) unnamed_addr #0 {
entry-block:
br label %for_loopback.outer
for_loopback.outer: ; preds = %then-block-33-, %entry-block
%.ph = phi i64 [ %.lcssa, %then-block-33- ], [ 0, %entry-block ]
%res.0.ph = phi i64 [ %8, %then-block-33- ], [ 0, %entry-block ]
br label %for_loopback
for_exit: ; preds = %for_loopback
%res.0.ph.lcssa = phi i64 [ %res.0.ph, %for_loopback ]
ret i64 %res.0.ph.lcssa
for_loopback: ; preds = %for_loopback.outer, %for_body
%2 = phi i64 [ %4, %for_body ], [ %.ph, %for_loopback.outer ]
%3 = icmp ult i64 %2, %1
br i1 %3, label %for_body, label %for_exit
for_body: ; preds = %for_loopback
%4 = add i64 %2, 1
%5 = icmp eq i64 %2, 0
br i1 %5, label %for_loopback, label %then-block-33-
then-block-33-: ; preds = %for_body
%.lcssa = phi i64 [ %4, %for_body ]
%.lcssa15 = phi i64 [ %2, %for_body ]
%6 = load i64* %0, align 8 ; <------- this load
%7 = mul i64 %6, %.lcssa15
%8 = add i64 %7, %res.0.ph
br label %for_loopback.outer
}
```
After:
```llvm
define i64 @baz(i64* noalias nocapture readonly dereferenceable(8), i64) unnamed_addr #0 {
entry-block:
%2 = load i64* %0, align 8 ; <------- load once instead
br label %for_loopback.outer
for_loopback.outer: ; preds = %then-block-33-, %entry-block
%.ph = phi i64 [ %.lcssa, %then-block-33- ], [ 0, %entry-block ]
%res.0.ph = phi i64 [ %8, %then-block-33- ], [ 0, %entry-block ]
br label %for_loopback
for_exit: ; preds = %for_loopback
%res.0.ph.lcssa = phi i64 [ %res.0.ph, %for_loopback ]
ret i64 %res.0.ph.lcssa
for_loopback: ; preds = %for_loopback.outer, %for_body
%3 = phi i64 [ %5, %for_body ], [ %.ph, %for_loopback.outer ]
%4 = icmp ult i64 %3, %1
br i1 %4, label %for_body, label %for_exit
for_body: ; preds = %for_loopback
%5 = add i64 %3, 1
%6 = icmp eq i64 %3, 0
br i1 %6, label %for_loopback, label %then-block-33-
then-block-33-: ; preds = %for_body
%.lcssa = phi i64 [ %5, %for_body ]
%.lcssa15 = phi i64 [ %3, %for_body ]
%7 = mul i64 %2, %.lcssa15
%8 = add i64 %7, %res.0.ph
br label %for_loopback.outer
}
```File tree
10 files changed
+237
-81
lines changed- src
- librustc_llvm
- librustc/middle/trans
- rustllvm
10 files changed
+237
-81
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
| 284 | + | |
289 | 285 | | |
290 | 286 | | |
291 | 287 | | |
| |||
962 | 958 | | |
963 | 959 | | |
964 | 960 | | |
965 | | - | |
| 961 | + | |
966 | 962 | | |
967 | 963 | | |
968 | 964 | | |
| |||
975 | 971 | | |
976 | 972 | | |
977 | 973 | | |
978 | | - | |
| 974 | + | |
979 | 975 | | |
980 | 976 | | |
981 | 977 | | |
| |||
1081 | 1077 | | |
1082 | 1078 | | |
1083 | 1079 | | |
1084 | | - | |
| 1080 | + | |
1085 | 1081 | | |
1086 | 1082 | | |
1087 | 1083 | | |
| |||
1095 | 1091 | | |
1096 | 1092 | | |
1097 | 1093 | | |
1098 | | - | |
| 1094 | + | |
1099 | 1095 | | |
1100 | 1096 | | |
1101 | 1097 | | |
| |||
1111 | 1107 | | |
1112 | 1108 | | |
1113 | 1109 | | |
1114 | | - | |
| 1110 | + | |
1115 | 1111 | | |
1116 | 1112 | | |
1117 | 1113 | | |
| |||
1156 | 1152 | | |
1157 | 1153 | | |
1158 | 1154 | | |
1159 | | - | |
| 1155 | + | |
1160 | 1156 | | |
1161 | 1157 | | |
1162 | 1158 | | |
| |||
2040 | 2036 | | |
2041 | 2037 | | |
2042 | 2038 | | |
2043 | | - | |
| 2039 | + | |
2044 | 2040 | | |
2045 | 2041 | | |
2046 | 2042 | | |
| |||
2056 | 2052 | | |
2057 | 2053 | | |
2058 | 2054 | | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
2059 | 2061 | | |
2060 | 2062 | | |
2061 | 2063 | | |
2062 | 2064 | | |
2063 | | - | |
| 2065 | + | |
2064 | 2066 | | |
2065 | 2067 | | |
2066 | | - | |
2067 | | - | |
2068 | | - | |
2069 | | - | |
2070 | | - | |
2071 | | - | |
2072 | 2068 | | |
2073 | 2069 | | |
2074 | 2070 | | |
2075 | 2071 | | |
2076 | 2072 | | |
2077 | | - | |
| 2073 | + | |
2078 | 2074 | | |
2079 | 2075 | | |
2080 | | - | |
2081 | | - | |
2082 | | - | |
2083 | | - | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
2084 | 2082 | | |
2085 | 2083 | | |
2086 | 2084 | | |
| |||
2094 | 2092 | | |
2095 | 2093 | | |
2096 | 2094 | | |
2097 | | - | |
| 2095 | + | |
2098 | 2096 | | |
2099 | 2097 | | |
2100 | 2098 | | |
2101 | 2099 | | |
2102 | | - | |
| 2100 | + | |
2103 | 2101 | | |
2104 | 2102 | | |
2105 | 2103 | | |
2106 | 2104 | | |
2107 | 2105 | | |
2108 | 2106 | | |
2109 | | - | |
2110 | | - | |
| 2107 | + | |
| 2108 | + | |
| 2109 | + | |
2111 | 2110 | | |
2112 | 2111 | | |
2113 | 2112 | | |
2114 | 2113 | | |
2115 | 2114 | | |
2116 | 2115 | | |
2117 | | - | |
| 2116 | + | |
2118 | 2117 | | |
2119 | 2118 | | |
2120 | 2119 | | |
| |||
2124 | 2123 | | |
2125 | 2124 | | |
2126 | 2125 | | |
| 2126 | + | |
| 2127 | + | |
2127 | 2128 | | |
2128 | 2129 | | |
2129 | 2130 | | |
2130 | | - | |
2131 | | - | |
2132 | | - | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
2133 | 2134 | | |
| 2135 | + | |
2134 | 2136 | | |
2135 | | - | |
| 2137 | + | |
2136 | 2138 | | |
| 2139 | + | |
2137 | 2140 | | |
2138 | | - | |
2139 | | - | |
2140 | | - | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
2141 | 2164 | | |
| 2165 | + | |
2142 | 2166 | | |
2143 | 2167 | | |
2144 | 2168 | | |
2145 | 2169 | | |
2146 | 2170 | | |
2147 | | - | |
2148 | | - | |
| 2171 | + | |
| 2172 | + | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
2149 | 2176 | | |
2150 | 2177 | | |
2151 | | - | |
| 2178 | + | |
2152 | 2179 | | |
2153 | 2180 | | |
2154 | 2181 | | |
2155 | 2182 | | |
| 2183 | + | |
2156 | 2184 | | |
2157 | 2185 | | |
2158 | | - | |
2159 | | - | |
2160 | | - | |
| 2186 | + | |
| 2187 | + | |
| 2188 | + | |
| 2189 | + | |
2161 | 2190 | | |
2162 | | - | |
2163 | | - | |
2164 | | - | |
| 2191 | + | |
| 2192 | + | |
| 2193 | + | |
| 2194 | + | |
| 2195 | + | |
| 2196 | + | |
2165 | 2197 | | |
2166 | 2198 | | |
2167 | 2199 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| |||
681 | 681 | | |
682 | 682 | | |
683 | 683 | | |
684 | | - | |
| 684 | + | |
685 | 685 | | |
686 | 686 | | |
687 | 687 | | |
688 | 688 | | |
689 | 689 | | |
690 | | - | |
| 690 | + | |
691 | 691 | | |
692 | 692 | | |
693 | 693 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
| 158 | + | |
159 | 159 | | |
160 | 160 | | |
161 | 161 | | |
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
177 | | - | |
178 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
179 | 180 | | |
180 | 181 | | |
181 | 182 | | |
| |||
777 | 778 | | |
778 | 779 | | |
779 | 780 | | |
780 | | - | |
| 781 | + | |
781 | 782 | | |
782 | 783 | | |
783 | 784 | | |
| |||
802 | 803 | | |
803 | 804 | | |
804 | 805 | | |
805 | | - | |
| 806 | + | |
806 | 807 | | |
807 | 808 | | |
808 | 809 | | |
809 | 810 | | |
810 | | - | |
| 811 | + | |
811 | 812 | | |
812 | 813 | | |
813 | 814 | | |
| |||
820 | 821 | | |
821 | 822 | | |
822 | 823 | | |
823 | | - | |
824 | | - | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
825 | 827 | | |
826 | 828 | | |
827 | 829 | | |
828 | 830 | | |
829 | 831 | | |
830 | 832 | | |
831 | | - | |
| 833 | + | |
832 | 834 | | |
833 | 835 | | |
834 | 836 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
588 | 588 | | |
589 | 589 | | |
590 | 590 | | |
591 | | - | |
| 591 | + | |
592 | 592 | | |
593 | 593 | | |
594 | 594 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
553 | 553 | | |
554 | 554 | | |
555 | 555 | | |
556 | | - | |
| 556 | + | |
557 | 557 | | |
558 | 558 | | |
559 | 559 | | |
| |||
0 commit comments