113c5c64bSJohannes Doerfert //===------ IslExprBuilder.cpp ----- Code generate isl AST expressions ----===//
213c5c64bSJohannes Doerfert //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
613c5c64bSJohannes Doerfert //
713c5c64bSJohannes Doerfert //===----------------------------------------------------------------------===//
813c5c64bSJohannes Doerfert //
913c5c64bSJohannes Doerfert //===----------------------------------------------------------------------===//
1013c5c64bSJohannes Doerfert
1113c5c64bSJohannes Doerfert #include "polly/CodeGen/IslExprBuilder.h"
121be726a4STobias Grosser #include "polly/CodeGen/RuntimeDebugBuilder.h"
13404a0f81SJohannes Doerfert #include "polly/Options.h"
141a28a893SJohannes Doerfert #include "polly/ScopInfo.h"
1513c5c64bSJohannes Doerfert #include "polly/Support/GICHelper.h"
16b021a4faSTobias Grosser #include "llvm/Transforms/Utils/BasicBlockUtils.h"
1713c5c64bSJohannes Doerfert
1813c5c64bSJohannes Doerfert using namespace llvm;
1913c5c64bSJohannes Doerfert using namespace polly;
2013c5c64bSJohannes Doerfert
21c80d6979STobias Grosser /// Different overflow tracking modes.
22404a0f81SJohannes Doerfert enum OverflowTrackingChoice {
23404a0f81SJohannes Doerfert OT_NEVER, ///< Never tack potential overflows.
24404a0f81SJohannes Doerfert OT_REQUEST, ///< Track potential overflows if requested.
25404a0f81SJohannes Doerfert OT_ALWAYS ///< Always track potential overflows.
26404a0f81SJohannes Doerfert };
27404a0f81SJohannes Doerfert
28404a0f81SJohannes Doerfert static cl::opt<OverflowTrackingChoice> OTMode(
29404a0f81SJohannes Doerfert "polly-overflow-tracking",
30404a0f81SJohannes Doerfert cl::desc("Define where potential integer overflows in generated "
31404a0f81SJohannes Doerfert "expressions should be tracked."),
32404a0f81SJohannes Doerfert cl::values(clEnumValN(OT_NEVER, "never", "Never track the overflow bit."),
33404a0f81SJohannes Doerfert clEnumValN(OT_REQUEST, "request",
34404a0f81SJohannes Doerfert "Track the overflow bit if requested."),
35404a0f81SJohannes Doerfert clEnumValN(OT_ALWAYS, "always",
36732afdd0SMehdi Amini "Always track the overflow bit.")),
37*95a13425SFangrui Song cl::Hidden, cl::init(OT_REQUEST), cl::cat(PollyCategory));
38404a0f81SJohannes Doerfert
IslExprBuilder(Scop & S,PollyIRBuilder & Builder,IDToValueTy & IDToValue,ValueMapT & GlobalMap,const DataLayout & DL,ScalarEvolution & SE,DominatorTree & DT,LoopInfo & LI,BasicBlock * StartBlock)39404a0f81SJohannes Doerfert IslExprBuilder::IslExprBuilder(Scop &S, PollyIRBuilder &Builder,
40404a0f81SJohannes Doerfert IDToValueTy &IDToValue, ValueMapT &GlobalMap,
41404a0f81SJohannes Doerfert const DataLayout &DL, ScalarEvolution &SE,
42acf80064SEli Friedman DominatorTree &DT, LoopInfo &LI,
43acf80064SEli Friedman BasicBlock *StartBlock)
44404a0f81SJohannes Doerfert : S(S), Builder(Builder), IDToValue(IDToValue), GlobalMap(GlobalMap),
45acf80064SEli Friedman DL(DL), SE(SE), DT(DT), LI(LI), StartBlock(StartBlock) {
46404a0f81SJohannes Doerfert OverflowState = (OTMode == OT_ALWAYS) ? Builder.getFalse() : nullptr;
47404a0f81SJohannes Doerfert }
48404a0f81SJohannes Doerfert
setTrackOverflow(bool Enable)49404a0f81SJohannes Doerfert void IslExprBuilder::setTrackOverflow(bool Enable) {
50404a0f81SJohannes Doerfert // If potential overflows are tracked always or never we ignore requests
51a6d48f59SMichael Kruse // to change the behavior.
52404a0f81SJohannes Doerfert if (OTMode != OT_REQUEST)
53404a0f81SJohannes Doerfert return;
54404a0f81SJohannes Doerfert
55404a0f81SJohannes Doerfert if (Enable) {
56404a0f81SJohannes Doerfert // If tracking should be enabled initialize the OverflowState.
57404a0f81SJohannes Doerfert OverflowState = Builder.getFalse();
58404a0f81SJohannes Doerfert } else {
59404a0f81SJohannes Doerfert // If tracking should be disabled just unset the OverflowState.
60404a0f81SJohannes Doerfert OverflowState = nullptr;
61404a0f81SJohannes Doerfert }
62404a0f81SJohannes Doerfert }
63404a0f81SJohannes Doerfert
getOverflowState() const64404a0f81SJohannes Doerfert Value *IslExprBuilder::getOverflowState() const {
65404a0f81SJohannes Doerfert // If the overflow tracking was requested but it is disabled we avoid the
66404a0f81SJohannes Doerfert // additional nullptr checks at the call sides but instead provide a
67404a0f81SJohannes Doerfert // meaningful result.
68404a0f81SJohannes Doerfert if (OTMode == OT_NEVER)
69404a0f81SJohannes Doerfert return Builder.getFalse();
70404a0f81SJohannes Doerfert return OverflowState;
71404a0f81SJohannes Doerfert }
72404a0f81SJohannes Doerfert
hasLargeInts(isl::ast_expr Expr)7375d133f0STobias Grosser bool IslExprBuilder::hasLargeInts(isl::ast_expr Expr) {
7475d133f0STobias Grosser enum isl_ast_expr_type Type = isl_ast_expr_get_type(Expr.get());
7575d133f0STobias Grosser
7675d133f0STobias Grosser if (Type == isl_ast_expr_id)
7775d133f0STobias Grosser return false;
7875d133f0STobias Grosser
7975d133f0STobias Grosser if (Type == isl_ast_expr_int) {
8075d133f0STobias Grosser isl::val Val = Expr.get_val();
8175d133f0STobias Grosser APInt APValue = APIntFromVal(Val);
8275d133f0STobias Grosser auto BitWidth = APValue.getBitWidth();
8375d133f0STobias Grosser return BitWidth >= 64;
8475d133f0STobias Grosser }
8575d133f0STobias Grosser
8675d133f0STobias Grosser assert(Type == isl_ast_expr_op && "Expected isl_ast_expr of type operation");
8775d133f0STobias Grosser
8875d133f0STobias Grosser int NumArgs = isl_ast_expr_get_op_n_arg(Expr.get());
8975d133f0STobias Grosser
9075d133f0STobias Grosser for (int i = 0; i < NumArgs; i++) {
9175d133f0STobias Grosser isl::ast_expr Operand = Expr.get_op_arg(i);
9275d133f0STobias Grosser if (hasLargeInts(Operand))
9375d133f0STobias Grosser return true;
9475d133f0STobias Grosser }
9575d133f0STobias Grosser
9675d133f0STobias Grosser return false;
9775d133f0STobias Grosser }
9875d133f0STobias Grosser
createBinOp(BinaryOperator::BinaryOps Opc,Value * LHS,Value * RHS,const Twine & Name)99404a0f81SJohannes Doerfert Value *IslExprBuilder::createBinOp(BinaryOperator::BinaryOps Opc, Value *LHS,
100404a0f81SJohannes Doerfert Value *RHS, const Twine &Name) {
1013717aa5dSTobias Grosser // Handle the plain operation (without overflow tracking) first.
1023717aa5dSTobias Grosser if (!OverflowState) {
103404a0f81SJohannes Doerfert switch (Opc) {
104404a0f81SJohannes Doerfert case Instruction::Add:
105404a0f81SJohannes Doerfert return Builder.CreateNSWAdd(LHS, RHS, Name);
106404a0f81SJohannes Doerfert case Instruction::Sub:
107404a0f81SJohannes Doerfert return Builder.CreateNSWSub(LHS, RHS, Name);
108404a0f81SJohannes Doerfert case Instruction::Mul:
109404a0f81SJohannes Doerfert return Builder.CreateNSWMul(LHS, RHS, Name);
110404a0f81SJohannes Doerfert default:
111404a0f81SJohannes Doerfert llvm_unreachable("Unknown binary operator!");
112404a0f81SJohannes Doerfert }
113404a0f81SJohannes Doerfert }
114404a0f81SJohannes Doerfert
115404a0f81SJohannes Doerfert Function *F = nullptr;
116404a0f81SJohannes Doerfert Module *M = Builder.GetInsertBlock()->getModule();
117404a0f81SJohannes Doerfert switch (Opc) {
118404a0f81SJohannes Doerfert case Instruction::Add:
119404a0f81SJohannes Doerfert F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow,
120404a0f81SJohannes Doerfert {LHS->getType()});
121404a0f81SJohannes Doerfert break;
122404a0f81SJohannes Doerfert case Instruction::Sub:
123404a0f81SJohannes Doerfert F = Intrinsic::getDeclaration(M, Intrinsic::ssub_with_overflow,
124404a0f81SJohannes Doerfert {LHS->getType()});
125404a0f81SJohannes Doerfert break;
126404a0f81SJohannes Doerfert case Instruction::Mul:
127404a0f81SJohannes Doerfert F = Intrinsic::getDeclaration(M, Intrinsic::smul_with_overflow,
128404a0f81SJohannes Doerfert {LHS->getType()});
129404a0f81SJohannes Doerfert break;
130404a0f81SJohannes Doerfert default:
131404a0f81SJohannes Doerfert llvm_unreachable("No overflow intrinsic for binary operator found!");
132404a0f81SJohannes Doerfert }
133404a0f81SJohannes Doerfert
134404a0f81SJohannes Doerfert auto *ResultStruct = Builder.CreateCall(F, {LHS, RHS}, Name);
135404a0f81SJohannes Doerfert assert(ResultStruct->getType()->isStructTy());
136404a0f81SJohannes Doerfert
137404a0f81SJohannes Doerfert auto *OverflowFlag =
138404a0f81SJohannes Doerfert Builder.CreateExtractValue(ResultStruct, 1, Name + ".obit");
139404a0f81SJohannes Doerfert
140404a0f81SJohannes Doerfert // If all overflows are tracked we do not combine the results as this could
141404a0f81SJohannes Doerfert // cause dominance problems. Instead we will always keep the last overflow
142404a0f81SJohannes Doerfert // flag as current state.
143404a0f81SJohannes Doerfert if (OTMode == OT_ALWAYS)
144404a0f81SJohannes Doerfert OverflowState = OverflowFlag;
145404a0f81SJohannes Doerfert else
146404a0f81SJohannes Doerfert OverflowState =
147404a0f81SJohannes Doerfert Builder.CreateOr(OverflowState, OverflowFlag, "polly.overflow.state");
148404a0f81SJohannes Doerfert
149404a0f81SJohannes Doerfert return Builder.CreateExtractValue(ResultStruct, 0, Name + ".res");
150404a0f81SJohannes Doerfert }
151404a0f81SJohannes Doerfert
createAdd(Value * LHS,Value * RHS,const Twine & Name)152404a0f81SJohannes Doerfert Value *IslExprBuilder::createAdd(Value *LHS, Value *RHS, const Twine &Name) {
153404a0f81SJohannes Doerfert return createBinOp(Instruction::Add, LHS, RHS, Name);
154404a0f81SJohannes Doerfert }
155404a0f81SJohannes Doerfert
createSub(Value * LHS,Value * RHS,const Twine & Name)156404a0f81SJohannes Doerfert Value *IslExprBuilder::createSub(Value *LHS, Value *RHS, const Twine &Name) {
157404a0f81SJohannes Doerfert return createBinOp(Instruction::Sub, LHS, RHS, Name);
158404a0f81SJohannes Doerfert }
159404a0f81SJohannes Doerfert
createMul(Value * LHS,Value * RHS,const Twine & Name)160404a0f81SJohannes Doerfert Value *IslExprBuilder::createMul(Value *LHS, Value *RHS, const Twine &Name) {
161404a0f81SJohannes Doerfert return createBinOp(Instruction::Mul, LHS, RHS, Name);
162404a0f81SJohannes Doerfert }
163404a0f81SJohannes Doerfert
getWidestType(Type * T1,Type * T2)1643717aa5dSTobias Grosser Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) {
16513c5c64bSJohannes Doerfert assert(isa<IntegerType>(T1) && isa<IntegerType>(T2));
16613c5c64bSJohannes Doerfert
16713c5c64bSJohannes Doerfert if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits())
16813c5c64bSJohannes Doerfert return T2;
16913c5c64bSJohannes Doerfert else
17013c5c64bSJohannes Doerfert return T1;
17113c5c64bSJohannes Doerfert }
17213c5c64bSJohannes Doerfert
createOpUnary(__isl_take isl_ast_expr * Expr)17313c5c64bSJohannes Doerfert Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) {
17413c5c64bSJohannes Doerfert assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus &&
17513c5c64bSJohannes Doerfert "Unsupported unary operation");
17613c5c64bSJohannes Doerfert
1773717aa5dSTobias Grosser Value *V;
1783717aa5dSTobias Grosser Type *MaxType = getType(Expr);
1793717aa5dSTobias Grosser assert(MaxType->isIntegerTy() &&
1800837c2daSJohannes Doerfert "Unary expressions can only be created for integer types");
18113c5c64bSJohannes Doerfert
1823717aa5dSTobias Grosser V = create(isl_ast_expr_get_op_arg(Expr, 0));
1833717aa5dSTobias Grosser MaxType = getWidestType(MaxType, V->getType());
1843717aa5dSTobias Grosser
1853717aa5dSTobias Grosser if (MaxType != V->getType())
1863717aa5dSTobias Grosser V = Builder.CreateSExt(V, MaxType);
1873717aa5dSTobias Grosser
18813c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
1893717aa5dSTobias Grosser return createSub(ConstantInt::getNullValue(MaxType), V);
19013c5c64bSJohannes Doerfert }
19113c5c64bSJohannes Doerfert
createOpNAry(__isl_take isl_ast_expr * Expr)19213c5c64bSJohannes Doerfert Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) {
19313c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
19413c5c64bSJohannes Doerfert "isl ast expression not of type isl_ast_op");
19513c5c64bSJohannes Doerfert assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 &&
19613c5c64bSJohannes Doerfert "We need at least two operands in an n-ary operation");
19713c5c64bSJohannes Doerfert
19843de1787STobias Grosser CmpInst::Predicate Pred;
19943de1787STobias Grosser switch (isl_ast_expr_get_op_type(Expr)) {
20043de1787STobias Grosser default:
20143de1787STobias Grosser llvm_unreachable("This is not a an n-ary isl ast expression");
20243de1787STobias Grosser case isl_ast_op_max:
20343de1787STobias Grosser Pred = CmpInst::ICMP_SGT;
20443de1787STobias Grosser break;
20543de1787STobias Grosser case isl_ast_op_min:
20643de1787STobias Grosser Pred = CmpInst::ICMP_SLT;
20743de1787STobias Grosser break;
20843de1787STobias Grosser }
20913c5c64bSJohannes Doerfert
21043de1787STobias Grosser Value *V = create(isl_ast_expr_get_op_arg(Expr, 0));
2113717aa5dSTobias Grosser
21243de1787STobias Grosser for (int i = 1; i < isl_ast_expr_get_op_n_arg(Expr); ++i) {
21343de1787STobias Grosser Value *OpV = create(isl_ast_expr_get_op_arg(Expr, i));
2143717aa5dSTobias Grosser Type *Ty = getWidestType(V->getType(), OpV->getType());
2153717aa5dSTobias Grosser
2163717aa5dSTobias Grosser if (Ty != OpV->getType())
2173717aa5dSTobias Grosser OpV = Builder.CreateSExt(OpV, Ty);
2183717aa5dSTobias Grosser
2193717aa5dSTobias Grosser if (Ty != V->getType())
2203717aa5dSTobias Grosser V = Builder.CreateSExt(V, Ty);
2213717aa5dSTobias Grosser
22243de1787STobias Grosser Value *Cmp = Builder.CreateICmp(Pred, V, OpV);
2233717aa5dSTobias Grosser V = Builder.CreateSelect(Cmp, V, OpV);
22413c5c64bSJohannes Doerfert }
22513c5c64bSJohannes Doerfert
2263717aa5dSTobias Grosser // TODO: We can truncate the result, if it fits into a smaller type. This can
2273717aa5dSTobias Grosser // help in cases where we have larger operands (e.g. i67) but the result is
2283717aa5dSTobias Grosser // known to fit into i64. Without the truncation, the larger i67 type may
2293717aa5dSTobias Grosser // force all subsequent operations to be performed on a non-native type.
23013c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
23113c5c64bSJohannes Doerfert return V;
23213c5c64bSJohannes Doerfert }
23313c5c64bSJohannes Doerfert
234ff9b37e9SNikita Popov std::pair<Value *, Type *>
createAccessAddress(__isl_take isl_ast_expr * Expr)235ad84c6f6SMichael Kruse IslExprBuilder::createAccessAddress(__isl_take isl_ast_expr *Expr) {
236ed878311SJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
237ed878311SJohannes Doerfert "isl ast expression not of type isl_ast_op");
238ed878311SJohannes Doerfert assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_access &&
239ed878311SJohannes Doerfert "not an access isl ast expression");
240f919d8b3STobias Grosser assert(isl_ast_expr_get_op_n_arg(Expr) >= 1 &&
241ed878311SJohannes Doerfert "We need at least two operands to create a member access.");
242ed878311SJohannes Doerfert
2431a28a893SJohannes Doerfert Value *Base, *IndexOp, *Access;
2441a28a893SJohannes Doerfert isl_ast_expr *BaseExpr;
2451a28a893SJohannes Doerfert isl_id *BaseId;
246a63b2579SJohannes Doerfert
2471a28a893SJohannes Doerfert BaseExpr = isl_ast_expr_get_op_arg(Expr, 0);
2481a28a893SJohannes Doerfert BaseId = isl_ast_expr_get_id(BaseExpr);
2491a28a893SJohannes Doerfert isl_ast_expr_free(BaseExpr);
2501a28a893SJohannes Doerfert
25104b909fcSTobias Grosser const ScopArrayInfo *SAI = nullptr;
25204b909fcSTobias Grosser
2531be726a4STobias Grosser if (PollyDebugPrinting)
2541be726a4STobias Grosser RuntimeDebugBuilder::createCPUPrinter(Builder, isl_id_get_name(BaseId));
2551be726a4STobias Grosser
25604b909fcSTobias Grosser if (IDToSAI)
25704b909fcSTobias Grosser SAI = (*IDToSAI)[BaseId];
25804b909fcSTobias Grosser
25904b909fcSTobias Grosser if (!SAI)
260206e9e3bSTobias Grosser SAI = ScopArrayInfo::getFromId(isl::manage(BaseId));
26104b909fcSTobias Grosser else
26204b909fcSTobias Grosser isl_id_free(BaseId);
26304b909fcSTobias Grosser
26404b909fcSTobias Grosser assert(SAI && "No ScopArrayInfo found for this isl_id.");
26504b909fcSTobias Grosser
2661a28a893SJohannes Doerfert Base = SAI->getBasePtr();
2670d8874c0STobias Grosser
2680d8874c0STobias Grosser if (auto NewBase = GlobalMap.lookup(Base))
2690d8874c0STobias Grosser Base = NewBase;
2700d8874c0STobias Grosser
271ed878311SJohannes Doerfert assert(Base->getType()->isPointerTy() && "Access base should be a pointer");
272314587d7STobias Grosser StringRef BaseName = Base->getName();
2731a28a893SJohannes Doerfert
2745db5d2daSTobias Grosser auto PointerTy = PointerType::get(SAI->getElementType(),
2755db5d2daSTobias Grosser Base->getType()->getPointerAddressSpace());
2765db5d2daSTobias Grosser if (Base->getType() != PointerTy) {
2775db5d2daSTobias Grosser Base =
2785db5d2daSTobias Grosser Builder.CreateBitCast(Base, PointerTy, "polly.access.cast." + BaseName);
2795db5d2daSTobias Grosser }
280ed878311SJohannes Doerfert
281f919d8b3STobias Grosser if (isl_ast_expr_get_op_n_arg(Expr) == 1) {
282f919d8b3STobias Grosser isl_ast_expr_free(Expr);
2831be726a4STobias Grosser if (PollyDebugPrinting)
2841be726a4STobias Grosser RuntimeDebugBuilder::createCPUPrinter(Builder, "\n");
285ff9b37e9SNikita Popov return {Base, SAI->getElementType()};
286f919d8b3STobias Grosser }
287f919d8b3STobias Grosser
2882ef33e9fSJohannes Doerfert IndexOp = nullptr;
2892ef33e9fSJohannes Doerfert for (unsigned u = 1, e = isl_ast_expr_get_op_n_arg(Expr); u < e; u++) {
2902ef33e9fSJohannes Doerfert Value *NextIndex = create(isl_ast_expr_get_op_arg(Expr, u));
2912ef33e9fSJohannes Doerfert assert(NextIndex->getType()->isIntegerTy() &&
292ed878311SJohannes Doerfert "Access index should be an integer");
293ed878311SJohannes Doerfert
2941be726a4STobias Grosser if (PollyDebugPrinting)
2951be726a4STobias Grosser RuntimeDebugBuilder::createCPUPrinter(Builder, "[", NextIndex, "]");
2961be726a4STobias Grosser
2973717aa5dSTobias Grosser if (!IndexOp) {
2983717aa5dSTobias Grosser IndexOp = NextIndex;
2993717aa5dSTobias Grosser } else {
3003717aa5dSTobias Grosser Type *Ty = getWidestType(NextIndex->getType(), IndexOp->getType());
3013717aa5dSTobias Grosser
3023717aa5dSTobias Grosser if (Ty != NextIndex->getType())
3033717aa5dSTobias Grosser NextIndex = Builder.CreateIntCast(NextIndex, Ty, true);
3043717aa5dSTobias Grosser if (Ty != IndexOp->getType())
3053717aa5dSTobias Grosser IndexOp = Builder.CreateIntCast(IndexOp, Ty, true);
3063717aa5dSTobias Grosser
3073717aa5dSTobias Grosser IndexOp = createAdd(IndexOp, NextIndex, "polly.access.add." + BaseName);
3083717aa5dSTobias Grosser }
3092ef33e9fSJohannes Doerfert
3102ef33e9fSJohannes Doerfert // For every but the last dimension multiply the size, for the last
3112ef33e9fSJohannes Doerfert // dimension we can exit the loop.
3122ef33e9fSJohannes Doerfert if (u + 1 >= e)
3132ef33e9fSJohannes Doerfert break;
3142ef33e9fSJohannes Doerfert
31526253843STobias Grosser const SCEV *DimSCEV = SAI->getDimensionSize(u);
3160d8874c0STobias Grosser
317762fbbe5SFlorian Hahn llvm::ValueToSCEVMapTy Map;
318762fbbe5SFlorian Hahn for (auto &KV : GlobalMap)
319762fbbe5SFlorian Hahn Map[KV.first] = SE.getSCEV(KV.second);
320f4bb7a6aSTobias Grosser DimSCEV = SCEVParameterRewriter::rewrite(DimSCEV, SE, Map);
321e69e1141SJohannes Doerfert Value *DimSize =
322e69e1141SJohannes Doerfert expandCodeFor(S, SE, DL, "polly", DimSCEV, DimSCEV->getType(),
323acf80064SEli Friedman &*Builder.GetInsertPoint(), nullptr,
324acf80064SEli Friedman StartBlock->getSinglePredecessor());
325c642e954STobias Grosser
3263717aa5dSTobias Grosser Type *Ty = getWidestType(DimSize->getType(), IndexOp->getType());
3273717aa5dSTobias Grosser
3283717aa5dSTobias Grosser if (Ty != IndexOp->getType())
3293717aa5dSTobias Grosser IndexOp = Builder.CreateSExtOrTrunc(IndexOp, Ty,
3303717aa5dSTobias Grosser "polly.access.sext." + BaseName);
3313717aa5dSTobias Grosser if (Ty != DimSize->getType())
3323717aa5dSTobias Grosser DimSize = Builder.CreateSExtOrTrunc(DimSize, Ty,
3333717aa5dSTobias Grosser "polly.access.sext." + BaseName);
334404a0f81SJohannes Doerfert IndexOp = createMul(IndexOp, DimSize, "polly.access.mul." + BaseName);
3352ef33e9fSJohannes Doerfert }
3362ef33e9fSJohannes Doerfert
3372c68ecccSNikita Popov Access = Builder.CreateGEP(SAI->getElementType(), Base, IndexOp,
3382c68ecccSNikita Popov "polly.access." + BaseName);
339ed878311SJohannes Doerfert
3401be726a4STobias Grosser if (PollyDebugPrinting)
3411be726a4STobias Grosser RuntimeDebugBuilder::createCPUPrinter(Builder, "\n");
342ed878311SJohannes Doerfert isl_ast_expr_free(Expr);
343ff9b37e9SNikita Popov return {Access, SAI->getElementType()};
344ed878311SJohannes Doerfert }
345ed878311SJohannes Doerfert
createOpAccess(__isl_take isl_ast_expr * Expr)346ad84c6f6SMichael Kruse Value *IslExprBuilder::createOpAccess(__isl_take isl_ast_expr *Expr) {
347ff9b37e9SNikita Popov auto Info = createAccessAddress(Expr);
348ff9b37e9SNikita Popov assert(Info.first && "Could not create op access address");
349ff9b37e9SNikita Popov return Builder.CreateLoad(Info.second, Info.first,
350ff9b37e9SNikita Popov Info.first->getName() + ".load");
351dcb5f1dcSJohannes Doerfert }
352dcb5f1dcSJohannes Doerfert
createOpBin(__isl_take isl_ast_expr * Expr)35313c5c64bSJohannes Doerfert Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
35413c5c64bSJohannes Doerfert Value *LHS, *RHS, *Res;
3553717aa5dSTobias Grosser Type *MaxType;
35613c5c64bSJohannes Doerfert isl_ast_op_type OpType;
35713c5c64bSJohannes Doerfert
35813c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
35913c5c64bSJohannes Doerfert "isl ast expression not of type isl_ast_op");
36013c5c64bSJohannes Doerfert assert(isl_ast_expr_get_op_n_arg(Expr) == 2 &&
36113c5c64bSJohannes Doerfert "not a binary isl ast expression");
36213c5c64bSJohannes Doerfert
36313c5c64bSJohannes Doerfert OpType = isl_ast_expr_get_op_type(Expr);
36413c5c64bSJohannes Doerfert
365561d36b3SJohannes Doerfert LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
366561d36b3SJohannes Doerfert RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
3670837c2daSJohannes Doerfert
3683717aa5dSTobias Grosser Type *LHSType = LHS->getType();
3693717aa5dSTobias Grosser Type *RHSType = RHS->getType();
3703717aa5dSTobias Grosser
3713717aa5dSTobias Grosser MaxType = getWidestType(LHSType, RHSType);
3723717aa5dSTobias Grosser
3733717aa5dSTobias Grosser // Take the result into account when calculating the widest type.
3743717aa5dSTobias Grosser //
3753717aa5dSTobias Grosser // For operations such as '+' the result may require a type larger than
3763717aa5dSTobias Grosser // the type of the individual operands. For other operations such as '/', the
3773717aa5dSTobias Grosser // result type cannot be larger than the type of the individual operand. isl
3783717aa5dSTobias Grosser // does not calculate correct types for these operations and we consequently
3793717aa5dSTobias Grosser // exclude those operations here.
38013c5c64bSJohannes Doerfert switch (OpType) {
38113c5c64bSJohannes Doerfert case isl_ast_op_pdiv_q:
38213c5c64bSJohannes Doerfert case isl_ast_op_pdiv_r:
38313c5c64bSJohannes Doerfert case isl_ast_op_div:
38413c5c64bSJohannes Doerfert case isl_ast_op_fdiv_q:
38513e222caSTobias Grosser case isl_ast_op_zdiv_r:
3863717aa5dSTobias Grosser // Do nothing
38713c5c64bSJohannes Doerfert break;
38813c5c64bSJohannes Doerfert case isl_ast_op_add:
38913c5c64bSJohannes Doerfert case isl_ast_op_sub:
39013c5c64bSJohannes Doerfert case isl_ast_op_mul:
3913717aa5dSTobias Grosser MaxType = getWidestType(MaxType, getType(Expr));
39213c5c64bSJohannes Doerfert break;
39313c5c64bSJohannes Doerfert default:
39413c5c64bSJohannes Doerfert llvm_unreachable("This is no binary isl ast expression");
39513c5c64bSJohannes Doerfert }
39613c5c64bSJohannes Doerfert
3973717aa5dSTobias Grosser if (MaxType != RHS->getType())
3983717aa5dSTobias Grosser RHS = Builder.CreateSExt(RHS, MaxType);
3993717aa5dSTobias Grosser
4003717aa5dSTobias Grosser if (MaxType != LHS->getType())
4013717aa5dSTobias Grosser LHS = Builder.CreateSExt(LHS, MaxType);
4023717aa5dSTobias Grosser
40313c5c64bSJohannes Doerfert switch (OpType) {
40413c5c64bSJohannes Doerfert default:
40513c5c64bSJohannes Doerfert llvm_unreachable("This is no binary isl ast expression");
40613c5c64bSJohannes Doerfert case isl_ast_op_add:
407404a0f81SJohannes Doerfert Res = createAdd(LHS, RHS);
40813c5c64bSJohannes Doerfert break;
40913c5c64bSJohannes Doerfert case isl_ast_op_sub:
410404a0f81SJohannes Doerfert Res = createSub(LHS, RHS);
41113c5c64bSJohannes Doerfert break;
41213c5c64bSJohannes Doerfert case isl_ast_op_mul:
413404a0f81SJohannes Doerfert Res = createMul(LHS, RHS);
41413c5c64bSJohannes Doerfert break;
41513c5c64bSJohannes Doerfert case isl_ast_op_div:
4163717aa5dSTobias Grosser Res = Builder.CreateSDiv(LHS, RHS, "pexp.div", true);
417cdb38e56STobias Grosser break;
41813c5c64bSJohannes Doerfert case isl_ast_op_pdiv_q: // Dividend is non-negative
4193717aa5dSTobias Grosser Res = Builder.CreateUDiv(LHS, RHS, "pexp.p_div_q");
42013c5c64bSJohannes Doerfert break;
4213717aa5dSTobias Grosser case isl_ast_op_fdiv_q: { // Round towards -infty
4223717aa5dSTobias Grosser if (auto *Const = dyn_cast<ConstantInt>(RHS)) {
4233717aa5dSTobias Grosser auto &Val = Const->getValue();
4243717aa5dSTobias Grosser if (Val.isPowerOf2() && Val.isNonNegative()) {
4253717aa5dSTobias Grosser Res = Builder.CreateAShr(LHS, Val.ceilLogBase2(), "polly.fdiv_q.shr");
426cb73f150STobias Grosser break;
4273717aa5dSTobias Grosser }
4283717aa5dSTobias Grosser }
4293717aa5dSTobias Grosser // TODO: Review code and check that this calculation does not yield
430a6d48f59SMichael Kruse // incorrect overflow in some edge cases.
4313717aa5dSTobias Grosser //
4323717aa5dSTobias Grosser // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
4333717aa5dSTobias Grosser Value *One = ConstantInt::get(MaxType, 1);
4343717aa5dSTobias Grosser Value *Zero = ConstantInt::get(MaxType, 0);
4353717aa5dSTobias Grosser Value *Sum1 = createSub(LHS, RHS, "pexp.fdiv_q.0");
4363717aa5dSTobias Grosser Value *Sum2 = createAdd(Sum1, One, "pexp.fdiv_q.1");
4373717aa5dSTobias Grosser Value *isNegative = Builder.CreateICmpSLT(LHS, Zero, "pexp.fdiv_q.2");
4383717aa5dSTobias Grosser Value *Dividend =
4393717aa5dSTobias Grosser Builder.CreateSelect(isNegative, Sum2, LHS, "pexp.fdiv_q.3");
4403717aa5dSTobias Grosser Res = Builder.CreateSDiv(Dividend, RHS, "pexp.fdiv_q.4");
4413717aa5dSTobias Grosser break;
4423717aa5dSTobias Grosser }
44313c5c64bSJohannes Doerfert case isl_ast_op_pdiv_r: // Dividend is non-negative
444cdb38e56STobias Grosser Res = Builder.CreateURem(LHS, RHS, "pexp.pdiv_r");
445cdb38e56STobias Grosser break;
4463717aa5dSTobias Grosser
44713e222caSTobias Grosser case isl_ast_op_zdiv_r: // Result only compared against zero
4485c527f99SMichael Kruse Res = Builder.CreateSRem(LHS, RHS, "pexp.zdiv_r");
44913c5c64bSJohannes Doerfert break;
45013c5c64bSJohannes Doerfert }
45113c5c64bSJohannes Doerfert
4523717aa5dSTobias Grosser // TODO: We can truncate the result, if it fits into a smaller type. This can
4533717aa5dSTobias Grosser // help in cases where we have larger operands (e.g. i67) but the result is
4543717aa5dSTobias Grosser // known to fit into i64. Without the truncation, the larger i67 type may
4553717aa5dSTobias Grosser // force all subsequent operations to be performed on a non-native type.
45613c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
45713c5c64bSJohannes Doerfert return Res;
45813c5c64bSJohannes Doerfert }
45913c5c64bSJohannes Doerfert
createOpSelect(__isl_take isl_ast_expr * Expr)46013c5c64bSJohannes Doerfert Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
46113c5c64bSJohannes Doerfert assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select &&
46213c5c64bSJohannes Doerfert "Unsupported unary isl ast expression");
46313c5c64bSJohannes Doerfert Value *LHS, *RHS, *Cond;
4643717aa5dSTobias Grosser Type *MaxType = getType(Expr);
46513c5c64bSJohannes Doerfert
46613c5c64bSJohannes Doerfert Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
467219b20e1SJohannes Doerfert if (!Cond->getType()->isIntegerTy(1))
468219b20e1SJohannes Doerfert Cond = Builder.CreateIsNotNull(Cond);
46913c5c64bSJohannes Doerfert
47013c5c64bSJohannes Doerfert LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
47113c5c64bSJohannes Doerfert RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
47213c5c64bSJohannes Doerfert
4733717aa5dSTobias Grosser MaxType = getWidestType(MaxType, LHS->getType());
4743717aa5dSTobias Grosser MaxType = getWidestType(MaxType, RHS->getType());
4753717aa5dSTobias Grosser
4763717aa5dSTobias Grosser if (MaxType != RHS->getType())
4773717aa5dSTobias Grosser RHS = Builder.CreateSExt(RHS, MaxType);
4783717aa5dSTobias Grosser
4793717aa5dSTobias Grosser if (MaxType != LHS->getType())
4803717aa5dSTobias Grosser LHS = Builder.CreateSExt(LHS, MaxType);
4813717aa5dSTobias Grosser
4823717aa5dSTobias Grosser // TODO: Do we want to truncate the result?
48313c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
48413c5c64bSJohannes Doerfert return Builder.CreateSelect(Cond, LHS, RHS);
48513c5c64bSJohannes Doerfert }
48613c5c64bSJohannes Doerfert
createOpICmp(__isl_take isl_ast_expr * Expr)48713c5c64bSJohannes Doerfert Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
48813c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
48913c5c64bSJohannes Doerfert "Expected an isl_ast_expr_op expression");
49013c5c64bSJohannes Doerfert
49113c5c64bSJohannes Doerfert Value *LHS, *RHS, *Res;
49213c5c64bSJohannes Doerfert
493561d36b3SJohannes Doerfert auto *Op0 = isl_ast_expr_get_op_arg(Expr, 0);
494561d36b3SJohannes Doerfert auto *Op1 = isl_ast_expr_get_op_arg(Expr, 1);
495561d36b3SJohannes Doerfert bool HasNonAddressOfOperand =
496561d36b3SJohannes Doerfert isl_ast_expr_get_type(Op0) != isl_ast_expr_op ||
497561d36b3SJohannes Doerfert isl_ast_expr_get_type(Op1) != isl_ast_expr_op ||
498561d36b3SJohannes Doerfert isl_ast_expr_get_op_type(Op0) != isl_ast_op_address_of ||
499561d36b3SJohannes Doerfert isl_ast_expr_get_op_type(Op1) != isl_ast_op_address_of;
50013c5c64bSJohannes Doerfert
501561d36b3SJohannes Doerfert LHS = create(Op0);
502561d36b3SJohannes Doerfert RHS = create(Op1);
503561d36b3SJohannes Doerfert
504561d36b3SJohannes Doerfert auto *LHSTy = LHS->getType();
505561d36b3SJohannes Doerfert auto *RHSTy = RHS->getType();
506561d36b3SJohannes Doerfert bool IsPtrType = LHSTy->isPointerTy() || RHSTy->isPointerTy();
507561d36b3SJohannes Doerfert bool UseUnsignedCmp = IsPtrType && !HasNonAddressOfOperand;
508561d36b3SJohannes Doerfert
509561d36b3SJohannes Doerfert auto *PtrAsIntTy = Builder.getIntNTy(DL.getPointerSizeInBits());
510561d36b3SJohannes Doerfert if (LHSTy->isPointerTy())
511561d36b3SJohannes Doerfert LHS = Builder.CreatePtrToInt(LHS, PtrAsIntTy);
512561d36b3SJohannes Doerfert if (RHSTy->isPointerTy())
513561d36b3SJohannes Doerfert RHS = Builder.CreatePtrToInt(RHS, PtrAsIntTy);
5145e8de624SJohannes Doerfert
5153717aa5dSTobias Grosser if (LHS->getType() != RHS->getType()) {
5163717aa5dSTobias Grosser Type *MaxType = LHS->getType();
5173717aa5dSTobias Grosser MaxType = getWidestType(MaxType, RHS->getType());
5183717aa5dSTobias Grosser
5193717aa5dSTobias Grosser if (MaxType != RHS->getType())
5203717aa5dSTobias Grosser RHS = Builder.CreateSExt(RHS, MaxType);
5213717aa5dSTobias Grosser
5223717aa5dSTobias Grosser if (MaxType != LHS->getType())
5233717aa5dSTobias Grosser LHS = Builder.CreateSExt(LHS, MaxType);
5243717aa5dSTobias Grosser }
52513c5c64bSJohannes Doerfert
5265e8de624SJohannes Doerfert isl_ast_op_type OpType = isl_ast_expr_get_op_type(Expr);
5275e8de624SJohannes Doerfert assert(OpType >= isl_ast_op_eq && OpType <= isl_ast_op_gt &&
5285e8de624SJohannes Doerfert "Unsupported ICmp isl ast expression");
529e7774f49SKazu Hirata static_assert(isl_ast_op_eq + 4 == isl_ast_op_gt,
5305e8de624SJohannes Doerfert "Isl ast op type interface changed");
5315e8de624SJohannes Doerfert
5325e8de624SJohannes Doerfert CmpInst::Predicate Predicates[5][2] = {
5335e8de624SJohannes Doerfert {CmpInst::ICMP_EQ, CmpInst::ICMP_EQ},
5345e8de624SJohannes Doerfert {CmpInst::ICMP_SLE, CmpInst::ICMP_ULE},
5355e8de624SJohannes Doerfert {CmpInst::ICMP_SLT, CmpInst::ICMP_ULT},
5365e8de624SJohannes Doerfert {CmpInst::ICMP_SGE, CmpInst::ICMP_UGE},
5375e8de624SJohannes Doerfert {CmpInst::ICMP_SGT, CmpInst::ICMP_UGT},
5385e8de624SJohannes Doerfert };
5395e8de624SJohannes Doerfert
540561d36b3SJohannes Doerfert Res = Builder.CreateICmp(Predicates[OpType - isl_ast_op_eq][UseUnsignedCmp],
541561d36b3SJohannes Doerfert LHS, RHS);
5425e8de624SJohannes Doerfert
54313c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
54413c5c64bSJohannes Doerfert return Res;
54513c5c64bSJohannes Doerfert }
54613c5c64bSJohannes Doerfert
createOpBoolean(__isl_take isl_ast_expr * Expr)54713c5c64bSJohannes Doerfert Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
54813c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
54913c5c64bSJohannes Doerfert "Expected an isl_ast_expr_op expression");
55013c5c64bSJohannes Doerfert
55113c5c64bSJohannes Doerfert Value *LHS, *RHS, *Res;
55213c5c64bSJohannes Doerfert isl_ast_op_type OpType;
55313c5c64bSJohannes Doerfert
55413c5c64bSJohannes Doerfert OpType = isl_ast_expr_get_op_type(Expr);
55513c5c64bSJohannes Doerfert
55613c5c64bSJohannes Doerfert assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
55713c5c64bSJohannes Doerfert "Unsupported isl_ast_op_type");
55813c5c64bSJohannes Doerfert
55913c5c64bSJohannes Doerfert LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
56013c5c64bSJohannes Doerfert RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
56113c5c64bSJohannes Doerfert
56213c5c64bSJohannes Doerfert // Even though the isl pretty printer prints the expressions as 'exp && exp'
56313c5c64bSJohannes Doerfert // or 'exp || exp', we actually code generate the bitwise expressions
56413c5c64bSJohannes Doerfert // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
56513c5c64bSJohannes Doerfert // but it is, due to the use of i1 types, otherwise equivalent. The reason
56613c5c64bSJohannes Doerfert // to go for bitwise operations is, that we assume the reduced control flow
567a6d48f59SMichael Kruse // will outweigh the overhead introduced by evaluating unneeded expressions.
56813c5c64bSJohannes Doerfert // The isl code generation currently does not take advantage of the fact that
56913c5c64bSJohannes Doerfert // the expression after an '||' or '&&' is in some cases not evaluated.
57013c5c64bSJohannes Doerfert // Evaluating it anyways does not cause any undefined behaviour.
57113c5c64bSJohannes Doerfert //
57213c5c64bSJohannes Doerfert // TODO: Document in isl itself, that the unconditionally evaluating the
57313c5c64bSJohannes Doerfert // second part of '||' or '&&' expressions is safe.
574b164c795SJohannes Doerfert if (!LHS->getType()->isIntegerTy(1))
575b164c795SJohannes Doerfert LHS = Builder.CreateIsNotNull(LHS);
576b164c795SJohannes Doerfert if (!RHS->getType()->isIntegerTy(1))
577b164c795SJohannes Doerfert RHS = Builder.CreateIsNotNull(RHS);
57813c5c64bSJohannes Doerfert
57913c5c64bSJohannes Doerfert switch (OpType) {
58013c5c64bSJohannes Doerfert default:
58113c5c64bSJohannes Doerfert llvm_unreachable("Unsupported boolean expression");
58213c5c64bSJohannes Doerfert case isl_ast_op_and:
58313c5c64bSJohannes Doerfert Res = Builder.CreateAnd(LHS, RHS);
58413c5c64bSJohannes Doerfert break;
58513c5c64bSJohannes Doerfert case isl_ast_op_or:
58613c5c64bSJohannes Doerfert Res = Builder.CreateOr(LHS, RHS);
58713c5c64bSJohannes Doerfert break;
58813c5c64bSJohannes Doerfert }
58913c5c64bSJohannes Doerfert
59013c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
59113c5c64bSJohannes Doerfert return Res;
59213c5c64bSJohannes Doerfert }
59313c5c64bSJohannes Doerfert
594b021a4faSTobias Grosser Value *
createOpBooleanConditional(__isl_take isl_ast_expr * Expr)595b021a4faSTobias Grosser IslExprBuilder::createOpBooleanConditional(__isl_take isl_ast_expr *Expr) {
596b021a4faSTobias Grosser assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
597b021a4faSTobias Grosser "Expected an isl_ast_expr_op expression");
598b021a4faSTobias Grosser
599b021a4faSTobias Grosser Value *LHS, *RHS;
600b021a4faSTobias Grosser isl_ast_op_type OpType;
601b021a4faSTobias Grosser
602b021a4faSTobias Grosser Function *F = Builder.GetInsertBlock()->getParent();
603b021a4faSTobias Grosser LLVMContext &Context = F->getContext();
604b021a4faSTobias Grosser
605b021a4faSTobias Grosser OpType = isl_ast_expr_get_op_type(Expr);
606b021a4faSTobias Grosser
607b021a4faSTobias Grosser assert((OpType == isl_ast_op_and_then || OpType == isl_ast_op_or_else) &&
608b021a4faSTobias Grosser "Unsupported isl_ast_op_type");
609b021a4faSTobias Grosser
610b021a4faSTobias Grosser auto InsertBB = Builder.GetInsertBlock();
611b021a4faSTobias Grosser auto InsertPoint = Builder.GetInsertPoint();
612b8f58b53SDuncan P. N. Exon Smith auto NextBB = SplitBlock(InsertBB, &*InsertPoint, &DT, &LI);
613b021a4faSTobias Grosser BasicBlock *CondBB = BasicBlock::Create(Context, "polly.cond", F);
614b021a4faSTobias Grosser LI.changeLoopFor(CondBB, LI.getLoopFor(InsertBB));
615b021a4faSTobias Grosser DT.addNewBlock(CondBB, InsertBB);
616b021a4faSTobias Grosser
617b021a4faSTobias Grosser InsertBB->getTerminator()->eraseFromParent();
618b021a4faSTobias Grosser Builder.SetInsertPoint(InsertBB);
619b021a4faSTobias Grosser auto BR = Builder.CreateCondBr(Builder.getTrue(), NextBB, CondBB);
620b021a4faSTobias Grosser
621b021a4faSTobias Grosser Builder.SetInsertPoint(CondBB);
622b021a4faSTobias Grosser Builder.CreateBr(NextBB);
623b021a4faSTobias Grosser
624b021a4faSTobias Grosser Builder.SetInsertPoint(InsertBB->getTerminator());
625b021a4faSTobias Grosser
626b021a4faSTobias Grosser LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
627b021a4faSTobias Grosser if (!LHS->getType()->isIntegerTy(1))
628b021a4faSTobias Grosser LHS = Builder.CreateIsNotNull(LHS);
629b021a4faSTobias Grosser auto LeftBB = Builder.GetInsertBlock();
630b021a4faSTobias Grosser
631b021a4faSTobias Grosser if (OpType == isl_ast_op_and || OpType == isl_ast_op_and_then)
632b021a4faSTobias Grosser BR->setCondition(Builder.CreateNeg(LHS));
633b021a4faSTobias Grosser else
634b021a4faSTobias Grosser BR->setCondition(LHS);
635b021a4faSTobias Grosser
636b021a4faSTobias Grosser Builder.SetInsertPoint(CondBB->getTerminator());
637b021a4faSTobias Grosser RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
638b021a4faSTobias Grosser if (!RHS->getType()->isIntegerTy(1))
639b021a4faSTobias Grosser RHS = Builder.CreateIsNotNull(RHS);
640b021a4faSTobias Grosser auto RightBB = Builder.GetInsertBlock();
641b021a4faSTobias Grosser
642b021a4faSTobias Grosser Builder.SetInsertPoint(NextBB->getTerminator());
643b021a4faSTobias Grosser auto PHI = Builder.CreatePHI(Builder.getInt1Ty(), 2);
644b021a4faSTobias Grosser PHI->addIncoming(OpType == isl_ast_op_and_then ? Builder.getFalse()
645b021a4faSTobias Grosser : Builder.getTrue(),
646b021a4faSTobias Grosser LeftBB);
647b021a4faSTobias Grosser PHI->addIncoming(RHS, RightBB);
648b021a4faSTobias Grosser
649b021a4faSTobias Grosser isl_ast_expr_free(Expr);
650b021a4faSTobias Grosser return PHI;
651b021a4faSTobias Grosser }
652b021a4faSTobias Grosser
createOp(__isl_take isl_ast_expr * Expr)65313c5c64bSJohannes Doerfert Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
65413c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
65513c5c64bSJohannes Doerfert "Expression not of type isl_ast_expr_op");
65613c5c64bSJohannes Doerfert switch (isl_ast_expr_get_op_type(Expr)) {
65713c5c64bSJohannes Doerfert case isl_ast_op_error:
65813c5c64bSJohannes Doerfert case isl_ast_op_cond:
65913c5c64bSJohannes Doerfert case isl_ast_op_call:
66013c5c64bSJohannes Doerfert case isl_ast_op_member:
66113c5c64bSJohannes Doerfert llvm_unreachable("Unsupported isl ast expression");
662ed878311SJohannes Doerfert case isl_ast_op_access:
663ed878311SJohannes Doerfert return createOpAccess(Expr);
66413c5c64bSJohannes Doerfert case isl_ast_op_max:
66513c5c64bSJohannes Doerfert case isl_ast_op_min:
66613c5c64bSJohannes Doerfert return createOpNAry(Expr);
66713c5c64bSJohannes Doerfert case isl_ast_op_add:
66813c5c64bSJohannes Doerfert case isl_ast_op_sub:
66913c5c64bSJohannes Doerfert case isl_ast_op_mul:
67013c5c64bSJohannes Doerfert case isl_ast_op_div:
67113c5c64bSJohannes Doerfert case isl_ast_op_fdiv_q: // Round towards -infty
67213c5c64bSJohannes Doerfert case isl_ast_op_pdiv_q: // Dividend is non-negative
67313c5c64bSJohannes Doerfert case isl_ast_op_pdiv_r: // Dividend is non-negative
67413e222caSTobias Grosser case isl_ast_op_zdiv_r: // Result only compared against zero
67513c5c64bSJohannes Doerfert return createOpBin(Expr);
67613c5c64bSJohannes Doerfert case isl_ast_op_minus:
67713c5c64bSJohannes Doerfert return createOpUnary(Expr);
67813c5c64bSJohannes Doerfert case isl_ast_op_select:
67913c5c64bSJohannes Doerfert return createOpSelect(Expr);
68013c5c64bSJohannes Doerfert case isl_ast_op_and:
68113c5c64bSJohannes Doerfert case isl_ast_op_or:
68213c5c64bSJohannes Doerfert return createOpBoolean(Expr);
683b021a4faSTobias Grosser case isl_ast_op_and_then:
684b021a4faSTobias Grosser case isl_ast_op_or_else:
685b021a4faSTobias Grosser return createOpBooleanConditional(Expr);
68613c5c64bSJohannes Doerfert case isl_ast_op_eq:
68713c5c64bSJohannes Doerfert case isl_ast_op_le:
68813c5c64bSJohannes Doerfert case isl_ast_op_lt:
68913c5c64bSJohannes Doerfert case isl_ast_op_ge:
69013c5c64bSJohannes Doerfert case isl_ast_op_gt:
69113c5c64bSJohannes Doerfert return createOpICmp(Expr);
692dcb5f1dcSJohannes Doerfert case isl_ast_op_address_of:
693dcb5f1dcSJohannes Doerfert return createOpAddressOf(Expr);
69413c5c64bSJohannes Doerfert }
69513c5c64bSJohannes Doerfert
69613c5c64bSJohannes Doerfert llvm_unreachable("Unsupported isl_ast_expr_op kind.");
69713c5c64bSJohannes Doerfert }
69813c5c64bSJohannes Doerfert
createOpAddressOf(__isl_take isl_ast_expr * Expr)699dcb5f1dcSJohannes Doerfert Value *IslExprBuilder::createOpAddressOf(__isl_take isl_ast_expr *Expr) {
700dcb5f1dcSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
701dcb5f1dcSJohannes Doerfert "Expected an isl_ast_expr_op expression.");
702dcb5f1dcSJohannes Doerfert assert(isl_ast_expr_get_op_n_arg(Expr) == 1 && "Address of should be unary.");
703dcb5f1dcSJohannes Doerfert
704dcb5f1dcSJohannes Doerfert isl_ast_expr *Op = isl_ast_expr_get_op_arg(Expr, 0);
705dcb5f1dcSJohannes Doerfert assert(isl_ast_expr_get_type(Op) == isl_ast_expr_op &&
706dcb5f1dcSJohannes Doerfert "Expected address of operator to be an isl_ast_expr_op expression.");
707dcb5f1dcSJohannes Doerfert assert(isl_ast_expr_get_op_type(Op) == isl_ast_op_access &&
708dcb5f1dcSJohannes Doerfert "Expected address of operator to be an access expression.");
709dcb5f1dcSJohannes Doerfert
710ff9b37e9SNikita Popov Value *V = createAccessAddress(Op).first;
711dcb5f1dcSJohannes Doerfert
712dcb5f1dcSJohannes Doerfert isl_ast_expr_free(Expr);
713dcb5f1dcSJohannes Doerfert
714dcb5f1dcSJohannes Doerfert return V;
715dcb5f1dcSJohannes Doerfert }
716dcb5f1dcSJohannes Doerfert
createId(__isl_take isl_ast_expr * Expr)71713c5c64bSJohannes Doerfert Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
71813c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id &&
71913c5c64bSJohannes Doerfert "Expression not of type isl_ast_expr_ident");
72013c5c64bSJohannes Doerfert
72113c5c64bSJohannes Doerfert isl_id *Id;
72213c5c64bSJohannes Doerfert Value *V;
72313c5c64bSJohannes Doerfert
72413c5c64bSJohannes Doerfert Id = isl_ast_expr_get_id(Expr);
72513c5c64bSJohannes Doerfert
72613c5c64bSJohannes Doerfert assert(IDToValue.count(Id) && "Identifier not found");
72713c5c64bSJohannes Doerfert
72813c5c64bSJohannes Doerfert V = IDToValue[Id];
729c4898504SJohannes Doerfert if (!V)
7303717aa5dSTobias Grosser V = UndefValue::get(getType(Expr));
73113c5c64bSJohannes Doerfert
732561d36b3SJohannes Doerfert if (V->getType()->isPointerTy())
733561d36b3SJohannes Doerfert V = Builder.CreatePtrToInt(V, Builder.getIntNTy(DL.getPointerSizeInBits()));
734561d36b3SJohannes Doerfert
7353284f197STobias Grosser assert(V && "Unknown parameter id found");
7363284f197STobias Grosser
73713c5c64bSJohannes Doerfert isl_id_free(Id);
73813c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
73913c5c64bSJohannes Doerfert
74013c5c64bSJohannes Doerfert return V;
74113c5c64bSJohannes Doerfert }
74213c5c64bSJohannes Doerfert
getType(__isl_keep isl_ast_expr * Expr)7433717aa5dSTobias Grosser IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) {
7443717aa5dSTobias Grosser // XXX: We assume i64 is large enough. This is often true, but in general
7453717aa5dSTobias Grosser // incorrect. Also, on 32bit architectures, it would be beneficial to
7463717aa5dSTobias Grosser // use a smaller type. We can and should directly derive this information
7473717aa5dSTobias Grosser // during code generation.
7483717aa5dSTobias Grosser return IntegerType::get(Builder.getContext(), 64);
7493717aa5dSTobias Grosser }
7503717aa5dSTobias Grosser
createInt(__isl_take isl_ast_expr * Expr)75113c5c64bSJohannes Doerfert Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
75213c5c64bSJohannes Doerfert assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int &&
75313c5c64bSJohannes Doerfert "Expression not of type isl_ast_expr_int");
7543717aa5dSTobias Grosser isl_val *Val;
7553717aa5dSTobias Grosser Value *V;
7563717aa5dSTobias Grosser APInt APValue;
7573717aa5dSTobias Grosser IntegerType *T;
75813c5c64bSJohannes Doerfert
7593717aa5dSTobias Grosser Val = isl_ast_expr_get_val(Expr);
7603717aa5dSTobias Grosser APValue = APIntFromVal(Val);
7613717aa5dSTobias Grosser
7623717aa5dSTobias Grosser auto BitWidth = APValue.getBitWidth();
7633717aa5dSTobias Grosser if (BitWidth <= 64)
7643717aa5dSTobias Grosser T = getType(Expr);
7653717aa5dSTobias Grosser else
7663717aa5dSTobias Grosser T = Builder.getIntNTy(BitWidth);
7673717aa5dSTobias Grosser
7686bec3e93SJay Foad APValue = APValue.sext(T->getBitWidth());
7693717aa5dSTobias Grosser V = ConstantInt::get(T, APValue);
77013c5c64bSJohannes Doerfert
77113c5c64bSJohannes Doerfert isl_ast_expr_free(Expr);
77213c5c64bSJohannes Doerfert return V;
77313c5c64bSJohannes Doerfert }
77413c5c64bSJohannes Doerfert
create(__isl_take isl_ast_expr * Expr)77513c5c64bSJohannes Doerfert Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
77613c5c64bSJohannes Doerfert switch (isl_ast_expr_get_type(Expr)) {
77713c5c64bSJohannes Doerfert case isl_ast_expr_error:
77813c5c64bSJohannes Doerfert llvm_unreachable("Code generation error");
77913c5c64bSJohannes Doerfert case isl_ast_expr_op:
78013c5c64bSJohannes Doerfert return createOp(Expr);
78113c5c64bSJohannes Doerfert case isl_ast_expr_id:
78213c5c64bSJohannes Doerfert return createId(Expr);
78313c5c64bSJohannes Doerfert case isl_ast_expr_int:
78413c5c64bSJohannes Doerfert return createInt(Expr);
78513c5c64bSJohannes Doerfert }
78613c5c64bSJohannes Doerfert
78713c5c64bSJohannes Doerfert llvm_unreachable("Unexpected enum value");
78813c5c64bSJohannes Doerfert }
789