[PATCH] D59926: Constant folding sometimes creates large constants even though a smaller constant is used multiple times

Ramakota Reddy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 28 04:54:01 PDT 2019


ramred01 created this revision.
ramred01 added a reviewer: spatel.
Herald added subscribers: jdoerfert, mgorny.

Sometimes, the constant folding pass creates a large constant from smaller constants, even though the smaller constant is used multiple times and hence can benefit from materializing the constant once and reusing that for multiple operations instead of creating a new large constant and trying to materialize both the constants into two registers.

The simple test case is as follows:

int test(int A, int B) {

  int t = A * B >> 8;
  return ((t <= 4096) ? t : 4096);

}

Here, the right shift by 8 is folded into the 4096 for the compare.  Thus the generated code materializes both 4096 as well as 4096 << 8.

We add a new pass to analyze the number of times a given constant is used.  If a constant is used more than once, we prevent folding on that constant as it is better to materialize it once and reuse.


https://reviews.llvm.org/D59926

Files:
  include/llvm/InitializePasses.h
  include/llvm/Transforms/InstCombine/ConstantFoldAnalysis.h
  lib/Transforms/InstCombine/CMakeLists.txt
  lib/Transforms/InstCombine/ConstantFoldAnalysis.cpp
  lib/Transforms/InstCombine/InstCombineCompares.cpp
  lib/Transforms/InstCombine/InstCombineInternal.h
  lib/Transforms/InstCombine/InstructionCombining.cpp
  lib/Transforms/Utils/SimplifyCFG.cpp
  test/Transforms/InstCombine/do_not_create_large_constants.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59926.192606.patch
Type: text/x-patch
Size: 18273 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190328/13b017fb/attachment.bin>


More information about the llvm-commits mailing list