-/// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes
-/// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High)
-/// The first two arguments can be any bit width. The result is the same width
-/// as %Value. The operation replaces bits between %Low and %High with the value
-/// in %Replacement. If %Replacement is not the same width, it is truncated or
-/// zero extended as appropriate to fit the bits being replaced. If %Low is
-/// greater than %High then the inverse set of bits are replaced.
-/// @brief Lowering of llvm.bit.part.set intrinsic.
-static Instruction *LowerPartSet(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() != 4 || !FT->getParamType(0)->isInteger() ||
- !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() ||
- !FT->getParamType(3)->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::WeakLinkage);
-
- // If we haven't defined the impl function yet, do so now
- if (F->isDeclaration()) {
- // Get the arguments for the function.
- Function::arg_iterator args = F->arg_begin();
- Value* Val = args++; Val->setName("Val");
- Value* Rep = args++; Rep->setName("Rep");
- Value* Lo = args++; Lo->setName("Lo");
- Value* Hi = args++; Hi->setName("Hi");
-
- // Get some types we need
- const IntegerType* ValTy = cast<IntegerType>(Val->getType());
- const IntegerType* RepTy = cast<IntegerType>(Rep->getType());
- uint32_t ValBits = ValTy->getBitWidth();
- uint32_t RepBits = RepTy->getBitWidth();
-
- // Constant Definitions
- ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits);
- ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy);
- ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy);
- ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1);
- ConstantInt* ValOne = ConstantInt::get(ValTy, 1);
- ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0);
- ConstantInt* ValZero = ConstantInt::get(ValTy, 0);
-
- // Basic blocks we fill in below.
- BasicBlock* entry = new BasicBlock("entry", F, 0);
- BasicBlock* large = new BasicBlock("large", F, 0);
- BasicBlock* small = new BasicBlock("small", F, 0);
- BasicBlock* reverse = new BasicBlock("reverse", F, 0);
- BasicBlock* result = new BasicBlock("result", F, 0);
-
- // BASIC BLOCK: entry
- // First, get the number of bits that we're placing as an i32
- ICmpInst* is_forward =
- new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
- SelectInst* Hi_pn = new SelectInst(is_forward, Hi, Lo, "", entry);
- SelectInst* Lo_pn = new SelectInst(is_forward, Lo, Hi, "", entry);
- BinaryOperator* NumBits = BinaryOperator::createSub(Hi_pn, Lo_pn, "",entry);
- NumBits = BinaryOperator::createAdd(NumBits, One, "", entry);
- // Now, convert Lo and Hi to ValTy bit width
- if (ValBits > 32) {
- Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
- } else if (ValBits < 32) {
- Lo = new TruncInst(Lo_pn, ValTy, "", entry);
- }
- // Determine if the replacement bits are larger than the number of bits we
- // are replacing and deal with it.
- ICmpInst* is_large =
- new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry);
- new BranchInst(large, small, is_large, entry);
-
- // BASIC BLOCK: large
- Instruction* MaskBits =
- BinaryOperator::createSub(RepBitWidth, NumBits, "", large);
- MaskBits = CastInst::createIntegerCast(MaskBits, RepMask->getType(),
- false, "", large);
- BinaryOperator* Mask1 =
- BinaryOperator::createLShr(RepMask, MaskBits, "", large);
- BinaryOperator* Rep2 = BinaryOperator::createAnd(Mask1, Rep, "", large);
- new BranchInst(small, large);
-
- // BASIC BLOCK: small
- PHINode* Rep3 = new PHINode(RepTy, "", small);
- Rep3->reserveOperandSpace(2);
- Rep3->addIncoming(Rep2, large);
- Rep3->addIncoming(Rep, entry);
- Value* Rep4 = Rep3;
- if (ValBits > RepBits)
- Rep4 = new ZExtInst(Rep3, ValTy, "", small);
- else if (ValBits < RepBits)
- Rep4 = new TruncInst(Rep3, ValTy, "", small);
- new BranchInst(result, reverse, is_forward, small);
-
- // BASIC BLOCK: reverse (reverses the bits of the replacement)
- // Set up our loop counter as a PHI so we can decrement on each iteration.
- // We will loop for the number of bits in the replacement value.
- PHINode *Count = new PHINode(Type::Int32Ty, "count", reverse);
- Count->reserveOperandSpace(2);
- Count->addIncoming(NumBits, small);
-
- // Get the value that we are shifting bits out of as a PHI because
- // we'll change this with each iteration.
- PHINode *BitsToShift = new PHINode(Val->getType(), "val", reverse);
- BitsToShift->reserveOperandSpace(2);
- BitsToShift->addIncoming(Rep4, small);
-
- // Get the result of the last computation or zero on first iteration
- PHINode *RRes = new PHINode(Val->getType(), "rres", reverse);
- RRes->reserveOperandSpace(2);
- RRes->addIncoming(ValZero, small);
-
- // Decrement the loop counter by one
- Instruction *Decr = BinaryOperator::createSub(Count, One, "", reverse);
- Count->addIncoming(Decr, reverse);
-
- // Get the bit that we want to move into the result
- Value *Bit = BinaryOperator::createAnd(BitsToShift, ValOne, "", reverse);
-
- // Compute the new value of the bits to shift for the next iteration.
- Value *NewVal = BinaryOperator::createLShr(BitsToShift, ValOne,"", reverse);
- BitsToShift->addIncoming(NewVal, reverse);
-
- // Shift the bit we extracted into the low bit of the result.
- Instruction *NewRes = BinaryOperator::createShl(RRes, ValOne, "", reverse);
- NewRes = BinaryOperator::createOr(NewRes, Bit, "", reverse);
- RRes->addIncoming(NewRes, reverse);
-
- // Terminate loop if we've moved all the bits.
- ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse);
- new BranchInst(result, reverse, Cond, reverse);
-
- // BASIC BLOCK: result
- PHINode *Rplcmnt = new PHINode(Val->getType(), "", result);
- Rplcmnt->reserveOperandSpace(2);
- Rplcmnt->addIncoming(NewRes, reverse);
- Rplcmnt->addIncoming(Rep4, small);
- Value* t0 = CastInst::createIntegerCast(NumBits,ValTy,false,"",result);
- Value* t1 = BinaryOperator::createShl(ValMask, Lo, "", result);
- Value* t2 = BinaryOperator::createNot(t1, "", result);
- Value* t3 = BinaryOperator::createShl(t1, t0, "", result);
- Value* t4 = BinaryOperator::createOr(t2, t3, "", result);
- Value* t5 = BinaryOperator::createAnd(t4, Val, "", result);
- Value* t6 = BinaryOperator::createShl(Rplcmnt, Lo, "", result);
- Value* Rslt = BinaryOperator::createOr(t5, t6, "part_set", result);
- new ReturnInst(Rslt, result);
- }
-
- // Return a call to the implementation function
- Value *Args[] = {
- CI->getOperand(1),
- CI->getOperand(2),
- CI->getOperand(3),
- CI->getOperand(4)
- };
- return new CallInst(F, Args, array_endof(Args), CI->getName(), CI);