IntrinsicLowering.cpp revision b76efb71d41dc1ae33e47d5d9ef79df25cde0b5d
1//===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the IntrinsicLowering class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Constants.h" 15#include "llvm/DerivedTypes.h" 16#include "llvm/Module.h" 17#include "llvm/Instructions.h" 18#include "llvm/Type.h" 19#include "llvm/CodeGen/IntrinsicLowering.h" 20#include "llvm/Support/Streams.h" 21using namespace llvm; 22 23template <class ArgIt> 24static void EnsureFunctionExists(Module &M, const char *Name, 25 ArgIt ArgBegin, ArgIt ArgEnd, 26 const Type *RetTy) { 27 // Insert a correctly-typed definition now. 28 std::vector<const Type *> ParamTys; 29 for (ArgIt I = ArgBegin; I != ArgEnd; ++I) 30 ParamTys.push_back(I->getType()); 31 M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); 32} 33 34/// ReplaceCallWith - This function is used when we want to lower an intrinsic 35/// call to a call of an external function. This handles hard cases such as 36/// when there was already a prototype for the external function, and if that 37/// prototype doesn't match the arguments we expect to pass in. 38template <class ArgIt> 39static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, 40 ArgIt ArgBegin, ArgIt ArgEnd, 41 const Type *RetTy, Constant *&FCache) { 42 if (!FCache) { 43 // If we haven't already looked up this function, check to see if the 44 // program already contains a function with this name. 45 Module *M = CI->getParent()->getParent()->getParent(); 46 // Get or insert the definition now. 47 std::vector<const Type *> ParamTys; 48 for (ArgIt I = ArgBegin; I != ArgEnd; ++I) 49 ParamTys.push_back((*I)->getType()); 50 FCache = M->getOrInsertFunction(NewFn, 51 FunctionType::get(RetTy, ParamTys, false)); 52 } 53 54 std::vector<Value*> Operands(ArgBegin, ArgEnd); 55 CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI); 56 if (!CI->use_empty()) 57 CI->replaceAllUsesWith(NewCI); 58 return NewCI; 59} 60 61void IntrinsicLowering::AddPrototypes(Module &M) { 62 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 63 if (I->isExternal() && !I->use_empty()) 64 switch (I->getIntrinsicID()) { 65 default: break; 66 case Intrinsic::setjmp: 67 EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), 68 Type::Int32Ty); 69 break; 70 case Intrinsic::longjmp: 71 EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), 72 Type::VoidTy); 73 break; 74 case Intrinsic::siglongjmp: 75 EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), 76 Type::VoidTy); 77 break; 78 case Intrinsic::memcpy_i32: 79 case Intrinsic::memcpy_i64: 80 EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(), 81 I->arg_begin()->getType()); 82 break; 83 case Intrinsic::memmove_i32: 84 case Intrinsic::memmove_i64: 85 EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(), 86 I->arg_begin()->getType()); 87 break; 88 case Intrinsic::memset_i32: 89 case Intrinsic::memset_i64: 90 M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty), 91 PointerType::get(Type::Int8Ty), 92 Type::Int32Ty, (--(--I->arg_end()))->getType(), 93 (Type *)0); 94 break; 95 case Intrinsic::isunordered_f32: 96 case Intrinsic::isunordered_f64: 97 EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(), 98 Type::BoolTy); 99 break; 100 case Intrinsic::sqrt_f32: 101 case Intrinsic::sqrt_f64: 102 if(I->arg_begin()->getType() == Type::FloatTy) 103 EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), 104 Type::FloatTy); 105 else 106 EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), 107 Type::DoubleTy); 108 break; 109 } 110} 111 112/// LowerBSWAP - Emit the code to lower bswap of V before the specified 113/// instruction IP. 114static Value *LowerBSWAP(Value *V, Instruction *IP) { 115 assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); 116 117 unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); 118 119 switch(BitSize) { 120 default: assert(0 && "Unhandled type size of value to byteswap!"); 121 case 16: { 122 Value *Tmp1 = new ShiftInst(Instruction::Shl, V, 123 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP); 124 Value *Tmp2 = new ShiftInst(Instruction::LShr, V, 125 ConstantInt::get(Type::Int8Ty,8),"bswap.1",IP); 126 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP); 127 break; 128 } 129 case 32: { 130 Value *Tmp4 = new ShiftInst(Instruction::Shl, V, 131 ConstantInt::get(Type::Int8Ty,24),"bswap.4", IP); 132 Value *Tmp3 = new ShiftInst(Instruction::Shl, V, 133 ConstantInt::get(Type::Int8Ty,8),"bswap.3",IP); 134 Value *Tmp2 = new ShiftInst(Instruction::LShr, V, 135 ConstantInt::get(Type::Int8Ty,8),"bswap.2",IP); 136 Value *Tmp1 = new ShiftInst(Instruction::LShr, V, 137 ConstantInt::get(Type::Int8Ty,24),"bswap.1", IP); 138 Tmp3 = BinaryOperator::createAnd(Tmp3, 139 ConstantInt::get(Type::Int32Ty, 0xFF0000), 140 "bswap.and3", IP); 141 Tmp2 = BinaryOperator::createAnd(Tmp2, 142 ConstantInt::get(Type::Int32Ty, 0xFF00), 143 "bswap.and2", IP); 144 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP); 145 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP); 146 V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP); 147 break; 148 } 149 case 64: { 150 Value *Tmp8 = new ShiftInst(Instruction::Shl, V, 151 ConstantInt::get(Type::Int8Ty,56),"bswap.8", IP); 152 Value *Tmp7 = new ShiftInst(Instruction::Shl, V, 153 ConstantInt::get(Type::Int8Ty,40),"bswap.7", IP); 154 Value *Tmp6 = new ShiftInst(Instruction::Shl, V, 155 ConstantInt::get(Type::Int8Ty,24),"bswap.6", IP); 156 Value *Tmp5 = new ShiftInst(Instruction::Shl, V, 157 ConstantInt::get(Type::Int8Ty,8),"bswap.5", IP); 158 Value* Tmp4 = new ShiftInst(Instruction::LShr, V, 159 ConstantInt::get(Type::Int8Ty,8),"bswap.4", IP); 160 Value* Tmp3 = new ShiftInst(Instruction::LShr, V, 161 ConstantInt::get(Type::Int8Ty,24),"bswap.3", IP); 162 Value* Tmp2 = new ShiftInst(Instruction::LShr, V, 163 ConstantInt::get(Type::Int8Ty,40),"bswap.2", IP); 164 Value* Tmp1 = new ShiftInst(Instruction::LShr, V, 165 ConstantInt::get(Type::Int8Ty,56),"bswap.1", IP); 166 Tmp7 = BinaryOperator::createAnd(Tmp7, 167 ConstantInt::get(Type::Int64Ty, 168 0xFF000000000000ULL), 169 "bswap.and7", IP); 170 Tmp6 = BinaryOperator::createAnd(Tmp6, 171 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL), 172 "bswap.and6", IP); 173 Tmp5 = BinaryOperator::createAnd(Tmp5, 174 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL), 175 "bswap.and5", IP); 176 Tmp4 = BinaryOperator::createAnd(Tmp4, 177 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL), 178 "bswap.and4", IP); 179 Tmp3 = BinaryOperator::createAnd(Tmp3, 180 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL), 181 "bswap.and3", IP); 182 Tmp2 = BinaryOperator::createAnd(Tmp2, 183 ConstantInt::get(Type::Int64Ty, 0xFF00ULL), 184 "bswap.and2", IP); 185 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP); 186 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP); 187 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP); 188 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP); 189 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP); 190 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP); 191 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP); 192 break; 193 } 194 } 195 return V; 196} 197 198/// LowerCTPOP - Emit the code to lower ctpop of V before the specified 199/// instruction IP. 200static Value *LowerCTPOP(Value *V, Instruction *IP) { 201 assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); 202 203 static const uint64_t MaskValues[6] = { 204 0x5555555555555555ULL, 0x3333333333333333ULL, 205 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, 206 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL 207 }; 208 209 unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); 210 211 for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) { 212 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); 213 Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP); 214 Value *VShift = new ShiftInst(Instruction::LShr, V, 215 ConstantInt::get(Type::Int8Ty, i), "ctpop.sh", IP); 216 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); 217 V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); 218 } 219 220 return V; 221} 222 223/// LowerCTLZ - Emit the code to lower ctlz of V before the specified 224/// instruction IP. 225static Value *LowerCTLZ(Value *V, Instruction *IP) { 226 227 unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); 228 for (unsigned i = 1; i != BitSize; i <<= 1) { 229 Value *ShVal = ConstantInt::get(Type::Int8Ty, i); 230 ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP); 231 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP); 232 } 233 234 V = BinaryOperator::createNot(V, "", IP); 235 return LowerCTPOP(V, IP); 236} 237 238 239 240void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { 241 Function *Callee = CI->getCalledFunction(); 242 assert(Callee && "Cannot lower an indirect call!"); 243 244 switch (Callee->getIntrinsicID()) { 245 case Intrinsic::not_intrinsic: 246 cerr << "Cannot lower a call to a non-intrinsic function '" 247 << Callee->getName() << "'!\n"; 248 abort(); 249 default: 250 cerr << "Error: Code generator does not support intrinsic function '" 251 << Callee->getName() << "'!\n"; 252 abort(); 253 254 // The setjmp/longjmp intrinsics should only exist in the code if it was 255 // never optimized (ie, right out of the CFE), or if it has been hacked on 256 // by the lowerinvoke pass. In both cases, the right thing to do is to 257 // convert the call to an explicit setjmp or longjmp call. 258 case Intrinsic::setjmp: { 259 static Constant *SetjmpFCache = 0; 260 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), 261 Type::Int32Ty, SetjmpFCache); 262 if (CI->getType() != Type::VoidTy) 263 CI->replaceAllUsesWith(V); 264 break; 265 } 266 case Intrinsic::sigsetjmp: 267 if (CI->getType() != Type::VoidTy) 268 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); 269 break; 270 271 case Intrinsic::longjmp: { 272 static Constant *LongjmpFCache = 0; 273 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), 274 Type::VoidTy, LongjmpFCache); 275 break; 276 } 277 278 case Intrinsic::siglongjmp: { 279 // Insert the call to abort 280 static Constant *AbortFCache = 0; 281 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), 282 Type::VoidTy, AbortFCache); 283 break; 284 } 285 case Intrinsic::ctpop_i8: 286 case Intrinsic::ctpop_i16: 287 case Intrinsic::ctpop_i32: 288 case Intrinsic::ctpop_i64: 289 CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI)); 290 break; 291 292 case Intrinsic::bswap_i16: 293 case Intrinsic::bswap_i32: 294 case Intrinsic::bswap_i64: 295 CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI)); 296 break; 297 298 case Intrinsic::ctlz_i8: 299 case Intrinsic::ctlz_i16: 300 case Intrinsic::ctlz_i32: 301 case Intrinsic::ctlz_i64: 302 CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI)); 303 break; 304 305 case Intrinsic::cttz_i8: 306 case Intrinsic::cttz_i16: 307 case Intrinsic::cttz_i32: 308 case Intrinsic::cttz_i64: { 309 // cttz(x) -> ctpop(~X & (X-1)) 310 Value *Src = CI->getOperand(1); 311 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI); 312 Value *SrcM1 = ConstantInt::get(Src->getType(), 1); 313 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI); 314 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI); 315 CI->replaceAllUsesWith(Src); 316 break; 317 } 318 319 case Intrinsic::stacksave: 320 case Intrinsic::stackrestore: { 321 static bool Warned = false; 322 if (!Warned) 323 cerr << "WARNING: this target does not support the llvm.stack" 324 << (Callee->getIntrinsicID() == Intrinsic::stacksave ? 325 "save" : "restore") << " intrinsic.\n"; 326 Warned = true; 327 if (Callee->getIntrinsicID() == Intrinsic::stacksave) 328 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); 329 break; 330 } 331 332 case Intrinsic::returnaddress: 333 case Intrinsic::frameaddress: 334 cerr << "WARNING: this target does not support the llvm." 335 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? 336 "return" : "frame") << "address intrinsic.\n"; 337 CI->replaceAllUsesWith(ConstantPointerNull::get( 338 cast<PointerType>(CI->getType()))); 339 break; 340 341 case Intrinsic::prefetch: 342 break; // Simply strip out prefetches on unsupported architectures 343 344 case Intrinsic::pcmarker: 345 break; // Simply strip out pcmarker on unsupported architectures 346 case Intrinsic::readcyclecounter: { 347 cerr << "WARNING: this target does not support the llvm.readcyclecoun" 348 << "ter intrinsic. It is being lowered to a constant 0\n"; 349 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0)); 350 break; 351 } 352 353 case Intrinsic::dbg_stoppoint: 354 case Intrinsic::dbg_region_start: 355 case Intrinsic::dbg_region_end: 356 case Intrinsic::dbg_func_start: 357 case Intrinsic::dbg_declare: 358 break; // Simply strip out debugging intrinsics 359 360 case Intrinsic::memcpy_i32: { 361 static Constant *MemcpyFCache = 0; 362 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, 363 (*(CI->op_begin()+1))->getType(), MemcpyFCache); 364 break; 365 } 366 case Intrinsic::memcpy_i64: { 367 static Constant *MemcpyFCache = 0; 368 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, 369 (*(CI->op_begin()+1))->getType(), MemcpyFCache); 370 break; 371 } 372 case Intrinsic::memmove_i32: { 373 static Constant *MemmoveFCache = 0; 374 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, 375 (*(CI->op_begin()+1))->getType(), MemmoveFCache); 376 break; 377 } 378 case Intrinsic::memmove_i64: { 379 static Constant *MemmoveFCache = 0; 380 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, 381 (*(CI->op_begin()+1))->getType(), MemmoveFCache); 382 break; 383 } 384 case Intrinsic::memset_i32: { 385 static Constant *MemsetFCache = 0; 386 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, 387 (*(CI->op_begin()+1))->getType(), MemsetFCache); 388 } 389 case Intrinsic::memset_i64: { 390 static Constant *MemsetFCache = 0; 391 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, 392 (*(CI->op_begin()+1))->getType(), MemsetFCache); 393 break; 394 } 395 case Intrinsic::isunordered_f32: 396 case Intrinsic::isunordered_f64: { 397 Value *L = CI->getOperand(1); 398 Value *R = CI->getOperand(2); 399 400 Value *LIsNan = new FCmpInst(FCmpInst::FCMP_ONE, L, L, "LIsNan", CI); 401 Value *RIsNan = new FCmpInst(FCmpInst::FCMP_ONE, R, R, "RIsNan", CI); 402 CI->replaceAllUsesWith( 403 BinaryOperator::create(Instruction::Or, LIsNan, RIsNan, 404 "isunordered", CI)); 405 break; 406 } 407 case Intrinsic::sqrt_f32: { 408 static Constant *sqrtfFCache = 0; 409 ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), 410 Type::FloatTy, sqrtfFCache); 411 break; 412 } 413 case Intrinsic::sqrt_f64: { 414 static Constant *sqrtFCache = 0; 415 ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), 416 Type::DoubleTy, sqrtFCache); 417 break; 418 } 419 } 420 421 assert(CI->use_empty() && 422 "Lowering should have eliminated any uses of the intrinsic call!"); 423 CI->eraseFromParent(); 424} 425