Coverage Report

Created: 2019-04-21 11:35

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