blob: c1043e446beb6d1fa6660819f6229b1da607ba93 [file] [log] [blame]
Sebastian Popc62c6792013-11-12 22:47:20 +00001//===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sebastian Popc62c6792013-11-12 22:47:20 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This implements an analysis pass that tries to delinearize all GEP
10// instructions in all loops using the SCEV analysis functionality. This pass is
11// only used for testing purposes: if your pass needs delinearization, please
12// use the on-demand SCEVAddRecExpr::delinearize() function.
13//
14//===----------------------------------------------------------------------===//
15
Sebastian Popc62c6792013-11-12 22:47:20 +000016#include "llvm/Analysis/LoopInfo.h"
17#include "llvm/Analysis/Passes.h"
18#include "llvm/Analysis/ScalarEvolution.h"
19#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000020#include "llvm/IR/Constants.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000023#include "llvm/IR/InstIterator.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/IR/Instructions.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Pass.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000028#include "llvm/Support/Debug.h"
Sebastian Popc62c6792013-11-12 22:47:20 +000029#include "llvm/Support/raw_ostream.h"
30
31using namespace llvm;
32
Chandler Carruthf1221bd2014-04-22 02:48:03 +000033#define DL_NAME "delinearize"
34#define DEBUG_TYPE DL_NAME
35
Benjamin Kramer9e501ec2013-11-13 15:35:17 +000036namespace {
37
Sebastian Popc62c6792013-11-12 22:47:20 +000038class Delinearization : public FunctionPass {
39 Delinearization(const Delinearization &); // do not implement
40protected:
41 Function *F;
42 LoopInfo *LI;
43 ScalarEvolution *SE;
44
45public:
46 static char ID; // Pass identification, replacement for typeid
47
48 Delinearization() : FunctionPass(ID) {
49 initializeDelinearizationPass(*PassRegistry::getPassRegistry());
50 }
Craig Toppere9ba7592014-03-05 07:30:04 +000051 bool runOnFunction(Function &F) override;
52 void getAnalysisUsage(AnalysisUsage &AU) const override;
Craig Topper9f008862014-04-15 04:59:12 +000053 void print(raw_ostream &O, const Module *M = nullptr) const override;
Sebastian Popc62c6792013-11-12 22:47:20 +000054};
55
Benjamin Kramer9e501ec2013-11-13 15:35:17 +000056} // end anonymous namespace
57
Sebastian Popc62c6792013-11-12 22:47:20 +000058void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000060 AU.addRequired<LoopInfoWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +000061 AU.addRequired<ScalarEvolutionWrapperPass>();
Sebastian Popc62c6792013-11-12 22:47:20 +000062}
63
64bool Delinearization::runOnFunction(Function &F) {
65 this->F = &F;
Chandler Carruth2f1fd162015-08-17 02:08:17 +000066 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth4f8f3072015-01-17 14:16:18 +000067 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Sebastian Popc62c6792013-11-12 22:47:20 +000068 return false;
69}
70
Sebastian Popc62c6792013-11-12 22:47:20 +000071void Delinearization::print(raw_ostream &O, const Module *) const {
72 O << "Delinearization on function " << F->getName() << ":\n";
73 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
74 Instruction *Inst = &(*I);
Sebastian Pop7ee14722013-11-13 22:37:58 +000075
76 // Only analyze loads and stores.
Sebastian Popc62c6792013-11-12 22:47:20 +000077 if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
78 !isa<GetElementPtrInst>(Inst))
79 continue;
80
81 const BasicBlock *BB = Inst->getParent();
82 // Delinearize the memory access as analyzed in all the surrounding loops.
83 // Do not analyze memory accesses outside loops.
Craig Topper9f008862014-04-15 04:59:12 +000084 for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
Renato Golin038ede22018-03-09 21:05:58 +000085 const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(Inst), L);
Sebastian Pop28e6b972014-05-27 22:41:51 +000086
87 const SCEVUnknown *BasePointer =
88 dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn));
89 // Do not delinearize if we cannot find the base pointer.
90 if (!BasePointer)
91 break;
92 AccessFn = SE->getMinusSCEV(AccessFn, BasePointer);
Sebastian Pop28e6b972014-05-27 22:41:51 +000093
Tobias Grosserc3d9db22014-04-09 07:53:49 +000094 O << "\n";
95 O << "Inst:" << *Inst << "\n";
96 O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
Tobias Grosser374bce02015-10-12 08:02:00 +000097 O << "AccessFunction: " << *AccessFn << "\n";
Sebastian Popc62c6792013-11-12 22:47:20 +000098
99 SmallVector<const SCEV *, 3> Subscripts, Sizes;
Tobias Grosser374bce02015-10-12 08:02:00 +0000100 SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst));
Sebastian Popa6e58602014-05-27 22:41:45 +0000101 if (Subscripts.size() == 0 || Sizes.size() == 0 ||
Sebastian Pop448712b2014-05-07 18:01:20 +0000102 Subscripts.size() != Sizes.size()) {
Sebastian Popc62c6792013-11-12 22:47:20 +0000103 O << "failed to delinearize\n";
104 continue;
105 }
Sebastian Pop28e6b972014-05-27 22:41:51 +0000106
107 O << "Base offset: " << *BasePointer << "\n";
Sebastian Popc62c6792013-11-12 22:47:20 +0000108 O << "ArrayDecl[UnknownSize]";
Sebastian Pop448712b2014-05-07 18:01:20 +0000109 int Size = Subscripts.size();
Sebastian Popc62c6792013-11-12 22:47:20 +0000110 for (int i = 0; i < Size - 1; i++)
111 O << "[" << *Sizes[i] << "]";
112 O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
113
114 O << "ArrayRef";
115 for (int i = 0; i < Size; i++)
116 O << "[" << *Subscripts[i] << "]";
117 O << "\n";
118 }
119 }
120}
121
122char Delinearization::ID = 0;
123static const char delinearization_name[] = "Delinearization";
124INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
125 true)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000126INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Sebastian Popc62c6792013-11-12 22:47:20 +0000127INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
128
129FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }