[llvm-commits] [PATCH] Make Inits FoldingSetNodes

Chris Lattner clattner at apple.com
Thu Jul 14 09:48:03 PDT 2011


On Jul 14, 2011, at 8:13 AM, David A. Greene wrote:

>> 
>>    +const CodeInit *CodeInit::get(const std::string &V) {
>>    +  FoldingSetNodeID ID;
>>    +  ID.AddInteger(initCode);
>>    +  ID.AddString(V);
>> 
>> again.
> 
> Ok, I'm confused.  I thought FoldingSet existed to implement the
> Flyweight pattern.  That's what I'm trying to do here.  It's not enough
> to put the integer values in a (static) DenseMap and the string values
> in a (static) StringMap.  I want to get rid of the duplicate objects
> _around_ the integer and string values.

What you're implementing here is a way to unique init's.  Folding set is a map data structure that is useful when the "key" is very expensive to represent.  As such, it makes sense for vectors of arguments, but doesn't make sense for trivial things.  If you want to unique a CodeInit, just use:

StringMap<CodeInit*> TheCodeInits;

See lib/VMCore/Type.cpp which uniques llvm IR types as an example.

-Chris



More information about the llvm-commits mailing list