150f02cb2SNick Lewycky //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===// 2450aa64fSDan Gohman // 3450aa64fSDan Gohman // The LLVM Compiler Infrastructure 4450aa64fSDan Gohman // 5450aa64fSDan Gohman // This file is distributed under the University of Illinois Open Source 6450aa64fSDan Gohman // License. See LICENSE.TXT for details. 7450aa64fSDan Gohman // 8450aa64fSDan Gohman //===----------------------------------------------------------------------===// 9450aa64fSDan Gohman // 10450aa64fSDan Gohman // This file defines several CodeGen-specific LLVM IR analysis utilties. 11450aa64fSDan Gohman // 12450aa64fSDan Gohman //===----------------------------------------------------------------------===// 13450aa64fSDan Gohman 14450aa64fSDan Gohman #include "llvm/CodeGen/Analysis.h" 1575d7d5e9SDan Gohman #include "llvm/Analysis/ValueTracking.h" 16ed0881b2SChandler Carruth #include "llvm/CodeGen/MachineFunction.h" 179fb823bbSChandler Carruth #include "llvm/IR/DataLayout.h" 189fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h" 199fb823bbSChandler Carruth #include "llvm/IR/Function.h" 209fb823bbSChandler Carruth #include "llvm/IR/Instructions.h" 219fb823bbSChandler Carruth #include "llvm/IR/IntrinsicInst.h" 229fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 239fb823bbSChandler Carruth #include "llvm/IR/Module.h" 24450aa64fSDan Gohman #include "llvm/Support/ErrorHandling.h" 25450aa64fSDan Gohman #include "llvm/Support/MathExtras.h" 26ed0881b2SChandler Carruth #include "llvm/Target/TargetLowering.h" 27450aa64fSDan Gohman using namespace llvm; 28450aa64fSDan Gohman 29450aa64fSDan Gohman /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence 30450aa64fSDan Gohman /// of insertvalue or extractvalue indices that identify a member, return 31450aa64fSDan Gohman /// the linearized index of the start of the member. 32450aa64fSDan Gohman /// 33229907cdSChris Lattner unsigned llvm::ComputeLinearIndex(Type *Ty, 34450aa64fSDan Gohman const unsigned *Indices, 35450aa64fSDan Gohman const unsigned *IndicesEnd, 36450aa64fSDan Gohman unsigned CurIndex) { 37450aa64fSDan Gohman // Base case: We're done. 38450aa64fSDan Gohman if (Indices && Indices == IndicesEnd) 39450aa64fSDan Gohman return CurIndex; 40450aa64fSDan Gohman 41450aa64fSDan Gohman // Given a struct type, recursively traverse the elements. 42229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 43450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(), 44450aa64fSDan Gohman EI = EB, 45450aa64fSDan Gohman EE = STy->element_end(); 46450aa64fSDan Gohman EI != EE; ++EI) { 47450aa64fSDan Gohman if (Indices && *Indices == unsigned(EI - EB)) 48aadc5596SDan Gohman return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex); 49aadc5596SDan Gohman CurIndex = ComputeLinearIndex(*EI, 0, 0, CurIndex); 50450aa64fSDan Gohman } 51450aa64fSDan Gohman return CurIndex; 52450aa64fSDan Gohman } 53450aa64fSDan Gohman // Given an array type, recursively traverse the elements. 54229907cdSChris Lattner else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 55229907cdSChris Lattner Type *EltTy = ATy->getElementType(); 56450aa64fSDan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { 57450aa64fSDan Gohman if (Indices && *Indices == i) 58aadc5596SDan Gohman return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex); 59aadc5596SDan Gohman CurIndex = ComputeLinearIndex(EltTy, 0, 0, CurIndex); 60450aa64fSDan Gohman } 61450aa64fSDan Gohman return CurIndex; 62450aa64fSDan Gohman } 63450aa64fSDan Gohman // We haven't found the type we're looking for, so keep searching. 64450aa64fSDan Gohman return CurIndex + 1; 65450aa64fSDan Gohman } 66450aa64fSDan Gohman 67450aa64fSDan Gohman /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of 68450aa64fSDan Gohman /// EVTs that represent all the individual underlying 69450aa64fSDan Gohman /// non-aggregate types that comprise it. 70450aa64fSDan Gohman /// 71450aa64fSDan Gohman /// If Offsets is non-null, it points to a vector to be filled in 72450aa64fSDan Gohman /// with the in-memory offsets of each of the individual values. 73450aa64fSDan Gohman /// 74229907cdSChris Lattner void llvm::ComputeValueVTs(const TargetLowering &TLI, Type *Ty, 75450aa64fSDan Gohman SmallVectorImpl<EVT> &ValueVTs, 76450aa64fSDan Gohman SmallVectorImpl<uint64_t> *Offsets, 77450aa64fSDan Gohman uint64_t StartingOffset) { 78450aa64fSDan Gohman // Given a struct type, recursively traverse the elements. 79229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 80cdfe20b9SMicah Villmow const StructLayout *SL = TLI.getDataLayout()->getStructLayout(STy); 81450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(), 82450aa64fSDan Gohman EI = EB, 83450aa64fSDan Gohman EE = STy->element_end(); 84450aa64fSDan Gohman EI != EE; ++EI) 85450aa64fSDan Gohman ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, 86450aa64fSDan Gohman StartingOffset + SL->getElementOffset(EI - EB)); 87450aa64fSDan Gohman return; 88450aa64fSDan Gohman } 89450aa64fSDan Gohman // Given an array type, recursively traverse the elements. 90229907cdSChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 91229907cdSChris Lattner Type *EltTy = ATy->getElementType(); 92cdfe20b9SMicah Villmow uint64_t EltSize = TLI.getDataLayout()->getTypeAllocSize(EltTy); 93450aa64fSDan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) 94450aa64fSDan Gohman ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, 95450aa64fSDan Gohman StartingOffset + i * EltSize); 96450aa64fSDan Gohman return; 97450aa64fSDan Gohman } 98450aa64fSDan Gohman // Interpret void as zero return values. 99450aa64fSDan Gohman if (Ty->isVoidTy()) 100450aa64fSDan Gohman return; 101450aa64fSDan Gohman // Base case: we can get an EVT for this LLVM IR type. 102450aa64fSDan Gohman ValueVTs.push_back(TLI.getValueType(Ty)); 103450aa64fSDan Gohman if (Offsets) 104450aa64fSDan Gohman Offsets->push_back(StartingOffset); 105450aa64fSDan Gohman } 106450aa64fSDan Gohman 107450aa64fSDan Gohman /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. 108450aa64fSDan Gohman GlobalVariable *llvm::ExtractTypeInfo(Value *V) { 109450aa64fSDan Gohman V = V->stripPointerCasts(); 110450aa64fSDan Gohman GlobalVariable *GV = dyn_cast<GlobalVariable>(V); 111450aa64fSDan Gohman 112fa60b0eeSBill Wendling if (GV && GV->getName() == "llvm.eh.catch.all.value") { 113450aa64fSDan Gohman assert(GV->hasInitializer() && 114450aa64fSDan Gohman "The EH catch-all value must have an initializer"); 115450aa64fSDan Gohman Value *Init = GV->getInitializer(); 116450aa64fSDan Gohman GV = dyn_cast<GlobalVariable>(Init); 117450aa64fSDan Gohman if (!GV) V = cast<ConstantPointerNull>(Init); 118450aa64fSDan Gohman } 119450aa64fSDan Gohman 120450aa64fSDan Gohman assert((GV || isa<ConstantPointerNull>(V)) && 121450aa64fSDan Gohman "TypeInfo must be a global variable or NULL"); 122450aa64fSDan Gohman return GV; 123450aa64fSDan Gohman } 124450aa64fSDan Gohman 125450aa64fSDan Gohman /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being 126450aa64fSDan Gohman /// processed uses a memory 'm' constraint. 127450aa64fSDan Gohman bool 128e8360b71SJohn Thompson llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos, 129450aa64fSDan Gohman const TargetLowering &TLI) { 130450aa64fSDan Gohman for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { 131450aa64fSDan Gohman InlineAsm::ConstraintInfo &CI = CInfos[i]; 132450aa64fSDan Gohman for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { 133450aa64fSDan Gohman TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); 134450aa64fSDan Gohman if (CType == TargetLowering::C_Memory) 135450aa64fSDan Gohman return true; 136450aa64fSDan Gohman } 137450aa64fSDan Gohman 138450aa64fSDan Gohman // Indirect operand accesses access memory. 139450aa64fSDan Gohman if (CI.isIndirect) 140450aa64fSDan Gohman return true; 141450aa64fSDan Gohman } 142450aa64fSDan Gohman 143450aa64fSDan Gohman return false; 144450aa64fSDan Gohman } 145450aa64fSDan Gohman 146450aa64fSDan Gohman /// getFCmpCondCode - Return the ISD condition code corresponding to 147450aa64fSDan Gohman /// the given LLVM IR floating-point condition code. This includes 148450aa64fSDan Gohman /// consideration of global floating-point math flags. 149450aa64fSDan Gohman /// 150450aa64fSDan Gohman ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) { 151450aa64fSDan Gohman switch (Pred) { 15250f02cb2SNick Lewycky case FCmpInst::FCMP_FALSE: return ISD::SETFALSE; 15350f02cb2SNick Lewycky case FCmpInst::FCMP_OEQ: return ISD::SETOEQ; 15450f02cb2SNick Lewycky case FCmpInst::FCMP_OGT: return ISD::SETOGT; 15550f02cb2SNick Lewycky case FCmpInst::FCMP_OGE: return ISD::SETOGE; 15650f02cb2SNick Lewycky case FCmpInst::FCMP_OLT: return ISD::SETOLT; 15750f02cb2SNick Lewycky case FCmpInst::FCMP_OLE: return ISD::SETOLE; 15850f02cb2SNick Lewycky case FCmpInst::FCMP_ONE: return ISD::SETONE; 15950f02cb2SNick Lewycky case FCmpInst::FCMP_ORD: return ISD::SETO; 16050f02cb2SNick Lewycky case FCmpInst::FCMP_UNO: return ISD::SETUO; 16150f02cb2SNick Lewycky case FCmpInst::FCMP_UEQ: return ISD::SETUEQ; 16250f02cb2SNick Lewycky case FCmpInst::FCMP_UGT: return ISD::SETUGT; 16350f02cb2SNick Lewycky case FCmpInst::FCMP_UGE: return ISD::SETUGE; 16450f02cb2SNick Lewycky case FCmpInst::FCMP_ULT: return ISD::SETULT; 16550f02cb2SNick Lewycky case FCmpInst::FCMP_ULE: return ISD::SETULE; 16650f02cb2SNick Lewycky case FCmpInst::FCMP_UNE: return ISD::SETUNE; 16750f02cb2SNick Lewycky case FCmpInst::FCMP_TRUE: return ISD::SETTRUE; 16846a9f016SDavid Blaikie default: llvm_unreachable("Invalid FCmp predicate opcode!"); 169450aa64fSDan Gohman } 17050f02cb2SNick Lewycky } 17150f02cb2SNick Lewycky 17250f02cb2SNick Lewycky ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) { 17350f02cb2SNick Lewycky switch (CC) { 17450f02cb2SNick Lewycky case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ; 17550f02cb2SNick Lewycky case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE; 17650f02cb2SNick Lewycky case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT; 17750f02cb2SNick Lewycky case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE; 17850f02cb2SNick Lewycky case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT; 17950f02cb2SNick Lewycky case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE; 18046a9f016SDavid Blaikie default: return CC; 18150f02cb2SNick Lewycky } 182450aa64fSDan Gohman } 183450aa64fSDan Gohman 184450aa64fSDan Gohman /// getICmpCondCode - Return the ISD condition code corresponding to 185450aa64fSDan Gohman /// the given LLVM IR integer condition code. 186450aa64fSDan Gohman /// 187450aa64fSDan Gohman ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) { 188450aa64fSDan Gohman switch (Pred) { 189450aa64fSDan Gohman case ICmpInst::ICMP_EQ: return ISD::SETEQ; 190450aa64fSDan Gohman case ICmpInst::ICMP_NE: return ISD::SETNE; 191450aa64fSDan Gohman case ICmpInst::ICMP_SLE: return ISD::SETLE; 192450aa64fSDan Gohman case ICmpInst::ICMP_ULE: return ISD::SETULE; 193450aa64fSDan Gohman case ICmpInst::ICMP_SGE: return ISD::SETGE; 194450aa64fSDan Gohman case ICmpInst::ICMP_UGE: return ISD::SETUGE; 195450aa64fSDan Gohman case ICmpInst::ICMP_SLT: return ISD::SETLT; 196450aa64fSDan Gohman case ICmpInst::ICMP_ULT: return ISD::SETULT; 197450aa64fSDan Gohman case ICmpInst::ICMP_SGT: return ISD::SETGT; 198450aa64fSDan Gohman case ICmpInst::ICMP_UGT: return ISD::SETUGT; 199450aa64fSDan Gohman default: 200450aa64fSDan Gohman llvm_unreachable("Invalid ICmp predicate opcode!"); 201450aa64fSDan Gohman } 202450aa64fSDan Gohman } 203450aa64fSDan Gohman 204ffc44549SStephen Lin static bool isNoopBitcast(Type *T1, Type *T2, 205ffc44549SStephen Lin const TargetLowering& TLI) { 206ffc44549SStephen Lin return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) || 207ffc44549SStephen Lin (isa<VectorType>(T1) && isa<VectorType>(T2) && 208ffc44549SStephen Lin TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2))); 209ffc44549SStephen Lin } 2104f3615deSChris Lattner 211ffc44549SStephen Lin /// sameNoopInput - Return true if V1 == V2, else if either V1 or V2 is a noop 212ffc44549SStephen Lin /// (i.e., lowers to no machine code), look through it (and any transitive noop 213ffc44549SStephen Lin /// operands to it) and check if it has the same noop input value. This is 214ffc44549SStephen Lin /// used to determine if a tail call can be formed. 215ffc44549SStephen Lin static bool sameNoopInput(const Value *V1, const Value *V2, 216ffc44549SStephen Lin SmallVectorImpl<unsigned> &Els1, 217ffc44549SStephen Lin SmallVectorImpl<unsigned> &Els2, 218ffc44549SStephen Lin const TargetLowering &TLI) { 219ffc44549SStephen Lin using std::swap; 220ffc44549SStephen Lin bool swapParity = false; 221ffc44549SStephen Lin bool equalEls = Els1 == Els2; 222ffc44549SStephen Lin while (true) { 223ffc44549SStephen Lin if ((equalEls && V1 == V2) || isa<UndefValue>(V1) || isa<UndefValue>(V2)) { 224ffc44549SStephen Lin if (swapParity) 225ffc44549SStephen Lin // Revert to original Els1 and Els2 to avoid confusing recursive calls 226ffc44549SStephen Lin swap(Els1, Els2); 227ffc44549SStephen Lin return true; 228ffc44549SStephen Lin } 229182fe3eeSChris Lattner 230ffc44549SStephen Lin // Try to look through V1; if V1 is not an instruction, it can't be looked 231ffc44549SStephen Lin // through. 232ffc44549SStephen Lin const Instruction *I = dyn_cast<Instruction>(V1); 233ffc44549SStephen Lin const Value *NoopInput = 0; 234ffc44549SStephen Lin if (I != 0 && I->getNumOperands() > 0) { 235182fe3eeSChris Lattner Value *Op = I->getOperand(0); 236ffc44549SStephen Lin if (isa<TruncInst>(I)) { 2374f3615deSChris Lattner // Look through truly no-op truncates. 238ffc44549SStephen Lin if (TLI.isTruncateFree(Op->getType(), I->getType())) 239ffc44549SStephen Lin NoopInput = Op; 240ffc44549SStephen Lin } else if (isa<BitCastInst>(I)) { 2414f3615deSChris Lattner // Look through truly no-op bitcasts. 242ffc44549SStephen Lin if (isNoopBitcast(Op->getType(), I->getType(), TLI)) 243ffc44549SStephen Lin NoopInput = Op; 244ffc44549SStephen Lin } else if (isa<GetElementPtrInst>(I)) { 245ffc44549SStephen Lin // Look through getelementptr 246ffc44549SStephen Lin if (cast<GetElementPtrInst>(I)->hasAllZeroIndices()) 247ffc44549SStephen Lin NoopInput = Op; 248ffc44549SStephen Lin } else if (isa<IntToPtrInst>(I)) { 249182fe3eeSChris Lattner // Look through inttoptr. 250ffc44549SStephen Lin // Make sure this isn't a truncating or extending cast. We could 251ffc44549SStephen Lin // support this eventually, but don't bother for now. 252ffc44549SStephen Lin if (!isa<VectorType>(I->getType()) && 253ffc44549SStephen Lin TLI.getPointerTy().getSizeInBits() == 254182fe3eeSChris Lattner cast<IntegerType>(Op->getType())->getBitWidth()) 255ffc44549SStephen Lin NoopInput = Op; 256ffc44549SStephen Lin } else if (isa<PtrToIntInst>(I)) { 257182fe3eeSChris Lattner // Look through ptrtoint. 258ffc44549SStephen Lin // Make sure this isn't a truncating or extending cast. We could 259ffc44549SStephen Lin // support this eventually, but don't bother for now. 260ffc44549SStephen Lin if (!isa<VectorType>(I->getType()) && 261ffc44549SStephen Lin TLI.getPointerTy().getSizeInBits() == 262182fe3eeSChris Lattner cast<IntegerType>(I->getType())->getBitWidth()) 263ffc44549SStephen Lin NoopInput = Op; 264*b8bd232aSStephen Lin } else if (isa<CallInst>(I)) { 265*b8bd232aSStephen Lin // Look through call 266*b8bd232aSStephen Lin for (User::const_op_iterator i = I->op_begin(), 267*b8bd232aSStephen Lin // Skip Callee 268*b8bd232aSStephen Lin e = I->op_end() - 1; 269*b8bd232aSStephen Lin i != e; ++i) { 270*b8bd232aSStephen Lin unsigned attrInd = i - I->op_begin() + 1; 271*b8bd232aSStephen Lin if (cast<CallInst>(I)->paramHasAttr(attrInd, Attribute::Returned) && 272*b8bd232aSStephen Lin isNoopBitcast((*i)->getType(), I->getType(), TLI)) { 273*b8bd232aSStephen Lin NoopInput = *i; 274*b8bd232aSStephen Lin break; 275*b8bd232aSStephen Lin } 276*b8bd232aSStephen Lin } 277*b8bd232aSStephen Lin } else if (isa<InvokeInst>(I)) { 278*b8bd232aSStephen Lin // Look through invoke 279*b8bd232aSStephen Lin for (User::const_op_iterator i = I->op_begin(), 280*b8bd232aSStephen Lin // Skip BB, BB, Callee 281*b8bd232aSStephen Lin e = I->op_end() - 3; 282*b8bd232aSStephen Lin i != e; ++i) { 283*b8bd232aSStephen Lin unsigned attrInd = i - I->op_begin() + 1; 284*b8bd232aSStephen Lin if (cast<InvokeInst>(I)->paramHasAttr(attrInd, Attribute::Returned) && 285*b8bd232aSStephen Lin isNoopBitcast((*i)->getType(), I->getType(), TLI)) { 286*b8bd232aSStephen Lin NoopInput = *i; 287*b8bd232aSStephen Lin break; 288*b8bd232aSStephen Lin } 289*b8bd232aSStephen Lin } 290ffc44549SStephen Lin } 291182fe3eeSChris Lattner } 292182fe3eeSChris Lattner 293ffc44549SStephen Lin if (NoopInput) { 294ffc44549SStephen Lin V1 = NoopInput; 295ffc44549SStephen Lin continue; 2964f3615deSChris Lattner } 2974f3615deSChris Lattner 298ffc44549SStephen Lin // If we already swapped, avoid infinite loop 299ffc44549SStephen Lin if (swapParity) 300ffc44549SStephen Lin break; 301ffc44549SStephen Lin 302ffc44549SStephen Lin // Otherwise, swap V1<->V2, Els1<->Els2 303ffc44549SStephen Lin swap(V1, V2); 304ffc44549SStephen Lin swap(Els1, Els2); 305ffc44549SStephen Lin swapParity = !swapParity; 306ffc44549SStephen Lin } 307ffc44549SStephen Lin 308ffc44549SStephen Lin for (unsigned n = 0; n < 2; ++n) { 309ffc44549SStephen Lin if (isa<InsertValueInst>(V1)) { 310ffc44549SStephen Lin if (isa<StructType>(V1->getType())) { 311ffc44549SStephen Lin // Look through insertvalue 312ffc44549SStephen Lin unsigned i, e; 313ffc44549SStephen Lin for (i = 0, e = cast<StructType>(V1->getType())->getNumElements(); 314ffc44549SStephen Lin i != e; ++i) { 315ffc44549SStephen Lin const Value *InScalar = FindInsertedValue(const_cast<Value*>(V1), i); 316ffc44549SStephen Lin if (InScalar == 0) 317ffc44549SStephen Lin break; 318ffc44549SStephen Lin Els1.push_back(i); 319ffc44549SStephen Lin if (!sameNoopInput(InScalar, V2, Els1, Els2, TLI)) { 320ffc44549SStephen Lin Els1.pop_back(); 321ffc44549SStephen Lin break; 322ffc44549SStephen Lin } 323ffc44549SStephen Lin Els1.pop_back(); 324ffc44549SStephen Lin } 325ffc44549SStephen Lin if (i == e) { 326ffc44549SStephen Lin if (swapParity) 327ffc44549SStephen Lin swap(Els1, Els2); 328ffc44549SStephen Lin return true; 329ffc44549SStephen Lin } 330ffc44549SStephen Lin } 331ffc44549SStephen Lin } else if (!Els1.empty() && isa<ExtractValueInst>(V1)) { 332ffc44549SStephen Lin const ExtractValueInst *EVI = cast<ExtractValueInst>(V1); 333ffc44549SStephen Lin unsigned i = Els1.back(); 334ffc44549SStephen Lin // If the scalar value being inserted is an extractvalue of the right 335ffc44549SStephen Lin // index from the call, then everything is good. 336ffc44549SStephen Lin if (isa<StructType>(EVI->getOperand(0)->getType()) && 337ffc44549SStephen Lin EVI->getNumIndices() == 1 && EVI->getIndices()[0] == i) { 338ffc44549SStephen Lin // Look through extractvalue 339ffc44549SStephen Lin Els1.pop_back(); 340ffc44549SStephen Lin if (sameNoopInput(EVI->getOperand(0), V2, Els1, Els2, TLI)) { 341ffc44549SStephen Lin Els1.push_back(i); 342ffc44549SStephen Lin if (swapParity) 343ffc44549SStephen Lin swap(Els1, Els2); 344ffc44549SStephen Lin return true; 345ffc44549SStephen Lin } 346ffc44549SStephen Lin Els1.push_back(i); 347ffc44549SStephen Lin } 348ffc44549SStephen Lin } 349ffc44549SStephen Lin 350ffc44549SStephen Lin swap(V1, V2); 351ffc44549SStephen Lin swap(Els1, Els2); 352ffc44549SStephen Lin swapParity = !swapParity; 353ffc44549SStephen Lin } 354ffc44549SStephen Lin 355ffc44549SStephen Lin if (swapParity) 356ffc44549SStephen Lin swap(Els1, Els2); 357ffc44549SStephen Lin return false; 358ffc44549SStephen Lin } 3594f3615deSChris Lattner 360450aa64fSDan Gohman /// Test if the given instruction is in a position to be optimized 361450aa64fSDan Gohman /// with a tail-call. This roughly means that it's in a block with 362450aa64fSDan Gohman /// a return and there's nothing that needs to be scheduled 363450aa64fSDan Gohman /// between it and the return. 364450aa64fSDan Gohman /// 365450aa64fSDan Gohman /// This function only tests target-independent requirements. 366ffc44549SStephen Lin bool llvm::isInTailCallPosition(ImmutableCallSite CS, 367ffc44549SStephen Lin const TargetLowering &TLI) { 368450aa64fSDan Gohman const Instruction *I = CS.getInstruction(); 369450aa64fSDan Gohman const BasicBlock *ExitBB = I->getParent(); 370450aa64fSDan Gohman const TerminatorInst *Term = ExitBB->getTerminator(); 371450aa64fSDan Gohman const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); 372450aa64fSDan Gohman 373450aa64fSDan Gohman // The block must end in a return statement or unreachable. 374450aa64fSDan Gohman // 375450aa64fSDan Gohman // FIXME: Decline tailcall if it's not guaranteed and if the block ends in 376450aa64fSDan Gohman // an unreachable, for now. The way tailcall optimization is currently 377450aa64fSDan Gohman // implemented means it will add an epilogue followed by a jump. That is 378450aa64fSDan Gohman // not profitable. Also, if the callee is a special function (e.g. 379450aa64fSDan Gohman // longjmp on x86), it can end up causing miscompilation that has not 380450aa64fSDan Gohman // been fully understood. 381450aa64fSDan Gohman if (!Ret && 38250f02cb2SNick Lewycky (!TLI.getTargetMachine().Options.GuaranteedTailCallOpt || 3834f3615deSChris Lattner !isa<UnreachableInst>(Term))) 3844f3615deSChris Lattner return false; 385450aa64fSDan Gohman 386450aa64fSDan Gohman // If I will have a chain, make sure no other instruction that will have a 387450aa64fSDan Gohman // chain interposes between I and the return. 388450aa64fSDan Gohman if (I->mayHaveSideEffects() || I->mayReadFromMemory() || 38975d7d5e9SDan Gohman !isSafeToSpeculativelyExecute(I)) 390450aa64fSDan Gohman for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; 391450aa64fSDan Gohman --BBI) { 392450aa64fSDan Gohman if (&*BBI == I) 393450aa64fSDan Gohman break; 394450aa64fSDan Gohman // Debug info intrinsics do not get in the way of tail call optimization. 395450aa64fSDan Gohman if (isa<DbgInfoIntrinsic>(BBI)) 396450aa64fSDan Gohman continue; 397450aa64fSDan Gohman if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || 39875d7d5e9SDan Gohman !isSafeToSpeculativelyExecute(BBI)) 399450aa64fSDan Gohman return false; 400450aa64fSDan Gohman } 401450aa64fSDan Gohman 402450aa64fSDan Gohman // If the block ends with a void return or unreachable, it doesn't matter 403450aa64fSDan Gohman // what the call's return type is. 404450aa64fSDan Gohman if (!Ret || Ret->getNumOperands() == 0) return true; 405450aa64fSDan Gohman 406450aa64fSDan Gohman // If the return value is undef, it doesn't matter what the call's 407450aa64fSDan Gohman // return type is. 408450aa64fSDan Gohman if (isa<UndefValue>(Ret->getOperand(0))) return true; 409450aa64fSDan Gohman 410450aa64fSDan Gohman // Conservatively require the attributes of the call to match those of 411450aa64fSDan Gohman // the return. Ignore noalias because it doesn't affect the call sequence. 412b1f3b498SEvan Cheng const Function *F = ExitBB->getParent(); 4134f972ea2SBill Wendling AttributeSet CallerAttrs = F->getAttributes(); 4144f972ea2SBill Wendling if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex). 4154f972ea2SBill Wendling removeAttribute(Attribute::NoAlias) != 4164f972ea2SBill Wendling AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex). 4174f972ea2SBill Wendling removeAttribute(Attribute::NoAlias)) 418450aa64fSDan Gohman return false; 419450aa64fSDan Gohman 420450aa64fSDan Gohman // It's not safe to eliminate the sign / zero extension of the return value. 4214f972ea2SBill Wendling if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || 4224f972ea2SBill Wendling CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) 423450aa64fSDan Gohman return false; 424450aa64fSDan Gohman 425ffc44549SStephen Lin // Otherwise, make sure the return value and I have the same value 426ffc44549SStephen Lin SmallVector<unsigned, 4> Els1, Els2; 427ffc44549SStephen Lin return sameNoopInput(Ret->getOperand(0), I, Els1, Els2, TLI); 428450aa64fSDan Gohman } 429