X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FIntrinsicLowering.cpp;h=bd3d74d27ac82203590d07e42b49abea3c9e5733;hb=cc41586b9d79532172b37e1f44a9077da4b73fc9;hp=501c12c6fe42b5f5cda6f76a81c2be5d1270589e;hpb=df41353497b7b561649303a6a8bf23a7cb995f5a;p=oota-llvm.git diff --git a/lib/CodeGen/IntrinsicLowering.cpp b/lib/CodeGen/IntrinsicLowering.cpp index 501c12c6fe4..bd3d74d27ac 100644 --- a/lib/CodeGen/IntrinsicLowering.cpp +++ b/lib/CodeGen/IntrinsicLowering.cpp @@ -20,6 +20,7 @@ #include "llvm/Support/Streams.h" #include "llvm/Target/TargetData.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/STLExtras.h" using namespace llvm; template @@ -53,8 +54,8 @@ static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, FunctionType::get(RetTy, ParamTys, false)); } - SmallVector Operands(ArgBegin, ArgEnd); - CallInst *NewCI = new CallInst(FCache, &Operands[0], Operands.size(), + SmallVector Args(ArgBegin, ArgEnd); + CallInst *NewCI = new CallInst(FCache, Args.begin(), Args.end(), CI->getName(), CI); if (!CI->use_empty()) CI->replaceAllUsesWith(NewCI); @@ -98,14 +99,65 @@ void IntrinsicLowering::AddPrototypes(Module &M) { PointerType::get(Type::Int8Ty), Type::Int32Ty, TD.getIntPtrType(), (Type *)0); break; - case Intrinsic::sqrt_f32: - case Intrinsic::sqrt_f64: - if(I->arg_begin()->getType() == Type::FloatTy) + case Intrinsic::sqrt: + switch((int)I->arg_begin()->getType()->getTypeID()) { + case Type::FloatTyID: EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), Type::FloatTy); - else + case Type::DoubleTyID: EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), Type::DoubleTy); + case Type::X86_FP80TyID: + case Type::FP128TyID: + case Type::PPC_FP128TyID: + EnsureFunctionExists(M, "sqrtl", I->arg_begin(), I->arg_end(), + I->arg_begin()->getType()); + } + break; + case Intrinsic::sin: + switch((int)I->arg_begin()->getType()->getTypeID()) { + case Type::FloatTyID: + EnsureFunctionExists(M, "sinf", I->arg_begin(), I->arg_end(), + Type::FloatTy); + case Type::DoubleTyID: + EnsureFunctionExists(M, "sin", I->arg_begin(), I->arg_end(), + Type::DoubleTy); + case Type::X86_FP80TyID: + case Type::FP128TyID: + case Type::PPC_FP128TyID: + EnsureFunctionExists(M, "sinl", I->arg_begin(), I->arg_end(), + I->arg_begin()->getType()); + } + break; + case Intrinsic::cos: + switch((int)I->arg_begin()->getType()->getTypeID()) { + case Type::FloatTyID: + EnsureFunctionExists(M, "cosf", I->arg_begin(), I->arg_end(), + Type::FloatTy); + case Type::DoubleTyID: + EnsureFunctionExists(M, "cos", I->arg_begin(), I->arg_end(), + Type::DoubleTy); + case Type::X86_FP80TyID: + case Type::FP128TyID: + case Type::PPC_FP128TyID: + EnsureFunctionExists(M, "cosl", I->arg_begin(), I->arg_end(), + I->arg_begin()->getType()); + } + break; + case Intrinsic::pow: + switch((int)I->arg_begin()->getType()->getTypeID()) { + case Type::FloatTyID: + EnsureFunctionExists(M, "powf", I->arg_begin(), I->arg_end(), + Type::FloatTy); + case Type::DoubleTyID: + EnsureFunctionExists(M, "pow", I->arg_begin(), I->arg_end(), + Type::DoubleTy); + case Type::X86_FP80TyID: + case Type::FP128TyID: + case Type::PPC_FP128TyID: + EnsureFunctionExists(M, "powl", I->arg_begin(), I->arg_end(), + I->arg_begin()->getType()); + } break; } } @@ -144,7 +196,7 @@ static Value *LowerBSWAP(Value *V, Instruction *IP) { "bswap.and2", IP); Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP); Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP); - V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP); + V = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.i32", IP); break; } case 64: { @@ -208,17 +260,30 @@ static Value *LowerCTPOP(Value *V, Instruction *IP) { }; unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); - - for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) { - Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); - Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP); - Value *VShift = BinaryOperator::createLShr(V, - ConstantInt::get(V->getType(), i), "ctpop.sh", IP); - Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); - V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); + unsigned WordSize = (BitSize + 63) / 64; + Value *Count = ConstantInt::get(V->getType(), 0); + + for (unsigned n = 0; n < WordSize; ++n) { + Value *PartValue = V; + for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize); + i <<= 1, ++ct) { + Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); + Value *LHS = BinaryOperator::createAnd( + PartValue, MaskCst, "cppop.and1", IP); + Value *VShift = BinaryOperator::createLShr(PartValue, + ConstantInt::get(V->getType(), i), "ctpop.sh", IP); + Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); + PartValue = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); + } + Count = BinaryOperator::createAdd(PartValue, Count, "ctpop.part", IP); + if (BitSize > 64) { + V = BinaryOperator::createLShr(V, ConstantInt::get(V->getType(), 64), + "ctpop.part.sh", IP); + BitSize -= 64; + } } - return CastInst::createIntegerCast(V, Type::Int32Ty, false, "ctpop", IP); + return Count; } /// LowerCTLZ - Emit the code to lower ctlz of V before the specified @@ -226,7 +291,7 @@ static Value *LowerCTPOP(Value *V, Instruction *IP) { static Value *LowerCTLZ(Value *V, Instruction *IP) { unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); - for (unsigned i = 1; i != BitSize; i <<= 1) { + for (unsigned i = 1; i < BitSize; i <<= 1) { Value *ShVal = ConstantInt::get(V->getType(), i); ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP); V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP); @@ -403,11 +468,12 @@ static Instruction *LowerPartSelect(CallInst *CI) { } // Return a call to the implementation function - Value *Args[3]; - Args[0] = CI->getOperand(1); - Args[1] = CI->getOperand(2); - Args[2] = CI->getOperand(3); - return new CallInst(F, Args, 3, CI->getName(), CI); + Value *Args[] = { + CI->getOperand(1), + CI->getOperand(2), + CI->getOperand(3) + }; + return new CallInst(F, Args, array_endof(Args), CI->getName(), CI); } /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes @@ -441,57 +507,6 @@ static Instruction *LowerPartSet(CallInst *CI) { // If we haven't defined the impl function yet, do so now if (F->isDeclaration()) { - // Note: the following code is based on code generated by llvm2cpp with - // the following input. This is just *one* example of a generated function. - // The functions vary by bit width of result and first two arguments. - // The generated code has been changed to deal with any bit width not just - // the 32/64 bitwidths used in the above sample. - // - // define i64 @part_set(i64 %Val, i32 %Rep, i32 %Lo, i32 %Hi) { - // entry: - // %is_forward = icmp ult i32 %Lo, %Hi - // %Lo.pn = select i1 %is_forward, i32 %Hi, i32 %Lo - // %Hi.pn = select i1 %is_forward, i32 %Lo, i32 %Hi - // %iftmp.16.0 = sub i32 %Lo.pn, %Hi.pn - // icmp ult i32 %iftmp.16.0, 32 - // br i1 %1, label %cond_true11, label %cond_next19 - // cond_true11: - // %tmp13 = sub i32 32, %iftmp.16.0 - // %tmp14 = lshr i32 -1, %tmp13 - // %tmp16 = and i32 %tmp14, %Rep - // br label %cond_next19 - // cond_next19: - // %iftmp.17.0 = phi i32 [ %tmp16, %cond_true11 ], [ %Rep, %entry ] - // %tmp2021 = zext i32 %iftmp.17.0 to i64 - // icmp ugt i32 %Lo, %Hi - // br i1 %2, label %cond_next60, label %cond_true24 - // cond_true24: - // %tmp25.cast = zext i32 %Hi to i64 - // %tmp26 = lshr i64 -1, %tmp25.cast - // %tmp27.cast = zext i32 %Lo to i64 - // %tmp28 = shl i64 %tmp26, %tmp27.cast - // %tmp28not = xor i64 %tmp28, -1 - // %tmp31 = shl i64 %tmp2021, %tmp27.cast - // %tmp34 = and i64 %tmp28not, %Val - // %Val_addr.064 = or i64 %tmp31, %tmp34 - // ret i64 %Val_addr.064 - // cond_next60: - // %tmp39.cast = zext i32 %Lo to i64 - // %tmp40 = shl i64 -1, %tmp39.cast - // %tmp41.cast = zext i32 %Hi to i64 - // %tmp42 = shl i64 -1, %tmp41.cast - // %tmp45.demorgan = or i64 %tmp42, %tmp40 - // %tmp45 = xor i64 %tmp45.demorgan, -1 - // %tmp47 = and i64 %tmp45, %Val - // %tmp50 = shl i64 %tmp2021, %tmp39.cast - // %tmp52 = sub i32 32, %Hi - // %tmp52.cast = zext i32 %tmp52 to i64 - // %tmp54 = lshr i64 %tmp2021, %tmp52.cast - // %tmp57 = or i64 %tmp50, %tmp47 - // %Val_addr.0 = or i64 %tmp57, %tmp54 - // ret i64 %Val_addr.0 - // } - // Get the arguments for the function. Function::arg_iterator args = F->arg_begin(); Value* Val = args++; Val->setName("Val"); @@ -509,27 +524,31 @@ static Instruction *LowerPartSet(CallInst *CI) { ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits); ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy); ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy); - - BasicBlock* entry = new BasicBlock("entry",F,0); - BasicBlock* large = new BasicBlock("large",F,0); - BasicBlock* small = new BasicBlock("small",F,0); - BasicBlock* forward = new BasicBlock("forward",F,0); - BasicBlock* reverse = new BasicBlock("reverse",F,0); - - // Block entry (entry) + 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* Lo_pn = new SelectInst(is_forward, Hi, Lo, "", entry); - SelectInst* Hi_pn = new SelectInst(is_forward, Lo, Hi, "", entry); - BinaryOperator* NumBits = BinaryOperator::createSub(Lo_pn, Hi_pn, "",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) { - Hi = new ZExtInst(Hi, ValTy, "", entry); - Lo = new ZExtInst(Lo, ValTy, "", entry); + Lo = new ZExtInst(Lo_pn, ValTy, "", entry); } else if (ValBits < 32) { - Hi = new TruncInst(Hi, ValTy, "", entry); - Lo = new TruncInst(Lo, ValTy, "", entry); + 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. @@ -537,15 +556,17 @@ static Instruction *LowerPartSet(CallInst *CI) { new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry); new BranchInst(large, small, is_large, entry); - // Block "large" - BinaryOperator* MaskBits = + // 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); - // Block "small" + // BASIC BLOCK: small PHINode* Rep3 = new PHINode(RepTy, "", small); Rep3->reserveOperandSpace(2); Rep3->addIncoming(Rep2, large); @@ -555,46 +576,70 @@ static Instruction *LowerPartSet(CallInst *CI) { Rep4 = new ZExtInst(Rep3, ValTy, "", small); else if (ValBits < RepBits) Rep4 = new TruncInst(Rep3, ValTy, "", small); - ICmpInst* is_reverse = - new ICmpInst(ICmpInst::ICMP_UGT, Lo, Hi, "", small); - new BranchInst(reverse, forward, is_reverse, small); - - // Block "forward" - Value* t1 = BinaryOperator::createLShr(ValMask, Hi, "", forward); - Value* t2 = BinaryOperator::createShl(t1, Lo, "", forward); - Value* nott2 = BinaryOperator::createXor(t2, ValMask, "", forward); - Value* t3 = BinaryOperator::createShl(Rep4, Lo, "", forward); - Value* t4 = BinaryOperator::createAnd(nott2, Val, "", forward); - Value* FRslt = BinaryOperator::createOr(t3, t4, "", forward); - new ReturnInst(FRslt, forward); - - // Block "reverse" - Value* t5 = BinaryOperator::createShl(ValMask, Lo, "", reverse); - Value* t6 = BinaryOperator::createShl(ValMask, Hi, "", reverse); - Value* t7 = BinaryOperator::createOr(t6, t5, "", reverse); - Value* t8 = BinaryOperator::createXor(t7, ValMask, "", reverse); - Value* t9 = BinaryOperator::createAnd(t8, Val, "", reverse); - Value* t10 = BinaryOperator::createShl(Rep4, Lo, "", reverse); - if (RepBits < ValBits) - RepBitWidth = - cast(ConstantExpr::getZExt(RepBitWidth, ValTy)); - else if (RepBits > ValBits) - RepBitWidth = - cast(ConstantExpr::getTrunc(RepBitWidth, ValTy)); - Value* t11 = BinaryOperator::createSub(RepBitWidth, Hi, "", reverse); - Value* t13 = BinaryOperator::createLShr(Rep4, t11, "",reverse); - Value* t14 = BinaryOperator::createOr(t10, t9, "", reverse); - Value* RRslt = BinaryOperator::createOr(t14, t13, "", reverse); - new ReturnInst(RRslt, reverse); + 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[3]; - Args[0] = CI->getOperand(1); - Args[1] = CI->getOperand(2); - Args[2] = CI->getOperand(3); - Args[3] = CI->getOperand(4); - return new CallInst(F, Args, 4, CI->getName(), CI); + 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); } @@ -713,11 +758,23 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { case Intrinsic::dbg_region_end: case Intrinsic::dbg_func_start: case Intrinsic::dbg_declare: + break; // Simply strip out debugging intrinsics + case Intrinsic::eh_exception: - case Intrinsic::eh_selector: - case Intrinsic::eh_filter: - break; // Simply strip out debugging and eh intrinsics + case Intrinsic::eh_selector_i32: + case Intrinsic::eh_selector_i64: + CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); + break; + + case Intrinsic::eh_typeid_for_i32: + case Intrinsic::eh_typeid_for_i64: + // Return something different to eh_selector. + CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); + break; + case Intrinsic::var_annotation: + break; // Strip out annotate intrinsic + case Intrinsic::memcpy_i32: case Intrinsic::memcpy_i64: { static Constant *MemcpyFCache = 0; @@ -776,16 +833,27 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { MemsetFCache); break; } - case Intrinsic::sqrt_f32: { + case Intrinsic::sqrt: { static Constant *sqrtfFCache = 0; - ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), - Type::FloatTy, sqrtfFCache); - break; - } - case Intrinsic::sqrt_f64: { static Constant *sqrtFCache = 0; - ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), + static Constant *sqrtLDCache = 0; + switch (CI->getOperand(1)->getType()->getTypeID()) { + default: assert(0 && "Invalid type in sqrt"); abort(); + case Type::FloatTyID: + ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), + Type::FloatTy, sqrtfFCache); + break; + case Type::DoubleTyID: + ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), Type::DoubleTy, sqrtFCache); + break; + case Type::X86_FP80TyID: + case Type::FP128TyID: + case Type::PPC_FP128TyID: + ReplaceCallWith("sqrtl", CI, CI->op_begin()+1, CI->op_end(), + CI->getOperand(1)->getType(), sqrtLDCache); + break; + } break; } }