llvm.org GIT mirror llvm / 5a057dc
[InstCombine] Factor the logic for propagating !nonnull and !range metadata out of InstCombine and into helpers. NFC, this just exposes the logic used by InstCombine when propagating metadata from one load instruction to another. The plan is to use this in SROA to address PR32902. If anyone has better ideas about how to factor this or name variables, I'm all ears, but this seemed like a pretty good start and lets us make progress on the PR. This is based on a patch by Ariel Ben-Yehuda (D34285). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306267 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 2 years ago
3 changed file(s) with 64 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
379379 /// during lowering by the GC infrastructure.
380380 bool callsGCLeafFunction(ImmutableCallSite CS);
381381
382 /// Copy a nonnull metadata node to a new load instruction.
383 ///
384 /// This handles mapping it to range metadata if the new load is an integer
385 /// load instead of a pointer load.
386 void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI);
387
388 /// Copy a range metadata node to a new load instruction.
389 ///
390 /// This handles mapping it to nonnull metadata if the new load is a pointer
391 /// load instead of an integer load and the range doesn't cover null.
392 void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N,
393 LoadInst &NewLI);
394
382395 //===----------------------------------------------------------------------===//
383396 // Intrinsic pattern matching
384397 //
488488 break;
489489
490490 case LLVMContext::MD_nonnull:
491 // This only directly applies if the new type is also a pointer.
492 if (NewTy->isPointerTy()) {
493 NewLoad->setMetadata(ID, N);
494 break;
495 }
496 // If it's integral now, translate it to !range metadata.
497 if (NewTy->isIntegerTy()) {
498 auto *ITy = cast(NewTy);
499 auto *NullInt = ConstantExpr::getPtrToInt(
500 ConstantPointerNull::get(cast(Ptr->getType())), ITy);
501 auto *NonNullInt =
502 ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
503 NewLoad->setMetadata(LLVMContext::MD_range,
504 MDB.createRange(NonNullInt, NullInt));
505 }
491 copyNonnullMetadata(LI, N, *NewLoad);
506492 break;
507493 case LLVMContext::MD_align:
508494 case LLVMContext::MD_dereferenceable:
512498 NewLoad->setMetadata(ID, N);
513499 break;
514500 case LLVMContext::MD_range:
515 // FIXME: It would be nice to propagate this in some way, but the type
516 // conversions make it hard.
517
518 // If it's a pointer now and the range does not contain 0, make it !nonnull.
519 if (NewTy->isPointerTy()) {
520 unsigned BitWidth = IC.getDataLayout().getTypeSizeInBits(NewTy);
521 if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
522 MDNode *NN = MDNode::get(LI.getContext(), None);
523 NewLoad->setMetadata(LLVMContext::MD_nonnull, NN);
524 }
525 }
501 copyRangeMetadata(IC.getDataLayout(), LI, N, *NewLoad);
526502 break;
527503 }
528504 }
2525 #include "llvm/Analysis/MemoryBuiltins.h"
2626 #include "llvm/Analysis/ValueTracking.h"
2727 #include "llvm/IR/CFG.h"
28 #include "llvm/IR/ConstantRange.h"
2829 #include "llvm/IR/Constants.h"
2930 #include "llvm/IR/DIBuilder.h"
3031 #include "llvm/IR/DataLayout.h"
10801081 }
10811082
10821083 /// See if there is a dbg.value intrinsic for DIVar for the PHI node.
1083 static bool PhiHasDebugValue(DILocalVariable *DIVar,
1084 static bool PhiHasDebugValue(DILocalVariable *DIVar,
10841085 DIExpression *DIExpr,
10851086 PHINode *APN) {
10861087 // Since we can't guarantee that the original dbg.declare instrinsic
11581159 DbgValue->insertAfter(LI);
11591160 }
11601161
1161 /// Inserts a llvm.dbg.value intrinsic after a phi
1162 /// Inserts a llvm.dbg.value intrinsic after a phi
11621163 /// that has an associated llvm.dbg.decl intrinsic.
11631164 void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
11641165 PHINode *APN, DIBuilder &Builder) {
17411742 // Preserve !invariant.group in K.
17421743 break;
17431744 case LLVMContext::MD_align:
1744 K->setMetadata(Kind,
1745 K->setMetadata(Kind,
17451746 MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
17461747 break;
17471748 case LLVMContext::MD_dereferenceable:
17481749 case LLVMContext::MD_dereferenceable_or_null:
1749 K->setMetadata(Kind,
1750 K->setMetadata(Kind,
17501751 MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
17511752 break;
17521753 }
18441845 }
18451846
18461847 return false;
1848 }
1849
1850 void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N,
1851 LoadInst &NewLI) {
1852 auto *NewTy = NewLI.getType();
1853
1854 // This only directly applies if the new type is also a pointer.
1855 if (NewTy->isPointerTy()) {
1856 NewLI.setMetadata(LLVMContext::MD_nonnull, N);
1857 return;
1858 }
1859
1860 // The only other translation we can do is to integral loads with !range
1861 // metadata.
1862 if (!NewTy->isIntegerTy())
1863 return;
1864
1865 MDBuilder MDB(NewLI.getContext());
1866 const Value *Ptr = OldLI.getPointerOperand();
1867 auto *ITy = cast(NewTy);
1868 auto *NullInt = ConstantExpr::getPtrToInt(
1869 ConstantPointerNull::get(cast(Ptr->getType())), ITy);
1870 auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
1871 NewLI.setMetadata(LLVMContext::MD_range,
1872 MDB.createRange(NonNullInt, NullInt));
1873 }
1874
1875 void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI,
1876 MDNode *N, LoadInst &NewLI) {
1877 auto *NewTy = NewLI.getType();
1878
1879 // Give up unless it is converted to a pointer where there is a single very
1880 // valuable mapping we can do reliably.
1881 // FIXME: It would be nice to propagate this in more ways, but the type
1882 // conversions make it hard.
1883 if (!NewTy->isPointerTy())
1884 return;
1885
1886 unsigned BitWidth = DL.getTypeSizeInBits(NewTy);
1887 if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
1888 MDNode *NN = MDNode::get(OldLI.getContext(), None);
1889 NewLI.setMetadata(LLVMContext::MD_nonnull, NN);
1890 }
18471891 }
18481892
18491893 namespace {
19672011 unsigned NumMaskedBits = AndMask.countPopulation();
19682012 if (!MatchBitReversals && NumMaskedBits % 8 != 0)
19692013 return Result;
1970
2014
19712015 auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
19722016 MatchBitReversals, BPS);
19732017 if (!Res)