[llvm-commits] [llvm] r126097 - in /llvm/trunk: lib/Support/APInt.cpp lib/Target/X86/README.txt test/CodeGen/X86/divide-by-constant.ll unittests/ADT/APIntTest.cpp
Cameron Zwarich
zwarich at apple.com
Sun Feb 20 16:22:03 PST 2011
Author: zwarich
Date: Sun Feb 20 18:22:02 2011
New Revision: 126097
URL: http://llvm.org/viewvc/llvm-project?rev=126097&view=rev
Log:
The signed version of our "magic number" computation for the integer approximation
of a constant had a minor typo introduced when copying it from the book, which
caused it to favor negative approximations over positive approximations in many
cases. Positive approximations require fewer operations beyond the multiplication.
In the case of division by 3, we still generate code that is a single instruction
larger than GCC's code.
Modified:
llvm/trunk/lib/Support/APInt.cpp
llvm/trunk/lib/Target/X86/README.txt
llvm/trunk/test/CodeGen/X86/divide-by-constant.ll
llvm/trunk/unittests/ADT/APIntTest.cpp
Modified: llvm/trunk/lib/Support/APInt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=126097&r1=126096&r2=126097&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APInt.cpp (original)
+++ llvm/trunk/lib/Support/APInt.cpp Sun Feb 20 18:22:02 2011
@@ -1505,7 +1505,7 @@
r2 = r2 - ad;
}
delta = ad - r2;
- } while (q1.ule(delta) || (q1 == delta && r1 == 0));
+ } while (q1.ult(delta) || (q1 == delta && r1 == 0));
mag.m = q2 + 1;
if (d.isNegative()) mag.m = -mag.m; // resulting magic number
Modified: llvm/trunk/lib/Target/X86/README.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/README.txt?rev=126097&r1=126096&r2=126097&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/README.txt (original)
+++ llvm/trunk/lib/Target/X86/README.txt Sun Feb 20 18:22:02 2011
@@ -1888,12 +1888,10 @@
_t: ## @t
movslq %edi, %rax
- imulq $-1431655765, %rax, %rcx ## imm = 0xFFFFFFFFAAAAAAAB
- shrq $32, %rcx
- addl %ecx, %eax
- movl %eax, %ecx
- shrl $31, %ecx
- shrl %eax
+ imulq $1431655766, %rax, %rax ## imm = 0x55555556
+ movq %rax, %rcx
+ shrq $63, %rcx
+ shrq $32, %rax
addl %ecx, %eax
movsbl %al, %eax
ret
Modified: llvm/trunk/test/CodeGen/X86/divide-by-constant.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/divide-by-constant.ll?rev=126097&r1=126096&r2=126097&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/X86/divide-by-constant.ll (original)
+++ llvm/trunk/test/CodeGen/X86/divide-by-constant.ll Sun Feb 20 18:22:02 2011
@@ -40,7 +40,7 @@
%div = sdiv i16 %x, 33 ; <i32> [#uses=1]
ret i16 %div
; CHECK: test4:
-; CHECK: imull $-1985, %ecx, %ecx
+; CHECK: imull $1986, %eax, %eax
}
define i32 @test5(i32 %A) nounwind {
Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=126097&r1=126096&r2=126097&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Sun Feb 20 18:22:02 2011
@@ -332,6 +332,24 @@
EXPECT_EQ(APInt(15, 9).exactLogBase2(), -1);
}
+TEST(APIntTest, magic) {
+ EXPECT_EQ(APInt(32, 3).magic().m, APInt(32, "55555556", 16));
+ EXPECT_EQ(APInt(32, 3).magic().s, 0U);
+ EXPECT_EQ(APInt(32, 5).magic().m, APInt(32, "66666667", 16));
+ EXPECT_EQ(APInt(32, 5).magic().s, 1U);
+ EXPECT_EQ(APInt(32, 7).magic().m, APInt(32, "92492493", 16));
+ EXPECT_EQ(APInt(32, 7).magic().s, 2U);
+}
+
+TEST(APIntTest, magicu) {
+ EXPECT_EQ(APInt(32, 3).magicu().m, APInt(32, "AAAAAAAB", 16));
+ EXPECT_EQ(APInt(32, 3).magicu().s, 1U);
+ EXPECT_EQ(APInt(32, 5).magicu().m, APInt(32, "CCCCCCCD", 16));
+ EXPECT_EQ(APInt(32, 5).magicu().s, 2U);
+ EXPECT_EQ(APInt(32, 7).magicu().m, APInt(32, "24924925", 16));
+ EXPECT_EQ(APInt(32, 7).magicu().s, 3U);
+}
+
#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG
TEST(APIntTest, StringDeath) {
More information about the llvm-commits
mailing list