[PATCH] X86TargetLowering::LowerToBT

Chris Sears chris.sears at gmail.com
Wed Feb 4 18:49:26 PST 2015

Hi joker-eph,

This is a patch to X86TargetLowering::LowerToBT() which was hashed over on the Developers list with Intel concurring. I'm finally figuring out Phabricator and I expect a few more problems there.

**bt_pat** generates the BT instruction for X86 bit testing in cases where code size is important. It checks whether the -Oz (optimize for size) flag is set or whether the containing function's PGO cold attribute is set. If either are true it emits BT for tests of bits 8-31 instead of TEST. Previously, TEST was always used for bits 0-31 and BT was always used for bits 32-63.

Since the BT instruction is 16b smaller than TEST for the bits 8-31 case, 32b vs 48b, and not irredeemably slower, it makes sense to use BT in cases where size matters.

Similar logic is possible for BTC and BTS. However, LowerToBTC and LowerToBTS would need to be written and used and that's a larger patch.



Index: X86ISelLowering.cpp
--- X86ISelLowering.cpp
+++ X86ISelLowering.cpp
@@ -15237,8 +15237,22 @@
       RHS = AndLHS.getOperand(1);
-    // Use BT if the immediate can't be encoded in a TEST instruction.
-    if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
+    // choosing between BT and TEST for reg,imm
+    //               bit 0-7    bit 8-31     bit 32-63
+    // TEST reg,imm  24b        48b          N/A
+    // BT   reg,imm  32b        32b          40b
+    // TEST AL, AX, EAX and RAX use 8b less
+    // extended registers + bits 32-63 require the REX prefix
+    // for bits 32-63, there is no TESTQ and so BTQ is required
+    const Function *F = DAG.getMachineFunction().getFunction();
+    bool  OptForSize  = F->getAttributes().hasAttribute(
+            AttributeSet::FunctionIndex, Attribute::OptimizeForSize);
+    bool  OptForCold  = F->getAttributes().hasAttribute(
+            AttributeSet::FunctionIndex, Attribute::Cold);
+    if (isPowerOf2_64(AndRHSVal) &&
+        (!isUInt<32>(AndRHSVal) ||
+        (!isUInt<16>(AndRHSVal) && (OptForSize | OptForCold)))) {
       LHS = AndLHS;
       RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType());

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7425.19370.patch
Type: text/x-patch
Size: 1282 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150205/22027358/attachment.bin>

More information about the llvm-commits mailing list