[PATCH] SimplifyCFG: turn recursive GatherConstantCompares into iterative

Mehdi Amini mehdi.amini at apple.com
Mon Nov 17 13:40:28 PST 2014


> On Nov 17, 2014, at 12:24 PM, Michael Ilseman <milseman at apple.com> wrote:
> 
> I like the change, but have a few minor comments:
> I would replace the stack with a SmallVector, as this code tends to return early once we start exceeding 8 entries (else the Span is too large).

It is not exactly the case, the limit for the Span is for a single comparison. We can have an infinite sequence of or/and chained.
However it is true that the common case it probably a short chain. So I replaced the stack with a SmallVector, but the vector of matched value (that will be turned as the cases of the switch) as well.


> 
> +// Try to match Instruction I as a comparison against a constant and populates
> +// Vals with the set of value that match (or does not depending on isEQ).
> +// Return nullptr on failure, or return the Value the comparison matched against
> +// on success
> +static Value* GatherConstantComparesMatch(Instruction *I,
> +                                          Value *CurrValue,
> 
> It took me a while of reading the code to realize what CurrValue as a parameter (as opposed to a return value) is for. You could briefly mention it in the comment before the function. It also seems like CurrValue, if set, will also always be the return value on success. Does it make sense to instead have that book-keeping be done in the caller of GatherConstantComparesMatch? Also, why does GatherConstantComparesMatch take in Extra? 

I add a comment, I can’t keep it out of the function because it changes the control flow inside.
I removed Extra, it was unused, well spotted!

Attached is the update patch.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-SimplifyCFG-turn-recursive-GatherConstantCompares-in.patch
Type: application/octet-stream
Size: 11412 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141117/8734fd76/attachment.obj>
-------------- next part --------------



Mehdi



> 
> 
>> On Nov 17, 2014, at 11:25 AM, Mehdi Amini <mehdi.amini at apple.com> wrote:
>> 
>> Hi all,
>> 
>> A long sequence of || or && of integer comparison against the same value is turned into a switch in SimplifyCFG.
>> 
>> The implementation was recursive and could lead to a stack explosion. I turned it into an iterative implementation in this patch.
>> 
>> As a side question, the existing code assumes that the “icmp" are always taking the constant as the second operand (it never tries to match the first operand). Is it something that is canonicalized in the IR and can be assumed here?
>> 
>> Thanks,
>> 
>> Mehdi
>> 
>> 
>> <0001-SimplifyCFG-turn-recursive-GatherConstantCompares-in.patch>_______________________________________________
>> llvm-commits mailing list
>> llvm-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 



More information about the llvm-commits mailing list