|
26 | 26 | #include "llvm/Analysis/MemoryBuiltins.h"
|
27 | 27 | #include "llvm/Analysis/ValueTracking.h"
|
28 | 28 | #include "llvm/IR/CFG.h"
|
| 29 | +#include "llvm/IR/ConstantRange.h" |
29 | 30 | #include "llvm/IR/Constants.h"
|
30 | 31 | #include "llvm/IR/DIBuilder.h"
|
31 | 32 | #include "llvm/IR/DataLayout.h"
|
@@ -1081,7 +1082,7 @@ static bool LdStHasDebugValue(DILocalVariable *DIVar, DIExpression *DIExpr,
|
1081 | 1082 | }
|
1082 | 1083 |
|
1083 | 1084 | /// See if there is a dbg.value intrinsic for DIVar for the PHI node.
|
1084 |
| -static bool PhiHasDebugValue(DILocalVariable *DIVar, |
| 1085 | +static bool PhiHasDebugValue(DILocalVariable *DIVar, |
1085 | 1086 | DIExpression *DIExpr,
|
1086 | 1087 | PHINode *APN) {
|
1087 | 1088 | // Since we can't guarantee that the original dbg.declare instrinsic
|
@@ -1159,7 +1160,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
|
1159 | 1160 | DbgValue->insertAfter(LI);
|
1160 | 1161 | }
|
1161 | 1162 |
|
1162 |
| -/// Inserts a llvm.dbg.value intrinsic after a phi |
| 1163 | +/// Inserts a llvm.dbg.value intrinsic after a phi |
1163 | 1164 | /// that has an associated llvm.dbg.decl intrinsic.
|
1164 | 1165 | void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
|
1165 | 1166 | PHINode *APN, DIBuilder &Builder) {
|
@@ -1742,12 +1743,12 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
|
1742 | 1743 | // Preserve !invariant.group in K.
|
1743 | 1744 | break;
|
1744 | 1745 | case LLVMContext::MD_align:
|
1745 |
| - K->setMetadata(Kind, |
| 1746 | + K->setMetadata(Kind, |
1746 | 1747 | MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
|
1747 | 1748 | break;
|
1748 | 1749 | case LLVMContext::MD_dereferenceable:
|
1749 | 1750 | case LLVMContext::MD_dereferenceable_or_null:
|
1750 |
| - K->setMetadata(Kind, |
| 1751 | + K->setMetadata(Kind, |
1751 | 1752 | MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
|
1752 | 1753 | break;
|
1753 | 1754 | }
|
@@ -1847,6 +1848,49 @@ bool llvm::callsGCLeafFunction(ImmutableCallSite CS) {
|
1847 | 1848 | return false;
|
1848 | 1849 | }
|
1849 | 1850 |
|
| 1851 | +void llvm::copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, |
| 1852 | + LoadInst &NewLI) { |
| 1853 | + auto *NewTy = NewLI.getType(); |
| 1854 | + |
| 1855 | + // This only directly applies if the new type is also a pointer. |
| 1856 | + if (NewTy->isPointerTy()) { |
| 1857 | + NewLI.setMetadata(LLVMContext::MD_nonnull, N); |
| 1858 | + return; |
| 1859 | + } |
| 1860 | + |
| 1861 | + // The only other translation we can do is to integral loads with !range |
| 1862 | + // metadata. |
| 1863 | + if (!NewTy->isIntegerTy()) |
| 1864 | + return; |
| 1865 | + |
| 1866 | + MDBuilder MDB(NewLI.getContext()); |
| 1867 | + const Value *Ptr = OldLI.getPointerOperand(); |
| 1868 | + auto *ITy = cast<IntegerType>(NewTy); |
| 1869 | + auto *NullInt = ConstantExpr::getPtrToInt( |
| 1870 | + ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy); |
| 1871 | + auto *NonNullInt = ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1)); |
| 1872 | + NewLI.setMetadata(LLVMContext::MD_range, |
| 1873 | + MDB.createRange(NonNullInt, NullInt)); |
| 1874 | +} |
| 1875 | + |
| 1876 | +void llvm::copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, |
| 1877 | + MDNode *N, LoadInst &NewLI) { |
| 1878 | + auto *NewTy = NewLI.getType(); |
| 1879 | + |
| 1880 | + // Give up unless it is converted to a pointer where there is a single very |
| 1881 | + // valuable mapping we can do reliably. |
| 1882 | + // FIXME: It would be nice to propagate this in more ways, but the type |
| 1883 | + // conversions make it hard. |
| 1884 | + if (!NewTy->isPointerTy()) |
| 1885 | + return; |
| 1886 | + |
| 1887 | + unsigned BitWidth = DL.getTypeSizeInBits(NewTy); |
| 1888 | + if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) { |
| 1889 | + MDNode *NN = MDNode::get(OldLI.getContext(), None); |
| 1890 | + NewLI.setMetadata(LLVMContext::MD_nonnull, NN); |
| 1891 | + } |
| 1892 | +} |
| 1893 | + |
1850 | 1894 | namespace {
|
1851 | 1895 | /// A potential constituent of a bitreverse or bswap expression. See
|
1852 | 1896 | /// collectBitParts for a fuller explanation.
|
@@ -1968,7 +2012,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
|
1968 | 2012 | unsigned NumMaskedBits = AndMask.countPopulation();
|
1969 | 2013 | if (!MatchBitReversals && NumMaskedBits % 8 != 0)
|
1970 | 2014 | return Result;
|
1971 |
| - |
| 2015 | + |
1972 | 2016 | auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
|
1973 | 2017 | MatchBitReversals, BPS);
|
1974 | 2018 | if (!Res)
|
|
0 commit comments