-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoopSplitting.cpp
507 lines (416 loc) · 25.1 KB
/
LoopSplitting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/ScalarEvolutionExpander.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/Pass.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <typeinfo>
using namespace llvm;
#define DEBUG_TYPE "hello"
namespace {
struct SplitInfo {
Value *point;
Value *iterationValue;
Value *isTrueAtPoint;
Value *nextValue;
Value *isTrueAtNextPoint;
CmpInst *comparison;
int order;
SplitInfo(Value *point, Value *iterationValue, Value *isTrueAtPoint, Value *nextValue, Value *isTrueAtNextPoint, CmpInst *comparison, int order) {
this->point = point;
this->isTrueAtPoint = isTrueAtPoint;
this->comparison = comparison;
this->iterationValue = iterationValue;
this->nextValue = nextValue;
this->isTrueAtNextPoint = isTrueAtNextPoint;
this->order = order;
}
};
// Hello2 - The second implementation with getAnalysisUsage implemented.
struct LoopSplitting : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
private:
SCEVExpander *expander;
public:
LoopSplitting() : FunctionPass(ID) {}
Value * calculateIntersectionPoint(const SCEVAddRecExpr *first,
const SCEVAddRecExpr *second,
CmpInst *compare,
IRBuilder<> *builder) {
auto predicate = compare->getPredicate();
assert(first->isAffine() && second->isAffine() && "SCEVs must be affine");
assert((predicate == CmpInst::ICMP_UGE || predicate == CmpInst::ICMP_ULT || predicate == CmpInst::ICMP_ULE ||
predicate == CmpInst::ICMP_SGT || predicate == CmpInst::ICMP_SGE || predicate == CmpInst::ICMP_SLT ||
predicate == CmpInst::ICMP_SLE) &&
"We only treat integer inequalities for now");
const auto b1 = this->expander->expandCodeFor(first->getOperand(0),
first->getOperand(1)->getType(),
compare);
const auto a1 = this->expander->expandCodeFor(first->getOperand(1),
first->getOperand(0)->getType(),
compare);
const auto b2 = this->expander->expandCodeFor(second->getOperand(0),
second->getOperand(1)->getType(),
compare);
const auto a2 = this->expander->expandCodeFor(second->getOperand(1),
second->getOperand(0)->getType(),
compare);
const auto b = builder->CreateSub(b2, b1, "b"); // b2 - b1
const auto a = builder->CreateSub(a1, a2, "a"); // a1 - a2
const auto div = builder->CreateSDiv(b, a, "div"); // b / a
return div;
}
Value * calculateIntersectionPoint(const SCEVAddRecExpr *add,
Value *constant,
CmpInst *compare,
IRBuilder<> &builder) {
auto predicate = compare->getPredicate();
assert(add->isAffine() && "SCEVs must be affine");
assert((predicate == CmpInst::ICMP_UGE || predicate == CmpInst::ICMP_ULT ||
predicate == CmpInst::ICMP_ULE || predicate == CmpInst::ICMP_SGT ||
predicate == CmpInst::ICMP_SGE || predicate == CmpInst::ICMP_SLT ||
predicate == CmpInst::ICMP_SLE || predicate == CmpInst::ICMP_UGT) &&
"We only treat integer inequalities for now");
const auto b = this->expander->expandCodeFor(add->getOperand(0),
constant->getType(),
compare->getParent()->getFirstNonPHI());
const auto a = this->expander->expandCodeFor(add->getOperand(1),
constant->getType(),
builder.GetInsertBlock()->getFirstNonPHI());
const auto y = constant;
// Como y = ax + b
// colocamos como y a constante e calculamos x a partir disso:
// Ou seja: x = (y - b) / a
const auto temp1 = builder.CreateSub(y, b, "yMinusB"); // y - b
const auto x = builder.CreateSDiv(temp1, a, "x"); // (y - b) / a
return x;
}
bool isExiting(const Loop &L, BasicBlock *BB) {
bool exiting = L.isLoopExiting(BB);
for (Loop *loop : L.getSubLoops()) {
exiting = isExiting(*loop, BB) || exiting;
}
return exiting;
}
std::set<CmpInst *> getLoopComparisons(LoopInfo &LI, const Loop &L) {
std::set<CmpInst *> AllCmps;
for (BasicBlock *BB : L.blocks()) {
for (Instruction &I : *BB) {
if (SelectInst *Sel = dyn_cast<SelectInst>(&I)) {
if (CmpInst *Cmp = dyn_cast<CmpInst>(Sel->getCondition()))
AllCmps.insert(Cmp);
}
}
bool exiting = false;
for (Loop *loop : LI) {
exiting = isExiting(*loop, BB);
}
if (!LI.getLoopFor(BB)->isLoopExiting(BB)) {
BranchInst *Br = dyn_cast<BranchInst>(BB->getTerminator());
if (Br && Br->isConditional()) {
if (CmpInst *Cmp = dyn_cast<CmpInst>(Br->getCondition())) {
AllCmps.insert(Cmp);
}
}
}
}
return AllCmps;
}
std::set<CmpInst *> collectCandidateInstructions(LoopInfo &LI, Loop &loop) {
auto comparisons = std::set<CmpInst *>();
for (Loop *subloop : loop.getSubLoops()) {
auto subLoopComparisons = collectCandidateInstructions(LI, *subloop);
comparisons.insert(subLoopComparisons.begin(), subLoopComparisons.end());
}
auto loopComparisons = getLoopComparisons(LI, loop);
comparisons.insert(loopComparisons.begin(), loopComparisons.end());
return comparisons;
}
std::set<CmpInst *> collectCandidateInstructions(LoopInfo &LI) {
auto allComparisons = std::set<CmpInst *>();
for (Loop *loop : LI) {
auto loopComparisons = collectCandidateInstructions(LI, *loop);
allComparisons.insert(loopComparisons.begin(), loopComparisons.end());
}
return allComparisons;
}
bool isAffineAndConstantComparison(ScalarEvolution &SE, CmpInst &comparison) {
auto first = SE.getSCEV(comparison.getOperand(0));
auto second = SE.getSCEV(comparison.getOperand(1));
if ((first->getSCEVType() == scConstant || second->getSCEVType() == scConstant) &&
(first->getSCEVType() == scAddRecExpr || second->getSCEVType() == scAddRecExpr)) {
auto *addRec = dyn_cast<SCEVAddRecExpr>(first);
if (addRec == nullptr) {
addRec = dyn_cast<SCEVAddRecExpr>(second);
}
return addRec->isAffine();
}
return false;
}
bool isBothAffineComparison(ScalarEvolution &SE, CmpInst &comparison) {
auto first = SE.getSCEV(comparison.getOperand(0));
auto second = SE.getSCEV(comparison.getOperand(1));
if (first->getSCEVType() == scAddRecExpr && second->getSCEVType() == scAddRecExpr) {
auto *firstAddRec = cast<SCEVAddRecExpr>(first);
auto *secondAddRec = cast<SCEVAddRecExpr>(second);
return firstAddRec->isAffine() && secondAddRec->isAffine();
}
return false;
}
Loop * getLoop(CmpInst *comparison, LoopInfo &LI, ScalarEvolution &SE) {
if (isAffineAndConstantComparison(SE, *comparison)) {
const SCEVAddRecExpr *affine;
auto first = SE.getSCEV(comparison->getOperand(0));
auto second = SE.getSCEV(comparison->getOperand(1));
if (first->getSCEVType() == scConstant) {
affine = cast<SCEVAddRecExpr>(second);
} else {
affine = cast<SCEVAddRecExpr>(first);
}
auto loop = affine->getLoop(); // const reference, find it in hierarchy :(
auto tempLoop = LI.getLoopFor(comparison->getParent());
while (true) {
if (loop == tempLoop) {
return tempLoop;
}
tempLoop = tempLoop->getParentLoop();
}
} else if (isBothAffineComparison(SE, *comparison)) {
auto firstAffine = cast<SCEVAddRecExpr>(SE.getSCEV(comparison->getOperand(0)));
auto secondAffine = cast<SCEVAddRecExpr>(SE.getSCEV(comparison->getOperand(1)));
auto firstLoop = firstAffine->getLoop();
auto secondLoop = secondAffine->getLoop();
auto tempLoop = LI.getLoopFor(comparison->getParent());
while (true) {
if (firstLoop == tempLoop || secondLoop == tempLoop) {
return tempLoop;
}
tempLoop = tempLoop->getParentLoop();
}
}
return nullptr;
}
SplitInfo *splitInforForAddRecAndConstant(ScalarEvolution &SE,
LoopInfo &LI,
CmpInst *instruction,
const SCEVAddRecExpr *affine,
Value *constant,
bool firstIsConstant,
Instruction *constantInstruction = nullptr) {
constant->print(errs());
errs() << "\n";
auto loop = getLoop(instruction, LI, SE);
auto insertionPoint = loop->getLoopPreheader()->getTerminator();
auto builder = IRBuilder<>(insertionPoint);
if (constantInstruction != nullptr)
builder.Insert(constantInstruction);
auto value = calculateIntersectionPoint(affine, constant, instruction, builder);
auto scevValue = SE.getSCEV(value);
auto iterationScev = affine->evaluateAtIteration(scevValue, SE);
auto iterationValue = expander->expandCodeFor(iterationScev, constant->getType(), insertionPoint);
auto nextScevValue = SE.getAddExpr(scevValue, SE.getOne(value->getType()));
auto nextIterationScev = affine->evaluateAtIteration(nextScevValue, SE);
auto nextIterationValue = expander->expandCodeFor(nextIterationScev,
constant->getType(), insertionPoint);
Value *v = builder.CreateICmp(instruction->getPredicate(),
firstIsConstant ? constant : iterationValue,
firstIsConstant ? iterationValue : constant);
Value *nextPoint = builder.CreateICmp(instruction->getPredicate(),
firstIsConstant ? constant : nextIterationValue,
firstIsConstant ? nextIterationValue : constant);
auto loopBranch = cast<BranchInst>(loop->getExitingBlock()->getTerminator());
assert(loopBranch->isConditional() && "Expected to be conditional!");
CmpInst *exitCmp = cast<CmpInst>(loopBranch->getCondition());
auto loopScev = cast<SCEVAddRecExpr>(SE.getSCEV(exitCmp->getOperand(0)));
auto loopEndValue = expander->expandCodeFor(loopScev->evaluateAtIteration(scevValue, SE), constant->getType(), insertionPoint);
auto valueAfterLoop = expander->expandCodeFor(loopScev->evaluateAtIteration(nextScevValue, SE), constant->getType(), insertionPoint);
auto splitInfo = new SplitInfo(value, loopEndValue, v, valueAfterLoop, nextPoint, instruction, firstIsConstant ? 0 : 1);
return splitInfo;
}
std::vector<SplitInfo> comparisonToSplitInfo(ScalarEvolution &SE,
LoopInfo &LI,
std::set<CmpInst *> &comparisons) {
auto splitInfoVector = std::vector<SplitInfo>();
for (CmpInst *instruction : comparisons) {
if (isAffineAndConstantComparison(SE, *instruction)) {
auto first = SE.getSCEV(instruction->getOperand(0));
auto second = SE.getSCEV(instruction->getOperand(1));
const SCEVConstant *constant;
const SCEVAddRecExpr *affine;
bool firstIsConstant = false;
if (first->getSCEVType() == scConstant) {
constant = cast<SCEVConstant>(first);
affine = cast<SCEVAddRecExpr>(second);
firstIsConstant = true;
} else {
constant = cast<SCEVConstant>(second);
affine = cast<SCEVAddRecExpr>(first);
}
auto splitInfo = splitInforForAddRecAndConstant(SE,
LI,
instruction,
affine,
constant->getValue(),
firstIsConstant);
splitInfoVector.push_back(*splitInfo);
} else if (isBothAffineComparison(SE, *instruction)) {
auto firstAffine = cast<SCEVAddRecExpr>(SE.getSCEV(instruction->getOperand(0)));
auto secondAffine = cast<SCEVAddRecExpr>(SE.getSCEV(instruction->getOperand(1)));
if (firstAffine->getLoop() == secondAffine->getLoop()) {
auto loop = getLoop(instruction, LI, SE);
auto insertionPoint = loop->getLoopPreheader()->getFirstNonPHI();
auto builder = IRBuilder<>(insertionPoint);
auto value = calculateIntersectionPoint(firstAffine, secondAffine, instruction, &builder);
auto scevValue = SE.getSCEV(value);
auto firstIterationScev = firstAffine->evaluateAtIteration(scevValue, SE);
auto firstIterationValue = expander->expandCodeFor(firstIterationScev, value->getType(), insertionPoint);
auto secondIterationScev = secondAffine->evaluateAtIteration(scevValue, SE);
auto secondIterationValue = expander->expandCodeFor(secondIterationScev, value->getType(), insertionPoint);
auto nextScevValue = SE.getAddExpr(scevValue, SE.getOne(value->getType()));
auto firstNextIterationScev = firstAffine->evaluateAtIteration(nextScevValue, SE);
auto firstNextIterationValue = expander->expandCodeFor(firstNextIterationScev,
value->getType(),
insertionPoint);
auto secondNextIterationScev = secondAffine->evaluateAtIteration(nextScevValue, SE);
auto secondNextIterationValue = expander->expandCodeFor(secondNextIterationScev,
value->getType(), insertionPoint);
Value *v = builder.CreateICmp(instruction->getPredicate(), firstIterationValue, secondIterationValue);
Value *nextPoint = builder.CreateICmp(instruction->getPredicate(), firstNextIterationValue, secondNextIterationValue);
auto loopBranch = cast<BranchInst>(loop->getExitingBlock()->getTerminator());
assert(loopBranch->isConditional() && "Expected to be conditional!");
CmpInst *exitCmp = cast<CmpInst>(loopBranch->getCondition());
auto loopScev = cast<SCEVAddRecExpr>(SE.getSCEV(exitCmp->getOperand(0)));
auto loopEndValue = expander->expandCodeFor(loopScev->evaluateAtIteration(scevValue, SE), value->getType(), insertionPoint);
auto valueAfterLoop = expander->expandCodeFor(loopScev->evaluateAtIteration(nextScevValue, SE), value->getType(), insertionPoint);
auto splitInfo = SplitInfo(value, loopEndValue, v, valueAfterLoop, nextPoint, instruction, 0);
splitInfoVector.push_back(splitInfo);
} else {
auto firstLoop = firstAffine->getLoop();
auto secondLoop = secondAffine->getLoop();
if (firstLoop->contains(secondLoop)) {
SplitInfo *splitInfo =splitInfo = splitInforForAddRecAndConstant(SE, LI, instruction, secondAffine, instruction->getOperand(0), true);
splitInfoVector.push_back(*splitInfo);
} else if (secondLoop->contains(firstLoop)) {
SplitInfo *splitInfo= splitInforForAddRecAndConstant(SE, LI, instruction, firstAffine, instruction->getOperand(1), false);
splitInfoVector.push_back(*splitInfo);
}
}
}
}
return splitInfoVector;
}
Loop *cloneLoop(Function *F, Loop *L, LoopInfo *LI, DominatorTree &DT, const Twine &NameSuffix,
ValueToValueMapTy &VMap) {
// original preheader of the loop
const auto PreHeader = L->getLoopPreheader();
// keep track of the original predecessors
std::set<BasicBlock *> AllPredecessors;
for (auto PredIt = pred_begin(PreHeader), E = pred_end(PreHeader);
PredIt != E; PredIt++)
AllPredecessors.insert(*PredIt);
BasicBlock *ExitBlock = L->getExitBlock();
SmallVector<BasicBlock *, 8> Blocks;
const auto ClonedLoop = cloneLoopWithPreheader(PreHeader, PreHeader, L, VMap, NameSuffix, LI, &DT, Blocks);
VMap[ExitBlock] = PreHeader; // chain: cloned loop -> original loop
remapInstructionsInBlocks(Blocks, VMap);
// remap original predecessors to the cloned loop
for (BasicBlock *PredBB : AllPredecessors) {
Instruction *TI = PredBB->getTerminator();
for (unsigned i = 0; i < TI->getNumOperands(); i++) {
if (TI->getOperand(i) == PreHeader)
TI->setOperand(i, ClonedLoop->getLoopPreheader());
}
}
return ClonedLoop;
}
void splitLoop(ScalarEvolution &SE, Function *F, DominatorTree &DT, Loop *loop, LoopInfo *LI, SplitInfo *info) {
const CmpInst::Predicate predicate = info->comparison->getPredicate();
assert((predicate == CmpInst::ICMP_UGE || predicate == CmpInst::ICMP_ULT ||
predicate == CmpInst::ICMP_ULE || predicate == CmpInst::ICMP_SGT ||
predicate == CmpInst::ICMP_SGE || predicate == CmpInst::ICMP_SLT ||
predicate == CmpInst::ICMP_SLE || predicate == CmpInst::ICMP_UGT) &&
"We only treat integer inequalities for now");
// só funciona para < e >
info->comparison->setOperand(0, ConstantInt::getFalse(F->getContext()));
info->comparison->setOperand(1, info->isTrueAtNextPoint);
auto comparisonPredicate = info->comparison->getPredicate();
info->comparison->setPredicate(ICmpInst::ICMP_EQ);
ValueToValueMapTy VMap;
auto clonedLoop = cloneLoop(F, loop, LI, DT, "FirstLoop", VMap);
info->comparison->setOperand(0, ConstantInt::getTrue(F->getContext()));
BranchInst *ClonedBr = cast<BranchInst>(clonedLoop->getExitingBlock()->getTerminator());
assert(ClonedBr->isConditional() && "Expected to be conditional!");
for (PHINode &PHI : loop->getHeader()->phis()) {
PHINode *ClonedPHI = dyn_cast<PHINode>(VMap[&PHI]);
Value *LastValue = ClonedPHI;
if (clonedLoop->getExitingBlock() == clonedLoop->getLoopLatch()) {
LastValue =
ClonedPHI->getIncomingValueForBlock(clonedLoop->getLoopLatch());
} else
assert(clonedLoop->getExitingBlock() == clonedLoop->getHeader() &&
"Expected exiting block to be the loop header!");
PHI.setIncomingValue(PHI.getBasicBlockIndex(loop->getLoopPreheader()),
LastValue);
}
auto builder = IRBuilder<>(clonedLoop->getHeader()->getFirstNonPHI());
if (CmpInst *ExitCmp = dyn_cast<CmpInst>(ClonedBr->getCondition())) {
Value *iterationValue;
Value *nextIterationValue;
if (VMap[info->iterationValue]) {
iterationValue = VMap[info->iterationValue];
} else {
iterationValue = info->iterationValue;
}
if (VMap[info->nextValue]) {
nextIterationValue = VMap[info->nextValue];
} else {
nextIterationValue = info->nextValue;
}
auto first = info->order == 0 ? iterationValue : nextIterationValue;
auto second = info->order == 0 ? nextIterationValue : iterationValue;
Value *isTrueAtPoint = VMap[info->isTrueAtPoint];
if (isTrueAtPoint == nullptr) {
isTrueAtPoint = info->isTrueAtPoint;
}
if (comparisonPredicate == CmpInst::ICMP_SGT || comparisonPredicate == CmpInst::ICMP_SGE) {
auto sel = builder.CreateSelect(isTrueAtPoint, second, first);
ExitCmp->setOperand(1, sel);
} else {
auto sel = builder.CreateSelect(isTrueAtPoint, first, second);
ExitCmp->setOperand(1, sel);
}
}
}
bool runOnFunction(Function &F) override {
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
this->expander = new SCEVExpander(SE, DataLayout(F.getParent()), "name");
while (true) {
auto allComparisons = collectCandidateInstructions(LI);
auto splitInfo = comparisonToSplitInfo(SE, LI, allComparisons);
if (splitInfo.size() == 0) break;
for (auto splitInfo : splitInfo) {
auto loop = getLoop(splitInfo.comparison, LI, SE);
splitLoop(SE, &F, DT, loop, &LI, &splitInfo);
}
}
return true;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
getLoopAnalysisUsage(AU);
}
};
}
char LoopSplitting::ID = 0;
static RegisterPass<LoopSplitting>
Y("loopsplit", "Hello World Pass (with getAnalysisUsage implemented)");