2011-12-02 23:16:29 +01:00
|
|
|
//===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===//
|
2010-04-21 03:22:34 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines several CodeGen-specific LLVM IR analysis utilties.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2011-12-15 00:49:11 +01:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2010-04-21 03:22:34 +02:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/IntrinsicInst.h"
|
|
|
|
#include "llvm/LLVMContext.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2010-12-01 00:55:39 +01:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
2010-04-21 03:22:34 +02:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
|
|
|
|
/// of insertvalue or extractvalue indices that identify a member, return
|
|
|
|
/// the linearized index of the start of the member.
|
|
|
|
///
|
2011-07-18 06:54:35 +02:00
|
|
|
unsigned llvm::ComputeLinearIndex(Type *Ty,
|
2010-04-21 03:22:34 +02:00
|
|
|
const unsigned *Indices,
|
|
|
|
const unsigned *IndicesEnd,
|
|
|
|
unsigned CurIndex) {
|
|
|
|
// Base case: We're done.
|
|
|
|
if (Indices && Indices == IndicesEnd)
|
|
|
|
return CurIndex;
|
|
|
|
|
|
|
|
// Given a struct type, recursively traverse the elements.
|
2011-07-18 06:54:35 +02:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty)) {
|
2010-04-21 03:22:34 +02:00
|
|
|
for (StructType::element_iterator EB = STy->element_begin(),
|
|
|
|
EI = EB,
|
|
|
|
EE = STy->element_end();
|
|
|
|
EI != EE; ++EI) {
|
|
|
|
if (Indices && *Indices == unsigned(EI - EB))
|
2010-10-06 18:18:29 +02:00
|
|
|
return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex);
|
|
|
|
CurIndex = ComputeLinearIndex(*EI, 0, 0, CurIndex);
|
2010-04-21 03:22:34 +02:00
|
|
|
}
|
|
|
|
return CurIndex;
|
|
|
|
}
|
|
|
|
// Given an array type, recursively traverse the elements.
|
2011-07-18 06:54:35 +02:00
|
|
|
else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Type *EltTy = ATy->getElementType();
|
2010-04-21 03:22:34 +02:00
|
|
|
for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
|
|
|
|
if (Indices && *Indices == i)
|
2010-10-06 18:18:29 +02:00
|
|
|
return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
|
|
|
|
CurIndex = ComputeLinearIndex(EltTy, 0, 0, CurIndex);
|
2010-04-21 03:22:34 +02:00
|
|
|
}
|
|
|
|
return CurIndex;
|
|
|
|
}
|
|
|
|
// We haven't found the type we're looking for, so keep searching.
|
|
|
|
return CurIndex + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
|
|
|
|
/// EVTs that represent all the individual underlying
|
|
|
|
/// non-aggregate types that comprise it.
|
|
|
|
///
|
|
|
|
/// If Offsets is non-null, it points to a vector to be filled in
|
|
|
|
/// with the in-memory offsets of each of the individual values.
|
|
|
|
///
|
2011-07-18 06:54:35 +02:00
|
|
|
void llvm::ComputeValueVTs(const TargetLowering &TLI, Type *Ty,
|
2010-04-21 03:22:34 +02:00
|
|
|
SmallVectorImpl<EVT> &ValueVTs,
|
|
|
|
SmallVectorImpl<uint64_t> *Offsets,
|
|
|
|
uint64_t StartingOffset) {
|
|
|
|
// Given a struct type, recursively traverse the elements.
|
2011-07-18 06:54:35 +02:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty)) {
|
2010-04-21 03:22:34 +02:00
|
|
|
const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
|
|
|
|
for (StructType::element_iterator EB = STy->element_begin(),
|
|
|
|
EI = EB,
|
|
|
|
EE = STy->element_end();
|
|
|
|
EI != EE; ++EI)
|
|
|
|
ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
|
|
|
|
StartingOffset + SL->getElementOffset(EI - EB));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Given an array type, recursively traverse the elements.
|
2011-07-18 06:54:35 +02:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Type *EltTy = ATy->getElementType();
|
2010-04-21 03:22:34 +02:00
|
|
|
uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy);
|
|
|
|
for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
|
|
|
|
ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
|
|
|
|
StartingOffset + i * EltSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Interpret void as zero return values.
|
|
|
|
if (Ty->isVoidTy())
|
|
|
|
return;
|
|
|
|
// Base case: we can get an EVT for this LLVM IR type.
|
|
|
|
ValueVTs.push_back(TLI.getValueType(Ty));
|
|
|
|
if (Offsets)
|
|
|
|
Offsets->push_back(StartingOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
|
|
|
|
GlobalVariable *llvm::ExtractTypeInfo(Value *V) {
|
|
|
|
V = V->stripPointerCasts();
|
|
|
|
GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
|
|
|
|
|
2010-07-27 00:36:52 +02:00
|
|
|
if (GV && GV->getName() == "llvm.eh.catch.all.value") {
|
2010-04-21 03:22:34 +02:00
|
|
|
assert(GV->hasInitializer() &&
|
|
|
|
"The EH catch-all value must have an initializer");
|
|
|
|
Value *Init = GV->getInitializer();
|
|
|
|
GV = dyn_cast<GlobalVariable>(Init);
|
|
|
|
if (!GV) V = cast<ConstantPointerNull>(Init);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((GV || isa<ConstantPointerNull>(V)) &&
|
|
|
|
"TypeInfo must be a global variable or NULL");
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
|
|
|
|
/// processed uses a memory 'm' constraint.
|
|
|
|
bool
|
2010-10-29 19:29:13 +02:00
|
|
|
llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos,
|
2010-04-21 03:22:34 +02:00
|
|
|
const TargetLowering &TLI) {
|
|
|
|
for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
|
|
|
|
InlineAsm::ConstraintInfo &CI = CInfos[i];
|
|
|
|
for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
|
|
|
|
TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
|
|
|
|
if (CType == TargetLowering::C_Memory)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indirect operand accesses access memory.
|
|
|
|
if (CI.isIndirect)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getFCmpCondCode - Return the ISD condition code corresponding to
|
|
|
|
/// the given LLVM IR floating-point condition code. This includes
|
|
|
|
/// consideration of global floating-point math flags.
|
|
|
|
///
|
|
|
|
ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {
|
|
|
|
switch (Pred) {
|
2011-12-02 23:16:29 +01:00
|
|
|
case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;
|
|
|
|
case FCmpInst::FCMP_OEQ: return ISD::SETOEQ;
|
|
|
|
case FCmpInst::FCMP_OGT: return ISD::SETOGT;
|
|
|
|
case FCmpInst::FCMP_OGE: return ISD::SETOGE;
|
|
|
|
case FCmpInst::FCMP_OLT: return ISD::SETOLT;
|
|
|
|
case FCmpInst::FCMP_OLE: return ISD::SETOLE;
|
|
|
|
case FCmpInst::FCMP_ONE: return ISD::SETONE;
|
|
|
|
case FCmpInst::FCMP_ORD: return ISD::SETO;
|
|
|
|
case FCmpInst::FCMP_UNO: return ISD::SETUO;
|
|
|
|
case FCmpInst::FCMP_UEQ: return ISD::SETUEQ;
|
|
|
|
case FCmpInst::FCMP_UGT: return ISD::SETUGT;
|
|
|
|
case FCmpInst::FCMP_UGE: return ISD::SETUGE;
|
|
|
|
case FCmpInst::FCMP_ULT: return ISD::SETULT;
|
|
|
|
case FCmpInst::FCMP_ULE: return ISD::SETULE;
|
|
|
|
case FCmpInst::FCMP_UNE: return ISD::SETUNE;
|
|
|
|
case FCmpInst::FCMP_TRUE: return ISD::SETTRUE;
|
2012-01-20 22:51:11 +01:00
|
|
|
default: llvm_unreachable("Invalid FCmp predicate opcode!");
|
2011-12-02 23:16:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {
|
|
|
|
switch (CC) {
|
|
|
|
case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;
|
|
|
|
case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;
|
|
|
|
case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;
|
|
|
|
case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;
|
|
|
|
case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;
|
|
|
|
case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;
|
2012-01-20 22:51:11 +01:00
|
|
|
default: return CC;
|
2010-04-21 03:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getICmpCondCode - Return the ISD condition code corresponding to
|
|
|
|
/// the given LLVM IR integer condition code.
|
|
|
|
///
|
|
|
|
ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {
|
|
|
|
switch (Pred) {
|
|
|
|
case ICmpInst::ICMP_EQ: return ISD::SETEQ;
|
|
|
|
case ICmpInst::ICMP_NE: return ISD::SETNE;
|
|
|
|
case ICmpInst::ICMP_SLE: return ISD::SETLE;
|
|
|
|
case ICmpInst::ICMP_ULE: return ISD::SETULE;
|
|
|
|
case ICmpInst::ICMP_SGE: return ISD::SETGE;
|
|
|
|
case ICmpInst::ICMP_UGE: return ISD::SETUGE;
|
|
|
|
case ICmpInst::ICMP_SLT: return ISD::SETLT;
|
|
|
|
case ICmpInst::ICMP_ULT: return ISD::SETULT;
|
|
|
|
case ICmpInst::ICMP_SGT: return ISD::SETGT;
|
|
|
|
case ICmpInst::ICMP_UGT: return ISD::SETUGT;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid ICmp predicate opcode!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test if the given instruction is in a position to be optimized
|
|
|
|
/// with a tail-call. This roughly means that it's in a block with
|
|
|
|
/// a return and there's nothing that needs to be scheduled
|
|
|
|
/// between it and the return.
|
|
|
|
///
|
|
|
|
/// This function only tests target-independent requirements.
|
|
|
|
bool llvm::isInTailCallPosition(ImmutableCallSite CS, Attributes CalleeRetAttr,
|
|
|
|
const TargetLowering &TLI) {
|
|
|
|
const Instruction *I = CS.getInstruction();
|
|
|
|
const BasicBlock *ExitBB = I->getParent();
|
|
|
|
const TerminatorInst *Term = ExitBB->getTerminator();
|
|
|
|
const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
|
|
|
|
|
|
|
|
// The block must end in a return statement or unreachable.
|
|
|
|
//
|
|
|
|
// FIXME: Decline tailcall if it's not guaranteed and if the block ends in
|
|
|
|
// an unreachable, for now. The way tailcall optimization is currently
|
|
|
|
// implemented means it will add an epilogue followed by a jump. That is
|
|
|
|
// not profitable. Also, if the callee is a special function (e.g.
|
|
|
|
// longjmp on x86), it can end up causing miscompilation that has not
|
|
|
|
// been fully understood.
|
|
|
|
if (!Ret &&
|
2011-12-02 23:16:29 +01:00
|
|
|
(!TLI.getTargetMachine().Options.GuaranteedTailCallOpt ||
|
|
|
|
!isa<UnreachableInst>(Term))) return false;
|
2010-04-21 03:22:34 +02:00
|
|
|
|
|
|
|
// If I will have a chain, make sure no other instruction that will have a
|
|
|
|
// chain interposes between I and the return.
|
|
|
|
if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
|
2011-12-15 00:49:11 +01:00
|
|
|
!isSafeToSpeculativelyExecute(I))
|
2010-04-21 03:22:34 +02:00
|
|
|
for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
|
|
|
|
--BBI) {
|
|
|
|
if (&*BBI == I)
|
|
|
|
break;
|
|
|
|
// Debug info intrinsics do not get in the way of tail call optimization.
|
|
|
|
if (isa<DbgInfoIntrinsic>(BBI))
|
|
|
|
continue;
|
|
|
|
if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
|
2011-12-15 00:49:11 +01:00
|
|
|
!isSafeToSpeculativelyExecute(BBI))
|
2010-04-21 03:22:34 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the block ends with a void return or unreachable, it doesn't matter
|
|
|
|
// what the call's return type is.
|
|
|
|
if (!Ret || Ret->getNumOperands() == 0) return true;
|
|
|
|
|
|
|
|
// If the return value is undef, it doesn't matter what the call's
|
|
|
|
// return type is.
|
|
|
|
if (isa<UndefValue>(Ret->getOperand(0))) return true;
|
|
|
|
|
|
|
|
// Conservatively require the attributes of the call to match those of
|
|
|
|
// the return. Ignore noalias because it doesn't affect the call sequence.
|
2011-03-19 18:03:16 +01:00
|
|
|
const Function *F = ExitBB->getParent();
|
2012-01-20 18:56:17 +01:00
|
|
|
Attributes CallerRetAttr = F->getAttributes().getRetAttributes();
|
2010-04-21 03:22:34 +02:00
|
|
|
if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// It's not safe to eliminate the sign / zero extension of the return value.
|
|
|
|
if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, make sure the unmodified return value of I is the return value.
|
|
|
|
for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
|
|
|
|
U = dyn_cast<Instruction>(U->getOperand(0))) {
|
|
|
|
if (!U)
|
|
|
|
return false;
|
|
|
|
if (!U->hasOneUse())
|
|
|
|
return false;
|
|
|
|
if (U == I)
|
|
|
|
break;
|
|
|
|
// Check for a truly no-op truncate.
|
|
|
|
if (isa<TruncInst>(U) &&
|
|
|
|
TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
|
|
|
|
continue;
|
|
|
|
// Check for a truly no-op bitcast.
|
|
|
|
if (isa<BitCastInst>(U) &&
|
|
|
|
(U->getOperand(0)->getType() == U->getType() ||
|
|
|
|
(U->getOperand(0)->getType()->isPointerTy() &&
|
|
|
|
U->getType()->isPointerTy())))
|
|
|
|
continue;
|
|
|
|
// Otherwise it's not a true no-op.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-12-01 00:55:39 +01:00
|
|
|
bool llvm::isInTailCallPosition(SelectionDAG &DAG, SDNode *Node,
|
2012-04-10 03:51:00 +02:00
|
|
|
SDValue &Chain, const TargetLowering &TLI) {
|
2010-12-01 00:55:39 +01:00
|
|
|
const Function *F = DAG.getMachineFunction().getFunction();
|
|
|
|
|
|
|
|
// Conservatively require the attributes of the call to match those of
|
|
|
|
// the return. Ignore noalias because it doesn't affect the call sequence.
|
2012-01-20 18:56:17 +01:00
|
|
|
Attributes CallerRetAttr = F->getAttributes().getRetAttributes();
|
2010-12-01 00:55:39 +01:00
|
|
|
if (CallerRetAttr & ~Attribute::NoAlias)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// It's not safe to eliminate the sign / zero extension of the return value.
|
|
|
|
if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if the only use is a function return node.
|
2012-04-10 03:51:00 +02:00
|
|
|
return TLI.isUsedByReturnOnly(Node, Chain);
|
2010-12-01 00:55:39 +01:00
|
|
|
}
|