Skip to content

Commit 948abf1

Browse files
AditiRMAditi-Medhane
andauthored
[PowerPC] Add BCDCOPYSIGN and BCDSETSIGN Instruction Support (#144874)
Support the following BCD format conversion builtins for PowerPC. - `__builtin_bcdcopysign` – Conversion that returns the decimal value of the first parameter combined with the sign code of the second parameter. ` - `__builtin_bcdsetsign` – Conversion that sets the sign code of the input parameter in packed decimal format. > Note: This built-in function is valid only when all following conditions are met: > -qarch is set to utilize POWER9 technology. > The bcd.h file is included. ## Prototypes ```c vector unsigned char __builtin_bcdcopysign(vector unsigned char, vector unsigned char); vector unsigned char __builtin_bcdsetsign(vector unsigned char, unsigned char); ``` ## Usage Details `__builtin_bcdsetsign`: Returns the packed decimal value of the first parameter combined with the sign code. The sign code is set according to the following rules: - If the packed decimal value of the first parameter is positive, the following rules apply: - If the second parameter is 0, the sign code is set to 0xC. - If the second parameter is 1, the sign code is set to 0xF. - If the packed decimal value of the first parameter is negative, the sign code is set to 0xD. > notes: > The second parameter can only be 0 or 1. > You can determine whether a packed decimal value is positive or negative as follows: > - Packed decimal values with sign codes **0xA, 0xC, 0xE, or 0xF** are interpreted as positive. > - Packed decimal values with sign codes **0xB or 0xD** are interpreted as negative. --------- Co-authored-by: Aditi-Medhane <aditi.medhane@ibm.com>
1 parent 9cadc4e commit 948abf1

File tree

7 files changed

+85
-2
lines changed

7 files changed

+85
-2
lines changed

clang/include/clang/Basic/BuiltinsPPC.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ TARGET_BUILTIN(__builtin_ppc_bcdsub_p, "iiV16UcV16Uc", "",
580580
"isa-v207-instructions")
581581

582582
// P9 Binary-coded decimal (BCD) builtins.
583+
TARGET_BUILTIN(__builtin_ppc_bcdcopysign, "V16UcV16UcV16Uc", "", "power9-vector")
584+
TARGET_BUILTIN(__builtin_ppc_bcdsetsign, "V16UcV16UcUc", "t", "power9-vector")
583585
TARGET_BUILTIN(__builtin_ppc_national2packed, "V16UcV16UcUc", "t", "power9-vector")
584586
TARGET_BUILTIN(__builtin_ppc_packed2national, "V16UcV16Uc", "", "power9-vector")
585587
TARGET_BUILTIN(__builtin_ppc_packed2zoned, "V16UcV16UcUc", "t", "power9-vector")

clang/lib/Basic/Targets/PPC.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ bool PPCTargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
8989
}
9090

9191
static void defineXLCompatMacros(MacroBuilder &Builder) {
92+
Builder.defineMacro("__builtin_bcdcopysign", "__builtin_ppc_bcdcopysign");
93+
Builder.defineMacro("__builtin_bcdsetsign", "__builtin_ppc_bcdsetsign");
9294
Builder.defineMacro("__builtin_national2packed",
9395
"__builtin_ppc_national2packed");
9496
Builder.defineMacro("__builtin_packed2national",

clang/lib/Sema/SemaPPC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ bool SemaPPC::CheckPPCBuiltinFunctionCall(const TargetInfo &TI,
108108
switch (BuiltinID) {
109109
default:
110110
return false;
111+
case PPC::BI__builtin_ppc_bcdsetsign:
111112
case PPC::BI__builtin_ppc_national2packed:
112113
case PPC::BI__builtin_ppc_packed2zoned:
113114
case PPC::BI__builtin_ppc_zoned2packed:
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
// REQUIRES: powerpc-registered-target
3+
// RUN: %clang_cc1 -triple powerpc64le-unknown-unknown -O2 -target-cpu pwr9 \
4+
// RUN: -emit-llvm %s -o - | FileCheck %s
5+
// RUN: %clang_cc1 -triple powerpc64-unknown-unknown -O2 -target-cpu pwr9 \
6+
// RUN: -emit-llvm %s -o - | FileCheck %s
7+
// RUN: %clang_cc1 -triple powerpc-unknown-unknown -O2 -target-cpu pwr9 \
8+
// RUN: -emit-llvm %s -o - | FileCheck %s
9+
10+
// CHECK-LABEL: test_bcdcopysign
11+
// CHECK: [[TMP0:%.*]] = tail call <16 x i8> @llvm.ppc.bcdcopysign(<16 x i8> %a, <16 x i8> %b)
12+
// CHECK-NEXT: ret <16 x i8> [[TMP0]]
13+
vector unsigned char test_bcdcopysign(vector unsigned char a, vector unsigned char b) {
14+
return __builtin_ppc_bcdcopysign(a, b);
15+
}
16+
17+
// CHECK-LABEL: test_bcdsetsign_imm0
18+
// CHECK: [[TMP0:%.*]] = tail call <16 x i8> @llvm.ppc.bcdsetsign(<16 x i8> %a, i32 0)
19+
// CHECK-NEXT: ret <16 x i8> [[TMP0]]
20+
vector unsigned char test_bcdsetsign_imm0(vector unsigned char a) {
21+
return __builtin_ppc_bcdsetsign(a, '\0');
22+
}
23+
24+
// CHECK-LABEL: test_bcdsetsign_imm1
25+
// CHECK: [[TMP0:%.*]] = tail call <16 x i8> @llvm.ppc.bcdsetsign(<16 x i8> %a, i32 1)
26+
// CHECK-NEXT: ret <16 x i8> [[TMP0]]
27+
vector unsigned char test_bcdsetsign_imm1(vector unsigned char a) {
28+
return __builtin_ppc_bcdsetsign(a, '\1');
29+
}

llvm/include/llvm/IR/IntrinsicsPowerPC.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,13 @@ let TargetPrefix = "ppc" in { // All intrinsics start with "llvm.ppc.".
676676
def int_ppc_addg6s: ClangBuiltin<"__builtin_addg6s">,
677677
DefaultAttrsIntrinsic<[llvm_i32_ty], [llvm_i32_ty, llvm_i32_ty], [IntrNoMem]>;
678678

679+
// BCD Format conversion intrinsics
680+
def int_ppc_bcdcopysign : ClangBuiltin<"__builtin_ppc_bcdcopysign">,
681+
DefaultAttrsIntrinsic<[llvm_v16i8_ty], [llvm_v16i8_ty, llvm_v16i8_ty], [IntrNoMem]>;
682+
def int_ppc_bcdsetsign : ClangBuiltin<"__builtin_ppc_bcdsetsign">,
683+
DefaultAttrsIntrinsic<[llvm_v16i8_ty], [llvm_v16i8_ty, llvm_i32_ty],
684+
[IntrNoMem, ImmArg<ArgIndex<1>>]>;
685+
679686
def int_ppc_bcdadd : ClangBuiltin<"__builtin_ppc_bcdadd">,
680687
DefaultAttrsIntrinsic<
681688
[llvm_v16i8_ty], [llvm_v16i8_ty, llvm_v16i8_ty, llvm_i32_ty],

llvm/lib/Target/PowerPC/PPCInstrAltivec.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,9 +1630,11 @@ def BCDCTSQ_rec : VX_VT5_EO5_VB5_XO9_o <0, 385, "bcdctsq.", []>;
16301630

16311631
// Decimal Copy-Sign/Set-Sign
16321632
let Defs = [CR6] in
1633-
def BCDCPSGN_rec : VX1_VT5_VA5_VB5<833, "bcdcpsgn.", []>;
1633+
def BCDCPSGN_rec : VX1_VT5_VA5_VB5<833, "bcdcpsgn.",
1634+
[(set v16i8:$VD, (int_ppc_bcdcopysign v16i8:$VA, v16i8:$VB))]>;
16341635

1635-
def BCDSETSGN_rec : VX_VT5_EO5_VB5_PS1_XO9_o<31, 385, "bcdsetsgn.", []>;
1636+
def BCDSETSGN_rec : VX_VT5_EO5_VB5_PS1_XO9_o<31, 385, "bcdsetsgn.",
1637+
[(set v16i8:$VD, (int_ppc_bcdsetsign v16i8:$VB, i32:$PS))]>;
16361638

16371639
// Decimal Shift/Unsigned-Shift/Shift-and-Round
16381640
def BCDS_rec : VX_VT5_VA5_VB5_PS1_XO9_o<193, "bcds." , []>;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc -verify-machineinstrs -mtriple=powerpc64le-unknown-unknown -mcpu=pwr9 \
3+
; RUN: --ppc-asm-full-reg-names < %s | FileCheck %s
4+
; RUN: llc -verify-machineinstrs -mtriple=powerpc64-unknown-unknown -mcpu=pwr9 \
5+
; RUN: --ppc-asm-full-reg-names < %s | FileCheck %s
6+
; RUN: llc -verify-machineinstrs -mcpu=pwr9 -mtriple=powerpc64-ibm-aix-xcoff \
7+
; RUN: -ppc-asm-full-reg-names < %s | FileCheck %s
8+
9+
define dso_local <16 x i8> @test_bcdcopysign(<16 x i8> noundef %a, <16 x i8> noundef %b) {
10+
; CHECK-LABEL: test_bcdcopysign:
11+
; CHECK: # %bb.0: # %entry
12+
; CHECK-NEXT: bcdcpsgn. v2, v2, v3
13+
; CHECK-NEXT: blr
14+
entry:
15+
%0 = tail call <16 x i8> @llvm.ppc.bcdcopysign(<16 x i8> %a, <16 x i8> %b)
16+
ret <16 x i8> %0
17+
}
18+
19+
define dso_local <16 x i8> @test_bcdsetsign_imm0(<16 x i8> noundef %a) {
20+
; CHECK-LABEL: test_bcdsetsign_imm0:
21+
; CHECK: # %bb.0: # %entry
22+
; CHECK-NEXT: bcdsetsgn. v2, v2, 0
23+
; CHECK-NEXT: blr
24+
entry:
25+
%0 = tail call <16 x i8> @llvm.ppc.bcdsetsign(<16 x i8> %a, i32 0)
26+
ret <16 x i8> %0
27+
}
28+
29+
define dso_local <16 x i8> @test_bcdsetsign_imm1(<16 x i8> noundef %a) {
30+
; CHECK-LABEL: test_bcdsetsign_imm1:
31+
; CHECK: # %bb.0: # %entry
32+
; CHECK-NEXT: bcdsetsgn. v2, v2, 1
33+
; CHECK-NEXT: blr
34+
entry:
35+
%0 = tail call <16 x i8> @llvm.ppc.bcdsetsign(<16 x i8> %a, i32 1)
36+
ret <16 x i8> %0
37+
}
38+
39+
declare <16 x i8> @llvm.ppc.bcdcopysign(<16 x i8>, <16 x i8>)
40+
declare <16 x i8> @llvm.ppc.bcdsetsign(<16 x i8>, i32)

0 commit comments

Comments
 (0)