Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 5a057dc

Browse files
committed
[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
1 parent 92c7507 commit 5a057dc

File tree

3 files changed

+64
-31
lines changed

3 files changed

+64
-31
lines changed

include/llvm/Transforms/Utils/Local.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,19 @@ unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
380380
/// during lowering by the GC infrastructure.
381381
bool callsGCLeafFunction(ImmutableCallSite CS);
382382

383+
/// Copy a nonnull metadata node to a new load instruction.
384+
///
385+
/// This handles mapping it to range metadata if the new load is an integer
386+
/// load instead of a pointer load.
387+
void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI);
388+
389+
/// Copy a range metadata node to a new load instruction.
390+
///
391+
/// This handles mapping it to nonnull metadata if the new load is a pointer
392+
/// load instead of an integer load and the range doesn't cover null.
393+
void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N,
394+
LoadInst &NewLI);
395+
383396
//===----------------------------------------------------------------------===//
384397
// Intrinsic pattern matching
385398
//

lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -489,21 +489,7 @@ static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewT
489489
break;
490490

491491
case LLVMContext::MD_nonnull:
492-
// This only directly applies if the new type is also a pointer.
493-
if (NewTy->isPointerTy()) {
494-
NewLoad->setMetadata(ID, N);
495-
break;
496-
}
497-
// If it's integral now, translate it to !range metadata.
498-
if (NewTy->isIntegerTy()) {
499-
auto *ITy = cast<IntegerType>(NewTy);
500-
auto *NullInt = ConstantExpr::getPtrToInt(
501-
ConstantPointerNull::get(cast<PointerType>(Ptr->getType())), ITy);
502-
auto *NonNullInt =
503-
ConstantExpr::getAdd(NullInt, ConstantInt::get(ITy, 1));
504-
NewLoad->setMetadata(LLVMContext::MD_range,
505-
MDB.createRange(NonNullInt, NullInt));
506-
}
492+
copyNonnullMetadata(LI, N, *NewLoad);
507493
break;
508494
case LLVMContext::MD_align:
509495
case LLVMContext::MD_dereferenceable:
@@ -513,17 +499,7 @@ static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewT
513499
NewLoad->setMetadata(ID, N);
514500
break;
515501
case LLVMContext::MD_range:
516-
// FIXME: It would be nice to propagate this in some way, but the type
517-
// conversions make it hard.
518-
519-
// If it's a pointer now and the range does not contain 0, make it !nonnull.
520-
if (NewTy->isPointerTy()) {
521-
unsigned BitWidth = IC.getDataLayout().getTypeSizeInBits(NewTy);
522-
if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
523-
MDNode *NN = MDNode::get(LI.getContext(), None);
524-
NewLoad->setMetadata(LLVMContext::MD_nonnull, NN);
525-
}
526-
}
502+
copyRangeMetadata(IC.getDataLayout(), LI, N, *NewLoad);
527503
break;
528504
}
529505
}

lib/Transforms/Utils/Local.cpp

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Analysis/MemoryBuiltins.h"
2727
#include "llvm/Analysis/ValueTracking.h"
2828
#include "llvm/IR/CFG.h"
29+
#include "llvm/IR/ConstantRange.h"
2930
#include "llvm/IR/Constants.h"
3031
#include "llvm/IR/DIBuilder.h"
3132
#include "llvm/IR/DataLayout.h"
@@ -1081,7 +1082,7 @@ static bool LdStHasDebugValue(DILocalVariable *DIVar, DIExpression *DIExpr,
10811082
}
10821083

10831084
/// 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,
10851086
DIExpression *DIExpr,
10861087
PHINode *APN) {
10871088
// Since we can't guarantee that the original dbg.declare instrinsic
@@ -1159,7 +1160,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
11591160
DbgValue->insertAfter(LI);
11601161
}
11611162

1162-
/// Inserts a llvm.dbg.value intrinsic after a phi
1163+
/// Inserts a llvm.dbg.value intrinsic after a phi
11631164
/// that has an associated llvm.dbg.decl intrinsic.
11641165
void llvm::ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
11651166
PHINode *APN, DIBuilder &Builder) {
@@ -1742,12 +1743,12 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J,
17421743
// Preserve !invariant.group in K.
17431744
break;
17441745
case LLVMContext::MD_align:
1745-
K->setMetadata(Kind,
1746+
K->setMetadata(Kind,
17461747
MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
17471748
break;
17481749
case LLVMContext::MD_dereferenceable:
17491750
case LLVMContext::MD_dereferenceable_or_null:
1750-
K->setMetadata(Kind,
1751+
K->setMetadata(Kind,
17511752
MDNode::getMostGenericAlignmentOrDereferenceable(JMD, KMD));
17521753
break;
17531754
}
@@ -1847,6 +1848,49 @@ bool llvm::callsGCLeafFunction(ImmutableCallSite CS) {
18471848
return false;
18481849
}
18491850

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+
18501894
namespace {
18511895
/// A potential constituent of a bitreverse or bswap expression. See
18521896
/// collectBitParts for a fuller explanation.
@@ -1968,7 +2012,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals,
19682012
unsigned NumMaskedBits = AndMask.countPopulation();
19692013
if (!MatchBitReversals && NumMaskedBits % 8 != 0)
19702014
return Result;
1971-
2015+
19722016
auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps,
19732017
MatchBitReversals, BPS);
19742018
if (!Res)

0 commit comments

Comments
 (0)