<div dir="ltr"><div>While working on a project I wound up generating a fairly large lookup table (10k entries) of callbacks inside of a static constructor. Clang was taking upwards of ~10 minutes to compile the lookup table with -O3. I generated a smaller test case (<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__www.inolen.com_static-5Finitializer-5Ftest.ll&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=mP4_rARprE2JxjgHYSuMn4VOptZVP74x6FIY7itOCMo&s=-TwgpuS_60ogS2loxUqgEx_mRdPl0b_5OMor9CHHw7o&e=">http://www.inolen.com/static_initializer_test.ll</a>) and running it with -ftime-report pointed fingers at MemCpyOptimizer and GlobalOpt.</div><div><br></div><div>Running memcpyopt through opt took around ~1 minute. The culprit was MemCpyOptimizer insertion sorting the ranges as it discovered them. I changed this up such that ranges are always appended to the list, and once they've all been scanned they're sorted and merged (n log n vs n^2).</div><div><br></div><div>Running globalopt took around ~9 minutes. The slowdown came from how GlobalOpt merged stores from static constructors individually into the global initializer in EvaluateStaticConstructor. For each store it discovered and wanted to commit, it would copy the existing global initializer and then merge in the store. I changed this so that stores are now grouped by global, and sorted from most significant to least significant by their GEP indexes (e.g. a store to GEP 0, 0 comes before GEP 0, 0, 1). With this representation, the existing initializer can be copied and all new stores merged into it in a single pass.<br></div><div><br></div><div>In the end, the lookup table that was taking ~10 minutes to compile now compiles in around 5 seconds on my machine. I've ran 'make check' and the test-suite project, which all passed. With that said however, I'm not entirely confident in my logic, especially in the globalopt changes. Please review carefully.</div><div><br></div><div>Cheers,</div><div>Anthony</div><div><br></div></div>