Implement the llvm.bit.part_select.iN.iN.iN overloaded intrinsic.
authorReid Spencer <rspencer@reidspencer.com>
Wed, 4 Apr 2007 23:48:25 +0000 (23:48 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Wed, 4 Apr 2007 23:48:25 +0000 (23:48 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@35678 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Intrinsics.td
lib/CodeGen/IntrinsicLowering.cpp
lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
lib/VMCore/Verifier.cpp
test/CodeGen/Generic/bit-intrinsics.ll [new file with mode: 0644]

index 7b85d621368000b9b7576f1b7517aa4aa9768b61..915181ac6392065ee6a911c2d054246e9e762e4d 100644 (file)
@@ -203,11 +203,13 @@ def int_siglongjmp : Intrinsic<[llvm_void_ty, llvm_ptr_ty, llvm_i32_ty]>;
 
 // None of these intrinsics accesses memory at all.
 let Properties = [IntrNoMem] in {
+  def int_bit_part_select : 
+     Intrinsic<[llvm_int_ty, llvm_int_ty, llvm_i32_ty, llvm_i32_ty]>;
   def int_bswap: Intrinsic<[llvm_int_ty, llvm_int_ty]>;
   def int_ctpop: Intrinsic<[llvm_i32_ty, llvm_int_ty]>;
   def int_ctlz : Intrinsic<[llvm_i32_ty, llvm_int_ty]>;
   def int_cttz : Intrinsic<[llvm_i32_ty, llvm_int_ty]>;
-} 
+}
 
 //===------------------------ Debugger Intrinsics -------------------------===//
 //
index 7b772d78f279d8ed8ba504caa0727d47a34ce1e1..359ca00000270b56de02ba38e1b8e71c564bac4a 100644 (file)
@@ -236,6 +236,177 @@ static Value *LowerCTLZ(Value *V, Instruction *IP) {
   return LowerCTPOP(V, IP);
 }
 
+/// Convert the llvm.bit.part_select.iX.iY.iZ intrinsic. This intrinsic takes 
+/// three integer operands of arbitrary bit width. The first operand is the 
+/// value from which to select the bits. The second and third operands define a 
+/// range of bits to select.  The result is the bits selected and has a 
+/// corresponding width of Left-Right (second operand - third operand).
+/// @see IEEE 1666-2005, System C, Section 7.2.6, pg 175. 
+/// @brief Lowering of llvm.bit.part_select intrinsic.
+static Instruction *LowerBitPartSelect(CallInst *CI) {
+  // Make sure we're dealing with a part select intrinsic here
+  Function *F = CI->getCalledFunction();
+  const FunctionType *FT = F->getFunctionType();
+  if (!F->isDeclaration() || !FT->getReturnType()->isInteger() ||
+      FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() ||
+      !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger())
+    return CI;
+
+  // Get the intrinsic implementation function by converting all the . to _
+  // in the intrinsic's function name and then reconstructing the function
+  // declaration.
+  std::string Name(F->getName());
+  for (unsigned i = 4; i < Name.length(); ++i)
+    if (Name[i] == '.')
+      Name[i] = '_';
+  Module* M = F->getParent();
+  F = cast<Function>(M->getOrInsertFunction(Name, FT));
+  F->setLinkage(GlobalValue::InternalLinkage);
+
+  // If we haven't defined the impl function yet, do so now
+  if (F->isDeclaration()) {
+
+    // Get the arguments to the function
+    Value* Val = F->getOperand(0);
+    Value* Left = F->getOperand(1);
+    Value* Right = F->getOperand(2);
+
+    // We want to select a range of bits here such that [Left, Right] is shifted
+    // down to the low bits. However, it is quite possible that Left is smaller
+    // than Right in which case the bits have to be reversed. 
+    
+    // Create the blocks we will need for the two cases (forward, reverse)
+    BasicBlock* CurBB   = new BasicBlock("entry", F);
+    BasicBlock *RevSize = new BasicBlock("revsize", CurBB->getParent());
+    BasicBlock *FwdSize = new BasicBlock("fwdsize", CurBB->getParent());
+    BasicBlock *Compute = new BasicBlock("compute", CurBB->getParent());
+    BasicBlock *Reverse = new BasicBlock("reverse", CurBB->getParent());
+    BasicBlock *RsltBlk = new BasicBlock("result",  CurBB->getParent());
+
+    // Cast Left and Right to the size of Val so the widths are all the same
+    if (Left->getType() != Val->getType())
+      Left = CastInst::createIntegerCast(Left, Val->getType(), false, 
+                                         "tmp", CurBB);
+    if (Right->getType() != Val->getType())
+      Right = CastInst::createIntegerCast(Right, Val->getType(), false, 
+                                          "tmp", CurBB);
+
+    // Compute a few things that both cases will need, up front.
+    Constant* Zero = ConstantInt::get(Val->getType(), 0);
+    Constant* One = ConstantInt::get(Val->getType(), 1);
+    Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType());
+
+    // Compare the Left and Right bit positions. This is used to determine 
+    // which case we have (forward or reverse)
+    ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Left, Right, "less",CurBB);
+    new BranchInst(RevSize, FwdSize, Cmp, CurBB);
+
+    // First, copmute the number of bits in the forward case.
+    Instruction* FBitSize = 
+      BinaryOperator::createSub(Left, Right,"fbits", FwdSize);
+    new BranchInst(Compute, FwdSize);
+
+    // Second, compute the number of bits in the reverse case.
+    Instruction* RBitSize = 
+      BinaryOperator::createSub(Right, Left, "rbits", RevSize);
+    new BranchInst(Compute, RevSize);
+
+    // Now, compute the bit range. Start by getting the bitsize and the shift
+    // amount (either Left or Right) from PHI nodes. Then we compute a mask for 
+    // the number of bits we want in the range. We shift the bits down to the 
+    // least significant bits, apply the mask to zero out unwanted high bits, 
+    // and we have computed the "forward" result. It may still need to be 
+    // reversed.
+
+    // Get the BitSize from one of the two subtractions
+    PHINode *BitSize = new PHINode(Val->getType(), "bits", Compute);
+    BitSize->reserveOperandSpace(2);
+    BitSize->addIncoming(FBitSize, FwdSize);
+    BitSize->addIncoming(RBitSize, RevSize);
+
+    // Get the ShiftAmount as the smaller of Left/Right
+    PHINode *ShiftAmt = new PHINode(Val->getType(), "shiftamt", Compute);
+    ShiftAmt->reserveOperandSpace(2);
+    ShiftAmt->addIncoming(Right, FwdSize);
+    ShiftAmt->addIncoming(Left, RevSize);
+
+    // Increment the bit size
+    Instruction *BitSizePlusOne = 
+      BinaryOperator::createAdd(BitSize, One, "bits", Compute);
+
+    // Create a Mask to zero out the high order bits.
+    Instruction* Mask = 
+      BinaryOperator::createShl(AllOnes, BitSizePlusOne, "mask", Compute);
+    Mask = BinaryOperator::createNot(Mask, "mask", Compute);
+
+    // Shift the bits down and apply the mask
+    Instruction* FRes = 
+      BinaryOperator::createLShr(Val, ShiftAmt, "fres", Compute);
+    FRes = BinaryOperator::createAnd(FRes, Mask, "fres", Compute);
+    new BranchInst(Reverse, RsltBlk, Cmp, Compute);
+
+    // In the Reverse block we have the mask already in FRes but we must reverse
+    // it by shifting FRes bits right and putting them in RRes by shifting them 
+    // in from left.
+
+    // First set up our loop counters
+    PHINode *Count = new PHINode(Val->getType(), "count", Reverse);
+    Count->reserveOperandSpace(2);
+    Count->addIncoming(BitSizePlusOne, Compute);
+
+    // Next, get the value that we are shifting.
+    PHINode *BitsToShift   = new PHINode(Val->getType(), "val", Reverse);
+    BitsToShift->reserveOperandSpace(2);
+    BitsToShift->addIncoming(FRes, Compute);
+
+    // Finally, get the result of the last computation
+    PHINode *RRes  = new PHINode(Val->getType(), "rres", Reverse);
+    RRes->reserveOperandSpace(2);
+    RRes->addIncoming(Zero, Compute);
+
+    // Decrement the counter
+    Instruction *Decr = BinaryOperator::createSub(Count, One, "decr", Reverse);
+    Count->addIncoming(Decr, Reverse);
+
+    // Compute the Bit that we want to move
+    Instruction *Bit = 
+      BinaryOperator::createAnd(BitsToShift, One, "bit", Reverse);
+
+    // Compute the new value for next iteration.
+    Instruction *NewVal = 
+      BinaryOperator::createLShr(BitsToShift, One, "rshift", Reverse);
+    BitsToShift->addIncoming(NewVal, Reverse);
+
+    // Shift the bit into the low bits of the result.
+    Instruction *NewRes = 
+      BinaryOperator::createShl(RRes, One, "lshift", Reverse);
+    NewRes = BinaryOperator::createOr(NewRes, Bit, "addbit", Reverse);
+    RRes->addIncoming(NewRes, Reverse);
+    
+    // Terminate loop if we've moved all the bits.
+    ICmpInst *Cond = 
+      new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse);
+    new BranchInst(RsltBlk, Reverse, Cond, Reverse);
+
+    // Finally, in the result block, select one of the two results with a PHI
+    // node and return the result;
+    CurBB = RsltBlk;
+    PHINode *BitSelect = new PHINode(Val->getType(), "part_select", CurBB);
+    BitSelect->reserveOperandSpace(2);
+    BitSelect->addIncoming(FRes, Compute);
+    BitSelect->addIncoming(NewRes, Reverse);
+    new ReturnInst(BitSelect, CurBB);
+  }
+
+  // Return a call to the implementation function
+  Value *Args[3];
+  Args[0] = CI->getOperand(0);
+  Args[1] = CI->getOperand(1);
+  Args[2] = CI->getOperand(2);
+  return new CallInst(F, Args, 3, CI->getName(), CI);
+}
+
+
 void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
   Function *Callee = CI->getCalledFunction();
   assert(Callee && "Cannot lower an indirect call!");
