InstCombineCalls.cpp revision 27ceaa13f820e33c44e29adc2095dd3e1e69d09f
1//===- InstCombineCalls.cpp -----------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the visitCall and visitInvoke functions. 11// 12//===----------------------------------------------------------------------===// 13 14#include "InstCombine.h" 15#include "llvm/IntrinsicInst.h" 16#include "llvm/Support/CallSite.h" 17#include "llvm/Target/TargetData.h" 18#include "llvm/Analysis/MemoryBuiltins.h" 19#include "llvm/Transforms/Utils/BuildLibCalls.h" 20using namespace llvm; 21 22/// getPromotedType - Return the specified type promoted as it would be to pass 23/// though a va_arg area. 24static const Type *getPromotedType(const Type *Ty) { 25 if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { 26 if (ITy->getBitWidth() < 32) 27 return Type::getInt32Ty(Ty->getContext()); 28 } 29 return Ty; 30} 31 32/// EnforceKnownAlignment - If the specified pointer points to an object that 33/// we control, modify the object's alignment to PrefAlign. This isn't 34/// often possible though. If alignment is important, a more reliable approach 35/// is to simply align all global variables and allocation instructions to 36/// their preferred alignment from the beginning. 37/// 38static unsigned EnforceKnownAlignment(Value *V, 39 unsigned Align, unsigned PrefAlign) { 40 41 User *U = dyn_cast<User>(V); 42 if (!U) return Align; 43 44 switch (Operator::getOpcode(U)) { 45 default: break; 46 case Instruction::BitCast: 47 return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); 48 case Instruction::GetElementPtr: { 49 // If all indexes are zero, it is just the alignment of the base pointer. 50 bool AllZeroOperands = true; 51 for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) 52 if (!isa<Constant>(*i) || 53 !cast<Constant>(*i)->isNullValue()) { 54 AllZeroOperands = false; 55 break; 56 } 57 58 if (AllZeroOperands) { 59 // Treat this like a bitcast. 60 return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); 61 } 62 break; 63 } 64 } 65 66 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 67 // If there is a large requested alignment and we can, bump up the alignment 68 // of the global. 69 if (!GV->isDeclaration()) { 70 if (GV->getAlignment() >= PrefAlign) 71 Align = GV->getAlignment(); 72 else { 73 GV->setAlignment(PrefAlign); 74 Align = PrefAlign; 75 } 76 } 77 } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 78 // If there is a requested alignment and if this is an alloca, round up. 79 if (AI->getAlignment() >= PrefAlign) 80 Align = AI->getAlignment(); 81 else { 82 AI->setAlignment(PrefAlign); 83 Align = PrefAlign; 84 } 85 } 86 87 return Align; 88} 89 90/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that 91/// we can determine, return it, otherwise return 0. If PrefAlign is specified, 92/// and it is more than the alignment of the ultimate object, see if we can 93/// increase the alignment of the ultimate object, making this check succeed. 94unsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, 95 unsigned PrefAlign) { 96 unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : 97 sizeof(PrefAlign) * CHAR_BIT; 98 APInt Mask = APInt::getAllOnesValue(BitWidth); 99 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 100 ComputeMaskedBits(V, Mask, KnownZero, KnownOne); 101 unsigned TrailZ = KnownZero.countTrailingOnes(); 102 unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); 103 104 if (PrefAlign > Align) 105 Align = EnforceKnownAlignment(V, Align, PrefAlign); 106 107 // We don't need to make any adjustment. 108 return Align; 109} 110 111Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { 112 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); 113 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); 114 unsigned MinAlign = std::min(DstAlign, SrcAlign); 115 unsigned CopyAlign = MI->getAlignment(); 116 117 if (CopyAlign < MinAlign) { 118 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), 119 MinAlign, false)); 120 return MI; 121 } 122 123 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with 124 // load/store. 125 ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); 126 if (MemOpLength == 0) return 0; 127 128 // Source and destination pointer types are always "i8*" for intrinsic. See 129 // if the size is something we can handle with a single primitive load/store. 130 // A single load+store correctly handles overlapping memory in the memmove 131 // case. 132 unsigned Size = MemOpLength->getZExtValue(); 133 if (Size == 0) return MI; // Delete this mem transfer. 134 135 if (Size > 8 || (Size&(Size-1))) 136 return 0; // If not 1/2/4/8 bytes, exit. 137 138 // Use an integer load+store unless we can find something better. 139 Type *NewPtrTy = 140 PointerType::getUnqual(IntegerType::get(MI->getContext(), Size<<3)); 141 142 // Memcpy forces the use of i8* for the source and destination. That means 143 // that if you're using memcpy to move one double around, you'll get a cast 144 // from double* to i8*. We'd much rather use a double load+store rather than 145 // an i64 load+store, here because this improves the odds that the source or 146 // dest address will be promotable. See if we can find a better type than the 147 // integer datatype. 148 Value *StrippedDest = MI->getOperand(1)->stripPointerCasts(); 149 if (StrippedDest != MI->getOperand(1)) { 150 const Type *SrcETy = cast<PointerType>(StrippedDest->getType()) 151 ->getElementType(); 152 if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { 153 // The SrcETy might be something like {{{double}}} or [1 x double]. Rip 154 // down through these levels if so. 155 while (!SrcETy->isSingleValueType()) { 156 if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { 157 if (STy->getNumElements() == 1) 158 SrcETy = STy->getElementType(0); 159 else 160 break; 161 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { 162 if (ATy->getNumElements() == 1) 163 SrcETy = ATy->getElementType(); 164 else 165 break; 166 } else 167 break; 168 } 169 170 if (SrcETy->isSingleValueType()) 171 NewPtrTy = PointerType::getUnqual(SrcETy); 172 } 173 } 174 175 176 // If the memcpy/memmove provides better alignment info than we can 177 // infer, use it. 178 SrcAlign = std::max(SrcAlign, CopyAlign); 179 DstAlign = std::max(DstAlign, CopyAlign); 180 181 Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewPtrTy); 182 Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewPtrTy); 183 Instruction *L = new LoadInst(Src, "tmp", false, SrcAlign); 184 InsertNewInstBefore(L, *MI); 185 InsertNewInstBefore(new StoreInst(L, Dest, false, DstAlign), *MI); 186 187 // Set the size of the copy to 0, it will be deleted on the next iteration. 188 MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); 189 return MI; 190} 191 192Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { 193 unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); 194 if (MI->getAlignment() < Alignment) { 195 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), 196 Alignment, false)); 197 return MI; 198 } 199 200 // Extract the length and alignment and fill if they are constant. 201 ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); 202 ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); 203 if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8)) 204 return 0; 205 uint64_t Len = LenC->getZExtValue(); 206 Alignment = MI->getAlignment(); 207 208 // If the length is zero, this is a no-op 209 if (Len == 0) return MI; // memset(d,c,0,a) -> noop 210 211 // memset(s,c,n) -> store s, c (for n=1,2,4,8) 212 if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { 213 const Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. 214 215 Value *Dest = MI->getDest(); 216 Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy)); 217 218 // Alignment 0 is identity for alignment 1 for memset, but not store. 219 if (Alignment == 0) Alignment = 1; 220 221 // Extract the fill value and store. 222 uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; 223 InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), 224 Dest, false, Alignment), *MI); 225 226 // Set the size of the copy to 0, it will be deleted on the next iteration. 227 MI->setLength(Constant::getNullValue(LenC->getType())); 228 return MI; 229 } 230 231 return 0; 232} 233 234/// visitCallInst - CallInst simplification. This mostly only handles folding 235/// of intrinsic instructions. For normal calls, it allows visitCallSite to do 236/// the heavy lifting. 237/// 238Instruction *InstCombiner::visitCallInst(CallInst &CI) { 239 if (isFreeCall(&CI)) 240 return visitFree(CI); 241 242 // If the caller function is nounwind, mark the call as nounwind, even if the 243 // callee isn't. 244 if (CI.getParent()->getParent()->doesNotThrow() && 245 !CI.doesNotThrow()) { 246 CI.setDoesNotThrow(); 247 return &CI; 248 } 249 250 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); 251 if (!II) return visitCallSite(&CI); 252 253 // Intrinsics cannot occur in an invoke, so handle them here instead of in 254 // visitCallSite. 255 if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { 256 bool Changed = false; 257 258 // memmove/cpy/set of zero bytes is a noop. 259 if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { 260 if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); 261 262 if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) 263 if (CI->getZExtValue() == 1) { 264 // Replace the instruction with just byte operations. We would 265 // transform other cases to loads/stores, but we don't know if 266 // alignment is sufficient. 267 } 268 } 269 270 // If we have a memmove and the source operation is a constant global, 271 // then the source and dest pointers can't alias, so we can change this 272 // into a call to memcpy. 273 if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { 274 if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) 275 if (GVSrc->isConstant()) { 276 Module *M = CI.getParent()->getParent()->getParent(); 277 Intrinsic::ID MemCpyID = Intrinsic::memcpy; 278 const Type *Tys[1]; 279 Tys[0] = CI.getOperand(3)->getType(); 280 CI.setOperand(0, 281 Intrinsic::getDeclaration(M, MemCpyID, Tys, 1)); 282 Changed = true; 283 } 284 } 285 286 if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { 287 // memmove(x,x,size) -> noop. 288 if (MTI->getSource() == MTI->getDest()) 289 return EraseInstFromFunction(CI); 290 } 291 292 // If we can determine a pointer alignment that is bigger than currently 293 // set, update the alignment. 294 if (isa<MemTransferInst>(MI)) { 295 if (Instruction *I = SimplifyMemTransfer(MI)) 296 return I; 297 } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { 298 if (Instruction *I = SimplifyMemSet(MSI)) 299 return I; 300 } 301 302 if (Changed) return II; 303 } 304 305 switch (II->getIntrinsicID()) { 306 default: break; 307 case Intrinsic::objectsize: { 308 // We need target data for just about everything so depend on it. 309 if (!TD) break; 310 311 const Type *ReturnTy = CI.getType(); 312 bool Min = (cast<ConstantInt>(II->getOperand(2))->getZExtValue() == 1); 313 314 // Get to the real allocated thing and offset as fast as possible. 315 Value *Op1 = II->getOperand(1)->stripPointerCasts(); 316 317 // If we've stripped down to a single global variable that we 318 // can know the size of then just return that. 319 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) { 320 if (GV->hasDefinitiveInitializer()) { 321 Constant *C = GV->getInitializer(); 322 uint64_t GlobalSize = TD->getTypeAllocSize(C->getType()); 323 return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, GlobalSize)); 324 } else { 325 // Can't determine size of the GV. 326 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); 327 return ReplaceInstUsesWith(CI, RetVal); 328 } 329 } else if (AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) { 330 // Get alloca size. 331 if (AI->getAllocatedType()->isSized()) { 332 uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); 333 if (AI->isArrayAllocation()) { 334 const ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()); 335 if (!C) break; 336 AllocaSize *= C->getZExtValue(); 337 } 338 return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, AllocaSize)); 339 } 340 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) { 341 // Only handle constant GEPs here. 342 if (CE->getOpcode() != Instruction::GetElementPtr) break; 343 GEPOperator *GEP = cast<GEPOperator>(CE); 344 345 // Make sure we're not a constant offset from an external 346 // global. 347 Value *Operand = GEP->getPointerOperand(); 348 Operand = Operand->stripPointerCasts(); 349 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) 350 if (!GV->hasDefinitiveInitializer()) break; 351 352 // Get what we're pointing to and its size. 353 const PointerType *BaseType = 354 cast<PointerType>(Operand->getType()); 355 uint64_t Size = TD->getTypeAllocSize(BaseType->getElementType()); 356 357 // Get the current byte offset into the thing. Use the original 358 // operand in case we're looking through a bitcast. 359 SmallVector<Value*, 8> Ops(CE->op_begin()+1, CE->op_end()); 360 const PointerType *OffsetType = 361 cast<PointerType>(GEP->getPointerOperand()->getType()); 362 uint64_t Offset = TD->getIndexedOffset(OffsetType, &Ops[0], Ops.size()); 363 364 if (Size < Offset) { 365 // Out of bound reference? Negative index normalized to large 366 // index? Just return "I don't know". 367 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); 368 return ReplaceInstUsesWith(CI, RetVal); 369 } 370 371 Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset); 372 return ReplaceInstUsesWith(CI, RetVal); 373 374 } 375 376 // Do not return "I don't know" here. Later optimization passes could 377 // make it possible to evaluate objectsize to a constant. 378 break; 379 } 380 case Intrinsic::bswap: 381 // bswap(bswap(x)) -> x 382 if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) 383 if (Operand->getIntrinsicID() == Intrinsic::bswap) 384 return ReplaceInstUsesWith(CI, Operand->getOperand(1)); 385 386 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) 387 if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) { 388 if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0))) 389 if (Operand->getIntrinsicID() == Intrinsic::bswap) { 390 unsigned C = Operand->getType()->getPrimitiveSizeInBits() - 391 TI->getType()->getPrimitiveSizeInBits(); 392 Value *CV = ConstantInt::get(Operand->getType(), C); 393 Value *V = Builder->CreateLShr(Operand->getOperand(1), CV); 394 return new TruncInst(V, TI->getType()); 395 } 396 } 397 398 break; 399 case Intrinsic::powi: 400 if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) { 401 // powi(x, 0) -> 1.0 402 if (Power->isZero()) 403 return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); 404 // powi(x, 1) -> x 405 if (Power->isOne()) 406 return ReplaceInstUsesWith(CI, II->getOperand(1)); 407 // powi(x, -1) -> 1/x 408 if (Power->isAllOnesValue()) 409 return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), 410 II->getOperand(1)); 411 } 412 break; 413 case Intrinsic::cttz: { 414 // If all bits below the first known one are known zero, 415 // this value is constant. 416 const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); 417 uint32_t BitWidth = IT->getBitWidth(); 418 APInt KnownZero(BitWidth, 0); 419 APInt KnownOne(BitWidth, 0); 420 ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth), 421 KnownZero, KnownOne); 422 unsigned TrailingZeros = KnownOne.countTrailingZeros(); 423 APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros)); 424 if ((Mask & KnownZero) == Mask) 425 return ReplaceInstUsesWith(CI, ConstantInt::get(IT, 426 APInt(BitWidth, TrailingZeros))); 427 428 } 429 break; 430 case Intrinsic::ctlz: { 431 // If all bits above the first known one are known zero, 432 // this value is constant. 433 const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); 434 uint32_t BitWidth = IT->getBitWidth(); 435 APInt KnownZero(BitWidth, 0); 436 APInt KnownOne(BitWidth, 0); 437 ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth), 438 KnownZero, KnownOne); 439 unsigned LeadingZeros = KnownOne.countLeadingZeros(); 440 APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros)); 441 if ((Mask & KnownZero) == Mask) 442 return ReplaceInstUsesWith(CI, ConstantInt::get(IT, 443 APInt(BitWidth, LeadingZeros))); 444 445 } 446 break; 447 case Intrinsic::uadd_with_overflow: { 448 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2); 449 const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); 450 uint32_t BitWidth = IT->getBitWidth(); 451 APInt Mask = APInt::getSignBit(BitWidth); 452 APInt LHSKnownZero(BitWidth, 0); 453 APInt LHSKnownOne(BitWidth, 0); 454 ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); 455 bool LHSKnownNegative = LHSKnownOne[BitWidth - 1]; 456 bool LHSKnownPositive = LHSKnownZero[BitWidth - 1]; 457 458 if (LHSKnownNegative || LHSKnownPositive) { 459 APInt RHSKnownZero(BitWidth, 0); 460 APInt RHSKnownOne(BitWidth, 0); 461 ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); 462 bool RHSKnownNegative = RHSKnownOne[BitWidth - 1]; 463 bool RHSKnownPositive = RHSKnownZero[BitWidth - 1]; 464 if (LHSKnownNegative && RHSKnownNegative) { 465 // The sign bit is set in both cases: this MUST overflow. 466 // Create a simple add instruction, and insert it into the struct. 467 Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI); 468 Worklist.Add(Add); 469 Constant *V[] = { 470 UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext()) 471 }; 472 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 473 return InsertValueInst::Create(Struct, Add, 0); 474 } 475 476 if (LHSKnownPositive && RHSKnownPositive) { 477 // The sign bit is clear in both cases: this CANNOT overflow. 478 // Create a simple add instruction, and insert it into the struct. 479 Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI); 480 Worklist.Add(Add); 481 Constant *V[] = { 482 UndefValue::get(LHS->getType()), 483 ConstantInt::getFalse(II->getContext()) 484 }; 485 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 486 return InsertValueInst::Create(Struct, Add, 0); 487 } 488 } 489 } 490 // FALL THROUGH uadd into sadd 491 case Intrinsic::sadd_with_overflow: 492 // Canonicalize constants into the RHS. 493 if (isa<Constant>(II->getOperand(1)) && 494 !isa<Constant>(II->getOperand(2))) { 495 Value *LHS = II->getOperand(1); 496 II->setOperand(1, II->getOperand(2)); 497 II->setOperand(2, LHS); 498 return II; 499 } 500 501 // X + undef -> undef 502 if (isa<UndefValue>(II->getOperand(2))) 503 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); 504 505 if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) { 506 // X + 0 -> {X, false} 507 if (RHS->isZero()) { 508 Constant *V[] = { 509 UndefValue::get(II->getOperand(0)->getType()), 510 ConstantInt::getFalse(II->getContext()) 511 }; 512 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 513 return InsertValueInst::Create(Struct, II->getOperand(1), 0); 514 } 515 } 516 break; 517 case Intrinsic::usub_with_overflow: 518 case Intrinsic::ssub_with_overflow: 519 // undef - X -> undef 520 // X - undef -> undef 521 if (isa<UndefValue>(II->getOperand(1)) || 522 isa<UndefValue>(II->getOperand(2))) 523 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); 524 525 if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) { 526 // X - 0 -> {X, false} 527 if (RHS->isZero()) { 528 Constant *V[] = { 529 UndefValue::get(II->getOperand(1)->getType()), 530 ConstantInt::getFalse(II->getContext()) 531 }; 532 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 533 return InsertValueInst::Create(Struct, II->getOperand(1), 0); 534 } 535 } 536 break; 537 case Intrinsic::umul_with_overflow: 538 case Intrinsic::smul_with_overflow: 539 // Canonicalize constants into the RHS. 540 if (isa<Constant>(II->getOperand(1)) && 541 !isa<Constant>(II->getOperand(2))) { 542 Value *LHS = II->getOperand(1); 543 II->setOperand(1, II->getOperand(2)); 544 II->setOperand(2, LHS); 545 return II; 546 } 547 548 // X * undef -> undef 549 if (isa<UndefValue>(II->getOperand(2))) 550 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); 551 552 if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(2))) { 553 // X*0 -> {0, false} 554 if (RHSI->isZero()) 555 return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType())); 556 557 // X * 1 -> {X, false} 558 if (RHSI->equalsInt(1)) { 559 Constant *V[] = { 560 UndefValue::get(II->getOperand(1)->getType()), 561 ConstantInt::getFalse(II->getContext()) 562 }; 563 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 564 return InsertValueInst::Create(Struct, II->getOperand(1), 0); 565 } 566 } 567 break; 568 case Intrinsic::ppc_altivec_lvx: 569 case Intrinsic::ppc_altivec_lvxl: 570 case Intrinsic::x86_sse_loadu_ps: 571 case Intrinsic::x86_sse2_loadu_pd: 572 case Intrinsic::x86_sse2_loadu_dq: 573 // Turn PPC lvx -> load if the pointer is known aligned. 574 // Turn X86 loadups -> load if the pointer is known aligned. 575 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { 576 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), 577 PointerType::getUnqual(II->getType())); 578 return new LoadInst(Ptr); 579 } 580 break; 581 case Intrinsic::ppc_altivec_stvx: 582 case Intrinsic::ppc_altivec_stvxl: 583 // Turn stvx -> store if the pointer is known aligned. 584 if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { 585 const Type *OpPtrTy = 586 PointerType::getUnqual(II->getOperand(1)->getType()); 587 Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy); 588 return new StoreInst(II->getOperand(1), Ptr); 589 } 590 break; 591 case Intrinsic::x86_sse_storeu_ps: 592 case Intrinsic::x86_sse2_storeu_pd: 593 case Intrinsic::x86_sse2_storeu_dq: 594 // Turn X86 storeu -> store if the pointer is known aligned. 595 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { 596 const Type *OpPtrTy = 597 PointerType::getUnqual(II->getOperand(2)->getType()); 598 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy); 599 return new StoreInst(II->getOperand(2), Ptr); 600 } 601 break; 602 603 case Intrinsic::x86_sse_cvttss2si: { 604 // These intrinsics only demands the 0th element of its input vector. If 605 // we can simplify the input based on that, do so now. 606 unsigned VWidth = 607 cast<VectorType>(II->getOperand(1)->getType())->getNumElements(); 608 APInt DemandedElts(VWidth, 1); 609 APInt UndefElts(VWidth, 0); 610 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, 611 UndefElts)) { 612 II->setOperand(1, V); 613 return II; 614 } 615 break; 616 } 617 618 case Intrinsic::ppc_altivec_vperm: 619 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. 620 if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { 621 assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); 622 623 // Check that all of the elements are integer constants or undefs. 624 bool AllEltsOk = true; 625 for (unsigned i = 0; i != 16; ++i) { 626 if (!isa<ConstantInt>(Mask->getOperand(i)) && 627 !isa<UndefValue>(Mask->getOperand(i))) { 628 AllEltsOk = false; 629 break; 630 } 631 } 632 633 if (AllEltsOk) { 634 // Cast the input vectors to byte vectors. 635 Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType()); 636 Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType()); 637 Value *Result = UndefValue::get(Op0->getType()); 638 639 // Only extract each element once. 640 Value *ExtractedElts[32]; 641 memset(ExtractedElts, 0, sizeof(ExtractedElts)); 642 643 for (unsigned i = 0; i != 16; ++i) { 644 if (isa<UndefValue>(Mask->getOperand(i))) 645 continue; 646 unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); 647 Idx &= 31; // Match the hardware behavior. 648 649 if (ExtractedElts[Idx] == 0) { 650 ExtractedElts[Idx] = 651 Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1, 652 ConstantInt::get(Type::getInt32Ty(II->getContext()), 653 Idx&15, false), "tmp"); 654 } 655 656 // Insert this value into the result vector. 657 Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], 658 ConstantInt::get(Type::getInt32Ty(II->getContext()), 659 i, false), "tmp"); 660 } 661 return CastInst::Create(Instruction::BitCast, Result, CI.getType()); 662 } 663 } 664 break; 665 666 case Intrinsic::stackrestore: { 667 // If the save is right next to the restore, remove the restore. This can 668 // happen when variable allocas are DCE'd. 669 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { 670 if (SS->getIntrinsicID() == Intrinsic::stacksave) { 671 BasicBlock::iterator BI = SS; 672 if (&*++BI == II) 673 return EraseInstFromFunction(CI); 674 } 675 } 676 677 // Scan down this block to see if there is another stack restore in the 678 // same block without an intervening call/alloca. 679 BasicBlock::iterator BI = II; 680 TerminatorInst *TI = II->getParent()->getTerminator(); 681 bool CannotRemove = false; 682 for (++BI; &*BI != TI; ++BI) { 683 if (isa<AllocaInst>(BI) || isMalloc(BI)) { 684 CannotRemove = true; 685 break; 686 } 687 if (CallInst *BCI = dyn_cast<CallInst>(BI)) { 688 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { 689 // If there is a stackrestore below this one, remove this one. 690 if (II->getIntrinsicID() == Intrinsic::stackrestore) 691 return EraseInstFromFunction(CI); 692 // Otherwise, ignore the intrinsic. 693 } else { 694 // If we found a non-intrinsic call, we can't remove the stack 695 // restore. 696 CannotRemove = true; 697 break; 698 } 699 } 700 } 701 702 // If the stack restore is in a return/unwind block and if there are no 703 // allocas or calls between the restore and the return, nuke the restore. 704 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) 705 return EraseInstFromFunction(CI); 706 break; 707 } 708 } 709 710 return visitCallSite(II); 711} 712 713// InvokeInst simplification 714// 715Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { 716 return visitCallSite(&II); 717} 718 719/// isSafeToEliminateVarargsCast - If this cast does not affect the value 720/// passed through the varargs area, we can eliminate the use of the cast. 721static bool isSafeToEliminateVarargsCast(const CallSite CS, 722 const CastInst * const CI, 723 const TargetData * const TD, 724 const int ix) { 725 if (!CI->isLosslessCast()) 726 return false; 727 728 // The size of ByVal arguments is derived from the type, so we 729 // can't change to a type with a different size. If the size were 730 // passed explicitly we could avoid this check. 731 if (!CS.paramHasAttr(ix, Attribute::ByVal)) 732 return true; 733 734 const Type* SrcTy = 735 cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); 736 const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); 737 if (!SrcTy->isSized() || !DstTy->isSized()) 738 return false; 739 if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) 740 return false; 741 return true; 742} 743 744// Try to fold some different type of calls here. 745// Currently we're only working with the checking functions, memcpy_chk, 746// mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk, 747// strcat_chk and strncat_chk. 748Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) { 749 if (CI->getCalledFunction() == 0) return 0; 750 751 StringRef Name = CI->getCalledFunction()->getName(); 752 BasicBlock *BB = CI->getParent(); 753 IRBuilder<> B(CI->getParent()->getContext()); 754 755 // Set the builder to the instruction after the call. 756 B.SetInsertPoint(BB, CI); 757 758 if (Name == "__memcpy_chk") { 759 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); 760 if (!SizeCI) 761 return 0; 762 ConstantInt *SizeArg = dyn_cast<ConstantInt>(CI->getOperand(3)); 763 if (!SizeArg) 764 return 0; 765 if (SizeCI->isAllOnesValue() || 766 SizeCI->getZExtValue() <= SizeArg->getZExtValue()) { 767 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 768 1, B, TD); 769 return ReplaceInstUsesWith(*CI, CI->getOperand(1)); 770 } 771 return 0; 772 } 773 774 // Should be similar to memcpy. 775 if (Name == "__mempcpy_chk") { 776 return 0; 777 } 778 779 if (Name == "__memmove_chk") { 780 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); 781 if (!SizeCI) 782 return 0; 783 ConstantInt *SizeArg = dyn_cast<ConstantInt>(CI->getOperand(3)); 784 if (!SizeArg) 785 return 0; 786 if (SizeCI->isAllOnesValue() || 787 SizeCI->getZExtValue() <= SizeArg->getZExtValue()) { 788 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), 789 1, B, TD); 790 return ReplaceInstUsesWith(*CI, CI->getOperand(1)); 791 } 792 return 0; 793 } 794 795 if (Name == "__memset_chk") { 796 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); 797 if (!SizeCI) 798 return 0; 799 ConstantInt *SizeArg = dyn_cast<ConstantInt>(CI->getOperand(3)); 800 if (!SizeArg) 801 return 0; 802 if (SizeCI->isAllOnesValue() || 803 SizeCI->getZExtValue() <= SizeArg->getZExtValue()) { 804 Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(), 805 false); 806 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD); 807 return ReplaceInstUsesWith(*CI, CI->getOperand(1)); 808 } 809 return 0; 810 } 811 812 if (Name == "__strcpy_chk") { 813 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3)); 814 if (!SizeCI) 815 return 0; 816 // If a) we don't have any length information, or b) we know this will 817 // fit then just lower to a plain strcpy. Otherwise we'll keep our 818 // strcpy_chk call which may fail at runtime if the size is too long. 819 // TODO: It might be nice to get a maximum length out of the possible 820 // string lengths for varying. 821 if (SizeCI->isAllOnesValue() || 822 SizeCI->getZExtValue() >= GetStringLength(CI->getOperand(2))) { 823 Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD); 824 return ReplaceInstUsesWith(*CI, Ret); 825 } 826 return 0; 827 } 828 829 // Should be similar to strcpy. 830 if (Name == "__stpcpy_chk") { 831 return 0; 832 } 833 834 if (Name == "__strncpy_chk") { 835 ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4)); 836 if (!SizeCI) 837 return 0; 838 ConstantInt *SizeArg = dyn_cast<ConstantInt>(CI->getOperand(3)); 839 if (!SizeArg) 840 return 0; 841 if (SizeCI->isAllOnesValue() || 842 SizeCI->getZExtValue() <= SizeArg->getZExtValue()) { 843 Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD); 844 return ReplaceInstUsesWith(*CI, Ret); 845 } 846 return 0; 847 } 848 849 if (Name == "__strcat_chk") { 850 return 0; 851 } 852 853 if (Name == "__strncat_chk") { 854 return 0; 855 } 856 857 return 0; 858} 859 860// visitCallSite - Improvements for call and invoke instructions. 861// 862Instruction *InstCombiner::visitCallSite(CallSite CS) { 863 bool Changed = false; 864 865 // If the callee is a constexpr cast of a function, attempt to move the cast 866 // to the arguments of the call/invoke. 867 if (transformConstExprCastCall(CS)) return 0; 868 869 Value *Callee = CS.getCalledValue(); 870 871 if (Function *CalleeF = dyn_cast<Function>(Callee)) 872 // If the call and callee calling conventions don't match, this call must 873 // be unreachable, as the call is undefined. 874 if (CalleeF->getCallingConv() != CS.getCallingConv() && 875 // Only do this for calls to a function with a body. A prototype may 876 // not actually end up matching the implementation's calling conv for a 877 // variety of reasons (e.g. it may be written in assembly). 878 !CalleeF->isDeclaration()) { 879 Instruction *OldCall = CS.getInstruction(); 880 new StoreInst(ConstantInt::getTrue(Callee->getContext()), 881 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), 882 OldCall); 883 // If OldCall dues not return void then replaceAllUsesWith undef. 884 // This allows ValueHandlers and custom metadata to adjust itself. 885 if (!OldCall->getType()->isVoidTy()) 886 OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); 887 if (isa<CallInst>(OldCall)) 888 return EraseInstFromFunction(*OldCall); 889 890 // We cannot remove an invoke, because it would change the CFG, just 891 // change the callee to a null pointer. 892 cast<InvokeInst>(OldCall)->setOperand(0, 893 Constant::getNullValue(CalleeF->getType())); 894 return 0; 895 } 896 897 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { 898 // This instruction is not reachable, just remove it. We insert a store to 899 // undef so that we know that this code is not reachable, despite the fact 900 // that we can't modify the CFG here. 901 new StoreInst(ConstantInt::getTrue(Callee->getContext()), 902 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), 903 CS.getInstruction()); 904 905 // If CS dues not return void then replaceAllUsesWith undef. 906 // This allows ValueHandlers and custom metadata to adjust itself. 907 if (!CS.getInstruction()->getType()->isVoidTy()) 908 CS.getInstruction()-> 909 replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); 910 911 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { 912 // Don't break the CFG, insert a dummy cond branch. 913 BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), 914 ConstantInt::getTrue(Callee->getContext()), II); 915 } 916 return EraseInstFromFunction(*CS.getInstruction()); 917 } 918 919 if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) 920 if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) 921 if (In->getIntrinsicID() == Intrinsic::init_trampoline) 922 return transformCallThroughTrampoline(CS); 923 924 const PointerType *PTy = cast<PointerType>(Callee->getType()); 925 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 926 if (FTy->isVarArg()) { 927 int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); 928 // See if we can optimize any arguments passed through the varargs area of 929 // the call. 930 for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), 931 E = CS.arg_end(); I != E; ++I, ++ix) { 932 CastInst *CI = dyn_cast<CastInst>(*I); 933 if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { 934 *I = CI->getOperand(0); 935 Changed = true; 936 } 937 } 938 } 939 940 if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { 941 // Inline asm calls cannot throw - mark them 'nounwind'. 942 CS.setDoesNotThrow(); 943 Changed = true; 944 } 945 946 // Try to optimize the call if possible, we require TargetData for most of 947 // this. None of these calls are seen as possibly dead so go ahead and 948 // delete the instruction now. 949 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) { 950 Instruction *I = tryOptimizeCall(CI, TD); 951 return I ? EraseInstFromFunction(*I): 0; 952 } 953 954 return Changed ? CS.getInstruction() : 0; 955} 956 957// transformConstExprCastCall - If the callee is a constexpr cast of a function, 958// attempt to move the cast to the arguments of the call/invoke. 959// 960bool InstCombiner::transformConstExprCastCall(CallSite CS) { 961 if (!isa<ConstantExpr>(CS.getCalledValue())) return false; 962 ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); 963 if (CE->getOpcode() != Instruction::BitCast || 964 !isa<Function>(CE->getOperand(0))) 965 return false; 966 Function *Callee = cast<Function>(CE->getOperand(0)); 967 Instruction *Caller = CS.getInstruction(); 968 const AttrListPtr &CallerPAL = CS.getAttributes(); 969 970 // Okay, this is a cast from a function to a different type. Unless doing so 971 // would cause a type conversion of one of our arguments, change this call to 972 // be a direct call with arguments casted to the appropriate types. 973 // 974 const FunctionType *FT = Callee->getFunctionType(); 975 const Type *OldRetTy = Caller->getType(); 976 const Type *NewRetTy = FT->getReturnType(); 977 978 if (NewRetTy->isStructTy()) 979 return false; // TODO: Handle multiple return values. 980 981 // Check to see if we are changing the return type... 982 if (OldRetTy != NewRetTy) { 983 if (Callee->isDeclaration() && 984 // Conversion is ok if changing from one pointer type to another or from 985 // a pointer to an integer of the same size. 986 !((OldRetTy->isPointerTy() || !TD || 987 OldRetTy == TD->getIntPtrType(Caller->getContext())) && 988 (NewRetTy->isPointerTy() || !TD || 989 NewRetTy == TD->getIntPtrType(Caller->getContext())))) 990 return false; // Cannot transform this return value. 991 992 if (!Caller->use_empty() && 993 // void -> non-void is handled specially 994 !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy)) 995 return false; // Cannot transform this return value. 996 997 if (!CallerPAL.isEmpty() && !Caller->use_empty()) { 998 Attributes RAttrs = CallerPAL.getRetAttributes(); 999 if (RAttrs & Attribute::typeIncompatible(NewRetTy)) 1000 return false; // Attribute not compatible with transformed value. 1001 } 1002 1003 // If the callsite is an invoke instruction, and the return value is used by 1004 // a PHI node in a successor, we cannot change the return type of the call 1005 // because there is no place to put the cast instruction (without breaking 1006 // the critical edge). Bail out in this case. 1007 if (!Caller->use_empty()) 1008 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) 1009 for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); 1010 UI != E; ++UI) 1011 if (PHINode *PN = dyn_cast<PHINode>(*UI)) 1012 if (PN->getParent() == II->getNormalDest() || 1013 PN->getParent() == II->getUnwindDest()) 1014 return false; 1015 } 1016 1017 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); 1018 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); 1019 1020 CallSite::arg_iterator AI = CS.arg_begin(); 1021 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { 1022 const Type *ParamTy = FT->getParamType(i); 1023 const Type *ActTy = (*AI)->getType(); 1024 1025 if (!CastInst::isCastable(ActTy, ParamTy)) 1026 return false; // Cannot transform this parameter value. 1027 1028 if (CallerPAL.getParamAttributes(i + 1) 1029 & Attribute::typeIncompatible(ParamTy)) 1030 return false; // Attribute not compatible with transformed value. 1031 1032 // Converting from one pointer type to another or between a pointer and an 1033 // integer of the same size is safe even if we do not have a body. 1034 bool isConvertible = ActTy == ParamTy || 1035 (TD && ((ParamTy->isPointerTy() || 1036 ParamTy == TD->getIntPtrType(Caller->getContext())) && 1037 (ActTy->isPointerTy() || 1038 ActTy == TD->getIntPtrType(Caller->getContext())))); 1039 if (Callee->isDeclaration() && !isConvertible) return false; 1040 } 1041 1042 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && 1043 Callee->isDeclaration()) 1044 return false; // Do not delete arguments unless we have a function body. 1045 1046 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && 1047 !CallerPAL.isEmpty()) 1048 // In this case we have more arguments than the new function type, but we 1049 // won't be dropping them. Check that these extra arguments have attributes 1050 // that are compatible with being a vararg call argument. 1051 for (unsigned i = CallerPAL.getNumSlots(); i; --i) { 1052 if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) 1053 break; 1054 Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; 1055 if (PAttrs & Attribute::VarArgsIncompatible) 1056 return false; 1057 } 1058 1059 // Okay, we decided that this is a safe thing to do: go ahead and start 1060 // inserting cast instructions as necessary... 1061 std::vector<Value*> Args; 1062 Args.reserve(NumActualArgs); 1063 SmallVector<AttributeWithIndex, 8> attrVec; 1064 attrVec.reserve(NumCommonArgs); 1065 1066 // Get any return attributes. 1067 Attributes RAttrs = CallerPAL.getRetAttributes(); 1068 1069 // If the return value is not being used, the type may not be compatible 1070 // with the existing attributes. Wipe out any problematic attributes. 1071 RAttrs &= ~Attribute::typeIncompatible(NewRetTy); 1072 1073 // Add the new return attributes. 1074 if (RAttrs) 1075 attrVec.push_back(AttributeWithIndex::get(0, RAttrs)); 1076 1077 AI = CS.arg_begin(); 1078 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { 1079 const Type *ParamTy = FT->getParamType(i); 1080 if ((*AI)->getType() == ParamTy) { 1081 Args.push_back(*AI); 1082 } else { 1083 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, 1084 false, ParamTy, false); 1085 Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp")); 1086 } 1087 1088 // Add any parameter attributes. 1089 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) 1090 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); 1091 } 1092 1093 // If the function takes more arguments than the call was taking, add them 1094 // now. 1095 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) 1096 Args.push_back(Constant::getNullValue(FT->getParamType(i))); 1097 1098 // If we are removing arguments to the function, emit an obnoxious warning. 1099 if (FT->getNumParams() < NumActualArgs) { 1100 if (!FT->isVarArg()) { 1101 errs() << "WARNING: While resolving call to function '" 1102 << Callee->getName() << "' arguments were dropped!\n"; 1103 } else { 1104 // Add all of the arguments in their promoted form to the arg list. 1105 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { 1106 const Type *PTy = getPromotedType((*AI)->getType()); 1107 if (PTy != (*AI)->getType()) { 1108 // Must promote to pass through va_arg area! 1109 Instruction::CastOps opcode = 1110 CastInst::getCastOpcode(*AI, false, PTy, false); 1111 Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp")); 1112 } else { 1113 Args.push_back(*AI); 1114 } 1115 1116 // Add any parameter attributes. 1117 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) 1118 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); 1119 } 1120 } 1121 } 1122 1123 if (Attributes FnAttrs = CallerPAL.getFnAttributes()) 1124 attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); 1125 1126 if (NewRetTy->isVoidTy()) 1127 Caller->setName(""); // Void type should not have a name. 1128 1129 const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(), 1130 attrVec.end()); 1131 1132 Instruction *NC; 1133 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { 1134 NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), 1135 Args.begin(), Args.end(), 1136 Caller->getName(), Caller); 1137 cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); 1138 cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); 1139 } else { 1140 NC = CallInst::Create(Callee, Args.begin(), Args.end(), 1141 Caller->getName(), Caller); 1142 CallInst *CI = cast<CallInst>(Caller); 1143 if (CI->isTailCall()) 1144 cast<CallInst>(NC)->setTailCall(); 1145 cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); 1146 cast<CallInst>(NC)->setAttributes(NewCallerPAL); 1147 } 1148 1149 // Insert a cast of the return type as necessary. 1150 Value *NV = NC; 1151 if (OldRetTy != NV->getType() && !Caller->use_empty()) { 1152 if (!NV->getType()->isVoidTy()) { 1153 Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, 1154 OldRetTy, false); 1155 NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); 1156 1157 // If this is an invoke instruction, we should insert it after the first 1158 // non-phi, instruction in the normal successor block. 1159 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { 1160 BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); 1161 InsertNewInstBefore(NC, *I); 1162 } else { 1163 // Otherwise, it's a call, just insert cast right after the call instr 1164 InsertNewInstBefore(NC, *Caller); 1165 } 1166 Worklist.AddUsersToWorkList(*Caller); 1167 } else { 1168 NV = UndefValue::get(Caller->getType()); 1169 } 1170 } 1171 1172 1173 if (!Caller->use_empty()) 1174 Caller->replaceAllUsesWith(NV); 1175 1176 EraseInstFromFunction(*Caller); 1177 return true; 1178} 1179 1180// transformCallThroughTrampoline - Turn a call to a function created by the 1181// init_trampoline intrinsic into a direct call to the underlying function. 1182// 1183Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { 1184 Value *Callee = CS.getCalledValue(); 1185 const PointerType *PTy = cast<PointerType>(Callee->getType()); 1186 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1187 const AttrListPtr &Attrs = CS.getAttributes(); 1188 1189 // If the call already has the 'nest' attribute somewhere then give up - 1190 // otherwise 'nest' would occur twice after splicing in the chain. 1191 if (Attrs.hasAttrSomewhere(Attribute::Nest)) 1192 return 0; 1193 1194 IntrinsicInst *Tramp = 1195 cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); 1196 1197 Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); 1198 const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); 1199 const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); 1200 1201 const AttrListPtr &NestAttrs = NestF->getAttributes(); 1202 if (!NestAttrs.isEmpty()) { 1203 unsigned NestIdx = 1; 1204 const Type *NestTy = 0; 1205 Attributes NestAttr = Attribute::None; 1206 1207 // Look for a parameter marked with the 'nest' attribute. 1208 for (FunctionType::param_iterator I = NestFTy->param_begin(), 1209 E = NestFTy->param_end(); I != E; ++NestIdx, ++I) 1210 if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) { 1211 // Record the parameter type and any other attributes. 1212 NestTy = *I; 1213 NestAttr = NestAttrs.getParamAttributes(NestIdx); 1214 break; 1215 } 1216 1217 if (NestTy) { 1218 Instruction *Caller = CS.getInstruction(); 1219 std::vector<Value*> NewArgs; 1220 NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); 1221 1222 SmallVector<AttributeWithIndex, 8> NewAttrs; 1223 NewAttrs.reserve(Attrs.getNumSlots() + 1); 1224 1225 // Insert the nest argument into the call argument list, which may 1226 // mean appending it. Likewise for attributes. 1227 1228 // Add any result attributes. 1229 if (Attributes Attr = Attrs.getRetAttributes()) 1230 NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); 1231 1232 { 1233 unsigned Idx = 1; 1234 CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); 1235 do { 1236 if (Idx == NestIdx) { 1237 // Add the chain argument and attributes. 1238 Value *NestVal = Tramp->getOperand(3); 1239 if (NestVal->getType() != NestTy) 1240 NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); 1241 NewArgs.push_back(NestVal); 1242 NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); 1243 } 1244 1245 if (I == E) 1246 break; 1247 1248 // Add the original argument and attributes. 1249 NewArgs.push_back(*I); 1250 if (Attributes Attr = Attrs.getParamAttributes(Idx)) 1251 NewAttrs.push_back 1252 (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); 1253 1254 ++Idx, ++I; 1255 } while (1); 1256 } 1257 1258 // Add any function attributes. 1259 if (Attributes Attr = Attrs.getFnAttributes()) 1260 NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); 1261 1262 // The trampoline may have been bitcast to a bogus type (FTy). 1263 // Handle this by synthesizing a new function type, equal to FTy 1264 // with the chain parameter inserted. 1265 1266 std::vector<const Type*> NewTypes; 1267 NewTypes.reserve(FTy->getNumParams()+1); 1268 1269 // Insert the chain's type into the list of parameter types, which may 1270 // mean appending it. 1271 { 1272 unsigned Idx = 1; 1273 FunctionType::param_iterator I = FTy->param_begin(), 1274 E = FTy->param_end(); 1275 1276 do { 1277 if (Idx == NestIdx) 1278 // Add the chain's type. 1279 NewTypes.push_back(NestTy); 1280 1281 if (I == E) 1282 break; 1283 1284 // Add the original type. 1285 NewTypes.push_back(*I); 1286 1287 ++Idx, ++I; 1288 } while (1); 1289 } 1290 1291 // Replace the trampoline call with a direct call. Let the generic 1292 // code sort out any function type mismatches. 1293 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, 1294 FTy->isVarArg()); 1295 Constant *NewCallee = 1296 NestF->getType() == PointerType::getUnqual(NewFTy) ? 1297 NestF : ConstantExpr::getBitCast(NestF, 1298 PointerType::getUnqual(NewFTy)); 1299 const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(), 1300 NewAttrs.end()); 1301 1302 Instruction *NewCaller; 1303 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { 1304 NewCaller = InvokeInst::Create(NewCallee, 1305 II->getNormalDest(), II->getUnwindDest(), 1306 NewArgs.begin(), NewArgs.end(), 1307 Caller->getName(), Caller); 1308 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); 1309 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); 1310 } else { 1311 NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), 1312 Caller->getName(), Caller); 1313 if (cast<CallInst>(Caller)->isTailCall()) 1314 cast<CallInst>(NewCaller)->setTailCall(); 1315 cast<CallInst>(NewCaller)-> 1316 setCallingConv(cast<CallInst>(Caller)->getCallingConv()); 1317 cast<CallInst>(NewCaller)->setAttributes(NewPAL); 1318 } 1319 if (!Caller->getType()->isVoidTy()) 1320 Caller->replaceAllUsesWith(NewCaller); 1321 Caller->eraseFromParent(); 1322 Worklist.Remove(Caller); 1323 return 0; 1324 } 1325 } 1326 1327 // Replace the trampoline call with a direct call. Since there is no 'nest' 1328 // parameter, there is no need to adjust the argument list. Let the generic 1329 // code sort out any function type mismatches. 1330 Constant *NewCallee = 1331 NestF->getType() == PTy ? NestF : 1332 ConstantExpr::getBitCast(NestF, PTy); 1333 CS.setCalledFunction(NewCallee); 1334 return CS.getInstruction(); 1335} 1336 1337