Coverage Report

Created: 2017-11-23 03:11

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