Skip to content

Commit f7ff467

Browse files
committed
Merging r246900:
------------------------------------------------------------------------ r246900 | hfinkel | 2015-09-04 20:02:59 -0400 (Fri, 04 Sep 2015) | 14 lines [PowerPC] Fix and(or(x, c1), c2) -> rlwimi generation PPCISelDAGToDAG has a transformation that generates a rlwimi instruction from an input pattern that looks like this: and(or(x, c1), c2) but the associated logic does not work if there are bits that are 1 in c1 but 0 in c2 (these are normally canonicalized away, but that can't happen if the 'or' has other users. Make sure we abort the transformation if such bits are discovered. Fixes PR24704. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_37@252480 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1cd4501 commit f7ff467

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/Target/PowerPC/PPCISelDAGToDAG.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,13 +2570,25 @@ SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
25702570
return nullptr;
25712571
}
25722572
// ISD::OR doesn't get all the bitfield insertion fun.
2573-
// (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
2573+
// (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) might be a
2574+
// bitfield insert.
25742575
if (isInt32Immediate(N->getOperand(1), Imm) &&
25752576
N->getOperand(0).getOpcode() == ISD::OR &&
25762577
isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
2578+
// The idea here is to check whether this is equivalent to:
2579+
// (c1 & m) | (x & ~m)
2580+
// where m is a run-of-ones mask. The logic here is that, for each bit in
2581+
// c1 and c2:
2582+
// - if both are 1, then the output will be 1.
2583+
// - if both are 0, then the output will be 0.
2584+
// - if the bit in c1 is 0, and the bit in c2 is 1, then the output will
2585+
// come from x.
2586+
// - if the bit in c1 is 1, and the bit in c2 is 0, then the output will
2587+
// be 0.
2588+
// If that last condition is never the case, then we can form m from the
2589+
// bits that are the same between c1 and c2.
25772590
unsigned MB, ME;
2578-
Imm = ~(Imm^Imm2);
2579-
if (isRunOfOnes(Imm, MB, ME)) {
2591+
if (isRunOfOnes(~(Imm^Imm2), MB, ME) && !(~Imm & Imm2)) {
25802592
SDValue Ops[] = { N->getOperand(0).getOperand(0),
25812593
N->getOperand(0).getOperand(1),
25822594
getI32Imm(0, dl), getI32Imm(MB, dl),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: llc < %s | FileCheck %s
2+
target datalayout = "E-m:e-i64:64-n32:64"
3+
target triple = "powerpc64-unknown-linux-gnu"
4+
5+
@m = external global i32, align 4
6+
7+
; Function Attrs: nounwind
8+
define signext i32 @main() #0 {
9+
entry:
10+
11+
; CHECK-LABEL: @main
12+
; CHECK-NOT: rlwimi
13+
; CHECK: andi
14+
15+
%0 = load i32, i32* @m, align 4
16+
%or = or i32 %0, 250
17+
store i32 %or, i32* @m, align 4
18+
%and = and i32 %or, 249
19+
%sub.i = sub i32 %and, 0
20+
%sext = shl i32 %sub.i, 24
21+
%conv = ashr exact i32 %sext, 24
22+
ret i32 %conv
23+
}
24+
25+
attributes #0 = { nounwind "target-cpu"="pwr7" }
26+
attributes #1 = { nounwind }
27+

0 commit comments

Comments
 (0)