mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-09-13 13:49:17 +02:00
* optimize x < length(arr) and x >= length(arr) as proposed in #40292
This commit is contained in:
parent
b0076a4709
commit
e130557d9f
@ -1739,6 +1739,31 @@ implementation
|
||||
;
|
||||
end;
|
||||
|
||||
{
|
||||
compile x < length(arr) as x <= high(arr)
|
||||
compile x >= length(arr) as x > high(arr)
|
||||
|
||||
tested by tests/webtbs/tw40292.pp
|
||||
}
|
||||
if (nodetype in [ltn,gten]) and
|
||||
(right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_length_x) and
|
||||
((is_dynamic_array(tinlinenode(right).left.resultdef)) or
|
||||
(is_open_array(tinlinenode(right).left.resultdef))
|
||||
) then
|
||||
begin
|
||||
case nodetype of
|
||||
ltn:
|
||||
result:=caddnode.create(lten,left,cinlinenode.create(in_high_x,false,tinlinenode(right).left));
|
||||
gten:
|
||||
result:=caddnode.create(gtn,left,cinlinenode.create(in_high_x,false,tinlinenode(right).left));
|
||||
else
|
||||
Internalerror(2024041701);
|
||||
end;
|
||||
left:=nil;
|
||||
tinlinenode(right).left:=nil;
|
||||
exit;
|
||||
end;
|
||||
|
||||
{ using sqr(x) for reals instead of x*x might reduces register pressure and/or
|
||||
memory accesses while sqr(<real>) has no drawback }
|
||||
if
|
||||
|
12
tests/webtbs/tw40292.pp
Normal file
12
tests/webtbs/tw40292.pp
Normal file
@ -0,0 +1,12 @@
|
||||
var
|
||||
arr: array of longint;
|
||||
x: longint;
|
||||
begin
|
||||
x:=12+random(0);
|
||||
setlength(arr,13+random(0));
|
||||
if (x < length(arr)) <> (x <= high(arr)) then
|
||||
halt(1);
|
||||
x:=13+random(0);
|
||||
if (x >= length(arr)) <> (x > high(arr)) then
|
||||
halt(2);
|
||||
end.
|
Loading…
Reference in New Issue
Block a user