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" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence |
| 30 | /// of insertvalue or extractvalue indices that identify a member, return |
| 31 | /// the linearized index of the start of the member. |
| 32 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 33 | unsigned llvm::ComputeLinearIndex(Type *Ty, |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 34 | const unsigned *Indices, |
| 35 | const unsigned *IndicesEnd, |
| 36 | unsigned CurIndex) { |
| 37 | // Base case: We're done. |
| 38 | if (Indices && Indices == IndicesEnd) |
| 39 | return CurIndex; |
| 40 | |
| 41 | // Given a struct type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 42 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 43 | for (StructType::element_iterator EB = STy->element_begin(), |
| 44 | EI = EB, |
| 45 | EE = STy->element_end(); |
| 46 | EI != EE; ++EI) { |
| 47 | if (Indices && *Indices == unsigned(EI - EB)) |
Dan Gohman | 0dadb15 | 2010-10-06 16:18:29 +0000 | [diff] [blame] | 48 | return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex); |
| 49 | CurIndex = ComputeLinearIndex(*EI, 0, 0, CurIndex); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 50 | } |
| 51 | return CurIndex; |
| 52 | } |
| 53 | // Given an array type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 54 | else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 55 | Type *EltTy = ATy->getElementType(); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 56 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) { |
| 57 | if (Indices && *Indices == i) |
Dan Gohman | 0dadb15 | 2010-10-06 16:18:29 +0000 | [diff] [blame] | 58 | return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex); |
| 59 | CurIndex = ComputeLinearIndex(EltTy, 0, 0, CurIndex); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 60 | } |
| 61 | return CurIndex; |
| 62 | } |
| 63 | // We haven't found the type we're looking for, so keep searching. |
| 64 | return CurIndex + 1; |
| 65 | } |
| 66 | |
| 67 | /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of |
| 68 | /// EVTs that represent all the individual underlying |
| 69 | /// non-aggregate types that comprise it. |
| 70 | /// |
| 71 | /// If Offsets is non-null, it points to a vector to be filled in |
| 72 | /// with the in-memory offsets of each of the individual values. |
| 73 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 74 | void llvm::ComputeValueVTs(const TargetLowering &TLI, Type *Ty, |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 75 | SmallVectorImpl<EVT> &ValueVTs, |
| 76 | SmallVectorImpl<uint64_t> *Offsets, |
| 77 | uint64_t StartingOffset) { |
| 78 | // Given a struct type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 79 | if (StructType *STy = dyn_cast<StructType>(Ty)) { |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 80 | const StructLayout *SL = TLI.getDataLayout()->getStructLayout(STy); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 81 | for (StructType::element_iterator EB = STy->element_begin(), |
| 82 | EI = EB, |
| 83 | EE = STy->element_end(); |
| 84 | EI != EE; ++EI) |
| 85 | ComputeValueVTs(TLI, *EI, ValueVTs, Offsets, |
| 86 | StartingOffset + SL->getElementOffset(EI - EB)); |
| 87 | return; |
| 88 | } |
| 89 | // Given an array type, recursively traverse the elements. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 90 | if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| 91 | Type *EltTy = ATy->getElementType(); |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 92 | uint64_t EltSize = TLI.getDataLayout()->getTypeAllocSize(EltTy); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 93 | for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) |
| 94 | ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets, |
| 95 | StartingOffset + i * EltSize); |
| 96 | return; |
| 97 | } |
| 98 | // Interpret void as zero return values. |
| 99 | if (Ty->isVoidTy()) |
| 100 | return; |
| 101 | // Base case: we can get an EVT for this LLVM IR type. |
| 102 | ValueVTs.push_back(TLI.getValueType(Ty)); |
| 103 | if (Offsets) |
| 104 | Offsets->push_back(StartingOffset); |
| 105 | } |
| 106 | |
| 107 | /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V. |
| 108 | GlobalVariable *llvm::ExtractTypeInfo(Value *V) { |
| 109 | V = V->stripPointerCasts(); |
| 110 | GlobalVariable *GV = dyn_cast<GlobalVariable>(V); |
| 111 | |
Bill Wendling | 23295cc | 2010-07-26 22:36:52 +0000 | [diff] [blame] | 112 | if (GV && GV->getName() == "llvm.eh.catch.all.value") { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 113 | assert(GV->hasInitializer() && |
| 114 | "The EH catch-all value must have an initializer"); |
| 115 | Value *Init = GV->getInitializer(); |
| 116 | GV = dyn_cast<GlobalVariable>(Init); |
| 117 | if (!GV) V = cast<ConstantPointerNull>(Init); |
| 118 | } |
| 119 | |
| 120 | assert((GV || isa<ConstantPointerNull>(V)) && |
| 121 | "TypeInfo must be a global variable or NULL"); |
| 122 | return GV; |
| 123 | } |
| 124 | |
| 125 | /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being |
| 126 | /// processed uses a memory 'm' constraint. |
| 127 | bool |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 128 | llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos, |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 129 | const TargetLowering &TLI) { |
| 130 | for (unsigned i = 0, e = CInfos.size(); i != e; ++i) { |
| 131 | InlineAsm::ConstraintInfo &CI = CInfos[i]; |
| 132 | for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) { |
| 133 | TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]); |
| 134 | if (CType == TargetLowering::C_Memory) |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | // Indirect operand accesses access memory. |
| 139 | if (CI.isIndirect) |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /// getFCmpCondCode - Return the ISD condition code corresponding to |
| 147 | /// the given LLVM IR floating-point condition code. This includes |
| 148 | /// consideration of global floating-point math flags. |
| 149 | /// |
| 150 | ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 151 | switch (Pred) { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 152 | case FCmpInst::FCMP_FALSE: return ISD::SETFALSE; |
| 153 | case FCmpInst::FCMP_OEQ: return ISD::SETOEQ; |
| 154 | case FCmpInst::FCMP_OGT: return ISD::SETOGT; |
| 155 | case FCmpInst::FCMP_OGE: return ISD::SETOGE; |
| 156 | case FCmpInst::FCMP_OLT: return ISD::SETOLT; |
| 157 | case FCmpInst::FCMP_OLE: return ISD::SETOLE; |
| 158 | case FCmpInst::FCMP_ONE: return ISD::SETONE; |
| 159 | case FCmpInst::FCMP_ORD: return ISD::SETO; |
| 160 | case FCmpInst::FCMP_UNO: return ISD::SETUO; |
| 161 | case FCmpInst::FCMP_UEQ: return ISD::SETUEQ; |
| 162 | case FCmpInst::FCMP_UGT: return ISD::SETUGT; |
| 163 | case FCmpInst::FCMP_UGE: return ISD::SETUGE; |
| 164 | case FCmpInst::FCMP_ULT: return ISD::SETULT; |
| 165 | case FCmpInst::FCMP_ULE: return ISD::SETULE; |
| 166 | case FCmpInst::FCMP_UNE: return ISD::SETUNE; |
| 167 | case FCmpInst::FCMP_TRUE: return ISD::SETTRUE; |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 168 | default: llvm_unreachable("Invalid FCmp predicate opcode!"); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 169 | } |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) { |
| 173 | switch (CC) { |
| 174 | case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ; |
| 175 | case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE; |
| 176 | case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT; |
| 177 | case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE; |
| 178 | case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT; |
| 179 | case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE; |
David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 180 | default: return CC; |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 181 | } |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /// getICmpCondCode - Return the ISD condition code corresponding to |
| 185 | /// the given LLVM IR integer condition code. |
| 186 | /// |
| 187 | ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) { |
| 188 | switch (Pred) { |
| 189 | case ICmpInst::ICMP_EQ: return ISD::SETEQ; |
| 190 | case ICmpInst::ICMP_NE: return ISD::SETNE; |
| 191 | case ICmpInst::ICMP_SLE: return ISD::SETLE; |
| 192 | case ICmpInst::ICMP_ULE: return ISD::SETULE; |
| 193 | case ICmpInst::ICMP_SGE: return ISD::SETGE; |
| 194 | case ICmpInst::ICMP_UGE: return ISD::SETUGE; |
| 195 | case ICmpInst::ICMP_SLT: return ISD::SETLT; |
| 196 | case ICmpInst::ICMP_ULT: return ISD::SETULT; |
| 197 | case ICmpInst::ICMP_SGT: return ISD::SETGT; |
| 198 | case ICmpInst::ICMP_UGT: return ISD::SETUGT; |
| 199 | default: |
| 200 | llvm_unreachable("Invalid ICmp predicate opcode!"); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 204 | static bool isNoopBitcast(Type *T1, Type *T2, |
Michael Gottesman | 9cb1685 | 2013-07-22 21:05:47 +0000 | [diff] [blame] | 205 | const TargetLoweringBase& TLI) { |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 206 | return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) || |
| 207 | (isa<VectorType>(T1) && isa<VectorType>(T2) && |
| 208 | TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2))); |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 211 | /// Look through operations that will be free to find the earliest source of |
| 212 | /// this value. |
| 213 | /// |
| 214 | /// @param ValLoc If V has aggegate type, we will be interested in a particular |
| 215 | /// scalar component. This records its address; the reverse of this list gives a |
| 216 | /// sequence of indices appropriate for an extractvalue to locate the important |
| 217 | /// value. This value is updated during the function and on exit will indicate |
| 218 | /// similar information for the Value returned. |
| 219 | /// |
| 220 | /// @param DataBits If this function looks through truncate instructions, this |
| 221 | /// will record the smallest size attained. |
| 222 | static const Value *getNoopInput(const Value *V, |
| 223 | SmallVectorImpl<unsigned> &ValLoc, |
| 224 | unsigned &DataBits, |
| 225 | const TargetLoweringBase &TLI) { |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 226 | while (true) { |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 227 | // Try to look through V1; if V1 is not an instruction, it can't be looked |
| 228 | // through. |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 229 | const Instruction *I = dyn_cast<Instruction>(V); |
| 230 | if (!I || I->getNumOperands() == 0) return V; |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 231 | const Value *NoopInput = 0; |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 232 | |
| 233 | Value *Op = I->getOperand(0); |
| 234 | if (isa<BitCastInst>(I)) { |
| 235 | // Look through truly no-op bitcasts. |
| 236 | if (isNoopBitcast(Op->getType(), I->getType(), TLI)) |
| 237 | NoopInput = Op; |
| 238 | } else if (isa<GetElementPtrInst>(I)) { |
| 239 | // Look through getelementptr |
| 240 | if (cast<GetElementPtrInst>(I)->hasAllZeroIndices()) |
| 241 | NoopInput = Op; |
| 242 | } else if (isa<IntToPtrInst>(I)) { |
| 243 | // Look through inttoptr. |
| 244 | // Make sure this isn't a truncating or extending cast. We could |
| 245 | // support this eventually, but don't bother for now. |
| 246 | if (!isa<VectorType>(I->getType()) && |
| 247 | TLI.getPointerTy().getSizeInBits() == |
| 248 | cast<IntegerType>(Op->getType())->getBitWidth()) |
| 249 | NoopInput = Op; |
| 250 | } else if (isa<PtrToIntInst>(I)) { |
| 251 | // Look through ptrtoint. |
| 252 | // Make sure this isn't a truncating or extending cast. We could |
| 253 | // support this eventually, but don't bother for now. |
| 254 | if (!isa<VectorType>(I->getType()) && |
| 255 | TLI.getPointerTy().getSizeInBits() == |
| 256 | cast<IntegerType>(I->getType())->getBitWidth()) |
| 257 | NoopInput = Op; |
| 258 | } else if (isa<TruncInst>(I) && |
| 259 | TLI.allowTruncateForTailCall(Op->getType(), I->getType())) { |
| 260 | DataBits = std::min(DataBits, I->getType()->getPrimitiveSizeInBits()); |
| 261 | NoopInput = Op; |
| 262 | } else if (isa<CallInst>(I)) { |
| 263 | // Look through call (skipping callee) |
| 264 | for (User::const_op_iterator i = I->op_begin(), e = I->op_end() - 1; |
| 265 | i != e; ++i) { |
| 266 | unsigned attrInd = i - I->op_begin() + 1; |
| 267 | if (cast<CallInst>(I)->paramHasAttr(attrInd, Attribute::Returned) && |
| 268 | isNoopBitcast((*i)->getType(), I->getType(), TLI)) { |
| 269 | NoopInput = *i; |
| 270 | break; |
Stephen Lin | 456ca04 | 2013-04-20 05:14:40 +0000 | [diff] [blame] | 271 | } |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 272 | } |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 273 | } else if (isa<InvokeInst>(I)) { |
| 274 | // Look through invoke (skipping BB, BB, Callee) |
| 275 | for (User::const_op_iterator i = I->op_begin(), e = I->op_end() - 3; |
| 276 | i != e; ++i) { |
| 277 | unsigned attrInd = i - I->op_begin() + 1; |
| 278 | if (cast<InvokeInst>(I)->paramHasAttr(attrInd, Attribute::Returned) && |
| 279 | isNoopBitcast((*i)->getType(), I->getType(), TLI)) { |
| 280 | NoopInput = *i; |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) { |
| 285 | // Value may come from either the aggregate or the scalar |
| 286 | ArrayRef<unsigned> InsertLoc = IVI->getIndices(); |
| 287 | if (std::equal(InsertLoc.rbegin(), InsertLoc.rend(), |
| 288 | ValLoc.rbegin())) { |
| 289 | // The type being inserted is a nested sub-type of the aggregate; we |
| 290 | // have to remove those initial indices to get the location we're |
| 291 | // interested in for the operand. |
| 292 | ValLoc.resize(ValLoc.size() - InsertLoc.size()); |
| 293 | NoopInput = IVI->getInsertedValueOperand(); |
| 294 | } else { |
| 295 | // The struct we're inserting into has the value we're interested in, no |
| 296 | // change of address. |
| 297 | NoopInput = Op; |
| 298 | } |
| 299 | } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) { |
| 300 | // The part we're interested in will inevitably be some sub-section of the |
| 301 | // previous aggregate. Combine the two paths to obtain the true address of |
| 302 | // our element. |
| 303 | ArrayRef<unsigned> ExtractLoc = EVI->getIndices(); |
| 304 | std::copy(ExtractLoc.rbegin(), ExtractLoc.rend(), |
| 305 | std::back_inserter(ValLoc)); |
| 306 | NoopInput = Op; |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 307 | } |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 308 | // Terminate if we couldn't find anything to look through. |
| 309 | if (!NoopInput) |
| 310 | return V; |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 311 | |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 312 | V = NoopInput; |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 313 | } |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 315 | |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 316 | /// Return true if this scalar return value only has bits discarded on its path |
| 317 | /// from the "tail call" to the "ret". This includes the obvious noop |
| 318 | /// instructions handled by getNoopInput above as well as free truncations (or |
| 319 | /// extensions prior to the call). |
| 320 | static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal, |
| 321 | SmallVectorImpl<unsigned> &RetIndices, |
| 322 | SmallVectorImpl<unsigned> &CallIndices, |
Tim Northover | 6a4e44f | 2013-08-12 09:45:46 +0000 | [diff] [blame] | 323 | bool AllowDifferingSizes, |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 324 | const TargetLoweringBase &TLI) { |
| 325 | |
| 326 | // Trace the sub-value needed by the return value as far back up the graph as |
| 327 | // possible, in the hope that it will intersect with the value produced by the |
| 328 | // call. In the simple case with no "returned" attribute, the hope is actually |
| 329 | // that we end up back at the tail call instruction itself. |
| 330 | unsigned BitsRequired = UINT_MAX; |
| 331 | RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI); |
| 332 | |
| 333 | // If this slot in the value returned is undef, it doesn't matter what the |
| 334 | // call puts there, it'll be fine. |
| 335 | if (isa<UndefValue>(RetVal)) |
| 336 | return true; |
| 337 | |
| 338 | // Now do a similar search up through the graph to find where the value |
| 339 | // actually returned by the "tail call" comes from. In the simple case without |
| 340 | // a "returned" attribute, the search will be blocked immediately and the loop |
| 341 | // a Noop. |
| 342 | unsigned BitsProvided = UINT_MAX; |
| 343 | CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI); |
| 344 | |
| 345 | // There's no hope if we can't actually trace them to (the same part of!) the |
| 346 | // same value. |
| 347 | if (CallVal != RetVal || CallIndices != RetIndices) |
| 348 | return false; |
| 349 | |
| 350 | // However, intervening truncates may have made the call non-tail. Make sure |
| 351 | // all the bits that are needed by the "ret" have been provided by the "tail |
| 352 | // call". FIXME: with sufficiently cunning bit-tracking, we could look through |
| 353 | // extensions too. |
Tim Northover | 6a4e44f | 2013-08-12 09:45:46 +0000 | [diff] [blame] | 354 | if (BitsProvided < BitsRequired || |
| 355 | (!AllowDifferingSizes && BitsProvided != BitsRequired)) |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 356 | return false; |
| 357 | |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | /// For an aggregate type, determine whether a given index is within bounds or |
| 362 | /// not. |
| 363 | static bool indexReallyValid(CompositeType *T, unsigned Idx) { |
| 364 | if (ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 365 | return Idx < AT->getNumElements(); |
| 366 | |
| 367 | return Idx < cast<StructType>(T)->getNumElements(); |
| 368 | } |
| 369 | |
| 370 | /// Move the given iterators to the next leaf type in depth first traversal. |
| 371 | /// |
| 372 | /// Performs a depth-first traversal of the type as specified by its arguments, |
| 373 | /// stopping at the next leaf node (which may be a legitimate scalar type or an |
| 374 | /// empty struct or array). |
| 375 | /// |
| 376 | /// @param SubTypes List of the partial components making up the type from |
| 377 | /// outermost to innermost non-empty aggregate. The element currently |
| 378 | /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1). |
| 379 | /// |
| 380 | /// @param Path Set of extractvalue indices leading from the outermost type |
| 381 | /// (SubTypes[0]) to the leaf node currently represented. |
| 382 | /// |
| 383 | /// @returns true if a new type was found, false otherwise. Calling this |
| 384 | /// function again on a finished iterator will repeatedly return |
| 385 | /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty |
| 386 | /// aggregate or a non-aggregate |
Benjamin Kramer | b0e8d37 | 2013-08-09 14:44:41 +0000 | [diff] [blame] | 387 | static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes, |
| 388 | SmallVectorImpl<unsigned> &Path) { |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 389 | // First march back up the tree until we can successfully increment one of the |
| 390 | // coordinates in Path. |
| 391 | while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) { |
| 392 | Path.pop_back(); |
| 393 | SubTypes.pop_back(); |
| 394 | } |
| 395 | |
| 396 | // If we reached the top, then the iterator is done. |
| 397 | if (Path.empty()) |
| 398 | return false; |
| 399 | |
| 400 | // We know there's *some* valid leaf now, so march back down the tree picking |
| 401 | // out the left-most element at each node. |
| 402 | ++Path.back(); |
| 403 | Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back()); |
| 404 | while (DeeperType->isAggregateType()) { |
| 405 | CompositeType *CT = cast<CompositeType>(DeeperType); |
| 406 | if (!indexReallyValid(CT, 0)) |
| 407 | return true; |
| 408 | |
| 409 | SubTypes.push_back(CT); |
| 410 | Path.push_back(0); |
| 411 | |
| 412 | DeeperType = CT->getTypeAtIndex(0U); |
| 413 | } |
| 414 | |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | /// Find the first non-empty, scalar-like type in Next and setup the iterator |
| 419 | /// components. |
| 420 | /// |
| 421 | /// Assuming Next is an aggregate of some kind, this function will traverse the |
| 422 | /// tree from left to right (i.e. depth-first) looking for the first |
| 423 | /// non-aggregate type which will play a role in function return. |
| 424 | /// |
| 425 | /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup |
| 426 | /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first |
| 427 | /// i32 in that type. |
| 428 | static bool firstRealType(Type *Next, |
| 429 | SmallVectorImpl<CompositeType *> &SubTypes, |
| 430 | SmallVectorImpl<unsigned> &Path) { |
| 431 | // First initialise the iterator components to the first "leaf" node |
| 432 | // (i.e. node with no valid sub-type at any index, so {} does count as a leaf |
| 433 | // despite nominally being an aggregate). |
| 434 | while (Next->isAggregateType() && |
| 435 | indexReallyValid(cast<CompositeType>(Next), 0)) { |
| 436 | SubTypes.push_back(cast<CompositeType>(Next)); |
| 437 | Path.push_back(0); |
| 438 | Next = cast<CompositeType>(Next)->getTypeAtIndex(0U); |
| 439 | } |
| 440 | |
| 441 | // If there's no Path now, Next was originally scalar already (or empty |
| 442 | // leaf). We're done. |
| 443 | if (Path.empty()) |
| 444 | return true; |
| 445 | |
| 446 | // Otherwise, use normal iteration to keep looking through the tree until we |
| 447 | // find a non-aggregate type. |
| 448 | while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()) { |
| 449 | if (!advanceToNextLeafType(SubTypes, Path)) |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | /// Set the iterator data-structures to the next non-empty, non-aggregate |
| 457 | /// subtype. |
Benjamin Kramer | b0e8d37 | 2013-08-09 14:44:41 +0000 | [diff] [blame] | 458 | static bool nextRealType(SmallVectorImpl<CompositeType *> &SubTypes, |
| 459 | SmallVectorImpl<unsigned> &Path) { |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 460 | do { |
| 461 | if (!advanceToNextLeafType(SubTypes, Path)) |
| 462 | return false; |
| 463 | |
| 464 | assert(!Path.empty() && "found a leaf but didn't set the path?"); |
| 465 | } while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()); |
| 466 | |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 471 | /// Test if the given instruction is in a position to be optimized |
| 472 | /// with a tail-call. This roughly means that it's in a block with |
| 473 | /// a return and there's nothing that needs to be scheduled |
| 474 | /// between it and the return. |
| 475 | /// |
| 476 | /// This function only tests target-independent requirements. |
Stephen Lin | 5c34e08 | 2013-04-20 04:27:51 +0000 | [diff] [blame] | 477 | bool llvm::isInTailCallPosition(ImmutableCallSite CS, |
| 478 | const TargetLowering &TLI) { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 479 | const Instruction *I = CS.getInstruction(); |
| 480 | const BasicBlock *ExitBB = I->getParent(); |
| 481 | const TerminatorInst *Term = ExitBB->getTerminator(); |
| 482 | const ReturnInst *Ret = dyn_cast<ReturnInst>(Term); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 483 | |
| 484 | // The block must end in a return statement or unreachable. |
| 485 | // |
| 486 | // FIXME: Decline tailcall if it's not guaranteed and if the block ends in |
| 487 | // an unreachable, for now. The way tailcall optimization is currently |
| 488 | // implemented means it will add an epilogue followed by a jump. That is |
| 489 | // not profitable. Also, if the callee is a special function (e.g. |
| 490 | // longjmp on x86), it can end up causing miscompilation that has not |
| 491 | // been fully understood. |
| 492 | if (!Ret && |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 493 | (!TLI.getTargetMachine().Options.GuaranteedTailCallOpt || |
Chris Lattner | cd6015c | 2012-06-01 05:01:15 +0000 | [diff] [blame] | 494 | !isa<UnreachableInst>(Term))) |
| 495 | return false; |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 496 | |
| 497 | // If I will have a chain, make sure no other instruction that will have a |
| 498 | // chain interposes between I and the return. |
| 499 | if (I->mayHaveSideEffects() || I->mayReadFromMemory() || |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 500 | !isSafeToSpeculativelyExecute(I)) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 501 | for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ; |
| 502 | --BBI) { |
| 503 | if (&*BBI == I) |
| 504 | break; |
| 505 | // Debug info intrinsics do not get in the way of tail call optimization. |
| 506 | if (isa<DbgInfoIntrinsic>(BBI)) |
| 507 | continue; |
| 508 | if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() || |
Dan Gohman | f042660 | 2011-12-14 23:49:11 +0000 | [diff] [blame] | 509 | !isSafeToSpeculativelyExecute(BBI)) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 510 | return false; |
| 511 | } |
| 512 | |
Michael Gottesman | 9d6852c | 2013-08-20 08:36:50 +0000 | [diff] [blame^] | 513 | return returnTypeIsEligibleForTailCall(ExitBB->getParent(), I, Ret, TLI); |
| 514 | } |
| 515 | |
| 516 | bool llvm::returnTypeIsEligibleForTailCall(const Function *F, |
| 517 | const Instruction *I, |
| 518 | const ReturnInst *Ret, |
| 519 | const TargetLoweringBase &TLI) { |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 520 | // If the block ends with a void return or unreachable, it doesn't matter |
| 521 | // what the call's return type is. |
| 522 | if (!Ret || Ret->getNumOperands() == 0) return true; |
| 523 | |
| 524 | // If the return value is undef, it doesn't matter what the call's |
| 525 | // return type is. |
| 526 | if (isa<UndefValue>(Ret->getOperand(0))) return true; |
| 527 | |
Tim Northover | 6a4e44f | 2013-08-12 09:45:46 +0000 | [diff] [blame] | 528 | // Make sure the attributes attached to each return are compatible. |
Michael Gottesman | 9d6852c | 2013-08-20 08:36:50 +0000 | [diff] [blame^] | 529 | AttrBuilder CallerAttrs(F->getAttributes(), |
Tim Northover | 6a4e44f | 2013-08-12 09:45:46 +0000 | [diff] [blame] | 530 | AttributeSet::ReturnIndex); |
| 531 | AttrBuilder CalleeAttrs(cast<CallInst>(I)->getAttributes(), |
| 532 | AttributeSet::ReturnIndex); |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 533 | |
Tim Northover | 6a4e44f | 2013-08-12 09:45:46 +0000 | [diff] [blame] | 534 | // Noalias is completely benign as far as calling convention goes, it |
| 535 | // shouldn't affect whether the call is a tail call. |
| 536 | CallerAttrs = CallerAttrs.removeAttribute(Attribute::NoAlias); |
| 537 | CalleeAttrs = CalleeAttrs.removeAttribute(Attribute::NoAlias); |
| 538 | |
| 539 | bool AllowDifferingSizes = true; |
| 540 | if (CallerAttrs.contains(Attribute::ZExt)) { |
| 541 | if (!CalleeAttrs.contains(Attribute::ZExt)) |
| 542 | return false; |
| 543 | |
| 544 | AllowDifferingSizes = false; |
| 545 | CallerAttrs.removeAttribute(Attribute::ZExt); |
| 546 | CalleeAttrs.removeAttribute(Attribute::ZExt); |
| 547 | } else if (CallerAttrs.contains(Attribute::SExt)) { |
| 548 | if (!CalleeAttrs.contains(Attribute::SExt)) |
| 549 | return false; |
| 550 | |
| 551 | AllowDifferingSizes = false; |
| 552 | CallerAttrs.removeAttribute(Attribute::SExt); |
| 553 | CalleeAttrs.removeAttribute(Attribute::SExt); |
| 554 | } |
| 555 | |
| 556 | // If they're still different, there's some facet we don't understand |
| 557 | // (currently only "inreg", but in future who knows). It may be OK but the |
| 558 | // only safe option is to reject the tail call. |
| 559 | if (CallerAttrs != CalleeAttrs) |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 560 | return false; |
| 561 | |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 562 | const Value *RetVal = Ret->getOperand(0), *CallVal = I; |
| 563 | SmallVector<unsigned, 4> RetPath, CallPath; |
| 564 | SmallVector<CompositeType *, 4> RetSubTypes, CallSubTypes; |
| 565 | |
| 566 | bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath); |
| 567 | bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath); |
| 568 | |
| 569 | // Nothing's actually returned, it doesn't matter what the callee put there |
| 570 | // it's a valid tail call. |
| 571 | if (RetEmpty) |
| 572 | return true; |
| 573 | |
| 574 | // Iterate pairwise through each of the value types making up the tail call |
| 575 | // and the corresponding return. For each one we want to know whether it's |
| 576 | // essentially going directly from the tail call to the ret, via operations |
| 577 | // that end up not generating any code. |
| 578 | // |
| 579 | // We allow a certain amount of covariance here. For example it's permitted |
| 580 | // for the tail call to define more bits than the ret actually cares about |
| 581 | // (e.g. via a truncate). |
| 582 | do { |
| 583 | if (CallEmpty) { |
| 584 | // We've exhausted the values produced by the tail call instruction, the |
| 585 | // rest are essentially undef. The type doesn't really matter, but we need |
| 586 | // *something*. |
| 587 | Type *SlotType = RetSubTypes.back()->getTypeAtIndex(RetPath.back()); |
| 588 | CallVal = UndefValue::get(SlotType); |
| 589 | } |
| 590 | |
| 591 | // The manipulations performed when we're looking through an insertvalue or |
| 592 | // an extractvalue would happen at the front of the RetPath list, so since |
| 593 | // we have to copy it anyway it's more efficient to create a reversed copy. |
| 594 | using std::copy; |
| 595 | SmallVector<unsigned, 4> TmpRetPath, TmpCallPath; |
| 596 | copy(RetPath.rbegin(), RetPath.rend(), std::back_inserter(TmpRetPath)); |
| 597 | copy(CallPath.rbegin(), CallPath.rend(), std::back_inserter(TmpCallPath)); |
| 598 | |
| 599 | // Finally, we can check whether the value produced by the tail call at this |
| 600 | // index is compatible with the value we return. |
Tim Northover | 6a4e44f | 2013-08-12 09:45:46 +0000 | [diff] [blame] | 601 | if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath, |
| 602 | AllowDifferingSizes, TLI)) |
Tim Northover | d113448 | 2013-08-06 09:12:35 +0000 | [diff] [blame] | 603 | return false; |
| 604 | |
| 605 | CallEmpty = !nextRealType(CallSubTypes, CallPath); |
| 606 | } while(nextRealType(RetSubTypes, RetPath)); |
| 607 | |
| 608 | return true; |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 609 | } |