2011-04-29 14:27:02 +08:00
|
|
|
//===------ CodeGeneration.cpp - Code generate the Scops. -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The CodeGeneration pass takes a Scop created by ScopInfo and translates it
|
|
|
|
// back to LLVM-IR using Cloog.
|
|
|
|
//
|
|
|
|
// The Scop describes the high level memory behaviour of a control flow region.
|
|
|
|
// Transformation passes can update the schedule (execution order) of statements
|
|
|
|
// in the Scop. Cloog is used to generate an abstract syntax tree (clast) that
|
|
|
|
// reflects the updated execution order. This clast is used to create new
|
|
|
|
// LLVM-IR that is computational equivalent to the original control flow region,
|
|
|
|
// but executes its code in the new execution order defined by the changed
|
|
|
|
// scattering.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "polly-codegen"
|
|
|
|
|
|
|
|
#include "polly/Cloog.h"
|
|
|
|
#include "polly/Dependences.h"
|
2012-02-01 22:23:29 +08:00
|
|
|
#include "polly/LinkAllPasses.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
#include "polly/TempScopInfo.h"
|
2012-04-25 21:18:28 +08:00
|
|
|
#include "polly/CodeGen/CodeGeneration.h"
|
|
|
|
#include "polly/CodeGen/BlockGenerators.h"
|
|
|
|
#include "polly/CodeGen/LoopGenerators.h"
|
2012-04-25 21:16:49 +08:00
|
|
|
#include "polly/Support/GICHelper.h"
|
2012-02-01 22:23:29 +08:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
2012-03-29 21:10:26 +08:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2012-02-01 22:23:29 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Target/TargetData.h"
|
2012-02-01 22:23:29 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
#define CLOOG_INT_GMP 1
|
|
|
|
#include "cloog/cloog.h"
|
|
|
|
#include "cloog/isl/cloog.h"
|
|
|
|
|
2011-12-28 10:48:26 +08:00
|
|
|
#include "isl/aff.h"
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
using namespace polly;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
struct isl_set;
|
|
|
|
|
|
|
|
namespace polly {
|
|
|
|
static cl::opt<bool>
|
|
|
|
OpenMP("enable-polly-openmp",
|
|
|
|
cl::desc("Generate OpenMP parallel code"), cl::Hidden,
|
|
|
|
cl::value_desc("OpenMP code generation enabled if true"),
|
2012-03-17 01:17:16 +08:00
|
|
|
cl::init(false), cl::ZeroOrMore);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
AtLeastOnce("enable-polly-atLeastOnce",
|
|
|
|
cl::desc("Give polly the hint, that every loop is executed at least"
|
|
|
|
"once"), cl::Hidden,
|
|
|
|
cl::value_desc("OpenMP code generation enabled if true"),
|
2012-03-17 01:17:16 +08:00
|
|
|
cl::init(false), cl::ZeroOrMore);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
typedef DenseMap<const char*, Value*> CharMapT;
|
|
|
|
|
|
|
|
/// Class to generate LLVM-IR that calculates the value of a clast_expr.
|
|
|
|
class ClastExpCodeGen {
|
|
|
|
IRBuilder<> &Builder;
|
2012-03-23 20:20:36 +08:00
|
|
|
const CharMapT &IVS;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *codegen(const clast_name *e, Type *Ty);
|
|
|
|
Value *codegen(const clast_term *e, Type *Ty);
|
|
|
|
Value *codegen(const clast_binary *e, Type *Ty);
|
|
|
|
Value *codegen(const clast_reduction *r, Type *Ty);
|
|
|
|
public:
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
// A generator for clast expressions.
|
|
|
|
//
|
|
|
|
// @param B The IRBuilder that defines where the code to calculate the
|
|
|
|
// clast expressions should be inserted.
|
|
|
|
// @param IVMAP A Map that translates strings describing the induction
|
|
|
|
// variables to the Values* that represent these variables
|
|
|
|
// on the LLVM side.
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
// Generates code to calculate a given clast expression.
|
|
|
|
//
|
|
|
|
// @param e The expression to calculate.
|
|
|
|
// @return The Value that holds the result.
|
|
|
|
Value *codegen(const clast_expr *e, Type *Ty);
|
|
|
|
};
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) {
|
2012-03-23 20:20:36 +08:00
|
|
|
CharMapT::const_iterator I = IVS.find(e->name);
|
2012-01-25 00:42:28 +08:00
|
|
|
|
2012-03-23 20:20:36 +08:00
|
|
|
assert(I != IVS.end() && "Clast name not found");
|
2012-01-25 00:42:28 +08:00
|
|
|
|
|
|
|
return Builder.CreateSExtOrBitCast(I->second, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ClastExpCodeGen::codegen(const clast_term *e, Type *Ty) {
|
|
|
|
APInt a = APInt_from_MPZ(e->val);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *ConstOne = ConstantInt::get(Builder.getContext(), a);
|
|
|
|
ConstOne = Builder.CreateSExtOrBitCast(ConstOne, Ty);
|
|
|
|
|
|
|
|
if (!e->var)
|
2011-04-29 14:27:02 +08:00
|
|
|
return ConstOne;
|
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *var = codegen(e->var, Ty);
|
|
|
|
return Builder.CreateMul(ConstOne, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *ClastExpCodeGen::codegen(const clast_binary *e, Type *Ty) {
|
|
|
|
Value *LHS = codegen(e->LHS, Ty);
|
|
|
|
|
|
|
|
APInt RHS_AP = APInt_from_MPZ(e->RHS);
|
|
|
|
|
|
|
|
Value *RHS = ConstantInt::get(Builder.getContext(), RHS_AP);
|
|
|
|
RHS = Builder.CreateSExtOrBitCast(RHS, Ty);
|
|
|
|
|
|
|
|
switch (e->type) {
|
|
|
|
case clast_bin_mod:
|
|
|
|
return Builder.CreateSRem(LHS, RHS);
|
|
|
|
case clast_bin_fdiv:
|
|
|
|
{
|
2012-02-16 22:13:19 +08:00
|
|
|
// floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
|
2012-02-16 17:56:10 +08:00
|
|
|
Value *One = ConstantInt::get(Ty, 1);
|
|
|
|
Value *Zero = ConstantInt::get(Ty, 0);
|
2012-02-16 22:13:19 +08:00
|
|
|
Value *Sum1 = Builder.CreateSub(LHS, RHS);
|
|
|
|
Value *Sum2 = Builder.CreateAdd(Sum1, One);
|
|
|
|
Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
|
|
|
|
Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
|
|
|
|
return Builder.CreateSDiv(Dividend, RHS);
|
2012-01-25 00:42:28 +08:00
|
|
|
}
|
|
|
|
case clast_bin_cdiv:
|
|
|
|
{
|
2012-02-16 22:13:19 +08:00
|
|
|
// ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d
|
|
|
|
Value *One = ConstantInt::get(Ty, 1);
|
2012-02-16 17:56:10 +08:00
|
|
|
Value *Zero = ConstantInt::get(Ty, 0);
|
2012-02-16 22:13:19 +08:00
|
|
|
Value *Sum1 = Builder.CreateAdd(LHS, RHS);
|
|
|
|
Value *Sum2 = Builder.CreateSub(Sum1, One);
|
|
|
|
Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
|
|
|
|
Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2);
|
|
|
|
return Builder.CreateSDiv(Dividend, RHS);
|
2012-01-25 00:42:28 +08:00
|
|
|
}
|
|
|
|
case clast_bin_div:
|
|
|
|
return Builder.CreateSDiv(LHS, RHS);
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm_unreachable("Unknown clast binary expression type");
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *ClastExpCodeGen::codegen(const clast_reduction *r, Type *Ty) {
|
|
|
|
assert(( r->type == clast_red_min
|
|
|
|
|| r->type == clast_red_max
|
|
|
|
|| r->type == clast_red_sum)
|
|
|
|
&& "Clast reduction type not supported");
|
|
|
|
Value *old = codegen(r->elts[0], Ty);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
for (int i=1; i < r->n; ++i) {
|
|
|
|
Value *exprValue = codegen(r->elts[i], Ty);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
switch (r->type) {
|
|
|
|
case clast_red_min:
|
2011-04-29 14:27:02 +08:00
|
|
|
{
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *cmp = Builder.CreateICmpSLT(old, exprValue);
|
|
|
|
old = Builder.CreateSelect(cmp, old, exprValue);
|
|
|
|
break;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2012-01-25 00:42:28 +08:00
|
|
|
case clast_red_max:
|
2011-04-29 14:27:02 +08:00
|
|
|
{
|
2012-01-25 00:42:28 +08:00
|
|
|
Value *cmp = Builder.CreateICmpSGT(old, exprValue);
|
|
|
|
old = Builder.CreateSelect(cmp, old, exprValue);
|
2011-04-29 14:27:02 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-01-25 00:42:28 +08:00
|
|
|
case clast_red_sum:
|
|
|
|
old = Builder.CreateAdd(old, exprValue);
|
|
|
|
break;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
return old;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap)
|
2012-01-25 00:42:28 +08:00
|
|
|
: Builder(B), IVS(IVMap) {}
|
|
|
|
|
|
|
|
Value *ClastExpCodeGen::codegen(const clast_expr *e, Type *Ty) {
|
|
|
|
switch(e->type) {
|
|
|
|
case clast_expr_name:
|
|
|
|
return codegen((const clast_name *)e, Ty);
|
|
|
|
case clast_expr_term:
|
|
|
|
return codegen((const clast_term *)e, Ty);
|
|
|
|
case clast_expr_bin:
|
|
|
|
return codegen((const clast_binary *)e, Ty);
|
|
|
|
case clast_expr_red:
|
|
|
|
return codegen((const clast_reduction *)e, Ty);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:28 +08:00
|
|
|
llvm_unreachable("Unknown clast expression!");
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
class ClastStmtCodeGen {
|
2012-03-23 18:35:18 +08:00
|
|
|
public:
|
|
|
|
const std::vector<std::string> &getParallelLoops();
|
|
|
|
|
|
|
|
private:
|
2011-04-29 14:27:02 +08:00
|
|
|
// The Scop we code generate.
|
|
|
|
Scop *S;
|
2012-02-14 22:02:27 +08:00
|
|
|
Pass *P;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// The Builder specifies the current location to code generate at.
|
|
|
|
IRBuilder<> &Builder;
|
|
|
|
|
|
|
|
// Map the Values from the old code to their counterparts in the new code.
|
|
|
|
ValueMapT ValueMap;
|
|
|
|
|
|
|
|
// clastVars maps from the textual representation of a clast variable to its
|
|
|
|
// current *Value. clast variables are scheduling variables, original
|
|
|
|
// induction variables or parameters. They are used either in loop bounds or
|
|
|
|
// to define the statement instance that is executed.
|
|
|
|
//
|
|
|
|
// for (s = 0; s < n + 3; ++i)
|
|
|
|
// for (t = s; t < m; ++j)
|
|
|
|
// Stmt(i = s + 3 * m, j = t);
|
|
|
|
//
|
|
|
|
// {s,t,i,j,n,m} is the set of clast variables in this clast.
|
2012-03-23 20:20:36 +08:00
|
|
|
CharMapT ClastVars;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
// Codegenerator for clast expressions.
|
|
|
|
ClastExpCodeGen ExpGen;
|
|
|
|
|
|
|
|
// Do we currently generate parallel code?
|
|
|
|
bool parallelCodeGeneration;
|
|
|
|
|
|
|
|
std::vector<std::string> parallelLoops;
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
void codegen(const clast_assignment *a);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void codegen(const clast_assignment *a, ScopStmt *Statement,
|
|
|
|
unsigned Dimension, int vectorDim,
|
2012-01-25 00:42:32 +08:00
|
|
|
std::vector<ValueMapT> *VectorVMap = 0);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void codegenSubstitutions(const clast_stmt *Assignment,
|
|
|
|
ScopStmt *Statement, int vectorDim = 0,
|
2012-01-25 00:42:32 +08:00
|
|
|
std::vector<ValueMapT> *VectorVMap = 0);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
void codegen(const clast_user_stmt *u, std::vector<Value*> *IVS = NULL,
|
2012-01-25 00:42:32 +08:00
|
|
|
const char *iterator = NULL, isl_set *scatteringDomain = 0);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
void codegen(const clast_block *b);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// @brief Create a classical sequential loop.
|
2012-03-23 18:35:18 +08:00
|
|
|
void codegenForSequential(const clast_for *f);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// @brief Create OpenMP structure values.
|
|
|
|
///
|
2012-03-23 18:35:18 +08:00
|
|
|
/// Create a list of values that has to be stored into the OpenMP subfuncition
|
2011-04-29 14:27:02 +08:00
|
|
|
/// structure.
|
2012-03-23 18:35:18 +08:00
|
|
|
SetVector<Value*> getOMPValues();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-23 20:20:36 +08:00
|
|
|
/// @brief Update the internal structures according to a Value Map.
|
|
|
|
///
|
|
|
|
/// @param VMap A map from old to new values.
|
|
|
|
/// @param Reverse If true, we assume the update should be reversed.
|
2012-03-23 18:35:18 +08:00
|
|
|
void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
|
2012-03-23 20:20:36 +08:00
|
|
|
bool Reverse);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
/// @brief Create an OpenMP parallel for loop.
|
|
|
|
///
|
|
|
|
/// This loop reflects a loop as if it would have been created by an OpenMP
|
|
|
|
/// statement.
|
2012-01-25 00:42:32 +08:00
|
|
|
void codegenForOpenMP(const clast_for *f);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
bool isInnermostLoop(const clast_for *f);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
/// @brief Get the number of loop iterations for this loop.
|
|
|
|
/// @param f The clast for loop to check.
|
|
|
|
int getNumberOfIterations(const clast_for *f);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
/// @brief Create vector instructions for this loop.
|
|
|
|
void codegenForVector(const clast_for *f);
|
|
|
|
|
|
|
|
void codegen(const clast_for *f);
|
|
|
|
|
|
|
|
Value *codegen(const clast_equation *eq);
|
|
|
|
|
|
|
|
void codegen(const clast_guard *g);
|
|
|
|
|
|
|
|
void codegen(const clast_stmt *stmt);
|
|
|
|
|
|
|
|
void addParameters(const CloogNames *names);
|
|
|
|
|
2012-03-15 17:34:48 +08:00
|
|
|
IntegerType *getIntPtrTy();
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
public:
|
|
|
|
void codegen(const clast_root *r);
|
|
|
|
|
2012-03-15 17:34:52 +08:00
|
|
|
ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P);
|
2012-01-25 00:42:32 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-03-15 17:34:48 +08:00
|
|
|
IntegerType *ClastStmtCodeGen::getIntPtrTy() {
|
|
|
|
return P->getAnalysis<TargetData>().getIntPtrType(Builder.getContext());
|
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() {
|
|
|
|
return parallelLoops;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClastStmtCodeGen::codegen(const clast_assignment *a) {
|
2012-03-15 17:34:48 +08:00
|
|
|
Value *V= ExpGen.codegen(a->RHS, getIntPtrTy());
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars[a->LHS] = V;
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
|
|
|
|
2012-04-02 20:26:13 +08:00
|
|
|
void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt,
|
|
|
|
unsigned Dim, int VectorDim,
|
2012-01-25 00:42:32 +08:00
|
|
|
std::vector<ValueMapT> *VectorVMap) {
|
|
|
|
const PHINode *PN;
|
2012-04-02 20:26:13 +08:00
|
|
|
Value *RHS;
|
|
|
|
|
|
|
|
assert(!A->LHS && "Statement assignments do not have left hand side");
|
|
|
|
|
|
|
|
PN = Stmt->getInductionVariableForDimension(Dim);
|
2012-04-03 20:24:32 +08:00
|
|
|
RHS = ExpGen.codegen(A->RHS, Builder.getInt64Ty());
|
|
|
|
RHS = Builder.CreateTruncOrBitCast(RHS, PN->getType());
|
2012-01-25 00:42:32 +08:00
|
|
|
|
|
|
|
if (VectorVMap)
|
2012-04-02 20:26:13 +08:00
|
|
|
(*VectorVMap)[VectorDim][PN] = RHS;
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-04-02 20:26:13 +08:00
|
|
|
ValueMap[PN] = RHS;
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClastStmtCodeGen::codegenSubstitutions(const clast_stmt *Assignment,
|
|
|
|
ScopStmt *Statement, int vectorDim,
|
|
|
|
std::vector<ValueMapT> *VectorVMap) {
|
|
|
|
int Dimension = 0;
|
|
|
|
|
|
|
|
while (Assignment) {
|
|
|
|
assert(CLAST_STMT_IS_A(Assignment, stmt_ass)
|
|
|
|
&& "Substitions are expected to be assignments");
|
|
|
|
codegen((const clast_assignment *)Assignment, Statement, Dimension,
|
|
|
|
vectorDim, VectorVMap);
|
|
|
|
Assignment = Assignment->next;
|
|
|
|
Dimension++;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
void ClastStmtCodeGen::codegen(const clast_user_stmt *u,
|
|
|
|
std::vector<Value*> *IVS , const char *iterator,
|
2012-03-02 19:26:52 +08:00
|
|
|
isl_set *Domain) {
|
2012-01-25 00:42:32 +08:00
|
|
|
ScopStmt *Statement = (ScopStmt *)u->statement->usr;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
if (u->substitutions)
|
|
|
|
codegenSubstitutions(u->substitutions, Statement);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-02 19:27:28 +08:00
|
|
|
int VectorDimensions = IVS ? IVS->size() : 1;
|
|
|
|
|
|
|
|
if (VectorDimensions == 1) {
|
2012-03-02 23:20:39 +08:00
|
|
|
BlockGenerator::generate(Builder, *Statement, ValueMap, P);
|
2012-03-02 19:27:28 +08:00
|
|
|
return;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-02 19:27:28 +08:00
|
|
|
VectorValueMapT VectorMap(VectorDimensions);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
|
|
|
if (IVS) {
|
|
|
|
assert (u->substitutions && "Substitutions expected!");
|
|
|
|
int i = 0;
|
|
|
|
for (std::vector<Value*>::iterator II = IVS->begin(), IE = IVS->end();
|
|
|
|
II != IE; ++II) {
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars[iterator] = *II;
|
2012-03-02 19:26:52 +08:00
|
|
|
codegenSubstitutions(u->substitutions, Statement, i, &VectorMap);
|
2012-01-25 00:42:32 +08:00
|
|
|
i++;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-03-02 23:20:39 +08:00
|
|
|
VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Domain, P);
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClastStmtCodeGen::codegen(const clast_block *b) {
|
|
|
|
if (b->body)
|
|
|
|
codegen(b->body);
|
|
|
|
}
|
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
void ClastStmtCodeGen::codegenForSequential(const clast_for *f) {
|
|
|
|
Value *LowerBound, *UpperBound, *IV, *Stride;
|
2012-02-14 22:02:27 +08:00
|
|
|
BasicBlock *AfterBB;
|
2012-03-15 17:34:48 +08:00
|
|
|
Type *IntPtrTy = getIntPtrTy();
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
LowerBound = ExpGen.codegen(f->LB, IntPtrTy);
|
|
|
|
UpperBound = ExpGen.codegen(f->UB, IntPtrTy);
|
|
|
|
Stride = Builder.getInt(APInt_from_MPZ(f->stride));
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-04-23 21:03:56 +08:00
|
|
|
IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
// Add loop iv to symbols.
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars[f->iterator] = IV;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
if (f->body)
|
|
|
|
codegen(f->body);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
// Loop is finished, so remove its iv from the live symbols.
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars.erase(f->iterator);
|
2012-02-14 22:02:27 +08:00
|
|
|
Builder.SetInsertPoint(AfterBB->begin());
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
SetVector<Value*> ClastStmtCodeGen::getOMPValues() {
|
|
|
|
SetVector<Value*> Values;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
// The clast variables
|
2012-03-23 20:20:36 +08:00
|
|
|
for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
|
2012-01-25 00:42:32 +08:00
|
|
|
I != E; I++)
|
2012-03-23 18:35:18 +08:00
|
|
|
Values.insert(I->second);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
// The memory reference base addresses
|
2012-01-25 00:42:32 +08:00
|
|
|
for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
|
|
|
|
ScopStmt *Stmt = *SI;
|
|
|
|
for (SmallVector<MemoryAccess*, 8>::iterator I = Stmt->memacc_begin(),
|
|
|
|
E = Stmt->memacc_end(); I != E; ++I) {
|
|
|
|
Value *BaseAddr = const_cast<Value*>((*I)->getBaseAddr());
|
2012-03-23 18:35:18 +08:00
|
|
|
Values.insert((BaseAddr));
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
return Values;
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
void ClastStmtCodeGen::updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
|
2012-03-23 20:20:36 +08:00
|
|
|
bool Reverse) {
|
2012-03-23 18:35:18 +08:00
|
|
|
std::set<Value*> Inserted;
|
|
|
|
|
2012-03-23 20:20:36 +08:00
|
|
|
if (Reverse) {
|
|
|
|
OMPGenerator::ValueToValueMapTy ReverseMap;
|
|
|
|
|
|
|
|
for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
|
|
|
|
I != E; ++I)
|
|
|
|
ReverseMap.insert(std::make_pair(I->second, I->first));
|
|
|
|
|
|
|
|
for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
|
|
|
|
I != E; I++) {
|
|
|
|
ClastVars[I->first] = ReverseMap[I->second];
|
|
|
|
Inserted.insert(I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FIXME: At the moment we do not reverse the update of the ValueMap.
|
|
|
|
/// This is incomplet, but the failure should be obvious, such that
|
|
|
|
/// we can fix this later.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
|
2012-01-25 00:42:32 +08:00
|
|
|
I != E; I++) {
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars[I->first] = VMap[I->second];
|
2012-03-23 18:35:18 +08:00
|
|
|
Inserted.insert(I->second);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
|
2012-03-23 20:20:36 +08:00
|
|
|
I != E; ++I) {
|
2012-03-23 18:35:18 +08:00
|
|
|
if (Inserted.count(I->first))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ValueMap[I->first] = I->second;
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-29 21:10:26 +08:00
|
|
|
static void clearDomtree(Function *F, DominatorTree &DT) {
|
|
|
|
DomTreeNode *N = DT.getNode(&F->getEntryBlock());
|
|
|
|
std::vector<BasicBlock*> Nodes;
|
|
|
|
for (po_iterator<DomTreeNode*> I = po_begin(N), E = po_end(N); I != E; ++I)
|
|
|
|
Nodes.push_back(I->getBlock());
|
|
|
|
|
|
|
|
for (std::vector<BasicBlock*>::iterator I = Nodes.begin(), E = Nodes.end();
|
|
|
|
I != E; ++I)
|
|
|
|
DT.eraseNode(*I);
|
|
|
|
}
|
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
void ClastStmtCodeGen::codegenForOpenMP(const clast_for *For) {
|
2012-03-23 20:20:32 +08:00
|
|
|
Value *Stride, *LB, *UB, *IV;
|
2012-03-23 18:35:18 +08:00
|
|
|
BasicBlock::iterator LoopBody;
|
|
|
|
IntegerType *IntPtrTy = getIntPtrTy();
|
|
|
|
SetVector<Value*> Values;
|
|
|
|
OMPGenerator::ValueToValueMapTy VMap;
|
|
|
|
OMPGenerator OMPGen(Builder, P);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
Stride = Builder.getInt(APInt_from_MPZ(For->stride));
|
|
|
|
Stride = Builder.CreateSExtOrBitCast(Stride, IntPtrTy);
|
2012-03-23 20:20:32 +08:00
|
|
|
LB = ExpGen.codegen(For->LB, IntPtrTy);
|
|
|
|
UB = ExpGen.codegen(For->UB, IntPtrTy);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
Values = getOMPValues();
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-23 20:20:32 +08:00
|
|
|
IV = OMPGen.createParallelLoop(LB, UB, Stride, Values, VMap, &LoopBody);
|
2012-03-23 18:35:18 +08:00
|
|
|
BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
|
|
|
|
Builder.SetInsertPoint(LoopBody);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-23 20:20:36 +08:00
|
|
|
updateWithValueMap(VMap, /* reverse */ false);
|
|
|
|
ClastVars[For->iterator] = IV;
|
2012-03-23 18:35:18 +08:00
|
|
|
|
|
|
|
if (For->body)
|
|
|
|
codegen(For->body);
|
|
|
|
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars.erase(For->iterator);
|
|
|
|
updateWithValueMap(VMap, /* reverse */ true);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-29 21:10:26 +08:00
|
|
|
clearDomtree((*LoopBody).getParent()->getParent(),
|
|
|
|
P->getAnalysis<DominatorTree>());
|
|
|
|
|
2012-03-23 18:35:18 +08:00
|
|
|
Builder.SetInsertPoint(AfterLoop);
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
bool ClastStmtCodeGen::isInnermostLoop(const clast_for *f) {
|
|
|
|
const clast_stmt *stmt = f->body;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
while (stmt) {
|
|
|
|
if (!CLAST_STMT_IS_A(stmt, stmt_user))
|
|
|
|
return false;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
stmt = stmt->next;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ClastStmtCodeGen::getNumberOfIterations(const clast_for *f) {
|
|
|
|
isl_set *loopDomain = isl_set_copy(isl_set_from_cloog_domain(f->domain));
|
|
|
|
isl_set *tmp = isl_set_copy(loopDomain);
|
|
|
|
|
|
|
|
// Calculate a map similar to the identity map, but with the last input
|
|
|
|
// and output dimension not related.
|
|
|
|
// [i0, i1, i2, i3] -> [i0, i1, i2, o0]
|
|
|
|
isl_space *Space = isl_set_get_space(loopDomain);
|
|
|
|
Space = isl_space_drop_outputs(Space,
|
|
|
|
isl_set_dim(loopDomain, isl_dim_set) - 2, 1);
|
|
|
|
Space = isl_space_map_from_set(Space);
|
|
|
|
isl_map *identity = isl_map_identity(Space);
|
|
|
|
identity = isl_map_add_dims(identity, isl_dim_in, 1);
|
|
|
|
identity = isl_map_add_dims(identity, isl_dim_out, 1);
|
|
|
|
|
|
|
|
isl_map *map = isl_map_from_domain_and_range(tmp, loopDomain);
|
|
|
|
map = isl_map_intersect(map, identity);
|
|
|
|
|
|
|
|
isl_map *lexmax = isl_map_lexmax(isl_map_copy(map));
|
|
|
|
isl_map *lexmin = isl_map_lexmin(map);
|
|
|
|
isl_map *sub = isl_map_sum(lexmax, isl_map_neg(lexmin));
|
|
|
|
|
|
|
|
isl_set *elements = isl_map_range(sub);
|
|
|
|
|
|
|
|
if (!isl_set_is_singleton(elements)) {
|
|
|
|
isl_set_free(elements);
|
|
|
|
return -1;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
isl_point *p = isl_set_sample_point(elements);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
isl_int v;
|
|
|
|
isl_int_init(v);
|
|
|
|
isl_point_get_coordinate(p, isl_dim_set, isl_set_n_dim(loopDomain) - 1, &v);
|
|
|
|
int numberIterations = isl_int_get_si(v);
|
|
|
|
isl_int_clear(v);
|
|
|
|
isl_point_free(p);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
return (numberIterations) / isl_int_get_si(f->stride) + 1;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
void ClastStmtCodeGen::codegenForVector(const clast_for *F) {
|
|
|
|
DEBUG(dbgs() << "Vectorizing loop '" << F->iterator << "'\n";);
|
|
|
|
int VectorWidth = getNumberOfIterations(F);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
Value *LB = ExpGen.codegen(F->LB, getIntPtrTy());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
APInt Stride = APInt_from_MPZ(F->stride);
|
2012-01-25 00:42:32 +08:00
|
|
|
IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType());
|
|
|
|
Stride = Stride.zext(LoopIVType->getBitWidth());
|
|
|
|
Value *StrideValue = ConstantInt::get(LoopIVType, Stride);
|
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
std::vector<Value*> IVS(VectorWidth);
|
2012-01-25 00:42:32 +08:00
|
|
|
IVS[0] = LB;
|
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
for (int i = 1; i < VectorWidth; i++)
|
2012-01-25 00:42:32 +08:00
|
|
|
IVS[i] = Builder.CreateAdd(IVS[i-1], StrideValue, "p_vector_iv");
|
|
|
|
|
2012-03-15 17:34:58 +08:00
|
|
|
isl_set *Domain = isl_set_from_cloog_domain(F->domain);
|
2012-01-25 00:42:32 +08:00
|
|
|
|
|
|
|
// Add loop iv to symbols.
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars[F->iterator] = LB;
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
const clast_stmt *Stmt = F->body;
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2012-03-15 17:34:55 +08:00
|
|
|
while (Stmt) {
|
2012-03-15 17:34:58 +08:00
|
|
|
codegen((const clast_user_stmt *)Stmt, &IVS, F->iterator,
|
|
|
|
isl_set_copy(Domain));
|
2012-03-15 17:34:55 +08:00
|
|
|
Stmt = Stmt->next;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
// Loop is finished, so remove its iv from the live symbols.
|
2012-03-15 17:34:58 +08:00
|
|
|
isl_set_free(Domain);
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars.erase(F->iterator);
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
void ClastStmtCodeGen::codegen(const clast_for *f) {
|
2012-05-06 18:22:19 +08:00
|
|
|
bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
|
2012-03-15 17:34:52 +08:00
|
|
|
if ((Vector || OpenMP) && P->getAnalysis<Dependences>().isParallelFor(f)) {
|
2012-03-02 19:26:42 +08:00
|
|
|
if (Vector && isInnermostLoop(f) && (-1 != getNumberOfIterations(f))
|
|
|
|
&& (getNumberOfIterations(f) <= 16)) {
|
|
|
|
codegenForVector(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OpenMP && !parallelCodeGeneration) {
|
|
|
|
parallelCodeGeneration = true;
|
|
|
|
parallelLoops.push_back(f->iterator);
|
|
|
|
codegenForOpenMP(f);
|
|
|
|
parallelCodeGeneration = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
codegenForSequential(f);
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *ClastStmtCodeGen::codegen(const clast_equation *eq) {
|
2012-03-15 17:34:48 +08:00
|
|
|
Value *LHS = ExpGen.codegen(eq->LHS, getIntPtrTy());
|
|
|
|
Value *RHS = ExpGen.codegen(eq->RHS, getIntPtrTy());
|
2012-01-25 00:42:32 +08:00
|
|
|
CmpInst::Predicate P;
|
|
|
|
|
|
|
|
if (eq->sign == 0)
|
|
|
|
P = ICmpInst::ICMP_EQ;
|
|
|
|
else if (eq->sign > 0)
|
|
|
|
P = ICmpInst::ICMP_SGE;
|
|
|
|
else
|
|
|
|
P = ICmpInst::ICMP_SLE;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
return Builder.CreateICmp(P, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClastStmtCodeGen::codegen(const clast_guard *g) {
|
|
|
|
Function *F = Builder.GetInsertBlock()->getParent();
|
|
|
|
LLVMContext &Context = F->getContext();
|
2012-02-14 22:02:27 +08:00
|
|
|
|
|
|
|
BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
|
|
|
|
Builder.GetInsertPoint(), P);
|
|
|
|
CondBB->setName("polly.cond");
|
|
|
|
BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
|
|
|
|
MergeBB->setName("polly.merge");
|
2012-01-25 00:42:32 +08:00
|
|
|
BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
|
2012-02-14 22:02:27 +08:00
|
|
|
|
2012-03-15 17:34:52 +08:00
|
|
|
DominatorTree &DT = P->getAnalysis<DominatorTree>();
|
|
|
|
DT.addNewBlock(ThenBB, CondBB);
|
|
|
|
DT.changeImmediateDominator(MergeBB, CondBB);
|
2012-02-14 22:02:27 +08:00
|
|
|
|
|
|
|
CondBB->getTerminator()->eraseFromParent();
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(CondBB);
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
Value *Predicate = codegen(&(g->eq[0]));
|
|
|
|
|
|
|
|
for (int i = 1; i < g->n; ++i) {
|
|
|
|
Value *TmpPredicate = codegen(&(g->eq[i]));
|
|
|
|
Predicate = Builder.CreateAnd(Predicate, TmpPredicate);
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
Builder.CreateCondBr(Predicate, ThenBB, MergeBB);
|
|
|
|
Builder.SetInsertPoint(ThenBB);
|
2012-02-14 22:02:27 +08:00
|
|
|
Builder.CreateBr(MergeBB);
|
|
|
|
Builder.SetInsertPoint(ThenBB->begin());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
codegen(g->then);
|
2012-02-16 17:56:21 +08:00
|
|
|
|
|
|
|
Builder.SetInsertPoint(MergeBB->begin());
|
2012-01-25 00:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClastStmtCodeGen::codegen(const clast_stmt *stmt) {
|
|
|
|
if (CLAST_STMT_IS_A(stmt, stmt_root))
|
|
|
|
assert(false && "No second root statement expected");
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_ass))
|
|
|
|
codegen((const clast_assignment *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_user))
|
|
|
|
codegen((const clast_user_stmt *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_block))
|
|
|
|
codegen((const clast_block *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_for))
|
|
|
|
codegen((const clast_for *)stmt);
|
|
|
|
else if (CLAST_STMT_IS_A(stmt, stmt_guard))
|
|
|
|
codegen((const clast_guard *)stmt);
|
|
|
|
|
|
|
|
if (stmt->next)
|
|
|
|
codegen(stmt->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClastStmtCodeGen::addParameters(const CloogNames *names) {
|
2012-03-15 17:34:52 +08:00
|
|
|
SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
|
2012-01-25 00:42:32 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
assert(i < names->nb_parameters && "Not enough parameter names");
|
|
|
|
|
|
|
|
const SCEV *Param = *PI;
|
|
|
|
Type *Ty = Param->getType();
|
|
|
|
|
|
|
|
Instruction *insertLocation = --(Builder.GetInsertBlock()->end());
|
|
|
|
Value *V = Rewriter.expandCodeFor(Param, Ty, insertLocation);
|
2012-03-23 20:20:36 +08:00
|
|
|
ClastVars[names->parameters[i]] = V;
|
2012-01-25 00:42:32 +08:00
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-25 00:42:32 +08:00
|
|
|
void ClastStmtCodeGen::codegen(const clast_root *r) {
|
|
|
|
addParameters(r->names);
|
|
|
|
|
|
|
|
parallelCodeGeneration = false;
|
|
|
|
|
|
|
|
const clast_stmt *stmt = (const clast_stmt*) r;
|
|
|
|
if (stmt->next)
|
|
|
|
codegen(stmt->next);
|
|
|
|
}
|
|
|
|
|
2012-03-15 17:34:52 +08:00
|
|
|
ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P) :
|
2012-03-23 20:20:36 +08:00
|
|
|
S(scop), P(P), Builder(B), ExpGen(Builder, ClastVars) {}
|
2012-01-25 00:42:32 +08:00
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
namespace {
|
|
|
|
class CodeGeneration : public ScopPass {
|
|
|
|
Region *region;
|
|
|
|
Scop *S;
|
|
|
|
DominatorTree *DT;
|
2011-05-15 03:01:49 +08:00
|
|
|
RegionInfo *RI;
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
std::vector<std::string> parallelLoops;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
CodeGeneration() : ScopPass(ID) {}
|
|
|
|
|
2011-05-15 03:01:49 +08:00
|
|
|
// Split the entry edge of the region and generate a new basic block on this
|
|
|
|
// edge. This function also updates ScopInfo and RegionInfo.
|
|
|
|
//
|
|
|
|
// @param region The region where the entry edge will be splitted.
|
|
|
|
BasicBlock *splitEdgeAdvanced(Region *region) {
|
|
|
|
BasicBlock *newBlock;
|
|
|
|
BasicBlock *splitBlock;
|
|
|
|
|
|
|
|
newBlock = SplitEdge(region->getEnteringBlock(), region->getEntry(), this);
|
|
|
|
|
|
|
|
if (DT->dominates(region->getEntry(), newBlock)) {
|
2012-02-15 17:58:50 +08:00
|
|
|
BasicBlock *OldBlock = region->getEntry();
|
|
|
|
std::string OldName = OldBlock->getName();
|
|
|
|
|
2011-05-15 03:01:49 +08:00
|
|
|
// Update ScopInfo.
|
|
|
|
for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI)
|
2012-02-15 17:58:53 +08:00
|
|
|
if ((*SI)->getBasicBlock() == OldBlock) {
|
2011-05-15 03:01:49 +08:00
|
|
|
(*SI)->setBasicBlock(newBlock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update RegionInfo.
|
2012-02-15 17:58:50 +08:00
|
|
|
splitBlock = OldBlock;
|
|
|
|
OldBlock->setName("polly.split");
|
|
|
|
newBlock->setName(OldName);
|
2011-05-15 03:01:49 +08:00
|
|
|
region->replaceEntry(newBlock);
|
2011-05-15 03:01:55 +08:00
|
|
|
RI->setRegionFor(newBlock, region);
|
2011-05-15 03:01:49 +08:00
|
|
|
} else {
|
|
|
|
RI->setRegionFor(newBlock, region->getParent());
|
|
|
|
splitBlock = newBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a split block that branches either to the old code or to a new basic
|
|
|
|
// block where the new code can be inserted.
|
|
|
|
//
|
2012-02-12 20:09:41 +08:00
|
|
|
// @param Builder A builder that will be set to point to a basic block, where
|
2011-05-15 03:01:49 +08:00
|
|
|
// the new code can be generated.
|
|
|
|
// @return The split basic block.
|
2012-02-12 20:09:41 +08:00
|
|
|
BasicBlock *addSplitAndStartBlock(IRBuilder<> *Builder) {
|
|
|
|
BasicBlock *StartBlock, *SplitBlock;
|
|
|
|
|
|
|
|
SplitBlock = splitEdgeAdvanced(region);
|
|
|
|
SplitBlock->setName("polly.split_new_and_old");
|
|
|
|
Function *F = SplitBlock->getParent();
|
|
|
|
StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F);
|
|
|
|
SplitBlock->getTerminator()->eraseFromParent();
|
|
|
|
Builder->SetInsertPoint(SplitBlock);
|
|
|
|
Builder->CreateCondBr(Builder->getTrue(), StartBlock, region->getEntry());
|
|
|
|
DT->addNewBlock(StartBlock, SplitBlock);
|
|
|
|
Builder->SetInsertPoint(StartBlock);
|
|
|
|
return SplitBlock;
|
2011-05-15 03:01:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the control flow of the newly generated code with the existing code.
|
|
|
|
//
|
2012-02-12 20:09:41 +08:00
|
|
|
// @param SplitBlock The basic block where the control flow was split between
|
2011-05-15 03:01:49 +08:00
|
|
|
// old and new version of the Scop.
|
2012-02-12 20:09:41 +08:00
|
|
|
// @param Builder An IRBuilder that points to the last instruction of the
|
2011-05-15 03:01:49 +08:00
|
|
|
// newly generated code.
|
2012-02-12 20:09:41 +08:00
|
|
|
void mergeControlFlow(BasicBlock *SplitBlock, IRBuilder<> *Builder) {
|
|
|
|
BasicBlock *MergeBlock;
|
2011-05-15 03:01:49 +08:00
|
|
|
Region *R = region;
|
|
|
|
|
|
|
|
if (R->getExit()->getSinglePredecessor())
|
|
|
|
// No splitEdge required. A block with a single predecessor cannot have
|
|
|
|
// PHI nodes that would complicate life.
|
2012-02-12 20:09:41 +08:00
|
|
|
MergeBlock = R->getExit();
|
2011-05-15 03:01:49 +08:00
|
|
|
else {
|
2012-02-12 20:09:41 +08:00
|
|
|
MergeBlock = SplitEdge(R->getExitingBlock(), R->getExit(), this);
|
2011-05-15 03:01:49 +08:00
|
|
|
// SplitEdge will never split R->getExit(), as R->getExit() has more than
|
|
|
|
// one predecessor. Hence, mergeBlock is always a newly generated block.
|
2012-02-12 20:09:41 +08:00
|
|
|
R->replaceExit(MergeBlock);
|
2011-05-15 03:01:49 +08:00
|
|
|
}
|
|
|
|
|
2012-02-12 20:09:41 +08:00
|
|
|
Builder->CreateBr(MergeBlock);
|
2012-02-12 20:09:46 +08:00
|
|
|
MergeBlock->setName("polly.merge_new_and_old");
|
2011-05-15 03:01:49 +08:00
|
|
|
|
2012-02-12 20:09:41 +08:00
|
|
|
if (DT->dominates(SplitBlock, MergeBlock))
|
|
|
|
DT->changeImmediateDominator(MergeBlock, SplitBlock);
|
2011-05-15 03:01:49 +08:00
|
|
|
}
|
|
|
|
|
2011-04-29 14:27:02 +08:00
|
|
|
bool runOnScop(Scop &scop) {
|
|
|
|
S = &scop;
|
|
|
|
region = &S->getRegion();
|
|
|
|
DT = &getAnalysis<DominatorTree>();
|
2011-05-15 03:01:49 +08:00
|
|
|
RI = &getAnalysis<RegionInfo>();
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
parallelLoops.clear();
|
|
|
|
|
2011-05-15 03:01:49 +08:00
|
|
|
assert(region->isSimple() && "Only simple regions are supported");
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-02-01 22:23:33 +08:00
|
|
|
// In the CFG the optimized code of the SCoP is generated next to the
|
|
|
|
// original code. Both the new and the original version of the code remain
|
|
|
|
// in the CFG. A branch statement decides which version is executed.
|
|
|
|
// For now, we always execute the new version (the old one is dead code
|
|
|
|
// eliminated by the cleanup passes). In the future we may decide to execute
|
|
|
|
// the new version only if certain run time checks succeed. This will be
|
|
|
|
// useful to support constructs for which we cannot prove all assumptions at
|
|
|
|
// compile time.
|
2011-05-15 03:01:49 +08:00
|
|
|
//
|
|
|
|
// Before transformation:
|
|
|
|
//
|
|
|
|
// bb0
|
|
|
|
// |
|
|
|
|
// orig_scop
|
|
|
|
// |
|
|
|
|
// bb1
|
|
|
|
//
|
|
|
|
// After transformation:
|
|
|
|
// bb0
|
|
|
|
// |
|
|
|
|
// polly.splitBlock
|
2011-08-02 06:39:00 +08:00
|
|
|
// / \.
|
2011-05-15 03:01:49 +08:00
|
|
|
// | startBlock
|
|
|
|
// | |
|
|
|
|
// orig_scop new_scop
|
|
|
|
// \ /
|
|
|
|
// \ /
|
|
|
|
// bb1 (joinBlock)
|
|
|
|
IRBuilder<> builder(region->getEntry());
|
|
|
|
|
|
|
|
// The builder will be set to startBlock.
|
|
|
|
BasicBlock *splitBlock = addSplitAndStartBlock(&builder);
|
2012-02-14 22:02:27 +08:00
|
|
|
BasicBlock *StartBlock = builder.GetInsertBlock();
|
|
|
|
|
|
|
|
mergeControlFlow(splitBlock, &builder);
|
|
|
|
builder.SetInsertPoint(StartBlock->begin());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2012-03-15 17:34:52 +08:00
|
|
|
ClastStmtCodeGen CodeGen(S, builder, this);
|
2011-05-15 03:02:39 +08:00
|
|
|
CloogInfo &C = getAnalysis<CloogInfo>();
|
|
|
|
CodeGen.codegen(C.getClast());
|
2011-04-29 14:27:02 +08:00
|
|
|
|
|
|
|
parallelLoops.insert(parallelLoops.begin(),
|
|
|
|
CodeGen.getParallelLoops().begin(),
|
|
|
|
CodeGen.getParallelLoops().end());
|
|
|
|
|
2011-05-15 03:02:34 +08:00
|
|
|
return true;
|
2011-04-29 14:27:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void printScop(raw_ostream &OS) const {
|
|
|
|
for (std::vector<std::string>::const_iterator PI = parallelLoops.begin(),
|
|
|
|
PE = parallelLoops.end(); PI != PE; ++PI)
|
|
|
|
OS << "Parallel loop with iterator '" << *PI << "' generated\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<CloogInfo>();
|
|
|
|
AU.addRequired<Dependences>();
|
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addRequired<RegionInfo>();
|
2011-10-08 08:30:40 +08:00
|
|
|
AU.addRequired<ScalarEvolution>();
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.addRequired<ScopDetection>();
|
|
|
|
AU.addRequired<ScopInfo>();
|
|
|
|
AU.addRequired<TargetData>();
|
|
|
|
|
|
|
|
AU.addPreserved<CloogInfo>();
|
|
|
|
AU.addPreserved<Dependences>();
|
2011-05-15 03:02:45 +08:00
|
|
|
|
2011-05-23 23:23:36 +08:00
|
|
|
// FIXME: We do not create LoopInfo for the newly generated loops.
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.addPreserved<LoopInfo>();
|
|
|
|
AU.addPreserved<DominatorTree>();
|
|
|
|
AU.addPreserved<ScopDetection>();
|
|
|
|
AU.addPreserved<ScalarEvolution>();
|
2011-05-15 03:02:45 +08:00
|
|
|
|
2011-05-23 23:23:36 +08:00
|
|
|
// FIXME: We do not yet add regions for the newly generated code to the
|
|
|
|
// region tree.
|
2011-04-29 14:27:02 +08:00
|
|
|
AU.addPreserved<RegionInfo>();
|
|
|
|
AU.addPreserved<TempScopInfo>();
|
|
|
|
AU.addPreserved<ScopInfo>();
|
|
|
|
AU.addPreservedID(IndependentBlocksID);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char CodeGeneration::ID = 1;
|
|
|
|
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
|
2012-03-06 15:38:57 +08:00
|
|
|
"Polly - Create LLVM-IR from SCoPs", false, false)
|
2011-10-08 08:30:40 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(CloogInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(Dependences)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(RegionInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopDetection)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetData)
|
|
|
|
INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
|
2012-03-06 15:38:57 +08:00
|
|
|
"Polly - Create LLVM-IR from SCoPs", false, false)
|
2011-04-29 14:27:02 +08:00
|
|
|
|
2011-11-17 20:56:10 +08:00
|
|
|
Pass *polly::createCodeGenerationPass() {
|
2011-04-29 14:27:02 +08:00
|
|
|
return new CodeGeneration();
|
|
|
|
}
|