llvm-6502/lib/Analysis
Duncan P. N. Exon Smith dad20b2ae2 IR: Split Metadata from Value
Split `Metadata` away from the `Value` class hierarchy, as part of
PR21532.  Assembly and bitcode changes are in the wings, but this is the
bulk of the change for the IR C++ API.

I have a follow-up patch prepared for `clang`.  If this breaks other
sub-projects, I apologize in advance :(.  Help me compile it on Darwin
I'll try to fix it.  FWIW, the errors should be easy to fix, so it may
be simpler to just fix it yourself.

This breaks the build for all metadata-related code that's out-of-tree.
Rest assured the transition is mechanical and the compiler should catch
almost all of the problems.

Here's a quick guide for updating your code:

  - `Metadata` is the root of a class hierarchy with three main classes:
    `MDNode`, `MDString`, and `ValueAsMetadata`.  It is distinct from
    the `Value` class hierarchy.  It is typeless -- i.e., instances do
    *not* have a `Type`.

  - `MDNode`'s operands are all `Metadata *` (instead of `Value *`).

  - `TrackingVH<MDNode>` and `WeakVH` referring to metadata can be
    replaced with `TrackingMDNodeRef` and `TrackingMDRef`, respectively.

    If you're referring solely to resolved `MDNode`s -- post graph
    construction -- just use `MDNode*`.

  - `MDNode` (and the rest of `Metadata`) have only limited support for
    `replaceAllUsesWith()`.

    As long as an `MDNode` is pointing at a forward declaration -- the
    result of `MDNode::getTemporary()` -- it maintains a side map of its
    uses and can RAUW itself.  Once the forward declarations are fully
    resolved RAUW support is dropped on the ground.  This means that
    uniquing collisions on changing operands cause nodes to become
    "distinct".  (This already happened fairly commonly, whenever an
    operand went to null.)

    If you're constructing complex (non self-reference) `MDNode` cycles,
    you need to call `MDNode::resolveCycles()` on each node (or on a
    top-level node that somehow references all of the nodes).  Also,
    don't do that.  Metadata cycles (and the RAUW machinery needed to
    construct them) are expensive.

  - An `MDNode` can only refer to a `Constant` through a bridge called
    `ConstantAsMetadata` (one of the subclasses of `ValueAsMetadata`).

    As a side effect, accessing an operand of an `MDNode` that is known
    to be, e.g., `ConstantInt`, takes three steps: first, cast from
    `Metadata` to `ConstantAsMetadata`; second, extract the `Constant`;
    third, cast down to `ConstantInt`.

    The eventual goal is to introduce `MDInt`/`MDFloat`/etc. and have
    metadata schema owners transition away from using `Constant`s when
    the type isn't important (and they don't care about referring to
    `GlobalValue`s).

    In the meantime, I've added transitional API to the `mdconst`
    namespace that matches semantics with the old code, in order to
    avoid adding the error-prone three-step equivalent to every call
    site.  If your old code was:

        MDNode *N = foo();
        bar(isa             <ConstantInt>(N->getOperand(0)));
        baz(cast            <ConstantInt>(N->getOperand(1)));
        bak(cast_or_null    <ConstantInt>(N->getOperand(2)));
        bat(dyn_cast        <ConstantInt>(N->getOperand(3)));
        bay(dyn_cast_or_null<ConstantInt>(N->getOperand(4)));

    you can trivially match its semantics with:

        MDNode *N = foo();
        bar(mdconst::hasa               <ConstantInt>(N->getOperand(0)));
        baz(mdconst::extract            <ConstantInt>(N->getOperand(1)));
        bak(mdconst::extract_or_null    <ConstantInt>(N->getOperand(2)));
        bat(mdconst::dyn_extract        <ConstantInt>(N->getOperand(3)));
        bay(mdconst::dyn_extract_or_null<ConstantInt>(N->getOperand(4)));

    and when you transition your metadata schema to `MDInt`:

        MDNode *N = foo();
        bar(isa             <MDInt>(N->getOperand(0)));
        baz(cast            <MDInt>(N->getOperand(1)));
        bak(cast_or_null    <MDInt>(N->getOperand(2)));
        bat(dyn_cast        <MDInt>(N->getOperand(3)));
        bay(dyn_cast_or_null<MDInt>(N->getOperand(4)));

  - A `CallInst` -- specifically, intrinsic instructions -- can refer to
    metadata through a bridge called `MetadataAsValue`.  This is a
    subclass of `Value` where `getType()->isMetadataTy()`.

    `MetadataAsValue` is the *only* class that can legally refer to a
    `LocalAsMetadata`, which is a bridged form of non-`Constant` values
    like `Argument` and `Instruction`.  It can also refer to any other
    `Metadata` subclass.

