@@ -2106,6 +2106,110 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP,
2106
2106
return nullptr ;
2107
2107
}
2108
2108
2109
+ Value *InstCombiner::getFreelyInvertedImpl (Value *V, bool WillInvertAllUses,
2110
+ BuilderTy *Builder,
2111
+ bool &DoesConsume, unsigned Depth) {
2112
+ static Value *const NonNull = reinterpret_cast <Value *>(uintptr_t (1 ));
2113
+ // ~(~(X)) -> X.
2114
+ Value *A, *B;
2115
+ if (match (V, m_Not (m_Value (A)))) {
2116
+ DoesConsume = true ;
2117
+ return A;
2118
+ }
2119
+
2120
+ Constant *C;
2121
+ // Constants can be considered to be not'ed values.
2122
+ if (match (V, m_ImmConstant (C)))
2123
+ return ConstantExpr::getNot (C);
2124
+
2125
+ if (Depth++ >= MaxAnalysisRecursionDepth)
2126
+ return nullptr ;
2127
+
2128
+ // The rest of the cases require that we invert all uses so don't bother
2129
+ // doing the analysis if we know we can't use the result.
2130
+ if (!WillInvertAllUses)
2131
+ return nullptr ;
2132
+
2133
+ // Compares can be inverted if all of their uses are being modified to use
2134
+ // the ~V.
2135
+ if (auto *I = dyn_cast<CmpInst>(V)) {
2136
+ if (Builder != nullptr )
2137
+ return Builder->CreateCmp (I->getInversePredicate (), I->getOperand (0 ),
2138
+ I->getOperand (1 ));
2139
+ return NonNull;
2140
+ }
2141
+
2142
+ // If `V` is of the form `A + B` then `-1 - V` can be folded into
2143
+ // `(-1 - B) - A` if we are willing to invert all of the uses.
2144
+ if (match (V, m_Add (m_Value (A), m_Value (B)))) {
2145
+ if (auto *BV = getFreelyInvertedImpl (B, B->hasOneUse (), Builder,
2146
+ DoesConsume, Depth))
2147
+ return Builder ? Builder->CreateSub (BV, A) : NonNull;
2148
+ if (auto *AV = getFreelyInvertedImpl (A, A->hasOneUse (), Builder,
2149
+ DoesConsume, Depth))
2150
+ return Builder ? Builder->CreateSub (AV, B) : NonNull;
2151
+ return nullptr ;
2152
+ }
2153
+
2154
+ // If `V` is of the form `A ^ ~B` then `~(A ^ ~B)` can be folded
2155
+ // into `A ^ B` if we are willing to invert all of the uses.
2156
+ if (match (V, m_Xor (m_Value (A), m_Value (B)))) {
2157
+ if (auto *BV = getFreelyInvertedImpl (B, B->hasOneUse (), Builder,
2158
+ DoesConsume, Depth))
2159
+ return Builder ? Builder->CreateXor (A, BV) : NonNull;
2160
+ if (auto *AV = getFreelyInvertedImpl (A, A->hasOneUse (), Builder,
2161
+ DoesConsume, Depth))
2162
+ return Builder ? Builder->CreateXor (AV, B) : NonNull;
2163
+ return nullptr ;
2164
+ }
2165
+
2166
+ // If `V` is of the form `B - A` then `-1 - V` can be folded into
2167
+ // `A + (-1 - B)` if we are willing to invert all of the uses.
2168
+ if (match (V, m_Sub (m_Value (A), m_Value (B)))) {
2169
+ if (auto *AV = getFreelyInvertedImpl (A, A->hasOneUse (), Builder,
2170
+ DoesConsume, Depth))
2171
+ return Builder ? Builder->CreateAdd (AV, B) : NonNull;
2172
+ return nullptr ;
2173
+ }
2174
+
2175
+ // If `V` is of the form `(~A) s>> B` then `~((~A) s>> B)` can be folded
2176
+ // into `A s>> B` if we are willing to invert all of the uses.
2177
+ if (match (V, m_AShr (m_Value (A), m_Value (B)))) {
2178
+ if (auto *AV = getFreelyInvertedImpl (A, A->hasOneUse (), Builder,
2179
+ DoesConsume, Depth))
2180
+ return Builder ? Builder->CreateAShr (AV, B) : NonNull;
2181
+ return nullptr ;
2182
+ }
2183
+
2184
+ Value *Cond;
2185
+ // LogicOps are special in that we canonicalize them at the cost of an
2186
+ // instruction.
2187
+ bool IsSelect = match (V, m_Select (m_Value (Cond), m_Value (A), m_Value (B))) &&
2188
+ !shouldAvoidAbsorbingNotIntoSelect (*cast<SelectInst>(V));
2189
+ // Selects/min/max with invertible operands are freely invertible
2190
+ if (IsSelect || match (V, m_MaxOrMin (m_Value (A), m_Value (B)))) {
2191
+ if (!getFreelyInvertedImpl (B, B->hasOneUse (), /* Builder*/ nullptr ,
2192
+ DoesConsume, Depth))
2193
+ return nullptr ;
2194
+ if (Value *NotA = getFreelyInvertedImpl (A, A->hasOneUse (), Builder,
2195
+ DoesConsume, Depth)) {
2196
+ if (Builder != nullptr ) {
2197
+ Value *NotB = getFreelyInvertedImpl (B, B->hasOneUse (), Builder,
2198
+ DoesConsume, Depth);
2199
+ assert (NotB != nullptr &&
2200
+ " Unable to build inverted value for known freely invertable op" );
2201
+ if (auto *II = dyn_cast<IntrinsicInst>(V))
2202
+ return Builder->CreateBinaryIntrinsic (
2203
+ getInverseMinMaxIntrinsic (II->getIntrinsicID ()), NotA, NotB);
2204
+ return Builder->CreateSelect (Cond, NotA, NotB);
2205
+ }
2206
+ return NonNull;
2207
+ }
2208
+ }
2209
+
2210
+ return nullptr ;
2211
+ }
2212
+
2109
2213
Instruction *InstCombinerImpl::visitGetElementPtrInst (GetElementPtrInst &GEP) {
2110
2214
Value *PtrOp = GEP.getOperand (0 );
2111
2215
SmallVector<Value *, 8 > Indices (GEP.indices ());
0 commit comments