Skip to content

Commit 7506900

Browse files
authored
[RISCV] Add th.ext(u) to isSignExtendingOpW in RISCVOptWInstrs. (llvm#157147)
These instructions extract and range of bits, move them to the LSB then sign or zero extend the result to fill the upper bits. If the extracted bits is small enough then the result will have 33 sign bits.
1 parent faab8f7 commit 7506900

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

llvm/lib/Target/RISCV/RISCVOptWInstrs.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,16 @@ static bool isSignExtendingOpW(const MachineInstr &MI, unsigned OpNo) {
409409
assert(Log2SEW >= 3 && Log2SEW <= 6 && "Unexpected Log2SEW");
410410
return Log2SEW <= 5;
411411
}
412+
case RISCV::TH_EXT: {
413+
unsigned Msb = MI.getOperand(2).getImm();
414+
unsigned Lsb = MI.getOperand(3).getImm();
415+
return Msb >= Lsb && (Msb - Lsb + 1) <= 32;
416+
}
417+
case RISCV::TH_EXTU: {
418+
unsigned Msb = MI.getOperand(2).getImm();
419+
unsigned Lsb = MI.getOperand(3).getImm();
420+
return Msb >= Lsb && (Msb - Lsb + 1) < 32;
421+
}
412422
}
413423

414424
return false;

llvm/test/CodeGen/RISCV/rv64xtheadbb.ll

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,3 +1122,101 @@ define i64 @bswap_i64(i64 %a) {
11221122
%1 = call i64 @llvm.bswap.i64(i64 %a)
11231123
ret i64 %1
11241124
}
1125+
1126+
define void @sextw_removal_ext(i32 signext %arg, i32 signext %arg1) nounwind {
1127+
; RV64I-LABEL: sextw_removal_ext:
1128+
; RV64I: # %bb.0: # %bb
1129+
; RV64I-NEXT: addi sp, sp, -16
1130+
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
1131+
; RV64I-NEXT: sraw a0, a0, a1
1132+
; RV64I-NEXT: .LBB36_1: # %bb2
1133+
; RV64I-NEXT: # =>This Inner Loop Header: Depth=1
1134+
; RV64I-NEXT: call foo
1135+
; RV64I-NEXT: slli a0, a0, 16
1136+
; RV64I-NEXT: srai a0, a0, 32
1137+
; RV64I-NEXT: bnez a0, .LBB36_1
1138+
; RV64I-NEXT: # %bb.2: # %bb7
1139+
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
1140+
; RV64I-NEXT: addi sp, sp, 16
1141+
; RV64I-NEXT: ret
1142+
;
1143+
; RV64XTHEADBB-LABEL: sextw_removal_ext:
1144+
; RV64XTHEADBB: # %bb.0: # %bb
1145+
; RV64XTHEADBB-NEXT: addi sp, sp, -16
1146+
; RV64XTHEADBB-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
1147+
; RV64XTHEADBB-NEXT: sraw a0, a0, a1
1148+
; RV64XTHEADBB-NEXT: .LBB36_1: # %bb2
1149+
; RV64XTHEADBB-NEXT: # =>This Inner Loop Header: Depth=1
1150+
; RV64XTHEADBB-NEXT: call foo
1151+
; RV64XTHEADBB-NEXT: th.ext a0, a0, 47, 16
1152+
; RV64XTHEADBB-NEXT: bnez a0, .LBB36_1
1153+
; RV64XTHEADBB-NEXT: # %bb.2: # %bb7
1154+
; RV64XTHEADBB-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
1155+
; RV64XTHEADBB-NEXT: addi sp, sp, 16
1156+
; RV64XTHEADBB-NEXT: ret
1157+
bb:
1158+
%i = ashr i32 %arg, %arg1
1159+
br label %bb2
1160+
1161+
bb2: ; preds = %bb2, %bb
1162+
%i3 = phi i32 [ %i, %bb ], [ %i7, %bb2 ]
1163+
%i4 = tail call i64 @foo(i32 signext %i3)
1164+
%i5 = shl i64 %i4, 16
1165+
%i6 = ashr i64 %i5, 32
1166+
%i7 = trunc i64 %i6 to i32
1167+
%i8 = icmp eq i32 %i7, 0
1168+
br i1 %i8, label %bb7, label %bb2
1169+
1170+
bb7: ; preds = %bb2
1171+
ret void
1172+
}
1173+
1174+
declare i64 @foo(i32 signext)
1175+
1176+
define void @sextw_removal_extu(i32 signext %arg, i32 signext %arg1) nounwind {
1177+
; RV64I-LABEL: sextw_removal_extu:
1178+
; RV64I: # %bb.0: # %bb
1179+
; RV64I-NEXT: addi sp, sp, -16
1180+
; RV64I-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
1181+
; RV64I-NEXT: sraw a0, a0, a1
1182+
; RV64I-NEXT: .LBB37_1: # %bb2
1183+
; RV64I-NEXT: # =>This Inner Loop Header: Depth=1
1184+
; RV64I-NEXT: call foo
1185+
; RV64I-NEXT: slli a0, a0, 16
1186+
; RV64I-NEXT: srli a0, a0, 33
1187+
; RV64I-NEXT: bnez a0, .LBB37_1
1188+
; RV64I-NEXT: # %bb.2: # %bb7
1189+
; RV64I-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
1190+
; RV64I-NEXT: addi sp, sp, 16
1191+
; RV64I-NEXT: ret
1192+
;
1193+
; RV64XTHEADBB-LABEL: sextw_removal_extu:
1194+
; RV64XTHEADBB: # %bb.0: # %bb
1195+
; RV64XTHEADBB-NEXT: addi sp, sp, -16
1196+
; RV64XTHEADBB-NEXT: sd ra, 8(sp) # 8-byte Folded Spill
1197+
; RV64XTHEADBB-NEXT: sraw a0, a0, a1
1198+
; RV64XTHEADBB-NEXT: .LBB37_1: # %bb2
1199+
; RV64XTHEADBB-NEXT: # =>This Inner Loop Header: Depth=1
1200+
; RV64XTHEADBB-NEXT: call foo
1201+
; RV64XTHEADBB-NEXT: th.extu a0, a0, 47, 17
1202+
; RV64XTHEADBB-NEXT: bnez a0, .LBB37_1
1203+
; RV64XTHEADBB-NEXT: # %bb.2: # %bb7
1204+
; RV64XTHEADBB-NEXT: ld ra, 8(sp) # 8-byte Folded Reload
1205+
; RV64XTHEADBB-NEXT: addi sp, sp, 16
1206+
; RV64XTHEADBB-NEXT: ret
1207+
bb:
1208+
%i = ashr i32 %arg, %arg1
1209+
br label %bb2
1210+
1211+
bb2: ; preds = %bb2, %bb
1212+
%i3 = phi i32 [ %i, %bb ], [ %i7, %bb2 ]
1213+
%i4 = tail call i64 @foo(i32 signext %i3)
1214+
%i5 = shl i64 %i4, 16
1215+
%i6 = lshr i64 %i5, 33
1216+
%i7 = trunc i64 %i6 to i32
1217+
%i8 = icmp eq i32 %i7, 0
1218+
br i1 %i8, label %bb7, label %bb2
1219+
1220+
bb7: ; preds = %bb2
1221+
ret void
1222+
}

0 commit comments

Comments
 (0)