1 //===-- Analysis.cpp - CodeGen LLVM IR Analysis Utilities -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines several CodeGen-specific LLVM IR analysis utilities.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/Analysis.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/SelectionDAG.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Target/TargetInstrInfo.h"
30 #include "llvm/Target/TargetSubtargetInfo.h"
31 #include "llvm/Transforms/Utils/GlobalStatus.h"
35 /// Compute the linearized index of a member in a nested aggregate/struct/array
36 /// by recursing and accumulating CurIndex as long as there are indices in the
38 unsigned llvm::ComputeLinearIndex(Type *Ty,
39 const unsigned *Indices,
40 const unsigned *IndicesEnd,
42 // Base case: We're done.
43 if (Indices && Indices == IndicesEnd)
46 // Given a struct type, recursively traverse the elements.
47 if (StructType *STy = dyn_cast<StructType>(Ty)) {
48 for (StructType::element_iterator EB = STy->element_begin(),
50 EE = STy->element_end();
52 if (Indices && *Indices == unsigned(EI - EB))
53 return ComputeLinearIndex(*EI, Indices+1, IndicesEnd, CurIndex);
54 CurIndex = ComputeLinearIndex(*EI, nullptr, nullptr, CurIndex);
56 assert(!Indices && "Unexpected out of bound");
59 // Given an array type, recursively traverse the elements.
60 else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
61 Type *EltTy = ATy->getElementType();
62 unsigned NumElts = ATy->getNumElements();
63 // Compute the Linear offset when jumping one element of the array
64 unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);
66 assert(*Indices < NumElts && "Unexpected out of bound");
67 // If the indice is inside the array, compute the index to the requested
68 // elt and recurse inside the element with the end of the indices list
69 CurIndex += EltLinearOffset* *Indices;
70 return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
72 CurIndex += EltLinearOffset*NumElts;
75 // We haven't found the type we're looking for, so keep searching.
79 /// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
80 /// EVTs that represent all the individual underlying
81 /// non-aggregate types that comprise it.
83 /// If Offsets is non-null, it points to a vector to be filled in
84 /// with the in-memory offsets of each of the individual values.
86 void llvm::ComputeValueVTs(const TargetLowering &TLI, const DataLayout &DL,
87 Type *Ty, SmallVectorImpl<EVT> &ValueVTs,
88 SmallVectorImpl<uint64_t> *Offsets,
89 uint64_t StartingOffset) {
90 // Given a struct type, recursively traverse the elements.
91 if (StructType *STy = dyn_cast<StructType>(Ty)) {
92 const StructLayout *SL = DL.getStructLayout(STy);
93 for (StructType::element_iterator EB = STy->element_begin(),
95 EE = STy->element_end();
97 ComputeValueVTs(TLI, DL, *EI, ValueVTs, Offsets,
98 StartingOffset + SL->getElementOffset(EI - EB));
101 // Given an array type, recursively traverse the elements.
102 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
103 Type *EltTy = ATy->getElementType();
104 uint64_t EltSize = DL.getTypeAllocSize(EltTy);
105 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
106 ComputeValueVTs(TLI, DL, EltTy, ValueVTs, Offsets,
107 StartingOffset + i * EltSize);
110 // Interpret void as zero return values.
113 // Base case: we can get an EVT for this LLVM IR type.
114 ValueVTs.push_back(TLI.getValueType(DL, Ty));
116 Offsets->push_back(StartingOffset);
119 /// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
120 GlobalValue *llvm::ExtractTypeInfo(Value *V) {
121 V = V->stripPointerCasts();
122 GlobalValue *GV = dyn_cast<GlobalValue>(V);
123 GlobalVariable *Var = dyn_cast<GlobalVariable>(V);
125 if (Var && Var->getName() == "llvm.eh.catch.all.value") {
126 assert(Var->hasInitializer() &&
127 "The EH catch-all value must have an initializer");
128 Value *Init = Var->getInitializer();
129 GV = dyn_cast<GlobalValue>(Init);
130 if (!GV) V = cast<ConstantPointerNull>(Init);
133 assert((GV || isa<ConstantPointerNull>(V)) &&
134 "TypeInfo must be a global variable or NULL");
138 /// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
139 /// processed uses a memory 'm' constraint.
141 llvm::hasInlineAsmMemConstraint(InlineAsm::ConstraintInfoVector &CInfos,
142 const TargetLowering &TLI) {
143 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
144 InlineAsm::ConstraintInfo &CI = CInfos[i];
145 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
146 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
147 if (CType == TargetLowering::C_Memory)
151 // Indirect operand accesses access memory.
159 /// getFCmpCondCode - Return the ISD condition code corresponding to
160 /// the given LLVM IR floating-point condition code. This includes
161 /// consideration of global floating-point math flags.
163 ISD::CondCode llvm::getFCmpCondCode(FCmpInst::Predicate Pred) {
165 case FCmpInst::FCMP_FALSE: return ISD::SETFALSE;
166 case FCmpInst::FCMP_OEQ: return ISD::SETOEQ;
167 case FCmpInst::FCMP_OGT: return ISD::SETOGT;
168 case FCmpInst::FCMP_OGE: return ISD::SETOGE;
169 case FCmpInst::FCMP_OLT: return ISD::SETOLT;
170 case FCmpInst::FCMP_OLE: return ISD::SETOLE;
171 case FCmpInst::FCMP_ONE: return ISD::SETONE;
172 case FCmpInst::FCMP_ORD: return ISD::SETO;
173 case FCmpInst::FCMP_UNO: return ISD::SETUO;
174 case FCmpInst::FCMP_UEQ: return ISD::SETUEQ;
175 case FCmpInst::FCMP_UGT: return ISD::SETUGT;
176 case FCmpInst::FCMP_UGE: return ISD::SETUGE;
177 case FCmpInst::FCMP_ULT: return ISD::SETULT;
178 case FCmpInst::FCMP_ULE: return ISD::SETULE;
179 case FCmpInst::FCMP_UNE: return ISD::SETUNE;
180 case FCmpInst::FCMP_TRUE: return ISD::SETTRUE;
181 default: llvm_unreachable("Invalid FCmp predicate opcode!");
185 ISD::CondCode llvm::getFCmpCodeWithoutNaN(ISD::CondCode CC) {
187 case ISD::SETOEQ: case ISD::SETUEQ: return ISD::SETEQ;
188 case ISD::SETONE: case ISD::SETUNE: return ISD::SETNE;
189 case ISD::SETOLT: case ISD::SETULT: return ISD::SETLT;
190 case ISD::SETOLE: case ISD::SETULE: return ISD::SETLE;
191 case ISD::SETOGT: case ISD::SETUGT: return ISD::SETGT;
192 case ISD::SETOGE: case ISD::SETUGE: return ISD::SETGE;
197 /// getICmpCondCode - Return the ISD condition code corresponding to
198 /// the given LLVM IR integer condition code.
200 ISD::CondCode llvm::getICmpCondCode(ICmpInst::Predicate Pred) {
202 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
203 case ICmpInst::ICMP_NE: return ISD::SETNE;
204 case ICmpInst::ICMP_SLE: return ISD::SETLE;
205 case ICmpInst::ICMP_ULE: return ISD::SETULE;
206 case ICmpInst::ICMP_SGE: return ISD::SETGE;
207 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
208 case ICmpInst::ICMP_SLT: return ISD::SETLT;
209 case ICmpInst::ICMP_ULT: return ISD::SETULT;
210 case ICmpInst::ICMP_SGT: return ISD::SETGT;
211 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
213 llvm_unreachable("Invalid ICmp predicate opcode!");
217 static bool isNoopBitcast(Type *T1, Type *T2,
218 const TargetLoweringBase& TLI) {
219 return T1 == T2 || (T1->isPointerTy() && T2->isPointerTy()) ||
220 (isa<VectorType>(T1) && isa<VectorType>(T2) &&
221 TLI.isTypeLegal(EVT::getEVT(T1)) && TLI.isTypeLegal(EVT::getEVT(T2)));
224 /// Look through operations that will be free to find the earliest source of
227 /// @param ValLoc If V has aggegate type, we will be interested in a particular
228 /// scalar component. This records its address; the reverse of this list gives a
229 /// sequence of indices appropriate for an extractvalue to locate the important
230 /// value. This value is updated during the function and on exit will indicate
231 /// similar information for the Value returned.
233 /// @param DataBits If this function looks through truncate instructions, this
234 /// will record the smallest size attained.
235 static const Value *getNoopInput(const Value *V,
236 SmallVectorImpl<unsigned> &ValLoc,
238 const TargetLoweringBase &TLI,
239 const DataLayout &DL) {
241 // Try to look through V1; if V1 is not an instruction, it can't be looked
243 const Instruction *I = dyn_cast<Instruction>(V);
244 if (!I || I->getNumOperands() == 0) return V;
245 const Value *NoopInput = nullptr;
247 Value *Op = I->getOperand(0);
248 if (isa<BitCastInst>(I)) {
249 // Look through truly no-op bitcasts.
250 if (isNoopBitcast(Op->getType(), I->getType(), TLI))
252 } else if (isa<GetElementPtrInst>(I)) {
253 // Look through getelementptr
254 if (cast<GetElementPtrInst>(I)->hasAllZeroIndices())
256 } else if (isa<IntToPtrInst>(I)) {
257 // Look through inttoptr.
258 // Make sure this isn't a truncating or extending cast. We could
259 // support this eventually, but don't bother for now.
260 if (!isa<VectorType>(I->getType()) &&
261 DL.getPointerSizeInBits() ==
262 cast<IntegerType>(Op->getType())->getBitWidth())
264 } else if (isa<PtrToIntInst>(I)) {
265 // Look through ptrtoint.
266 // Make sure this isn't a truncating or extending cast. We could
267 // support this eventually, but don't bother for now.
268 if (!isa<VectorType>(I->getType()) &&
269 DL.getPointerSizeInBits() ==
270 cast<IntegerType>(I->getType())->getBitWidth())
272 } else if (isa<TruncInst>(I) &&
273 TLI.allowTruncateForTailCall(Op->getType(), I->getType())) {
274 DataBits = std::min(DataBits, I->getType()->getPrimitiveSizeInBits());
276 } else if (isa<CallInst>(I)) {
277 // Look through call (skipping callee)
278 for (User::const_op_iterator i = I->op_begin(), e = I->op_end() - 1;
280 unsigned attrInd = i - I->op_begin() + 1;
281 if (cast<CallInst>(I)->paramHasAttr(attrInd, Attribute::Returned) &&
282 isNoopBitcast((*i)->getType(), I->getType(), TLI)) {
287 } else if (isa<InvokeInst>(I)) {
288 // Look through invoke (skipping BB, BB, Callee)
289 for (User::const_op_iterator i = I->op_begin(), e = I->op_end() - 3;
291 unsigned attrInd = i - I->op_begin() + 1;
292 if (cast<InvokeInst>(I)->paramHasAttr(attrInd, Attribute::Returned) &&
293 isNoopBitcast((*i)->getType(), I->getType(), TLI)) {
298 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(V)) {
299 // Value may come from either the aggregate or the scalar
300 ArrayRef<unsigned> InsertLoc = IVI->getIndices();
301 if (ValLoc.size() >= InsertLoc.size() &&
302 std::equal(InsertLoc.begin(), InsertLoc.end(), ValLoc.rbegin())) {
303 // The type being inserted is a nested sub-type of the aggregate; we
304 // have to remove those initial indices to get the location we're
305 // interested in for the operand.
306 ValLoc.resize(ValLoc.size() - InsertLoc.size());
307 NoopInput = IVI->getInsertedValueOperand();
309 // The struct we're inserting into has the value we're interested in, no
310 // change of address.
313 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
314 // The part we're interested in will inevitably be some sub-section of the
315 // previous aggregate. Combine the two paths to obtain the true address of
317 ArrayRef<unsigned> ExtractLoc = EVI->getIndices();
318 ValLoc.append(ExtractLoc.rbegin(), ExtractLoc.rend());
321 // Terminate if we couldn't find anything to look through.
329 /// Return true if this scalar return value only has bits discarded on its path
330 /// from the "tail call" to the "ret". This includes the obvious noop
331 /// instructions handled by getNoopInput above as well as free truncations (or
332 /// extensions prior to the call).
333 static bool slotOnlyDiscardsData(const Value *RetVal, const Value *CallVal,
334 SmallVectorImpl<unsigned> &RetIndices,
335 SmallVectorImpl<unsigned> &CallIndices,
336 bool AllowDifferingSizes,
337 const TargetLoweringBase &TLI,
338 const DataLayout &DL) {
340 // Trace the sub-value needed by the return value as far back up the graph as
341 // possible, in the hope that it will intersect with the value produced by the
342 // call. In the simple case with no "returned" attribute, the hope is actually
343 // that we end up back at the tail call instruction itself.
344 unsigned BitsRequired = UINT_MAX;
345 RetVal = getNoopInput(RetVal, RetIndices, BitsRequired, TLI, DL);
347 // If this slot in the value returned is undef, it doesn't matter what the
348 // call puts there, it'll be fine.
349 if (isa<UndefValue>(RetVal))
352 // Now do a similar search up through the graph to find where the value
353 // actually returned by the "tail call" comes from. In the simple case without
354 // a "returned" attribute, the search will be blocked immediately and the loop
356 unsigned BitsProvided = UINT_MAX;
357 CallVal = getNoopInput(CallVal, CallIndices, BitsProvided, TLI, DL);
359 // There's no hope if we can't actually trace them to (the same part of!) the
361 if (CallVal != RetVal || CallIndices != RetIndices)
364 // However, intervening truncates may have made the call non-tail. Make sure
365 // all the bits that are needed by the "ret" have been provided by the "tail
366 // call". FIXME: with sufficiently cunning bit-tracking, we could look through
368 if (BitsProvided < BitsRequired ||
369 (!AllowDifferingSizes && BitsProvided != BitsRequired))
375 /// For an aggregate type, determine whether a given index is within bounds or
377 static bool indexReallyValid(CompositeType *T, unsigned Idx) {
378 if (ArrayType *AT = dyn_cast<ArrayType>(T))
379 return Idx < AT->getNumElements();
381 return Idx < cast<StructType>(T)->getNumElements();
384 /// Move the given iterators to the next leaf type in depth first traversal.
386 /// Performs a depth-first traversal of the type as specified by its arguments,
387 /// stopping at the next leaf node (which may be a legitimate scalar type or an
388 /// empty struct or array).
390 /// @param SubTypes List of the partial components making up the type from
391 /// outermost to innermost non-empty aggregate. The element currently
392 /// represented is SubTypes.back()->getTypeAtIndex(Path.back() - 1).
394 /// @param Path Set of extractvalue indices leading from the outermost type
395 /// (SubTypes[0]) to the leaf node currently represented.
397 /// @returns true if a new type was found, false otherwise. Calling this
398 /// function again on a finished iterator will repeatedly return
399 /// false. SubTypes.back()->getTypeAtIndex(Path.back()) is either an empty
400 /// aggregate or a non-aggregate
401 static bool advanceToNextLeafType(SmallVectorImpl<CompositeType *> &SubTypes,
402 SmallVectorImpl<unsigned> &Path) {
403 // First march back up the tree until we can successfully increment one of the
404 // coordinates in Path.
405 while (!Path.empty() && !indexReallyValid(SubTypes.back(), Path.back() + 1)) {
410 // If we reached the top, then the iterator is done.
414 // We know there's *some* valid leaf now, so march back down the tree picking
415 // out the left-most element at each node.
417 Type *DeeperType = SubTypes.back()->getTypeAtIndex(Path.back());
418 while (DeeperType->isAggregateType()) {
419 CompositeType *CT = cast<CompositeType>(DeeperType);
420 if (!indexReallyValid(CT, 0))
423 SubTypes.push_back(CT);
426 DeeperType = CT->getTypeAtIndex(0U);
432 /// Find the first non-empty, scalar-like type in Next and setup the iterator
435 /// Assuming Next is an aggregate of some kind, this function will traverse the
436 /// tree from left to right (i.e. depth-first) looking for the first
437 /// non-aggregate type which will play a role in function return.
439 /// For example, if Next was {[0 x i64], {{}, i32, {}}, i32} then we would setup
440 /// Path as [1, 1] and SubTypes as [Next, {{}, i32, {}}] to represent the first
441 /// i32 in that type.
442 static bool firstRealType(Type *Next,
443 SmallVectorImpl<CompositeType *> &SubTypes,
444 SmallVectorImpl<unsigned> &Path) {
445 // First initialise the iterator components to the first "leaf" node
446 // (i.e. node with no valid sub-type at any index, so {} does count as a leaf
447 // despite nominally being an aggregate).
448 while (Next->isAggregateType() &&
449 indexReallyValid(cast<CompositeType>(Next), 0)) {
450 SubTypes.push_back(cast<CompositeType>(Next));
452 Next = cast<CompositeType>(Next)->getTypeAtIndex(0U);
455 // If there's no Path now, Next was originally scalar already (or empty
456 // leaf). We're done.
460 // Otherwise, use normal iteration to keep looking through the tree until we
461 // find a non-aggregate type.
462 while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType()) {
463 if (!advanceToNextLeafType(SubTypes, Path))
470 /// Set the iterator data-structures to the next non-empty, non-aggregate
472 static bool nextRealType(SmallVectorImpl<CompositeType *> &SubTypes,
473 SmallVectorImpl<unsigned> &Path) {
475 if (!advanceToNextLeafType(SubTypes, Path))
478 assert(!Path.empty() && "found a leaf but didn't set the path?");
479 } while (SubTypes.back()->getTypeAtIndex(Path.back())->isAggregateType());
485 /// Test if the given instruction is in a position to be optimized
486 /// with a tail-call. This roughly means that it's in a block with
487 /// a return and there's nothing that needs to be scheduled
488 /// between it and the return.
490 /// This function only tests target-independent requirements.
491 bool llvm::isInTailCallPosition(ImmutableCallSite CS, const TargetMachine &TM) {
492 const Instruction *I = CS.getInstruction();
493 const BasicBlock *ExitBB = I->getParent();
494 const TerminatorInst *Term = ExitBB->getTerminator();
495 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
497 // The block must end in a return statement or unreachable.
499 // FIXME: Decline tailcall if it's not guaranteed and if the block ends in
500 // an unreachable, for now. The way tailcall optimization is currently
501 // implemented means it will add an epilogue followed by a jump. That is
502 // not profitable. Also, if the callee is a special function (e.g.
503 // longjmp on x86), it can end up causing miscompilation that has not
504 // been fully understood.
506 (!TM.Options.GuaranteedTailCallOpt || !isa<UnreachableInst>(Term)))
509 // If I will have a chain, make sure no other instruction that will have a
510 // chain interposes between I and the return.
511 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
512 !isSafeToSpeculativelyExecute(I))
513 for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {
516 // Debug info intrinsics do not get in the way of tail call optimization.
517 if (isa<DbgInfoIntrinsic>(BBI))
519 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
520 !isSafeToSpeculativelyExecute(&*BBI))
524 const Function *F = ExitBB->getParent();
525 return returnTypeIsEligibleForTailCall(
526 F, I, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
529 bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
530 const Instruction *I,
531 const ReturnInst *Ret,
532 const TargetLoweringBase &TLI) {
533 // If the block ends with a void return or unreachable, it doesn't matter
534 // what the call's return type is.
535 if (!Ret || Ret->getNumOperands() == 0) return true;
537 // If the return value is undef, it doesn't matter what the call's
539 if (isa<UndefValue>(Ret->getOperand(0))) return true;
541 // Make sure the attributes attached to each return are compatible.
542 AttrBuilder CallerAttrs(F->getAttributes(),
543 AttributeSet::ReturnIndex);
544 AttrBuilder CalleeAttrs(cast<CallInst>(I)->getAttributes(),
545 AttributeSet::ReturnIndex);
547 // Noalias is completely benign as far as calling convention goes, it
548 // shouldn't affect whether the call is a tail call.
549 CallerAttrs = CallerAttrs.removeAttribute(Attribute::NoAlias);
550 CalleeAttrs = CalleeAttrs.removeAttribute(Attribute::NoAlias);
552 bool AllowDifferingSizes = true;
553 if (CallerAttrs.contains(Attribute::ZExt)) {
554 if (!CalleeAttrs.contains(Attribute::ZExt))
557 AllowDifferingSizes = false;
558 CallerAttrs.removeAttribute(Attribute::ZExt);
559 CalleeAttrs.removeAttribute(Attribute::ZExt);
560 } else if (CallerAttrs.contains(Attribute::SExt)) {
561 if (!CalleeAttrs.contains(Attribute::SExt))
564 AllowDifferingSizes = false;
565 CallerAttrs.removeAttribute(Attribute::SExt);
566 CalleeAttrs.removeAttribute(Attribute::SExt);
569 // If they're still different, there's some facet we don't understand
570 // (currently only "inreg", but in future who knows). It may be OK but the
571 // only safe option is to reject the tail call.
572 if (CallerAttrs != CalleeAttrs)
575 const Value *RetVal = Ret->getOperand(0), *CallVal = I;
576 SmallVector<unsigned, 4> RetPath, CallPath;
577 SmallVector<CompositeType *, 4> RetSubTypes, CallSubTypes;
579 bool RetEmpty = !firstRealType(RetVal->getType(), RetSubTypes, RetPath);
580 bool CallEmpty = !firstRealType(CallVal->getType(), CallSubTypes, CallPath);
582 // Nothing's actually returned, it doesn't matter what the callee put there
583 // it's a valid tail call.
587 // Iterate pairwise through each of the value types making up the tail call
588 // and the corresponding return. For each one we want to know whether it's
589 // essentially going directly from the tail call to the ret, via operations
590 // that end up not generating any code.
592 // We allow a certain amount of covariance here. For example it's permitted
593 // for the tail call to define more bits than the ret actually cares about
594 // (e.g. via a truncate).
597 // We've exhausted the values produced by the tail call instruction, the
598 // rest are essentially undef. The type doesn't really matter, but we need
600 Type *SlotType = RetSubTypes.back()->getTypeAtIndex(RetPath.back());
601 CallVal = UndefValue::get(SlotType);
604 // The manipulations performed when we're looking through an insertvalue or
605 // an extractvalue would happen at the front of the RetPath list, so since
606 // we have to copy it anyway it's more efficient to create a reversed copy.
607 SmallVector<unsigned, 4> TmpRetPath(RetPath.rbegin(), RetPath.rend());
608 SmallVector<unsigned, 4> TmpCallPath(CallPath.rbegin(), CallPath.rend());
610 // Finally, we can check whether the value produced by the tail call at this
611 // index is compatible with the value we return.
612 if (!slotOnlyDiscardsData(RetVal, CallVal, TmpRetPath, TmpCallPath,
613 AllowDifferingSizes, TLI,
614 F->getParent()->getDataLayout()))
617 CallEmpty = !nextRealType(CallSubTypes, CallPath);
618 } while(nextRealType(RetSubTypes, RetPath));
623 bool llvm::canBeOmittedFromSymbolTable(const GlobalValue *GV) {
624 if (!GV->hasLinkOnceODRLinkage())
627 if (GV->hasUnnamedAddr())
630 // If it is a non constant variable, it needs to be uniqued across shared
632 if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) {
633 if (!Var->isConstant())
637 // An alias can point to a variable. We could try to resolve the alias to
638 // decide, but for now just don't hide them.
639 if (isa<GlobalAlias>(GV))
643 if (GlobalStatus::analyzeGlobal(GV, GS))
646 return !GS.IsCompared;
649 static void collectFuncletMembers(
650 DenseMap<const MachineBasicBlock *, int> &FuncletMembership, int Funclet,
651 const MachineBasicBlock *MBB) {
652 // Don't revisit blocks.
653 if (FuncletMembership.count(MBB) > 0) {
654 if (FuncletMembership[MBB] != Funclet) {
655 assert(false && "MBB is part of two funclets!");
656 report_fatal_error("MBB is part of two funclets!");
661 // Add this MBB to our funclet.
662 FuncletMembership[MBB] = Funclet;
664 bool IsReturn = false;
665 int NumTerminators = 0;
666 for (const MachineInstr &MI : MBB->terminators()) {
667 IsReturn |= MI.isReturn();
670 assert((!IsReturn || NumTerminators == 1) &&
671 "Expected only one terminator when a return is present!");
673 // Returns are boundaries where funclet transfer can occur, don't follow
678 for (const MachineBasicBlock *SMBB : MBB->successors())
679 if (!SMBB->isEHPad())
680 collectFuncletMembers(FuncletMembership, Funclet, SMBB);
683 DenseMap<const MachineBasicBlock *, int>
684 llvm::getFuncletMembership(const MachineFunction &MF) {
685 DenseMap<const MachineBasicBlock *, int> FuncletMembership;
687 // We don't have anything to do if there aren't any EH pads.
688 if (!MF.getMMI().hasEHFunclets())
689 return FuncletMembership;
691 int EntryBBNumber = MF.front().getNumber();
692 bool IsSEH = isAsynchronousEHPersonality(
693 classifyEHPersonality(MF.getFunction()->getPersonalityFn()));
695 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
696 SmallVector<const MachineBasicBlock *, 16> FuncletBlocks;
697 SmallVector<const MachineBasicBlock *, 16> UnreachableBlocks;
698 SmallVector<const MachineBasicBlock *, 16> SEHCatchPads;
699 SmallVector<std::pair<const MachineBasicBlock *, int>, 16> CatchRetSuccessors;
700 for (const MachineBasicBlock &MBB : MF) {
701 if (MBB.isEHFuncletEntry()) {
702 FuncletBlocks.push_back(&MBB);
703 } else if (IsSEH && MBB.isEHPad()) {
704 SEHCatchPads.push_back(&MBB);
705 } else if (MBB.pred_empty()) {
706 UnreachableBlocks.push_back(&MBB);
709 MachineBasicBlock::const_iterator MBBI = MBB.getFirstTerminator();
710 // CatchPads are not funclets for SEH so do not consider CatchRet to
711 // transfer control to another funclet.
712 if (MBBI->getOpcode() != TII->getCatchReturnOpcode())
715 // FIXME: SEH CatchPads are not necessarily in the parent function:
716 // they could be inside a finally block.
717 const MachineBasicBlock *Successor = MBBI->getOperand(0).getMBB();
718 const MachineBasicBlock *SuccessorColor = MBBI->getOperand(1).getMBB();
719 CatchRetSuccessors.push_back(
720 {Successor, IsSEH ? EntryBBNumber : SuccessorColor->getNumber()});
723 // We don't have anything to do if there aren't any EH pads.
724 if (FuncletBlocks.empty())
725 return FuncletMembership;
727 // Identify all the basic blocks reachable from the function entry.
728 collectFuncletMembers(FuncletMembership, EntryBBNumber, &MF.front());
729 // All blocks not part of a funclet are in the parent function.
730 for (const MachineBasicBlock *MBB : UnreachableBlocks)
731 collectFuncletMembers(FuncletMembership, EntryBBNumber, MBB);
732 // Next, identify all the blocks inside the funclets.
733 for (const MachineBasicBlock *MBB : FuncletBlocks)
734 collectFuncletMembers(FuncletMembership, MBB->getNumber(), MBB);
735 // SEH CatchPads aren't really funclets, handle them separately.
736 for (const MachineBasicBlock *MBB : SEHCatchPads)
737 collectFuncletMembers(FuncletMembership, EntryBBNumber, MBB);
738 // Finally, identify all the targets of a catchret.
739 for (std::pair<const MachineBasicBlock *, int> CatchRetPair :
741 collectFuncletMembers(FuncletMembership, CatchRetPair.second,
743 return FuncletMembership;