[llvm-commits] [PATCH] Make EarlyCSE understand commutativity

Michael Ilseman milseman at apple.com
Tue Oct 2 16:20:47 PDT 2012


Here's an implementation of what Chris described. It doesn't actually swap the operands, so the IR will remain unchanged if there's no actual CSE performed.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: EarlyCSE.patch
Type: application/octet-stream
Size: 3480 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20121002/d5e5978b/attachment.obj>
-------------- next part --------------

On Oct 1, 2012, at 10:51 PM, Chris Lattner <clattner at apple.com> wrote:

> 
> On Oct 1, 2012, at 1:14 PM, Michael Ilseman <milseman at apple.com> wrote:
>> I could also swap based on the addresses of the operands, which is how GVN does it. Swapping itself would still change execution to execution, but it will be consistent within a single execution.
> 
> I think that this is the right way to go.  The code in EarlyCSE should stay really simple, and I don't like the idea of adding yet-another expression abstraction that has to stay up to date with the IR as we add new things to it.
> 
> 
> What I'm suggesting is that the hashing code can look like (psuedo code obviously):
> 
> unsigned DenseMapInfo<SimpleValue>::getHashValue(SimpleValue Val) {
>  Instruction *Inst = Val.Inst;
> 
>  // Hash in all of the operands as pointers.
>  bool Swapped = false;
>  if (isa<BinaryOperator>(Inst) && Inst->isCommutative() && Inst->getOperand(0) > Inst->getOperand(1)) {
>    Inst->swapOperands();
>    Swapped = true;
>  }
> 
>  unsigned Res = 0;
>  for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
>    Res ^= getHash(Inst->getOperand(i)) << (i & 0xF);
> 
> ...
> 
>  // Mix in the opcode.
>  Res = (Res << 1) ^ Inst->getOpcode();
> 
>  if (Swapped) Inst->swapOperands();
>  return Res;
> }
> 
> Similarly for compares.
> 
> Of course, it would be much better to avoid actually swapping the operands, and I don't think it would make the code any more complex to do that.
> 
> -Chris
> 



More information about the llvm-commits mailing list