//===- 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/IntrinsicInst.h"
#include "llvm/Support/CallSite.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Analysis/MemoryBuiltins.h"
using namespace llvm;
/// getPromotedType - Return the specified type promoted as it would be to pass
/// though a va_arg area.
static const Type *getPromotedType(const Type *Ty) {
if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) {
if (ITy->getBitWidth() < 32)
return Type::getInt32Ty(Ty->getContext());
}
return Ty;
}
/// EnforceKnownAlignment - If the specified pointer points to an object that
/// we control, modify the object's alignment to PrefAlign. This isn't
/// often possible though. If alignment is important, a more reliable approach
/// is to simply align all global variables and allocation instructions to
/// their preferred alignment from the beginning.
///
static unsigned EnforceKnownAlignment(Value *V,
unsigned Align, unsigned PrefAlign) {
User *U = dyn_cast<User>(V);
if (!U) return Align;
switch (Operator::getOpcode(U)) {
default: break;
case Instruction::BitCast:
return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
case Instruction::GetElementPtr: {
// If all indexes are zero, it is just the alignment of the base pointer.
bool AllZeroOperands = true;
for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i)
if (!isa<Constant>(*i) ||
!cast<Constant>(*i)->isNullValue()) {
AllZeroOperands = false;
break;
}
if (AllZeroOperands) {
// Treat this like a bitcast.
return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign);
}
break;
}
}
if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
// If there is a large requested alignment and we can, bump up the alignment
// of the global.
if (!GV->isDeclaration()) {
if (GV->getAlignment() >= PrefAlign)
Align = GV->getAlignment();
else {
GV->setAlignment(PrefAlign);
Align = PrefAlign;
}
}
} else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
// If there is a requested alignment and if this is an alloca, round up.
if (AI->getAlignment() >= PrefAlign)
Align = AI->getAlignment();
else {
AI->setAlignment(PrefAlign);
Align = PrefAlign;
}