Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1 | //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===// |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines several CodeGen-specific LLVM IR analysis utilties. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/Analysis.h" |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
| 18 | #include "llvm/IR/DerivedTypes.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/Instructions.h" |
| 21 | #include "llvm/IR/IntrinsicInst.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/Module.h" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetLowering.h" |
| 27 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
| 30 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
| 31 | /// of insertvalue or extractvalue indices that identify a member, return |
| 32 | /// the linearized index of the start of the member. |
| 33 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 34 | unsigned llvm::ComputeLinearIndex(Type *Ty, |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 35 | const unsigned *Indices, |
| 36 | const unsigned *IndicesEnd, |
| 37 | unsigned CurIndex) { |
| 38 | // Base case: We're done. |
| 39 | if (Indices && Indices == IndicesEnd) |
| 40 | return CurIndex; |
| 41 | |
| 42 | // Given a struct type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 43 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 44 | for (StructType::element_iterator EB = STy->element_begin(), |
| 45 | EI = EB, |
| 46 | EE = STy->element_end(); |
| 47 | EI != EE; ++EI) { |
| 48 | if (Indices && *Indices == unsigned(EI - EB)) |
Dan Gohman | 0dadb15 | 2010-10-06 16:18:29 +0000 | [diff] [blame] | 49 | return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex); |
| 50 | CurIndex = ComputeLinearIndex(*EI, 0, 0, CurIndex); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 51 | } |
| 52 | return CurIndex; |
| 53 | } |
| 54 | // Given an array type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 55 | else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 56 | Type *EltTy = ATy->getElementType(); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 57 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 58 | if (Indices && *Indices == i) |
Dan Gohman | 0dadb15 | 2010-10-06 16:18:29 +0000 | [diff] [blame] | 59 | return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex); |
| 60 | CurIndex = ComputeLinearIndex(EltTy, 0, 0, CurIndex); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 61 | } |
| 62 | return CurIndex; |
| 63 | } |
| 64 | // We haven't found the type we're looking for, so keep searching. |
| 65 | return CurIndex + 1; |
| 66 | } |
| 67 | |
| 68 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
| 69 | /// EVTs that represent all the individual underlying |
| 70 | /// non-aggregate types that comprise it. |
| 71 | /// |
| 72 | /// If Offsets is non-null, it points to a vector to be filled in |
| 73 | /// with the in-memory offsets of each of the individual values. |
| 74 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 75 | void llvm::ComputeValueVTs(const TargetLowering &TLI, Type *Ty, |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 76 | SmallVectorImpl<EVT> &ValueVTs, |
| 77 | SmallVectorImpl<uint64_t> *Offsets, |
| 78 | uint64_t StartingOffset) { |
| 79 | // Given a struct type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 80 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 81 | const StructLayout *SL = TLI.getDataLayout()->getStructLayout(STy); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 82 | for (StructType::element_iterator EB = STy->element_begin(), |
| 83 | EI = EB, |
| 84 | EE = STy->element_end(); |
| 85 | EI != EE; ++EI) |
| 86 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 87 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 88 | return; |
| 89 | } |
| 90 | // Given an array type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 91 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 92 | Type *EltTy = ATy->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 93 | uint64_t EltSize = TLI.getDataLayout()->getTypeAllocSize(EltTy); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 94 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 95 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 96 | StartingOffset + i * EltSize); |
| 97 | return; |
| 98 | } |
| 99 | // Interpret void as zero return values. |
| 100 | if (Ty->isVoidTy()) |
| 101 | return; |
| 102 | // Base case: we can get an EVT for this LLVM IR type. |
| 103 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 104 | if (Offsets) |
| 105 | Offsets->push_back(StartingOffset); |
| 106 | } |
| 107 | |
| 108 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 109 | GlobalVariable *llvm::ExtractTypeInfo(Value *V) { |
| 110 | V = V->stripPointerCasts(); |
| 111 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 112 | |
Bill Wendling | 23295cc | 2010-07-26 22:36:52 +0000 | [diff] [blame] | 113 | if (GV && GV->getName() == "llvm.eh.catch.all.value") { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 114 | assert(GV->hasInitializer() && |
| 115 | "The EH catch-all value must have an initializer"); |
| 116 | Value *Init = GV->getInitializer(); |
| 117 | GV = dyn_cast<GlobalVariable>(Init); |
| 118 | if (!GV) V = cast<ConstantPointerNull>(Init); |
| 119 | } |
| 120 | |
| 121 | assert((GV || isa<ConstantPointerNull>(V)) && |
| 122 | "TypeInfo must be a global variable or NULL"); |
| 123 | return GV; |
| 124 | } |
| 125 | |
| 126 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 127 | /// processed uses a memory 'm' constraint. |
| 128 | bool |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 129 | llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos, |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 130 | const TargetLowering &TLI) { |
| 131 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 132 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 133 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 134 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 135 | if (CType == TargetLowering::C_Memory) |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | // Indirect operand accesses access memory. |
| 140 | if (CI.isIndirect) |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 148 | /// the given LLVM IR floating-point condition code. This includes |
| 149 | /// consideration of global floating-point math flags. |
| 150 | /// |
| 151 | ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 152 | switch (Pred) { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 153 | case FCmpInst::FCMP_FALSE: return ISD::SETFALSE; |
| 154 | case FCmpInst::FCMP_OEQ: return ISD::SETOEQ; |
| 155 | case FCmpInst::FCMP_OGT: return ISD::SETOGT; |
| 156 | case FCmpInst::FCMP_OGE: return ISD::SETOGE; |
| 157 | case FCmpInst::FCMP_OLT: return ISD::SETOLT; |
| 158 | case FCmpInst::FCMP_OLE: return ISD::SETOLE; |
| 159 | case FCmpInst::FCMP_ONE: return ISD::SETONE; |
| 160 | case FCmpInst::FCMP_ORD: return ISD::SETO; |
| 161 | case FCmpInst::FCMP_UNO: return ISD::SETUO; |
| 162 | case FCmpInst::FCMP_UEQ: return ISD::SETUEQ; |
| 163 | case FCmpInst::FCMP_UGT: return ISD::SETUGT; |
| 164 | case FCmpInst::FCMP_UGE: return ISD::SETUGE; |
| 165 | case FCmpInst::FCMP_ULT: return ISD::SETULT; |
| 166 | case FCmpInst::FCMP_ULE: return ISD::SETULE; |
| 167 | case FCmpInst::FCMP_UNE: return ISD::SETUNE; |
| 168 | case FCmpInst::FCMP_TRUE: return ISD::SETTRUE; |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 169 | default: llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 170 | } |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) { |
| 174 | switch (CC) { |
| 175 | case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ; |
| 176 | case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE; |
| 177 | case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT; |
| 178 | case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE; |
| 179 | case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT; |
| 180 | case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE; |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 181 | default: return CC; |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 182 | } |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 186 | /// the given LLVM IR integer condition code. |
| 187 | /// |
| 188 | ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) { |
| 189 | switch (Pred) { |
| 190 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 191 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 192 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 193 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 194 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 195 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 196 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 197 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 198 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 199 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 200 | default: |
| 201 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 205 | |
| 206 | /// getNoopInput - If V is a noop (i.e., lowers to no machine code), look |
| 207 | /// through it (and any transitive noop operands to it) and return its input |
| 208 | /// value. This is used to determine if a tail call can be formed. |
| 209 | /// |
| 210 | static const Value *getNoopInput(const Value *V, const TargetLowering &TLI) { |
| 211 | // If V is not an instruction, it can't be looked through. |
Chris Lattner | 5b0d946 | 2012-06-01 05:16:33 +0000 | [diff] [blame] | 212 | const Instruction *I = dyn_cast<Instruction>(V); |
| 213 | if (I == 0 || !I->hasOneUse() || I->getNumOperands() == 0) return V; |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 5b0d946 | 2012-06-01 05:16:33 +0000 | [diff] [blame] | 215 | Value *Op = I->getOperand(0); |
| 216 | |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 217 | // Look through truly no-op truncates. |
Chris Lattner | 5b0d946 | 2012-06-01 05:16:33 +0000 | [diff] [blame] | 218 | if (isa<TruncInst>(I) && |
| 219 | TLI.isTruncateFree(I->getOperand(0)->getType(), I->getType())) |
| 220 | return getNoopInput(I->getOperand(0), TLI); |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 221 | |
| 222 | // Look through truly no-op bitcasts. |
Chris Lattner | 5b0d946 | 2012-06-01 05:16:33 +0000 | [diff] [blame] | 223 | if (isa<BitCastInst>(I)) { |
| 224 | // No type change at all. |
| 225 | if (Op->getType() == I->getType()) |
| 226 | return getNoopInput(Op, TLI); |
| 227 | |
| 228 | // Pointer to pointer cast. |
| 229 | if (Op->getType()->isPointerTy() && I->getType()->isPointerTy()) |
| 230 | return getNoopInput(Op, TLI); |
| 231 | |
| 232 | if (isa<VectorType>(Op->getType()) && isa<VectorType>(I->getType()) && |
| 233 | TLI.isTypeLegal(EVT::getEVT(Op->getType())) && |
| 234 | TLI.isTypeLegal(EVT::getEVT(I->getType()))) |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 235 | return getNoopInput(Op, TLI); |
| 236 | } |
Chris Lattner | 5b0d946 | 2012-06-01 05:16:33 +0000 | [diff] [blame] | 237 | |
| 238 | // Look through inttoptr. |
| 239 | if (isa<IntToPtrInst>(I) && !isa<VectorType>(I->getType())) { |
| 240 | // Make sure this isn't a truncating or extending cast. We could support |
| 241 | // this eventually, but don't bother for now. |
| 242 | if (TLI.getPointerTy().getSizeInBits() == |
| 243 | cast<IntegerType>(Op->getType())->getBitWidth()) |
| 244 | return getNoopInput(Op, TLI); |
| 245 | } |
| 246 | |
| 247 | // Look through ptrtoint. |
| 248 | if (isa<PtrToIntInst>(I) && !isa<VectorType>(I->getType())) { |
| 249 | // Make sure this isn't a truncating or extending cast. We could support |
| 250 | // this eventually, but don't bother for now. |
| 251 | if (TLI.getPointerTy().getSizeInBits() == |
| 252 | cast<IntegerType>(I->getType())->getBitWidth()) |
| 253 | return getNoopInput(Op, TLI); |
| 254 | } |
| 255 | |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 256 | |
| 257 | // Otherwise it's not something we can look through. |
| 258 | return V; |
| 259 | } |
| 260 | |
| 261 | |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 262 | /// Test if the given instruction is in a position to be optimized |
| 263 | /// with a tail-call. This roughly means that it's in a block with |
| 264 | /// a return and there's nothing that needs to be scheduled |
| 265 | /// between it and the return. |
| 266 | /// |
| 267 | /// This function only tests target-independent requirements. |
Bill Wendling | 1a17bd2 | 2013-01-18 21:50:24 +0000 | [diff] [blame^] | 268 | bool llvm::isInTailCallPosition(ImmutableCallSite CS,const TargetLowering &TLI){ |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 269 | const Instruction *I = CS.getInstruction(); |
| 270 | const BasicBlock *ExitBB = I->getParent(); |
| 271 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 272 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 273 | |
| 274 | // The block must end in a return statement or unreachable. |
| 275 | // |
| 276 | // FIXME: Decline tailcall if it's not guaranteed and if the block ends in |
| 277 | // an unreachable, for now. The way tailcall optimization is currently |
| 278 | // implemented means it will add an epilogue followed by a jump. That is |
| 279 | // not profitable. Also, if the callee is a special function (e.g. |
| 280 | // longjmp on x86), it can end up causing miscompilation that has not |
| 281 | // been fully understood. |
| 282 | if (!Ret && |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 283 | (!TLI.getTargetMachine().Options.GuaranteedTailCallOpt || |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 284 | !isa<UnreachableInst>(Term))) |
| 285 | return false; |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 286 | |
| 287 | // If I will have a chain, make sure no other instruction that will have a |
| 288 | // chain interposes between I and the return. |
| 289 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 290 | !isSafeToSpeculativelyExecute(I)) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 291 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 292 | --BBI) { |
| 293 | if (&*BBI == I) |
| 294 | break; |
| 295 | // Debug info intrinsics do not get in the way of tail call optimization. |
| 296 | if (isa<DbgInfoIntrinsic>(BBI)) |
| 297 | continue; |
| 298 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 299 | !isSafeToSpeculativelyExecute(BBI)) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
| 303 | // If the block ends with a void return or unreachable, it doesn't matter |
| 304 | // what the call's return type is. |
| 305 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 306 | |
| 307 | // If the return value is undef, it doesn't matter what the call's |
| 308 | // return type is. |
| 309 | if (isa<UndefValue>(Ret->getOperand(0))) return true; |
| 310 | |
| 311 | // Conservatively require the attributes of the call to match those of |
| 312 | // the return. Ignore noalias because it doesn't affect the call sequence. |
Evan Cheng | 9344f97 | 2011-03-19 17:03:16 +0000 | [diff] [blame] | 313 | const Function *F = ExitBB->getParent(); |
Bill Wendling | 1a17bd2 | 2013-01-18 21:50:24 +0000 | [diff] [blame^] | 314 | AttributeSet CallerAttrs = F->getAttributes(); |
| 315 | if (AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex). |
| 316 | removeAttribute(Attribute::NoAlias) != |
| 317 | AttrBuilder(CallerAttrs, AttributeSet::ReturnIndex). |
| 318 | removeAttribute(Attribute::NoAlias)) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 319 | return false; |
| 320 | |
| 321 | // It's not safe to eliminate the sign / zero extension of the return value. |
Bill Wendling | 1a17bd2 | 2013-01-18 21:50:24 +0000 | [diff] [blame^] | 322 | if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || |
| 323 | CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 324 | return false; |
| 325 | |
| 326 | // Otherwise, make sure the unmodified return value of I is the return value. |
Chris Lattner | f59e4e3 | 2012-06-01 05:29:15 +0000 | [diff] [blame] | 327 | // We handle two cases: multiple return values + scalars. |
| 328 | Value *RetVal = Ret->getOperand(0); |
| 329 | if (!isa<InsertValueInst>(RetVal) || !isa<StructType>(RetVal->getType())) |
| 330 | // Handle scalars first. |
| 331 | return getNoopInput(Ret->getOperand(0), TLI) == I; |
| 332 | |
| 333 | // If this is an aggregate return, look through the insert/extract values and |
| 334 | // see if each is transparent. |
| 335 | for (unsigned i = 0, e =cast<StructType>(RetVal->getType())->getNumElements(); |
| 336 | i != e; ++i) { |
Chris Lattner | 74ee0ef | 2012-06-01 15:02:52 +0000 | [diff] [blame] | 337 | const Value *InScalar = FindInsertedValue(RetVal, i); |
| 338 | if (InScalar == 0) return false; |
| 339 | InScalar = getNoopInput(InScalar, TLI); |
Chris Lattner | f59e4e3 | 2012-06-01 05:29:15 +0000 | [diff] [blame] | 340 | |
| 341 | // If the scalar value being inserted is an extractvalue of the right index |
| 342 | // from the call, then everything is good. |
| 343 | const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(InScalar); |
| 344 | if (EVI == 0 || EVI->getOperand(0) != I || EVI->getNumIndices() != 1 || |
| 345 | EVI->getIndices()[0] != i) |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | return true; |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 350 | } |