2013-12-22 00:04:03 +00:00
|
|
|
//===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2016-07-23 20:41:05 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
2013-12-22 00:04:03 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-12-22 00:04:03 +00:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/Pass.h"
|
2013-12-22 00:04:03 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#define DL_NAME "delinearize"
|
|
|
|
#define DEBUG_TYPE DL_NAME
|
|
|
|
|
2013-12-22 00:04:03 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
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-11-24 09:08:18 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
void print(raw_ostream &O, const Module *M = nullptr) const override;
|
2013-12-22 00:04:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2015-05-27 18:44:32 +00:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2015-12-30 11:46:15 +00:00
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
2013-12-22 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Delinearization::runOnFunction(Function &F) {
|
|
|
|
this->F = &F;
|
2015-12-30 11:46:15 +00:00
|
|
|
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
2015-05-27 18:44:32 +00:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2013-12-22 00:04:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Value *getPointerOperand(Instruction &Inst) {
|
|
|
|
if (LoadInst *Load = dyn_cast<LoadInst>(&Inst))
|
|
|
|
return Load->getPointerOperand();
|
|
|
|
else if (StoreInst *Store = dyn_cast<StoreInst>(&Inst))
|
|
|
|
return Store->getPointerOperand();
|
|
|
|
else if (GetElementPtrInst *Gep = dyn_cast<GetElementPtrInst>(&Inst))
|
|
|
|
return Gep->getPointerOperand();
|
2014-11-24 09:08:18 +00:00
|
|
|
return nullptr;
|
2013-12-22 00:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Delinearization::print(raw_ostream &O, const Module *) const {
|
|
|
|
O << "Delinearization on function " << F->getName() << ":\n";
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
|
|
|
Instruction *Inst = &(*I);
|
|
|
|
|
|
|
|
// Only analyze loads and stores.
|
|
|
|
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-11-24 09:08:18 +00:00
|
|
|
for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
|
2013-12-22 00:04:03 +00:00
|
|
|
const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(*Inst), L);
|
2014-11-24 09:08:18 +00: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);
|
|
|
|
|
|
|
|
O << "\n";
|
|
|
|
O << "Inst:" << *Inst << "\n";
|
|
|
|
O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
|
2015-12-30 11:46:15 +00:00
|
|
|
O << "AccessFunction: " << *AccessFn << "\n";
|
2013-12-22 00:04:03 +00:00
|
|
|
|
|
|
|
SmallVector<const SCEV *, 3> Subscripts, Sizes;
|
2015-12-30 11:46:15 +00:00
|
|
|
SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst));
|
2014-11-24 09:08:18 +00:00
|
|
|
if (Subscripts.size() == 0 || Sizes.size() == 0 ||
|
|
|
|
Subscripts.size() != Sizes.size()) {
|
2013-12-22 00:04:03 +00:00
|
|
|
O << "failed to delinearize\n";
|
|
|
|
continue;
|
|
|
|
}
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
O << "Base offset: " << *BasePointer << "\n";
|
2013-12-22 00:04:03 +00:00
|
|
|
O << "ArrayDecl[UnknownSize]";
|
2014-11-24 09:08:18 +00:00
|
|
|
int Size = Subscripts.size();
|
2013-12-22 00:04:03 +00: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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char Delinearization::ID = 0;
|
|
|
|
static const char delinearization_name[] = "Delinearization";
|
|
|
|
INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
|
|
|
|
true)
|
2015-05-27 18:44:32 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2013-12-22 00:04:03 +00:00
|
|
|
INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }
|