This is an archive of the discontinued LLVM Phabricator instance.

Added InstCombine transform for pattern "(A ^ B) | ((~A) ^ B) -> True".
Needs ReviewPublic

Authored by sonamkumari on Jul 27 2014, 11:42 PM.

Details

Summary

This patch implements transform for pattern "(A ^ B) | ((~A) ^ B) -> True".

Please help in reviewing the same.

Thanks,
Sonam.

Diff Detail

Event Timeline

sonamkumari retitled this revision from to Added InstCombine transform for pattern "(A ^ B) | ((~A) ^ B) -> True"..
sonamkumari updated this object.
sonamkumari edited the test plan for this revision. (Show Details)
sonamkumari added a subscriber: Unknown Object (MLST).
majnemer added inline comments.
lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
1283 ↗(On Diff #11938)

This change seems irrelevant to the patch.

1986–1989 ↗(On Diff #11938)

See my comments in your other differential, D4690.

test/Transforms/InstCombine/or.ll
411–420 ↗(On Diff #11938)

Again, please see my comments in D4690.

sonamkumari updated this revision to Diff 12015.EditedJul 30 2014, 4:13 AM

Hi David,

Thanks for your valuable review comments.
I have made the changes as you mentioned.
Please help in reviewing the patch.

Thanks,
Sonam.

Thinking about this with some colleagues, I'm starting to believe that this can be fixed in a more general way.

We do not optimize (A ^ B) | ((A ^ -1) ^ B) but we correctly optimize (A ^ B) | ((A ^ B) ^ -1); this makes me think that we are looking at a bug in reassociate.

lib/Analysis/InstructionSimplify.cpp
1606

This comment looks wrong, it should probably be:

// (A ^ B) | ((~A) ^ B) = -1

I've gone ahead and fixed this in rL214342; it includes a more general transform which should handle more cases.

sonamkumari updated this revision to Diff 12192.Aug 5 2014, 3:21 AM
sonamkumari added a reviewer: majnemer.

Hi David,

Thanks for your valuable comments.I will try to consider general case while doing any optimization in future.
But the patch which you uploaded in rL214342 fails for the following test cases as it doesn't take into consideration the symmetric case.

The test cases for which it fails are :

define i32 @test14(i32 %x, i32 %y) {

%nega = xor i32 %x, -1
%xor = xor i32 %nega, %y
%xor1 = xor i32 %x, %y
%or = or i32 %xor, %xor1
ret i32 %or

}

define i32 @test15(i32 %x, i32 %y) {

%xor = xor i32 %x, %y
%nega = xor i32 %x, -1
%xor1 = xor i32 %nega, %y
%or = or i32 %xor, %xor1
ret i32 %or

}

So, I modified the patch and submitting the same.
Kindly review it.

Thanks,
Sonam.

suyog added a reviewer: suyog.Aug 7 2014, 4:38 AM
suyog edited edge metadata.

LGTM. I would wait for David to comment on it though.

majnemer edited edge metadata.Aug 7 2014, 11:06 PM

I don't think we need to do anything here, running reassociate before instcombine cleans things up.

$ cat t.ll
define i32 @test14(i32 %x, i32 %y) {
  %nega = xor i32 %x, -1
  %xor = xor i32 %nega, %y
  %xor1 = xor i32 %x, %y
  %or = or i32 %xor, %xor1
  ret i32 %or
}

define i32 @test15(i32 %x, i32 %y) {
  %xor = xor i32 %x, %y
  %nega = xor i32 %x, -1
  %xor1 = xor i32 %nega, %y
  %or = or i32 %xor, %xor1
  ret i32 %or
}

$ ~/llvm/Debug+Asserts/bin/opt -reassociate -instcombine t.ll -o - -S
; ModuleID = 't.ll'

define i32 @test14(i32 %x, i32 %y) {
  ret i32 -1
}

define i32 @test15(i32 %x, i32 %y) {
  ret i32 -1
}
silvas resigned from this revision.Jul 8 2016, 11:38 PM
silvas removed a reviewer: silvas.
dexonsmith resigned from this revision.Jul 11 2016, 6:41 PM
dexonsmith removed a reviewer: dexonsmith.
espindola edited reviewers, added: espindola; removed: rafael.Mar 15 2018, 11:02 AM