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
| //===- GVNExpression.h - GVN Expression classes -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
///
/// The header file for the GVN pass that contains expression handling
/// classes
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
#define LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/ArrayRecycler.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <utility>
namespace llvm {
class BasicBlock;
class Type;
namespace GVNExpression {
enum ExpressionType {
ET_Base,
ET_Constant,
ET_Variable,
ET_Dead,
ET_Unknown,
ET_BasicStart,
ET_Basic,
ET_AggregateValue,
ET_Phi,
ET_MemoryStart,
ET_Call,
ET_Load,
ET_Store,
ET_MemoryEnd,
ET_BasicEnd
};
class Expression {
private:
ExpressionType EType;
unsigned Opcode;
mutable hash_code HashVal = 0;
public:
Expression(ExpressionType ET = ET_Base, unsigned O = ~2U)
: EType(ET), Opcode(O) {}
Expression(const Expression &) = delete;
Expression &operator=(const Expression &) = delete;
virtual ~Expression();
static unsigned getEmptyKey() { return ~0U; }
static unsigned getTombstoneKey() { return ~1U; }
bool operator!=(const Expression &Other) const { return !(*this == Other); }
bool operator==(const Expression &Other) const {
if (getOpcode() != Other.getOpcode())
return false;
if (getOpcode() == getEmptyKey() || getOpcode() == getTombstoneKey())
return true;
// Compare the expression type for anything but load and store.
// For load and store we set the opcode to zero to make them equal.
if (getExpressionType() != ET_Load && getExpressionType() != ET_Store &&
getExpressionType() != Other.getExpressionType())
return false;
return equals(Other);
}
hash_code getComputedHash() const {
// It's theoretically possible for a thing to hash to zero. In that case,
// we will just compute the hash a few extra times, which is no worse that
// we did before, which was to compute it always.
if (static_cast<unsigned>(HashVal) == 0)
HashVal = getHashValue();
return HashVal;
}
virtual bool equals(const Expression &Other) const { return true; }
// Return true if the two expressions are exactly the same, including the
// normally ignored fields.
virtual bool exactlyEquals(const Expression &Other) const {
return getExpressionType() == Other.getExpressionType() && equals(Other);
}
unsigned getOpcode() const { return Opcode; }
void setOpcode(unsigned opcode) { Opcode = opcode; }
ExpressionType getExpressionType() const { return EType; }
// We deliberately leave the expression type out of the hash value.
virtual hash_code getHashValue() const { return getOpcode(); }
// Debugging support
virtual void printInternal(raw_ostream &OS, bool PrintEType) const {
if (PrintEType)
OS << "etype = " << getExpressionType() << ",";
OS << "opcode = " << getOpcode() << ", ";
}
void print(raw_ostream &OS) const {
OS << "{ ";
printInternal(OS, true);
OS << "}";
}
LLVM_DUMP_METHOD void dump() const;
};
inline raw_ostream &operator<<(raw_ostream &OS, const Expression &E) {
E.print(OS);
return OS;
}
class BasicExpression : public Expression {
private:
using RecyclerType = ArrayRecycler<Value *>;
using RecyclerCapacity = RecyclerType::Capacity;
Value **Operands = nullptr;
unsigned MaxOperands;
unsigned NumOperands = 0;
Type *ValueType = nullptr;
public:
BasicExpression(unsigned NumOperands)
: BasicExpression(NumOperands, ET_Basic) {}
BasicExpression(unsigned NumOperands, ExpressionType ET)
: Expression(ET), MaxOperands(NumOperands) {}
BasicExpression() = delete;
BasicExpression(const BasicExpression &) = delete;
BasicExpression &operator=(const BasicExpression &) = delete;
~BasicExpression() override;
static bool classof(const Expression *EB) {
ExpressionType ET = EB->getExpressionType();
return ET > ET_BasicStart && ET < ET_BasicEnd;
}
/// Swap two operands. Used during GVN to put commutative operands in
/// order.
void swapOperands(unsigned First, unsigned Second) {
std::swap(Operands[First], Operands[Second]);
}
Value *getOperand(unsigned N) const {
assert(Operands && "Operands not allocated");
assert(N < NumOperands && "Operand out of range");
return Operands[N];
}
void setOperand(unsigned N, Value *V) {
assert(Operands && "Operands not allocated before setting");
assert(N < NumOperands && "Operand out of range");
Operands[N] = V;
}
unsigned getNumOperands() const { return NumOperands; }
using op_iterator = Value **;
using const_op_iterator = Value *const *;
op_iterator op_begin() { return Operands; }
op_iterator op_end() { return Operands + NumOperands; }
const_op_iterator op_begin() const { return Operands; }
const_op_iterator op_end() const { return Operands + NumOperands; }
iterator_range<op_iterator> operands() {
return iterator_range<op_iterator>(op_begin(), op_end());
}
iterator_range<const_op_iterator> operands() const {
return iterator_range<const_op_iterator>(op_begin(), op_end());
}
void op_push_back(Value *Arg) {
assert(NumOperands < MaxOperands && "Tried to add too many operands");
assert(Operands && "Operandss not allocated before pushing");
Operands[NumOperands++] = Arg;
}
bool op_empty() const { return getNumOperands() == 0; }
void allocateOperands(RecyclerType &Recycler, BumpPtrAllocator &Allocator) {
assert(!Operands && "Operands already allocated");
Operands = Recycler.allocate(RecyclerCapacity::get(MaxOperands), Allocator);
}
void deallocateOperands(RecyclerType &Recycler) {
Recycler.deallocate(RecyclerCapacity::get(MaxOperands), Operands);
}
void setType(Type *T) { ValueType = T; }
Type *getType() const { return ValueType; }
bool equals(const Expression &Other) const override {
if (getOpcode() != Other.getOpcode())
return false;
const auto &OE = cast<BasicExpression>(Other);
return getType() == OE.getType() && NumOperands == OE.NumOperands &&
std::equal(op_begin(), op_end(), OE.op_begin());
}
hash_code getHashValue() const override {
return hash_combine(this->Expression::getHashValue(), ValueType,
hash_combine_range(op_begin(), op_end()));
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeBasic, ";
this->Expression::printInternal(OS, false);
OS << "operands = {";
for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
OS << "[" << i << "] = ";
Operands[i]->printAsOperand(OS);
OS << " ";
}
OS << "} ";
}
};
class op_inserter
: public std::iterator<std::output_iterator_tag, void, void, void, void> {
private:
using Container = BasicExpression;
Container *BE;
public:
explicit op_inserter(BasicExpression &E) : BE(&E) {}
explicit op_inserter(BasicExpression *E) : BE(E) {}
op_inserter &operator=(Value *val) {
BE->op_push_back(val);
return *this;
}
op_inserter &operator*() { return *this; }
op_inserter &operator++() { return *this; }
op_inserter &operator++(int) { return *this; }
};
class MemoryExpression : public BasicExpression {
private:
const MemoryAccess *MemoryLeader;
public:
MemoryExpression(unsigned NumOperands, enum ExpressionType EType,
const MemoryAccess *MemoryLeader)
: BasicExpression(NumOperands, EType), MemoryLeader(MemoryLeader) {}
MemoryExpression() = delete;
MemoryExpression(const MemoryExpression &) = delete;
MemoryExpression &operator=(const MemoryExpression &) = delete;
static bool classof(const Expression *EB) {
return EB->getExpressionType() > ET_MemoryStart &&
EB->getExpressionType() < ET_MemoryEnd;
}
hash_code getHashValue() const override {
return hash_combine(this->BasicExpression::getHashValue(), MemoryLeader);
}
bool equals(const Expression &Other) const override {
if (!this->BasicExpression::equals(Other))
return false;
const MemoryExpression &OtherMCE = cast<MemoryExpression>(Other);
return MemoryLeader == OtherMCE.MemoryLeader;
}
const MemoryAccess *getMemoryLeader() const { return MemoryLeader; }
void setMemoryLeader(const MemoryAccess *ML) { MemoryLeader = ML; }
};
class CallExpression final : public MemoryExpression {
private:
CallInst *Call;
public:
CallExpression(unsigned NumOperands, CallInst *C,
const MemoryAccess *MemoryLeader)
: MemoryExpression(NumOperands, ET_Call, MemoryLeader), Call(C) {}
CallExpression() = delete;
CallExpression(const CallExpression &) = delete;
CallExpression &operator=(const CallExpression &) = delete;
~CallExpression() override;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Call;
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeCall, ";
this->BasicExpression::printInternal(OS, false);
OS << " represents call at ";
Call->printAsOperand(OS);
}
};
class LoadExpression final : public MemoryExpression {
private:
LoadInst *Load;
MaybeAlign Alignment;
public:
LoadExpression(unsigned NumOperands, LoadInst *L,
const MemoryAccess *MemoryLeader)
: LoadExpression(ET_Load, NumOperands, L, MemoryLeader) {}
LoadExpression(enum ExpressionType EType, unsigned NumOperands, LoadInst *L,
const MemoryAccess *MemoryLeader)
: MemoryExpression(NumOperands, EType, MemoryLeader), Load(L) {
if (L)
Alignment = MaybeAlign(L->getAlignment());
}
LoadExpression() = delete;
LoadExpression(const LoadExpression &) = delete;
LoadExpression &operator=(const LoadExpression &) = delete;
~LoadExpression() override;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Load;
}
LoadInst *getLoadInst() const { return Load; }
void setLoadInst(LoadInst *L) { Load = L; }
MaybeAlign getAlignment() const { return Alignment; }
void setAlignment(MaybeAlign Align) { Alignment = Align; }
bool equals(const Expression &Other) const override;
bool exactlyEquals(const Expression &Other) const override {
return Expression::exactlyEquals(Other) &&
cast<LoadExpression>(Other).getLoadInst() == getLoadInst();
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeLoad, ";
this->BasicExpression::printInternal(OS, false);
OS << " represents Load at ";
Load->printAsOperand(OS);
OS << " with MemoryLeader " << *getMemoryLeader();
}
};
class StoreExpression final : public MemoryExpression {
private:
StoreInst *Store;
Value *StoredValue;
public:
StoreExpression(unsigned NumOperands, StoreInst *S, Value *StoredValue,
const MemoryAccess *MemoryLeader)
: MemoryExpression(NumOperands, ET_Store, MemoryLeader), Store(S),
StoredValue(StoredValue) {}
StoreExpression() = delete;
StoreExpression(const StoreExpression &) = delete;
StoreExpression &operator=(const StoreExpression &) = delete;
~StoreExpression() override;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Store;
}
StoreInst *getStoreInst() const { return Store; }
Value *getStoredValue() const { return StoredValue; }
bool equals(const Expression &Other) const override;
bool exactlyEquals(const Expression &Other) const override {
return Expression::exactlyEquals(Other) &&
cast<StoreExpression>(Other).getStoreInst() == getStoreInst();
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeStore, ";
this->BasicExpression::printInternal(OS, false);
OS << " represents Store " << *Store;
OS << " with StoredValue ";
StoredValue->printAsOperand(OS);
OS << " and MemoryLeader " << *getMemoryLeader();
}
};
class AggregateValueExpression final : public BasicExpression {
private:
unsigned MaxIntOperands;
unsigned NumIntOperands = 0;
unsigned *IntOperands = nullptr;
public:
AggregateValueExpression(unsigned NumOperands, unsigned NumIntOperands)
: BasicExpression(NumOperands, ET_AggregateValue),
MaxIntOperands(NumIntOperands) {}
AggregateValueExpression() = delete;
AggregateValueExpression(const AggregateValueExpression &) = delete;
AggregateValueExpression &
operator=(const AggregateValueExpression &) = delete;
~AggregateValueExpression() override;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_AggregateValue;
}
using int_arg_iterator = unsigned *;
using const_int_arg_iterator = const unsigned *;
int_arg_iterator int_op_begin() { return IntOperands; }
int_arg_iterator int_op_end() { return IntOperands + NumIntOperands; }
const_int_arg_iterator int_op_begin() const { return IntOperands; }
const_int_arg_iterator int_op_end() const {
return IntOperands + NumIntOperands;
}
unsigned int_op_size() const { return NumIntOperands; }
bool int_op_empty() const { return NumIntOperands == 0; }
void int_op_push_back(unsigned IntOperand) {
assert(NumIntOperands < MaxIntOperands &&
"Tried to add too many int operands");
assert(IntOperands && "Operands not allocated before pushing");
IntOperands[NumIntOperands++] = IntOperand;
}
virtual void allocateIntOperands(BumpPtrAllocator &Allocator) {
assert(!IntOperands && "Operands already allocated");
IntOperands = Allocator.Allocate<unsigned>(MaxIntOperands);
}
bool equals(const Expression &Other) const override {
if (!this->BasicExpression::equals(Other))
return false;
const AggregateValueExpression &OE = cast<AggregateValueExpression>(Other);
return NumIntOperands == OE.NumIntOperands &&
std::equal(int_op_begin(), int_op_end(), OE.int_op_begin());
}
hash_code getHashValue() const override {
return hash_combine(this->BasicExpression::getHashValue(),
hash_combine_range(int_op_begin(), int_op_end()));
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeAggregateValue, ";
this->BasicExpression::printInternal(OS, false);
OS << ", intoperands = {";
for (unsigned i = 0, e = int_op_size(); i != e; ++i) {
OS << "[" << i << "] = " << IntOperands[i] << " ";
}
OS << "}";
}
};
class int_op_inserter
: public std::iterator<std::output_iterator_tag, void, void, void, void> {
private:
using Container = AggregateValueExpression;
Container *AVE;
public:
explicit int_op_inserter(AggregateValueExpression &E) : AVE(&E) {}
explicit int_op_inserter(AggregateValueExpression *E) : AVE(E) {}
int_op_inserter &operator=(unsigned int val) {
AVE->int_op_push_back(val);
return *this;
}
int_op_inserter &operator*() { return *this; }
int_op_inserter &operator++() { return *this; }
int_op_inserter &operator++(int) { return *this; }
};
class PHIExpression final : public BasicExpression {
private:
BasicBlock *BB;
public:
PHIExpression(unsigned NumOperands, BasicBlock *B)
: BasicExpression(NumOperands, ET_Phi), BB(B) {}
PHIExpression() = delete;
PHIExpression(const PHIExpression &) = delete;
PHIExpression &operator=(const PHIExpression &) = delete;
~PHIExpression() override;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Phi;
}
bool equals(const Expression &Other) const override {
if (!this->BasicExpression::equals(Other))
return false;
const PHIExpression &OE = cast<PHIExpression>(Other);
return BB == OE.BB;
}
hash_code getHashValue() const override {
return hash_combine(this->BasicExpression::getHashValue(), BB);
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypePhi, ";
this->BasicExpression::printInternal(OS, false);
OS << "bb = " << BB;
}
};
class DeadExpression final : public Expression {
public:
DeadExpression() : Expression(ET_Dead) {}
DeadExpression(const DeadExpression &) = delete;
DeadExpression &operator=(const DeadExpression &) = delete;
static bool classof(const Expression *E) {
return E->getExpressionType() == ET_Dead;
}
};
class VariableExpression final : public Expression {
private:
Value *VariableValue;
public:
VariableExpression(Value *V) : Expression(ET_Variable), VariableValue(V) {}
VariableExpression() = delete;
VariableExpression(const VariableExpression &) = delete;
VariableExpression &operator=(const VariableExpression &) = delete;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Variable;
}
Value *getVariableValue() const { return VariableValue; }
void setVariableValue(Value *V) { VariableValue = V; }
bool equals(const Expression &Other) const override {
const VariableExpression &OC = cast<VariableExpression>(Other);
return VariableValue == OC.VariableValue;
}
hash_code getHashValue() const override {
return hash_combine(this->Expression::getHashValue(),
VariableValue->getType(), VariableValue);
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeVariable, ";
this->Expression::printInternal(OS, false);
OS << " variable = " << *VariableValue;
}
};
class ConstantExpression final : public Expression {
private:
Constant *ConstantValue = nullptr;
public:
ConstantExpression() : Expression(ET_Constant) {}
ConstantExpression(Constant *constantValue)
: Expression(ET_Constant), ConstantValue(constantValue) {}
ConstantExpression(const ConstantExpression &) = delete;
ConstantExpression &operator=(const ConstantExpression &) = delete;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Constant;
}
Constant *getConstantValue() const { return ConstantValue; }
void setConstantValue(Constant *V) { ConstantValue = V; }
bool equals(const Expression &Other) const override {
const ConstantExpression &OC = cast<ConstantExpression>(Other);
return ConstantValue == OC.ConstantValue;
}
hash_code getHashValue() const override {
return hash_combine(this->Expression::getHashValue(),
ConstantValue->getType(), ConstantValue);
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeConstant, ";
this->Expression::printInternal(OS, false);
OS << " constant = " << *ConstantValue;
}
};
class UnknownExpression final : public Expression {
private:
Instruction *Inst;
public:
UnknownExpression(Instruction *I) : Expression(ET_Unknown), Inst(I) {}
UnknownExpression() = delete;
UnknownExpression(const UnknownExpression &) = delete;
UnknownExpression &operator=(const UnknownExpression &) = delete;
static bool classof(const Expression *EB) {
return EB->getExpressionType() == ET_Unknown;
}
Instruction *getInstruction() const { return Inst; }
void setInstruction(Instruction *I) { Inst = I; }
bool equals(const Expression &Other) const override {
const auto &OU = cast<UnknownExpression>(Other);
return Inst == OU.Inst;
}
hash_code getHashValue() const override {
return hash_combine(this->Expression::getHashValue(), Inst);
}
// Debugging support
void printInternal(raw_ostream &OS, bool PrintEType) const override {
if (PrintEType)
OS << "ExpressionTypeUnknown, ";
this->Expression::printInternal(OS, false);
OS << " inst = " << *Inst;
}
};
} // end namespace GVNExpression
} // end namespace llvm
#endif // LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
|