X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FIntrinsicLowering.cpp;h=5c0484f2a7ffa96923b885d586769078058acbb8;hb=e984e504b5f3090ab270cbdab02638ac3a2afb21;hp=b870545008a9b13f749ea2c11e42fcc0b0e8c813;hpb=76c94b616924b19b850e274df0c6485b9395fcb9;p=oota-llvm.git diff --git a/lib/CodeGen/IntrinsicLowering.cpp b/lib/CodeGen/IntrinsicLowering.cpp index b870545008a..5c0484f2a7f 100644 --- a/lib/CodeGen/IntrinsicLowering.cpp +++ b/lib/CodeGen/IntrinsicLowering.cpp @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -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); @@ -80,32 +81,84 @@ void IntrinsicLowering::AddPrototypes(Module &M) { break; case Intrinsic::memcpy_i32: case Intrinsic::memcpy_i64: - M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty), - PointerType::get(Type::Int8Ty), - PointerType::get(Type::Int8Ty), + M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty), + PointerType::getUnqual(Type::Int8Ty), + PointerType::getUnqual(Type::Int8Ty), TD.getIntPtrType(), (Type *)0); break; case Intrinsic::memmove_i32: case Intrinsic::memmove_i64: - M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty), - PointerType::get(Type::Int8Ty), - PointerType::get(Type::Int8Ty), + M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty), + PointerType::getUnqual(Type::Int8Ty), + PointerType::getUnqual(Type::Int8Ty), TD.getIntPtrType(), (Type *)0); break; case Intrinsic::memset_i32: case Intrinsic::memset_i64: - M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty), - PointerType::get(Type::Int8Ty), Type::Int32Ty, + M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty), + PointerType::getUnqual(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 +197,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 +261,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 +292,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); @@ -408,7 +474,7 @@ static Instruction *LowerPartSelect(CallInst *CI) { CI->getOperand(2), CI->getOperand(3) }; - return new CallInst(F, Args, sizeof(Args)/sizeof(Args[0]), CI->getName(), CI); + return new CallInst(F, Args, array_endof(Args), CI->getName(), CI); } /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes @@ -557,11 +623,13 @@ static Instruction *LowerPartSet(CallInst *CI) { Rplcmnt->addIncoming(NewRes, reverse); Rplcmnt->addIncoming(Rep4, small); Value* t0 = CastInst::createIntegerCast(NumBits,ValTy,false,"",result); - Value* t1 = BinaryOperator::createShl(ValMask, t0, "", result); - Value* t2 = BinaryOperator::createShl(t1, Lo, "", result); - Value* t3 = BinaryOperator::createAnd(t2, Val, "", result); - Value* t4 = BinaryOperator::createShl(Rplcmnt, Lo, "", result); - Value* Rslt = BinaryOperator::createOr(t3, t4, "part_set", 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); } @@ -572,7 +640,7 @@ static Instruction *LowerPartSet(CallInst *CI) { CI->getOperand(3), CI->getOperand(4) }; - return new CallInst(F, Args, sizeof(Args)/sizeof(Args[0]), CI->getName(), CI); + return new CallInst(F, Args, array_endof(Args), CI->getName(), CI); } @@ -691,11 +759,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; @@ -754,18 +834,34 @@ 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; } + case Intrinsic::flt_rounds: + // Lower to "round to the nearest" + if (CI->getType() != Type::VoidTy) + CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); + break; } assert(CI->use_empty() &&