Skip to content

Commit 955655f

Browse files
committed
[PowerPC] reduce rotate in BitPermutationSelector
BitPermutationSelector builds the output value by repeating rotate-and-mask instructions with input registers. Here, we may avoid one rotate instruction if we start building from an input register that does not require rotation. For example of the test case bitfieldinsert.ll, it first rotates left r4 by 8 bits and then inserts some bits from r5 without rotation. This can be executed by one rlwimi instruction, which rotates r4 by 8 bits and inserts its bits into r5. This patch adds a check for rotation amounts in the comparator used in sorting to process the input without rotation first. Differential Revision: https://reviews.llvm.org/D47765 llvm-svn: 334011
1 parent 613fea2 commit 955655f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,10 @@ class BitPermutationSelector {
11611161
return true;
11621162
else if (NumGroups < Other.NumGroups)
11631163
return false;
1164+
else if (RLAmt == 0 && Other.RLAmt != 0)
1165+
return true;
1166+
else if (RLAmt != 0 && Other.RLAmt == 0)
1167+
return false;
11641168
else if (FirstGroupStartIdx < Other.FirstGroupStartIdx)
11651169
return true;
11661170
return false;
@@ -1374,7 +1378,9 @@ class BitPermutationSelector {
13741378
}
13751379

13761380
// Take all (SDValue, RLAmt) pairs and sort them by the number of groups
1377-
// associated with each. If there is a degeneracy, pick the one that occurs
1381+
// associated with each. If the number of groups are same, we prefer a group
1382+
// which does not require rotate, i.e. RLAmt is 0, to avoid the first rotate
1383+
// instruction. If there is a degeneracy, pick the one that occurs
13781384
// first (in the final value).
13791385
void collectValueRotInfo() {
13801386
ValueRots.clear();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-linux-gnu -mcpu=pwr8 < %s | FileCheck %s
2+
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-linux-gnu -mcpu=pwr8 < %s | FileCheck %s
3+
4+
; bitfieldinsert32: Test for rlwimi
5+
; equivalent C code
6+
; struct s32 {
7+
; int a:8;
8+
; int b:16;
9+
; int c:8;
10+
; };
11+
; void bitfieldinsert32(struct s32 *p, unsigned int v) {
12+
; p->b = v;
13+
; }
14+
15+
%struct.s32 = type { i32 }
16+
17+
define void @bitfieldinsert32(%struct.s32* nocapture %p, i32 zeroext %v) {
18+
; CHECK-LABEL: @bitfieldinsert32
19+
; CHECK: lwz [[REG1:[0-9]+]], 0(3)
20+
; CHECK: rlwimi [[REG1]], 4, 8, 8, 23
21+
; CHECK: stw [[REG1]], 0(3)
22+
; CHECK: blr
23+
entry:
24+
%0 = getelementptr inbounds %struct.s32, %struct.s32* %p, i64 0, i32 0
25+
%bf.load = load i32, i32* %0, align 4
26+
%bf.value = shl i32 %v, 8
27+
%bf.shl = and i32 %bf.value, 16776960
28+
%bf.clear = and i32 %bf.load, -16776961
29+
%bf.set = or i32 %bf.clear, %bf.shl
30+
store i32 %bf.set, i32* %0, align 4
31+
ret void
32+
}
33+

0 commit comments

Comments
 (0)