2013-11-12 23:47:20 +01:00
|
|
|
//===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-11-12 23:47:20 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements an analysis pass that tries to delinearize all GEP
|
|
|
|
// instructions in all loops using the SCEV analysis functionality. This pass is
|
|
|
|
// only used for testing purposes: if your pass needs delinearization, please
|
|
|
|
// use the on-demand SCEVAddRecExpr::delinearize() function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-16 06:10:22 +02:00
|
|
|
#include "llvm/Analysis/Delinearization.h"
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2016-04-18 11:17:29 +02:00
|
|
|
#include "llvm/IR/Constants.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2014-03-04 11:30:26 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2020-09-16 06:10:22 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/IR/Type.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm/Pass.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 04:48:03 +02:00
|
|
|
#define DL_NAME "delinearize"
|
|
|
|
#define DEBUG_TYPE DL_NAME
|
|
|
|
|
2013-11-13 16:35:17 +01:00
|
|
|
namespace {
|
|
|
|
|
2013-11-12 23:47:20 +01:00
|
|
|
class Delinearization : public FunctionPass {
|
|
|
|
Delinearization(const Delinearization &); // do not implement
|
|
|
|
protected:
|
|
|
|
Function *F;
|
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
|
|
|
Delinearization() : FunctionPass(ID) {
|
|
|
|
initializeDelinearizationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-05 08:30:04 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2014-04-15 06:59:12 +02:00
|
|
|
void print(raw_ostream &O, const Module *M = nullptr) const override;
|
2013-11-12 23:47:20 +01:00
|
|
|
};
|
|
|
|
|
2020-09-16 06:10:22 +02:00
|
|
|
void printDelinearization(raw_ostream &O, Function *F, LoopInfo *LI,
|
|
|
|
ScalarEvolution *SE) {
|
2013-11-12 23:47:20 +01:00
|
|
|
O << "Delinearization on function " << F->getName() << ":\n";
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
|
|
|
Instruction *Inst = &(*I);
|
2013-11-13 23:37:58 +01:00
|
|
|
|
|
|
|
// Only analyze loads and stores.
|
2013-11-12 23:47:20 +01:00
|
|
|
if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
|
|
|
|
!isa<GetElementPtrInst>(Inst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const BasicBlock *BB = Inst->getParent();
|
|
|
|
// Delinearize the memory access as analyzed in all the surrounding loops.
|
|
|
|
// Do not analyze memory accesses outside loops.
|
2014-04-15 06:59:12 +02:00
|
|
|
for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
|
2018-03-09 22:05:58 +01:00
|
|
|
const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(Inst), L);
|
2014-05-28 00:41:51 +02:00
|
|
|
|
|
|
|
const SCEVUnknown *BasePointer =
|
|
|
|
dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn));
|
|
|
|
// Do not delinearize if we cannot find the base pointer.
|
|
|
|
if (!BasePointer)
|
|
|
|
break;
|
|
|
|
AccessFn = SE->getMinusSCEV(AccessFn, BasePointer);
|
|
|
|
|
2014-04-09 09:53:49 +02:00
|
|
|
O << "\n";
|
|
|
|
O << "Inst:" << *Inst << "\n";
|
|
|
|
O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
|
2015-10-12 10:02:00 +02:00
|
|
|
O << "AccessFunction: " << *AccessFn << "\n";
|
2013-11-12 23:47:20 +01:00
|
|
|
|
|
|
|
SmallVector<const SCEV *, 3> Subscripts, Sizes;
|
2015-10-12 10:02:00 +02:00
|
|
|
SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst));
|
2014-05-28 00:41:45 +02:00
|
|
|
if (Subscripts.size() == 0 || Sizes.size() == 0 ||
|
2014-05-07 20:01:20 +02:00
|
|
|
Subscripts.size() != Sizes.size()) {
|
2013-11-12 23:47:20 +01:00
|
|
|
O << "failed to delinearize\n";
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-28 00:41:51 +02:00
|
|
|
|
|
|
|
O << "Base offset: " << *BasePointer << "\n";
|
2013-11-12 23:47:20 +01:00
|
|
|
O << "ArrayDecl[UnknownSize]";
|
2014-05-07 20:01:20 +02:00
|
|
|
int Size = Subscripts.size();
|
2013-11-12 23:47:20 +01:00
|
|
|
for (int i = 0; i < Size - 1; i++)
|
|
|
|
O << "[" << *Sizes[i] << "]";
|
|
|
|
O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
|
|
|
|
|
|
|
|
O << "ArrayRef";
|
|
|
|
for (int i = 0; i < Size; i++)
|
|
|
|
O << "[" << *Subscripts[i] << "]";
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 06:10:22 +02:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Delinearization::runOnFunction(Function &F) {
|
|
|
|
this->F = &F;
|
|
|
|
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delinearization::print(raw_ostream &O, const Module *) const {
|
|
|
|
printDelinearization(O, F, LI, SE);
|
|
|
|
}
|
|
|
|
|
2013-11-12 23:47:20 +01:00
|
|
|
char Delinearization::ID = 0;
|
|
|
|
static const char delinearization_name[] = "Delinearization";
|
|
|
|
INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
|
|
|
|
true)
|
2015-01-17 15:16:18 +01:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2013-11-12 23:47:20 +01:00
|
|
|
INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }
|
2020-09-16 06:10:22 +02:00
|
|
|
|
|
|
|
DelinearizationPrinterPass::DelinearizationPrinterPass(raw_ostream &OS)
|
|
|
|
: OS(OS) {}
|
|
|
|
PreservedAnalyses DelinearizationPrinterPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
printDelinearization(OS, &F, &AM.getResult<LoopAnalysis>(F),
|
|
|
|
&AM.getResult<ScalarEvolutionAnalysis>(F));
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|