]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Transforms/Scalar/GVNExpression.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Transforms / Scalar / GVNExpression.h
1 //======- GVNExpression.h - GVN Expression classes --------------*- C++ -*-===//
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 /// \file
10 ///
11 /// The header file for the GVN pass that contains expression handling
12 /// classes
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
17 #define LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H
18
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"
30 #include <algorithm>
31 #include <cassert>
32 #include <iterator>
33 #include <utility>
34
35 namespace llvm {
36
37 namespace GVNExpression {
38
39 enum ExpressionType {
40   ET_Base,
41   ET_Constant,
42   ET_Variable,
43   ET_Dead,
44   ET_Unknown,
45   ET_BasicStart,
46   ET_Basic,
47   ET_AggregateValue,
48   ET_Phi,
49   ET_MemoryStart,
50   ET_Call,
51   ET_Load,
52   ET_Store,
53   ET_MemoryEnd,
54   ET_BasicEnd
55 };
56
57 class Expression {
58 private:
59   ExpressionType EType;
60   unsigned Opcode;
61   mutable hash_code HashVal;
62
63 public:
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();
69
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())
75       return false;
76     if (getOpcode() == getEmptyKey() || getOpcode() == getTombstoneKey())
77       return true;
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())
82       return false;
83
84     return equals(Other);
85   }
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();
92     return HashVal;
93   }
94
95   virtual bool equals(const Expression &Other) const { return true; }
96
97   unsigned getOpcode() const { return Opcode; }
98   void setOpcode(unsigned opcode) { Opcode = opcode; }
99   ExpressionType getExpressionType() const { return EType; }
100
101   // We deliberately leave the expression type out of the hash value.
102   virtual hash_code getHashValue() const { return getOpcode(); }
103
104   //
105   // Debugging support
106   //
107   virtual void printInternal(raw_ostream &OS, bool PrintEType) const {
108     if (PrintEType)
109       OS << "etype = " << getExpressionType() << ",";
110     OS << "opcode = " << getOpcode() << ", ";
111   }
112
113   void print(raw_ostream &OS) const {
114     OS << "{ ";
115     printInternal(OS, true);
116     OS << "}";
117   }
118
119   LLVM_DUMP_METHOD void dump() const {
120     print(dbgs());
121     dbgs() << "\n";
122   }
123 };
124
125 inline raw_ostream &operator<<(raw_ostream &OS, const Expression &E) {
126   E.print(OS);
127   return OS;
128 }
129
130 class BasicExpression : public Expression {
131 private:
132   typedef ArrayRecycler<Value *> RecyclerType;
133   typedef RecyclerType::Capacity RecyclerCapacity;
134   Value **Operands;
135   unsigned MaxOperands;
136   unsigned NumOperands;
137   Type *ValueType;
138
139 public:
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;
149
150   static bool classof(const Expression *EB) {
151     ExpressionType ET = EB->getExpressionType();
152     return ET > ET_BasicStart && ET < ET_BasicEnd;
153   }
154
155   /// \brief Swap two operands. Used during GVN to put commutative operands in
156   /// order.
157   void swapOperands(unsigned First, unsigned Second) {
158     std::swap(Operands[First], Operands[Second]);
159   }
160
161   Value *getOperand(unsigned N) const {
162     assert(Operands && "Operands not allocated");
163     assert(N < NumOperands && "Operand out of range");
164     return Operands[N];
165   }
166
167   void setOperand(unsigned N, Value *V) {
168     assert(Operands && "Operands not allocated before setting");
169     assert(N < NumOperands && "Operand out of range");
170     Operands[N] = V;
171   }
172
173   unsigned getNumOperands() const { return NumOperands; }
174
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());
183   }
184   iterator_range<const_op_iterator> operands() const {
185     return iterator_range<const_op_iterator>(op_begin(), op_end());
186   }
187
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;
192   }
193   bool op_empty() const { return getNumOperands() == 0; }
194
195   void allocateOperands(RecyclerType &Recycler, BumpPtrAllocator &Allocator) {
196     assert(!Operands && "Operands already allocated");
197     Operands = Recycler.allocate(RecyclerCapacity::get(MaxOperands), Allocator);
198   }
199   void deallocateOperands(RecyclerType &Recycler) {
200     Recycler.deallocate(RecyclerCapacity::get(MaxOperands), Operands);
201   }
202
203   void setType(Type *T) { ValueType = T; }
204   Type *getType() const { return ValueType; }
205
206   bool equals(const Expression &Other) const override {
207     if (getOpcode() != Other.getOpcode())
208       return false;
209
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());
213   }
214
215   hash_code getHashValue() const override {
216     return hash_combine(this->Expression::getHashValue(), ValueType,
217                         hash_combine_range(op_begin(), op_end()));
218   }
219
220   //
221   // Debugging support
222   //
223   void printInternal(raw_ostream &OS, bool PrintEType) const override {
224     if (PrintEType)
225       OS << "ExpressionTypeBasic, ";
226
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);
232       OS << "  ";
233     }
234     OS << "} ";
235   }
236 };
237
238 class op_inserter
239     : public std::iterator<std::output_iterator_tag, void, void, void, void> {
240 private:
241   typedef BasicExpression Container;
242   Container *BE;
243
244 public:
245   explicit op_inserter(BasicExpression &E) : BE(&E) {}
246   explicit op_inserter(BasicExpression *E) : BE(E) {}
247
248   op_inserter &operator=(Value *val) {
249     BE->op_push_back(val);
250     return *this;
251   }
252   op_inserter &operator*() { return *this; }
253   op_inserter &operator++() { return *this; }
254   op_inserter &operator++(int) { return *this; }
255 };
256
257 class MemoryExpression : public BasicExpression {
258 private:
259   const MemoryAccess *MemoryLeader;
260
261 public:
262   MemoryExpression(unsigned NumOperands, enum ExpressionType EType,
263                    const MemoryAccess *MemoryLeader)
264       : BasicExpression(NumOperands, EType), MemoryLeader(MemoryLeader){};
265
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;
272   }
273   hash_code getHashValue() const override {
274     return hash_combine(this->BasicExpression::getHashValue(), MemoryLeader);
275   }
276
277   bool equals(const Expression &Other) const override {
278     if (!this->BasicExpression::equals(Other))
279       return false;
280     const MemoryExpression &OtherMCE = cast<MemoryExpression>(Other);
281
282     return MemoryLeader == OtherMCE.MemoryLeader;
283   }
284
285   const MemoryAccess *getMemoryLeader() const { return MemoryLeader; }
286   void setMemoryLeader(const MemoryAccess *ML) { MemoryLeader = ML; }
287 };
288
289 class CallExpression final : public MemoryExpression {
290 private:
291   CallInst *Call;
292
293 public:
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;
301
302   static bool classof(const Expression *EB) {
303     return EB->getExpressionType() == ET_Call;
304   }
305
306   //
307   // Debugging support
308   //
309   void printInternal(raw_ostream &OS, bool PrintEType) const override {
310     if (PrintEType)
311       OS << "ExpressionTypeCall, ";
312     this->BasicExpression::printInternal(OS, false);
313     OS << " represents call at ";
314     Call->printAsOperand(OS);
315   }
316 };
317
318 class LoadExpression final : public MemoryExpression {
319 private:
320   LoadInst *Load;
321   unsigned Alignment;
322
323 public:
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;
331   }
332   LoadExpression() = delete;
333   LoadExpression(const LoadExpression &) = delete;
334   LoadExpression &operator=(const LoadExpression &) = delete;
335   ~LoadExpression() override;
336
337   static bool classof(const Expression *EB) {
338     return EB->getExpressionType() == ET_Load;
339   }
340
341   LoadInst *getLoadInst() const { return Load; }
342   void setLoadInst(LoadInst *L) { Load = L; }
343
344   unsigned getAlignment() const { return Alignment; }
345   void setAlignment(unsigned Align) { Alignment = Align; }
346
347   bool equals(const Expression &Other) const override;
348
349   //
350   // Debugging support
351   //
352   void printInternal(raw_ostream &OS, bool PrintEType) const override {
353     if (PrintEType)
354       OS << "ExpressionTypeLoad, ";
355     this->BasicExpression::printInternal(OS, false);
356     OS << " represents Load at ";
357     Load->printAsOperand(OS);
358     OS << " with MemoryLeader " << *getMemoryLeader();
359   }
360 };
361
362 class StoreExpression final : public MemoryExpression {
363 private:
364   StoreInst *Store;
365   Value *StoredValue;
366
367 public:
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;
376
377   static bool classof(const Expression *EB) {
378     return EB->getExpressionType() == ET_Store;
379   }
380
381   StoreInst *getStoreInst() const { return Store; }
382   Value *getStoredValue() const { return StoredValue; }
383
384   bool equals(const Expression &Other) const override;
385
386   // Debugging support
387   //
388   void printInternal(raw_ostream &OS, bool PrintEType) const override {
389     if (PrintEType)
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();
396   }
397 };
398
399 class AggregateValueExpression final : public BasicExpression {
400 private:
401   unsigned MaxIntOperands;
402   unsigned NumIntOperands;
403   unsigned *IntOperands;
404
405 public:
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;
415
416   static bool classof(const Expression *EB) {
417     return EB->getExpressionType() == ET_AggregateValue;
418   }
419
420   typedef unsigned *int_arg_iterator;
421   typedef const unsigned *const_int_arg_iterator;
422
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;
428   }
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;
436   }
437
438   virtual void allocateIntOperands(BumpPtrAllocator &Allocator) {
439     assert(!IntOperands && "Operands already allocated");
440     IntOperands = Allocator.Allocate<unsigned>(MaxIntOperands);
441   }
442
443   bool equals(const Expression &Other) const override {
444     if (!this->BasicExpression::equals(Other))
445       return false;
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());
449   }
450
451   hash_code getHashValue() const override {
452     return hash_combine(this->BasicExpression::getHashValue(),
453                         hash_combine_range(int_op_begin(), int_op_end()));
454   }
455
456   //
457   // Debugging support
458   //
459   void printInternal(raw_ostream &OS, bool PrintEType) const override {
460     if (PrintEType)
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] << "  ";
466     }
467     OS << "}";
468   }
469 };
470
471 class int_op_inserter
472     : public std::iterator<std::output_iterator_tag, void, void, void, void> {
473 private:
474   typedef AggregateValueExpression Container;
475   Container *AVE;
476
477 public:
478   explicit int_op_inserter(AggregateValueExpression &E) : AVE(&E) {}
479   explicit int_op_inserter(AggregateValueExpression *E) : AVE(E) {}
480
481   int_op_inserter &operator=(unsigned int val) {
482     AVE->int_op_push_back(val);
483     return *this;
484   }
485   int_op_inserter &operator*() { return *this; }
486   int_op_inserter &operator++() { return *this; }
487   int_op_inserter &operator++(int) { return *this; }
488 };
489
490 class PHIExpression final : public BasicExpression {
491 private:
492   BasicBlock *BB;
493
494 public:
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;
501
502   static bool classof(const Expression *EB) {
503     return EB->getExpressionType() == ET_Phi;
504   }
505
506   bool equals(const Expression &Other) const override {
507     if (!this->BasicExpression::equals(Other))
508       return false;
509     const PHIExpression &OE = cast<PHIExpression>(Other);
510     return BB == OE.BB;
511   }
512
513   hash_code getHashValue() const override {
514     return hash_combine(this->BasicExpression::getHashValue(), BB);
515   }
516
517   //
518   // Debugging support
519   //
520   void printInternal(raw_ostream &OS, bool PrintEType) const override {
521     if (PrintEType)
522       OS << "ExpressionTypePhi, ";
523     this->BasicExpression::printInternal(OS, false);
524     OS << "bb = " << BB;
525   }
526 };
527
528 class DeadExpression final : public Expression {
529 public:
530   DeadExpression() : Expression(ET_Dead) {}
531   DeadExpression(const DeadExpression &) = delete;
532   DeadExpression &operator=(const DeadExpression &) = delete;
533
534   static bool classof(const Expression *E) {
535     return E->getExpressionType() == ET_Dead;
536   }
537 };
538
539 class VariableExpression final : public Expression {
540 private:
541   Value *VariableValue;
542
543 public:
544   VariableExpression(Value *V) : Expression(ET_Variable), VariableValue(V) {}
545   VariableExpression() = delete;
546   VariableExpression(const VariableExpression &) = delete;
547   VariableExpression &operator=(const VariableExpression &) = delete;
548
549   static bool classof(const Expression *EB) {
550     return EB->getExpressionType() == ET_Variable;
551   }
552
553   Value *getVariableValue() const { return VariableValue; }
554   void setVariableValue(Value *V) { VariableValue = V; }
555
556   bool equals(const Expression &Other) const override {
557     const VariableExpression &OC = cast<VariableExpression>(Other);
558     return VariableValue == OC.VariableValue;
559   }
560
561   hash_code getHashValue() const override {
562     return hash_combine(this->Expression::getHashValue(),
563                         VariableValue->getType(), VariableValue);
564   }
565
566   //
567   // Debugging support
568   //
569   void printInternal(raw_ostream &OS, bool PrintEType) const override {
570     if (PrintEType)
571       OS << "ExpressionTypeVariable, ";
572     this->Expression::printInternal(OS, false);
573     OS << " variable = " << *VariableValue;
574   }
575 };
576
577 class ConstantExpression final : public Expression {
578 private:
579   Constant *ConstantValue = nullptr;
580
581 public:
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;
587
588   static bool classof(const Expression *EB) {
589     return EB->getExpressionType() == ET_Constant;
590   }
591
592   Constant *getConstantValue() const { return ConstantValue; }
593   void setConstantValue(Constant *V) { ConstantValue = V; }
594
595   bool equals(const Expression &Other) const override {
596     const ConstantExpression &OC = cast<ConstantExpression>(Other);
597     return ConstantValue == OC.ConstantValue;
598   }
599
600   hash_code getHashValue() const override {
601     return hash_combine(this->Expression::getHashValue(),
602                         ConstantValue->getType(), ConstantValue);
603   }
604
605   //
606   // Debugging support
607   //
608   void printInternal(raw_ostream &OS, bool PrintEType) const override {
609     if (PrintEType)
610       OS << "ExpressionTypeConstant, ";
611     this->Expression::printInternal(OS, false);
612     OS << " constant = " << *ConstantValue;
613   }
614 };
615
616 class UnknownExpression final : public Expression {
617 private:
618   Instruction *Inst;
619
620 public:
621   UnknownExpression(Instruction *I) : Expression(ET_Unknown), Inst(I) {}
622   UnknownExpression() = delete;
623   UnknownExpression(const UnknownExpression &) = delete;
624   UnknownExpression &operator=(const UnknownExpression &) = delete;
625
626   static bool classof(const Expression *EB) {
627     return EB->getExpressionType() == ET_Unknown;
628   }
629
630   Instruction *getInstruction() const { return Inst; }
631   void setInstruction(Instruction *I) { Inst = I; }
632
633   bool equals(const Expression &Other) const override {
634     const auto &OU = cast<UnknownExpression>(Other);
635     return Inst == OU.Inst;
636   }
637
638   hash_code getHashValue() const override {
639     return hash_combine(this->Expression::getHashValue(), Inst);
640   }
641
642   //
643   // Debugging support
644   //
645   void printInternal(raw_ostream &OS, bool PrintEType) const override {
646     if (PrintEType)
647       OS << "ExpressionTypeUnknown, ";
648     this->Expression::printInternal(OS, false);
649     OS << " inst = " << *Inst;
650   }
651 };
652
653 } // end namespace GVNExpression
654
655 } // end namespace llvm
656
657 #endif // LLVM_TRANSFORMS_SCALAR_GVNEXPRESSION_H