[PATCH] D28337: [InstSimplify] if the condition of a select is known, eliminate the select
Sanjay Patel via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 4 17:21:24 PST 2017
spatel created this revision.
spatel added reviewers: efriedma, hfinkel, majnemer.
spatel added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.
This is a generic (and potentially expensive) solution for PR31512:
https://llvm.org/bugs/show_bug.cgi?id=31512
The motivation is that we will need to increase usage of llvm.assume and/or metadata to solve PR28430:
https://llvm.org/bugs/show_bug.cgi?id=28430
...and this kind of simplification is needed to take advantage of that extra information.
A couple of things I learned while looking at this:
1. We already have a call to computeKnownBits() at the end of llvm::SimplifyInstruction(), so this case already works: define i8 @assume_sel_cond4(i1 %cond, i8 %x, i8 %y) { call void @llvm.assume(i1 %cond) %notcond = xor i1 %cond, 1 <--- known false via computeKnownBits %sel = select i1 %notcond, i8 %x, i8 %y ret i8 %sel }
2. The test cases in this patch will not work with 'opt -instcombine' even though instcombine always calls SimplifySelectInst() before it tries other transforms. This is because in InstCombiner::visitSelectInst(): if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, DL, &TLI, &DT, &AC))
...we don't include the select instruction as the context instruction. I don't think that's intentional, but that is true for many of the Simplify* calls in InstCombiner. As a counter example, we do provide the context instruction when InstCombiner::visitICmpInst() calls SimplifyICmpInst().
https://reviews.llvm.org/D28337
Files:
lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/select.ll
Index: test/Transforms/InstSimplify/select.ll
===================================================================
--- test/Transforms/InstSimplify/select.ll
+++ test/Transforms/InstSimplify/select.ll
@@ -402,15 +402,14 @@
ret i32* %sel
}
-; FIXME: If the condition is known, we don't need to select.
+; If the condition is known, we don't need to select.
declare void @llvm.assume(i1)
define i8 @assume_sel_cond(i1 %cond, i8 %x, i8 %y) {
; CHECK-LABEL: @assume_sel_cond(
; CHECK-NEXT: call void @llvm.assume(i1 %cond)
-; CHECK-NEXT: [[SEL:%.*]] = select i1 %cond, i8 %x, i8 %y
-; CHECK-NEXT: ret i8 [[SEL]]
+; CHECK-NEXT: ret i8 %x
;
call void @llvm.assume(i1 %cond)
%sel = select i1 %cond, i8 %x, i8 %y
@@ -421,8 +420,7 @@
; CHECK-LABEL: @do_not_assume_sel_cond(
; CHECK-NEXT: [[NOTCOND:%.*]] = icmp eq i1 %cond, false
; CHECK-NEXT: call void @llvm.assume(i1 [[NOTCOND]])
-; CHECK-NEXT: [[SEL:%.*]] = select i1 %cond, i8 %x, i8 %y
-; CHECK-NEXT: ret i8 [[SEL]]
+; CHECK-NEXT: ret i8 %y
;
%notcond = icmp eq i1 %cond, false
call void @llvm.assume(i1 %notcond)
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -3708,6 +3708,16 @@
simplifySelectWithICmpCond(CondVal, TrueVal, FalseVal, Q, MaxRecurse))
return V;
+ // If we can compute the condition, there's no need for a select.
+ if (CondVal->getType()->getPrimitiveSizeInBits() == 1) {
+ APInt KnownOne(1, 0), KnownZero(1, 0);
+ computeKnownBits(CondVal, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
+ if (KnownOne == 1)
+ return TrueVal;
+ if (KnownZero == 1)
+ return FalseVal;
+ }
+
return nullptr;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28337.83169.patch
Type: text/x-patch
Size: 1816 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170105/804661fa/attachment.bin>
More information about the llvm-commits
mailing list