Add bswap intrinsics as documented in the Language Reference


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25309 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/BasicAliasAnalysis.cpp b/lib/Analysis/BasicAliasAnalysis.cpp
index 86cc564..71bcbd2 100644
--- a/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/lib/Analysis/BasicAliasAnalysis.cpp
@@ -708,7 +708,8 @@
 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",
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index 4022bdd..915a7cb 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -37,6 +37,13 @@
   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 @@
         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])) {