| Issue |
175565
|
| Summary |
for loop behaves like do .. while
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
popescun
|
## What
Current implementation of [ForExprAST](https://github.com/llvm/llvm-project/blob/main/llvm/examples/Kaleidoscope/Chapter8/toy.cpp#L910) behaves like do .. while, the body is executed always at least once, which is incorrect.
## Why
The end condition is [checked](https://github.com/llvm/llvm-project/blob/main/llvm/examples/Kaleidoscope/Chapter8/toy.cpp#L977) in the loop basic block after the body _expression_.
## How
We need to check the condition before the body is executed. A possible fix is provided in [ir_code_generator.cpp](https://github.com/popescun/llvm-kaleidoscope/blob/main/ir_code_generator.cpp#L480), where body _expression_ is moved to a separate basic block. Now the emitted code looks as:
```cpp
/**
* Output for-oop as:
* entry:
* var = alloca double
* start = startexpr
* store start -> var
* goto loop
*
* loop:
* curvar = load var
* endcond = endxpr
* br endcond, body, afterloop
*
* body:
* bodyexpr
* step = stepexpr
* nextvar = curvar + step
* store nextvar -> var
* br endcond, loop, afterloop
*
* afterlopp:
* return 0.0
*/
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs