while we're at it, handle 'sdiv exact' of a power of 2 also,
authorChris Lattner <sabre@nondot.org>
Mon, 18 Apr 2011 07:00:40 +0000 (07:00 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 18 Apr 2011 07:00:40 +0000 (07:00 +0000)
this fixes a few rejects on c++ iterator loops.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129694 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/SelectionDAG/FastISel.cpp
test/CodeGen/X86/fast-isel-x86-64.ll

index d3a721f87546e7308a7f651596caa446c311a5df..76e9a7cac2d809845f41b8a362c1c109903aff3c 100644 (file)
@@ -360,6 +360,14 @@ bool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
   if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
     uint64_t Imm = CI->getZExtValue();
     
+    // Transform "sdiv exact X, 8" -> "sra X, 3".
+    if (ISDOpcode == ISD::SDIV && isa<BinaryOperator>(I) &&
+        cast<BinaryOperator>(I)->isExact() &&
+        isPowerOf2_64(Imm)) {
+      Imm = Log2_64(Imm);
+      ISDOpcode = ISD::SRA;
+    }
+    
     unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
                                       Op0IsKill, Imm, VT.getSimpleVT());
     if (ResultReg == 0) return false;
index dbf65f0b3fcd6747848a9a203463dc641aad0e5f..bf886e079f6ae90e4f5ba4cca017570b4462edb8 100644 (file)
@@ -119,3 +119,11 @@ define i32 @test10(i32 %X) nounwind {
 ; CHECK: test10:
 ; CHECK: shrl  $3, 
 }
+
+define i32 @test11(i32 %X) nounwind {
+  %Y = sdiv exact i32 %X, 8
+  ret i32 %Y
+; CHECK: test11:
+; CHECK: sarl  $3, 
+}
+