2014-09-08 04:05:11 +08:00
|
|
|
//===----------------------- AlignmentFromAssumptions.cpp -----------------===//
|
|
|
|
// Set Load/Store Alignments From Assumptions
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a ScalarEvolution-based transformation to set
|
|
|
|
// the alignments of load, stores and memory intrinsics based on the truth
|
|
|
|
// expressions of assume intrinsics. The primary motivation is to handle
|
|
|
|
// complex alignment assumptions that apply to vector loads and stores that
|
|
|
|
// appear after vectorization and unrolling.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define AA_NAME "alignment-from-assumptions"
|
|
|
|
#define DEBUG_TYPE AA_NAME
|
2016-06-15 14:18:01 +08:00
|
|
|
#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
|
2014-09-08 04:05:11 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-12-12 01:46:01 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2016-12-19 16:22:17 +08:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
2014-09-08 04:05:11 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-09-08 04:05:11 +08:00
|
|
|
#include "llvm/IR/Constant.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-09-08 04:05:11 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2014-09-08 04:05:11 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumLoadAlignChanged,
|
|
|
|
"Number of loads changed by alignment assumptions");
|
|
|
|
STATISTIC(NumStoreAlignChanged,
|
|
|
|
"Number of stores changed by alignment assumptions");
|
|
|
|
STATISTIC(NumMemIntAlignChanged,
|
|
|
|
"Number of memory intrinsics changed by alignment assumptions");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct AlignmentFromAssumptions : public FunctionPass {
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
AlignmentFromAssumptions() : FunctionPass(ID) {
|
|
|
|
initializeAlignmentFromAssumptionsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:11:45 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2014-09-08 04:05:11 +08:00
|
|
|
|
2015-04-11 10:11:45 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2016-12-19 16:22:17 +08:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
[PM] Port ScalarEvolution to the new pass manager.
This change makes ScalarEvolution a stand-alone object and just produces
one from a pass as needed. Making this work well requires making the
object movable, using references instead of overwritten pointers in
a number of places, and other refactorings.
I've also wired it up to the new pass manager and added a RUN line to
a test to exercise it under the new pass manager. This includes basic
printing support much like with other analyses.
But there is a big and somewhat scary change here. Prior to this patch
ScalarEvolution was never *actually* invalidated!!! Re-running the pass
just re-wired up the various other analyses and didn't remove any of the
existing entries in the SCEV caches or clear out anything at all. This
might seem OK as everything in SCEV that can uses ValueHandles to track
updates to the values that serve as SCEV keys. However, this still means
that as we ran SCEV over each function in the module, we kept
accumulating more and more SCEVs into the cache. At the end, we would
have a SCEV cache with every value that we ever needed a SCEV for in the
entire module!!! Yowzers. The releaseMemory routine would dump all of
this, but that isn't realy called during normal runs of the pipeline as
far as I can see.
To make matters worse, there *is* actually a key that we don't update
with value handles -- there is a map keyed off of Loop*s. Because
LoopInfo *does* release its memory from run to run, it is entirely
possible to run SCEV over one function, then over another function, and
then lookup a Loop* from the second function but find an entry inserted
for the first function! Ouch.
To make matters still worse, there are plenty of updates that *don't*
trip a value handle. It seems incredibly unlikely that today GVN or
another pass that invalidates SCEV can update values in *just* such
a way that a subsequent run of SCEV will incorrectly find lookups in
a cache, but it is theoretically possible and would be a nightmare to
debug.
With this refactoring, I've fixed all this by actually destroying and
recreating the ScalarEvolution object from run to run. Technically, this
could increase the amount of malloc traffic we see, but then again it is
also technically correct. ;] I don't actually think we're suffering from
tons of malloc traffic from SCEV because if we were, the fact that we
never clear the memory would seem more likely to have come up as an
actual problem before now. So, I've made the simple fix here. If in fact
there are serious issues with too much allocation and deallocation,
I can work on a clever fix that preserves the allocations (while
clearing the data) between each run, but I'd prefer to do that kind of
optimization with a test case / benchmark that shows why we need such
cleverness (and that can test that we actually make it faster). It's
possible that this will make some things faster by making the SCEV
caches have higher locality (due to being significantly smaller) so
until there is a clear benchmark, I think the simple change is best.
Differential Revision: http://reviews.llvm.org/D12063
llvm-svn: 245193
2015-08-17 10:08:17 +08:00
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
2014-09-08 04:05:11 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
|
|
|
|
|
|
|
AU.setPreservesCFG();
|
2015-12-12 01:46:01 +08:00
|
|
|
AU.addPreserved<AAResultsWrapperPass>();
|
|
|
|
AU.addPreserved<GlobalsAAWrapperPass>();
|
2015-01-17 22:16:18 +08:00
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2014-09-08 04:05:11 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
[PM] Port ScalarEvolution to the new pass manager.
This change makes ScalarEvolution a stand-alone object and just produces
one from a pass as needed. Making this work well requires making the
object movable, using references instead of overwritten pointers in
a number of places, and other refactorings.
I've also wired it up to the new pass manager and added a RUN line to
a test to exercise it under the new pass manager. This includes basic
printing support much like with other analyses.
But there is a big and somewhat scary change here. Prior to this patch
ScalarEvolution was never *actually* invalidated!!! Re-running the pass
just re-wired up the various other analyses and didn't remove any of the
existing entries in the SCEV caches or clear out anything at all. This
might seem OK as everything in SCEV that can uses ValueHandles to track
updates to the values that serve as SCEV keys. However, this still means
that as we ran SCEV over each function in the module, we kept
accumulating more and more SCEVs into the cache. At the end, we would
have a SCEV cache with every value that we ever needed a SCEV for in the
entire module!!! Yowzers. The releaseMemory routine would dump all of
this, but that isn't realy called during normal runs of the pipeline as
far as I can see.
To make matters worse, there *is* actually a key that we don't update
with value handles -- there is a map keyed off of Loop*s. Because
LoopInfo *does* release its memory from run to run, it is entirely
possible to run SCEV over one function, then over another function, and
then lookup a Loop* from the second function but find an entry inserted
for the first function! Ouch.
To make matters still worse, there are plenty of updates that *don't*
trip a value handle. It seems incredibly unlikely that today GVN or
another pass that invalidates SCEV can update values in *just* such
a way that a subsequent run of SCEV will incorrectly find lookups in
a cache, but it is theoretically possible and would be a nightmare to
debug.
With this refactoring, I've fixed all this by actually destroying and
recreating the ScalarEvolution object from run to run. Technically, this
could increase the amount of malloc traffic we see, but then again it is
also technically correct. ;] I don't actually think we're suffering from
tons of malloc traffic from SCEV because if we were, the fact that we
never clear the memory would seem more likely to have come up as an
actual problem before now. So, I've made the simple fix here. If in fact
there are serious issues with too much allocation and deallocation,
I can work on a clever fix that preserves the allocations (while
clearing the data) between each run, but I'd prefer to do that kind of
optimization with a test case / benchmark that shows why we need such
cleverness (and that can test that we actually make it faster). It's
possible that this will make some things faster by making the SCEV
caches have higher locality (due to being significantly smaller) so
until there is a clear benchmark, I think the simple change is best.
Differential Revision: http://reviews.llvm.org/D12063
llvm-svn: 245193
2015-08-17 10:08:17 +08:00
|
|
|
AU.addPreserved<ScalarEvolutionWrapperPass>();
|
2014-09-08 04:05:11 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 14:18:01 +08:00
|
|
|
AlignmentFromAssumptionsPass Impl;
|
2014-09-08 04:05:11 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-09-08 04:05:11 +08:00
|
|
|
|
|
|
|
char AlignmentFromAssumptions::ID = 0;
|
|
|
|
static const char aip_name[] = "Alignment from assumptions";
|
|
|
|
INITIALIZE_PASS_BEGIN(AlignmentFromAssumptions, AA_NAME,
|
|
|
|
aip_name, false, false)
|
2016-12-19 16:22:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
2014-09-08 04:05:11 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
[PM] Port ScalarEvolution to the new pass manager.
This change makes ScalarEvolution a stand-alone object and just produces
one from a pass as needed. Making this work well requires making the
object movable, using references instead of overwritten pointers in
a number of places, and other refactorings.
I've also wired it up to the new pass manager and added a RUN line to
a test to exercise it under the new pass manager. This includes basic
printing support much like with other analyses.
But there is a big and somewhat scary change here. Prior to this patch
ScalarEvolution was never *actually* invalidated!!! Re-running the pass
just re-wired up the various other analyses and didn't remove any of the
existing entries in the SCEV caches or clear out anything at all. This
might seem OK as everything in SCEV that can uses ValueHandles to track
updates to the values that serve as SCEV keys. However, this still means
that as we ran SCEV over each function in the module, we kept
accumulating more and more SCEVs into the cache. At the end, we would
have a SCEV cache with every value that we ever needed a SCEV for in the
entire module!!! Yowzers. The releaseMemory routine would dump all of
this, but that isn't realy called during normal runs of the pipeline as
far as I can see.
To make matters worse, there *is* actually a key that we don't update
with value handles -- there is a map keyed off of Loop*s. Because
LoopInfo *does* release its memory from run to run, it is entirely
possible to run SCEV over one function, then over another function, and
then lookup a Loop* from the second function but find an entry inserted
for the first function! Ouch.
To make matters still worse, there are plenty of updates that *don't*
trip a value handle. It seems incredibly unlikely that today GVN or
another pass that invalidates SCEV can update values in *just* such
a way that a subsequent run of SCEV will incorrectly find lookups in
a cache, but it is theoretically possible and would be a nightmare to
debug.
With this refactoring, I've fixed all this by actually destroying and
recreating the ScalarEvolution object from run to run. Technically, this
could increase the amount of malloc traffic we see, but then again it is
also technically correct. ;] I don't actually think we're suffering from
tons of malloc traffic from SCEV because if we were, the fact that we
never clear the memory would seem more likely to have come up as an
actual problem before now. So, I've made the simple fix here. If in fact
there are serious issues with too much allocation and deallocation,
I can work on a clever fix that preserves the allocations (while
clearing the data) between each run, but I'd prefer to do that kind of
optimization with a test case / benchmark that shows why we need such
cleverness (and that can test that we actually make it faster). It's
possible that this will make some things faster by making the SCEV
caches have higher locality (due to being significantly smaller) so
until there is a clear benchmark, I think the simple change is best.
Differential Revision: http://reviews.llvm.org/D12063
llvm-svn: 245193
2015-08-17 10:08:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
|
2014-09-08 04:05:11 +08:00
|
|
|
INITIALIZE_PASS_END(AlignmentFromAssumptions, AA_NAME,
|
|
|
|
aip_name, false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createAlignmentFromAssumptionsPass() {
|
|
|
|
return new AlignmentFromAssumptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given an expression for the (constant) alignment, AlignSCEV, and an
|
|
|
|
// expression for the displacement between a pointer and the aligned address,
|
2014-09-08 07:16:24 +08:00
|
|
|
// DiffSCEV, compute the alignment of the displaced pointer if it can be reduced
|
|
|
|
// to a constant. Using SCEV to compute alignment handles the case where
|
|
|
|
// DiffSCEV is a recurrence with constant start such that the aligned offset
|
|
|
|
// is constant. e.g. {16,+,32} % 32 -> 16.
|
2014-09-08 04:05:11 +08:00
|
|
|
static unsigned getNewAlignmentDiff(const SCEV *DiffSCEV,
|
|
|
|
const SCEV *AlignSCEV,
|
|
|
|
ScalarEvolution *SE) {
|
|
|
|
// DiffUnits = Diff % int64_t(Alignment)
|
|
|
|
const SCEV *DiffAlignDiv = SE->getUDivExpr(DiffSCEV, AlignSCEV);
|
|
|
|
const SCEV *DiffAlign = SE->getMulExpr(DiffAlignDiv, AlignSCEV);
|
|
|
|
const SCEV *DiffUnitsSCEV = SE->getMinusSCEV(DiffAlign, DiffSCEV);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\talignment relative to " << *AlignSCEV << " is "
|
|
|
|
<< *DiffUnitsSCEV << " (diff: " << *DiffSCEV << ")\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
|
|
|
|
if (const SCEVConstant *ConstDUSCEV =
|
|
|
|
dyn_cast<SCEVConstant>(DiffUnitsSCEV)) {
|
|
|
|
int64_t DiffUnits = ConstDUSCEV->getValue()->getSExtValue();
|
|
|
|
|
|
|
|
// If the displacement is an exact multiple of the alignment, then the
|
|
|
|
// displaced pointer has the same alignment as the aligned pointer, so
|
|
|
|
// return the alignment value.
|
|
|
|
if (!DiffUnits)
|
|
|
|
return (unsigned)
|
|
|
|
cast<SCEVConstant>(AlignSCEV)->getValue()->getSExtValue();
|
|
|
|
|
|
|
|
// If the displacement is not an exact multiple, but the remainder is a
|
|
|
|
// constant, then return this remainder (but only if it is a power of 2).
|
2015-03-10 04:20:16 +08:00
|
|
|
uint64_t DiffUnitsAbs = std::abs(DiffUnits);
|
2014-09-08 04:05:11 +08:00
|
|
|
if (isPowerOf2_64(DiffUnitsAbs))
|
|
|
|
return (unsigned) DiffUnitsAbs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is an address given by an offset OffSCEV from AASCEV which has an
|
|
|
|
// alignment AlignSCEV. Use that information, if possible, to compute a new
|
|
|
|
// alignment for Ptr.
|
|
|
|
static unsigned getNewAlignment(const SCEV *AASCEV, const SCEV *AlignSCEV,
|
|
|
|
const SCEV *OffSCEV, Value *Ptr,
|
|
|
|
ScalarEvolution *SE) {
|
|
|
|
const SCEV *PtrSCEV = SE->getSCEV(Ptr);
|
|
|
|
const SCEV *DiffSCEV = SE->getMinusSCEV(PtrSCEV, AASCEV);
|
|
|
|
|
2014-09-11 16:40:17 +08:00
|
|
|
// On 32-bit platforms, DiffSCEV might now have type i32 -- we've always
|
|
|
|
// sign-extended OffSCEV to i64, so make sure they agree again.
|
|
|
|
DiffSCEV = SE->getNoopOrSignExtend(DiffSCEV, OffSCEV->getType());
|
|
|
|
|
2014-09-08 04:05:11 +08:00
|
|
|
// What we really want to know is the overall offset to the aligned
|
|
|
|
// address. This address is displaced by the provided offset.
|
|
|
|
DiffSCEV = SE->getMinusSCEV(DiffSCEV, OffSCEV);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "AFI: alignment of " << *Ptr << " relative to "
|
|
|
|
<< *AlignSCEV << " and offset " << *OffSCEV
|
|
|
|
<< " using diff " << *DiffSCEV << "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
|
|
|
|
unsigned NewAlignment = getNewAlignmentDiff(DiffSCEV, AlignSCEV, SE);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tnew alignment: " << NewAlignment << "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
|
|
|
|
if (NewAlignment) {
|
|
|
|
return NewAlignment;
|
|
|
|
} else if (const SCEVAddRecExpr *DiffARSCEV =
|
|
|
|
dyn_cast<SCEVAddRecExpr>(DiffSCEV)) {
|
|
|
|
// The relative offset to the alignment assumption did not yield a constant,
|
|
|
|
// but we should try harder: if we assume that a is 32-byte aligned, then in
|
|
|
|
// for (i = 0; i < 1024; i += 4) r += a[i]; not all of the loads from a are
|
|
|
|
// 32-byte aligned, but instead alternate between 32 and 16-byte alignment.
|
|
|
|
// As a result, the new alignment will not be a constant, but can still
|
|
|
|
// be improved over the default (of 4) to 16.
|
|
|
|
|
|
|
|
const SCEV *DiffStartSCEV = DiffARSCEV->getStart();
|
|
|
|
const SCEV *DiffIncSCEV = DiffARSCEV->getStepRecurrence(*SE);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\ttrying start/inc alignment using start "
|
|
|
|
<< *DiffStartSCEV << " and inc " << *DiffIncSCEV << "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
|
|
|
|
// Now compute the new alignment using the displacement to the value in the
|
|
|
|
// first iteration, and also the alignment using the per-iteration delta.
|
|
|
|
// If these are the same, then use that answer. Otherwise, use the smaller
|
|
|
|
// one, but only if it divides the larger one.
|
|
|
|
NewAlignment = getNewAlignmentDiff(DiffStartSCEV, AlignSCEV, SE);
|
|
|
|
unsigned NewIncAlignment = getNewAlignmentDiff(DiffIncSCEV, AlignSCEV, SE);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tnew start alignment: " << NewAlignment << "\n");
|
|
|
|
LLVM_DEBUG(dbgs() << "\tnew inc alignment: " << NewIncAlignment << "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
|
2014-09-11 05:05:52 +08:00
|
|
|
if (!NewAlignment || !NewIncAlignment) {
|
|
|
|
return 0;
|
|
|
|
} else if (NewAlignment > NewIncAlignment) {
|
2014-09-08 04:05:11 +08:00
|
|
|
if (NewAlignment % NewIncAlignment == 0) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tnew start/inc alignment: " << NewIncAlignment
|
|
|
|
<< "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
return NewIncAlignment;
|
|
|
|
}
|
|
|
|
} else if (NewIncAlignment > NewAlignment) {
|
|
|
|
if (NewIncAlignment % NewAlignment == 0) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tnew start/inc alignment: " << NewAlignment
|
|
|
|
<< "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
return NewAlignment;
|
|
|
|
}
|
2014-09-11 05:05:52 +08:00
|
|
|
} else if (NewIncAlignment == NewAlignment) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tnew start/inc alignment: " << NewAlignment
|
|
|
|
<< "\n");
|
2014-09-08 04:05:11 +08:00
|
|
|
return NewAlignment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-15 14:18:01 +08:00
|
|
|
bool AlignmentFromAssumptionsPass::extractAlignmentInfo(CallInst *I,
|
|
|
|
Value *&AAPtr,
|
|
|
|
const SCEV *&AlignSCEV,
|
|
|
|
const SCEV *&OffSCEV) {
|
2014-09-08 04:05:11 +08:00
|
|
|
// An alignment assume must be a statement about the least-significant
|
|
|
|
// bits of the pointer being zero, possibly with some offset.
|
|
|
|
ICmpInst *ICI = dyn_cast<ICmpInst>(I->getArgOperand(0));
|
|
|
|
if (!ICI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// This must be an expression of the form: x & m == 0.
|
|
|
|
if (ICI->getPredicate() != ICmpInst::ICMP_EQ)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Swap things around so that the RHS is 0.
|
|
|
|
Value *CmpLHS = ICI->getOperand(0);
|
|
|
|
Value *CmpRHS = ICI->getOperand(1);
|
|
|
|
const SCEV *CmpLHSSCEV = SE->getSCEV(CmpLHS);
|
|
|
|
const SCEV *CmpRHSSCEV = SE->getSCEV(CmpRHS);
|
|
|
|
if (CmpLHSSCEV->isZero())
|
|
|
|
std::swap(CmpLHS, CmpRHS);
|
|
|
|
else if (!CmpRHSSCEV->isZero())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BinaryOperator *CmpBO = dyn_cast<BinaryOperator>(CmpLHS);
|
|
|
|
if (!CmpBO || CmpBO->getOpcode() != Instruction::And)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Swap things around so that the right operand of the and is a constant
|
|
|
|
// (the mask); we cannot deal with variable masks.
|
|
|
|
Value *AndLHS = CmpBO->getOperand(0);
|
|
|
|
Value *AndRHS = CmpBO->getOperand(1);
|
|
|
|
const SCEV *AndLHSSCEV = SE->getSCEV(AndLHS);
|
|
|
|
const SCEV *AndRHSSCEV = SE->getSCEV(AndRHS);
|
|
|
|
if (isa<SCEVConstant>(AndLHSSCEV)) {
|
|
|
|
std::swap(AndLHS, AndRHS);
|
|
|
|
std::swap(AndLHSSCEV, AndRHSSCEV);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEVConstant *MaskSCEV = dyn_cast<SCEVConstant>(AndRHSSCEV);
|
|
|
|
if (!MaskSCEV)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The mask must have some trailing ones (otherwise the condition is
|
|
|
|
// trivial and tells us nothing about the alignment of the left operand).
|
2015-12-18 04:28:46 +08:00
|
|
|
unsigned TrailingOnes = MaskSCEV->getAPInt().countTrailingOnes();
|
2014-09-08 04:05:11 +08:00
|
|
|
if (!TrailingOnes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Cap the alignment at the maximum with which LLVM can deal (and make sure
|
|
|
|
// we don't overflow the shift).
|
|
|
|
uint64_t Alignment;
|
|
|
|
TrailingOnes = std::min(TrailingOnes,
|
|
|
|
unsigned(sizeof(unsigned) * CHAR_BIT - 1));
|
|
|
|
Alignment = std::min(1u << TrailingOnes, +Value::MaximumAlignment);
|
|
|
|
|
|
|
|
Type *Int64Ty = Type::getInt64Ty(I->getParent()->getParent()->getContext());
|
|
|
|
AlignSCEV = SE->getConstant(Int64Ty, Alignment);
|
|
|
|
|
|
|
|
// The LHS might be a ptrtoint instruction, or it might be the pointer
|
|
|
|
// with an offset.
|
|
|
|
AAPtr = nullptr;
|
|
|
|
OffSCEV = nullptr;
|
|
|
|
if (PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(AndLHS)) {
|
|
|
|
AAPtr = PToI->getPointerOperand();
|
2015-09-23 09:59:04 +08:00
|
|
|
OffSCEV = SE->getZero(Int64Ty);
|
2014-09-08 04:05:11 +08:00
|
|
|
} else if (const SCEVAddExpr* AndLHSAddSCEV =
|
|
|
|
dyn_cast<SCEVAddExpr>(AndLHSSCEV)) {
|
|
|
|
// Try to find the ptrtoint; subtract it and the rest is the offset.
|
|
|
|
for (SCEVAddExpr::op_iterator J = AndLHSAddSCEV->op_begin(),
|
|
|
|
JE = AndLHSAddSCEV->op_end(); J != JE; ++J)
|
|
|
|
if (const SCEVUnknown *OpUnk = dyn_cast<SCEVUnknown>(*J))
|
|
|
|
if (PtrToIntInst *PToI = dyn_cast<PtrToIntInst>(OpUnk->getValue())) {
|
|
|
|
AAPtr = PToI->getPointerOperand();
|
|
|
|
OffSCEV = SE->getMinusSCEV(AndLHSAddSCEV, *J);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AAPtr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sign extend the offset to 64 bits (so that it is like all of the other
|
|
|
|
// expressions).
|
|
|
|
unsigned OffSCEVBits = OffSCEV->getType()->getPrimitiveSizeInBits();
|
|
|
|
if (OffSCEVBits < 64)
|
|
|
|
OffSCEV = SE->getSignExtendExpr(OffSCEV, Int64Ty);
|
|
|
|
else if (OffSCEVBits > 64)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
AAPtr = AAPtr->stripPointerCasts();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-15 14:18:01 +08:00
|
|
|
bool AlignmentFromAssumptionsPass::processAssumption(CallInst *ACall) {
|
2014-09-08 04:05:11 +08:00
|
|
|
Value *AAPtr;
|
|
|
|
const SCEV *AlignSCEV, *OffSCEV;
|
|
|
|
if (!extractAlignmentInfo(ACall, AAPtr, AlignSCEV, OffSCEV))
|
|
|
|
return false;
|
|
|
|
|
2016-09-25 04:00:38 +08:00
|
|
|
// Skip ConstantPointerNull and UndefValue. Assumptions on these shouldn't
|
|
|
|
// affect other users.
|
|
|
|
if (isa<ConstantData>(AAPtr))
|
|
|
|
return false;
|
|
|
|
|
2014-09-08 04:05:11 +08:00
|
|
|
const SCEV *AASCEV = SE->getSCEV(AAPtr);
|
|
|
|
|
|
|
|
// Apply the assumption to all other users of the specified pointer.
|
|
|
|
SmallPtrSet<Instruction *, 32> Visited;
|
|
|
|
SmallVector<Instruction*, 16> WorkList;
|
|
|
|
for (User *J : AAPtr->users()) {
|
|
|
|
if (J == ACall)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Instruction *K = dyn_cast<Instruction>(J))
|
2015-03-10 10:37:25 +08:00
|
|
|
if (isValidAssumeForContext(ACall, K, DT))
|
2014-09-08 04:05:11 +08:00
|
|
|
WorkList.push_back(K);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
Instruction *J = WorkList.pop_back_val();
|
|
|
|
|
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(J)) {
|
|
|
|
unsigned NewAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
|
|
|
|
LI->getPointerOperand(), SE);
|
|
|
|
|
|
|
|
if (NewAlignment > LI->getAlignment()) {
|
|
|
|
LI->setAlignment(NewAlignment);
|
|
|
|
++NumLoadAlignChanged;
|
|
|
|
}
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(J)) {
|
|
|
|
unsigned NewAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
|
|
|
|
SI->getPointerOperand(), SE);
|
|
|
|
|
|
|
|
if (NewAlignment > SI->getAlignment()) {
|
|
|
|
SI->setAlignment(NewAlignment);
|
|
|
|
++NumStoreAlignChanged;
|
|
|
|
}
|
|
|
|
} else if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(J)) {
|
|
|
|
unsigned NewDestAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
|
|
|
|
MI->getDest(), SE);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tmem inst: " << NewDestAlignment << "\n";);
|
[AlignmentFromAssumptions] Set source and dest alignments of memory intrinsiscs separately
Summary:
This change is part of step five in the series of changes to remove alignment argument from
memcpy/memmove/memset in favour of alignment attributes. In particular, this changes the
AlignmentFromAssumptions pass to cease using the old getAlignment()/setAlignment API of
MemoryIntrinsic in favour of getting/setting source & dest specific alignments through
the new API. This allows us to simplify some of the code in this pass and also be more
aggressive about setting the source and destination alignments separately.
Steps:
Step 1) Remove alignment parameter and create alignment parameter attributes for
memcpy/memmove/memset. ( rL322965, rC322964, rL322963 )
Step 2) Expand the IRBuilder API to allow creation of memcpy/memmove with differing
source and dest alignments. ( rL323597 )
Step 3) Update Clang to use the new IRBuilder API. ( rC323617 )
Step 4) Update Polly to use the new IRBuilder API. ( rL323618 )
Step 5) Update LLVM passes that create memcpy/memmove calls to use the new IRBuilder API,
and those that use use MemIntrinsicInst::[get|set]Alignment() to use [get|set]DestAlignment()
and [get|set]SourceAlignment() instead. ( rL323886, rL323891, rL324148, rL324273, rL324278,
rL324384, rL324395, rL324402, rL324626, rL324642, rL324653, rL324654, rL324773, rL324774,
rL324781, rL324784, rL324955, rL324960 )
Step 6) Remove the single-alignment IRBuilder API for memcpy/memmove, and the
MemIntrinsicInst::[get|set]Alignment() methods.
Reference
http://lists.llvm.org/pipermail/llvm-dev/2015-August/089384.html
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20151109/312083.html
Reviewers: hfinkel, bollu, reames
Reviewed By: reames
Subscribers: reames, llvm-commits
Differential Revision: https://reviews.llvm.org/D43081
llvm-svn: 325816
2018-02-23 02:55:59 +08:00
|
|
|
if (NewDestAlignment > MI->getDestAlignment()) {
|
|
|
|
MI->setDestAlignment(NewDestAlignment);
|
|
|
|
++NumMemIntAlignChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For memory transfers, there is also a source alignment that
|
|
|
|
// can be set.
|
2014-09-08 04:05:11 +08:00
|
|
|
if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
|
|
|
|
unsigned NewSrcAlignment = getNewAlignment(AASCEV, AlignSCEV, OffSCEV,
|
|
|
|
MTI->getSource(), SE);
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "\tmem trans: " << NewSrcAlignment << "\n";);
|
[AlignmentFromAssumptions] Set source and dest alignments of memory intrinsiscs separately
Summary:
This change is part of step five in the series of changes to remove alignment argument from
memcpy/memmove/memset in favour of alignment attributes. In particular, this changes the
AlignmentFromAssumptions pass to cease using the old getAlignment()/setAlignment API of
MemoryIntrinsic in favour of getting/setting source & dest specific alignments through
the new API. This allows us to simplify some of the code in this pass and also be more
aggressive about setting the source and destination alignments separately.
Steps:
Step 1) Remove alignment parameter and create alignment parameter attributes for
memcpy/memmove/memset. ( rL322965, rC322964, rL322963 )
Step 2) Expand the IRBuilder API to allow creation of memcpy/memmove with differing
source and dest alignments. ( rL323597 )
Step 3) Update Clang to use the new IRBuilder API. ( rC323617 )
Step 4) Update Polly to use the new IRBuilder API. ( rL323618 )
Step 5) Update LLVM passes that create memcpy/memmove calls to use the new IRBuilder API,
and those that use use MemIntrinsicInst::[get|set]Alignment() to use [get|set]DestAlignment()
and [get|set]SourceAlignment() instead. ( rL323886, rL323891, rL324148, rL324273, rL324278,
rL324384, rL324395, rL324402, rL324626, rL324642, rL324653, rL324654, rL324773, rL324774,
rL324781, rL324784, rL324955, rL324960 )
Step 6) Remove the single-alignment IRBuilder API for memcpy/memmove, and the
MemIntrinsicInst::[get|set]Alignment() methods.
Reference
http://lists.llvm.org/pipermail/llvm-dev/2015-August/089384.html
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20151109/312083.html
Reviewers: hfinkel, bollu, reames
Reviewed By: reames
Subscribers: reames, llvm-commits
Differential Revision: https://reviews.llvm.org/D43081
llvm-svn: 325816
2018-02-23 02:55:59 +08:00
|
|
|
|
|
|
|
if (NewSrcAlignment > MTI->getSourceAlignment()) {
|
|
|
|
MTI->setSourceAlignment(NewSrcAlignment);
|
2014-09-08 04:05:11 +08:00
|
|
|
++NumMemIntAlignChanged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we've updated that use of the pointer, look for other uses of
|
|
|
|
// the pointer to update.
|
|
|
|
Visited.insert(J);
|
|
|
|
for (User *UJ : J->users()) {
|
|
|
|
Instruction *K = cast<Instruction>(UJ);
|
2015-03-10 10:37:25 +08:00
|
|
|
if (!Visited.count(K) && isValidAssumeForContext(ACall, K, DT))
|
2014-09-08 04:05:11 +08:00
|
|
|
WorkList.push_back(K);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlignmentFromAssumptions::runOnFunction(Function &F) {
|
2016-05-04 06:32:30 +08:00
|
|
|
if (skipFunction(F))
|
|
|
|
return false;
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
2016-06-15 14:18:01 +08:00
|
|
|
ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
|
|
|
DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
return Impl.runImpl(F, AC, SE, DT);
|
2016-06-15 14:18:01 +08:00
|
|
|
}
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
bool AlignmentFromAssumptionsPass::runImpl(Function &F, AssumptionCache &AC,
|
|
|
|
ScalarEvolution *SE_,
|
2016-06-15 14:18:01 +08:00
|
|
|
DominatorTree *DT_) {
|
|
|
|
SE = SE_;
|
|
|
|
DT = DT_;
|
2014-09-08 04:05:11 +08:00
|
|
|
|
2016-06-15 14:18:01 +08:00
|
|
|
bool Changed = false;
|
2016-12-19 16:22:17 +08:00
|
|
|
for (auto &AssumeVH : AC.assumptions())
|
|
|
|
if (AssumeVH)
|
|
|
|
Changed |= processAssumption(cast<CallInst>(AssumeVH));
|
2014-09-08 04:05:11 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2016-06-15 14:18:01 +08:00
|
|
|
PreservedAnalyses
|
|
|
|
AlignmentFromAssumptionsPass::run(Function &F, FunctionAnalysisManager &AM) {
|
|
|
|
|
2016-12-19 16:22:17 +08:00
|
|
|
AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
|
2016-06-15 14:18:01 +08:00
|
|
|
ScalarEvolution &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
|
|
|
|
DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
|
2017-01-24 20:55:57 +08:00
|
|
|
if (!runImpl(F, AC, &SE, &DT))
|
2016-06-15 14:18:01 +08:00
|
|
|
return PreservedAnalyses::all();
|
2017-01-15 14:32:49 +08:00
|
|
|
|
2016-06-15 14:18:01 +08:00
|
|
|
PreservedAnalyses PA;
|
2017-01-15 14:32:49 +08:00
|
|
|
PA.preserveSet<CFGAnalyses>();
|
2016-06-15 14:18:01 +08:00
|
|
|
PA.preserve<AAManager>();
|
|
|
|
PA.preserve<ScalarEvolutionAnalysis>();
|
|
|
|
PA.preserve<GlobalsAA>();
|
|
|
|
return PA;
|
|
|
|
}
|