[PATCH] CodeGenPrep: rewrite a few loops in C++11 style

Duncan P. N. Exon Smith dexonsmith at apple.com
Wed Jan 7 15:31:43 PST 2015


> On 2015-Jan-07, at 15:23, Ramkumar Ramachandra <artagnon at gmail.com> wrote:
> 
> Sean Silva wrote:
>>> It depends on what you want to construct from the iteration.
>> 
>> Why did you choose Function::iterator instead of BasicBlock &?
> 
> Honestly? Because this doesn't work:
> 
>    for (BasicBlock &BB : F)
>      EverMadeChange |= bypassSlowDivision(F, &BB, BypassWidths);
> 
> I'm going to refactor the other two hunks to do BasicBlock and revert
> this hunk, before resubmitting.

Interesting.

Looking into why, it's because `bypassSlowDivision()` takes a reference
to a `Function::iterator`, which it's going to modify.  Eventually it
gets to here in `insertFastDiv()`:

    // Basic Block is split before divide
    BasicBlock *MainBB = I;
    BasicBlock *SuccessorBB = I->splitBasicBlock(J);
    ++I; //advance iterator I to successorBB

So a range-based for loop changes behaviour here, and is likely
incorrect.



More information about the llvm-commits mailing list