llvm.org GIT mirror llvm / c9e3e3c
Don't IPO over functions that can be de-refined Summary: Fixes PR26774. If you're aware of the issue, feel free to skip the "Motivation" section and jump directly to "This patch". Motivation: I define "refinement" as discarding behaviors from a program that the optimizer has license to discard. So transforming: ``` void f(unsigned x) { unsigned t = 5 / x; (void)t; } ``` to ``` void f(unsigned x) { } ``` is refinement, since the behavior went from "if x == 0 then undefined else nothing" to "nothing" (the optimizer has license to discard undefined behavior). Refinement is a fundamental aspect of many mid-level optimizations done by LLVM. For instance, transforming `x == (x + 1)` to `false` also involves refinement since the expression's value went from "if x is `undef` then { `true` or `false` } else { `false` }" to "`false`" (by definition, the optimizer has license to fold `undef` to any non-`undef` value). Unfortunately, refinement implies that the optimizer cannot assume that the implementation of a function it can see has all of the behavior an unoptimized or a differently optimized version of the same function can have. This is a problem for functions with comdat linkage, where a function can be replaced by an unoptimized or a differently optimized version of the same source level function. For instance, FunctionAttrs cannot assume a comdat function is actually `readnone` even if it does not have any loads or stores in it; since there may have been loads and stores in the "original function" that were refined out in the currently visible variant, and at the link step the linker may in fact choose an implementation with a load or a store. As an example, consider a function that does two atomic loads from the same memory location, and writes to memory only if the two values are not equal. The optimizer is allowed to refine this function by first CSE'ing the two loads, and the folding the comparision to always report that the two values are equal. Such a refined variant will look like it is `readonly`. However, the unoptimized version of the function can still write to memory (since the two loads //can// result in different values), and selecting the unoptimized version at link time will retroactively invalidate transforms we may have done under the assumption that the function does not write to memory. Note: this is not just a problem with atomics or with linking differently optimized object files. See PR26774 for more realistic examples that involved neither. This patch: This change introduces a new set of linkage types, predicated as `GlobalValue::mayBeDerefined` that returns true if the linkage type allows a function to be replaced by a differently optimized variant at link time. It then changes a set of IPO passes to bail out if they see such a function. Reviewers: chandlerc, hfinkel, dexonsmith, joker.eph, rnk Subscribers: mcrosier, llvm-commits Differential Revision: http://reviews.llvm.org/D18634 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265762 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 3 years ago
32 changed file(s) with 336 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
9595 void destroyConstantImpl();
9696 Value *handleOperandChangeImpl(Value *From, Value *To);
9797
98 /// Returns true if the definition of this global may be replaced by a
99 /// differently optimized variant of the same source level function at link
100 /// time.
101 bool mayBeDerefined() const {
102 switch (getLinkage()) {
103 case WeakODRLinkage:
104 case LinkOnceODRLinkage:
105 case AvailableExternallyLinkage:
106 return true;
107
108 case WeakAnyLinkage:
109 case LinkOnceAnyLinkage:
110 case CommonLinkage:
111 case ExternalWeakLinkage:
112 case ExternalLinkage:
113 case AppendingLinkage:
114 case InternalLinkage:
115 case PrivateLinkage:
116 return mayBeOverridden();
117 }
118
119 llvm_unreachable("Fully covered switch above!");
120 }
121
122 /// Whether the definition of this global may be replaced by something
123 /// non-equivalent at link time. For example, if a function has weak linkage
124 /// then the code defining it may be replaced by different code.
125 bool mayBeOverridden() const {
126 switch (getLinkage()) {
127 case WeakAnyLinkage:
128 case LinkOnceAnyLinkage:
129 case CommonLinkage:
130 case ExternalWeakLinkage:
131 return true;
132
133 case AvailableExternallyLinkage:
134 case LinkOnceODRLinkage:
135 case WeakODRLinkage:
136 // The above three cannot be overridden but can be de-refined.
137
138 case ExternalLinkage:
139 case AppendingLinkage:
140 case InternalLinkage:
141 case PrivateLinkage:
142 return false;
143 }
144
145 llvm_unreachable("Fully covered switch above!");
146 }
147
98148 protected:
99149 /// \brief The intrinsic ID for this subclass (which must be a Function).
100150 ///
241291 isAvailableExternallyLinkage(Linkage);
242292 }
243293
244 /// Whether the definition of this global may be replaced by something
245 /// non-equivalent at link time. For example, if a function has weak linkage
246 /// then the code defining it may be replaced by different code.
247 static bool mayBeOverridden(LinkageTypes Linkage) {
248 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
249 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
250 }
251
252294 /// Whether the definition of this global may be replaced at link time. NB:
253295 /// Using this method outside of the code generators is almost always a
254296 /// mistake: when working at the IR level use mayBeOverridden instead as it
258300 Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage ||
259301 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
260302 }
303
304 /// Return true if the currently visible definition of this global (if any) is
305 /// exactly the definition we will see at runtime.
306 ///
307 /// Non-exact linkage types inhibits most non-inlining IPO, since a
308 /// differently optimized variant of the same function can have different
309 /// observable or undefined behavior than in the variant currently visible.
310 /// For instance, we could have started with
311 ///
312 /// void foo(int *v) {
313 /// int t = 5 / v[0];
314 /// (void) t;
315 /// }
316 ///
317 /// and "refined" it to
318 ///
319 /// void foo(int *v) { }
320 ///
321 /// However, we cannot infer readnone for `foo`, since that would justify
322 /// DSE'ing a store to `v[0]` across a call to `foo`, which can cause
323 /// undefined behavior if the linker replaces the actual call destination with
324 /// the unoptimized `foo`.
325 ///
326 /// Inlining is okay across non-exact linkage types as long as they're not
327 /// interposable (see \c isInterposable), since in such cases the currently
328 /// visible variant is *a* correct implementation of the original source
329 /// function; it just isn't the *only* correct implementation.
330 bool isDefinitionExact() const {
331 return !mayBeDerefined();
332 }
333
334 /// Return true if this global has an exact defintion.
335 bool hasExactDefinition() const {
336 // While this computes exactly the same thing as
337 // isStrongDefinitionForLinker, the intended uses are different. This
338 // function is intended to help decide if specific inter-procedural
339 // transforms are correct, while isStrongDefinitionForLinker's intended use
340 // is in low level code generation.
341 return !isDeclaration() && isDefinitionExact();
342 }
343
344 /// Return true if this global's definition can be substituted with an
345 /// *arbitrary* definition at link time. We cannot do any IPO or inlinining
346 /// across interposable call edges, since the callee can be replaced with
347 /// something arbitrary at link time.
348 bool isInterposable() const { return mayBeOverridden(); }
261349
262350 bool hasExternalLinkage() const { return isExternalLinkage(getLinkage()); }
263351 bool hasAvailableExternallyLinkage() const {
290378 return isDiscardableIfUnused(getLinkage());
291379 }
292380
293 bool mayBeOverridden() const { return mayBeOverridden(getLinkage()); }
294
295381 bool isWeakForLinker() const { return isWeakForLinker(getLinkage()); }
296382
297383 /// Copy all additional attributes (those not needed to create a GlobalValue)
364450
365451 /// Returns true if this global's definition will be the one chosen by the
366452 /// linker.
453 ///
454 /// NB! Ideally this should not be used at the IR level at all. If you're
455 /// interested in optimization constraints implied by the linker's ability to
456 /// choose an implementation, prefer using \c hasExactDefinition.
367457 bool isStrongDefinitionForLinker() const {
368458 return !(isDeclarationForLinker() || isWeakForLinker());
369459 }
9393 /// unique.
9494 inline bool hasDefinitiveInitializer() const {
9595 return hasInitializer() &&
96 // The initializer of a global variable with weak linkage may change at
97 // link time.
98 !mayBeOverridden() &&
96 // The initializer of a global variable may change to something arbitrary
97 // at link time.
98 !isInterposable() &&
9999 // The initializer of a global variable with the externally_initialized
100100 // marker may change at runtime before C++ initializers are evaluated.
101101 !isExternallyInitialized();
358358 if (!Op) {
359359 // The only non-operator case we can handle are GlobalAliases.
360360 if (const GlobalAlias *GA = dyn_cast(V)) {
361 if (!GA->mayBeOverridden()) {
361 if (!GA->isInterposable()) {
362362 V = GA->getAliasee();
363363 continue;
364364 }
530530 return GV->getInitializer();
531531
532532 if (auto *GA = dyn_cast(C))
533 if (GA->getAliasee() && !GA->mayBeOverridden())
533 if (GA->getAliasee() && !GA->isInterposable())
534534 return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
535535
536536 // If the loaded value isn't a constant expr, we can't handle it.
470470 const std::vector &SCC = *I;
471471 assert(!SCC.empty() && "SCC with no functions?");
472472
473 if (!SCC[0]->getFunction() || SCC[0]->getFunction()->mayBeOverridden()) {
474 // Calls externally or is weak - can't say anything useful. Remove any existing
475 // function records (may have been created when scanning globals).
473 if (!SCC[0]->getFunction() || !SCC[0]->getFunction()->isDefinitionExact()) {
474 // Calls externally or not exact - can't say anything useful. Remove any
475 // existing function records (may have been created when scanning
476 // globals).
476477 for (auto *Node : SCC)
477478 FunctionInfos.erase(Node->getFunction());
478479 continue;
698699 auto *InputGVar = dyn_cast(InputGV);
699700 if (GVar && InputGVar &&
700701 !GVar->isDeclaration() && !InputGVar->isDeclaration() &&
701 !GVar->mayBeOverridden() && !InputGVar->mayBeOverridden()) {
702 !GVar->isInterposable() && !InputGVar->isInterposable()) {
702703 Type *GVType = GVar->getInitializer()->getType();
703704 Type *InputGVType = InputGVar->getInitializer()->getType();
704705 if (GVType->isSized() && InputGVType->isSized() &&
11241124 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
11251125 V = cast(V)->getOperand(0);
11261126 } else if (GlobalAlias *GA = dyn_cast(V)) {
1127 if (GA->mayBeOverridden())
1127 if (GA->isInterposable())
11281128 break;
11291129 V = GA->getAliasee();
11301130 } else {
14761476 if (CS.getCaller()->hasFnAttribute(Attribute::OptimizeNone))
14771477 return llvm::InlineCost::getNever();
14781478
1479 // Don't inline functions which can be redefined at link-time to mean
1480 // something else. Don't inline functions marked noinline or call sites
1481 // marked noinline.
1482 if (Callee->mayBeOverridden() ||
1479 // Don't inline functions which can be interposed at link-time. Don't inline
1480 // functions marked noinline or call sites marked noinline.
1481 // Note: inlining non-exact non-interposable fucntions is fine, since we know
1482 // we have *a* correct implementation of the source level function.
1483 if (Callee->isInterposable() ||
14831484 Callee->hasFnAttribute(Attribute::NoInline) || CS.isNoInline())
14841485 return llvm::InlineCost::getNever();
14851486
615615 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
616616 V = cast(V)->getOperand(0);
617617 } else if (GlobalAlias *GA = dyn_cast(V)) {
618 if (GA->mayBeOverridden())
618 if (GA->isInterposable())
619619 break;
620620 V = GA->getAliasee();
621621 } else {
298298 BaseAlign = AI->getAlignment();
299299 } else if (const GlobalVariable *GV = dyn_cast(Base)) {
300300 // Global variables are not necessarily safe to load from if they are
301 // overridden. Their size may change or they may be weak and require a test
302 // to determine if they were in fact provided.
303 if (!GV->mayBeOverridden()) {
301 // interposed arbitrarily. Their size may change or they may be weak and
302 // require a test to determine if they were in fact provided.
303 if (!GV->isInterposable()) {
304304 BaseType = GV->getType()->getElementType();
305305 BaseAlign = GV->getAlignment();
306306 }
528528 }
529529
530530 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
531 if (GA.mayBeOverridden())
531 if (GA.isInterposable())
532532 return unknown();
533533 return compute(GA.getAliasee());
534534 }
48274827 else if (isa(V))
48284828 return getZero(V->getType());
48294829 else if (GlobalAlias *GA = dyn_cast(V))
4830 return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee());
4830 return GA->isInterposable() ? getUnknown(V) : getSCEV(GA->getAliasee());
48314831 else if (!isa(V))
48324832 return getUnknown(V);
48334833
14491449 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
14501450 // the bits of its aliasee.
14511451 if (GlobalAlias *GA = dyn_cast(V)) {
1452 if (!GA->mayBeOverridden())
1452 if (!GA->isInterposable())
14531453 computeKnownBits(GA->getAliasee(), KnownZero, KnownOne, Depth + 1, Q);
14541454 return;
14551455 }
26392639 Operator::getOpcode(Ptr) == Instruction::AddrSpaceCast) {
26402640 Ptr = cast(Ptr)->getOperand(0);
26412641 } else if (GlobalAlias *GA = dyn_cast(Ptr)) {
2642 if (GA->mayBeOverridden())
2642 if (GA->isInterposable())
26432643 break;
26442644 Ptr = GA->getAliasee();
26452645 } else {
28352835 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
28362836 V = cast(V)->getOperand(0);
28372837 } else if (GlobalAlias *GA = dyn_cast(V)) {
2838 if (GA->mayBeOverridden())
2838 if (GA->isInterposable())
28392839 return V;
28402840 V = GA->getAliasee();
28412841 } else {
459459 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
460460 V = cast(V)->getOperand(0);
461461 } else if (GlobalAlias *GA = dyn_cast(V)) {
462 if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden())
462 if (StripKind == PSK_ZeroIndices || GA->isInterposable())
463463 return V;
464464 V = GA->getAliasee();
465465 } else {
625625 if (const auto *GA2 = dyn_cast(GV)) {
626626 Assert(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
627627
628 Assert(!GA2->mayBeOverridden(), "Alias cannot point to a weak alias",
628 Assert(!GA2->isInterposable(), "Alias cannot point to an interposable alias",
629629 &GA);
630630 } else {
631631 // Only continue verifying subexpressions of GlobalAliases.
328328 // %v = load i32 %p
329329 // ret void
330330 // }
331 if (!Fn.isStrongDefinitionForLinker())
331 if (!Fn.hasExactDefinition())
332332 return false;
333333
334334 // Functions with local linkage should already have been handled, except the
6868 // Already perfect!
6969 return MAK_ReadNone;
7070
71 // Definitions with weak linkage may be overridden at linktime with
72 // something that writes memory, so treat them like declarations.
73 if (F.isDeclaration() || F.mayBeOverridden()) {
71 // Non-exact function definitions may not be selected at link time, and an
72 // alternative version that writes to memory may be selected. See the comment
73 // on GlobalValue::isDefinitionExact for more details.
74 if (!F.hasExactDefinition()) {
7475 if (AliasAnalysis::onlyReadsMemory(MRB))
7576 return MAK_ReadOnly;
7677
283284 }
284285
285286 Function *F = CS.getCalledFunction();
286 if (!F || F->isDeclaration() || F->mayBeOverridden() ||
287 !SCCNodes.count(F)) {
287 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
288288 Captured = true;
289289 return true;
290290 }
489489 // Check each function in turn, determining which pointer arguments are not
490490 // captured.
491491 for (Function *F : SCCNodes) {
492 // Definitions with weak linkage may be overridden at linktime with
493 // something that captures pointers, so treat them like declarations.
494 if (F->isDeclaration() || F->mayBeOverridden())
492 // We can infer and propagate function attributes only when we know that the
493 // definition we'll get at link time is *exactly* the definition we see now.
494 // For more details, see GlobalValue::mayBeDerefined.
495 if (!F->hasExactDefinition())
495496 continue;
496497
497498 // Functions that are readonly (or readnone) and nounwind and don't return
744745 if (F->doesNotAlias(0))
745746 continue;
746747
747 // Definitions with weak linkage may be overridden at linktime, so
748 // treat them like declarations.
749 if (F->isDeclaration() || F->mayBeOverridden())
748 // We can infer and propagate function attributes only when we know that the
749 // definition we'll get at link time is *exactly* the definition we see now.
750 // For more details, see GlobalValue::mayBeDerefined.
751 if (!F->hasExactDefinition())
750752 return false;
751753
752754 // We annotate noalias return values, which are only applicable to
858860 Attribute::NonNull))
859861 continue;
860862
861 // Definitions with weak linkage may be overridden at linktime, so
862 // treat them like declarations.
863 if (F->isDeclaration() || F->mayBeOverridden())
863 // We can infer and propagate function attributes only when we know that the
864 // definition we'll get at link time is *exactly* the definition we see now.
865 // For more details, see GlobalValue::mayBeDerefined.
866 if (!F->hasExactDefinition())
864867 return false;
865868
866869 // We annotate nonnull return values, which are only applicable to
23652365 }
23662366
23672367 // If the aliasee may change at link time, nothing can be done - bail out.
2368 if (J->mayBeOverridden())
2368 if (J->isInterposable())
23692369 continue;
23702370
23712371 Constant *Aliasee = J->getAliasee();
160160 if (F.getReturnType()->isVoidTy())
161161 return false; // No return value.
162162
163 // If this function could be overridden later in the link stage, we can't
164 // propagate information about its results into callers.
165 if (F.mayBeOverridden())
163 // We can infer and propagate the return value only when we know that the
164 // definition we'll get at link time is *exactly* the definition we see now.
165 // For more details, see GlobalValue::mayBeDerefined.
166 if (!F.isDefinitionExact())
166167 return false;
167168
168169 // Check to see if this function returns a constant.
15711571 if (!*I) continue;
15721572 Function *F = cast(*I);
15731573 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
1574 !F->mayBeOverridden()) {
1574 !F->isInterposable()) {
15751575 Changed |= insert(F);
15761576 }
15771577 }
15851585 if (!*I) continue;
15861586 Function *F = cast(*I);
15871587 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage() &&
1588 F->mayBeOverridden()) {
1588 F->isInterposable()) {
15891589 Changed |= insert(F);
15901590 }
15911591 }
16821682 // Replace G with a simple tail call to bitcast(F). Also replace direct uses
16831683 // of G with bitcast(F). Deletes G.
16841684 void MergeFunctions::writeThunk(Function *F, Function *G) {
1685 if (!G->mayBeOverridden()) {
1685 if (!G->isInterposable()) {
16861686 // Redirect direct callers of G to F.
16871687 replaceDirectCallers(G, F);
16881688 }
17431743
17441744 // Merge two equivalent functions. Upon completion, Function G is deleted.
17451745 void MergeFunctions::mergeTwoFunctions(Function *F, Function *G) {
1746 if (F->mayBeOverridden()) {
1747 assert(G->mayBeOverridden());
1746 if (F->isInterposable()) {
1747 assert(G->isInterposable());
17481748
17491749 // Make them both thunks to the same internal function.
17501750 Function *H = Function::Create(F->getFunctionType(), F->getLinkage(), "",
18271827 //
18281828 // When one function is weak and the other is strong there is an order imposed
18291829 // already. We process strong functions before weak functions.
1830 if ((OldF.getFunc()->mayBeOverridden() && NewFunction->mayBeOverridden()) ||
1831 (!OldF.getFunc()->mayBeOverridden() && !NewFunction->mayBeOverridden()))
1830 if ((OldF.getFunc()->isInterposable() && NewFunction->isInterposable()) ||
1831 (!OldF.getFunc()->isInterposable() && !NewFunction->isInterposable()))
18321832 if (OldF.getFunc()->getName() > NewFunction->getName()) {
18331833 // Swap the two functions.
18341834 Function *F = OldF.getFunc();
18381838 }
18391839
18401840 // Never thunk a strong function to a weak function.
1841 assert(!OldF.getFunc()->mayBeOverridden() || NewFunction->mayBeOverridden());
1841 assert(!OldF.getFunc()->isInterposable() || NewFunction->isInterposable());
18421842
18431843 DEBUG(dbgs() << " " << OldF.getFunc()->getName()
18441844 << " == " << NewFunction->getName() << '\n');
9292 if (!F) {
9393 SCCMightUnwind = true;
9494 SCCMightReturn = true;
95 } else if (F->isDeclaration() || F->mayBeOverridden()) {
95 } else if (F->isDeclaration() || F->isInterposable()) {
96 // Note: isInterposable (as opposed to hasExactDefinition) is fine above,
97 // since we're not inferring new attributes here, but only using existing,
98 // assumed to be correct, function attributes.
9699 SCCMightUnwind |= !F->doesNotThrow();
97100 SCCMightReturn |= !F->doesNotReturn();
98101 } else {
639639 }
640640
641641 if (GlobalAlias *GA = dyn_cast(P)) {
642 if (GA->mayBeOverridden())
642 if (!GA->isInterposable())
643643 return false;
644644 Worklist.push_back(GA->getAliasee());
645645 continue;
6969 /// possibly produce autoreleases.
7070 bool ObjCARCAPElim::MayAutorelease(ImmutableCallSite CS, unsigned Depth) {
7171 if (const Function *Callee = CS.getCalledFunction()) {
72 if (Callee->isDeclaration() || Callee->mayBeOverridden())
72 if (!Callee->hasExactDefinition())
7373 return true;
7474 for (const BasicBlock &BB : *Callee) {
7575 for (const Instruction &I : BB)
604604 cast(Arg)->hasAllZeroIndices())
605605 Arg = cast(Arg)->getPointerOperand();
606606 else if (isa(Arg) &&
607 !cast(Arg)->mayBeOverridden())
607 !cast(Arg)->isInterposable())
608608 Arg = cast(Arg)->getAliasee();
609609 else
610610 break;
17231723 if (F->isDeclaration())
17241724 continue;
17251725
1726 // If this is a strong or ODR definition of this function, then we can
1727 // propagate information about its result into callsites of it.
1728 if (!F->mayBeOverridden())
1726 // If this is an exact definition of this function, then we can propagate
1727 // information about its result into callsites of it.
1728 if (F->hasExactDefinition())
17291729 Solver.AddTrackedFunction(&*F);
17301730
17311731 // If this function only has direct calls that we can see, we can track its
15481548 if (Operator::getOpcode(Ptr) == Instruction::BitCast) {
15491549 Ptr = cast(Ptr)->getOperand(0);
15501550 } else if (GlobalAlias *GA = dyn_cast(Ptr)) {
1551 if (GA->mayBeOverridden())
1551 if (GA->isInterposable())
15521552 break;
15531553 Ptr = GA->getAliasee();
15541554 } else {
426426
427427 // Resolve function pointers.
428428 Function *Callee = dyn_cast(getVal(CS.getCalledValue()));
429 if (!Callee || Callee->mayBeOverridden()) {
429 if (!Callee || Callee->isInterposable()) {
430430 DEBUG(dbgs() << "Can not resolve function pointer.\n");
431431 return false; // Cannot resolve.
432432 }
0 ; RUN: opt < %s -basicaa -globals-aa -gvn -S | FileCheck %s
1
2 ; See PR26774
3
4 @X = internal global i32 4
5
6 define i32 @test(i32* %P) {
7 ; CHECK: @test
8 ; CHECK-NEXT: store i32 12, i32* @X
9 ; CHECK-NEXT: call void @doesnotmodX()
10 ; CHECK-NEXT: %V = load i32, i32* @X
11 ; CHECK-NEXT: ret i32 %V
12 store i32 12, i32* @X
13 call void @doesnotmodX( )
14 %V = load i32, i32* @X
15 ret i32 %V
16 }
17
18 define linkonce_odr void @doesnotmodX() {
19 ret void
20 }
0 ; RUN: opt < %s -functionattrs -S | FileCheck %s
1
2 ; See PR26774
3
4 ; CHECK-LABEL: define void @bar(i8* readonly) {
5 define void @bar(i8* readonly) {
6 call void @foo(i8* %0)
7 ret void
8 }
9
10
11 ; CHECK-LABEL: define linkonce_odr void @foo(i8* readonly) {
12 define linkonce_odr void @foo(i8* readonly) {
13 call void @bar(i8* %0)
14 ret void
15 }
0 ; RUN: opt < %s -ipconstprop -S | FileCheck %s
1
2 ; See PR26774
3
4 define i32 @baz() {
5 ret i32 10
6 }
7
8 ; We can const-prop @baz's return value *into* @foo, but cannot
9 ; constprop @foo's return value into bar.
10
11 define linkonce_odr i32 @foo() {
12 ; CHECK-LABEL: @foo(
13 ; CHECK-NEXT: %val = call i32 @baz()
14 ; CHECK-NEXT: ret i32 10
15
16 %val = call i32 @baz()
17 ret i32 %val
18 }
19
20 define i32 @bar() {
21 ; CHECK-LABEL: @bar(
22 ; CHECK-NEXT: %val = call i32 @foo()
23 ; CHECK-NEXT: ret i32 %val
24
25 %val = call i32 @foo()
26 ret i32 %val
27 }
0 ; RUN: opt -inline -S < %s | FileCheck %s
1
2 define i32 @caller() {
3 ; CHECK-LABEL: @caller(
4 ; CHECK-NEXT: %val2 = call i32 @linkonce_callee(i32 42)
5 ; CHECK-NEXT: ret i32 %val2
6
7 %val = call i32 @odr_callee()
8 %val2 = call i32 @linkonce_callee(i32 %val);
9 ret i32 %val2
10 }
11
12 define linkonce_odr i32 @odr_callee() {
13 ret i32 42
14 }
15
16 define linkonce i32 @linkonce_callee(i32 %val) {
17 ret i32 %val
18 }
0 ; RUN: opt -S -objc-arc-apelim < %s | FileCheck %s
1
2 ; See PR26774
3
4 @llvm.global_ctors = appending global [2 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_x }, { i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_y }]
5
6 @x = global i32 0
7
8 declare i32 @bar() nounwind
9
10 define linkonce_odr i32 @foo() nounwind {
11 entry:
12 ret i32 5
13 }
14
15 define internal void @__cxx_global_var_init() {
16 entry:
17 %call = call i32 @foo()
18 store i32 %call, i32* @x, align 4
19 ret void
20 }
21
22 define internal void @__dxx_global_var_init() {
23 entry:
24 %call = call i32 @bar()
25 store i32 %call, i32* @x, align 4
26 ret void
27 }
28
29 ; CHECK-LABEL: define internal void @_GLOBAL__I_x() {
30 define internal void @_GLOBAL__I_x() {
31 entry:
32 ; CHECK: call i8* @objc_autoreleasePoolPush()
33 ; CHECK-NEXT: call void @__cxx_global_var_init()
34 ; CHECK-NEXT: call void @objc_autoreleasePoolPop(i8* %0)
35 ; CHECK-NEXT: ret void
36
37 %0 = call i8* @objc_autoreleasePoolPush() nounwind
38 call void @__cxx_global_var_init()
39 call void @objc_autoreleasePoolPop(i8* %0) nounwind
40 ret void
41 }
42
43 define internal void @_GLOBAL__I_y() {
44 entry:
45 %0 = call i8* @objc_autoreleasePoolPush() nounwind
46 call void @__dxx_global_var_init()
47 call void @objc_autoreleasePoolPop(i8* %0) nounwind
48 ret void
49 }
50
51 declare i8* @objc_autoreleasePoolPush()
52 declare void @objc_autoreleasePoolPop(i8*)
0 ; RUN: opt < %s -ipsccp -S | FileCheck %s
1
2 ; See PR26774
3
4 define i32 @baz() {
5 ret i32 10
6 }
7
8 ; We can const-prop @baz's return value *into* @foo, but cannot
9 ; constprop @foo's return value into bar.
10
11 define linkonce_odr i32 @foo() {
12 ; CHECK-LABEL: @foo(
13 ; CHECK-NEXT: %val = call i32 @baz()
14 ; CHECK-NEXT: ret i32 10
15
16 %val = call i32 @baz()
17 ret i32 %val
18 }
19
20 define i32 @bar() {
21 ; CHECK-LABEL: @bar(
22 ; CHECK-NEXT: %val = call i32 @foo()
23 ; CHECK-NEXT: ret i32 %val
24
25 %val = call i32 @foo()
26 ret i32 %val
27 }
2828 @test3_a = global i32 42
2929 @test3_b = weak alias i32, i32* @test3_a
3030 @test3_c = alias i32, i32* @test3_b
31 ; CHECK: Alias cannot point to a weak alias
31 ; CHECK: Alias cannot point to an interposable alias
3232 ; CHECK-NEXT: i32* @test3_c