Coverage Report

Created: 2018-10-23 09:19

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