llvm.org GIT mirror llvm / c6da686
llvm: Add support for "-fno-delete-null-pointer-checks" Summary: Support for this option is needed for building Linux kernel. This is a very frequently requested feature by kernel developers. More details : https://lkml.org/lkml/2018/4/4/601 GCC option description for -fdelete-null-pointer-checks: This Assume that programs cannot safely dereference null pointers, and that no code or data element resides at address zero. -fno-delete-null-pointer-checks is the inverse of this implying that null pointer dereferencing is not undefined. This feature is implemented in LLVM IR in this CL as the function attribute "null-pointer-is-valid"="true" in IR (Under review at D47894). The CL updates several passes that assumed null pointer dereferencing is undefined to not optimize when the "null-pointer-is-valid"="true" attribute is present. Reviewers: t.p.northover, efriedma, jyknight, chandlerc, rnk, srhines, void, george.burgess.iv Reviewed By: efriedma, george.burgess.iv Subscribers: eraman, haicheng, george.burgess.iv, drinkcat, theraven, reames, sanjoy, xbolva00, llvm-commits Differential Revision: https://reviews.llvm.org/D47895 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@336613 91177308-0d34-0410-b5e6-96231b3b80d8 Manoj Gupta 1 year, 2 months ago
66 changed file(s) with 1689 addition(s) and 87 deletion(s). Raw diff Collapse all Expand all
14601460 trap or generate asynchronous exceptions. Exception handling schemes
14611461 that are recognized by LLVM to handle asynchronous exceptions, such
14621462 as SEH, will still provide their implementation defined semantics.
1463 ``"null-pointer-is-valid"``
1464 If ``"null-pointer-is-valid"`` is set to ``"true"``, then ``null`` address
1465 in address-space 0 is considered to be a valid address for memory loads and
1466 stores. Any analysis or optimization should not treat dereferencing a
1467 pointer to ``null`` as undefined behavior in this function.
1468 Note: Comparing address of a global variable to ``null`` may still
1469 evaluate to false because of a limitation in querying this attribute inside
1470 constant expressions.
14631471 ``optforfuzzing``
14641472 This attribute indicates that this function should be optimized
14651473 for maximum fuzzing signal.
5454 friend AAResultBase;
5555
5656 const DataLayout &DL;
57 const Function &F;
5758 const TargetLibraryInfo &TLI;
5859 AssumptionCache ∾
5960 DominatorTree *DT;
6061 LoopInfo *LI;
6162
6263 public:
63 BasicAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI,
64 AssumptionCache &AC, DominatorTree *DT = nullptr,
65 LoopInfo *LI = nullptr)
66 : AAResultBase(), DL(DL), TLI(TLI), AC(AC), DT(DT), LI(LI) {}
64 BasicAAResult(const DataLayout &DL, const Function &F,
65 const TargetLibraryInfo &TLI, AssumptionCache &AC,
66 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr)
67 : AAResultBase(), DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), LI(LI) {}
6768
6869 BasicAAResult(const BasicAAResult &Arg)
69 : AAResultBase(Arg), DL(Arg.DL), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
70 LI(Arg.LI) {}
70 : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
71 DT(Arg.DT), LI(Arg.LI) {}
7172 BasicAAResult(BasicAAResult &&Arg)
72 : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI), AC(Arg.AC),
73 DT(Arg.DT), LI(Arg.LI) {}
73 : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
74 AC(Arg.AC), DT(Arg.DT), LI(Arg.LI) {}
7475
7576 /// Handle invalidation events in the new pass manager.
76 bool invalidate(Function &F, const PreservedAnalyses &PA,
77 bool invalidate(Function &Fn, const PreservedAnalyses &PA,
7778 FunctionAnalysisManager::Invalidator &Inv);
7879
7980 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
9394
9495 /// Returns the behavior when calling the given function. For use when the
9596 /// call site is not known.
96 FunctionModRefBehavior getModRefBehavior(const Function *F);
97 FunctionModRefBehavior getModRefBehavior(const Function *Fn);
9798
9899 private:
99100 // A linear transformation of a Value; this class represents ZExt(SExt(V,
636636 if (hasRetAttr(Attribute::NonNull))
637637 return true;
638638 else if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 &&
639 getType()->getPointerAddressSpace() == 0)
639 !NullPointerIsDefined(getCaller(),
640 getType()->getPointerAddressSpace()))
640641 return true;
641642
642643 return false;
780780 /// Returns true if we should emit debug info for profiling.
781781 bool isDebugInfoForProfiling() const;
782782
783 /// Check if null pointer dereferencing is considered undefined behavior for
784 /// the function.
785 /// Return value: false => null pointer dereference is undefined.
786 /// Return value: true => null pointer dereference is not undefined.
787 bool nullPointerIsDefined() const;
788
783789 private:
784790 void allocHungoffUselist();
785791 template void setHungoffOperand(Constant *C);
792798 void setValueSubclassDataBit(unsigned Bit, bool On);
793799 };
794800
801 /// Check whether null pointer dereferencing is considered undefined behavior
802 /// for a given function or an address space.
803 /// Null pointer access in non-zero address space is not considered undefined.
804 /// Return value: false => null pointer dereference is undefined.
805 /// Return value: true => null pointer dereference is not undefined.
806 bool NullPointerIsDefined(const Function *F, unsigned AS = 0);
807
795808 template <>
796809 struct OperandTraits : public HungoffOperandTraits<3> {};
797810
8484 // depth otherwise the algorithm in aliasGEP will assert.
8585 static const unsigned MaxLookupSearchDepth = 6;
8686
87 bool BasicAAResult::invalidate(Function &F, const PreservedAnalyses &PA,
87 bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
8888 FunctionAnalysisManager::Invalidator &Inv) {
8989 // We don't care if this analysis itself is preserved, it has no state. But
9090 // we need to check that the analyses it depends on have been. Note that we
9191 // may be created without handles to some analyses and in that case don't
9292 // depend on them.
93 if (Inv.invalidate(F, PA) ||
94 (DT && Inv.invalidate(F, PA)) ||
95 (LI && Inv.invalidate(F, PA)))
93 if (Inv.invalidate(Fn, PA) ||
94 (DT && Inv.invalidate(Fn, PA)) ||
95 (LI && Inv.invalidate(Fn, PA)))
9696 return true;
9797
9898 // Otherwise this analysis result remains valid.
149149 /// Returns the size of the object specified by V or UnknownSize if unknown.
150150 static uint64_t getObjectSize(const Value *V, const DataLayout &DL,
151151 const TargetLibraryInfo &TLI,
152 bool NullIsValidLoc,
152153 bool RoundToAlign = false) {
153154 uint64_t Size;
154155 ObjectSizeOpts Opts;
155156 Opts.RoundToAlign = RoundToAlign;
157 Opts.NullIsUnknownSize = NullIsValidLoc;
156158 if (getObjectSize(V, Size, DL, &TLI, Opts))
157159 return Size;
158160 return MemoryLocation::UnknownSize;
162164 /// Size.
163165 static bool isObjectSmallerThan(const Value *V, uint64_t Size,
164166 const DataLayout &DL,
165 const TargetLibraryInfo &TLI) {
167 const TargetLibraryInfo &TLI,
168 bool NullIsValidLoc) {
166169 // Note that the meanings of the "object" are slightly different in the
167170 // following contexts:
168171 // c1: llvm::getObjectSize()
194197
195198 // This function needs to use the aligned object size because we allow
196199 // reads a bit past the end given sufficient alignment.
197 uint64_t ObjectSize = getObjectSize(V, DL, TLI, /*RoundToAlign*/ true);
200 uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc,
201 /*RoundToAlign*/ true);
198202
199203 return ObjectSize != MemoryLocation::UnknownSize && ObjectSize < Size;
200204 }
201205
202206 /// Returns true if we can prove that the object specified by V has size Size.
203207 static bool isObjectSize(const Value *V, uint64_t Size, const DataLayout &DL,
204 const TargetLibraryInfo &TLI) {
205 uint64_t ObjectSize = getObjectSize(V, DL, TLI);
208 const TargetLibraryInfo &TLI, bool NullIsValidLoc) {
209 uint64_t ObjectSize = getObjectSize(V, DL, TLI, NullIsValidLoc);
206210 return ObjectSize != MemoryLocation::UnknownSize && ObjectSize == Size;
207211 }
208212
16221626 // Null values in the default address space don't point to any object, so they
16231627 // don't alias any other pointer.
16241628 if (const ConstantPointerNull *CPN = dyn_cast(O1))
1625 if (CPN->getType()->getAddressSpace() == 0)
1629 if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
16261630 return NoAlias;
16271631 if (const ConstantPointerNull *CPN = dyn_cast(O2))
1628 if (CPN->getType()->getAddressSpace() == 0)
1632 if (!NullPointerIsDefined(&F, CPN->getType()->getAddressSpace()))
16291633 return NoAlias;
16301634
16311635 if (O1 != O2) {
16611665
16621666 // If the size of one access is larger than the entire object on the other
16631667 // side, then we know such behavior is undefined and can assume no alias.
1668 bool NullIsValidLocation = NullPointerIsDefined(&F);
16641669 if ((V1Size != MemoryLocation::UnknownSize &&
1665 isObjectSmallerThan(O2, V1Size, DL, TLI)) ||
1670 isObjectSmallerThan(O2, V1Size, DL, TLI, NullIsValidLocation)) ||
16661671 (V2Size != MemoryLocation::UnknownSize &&
1667 isObjectSmallerThan(O1, V2Size, DL, TLI)))
1672 isObjectSmallerThan(O1, V2Size, DL, TLI, NullIsValidLocation)))
16681673 return NoAlias;
16691674
16701675 // Check the cache before climbing up use-def chains. This also terminates
17241729 if (O1 == O2)
17251730 if (V1Size != MemoryLocation::UnknownSize &&
17261731 V2Size != MemoryLocation::UnknownSize &&
1727 (isObjectSize(O1, V1Size, DL, TLI) ||
1728 isObjectSize(O2, V2Size, DL, TLI)))
1732 (isObjectSize(O1, V1Size, DL, TLI, NullIsValidLocation) ||
1733 isObjectSize(O2, V2Size, DL, TLI, NullIsValidLocation)))
17291734 return AliasCache[Locs] = PartialAlias;
17301735
17311736 // Recurse back into the best AA results we have, potentially with refined
18691874
18701875 BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
18711876 return BasicAAResult(F.getParent()->getDataLayout(),
1877 F,
18721878 AM.getResult(F),
18731879 AM.getResult(F),
18741880 &AM.getResult(F),
19011907 auto &DTWP = getAnalysis();
19021908 auto *LIWP = getAnalysisIfAvailable();
19031909
1904 Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), TLIWP.getTLI(),
1910 Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), F, TLIWP.getTLI(),
19051911 ACT.getAssumptionCache(F), &DTWP.getDomTree(),
19061912 LIWP ? &LIWP->getLoopInfo() : nullptr));
19071913
19181924 BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
19191925 return BasicAAResult(
19201926 F.getParent()->getDataLayout(),
1927 F,
19211928 P.getAnalysis().getTLI(),
19221929 P.getAnalysis().getAssumptionCache(F));
19231930 }
15881588
15891589 Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty,
15901590 ArrayRef Operands,
1591 const TargetLibraryInfo *TLI) {
1591 const TargetLibraryInfo *TLI,
1592 ImmutableCallSite CS) {
15921593 if (Operands.size() == 1) {
15931594 if (isa(Operands[0])) {
15941595 // cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN
16021603 }
16031604
16041605 if (isa(Operands[0]) &&
1605 Operands[0]->getType()->getPointerAddressSpace() == 0) {
1606 !NullPointerIsDefined(
1607 CS.getCaller(), Operands[0]->getType()->getPointerAddressSpace())) {
16061608 // launder(null) == null == strip(null) iff in addrspace 0
16071609 if (IntrinsicID == Intrinsic::launder_invariant_group ||
16081610 IntrinsicID == Intrinsic::strip_invariant_group)
20062008 Constant *ConstantFoldVectorCall(StringRef Name, unsigned IntrinsicID,
20072009 VectorType *VTy, ArrayRef Operands,
20082010 const DataLayout &DL,
2009 const TargetLibraryInfo *TLI) {
2011 const TargetLibraryInfo *TLI,
2012 ImmutableCallSite CS) {
20102013 SmallVector Result(VTy->getNumElements());
20112014 SmallVector Lane(Operands.size());
20122015 Type *Ty = VTy->getElementType();
20692072 }
20702073
20712074 // Use the regular scalar folding to simplify this column.
2072 Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI);
2075 Constant *Folded = ConstantFoldScalarCall(Name, IntrinsicID, Ty, Lane, TLI, CS);
20732076 if (!Folded)
20742077 return nullptr;
20752078 Result[I] = Folded;
20942097
20952098 if (auto *VTy = dyn_cast(Ty))
20962099 return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands,
2097 F->getParent()->getDataLayout(), TLI);
2098
2099 return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);
2100 F->getParent()->getDataLayout(), TLI, CS);
2101
2102 return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI, CS);
21002103 }
21012104
21022105 bool llvm::isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI) {
19931993 if (Caller->hasFnAttribute(Attribute::OptimizeNone))
19941994 return llvm::InlineCost::getNever();
19951995
1996 // Don't inline a function that treats null pointer as valid into a caller
1997 // that does not have this attribute.
1998 if (!Caller->nullPointerIsDefined() && Callee->nullPointerIsDefined())
1999 return llvm::InlineCost::getNever();
2000
19962001 // Don't inline functions which can be interposed at link-time. Don't inline
19972002 // functions marked noinline or call sites marked noinline.
19982003 // Note: inlining non-exact non-interposable functions is fine, since we know
21192119 ConstantInt *LHSOffsetCI = dyn_cast(LHSOffset);
21202120 ConstantInt *RHSOffsetCI = dyn_cast(RHSOffset);
21212121 uint64_t LHSSize, RHSSize;
2122 ObjectSizeOpts Opts;
2123 Opts.NullIsUnknownSize =
2124 NullPointerIsDefined(cast(LHS)->getFunction());
21222125 if (LHSOffsetCI && RHSOffsetCI &&
2123 getObjectSize(LHS, LHSSize, DL, TLI) &&
2124 getObjectSize(RHS, RHSSize, DL, TLI)) {
2126 getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
2127 getObjectSize(RHS, RHSSize, DL, TLI, Opts)) {
21252128 const APInt &LHSOffsetValue = LHSOffsetCI->getValue();
21262129 const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
21272130 if (!LHSOffsetValue.isNegative() &&
703703 assert(isa(Val) && "Unknown live-in to the entry block");
704704 // Before giving up, see if we can prove the pointer non-null local to
705705 // this particular block.
706 if (Val->getType()->isPointerTy() &&
707 (isKnownNonZero(Val, DL) || isObjectDereferencedInBlock(Val, BB))) {
708 PointerType *PTy = cast(Val->getType());
706 PointerType *PTy = dyn_cast(Val->getType());
707 if (PTy &&
708 (isKnownNonZero(Val, DL) ||
709 (isObjectDereferencedInBlock(Val, BB) &&
710 !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())))) {
709711 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
710712 } else {
711713 Result = ValueLatticeElement::getOverdefined();
738740 << "' - overdefined because of pred (non local).\n");
739741 // Before giving up, see if we can prove the pointer non-null local to
740742 // this particular block.
741 if (Val->getType()->isPointerTy() &&
742 isObjectDereferencedInBlock(Val, BB)) {
743 PointerType *PTy = cast(Val->getType());
743 PointerType *PTy = dyn_cast(Val->getType());
744 if (PTy && isObjectDereferencedInBlock(Val, BB) &&
745 !NullPointerIsDefined(BB->getParent(), PTy->getAddressSpace())) {
744746 Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy));
745747 }
746748
499499 typedef PointerIntPair MemAccessInfo;
500500 typedef SmallVector MemAccessInfoList;
501501
502 AccessAnalysis(const DataLayout &Dl, AliasAnalysis *AA, LoopInfo *LI,
503 MemoryDepChecker::DepCandidates &DA,
502 AccessAnalysis(const DataLayout &Dl, Loop *TheLoop, AliasAnalysis *AA,
503 LoopInfo *LI, MemoryDepChecker::DepCandidates &DA,
504504 PredicatedScalarEvolution &PSE)
505 : DL(Dl), AST(*AA), LI(LI), DepCands(DA), IsRTCheckAnalysisNeeded(false),
506 PSE(PSE) {}
505 : DL(Dl), TheLoop(TheLoop), AST(*AA), LI(LI), DepCands(DA),
506 IsRTCheckAnalysisNeeded(false), PSE(PSE) {}
507507
508508 /// Register a load and whether it is only read from.
509509 void addLoad(MemoryLocation &Loc, bool IsReadOnly) {
577577 PtrAccessSet Accesses;
578578
579579 const DataLayout &DL;
580
581 /// The loop being checked.
582 const Loop *TheLoop;
580583
581584 /// List of accesses that need a further dependence check.
582585 MemAccessInfoList CheckDeps;
909912 for (Value *UnderlyingObj : TempObjects) {
910913 // nullptr never alias, don't join sets for pointer that have "null"
911914 // in their UnderlyingObjects list.
912 if (isa(UnderlyingObj))
915 if (isa(UnderlyingObj) &&
916 !NullPointerIsDefined(
917 TheLoop->getHeader()->getParent(),
918 UnderlyingObj->getType()->getPointerAddressSpace()))
913919 continue;
914920
915921 UnderlyingObjToAccessMap::iterator Prev =
10251031 bool IsNoWrapAddRec = !ShouldCheckWrap ||
10261032 PSE.hasNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW) ||
10271033 isNoWrapAddRec(Ptr, AR, PSE, Lp);
1028 bool IsInAddressSpaceZero = PtrTy->getAddressSpace() == 0;
1029 if (!IsNoWrapAddRec && !IsInBoundsGEP && !IsInAddressSpaceZero) {
1034 if (!IsNoWrapAddRec && !IsInBoundsGEP &&
1035 NullPointerIsDefined(Lp->getHeader()->getParent(),
1036 PtrTy->getAddressSpace())) {
10301037 if (Assume) {
10311038 PSE.setNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW);
10321039 IsNoWrapAddRec = true;
10721079 // If the SCEV could wrap but we have an inbounds gep with a unit stride we
10731080 // know we can't "wrap around the address space". In case of address space
10741081 // zero we know that this won't happen without triggering undefined behavior.
1075 if (!IsNoWrapAddRec && (IsInBoundsGEP || IsInAddressSpaceZero) &&
1076 Stride != 1 && Stride != -1) {
1082 if (!IsNoWrapAddRec && Stride != 1 && Stride != -1 &&
1083 (IsInBoundsGEP || !NullPointerIsDefined(Lp->getHeader()->getParent(),
1084 PtrTy->getAddressSpace()))) {
10771085 if (Assume) {
10781086 // We can avoid this case by adding a run-time check.
10791087 LLVM_DEBUG(dbgs() << "LAA: Non unit strided pointer which is not either "
18441852
18451853 MemoryDepChecker::DepCandidates DependentAccesses;
18461854 AccessAnalysis Accesses(TheLoop->getHeader()->getModule()->getDataLayout(),
1847 AA, LI, DependentAccesses, *PSE);
1855 TheLoop, AA, LI, DependentAccesses, *PSE);
18481856
18491857 // Holds the analyzed pointers. We don't want to call GetUnderlyingObjects
18501858 // multiple times on the same object. If the ptr is accessed twice, once
17681768 /// Currently this routine does not support vector GEPs.
17691769 static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
17701770 const Query &Q) {
1771 if (!GEP->isInBounds() || GEP->getPointerAddressSpace() != 0)
1771 const Function *F = nullptr;
1772 if (const Instruction *I = dyn_cast(GEP))
1773 F = I->getFunction();
1774
1775 if (!GEP->isInBounds() ||
1776 NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
17721777 return false;
17731778
17741779 // FIXME: Support vector-GEPs.
14991499 assert(isa(V2) && "Canonicalization guarantee!");
15001500 // GlobalVals can never be null unless they have external weak linkage.
15011501 // We don't try to evaluate aliases here.
1502 // NOTE: We should not be doing this constant folding if null pointer
1503 // is considered valid for the function. But currently there is no way to
1504 // query it from the Constant type.
15021505 if (!GV->hasExternalWeakLinkage() && !isa(GV) &&
1503 GV->getType()->getAddressSpace() == 0)
1506 !NullPointerIsDefined(nullptr /* F */,
1507 GV->getType()->getAddressSpace()))
15041508 return ICmpInst::ICMP_NE;
15051509 }
15061510 } else if (const BlockAddress *BA = dyn_cast(V1)) {
17301734 if (const GlobalValue *GV = dyn_cast(C2))
17311735 // Don't try to evaluate aliases. External weak GV can be null.
17321736 if (!isa(GV) && !GV->hasExternalWeakLinkage() &&
1733 GV->getType()->getAddressSpace() == 0) {
1737 !NullPointerIsDefined(nullptr /* F */,
1738 GV->getType()->getAddressSpace())) {
17341739 if (pred == ICmpInst::ICMP_EQ)
17351740 return ConstantInt::getFalse(C1->getContext());
17361741 else if (pred == ICmpInst::ICMP_NE)
17411746 if (const GlobalValue *GV = dyn_cast(C1))
17421747 // Don't try to evaluate aliases. External weak GV can be null.
17431748 if (!isa(GV) && !GV->hasExternalWeakLinkage() &&
1744 GV->getType()->getAddressSpace() == 0) {
1749 !NullPointerIsDefined(nullptr /* F */,
1750 GV->getType()->getAddressSpace())) {
17451751 if (pred == ICmpInst::ICMP_EQ)
17461752 return ConstantInt::getFalse(C1->getContext());
17471753 else if (pred == ICmpInst::ICMP_NE)
7878 if (getParent()->hasParamAttribute(getArgNo(), Attribute::NonNull))
7979 return true;
8080 else if (getDereferenceableBytes() > 0 &&
81 getType()->getPointerAddressSpace() == 0)
81 !NullPointerIsDefined(getParent(),
82 getType()->getPointerAddressSpace()))
8283 return true;
8384 return false;
8485 }
14141415 }
14151416 return None;
14161417 }
1418
1419 bool Function::nullPointerIsDefined() const {
1420 return getFnAttribute("null-pointer-is-valid")
1421 .getValueAsString()
1422 .equals("true");
1423 }
1424
1425 bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
1426 if (F && F->nullPointerIsDefined())
1427 return true;
1428
1429 if (AS != 0)
1430 return true;
1431
1432 return false;
1433 }
635635 /// reprocessing them.
636636 static bool AllUsesOfValueWillTrapIfNull(const Value *V,
637637 SmallPtrSetImpl &PHIs) {
638 for (const User *U : V->users())
638 for (const User *U : V->users()) {
639 if (const Instruction *I = dyn_cast(U)) {
640 // If null pointer is considered valid, then all uses are non-trapping.
641 // Non address-space 0 globals have already been pruned by the caller.
642 if (NullPointerIsDefined(I->getFunction()))
643 return false;
644 }
639645 if (isa(U)) {
640646 // Will trap.
641647 } else if (const StoreInst *SI = dyn_cast(U)) {
669675 //cerr << "NONTRAPPING USE: " << *U;
670676 return false;
671677 }
672
678 }
673679 return true;
674680 }
675681
696702 bool Changed = false;
697703 for (auto UI = V->user_begin(), E = V->user_end(); UI != E; ) {
698704 Instruction *I = cast(*UI++);
705 // Uses are non-trapping if null pointer is considered valid.
706 // Non address-space 0 globals are already pruned by the caller.
707 if (NullPointerIsDefined(I->getFunction()))
708 return false;
699709 if (LoadInst *LI = dyn_cast(I)) {
700710 LI->setOperand(0, NewV);
701711 Changed = true;
15831593 // users of the loaded value (often calls and loads) that would trap if the
15841594 // value was null.
15851595 if (GV->getInitializer()->getType()->isPointerTy() &&
1586 GV->getInitializer()->isNullValue()) {
1596 GV->getInitializer()->isNullValue() &&
1597 !NullPointerIsDefined(
1598 nullptr /* F */,
1599 GV->getInitializer()->getType()->getPointerAddressSpace())) {
15871600 if (Constant *SOVC = dyn_cast(StoredOnceVal)) {
15881601 if (GV->getInitializer()->getType() != SOVC->getType())
15891602 SOVC = ConstantExpr::getBitCast(SOVC, GV->getInitializer()->getType());
40194019 }
40204020 }
40214021
4022 if (isa(Callee) || isa(Callee)) {
4022 if ((isa(Callee) &&
4023 !NullPointerIsDefined(CS.getInstruction()->getFunction())) ||
4024 isa(Callee)) {
40234025 // If CS does not return void then replaceAllUsesWith undef.
40244026 // This allows ValueHandlers and custom metadata to adjust itself.
40254027 if (!CS.getInstruction()->getType()->isVoidTy())
963963 }
964964
965965 static bool canSimplifyNullStoreOrGEP(StoreInst &SI) {
966 if (SI.getPointerAddressSpace() != 0)
966 if (NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()))
967967 return false;
968968
969969 auto *Ptr = SI.getPointerOperand();
970970 if (GetElementPtrInst *GEPI = dyn_cast(Ptr))
971971 Ptr = GEPI->getOperand(0);
972 return isa(Ptr);
972 return (isa(Ptr) &&
973 !NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()));
973974 }
974975
975976 static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
976977 if (GetElementPtrInst *GEPI = dyn_cast(Op)) {
977978 const Value *GEPI0 = GEPI->getOperand(0);
978 if (isa(GEPI0) && GEPI->getPointerAddressSpace() == 0)
979 if (isa(GEPI0) &&
980 !NullPointerIsDefined(LI.getFunction(), GEPI->getPointerAddressSpace()))
979981 return true;
980982 }
981983 if (isa(Op) ||
982 (isa(Op) && LI.getPointerAddressSpace() == 0))
984 (isa(Op) &&
985 !NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace())))
983986 return true;
984987 return false;
985988 }
10751078
10761079 // load (select (cond, null, P)) -> load P
10771080 if (isa(SI->getOperand(1)) &&
1078 LI.getPointerAddressSpace() == 0) {
1081 !NullPointerIsDefined(SI->getFunction(),
1082 LI.getPointerAddressSpace())) {
10791083 LI.setOperand(0, SI->getOperand(2));
10801084 return &LI;
10811085 }
10821086
10831087 // load (select (cond, P, null)) -> load P
10841088 if (isa(SI->getOperand(2)) &&
1085 LI.getPointerAddressSpace() == 0) {
1089 !NullPointerIsDefined(SI->getFunction(),
1090 LI.getPointerAddressSpace())) {
10861091 LI.setOperand(0, SI->getOperand(1));
10871092 return &LI;
10881093 }
309309 }
310310
311311 static uint64_t getPointerSize(const Value *V, const DataLayout &DL,
312 const TargetLibraryInfo &TLI) {
312 const TargetLibraryInfo &TLI,
313 const Function *F) {
313314 uint64_t Size;
314 if (getObjectSize(V, Size, DL, &TLI))
315 ObjectSizeOpts Opts;
316 Opts.NullIsUnknownSize = NullPointerIsDefined(F);
317
318 if (getObjectSize(V, Size, DL, &TLI, Opts))
315319 return Size;
316320 return MemoryLocation::UnknownSize;
317321 }
342346 int64_t &EarlierOff, int64_t &LaterOff,
343347 Instruction *DepWrite,
344348 InstOverlapIntervalsTy &IOL,
345 AliasAnalysis &AA) {
349 AliasAnalysis &AA,
350 const Function *F) {
346351 // If we don't know the sizes of either access, then we can't do a comparison.
347352 if (Later.Size == MemoryLocation::UnknownSize ||
348353 Earlier.Size == MemoryLocation::UnknownSize)
371376 return OW_Unknown;
372377
373378 // If the "Later" store is to a recognizable object, get its size.
374 uint64_t ObjectSize = getPointerSize(UO2, DL, TLI);
379 uint64_t ObjectSize = getPointerSize(UO2, DL, TLI, F);
375380 if (ObjectSize != MemoryLocation::UnknownSize)
376381 if (ObjectSize == Later.Size && ObjectSize >= Earlier.Size)
377382 return OW_Complete;
709714 static void removeAccessedObjects(const MemoryLocation &LoadedLoc,
710715 SmallSetVector &DeadStackObjects,
711716 const DataLayout &DL, AliasAnalysis *AA,
712 const TargetLibraryInfo *TLI) {
717 const TargetLibraryInfo *TLI,
718 const Function *F) {
713719 const Value *UnderlyingPointer = GetUnderlyingObject(LoadedLoc.Ptr, DL);
714720
715721 // A constant can't be in the dead pointer set.
726732 // Remove objects that could alias LoadedLoc.
727733 DeadStackObjects.remove_if([&](Value *I) {
728734 // See if the loaded location could alias the stack location.
729 MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI));
735 MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI, F));
730736 return !AA->isNoAlias(StackLoc, LoadedLoc);
731737 });
732738 }
840846 // the call is live.
841847 DeadStackObjects.remove_if([&](Value *I) {
842848 // See if the call site touches the value.
843 return isRefSet(AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI)));
849 return isRefSet(AA->getModRefInfo(CS, I, getPointerSize(I, DL, *TLI,
850 BB.getParent())));
844851 });
845852
846853 // If all of the allocas were clobbered by the call then we're not going
879886
880887 // Remove any allocas from the DeadPointer set that are loaded, as this
881888 // makes any stores above the access live.
882 removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI);
889 removeAccessedObjects(LoadedLoc, DeadStackObjects, DL, AA, TLI, BB.getParent());
883890
884891 // If all of the allocas were clobbered by the access then we're not going
885892 // to find anything else to process.
11751182 !isPossibleSelfRead(Inst, Loc, DepWrite, *TLI, *AA)) {
11761183 int64_t InstWriteOffset, DepWriteOffset;
11771184 OverwriteResult OR = isOverwrite(Loc, DepLoc, DL, *TLI, DepWriteOffset,
1178 InstWriteOffset, DepWrite, IOL, *AA);
1185 InstWriteOffset, DepWrite, IOL, *AA,
1186 BB.getParent());
11791187 if (OR == OW_Complete) {
11801188 LLVM_DEBUG(dbgs() << "DSE: Remove Dead Store:\n DEAD: " << *DepWrite
11811189 << "\n KILLER: " << *Inst << '\n');
11321132 Constant *Ptr = PtrVal.getConstant();
11331133
11341134 // load null is undefined.
1135 if (isa(Ptr) && I.getPointerAddressSpace() == 0)
1136 return;
1135 if (isa(Ptr)) {
1136 if (NullPointerIsDefined(I.getFunction(), I.getPointerAddressSpace()))
1137 return (void)markOverdefined(IV, &I);
1138 else
1139 return;
1140 }
11371141
11381142 // Transform load (constant global) into the value loaded.
11391143 if (auto *GV = dyn_cast(Ptr)) {
20492049
20502050 if (auto *CI = dyn_cast(&I)) {
20512051 Value *Callee = CI->getCalledValue();
2052 if (isa(Callee) || isa(Callee)) {
2052 if ((isa(Callee) &&
2053 !NullPointerIsDefined(CI->getFunction())) ||
2054 isa(Callee)) {
20532055 changeToUnreachable(CI, /*UseLLVMTrap=*/false, false, DDT);
20542056 Changed = true;
20552057 break;
20782080
20792081 if (isa(Ptr) ||
20802082 (isa(Ptr) &&
2081 SI->getPointerAddressSpace() == 0)) {
2083 !NullPointerIsDefined(SI->getFunction(),
2084 SI->getPointerAddressSpace()))) {
20822085 changeToUnreachable(SI, true, false, DDT);
20832086 Changed = true;
20842087 break;
20902093 if (auto *II = dyn_cast(Terminator)) {
20912094 // Turn invokes that call 'nounwind' functions into ordinary calls.
20922095 Value *Callee = II->getCalledValue();
2093 if (isa(Callee) || isa(Callee)) {
2096 if ((isa(Callee) &&
2097 !NullPointerIsDefined(BB->getParent())) ||
2098 isa(Callee)) {
20942099 changeToUnreachable(II, true, false, DDT);
20952100 Changed = true;
20962101 } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
59495949 // Load from null is undefined.
59505950 if (LoadInst *LI = dyn_cast(Use))
59515951 if (!LI->isVolatile())
5952 return LI->getPointerAddressSpace() == 0;
5952 return !NullPointerIsDefined(LI->getFunction(),
5953 LI->getPointerAddressSpace());
59535954
59545955 // Store to null is undefined.
59555956 if (StoreInst *SI = dyn_cast(Use))
59565957 if (!SI->isVolatile())
5957 return SI->getPointerAddressSpace() == 0 &&
5958 return (!NullPointerIsDefined(SI->getFunction(),
5959 SI->getPointerAddressSpace())) &&
59585960 SI->getPointerOperand() == I;
59595961
59605962 // A call to null is undefined.
59615963 if (auto CS = CallSite(Use))
5962 return CS.getCalledValue() == I;
5964 return !NullPointerIsDefined(CS->getFunction()) &&
5965 CS.getCalledValue() == I;
59635966 }
59645967 return false;
59655968 }
1616
1717 bb68: ; preds = %bb26
1818 ; CHECK: MemoryUse(liveOnEntry)
19 ; CHECK-NEXT: %tmp69 = load i64, i64* null, align 8
20 %tmp69 = load i64, i64* null, align 8
21 ; CHECK: 1 = MemoryDef(3)
22 ; CHECK-NEXT: store i64 %tmp69, i64* %tmp, align 8
23 store i64 %tmp69, i64* %tmp, align 8
24 br label %bb77
25
26 bb77: ; preds = %bb68, %bb26
27 ; CHECK: 2 = MemoryPhi({bb26,3},{bb68,1})
28 ; CHECK: MemoryUse(2)
29 ; CHECK-NEXT: %tmp78 = load i64*, i64** %tmp25, align 8
30 %tmp78 = load i64*, i64** %tmp25, align 8
31 %tmp79 = getelementptr inbounds i64, i64* %tmp78, i64 undef
32 br label %bb26
33 }
34
35 define hidden void @quux_no_null_opt(%struct.hoge *%f) align 2 #0 {
36 ; CHECK-LABEL: quux_no_null_opt(
37 %tmp = getelementptr inbounds %struct.hoge, %struct.hoge* %f, i64 0, i32 1, i32 0
38 %tmp24 = getelementptr inbounds %struct.hoge, %struct.hoge* %f, i64 0, i32 1
39 %tmp25 = bitcast %struct.widget* %tmp24 to i64**
40 br label %bb26
41
42 bb26: ; preds = %bb77, %0
43 ; CHECK: 3 = MemoryPhi({%0,liveOnEntry},{bb77,2})
44 ; CHECK-NEXT: br i1 undef, label %bb68, label %bb77
45 br i1 undef, label %bb68, label %bb77
46
47 bb68: ; preds = %bb26
48 ; CHECK: MemoryUse(3)
1949 ; CHECK-NEXT: %tmp69 = load i64, i64* null, align 8
2050 %tmp69 = load i64, i64* null, align 8
2151 ; CHECK: 1 = MemoryDef(3)
120150 ; CHECK-NEXT: br label %bb26
121151 br label %bb26
122152 }
153
154 attributes #0 = { "null-pointer-is-valid"="true" }
99 ret void
1010 }
1111
12 define void @test1_no_null_opt(i8* %ptr) #0 {
13 ; CHECK: test1_no_null_opt
14 %A = load i8, i8* %ptr
15 br label %bb
16 bb:
17 icmp ne i8* %ptr, null
18 ; CHECK: icmp ne i8* %ptr, null
19 ret void
20 }
21
1222 define void @test2(i8* %ptr) {
1323 ; CHECK: test2
1424 store i8 0, i8* %ptr
1929 ret void
2030 }
2131
32 define void @test2_no_null_opt(i8* %ptr) #0 {
33 ; CHECK: test2_no_null_opt
34 store i8 0, i8* %ptr
35 br label %bb
36 bb:
37 icmp ne i8* %ptr, null
38 ; CHECK: icmp ne i8* %ptr, null
39 ret void
40 }
41
2242 define void @test3() {
43 ; CHECK: test3
44 %ptr = alloca i8
45 br label %bb
46 bb:
47 icmp ne i8* %ptr, null
48 ; CHECK-NOT: icmp
49 ret void
50 }
51
52 ;; OK to remove icmp here since ptr is coming from alloca.
53 define void @test3_no_null_opt() #0 {
2354 ; CHECK: test3
2455 %ptr = alloca i8
2556 br label %bb
4172 ret void
4273 }
4374
75 define void @test4_no_null_opt(i8* %dest, i8* %src) #0 {
76 ; CHECK: test4_no_null_opt
77 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %dest, i8* %src, i32 1, i1 false)
78 br label %bb
79 bb:
80 icmp ne i8* %dest, null
81 icmp ne i8* %src, null
82 ; CHECK: icmp ne i8* %dest, null
83 ; CHECK: icmp ne i8* %src, null
84 ret void
85 }
86
4487 declare void @llvm.memmove.p0i8.p0i8.i32(i8*, i8*, i32, i1)
4588 define void @test5(i8* %dest, i8* %src) {
4689 ; CHECK: test5
5396 ret void
5497 }
5598
99 define void @test5_no_null_opt(i8* %dest, i8* %src) #0 {
100 ; CHECK: test5_no_null_opt
101 call void @llvm.memmove.p0i8.p0i8.i32(i8* %dest, i8* %src, i32 1, i1 false)
102 br label %bb
103 bb:
104 icmp ne i8* %dest, null
105 icmp ne i8* %src, null
106 ; CHECK: icmp ne i8* %dest, null
107 ; CHECK: icmp ne i8* %src, null
108 ret void
109 }
110
56111 declare void @llvm.memset.p0i8.i32(i8*, i8, i32, i1)
57112 define void @test6(i8* %dest) {
58113 ; CHECK: test6
61116 bb:
62117 icmp ne i8* %dest, null
63118 ; CHECK-NOT: icmp
119 ret void
120 }
121
122 define void @test6_no_null_opt(i8* %dest) #0 {
123 ; CHECK: test6_no_null_opt
124 call void @llvm.memset.p0i8.i32(i8* %dest, i8 255, i32 1, i1 false)
125 br label %bb
126 bb:
127 icmp ne i8* %dest, null
128 ; CHECK: icmp ne i8* %dest, null
64129 ret void
65130 }
66131
160225 ; CHECK: call void @test12_helper(i8* nonnull %merged_arg)
161226 ret void
162227 }
228
229 attributes #0 = { "null-pointer-is-valid"="true" }
223223 ret i32* %q
224224 }
225225
226 define i32* @gep1_no_null_opt(i32* %p) #0 {
227 ; Should't be able to derive nonnull based on gep.
228 ; CHECK: define i32* @gep1_no_null_opt(
229 %q = getelementptr inbounds i32, i32* %p, i32 1
230 ret i32* %q
231 }
232
226233 ; CHECK: define i32 addrspace(3)* @gep2(
227234 define i32 addrspace(3)* @gep2(i32 addrspace(3)* %p) {
228235 %q = getelementptr inbounds i32, i32 addrspace(3)* %p, i32 1
229236 ret i32 addrspace(3)* %q
230237 }
238
239 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; This test checks if debug loc is propagated to load/store created by GVN/Instcombine.
1 ; RUN: opt < %s -gvn -S | FileCheck %s --check-prefixes=ALL
2 ; RUN: opt < %s -gvn -instcombine -S | FileCheck %s --check-prefixes=ALL
3
4 ; struct node {
5 ; int *v;
6 ; struct desc *descs;
7 ; };
8
9 ; struct desc {
10 ; struct node *node;
11 ; };
12
13 ; extern int bar(void *v, void* n);
14
15 ; int test(struct desc *desc)
16 ; {
17 ; void *v, *n;
18 ; v = !desc ? ((void *)0) : desc->node->v; // Line 15
19 ; n = &desc->node->descs[0]; // Line 16
20 ; return bar(v, n);
21 ; }
22
23 ; Line 16, Column 13:
24 ; n = &desc->node->descs[0];
25 ; ^
26
27 target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
28 target triple = "aarch64--linux-gnu"
29
30 %struct.desc = type { %struct.node* }
31 %struct.node = type { i32*, %struct.desc* }
32
33 define i32 @test_no_null_opt(%struct.desc* readonly %desc) local_unnamed_addr #0 !dbg !4 {
34 entry:
35 %tobool = icmp eq %struct.desc* %desc, null
36 br i1 %tobool, label %cond.end, label %cond.false, !dbg !9
37 ; ALL: br i1 %tobool, label %entry.cond.end_crit_edge, label %cond.false, !dbg [[LOC_15_6:![0-9]+]]
38 ; ALL: entry.cond.end_crit_edge:
39 ; ALL: load %struct.node*, %struct.node** null, align {{[0-9]+}}, !dbg [[LOC_16_13:![0-9]+]]
40
41 cond.false:
42 %0 = bitcast %struct.desc* %desc to i8***, !dbg !11
43 %1 = load i8**, i8*** %0, align 8, !dbg !11
44 %2 = load i8*, i8** %1, align 8
45 br label %cond.end, !dbg !9
46
47 cond.end:
48 ; ALL: phi %struct.node* [ %3, %cond.false ], [ %.pre, %entry.cond.end_crit_edge ]
49 ; ALL: phi i8* [ %2, %cond.false ], [ null, %entry.cond.end_crit_edge ]
50
51 %3 = phi i8* [ %2, %cond.false ], [ null, %entry ], !dbg !9
52 %node2 = getelementptr inbounds %struct.desc, %struct.desc* %desc, i64 0, i32 0
53 %4 = load %struct.node*, %struct.node** %node2, align 8, !dbg !10
54 %descs = getelementptr inbounds %struct.node, %struct.node* %4, i64 0, i32 1
55 %5 = bitcast %struct.desc** %descs to i8**
56 %6 = load i8*, i8** %5, align 8
57 %call = tail call i32 @bar(i8* %3, i8* %6)
58 ret i32 %call
59 }
60 attributes #0 = { "null-pointer-is-valid"="true" }
61
62 declare i32 @bar(i8*, i8*) local_unnamed_addr #1
63 !llvm.dbg.cu = !{!0}
64 !llvm.module.flags = !{!2, !3}
65
66 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug)
67 !1 = !DIFile(filename: "test.c", directory: ".")
68 !2 = !{i32 2, !"Dwarf Version", i32 4}
69 !3 = !{i32 2, !"Debug Info Version", i32 3}
70 !4 = distinct !DISubprogram(name: "test_no_null_opt", scope: !1, file: !1, line: 12, type: !5, isLocal: false, isDefinition: true, scopeLine: 13, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !8)
71 !5 = !DISubroutineType(types: !6)
72 !6 = !{!7}
73 !7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
74 !8 = !{}
75 !9 = !DILocation(line: 15, column: 6, scope: !4)
76 !10 = !DILocation(line: 16, column: 13, scope: !4)
77 !11 = !DILocation(line: 15, column: 34, scope: !4)
78
79 ;ALL: [[SCOPE:![0-9]+]] = distinct !DISubprogram(name: "test_no_null_opt",{{.*}}
80 ;ALL: [[LOC_15_6]] = !DILocation(line: 15, column: 6, scope: [[SCOPE]])
81 ;ALL: [[LOC_16_13]] = !DILocation(line: 16, column: 13, scope: [[SCOPE]])
0 ; RUN: opt -globalopt -S < %s | FileCheck %s
1 ; CHECK: @Y
2 ; CHECK: section ".foo"
3
4 %struct.xyz = type { double, i32 }
5
6 @Y = internal global %struct.xyz* null ,section ".foo" ; <%struct.xyz**> [#uses=2]
7 @numf2s = external global i32 ; [#uses=1]
8
9 define void @init_net() #0 {
10 ; CHECK-LABEL: init_net(
11 ; CHECK: load i32, i32* @numf2s
12 ; CHECK: call i8* @malloc
13 ; CHECK: store %struct.xyz* {{.*}}, %struct.xyz** @Y
14 entry:
15 %0 = load i32, i32* @numf2s, align 4 ; [#uses=1]
16 %mallocsize2 = shl i32 %0, 4 ; [#uses=1]
17 %malloccall3 = tail call i8* @malloc(i32 %mallocsize2) ; [#uses=1]
18 %1 = bitcast i8* %malloccall3 to %struct.xyz* ; <%struct.xyz*> [#uses=1]
19 store %struct.xyz* %1, %struct.xyz** @Y, align 8
20 ret void
21 }
22
23 define %struct.xyz* @load_train() #0 {
24 ; CHECK-LABEL: load_train(
25 ; CHECK: load %struct.xyz*, %struct.xyz** @Y
26 entry:
27 %0 = load %struct.xyz*, %struct.xyz** @Y, align 8 ; <%struct.xyz*> [#uses=0]
28 ret %struct.xyz* %0
29 }
30
31 declare noalias i8* @malloc(i32)
32
33 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 %struct.foo = type { i32, i32 }
4 @X = internal global %struct.foo* null
5 ; CHECK: @X
6 ; CHECK-NOT: @X.f0
7
8 define void @bar(i64 %Size) nounwind noinline #0 {
9 entry:
10 %mallocsize = mul i64 %Size, 8 ; [#uses=1]
11 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
12 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
13 store %struct.foo* %.sub, %struct.foo** @X, align 4
14 ret void
15 }
16
17 declare noalias i8* @malloc(i64)
18
19 define i32 @baz() nounwind readonly noinline #0 {
20 bb1.thread:
21 %0 = load %struct.foo*, %struct.foo** @X, align 4
22 br label %bb1
23
24 bb1: ; preds = %bb1, %bb1.thread
25 %i.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %indvar.next, %bb1 ]
26 %sum.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %3, %bb1 ]
27 %1 = getelementptr %struct.foo, %struct.foo* %0, i32 %i.0.reg2mem.0, i32 0
28 %2 = load i32, i32* %1, align 4
29 %3 = add i32 %2, %sum.0.reg2mem.0
30 %indvar.next = add i32 %i.0.reg2mem.0, 1
31 %exitcond = icmp eq i32 %indvar.next, 1200
32 br i1 %exitcond, label %bb2, label %bb1
33
34 bb2: ; preds = %bb1
35 ret i32 %3
36 }
37
38 attributes #0 = { "null-pointer-is-valid"="true" }
39
3535 ret i32 %3
3636 }
3737
38 define void @bam(i64 %Size) nounwind noinline #0 {
39 entry:
40 %0 = load %struct.foo*, %struct.foo** @X, align 4
41 ret void
42 }
43
44 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 %struct.foo = type { i32, i32 }
4 @X = internal global %struct.foo* null ; <%struct.foo**> [#uses=2]
5 ; CHECK: @X
6 ; CHECK-NOT: @X.f0
7
8 define void @bar(i32 %Size) nounwind noinline #0 {
9 entry:
10 %malloccall = tail call i8* @malloc(i64 8000000) ; [#uses=1]
11 %0 = bitcast i8* %malloccall to [1000000 x %struct.foo]* ; <[1000000 x %struct.foo]*> [#uses=1]
12 %.sub = getelementptr [1000000 x %struct.foo], [1000000 x %struct.foo]* %0, i32 0, i32 0 ; <%struct.foo*> [#uses=1]
13 store %struct.foo* %.sub, %struct.foo** @X, align 4
14 ret void
15 }
16
17 declare noalias i8* @malloc(i64)
18
19 define i32 @baz() nounwind readonly noinline #0 {
20 bb1.thread:
21 %0 = load %struct.foo*, %struct.foo** @X, align 4 ; <%struct.foo*> [#uses=1]
22 br label %bb1
23
24 bb1: ; preds = %bb1, %bb1.thread
25 %i.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %indvar.next, %bb1 ] ; [#uses=2]
26 %sum.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %3, %bb1 ] ; [#uses=1]
27 %1 = getelementptr %struct.foo, %struct.foo* %0, i32 %i.0.reg2mem.0, i32 0 ; [#uses=1]
28 %2 = load i32, i32* %1, align 4 ; [#uses=1]
29 %3 = add i32 %2, %sum.0.reg2mem.0 ; [#uses=2]
30 %indvar.next = add i32 %i.0.reg2mem.0, 1 ; [#uses=2]
31 %exitcond = icmp eq i32 %indvar.next, 1200 ; [#uses=1]
32 br i1 %exitcond, label %bb2, label %bb1
33
34 bb2: ; preds = %bb1
35 ret i32 %3
36 }
37
38 attributes #0 = { "null-pointer-is-valid"="true" }
3535 ret i32 %3
3636 }
3737
38 define void @bam(i64 %Size) nounwind noinline #0 {
39 entry:
40 %0 = load %struct.foo*, %struct.foo** @X, align 4
41 ret void
42 }
43
44 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 %struct.foo = type { i32, i32 }
4 @X = internal global %struct.foo* null
5 ; CHECK: @X
6 ; CHECK-NOT: @X.f0
7
8 define void @bar(i64 %Size) nounwind noinline #0 {
9 entry:
10 %mallocsize = mul i64 8, %Size ; [#uses=1]
11 ; CHECK: mul i64 8, %Size
12 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
13 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
14 store %struct.foo* %.sub, %struct.foo** @X, align 4
15 ret void
16 }
17
18 declare noalias i8* @malloc(i64)
19
20 define i32 @baz() nounwind readonly noinline #0 {
21 bb1.thread:
22 ; CHECK: load %struct.foo*, %struct.foo** @X, align 4
23 %0 = load %struct.foo*, %struct.foo** @X, align 4
24 br label %bb1
25
26 bb1: ; preds = %bb1, %bb1.thread
27 %i.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %indvar.next, %bb1 ]
28 %sum.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %3, %bb1 ]
29 %1 = getelementptr %struct.foo, %struct.foo* %0, i32 %i.0.reg2mem.0, i32 0
30 %2 = load i32, i32* %1, align 4
31 %3 = add i32 %2, %sum.0.reg2mem.0
32 %indvar.next = add i32 %i.0.reg2mem.0, 1
33 %exitcond = icmp eq i32 %indvar.next, 1200
34 br i1 %exitcond, label %bb2, label %bb1
35
36 bb2: ; preds = %bb1
37 ret i32 %3
38 }
39
40 attributes #0 = { "null-pointer-is-valid"="true" }
3636 ret i32 %3
3737 }
3838
39 define void @bam(i64 %Size) nounwind noinline #0 {
40 entry:
41 %0 = load %struct.foo*, %struct.foo** @X, align 4
42 ret void
43 }
44
45 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 %struct.foo = type { i32, i32 }
4
5 @X = internal global %struct.foo* null
6 ; CHECK: @X
7 ; CHECK-NOT: @X.f0
8
9 define void @bar(i64 %Size) nounwind noinline #0 {
10 entry:
11 %mallocsize = shl i64 %Size, 3 ; [#uses=1]
12 %malloccall = tail call i8* @malloc(i64 %mallocsize) ; [#uses=1]
13 ; CHECK: shl i64 %Size, 3
14 %.sub = bitcast i8* %malloccall to %struct.foo* ; <%struct.foo*> [#uses=1]
15 store %struct.foo* %.sub, %struct.foo** @X, align 4
16 ret void
17 }
18
19 declare noalias i8* @malloc(i64)
20
21 define i32 @baz() nounwind readonly noinline #0 {
22 ; CHECK-LABEL: @baz(
23 bb1.thread:
24 ; CHECK: load %struct.foo*, %struct.foo** @X, align 4
25 %0 = load %struct.foo*, %struct.foo** @X, align 4
26 br label %bb1
27
28 bb1: ; preds = %bb1, %bb1.thread
29 %i.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %indvar.next, %bb1 ]
30 %sum.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %3, %bb1 ]
31 %1 = getelementptr %struct.foo, %struct.foo* %0, i32 %i.0.reg2mem.0, i32 0
32 %2 = load i32, i32* %1, align 4
33 ; CHECK: load i32, i32* %1, align 4
34 %3 = add i32 %2, %sum.0.reg2mem.0
35 %indvar.next = add i32 %i.0.reg2mem.0, 1
36 %exitcond = icmp eq i32 %indvar.next, 1200
37 br i1 %exitcond, label %bb2, label %bb1
38
39 bb2: ; preds = %bb1
40 ret i32 %3
41 }
42
43 attributes #0 = { "null-pointer-is-valid"="true" }
3636 ret i32 %3
3737 }
3838
39 define void @bam(i64 %Size) nounwind noinline #0 {
40 entry:
41 %0 = load %struct.foo*, %struct.foo** @X, align 4
42 ret void
43 }
44
45 attributes #0 = { "null-pointer-is-valid"="true" }
46
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 %struct.foo = type { i32, i32 }
4
5 @X = internal global %struct.foo* null ; <%struct.foo**> [#uses=2]
6 ; CHECK: @X
7 ; CHECK-NOT: @X.f0
8
9 define void @bar(i32 %Size) nounwind noinline #0 {
10 ; CHECK-LABEL: @bar(
11 entry:
12 %malloccall = tail call i8* @malloc(i64 8000000) ; [#uses=1]
13 %tmp = bitcast i8* %malloccall to [1000000 x %struct.foo]* ; <[1000000 x %struct.foo]*> [#uses=1]
14 %.sub = getelementptr [1000000 x %struct.foo], [1000000 x %struct.foo]* %tmp, i32 0, i32 0 ; <%struct.foo*> [#uses=1]
15 store %struct.foo* %.sub, %struct.foo** @X, align 4
16 ret void
17 }
18
19 declare noalias i8* @malloc(i64)
20
21 define i32 @baz() nounwind readonly noinline #0 {
22 ; CHECK-LABEL: @baz(
23 bb1.thread:
24 %tmpLD1 = load %struct.foo*, %struct.foo** @X, align 4 ; <%struct.foo*> [#uses=1]
25 ; CHECK: load %struct.foo*, %struct.foo** @X, align 4
26 br label %bb1
27
28 bb1: ; preds = %bb1, %bb1.thread
29 %tmp = phi %struct.foo* [%tmpLD1, %bb1.thread ], [ %tmpLD2, %bb1 ] ; [#uses=2]
30 ; CHECK: %tmp = phi %struct.foo* [ %tmpLD1, %bb1.thread ], [ %tmpLD2, %bb1 ]
31 %i.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %indvar.next, %bb1 ] ; [#uses=2]
32 %sum.0.reg2mem.0 = phi i32 [ 0, %bb1.thread ], [ %tmp3, %bb1 ] ; [#uses=1]
33 %tmp1 = getelementptr %struct.foo, %struct.foo* %tmp, i32 %i.0.reg2mem.0, i32 0 ; [#uses=1]
34 %tmp2 = load i32, i32* %tmp1, align 4 ; [#uses=1]
35 ; CHECK: load i32, i32* %tmp1, align 4
36 %tmp6 = add i32 %tmp2, %sum.0.reg2mem.0 ; [#uses=2]
37 %tmp4 = getelementptr %struct.foo, %struct.foo* %tmp, i32 %i.0.reg2mem.0, i32 1 ; [#uses=1]
38 %tmp5 = load i32 , i32 * %tmp4
39 ; CHECK: load i32, i32* %tmp4
40 %tmp3 = add i32 %tmp5, %tmp6
41 %indvar.next = add i32 %i.0.reg2mem.0, 1 ; [#uses=2]
42
43 %tmpLD2 = load %struct.foo*, %struct.foo** @X, align 4 ; <%struct.foo*> [#uses=1]
44 ; CHECK: load %struct.foo*, %struct.foo** @X, align 4
45
46 %exitcond = icmp eq i32 %indvar.next, 1200 ; [#uses=1]
47 br i1 %exitcond, label %bb2, label %bb1
48
49 bb2: ; preds = %bb1
50 ret i32 %tmp3
51 }
52
53 attributes #0 = { "null-pointer-is-valid"="true" }
4141 bb2: ; preds = %bb1
4242 ret i32 %tmp3
4343 }
44
45 define void @bam(i64 %Size) nounwind noinline #0 {
46 entry:
47 %0 = load %struct.foo*, %struct.foo** @X, align 4
48 ret void
49 }
50
51 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1
2 @a = internal global i64* null, align 8
3 ; CHECK: @a
4
5 ; PR13968
6 define void @qux_no_null_opt() nounwind #0 {
7 ; CHECK-LABEL: @qux_no_null_opt(
8 ; CHECK: getelementptr i64*, i64** @a, i32 1
9 ; CHECK: store i64* inttoptr (i64 1 to i64*), i64** @a
10 %b = bitcast i64** @a to i8*
11 %g = getelementptr i64*, i64** @a, i32 1
12 %cmp = icmp ne i8* null, %b
13 %cmp2 = icmp eq i8* null, %b
14 %cmp3 = icmp eq i64** null, %g
15 store i64* inttoptr (i64 1 to i64*), i64** @a, align 8
16 %l = load i64*, i64** @a, align 8
17 ret void
18 }
19
20 define i64* @bar() {
21 %X = load i64*, i64** @a, align 8
22 ret i64* %X
23 ; CHECK-LABEL: @bar(
24 ; CHECK: load
25 }
26
27 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 @G = internal global i32* null ; [#uses=3]
4 ; CHECK: global
5
6 define void @init() #0 {
7 ; CHECK-LABEL: @init(
8 ; CHECK: store
9 ; CHECK: load
10 %malloccall = tail call i8* @malloc(i64 4) ; [#uses=1]
11 %P = bitcast i8* %malloccall to i32* ; [#uses=1]
12 store i32* %P, i32** @G
13 %GV = load i32*, i32** @G ; [#uses=1]
14 store i32 0, i32* %GV
15 ret void
16 }
17
18 declare noalias i8* @malloc(i64)
19
20 define i32 @get() #0 {
21 ; CHECK-LABEL: @get(
22 ; CHECK: load i32*, i32** @G
23 ; CHECK-NEXT: load i32, i32* %GV
24 %GV = load i32*, i32** @G ; [#uses=1]
25 %V = load i32, i32* %GV ; [#uses=1]
26 ret i32 %V
27 ; CHECK: ret i32 %V
28 }
29
30 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
11 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
22
3 @G = internal global i32* null ; [#uses=3]
3 @G = internal global i32* null ; [#uses=4]
44 ; CHECK-NOT: global
55
66 define void @init() {
2121 ; CHECK: ret i32 0
2222 }
2323
24 define void @foo(i64 %Size) nounwind noinline #0 {
25 entry:
26 %0 = load i32*, i32** @G, align 4
27 ret void
28 }
29
30 attributes #0 = { "null-pointer-is-valid"="true" }
31
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
2
3 @G = internal global i32* null
4
5 define void @t() #0 {
6 ; CHECK: @t()
7 ; CHECK: call i8* @malloc
8 ; CHECK: bitcast
9 ; CHECK: store
10 ; CHECK: load
11 ; CHECK: getelementptr
12 ; CHECK: store
13 %malloccall = tail call i8* @malloc(i64 mul (i64 100, i64 4))
14 %P = bitcast i8* %malloccall to i32*
15 store i32* %P, i32** @G
16 %GV = load i32*, i32** @G
17 %GVe = getelementptr i32, i32* %GV, i32 40
18 store i32 20, i32* %GVe
19 ret void
20 }
21
22 declare noalias i8* @malloc(i64)
23 attributes #0 = { "null-pointer-is-valid"="true" }
1616 }
1717
1818 declare noalias i8* @malloc(i64)
19
20 define void @foo(i64 %Size) nounwind noinline #0 {
21 entry:
22 %0 = load i32*, i32** @G, align 4
23 ret void
24 }
25
26 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1 ; CHECK: global
2
3 @G = internal global void ()* null ; [#uses=2]
4
5 define internal void @Actual() {
6 ; CHECK-LABEL: Actual(
7 ret void
8 }
9
10 define void @init() {
11 ; CHECK-LABEL: init(
12 ; CHECK: store void ()* @Actual, void ()** @G
13 store void ()* @Actual, void ()** @G
14 ret void
15 }
16
17 define void @doit() #0 {
18 ; CHECK-LABEL: doit(
19 %FP = load void ()*, void ()** @G ; [#uses=2]
20 ; CHECK: %FP = load void ()*, void ()** @G
21 %CC = icmp eq void ()* %FP, null ; [#uses=1]
22 ; CHECK: %CC = icmp eq void ()* %FP, null
23 br i1 %CC, label %isNull, label %DoCall
24 ; CHECK: br i1 %CC, label %isNull, label %DoCall
25
26 DoCall: ; preds = %0
27 ; CHECK: DoCall:
28 ; CHECK: call void %FP()
29 ; CHECK: ret void
30 call void %FP( )
31 ret void
32
33 isNull: ; preds = %0
34 ; CHECK: isNull:
35 ; CHECK: ret void
36 ret void
37 }
38
39 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt < %s -globalopt -S | FileCheck %s
1
2 @G = internal global void ()* null ; [#uses=2]
3 ; CHECK: global
4
5 define internal void @Actual() {
6 ; CHECK-LABEL: Actual(
7 ret void
8 }
9
10 define void @init() {
11 ; CHECK-LABEL: init(
12 ; CHECK: store void ()* @Actual, void ()** @G
13 store void ()* @Actual, void ()** @G
14 ret void
15 }
16
17 define void @doit() #0 {
18 ; CHECK-LABEL: doit(
19 ; CHECK: %FP = load void ()*, void ()** @G
20 ; CHECK: call void %FP()
21 %FP = load void ()*, void ()** @G ; [#uses=1]
22 call void %FP( )
23 ret void
24 }
25
26 attributes #0 = { "null-pointer-is-valid"="true" }
2222 ret i32 %cond
2323 }
2424
25 define void @fn_no_null_opt() #0 {
26 entry:
27 br label %if.end
28
29 for.cond1: ; preds = %if.end, %for.end
30 br i1 undef, label %if.end, label %if.end
31
32 if.end: ; preds = %lbl, %for.cond1
33 %e.2 = phi i32* [ undef, %entry ], [ null, %for.cond1 ], [ null, %for.cond1 ]
34 %0 = load i32, i32* %e.2, align 4
35 %call = call i32 @fn0(i32 %0)
36 br label %for.cond1
37 }
38
39 define internal i32 @fn0(i32 %p1) {
40 entry:
41 %tobool = icmp ne i32 %p1, 0
42 %cond = select i1 %tobool, i32 %p1, i32 %p1
43 ret i32 %cond
44 }
45
46 attributes #0 = { "null-pointer-is-valid"="true" }
47
2548 ; CHECK-LABEL: define void @fn2(
2649 ; CHECK: call i32 @fn1(i32 undef)
2750
2851 ; CHECK-LABEL: define internal i32 @fn1(
2952 ; CHECK:%[[COND:.*]] = select i1 undef, i32 undef, i32 undef
3053 ; CHECK: ret i32 %[[COND]]
54
55 ; CHECK-LABEL: define void @fn_no_null_opt(
56 ; CHECK: call i32 @fn0(i32 %0)
57
58 ; CHECK-LABEL: define internal i32 @fn0(
59 ; CHECK:%[[TOBOOL:.*]] = icmp ne i32 %p1, 0
60 ; CHECK:%[[COND:.*]] = select i1 %[[TOBOOL]], i32 %p1, i32 %p1
61 ; CHECK: ret i32 %[[COND]]
332332 ; CHECK-NEXT: ret i32
333333 }
334334
335 ; Calle with "null-pointer-is-valid"="true" attribute should not be inlined
336 ; into a caller without this attribute. Exception: alwaysinline callee
337 ; can still be inlined.
338
339 define i32 @null-pointer-is-valid_callee0(i32 %i) "null-pointer-is-valid"="true" {
340 ret i32 %i
341 ; CHECK: @null-pointer-is-valid_callee0(i32 %i)
342 ; CHECK-NEXT: ret i32
343 }
344
345 define i32 @null-pointer-is-valid_callee1(i32 %i) alwaysinline "null-pointer-is-valid"="true" {
346 ret i32 %i
347 ; CHECK: @null-pointer-is-valid_callee1(i32 %i)
348 ; CHECK-NEXT: ret i32
349 }
350
351 define i32 @null-pointer-is-valid_callee2(i32 %i) {
352 ret i32 %i
353 ; CHECK: @null-pointer-is-valid_callee2(i32 %i)
354 ; CHECK-NEXT: ret i32
355 }
356
357 define i32 @test_null-pointer-is-valid0(i32 %i) {
358 %1 = call i32 @null-pointer-is-valid_callee0(i32 %i)
359 ret i32 %1
360 ; CHECK: @test_null-pointer-is-valid0(
361 ; CHECK: call i32 @null-pointer-is-valid_callee0
362 ; CHECK-NEXT: ret i32
363 }
364
365 define i32 @test_null-pointer-is-valid1(i32 %i) {
366 %1 = call i32 @null-pointer-is-valid_callee1(i32 %i)
367 ret i32 %1
368 ; CHECK: @test_null-pointer-is-valid1(
369 ; CHECK-NEXT: ret i32
370 }
371
372 define i32 @test_null-pointer-is-valid2(i32 %i) "null-pointer-is-valid"="true" {
373 %1 = call i32 @null-pointer-is-valid_callee2(i32 %i)
374 ret i32 %1
375 ; CHECK: @test_null-pointer-is-valid2(
376 ; CHECK-NEXT: ret i32
377 }
378
335379 ; CHECK: attributes [[FPMAD_FALSE]] = { "less-precise-fpmad"="false" }
336380 ; CHECK: attributes [[FPMAD_TRUE]] = { "less-precise-fpmad"="true" }
337381 ; CHECK: attributes [[NOIMPLICITFLOAT]] = { noimplicitfloat }
101101 ret i32 %x
102102 }
103103
104 define i32 @test9_no_null_opt() #0 {
105 ; CHECK-LABEL: define i32 @test9_no_null_opt(
106 ; CHECK: load atomic i32, i32* null unordered
107 %x = load atomic i32, i32* null unordered, align 4
108 ret i32 %x
109 }
110
104111 ; FIXME: Could also fold
105112 define i32 @test10() {
106113 ; CHECK-LABEL: define i32 @test10(
109116 ret i32 %x
110117 }
111118
119 define i32 @test10_no_null_opt() #0 {
120 ; CHECK-LABEL: define i32 @test10_no_null_opt(
121 ; CHECK: load atomic i32, i32* null monotonic
122 %x = load atomic i32, i32* null monotonic, align 4
123 ret i32 %x
124 }
125
112126 ; Would this be legal to fold? Probably?
113127 define i32 @test11() {
114128 ; CHECK-LABEL: define i32 @test11(
129 ; CHECK: load atomic i32, i32* null seq_cst
130 %x = load atomic i32, i32* null seq_cst, align 4
131 ret i32 %x
132 }
133
134 define i32 @test11_no_null_opt() #0 {
135 ; CHECK-LABEL: define i32 @test11_no_null_opt(
115136 ; CHECK: load atomic i32, i32* null seq_cst
116137 %x = load atomic i32, i32* null seq_cst, align 4
117138 ret i32 %x
126147 ret i32 0
127148 }
128149
150 define i32 @test12_no_null_opt() #0 {
151 ; CHECK-LABEL: define i32 @test12_no_null_opt(
152 ; CHECK: store atomic i32 0, i32* null unordered
153 store atomic i32 0, i32* null unordered, align 4
154 ret i32 0
155 }
156
129157 ; FIXME: Could also fold
130158 define i32 @test13() {
131159 ; CHECK-LABEL: define i32 @test13(
134162 ret i32 0
135163 }
136164
165 define i32 @test13_no_null_opt() #0 {
166 ; CHECK-LABEL: define i32 @test13_no_null_opt(
167 ; CHECK: store atomic i32 0, i32* null monotonic
168 store atomic i32 0, i32* null monotonic, align 4
169 ret i32 0
170 }
171
137172 ; Would this be legal to fold? Probably?
138173 define i32 @test14() {
139174 ; CHECK-LABEL: define i32 @test14(
175 ; CHECK: store atomic i32 0, i32* null seq_cst
176 store atomic i32 0, i32* null seq_cst, align 4
177 ret i32 0
178 }
179
180 define i32 @test14_no_null_opt() #0 {
181 ; CHECK-LABEL: define i32 @test14_no_null_opt(
140182 ; CHECK: store atomic i32 0, i32* null seq_cst
141183 store atomic i32 0, i32* null seq_cst, align 4
142184 ret i32 0
286328 store atomic i64 %1, i64* %2 unordered, align 8
287329 ret void
288330 }
331
332 attributes #0 = { "null-pointer-is-valid"="true" }
33 ; CHECK-LABEL: define i8* @simplifyNullLaunder()
44 define i8* @simplifyNullLaunder() {
55 ; CHECK-NEXT: ret i8* null
6 %b2 = call i8* @llvm.launder.invariant.group.p0i8(i8* null)
7 ret i8* %b2
8 }
9
10 ; CHECK-LABEL: define i8* @dontSimplifyNullLaunderNoNullOpt()
11 define i8* @dontSimplifyNullLaunderNoNullOpt() #0 {
12 ; CHECK-NEXT: call i8* @llvm.launder.invariant.group.p0i8(i8* null)
613 %b2 = call i8* @llvm.launder.invariant.group.p0i8(i8* null)
714 ret i8* %b2
815 }
4047 ret i8* %b2
4148 }
4249
50 ; CHECK-LABEL: define i8* @dontSimplifyNullStripNonNullOpt()
51 define i8* @dontSimplifyNullStripNonNullOpt() #0 {
52 ; CHECK-NEXT: call i8* @llvm.strip.invariant.group.p0i8(i8* null)
53 %b2 = call i8* @llvm.strip.invariant.group.p0i8(i8* null)
54 ret i8* %b2
55 }
56
4357 ; CHECK-LABEL: define i8 addrspace(42)* @dontsimplifyNullStripForDifferentAddrspace()
4458 define i8 addrspace(42)* @dontsimplifyNullStripForDifferentAddrspace() {
4559 ; CHECK: %b2 = call i8 addrspace(42)* @llvm.strip.invariant.group.p42i8(i8 addrspace(42)* null)
6579 declare i8* @llvm.strip.invariant.group.p0i8(i8*)
6680 declare i8 addrspace(42)* @llvm.strip.invariant.group.p42i8(i8 addrspace(42)*)
6781
82 attributes #0 = { "null-pointer-is-valid"="true" }
4646 unreachable
4747 }
4848
49 ; CHECK-LABEL: @f2_no_null_opt(
50 define i64 @f2_no_null_opt() nounwind uwtable ssp #0 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
51 entry:
52 ; CHECK: invoke noalias i8* null()
53 %call = invoke noalias i8* null()
54 to label %invoke.cont unwind label %lpad
55
56 invoke.cont:
57 ; CHECK: call i64 @llvm.objectsize.i64.p0i8(i8* %call, i1 false, i1 false)
58 %0 = tail call i64 @llvm.objectsize.i64(i8* %call, i1 false)
59 ret i64 %0
60
61 lpad:
62 %1 = landingpad { i8*, i32 }
63 filter [0 x i8*] zeroinitializer
64 %2 = extractvalue { i8*, i32 } %1, 0
65 tail call void @__cxa_call_unexpected(i8* %2) noreturn nounwind
66 unreachable
67 }
68 attributes #0 = { "null-pointer-is-valid"="true" }
69
4970 ; CHECK-LABEL: @f3(
5071 define void @f3() nounwind uwtable ssp personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
5172 ; CHECK: invoke void @llvm.donothing()
0 ; RUN: opt < %s -instcombine -S | FileCheck %s
1
2 declare void @llvm.dbg.declare(metadata, metadata, metadata)
3 declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
4 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
5 declare void @foo(i8* nocapture, i8* nocapture)
6
7 define void @bar(i1 %flag) #0 !dbg !4 {
8 entry:
9 ; CHECK-LABEL: @bar(
10 ; CHECK: %[[T:[^ ]+]] = getelementptr inbounds [1 x i8], [1 x i8]* %text
11 ; CHECK: %[[B:[^ ]+]] = getelementptr inbounds [1 x i8], [1 x i8]* %buff
12 ; CHECK: if:
13 ; CHECK-NEXT: br label %bb2
14 ; CHECK: bb2:
15 ; CHECK-NEXT: br label %bb3
16 ; CHECK: bb3:
17 ; CHECK-NEXT: call void @llvm.dbg.declare
18 ; CHECK-NEXT: br label %fin
19 ; CHECK: call void @llvm.lifetime.start.p0i8(i64 1, i8* %[[T]])
20 ; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* %[[B]])
21 ; CHECK-NEXT: call void @foo(i8* %[[B]], i8* %[[T]])
22 ; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 1, i8* %[[B]])
23 ; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 1, i8* %[[T]])
24 %text = alloca [1 x i8], align 1
25 %buff = alloca [1 x i8], align 1
26 %0 = getelementptr inbounds [1 x i8], [1 x i8]* %text, i64 0, i64 0
27 %1 = getelementptr inbounds [1 x i8], [1 x i8]* %buff, i64 0, i64 0
28 br i1 %flag, label %if, label %else
29
30 if:
31 call void @llvm.lifetime.start.p0i8(i64 1, i8* %0)
32 call void @llvm.lifetime.start.p0i8(i64 1, i8* %1)
33 call void @llvm.lifetime.end.p0i8(i64 1, i8* %1)
34 call void @llvm.lifetime.end.p0i8(i64 1, i8* %0)
35 br label %bb2
36
37 bb2:
38 call void @llvm.lifetime.start.p0i8(i64 1, i8* %0)
39 call void @llvm.lifetime.start.p0i8(i64 1, i8* %1)
40 call void @llvm.lifetime.end.p0i8(i64 1, i8* %0)
41 call void @llvm.lifetime.end.p0i8(i64 1, i8* %1)
42 br label %bb3
43
44 bb3:
45 call void @llvm.lifetime.start.p0i8(i64 1, i8* %0)
46 call void @llvm.dbg.declare(metadata [1 x i8]* %text, metadata !14, metadata !25), !dbg !26
47 call void @llvm.lifetime.end.p0i8(i64 1, i8* %0)
48 br label %fin
49
50 else:
51 call void @llvm.lifetime.start.p0i8(i64 1, i8* %0)
52 call void @llvm.lifetime.start.p0i8(i64 1, i8* %1)
53 call void @foo(i8* %1, i8* %0)
54 call void @llvm.lifetime.end.p0i8(i64 1, i8* %1)
55 call void @llvm.lifetime.end.p0i8(i64 1, i8* %0)
56 br label %fin
57
58 fin:
59 ret void
60 }
61
62 attributes #0 = { "null-pointer-is-valid"="true" }
63
64 !llvm.dbg.cu = !{!0}
65 !llvm.module.flags = !{!22, !23}
66 !llvm.ident = !{!24}
67
68 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.8.0 (trunk 248826) (llvm/trunk 248827)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
69 !1 = !DIFile(filename: "test.cpp", directory: "/home/user")
70 !2 = !{}
71 !4 = distinct !DISubprogram(name: "bar", linkageName: "bar", scope: !1, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: true, unit: !0, retainedNodes: !8)
72 !5 = !DISubroutineType(types: !6)
73 !6 = !{null, !7}
74 !7 = !DIBasicType(name: "bool", size: 8, align: 8, encoding: DW_ATE_boolean)
75 !8 = !{!9, !11, !12, !14, !21}
76 !9 = !DILocalVariable(name: "Size", arg: 1, scope: !4, file: !1, line: 2, type: !10)
77 !10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
78 !11 = !DILocalVariable(name: "flag", arg: 2, scope: !4, file: !1, line: 2, type: !7)
79 !12 = !DILocalVariable(name: "i", scope: !13, file: !1, line: 3, type: !10)
80 !13 = distinct !DILexicalBlock(scope: !4, file: !1, line: 3, column: 3)
81 !14 = !DILocalVariable(name: "text", scope: !15, file: !1, line: 4, type: !17)
82 !15 = distinct !DILexicalBlock(scope: !16, file: !1, line: 3, column: 30)
83 !16 = distinct !DILexicalBlock(scope: !13, file: !1, line: 3, column: 3)
84 !17 = !DICompositeType(tag: DW_TAG_array_type, baseType: !18, size: 8, align: 8, elements: !19)
85 !18 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
86 !19 = !{!20}
87 !20 = !DISubrange(count: 1)
88 !21 = !DILocalVariable(name: "buff", scope: !15, file: !1, line: 5, type: !17)
89 !22 = !{i32 2, !"Dwarf Version", i32 4}
90 !23 = !{i32 2, !"Debug Info Version", i32 3}
91 !24 = !{!"clang version 3.8.0 (trunk 248826) (llvm/trunk 248827)"}
92 !25 = !DIExpression()
93 !26 = !DILocation(line: 4, column: 10, scope: !15)
5858 %R = load i32, i32* %V ; [#uses=1]
5959 ret i32 %R
6060 }
61
62 ; CHECK-LABEL: @test7_no_null_opt(
63 ; CHECK: %V = getelementptr i32, i32* null
64 ; CHECK: %R = load i32, i32* %V
65 define i32 @test7_no_null_opt(i32 %X) #0 {
66 %V = getelementptr i32, i32* null, i32 %X ; [#uses=1]
67 %R = load i32, i32* %V ; [#uses=1]
68 ret i32 %R
69 }
70 attributes #0 = { "null-pointer-is-valid"="true" }
6171
6272 ; CHECK-LABEL: @test8(
6373 ; CHECK-NOT: load
5757 ret void
5858 }
5959
60 ; CHECK-LABEL: test_call_no_null_opt
61 ; CHECK: alloca
62 ; CHECK: call void @llvm.memcpy.p0i8.p2i8.i64
63 ; CHECK-NOT: addrspacecast
64 ; CHECK: call i32 @foo(i32* %{{.*}})
65 define void @test_call_no_null_opt(i32 addrspace(1)* %out, i64 %x) #0 {
66 entry:
67 %data = alloca [8 x i32], align 4
68 %0 = bitcast [8 x i32]* %data to i8*
69 call void @llvm.memcpy.p0i8.p2i8.i64(i8* align 4 %0, i8 addrspace(2)* align 4 bitcast ([8 x i32] addrspace(2)* @test.data to i8 addrspace(2)*), i64 32, i1 false)
70 %arrayidx = getelementptr inbounds [8 x i32], [8 x i32]* %data, i64 0, i64 %x
71 %1 = call i32 @foo(i32* %arrayidx)
72 %arrayidx1 = getelementptr inbounds i32, i32 addrspace(1)* %out, i64 %x
73 store i32 %1, i32 addrspace(1)* %arrayidx1, align 4
74 ret void
75 }
76
6077 ; CHECK-LABEL: test_load_and_call
6178 ; CHECK: alloca
6279 ; CHECK: call void @llvm.memcpy.p0i8.p2i8.i64
7996 ret void
8097 }
8198
99 ; CHECK-LABEL: test_load_and_call_no_null_opt
100 ; CHECK: alloca
101 ; CHECK: call void @llvm.memcpy.p0i8.p2i8.i64
102 ; CHECK: load i32, i32* %{{.*}}
103 ; CHECK: call i32 @foo(i32* %{{.*}})
104 ; CHECK-NOT: addrspacecast
105 ; CHECK-NOT: load i32, i32 addrspace(2)*
106 define void @test_load_and_call_no_null_opt(i32 addrspace(1)* %out, i64 %x, i64 %y) #0 {
107 entry:
108 %data = alloca [8 x i32], align 4
109 %0 = bitcast [8 x i32]* %data to i8*
110 call void @llvm.memcpy.p0i8.p2i8.i64(i8* align 4 %0, i8 addrspace(2)* align 4 bitcast ([8 x i32] addrspace(2)* @test.data to i8 addrspace(2)*), i64 32, i1 false)
111 %arrayidx = getelementptr inbounds [8 x i32], [8 x i32]* %data, i64 0, i64 %x
112 %1 = load i32, i32* %arrayidx, align 4
113 %arrayidx1 = getelementptr inbounds i32, i32 addrspace(1)* %out, i64 %x
114 store i32 %1, i32 addrspace(1)* %arrayidx1, align 4
115 %2 = call i32 @foo(i32* %arrayidx)
116 %arrayidx2 = getelementptr inbounds i32, i32 addrspace(1)* %out, i64 %y
117 store i32 %2, i32 addrspace(1)* %arrayidx2, align 4
118 ret void
119 }
82120
83121 declare void @llvm.memcpy.p0i8.p2i8.i64(i8* nocapture writeonly, i8 addrspace(2)* nocapture readonly, i64, i1)
84122 declare i32 @foo(i32* %x)
123
124 attributes #0 = { "null-pointer-is-valid"="true" }
6666 ret void
6767 }
6868
69 define void @test2_no_null_opt() #0 {
70 %A = alloca %T
71 %B = alloca %T
72 %a = bitcast %T* %A to i8*
73 %b = bitcast %T* %B to i8*
74
75 ; CHECK-LABEL: @test2_no_null_opt(
76
77 ; %A alloca is deleted
78 ; CHECK-NEXT: alloca [124 x i8]
79 ; CHECK-NEXT: getelementptr inbounds [124 x i8], [124 x i8]*
80
81 ; use @G instead of %A
82 ; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %{{.*}}, i8* align 16 getelementptr inbounds (%T, %T* @G, i64 0, i32 0)
83 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %a, i8* align 4 bitcast (%T* @G to i8*), i64 124, i1 false)
84 call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %b, i8* align 4 %a, i64 124, i1 false)
85 call void @bar(i8* %b)
86 ret void
87 }
88
6989 define void @test2_addrspacecast() {
7090 %A = alloca %T
7191 %B = alloca %T
234254 call void @llvm.memcpy.p0i8.p0i8.i64(i8* getelementptr inbounds ([1000000 x i8], [1000000 x i8]* @bbb, i32 0, i32 0), i8* %arraydecay, i64 3, i1 false)
235255 ret void
236256 }
257
258 attributes #0 = { "null-pointer-is-valid"="true" }
383383 %V = load i32, i32 addrspace(1)* %P2
384384 ret i32 %V
385385 }
386
387 ;; It may be legal to load from a null address with null pointer valid attribute.
388 define i32 @test16_no_null_opt(i1 %C, i32* %P) #0 {
389 ; CHECK-LABEL: @test16_no_null_opt(
390 ; CHECK-NEXT: [[P2:%.*]] = select i1 [[C:%.*]], i32* [[P:%.*]], i32* null
391 ; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P2]], align 4
392 ; CHECK-NEXT: ret i32 [[V]]
393 ;
394 %P2 = select i1 %C, i32* %P, i32* null
395 %V = load i32, i32* %P2
396 ret i32 %V
397 }
398
399 define i32 @test16_no_null_opt_2(i1 %C, i32* %P) #0 {
400 ; CHECK-LABEL: @test16_no_null_opt_2(
401 ; CHECK-NEXT: [[P2:%.*]] = select i1 [[C:%.*]], i32* null, i32* [[P:%.*]]
402 ; CHECK-NEXT: [[V:%.*]] = load i32, i32* [[P2]], align 4
403 ; CHECK-NEXT: ret i32 [[V]]
404 ;
405 %P2 = select i1 %C, i32* null, i32* %P
406 %V = load i32, i32* %P2
407 ret i32 %V
408 }
409
410 attributes #0 = { "null-pointer-is-valid"="true" }
386411
387412 define i1 @test17(i32* %X, i1 %C) {
388413 ; CHECK-LABEL: @test17(
2727 ret void
2828 }
2929
30 define void @store_at_gep_off_no_null_opt(i64 %offset) #0 {
31 %ptr = getelementptr i32, i32 *null, i64 %offset
32 store i32 24, i32* %ptr
33 ret void
34 ; CHECK-LABEL: @store_at_gep_off_no_null_opt(i64 %offset)
35 ; CHECK-NEXT: %ptr = getelementptr i32, i32* null, i64 %offset
36 ; CHECK-NEXT: store i32 24, i32* %ptr
37 }
38
39 attributes #0 = { "null-pointer-is-valid"="true" }
40
3041 ;; Simple sinking tests
3142
3243 ; "if then else"
1212 ret void
1313 }
1414
15 define void @func_no_null_opt(i8* %i) nounwind ssp #0 {
16 ; CHECK-LABEL: @func_no_null_opt(
17 ; CHECK: @__strcpy_chk(i8* %arraydecay, i8* %i, i64 32)
18 entry:
19 %s = alloca [32 x i8], align 16
20 %arraydecay = getelementptr inbounds [32 x i8], [32 x i8]* %s, i32 0, i32 0
21 %call = call i8* @__strcpy_chk(i8* %arraydecay, i8* %i, i64 32)
22 call void @func2(i8* %arraydecay)
23 ret void
24 }
25
1526 declare i8* @__strcpy_chk(i8*, i8*, i64) nounwind
1627
1728 declare void @func2(i8*)
29
30 attributes #0 = { "null-pointer-is-valid"="true" }
161161 ret i32 %hello_l
162162 }
163163
164 define i32 @test_no_simplify2_no_null_opt(i32 %x) #0 {
165 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
166 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 %x
167 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* [[HELLO_P]])
168 ; CHECK-NEXT: ret i32 [[HELLO_L]]
169 ;
170 %hello_p = getelementptr inbounds [7 x i8], [7 x i8]* @null_hello, i32 0, i32 %x
171 %hello_l = call i32 @strlen(i8* %hello_p)
172 ret i32 %hello_l
173 }
174
164175 ; strlen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
165176
166177 define i32 @test_no_simplify3(i32 %x) {
176187 ret i32 %hello_l
177188 }
178189
190 define i32 @test_no_simplify3_on_null_opt(i32 %x) #0 {
191 ; CHECK-LABEL: @test_no_simplify3_on_null_opt(
192 ; CHECK-NEXT: [[AND:%.*]] = and i32 %x, 15
193 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 [[AND]]
194 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i32 @strlen(i8* [[HELLO_P]])
195 ; CHECK-NEXT: ret i32 [[HELLO_L]]
196 ;
197 %and = and i32 %x, 15
198 %hello_p = getelementptr inbounds [13 x i8], [13 x i8]* @null_hello_mid, i32 0, i32 %and
199 %hello_l = call i32 @strlen(i8* %hello_p)
200 ret i32 %hello_l
201 }
202
203 attributes #0 = { "null-pointer-is-valid"="true" }
165165 ret i64 %hello_l
166166 }
167167
168 define i64 @test_no_simplify2_no_null_opt(i32 %x) #0 {
169 ; CHECK-LABEL: @test_no_simplify2_no_null_opt(
170 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
171 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [7 x i32], [7 x i32]* @null_hello, i64 0, i64 [[TMP1]]
172 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(i32* [[HELLO_P]])
173 ; CHECK-NEXT: ret i64 [[HELLO_L]]
174 ;
175 %hello_p = getelementptr inbounds [7 x i32], [7 x i32]* @null_hello, i32 0, i32 %x
176 %hello_l = call i64 @wcslen(i32* %hello_p)
177 ret i64 %hello_l
178 }
179
168180 ; wcslen(@null_hello_mid + (x & 15)) should not be simplified to a sub instruction.
169181
170182 define i64 @test_no_simplify3(i32 %x) {
181193 ret i64 %hello_l
182194 }
183195
196 define i64 @test_no_simplify3_no_null_opt(i32 %x) #0 {
197 ; CHECK-LABEL: @test_no_simplify3_no_null_opt(
198 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 15
199 ; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[AND]] to i64
200 ; CHECK-NEXT: [[HELLO_P:%.*]] = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i64 0, i64 [[TMP1]]
201 ; CHECK-NEXT: [[HELLO_L:%.*]] = call i64 @wcslen(i32* [[HELLO_P]])
202 ; CHECK-NEXT: ret i64 [[HELLO_L]]
203 ;
204 %and = and i32 %x, 15
205 %hello_p = getelementptr inbounds [13 x i32], [13 x i32]* @null_hello_mid, i32 0, i32 %and
206 %hello_l = call i64 @wcslen(i32* %hello_p)
207 ret i64 %hello_l
208 }
209
184210 @str16 = constant [1 x i16] [i16 0]
185211
186212 define i64 @test_no_simplify4() {
191217 %l = call i64 @wcslen(i32* bitcast ([1 x i16]* @str16 to i32*))
192218 ret i64 %l
193219 }
220
221 attributes #0 = { "null-pointer-is-valid"="true" }
184184 ; CHECK-NEXT: ret i1 false
185185 }
186186
187 define i1 @gep13_no_null_opt(i8* %ptr) #0 {
188 ; We can't prove this GEP is non-null.
189 ; CHECK-LABEL: @gep13_no_null_opt(
190 ; CHECK: getelementptr
191 ; CHECK: icmp
192 ; CHECK: ret
193 %x = getelementptr inbounds i8, i8* %ptr, i32 1
194 %cmp = icmp eq i8* %x, null
195 ret i1 %cmp
196 }
197
187198 define i1 @gep14({ {}, i8 }* %ptr) {
188199 ; CHECK-LABEL: @gep14(
189200 ; We can't simplify this because the offset of one in the GEP actually doesn't
202213 %cmp = icmp eq i8* %x, null
203214 ret i1 %cmp
204215 ; CHECK-NEXT: ret i1 false
216 }
217
218 define i1 @gep15_no_null_opt({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) #0 {
219 ; We can't prove this GEP is non-null.
220 ; CHECK-LABEL: @gep15_no_null_opt(
221 ; CHECK: getelementptr
222 ; CHECK: icmp
223 ; CHECK: ret
224 %x = getelementptr inbounds { {}, [4 x {i8, i8}]}, { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
225 %cmp = icmp eq i8* %x, null
226 ret i1 %cmp
205227 }
206228
207229 define i1 @gep16(i8* %ptr, i32 %a) {
213235 %cmp = icmp eq i8* %x, null
214236 ret i1 %cmp
215237 ; CHECK-NEXT: ret i1 false
238 }
239
240 define i1 @gep16_no_null_opt(i8* %ptr, i32 %a) #0 {
241 ; We can't prove this GEP is non-null.
242 ; CHECK-LABEL: @gep16_no_null_opt(
243 ; CHECK getelementptr inbounds i8, i8* %ptr, i32 %b
244 ; CHECK: %cmp = icmp eq i8* %x, null
245 ; CHECK-NEXT: ret i1 %cmp
246 %b = or i32 %a, 1
247 %x = getelementptr inbounds i8, i8* %ptr, i32 %b
248 %cmp = icmp eq i8* %x, null
249 ret i1 %cmp
216250 }
217251
218252 define i1 @gep17() {
711745 ; CHECK: ret i1 false
712746 }
713747
748 define i1 @alloca_compare_no_null_opt(i64 %idx) #0 {
749 ; CHECK-LABEL: alloca_compare_no_null_opt(
750 ; CHECK: %sv = alloca { i32, i32, [124 x i32] }
751 ; CHECK: %cmp = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
752 ; CHECK: %X = icmp eq i32* %cmp, null
753 ; CHECK: ret i1 %X
754 %sv = alloca { i32, i32, [124 x i32] }
755 %cmp = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
756 %X = icmp eq i32* %cmp, null
757 ret i1 %X
758 }
714759 ; PR12075
715760 define i1 @infinite_gep() {
716761 ret i1 1
765810 %cmp = icmp eq i8* %x, null
766811 ret i1 %cmp
767812 ; CHECK-NEXT: ret i1 false
813 }
814
815 define i1 @alloca_gep_no_null_opt(i64 %a, i64 %b) #0 {
816 ; CHECK-LABEL: @alloca_gep_no_null_opt(
817 ; We can't prove this GEP is non-null.
818 ; CHECK: alloca
819 ; CHECK: getelementptr
820 ; CHECK: icmp
821 ; CHECK: ret
822 %strs = alloca [1000 x [1001 x i8]], align 16
823 %x = getelementptr inbounds [1000 x [1001 x i8]], [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
824 %cmp = icmp eq i8* %x, null
825 ret i1 %cmp
768826 }
769827
770828 define i1 @non_inbounds_gep_compare(i64* %a) {
864922 ; CHECK: ret i1 false
865923 }
866924
925 define i1 @nonnull_arg_no_null_opt(i32* nonnull %i) #0 {
926 %cmp = icmp eq i32* %i, null
927 ret i1 %cmp
928 ; CHECK-LABEL: @nonnull_arg_no_null_opt
929 ; CHECK: ret i1 false
930 }
931
867932 define i1 @nonnull_deref_arg(i32* dereferenceable(4) %i) {
868933 %cmp = icmp eq i32* %i, null
869934 ret i1 %cmp
871936 ; CHECK: ret i1 false
872937 }
873938
939 define i1 @nonnull_deref_arg_no_null_opt(i32* dereferenceable(4) %i) #0 {
940 %cmp = icmp eq i32* %i, null
941 ret i1 %cmp
942 ; CHECK-LABEL: @nonnull_deref_arg_no_null_opt
943 ; CHECK-NEXT: icmp
944 ; CHECK: ret
945 }
874946 define i1 @nonnull_deref_as_arg(i32 addrspace(1)* dereferenceable(4) %i) {
875947 %cmp = icmp eq i32 addrspace(1)* %i, null
876948 ret i1 %cmp
895967 ret i1 %cmp
896968 ; CHECK-LABEL: @returns_nonnull_deref
897969 ; CHECK: ret i1 false
970 }
971
972 define i1 @returns_nonnull_deref_no_null_opt () #0 {
973 %call = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
974 %cmp = icmp eq i32* %call, null
975 ret i1 %cmp
976 ; CHECK-LABEL: @returns_nonnull_deref_no_null_opt
977 ; CHECK: icmp
978 ; CHECK: ret
898979 }
899980
900981 declare dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
12751356 %x = icmp eq i32* null, inttoptr (i64 32 to i32*)
12761357 ret i1 %x
12771358 }
1359
1360 attributes #0 = { "null-pointer-is-valid"="true" }
2323 ; CHECK-LABEL: define void @test1(
2424 ; CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 null, i8* align 4 inttoptr (i64 4 to i8*), i64 24, i1 false)
2525 ; CHECK-NOT: store
26
27 define void @test1_no_null_opt() #0 {
28 entry:
29 br label %for.body.preheader
30
31 for.body.preheader: ; preds = %for.cond
32 br label %for.body
33
34 for.body: ; preds = %for.body, %for.body.preheader
35 %indvars.iv = phi i32 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
36 %add.ptr3 = getelementptr inbounds i32, i32* null, i32 %indvars.iv
37 %add.ptr4 = getelementptr inbounds i32, i32* %add.ptr3, i32 1
38 %0 = load i32, i32* %add.ptr4, align 4
39 store i32 %0, i32* %add.ptr3, align 4
40 %indvars.iv.next = add nsw i32 %indvars.iv, 1
41 %exitcond = icmp ne i32 %indvars.iv.next, 6
42 br i1 %exitcond, label %for.body, label %for.body.preheader
43 }
44
45 ; CHECK-LABEL: define void @test1_no_null_opt(
46 ; CHECK-NOT: call void @llvm.memcpy
47 ; CHECK: getelementptr
48 ; CHECK: getelementptr
49 ; CHECK: load
50 ; CHECK: store
51
52 attributes #0 = { "null-pointer-is-valid"="true" }
3232 bb3:
3333 ret void
3434 }
35
36 define void @fill_no_null_opt(i8** %ls1.20, i8** %ls2.21, i8* %cse3.22) #0 {
37 ; CHECK-LABEL: fill_no_null_opt(
38 ; CHECK: bb1.lver.check:
39 ; CHECK: %lver.safe = or i1 %memcheck.conflict, %{{.*}}
40 ; CHECK: br i1 %lver.safe, label %bb1.ph.lver.orig, label %bb1.ph
41 bb1.ph:
42 %ls1.20.promoted = load i8*, i8** %ls1.20
43 %ls2.21.promoted = load i8*, i8** %ls2.21
44 br label %bb1
45
46 bb1:
47 %_tmp302 = phi i8* [ %ls2.21.promoted, %bb1.ph ], [ %_tmp30, %bb1 ]
48 %_tmp281 = phi i8* [ %ls1.20.promoted, %bb1.ph ], [ %_tmp28, %bb1 ]
49 %_tmp14 = getelementptr i8, i8* %_tmp281, i16 -1
50 %_tmp15 = load i8, i8* %_tmp14
51 %add = add i8 %_tmp15, 1
52 store i8 %add, i8* %_tmp281
53 store i8 %add, i8* %_tmp302
54 %_tmp28 = getelementptr i8, i8* %_tmp281, i16 1
55 %_tmp30 = getelementptr i8, i8* %_tmp302, i16 1
56 br i1 false, label %bb1, label %bb3.loopexit
57
58 bb3.loopexit:
59 %_tmp30.lcssa = phi i8* [ %_tmp30, %bb1 ]
60 %_tmp15.lcssa = phi i8 [ %_tmp15, %bb1 ]
61 %_tmp28.lcssa = phi i8* [ %_tmp28, %bb1 ]
62 store i8* %_tmp28.lcssa, i8** %ls1.20
63 store i8 %_tmp15.lcssa, i8* %cse3.22
64 store i8* %_tmp30.lcssa, i8** %ls2.21
65 br label %bb3
66
67 bb3:
68 ret void
69 }
70
71 attributes #0 = { "null-pointer-is-valid"="true" }
7777 ret void
7878 }
7979
80 define void @test5_no_null_opt(i1 %cond, i8* %ptr) #0 {
81
82 ; CHECK-LABEL: test5_no_null_opt
83 ; CHECK: entry:
84 ; CHECK: %[[SEL:.*]] = select i1 %cond, i8* null, i8* %ptr
85 ; CHECK: store i8 2, i8* %[[SEL]]
86
87 entry:
88 br i1 %cond, label %bb1, label %bb3
89
90 bb3:
91 br label %bb2
92
93 bb1:
94 br label %bb2
95
96 bb2:
97 %ptr.2 = phi i8* [ %ptr, %bb3 ], [ null, %bb1 ]
98 store i8 2, i8* %ptr.2, align 8
99 ret void
100 }
101
80102 ; CHECK-LABEL: test6
81103 ; CHECK: entry:
82104 ; CHECK-NOT: select
95117 store i8 2, i8* %ptr.2, align 8
96118 ret void
97119 }
120
121 ; CHECK-LABEL: test6_no_null_opt
122 ; CHECK: entry:
123 ; CHECK: %[[SEL:.*]] = select i1 %cond, i8* null, i8* %ptr
124 ; CHECK: store i8 2, i8* %[[SEL]]
125
126 define void @test6_no_null_opt(i1 %cond, i8* %ptr) #0 {
127 entry:
128 br i1 %cond, label %bb1, label %bb2
129
130 bb1:
131 br label %bb2
132
133 bb2:
134 %ptr.2 = phi i8* [ %ptr, %entry ], [ null, %bb1 ]
135 store i8 2, i8* %ptr.2, align 8
136 ret void
137 }
138
98139
99140 define i32 @test7(i1 %X) {
100141 entry:
126167 }
127168 ; CHECK-LABEL: define void @test8(
128169 ; CHECK: call void %Y(
170
171 define void @test8_no_null_opt(i1 %X, void ()* %Y) #0 {
172 entry:
173 br i1 %X, label %if, label %else
174
175 if:
176 br label %else
177
178 else:
179 %phi = phi void ()* [ %Y, %entry ], [ null, %if ]
180 call void %phi()
181 ret void
182 }
183 attributes #0 = { "null-pointer-is-valid"="true" }
184
185 ; CHECK-LABEL: define void @test8_no_null_opt(
186 ; CHECK: %[[SEL:.*]] = select i1 %X, void ()* null, void ()* %Y
187 ; CHECK: call void %[[SEL]]
4343 filter [0 x i8*] zeroinitializer
4444 %1 = extractvalue { i8*, i32 } %0, 0
4545 tail call void @__cxa_call_unexpected(i8* %1) noreturn nounwind
46 unreachable
47 }
48
49 ; CHECK-LABEL: @f2_no_null_opt(
50 define i8* @f2_no_null_opt() nounwind uwtable ssp #0 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
51 entry:
52 ; CHECK: invoke noalias i8* null()
53 %call = invoke noalias i8* null()
54 to label %invoke.cont unwind label %lpad
55
56 ; CHECK: invoke.cont:
57 ; CHECK: ret i8* %call
58 invoke.cont:
59 ret i8* %call
60
61 lpad:
62 %0 = landingpad { i8*, i32 }
63 filter [0 x i8*] zeroinitializer
64 %1 = extractvalue { i8*, i32 } %0, 0
65 tail call void @__cxa_call_unexpected(i8* %1) noreturn nounwind
66 ; CHECK: unreachable
4667 unreachable
4768 }
4869
136157 cleanup
137158 ret void
138159 }
160
161 attributes #0 = { "null-pointer-is-valid"="true" }
3030 ; CHECK: phi i32* [ %a, %if.then ], [ %c, %if.else ]
3131 }
3232
33 define i32 @test1_no_null_opt(i32* %a, i32 %b, i32* %c, i32 %d) nounwind #0 {
34 entry:
35 %tobool = icmp eq i32 %b, 0
36 br i1 %tobool, label %if.else, label %if.then
37
38 if.then: ; preds = %entry
39 tail call void @bar() nounwind
40 br label %if.end7
41
42 if.else: ; preds = %entry
43 %tobool3 = icmp eq i32 %d, 0
44 br i1 %tobool3, label %if.end7, label %if.then4
45
46 if.then4: ; preds = %if.else
47 tail call void @bar() nounwind
48 br label %if.end7
49
50 if.end7: ; preds = %if.else, %if.then4, %if.then
51 %x.0 = phi i32* [ %a, %if.then ], [ %c, %if.then4 ], [ null, %if.else ]
52 %tmp9 = load i32, i32* %x.0
53 ret i32 %tmp9
54
55 ; CHECK-LABEL: @test1_no_null_opt(
56 ; CHECK: if.then:
57 ; CHECK: if.else:
58 ; CHECK: if.then4:
59 ; CHECK: br label %if.end7
60 ; CHECK: if.end7:
61 ; CHECK-NEXT: phi i32* [ %a, %if.then ], [ %c, %if.then4 ], [ null, %if.else ]
62 }
63
3364 define i32 @test2(i32* %a, i32 %b, i32* %c, i32 %d) nounwind {
3465 entry:
3566 %tobool = icmp eq i32 %b, 0
5889 ; CHECK-NOT: phi
5990 }
6091
92 define i32 @test2_no_null_opt(i32* %a, i32 %b, i32* %c, i32 %d) nounwind #0 {
93 entry:
94 %tobool = icmp eq i32 %b, 0
95 br i1 %tobool, label %if.else, label %if.then
96
97 if.then: ; preds = %entry
98 tail call void @bar() nounwind
99 br label %if.end7
100
101 if.else: ; preds = %entry
102 %tobool3 = icmp eq i32 %d, 0
103 br i1 %tobool3, label %if.end7, label %if.then4
104
105 if.then4: ; preds = %if.else
106 tail call void @bar() nounwind
107 br label %if.end7
108
109 if.end7: ; preds = %if.else, %if.then4, %if.then
110 %x.0 = phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
111 %tmp9 = load i32, i32* %x.0
112 ret i32 %tmp9
113 ; CHECK-LABEL: @test2_no_null_opt(
114 ; CHECK: if.then:
115 ; CHECK: if.else:
116 ; CHECK: if.then4:
117 ; CHECK: if.end7:
118 ; CHECK-NEXT: phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
119 }
120
61121 define i32 @test3(i32* %a, i32 %b, i32* %c, i32 %d) nounwind {
62122 entry:
63123 %tobool = icmp eq i32 %b, 0
83143 ; CHECK-LABEL: @test3(
84144 ; CHECK: if.end7:
85145 ; CHECK: phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
146 }
147
148 define i32 @test3_no_null_opt(i32* %a, i32 %b, i32* %c, i32 %d) nounwind #0 {
149 entry:
150 %tobool = icmp eq i32 %b, 0
151 br i1 %tobool, label %if.else, label %if.then
152
153 if.then: ; preds = %entry
154 tail call void @bar() nounwind
155 br label %if.end7
156
157 if.else: ; preds = %entry
158 %tobool3 = icmp eq i32 %d, 0
159 br i1 %tobool3, label %if.end7, label %if.then4
160
161 if.then4: ; preds = %if.else
162 tail call void @bar() nounwind
163 br label %if.end7
164
165 if.end7: ; preds = %if.else, %if.then4, %if.then
166 %x.0 = phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
167 tail call void @bar() nounwind
168 %tmp9 = load i32, i32* %x.0
169 ret i32 %tmp9
170 ; CHECK-LABEL: @test3_no_null_opt(
171 ; CHECK: if.then:
172 ; CHECK: if.else:
173 ; CHECK: if.then4:
174 ; CHECK: if.end7:
175 ; CHECK-NEXT: phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
86176 }
87177
88178 define i32 @test4(i32* %a, i32 %b, i32* %c, i32 %d) nounwind {
112202 ; CHECK-LABEL: @test4(
113203 ; CHECK-NOT: phi
114204 }
205
206 define i32 @test4_no_null_opt(i32* %a, i32 %b, i32* %c, i32 %d) nounwind #0 {
207 entry:
208 %tobool = icmp eq i32 %b, 0
209 br i1 %tobool, label %if.else, label %if.then
210
211 if.then: ; preds = %entry
212 tail call void @bar() nounwind
213 br label %if.end7
214
215 if.else: ; preds = %entry
216 %tobool3 = icmp eq i32 %d, 0
217 br i1 %tobool3, label %if.end7, label %if.then4
218
219 if.then4: ; preds = %if.else
220 tail call void @bar() nounwind
221 br label %if.end7
222
223 if.end7: ; preds = %if.else, %if.then4, %if.then
224 %x.0 = phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
225 %gep = getelementptr i32, i32* %x.0, i32 10
226 %tmp9 = load i32, i32* %gep
227 %tmp10 = or i32 %tmp9, 1
228 store i32 %tmp10, i32* %gep
229 ret i32 %tmp9
230 ; CHECK-LABEL: @test4_no_null_opt(
231 ; CHECK: if.then:
232 ; CHECK: if.else:
233 ; CHECK: if.then4:
234 ; CHECK: if.end7:
235 ; CHECK-NEXT: phi i32* [ %a, %if.then ], [ null, %if.then4 ], [ null, %if.else ]
236 }
237
238 attributes #0 = { "null-pointer-is-valid"="true" }
0 ; RUN: opt -S -simplifycfg < %s | FileCheck %s
1 define void @foo() nounwind ssp #0 !dbg !0 {
2 ; CHECK: store i32 42, i32* null
3 ; CHECK-NOT: call void @llvm.trap()
4 ; CHECK: ret void
5 store i32 42, i32* null, !dbg !5
6 ret void, !dbg !7
7 }
8
9 attributes #0 = { "null-pointer-is-valid"="true" }
10
11 !llvm.dbg.cu = !{!2}
12 !llvm.module.flags = !{!10}
13
14 !0 = distinct !DISubprogram(name: "foo", line: 3, isLocal: false, isDefinition: true, virtualIndex: 6, isOptimized: false, unit: !2, file: !8, scope: !1, type: !3)
15 !1 = !DIFile(filename: "foo.c", directory: "/private/tmp")
16 !2 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "Apple clang version 3.0 (tags/Apple/clang-206.1) (based on LLVM 3.0svn)", isOptimized: true, emissionKind: FullDebug, file: !8, enums: !{}, retainedTypes: !{})
17 !3 = !DISubroutineType(types: !4)
18 !4 = !{null}
19 !5 = !DILocation(line: 4, column: 2, scope: !6)
20 !6 = distinct !DILexicalBlock(line: 3, column: 12, file: !8, scope: !0)
21 !7 = !DILocation(line: 5, column: 1, scope: !6)
22 !8 = !DIFile(filename: "foo.c", directory: "/private/tmp")
23 !10 = !{i32 1, !"Debug Info Version", i32 3}
2020 ; CHECK: load volatile
2121 }
2222
23 define void @test1_no_null_opt(i32 %x) nounwind #0 {
24 entry:
25 %0 = icmp eq i32 %x, 0 ; [#uses=1]
26 br i1 %0, label %bb, label %return
27
28 bb: ; preds = %entry
29 %1 = load volatile i32, i32* null
30 unreachable
31
32 br label %return
33 return: ; preds = %entry
34 ret void
35 ; CHECK-LABEL: @test1_no_null_opt(
36 ; CHECK: load volatile
37 ; CHECK: unreachable
38 }
39
2340 ; rdar://7958343
2441 define void @test2() nounwind {
2542 entry:
2643 store i32 4,i32* null
2744 ret void
28
45
2946 ; CHECK-LABEL: @test2(
3047 ; CHECK: call void @llvm.trap
3148 ; CHECK: unreachable
49 }
50
51 define void @test2_no_null_opt() nounwind #0 {
52 entry:
53 store i32 4,i32* null
54 ret void
55 ; CHECK-LABEL: @test2_no_null_opt(
56 ; CHECK: store i32 4, i32* null
57 ; CHECK-NOT: call void @llvm.trap
58 ; CHECK: ret
3259 }
3360
3461 ; PR7369
3865 ret void
3966
4067 ; CHECK-LABEL: @test3(
68 ; CHECK: store volatile i32 4, i32* null
69 ; CHECK: ret
70 }
71
72 define void @test3_no_null_opt() nounwind #0 {
73 entry:
74 store volatile i32 4, i32* null
75 ret void
76
77 ; CHECK-LABEL: @test3_no_null_opt(
4178 ; CHECK: store volatile i32 4, i32* null
4279 ; CHECK: ret
4380 }
84121 ret void
85122 }
86123
124 attributes #0 = { "null-pointer-is-valid"="true" }
155155
156156 // Build the various AA results and register them.
157157 AC.reset(new AssumptionCache(F));
158 BAR.reset(new BasicAAResult(M.getDataLayout(), TLI, *AC));
158 BAR.reset(new BasicAAResult(M.getDataLayout(), F, TLI, *AC));
159159 AAR->addAAResult(*BAR);
160160
161161 return *AAR;
4949
5050 TestAnalyses(MemorySSATest &Test)
5151 : DT(*Test.F), AC(*Test.F), AA(Test.TLI),
52 BAA(Test.DL, Test.TLI, AC, &DT) {
52 BAA(Test.DL, *Test.F, Test.TLI, AC, &DT) {
5353 AA.addAAResult(BAA);
5454 MSSA = make_unique(*Test.F, &AA, &DT);
5555 Walker = MSSA->getWalker();