2010-01-05 07:32:13 +00:00
|
|
|
//===- InstCombineCalls.cpp -----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the visitCall and visitInvoke functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InstCombine.h"
|
|
|
|
#include "llvm/Support/CallSite.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2010-03-06 10:50:38 +00:00
|
|
|
#include "llvm/Transforms/Utils/BuildLibCalls.h"
|
2010-12-25 20:37:57 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2010-01-05 07:32:13 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// getPromotedType - Return the specified type promoted as it would be to pass
|
|
|
|
/// though a va_arg area.
|
2011-07-18 04:54:35 +00:00
|
|
|
static Type *getPromotedType(Type *Ty) {
|
|
|
|
if (IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
|
2010-01-05 07:32:13 +00:00
|
|
|
if (ITy->getBitWidth() < 32)
|
|
|
|
return Type::getInt32Ty(Ty->getContext());
|
|
|
|
}
|
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
|
2010-12-25 20:37:57 +00:00
|
|
|
unsigned DstAlign = getKnownAlignment(MI->getArgOperand(0), TD);
|
|
|
|
unsigned SrcAlign = getKnownAlignment(MI->getArgOperand(1), TD);
|
2010-01-05 07:32:13 +00:00
|
|
|
unsigned MinAlign = std::min(DstAlign, SrcAlign);
|
|
|
|
unsigned CopyAlign = MI->getAlignment();
|
|
|
|
|
|
|
|
if (CopyAlign < MinAlign) {
|
2012-02-03 00:07:04 +00:00
|
|
|
MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
|
2010-01-05 07:32:13 +00:00
|
|
|
MinAlign, false));
|
|
|
|
return MI;
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
|
|
|
|
// load/store.
|
2010-06-24 13:54:33 +00:00
|
|
|
ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getArgOperand(2));
|
2010-01-05 07:32:13 +00:00
|
|
|
if (MemOpLength == 0) return 0;
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Source and destination pointer types are always "i8*" for intrinsic. See
|
|
|
|
// if the size is something we can handle with a single primitive load/store.
|
|
|
|
// A single load+store correctly handles overlapping memory in the memmove
|
|
|
|
// case.
|
|
|
|
unsigned Size = MemOpLength->getZExtValue();
|
|
|
|
if (Size == 0) return MI; // Delete this mem transfer.
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
if (Size > 8 || (Size&(Size-1)))
|
|
|
|
return 0; // If not 1/2/4/8 bytes, exit.
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Use an integer load+store unless we can find something better.
|
Reapply address space patch after fixing an issue in MemCopyOptimizer.
Added support for address spaces and added a isVolatile field to memcpy, memmove, and memset,
e.g., llvm.memcpy.i32(i8*, i8*, i32, i32) -> llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100304 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-04 03:10:48 +00:00
|
|
|
unsigned SrcAddrSp =
|
2010-06-24 13:54:33 +00:00
|
|
|
cast<PointerType>(MI->getArgOperand(1)->getType())->getAddressSpace();
|
2010-04-16 15:33:14 +00:00
|
|
|
unsigned DstAddrSp =
|
2010-06-24 13:54:33 +00:00
|
|
|
cast<PointerType>(MI->getArgOperand(0)->getType())->getAddressSpace();
|
Reapply address space patch after fixing an issue in MemCopyOptimizer.
Added support for address spaces and added a isVolatile field to memcpy, memmove, and memset,
e.g., llvm.memcpy.i32(i8*, i8*, i32, i32) -> llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100304 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-04 03:10:48 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
|
Reapply address space patch after fixing an issue in MemCopyOptimizer.
Added support for address spaces and added a isVolatile field to memcpy, memmove, and memset,
e.g., llvm.memcpy.i32(i8*, i8*, i32, i32) -> llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100304 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-04 03:10:48 +00:00
|
|
|
Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
|
|
|
|
Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp);
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Memcpy forces the use of i8* for the source and destination. That means
|
|
|
|
// that if you're using memcpy to move one double around, you'll get a cast
|
|
|
|
// from double* to i8*. We'd much rather use a double load+store rather than
|
|
|
|
// an i64 load+store, here because this improves the odds that the source or
|
|
|
|
// dest address will be promotable. See if we can find a better type than the
|
|
|
|
// integer datatype.
|
2010-06-24 12:58:35 +00:00
|
|
|
Value *StrippedDest = MI->getArgOperand(0)->stripPointerCasts();
|
|
|
|
if (StrippedDest != MI->getArgOperand(0)) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *SrcETy = cast<PointerType>(StrippedDest->getType())
|
2010-01-05 07:32:13 +00:00
|
|
|
->getElementType();
|
|
|
|
if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
|
|
|
|
// The SrcETy might be something like {{{double}}} or [1 x double]. Rip
|
|
|
|
// down through these levels if so.
|
|
|
|
while (!SrcETy->isSingleValueType()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(SrcETy)) {
|
2010-01-05 07:32:13 +00:00
|
|
|
if (STy->getNumElements() == 1)
|
|
|
|
SrcETy = STy->getElementType(0);
|
|
|
|
else
|
|
|
|
break;
|
2011-07-18 04:54:35 +00:00
|
|
|
} else if (ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) {
|
2010-01-05 07:32:13 +00:00
|
|
|
if (ATy->getNumElements() == 1)
|
|
|
|
SrcETy = ATy->getElementType();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
Reapply address space patch after fixing an issue in MemCopyOptimizer.
Added support for address spaces and added a isVolatile field to memcpy, memmove, and memset,
e.g., llvm.memcpy.i32(i8*, i8*, i32, i32) -> llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100304 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-04 03:10:48 +00:00
|
|
|
if (SrcETy->isSingleValueType()) {
|
|
|
|
NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp);
|
|
|
|
NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp);
|
|
|
|
}
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// If the memcpy/memmove provides better alignment info than we can
|
|
|
|
// infer, use it.
|
|
|
|
SrcAlign = std::max(SrcAlign, CopyAlign);
|
|
|
|
DstAlign = std::max(DstAlign, CopyAlign);
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-06-25 07:57:14 +00:00
|
|
|
Value *Src = Builder->CreateBitCast(MI->getArgOperand(1), NewSrcPtrTy);
|
|
|
|
Value *Dest = Builder->CreateBitCast(MI->getArgOperand(0), NewDstPtrTy);
|
2011-05-18 19:57:14 +00:00
|
|
|
LoadInst *L = Builder->CreateLoad(Src, MI->isVolatile());
|
|
|
|
L->setAlignment(SrcAlign);
|
|
|
|
StoreInst *S = Builder->CreateStore(L, Dest, MI->isVolatile());
|
|
|
|
S->setAlignment(DstAlign);
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
// Set the size of the copy to 0, it will be deleted on the next iteration.
|
2010-06-28 16:50:57 +00:00
|
|
|
MI->setArgOperand(2, Constant::getNullValue(MemOpLength->getType()));
|
2010-01-05 07:32:13 +00:00
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
|
2010-12-25 20:52:04 +00:00
|
|
|
unsigned Alignment = getKnownAlignment(MI->getDest(), TD);
|
2010-01-05 07:32:13 +00:00
|
|
|
if (MI->getAlignment() < Alignment) {
|
|
|
|
MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
|
|
|
|
Alignment, false));
|
|
|
|
return MI;
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Extract the length and alignment and fill if they are constant.
|
|
|
|
ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength());
|
|
|
|
ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue());
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8))
|
2010-01-05 07:32:13 +00:00
|
|
|
return 0;
|
|
|
|
uint64_t Len = LenC->getZExtValue();
|
|
|
|
Alignment = MI->getAlignment();
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// If the length is zero, this is a no-op
|
|
|
|
if (Len == 0) return MI; // memset(d,c,0,a) -> noop
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// memset(s,c,n) -> store s, c (for n=1,2,4,8)
|
|
|
|
if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8.
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
Value *Dest = MI->getDest();
|
2010-12-20 01:05:30 +00:00
|
|
|
unsigned DstAddrSp = cast<PointerType>(Dest->getType())->getAddressSpace();
|
|
|
|
Type *NewDstPtrTy = PointerType::get(ITy, DstAddrSp);
|
|
|
|
Dest = Builder->CreateBitCast(Dest, NewDstPtrTy);
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
// Alignment 0 is identity for alignment 1 for memset, but not store.
|
|
|
|
if (Alignment == 0) Alignment = 1;
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Extract the fill value and store.
|
|
|
|
uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
|
2011-05-18 19:57:14 +00:00
|
|
|
StoreInst *S = Builder->CreateStore(ConstantInt::get(ITy, Fill), Dest,
|
|
|
|
MI->isVolatile());
|
|
|
|
S->setAlignment(Alignment);
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Set the size of the copy to 0, it will be deleted on the next iteration.
|
|
|
|
MI->setLength(Constant::getNullValue(LenC->getType()));
|
|
|
|
return MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-03 00:07:04 +00:00
|
|
|
/// visitCallInst - CallInst simplification. This mostly only handles folding
|
2010-01-05 07:32:13 +00:00
|
|
|
/// of intrinsic instructions. For normal calls, it allows visitCallSite to do
|
|
|
|
/// the heavy lifting.
|
|
|
|
///
|
|
|
|
Instruction *InstCombiner::visitCallInst(CallInst &CI) {
|
|
|
|
if (isFreeCall(&CI))
|
|
|
|
return visitFree(CI);
|
2010-05-27 19:09:06 +00:00
|
|
|
if (isMalloc(&CI))
|
|
|
|
return visitMalloc(CI);
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
// If the caller function is nounwind, mark the call as nounwind, even if the
|
|
|
|
// callee isn't.
|
|
|
|
if (CI.getParent()->getParent()->doesNotThrow() &&
|
|
|
|
!CI.doesNotThrow()) {
|
|
|
|
CI.setDoesNotThrow();
|
|
|
|
return &CI;
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI);
|
|
|
|
if (!II) return visitCallSite(&CI);
|
2010-06-24 12:58:35 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Intrinsics cannot occur in an invoke, so handle them here instead of in
|
|
|
|
// visitCallSite.
|
|
|
|
if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// memmove/cpy/set of zero bytes is a noop.
|
|
|
|
if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
|
2010-10-01 05:51:02 +00:00
|
|
|
if (NumBytes->isNullValue())
|
|
|
|
return EraseInstFromFunction(CI);
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
|
|
|
|
if (CI->getZExtValue() == 1) {
|
|
|
|
// Replace the instruction with just byte operations. We would
|
|
|
|
// transform other cases to loads/stores, but we don't know if
|
|
|
|
// alignment is sufficient.
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-10-01 05:51:02 +00:00
|
|
|
// No other transformations apply to volatile transfers.
|
|
|
|
if (MI->isVolatile())
|
|
|
|
return 0;
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
// If we have a memmove and the source operation is a constant global,
|
|
|
|
// then the source and dest pointers can't alias, so we can change this
|
|
|
|
// into a call to memcpy.
|
|
|
|
if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) {
|
|
|
|
if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
|
|
|
|
if (GVSrc->isConstant()) {
|
2010-04-16 23:37:20 +00:00
|
|
|
Module *M = CI.getParent()->getParent()->getParent();
|
2010-01-05 07:32:13 +00:00
|
|
|
Intrinsic::ID MemCpyID = Intrinsic::memcpy;
|
2011-07-12 14:06:48 +00:00
|
|
|
Type *Tys[3] = { CI.getArgOperand(0)->getType(),
|
|
|
|
CI.getArgOperand(1)->getType(),
|
|
|
|
CI.getArgOperand(2)->getType() };
|
2011-07-14 17:45:39 +00:00
|
|
|
CI.setCalledFunction(Intrinsic::getDeclaration(M, MemCpyID, Tys));
|
2010-01-05 07:32:13 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
|
|
|
|
// memmove(x,x,size) -> noop.
|
|
|
|
if (MTI->getSource() == MTI->getDest())
|
|
|
|
return EraseInstFromFunction(CI);
|
2010-04-16 23:37:20 +00:00
|
|
|
}
|
2010-01-05 07:32:13 +00:00
|
|
|
|
2010-04-16 23:37:20 +00:00
|
|
|
// If we can determine a pointer alignment that is bigger than currently
|
|
|
|
// set, update the alignment.
|
|
|
|
if (isa<MemTransferInst>(MI)) {
|
|
|
|
if (Instruction *I = SimplifyMemTransfer(MI))
|
2010-01-05 07:32:13 +00:00
|
|
|
return I;
|
|
|
|
} else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) {
|
|
|
|
if (Instruction *I = SimplifyMemSet(MSI))
|
|
|
|
return I;
|
|
|
|
}
|
2010-06-24 13:42:49 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
if (Changed) return II;
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
switch (II->getIntrinsicID()) {
|
|
|
|
default: break;
|
2010-02-09 21:24:27 +00:00
|
|
|
case Intrinsic::objectsize: {
|
2010-02-11 01:48:54 +00:00
|
|
|
// We need target data for just about everything so depend on it.
|
2010-02-09 21:24:27 +00:00
|
|
|
if (!TD) break;
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ReturnTy = CI.getType();
|
2011-01-06 13:07:49 +00:00
|
|
|
uint64_t DontKnow = II->getArgOperand(1) == Builder->getTrue() ? 0 : -1ULL;
|
2010-03-05 20:47:23 +00:00
|
|
|
|
2010-02-11 01:48:54 +00:00
|
|
|
// Get to the real allocated thing and offset as fast as possible.
|
2010-06-24 12:58:35 +00:00
|
|
|
Value *Op1 = II->getArgOperand(0)->stripPointerCasts();
|
2011-01-06 13:07:49 +00:00
|
|
|
|
|
|
|
uint64_t Offset = 0;
|
|
|
|
uint64_t Size = -1ULL;
|
|
|
|
|
|
|
|
// Try to look through constant GEPs.
|
|
|
|
if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1)) {
|
|
|
|
if (!GEP->hasAllConstantIndices()) break;
|
|
|
|
|
|
|
|
// Get the current byte offset into the thing. Use the original
|
|
|
|
// operand in case we're looking through a bitcast.
|
|
|
|
SmallVector<Value*, 8> Ops(GEP->idx_begin(), GEP->idx_end());
|
2011-12-05 06:29:09 +00:00
|
|
|
if (!GEP->getPointerOperandType()->isPointerTy())
|
|
|
|
return 0;
|
2011-07-19 14:01:37 +00:00
|
|
|
Offset = TD->getIndexedOffset(GEP->getPointerOperandType(), Ops);
|
2011-01-06 13:07:49 +00:00
|
|
|
|
|
|
|
Op1 = GEP->getPointerOperand()->stripPointerCasts();
|
|
|
|
|
|
|
|
// Make sure we're not a constant offset from an external
|
|
|
|
// global.
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1))
|
|
|
|
if (!GV->hasDefinitiveInitializer()) break;
|
|
|
|
}
|
|
|
|
|
2010-02-11 01:48:54 +00:00
|
|
|
// If we've stripped down to a single global variable that we
|
|
|
|
// can know the size of then just return that.
|
2010-02-09 21:24:27 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) {
|
|
|
|
if (GV->hasDefinitiveInitializer()) {
|
|
|
|
Constant *C = GV->getInitializer();
|
2011-01-06 13:07:49 +00:00
|
|
|
Size = TD->getTypeAllocSize(C->getType());
|
2010-02-09 21:24:27 +00:00
|
|
|
} else {
|
2010-03-05 20:47:23 +00:00
|
|
|
// Can't determine size of the GV.
|
2011-01-06 13:07:49 +00:00
|
|
|
Constant *RetVal = ConstantInt::get(ReturnTy, DontKnow);
|
2010-02-09 21:24:27 +00:00
|
|
|
return ReplaceInstUsesWith(CI, RetVal);
|
|
|
|
}
|
2010-03-05 20:47:23 +00:00
|
|
|
} else if (AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) {
|
|
|
|
// Get alloca size.
|
|
|
|
if (AI->getAllocatedType()->isSized()) {
|
2011-01-06 13:07:49 +00:00
|
|
|
Size = TD->getTypeAllocSize(AI->getAllocatedType());
|
2010-03-05 20:47:23 +00:00
|
|
|
if (AI->isArrayAllocation()) {
|
|
|
|
const ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize());
|
|
|
|
if (!C) break;
|
2011-01-06 13:07:49 +00:00
|
|
|
Size *= C->getZExtValue();
|
2010-03-05 20:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-08 22:54:36 +00:00
|
|
|
} else if (CallInst *MI = extractMallocCall(Op1)) {
|
2011-01-06 13:11:05 +00:00
|
|
|
// Get allocation size.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type* MallocType = getMallocAllocatedType(MI);
|
2011-01-06 13:07:49 +00:00
|
|
|
if (MallocType && MallocType->isSized())
|
|
|
|
if (Value *NElems = getMallocArraySize(MI, TD, true))
|
2010-03-08 22:54:36 +00:00
|
|
|
if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems))
|
2011-01-06 13:07:49 +00:00
|
|
|
Size = NElements->getZExtValue() * TD->getTypeAllocSize(MallocType);
|
|
|
|
}
|
2010-03-05 20:47:23 +00:00
|
|
|
|
|
|
|
// Do not return "I don't know" here. Later optimization passes could
|
|
|
|
// make it possible to evaluate objectsize to a constant.
|
2011-01-06 13:07:49 +00:00
|
|
|
if (Size == -1ULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Size < Offset) {
|
|
|
|
// Out of bound reference? Negative index normalized to large
|
|
|
|
// index? Just return "I don't know".
|
|
|
|
return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, DontKnow));
|
|
|
|
}
|
|
|
|
return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, Size-Offset));
|
2010-02-09 21:24:27 +00:00
|
|
|
}
|
2010-01-05 07:32:13 +00:00
|
|
|
case Intrinsic::bswap:
|
|
|
|
// bswap(bswap(x)) -> x
|
2010-06-24 12:58:35 +00:00
|
|
|
if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getArgOperand(0)))
|
2010-01-05 07:32:13 +00:00
|
|
|
if (Operand->getIntrinsicID() == Intrinsic::bswap)
|
2010-06-24 12:58:35 +00:00
|
|
|
return ReplaceInstUsesWith(CI, Operand->getArgOperand(0));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
|
2010-06-24 12:58:35 +00:00
|
|
|
if (TruncInst *TI = dyn_cast<TruncInst>(II->getArgOperand(0))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0)))
|
|
|
|
if (Operand->getIntrinsicID() == Intrinsic::bswap) {
|
|
|
|
unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
|
|
|
|
TI->getType()->getPrimitiveSizeInBits();
|
|
|
|
Value *CV = ConstantInt::get(Operand->getType(), C);
|
2010-06-24 12:58:35 +00:00
|
|
|
Value *V = Builder->CreateLShr(Operand->getArgOperand(0), CV);
|
2010-01-05 07:32:13 +00:00
|
|
|
return new TruncInst(V, TI->getType());
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
break;
|
|
|
|
case Intrinsic::powi:
|
2010-06-24 12:58:35 +00:00
|
|
|
if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
// powi(x, 0) -> 1.0
|
|
|
|
if (Power->isZero())
|
|
|
|
return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
|
|
|
|
// powi(x, 1) -> x
|
|
|
|
if (Power->isOne())
|
2010-06-24 12:58:35 +00:00
|
|
|
return ReplaceInstUsesWith(CI, II->getArgOperand(0));
|
2010-01-05 07:32:13 +00:00
|
|
|
// powi(x, -1) -> 1/x
|
|
|
|
if (Power->isAllOnesValue())
|
|
|
|
return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
|
2010-06-24 12:58:35 +00:00
|
|
|
II->getArgOperand(0));
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::cttz: {
|
|
|
|
// If all bits below the first known one are known zero,
|
|
|
|
// this value is constant.
|
2011-07-18 04:54:35 +00:00
|
|
|
IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType());
|
2011-07-01 21:52:38 +00:00
|
|
|
// FIXME: Try to simplify vectors of integers.
|
|
|
|
if (!IT) break;
|
2010-01-05 07:32:13 +00:00
|
|
|
uint32_t BitWidth = IT->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0);
|
|
|
|
APInt KnownOne(BitWidth, 0);
|
2012-04-04 12:51:34 +00:00
|
|
|
ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne);
|
2010-01-05 07:32:13 +00:00
|
|
|
unsigned TrailingZeros = KnownOne.countTrailingZeros();
|
|
|
|
APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
|
|
|
|
if ((Mask & KnownZero) == Mask)
|
|
|
|
return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
|
|
|
|
APInt(BitWidth, TrailingZeros)));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::ctlz: {
|
|
|
|
// If all bits above the first known one are known zero,
|
|
|
|
// this value is constant.
|
2011-07-18 04:54:35 +00:00
|
|
|
IntegerType *IT = dyn_cast<IntegerType>(II->getArgOperand(0)->getType());
|
2011-07-01 21:52:38 +00:00
|
|
|
// FIXME: Try to simplify vectors of integers.
|
|
|
|
if (!IT) break;
|
2010-01-05 07:32:13 +00:00
|
|
|
uint32_t BitWidth = IT->getBitWidth();
|
|
|
|
APInt KnownZero(BitWidth, 0);
|
|
|
|
APInt KnownOne(BitWidth, 0);
|
2012-04-04 12:51:34 +00:00
|
|
|
ComputeMaskedBits(II->getArgOperand(0), KnownZero, KnownOne);
|
2010-01-05 07:32:13 +00:00
|
|
|
unsigned LeadingZeros = KnownOne.countLeadingZeros();
|
|
|
|
APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
|
|
|
|
if ((Mask & KnownZero) == Mask)
|
|
|
|
return ReplaceInstUsesWith(CI, ConstantInt::get(IT,
|
|
|
|
APInt(BitWidth, LeadingZeros)));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::uadd_with_overflow: {
|
2010-06-24 12:58:35 +00:00
|
|
|
Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
|
2011-07-18 04:54:35 +00:00
|
|
|
IntegerType *IT = cast<IntegerType>(II->getArgOperand(0)->getType());
|
2010-01-05 07:32:13 +00:00
|
|
|
uint32_t BitWidth = IT->getBitWidth();
|
|
|
|
APInt LHSKnownZero(BitWidth, 0);
|
|
|
|
APInt LHSKnownOne(BitWidth, 0);
|
2012-04-04 12:51:34 +00:00
|
|
|
ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
|
2010-01-05 07:32:13 +00:00
|
|
|
bool LHSKnownNegative = LHSKnownOne[BitWidth - 1];
|
|
|
|
bool LHSKnownPositive = LHSKnownZero[BitWidth - 1];
|
|
|
|
|
|
|
|
if (LHSKnownNegative || LHSKnownPositive) {
|
|
|
|
APInt RHSKnownZero(BitWidth, 0);
|
|
|
|
APInt RHSKnownOne(BitWidth, 0);
|
2012-04-04 12:51:34 +00:00
|
|
|
ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
|
2010-01-05 07:32:13 +00:00
|
|
|
bool RHSKnownNegative = RHSKnownOne[BitWidth - 1];
|
|
|
|
bool RHSKnownPositive = RHSKnownZero[BitWidth - 1];
|
|
|
|
if (LHSKnownNegative && RHSKnownNegative) {
|
|
|
|
// The sign bit is set in both cases: this MUST overflow.
|
|
|
|
// Create a simple add instruction, and insert it into the struct.
|
2011-05-18 19:57:14 +00:00
|
|
|
Value *Add = Builder->CreateAdd(LHS, RHS);
|
|
|
|
Add->takeName(&CI);
|
2010-01-05 07:32:13 +00:00
|
|
|
Constant *V[] = {
|
2011-05-18 19:57:14 +00:00
|
|
|
UndefValue::get(LHS->getType()),
|
|
|
|
ConstantInt::getTrue(II->getContext())
|
2010-01-05 07:32:13 +00:00
|
|
|
};
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *ST = cast<StructType>(II->getType());
|
2011-06-20 04:01:31 +00:00
|
|
|
Constant *Struct = ConstantStruct::get(ST, V);
|
2010-01-05 07:32:13 +00:00
|
|
|
return InsertValueInst::Create(Struct, Add, 0);
|
|
|
|
}
|
2011-05-18 19:57:14 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
if (LHSKnownPositive && RHSKnownPositive) {
|
|
|
|
// The sign bit is clear in both cases: this CANNOT overflow.
|
|
|
|
// Create a simple add instruction, and insert it into the struct.
|
2011-05-18 19:57:14 +00:00
|
|
|
Value *Add = Builder->CreateNUWAdd(LHS, RHS);
|
|
|
|
Add->takeName(&CI);
|
2010-01-05 07:32:13 +00:00
|
|
|
Constant *V[] = {
|
|
|
|
UndefValue::get(LHS->getType()),
|
|
|
|
ConstantInt::getFalse(II->getContext())
|
|
|
|
};
|
2011-07-18 04:54:35 +00:00
|
|
|
StructType *ST = cast<StructType>(II->getType());
|
2011-06-20 04:01:31 +00:00
|
|
|
Constant *Struct = ConstantStruct::get(ST, V);
|
2010-01-05 07:32:13 +00:00
|
|
|
return InsertValueInst::Create(Struct, Add, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FALL THROUGH uadd into sadd
|
|
|
|
case Intrinsic::sadd_with_overflow:
|
|
|
|
// Canonicalize constants into the RHS.
|
2010-06-28 16:50:57 +00:00
|
|
|
if (isa<Constant>(II->getArgOperand(0)) &&
|
|
|
|
!isa<Constant>(II->getArgOperand(1))) {
|
|
|
|
Value *LHS = II->getArgOperand(0);
|
|
|
|
II->setArgOperand(0, II->getArgOperand(1));
|
|
|
|
II->setArgOperand(1, LHS);
|
2010-01-05 07:32:13 +00:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
|
|
|
|
// X + undef -> undef
|
2010-06-25 07:57:14 +00:00
|
|
|
if (isa<UndefValue>(II->getArgOperand(1)))
|
2010-01-05 07:32:13 +00:00
|
|
|
return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-06-25 07:57:14 +00:00
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
// X + 0 -> {X, false}
|
|
|
|
if (RHS->isZero()) {
|
|
|
|
Constant *V[] = {
|
2010-08-09 20:49:43 +00:00
|
|
|
UndefValue::get(II->getArgOperand(0)->getType()),
|
2010-01-05 07:32:13 +00:00
|
|
|
ConstantInt::getFalse(II->getContext())
|
|
|
|
};
|
2011-06-20 04:01:31 +00:00
|
|
|
Constant *Struct =
|
|
|
|
ConstantStruct::get(cast<StructType>(II->getType()), V);
|
2010-06-24 12:58:35 +00:00
|
|
|
return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::usub_with_overflow:
|
|
|
|
case Intrinsic::ssub_with_overflow:
|
|
|
|
// undef - X -> undef
|
|
|
|
// X - undef -> undef
|
2010-06-25 07:57:14 +00:00
|
|
|
if (isa<UndefValue>(II->getArgOperand(0)) ||
|
|
|
|
isa<UndefValue>(II->getArgOperand(1)))
|
2010-01-05 07:32:13 +00:00
|
|
|
return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-06-25 07:57:14 +00:00
|
|
|
if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
// X - 0 -> {X, false}
|
|
|
|
if (RHS->isZero()) {
|
|
|
|
Constant *V[] = {
|
2010-06-25 07:57:14 +00:00
|
|
|
UndefValue::get(II->getArgOperand(0)->getType()),
|
2010-01-05 07:32:13 +00:00
|
|
|
ConstantInt::getFalse(II->getContext())
|
|
|
|
};
|
2012-02-03 00:07:04 +00:00
|
|
|
Constant *Struct =
|
2011-06-20 04:01:31 +00:00
|
|
|
ConstantStruct::get(cast<StructType>(II->getType()), V);
|
2010-06-25 07:57:14 +00:00
|
|
|
return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2011-03-10 18:40:14 +00:00
|
|
|
case Intrinsic::umul_with_overflow: {
|
|
|
|
Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
|
|
|
|
unsigned BitWidth = cast<IntegerType>(LHS->getType())->getBitWidth();
|
|
|
|
|
|
|
|
APInt LHSKnownZero(BitWidth, 0);
|
|
|
|
APInt LHSKnownOne(BitWidth, 0);
|
2012-04-04 12:51:34 +00:00
|
|
|
ComputeMaskedBits(LHS, LHSKnownZero, LHSKnownOne);
|
2011-03-10 18:40:14 +00:00
|
|
|
APInt RHSKnownZero(BitWidth, 0);
|
|
|
|
APInt RHSKnownOne(BitWidth, 0);
|
2012-04-04 12:51:34 +00:00
|
|
|
ComputeMaskedBits(RHS, RHSKnownZero, RHSKnownOne);
|
2011-03-10 18:40:14 +00:00
|
|
|
|
2011-03-27 15:04:38 +00:00
|
|
|
// Get the largest possible values for each operand.
|
|
|
|
APInt LHSMax = ~LHSKnownZero;
|
|
|
|
APInt RHSMax = ~RHSKnownZero;
|
2011-03-10 18:40:14 +00:00
|
|
|
|
|
|
|
// If multiplying the maximum values does not overflow then we can turn
|
|
|
|
// this into a plain NUW mul.
|
2011-03-27 15:04:38 +00:00
|
|
|
bool Overflow;
|
|
|
|
LHSMax.umul_ov(RHSMax, Overflow);
|
|
|
|
if (!Overflow) {
|
2011-03-10 18:40:14 +00:00
|
|
|
Value *Mul = Builder->CreateNUWMul(LHS, RHS, "umul_with_overflow");
|
|
|
|
Constant *V[] = {
|
|
|
|
UndefValue::get(LHS->getType()),
|
|
|
|
Builder->getFalse()
|
|
|
|
};
|
2011-06-20 04:01:31 +00:00
|
|
|
Constant *Struct = ConstantStruct::get(cast<StructType>(II->getType()),V);
|
2011-03-10 18:40:14 +00:00
|
|
|
return InsertValueInst::Create(Struct, Mul, 0);
|
|
|
|
}
|
|
|
|
} // FALL THROUGH
|
2010-01-05 07:32:13 +00:00
|
|
|
case Intrinsic::smul_with_overflow:
|
|
|
|
// Canonicalize constants into the RHS.
|
2010-06-28 16:50:57 +00:00
|
|
|
if (isa<Constant>(II->getArgOperand(0)) &&
|
|
|
|
!isa<Constant>(II->getArgOperand(1))) {
|
|
|
|
Value *LHS = II->getArgOperand(0);
|
|
|
|
II->setArgOperand(0, II->getArgOperand(1));
|
|
|
|
II->setArgOperand(1, LHS);
|
2010-01-05 07:32:13 +00:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
|
|
|
|
// X * undef -> undef
|
2010-06-25 07:57:14 +00:00
|
|
|
if (isa<UndefValue>(II->getArgOperand(1)))
|
2010-01-05 07:32:13 +00:00
|
|
|
return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-06-25 07:57:14 +00:00
|
|
|
if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
// X*0 -> {0, false}
|
|
|
|
if (RHSI->isZero())
|
|
|
|
return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// X * 1 -> {X, false}
|
|
|
|
if (RHSI->equalsInt(1)) {
|
|
|
|
Constant *V[] = {
|
2010-06-24 12:58:35 +00:00
|
|
|
UndefValue::get(II->getArgOperand(0)->getType()),
|
2010-01-05 07:32:13 +00:00
|
|
|
ConstantInt::getFalse(II->getContext())
|
|
|
|
};
|
2012-02-03 00:07:04 +00:00
|
|
|
Constant *Struct =
|
2011-06-20 04:01:31 +00:00
|
|
|
ConstantStruct::get(cast<StructType>(II->getType()), V);
|
2010-06-24 12:58:35 +00:00
|
|
|
return InsertValueInst::Create(Struct, II->getArgOperand(0), 0);
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::ppc_altivec_lvx:
|
|
|
|
case Intrinsic::ppc_altivec_lvxl:
|
2011-04-13 00:36:11 +00:00
|
|
|
// Turn PPC lvx -> load if the pointer is known aligned.
|
2010-12-25 20:37:57 +00:00
|
|
|
if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) {
|
2010-06-24 12:58:35 +00:00
|
|
|
Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0),
|
2010-01-05 07:32:13 +00:00
|
|
|
PointerType::getUnqual(II->getType()));
|
|
|
|
return new LoadInst(Ptr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::ppc_altivec_stvx:
|
|
|
|
case Intrinsic::ppc_altivec_stvxl:
|
|
|
|
// Turn stvx -> store if the pointer is known aligned.
|
2010-12-25 20:37:57 +00:00
|
|
|
if (getOrEnforceKnownAlignment(II->getArgOperand(1), 16, TD) >= 16) {
|
2012-02-03 00:07:04 +00:00
|
|
|
Type *OpPtrTy =
|
2010-06-24 15:51:11 +00:00
|
|
|
PointerType::getUnqual(II->getArgOperand(0)->getType());
|
|
|
|
Value *Ptr = Builder->CreateBitCast(II->getArgOperand(1), OpPtrTy);
|
|
|
|
return new StoreInst(II->getArgOperand(0), Ptr);
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Intrinsic::x86_sse_storeu_ps:
|
|
|
|
case Intrinsic::x86_sse2_storeu_pd:
|
|
|
|
case Intrinsic::x86_sse2_storeu_dq:
|
|
|
|
// Turn X86 storeu -> store if the pointer is known aligned.
|
2010-12-25 20:37:57 +00:00
|
|
|
if (getOrEnforceKnownAlignment(II->getArgOperand(0), 16, TD) >= 16) {
|
2012-02-03 00:07:04 +00:00
|
|
|
Type *OpPtrTy =
|
2010-06-24 15:51:11 +00:00
|
|
|
PointerType::getUnqual(II->getArgOperand(1)->getType());
|
|
|
|
Value *Ptr = Builder->CreateBitCast(II->getArgOperand(0), OpPtrTy);
|
|
|
|
return new StoreInst(II->getArgOperand(1), Ptr);
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-01-10 07:19:37 +00:00
|
|
|
|
|
|
|
case Intrinsic::x86_sse_cvtss2si:
|
|
|
|
case Intrinsic::x86_sse_cvtss2si64:
|
|
|
|
case Intrinsic::x86_sse_cvttss2si:
|
|
|
|
case Intrinsic::x86_sse_cvttss2si64:
|
|
|
|
case Intrinsic::x86_sse2_cvtsd2si:
|
|
|
|
case Intrinsic::x86_sse2_cvtsd2si64:
|
|
|
|
case Intrinsic::x86_sse2_cvttsd2si:
|
|
|
|
case Intrinsic::x86_sse2_cvttsd2si64: {
|
|
|
|
// These intrinsics only demand the 0th element of their input vectors. If
|
2010-01-05 07:32:13 +00:00
|
|
|
// we can simplify the input based on that, do so now.
|
|
|
|
unsigned VWidth =
|
2010-06-25 07:57:14 +00:00
|
|
|
cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
|
2010-01-05 07:32:13 +00:00
|
|
|
APInt DemandedElts(VWidth, 1);
|
|
|
|
APInt UndefElts(VWidth, 0);
|
2010-07-22 10:37:47 +00:00
|
|
|
if (Value *V = SimplifyDemandedVectorElts(II->getArgOperand(0),
|
|
|
|
DemandedElts, UndefElts)) {
|
2010-06-28 16:50:57 +00:00
|
|
|
II->setArgOperand(0, V);
|
2010-01-05 07:32:13 +00:00
|
|
|
return II;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-01-10 07:19:37 +00:00
|
|
|
|
2011-05-17 22:13:31 +00:00
|
|
|
|
|
|
|
case Intrinsic::x86_sse41_pmovsxbw:
|
|
|
|
case Intrinsic::x86_sse41_pmovsxwd:
|
|
|
|
case Intrinsic::x86_sse41_pmovsxdq:
|
|
|
|
case Intrinsic::x86_sse41_pmovzxbw:
|
|
|
|
case Intrinsic::x86_sse41_pmovzxwd:
|
|
|
|
case Intrinsic::x86_sse41_pmovzxdq: {
|
2011-05-19 18:18:39 +00:00
|
|
|
// pmov{s|z}x ignores the upper half of their input vectors.
|
2011-05-17 22:13:31 +00:00
|
|
|
unsigned VWidth =
|
|
|
|
cast<VectorType>(II->getArgOperand(0)->getType())->getNumElements();
|
|
|
|
unsigned LowHalfElts = VWidth / 2;
|
2011-05-18 15:54:26 +00:00
|
|
|
APInt InputDemandedElts(APInt::getBitsSet(VWidth, 0, LowHalfElts));
|
2011-05-17 22:13:31 +00:00
|
|
|
APInt UndefElts(VWidth, 0);
|
|
|
|
if (Value *TmpV = SimplifyDemandedVectorElts(II->getArgOperand(0),
|
|
|
|
InputDemandedElts,
|
|
|
|
UndefElts)) {
|
|
|
|
II->setArgOperand(0, TmpV);
|
|
|
|
return II;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
case Intrinsic::ppc_altivec_vperm:
|
|
|
|
// Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
|
2012-01-27 03:08:05 +00:00
|
|
|
if (Constant *Mask = dyn_cast<Constant>(II->getArgOperand(2))) {
|
|
|
|
assert(Mask->getType()->getVectorNumElements() == 16 &&
|
|
|
|
"Bad type for intrinsic!");
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Check that all of the elements are integer constants or undefs.
|
|
|
|
bool AllEltsOk = true;
|
|
|
|
for (unsigned i = 0; i != 16; ++i) {
|
2012-01-27 03:08:05 +00:00
|
|
|
Constant *Elt = Mask->getAggregateElement(i);
|
|
|
|
if (Elt == 0 ||
|
|
|
|
!(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
AllEltsOk = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
if (AllEltsOk) {
|
|
|
|
// Cast the input vectors to byte vectors.
|
2010-07-22 10:37:47 +00:00
|
|
|
Value *Op0 = Builder->CreateBitCast(II->getArgOperand(0),
|
|
|
|
Mask->getType());
|
|
|
|
Value *Op1 = Builder->CreateBitCast(II->getArgOperand(1),
|
|
|
|
Mask->getType());
|
2010-01-05 07:32:13 +00:00
|
|
|
Value *Result = UndefValue::get(Op0->getType());
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Only extract each element once.
|
|
|
|
Value *ExtractedElts[32];
|
|
|
|
memset(ExtractedElts, 0, sizeof(ExtractedElts));
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
for (unsigned i = 0; i != 16; ++i) {
|
2012-01-27 03:08:05 +00:00
|
|
|
if (isa<UndefValue>(Mask->getAggregateElement(i)))
|
2010-01-05 07:32:13 +00:00
|
|
|
continue;
|
2012-02-03 00:07:04 +00:00
|
|
|
unsigned Idx =
|
2012-01-27 03:08:05 +00:00
|
|
|
cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
|
2010-01-05 07:32:13 +00:00
|
|
|
Idx &= 31; // Match the hardware behavior.
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
if (ExtractedElts[Idx] == 0) {
|
2012-02-03 00:07:04 +00:00
|
|
|
ExtractedElts[Idx] =
|
2011-09-27 20:39:19 +00:00
|
|
|
Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1,
|
|
|
|
Builder->getInt32(Idx&15));
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Insert this value into the result vector.
|
|
|
|
Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx],
|
2011-09-27 20:39:19 +00:00
|
|
|
Builder->getInt32(i));
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
return CastInst::Create(Instruction::BitCast, Result, CI.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-10-22 21:41:48 +00:00
|
|
|
case Intrinsic::arm_neon_vld1:
|
|
|
|
case Intrinsic::arm_neon_vld2:
|
|
|
|
case Intrinsic::arm_neon_vld3:
|
|
|
|
case Intrinsic::arm_neon_vld4:
|
|
|
|
case Intrinsic::arm_neon_vld2lane:
|
|
|
|
case Intrinsic::arm_neon_vld3lane:
|
|
|
|
case Intrinsic::arm_neon_vld4lane:
|
|
|
|
case Intrinsic::arm_neon_vst1:
|
|
|
|
case Intrinsic::arm_neon_vst2:
|
|
|
|
case Intrinsic::arm_neon_vst3:
|
|
|
|
case Intrinsic::arm_neon_vst4:
|
|
|
|
case Intrinsic::arm_neon_vst2lane:
|
|
|
|
case Intrinsic::arm_neon_vst3lane:
|
|
|
|
case Intrinsic::arm_neon_vst4lane: {
|
2010-12-25 20:52:04 +00:00
|
|
|
unsigned MemAlign = getKnownAlignment(II->getArgOperand(0), TD);
|
2010-10-22 21:41:48 +00:00
|
|
|
unsigned AlignArg = II->getNumArgOperands() - 1;
|
|
|
|
ConstantInt *IntrAlign = dyn_cast<ConstantInt>(II->getArgOperand(AlignArg));
|
|
|
|
if (IntrAlign && IntrAlign->getZExtValue() < MemAlign) {
|
|
|
|
II->setArgOperand(AlignArg,
|
|
|
|
ConstantInt::get(Type::getInt32Ty(II->getContext()),
|
|
|
|
MemAlign, false));
|
|
|
|
return II;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
case Intrinsic::stackrestore: {
|
|
|
|
// If the save is right next to the restore, remove the restore. This can
|
|
|
|
// happen when variable allocas are DCE'd.
|
2010-06-24 12:58:35 +00:00
|
|
|
if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getArgOperand(0))) {
|
2010-01-05 07:32:13 +00:00
|
|
|
if (SS->getIntrinsicID() == Intrinsic::stacksave) {
|
|
|
|
BasicBlock::iterator BI = SS;
|
|
|
|
if (&*++BI == II)
|
|
|
|
return EraseInstFromFunction(CI);
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Scan down this block to see if there is another stack restore in the
|
|
|
|
// same block without an intervening call/alloca.
|
|
|
|
BasicBlock::iterator BI = II;
|
|
|
|
TerminatorInst *TI = II->getParent()->getTerminator();
|
|
|
|
bool CannotRemove = false;
|
|
|
|
for (++BI; &*BI != TI; ++BI) {
|
|
|
|
if (isa<AllocaInst>(BI) || isMalloc(BI)) {
|
|
|
|
CannotRemove = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (CallInst *BCI = dyn_cast<CallInst>(BI)) {
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) {
|
|
|
|
// If there is a stackrestore below this one, remove this one.
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::stackrestore)
|
|
|
|
return EraseInstFromFunction(CI);
|
|
|
|
// Otherwise, ignore the intrinsic.
|
|
|
|
} else {
|
|
|
|
// If we found a non-intrinsic call, we can't remove the stack
|
|
|
|
// restore.
|
|
|
|
CannotRemove = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2011-07-31 06:30:59 +00:00
|
|
|
// If the stack restore is in a return, resume, or unwind block and if there
|
|
|
|
// are no allocas or calls between the restore and the return, nuke the
|
|
|
|
// restore.
|
2012-02-06 21:16:41 +00:00
|
|
|
if (!CannotRemove && (isa<ReturnInst>(TI) || isa<ResumeInst>(TI)))
|
2010-01-05 07:32:13 +00:00
|
|
|
return EraseInstFromFunction(CI);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return visitCallSite(II);
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokeInst simplification
|
|
|
|
//
|
|
|
|
Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
|
|
|
|
return visitCallSite(&II);
|
|
|
|
}
|
|
|
|
|
2012-02-03 00:07:04 +00:00
|
|
|
/// isSafeToEliminateVarargsCast - If this cast does not affect the value
|
2010-01-05 07:32:13 +00:00
|
|
|
/// passed through the varargs area, we can eliminate the use of the cast.
|
|
|
|
static bool isSafeToEliminateVarargsCast(const CallSite CS,
|
|
|
|
const CastInst * const CI,
|
|
|
|
const TargetData * const TD,
|
|
|
|
const int ix) {
|
|
|
|
if (!CI->isLosslessCast())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The size of ByVal arguments is derived from the type, so we
|
|
|
|
// can't change to a type with a different size. If the size were
|
|
|
|
// passed explicitly we could avoid this check.
|
2011-11-20 19:09:04 +00:00
|
|
|
if (!CS.isByValArgument(ix))
|
2010-01-05 07:32:13 +00:00
|
|
|
return true;
|
|
|
|
|
2012-02-03 00:07:04 +00:00
|
|
|
Type* SrcTy =
|
2010-01-05 07:32:13 +00:00
|
|
|
cast<PointerType>(CI->getOperand(0)->getType())->getElementType();
|
2011-07-18 04:54:35 +00:00
|
|
|
Type* DstTy = cast<PointerType>(CI->getType())->getElementType();
|
2010-01-05 07:32:13 +00:00
|
|
|
if (!SrcTy->isSized() || !DstTy->isSized())
|
|
|
|
return false;
|
|
|
|
if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-12 09:27:41 +00:00
|
|
|
namespace {
|
|
|
|
class InstCombineFortifiedLibCalls : public SimplifyFortifiedLibCalls {
|
|
|
|
InstCombiner *IC;
|
|
|
|
protected:
|
|
|
|
void replaceCall(Value *With) {
|
|
|
|
NewInstruction = IC->ReplaceInstUsesWith(*CI, With);
|
|
|
|
}
|
|
|
|
bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const {
|
2011-01-06 14:22:52 +00:00
|
|
|
if (CI->getArgOperand(SizeCIOp) == CI->getArgOperand(SizeArgOp))
|
|
|
|
return true;
|
2010-07-22 10:37:47 +00:00
|
|
|
if (ConstantInt *SizeCI =
|
|
|
|
dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) {
|
2010-03-12 09:27:41 +00:00
|
|
|
if (SizeCI->isAllOnesValue())
|
|
|
|
return true;
|
2011-03-15 00:25:41 +00:00
|
|
|
if (isString) {
|
|
|
|
uint64_t Len = GetStringLength(CI->getArgOperand(SizeArgOp));
|
|
|
|
// If the length is 0 we don't know how long it is and so we can't
|
|
|
|
// remove the check.
|
|
|
|
if (Len == 0) return false;
|
|
|
|
return SizeCI->getZExtValue() >= Len;
|
|
|
|
}
|
2010-07-22 10:37:47 +00:00
|
|
|
if (ConstantInt *Arg = dyn_cast<ConstantInt>(
|
|
|
|
CI->getArgOperand(SizeArgOp)))
|
2010-03-23 06:06:09 +00:00
|
|
|
return SizeCI->getZExtValue() >= Arg->getZExtValue();
|
2010-03-12 09:27:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
InstCombineFortifiedLibCalls(InstCombiner *IC) : IC(IC), NewInstruction(0) { }
|
|
|
|
Instruction *NewInstruction;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2010-03-06 10:50:38 +00:00
|
|
|
// Try to fold some different type of calls here.
|
2012-02-03 00:07:04 +00:00
|
|
|
// Currently we're only working with the checking functions, memcpy_chk,
|
2010-03-06 10:50:38 +00:00
|
|
|
// mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk,
|
|
|
|
// strcat_chk and strncat_chk.
|
|
|
|
Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) {
|
|
|
|
if (CI->getCalledFunction() == 0) return 0;
|
|
|
|
|
2010-03-12 09:27:41 +00:00
|
|
|
InstCombineFortifiedLibCalls Simplifier(this);
|
|
|
|
Simplifier.fold(CI, TD);
|
|
|
|
return Simplifier.NewInstruction;
|
2010-03-06 10:50:38 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 13:37:06 +00:00
|
|
|
static IntrinsicInst *FindInitTrampolineFromAlloca(Value *TrampMem) {
|
|
|
|
// Strip off at most one level of pointer casts, looking for an alloca. This
|
|
|
|
// is good enough in practice and simpler than handling any number of casts.
|
|
|
|
Value *Underlying = TrampMem->stripPointerCasts();
|
|
|
|
if (Underlying != TrampMem &&
|
|
|
|
(!Underlying->hasOneUse() || *Underlying->use_begin() != TrampMem))
|
|
|
|
return 0;
|
|
|
|
if (!isa<AllocaInst>(Underlying))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
IntrinsicInst *InitTrampoline = 0;
|
|
|
|
for (Value::use_iterator I = TrampMem->use_begin(), E = TrampMem->use_end();
|
|
|
|
I != E; I++) {
|
|
|
|
IntrinsicInst *II = dyn_cast<IntrinsicInst>(*I);
|
|
|
|
if (!II)
|
|
|
|
return 0;
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
|
|
|
|
if (InitTrampoline)
|
|
|
|
// More than one init_trampoline writes to this value. Give up.
|
|
|
|
return 0;
|
|
|
|
InitTrampoline = II;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
|
|
|
|
// Allow any number of calls to adjust.trampoline.
|
|
|
|
continue;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No call to init.trampoline found.
|
|
|
|
if (!InitTrampoline)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Check that the alloca is being used in the expected way.
|
|
|
|
if (InitTrampoline->getOperand(0) != TrampMem)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return InitTrampoline;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IntrinsicInst *FindInitTrampolineFromBB(IntrinsicInst *AdjustTramp,
|
|
|
|
Value *TrampMem) {
|
|
|
|
// Visit all the previous instructions in the basic block, and try to find a
|
|
|
|
// init.trampoline which has a direct path to the adjust.trampoline.
|
|
|
|
for (BasicBlock::iterator I = AdjustTramp,
|
|
|
|
E = AdjustTramp->getParent()->begin(); I != E; ) {
|
|
|
|
Instruction *Inst = --I;
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
|
|
|
|
if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
|
|
|
|
II->getOperand(0) == TrampMem)
|
|
|
|
return II;
|
|
|
|
if (Inst->mayWriteToMemory())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a call to llvm.adjust.trampoline, find and return the corresponding
|
|
|
|
// call to llvm.init.trampoline if the call to the trampoline can be optimized
|
|
|
|
// to a direct call to a function. Otherwise return NULL.
|
|
|
|
//
|
|
|
|
static IntrinsicInst *FindInitTrampoline(Value *Callee) {
|
|
|
|
Callee = Callee->stripPointerCasts();
|
|
|
|
IntrinsicInst *AdjustTramp = dyn_cast<IntrinsicInst>(Callee);
|
|
|
|
if (!AdjustTramp ||
|
|
|
|
AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Value *TrampMem = AdjustTramp->getOperand(0);
|
|
|
|
|
|
|
|
if (IntrinsicInst *IT = FindInitTrampolineFromAlloca(TrampMem))
|
|
|
|
return IT;
|
|
|
|
if (IntrinsicInst *IT = FindInitTrampolineFromBB(AdjustTramp, TrampMem))
|
|
|
|
return IT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// visitCallSite - Improvements for call and invoke instructions.
|
|
|
|
//
|
|
|
|
Instruction *InstCombiner::visitCallSite(CallSite CS) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
2010-12-20 08:25:06 +00:00
|
|
|
// If the callee is a pointer to a function, attempt to move any casts to the
|
|
|
|
// arguments of the call/invoke.
|
2010-01-05 07:32:13 +00:00
|
|
|
Value *Callee = CS.getCalledValue();
|
2010-12-20 08:25:06 +00:00
|
|
|
if (!isa<Function>(Callee) && transformConstExprCastCall(CS))
|
|
|
|
return 0;
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
if (Function *CalleeF = dyn_cast<Function>(Callee))
|
2010-02-01 18:11:34 +00:00
|
|
|
// If the call and callee calling conventions don't match, this call must
|
|
|
|
// be unreachable, as the call is undefined.
|
|
|
|
if (CalleeF->getCallingConv() != CS.getCallingConv() &&
|
|
|
|
// Only do this for calls to a function with a body. A prototype may
|
|
|
|
// not actually end up matching the implementation's calling conv for a
|
|
|
|
// variety of reasons (e.g. it may be written in assembly).
|
|
|
|
!CalleeF->isDeclaration()) {
|
2010-01-05 07:32:13 +00:00
|
|
|
Instruction *OldCall = CS.getInstruction();
|
|
|
|
new StoreInst(ConstantInt::getTrue(Callee->getContext()),
|
2012-02-03 00:07:04 +00:00
|
|
|
UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
|
2010-01-05 07:32:13 +00:00
|
|
|
OldCall);
|
|
|
|
// If OldCall dues not return void then replaceAllUsesWith undef.
|
|
|
|
// This allows ValueHandlers and custom metadata to adjust itself.
|
|
|
|
if (!OldCall->getType()->isVoidTy())
|
2011-05-18 00:32:01 +00:00
|
|
|
ReplaceInstUsesWith(*OldCall, UndefValue::get(OldCall->getType()));
|
2010-02-01 18:04:58 +00:00
|
|
|
if (isa<CallInst>(OldCall))
|
2010-01-05 07:32:13 +00:00
|
|
|
return EraseInstFromFunction(*OldCall);
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-02-01 18:04:58 +00:00
|
|
|
// We cannot remove an invoke, because it would change the CFG, just
|
|
|
|
// change the callee to a null pointer.
|
2010-03-20 21:00:25 +00:00
|
|
|
cast<InvokeInst>(OldCall)->setCalledFunction(
|
2010-02-01 18:04:58 +00:00
|
|
|
Constant::getNullValue(CalleeF->getType()));
|
2010-01-05 07:32:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) {
|
|
|
|
// This instruction is not reachable, just remove it. We insert a store to
|
|
|
|
// undef so that we know that this code is not reachable, despite the fact
|
|
|
|
// that we can't modify the CFG here.
|
|
|
|
new StoreInst(ConstantInt::getTrue(Callee->getContext()),
|
|
|
|
UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
|
|
|
|
CS.getInstruction());
|
|
|
|
|
2010-06-24 12:58:35 +00:00
|
|
|
// If CS does not return void then replaceAllUsesWith undef.
|
2010-01-05 07:32:13 +00:00
|
|
|
// This allows ValueHandlers and custom metadata to adjust itself.
|
|
|
|
if (!CS.getInstruction()->getType()->isVoidTy())
|
2011-05-18 00:32:01 +00:00
|
|
|
ReplaceInstUsesWith(*CS.getInstruction(),
|
|
|
|
UndefValue::get(CS.getInstruction()->getType()));
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
|
|
|
|
// Don't break the CFG, insert a dummy cond branch.
|
|
|
|
BranchInst::Create(II->getNormalDest(), II->getUnwindDest(),
|
|
|
|
ConstantInt::getTrue(Callee->getContext()), II);
|
|
|
|
}
|
|
|
|
return EraseInstFromFunction(*CS.getInstruction());
|
|
|
|
}
|
|
|
|
|
2011-09-06 13:37:06 +00:00
|
|
|
if (IntrinsicInst *II = FindInitTrampoline(Callee))
|
|
|
|
return transformCallThroughTrampoline(CS, II);
|
2010-01-05 07:32:13 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *PTy = cast<PointerType>(Callee->getType());
|
|
|
|
FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
|
2010-01-05 07:32:13 +00:00
|
|
|
if (FTy->isVarArg()) {
|
2011-11-29 01:18:23 +00:00
|
|
|
int ix = FTy->getNumParams();
|
2010-01-05 07:32:13 +00:00
|
|
|
// See if we can optimize any arguments passed through the varargs area of
|
|
|
|
// the call.
|
|
|
|
for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
|
|
|
|
E = CS.arg_end(); I != E; ++I, ++ix) {
|
|
|
|
CastInst *CI = dyn_cast<CastInst>(*I);
|
|
|
|
if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) {
|
|
|
|
*I = CI->getOperand(0);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) {
|
|
|
|
// Inline asm calls cannot throw - mark them 'nounwind'.
|
|
|
|
CS.setDoesNotThrow();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2010-03-06 10:50:38 +00:00
|
|
|
// Try to optimize the call if possible, we require TargetData for most of
|
|
|
|
// this. None of these calls are seen as possibly dead so go ahead and
|
|
|
|
// delete the instruction now.
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
|
|
|
|
Instruction *I = tryOptimizeCall(CI, TD);
|
2010-03-06 10:59:25 +00:00
|
|
|
// If we changed something return the result, etc. Otherwise let
|
|
|
|
// the fallthrough check.
|
|
|
|
if (I) return EraseInstFromFunction(*I);
|
2010-03-06 10:50:38 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
return Changed ? CS.getInstruction() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transformConstExprCastCall - If the callee is a constexpr cast of a function,
|
|
|
|
// attempt to move the cast to the arguments of the call/invoke.
|
|
|
|
//
|
|
|
|
bool InstCombiner::transformConstExprCastCall(CallSite CS) {
|
2010-12-20 08:25:06 +00:00
|
|
|
Function *Callee =
|
|
|
|
dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts());
|
|
|
|
if (Callee == 0)
|
2010-01-05 07:32:13 +00:00
|
|
|
return false;
|
|
|
|
Instruction *Caller = CS.getInstruction();
|
|
|
|
const AttrListPtr &CallerPAL = CS.getAttributes();
|
|
|
|
|
|
|
|
// Okay, this is a cast from a function to a different type. Unless doing so
|
|
|
|
// would cause a type conversion of one of our arguments, change this call to
|
|
|
|
// be a direct call with arguments casted to the appropriate types.
|
|
|
|
//
|
2011-07-18 04:54:35 +00:00
|
|
|
FunctionType *FT = Callee->getFunctionType();
|
|
|
|
Type *OldRetTy = Caller->getType();
|
|
|
|
Type *NewRetTy = FT->getReturnType();
|
2010-01-05 07:32:13 +00:00
|
|
|
|
2010-02-16 11:11:14 +00:00
|
|
|
if (NewRetTy->isStructTy())
|
2010-01-05 07:32:13 +00:00
|
|
|
return false; // TODO: Handle multiple return values.
|
|
|
|
|
|
|
|
// Check to see if we are changing the return type...
|
|
|
|
if (OldRetTy != NewRetTy) {
|
|
|
|
if (Callee->isDeclaration() &&
|
|
|
|
// Conversion is ok if changing from one pointer type to another or from
|
|
|
|
// a pointer to an integer of the same size.
|
2010-02-16 11:11:14 +00:00
|
|
|
!((OldRetTy->isPointerTy() || !TD ||
|
2010-01-05 07:32:13 +00:00
|
|
|
OldRetTy == TD->getIntPtrType(Caller->getContext())) &&
|
2010-02-16 11:11:14 +00:00
|
|
|
(NewRetTy->isPointerTy() || !TD ||
|
2010-01-05 07:32:13 +00:00
|
|
|
NewRetTy == TD->getIntPtrType(Caller->getContext()))))
|
|
|
|
return false; // Cannot transform this return value.
|
|
|
|
|
|
|
|
if (!Caller->use_empty() &&
|
|
|
|
// void -> non-void is handled specially
|
|
|
|
!NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy))
|
|
|
|
return false; // Cannot transform this return value.
|
|
|
|
|
|
|
|
if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
|
|
|
|
Attributes RAttrs = CallerPAL.getRetAttributes();
|
|
|
|
if (RAttrs & Attribute::typeIncompatible(NewRetTy))
|
|
|
|
return false; // Attribute not compatible with transformed value.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the callsite is an invoke instruction, and the return value is used by
|
|
|
|
// a PHI node in a successor, we cannot change the return type of the call
|
|
|
|
// because there is no place to put the cast instruction (without breaking
|
|
|
|
// the critical edge). Bail out in this case.
|
|
|
|
if (!Caller->use_empty())
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller))
|
|
|
|
for (Value::use_iterator UI = II->use_begin(), E = II->use_end();
|
|
|
|
UI != E; ++UI)
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(*UI))
|
|
|
|
if (PN->getParent() == II->getNormalDest() ||
|
|
|
|
PN->getParent() == II->getUnwindDest())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
|
|
|
|
unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
|
|
|
|
|
|
|
|
CallSite::arg_iterator AI = CS.arg_begin();
|
|
|
|
for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ParamTy = FT->getParamType(i);
|
|
|
|
Type *ActTy = (*AI)->getType();
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
if (!CastInst::isCastable(ActTy, ParamTy))
|
|
|
|
return false; // Cannot transform this parameter value.
|
|
|
|
|
2012-01-20 17:56:17 +00:00
|
|
|
Attributes Attrs = CallerPAL.getParamAttributes(i + 1);
|
2010-12-20 08:36:38 +00:00
|
|
|
if (Attrs & Attribute::typeIncompatible(ParamTy))
|
2010-01-05 07:32:13 +00:00
|
|
|
return false; // Attribute not compatible with transformed value.
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-12-20 08:36:38 +00:00
|
|
|
// If the parameter is passed as a byval argument, then we have to have a
|
|
|
|
// sized type and the sized type has to have the same size as the old type.
|
|
|
|
if (ParamTy != ActTy && (Attrs & Attribute::ByVal)) {
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *ParamPTy = dyn_cast<PointerType>(ParamTy);
|
2010-12-20 08:36:38 +00:00
|
|
|
if (ParamPTy == 0 || !ParamPTy->getElementType()->isSized() || TD == 0)
|
|
|
|
return false;
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *CurElTy = cast<PointerType>(ActTy)->getElementType();
|
2010-12-20 08:36:38 +00:00
|
|
|
if (TD->getTypeAllocSize(CurElTy) !=
|
|
|
|
TD->getTypeAllocSize(ParamPTy->getElementType()))
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
// Converting from one pointer type to another or between a pointer and an
|
|
|
|
// integer of the same size is safe even if we do not have a body.
|
|
|
|
bool isConvertible = ActTy == ParamTy ||
|
2010-02-16 11:11:14 +00:00
|
|
|
(TD && ((ParamTy->isPointerTy() ||
|
2010-01-05 07:32:13 +00:00
|
|
|
ParamTy == TD->getIntPtrType(Caller->getContext())) &&
|
2010-02-16 11:11:14 +00:00
|
|
|
(ActTy->isPointerTy() ||
|
2010-01-05 07:32:13 +00:00
|
|
|
ActTy == TD->getIntPtrType(Caller->getContext()))));
|
|
|
|
if (Callee->isDeclaration() && !isConvertible) return false;
|
|
|
|
}
|
|
|
|
|
2011-02-24 05:10:56 +00:00
|
|
|
if (Callee->isDeclaration()) {
|
|
|
|
// Do not delete arguments unless we have a function body.
|
|
|
|
if (FT->getNumParams() < NumActualArgs && !FT->isVarArg())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If the callee is just a declaration, don't change the varargsness of the
|
|
|
|
// call. We don't want to introduce a varargs call where one doesn't
|
|
|
|
// already exist.
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *APTy = cast<PointerType>(CS.getCalledValue()->getType());
|
2011-02-24 05:10:56 +00:00
|
|
|
if (FT->isVarArg()!=cast<FunctionType>(APTy->getElementType())->isVarArg())
|
|
|
|
return false;
|
2012-02-03 00:00:55 +00:00
|
|
|
|
|
|
|
// If both the callee and the cast type are varargs, we still have to make
|
|
|
|
// sure the number of fixed parameters are the same or we have the same
|
|
|
|
// ABI issues as if we introduce a varargs call.
|
2012-02-03 00:26:07 +00:00
|
|
|
if (FT->isVarArg() &&
|
|
|
|
cast<FunctionType>(APTy->getElementType())->isVarArg() &&
|
|
|
|
FT->getNumParams() !=
|
2012-02-03 00:00:55 +00:00
|
|
|
cast<FunctionType>(APTy->getElementType())->getNumParams())
|
|
|
|
return false;
|
2011-02-24 05:10:56 +00:00
|
|
|
}
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2012-02-03 00:00:50 +00:00
|
|
|
if (FT->getNumParams() < NumActualArgs && FT->isVarArg() &&
|
|
|
|
!CallerPAL.isEmpty())
|
|
|
|
// In this case we have more arguments than the new function type, but we
|
|
|
|
// won't be dropping them. Check that these extra arguments have attributes
|
|
|
|
// that are compatible with being a vararg call argument.
|
|
|
|
for (unsigned i = CallerPAL.getNumSlots(); i; --i) {
|
|
|
|
if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams())
|
|
|
|
break;
|
|
|
|
Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs;
|
|
|
|
if (PAttrs & Attribute::VarArgsIncompatible)
|
|
|
|
return false;
|
|
|
|
}
|
2010-01-05 07:32:13 +00:00
|
|
|
|
2012-02-03 00:07:04 +00:00
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
// Okay, we decided that this is a safe thing to do: go ahead and start
|
2011-02-24 05:10:56 +00:00
|
|
|
// inserting cast instructions as necessary.
|
2010-01-05 07:32:13 +00:00
|
|
|
std::vector<Value*> Args;
|
|
|
|
Args.reserve(NumActualArgs);
|
|
|
|
SmallVector<AttributeWithIndex, 8> attrVec;
|
|
|
|
attrVec.reserve(NumCommonArgs);
|
|
|
|
|
|
|
|
// Get any return attributes.
|
|
|
|
Attributes RAttrs = CallerPAL.getRetAttributes();
|
|
|
|
|
|
|
|
// If the return value is not being used, the type may not be compatible
|
|
|
|
// with the existing attributes. Wipe out any problematic attributes.
|
|
|
|
RAttrs &= ~Attribute::typeIncompatible(NewRetTy);
|
|
|
|
|
|
|
|
// Add the new return attributes.
|
|
|
|
if (RAttrs)
|
|
|
|
attrVec.push_back(AttributeWithIndex::get(0, RAttrs));
|
|
|
|
|
|
|
|
AI = CS.arg_begin();
|
|
|
|
for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ParamTy = FT->getParamType(i);
|
2010-01-05 07:32:13 +00:00
|
|
|
if ((*AI)->getType() == ParamTy) {
|
|
|
|
Args.push_back(*AI);
|
|
|
|
} else {
|
|
|
|
Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
|
|
|
|
false, ParamTy, false);
|
2011-09-27 20:39:19 +00:00
|
|
|
Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy));
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add any parameter attributes.
|
|
|
|
if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
|
|
|
|
attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the function takes more arguments than the call was taking, add them
|
|
|
|
// now.
|
|
|
|
for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
|
|
|
|
Args.push_back(Constant::getNullValue(FT->getParamType(i)));
|
|
|
|
|
|
|
|
// If we are removing arguments to the function, emit an obnoxious warning.
|
|
|
|
if (FT->getNumParams() < NumActualArgs) {
|
|
|
|
if (!FT->isVarArg()) {
|
|
|
|
errs() << "WARNING: While resolving call to function '"
|
|
|
|
<< Callee->getName() << "' arguments were dropped!\n";
|
|
|
|
} else {
|
|
|
|
// Add all of the arguments in their promoted form to the arg list.
|
|
|
|
for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *PTy = getPromotedType((*AI)->getType());
|
2010-01-05 07:32:13 +00:00
|
|
|
if (PTy != (*AI)->getType()) {
|
|
|
|
// Must promote to pass through va_arg area!
|
|
|
|
Instruction::CastOps opcode =
|
|
|
|
CastInst::getCastOpcode(*AI, false, PTy, false);
|
2011-09-27 20:39:19 +00:00
|
|
|
Args.push_back(Builder->CreateCast(opcode, *AI, PTy));
|
2010-01-05 07:32:13 +00:00
|
|
|
} else {
|
|
|
|
Args.push_back(*AI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any parameter attributes.
|
|
|
|
if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1))
|
|
|
|
attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Attributes FnAttrs = CallerPAL.getFnAttributes())
|
|
|
|
attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs));
|
|
|
|
|
|
|
|
if (NewRetTy->isVoidTy())
|
|
|
|
Caller->setName(""); // Void type should not have a name.
|
|
|
|
|
|
|
|
const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(),
|
|
|
|
attrVec.end());
|
|
|
|
|
|
|
|
Instruction *NC;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
|
2011-05-18 01:28:27 +00:00
|
|
|
NC = Builder->CreateInvoke(Callee, II->getNormalDest(),
|
2011-07-15 08:37:34 +00:00
|
|
|
II->getUnwindDest(), Args);
|
2011-05-18 01:28:27 +00:00
|
|
|
NC->takeName(II);
|
2010-01-05 07:32:13 +00:00
|
|
|
cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
|
|
|
|
cast<InvokeInst>(NC)->setAttributes(NewCallerPAL);
|
|
|
|
} else {
|
|
|
|
CallInst *CI = cast<CallInst>(Caller);
|
2011-07-15 08:37:34 +00:00
|
|
|
NC = Builder->CreateCall(Callee, Args);
|
2011-05-18 01:28:27 +00:00
|
|
|
NC->takeName(CI);
|
2010-01-05 07:32:13 +00:00
|
|
|
if (CI->isTailCall())
|
|
|
|
cast<CallInst>(NC)->setTailCall();
|
|
|
|
cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
|
|
|
|
cast<CallInst>(NC)->setAttributes(NewCallerPAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a cast of the return type as necessary.
|
|
|
|
Value *NV = NC;
|
|
|
|
if (OldRetTy != NV->getType() && !Caller->use_empty()) {
|
|
|
|
if (!NV->getType()->isVoidTy()) {
|
2010-12-20 08:25:06 +00:00
|
|
|
Instruction::CastOps opcode =
|
|
|
|
CastInst::getCastOpcode(NC, false, OldRetTy, false);
|
2011-09-27 20:39:19 +00:00
|
|
|
NV = NC = CastInst::Create(opcode, NC, OldRetTy);
|
2011-05-27 00:19:40 +00:00
|
|
|
NC->setDebugLoc(Caller->getDebugLoc());
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
// If this is an invoke instruction, we should insert it after the first
|
|
|
|
// non-phi, instruction in the normal successor block.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
|
2011-08-25 01:08:34 +00:00
|
|
|
BasicBlock::iterator I = II->getNormalDest()->getFirstInsertionPt();
|
2010-01-05 07:32:13 +00:00
|
|
|
InsertNewInstBefore(NC, *I);
|
|
|
|
} else {
|
2010-12-20 08:25:06 +00:00
|
|
|
// Otherwise, it's a call, just insert cast right after the call.
|
2010-01-05 07:32:13 +00:00
|
|
|
InsertNewInstBefore(NC, *Caller);
|
|
|
|
}
|
|
|
|
Worklist.AddUsersToWorkList(*Caller);
|
|
|
|
} else {
|
|
|
|
NV = UndefValue::get(Caller->getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Caller->use_empty())
|
2011-05-18 00:32:01 +00:00
|
|
|
ReplaceInstUsesWith(*Caller, NV);
|
|
|
|
|
2010-01-05 07:32:13 +00:00
|
|
|
EraseInstFromFunction(*Caller);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-06 13:37:06 +00:00
|
|
|
// transformCallThroughTrampoline - Turn a call to a function created by
|
|
|
|
// init_trampoline / adjust_trampoline intrinsic pair into a direct call to the
|
|
|
|
// underlying function.
|
2010-01-05 07:32:13 +00:00
|
|
|
//
|
2011-09-06 13:37:06 +00:00
|
|
|
Instruction *
|
|
|
|
InstCombiner::transformCallThroughTrampoline(CallSite CS,
|
|
|
|
IntrinsicInst *Tramp) {
|
2010-01-05 07:32:13 +00:00
|
|
|
Value *Callee = CS.getCalledValue();
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *PTy = cast<PointerType>(Callee->getType());
|
|
|
|
FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
|
2010-01-05 07:32:13 +00:00
|
|
|
const AttrListPtr &Attrs = CS.getAttributes();
|
|
|
|
|
|
|
|
// If the call already has the 'nest' attribute somewhere then give up -
|
|
|
|
// otherwise 'nest' would occur twice after splicing in the chain.
|
|
|
|
if (Attrs.hasAttrSomewhere(Attribute::Nest))
|
|
|
|
return 0;
|
|
|
|
|
2011-09-06 13:37:06 +00:00
|
|
|
assert(Tramp &&
|
|
|
|
"transformCallThroughTrampoline called with incorrect CallSite.");
|
2010-01-05 07:32:13 +00:00
|
|
|
|
2010-07-22 10:37:47 +00:00
|
|
|
Function *NestF =cast<Function>(Tramp->getArgOperand(1)->stripPointerCasts());
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *NestFPTy = cast<PointerType>(NestF->getType());
|
|
|
|
FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
|
2010-01-05 07:32:13 +00:00
|
|
|
|
|
|
|
const AttrListPtr &NestAttrs = NestF->getAttributes();
|
|
|
|
if (!NestAttrs.isEmpty()) {
|
|
|
|
unsigned NestIdx = 1;
|
2011-07-12 14:06:48 +00:00
|
|
|
Type *NestTy = 0;
|
2010-01-05 07:32:13 +00:00
|
|
|
Attributes NestAttr = Attribute::None;
|
|
|
|
|
|
|
|
// Look for a parameter marked with the 'nest' attribute.
|
|
|
|
for (FunctionType::param_iterator I = NestFTy->param_begin(),
|
|
|
|
E = NestFTy->param_end(); I != E; ++NestIdx, ++I)
|
|
|
|
if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) {
|
|
|
|
// Record the parameter type and any other attributes.
|
|
|
|
NestTy = *I;
|
|
|
|
NestAttr = NestAttrs.getParamAttributes(NestIdx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NestTy) {
|
|
|
|
Instruction *Caller = CS.getInstruction();
|
|
|
|
std::vector<Value*> NewArgs;
|
|
|
|
NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1);
|
|
|
|
|
|
|
|
SmallVector<AttributeWithIndex, 8> NewAttrs;
|
|
|
|
NewAttrs.reserve(Attrs.getNumSlots() + 1);
|
|
|
|
|
|
|
|
// Insert the nest argument into the call argument list, which may
|
|
|
|
// mean appending it. Likewise for attributes.
|
|
|
|
|
|
|
|
// Add any result attributes.
|
|
|
|
if (Attributes Attr = Attrs.getRetAttributes())
|
|
|
|
NewAttrs.push_back(AttributeWithIndex::get(0, Attr));
|
|
|
|
|
|
|
|
{
|
|
|
|
unsigned Idx = 1;
|
|
|
|
CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
|
|
|
|
do {
|
|
|
|
if (Idx == NestIdx) {
|
|
|
|
// Add the chain argument and attributes.
|
2010-06-24 12:58:35 +00:00
|
|
|
Value *NestVal = Tramp->getArgOperand(2);
|
2010-01-05 07:32:13 +00:00
|
|
|
if (NestVal->getType() != NestTy)
|
2011-05-18 23:58:37 +00:00
|
|
|
NestVal = Builder->CreateBitCast(NestVal, NestTy, "nest");
|
2010-01-05 07:32:13 +00:00
|
|
|
NewArgs.push_back(NestVal);
|
|
|
|
NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Add the original argument and attributes.
|
|
|
|
NewArgs.push_back(*I);
|
|
|
|
if (Attributes Attr = Attrs.getParamAttributes(Idx))
|
|
|
|
NewAttrs.push_back
|
|
|
|
(AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr));
|
|
|
|
|
|
|
|
++Idx, ++I;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any function attributes.
|
|
|
|
if (Attributes Attr = Attrs.getFnAttributes())
|
|
|
|
NewAttrs.push_back(AttributeWithIndex::get(~0, Attr));
|
|
|
|
|
|
|
|
// The trampoline may have been bitcast to a bogus type (FTy).
|
|
|
|
// Handle this by synthesizing a new function type, equal to FTy
|
|
|
|
// with the chain parameter inserted.
|
|
|
|
|
2011-07-12 14:06:48 +00:00
|
|
|
std::vector<Type*> NewTypes;
|
2010-01-05 07:32:13 +00:00
|
|
|
NewTypes.reserve(FTy->getNumParams()+1);
|
|
|
|
|
|
|
|
// Insert the chain's type into the list of parameter types, which may
|
|
|
|
// mean appending it.
|
|
|
|
{
|
|
|
|
unsigned Idx = 1;
|
|
|
|
FunctionType::param_iterator I = FTy->param_begin(),
|
|
|
|
E = FTy->param_end();
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (Idx == NestIdx)
|
|
|
|
// Add the chain's type.
|
|
|
|
NewTypes.push_back(NestTy);
|
|
|
|
|
|
|
|
if (I == E)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Add the original type.
|
|
|
|
NewTypes.push_back(*I);
|
|
|
|
|
|
|
|
++Idx, ++I;
|
|
|
|
} while (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the trampoline call with a direct call. Let the generic
|
|
|
|
// code sort out any function type mismatches.
|
2012-02-03 00:07:04 +00:00
|
|
|
FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes,
|
2010-01-05 07:32:13 +00:00
|
|
|
FTy->isVarArg());
|
|
|
|
Constant *NewCallee =
|
|
|
|
NestF->getType() == PointerType::getUnqual(NewFTy) ?
|
2012-02-03 00:07:04 +00:00
|
|
|
NestF : ConstantExpr::getBitCast(NestF,
|
2010-01-05 07:32:13 +00:00
|
|
|
PointerType::getUnqual(NewFTy));
|
|
|
|
const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(),
|
|
|
|
NewAttrs.end());
|
|
|
|
|
|
|
|
Instruction *NewCaller;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
|
|
|
|
NewCaller = InvokeInst::Create(NewCallee,
|
|
|
|
II->getNormalDest(), II->getUnwindDest(),
|
2011-07-15 08:37:34 +00:00
|
|
|
NewArgs);
|
2010-01-05 07:32:13 +00:00
|
|
|
cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
|
|
|
|
cast<InvokeInst>(NewCaller)->setAttributes(NewPAL);
|
|
|
|
} else {
|
2011-07-15 08:37:34 +00:00
|
|
|
NewCaller = CallInst::Create(NewCallee, NewArgs);
|
2010-01-05 07:32:13 +00:00
|
|
|
if (cast<CallInst>(Caller)->isTailCall())
|
|
|
|
cast<CallInst>(NewCaller)->setTailCall();
|
|
|
|
cast<CallInst>(NewCaller)->
|
|
|
|
setCallingConv(cast<CallInst>(Caller)->getCallingConv());
|
|
|
|
cast<CallInst>(NewCaller)->setAttributes(NewPAL);
|
|
|
|
}
|
2011-05-18 19:57:14 +00:00
|
|
|
|
|
|
|
return NewCaller;
|
2010-01-05 07:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace the trampoline call with a direct call. Since there is no 'nest'
|
|
|
|
// parameter, there is no need to adjust the argument list. Let the generic
|
|
|
|
// code sort out any function type mismatches.
|
|
|
|
Constant *NewCallee =
|
2012-02-03 00:07:04 +00:00
|
|
|
NestF->getType() == PTy ? NestF :
|
2010-01-05 07:32:13 +00:00
|
|
|
ConstantExpr::getBitCast(NestF, PTy);
|
|
|
|
CS.setCalledFunction(NewCallee);
|
|
|
|
return CS.getInstruction();
|
|
|
|
}
|