forked from chapel-lang/llvm-pgas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llvmAggregateGlobalOps.cpp
809 lines (689 loc) · 26.9 KB
/
llvmAggregateGlobalOps.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
* Copyright 2004-2015 Cray Inc.
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Merges sequences of loads or sequences of stores
// on adress space(globalSpace) into memcpy operations so
// that we can do fewer puts or gets. For example
// %i1 = getelementptr ... %p, ..., 1
// %i2 = getelementptr ... %p, ..., 2
// %v1 = load %i1
// %v2 = load %i2
//
// will be replaced by
// %tmp = alloca
// memcpy(%tmp, %p, ...)
// %i1 = getelementptr ... %tmp, ..., 1
// %i2 = getelementptr ... %tmp ..., 2
// %v1 = load %i1
// %v2 = load %i2
//
// This optimization doesn't worry about combining such loads
// or stores into memcpys or memsets since MemCpyOptimizer
// should do that. It's just small cases where there are
// a few elements - MemCpyOptimizer might decide it's better
// to load/store to inline the memcpy for example, or the
// code generator might have started with loads and stores.
#include "llvmAggregateGlobalOps.h"
#ifdef HAVE_LLVM
#include "llvmUtil.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#if HAVE_LLVM_VER >= 35
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/CallSite.h"
#else
#include "llvm/Support/InstIterator.h"
#include "llvm/Support/CallSite.h"
#endif
#if HAVE_LLVM_VER >= 35
#include "llvm/IR/Verifier.h"
#else
#include "llvm/Analysis/Verifier.h"
#endif
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#if HAVE_LLVM_VER >= 37
#include "llvm/IR/GetElementPtrTypeIterator.h"
#endif
#include <cstdio>
#include <list>
using namespace llvm;
namespace {
static const bool DEBUG = false;
static const bool extraChecks = false;
// Set a function name here to get lots of debugging output.
static const char* debugThisFn = "";
// If there is a gap between memory that we are loading,
// for example due to padding, or just because we didn't
// need some data, and the gap is < this amount, we
// will do one get and then just fish out the parts we
// used.
#define GET_EXTRA 64
static inline
bool isGlobalLoadOrStore(Instruction* I,
unsigned globalSpace,
bool findLoad, bool findStore)
{
if( findLoad && isa<LoadInst>(I) ) {
LoadInst *load = cast<LoadInst>(I);
if( load->getPointerAddressSpace() == globalSpace ) {
return true;
}
}
if( findStore && isa<StoreInst>(I)) {
StoreInst *store = cast<StoreInst>(I);
if( store->getPointerAddressSpace() == globalSpace ) {
return true;
}
}
return false;
}
static inline
Value* getLoadStorePointer(Instruction* I)
{
if( isa<LoadInst>(I) ) {
LoadInst *load = cast<LoadInst>(I);
return load->getPointerOperand();
}
if( isa<StoreInst>(I)) {
StoreInst *store = cast<StoreInst>(I);
return store->getPointerOperand();
}
return NULL;
}
static
Value* rebasePointer(Value* ptr, Value* oldBase, Value* newBase, const Twine &name,
IRBuilder<>* builder, const DataLayout &TD,
Value* oldBaseI, Value* newBaseI)
{
Type* iPtrTy = TD.getIntPtrType(ptr->getType());
Type* localPtrTy = ptr->getType()->getPointerElementType()->getPointerTo(0);
Value* ret;
if( ptr != oldBase ) {
// compute newBase + (ptr - oldBase)
Value* pI = builder->CreatePtrToInt(ptr, iPtrTy, name + ".ptr.i");
assert( oldBaseI );
assert( newBaseI );
// then subtract
Value* diff = builder->CreateSub(pI, oldBaseI, name + ".diff");
// then make sure same type
Value* ext = builder->CreateSExtOrTrunc(diff, newBaseI->getType(), ".ext.i");
// Now add
Value* sum = builder->CreateAdd(newBaseI, ext, name + ".sum");
ret = builder->CreateIntToPtr(sum, localPtrTy, name + ".cast");
} else {
ret = builder->CreatePointerCast(newBase, localPtrTy, name + ".cast");
}
return ret;
}
// Given a start and end load/store instruction (in the same basic block),
// reorder the instructions so that the addressing instructions are
// first, the load/store instructions are next, and then the
// uses of loaded values are last. This reordering is valid when
// the other instructions do not read or write memory.
// Returns the last instruction in the reordering.
static
Instruction* reorderAddressingMemopsUses(Instruction *FirstLoadOrStore,
Instruction *LastLoadOrStore,
bool DebugThis)
{
SmallPtrSet<Instruction*, 8> memopsUses;
Instruction *LastMemopUse = NULL;
for (BasicBlock::iterator BI = FirstLoadOrStore; !isa<TerminatorInst>(BI); ++BI) {
Instruction* insn = BI;
bool isUseOfMemop = false;
if( isa<StoreInst>(insn) || isa<LoadInst>(insn) ) {
memopsUses.insert(insn);
continue;
}
// Check -- are any operands to this instruction memopsUses?
for (User::op_iterator i = insn->op_begin(), e = insn->op_end(); i != e; ++i) {
Value *v = *i;
if(Instruction *uses_insn = dyn_cast<Instruction>(v)) {
if( memopsUses.count(uses_insn) ){
isUseOfMemop = true;
break;
}
}
}
if( isUseOfMemop ) memopsUses.insert(insn);
if( insn == LastLoadOrStore ) break;
}
LastMemopUse = LastLoadOrStore;
// Reorder the instructions here.
// Move all addressing instructions before StartInst.
// Move all uses of loaded values before LastLoadOrStore (which will be removed).
for (BasicBlock::iterator BI = FirstLoadOrStore; !isa<TerminatorInst>(BI);) {
Instruction* insn = BI++; // don't invalidate iterator.
// Leave loads/stores where they are (they will be removed)
if( isa<StoreInst>(insn) || isa<LoadInst>(insn) ) {
if( DebugThis ) {
errs() << "found load/store: "; insn->dump();
}
} else if( memopsUses.count(insn) ) {
if( DebugThis ) {
errs() << "found memop use: "; insn->dump();
}
// Move uses of memops to after the final memop.
insn->removeFromParent();
insn->insertAfter(LastMemopUse);
LastMemopUse = insn;
} else {
if( DebugThis ) {
errs() << "found other: "; insn->dump();
}
// Move addressing instructions to before the first memop.
insn->removeFromParent();
insn->insertBefore(FirstLoadOrStore);
}
if( insn == LastLoadOrStore ) break;
}
return LastMemopUse;
}
// The next several fns are stolen almost totally unmodified from MemCpyOptimizer.
// modified code areas say CUSTOM.
static int64_t GetOffsetFromIndex(const GEPOperator *GEP, unsigned Idx,
bool &VariableIdxFound, const DataLayout &TD){
// Skip over the first indices.
gep_type_iterator GTI = gep_type_begin(GEP);
for (unsigned i = 1; i != Idx; ++i, ++GTI)
/*skip along*/;
// Compute the offset implied by the rest of the indices.
int64_t Offset = 0;
for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
if (OpC == 0)
return VariableIdxFound = true;
if (OpC->isZero()) continue; // No offset.
// Handle struct indices, which add their field offset to the pointer.
if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Offset += TD.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
continue;
}
// Otherwise, we have a sequential type like an array or vector. Multiply
// the index by the ElementSize.
uint64_t Size = TD.getTypeAllocSize(GTI.getIndexedType());
Offset += Size*OpC->getSExtValue();
}
return Offset;
}
/// IsPointerOffset - Return true if Ptr1 is provably equal to Ptr2 plus a
/// constant offset, and return that constant offset. For example, Ptr1 might
/// be &A[42], and Ptr2 might be &A[40]. In this case offset would be -8.
static bool IsPointerOffset(Value *Ptr1, Value *Ptr2, int64_t &Offset,
const DataLayout &TD) {
Ptr1 = Ptr1->stripPointerCasts();
Ptr2 = Ptr2->stripPointerCasts();
GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
bool VariableIdxFound = false;
// If one pointer is a GEP and the other isn't, then see if the GEP is a
// constant offset from the base, as in "P" and "gep P, 1".
if (GEP1 && GEP2 == 0 && GEP1->getOperand(0)->stripPointerCasts() == Ptr2) {
Offset = -GetOffsetFromIndex(GEP1, 1, VariableIdxFound, TD);
return !VariableIdxFound;
}
if (GEP2 && GEP1 == 0 && GEP2->getOperand(0)->stripPointerCasts() == Ptr1) {
Offset = GetOffsetFromIndex(GEP2, 1, VariableIdxFound, TD);
return !VariableIdxFound;
}
// Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
// base. After that base, they may have some number of common (and
// potentially variable) indices. After that they handle some constant
// offset, which determines their offset from each other. At this point, we
// handle no other case.
if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
return false;
// Skip any common indices and track the GEP types.
unsigned Idx = 1;
for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
break;
int64_t Offset1 = GetOffsetFromIndex(GEP1, Idx, VariableIdxFound, TD);
int64_t Offset2 = GetOffsetFromIndex(GEP2, Idx, VariableIdxFound, TD);
if (VariableIdxFound) return false;
Offset = Offset2-Offset1;
return true;
}
struct MemOpRange { // from MemsetRange in MemCpyOptimizer
// Start/End - A semi range that describes the span that this range covers.
// The range is closed at the start and open at the end: [Start, End).
int64_t Start, End;
// CUSTOM: End including slack space, to allow for gaps
int64_t SlackEnd;
/// StartPtr - The getelementptr instruction that points to the start of the
/// range.
Value *StartPtr;
/// Alignment - The known alignment of the first store.
unsigned Alignment;
// The load or store instructions. Called TheStores because
// we stole this code from MemCpyOptimizer, but it might also store load instructions.
SmallVector<Instruction*, 16> TheStores;
};
struct MemOpRanges { // from MemsetRanges in MemCpyOptimizer
/// Ranges - A sorted list of the memset ranges. We use std::list here
/// because each element is relatively large and expensive to copy.
std::list<MemOpRange> Ranges;
typedef std::list<MemOpRange>::iterator range_iterator;
const DataLayout &TD;
MemOpRanges(const DataLayout &td) : TD(td) { }
typedef std::list<MemOpRange>::const_iterator const_iterator;
const_iterator begin() const { return Ranges.begin(); }
const_iterator end() const { return Ranges.end(); }
bool empty() const { return Ranges.empty(); }
bool moreThanOneOp() const {
if( Ranges.size() > 1 ) return true;
MemOpRanges::const_iterator I = begin();
MemOpRanges::const_iterator E = end();
if( I != E ) {
const MemOpRange &Range = *I;
if( Range.TheStores.size() > 1 ) return true;
}
return false;
}
void addInst(int64_t offsetFromFirst, Instruction *Inst) {
if( StoreInst *SI = dyn_cast<StoreInst>(Inst) ) {
addStore(offsetFromFirst, SI);
}
if( LoadInst *LI = dyn_cast<LoadInst>(Inst) ) {
addLoad(offsetFromFirst, LI);
}
}
void addStore(int64_t OffsetFromFirst, StoreInst *SI) {
int64_t StoreSize = TD.getTypeStoreSize(SI->getOperand(0)->getType());
int64_t Slack = 0; // TODO - compute slack based on structure padding.
// Make slack include padding if it is after this
// element in a structure.
addRange(OffsetFromFirst, StoreSize, Slack,
SI->getPointerOperand(), SI->getAlignment(), SI);
}
// CUSTOM because MemsetRanges doesn't work with LoadInsts.
void addLoad(int64_t OffsetFromFirst, LoadInst *LI) {
Type* ptrType = LI->getOperand(0)->getType();
int64_t LoadSize = TD.getTypeStoreSize(ptrType->getPointerElementType());
int64_t Slack = GET_EXTRA; // Pretend loads use more space...
addRange(OffsetFromFirst, LoadSize, Slack,
LI->getPointerOperand(), LI->getAlignment(), LI);
}
// CUSTOM adds Slack
void addRange(int64_t Start, int64_t Size, int64_t Slack, Value *Ptr,
unsigned Alignment, Instruction *Inst);
};
/// addRange - Add a new store to the MemOpRanges data structure. This adds a
/// new range for the specified store at the specified offset, merging into
/// existing ranges as appropriate.
///
/// Do a linear search of the ranges to see if this can be joined and/or to
/// find the insertion point in the list. We keep the ranges sorted for
/// simplicity here. This is a linear search of a linked list, which is ugly,
/// however the number of ranges is limited, so this won't get crazy slow.
// CUSTOM -- uses SlackEnd instead of End
void MemOpRanges::addRange(int64_t Start, int64_t Size, int64_t Slack, Value *Ptr,
unsigned Alignment, Instruction *Inst) {
int64_t End = Start+Size;
int64_t SlackEnd = Start+Size+Slack;
range_iterator I = Ranges.begin(), E = Ranges.end();
while (I != E && Start > I->SlackEnd)
++I;
// We now know that I == E, in which case we didn't find anything to merge
// with, or that Start <= I->End. If End < I->Start or I == E, then we need
// to insert a new range. Handle this now.
if (I == E || SlackEnd < I->Start) {
MemOpRange &R = *Ranges.insert(I, MemOpRange());
R.Start = Start;
R.End = End;
R.SlackEnd = SlackEnd;
R.StartPtr = Ptr;
R.Alignment = Alignment;
R.TheStores.push_back(Inst);
return;
}
// This store overlaps with I, add it.
I->TheStores.push_back(Inst);
// Update End too.
if (End > I->End) I->End = End;
// At this point, we may have an interval that completely contains our store.
// If so, just add it to the interval and return.
if (I->Start <= Start && I->SlackEnd >= SlackEnd)
return;
// Now we know that Start <= I->End and End >= I->Start so the range overlaps
// but is not entirely contained within the range.
// See if the range extends the start of the range. In this case, it couldn't
// possibly cause it to join the prior range, because otherwise we would have
// stopped on *it*.
if (Start < I->Start) {
I->Start = Start;
I->StartPtr = Ptr;
I->Alignment = Alignment;
}
// Now we know that Start <= I->End and Start >= I->Start (so the startpoint
// is in or right at the end of I), and that End >= I->Start. Extend I out to
// End.
if (SlackEnd > I->SlackEnd) {
I->SlackEnd = SlackEnd;
range_iterator NextI = I;
while (++NextI != E && SlackEnd >= NextI->Start) {
// Merge the range in.
I->TheStores.append(NextI->TheStores.begin(), NextI->TheStores.end());
if (NextI->SlackEnd > I->SlackEnd)
I->SlackEnd = NextI->SlackEnd;
if (NextI->End > I->End)
I->End = NextI->End;
Ranges.erase(NextI);
NextI = I;
}
}
}
// END stolen from MemCpyOptimizer.
struct AggregateGlobalOpsOpt : public FunctionPass {
const DataLayout *TD;
unsigned globalSpace;
public:
static char ID; // Pass identification, replacement for typeid
AggregateGlobalOpsOpt() : FunctionPass(ID) {
TD = 0;
errs() << "Warning: aggregate-global-opts using default configuration\n";
globalSpace = 100;
}
AggregateGlobalOpsOpt(unsigned _globalSpace) : FunctionPass(ID) {
TD = 0;
globalSpace = _globalSpace;
}
bool runOnFunction(Function &F);
private:
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
/*AU.addRequired<DominatorTree>();
AU.addRequired<MemoryDependenceAnalysis>();
AU.addRequired<AliasAnalysis>();
AU.addRequired<TargetLibraryInfo>();*/
AU.addPreserved<AliasAnalysis>();
AU.addPreserved<MemoryDependenceAnalysis>();
}
Instruction *tryAggregating(Instruction *I, Value *StartPtr, bool DebugThis);
};
} // end anon namespace.
char AggregateGlobalOpsOpt::ID = 0;
static RegisterPass<AggregateGlobalOpsOpt> X("aggregate-global-ops", "Aggregate Global Pointer Operations", false /* only looks at CFG */, false /* Analysis pass */ );
// createAggregateGlobalOpsOptPass - The public interface to this file...
FunctionPass *createAggregateGlobalOpsOptPass(unsigned globalSpace)
{
return new AggregateGlobalOpsOpt(globalSpace);
}
/// tryAggregating - When scanning forward over instructions, we look for
/// other loads or stores that could be aggregated with this one.
/// Returns the last instruction added (if one was added) since we might have
/// removed some loads or stores and that might invalidate an iterator.
Instruction *AggregateGlobalOpsOpt::tryAggregating(Instruction *StartInst, Value *StartPtr,
bool DebugThis) {
if (TD == 0) return 0;
Module* M = StartInst->getParent()->getParent()->getParent();
LLVMContext& Context = StartInst->getContext();
Type* int8Ty = Type::getInt8Ty(Context);
Type* sizeTy = Type::getInt64Ty(Context);
Type* globalInt8PtrTy = int8Ty->getPointerTo(globalSpace);
bool isLoad = isa<LoadInst>(StartInst);
bool isStore = isa<StoreInst>(StartInst);
Instruction *lastAddedInsn = NULL;
Instruction *LastLoadOrStore = NULL;
SmallVector<Instruction*, 8> toRemove;
// Okay, so we now have a single global load/store. Scan to find
// all subsequent stores of the same value to offset from the same pointer.
// Join these together into ranges, so we can decide whether contiguous blocks
// are stored.
MemOpRanges Ranges(*TD);
// Put the first store in since we want to preserve the order.
Ranges.addInst(0, StartInst);
BasicBlock::iterator BI = StartInst;
for (++BI; !isa<TerminatorInst>(BI); ++BI) {
if( isGlobalLoadOrStore(BI, globalSpace, isLoad, isStore) ) {
// OK!
} else {
// If the instruction is readnone, ignore it, otherwise bail out. We
// don't even allow readonly here because we don't want something like:
// A[1] = 2; strlen(A); A[2] = 2; -> memcpy(A, ...); strlen(A).
if (BI->mayWriteToMemory())
break;
if (isStore && BI->mayReadFromMemory())
break;
continue;
}
if ( isStore && isa<StoreInst>(BI) ) {
StoreInst *NextStore = cast<StoreInst>(BI);
// If this is a store, see if we can merge it in.
if (!NextStore->isSimple()) break;
// Check to see if this store is to a constant offset from the start ptr.
int64_t Offset;
if (!IsPointerOffset(StartPtr, NextStore->getPointerOperand(), Offset, *TD))
break;
Ranges.addStore(Offset, NextStore);
LastLoadOrStore = NextStore;
} else {
LoadInst *NextLoad = cast<LoadInst>(BI);
if (!NextLoad->isSimple()) break;
// Check to see if this load is to a constant offset from the start ptr.
int64_t Offset;
if (!IsPointerOffset(StartPtr, NextLoad->getPointerOperand(), Offset, *TD))
break;
Ranges.addLoad(Offset, NextLoad);
LastLoadOrStore = NextLoad;
}
}
// If we have no ranges, then we just had a single store with nothing that
// could be merged in. This is a very common case of course.
if (!Ranges.moreThanOneOp())
return 0;
// Divide the instructions between StartInst and LastLoadOrStore into
// addressing, memops, and uses of memops (uses of loads)
reorderAddressingMemopsUses(StartInst, LastLoadOrStore, DebugThis);
Instruction* insertBefore = StartInst;
IRBuilder<> builder(insertBefore);
// Now that we have full information about ranges, loop over the ranges and
// emit memcpy's for anything big enough to be worthwhile.
for (MemOpRanges::const_iterator I = Ranges.begin(), E = Ranges.end();
I != E; ++I) {
const MemOpRange &Range = *I;
Value* oldBaseI = NULL;
Value* newBaseI = NULL;
if (Range.TheStores.size() == 1) continue; // Don't bother if there's only one thing...
builder.SetInsertPoint(insertBefore);
// Otherwise, we do want to transform this! Create a new memcpy.
// Get the starting pointer of the block.
StartPtr = Range.StartPtr;
if( DebugThis ) {
errs() << "base is:";
StartPtr->dump();
}
// Determine alignment
unsigned Alignment = Range.Alignment;
if (Alignment == 0) {
Type *EltType =
cast<PointerType>(StartPtr->getType())->getElementType();
Alignment = TD->getABITypeAlignment(EltType);
}
Instruction *alloc = NULL;
Value *globalPtr = NULL;
// create temporary alloca space to communicate to/from.
alloc = makeAlloca(int8Ty, "agg.tmp", insertBefore,
Range.End-Range.Start, Alignment);
// Generate the old and new base pointers before we output
// anything else.
{
Type* iPtrTy = TD->getIntPtrType(alloc->getType());
Type* iNewBaseTy = TD->getIntPtrType(alloc->getType());
oldBaseI = builder.CreatePtrToInt(StartPtr, iPtrTy, "agg.tmp.oldb.i");
newBaseI = builder.CreatePtrToInt(alloc, iNewBaseTy, "agg.tmp.newb.i");
}
// If storing, do the stores we had into our alloca'd region.
if( isStore ) {
for (SmallVector<Instruction*, 16>::const_iterator
SI = Range.TheStores.begin(),
SE = Range.TheStores.end(); SI != SE; ++SI) {
StoreInst* oldStore = cast<StoreInst>(*SI);
if( DebugThis ) {
errs() << "have store in range:";
oldStore->dump();
}
Value* ptrToAlloc = rebasePointer(oldStore->getPointerOperand(),
StartPtr, alloc, "agg.tmp",
&builder, *TD, oldBaseI, newBaseI);
// Old load must not be volatile or atomic... or we shouldn't have put
// it in ranges
assert(!(oldStore->isVolatile() || oldStore->isAtomic()));
StoreInst* newStore =
builder.CreateStore(oldStore->getValueOperand(), ptrToAlloc);
newStore->setAlignment(oldStore->getAlignment());
newStore->takeName(oldStore);
}
}
// cast the pointer that was load/stored to i8 if necessary.
if( StartPtr->getType()->getPointerElementType() == int8Ty ) {
globalPtr = StartPtr;
} else {
globalPtr = builder.CreatePointerCast(StartPtr, globalInt8PtrTy, "agg.cast");
}
// Get a Constant* for the length.
Constant* len = ConstantInt::get(sizeTy, Range.End-Range.Start, false);
// Now add the memcpy instruction
unsigned addrSpaceDst,addrSpaceSrc;
addrSpaceDst = addrSpaceSrc = 0;
if( isStore ) addrSpaceDst = globalSpace;
if( isLoad ) addrSpaceSrc = globalSpace;
Type *types[3];
types[0] = PointerType::get(int8Ty, addrSpaceDst);
types[1] = PointerType::get(int8Ty, addrSpaceSrc);
types[2] = sizeTy;
Function *func = Intrinsic::getDeclaration(M, Intrinsic::memcpy, types);
Value* args[5]; // dst src len alignment isvolatile
if( isStore ) {
// it's a store (ie put)
args[0] = globalPtr;
args[1] = alloc;
} else {
// it's a load (ie get)
args[0] = alloc;
args[1] = globalPtr;
}
args[2] = len;
// alignment
args[3] = ConstantInt::get(Type::getInt32Ty(Context), 0, false);
// isvolatile
args[4] = ConstantInt::get(Type::getInt1Ty(Context), 0, false);
Instruction* aMemCpy = builder.CreateCall(func, args);
/*
DEBUG(dbgs() << "Replace ops:\n";
for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i)
dbgs() << *Range.TheStores[i] << '\n';
dbgs() << "With: " << *AMemSet << '\n');
*/
if (!Range.TheStores.empty())
aMemCpy->setDebugLoc(Range.TheStores[0]->getDebugLoc());
lastAddedInsn = aMemCpy;
// If loading, load from the memcpy'd region
if( isLoad ) {
for (SmallVector<Instruction*, 16>::const_iterator
SI = Range.TheStores.begin(),
SE = Range.TheStores.end(); SI != SE; ++SI) {
LoadInst* oldLoad = cast<LoadInst>(*SI);
if( DebugThis ) {
errs() << "have load in range:";
oldLoad->dump();
}
Value* ptrToAlloc = rebasePointer(oldLoad->getPointerOperand(),
StartPtr, alloc, "agg.tmp",
&builder, *TD, oldBaseI, newBaseI);
// Old load must not be volatile or atomic... or we shouldn't have put
// it in ranges
assert(!(oldLoad->isVolatile() || oldLoad->isAtomic()));
LoadInst* newLoad = builder.CreateLoad(ptrToAlloc);
newLoad->setAlignment(oldLoad->getAlignment());
oldLoad->replaceAllUsesWith(newLoad);
newLoad->takeName(oldLoad);
lastAddedInsn = newLoad;
}
}
// Save old loads/stores for removal
for (SmallVector<Instruction*, 16>::const_iterator
SI = Range.TheStores.begin(),
SE = Range.TheStores.end(); SI != SE; ++SI) {
Instruction* insn = *SI;
toRemove.push_back(insn);
}
}
// Zap all the old loads/stores
for (SmallVector<Instruction*, 16>::const_iterator
SI = toRemove.begin(),
SE = toRemove.end(); SI != SE; ++SI) {
(*SI)->eraseFromParent();
}
return lastAddedInsn;
}
// MemCpyOpt::runOnFunction - This is the main transformation entry point for a
// function.
//
bool AggregateGlobalOpsOpt::runOnFunction(Function &F) {
bool MadeChange = false;
bool DebugThis = DEBUG;
if( debugThisFn[0] && F.getName() == debugThisFn ) {
DebugThis = true;
}
//MD = &getAnalysis<MemoryDependenceAnalysis>();
#if HAVE_LLVM_VER >= 37
TD = & F.getParent()->getDataLayout();
#elif HAVE_LLVM_VER >= 35
TD = & getAnalysisIfAvailable<DataLayoutPass>()->getDataLayout();
#else
TD = getAnalysisIfAvailable<DataLayout>();
#endif
//TLI = &getAnalysis<TargetLibraryInfo>();
// Walk all instruction in the function.
for (Function::iterator BB = F.begin(), BBE = F.end(); BB != BBE; ++BB) {
if( DebugThis ) {
errs() << "Working on BB ";
BB->dump();
}
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
// Avoid invalidating the iterator.
Instruction *I = BI++;
if( isGlobalLoadOrStore(I, globalSpace, true, true) ) {
Instruction* lastAdded = tryAggregating(I, getLoadStorePointer(I), DebugThis);
if( lastAdded ) {
MadeChange = true;
BI = lastAdded;
}
}
}
if( DebugThis && MadeChange ) {
errs() << "After transform BB is ";
BB->dump();
}
}
if( extraChecks ) {
#if HAVE_LLVM_VER >= 35
assert(!verifyFunction(F, &errs()));
#else
verifyFunction(F);
#endif
}
//MD = 0;
return MadeChange;
}
#endif