dcandler created this revision. dcandler added reviewers: rsmith, t.p.northover, compnerd, void, joerg, efriedma, ostannard. Herald added subscribers: llvm-commits, cfe-commits, hiraditya, kristof.beyls, javed.absar. Herald added projects: clang, LLVM.
A number of inline assembly constraints are currently supported by LLVM, but rejected as invalid by Clang: Target independent constraints: s: An integer constant, but allowing only relocatable values ARM specific constraints: j: An immediate integer between 0 and 65535 (valid for MOVW) x: A 32, 64, or 128-bit floating-point/SIMD register: s0-s15, d0-d7, or q0-q3 N: An immediate integer between 0 and 31 (Thumb1 only) O: An immediate integer which is a multiple of 4 between -508 and 508. (Thumb1 only) This patch adds support to Clang for the missing constraints along with some checks to ensure that the constraints are used with the correct target and Thumb mode, and that immediates are within valid ranges (at least where possible). The constraints are already implemented in LLVM, but just a couple of minor corrections to checks (V8M Baseline includes MOVW so should work with 'j', 'N' and 'O' shouldn't be valid in Thumb2) so that Clang and LLVM are in line with each other and the documentation. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D65863 Files: clang/lib/Basic/Targets/ARM.cpp clang/test/Sema/arm_inline_asm_constraints.c llvm/lib/Target/ARM/ARMISelLowering.cpp
Index: llvm/lib/Target/ARM/ARMISelLowering.cpp =================================================================== --- llvm/lib/Target/ARM/ARMISelLowering.cpp +++ llvm/lib/Target/ARM/ARMISelLowering.cpp @@ -15143,7 +15143,7 @@ case 'j': // Constant suitable for movw, must be between 0 and // 65535. - if (Subtarget->hasV6T2Ops()) + if (Subtarget->hasV6T2Ops() || (Subtarget->hasV8MBaselineOps())) if (CVal >= 0 && CVal <= 65535) break; return; @@ -15251,7 +15251,7 @@ return; case 'N': - if (Subtarget->isThumb()) { // FIXME thumb2 + if (Subtarget->isThumb1Only()) { // This must be a constant between 0 and 31, for shift amounts. if (CVal >= 0 && CVal <= 31) break; @@ -15259,7 +15259,7 @@ return; case 'O': - if (Subtarget->isThumb()) { // FIXME thumb2 + if (Subtarget->isThumb1Only()) { // This must be a multiple of 4 between -508 and 508, for // ADD/SUB sp = sp + immediate. if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0)) Index: clang/test/Sema/arm_inline_asm_constraints.c =================================================================== --- /dev/null +++ clang/test/Sema/arm_inline_asm_constraints.c @@ -0,0 +1,305 @@ +// REQUIRES: arm-registered-target + +// RUN: %clang_cc1 -triple armv6 -verify=arm6 %s +// RUN: %clang_cc1 -triple armv7 -verify=arm7 %s +// RUN: %clang_cc1 -triple thumbv6 -verify=thumb1 %s +// RUN: %clang_cc1 -triple thumbv7 -verify=thumb2 %s + +// j: An immediate integer between 0 and 65535 (valid for MOVW) (ARM/Thumb2) +int test_j(int i) { + int res; + __asm("movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "j"(-1), [ input ] "r"(i) + :); + // arm6-error@13 {{invalid input constraint 'j' in asm}} + // arm7-error@13 {{value '-1' out of range for constraint 'j'}} + // thumb1-error@13 {{invalid input constraint 'j' in asm}} + // thumb2-error@13 {{value '-1' out of range for constraint 'j'}} + __asm("movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "j"(0), [ input ] "r"(i) + :); + // arm6-error@21 {{invalid input constraint 'j' in asm}} + // arm7-no-error + // thumb1-error@21 {{invalid input constraint 'j' in asm}} + // thumb2-no-error + __asm("movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "j"(65535), [ input ] "r"(i) + :); + // arm6-error@29 {{invalid input constraint 'j' in asm}} + // arm7-no-error + // thumb1-error@29 {{invalid input constraint 'j' in asm}} + // thumb2-no-error + __asm("movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "j"(65536), [ input ] "r"(i) + :); + // arm6-error@37 {{invalid input constraint 'j' in asm}} + // arm7-error@37 {{value '65536' out of range for constraint 'j'}} + // thumb1-error@37 {{invalid input constraint 'j' in asm}} + // thumb2-error@37 {{value '65536' out of range for constraint 'j'}} + return res; +} + +// I: An immediate integer valid for a data-processing instruction. (ARM/Thumb2) +// An immediate integer between -255 and -1. (Thumb1) +int test_I(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "I"(-1), [ input ] "r"(i) + :); // thumb1-error@53 {{value '-1' out of range for constraint 'I'}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "I"(0), [ input ] "r"(i) + :); // No errors expected. + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "I"(255), [ input ] "r"(i) + :); // No errors expected. + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "I"(256), [ input ] "r"(i) + :); // thumb1-error@68 {{value '256' out of range for constraint 'I'}} + return res; +} + +// J: An immediate integer between -4095 and 4095. (ARM/Thumb2) +// An immediate integer between -255 and -1. (Thumb1) +int test_J(int i) { + int res; + __asm( + "movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(-4096), [ input ] "r"(i) + :); + // arm6-error@80 {{value '-4096' out of range for constraint 'J'}} + // arm7-error@80 {{value '-4096' out of range for constraint 'J'}} + // thumb1-error@80 {{value '-4096' out of range for constraint 'J'}} + // thumb2-error@80 {{value '-4096' out of range for constraint 'J'}} + __asm( + "movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(-4095), [ input ] "r"(i) + :); + // thumb1-error@89 {{value '-4095' out of range for constraint 'J'}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(-256), [ input ] "r"(i) + :); + // thumb1-error@95 {{value '-256' out of range for constraint 'J'}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(-255), [ input ] "r"(i) + :); + // No errors expected. + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(-1), [ input ] "r"(i) + :); + // No errors expected. + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(0), [ input ] "r"(i) + :); + // thumb1-error@113 {{value '0' out of range for constraint 'J'}} + __asm( + "movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(4095), [ input ] "r"(i) + :); + // thumb1-error@119 {{value '4095' out of range for constraint 'J'}} + __asm( + "movw %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "J"(4096), [ input ] "r"(i) + :); + // arm6-error@125 {{value '4096' out of range for constraint 'J'}} + // arm7-error@125 {{value '4096' out of range for constraint 'J'}} + // thumb1-error@125 {{value '4096' out of range for constraint 'J'}} + // thumb2-error@125 {{value '4096' out of range for constraint 'J'}} + return res; +} + +// K: An immediate integer whose bitwise inverse is valid for a data-processing instruction. (ARM/Thumb2) +// An immediate integer between 0 and 255, with optional left-shift by some amount. (Thumb1) +int test_K(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "K"(123), [ input ] "r"(i) + :); + // No errors expected. + return res; +} + +// L: An immediate integer whose negation is valid for a data-processing instruction. (ARM/Thumb2) +// An immediate integer between -7 and 7. (Thumb1) +int test_L(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "L"(-8), [ input ] "r"(i) + :); // thumb1-error@154 {{value '-8' out of range for constraint 'L'}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "L"(-7), [ input ] "r"(i) + :); // No errors expected. + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "L"(7), [ input ] "r"(i) + :); // No errors expected. + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "L"(8), [ input ] "r"(i) + :); // thumb1-error@169 {{value '8' out of range for constraint 'L'}} + return res; +} + +// M: A power of two or a integer between 0 and 32. (ARM/Thumb2) +// An immediate integer which is a multiple of 4 between 0 and 1020. (Thumb1) +int test_M(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "M"(123), [ input ] "r"(i) + :); // No errors expected. + return res; +} + +// N: Invalid (ARM/Thumb2) +// An immediate integer between 0 and 31. (Thumb1) +int test_N(int i) { + int res; + __asm("add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "N"(-1), [ input ] "r"(i) + :); + // arm6-error@192 {{invalid input constraint 'N' in asm}} + // arm7-error@192 {{invalid input constraint 'N' in asm}} + // thumb1-error@192 {{value '-1' out of range for constraint 'N'}} + // thumb2-error@192 {{invalid input constraint 'N' in asm}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "N"(0), [ input ] "r"(i) + :); + // arm6-error@201 {{invalid input constraint 'N' in asm}} + // arm7-error@201 {{invalid input constraint 'N' in asm}} + // thumb1-no-error + // thumb2-error@201 {{invalid input constraint 'N' in asm}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "N"(31), [ input ] "r"(i) + :); + // arm6-error@210 {{invalid input constraint 'N' in asm}} + // arm7-error@210 {{invalid input constraint 'N' in asm}} + // thumb1-no-error + // thumb2-error@210 {{invalid input constraint 'N' in asm}} + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "N"(32), [ input ] "r"(i) + :); + // arm6-error@219 {{invalid input constraint 'N' in asm}} + // arm7-error@219 {{invalid input constraint 'N' in asm}} + // thumb1-error@219 {{value '32' out of range for constraint 'N'}} + // thumb2-error@219 {{invalid input constraint 'N' in asm}} + return res; +} + +// O: Invalid (ARM/Thumb2) +// An immediate integer which is a multiple of 4 between -508 and 508. (Thumb1) +int test_O(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "O"(1), [ input ] "r"(i) + :); + // arm6-error@235 {{invalid input constraint 'O' in asm}} + // arm7-error@235 {{invalid input constraint 'O' in asm}} + // thumb1-no-error + // thumb2-error@235 {{invalid input constraint 'O' in asm}} + return res; +} + +// l: Same as r (ARM) +// A low 32-bit GPR register (r0-r7). (Thumb1/Thumb2) +int test_l(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=l"(res) + : [ constant ] "i"(10), [ input ] "l"(i) + :); // No errors expected. + return res; +} + +// h: Invalid (ARM) +// A high 32-bit GPR register (r8-r15). (Thumb1/Thumb2) +int test_h(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=h"(res) + : [ constant ] "i"(10), [ input ] "h"(i) + :); + // arm6-error@262 {{invalid output constraint '=h' in asm}} + // arm7-error@262 {{invalid output constraint '=h' in asm}} + return res; +} + +// s: An integer constant, but allowing only relocatable values. +int g; + +int test_s(int i) { + int res; + __asm( + "add %0, %1;" + : [ result ] "=r"(res) + : [ constant ] "s"(&g), [ input ] "r"(i) + :); // No errors expected. + return res; +} + +// w: A 32, 64, or 128-bit floating-point/SIMD register: s0-s31, d0-d31, or q0-q15. +float test_w(float x) { + __asm__("vsqrt.f32 %0, %1" + : "=w"(x) + : "w"(x)); // No error expected. + return x; +} + +// x: A 32, 64, or 128-bit floating-point/SIMD register: s0-s15, d0-d7, or q0-q3. +float test_x(float x) { + __asm__("vsqrt.f32 %0, %1" + : "=x"(x) + : "x"(x)); // No error expected. + return x; +} + +// t: A 32, 64, or 128-bit floating-point/SIMD register: s0-s31, d0-d15, or q0-q7. +float test_t(float x) { + __asm__("vsqrt.f32 %0, %1" + : "=t"(x) + : "t"(x)); // No error expected. + return x; +} Index: clang/lib/Basic/Targets/ARM.cpp =================================================================== --- clang/lib/Basic/Targets/ARM.cpp +++ clang/lib/Basic/Targets/ARM.cpp @@ -884,19 +884,73 @@ switch (*Name) { default: break; - case 'l': // r0-r7 - case 'h': // r8-r15 - case 't': // VFP Floating point register single precision - case 'w': // VFP Floating point register double precision + case 'l': // r0-r7 if thumb, r0-r15 if ARM Info.setAllowsRegister(); return true; + case 'h': // r8-r15, thumb only + if (isThumb()) { + Info.setAllowsRegister(); + return true; + } + break; + case 's': // An integer constant, but allowing only relocatable values. + return true; + case 't': // s0-s31, d0-d31, or q0-q15 + case 'w': // s0-s15, d0-d7, or q0-q3 + case 'x': // s0-s31, d0-d15, or q0-q7 + Info.setAllowsRegister(); + return true; + case 'j': // An immediate integer between 0 and 65535 (valid for MOVW) + if (CPUAttr.equals("6T2") || + ArchVersion >= 7) // only available in ARMv6T2 and above + { + Info.setRequiresImmediate(0, 65535); + return true; + } + break; case 'I': + if (isThumb() && !supportsThumb2()) + Info.setRequiresImmediate(0, 255); + else + Info.setRequiresImmediate(); // valid for data processing + return true; case 'J': + if (isThumb() && !supportsThumb2()) + Info.setRequiresImmediate(-255, -1); + else + Info.setRequiresImmediate(-4095, 4095); + return true; case 'K': + if (isThumb() && !supportsThumb2()) + Info.setRequiresImmediate(0, 255); + else + Info.setRequiresImmediate(); // bitwise inverse valid for data processing + return true; case 'L': + if (isThumb() && !supportsThumb2()) + Info.setRequiresImmediate(-7, 7); + else + Info.setRequiresImmediate(); // negation valid for data processing + return true; case 'M': - // FIXME + // ARM/Thumb2: A power of two or a integer between 0 and 32. + // Thumb1: An immediate integer which is a multiple of 4 between 0 and 1020. + Info.setRequiresImmediate(); return true; + case 'N': + if (isThumb() && !supportsThumb2()) // Thumb1 only + { + Info.setRequiresImmediate(0, 31); + return true; + } + break; + case 'O': + if (isThumb() && !supportsThumb2()) // Thumb1 only + { + Info.setRequiresImmediate(); // a multiple of 4 between -508 and 508 + return true; + } + break; case 'Q': // A memory address that is a single base register. Info.setAllowsMemory(); return true;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits