Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 1 | //===------ IslExprBuilder.cpp ----- Code generate isl AST expressions ----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // 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 |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "polly/CodeGen/IslExprBuilder.h" |
Tobias Grosser | 1be726a | 2017-03-18 20:54:43 | [diff] [blame] | 12 | #include "polly/CodeGen/RuntimeDebugBuilder.h" |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 13 | #include "polly/Options.h" |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 | [diff] [blame] | 14 | #include "polly/ScopInfo.h" |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 15 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | b021a4f | 2015-03-04 18:14:59 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace polly; |
| 20 | |
Tobias Grosser | c80d697 | 2016-09-02 06:33:33 | [diff] [blame] | 21 | /// Different overflow tracking modes. |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 22 | enum OverflowTrackingChoice { |
| 23 | OT_NEVER, ///< Never tack potential overflows. |
| 24 | OT_REQUEST, ///< Track potential overflows if requested. |
| 25 | OT_ALWAYS ///< Always track potential overflows. |
| 26 | }; |
| 27 | |
| 28 | static cl::opt<OverflowTrackingChoice> OTMode( |
| 29 | "polly-overflow-tracking", |
| 30 | cl::desc("Define where potential integer overflows in generated " |
| 31 | "expressions should be tracked."), |
| 32 | cl::values(clEnumValN(OT_NEVER, "never", "Never track the overflow bit."), |
| 33 | clEnumValN(OT_REQUEST, "request", |
| 34 | "Track the overflow bit if requested."), |
| 35 | clEnumValN(OT_ALWAYS, "always", |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 | [diff] [blame] | 36 | "Always track the overflow bit.")), |
Fangrui Song | 95a1342 | 2022-06-05 08:07:50 | [diff] [blame] | 37 | cl::Hidden, cl::init(OT_REQUEST), cl::cat(PollyCategory)); |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 38 | |
| 39 | IslExprBuilder::IslExprBuilder(Scop &S, PollyIRBuilder &Builder, |
| 40 | IDToValueTy &IDToValue, ValueMapT &GlobalMap, |
| 41 | const DataLayout &DL, ScalarEvolution &SE, |
Eli Friedman | acf8006 | 2016-11-02 22:32:23 | [diff] [blame] | 42 | DominatorTree &DT, LoopInfo &LI, |
| 43 | BasicBlock *StartBlock) |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 44 | : S(S), Builder(Builder), IDToValue(IDToValue), GlobalMap(GlobalMap), |
Eli Friedman | acf8006 | 2016-11-02 22:32:23 | [diff] [blame] | 45 | DL(DL), SE(SE), DT(DT), LI(LI), StartBlock(StartBlock) { |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 46 | OverflowState = (OTMode == OT_ALWAYS) ? Builder.getFalse() : nullptr; |
| 47 | } |
| 48 | |
| 49 | void IslExprBuilder::setTrackOverflow(bool Enable) { |
| 50 | // If potential overflows are tracked always or never we ignore requests |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 | [diff] [blame] | 51 | // to change the behavior. |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 52 | if (OTMode != OT_REQUEST) |
| 53 | return; |
| 54 | |
| 55 | if (Enable) { |
| 56 | // If tracking should be enabled initialize the OverflowState. |
| 57 | OverflowState = Builder.getFalse(); |
| 58 | } else { |
| 59 | // If tracking should be disabled just unset the OverflowState. |
| 60 | OverflowState = nullptr; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | Value *IslExprBuilder::getOverflowState() const { |
| 65 | // If the overflow tracking was requested but it is disabled we avoid the |
| 66 | // additional nullptr checks at the call sides but instead provide a |
| 67 | // meaningful result. |
| 68 | if (OTMode == OT_NEVER) |
| 69 | return Builder.getFalse(); |
| 70 | return OverflowState; |
| 71 | } |
| 72 | |
Tobias Grosser | 75d133f | 2017-09-23 15:32:07 | [diff] [blame] | 73 | bool IslExprBuilder::hasLargeInts(isl::ast_expr Expr) { |
| 74 | enum isl_ast_expr_type Type = isl_ast_expr_get_type(Expr.get()); |
| 75 | |
| 76 | if (Type == isl_ast_expr_id) |
| 77 | return false; |
| 78 | |
| 79 | if (Type == isl_ast_expr_int) { |
| 80 | isl::val Val = Expr.get_val(); |
| 81 | APInt APValue = APIntFromVal(Val); |
| 82 | auto BitWidth = APValue.getBitWidth(); |
| 83 | return BitWidth >= 64; |
| 84 | } |
| 85 | |
| 86 | assert(Type == isl_ast_expr_op && "Expected isl_ast_expr of type operation"); |
| 87 | |
| 88 | int NumArgs = isl_ast_expr_get_op_n_arg(Expr.get()); |
| 89 | |
| 90 | for (int i = 0; i < NumArgs; i++) { |
| 91 | isl::ast_expr Operand = Expr.get_op_arg(i); |
| 92 | if (hasLargeInts(Operand)) |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 99 | Value *IslExprBuilder::createBinOp(BinaryOperator::BinaryOps Opc, Value *LHS, |
| 100 | Value *RHS, const Twine &Name) { |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 101 | // Handle the plain operation (without overflow tracking) first. |
| 102 | if (!OverflowState) { |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 103 | switch (Opc) { |
| 104 | case Instruction::Add: |
| 105 | return Builder.CreateNSWAdd(LHS, RHS, Name); |
| 106 | case Instruction::Sub: |
| 107 | return Builder.CreateNSWSub(LHS, RHS, Name); |
| 108 | case Instruction::Mul: |
| 109 | return Builder.CreateNSWMul(LHS, RHS, Name); |
| 110 | default: |
| 111 | llvm_unreachable("Unknown binary operator!"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | Function *F = nullptr; |
| 116 | Module *M = Builder.GetInsertBlock()->getModule(); |
| 117 | switch (Opc) { |
| 118 | case Instruction::Add: |
| 119 | F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow, |
| 120 | {LHS->getType()}); |
| 121 | break; |
| 122 | case Instruction::Sub: |
| 123 | F = Intrinsic::getDeclaration(M, Intrinsic::ssub_with_overflow, |
| 124 | {LHS->getType()}); |
| 125 | break; |
| 126 | case Instruction::Mul: |
| 127 | F = Intrinsic::getDeclaration(M, Intrinsic::smul_with_overflow, |
| 128 | {LHS->getType()}); |
| 129 | break; |
| 130 | default: |
| 131 | llvm_unreachable("No overflow intrinsic for binary operator found!"); |
| 132 | } |
| 133 | |
| 134 | auto *ResultStruct = Builder.CreateCall(F, {LHS, RHS}, Name); |
| 135 | assert(ResultStruct->getType()->isStructTy()); |
| 136 | |
| 137 | auto *OverflowFlag = |
| 138 | Builder.CreateExtractValue(ResultStruct, 1, Name + ".obit"); |
| 139 | |
| 140 | // If all overflows are tracked we do not combine the results as this could |
| 141 | // cause dominance problems. Instead we will always keep the last overflow |
| 142 | // flag as current state. |
| 143 | if (OTMode == OT_ALWAYS) |
| 144 | OverflowState = OverflowFlag; |
| 145 | else |
| 146 | OverflowState = |
| 147 | Builder.CreateOr(OverflowState, OverflowFlag, "polly.overflow.state"); |
| 148 | |
| 149 | return Builder.CreateExtractValue(ResultStruct, 0, Name + ".res"); |
| 150 | } |
| 151 | |
| 152 | Value *IslExprBuilder::createAdd(Value *LHS, Value *RHS, const Twine &Name) { |
| 153 | return createBinOp(Instruction::Add, LHS, RHS, Name); |
| 154 | } |
| 155 | |
| 156 | Value *IslExprBuilder::createSub(Value *LHS, Value *RHS, const Twine &Name) { |
| 157 | return createBinOp(Instruction::Sub, LHS, RHS, Name); |
| 158 | } |
| 159 | |
| 160 | Value *IslExprBuilder::createMul(Value *LHS, Value *RHS, const Twine &Name) { |
| 161 | return createBinOp(Instruction::Mul, LHS, RHS, Name); |
| 162 | } |
| 163 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 164 | Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) { |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 165 | assert(isa<IntegerType>(T1) && isa<IntegerType>(T2)); |
| 166 | |
| 167 | if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits()) |
| 168 | return T2; |
| 169 | else |
| 170 | return T1; |
| 171 | } |
| 172 | |
| 173 | Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) { |
| 174 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus && |
| 175 | "Unsupported unary operation"); |
| 176 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 177 | Value *V; |
| 178 | Type *MaxType = getType(Expr); |
| 179 | assert(MaxType->isIntegerTy() && |
Johannes Doerfert | 0837c2d | 2015-02-02 15:25:09 | [diff] [blame] | 180 | "Unary expressions can only be created for integer types"); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 181 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 182 | V = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 183 | MaxType = getWidestType(MaxType, V->getType()); |
| 184 | |
| 185 | if (MaxType != V->getType()) |
| 186 | V = Builder.CreateSExt(V, MaxType); |
| 187 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 188 | isl_ast_expr_free(Expr); |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 189 | return createSub(ConstantInt::getNullValue(MaxType), V); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) { |
| 193 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 194 | "isl ast expression not of type isl_ast_op"); |
| 195 | assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 && |
| 196 | "We need at least two operands in an n-ary operation"); |
| 197 | |
Tobias Grosser | 43de178 | 2016-06-12 04:49:41 | [diff] [blame] | 198 | CmpInst::Predicate Pred; |
| 199 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 200 | default: |
| 201 | llvm_unreachable("This is not a an n-ary isl ast expression"); |
| 202 | case isl_ast_op_max: |
| 203 | Pred = CmpInst::ICMP_SGT; |
| 204 | break; |
| 205 | case isl_ast_op_min: |
| 206 | Pred = CmpInst::ICMP_SLT; |
| 207 | break; |
| 208 | } |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 209 | |
Tobias Grosser | 43de178 | 2016-06-12 04:49:41 | [diff] [blame] | 210 | Value *V = create(isl_ast_expr_get_op_arg(Expr, 0)); |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 211 | |
Tobias Grosser | 43de178 | 2016-06-12 04:49:41 | [diff] [blame] | 212 | for (int i = 1; i < isl_ast_expr_get_op_n_arg(Expr); ++i) { |
| 213 | Value *OpV = create(isl_ast_expr_get_op_arg(Expr, i)); |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 214 | Type *Ty = getWidestType(V->getType(), OpV->getType()); |
| 215 | |
| 216 | if (Ty != OpV->getType()) |
| 217 | OpV = Builder.CreateSExt(OpV, Ty); |
| 218 | |
| 219 | if (Ty != V->getType()) |
| 220 | V = Builder.CreateSExt(V, Ty); |
| 221 | |
Tobias Grosser | 43de178 | 2016-06-12 04:49:41 | [diff] [blame] | 222 | Value *Cmp = Builder.CreateICmp(Pred, V, OpV); |
| 223 | V = Builder.CreateSelect(Cmp, V, OpV); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 224 | } |
| 225 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 226 | // TODO: We can truncate the result, if it fits into a smaller type. This can |
| 227 | // help in cases where we have larger operands (e.g. i67) but the result is |
| 228 | // known to fit into i64. Without the truncation, the larger i67 type may |
| 229 | // force all subsequent operations to be performed on a non-native type. |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 230 | isl_ast_expr_free(Expr); |
| 231 | return V; |
| 232 | } |
| 233 | |
Nikita Popov | ff9b37e | 2021-03-11 16:01:48 | [diff] [blame] | 234 | std::pair<Value *, Type *> |
Michael Kruse | ad84c6f | 2022-02-16 17:56:25 | [diff] [blame] | 235 | IslExprBuilder::createAccessAddress(__isl_take isl_ast_expr *Expr) { |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 236 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 237 | "isl ast expression not of type isl_ast_op"); |
| 238 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_access && |
| 239 | "not an access isl ast expression"); |
Tobias Grosser | f919d8b | 2016-08-04 13:57:29 | [diff] [blame] | 240 | assert(isl_ast_expr_get_op_n_arg(Expr) >= 1 && |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 241 | "We need at least two operands to create a member access."); |
| 242 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 | [diff] [blame] | 243 | Value *Base, *IndexOp, *Access; |
| 244 | isl_ast_expr *BaseExpr; |
| 245 | isl_id *BaseId; |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 | [diff] [blame] | 246 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 | [diff] [blame] | 247 | BaseExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 248 | BaseId = isl_ast_expr_get_id(BaseExpr); |
| 249 | isl_ast_expr_free(BaseExpr); |
| 250 | |
Tobias Grosser | 04b909f | 2016-07-21 13:15:51 | [diff] [blame] | 251 | const ScopArrayInfo *SAI = nullptr; |
| 252 | |
Tobias Grosser | 1be726a | 2017-03-18 20:54:43 | [diff] [blame] | 253 | if (PollyDebugPrinting) |
| 254 | RuntimeDebugBuilder::createCPUPrinter(Builder, isl_id_get_name(BaseId)); |
| 255 | |
Tobias Grosser | 04b909f | 2016-07-21 13:15:51 | [diff] [blame] | 256 | if (IDToSAI) |
| 257 | SAI = (*IDToSAI)[BaseId]; |
| 258 | |
| 259 | if (!SAI) |
Tobias Grosser | 206e9e3 | 2017-07-24 16:22:27 | [diff] [blame] | 260 | SAI = ScopArrayInfo::getFromId(isl::manage(BaseId)); |
Tobias Grosser | 04b909f | 2016-07-21 13:15:51 | [diff] [blame] | 261 | else |
| 262 | isl_id_free(BaseId); |
| 263 | |
| 264 | assert(SAI && "No ScopArrayInfo found for this isl_id."); |
| 265 | |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 | [diff] [blame] | 266 | Base = SAI->getBasePtr(); |
Tobias Grosser | 0d8874c | 2015-09-05 10:32:56 | [diff] [blame] | 267 | |
| 268 | if (auto NewBase = GlobalMap.lookup(Base)) |
| 269 | Base = NewBase; |
| 270 | |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 271 | assert(Base->getType()->isPointerTy() && "Access base should be a pointer"); |
Tobias Grosser | 314587d | 2015-01-07 07:43:34 | [diff] [blame] | 272 | StringRef BaseName = Base->getName(); |
Johannes Doerfert | 1a28a89 | 2014-10-05 11:32:18 | [diff] [blame] | 273 | |
Tobias Grosser | 5db5d2d | 2015-05-20 11:02:12 | [diff] [blame] | 274 | auto PointerTy = PointerType::get(SAI->getElementType(), |
| 275 | Base->getType()->getPointerAddressSpace()); |
| 276 | if (Base->getType() != PointerTy) { |
| 277 | Base = |
| 278 | Builder.CreateBitCast(Base, PointerTy, "polly.access.cast." + BaseName); |
| 279 | } |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 280 | |
Tobias Grosser | f919d8b | 2016-08-04 13:57:29 | [diff] [blame] | 281 | if (isl_ast_expr_get_op_n_arg(Expr) == 1) { |
| 282 | isl_ast_expr_free(Expr); |
Tobias Grosser | 1be726a | 2017-03-18 20:54:43 | [diff] [blame] | 283 | if (PollyDebugPrinting) |
| 284 | RuntimeDebugBuilder::createCPUPrinter(Builder, "\n"); |
Nikita Popov | ff9b37e | 2021-03-11 16:01:48 | [diff] [blame] | 285 | return {Base, SAI->getElementType()}; |
Tobias Grosser | f919d8b | 2016-08-04 13:57:29 | [diff] [blame] | 286 | } |
| 287 | |
Johannes Doerfert | 2ef33e9 | 2014-10-05 11:33:59 | [diff] [blame] | 288 | IndexOp = nullptr; |
| 289 | for (unsigned u = 1, e = isl_ast_expr_get_op_n_arg(Expr); u < e; u++) { |
| 290 | Value *NextIndex = create(isl_ast_expr_get_op_arg(Expr, u)); |
| 291 | assert(NextIndex->getType()->isIntegerTy() && |
| 292 | "Access index should be an integer"); |
| 293 | |
Tobias Grosser | 1be726a | 2017-03-18 20:54:43 | [diff] [blame] | 294 | if (PollyDebugPrinting) |
| 295 | RuntimeDebugBuilder::createCPUPrinter(Builder, "[", NextIndex, "]"); |
| 296 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 297 | if (!IndexOp) { |
| 298 | IndexOp = NextIndex; |
| 299 | } else { |
| 300 | Type *Ty = getWidestType(NextIndex->getType(), IndexOp->getType()); |
| 301 | |
| 302 | if (Ty != NextIndex->getType()) |
| 303 | NextIndex = Builder.CreateIntCast(NextIndex, Ty, true); |
| 304 | if (Ty != IndexOp->getType()) |
| 305 | IndexOp = Builder.CreateIntCast(IndexOp, Ty, true); |
| 306 | |
| 307 | IndexOp = createAdd(IndexOp, NextIndex, "polly.access.add." + BaseName); |
| 308 | } |
Johannes Doerfert | 2ef33e9 | 2014-10-05 11:33:59 | [diff] [blame] | 309 | |
| 310 | // For every but the last dimension multiply the size, for the last |
| 311 | // dimension we can exit the loop. |
| 312 | if (u + 1 >= e) |
| 313 | break; |
| 314 | |
Tobias Grosser | 2625384 | 2015-11-10 14:24:21 | [diff] [blame] | 315 | const SCEV *DimSCEV = SAI->getDimensionSize(u); |
Tobias Grosser | 0d8874c | 2015-09-05 10:32:56 | [diff] [blame] | 316 | |
Florian Hahn | 762fbbe | 2020-09-18 10:40:45 | [diff] [blame] | 317 | llvm::ValueToSCEVMapTy Map; |
| 318 | for (auto &KV : GlobalMap) |
| 319 | Map[KV.first] = SE.getSCEV(KV.second); |
Tobias Grosser | f4bb7a6 | 2015-10-04 10:18:32 | [diff] [blame] | 320 | DimSCEV = SCEVParameterRewriter::rewrite(DimSCEV, SE, Map); |
Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 | [diff] [blame] | 321 | Value *DimSize = |
| 322 | expandCodeFor(S, SE, DL, "polly", DimSCEV, DimSCEV->getType(), |
Eli Friedman | acf8006 | 2016-11-02 22:32:23 | [diff] [blame] | 323 | &*Builder.GetInsertPoint(), nullptr, |
| 324 | StartBlock->getSinglePredecessor()); |
Tobias Grosser | c642e954 | 2015-01-13 19:37:59 | [diff] [blame] | 325 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 326 | Type *Ty = getWidestType(DimSize->getType(), IndexOp->getType()); |
| 327 | |
| 328 | if (Ty != IndexOp->getType()) |
| 329 | IndexOp = Builder.CreateSExtOrTrunc(IndexOp, Ty, |
| 330 | "polly.access.sext." + BaseName); |
| 331 | if (Ty != DimSize->getType()) |
| 332 | DimSize = Builder.CreateSExtOrTrunc(DimSize, Ty, |
| 333 | "polly.access.sext." + BaseName); |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 334 | IndexOp = createMul(IndexOp, DimSize, "polly.access.mul." + BaseName); |
Johannes Doerfert | 2ef33e9 | 2014-10-05 11:33:59 | [diff] [blame] | 335 | } |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 336 | |
Nikita Popov | 2c68ecc | 2021-07-17 20:03:11 | [diff] [blame] | 337 | Access = Builder.CreateGEP(SAI->getElementType(), Base, IndexOp, |
| 338 | "polly.access." + BaseName); |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 339 | |
Tobias Grosser | 1be726a | 2017-03-18 20:54:43 | [diff] [blame] | 340 | if (PollyDebugPrinting) |
| 341 | RuntimeDebugBuilder::createCPUPrinter(Builder, "\n"); |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 342 | isl_ast_expr_free(Expr); |
Nikita Popov | ff9b37e | 2021-03-11 16:01:48 | [diff] [blame] | 343 | return {Access, SAI->getElementType()}; |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 344 | } |
| 345 | |
Michael Kruse | ad84c6f | 2022-02-16 17:56:25 | [diff] [blame] | 346 | Value *IslExprBuilder::createOpAccess(__isl_take isl_ast_expr *Expr) { |
Nikita Popov | ff9b37e | 2021-03-11 16:01:48 | [diff] [blame] | 347 | auto Info = createAccessAddress(Expr); |
| 348 | assert(Info.first && "Could not create op access address"); |
| 349 | return Builder.CreateLoad(Info.second, Info.first, |
| 350 | Info.first->getName() + ".load"); |
Johannes Doerfert | dcb5f1d | 2014-09-18 11:14:30 | [diff] [blame] | 351 | } |
| 352 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 353 | Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) { |
| 354 | Value *LHS, *RHS, *Res; |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 355 | Type *MaxType; |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 356 | isl_ast_op_type OpType; |
| 357 | |
| 358 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 359 | "isl ast expression not of type isl_ast_op"); |
| 360 | assert(isl_ast_expr_get_op_n_arg(Expr) == 2 && |
| 361 | "not a binary isl ast expression"); |
| 362 | |
| 363 | OpType = isl_ast_expr_get_op_type(Expr); |
| 364 | |
Johannes Doerfert | 561d36b | 2016-04-10 09:50:10 | [diff] [blame] | 365 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 366 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
Johannes Doerfert | 0837c2d | 2015-02-02 15:25:09 | [diff] [blame] | 367 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 368 | Type *LHSType = LHS->getType(); |
| 369 | Type *RHSType = RHS->getType(); |
| 370 | |
| 371 | MaxType = getWidestType(LHSType, RHSType); |
| 372 | |
| 373 | // Take the result into account when calculating the widest type. |
| 374 | // |
| 375 | // For operations such as '+' the result may require a type larger than |
| 376 | // the type of the individual operands. For other operations such as '/', the |
| 377 | // result type cannot be larger than the type of the individual operand. isl |
| 378 | // does not calculate correct types for these operations and we consequently |
| 379 | // exclude those operations here. |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 380 | switch (OpType) { |
| 381 | case isl_ast_op_pdiv_q: |
| 382 | case isl_ast_op_pdiv_r: |
| 383 | case isl_ast_op_div: |
| 384 | case isl_ast_op_fdiv_q: |
Tobias Grosser | 13e222c | 2014-12-07 16:04:29 | [diff] [blame] | 385 | case isl_ast_op_zdiv_r: |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 386 | // Do nothing |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 387 | break; |
| 388 | case isl_ast_op_add: |
| 389 | case isl_ast_op_sub: |
| 390 | case isl_ast_op_mul: |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 391 | MaxType = getWidestType(MaxType, getType(Expr)); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 392 | break; |
| 393 | default: |
| 394 | llvm_unreachable("This is no binary isl ast expression"); |
| 395 | } |
| 396 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 397 | if (MaxType != RHS->getType()) |
| 398 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 399 | |
| 400 | if (MaxType != LHS->getType()) |
| 401 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 402 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 403 | switch (OpType) { |
| 404 | default: |
| 405 | llvm_unreachable("This is no binary isl ast expression"); |
| 406 | case isl_ast_op_add: |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 407 | Res = createAdd(LHS, RHS); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 408 | break; |
| 409 | case isl_ast_op_sub: |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 410 | Res = createSub(LHS, RHS); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 411 | break; |
| 412 | case isl_ast_op_mul: |
Johannes Doerfert | 404a0f8 | 2016-05-12 15:12:43 | [diff] [blame] | 413 | Res = createMul(LHS, RHS); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 414 | break; |
| 415 | case isl_ast_op_div: |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 416 | Res = Builder.CreateSDiv(LHS, RHS, "pexp.div", true); |
Tobias Grosser | cdb38e5 | 2015-05-29 17:08:19 | [diff] [blame] | 417 | break; |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 418 | case isl_ast_op_pdiv_q: // Dividend is non-negative |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 419 | Res = Builder.CreateUDiv(LHS, RHS, "pexp.p_div_q"); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 420 | break; |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 421 | case isl_ast_op_fdiv_q: { // Round towards -infty |
| 422 | if (auto *Const = dyn_cast<ConstantInt>(RHS)) { |
| 423 | auto &Val = Const->getValue(); |
| 424 | if (Val.isPowerOf2() && Val.isNonNegative()) { |
| 425 | Res = Builder.CreateAShr(LHS, Val.ceilLogBase2(), "polly.fdiv_q.shr"); |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | // TODO: Review code and check that this calculation does not yield |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 | [diff] [blame] | 430 | // incorrect overflow in some edge cases. |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 431 | // |
| 432 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
| 433 | Value *One = ConstantInt::get(MaxType, 1); |
| 434 | Value *Zero = ConstantInt::get(MaxType, 0); |
| 435 | Value *Sum1 = createSub(LHS, RHS, "pexp.fdiv_q.0"); |
| 436 | Value *Sum2 = createAdd(Sum1, One, "pexp.fdiv_q.1"); |
| 437 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero, "pexp.fdiv_q.2"); |
| 438 | Value *Dividend = |
| 439 | Builder.CreateSelect(isNegative, Sum2, LHS, "pexp.fdiv_q.3"); |
| 440 | Res = Builder.CreateSDiv(Dividend, RHS, "pexp.fdiv_q.4"); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 441 | break; |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 442 | } |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 443 | case isl_ast_op_pdiv_r: // Dividend is non-negative |
Tobias Grosser | cdb38e5 | 2015-05-29 17:08:19 | [diff] [blame] | 444 | Res = Builder.CreateURem(LHS, RHS, "pexp.pdiv_r"); |
| 445 | break; |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 446 | |
Tobias Grosser | 13e222c | 2014-12-07 16:04:29 | [diff] [blame] | 447 | case isl_ast_op_zdiv_r: // Result only compared against zero |
Michael Kruse | 5c527f9 | 2016-06-03 18:51:48 | [diff] [blame] | 448 | Res = Builder.CreateSRem(LHS, RHS, "pexp.zdiv_r"); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 452 | // TODO: We can truncate the result, if it fits into a smaller type. This can |
| 453 | // help in cases where we have larger operands (e.g. i67) but the result is |
| 454 | // known to fit into i64. Without the truncation, the larger i67 type may |
| 455 | // force all subsequent operations to be performed on a non-native type. |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 456 | isl_ast_expr_free(Expr); |
| 457 | return Res; |
| 458 | } |
| 459 | |
| 460 | Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) { |
| 461 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select && |
| 462 | "Unsupported unary isl ast expression"); |
| 463 | Value *LHS, *RHS, *Cond; |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 464 | Type *MaxType = getType(Expr); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 465 | |
| 466 | Cond = create(isl_ast_expr_get_op_arg(Expr, 0)); |
Johannes Doerfert | 219b20e | 2014-10-07 14:37:59 | [diff] [blame] | 467 | if (!Cond->getType()->isIntegerTy(1)) |
| 468 | Cond = Builder.CreateIsNotNull(Cond); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 469 | |
| 470 | LHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 471 | RHS = create(isl_ast_expr_get_op_arg(Expr, 2)); |
| 472 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 473 | MaxType = getWidestType(MaxType, LHS->getType()); |
| 474 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 475 | |
| 476 | if (MaxType != RHS->getType()) |
| 477 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 478 | |
| 479 | if (MaxType != LHS->getType()) |
| 480 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 481 | |
| 482 | // TODO: Do we want to truncate the result? |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 483 | isl_ast_expr_free(Expr); |
| 484 | return Builder.CreateSelect(Cond, LHS, RHS); |
| 485 | } |
| 486 | |
| 487 | Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) { |
| 488 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 489 | "Expected an isl_ast_expr_op expression"); |
| 490 | |
| 491 | Value *LHS, *RHS, *Res; |
| 492 | |
Johannes Doerfert | 561d36b | 2016-04-10 09:50:10 | [diff] [blame] | 493 | auto *Op0 = isl_ast_expr_get_op_arg(Expr, 0); |
| 494 | auto *Op1 = isl_ast_expr_get_op_arg(Expr, 1); |
| 495 | bool HasNonAddressOfOperand = |
| 496 | isl_ast_expr_get_type(Op0) != isl_ast_expr_op || |
| 497 | isl_ast_expr_get_type(Op1) != isl_ast_expr_op || |
| 498 | isl_ast_expr_get_op_type(Op0) != isl_ast_op_address_of || |
| 499 | isl_ast_expr_get_op_type(Op1) != isl_ast_op_address_of; |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 500 | |
Johannes Doerfert | 561d36b | 2016-04-10 09:50:10 | [diff] [blame] | 501 | LHS = create(Op0); |
| 502 | RHS = create(Op1); |
| 503 | |
| 504 | auto *LHSTy = LHS->getType(); |
| 505 | auto *RHSTy = RHS->getType(); |
| 506 | bool IsPtrType = LHSTy->isPointerTy() || RHSTy->isPointerTy(); |
| 507 | bool UseUnsignedCmp = IsPtrType && !HasNonAddressOfOperand; |
| 508 | |
| 509 | auto *PtrAsIntTy = Builder.getIntNTy(DL.getPointerSizeInBits()); |
| 510 | if (LHSTy->isPointerTy()) |
| 511 | LHS = Builder.CreatePtrToInt(LHS, PtrAsIntTy); |
| 512 | if (RHSTy->isPointerTy()) |
| 513 | RHS = Builder.CreatePtrToInt(RHS, PtrAsIntTy); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 514 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 515 | if (LHS->getType() != RHS->getType()) { |
| 516 | Type *MaxType = LHS->getType(); |
| 517 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 518 | |
| 519 | if (MaxType != RHS->getType()) |
| 520 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 521 | |
| 522 | if (MaxType != LHS->getType()) |
| 523 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 524 | } |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 525 | |
Johannes Doerfert | 5e8de62 | 2014-09-18 11:14:07 | [diff] [blame] | 526 | isl_ast_op_type OpType = isl_ast_expr_get_op_type(Expr); |
| 527 | assert(OpType >= isl_ast_op_eq && OpType <= isl_ast_op_gt && |
| 528 | "Unsupported ICmp isl ast expression"); |
Kazu Hirata | e7774f49 | 2021-12-26 22:26:44 | [diff] [blame] | 529 | static_assert(isl_ast_op_eq + 4 == isl_ast_op_gt, |
| 530 | "Isl ast op type interface changed"); |
Johannes Doerfert | 5e8de62 | 2014-09-18 11:14:07 | [diff] [blame] | 531 | |
| 532 | CmpInst::Predicate Predicates[5][2] = { |
| 533 | {CmpInst::ICMP_EQ, CmpInst::ICMP_EQ}, |
| 534 | {CmpInst::ICMP_SLE, CmpInst::ICMP_ULE}, |
| 535 | {CmpInst::ICMP_SLT, CmpInst::ICMP_ULT}, |
| 536 | {CmpInst::ICMP_SGE, CmpInst::ICMP_UGE}, |
| 537 | {CmpInst::ICMP_SGT, CmpInst::ICMP_UGT}, |
| 538 | }; |
| 539 | |
Johannes Doerfert | 561d36b | 2016-04-10 09:50:10 | [diff] [blame] | 540 | Res = Builder.CreateICmp(Predicates[OpType - isl_ast_op_eq][UseUnsignedCmp], |
| 541 | LHS, RHS); |
Johannes Doerfert | 5e8de62 | 2014-09-18 11:14:07 | [diff] [blame] | 542 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 543 | isl_ast_expr_free(Expr); |
| 544 | return Res; |
| 545 | } |
| 546 | |
| 547 | Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) { |
| 548 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 549 | "Expected an isl_ast_expr_op expression"); |
| 550 | |
| 551 | Value *LHS, *RHS, *Res; |
| 552 | isl_ast_op_type OpType; |
| 553 | |
| 554 | OpType = isl_ast_expr_get_op_type(Expr); |
| 555 | |
| 556 | assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) && |
| 557 | "Unsupported isl_ast_op_type"); |
| 558 | |
| 559 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 560 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 561 | |
| 562 | // Even though the isl pretty printer prints the expressions as 'exp && exp' |
| 563 | // or 'exp || exp', we actually code generate the bitwise expressions |
| 564 | // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches, |
| 565 | // but it is, due to the use of i1 types, otherwise equivalent. The reason |
| 566 | // to go for bitwise operations is, that we assume the reduced control flow |
Michael Kruse | a6d48f5 | 2017-06-08 12:06:15 | [diff] [blame] | 567 | // will outweigh the overhead introduced by evaluating unneeded expressions. |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 568 | // The isl code generation currently does not take advantage of the fact that |
| 569 | // the expression after an '||' or '&&' is in some cases not evaluated. |
| 570 | // Evaluating it anyways does not cause any undefined behaviour. |
| 571 | // |
| 572 | // TODO: Document in isl itself, that the unconditionally evaluating the |
| 573 | // second part of '||' or '&&' expressions is safe. |
Johannes Doerfert | b164c79 | 2014-09-18 11:17:17 | [diff] [blame] | 574 | if (!LHS->getType()->isIntegerTy(1)) |
| 575 | LHS = Builder.CreateIsNotNull(LHS); |
| 576 | if (!RHS->getType()->isIntegerTy(1)) |
| 577 | RHS = Builder.CreateIsNotNull(RHS); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 578 | |
| 579 | switch (OpType) { |
| 580 | default: |
| 581 | llvm_unreachable("Unsupported boolean expression"); |
| 582 | case isl_ast_op_and: |
| 583 | Res = Builder.CreateAnd(LHS, RHS); |
| 584 | break; |
| 585 | case isl_ast_op_or: |
| 586 | Res = Builder.CreateOr(LHS, RHS); |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | isl_ast_expr_free(Expr); |
| 591 | return Res; |
| 592 | } |
| 593 | |
Tobias Grosser | b021a4f | 2015-03-04 18:14:59 | [diff] [blame] | 594 | Value * |
| 595 | IslExprBuilder::createOpBooleanConditional(__isl_take isl_ast_expr *Expr) { |
| 596 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 597 | "Expected an isl_ast_expr_op expression"); |
| 598 | |
| 599 | Value *LHS, *RHS; |
| 600 | isl_ast_op_type OpType; |
| 601 | |
| 602 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 603 | LLVMContext &Context = F->getContext(); |
| 604 | |
| 605 | OpType = isl_ast_expr_get_op_type(Expr); |
| 606 | |
| 607 | assert((OpType == isl_ast_op_and_then || OpType == isl_ast_op_or_else) && |
| 608 | "Unsupported isl_ast_op_type"); |
| 609 | |
| 610 | auto InsertBB = Builder.GetInsertBlock(); |
| 611 | auto InsertPoint = Builder.GetInsertPoint(); |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 | [diff] [blame] | 612 | auto NextBB = SplitBlock(InsertBB, &*InsertPoint, &DT, &LI); |
Tobias Grosser | b021a4f | 2015-03-04 18:14:59 | [diff] [blame] | 613 | BasicBlock *CondBB = BasicBlock::Create(Context, "polly.cond", F); |
| 614 | LI.changeLoopFor(CondBB, LI.getLoopFor(InsertBB)); |
| 615 | DT.addNewBlock(CondBB, InsertBB); |
| 616 | |
| 617 | InsertBB->getTerminator()->eraseFromParent(); |
| 618 | Builder.SetInsertPoint(InsertBB); |
| 619 | auto BR = Builder.CreateCondBr(Builder.getTrue(), NextBB, CondBB); |
| 620 | |
| 621 | Builder.SetInsertPoint(CondBB); |
| 622 | Builder.CreateBr(NextBB); |
| 623 | |
| 624 | Builder.SetInsertPoint(InsertBB->getTerminator()); |
| 625 | |
| 626 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 627 | if (!LHS->getType()->isIntegerTy(1)) |
| 628 | LHS = Builder.CreateIsNotNull(LHS); |
| 629 | auto LeftBB = Builder.GetInsertBlock(); |
| 630 | |
| 631 | if (OpType == isl_ast_op_and || OpType == isl_ast_op_and_then) |
| 632 | BR->setCondition(Builder.CreateNeg(LHS)); |
| 633 | else |
| 634 | BR->setCondition(LHS); |
| 635 | |
| 636 | Builder.SetInsertPoint(CondBB->getTerminator()); |
| 637 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 638 | if (!RHS->getType()->isIntegerTy(1)) |
| 639 | RHS = Builder.CreateIsNotNull(RHS); |
| 640 | auto RightBB = Builder.GetInsertBlock(); |
| 641 | |
| 642 | Builder.SetInsertPoint(NextBB->getTerminator()); |
| 643 | auto PHI = Builder.CreatePHI(Builder.getInt1Ty(), 2); |
| 644 | PHI->addIncoming(OpType == isl_ast_op_and_then ? Builder.getFalse() |
| 645 | : Builder.getTrue(), |
| 646 | LeftBB); |
| 647 | PHI->addIncoming(RHS, RightBB); |
| 648 | |
| 649 | isl_ast_expr_free(Expr); |
| 650 | return PHI; |
| 651 | } |
| 652 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 653 | Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) { |
| 654 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 655 | "Expression not of type isl_ast_expr_op"); |
| 656 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 657 | case isl_ast_op_error: |
| 658 | case isl_ast_op_cond: |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 659 | case isl_ast_op_call: |
| 660 | case isl_ast_op_member: |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 661 | llvm_unreachable("Unsupported isl ast expression"); |
Johannes Doerfert | ed87831 | 2014-08-03 01:50:50 | [diff] [blame] | 662 | case isl_ast_op_access: |
| 663 | return createOpAccess(Expr); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 664 | case isl_ast_op_max: |
| 665 | case isl_ast_op_min: |
| 666 | return createOpNAry(Expr); |
| 667 | case isl_ast_op_add: |
| 668 | case isl_ast_op_sub: |
| 669 | case isl_ast_op_mul: |
| 670 | case isl_ast_op_div: |
| 671 | case isl_ast_op_fdiv_q: // Round towards -infty |
| 672 | case isl_ast_op_pdiv_q: // Dividend is non-negative |
| 673 | case isl_ast_op_pdiv_r: // Dividend is non-negative |
Tobias Grosser | 13e222c | 2014-12-07 16:04:29 | [diff] [blame] | 674 | case isl_ast_op_zdiv_r: // Result only compared against zero |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 675 | return createOpBin(Expr); |
| 676 | case isl_ast_op_minus: |
| 677 | return createOpUnary(Expr); |
| 678 | case isl_ast_op_select: |
| 679 | return createOpSelect(Expr); |
| 680 | case isl_ast_op_and: |
| 681 | case isl_ast_op_or: |
| 682 | return createOpBoolean(Expr); |
Tobias Grosser | b021a4f | 2015-03-04 18:14:59 | [diff] [blame] | 683 | case isl_ast_op_and_then: |
| 684 | case isl_ast_op_or_else: |
| 685 | return createOpBooleanConditional(Expr); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 686 | case isl_ast_op_eq: |
| 687 | case isl_ast_op_le: |
| 688 | case isl_ast_op_lt: |
| 689 | case isl_ast_op_ge: |
| 690 | case isl_ast_op_gt: |
| 691 | return createOpICmp(Expr); |
Johannes Doerfert | dcb5f1d | 2014-09-18 11:14:30 | [diff] [blame] | 692 | case isl_ast_op_address_of: |
| 693 | return createOpAddressOf(Expr); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | llvm_unreachable("Unsupported isl_ast_expr_op kind."); |
| 697 | } |
| 698 | |
Johannes Doerfert | dcb5f1d | 2014-09-18 11:14:30 | [diff] [blame] | 699 | Value *IslExprBuilder::createOpAddressOf(__isl_take isl_ast_expr *Expr) { |
| 700 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 701 | "Expected an isl_ast_expr_op expression."); |
| 702 | assert(isl_ast_expr_get_op_n_arg(Expr) == 1 && "Address of should be unary."); |
| 703 | |
| 704 | isl_ast_expr *Op = isl_ast_expr_get_op_arg(Expr, 0); |
| 705 | assert(isl_ast_expr_get_type(Op) == isl_ast_expr_op && |
| 706 | "Expected address of operator to be an isl_ast_expr_op expression."); |
| 707 | assert(isl_ast_expr_get_op_type(Op) == isl_ast_op_access && |
| 708 | "Expected address of operator to be an access expression."); |
| 709 | |
Nikita Popov | ff9b37e | 2021-03-11 16:01:48 | [diff] [blame] | 710 | Value *V = createAccessAddress(Op).first; |
Johannes Doerfert | dcb5f1d | 2014-09-18 11:14:30 | [diff] [blame] | 711 | |
| 712 | isl_ast_expr_free(Expr); |
| 713 | |
| 714 | return V; |
| 715 | } |
| 716 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 717 | Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) { |
| 718 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id && |
| 719 | "Expression not of type isl_ast_expr_ident"); |
| 720 | |
| 721 | isl_id *Id; |
| 722 | Value *V; |
| 723 | |
| 724 | Id = isl_ast_expr_get_id(Expr); |
| 725 | |
| 726 | assert(IDToValue.count(Id) && "Identifier not found"); |
| 727 | |
| 728 | V = IDToValue[Id]; |
Johannes Doerfert | c489850 | 2015-11-07 19:46:04 | [diff] [blame] | 729 | if (!V) |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 730 | V = UndefValue::get(getType(Expr)); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 731 | |
Johannes Doerfert | 561d36b | 2016-04-10 09:50:10 | [diff] [blame] | 732 | if (V->getType()->isPointerTy()) |
| 733 | V = Builder.CreatePtrToInt(V, Builder.getIntNTy(DL.getPointerSizeInBits())); |
| 734 | |
Tobias Grosser | 3284f19 | 2015-03-10 22:35:43 | [diff] [blame] | 735 | assert(V && "Unknown parameter id found"); |
| 736 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 737 | isl_id_free(Id); |
| 738 | isl_ast_expr_free(Expr); |
| 739 | |
| 740 | return V; |
| 741 | } |
| 742 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 743 | IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) { |
| 744 | // XXX: We assume i64 is large enough. This is often true, but in general |
| 745 | // incorrect. Also, on 32bit architectures, it would be beneficial to |
| 746 | // use a smaller type. We can and should directly derive this information |
| 747 | // during code generation. |
| 748 | return IntegerType::get(Builder.getContext(), 64); |
| 749 | } |
| 750 | |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 751 | Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) { |
| 752 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int && |
| 753 | "Expression not of type isl_ast_expr_int"); |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 754 | isl_val *Val; |
| 755 | Value *V; |
| 756 | APInt APValue; |
| 757 | IntegerType *T; |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 758 | |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 759 | Val = isl_ast_expr_get_val(Expr); |
| 760 | APValue = APIntFromVal(Val); |
| 761 | |
| 762 | auto BitWidth = APValue.getBitWidth(); |
| 763 | if (BitWidth <= 64) |
| 764 | T = getType(Expr); |
| 765 | else |
| 766 | T = Builder.getIntNTy(BitWidth); |
| 767 | |
Jay Foad | 6bec3e9 | 2021-10-06 09:54:07 | [diff] [blame] | 768 | APValue = APValue.sext(T->getBitWidth()); |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 | [diff] [blame] | 769 | V = ConstantInt::get(T, APValue); |
Johannes Doerfert | 13c5c64 | 2014-07-29 21:06:08 | [diff] [blame] | 770 | |
| 771 | isl_ast_expr_free(Expr); |
| 772 | return V; |
| 773 | } |
| 774 | |
| 775 | Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) { |
| 776 | switch (isl_ast_expr_get_type(Expr)) { |
| 777 | case isl_ast_expr_error: |
| 778 | llvm_unreachable("Code generation error"); |
| 779 | case isl_ast_expr_op: |
| 780 | return createOp(Expr); |
| 781 | case isl_ast_expr_id: |
| 782 | return createId(Expr); |
| 783 | case isl_ast_expr_int: |
| 784 | return createInt(Expr); |
| 785 | } |
| 786 | |
| 787 | llvm_unreachable("Unexpected enum value"); |
| 788 | } |