[cfe-commits] r172476 - in /cfe/trunk: include/clang/Basic/Builtins.def lib/CodeGen/CGBuiltin.cpp test/CodeGen/builtins-multiprecision.c

Michael Gottesman mgottesman at apple.com
Mon Jan 14 13:44:30 PST 2013


Author: mgottesman
Date: Mon Jan 14 15:44:30 2013
New Revision: 172476

URL: http://llvm.org/viewvc/llvm-project?rev=172476&view=rev
Log:
Multiprecision subtraction builtins.

We lower these into 2x chained usub.with.overflow intrinsics.

Modified:
    cfe/trunk/include/clang/Basic/Builtins.def
    cfe/trunk/lib/CodeGen/CGBuiltin.cpp
    cfe/trunk/test/CodeGen/builtins-multiprecision.c

Modified: cfe/trunk/include/clang/Basic/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Builtins.def?rev=172476&r1=172475&r2=172476&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Builtins.def (original)
+++ cfe/trunk/include/clang/Basic/Builtins.def Mon Jan 14 15:44:30 2013
@@ -930,6 +930,10 @@
 BUILTIN(__builtin_addc, "UiUiCUiCUiCUi*", "n")
 BUILTIN(__builtin_addcl, "ULiULiCULiCULiCULi*", "n")
 BUILTIN(__builtin_addcll, "ULLiULLiCULLiCULLiCULLi*", "n")
+BUILTIN(__builtin_subcs, "UsUsCUsCUsCUs*", "n")
+BUILTIN(__builtin_subc, "UiUiCUiCUiCUi*", "n")
+BUILTIN(__builtin_subcl, "ULiULiCULiCULiCULi*", "n")
+BUILTIN(__builtin_subcll, "ULLiULLiCULLiCULLiCULLi*", "n")
 
 #undef BUILTIN
 #undef LIBBUILTIN

Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=172476&r1=172475&r2=172476&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Mon Jan 14 15:44:30 2013
@@ -1348,7 +1348,11 @@
   case Builtin::BI__builtin_addcs:
   case Builtin::BI__builtin_addc:
   case Builtin::BI__builtin_addcl:
