1 //======- GVNExpression.h - GVN Expression classes --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// The header file for the GVN pass that contains expression handling
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
17 #define LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/Analysis/MemorySSA.h"
22 #include "llvm/IR/Constant.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Value.h"
25 #include "llvm/Support/Allocator.h"
26 #include "llvm/Support/ArrayRecycler.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
37 namespace GVNExpression {
61 mutable hash_code HashVal;
64 Expression(ExpressionType ET = ET_Base, unsigned O = ~2U)
65 : EType(ET), Opcode(O), HashVal(0) {}
66 Expression(const Expression &) = delete;
67 Expression &operator=(const Expression &) = delete;
68 virtual ~Expression();
70 static unsigned getEmptyKey() { return ~0U; }
71 static unsigned getTombstoneKey() { return ~1U; }
72 bool operator!=(const Expression &Other) const { return !(*this == Other); }
73 bool operator==(const Expression &Other) const {
74 if (getOpcode() != Other.getOpcode())
76 if (getOpcode() == getEmptyKey() || getOpcode() == getTombstoneKey())
78 // Compare the expression type for anything but load and store.
79 // For load and store we set the opcode to zero to make them equal.
80 if (getExpressionType() != ET_Load && getExpressionType() != ET_Store &&
81 getExpressionType() != Other.getExpressionType())
86 hash_code getComputedHash() const {
87 // It's theoretically possible for a thing to hash to zero. In that case,
88 // we will just compute the hash a few extra times, which is no worse that
89 // we did before, which was to compute it always.
90 if (static_cast<unsigned>(HashVal) == 0)
91 HashVal = getHashValue();
95 virtual bool equals(const Expression &Other) const { return true; }
97 unsigned getOpcode() const { return Opcode; }
98 void setOpcode(unsigned opcode) { Opcode = opcode; }
99 ExpressionType getExpressionType() const { return EType; }
101 // We deliberately leave the expression type out of the hash value.
102 virtual hash_code getHashValue() const { return getOpcode(); }
107 virtual void printInternal(raw_ostream &OS, bool PrintEType) const {
109 OS << "etype = " << getExpressionType() << ",";
110 OS << "opcode = " << getOpcode() << ", ";
113 void print(raw_ostream &OS) const {
115 printInternal(OS, true);
119 LLVM_DUMP_METHOD void dump() const {
125 inline raw_ostream &operator<<(raw_ostream &OS, const Expression &E) {
130 class BasicExpression : public Expression {
132 typedef ArrayRecycler<Value *> RecyclerType;
133 typedef RecyclerType::Capacity RecyclerCapacity;
135 unsigned MaxOperands;
136 unsigned NumOperands;
140 BasicExpression(unsigned NumOperands)
141 : BasicExpression(NumOperands, ET_Basic) {}
142 BasicExpression(unsigned NumOperands, ExpressionType ET)
143 : Expression(ET), Operands(nullptr), MaxOperands(NumOperands),
144 NumOperands(0), ValueType(nullptr) {}
145 BasicExpression() = delete;
146 BasicExpression(const BasicExpression &) = delete;
147 BasicExpression &operator=(const BasicExpression &) = delete;
148 ~BasicExpression() override;
150 static bool classof(const Expression *EB) {
151 ExpressionType ET = EB->getExpressionType();
152 return ET > ET_BasicStart && ET < ET_BasicEnd;
155 /// \brief Swap two operands. Used during GVN to put commutative operands in
157 void swapOperands(unsigned First, unsigned Second) {
158 std::swap(Operands[First], Operands[Second]);
161 Value *getOperand(unsigned N) const {
162 assert(Operands && "Operands not allocated");
163 assert(N < NumOperands && "Operand out of range");
167 void setOperand(unsigned N, Value *V) {
168 assert(Operands && "Operands not allocated before setting");
169 assert(N < NumOperands && "Operand out of range");
173 unsigned getNumOperands() const { return NumOperands; }
175 typedef Value **op_iterator;
176 typedef Value *const *const_op_iterator;
177 op_iterator op_begin() { return Operands; }
178 op_iterator op_end() { return Operands + NumOperands; }
179 const_op_iterator op_begin() const { return Operands; }
180 const_op_iterator op_end() const { return Operands + NumOperands; }
181 iterator_range<op_iterator> operands() {
182 return iterator_range<op_iterator>(op_begin(), op_end());
184 iterator_range<const_op_iterator> operands() const {
185 return iterator_range<const_op_iterator>(op_begin(), op_end());
188 void op_push_back(Value *Arg) {
189 assert(NumOperands < MaxOperands && "Tried to add too many operands");
190 assert(Operands && "Operandss not allocated before pushing");
191 Operands[NumOperands++] = Arg;
193 bool op_empty() const { return getNumOperands() == 0; }
195 void allocateOperands(RecyclerType &Recycler, BumpPtrAllocator &Allocator) {
196 assert(!Operands && "Operands already allocated");
197 Operands = Recycler.allocate(RecyclerCapacity::get(MaxOperands), Allocator);
199 void deallocateOperands(RecyclerType &Recycler) {
200 Recycler.deallocate(RecyclerCapacity::get(MaxOperands), Operands);
203 void setType(Type *T) { ValueType = T; }
204 Type *getType() const { return ValueType; }
206 bool equals(const Expression &Other) const override {
207 if (getOpcode() != Other.getOpcode())
210 const auto &OE = cast<BasicExpression>(Other);
211 return getType() == OE.getType() && NumOperands == OE.NumOperands &&
212 std::equal(op_begin(), op_end(), OE.op_begin());
215 hash_code getHashValue() const override {
216 return hash_combine(this->Expression::getHashValue(), ValueType,
217 hash_combine_range(op_begin(), op_end()));
223 void printInternal(raw_ostream &OS, bool PrintEType) const override {
225 OS << "ExpressionTypeBasic, ";
227 this->Expression::printInternal(OS, false);
228 OS << "operands = {";
229 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
230 OS << "[" << i << "] = ";
231 Operands[i]->printAsOperand(OS);
239 : public std::iterator<std::output_iterator_tag, void, void, void, void> {
241 typedef BasicExpression Container;
245 explicit op_inserter(BasicExpression &E) : BE(&E) {}
246 explicit op_inserter(BasicExpression *E) : BE(E) {}
248 op_inserter &operator=(Value *val) {
249 BE->op_push_back(val);
252 op_inserter &operator*() { return *this; }
253 op_inserter &operator++() { return *this; }
254 op_inserter &operator++(int) { return *this; }
257 class MemoryExpression : public BasicExpression {
259 const MemoryAccess *MemoryLeader;
262 MemoryExpression(unsigned NumOperands, enum ExpressionType EType,
263 const MemoryAccess *MemoryLeader)
264 : BasicExpression(NumOperands, EType), MemoryLeader(MemoryLeader){};
266 MemoryExpression() = delete;
267 MemoryExpression(const MemoryExpression &) = delete;
268 MemoryExpression &operator=(const MemoryExpression &) = delete;
269 static bool classof(const Expression *EB) {
270 return EB->getExpressionType() > ET_MemoryStart &&
271 EB->getExpressionType() < ET_MemoryEnd;
273 hash_code getHashValue() const override {
274 return hash_combine(this->BasicExpression::getHashValue(), MemoryLeader);
277 bool equals(const Expression &Other) const override {
278 if (!this->BasicExpression::equals(Other))
280 const MemoryExpression &OtherMCE = cast<MemoryExpression>(Other);
282 return MemoryLeader == OtherMCE.MemoryLeader;
285 const MemoryAccess *getMemoryLeader() const { return MemoryLeader; }
286 void setMemoryLeader(const MemoryAccess *ML) { MemoryLeader = ML; }
289 class CallExpression final : public MemoryExpression {
294 CallExpression(unsigned NumOperands, CallInst *C,
295 const MemoryAccess *MemoryLeader)
296 : MemoryExpression(NumOperands, ET_Call, MemoryLeader), Call(C) {}
297 CallExpression() = delete;
298 CallExpression(const CallExpression &) = delete;
299 CallExpression &operator=(const CallExpression &) = delete;
300 ~CallExpression() override;
302 static bool classof(const Expression *EB) {
303 return EB->getExpressionType() == ET_Call;
309 void printInternal(raw_ostream &OS, bool PrintEType) const override {
311 OS << "ExpressionTypeCall, ";
312 this->BasicExpression::printInternal(OS, false);
313 OS << " represents call at ";
314 Call->printAsOperand(OS);
318 class LoadExpression final : public MemoryExpression {
324 LoadExpression(unsigned NumOperands, LoadInst *L,
325 const MemoryAccess *MemoryLeader)
326 : LoadExpression(ET_Load, NumOperands, L, MemoryLeader) {}
327 LoadExpression(enum ExpressionType EType, unsigned NumOperands, LoadInst *L,
328 const MemoryAccess *MemoryLeader)
329 : MemoryExpression(NumOperands, EType, MemoryLeader), Load(L) {
330 Alignment = L ? L->getAlignment() : 0;
332 LoadExpression() = delete;
333 LoadExpression(const LoadExpression &) = delete;
334 LoadExpression &operator=(const LoadExpression &) = delete;
335 ~LoadExpression() override;
337 static bool classof(const Expression *EB) {
338 return EB->getExpressionType() == ET_Load;
341 LoadInst *getLoadInst() const { return Load; }
342 void setLoadInst(LoadInst *L) { Load = L; }
344 unsigned getAlignment() const { return Alignment; }
345 void setAlignment(unsigned Align) { Alignment = Align; }
347 bool equals(const Expression &Other) const override;
352 void printInternal(raw_ostream &OS, bool PrintEType) const override {
354 OS << "ExpressionTypeLoad, ";
355 this->BasicExpression::printInternal(OS, false);
356 OS << " represents Load at ";
357 Load->printAsOperand(OS);
358 OS << " with MemoryLeader " << *getMemoryLeader();
362 class StoreExpression final : public MemoryExpression {
368 StoreExpression(unsigned NumOperands, StoreInst *S, Value *StoredValue,
369 const MemoryAccess *MemoryLeader)
370 : MemoryExpression(NumOperands, ET_Store, MemoryLeader), Store(S),
371 StoredValue(StoredValue) {}
372 StoreExpression() = delete;
373 StoreExpression(const StoreExpression &) = delete;
374 StoreExpression &operator=(const StoreExpression &) = delete;
375 ~StoreExpression() override;
377 static bool classof(const Expression *EB) {
378 return EB->getExpressionType() == ET_Store;
381 StoreInst *getStoreInst() const { return Store; }
382 Value *getStoredValue() const { return StoredValue; }
384 bool equals(const Expression &Other) const override;
388 void printInternal(raw_ostream &OS, bool PrintEType) const override {
390 OS << "ExpressionTypeStore, ";
391 this->BasicExpression::printInternal(OS, false);
392 OS << " represents Store " << *Store;
393 OS << " with StoredValue ";
394 StoredValue->printAsOperand(OS);
395 OS << " and MemoryLeader " << *getMemoryLeader();
399 class AggregateValueExpression final : public BasicExpression {
401 unsigned MaxIntOperands;
402 unsigned NumIntOperands;
403 unsigned *IntOperands;
406 AggregateValueExpression(unsigned NumOperands, unsigned NumIntOperands)
407 : BasicExpression(NumOperands, ET_AggregateValue),
408 MaxIntOperands(NumIntOperands), NumIntOperands(0),
409 IntOperands(nullptr) {}
410 AggregateValueExpression() = delete;
411 AggregateValueExpression(const AggregateValueExpression &) = delete;
412 AggregateValueExpression &
413 operator=(const AggregateValueExpression &) = delete;
414 ~AggregateValueExpression() override;
416 static bool classof(const Expression *EB) {
417 return EB->getExpressionType() == ET_AggregateValue;
420 typedef unsigned *int_arg_iterator;
421 typedef const unsigned *const_int_arg_iterator;
423 int_arg_iterator int_op_begin() { return IntOperands; }
424 int_arg_iterator int_op_end() { return IntOperands + NumIntOperands; }
425 const_int_arg_iterator int_op_begin() const { return IntOperands; }
426 const_int_arg_iterator int_op_end() const {
427 return IntOperands + NumIntOperands;
429 unsigned int_op_size() const { return NumIntOperands; }
430 bool int_op_empty() const { return NumIntOperands == 0; }
431 void int_op_push_back(unsigned IntOperand) {
432 assert(NumIntOperands < MaxIntOperands &&
433 "Tried to add too many int operands");
434 assert(IntOperands && "Operands not allocated before pushing");
435 IntOperands[NumIntOperands++] = IntOperand;
438 virtual void allocateIntOperands(BumpPtrAllocator &Allocator) {
439 assert(!IntOperands && "Operands already allocated");
440 IntOperands = Allocator.Allocate<unsigned>(MaxIntOperands);
443 bool equals(const Expression &Other) const override {
444 if (!this->BasicExpression::equals(Other))
446 const AggregateValueExpression &OE = cast<AggregateValueExpression>(Other);
447 return NumIntOperands == OE.NumIntOperands &&
448 std::equal(int_op_begin(), int_op_end(), OE.int_op_begin());
451 hash_code getHashValue() const override {
452 return hash_combine(this->BasicExpression::getHashValue(),
453 hash_combine_range(int_op_begin(), int_op_end()));
459 void printInternal(raw_ostream &OS, bool PrintEType) const override {
461 OS << "ExpressionTypeAggregateValue, ";
462 this->BasicExpression::printInternal(OS, false);
463 OS << ", intoperands = {";
464 for (unsigned i = 0, e = int_op_size(); i != e; ++i) {
465 OS << "[" << i << "] = " << IntOperands[i] << " ";
471 class int_op_inserter
472 : public std::iterator<std::output_iterator_tag, void, void, void, void> {
474 typedef AggregateValueExpression Container;
478 explicit int_op_inserter(AggregateValueExpression &E) : AVE(&E) {}
479 explicit int_op_inserter(AggregateValueExpression *E) : AVE(E) {}
481 int_op_inserter &operator=(unsigned int val) {
482 AVE->int_op_push_back(val);
485 int_op_inserter &operator*() { return *this; }
486 int_op_inserter &operator++() { return *this; }
487 int_op_inserter &operator++(int) { return *this; }
490 class PHIExpression final : public BasicExpression {
495 PHIExpression(unsigned NumOperands, BasicBlock *B)
496 : BasicExpression(NumOperands, ET_Phi), BB(B) {}
497 PHIExpression() = delete;
498 PHIExpression(const PHIExpression &) = delete;
499 PHIExpression &operator=(const PHIExpression &) = delete;
500 ~PHIExpression() override;
502 static bool classof(const Expression *EB) {
503 return EB->getExpressionType() == ET_Phi;
506 bool equals(const Expression &Other) const override {
507 if (!this->BasicExpression::equals(Other))
509 const PHIExpression &OE = cast<PHIExpression>(Other);
513 hash_code getHashValue() const override {
514 return hash_combine(this->BasicExpression::getHashValue(), BB);
520 void printInternal(raw_ostream &OS, bool PrintEType) const override {
522 OS << "ExpressionTypePhi, ";
523 this->BasicExpression::printInternal(OS, false);
528 class DeadExpression final : public Expression {
530 DeadExpression() : Expression(ET_Dead) {}
531 DeadExpression(const DeadExpression &) = delete;
532 DeadExpression &operator=(const DeadExpression &) = delete;
534 static bool classof(const Expression *E) {
535 return E->getExpressionType() == ET_Dead;
539 class VariableExpression final : public Expression {
541 Value *VariableValue;
544 VariableExpression(Value *V) : Expression(ET_Variable), VariableValue(V) {}
545 VariableExpression() = delete;
546 VariableExpression(const VariableExpression &) = delete;
547 VariableExpression &operator=(const VariableExpression &) = delete;
549 static bool classof(const Expression *EB) {
550 return EB->getExpressionType() == ET_Variable;
553 Value *getVariableValue() const { return VariableValue; }
554 void setVariableValue(Value *V) { VariableValue = V; }
556 bool equals(const Expression &Other) const override {
557 const VariableExpression &OC = cast<VariableExpression>(Other);
558 return VariableValue == OC.VariableValue;
561 hash_code getHashValue() const override {
562 return hash_combine(this->Expression::getHashValue(),
563 VariableValue->getType(), VariableValue);
569 void printInternal(raw_ostream &OS, bool PrintEType) const override {
571 OS << "ExpressionTypeVariable, ";
572 this->Expression::printInternal(OS, false);
573 OS << " variable = " << *VariableValue;
577 class ConstantExpression final : public Expression {
579 Constant *ConstantValue = nullptr;
582 ConstantExpression() : Expression(ET_Constant) {}
583 ConstantExpression(Constant *constantValue)
584 : Expression(ET_Constant), ConstantValue(constantValue) {}
585 ConstantExpression(const ConstantExpression &) = delete;
586 ConstantExpression &operator=(const ConstantExpression &) = delete;
588 static bool classof(const Expression *EB) {
589 return EB->getExpressionType() == ET_Constant;
592 Constant *getConstantValue() const { return ConstantValue; }
593 void setConstantValue(Constant *V) { ConstantValue = V; }
595 bool equals(const Expression &Other) const override {
596 const ConstantExpression &OC = cast<ConstantExpression>(Other);
597 return ConstantValue == OC.ConstantValue;
600 hash_code getHashValue() const override {
601 return hash_combine(this->Expression::getHashValue(),
602 ConstantValue->getType(), ConstantValue);
608 void printInternal(raw_ostream &OS, bool PrintEType) const override {
610 OS << "ExpressionTypeConstant, ";
611 this->Expression::printInternal(OS, false);
612 OS << " constant = " << *ConstantValue;
616 class UnknownExpression final : public Expression {
621 UnknownExpression(Instruction *I) : Expression(ET_Unknown), Inst(I) {}
622 UnknownExpression() = delete;
623 UnknownExpression(const UnknownExpression &) = delete;
624 UnknownExpression &operator=(const UnknownExpression &) = delete;
626 static bool classof(const Expression *EB) {
627 return EB->getExpressionType() == ET_Unknown;
630 Instruction *getInstruction() const { return Inst; }
631 void setInstruction(Instruction *I) { Inst = I; }
633 bool equals(const Expression &Other) const override {
634 const auto &OU = cast<UnknownExpression>(Other);
635 return Inst == OU.Inst;
638 hash_code getHashValue() const override {
639 return hash_combine(this->Expression::getHashValue(), Inst);
645 void printInternal(raw_ostream &OS, bool PrintEType) const override {
647 OS << "ExpressionTypeUnknown, ";
648 this->Expression::printInternal(OS, false);
649 OS << " inst = " << *Inst;
653 } // end namespace GVNExpression
655 } // end namespace llvm
657 #endif // LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H