Coverage Report

Created: 2017-06-28 17:40

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