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" 27ed0881b2SChandler Carruth #include "llvm/Target/TargetOptions.h" 28450aa64fSDan Gohman using namespace llvm; 29450aa64fSDan Gohman 30450aa64fSDan Gohman /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence 31450aa64fSDan Gohman /// of insertvalue or extractvalue indices that identify a member, return 32450aa64fSDan Gohman /// the linearized index of the start of the member. 33450aa64fSDan Gohman /// 34229907cdSChris Lattner unsigned llvm::ComputeLinearIndex(Type *Ty, 35450aa64fSDan Gohman const unsigned *Indices, 36450aa64fSDan Gohman const unsigned *IndicesEnd, 37450aa64fSDan Gohman unsigned CurIndex) { 38450aa64fSDan Gohman // Base case: We're done. 39450aa64fSDan Gohman if (Indices && Indices == IndicesEnd) 40450aa64fSDan Gohman return CurIndex; 41450aa64fSDan Gohman 42450aa64fSDan Gohman // Given a struct type, recursively traverse the elements. 43229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 44450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(), 45450aa64fSDan Gohman EI = EB, 46450aa64fSDan Gohman EE = STy->element_end(); 47450aa64fSDan Gohman EI != EE; ++EI) { 48450aa64fSDan Gohman if (Indices && *Indices == unsigned(EI - EB)) 49aadc5596SDan Gohman return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex); 50aadc5596SDan Gohman CurIndex = ComputeLinearIndex(*EI, 0, 0, CurIndex); 51450aa64fSDan Gohman } 52450aa64fSDan Gohman return CurIndex; 53450aa64fSDan Gohman } 54450aa64fSDan Gohman // Given an array type, recursively traverse the elements. 55229907cdSChris Lattner else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 56229907cdSChris Lattner Type *EltTy = ATy->getElementType(); 57450aa64fSDan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { 58450aa64fSDan Gohman if (Indices && *Indices == i) 59aadc5596SDan Gohman return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex); 60aadc5596SDan Gohman CurIndex = ComputeLinearIndex(EltTy, 0, 0, CurIndex); 61450aa64fSDan Gohman } 62450aa64fSDan Gohman return CurIndex; 63450aa64fSDan Gohman } 64450aa64fSDan Gohman // We haven't found the type we're looking for, so keep searching. 65450aa64fSDan Gohman return CurIndex + 1; 66450aa64fSDan Gohman } 67450aa64fSDan Gohman 68450aa64fSDan Gohman /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of 69450aa64fSDan Gohman /// EVTs that represent all the individual underlying 70450aa64fSDan Gohman /// non-aggregate types that comprise it. 71450aa64fSDan Gohman /// 72450aa64fSDan Gohman /// If Offsets is non-null, it points to a vector to be filled in 73450aa64fSDan Gohman /// with the in-memory offsets of each of the individual values. 74450aa64fSDan Gohman /// 75229907cdSChris Lattner void llvm::ComputeValueVTs(const TargetLowering &TLI, Type *Ty, 76450aa64fSDan Gohman SmallVectorImpl<EVT> &ValueVTs, 77450aa64fSDan Gohman SmallVectorImpl<uint64_t> *Offsets, 78450aa64fSDan Gohman uint64_t StartingOffset) { 79450aa64fSDan Gohman // Given a struct type, recursively traverse the elements. 80229907cdSChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 81cdfe20b9SMicah Villmow const StructLayout *SL = TLI.getDataLayout()->getStructLayout(STy); 82450aa64fSDan Gohman for (StructType::element_iterator EB = STy->element_begin(), 83450aa64fSDan Gohman EI = EB, 84450aa64fSDan Gohman EE = STy->element_end(); 85450aa64fSDan Gohman EI != EE; ++EI) 86450aa64fSDan Gohman ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, 87450aa64fSDan Gohman StartingOffset + SL->getElementOffset(EI - EB)); 88450aa64fSDan Gohman return; 89450aa64fSDan Gohman } 90450aa64fSDan Gohman // Given an array type, recursively traverse the elements. 91229907cdSChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 92229907cdSChris Lattner Type *EltTy = ATy->getElementType(); 93cdfe20b9SMicah Villmow uint64_t EltSize = TLI.getDataLayout()->getTypeAllocSize(EltTy); 94450aa64fSDan Gohman for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) 95450aa64fSDan Gohman ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, 96450aa64fSDan Gohman StartingOffset + i * EltSize); 97450aa64fSDan Gohman return; 98450aa64fSDan Gohman } 99450aa64fSDan Gohman // Interpret void as zero return values. 100450aa64fSDan Gohman if (Ty->isVoidTy()) 101450aa64fSDan Gohman return; 102450aa64fSDan Gohman // Base case: we can get an EVT for this LLVM IR type. 103450aa64fSDan Gohman ValueVTs.push_back(TLI.getValueType(Ty)); 104450aa64fSDan Gohman if (Offsets) 105450aa64fSDan Gohman Offsets->push_back(StartingOffset); 106450aa64fSDan Gohman } 107450aa64fSDan Gohman 108450aa64fSDan Gohman /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. 109450aa64fSDan Gohman GlobalVariable *llvm::ExtractTypeInfo(Value *V) { 110450aa64fSDan Gohman V = V->stripPointerCasts(); 111450aa64fSDan Gohman GlobalVariable *GV = dyn_cast<GlobalVariable>(V); 112450aa64fSDan Gohman 113fa60b0eeSBill Wendling if (GV && GV->getName() == "llvm.eh.catch.all.value") { 114450aa64fSDan Gohman assert(GV->hasInitializer() && 115450aa64fSDan Gohman "The EH catch-all value must have an initializer"); 116450aa64fSDan Gohman Value *Init = GV->getInitializer(); 117450aa64fSDan Gohman GV = dyn_cast<GlobalVariable>(Init); 118450aa64fSDan Gohman if (!GV) V = cast<ConstantPointerNull>(Init); 119450aa64fSDan Gohman } 120450aa64fSDan Gohman 121450aa64fSDan Gohman assert((GV || isa<ConstantPointerNull>(V)) && 122450aa64fSDan Gohman "TypeInfo must be a global variable or NULL"); 123450aa64fSDan Gohman return GV; 124450aa64fSDan Gohman } 125450aa64fSDan Gohman 126450aa64fSDan Gohman /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being 127450aa64fSDan Gohman /// processed uses a memory 'm' constraint. 128450aa64fSDan Gohman bool 129e8360b71SJohn Thompson llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos, 130450aa64fSDan Gohman const TargetLowering &TLI) { 131450aa64fSDan Gohman for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { 132450aa64fSDan Gohman InlineAsm::ConstraintInfo &CI = CInfos[i]; 133450aa64fSDan Gohman for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { 134450aa64fSDan Gohman TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); 135450aa64fSDan Gohman if (CType == TargetLowering::C_Memory) 136450aa64fSDan Gohman return true; 137450aa64fSDan Gohman } 138450aa64fSDan Gohman 139450aa64fSDan Gohman // Indirect operand accesses access memory. 140450aa64fSDan Gohman if (CI.isIndirect) 141450aa64fSDan Gohman return true; 142450aa64fSDan Gohman } 143450aa64fSDan Gohman 144450aa64fSDan Gohman return false; 145450aa64fSDan Gohman } 146450aa64fSDan Gohman 147450aa64fSDan Gohman /// getFCmpCondCode - Return the ISD condition code corresponding to 148450aa64fSDan Gohman /// the given LLVM IR floating-point condition code. This includes 149450aa64fSDan Gohman /// consideration of global floating-point math flags. 150450aa64fSDan Gohman /// 151450aa64fSDan Gohman ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) { 152450aa64fSDan Gohman switch (Pred) { 15350f02cb2SNick Lewycky case FCmpInst::FCMP_FALSE: return ISD::SETFALSE; 15450f02cb2SNick Lewycky case FCmpInst::FCMP_OEQ: return ISD::SETOEQ; 15550f02cb2SNick Lewycky case FCmpInst::FCMP_OGT: return ISD::SETOGT; 15650f02cb2SNick Lewycky case FCmpInst::FCMP_OGE: return ISD::SETOGE; 15750f02cb2SNick Lewycky case FCmpInst::FCMP_OLT: return ISD::SETOLT; 15850f02cb2SNick Lewycky case FCmpInst::FCMP_OLE: return ISD::SETOLE; 15950f02cb2SNick Lewycky case FCmpInst::FCMP_ONE: return ISD::SETONE; 16050f02cb2SNick Lewycky case FCmpInst::FCMP_ORD: return ISD::SETO; 16150f02cb2SNick Lewycky case FCmpInst::FCMP_UNO: return ISD::SETUO; 16250f02cb2SNick Lewycky case FCmpInst::FCMP_UEQ: return ISD::SETUEQ; 16350f02cb2SNick Lewycky case FCmpInst::FCMP_UGT: return ISD::SETUGT; 16450f02cb2SNick Lewycky case FCmpInst::FCMP_UGE: return ISD::SETUGE; 16550f02cb2SNick Lewycky case FCmpInst::FCMP_ULT: return ISD::SETULT; 16650f02cb2SNick Lewycky case FCmpInst::FCMP_ULE: return ISD::SETULE; 16750f02cb2SNick Lewycky case FCmpInst::FCMP_UNE: return ISD::SETUNE; 16850f02cb2SNick Lewycky case FCmpInst::FCMP_TRUE: return ISD::SETTRUE; 16946a9f016SDavid Blaikie default: llvm_unreachable("Invalid FCmp predicate opcode!"); 170450aa64fSDan Gohman } 17150f02cb2SNick Lewycky } 17250f02cb2SNick Lewycky 17350f02cb2SNick Lewycky ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) { 17450f02cb2SNick Lewycky switch (CC) { 17550f02cb2SNick Lewycky case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ; 17650f02cb2SNick Lewycky case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE; 17750f02cb2SNick Lewycky case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT; 17850f02cb2SNick Lewycky case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE; 17950f02cb2SNick Lewycky case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT; 18050f02cb2SNick Lewycky case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE; 18146a9f016SDavid Blaikie default: return CC; 18250f02cb2SNick Lewycky } 183450aa64fSDan Gohman } 184450aa64fSDan Gohman 185450aa64fSDan Gohman /// getICmpCondCode - Return the ISD condition code corresponding to 186450aa64fSDan Gohman /// the given LLVM IR integer condition code. 187450aa64fSDan Gohman /// 188450aa64fSDan Gohman ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) { 189450aa64fSDan Gohman switch (Pred) { 190450aa64fSDan Gohman case ICmpInst::ICMP_EQ: return ISD::SETEQ; 191450aa64fSDan Gohman case ICmpInst::ICMP_NE: return ISD::SETNE; 192450aa64fSDan Gohman case ICmpInst::ICMP_SLE: return ISD::SETLE; 193450aa64fSDan Gohman case ICmpInst::ICMP_ULE: return ISD::SETULE; 194450aa64fSDan Gohman case ICmpInst::ICMP_SGE: return ISD::SETGE; 195450aa64fSDan Gohman case ICmpInst::ICMP_UGE: return ISD::SETUGE; 196450aa64fSDan Gohman case ICmpInst::ICMP_SLT: return ISD::SETLT; 197450aa64fSDan Gohman case ICmpInst::ICMP_ULT: return ISD::SETULT; 198450aa64fSDan Gohman case ICmpInst::ICMP_SGT: return ISD::SETGT; 199450aa64fSDan Gohman case ICmpInst::ICMP_UGT: return ISD::SETUGT; 200450aa64fSDan Gohman default: 201450aa64fSDan Gohman llvm_unreachable("Invalid ICmp predicate opcode!"); 202450aa64fSDan Gohman } 203450aa64fSDan Gohman } 204450aa64fSDan Gohman 2054f3615deSChris Lattner 2064f3615deSChris Lattner /// getNoopInput - If V is a noop (i.e., lowers to no machine code), look 2074f3615deSChris Lattner /// through it (and any transitive noop operands to it) and return its input 2084f3615deSChris Lattner /// value. This is used to determine if a tail call can be formed. 2094f3615deSChris Lattner /// 2104f3615deSChris Lattner static const Value *getNoopInput(const Value *V, const TargetLowering &TLI) { 2114f3615deSChris Lattner // If V is not an instruction, it can't be looked through. 212182fe3eeSChris Lattner const Instruction *I = dyn_cast<Instruction>(V); 213182fe3eeSChris Lattner if (I == 0 || !I->hasOneUse() || I->getNumOperands() == 0) return V; 214182fe3eeSChris Lattner 215182fe3eeSChris Lattner Value *Op = I->getOperand(0); 2164f3615deSChris Lattner 2174f3615deSChris Lattner // Look through truly no-op truncates. 218182fe3eeSChris Lattner if (isa<TruncInst>(I) && 219182fe3eeSChris Lattner TLI.isTruncateFree(I->getOperand(0)->getType(), I->getType())) 220182fe3eeSChris Lattner return getNoopInput(I->getOperand(0), TLI); 2214f3615deSChris Lattner 2224f3615deSChris Lattner // Look through truly no-op bitcasts. 223182fe3eeSChris Lattner if (isa<BitCastInst>(I)) { 224182fe3eeSChris Lattner // No type change at all. 225182fe3eeSChris Lattner if (Op->getType() == I->getType()) 226182fe3eeSChris Lattner return getNoopInput(Op, TLI); 227182fe3eeSChris Lattner 2284f3615deSChris Lattner // Pointer to pointer cast. 229182fe3eeSChris Lattner if (Op->getType()->isPointerTy() && I->getType()->isPointerTy()) 230182fe3eeSChris Lattner return getNoopInput(Op, TLI); 231182fe3eeSChris Lattner 232182fe3eeSChris Lattner if (isa<VectorType>(Op->getType()) && isa<VectorType>(I->getType()) && 233182fe3eeSChris Lattner TLI.isTypeLegal(EVT::getEVT(Op->getType())) && 234182fe3eeSChris Lattner TLI.isTypeLegal(EVT::getEVT(I->getType()))) 2354f3615deSChris Lattner return getNoopInput(Op, TLI); 2364f3615deSChris Lattner } 2374f3615deSChris Lattner 238182fe3eeSChris Lattner // Look through inttoptr. 239182fe3eeSChris Lattner if (isa<IntToPtrInst>(I) && !isa<VectorType>(I->getType())) { 240182fe3eeSChris Lattner // Make sure this isn't a truncating or extending cast. We could support 241182fe3eeSChris Lattner // this eventually, but don't bother for now. 242182fe3eeSChris Lattner if (TLI.getPointerTy().getSizeInBits() == 243182fe3eeSChris Lattner cast<IntegerType>(Op->getType())->getBitWidth()) 244182fe3eeSChris Lattner return getNoopInput(Op, TLI); 245182fe3eeSChris Lattner } 246182fe3eeSChris Lattner 247182fe3eeSChris Lattner // Look through ptrtoint. 248182fe3eeSChris Lattner if (isa<PtrToIntInst>(I) && !isa<VectorType>(I->getType())) { 249182fe3eeSChris Lattner // Make sure this isn't a truncating or extending cast. We could support 250182fe3eeSChris Lattner // this eventually, but don't bother for now. 251182fe3eeSChris Lattner if (TLI.getPointerTy().getSizeInBits() == 252182fe3eeSChris Lattner cast<IntegerType>(I->getType())->getBitWidth()) 253182fe3eeSChris Lattner return getNoopInput(Op, TLI); 254182fe3eeSChris Lattner } 255182fe3eeSChris Lattner 256182fe3eeSChris Lattner 2574f3615deSChris Lattner // Otherwise it's not something we can look through. 2584f3615deSChris Lattner return V; 2594f3615deSChris Lattner } 2604f3615deSChris Lattner 2614f3615deSChris Lattner 262450aa64fSDan Gohman /// Test if the given instruction is in a position to be optimized 263450aa64fSDan Gohman /// with a tail-call. This roughly means that it's in a block with 264450aa64fSDan Gohman /// a return and there's nothing that needs to be scheduled 265450aa64fSDan Gohman /// between it and the return. 266450aa64fSDan Gohman /// 267450aa64fSDan Gohman /// This function only tests target-independent requirements. 268*4f972ea2SBill Wendling bool llvm::isInTailCallPosition(ImmutableCallSite CS,const TargetLowering &TLI){ 269450aa64fSDan Gohman const Instruction *I = CS.getInstruction(); 270450aa64fSDan Gohman const BasicBlock *ExitBB = I->getParent(); 271450aa64fSDan Gohman const TerminatorInst *Term = ExitBB->getTerminator(); 272450aa64fSDan Gohman const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); 273450aa64fSDan Gohman 274450aa64fSDan Gohman // The block must end in a return statement or unreachable. 275450aa64fSDan Gohman // 276450aa64fSDan Gohman // FIXME: Decline tailcall if it's not guaranteed and if the block ends in 277450aa64fSDan Gohman // an unreachable, for now. The way tailcall optimization is currently 278450aa64fSDan Gohman // implemented means it will add an epilogue followed by a jump. That is 279450aa64fSDan Gohman // not profitable. Also, if the callee is a special function (e.g. 280450aa64fSDan Gohman // longjmp on x86), it can end up causing miscompilation that has not 281450aa64fSDan Gohman // been fully understood. 282450aa64fSDan Gohman if (!Ret && 28350f02cb2SNick Lewycky (!TLI.getTargetMachine().Options.GuaranteedTailCallOpt || 2844f3615deSChris Lattner !isa<UnreachableInst>(Term))) 2854f3615deSChris Lattner return false; 286450aa64fSDan Gohman 287450aa64fSDan Gohman // If I will have a chain, make sure no other instruction that will have a 288450aa64fSDan Gohman // chain interposes between I and the return. 289450aa64fSDan Gohman if (I->mayHaveSideEffects() || I->mayReadFromMemory() || 29075d7d5e9SDan Gohman !isSafeToSpeculativelyExecute(I)) 291450aa64fSDan Gohman for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; 292450aa64fSDan Gohman --BBI) { 293450aa64fSDan Gohman if (&*BBI == I) 294450aa64fSDan Gohman break; 295450aa64fSDan Gohman // Debug info intrinsics do not get in the way of tail call optimization. 296450aa64fSDan Gohman if (isa<DbgInfoIntrinsic>(BBI)) 297450aa64fSDan Gohman continue; 298450aa64fSDan Gohman if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || 29975d7d5e9SDan Gohman !isSafeToSpeculativelyExecute(BBI)) 300450aa64fSDan Gohman return false; 301450aa64fSDan Gohman } 302450aa64fSDan Gohman 303450aa64fSDan Gohman // If the block ends with a void return or unreachable, it doesn't matter 304450aa64fSDan Gohman // what the call's return type is. 305450aa64fSDan Gohman if (!Ret || Ret->getNumOperands() == 0) return true; 306450aa64fSDan Gohman 307450aa64fSDan Gohman // If the return value is undef, it doesn't matter what the call's 308450aa64fSDan Gohman // return type is. 309450aa64fSDan Gohman if (isa<UndefValue>(Ret->getOperand(0))) return true; 310450aa64fSDan Gohman 311450aa64fSDan Gohman // Conservatively require the attributes of the call to match those of 312450aa64fSDan Gohman // the return. Ignore noalias because it doesn't affect the call sequence. 313b1f3b498SEvan Cheng const Function *F = ExitBB->getParent(); 314*4f972ea2SBill Wendling AttributeSet CallerAttrs = F->getAttributes(); 315*4f972ea2SBill Wendling if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex). 316*4f972ea2SBill Wendling removeAttribute(Attribute::NoAlias) != 317*4f972ea2SBill Wendling AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex). 318*4f972ea2SBill Wendling removeAttribute(Attribute::NoAlias)) 319450aa64fSDan Gohman return false; 320450aa64fSDan Gohman 321450aa64fSDan Gohman // It's not safe to eliminate the sign / zero extension of the return value. 322*4f972ea2SBill Wendling if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || 323*4f972ea2SBill Wendling CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) 324450aa64fSDan Gohman return false; 325450aa64fSDan Gohman 326450aa64fSDan Gohman // Otherwise, make sure the unmodified return value of I is the return value. 327466076b9SChris Lattner // We handle two cases: multiple return values + scalars. 328466076b9SChris Lattner Value *RetVal = Ret->getOperand(0); 329466076b9SChris Lattner if (!isa<InsertValueInst>(RetVal) || !isa<StructType>(RetVal->getType())) 330466076b9SChris Lattner // Handle scalars first. 3314f3615deSChris Lattner return getNoopInput(Ret->getOperand(0), TLI) == I; 332466076b9SChris Lattner 333466076b9SChris Lattner // If this is an aggregate return, look through the insert/extract values and 334466076b9SChris Lattner // see if each is transparent. 335466076b9SChris Lattner for (unsigned i = 0, e =cast<StructType>(RetVal->getType())->getNumElements(); 336466076b9SChris Lattner i != e; ++i) { 337cc84e6d2SChris Lattner const Value *InScalar = FindInsertedValue(RetVal, i); 338cc84e6d2SChris Lattner if (InScalar == 0) return false; 339cc84e6d2SChris Lattner InScalar = getNoopInput(InScalar, TLI); 340466076b9SChris Lattner 341466076b9SChris Lattner // If the scalar value being inserted is an extractvalue of the right index 342466076b9SChris Lattner // from the call, then everything is good. 343466076b9SChris Lattner const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(InScalar); 344466076b9SChris Lattner if (EVI == 0 || EVI->getOperand(0) != I || EVI->getNumIndices() != 1 || 345466076b9SChris Lattner EVI->getIndices()[0] != i) 346466076b9SChris Lattner return false; 347466076b9SChris Lattner } 348466076b9SChris Lattner 349466076b9SChris Lattner return true; 350450aa64fSDan Gohman } 351