llvm.org GIT mirror llvm / f2da724
New isAssociative/isCommutative inspection methods, graciously contributed by Casey Carter. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4459 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 17 years ago
2 changed file(s) with 60 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
7373 return iType >= BinaryOpsBegin && iType < BinaryOpsEnd;
7474 }
7575
76 /// isAssociative - Return true if the instruction is associative:
77 ///
78 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
79 ///
80 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
81 /// not applied to floating point types.
82 ///
83 bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
84 static bool isAssociative(unsigned op, const Type *Ty);
85
86 /// isCommutative - Return true if the instruction is commutative:
87 ///
88 /// Commutative operators satistify: (x op y) === (y op x)
89 ///
90 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
91 /// applied to any type.
92 ///
93 bool isCommutative() const { return isCommutative(getOpcode()); }
94 static bool isCommutative(unsigned op);
95
96
7697 virtual void print(std::ostream &OS) const;
7798
7899 /// Methods for support type inquiry through isa, cast, and dyn_cast:
9696
9797 return 0;
9898 }
99
100
101 /// isAssociative - Return true if the instruction is associative:
102 ///
103 /// Associative operators satisfy: x op (y op z) === (x op y) op z)
104 ///
105 /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when not
106 /// applied to floating point types.
107 ///
108 bool Instruction::isAssociative(unsigned Opcode, const Type *Ty) {
109 if (Opcode == Add || Opcode == Mul ||
110 Opcode == And || Opcode == Or || Opcode == Xor) {
111 // Floating point operations do not associate!
112 return !Ty->isFloatingPoint();
113 }
114 return 0;
115 }
116
117 /// isCommutative - Return true if the instruction is commutative:
118 ///
119 /// Commutative operators satistify: (x op y) === (y op x)
120 ///
121 /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
122 /// applied to any type.
123 ///
124 bool Instruction::isCommutative(unsigned op) {
125 switch (op) {
126 case Add:
127 case Mul:
128 case And:
129 case Or:
130 case Xor:
131 case SetEQ:
132 case SetNE:
133 return true;
134 default:
135 return false;
136 }
137 }