(I'll break all your testcases in a follow-up commit, when I propagate
this change to assembly.)

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223802 91177308-0d34-0410-b5e6-96231b3b80d8
2014-12-09 18:38:53 +00:00
..
IPA Remove the unused FindUsedTypes pass. 2014-11-24 20:53:26 +00:00
AliasAnalysis.cpp Reformat partially, where I touched for whitespace changes. 2014-10-28 11:54:52 +00:00
AliasAnalysisCounter.cpp [C++11] More 'nullptr' conversion. In some cases just using a boolean check instead of comparing to nullptr. 2014-04-15 04:59:12 +00:00
AliasAnalysisEvaluator.cpp AA metadata refactoring (introduce AAMDNodes) 2014-07-24 12:16:19 +00:00
AliasDebugger.cpp [C++11] Add 'override' keyword to virtual methods that override their base class. 2014-03-05 07:30:04 +00:00
AliasSetTracker.cpp AliasSet: Simplify mergeSetIn 2014-11-19 19:36:18 +00:00
Analysis.cpp Revert "Don't make assumptions about the name of private global variables." 2014-11-15 02:03:53 +00:00
AssumptionTracker.cpp Clean up assume intrinsic pattern matching, no need to check that the argument is a value. 2014-10-25 18:09:01 +00:00
BasicAliasAnalysis.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
BlockFrequencyInfo.cpp Revert "Introduce a string_ostream string builder facilty" 2014-06-26 22:52:05 +00:00
BlockFrequencyInfoImpl.cpp BFI: Saturate when combining edges to a successor 2014-12-05 19:13:42 +00:00
BranchProbabilityInfo.cpp IR: Split Metadata from Value 2014-12-09 18:38:53 +00:00
CaptureTracking.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
CFG.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
CFGPrinter.cpp Modernize raw_fd_ostream's constructor a bit. 2014-08-25 18:16:47 +00:00
CFLAliasAnalysis.cpp Remove redundant virtual on overriden functions. 2014-11-14 19:06:36 +00:00
CGSCCPassManager.cpp [PM] Add a new-PM-style CGSCC pass manager using the newly added 2014-04-21 11:12:00 +00:00
CMakeLists.txt Add file to CMake build as well. 2014-09-18 00:39:20 +00:00
CodeMetrics.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
ConstantFolding.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
CostModel.cpp [CostModel][x86] Improved cost model for alternate shuffles. 2014-07-03 22:24:18 +00:00
Delinearization.cpp remove BasePointer before delinearizing 2014-05-27 22:41:51 +00:00
DependenceAnalysis.cpp [DependenceAnalysis] Allow subscripts of different types 2014-11-16 16:52:44 +00:00
DominanceFrontier.cpp Templatify DominanceFrontier. 2014-07-12 21:59:52 +00:00
DomPrinter.cpp [PM] Split DominatorTree into a concrete analysis result object which 2014-01-13 13:07:17 +00:00
FunctionTargetTransformInfo.cpp Add a new pass FunctionTargetTransformInfo. This pass serves as a 2014-09-18 00:34:14 +00:00
InstCount.cpp [Modules] Fix potential ODR violations by sinking the DEBUG_TYPE 2014-04-22 02:48:03 +00:00
InstructionSimplify.cpp InstSimplify: Try to bring back the rest of r223583 2014-12-08 18:30:43 +00:00
Interval.cpp Revert "[C++11] Add predecessors(BasicBlock *) / successors(BasicBlock *) iterator ranges." 2014-07-21 17:06:51 +00:00
IntervalPartition.cpp [C++11] More 'nullptr' conversion. In some cases just using a boolean check instead of comparing to nullptr. 2014-04-15 04:59:12 +00:00
IVUsers.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
JumpInstrTableInfo.cpp Add Forward Control-Flow Integrity. 2014-11-11 21:08:02 +00:00
LazyCallGraph.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
LazyValueInfo.cpp LazyValueInfo: Actually re-visit partially solved block-values in solveBlockValue() 2014-11-25 17:23:05 +00:00
LibCallAliasAnalysis.cpp [C++11] More 'nullptr' conversion. In some cases just using a boolean check instead of comparing to nullptr. 2014-04-15 04:59:12 +00:00
LibCallSemantics.cpp remove function names from comments; NFC 2014-10-21 18:26:57 +00:00
Lint.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
LLVMBuild.txt
Loads.cpp Revert r220349 to re-instate r220277 with a fix for PR21330 -- quite 2014-11-25 08:20:27 +00:00
LoopInfo.cpp Revert "IR: MDNode => Value" 2014-11-11 21:30:22 +00:00
LoopPass.cpp Fix assertion in LICM doFinalization() 2014-09-24 16:48:31 +00:00
Makefile
MemDepPrinter.cpp [C++11] More 'nullptr' conversion. In some cases just using a boolean check instead of comparing to nullptr. 2014-04-15 04:59:12 +00:00
MemoryBuiltins.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
MemoryDependenceAnalysis.cpp Relax an assert a bit to avoid a crash on unreachable code. 2014-12-01 02:55:24 +00:00
ModuleDebugInfoPrinter.cpp [C++11] Change DebugInfoFinder to use range-based loops 2014-03-18 09:41:07 +00:00
NoAliasAnalysis.cpp Simplify code. No functionality change. 2014-10-05 12:21:57 +00:00
PHITransAddr.cpp Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.) 2014-09-07 18:57:58 +00:00
PostDominators.cpp [Modules] Fix potential ODR violations by sinking the DEBUG_TYPE 2014-04-22 02:48:03 +00:00
PtrUseVisitor.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
README.txt
RegionInfo.cpp Fix msc17 build. RegionInfo::RegionInfo::recalculate() doesn't make sense. 2014-07-20 03:57:51 +00:00
RegionPass.cpp Templatify RegionInfo so it works on MachineBasicBlocks 2014-07-19 18:29:29 +00:00
RegionPrinter.cpp Templatify RegionInfo so it works on MachineBasicBlocks 2014-07-19 18:29:29 +00:00
ScalarEvolution.cpp IR: Split Metadata from Value 2014-12-09 18:38:53 +00:00
ScalarEvolutionAliasAnalysis.cpp AA metadata refactoring (introduce AAMDNodes) 2014-07-24 12:16:19 +00:00
ScalarEvolutionExpander.cpp Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
ScalarEvolutionNormalization.cpp Fix typos in comments, NFC 2014-08-29 21:53:01 +00:00
ScopedNoAliasAA.cpp Revert "IR: MDNode => Value" 2014-11-11 21:30:22 +00:00
SparsePropagation.cpp [Modules] Fix potential ODR violations by sinking the DEBUG_TYPE 2014-04-22 02:48:03 +00:00
StratifiedSets.h Update SetVector to rely on the underlying set's insert to return a pair<iterator, bool> 2014-11-19 07:49:26 +00:00
TargetTransformInfo.cpp Masked Load / Store Intrinsics - the CodeGen part. 2014-12-04 09:40:44 +00:00
Trace.cpp Put the functionality for printing a value to a raw_ostream as an 2014-01-09 02:29:41 +00:00
TypeBasedAliasAnalysis.cpp IR: Split Metadata from Value 2014-12-09 18:38:53 +00:00
ValueTracking.cpp IR: Split Metadata from Value 2014-12-09 18:38:53 +00:00

Analysis Opportunities:

//===---------------------------------------------------------------------===//

In test/Transforms/LoopStrengthReduce/quadradic-exit-value.ll, the
ScalarEvolution expression for %r is this:

  {1,+,3,+,2}<loop>

Outside the loop, this could be evaluated simply as (%n * %n), however
ScalarEvolution currently evaluates it as

  (-2 + (2 * (trunc i65 (((zext i64 (-2 + %n) to i65) * (zext i64 (-1 + %n) to i65)) /u 2) to i64)) + (3 * %n))

In addition to being much more complicated, it involves i65 arithmetic,
which is very inefficient when expanded into code.

//===---------------------------------------------------------------------===//

In formatValue in test/CodeGen/X86/lsr-delayed-fold.ll,

ScalarEvolution is forming this expression:

((trunc i64 (-1 * %arg5) to i32) + (trunc i64 %arg5 to i32) + (-1 * (trunc i64 undef to i32)))

This could be folded to

(-1 * (trunc i64 undef to i32))

//===---------------------------------------------------------------------===//