-  case Builtin::BI__builtin_addcll: {
+  case Builtin::BI__builtin_addcll:
+  case Builtin::BI__builtin_subcs:
+  case Builtin::BI__builtin_subc:
+  case Builtin::BI__builtin_subcl:
+  case Builtin::BI__builtin_subcll: {
 
     // We translate all of these builtins from expressions of the form:
     //   int x = ..., y = ..., carryin = ..., carryout, result;
@@ -1374,7 +1378,23 @@
     std::pair<llvm::Value*, unsigned> CarryOutPtr =
       EmitPointerWithAlignment(E->getArg(3));
 
-    const llvm::Intrinsic::ID IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
+    // Decide if we are lowering to a uadd.with.overflow or usub.with.overflow.
+    llvm::Intrinsic::ID IntrinsicId;
+    switch (BuiltinID) {
+    default: llvm_unreachable("Unknown multiprecision builtin id.");
+    case Builtin::BI__builtin_addcs:
+    case Builtin::BI__builtin_addc:
+    case Builtin::BI__builtin_addcl:
+    case Builtin::BI__builtin_addcll:
+      IntrinsicId = llvm::Intrinsic::uadd_with_overflow;
+      break;
+    case Builtin::BI__builtin_subcs:
+    case Builtin::BI__builtin_subc:
+    case Builtin::BI__builtin_subcl:
+    case Builtin::BI__builtin_subcll:
+      IntrinsicId = llvm::Intrinsic::usub_with_overflow;
+      break;
+    }
 
     // Construct our resulting LLVM IR expression.
     llvm::Value *Carry1;

Modified: cfe/trunk/test/CodeGen/builtins-multiprecision.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtins-multiprecision.c?rev=172476&r1=172475&r2=172476&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/builtins-multiprecision.c (original)
+++ cfe/trunk/test/CodeGen/builtins-multiprecision.c Mon Jan 14 15:44:30 2013
@@ -75,3 +75,76 @@
 
   return carryout;
 }
+
+unsigned short test_subcs(unsigned short x, unsigned short y,
+                          unsigned short carryin, unsigned short *z) {
+  // CHECK: @test_subcs
+  // CHECK: %{{.+}} = {{.*}} call { i16, i1 } @llvm.usub.with.overflow.i16(i16 %x, i16 %y)
+  // CHECK: %{{.+}} = extractvalue { i16, i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { i16, i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = {{.*}} call { i16, i1 } @llvm.usub.with.overflow.i16(i16 %{{.+}}, i16 %carryin)
+  // CHECK: %{{.+}} = extractvalue { i16, i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { i16, i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = or i1 %{{.+}}, %{{.+}}
+  // CHECK: %{{.+}} = zext i1 %{{.+}} to i16
+  // CHECK: store i16 %{{.+}}, i16* %z, align 2
+
+  unsigned short carryout;
+  *z = __builtin_subcs(x, y, carryin, &carryout);
+
+  return carryout;
+}
+
+unsigned test_subc(unsigned x, unsigned y, unsigned carryin, unsigned *z) {
+  // CHECK: @test_subc
+  // CHECK: %{{.+}} = {{.*}} call { i32, i1 } @llvm.usub.with.overflow.i32(i32 %x, i32 %y)
+  // CHECK: %{{.+}} = extractvalue { i32, i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { i32, i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = {{.*}} call { i32, i1 } @llvm.usub.with.overflow.i32(i32 %{{.+}}, i32 %carryin)
+  // CHECK: %{{.+}} = extractvalue { i32, i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { i32, i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = or i1 %{{.+}}, %{{.+}}
+  // CHECK: %{{.+}} = zext i1 %{{.+}} to i32
+  // CHECK: store i32 %{{.+}}, i32* %z, align 4
+  unsigned carryout;
+  *z = __builtin_subc(x, y, carryin, &carryout);
+
+  return carryout;
+}
+
+unsigned long test_subcl(unsigned long x, unsigned long y,
+                         unsigned long carryin, unsigned long *z) {
+  // CHECK: @test_subcl([[UL:i32|i64]] %x
+  // CHECK: %{{.+}} = {{.*}} call { [[UL]], i1 } @llvm.usub.with.overflow.[[UL]]([[UL]] %x, [[UL]] %y)
+  // CHECK: %{{.+}} = extractvalue { [[UL]], i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { [[UL]], i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = {{.*}} call { [[UL]], i1 } @llvm.usub.with.overflow.[[UL]]([[UL]] %{{.+}}, [[UL]] %carryin)
+  // CHECK: %{{.+}} = extractvalue { [[UL]], i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { [[UL]], i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = or i1 %{{.+}}, %{{.+}}
+  // CHECK: %{{.+}} = zext i1 %{{.+}} to [[UL]]
+  // CHECK: store [[UL]] %{{.+}}, [[UL]]* %z
+  unsigned long carryout;
+  *z = __builtin_subcl(x, y, carryin, &carryout);
+
+  return carryout;
+}
+
+unsigned long long test_subcll(unsigned long long x, unsigned long long y,
+                               unsigned long long carryin,
+                               unsigned long long *z) {
+  // CHECK: @test_subcll
+  // CHECK: %{{.+}} = {{.*}} call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %x, i64 %y)
+  // CHECK: %{{.+}} = extractvalue { i64, i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { i64, i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = {{.*}} call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %{{.+}}, i64 %carryin)
+  // CHECK: %{{.+}} = extractvalue { i64, i1 } %{{.+}}, 1
+  // CHECK: %{{.+}} = extractvalue { i64, i1 } %{{.+}}, 0
+  // CHECK: %{{.+}} = or i1 %{{.+}}, %{{.+}}
+  // CHECK: %{{.+}} = zext i1 %{{.+}} to i64
+  // CHECK: store i64 %{{.+}}, i64* %z
+  unsigned long long carryout;
+  *z = __builtin_subcll(x, y, carryin, &carryout);
+
+  return carryout;
+}





More information about the cfe-commits mailing list