For PR1297:
authorReid Spencer <rspencer@reidspencer.com>
Sun, 1 Apr 2007 07:35:23 +0000 (07:35 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sun, 1 Apr 2007 07:35:23 +0000 (07:35 +0000)
Support overloaded intrinsics bswap, ctpop, cttz, ctlz.

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

lib/Analysis/ConstantFolding.cpp
lib/CodeGen/IntrinsicLowering.cpp
lib/Transforms/Scalar/InstructionCombining.cpp

index 1288674139cc8f743031c9fac34c524d4b3b22de..f0bd1dd0ad4555331721d7aa4064223c53d30652 100644 (file)
@@ -317,24 +317,12 @@ llvm::canConstantFoldCallTo(Function *F) {
   switch (F->getIntrinsicID()) {
   case Intrinsic::sqrt_f32:
   case Intrinsic::sqrt_f64:
-  case Intrinsic::bswap_i16:
-  case Intrinsic::bswap_i32:
-  case Intrinsic::bswap_i64:
   case Intrinsic::powi_f32:
   case Intrinsic::powi_f64:
-  // FIXME: these should be constant folded as well
-  //case Intrinsic::ctpop_i8:
-  //case Intrinsic::ctpop_i16:
-  //case Intrinsic::ctpop_i32:
-  //case Intrinsic::ctpop_i64:
-  //case Intrinsic::ctlz_i8:
-  //case Intrinsic::ctlz_i16:
-  //case Intrinsic::ctlz_i32:
-  //case Intrinsic::ctlz_i64:
-  //case Intrinsic::cttz_i8:
-  //case Intrinsic::cttz_i16:
-  //case Intrinsic::cttz_i32:
-  //case Intrinsic::cttz_i64:
+  case Intrinsic::bswap:
+  case Intrinsic::ctpop:
+  case Intrinsic::ctlz:
+  case Intrinsic::cttz:
     return true;
   default: break;
   }
@@ -445,13 +433,19 @@ llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) {
           break;
       }
     } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
-      uint64_t V = Op->getZExtValue();
-      if (Name == "llvm.bswap.i16")
-        return ConstantInt::get(Ty, ByteSwap_16(V));
-      else if (Name == "llvm.bswap.i32")
-        return ConstantInt::get(Ty, ByteSwap_32(V));
-      else if (Name == "llvm.bswap.i64")
-        return ConstantInt::get(Ty, ByteSwap_64(V));
+      const IntegerType *OpTy = cast<IntegerType>(Op->getType());
+      if (Name.size() > 11 && !memcmp(&Name[0], "llvm.bswap", 10)) {
+        return ConstantInt::get(Op->getValue().byteSwap());
+      } else if (Name.size() > 11 && !memcmp(&Name[0],"llvm.ctpop",10)) {
+        uint64_t ctpop = Op->getValue().countPopulation();
+        return ConstantInt::get(OpTy, ctpop);
+      } else if (Name.size() > 10 && !memcmp(&Name[0], "llvm.cttz", 9)) {
+        uint64_t cttz = Op->getValue().countTrailingZeros();
+        return ConstantInt::get(OpTy, cttz);
+      } else if (Name.size() > 10 && !memcmp(&Name[0], "llvm.ctlz", 9)) {
+        uint64_t ctlz = Op->getValue().countLeadingZeros();
+        return ConstantInt::get(OpTy, ctlz);
+      }
     }
   } else if (NumOperands == 2) {
     if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
index 3cfa436935f9b73c8c5e22f46a1e5484e1b3f97e..21b0e514ef6c8c3725b9e472da4c01d183e55dfb 100644 (file)
@@ -236,8 +236,6 @@ static Value *LowerCTLZ(Value *V, Instruction *IP) {
   return LowerCTPOP(V, IP);
 }
 
-
-
 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
   Function *Callee = CI->getCalledFunction();
   assert(Callee && "Cannot lower an indirect call!");
@@ -283,30 +281,19 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
                     Type::VoidTy, AbortFCache);
     break;
   }
-  case Intrinsic::ctpop_i8:
-  case Intrinsic::ctpop_i16:
-  case Intrinsic::ctpop_i32:
-  case Intrinsic::ctpop_i64:
+  case Intrinsic::ctpop:
     CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI));
     break;
 
-  case Intrinsic::bswap_i16:
-  case Intrinsic::bswap_i32:
-  case Intrinsic::bswap_i64:
+  case Intrinsic::bswap:
     CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI));
     break;
     
-  case Intrinsic::ctlz_i8:
-  case Intrinsic::ctlz_i16:
-  case Intrinsic::ctlz_i32:
-  case Intrinsic::ctlz_i64:
+  case Intrinsic::ctlz:
     CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI));
     break;
 
-  case Intrinsic::cttz_i8:
-  case Intrinsic::cttz_i16:
-  case Intrinsic::cttz_i32:
-  case Intrinsic::cttz_i64: {
+  case Intrinsic::cttz: {
     // cttz(x) -> ctpop(~X & (X-1))
     Value *Src = CI->getOperand(1);
     Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
index 2735dff48f4f4a99a30ce97b0392ef9d72c36677..81379c0222f68c5d8f92bae3efb1b33848617c37 100644 (file)
@@ -3624,11 +3624,11 @@ Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) {
   Module *M = I.getParent()->getParent()->getParent();
   const char *FnName = 0;
   if (I.getType() == Type::Int16Ty)
-    FnName = "llvm.bswap.i16";
+    FnName = "llvm.bswap.i16.i16";
   else if (I.getType() == Type::Int32Ty)
-    FnName = "llvm.bswap.i32";
+    FnName = "llvm.bswap.i32.i32";
   else if (I.getType() == Type::Int64Ty)
-    FnName = "llvm.bswap.i64";
+    FnName = "llvm.bswap.i64.i64";
   else
     assert(0 && "Unknown integer type!");
   Constant *F = M->getOrInsertFunction(FnName, I.getType(), I.getType(), NULL);
@@ -5173,29 +5173,11 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
         default: break;
         }
       } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0)) {
-        // Handle set{eq|ne} <intrinsic>, intcst.
-        switch (II->getIntrinsicID()) {
-        default: break;
-        case Intrinsic::bswap_i16: 
-          // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c))
-          AddToWorkList(II);  // Dead?
-          I.setOperand(0, II->getOperand(1));
-          I.setOperand(1, ConstantInt::get(Type::Int16Ty,
-                                           ByteSwap_16(CI->getZExtValue())));
-          return &I;
-        case Intrinsic::bswap_i32:   
-          // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c))
-          AddToWorkList(II);  // Dead?
-          I.setOperand(0, II->getOperand(1));
-          I.setOperand(1, ConstantInt::get(Type::Int32Ty,
-                                           ByteSwap_32(CI->getZExtValue())));
-          return &I;
-        case Intrinsic::bswap_i64:   
-          // icmp eq (bswap(x)), c -> icmp eq (x,bswap(c))
-          AddToWorkList(II);  // Dead?
+        // Handle icmp {eq|ne} <intrinsic>, intcst.
+        if (II->getIntrinsicID() == Intrinsic::bswap) {
+          AddToWorkList(II);
           I.setOperand(0, II->getOperand(1));
-          I.setOperand(1, ConstantInt::get(Type::Int64Ty,
-                                           ByteSwap_64(CI->getZExtValue())));
+          I.setOperand(1, ConstantInt::get(CI->getValue().byteSwap()));
           return &I;
         }
       }