@@ -304,6 +475,10 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
     break;
   }
 
+  case Intrinsic::bit_part_select:
+    CI->replaceAllUsesWith(LowerBitPartSelect(CI));
+    break;
+
   case Intrinsic::stacksave:
   case Intrinsic::stackrestore: {
     static bool Warned = false;
index 8e0660478e0b7d222e593311949aaa7a87613344..1bd2aae715ee7d3ced0542e463db160f074404a8 100644 (file)
@@ -2438,6 +2438,11 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
     DAG.setRoot(Tmp.getValue(1));
     return 0;
   }
+  case Intrinsic::bit_part_select: {
+    MVT::ValueType Ty = getValue(I.getOperand(1)).getValueType();
+    setValue(&I, DAG.getTargetConstant(0, Ty));
+    return 0;
+  }
   case Intrinsic::bswap:
     setValue(&I, DAG.getNode(ISD::BSWAP,
                              getValue(I.getOperand(1)).getValueType(),
index 052c963c1501d0b9d4081c444e2cb3815912c5b1..c2fba2905229670e9208a7726278913f605002a2 100644 (file)
@@ -1027,6 +1027,8 @@ void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...) {
         case Intrinsic::bswap:
           if (GotBits < 16 || GotBits % 16 != 0)
             CheckFailed("Intrinsic requires even byte width argument", F);
+          /* FALL THROUGH */
+        case Intrinsic::bit_part_select:
           if (ArgNo == 1) {
             unsigned ResultBits = 
               cast<IntegerType>(FTy->getReturnType())->getBitWidth();
diff --git a/test/CodeGen/Generic/bit-intrinsics.ll b/test/CodeGen/Generic/bit-intrinsics.ll
new file mode 100644 (file)
index 0000000..a260421
--- /dev/null
@@ -0,0 +1,14 @@
+; Make sure this testcase is supported by all code generators. Either the
+; intrinsic is supported natively or IntrinsicLowering provides it.
+; RUN: llvm-as < %s | llc
+
+
+declare i32 @llvm.bit.part.select.i32.i32(i32 %x, i32 %hi, i32 %lo)
+declare i16 @llvm.bit.part.select.i16.i16(i16 %x, i32 %hi, i32 %lo)
+define i32 @bit_part_select(i32 %A, i16 %B) {
+  %a = call i32 @llvm.bit.part.select.i32.i32(i32 %A, i32 8, i32 0)
+  %b = call i16 @llvm.bit.part.select.i16.i16(i16 %B, i32 8, i32 0)
+  %c = zext i16 %b to i32
+  %d = add i32 %a, %c
+  ret i32 %d
+}