public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug modula2/115536] New: Expression is evaluated incorrectly when encountering relops and indirection
@ 2024-06-18 10:22 gaius at gcc dot gnu.org
2024-06-18 10:31 ` [Bug modula2/115536] " gaius at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-06-18 10:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115536
Bug ID: 115536
Summary: Expression is evaluated incorrectly when encountering
relops and indirection
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: modula2
Assignee: gaius at gcc dot gnu.org
Reporter: gaius at gcc dot gnu.org
Target Milestone: ---
The following code fails with:
$ gm2 -g testcond2.mod
$ ./a.out
failed
MODULE condtest2 ;
FROM libc IMPORT printf, exit ;
PROCEDURE test (VAR a, b, c, d: CARDINAL) ;
BEGIN
IF (a = b) # (c = d)
THEN
printf ("passed\n")
ELSE
printf ("failed\n") ;
exit (1)
END
END test ;
VAR
e, f, g, h: CARDINAL ;
BEGIN
e := 1 ;
f := 2 ;
g := 3 ;
h := 3 ;
test (e, f, g, h)
END condtest2.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug modula2/115536] Expression is evaluated incorrectly when encountering relops and indirection
2024-06-18 10:22 [Bug modula2/115536] New: Expression is evaluated incorrectly when encountering relops and indirection gaius at gcc dot gnu.org
@ 2024-06-18 10:31 ` gaius at gcc dot gnu.org
2024-06-22 17:52 ` gaius at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-06-18 10:31 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115536
Gaius Mulley <gaius at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Last reconfirmed| |2024-06-18
Status|UNCONFIRMED |ASSIGNED
Ever confirmed|0 |1
--- Comment #1 from Gaius Mulley <gaius at gcc dot gnu.org> ---
Confirmed - it appears that the gcc/m2/gm2-compiler/M2Quads.mod:BuildRelOp
procedure is generating incorrect quadruples. During processing of the
inequality it detects the two boolean sub expressions and manipulates them to
deduce the result. However the manipulation has ignored the operands c and d
require de-referencing.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug modula2/115536] Expression is evaluated incorrectly when encountering relops and indirection
2024-06-18 10:22 [Bug modula2/115536] New: Expression is evaluated incorrectly when encountering relops and indirection gaius at gcc dot gnu.org
2024-06-18 10:31 ` [Bug modula2/115536] " gaius at gcc dot gnu.org
@ 2024-06-22 17:52 ` gaius at gcc dot gnu.org
2024-06-25 17:35 ` cvs-commit at gcc dot gnu.org
2024-06-25 17:37 ` gaius at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-06-22 17:52 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115536
--- Comment #2 from Gaius Mulley <gaius at gcc dot gnu.org> ---
Created attachment 58488
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58488&action=edit
Proposed fix
Here is a proposed fix. When in a const expression the fix creates marked
boolean constvars which are resolved to TRUE and FALSE in the respective
control flow paths. The basic block and constant resolving is applied on the
current scope and one path will be removed (with the unused assignment).
When outside a const expression a temporary boolean variable is created.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug modula2/115536] Expression is evaluated incorrectly when encountering relops and indirection
2024-06-18 10:22 [Bug modula2/115536] New: Expression is evaluated incorrectly when encountering relops and indirection gaius at gcc dot gnu.org
2024-06-18 10:31 ` [Bug modula2/115536] " gaius at gcc dot gnu.org
2024-06-22 17:52 ` gaius at gcc dot gnu.org
@ 2024-06-25 17:35 ` cvs-commit at gcc dot gnu.org
2024-06-25 17:37 ` gaius at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-06-25 17:35 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115536
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Gaius Mulley <gaius@gcc.gnu.org>:
https://gcc.gnu.org/g:9f168b412f44781013401492acfedf22afe7741b
commit r15-1618-g9f168b412f44781013401492acfedf22afe7741b
Author: Gaius Mulley <gaiusmod2@gmail.com>
Date: Tue Jun 25 18:35:22 2024 +0100
PR modula2/115536 Expression is evaluated incorrectly when encountering
relops and indirection
This fix ensures that we only call BuildRelOpFromBoolean if we are
inside a constant expression (where no indirection can be used).
The fix creates a temporary variable when a boolean is created from
a relop in other cases.
The previous pattern implementation would not work if the operands required
dereferencing during non const expressions. Comparison of relop results
in a constant expression are resolved by constant propagation, basic
block analysis and dead code removal. After the quadruples have been
optimized only one assignment to the boolean variable will remain for
const expressions. All quadruple pattern checking for boolean
expressions is removed by the patch. Thus the implementation becomes
more generic.
gcc/m2/ChangeLog:
PR modula2/115536
* gm2-compiler/M2BasicBlock.def (GetBasicBlockScope): New
procedure.
(GetBasicBlockStart): Ditto.
(GetBasicBlockEnd): Ditto.
(IsBasicBlockFirst): New procedure function.
* gm2-compiler/M2BasicBlock.mod (ConvertQuads2BasicBlock): Allow
conditional boolean quads to be removed.
(GetBasicBlockScope): Implement new procedure.
(GetBasicBlockStart): Ditto.
(GetBasicBlockEnd): Ditto.
(IsBasicBlockFirst): Implement new procedure function.
* gm2-compiler/M2GCCDeclare.def (FoldConstants): New parameter
declaration.
* gm2-compiler/M2GCCDeclare.mod (FoldConstants): New parameter
declaration.
(DeclareTypesConstantsProceduresInRange): Recreate basic blocks
after resolving constant expressions.
(CodeBecomes): Guard IsVariableSSA with IsVar.
* gm2-compiler/M2GenGCC.def (ResolveConstantExpressions): New
parameter declaration.
* gm2-compiler/M2GenGCC.mod (FoldIfLess): Remove relop pattern
detection.
(FoldIfGre): Ditto.
(FoldIfLessEqu): Ditto.
(FoldIfGreEqu): Ditto.
(FoldIfIn): Ditto.
(FoldIfNotIn): Ditto.
(FoldIfEqu): Ditto.
(FoldIfNotEqu): Ditto.
(FoldBecomes): Add BasicBlock parameter and allow conditional
boolean becomes to be folded in the first basic block.
(ResolveConstantExpressions): Reimplement.
* gm2-compiler/M2Quads.def (IsConstQuad): New procedure function.
(IsConditionalBooleanQuad): Ditto.
* gm2-compiler/M2Quads.mod (IsConstQuad): Implement new procedure
function.
(IsConditionalBooleanQuad): Ditto.
(MoveWithMode): Use GenQuadOTypetok.
(IsInitialisingConst): Rewrite using OpUsesOp1.
(OpUsesOp1): New procedure function.
(doBuildAssignment): Mark des as a VarConditional.
(ConvertBooleanToVariable): Call PutVarConditional.
(DumpQuadSummary): New procedure.
(BuildRelOpFromBoolean): Updated debugging and improved comments.
(BuildRelOp): Only call BuildRelOpFromBoolean if we are in a const
expression and both operands are boolean relops.
(GenQuadOTypeUniquetok): New procedure.
(BackPatch): Correct comment.
* gm2-compiler/SymbolTable.def (PutVarConditional): New procedure.
(IsVarConditional): New procedure function.
* gm2-compiler/SymbolTable.mod (PutVarConditional): Implement new
procedure.
(IsVarConditional): Implement new procedure function.
(SymConstVar): New field IsConditional.
(SymVar): New field IsConditional.
(MakeVar): Initialize IsConditional field.
(MakeConstVar): Initialize IsConditional field.
* gm2-compiler/M2Swig.mod (DoBasicBlock): Change parameters to
use BasicBlock.
* gm2-compiler/M2Code.mod (SecondDeclareAndOptimize): Use iterator
to FoldConstants over basic block list.
* gm2-compiler/M2SymInit.mod (AppendEntry): Replace parameters
with BasicBlock.
* gm2-compiler/P3Build.bnf (Relation): Call RecordOp for #, <> and
=.
gcc/testsuite/ChangeLog:
PR modula2/115536
* gm2/iso/const/pass/constbool4.mod: New test.
* gm2/iso/const/pass/constbool5.mod: New test.
* gm2/iso/run/pass/condtest2.mod: New test.
* gm2/iso/run/pass/condtest3.mod: New test.
* gm2/iso/run/pass/condtest4.mod: New test.
* gm2/iso/run/pass/condtest5.mod: New test.
* gm2/iso/run/pass/constbool4.mod: New test.
Signed-off-by: Gaius Mulley <gaiusmod2@gmail.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug modula2/115536] Expression is evaluated incorrectly when encountering relops and indirection
2024-06-18 10:22 [Bug modula2/115536] New: Expression is evaluated incorrectly when encountering relops and indirection gaius at gcc dot gnu.org
` (2 preceding siblings ...)
2024-06-25 17:35 ` cvs-commit at gcc dot gnu.org
@ 2024-06-25 17:37 ` gaius at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: gaius at gcc dot gnu.org @ 2024-06-25 17:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115536
Gaius Mulley <gaius at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
--- Comment #4 from Gaius Mulley <gaius at gcc dot gnu.org> ---
Closing now that the patch has been applied.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-06-25 17:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-18 10:22 [Bug modula2/115536] New: Expression is evaluated incorrectly when encountering relops and indirection gaius at gcc dot gnu.org
2024-06-18 10:31 ` [Bug modula2/115536] " gaius at gcc dot gnu.org
2024-06-22 17:52 ` gaius at gcc dot gnu.org
2024-06-25 17:35 ` cvs-commit at gcc dot gnu.org
2024-06-25 17:37 ` gaius at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).