Add bswap intrinsics as documented in the Language Reference
authorNate Begeman <natebegeman@mac.com>
Sat, 14 Jan 2006 01:25:24 +0000 (01:25 +0000)
committerNate Begeman <natebegeman@mac.com>
Sat, 14 Jan 2006 01:25:24 +0000 (01:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25309 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Intrinsics.h
include/llvm/Support/MathExtras.h
lib/Analysis/BasicAliasAnalysis.cpp
lib/Analysis/ConstantFolding.cpp
lib/Transforms/Utils/Local.cpp
lib/VMCore/Function.cpp
lib/VMCore/Verifier.cpp
test/Transforms/ConstProp/bswap.ll [new file with mode: 0644]

index d59121be51a462fb45aa13ba80cb7c71df01b97b..a0e01dddcf4345aede783c764f8732560ad94093 100644 (file)
@@ -58,19 +58,21 @@ namespace Intrinsic {
     dbg_func_start,   // Start of a function
     dbg_declare,      // Declare a local object
 
-
-    // Standard libc functions.
+    // Standard C library intrinsics.
     memcpy,         // Copy non-overlapping memory blocks
     memmove,        // Copy potentially overlapping memory blocks
     memset,         // Fill memory with a byte value
-
-    // libm related functions.
     isunordered,    // Return true if either argument is a NaN
-    ctpop, //count population
-    ctlz, //count leading zeros
-    cttz, //count trailing zeros
-    sqrt, //square root
-
+    sqrt,           // Square root
+
+    // Bit manipulation instrinsics.
+    bswap_i16,      // Byteswap 16 bits
+    bswap_i32,      // Byteswap 32 bits
+    bswap_i64,      // Byteswap 64 bits
+    ctpop,          // Count population
+    ctlz,           // Count leading zeros
+    cttz,           // Count trailing zeros
+    
     // Input/Output intrinsics.
     readport,
     writeport,
index 32cc473022ff90296a44faf481f6db20c10cf69a..1935f77b49ab9e119ef9f1dce4162583fbc297d9 100644 (file)
@@ -79,6 +79,32 @@ inline bool isPowerOf2_64(uint64_t Value) {
   return Value && !(Value & (Value - 1LL));
 }
 
+// ByteSwap_16 - This function returns a byte-swapped representation of the
+// 16-bit argument, Value.
+inline unsigned short ByteSwap_16(unsigned short Value) {
+  unsigned short Hi = Value << 8;
+  unsigned short Lo = Value >> 8;
+  return Hi | Lo;
+}
+
+// ByteSwap_32 - This function returns a byte-swapped representation of the
+// 32-bit argument, Value.
+inline unsigned ByteSwap_32(unsigned Value) {
+  unsigned Byte0 = Value & 0x000000FF;
+  unsigned Byte1 = Value & 0x0000FF00;
+  unsigned Byte2 = Value & 0x00FF0000;
+  unsigned Byte3 = Value & 0xFF000000;
+  return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
+}
+
+// ByteSwap_64 - This function returns a byte-swapped representation of the
+// 64-bit argument, Value.
+inline uint64_t ByteSwap_64(uint64_t Value) {
+  uint64_t Hi = ByteSwap_32(Value);
+  uint64_t Lo = ByteSwap_32(Value >> 32);
+  return (Hi << 32) | Lo;
+}
+
 // CountLeadingZeros_32 - this function performs the platform optimal form of
 // counting the number of zeros from the most significant bit to the first one
 // bit.  Ex. CountLeadingZeros_32(0x00F000FF) == 8.
index 86cc5646be66a398ad8523ca0179984397c2bb53..71bcbd2cf09a02d40b1542f521d114f6da6a15b1 100644 (file)
@@ -708,7 +708,8 @@ namespace {
 static const char *DoesntAccessMemoryTable[] = {
   // LLVM intrinsics:
   "llvm.frameaddress", "llvm.returnaddress", "llvm.readport",
-  "llvm.isunordered", "llvm.sqrt", "llvm.ctpop", "llvm.ctlz", "llvm.cttz",
+  "llvm.isunordered", "llvm.sqrt", "llvm.bswap.i16", "llvm.bswap.i32",
+  "llvm.bswap.i64", "llvm.ctpop", "llvm.ctlz", "llvm.cttz",
 
   "abs", "labs", "llabs", "imaxabs", "fabs", "fabsf", "fabsl",
   "trunc", "truncf", "truncl", "ldexp",
index 4022bdda1def3609941241c65535b48a6d59a750..915a7cb734f5d8e84ae665c15952c0e3bed0e973 100644 (file)
@@ -37,6 +37,13 @@ llvm::canConstantFoldCallTo(Function *F) {
   switch (F->getIntrinsicID()) {
   case Intrinsic::isunordered:
   case Intrinsic::sqrt:
+  case Intrinsic::bswap_i16:
+  case Intrinsic::bswap_i32:
+  case Intrinsic::bswap_i64:
+  // FIXME: these should be constant folded as well
+  //case Intrinsic::ctpop:
+  //case Intrinsic::ctlz:
+  //case Intrinsic::cttz:
     return true;
   default: break;
   }
@@ -142,6 +149,14 @@ llvm::ConstantFoldCall(Function *F, const std::vector<Constant*> &Operands) {
         default:
           break;
       }
+    } else if (ConstantUInt *Op = dyn_cast<ConstantUInt>(Operands[0])) {
+      uint64_t V = Op->getValue();
+      if (Name == "llvm.bswap.i16")
+        return ConstantUInt::get(Ty, ByteSwap_16(V));
+      else if (Name == "llvm.bswap.i32")
+        return ConstantUInt::get(Ty, ByteSwap_32(V));
+      else if (Name == "llvm.bswap.i64")
+        return ConstantUInt::get(Ty, ByteSwap_64(V));
     }
   } else if (Operands.size() == 2) {
     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
index 4e3b3e972e215627278d5fd657162ee5f0d291db..c4b69a40d4cebf9221899265327256920b2c716f 100644 (file)
@@ -298,6 +298,9 @@ bool llvm::isInstructionTriviallyDead(Instruction *I) {
       case Intrinsic::frameaddress:
       case Intrinsic::stacksave:
       case Intrinsic::isunordered:
+      case Intrinsic::bswap_i16:
+      case Intrinsic::bswap_i32:
+      case Intrinsic::bswap_i64:
       case Intrinsic::ctpop:
       case Intrinsic::ctlz:
       case Intrinsic::cttz:
index 0cbe14ce0416fb616f924bdb49862b23a7ee1fad..3abfef9c2d56431a9e55d724bdb8c5d75af7f843 100644 (file)
@@ -207,6 +207,11 @@ unsigned Function::getIntrinsicID() const {
   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
 
   switch (getName()[5]) {
+  case 'b':
+    if (getName() == "llvm.bswap.i16") return Intrinsic::bswap_i16;
+    if (getName() == "llvm.bswap.i32") return Intrinsic::bswap_i32;
+    if (getName() == "llvm.bswap.i64") return Intrinsic::bswap_i64;
+    break;
   case 'c':
     if (getName() == "llvm.ctpop") return Intrinsic::ctpop;
     if (getName() == "llvm.cttz") return Intrinsic::cttz;
index 9dd7184b1d7f31c418ef21ccfcab6345a7710610..f67a49702c02eac301c088253ce6dae84efe0f5d 100644 (file)
@@ -749,6 +749,36 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
     NumArgs = 0;
     break;
 
+  case Intrinsic::bswap_i16:
+    Assert1(FT->getNumParams() == 1,
+            "Illegal # arguments for intrinsic function!", IF);
+    Assert1(FT->getReturnType() == FT->getParamType(0),
+            "Return type does not match source type", IF);
+    Assert1(FT->getReturnType() == Type::UShortTy,
+            "Return type is not ushort!", IF);
+    NumArgs = 1;
+    break;    
+
+  case Intrinsic::bswap_i32:
+    Assert1(FT->getNumParams() == 1,
+            "Illegal # arguments for intrinsic function!", IF);
+    Assert1(FT->getReturnType() == FT->getParamType(0),
+            "Return type does not match source type", IF);
+    Assert1(FT->getReturnType() == Type::UIntTy,
+            "Return type is not uint!", IF);
+    NumArgs = 1;
+    break;    
+
+  case Intrinsic::bswap_i64:
+    Assert1(FT->getNumParams() == 1,
+            "Illegal # arguments for intrinsic function!", IF);
+    Assert1(FT->getReturnType() == FT->getParamType(0),
+            "Return type does not match source type", IF);
+    Assert1(FT->getReturnType() == Type::ULongTy,
+            "Return type is not ulong!", IF);
+    NumArgs = 1;
+    break;    
+    
   case Intrinsic::ctpop:
   case Intrinsic::ctlz:
   case Intrinsic::cttz:
diff --git a/test/Transforms/ConstProp/bswap.ll b/test/Transforms/ConstProp/bswap.ll
new file mode 100644 (file)
index 0000000..2d7f964
--- /dev/null
@@ -0,0 +1,22 @@
+; bswap should be constant folded when it is passed a constant argument
+
+; RUN: llvm-as < %s | opt -constprop | llvm-dis | not grep call
+
+declare ushort %llvm.bswap.i16(ushort)
+declare uint %llvm.bswap.i32(uint)
+declare ulong %llvm.bswap.i64(ulong)
+
+ushort %W() {
+       %Z = call ushort %llvm.bswap.i16(ushort 1)
+       ret ushort %Z
+}
+
+uint %X() {
+       %Z = call uint %llvm.bswap.i32(uint 1)
+       ret uint %Z
+}
+
+ulong %Y() {
+       %Z = call ulong %llvm.bswap.i64(ulong 1)
+       ret ulong %Z
+}