Bug Summary

File:llvm/include/llvm/IR/PatternMatch.h
Warning:line 232, column 9
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name InstCombineAndOrXor.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/Transforms/InstCombine -I include -I /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-16-232930-107970-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

1//===- InstCombineAndOrXor.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the visitAnd, visitOr, and visitXor functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/Analysis/CmpInstAnalysis.h"
15#include "llvm/Analysis/InstructionSimplify.h"
16#include "llvm/IR/ConstantRange.h"
17#include "llvm/IR/Intrinsics.h"
18#include "llvm/IR/PatternMatch.h"
19#include "llvm/Transforms/InstCombine/InstCombiner.h"
20#include "llvm/Transforms/Utils/Local.h"
21
22using namespace llvm;
23using namespace PatternMatch;
24
25#define DEBUG_TYPE"instcombine" "instcombine"
26
27/// Similar to getICmpCode but for FCmpInst. This encodes a fcmp predicate into
28/// a four bit mask.
29static unsigned getFCmpCode(FCmpInst::Predicate CC) {
30 assert(FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE &&(static_cast <bool> (FCmpInst::FCMP_FALSE <= CC &&
CC <= FCmpInst::FCMP_TRUE && "Unexpected FCmp predicate!"
) ? void (0) : __assert_fail ("FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE && \"Unexpected FCmp predicate!\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 31
, __extension__ __PRETTY_FUNCTION__))
31 "Unexpected FCmp predicate!")(static_cast <bool> (FCmpInst::FCMP_FALSE <= CC &&
CC <= FCmpInst::FCMP_TRUE && "Unexpected FCmp predicate!"
) ? void (0) : __assert_fail ("FCmpInst::FCMP_FALSE <= CC && CC <= FCmpInst::FCMP_TRUE && \"Unexpected FCmp predicate!\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 31
, __extension__ __PRETTY_FUNCTION__))
;
32 // Take advantage of the bit pattern of FCmpInst::Predicate here.
33 // U L G E
34 static_assert(FCmpInst::FCMP_FALSE == 0, ""); // 0 0 0 0
35 static_assert(FCmpInst::FCMP_OEQ == 1, ""); // 0 0 0 1
36 static_assert(FCmpInst::FCMP_OGT == 2, ""); // 0 0 1 0
37 static_assert(FCmpInst::FCMP_OGE == 3, ""); // 0 0 1 1
38 static_assert(FCmpInst::FCMP_OLT == 4, ""); // 0 1 0 0
39 static_assert(FCmpInst::FCMP_OLE == 5, ""); // 0 1 0 1
40 static_assert(FCmpInst::FCMP_ONE == 6, ""); // 0 1 1 0
41 static_assert(FCmpInst::FCMP_ORD == 7, ""); // 0 1 1 1
42 static_assert(FCmpInst::FCMP_UNO == 8, ""); // 1 0 0 0
43 static_assert(FCmpInst::FCMP_UEQ == 9, ""); // 1 0 0 1
44 static_assert(FCmpInst::FCMP_UGT == 10, ""); // 1 0 1 0
45 static_assert(FCmpInst::FCMP_UGE == 11, ""); // 1 0 1 1
46 static_assert(FCmpInst::FCMP_ULT == 12, ""); // 1 1 0 0
47 static_assert(FCmpInst::FCMP_ULE == 13, ""); // 1 1 0 1
48 static_assert(FCmpInst::FCMP_UNE == 14, ""); // 1 1 1 0
49 static_assert(FCmpInst::FCMP_TRUE == 15, ""); // 1 1 1 1
50 return CC;
51}
52
53/// This is the complement of getICmpCode, which turns an opcode and two
54/// operands into either a constant true or false, or a brand new ICmp
55/// instruction. The sign is passed in to determine which kind of predicate to
56/// use in the new icmp instruction.
57static Value *getNewICmpValue(unsigned Code, bool Sign, Value *LHS, Value *RHS,
58 InstCombiner::BuilderTy &Builder) {
59 ICmpInst::Predicate NewPred;
60 if (Constant *TorF = getPredForICmpCode(Code, Sign, LHS->getType(), NewPred))
61 return TorF;
62 return Builder.CreateICmp(NewPred, LHS, RHS);
63}
64
65/// This is the complement of getFCmpCode, which turns an opcode and two
66/// operands into either a FCmp instruction, or a true/false constant.
67static Value *getFCmpValue(unsigned Code, Value *LHS, Value *RHS,
68 InstCombiner::BuilderTy &Builder) {
69 const auto Pred = static_cast<FCmpInst::Predicate>(Code);
70 assert(FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE &&(static_cast <bool> (FCmpInst::FCMP_FALSE <= Pred &&
Pred <= FCmpInst::FCMP_TRUE && "Unexpected FCmp predicate!"
) ? void (0) : __assert_fail ("FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE && \"Unexpected FCmp predicate!\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 71
, __extension__ __PRETTY_FUNCTION__))
71 "Unexpected FCmp predicate!")(static_cast <bool> (FCmpInst::FCMP_FALSE <= Pred &&
Pred <= FCmpInst::FCMP_TRUE && "Unexpected FCmp predicate!"
) ? void (0) : __assert_fail ("FCmpInst::FCMP_FALSE <= Pred && Pred <= FCmpInst::FCMP_TRUE && \"Unexpected FCmp predicate!\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 71
, __extension__ __PRETTY_FUNCTION__))
;
72 if (Pred == FCmpInst::FCMP_FALSE)
73 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
74 if (Pred == FCmpInst::FCMP_TRUE)
75 return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
76 return Builder.CreateFCmp(Pred, LHS, RHS);
77}
78
79/// Transform BITWISE_OP(BSWAP(A),BSWAP(B)) or
80/// BITWISE_OP(BSWAP(A), Constant) to BSWAP(BITWISE_OP(A, B))
81/// \param I Binary operator to transform.
82/// \return Pointer to node that must replace the original binary operator, or
83/// null pointer if no transformation was made.
84static Value *SimplifyBSwap(BinaryOperator &I,
85 InstCombiner::BuilderTy &Builder) {
86 assert(I.isBitwiseLogicOp() && "Unexpected opcode for bswap simplifying")(static_cast <bool> (I.isBitwiseLogicOp() && "Unexpected opcode for bswap simplifying"
) ? void (0) : __assert_fail ("I.isBitwiseLogicOp() && \"Unexpected opcode for bswap simplifying\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 86
, __extension__ __PRETTY_FUNCTION__))
;
87
88 Value *OldLHS = I.getOperand(0);
89 Value *OldRHS = I.getOperand(1);
90
91 Value *NewLHS;
92 if (!match(OldLHS, m_BSwap(m_Value(NewLHS))))
93 return nullptr;
94
95 Value *NewRHS;
96 const APInt *C;
97
98 if (match(OldRHS, m_BSwap(m_Value(NewRHS)))) {
99 // OP( BSWAP(x), BSWAP(y) ) -> BSWAP( OP(x, y) )
100 if (!OldLHS->hasOneUse() && !OldRHS->hasOneUse())
101 return nullptr;
102 // NewRHS initialized by the matcher.
103 } else if (match(OldRHS, m_APInt(C))) {
104 // OP( BSWAP(x), CONSTANT ) -> BSWAP( OP(x, BSWAP(CONSTANT) ) )
105 if (!OldLHS->hasOneUse())
106 return nullptr;
107 NewRHS = ConstantInt::get(I.getType(), C->byteSwap());
108 } else
109 return nullptr;
110
111 Value *BinOp = Builder.CreateBinOp(I.getOpcode(), NewLHS, NewRHS);
112 Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap,
113 I.getType());
114 return Builder.CreateCall(F, BinOp);
115}
116
117/// Emit a computation of: (V >= Lo && V < Hi) if Inside is true, otherwise
118/// (V < Lo || V >= Hi). This method expects that Lo < Hi. IsSigned indicates
119/// whether to treat V, Lo, and Hi as signed or not.
120Value *InstCombinerImpl::insertRangeTest(Value *V, const APInt &Lo,
121 const APInt &Hi, bool isSigned,
122 bool Inside) {
123 assert((isSigned ? Lo.slt(Hi) : Lo.ult(Hi)) &&(static_cast <bool> ((isSigned ? Lo.slt(Hi) : Lo.ult(Hi
)) && "Lo is not < Hi in range emission code!") ? void
(0) : __assert_fail ("(isSigned ? Lo.slt(Hi) : Lo.ult(Hi)) && \"Lo is not < Hi in range emission code!\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 124
, __extension__ __PRETTY_FUNCTION__))
124 "Lo is not < Hi in range emission code!")(static_cast <bool> ((isSigned ? Lo.slt(Hi) : Lo.ult(Hi
)) && "Lo is not < Hi in range emission code!") ? void
(0) : __assert_fail ("(isSigned ? Lo.slt(Hi) : Lo.ult(Hi)) && \"Lo is not < Hi in range emission code!\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 124
, __extension__ __PRETTY_FUNCTION__))
;
125
126 Type *Ty = V->getType();
127
128 // V >= Min && V < Hi --> V < Hi
129 // V < Min || V >= Hi --> V >= Hi
130 ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
131 if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
132 Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
133 return Builder.CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));
134 }
135
136 // V >= Lo && V < Hi --> V - Lo u< Hi - Lo
137 // V < Lo || V >= Hi --> V - Lo u>= Hi - Lo
138 Value *VMinusLo =
139 Builder.CreateSub(V, ConstantInt::get(Ty, Lo), V->getName() + ".off");
140 Constant *HiMinusLo = ConstantInt::get(Ty, Hi - Lo);
141 return Builder.CreateICmp(Pred, VMinusLo, HiMinusLo);
142}
143
144/// Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns
145/// that can be simplified.
146/// One of A and B is considered the mask. The other is the value. This is
147/// described as the "AMask" or "BMask" part of the enum. If the enum contains
148/// only "Mask", then both A and B can be considered masks. If A is the mask,
149/// then it was proven that (A & C) == C. This is trivial if C == A or C == 0.
150/// If both A and C are constants, this proof is also easy.
151/// For the following explanations, we assume that A is the mask.
152///
153/// "AllOnes" declares that the comparison is true only if (A & B) == A or all
154/// bits of A are set in B.
155/// Example: (icmp eq (A & 3), 3) -> AMask_AllOnes
156///
157/// "AllZeros" declares that the comparison is true only if (A & B) == 0 or all
158/// bits of A are cleared in B.
159/// Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes
160///
161/// "Mixed" declares that (A & B) == C and C might or might not contain any
162/// number of one bits and zero bits.
163/// Example: (icmp eq (A & 3), 1) -> AMask_Mixed
164///
165/// "Not" means that in above descriptions "==" should be replaced by "!=".
166/// Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes
167///
168/// If the mask A contains a single bit, then the following is equivalent:
169/// (icmp eq (A & B), A) equals (icmp ne (A & B), 0)
170/// (icmp ne (A & B), A) equals (icmp eq (A & B), 0)
171enum MaskedICmpType {
172 AMask_AllOnes = 1,
173 AMask_NotAllOnes = 2,
174 BMask_AllOnes = 4,
175 BMask_NotAllOnes = 8,
176 Mask_AllZeros = 16,
177 Mask_NotAllZeros = 32,
178 AMask_Mixed = 64,
179 AMask_NotMixed = 128,
180 BMask_Mixed = 256,
181 BMask_NotMixed = 512
182};
183
184/// Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C)
185/// satisfies.
186static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
187 ICmpInst::Predicate Pred) {
188 const APInt *ConstA = nullptr, *ConstB = nullptr, *ConstC = nullptr;
189 match(A, m_APInt(ConstA));
53
Passing null pointer value via 1st parameter 'V'
54
Calling 'match<llvm::Value, llvm::PatternMatch::apint_match>'
190 match(B, m_APInt(ConstB));
191 match(C, m_APInt(ConstC));
192 bool IsEq = (Pred == ICmpInst::ICMP_EQ);
193 bool IsAPow2 = ConstA && ConstA->isPowerOf2();
194 bool IsBPow2 = ConstB && ConstB->isPowerOf2();
195 unsigned MaskVal = 0;
196 if (ConstC && ConstC->isZero()) {
197 // if C is zero, then both A and B qualify as mask
198 MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed)
199 : (Mask_NotAllZeros | AMask_NotMixed | BMask_NotMixed));
200 if (IsAPow2)
201 MaskVal |= (IsEq ? (AMask_NotAllOnes | AMask_NotMixed)
202 : (AMask_AllOnes | AMask_Mixed));
203 if (IsBPow2)
204 MaskVal |= (IsEq ? (BMask_NotAllOnes | BMask_NotMixed)
205 : (BMask_AllOnes | BMask_Mixed));
206 return MaskVal;
207 }
208
209 if (A == C) {
210 MaskVal |= (IsEq ? (AMask_AllOnes | AMask_Mixed)
211 : (AMask_NotAllOnes | AMask_NotMixed));
212 if (IsAPow2)
213 MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed)
214 : (Mask_AllZeros | AMask_Mixed));
215 } else if (ConstA && ConstC && ConstC->isSubsetOf(*ConstA)) {
216 MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed);
217 }
218
219 if (B == C) {
220 MaskVal |= (IsEq ? (BMask_AllOnes | BMask_Mixed)
221 : (BMask_NotAllOnes | BMask_NotMixed));
222 if (IsBPow2)
223 MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed)
224 : (Mask_AllZeros | BMask_Mixed));
225 } else if (ConstB && ConstC && ConstC->isSubsetOf(*ConstB)) {
226 MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed);
227 }
228
229 return MaskVal;
230}
231
232/// Convert an analysis of a masked ICmp into its equivalent if all boolean
233/// operations had the opposite sense. Since each "NotXXX" flag (recording !=)
234/// is adjacent to the corresponding normal flag (recording ==), this just
235/// involves swapping those bits over.
236static unsigned conjugateICmpMask(unsigned Mask) {
237 unsigned NewMask;
238 NewMask = (Mask & (AMask_AllOnes | BMask_AllOnes | Mask_AllZeros |
239 AMask_Mixed | BMask_Mixed))
240 << 1;
241
242 NewMask |= (Mask & (AMask_NotAllOnes | BMask_NotAllOnes | Mask_NotAllZeros |
243 AMask_NotMixed | BMask_NotMixed))
244 >> 1;
245
246 return NewMask;
247}
248
249// Adapts the external decomposeBitTestICmp for local use.
250static bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
251 Value *&X, Value *&Y, Value *&Z) {
252 APInt Mask;
253 if (!llvm::decomposeBitTestICmp(LHS, RHS, Pred, X, Mask))
31
Assuming the condition is false
32
Taking false branch
254 return false;
255
256 Y = ConstantInt::get(X->getType(), Mask);
33
Value assigned to 'R12'
257 Z = ConstantInt::get(X->getType(), 0);
258 return true;
34
Returning the value 1, which participates in a condition later
259}
260
261/// Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).
262/// Return the pattern classes (from MaskedICmpType) for the left hand side and
263/// the right hand side as a pair.
264/// LHS and RHS are the left hand side and the right hand side ICmps and PredL
265/// and PredR are their predicates, respectively.
266static
267Optional<std::pair<unsigned, unsigned>>
268getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C,
269 Value *&D, Value *&E, ICmpInst *LHS,
270 ICmpInst *RHS,
271 ICmpInst::Predicate &PredL,
272 ICmpInst::Predicate &PredR) {
273 // Don't allow pointers. Splat vectors are fine.
274 if (!LHS->getOperand(0)->getType()->isIntOrIntVectorTy() ||
8
Calling 'Type::isIntOrIntVectorTy'
14
Returning from 'Type::isIntOrIntVectorTy'
22
Taking false branch
275 !RHS->getOperand(0)->getType()->isIntOrIntVectorTy())
15
Calling 'Type::isIntOrIntVectorTy'
21
Returning from 'Type::isIntOrIntVectorTy'
276 return None;
277
278 // Here comes the tricky part:
279 // LHS might be of the form L11 & L12 == X, X == L21 & L22,
280 // and L11 & L12 == L21 & L22. The same goes for RHS.
281 // Now we must find those components L** and R**, that are equal, so
282 // that we can extract the parameters A, B, C, D, and E for the canonical
283 // above.
284 Value *L1 = LHS->getOperand(0);
285 Value *L2 = LHS->getOperand(1);
286 Value *L11, *L12, *L21, *L22;
287 // Check whether the icmp can be decomposed into a bit test.
288 if (decomposeBitTestICmp(L1, L2, PredL, L11, L12, L2)) {
23
Taking true branch
289 L21 = L22 = L1 = nullptr;
290 } else {
291 // Look for ANDs in the LHS icmp.
292 if (!match(L1, m_And(m_Value(L11), m_Value(L12)))) {
293 // Any icmp can be viewed as being trivially masked; if it allows us to
294 // remove one, it's worth it.
295 L11 = L1;
296 L12 = Constant::getAllOnesValue(L1->getType());
297 }
298
299 if (!match(L2, m_And(m_Value(L21), m_Value(L22)))) {
300 L21 = L2;
301 L22 = Constant::getAllOnesValue(L2->getType());
302 }
303 }
304
305 // Bail if LHS was a icmp that can't be decomposed into an equality.
306 if (!ICmpInst::isEquality(PredL))
24
Calling 'ICmpInst::isEquality'
28
Returning from 'ICmpInst::isEquality'
29
Taking false branch
307 return None;
308
309 Value *R1 = RHS->getOperand(0);
310 Value *R2 = RHS->getOperand(1);
311 Value *R11, *R12;
312 bool Ok = false;
313 if (decomposeBitTestICmp(R1, R2, PredR, R11, R12, R2)) {
30
Calling 'decomposeBitTestICmp'
35
Returning from 'decomposeBitTestICmp'
314 if (R11 == L11 || R11 == L12 || R11
37.1
'R11' is not equal to 'L21'
37.1
'R11' is not equal to 'L21'
37.1
'R11' is not equal to 'L21'
37.1
'R11' is not equal to 'L21'
== L21 || R11
37.2
'R11' is not equal to 'L22'
37.2
'R11' is not equal to 'L22'
37.2
'R11' is not equal to 'L22'
37.2
'R11' is not equal to 'L22'
== L22) {
36
Assuming 'R11' is not equal to 'L11'
37
Assuming 'R11' is not equal to 'L12'
315 A = R11;
316 D = R12;
317 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
38
Assuming 'R12' is not equal to 'L11'
39
Assuming 'R12' is not equal to 'L12'
40
Assuming 'R12' is equal to 'L21'
318 A = R12;
41
Null pointer value stored to 'A'
319 D = R11;
320 } else {
321 return None;
322 }
323 E = R2;
324 R1 = nullptr;
325 Ok = true;
326 } else {
327 if (!match(R1, m_And(m_Value(R11), m_Value(R12)))) {
328 // As before, model no mask as a trivial mask if it'll let us do an
329 // optimization.
330 R11 = R1;
331 R12 = Constant::getAllOnesValue(R1->getType());
332 }
333
334 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
335 A = R11;
336 D = R12;
337 E = R2;
338 Ok = true;
339 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
340 A = R12;
341 D = R11;
342 E = R2;
343 Ok = true;
344 }
345 }
346
347 // Bail if RHS was a icmp that can't be decomposed into an equality.
348 if (!ICmpInst::isEquality(PredR))
42
Calling 'ICmpInst::isEquality'
45
Returning from 'ICmpInst::isEquality'
46
Taking false branch
349 return None;
350
351 // Look for ANDs on the right side of the RHS icmp.
352 if (!Ok
46.1
'Ok' is true
46.1
'Ok' is true
46.1
'Ok' is true
46.1
'Ok' is true
) {
47
Taking false branch
353 if (!match(R2, m_And(m_Value(R11), m_Value(R12)))) {
354 R11 = R2;
355 R12 = Constant::getAllOnesValue(R2->getType());
356 }
357
358 if (R11 == L11 || R11 == L12 || R11 == L21 || R11 == L22) {
359 A = R11;
360 D = R12;
361 E = R1;
362 Ok = true;
363 } else if (R12 == L11 || R12 == L12 || R12 == L21 || R12 == L22) {
364 A = R12;
365 D = R11;
366 E = R1;
367 Ok = true;
368 } else {
369 return None;
370 }
371
372 assert(Ok && "Failed to find AND on the right side of the RHS icmp.")(static_cast <bool> (Ok && "Failed to find AND on the right side of the RHS icmp."
) ? void (0) : __assert_fail ("Ok && \"Failed to find AND on the right side of the RHS icmp.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 372
, __extension__ __PRETTY_FUNCTION__))
;
373 }
374
375 if (L11
47.1
'L11' is not equal to 'A'
47.1
'L11' is not equal to 'A'
47.1
'L11' is not equal to 'A'
47.1
'L11' is not equal to 'A'
== A) {
48
Taking false branch
376 B = L12;
377 C = L2;
378 } else if (L12
48.1
'L12' is not equal to 'A'
48.1
'L12' is not equal to 'A'
48.1
'L12' is not equal to 'A'
48.1
'L12' is not equal to 'A'
== A) {
49
Taking false branch
379 B = L11;
380 C = L2;
381 } else if (L21
49.1
'L21' is equal to 'A'
49.1
'L21' is equal to 'A'
49.1
'L21' is equal to 'A'
49.1
'L21' is equal to 'A'
== A) {
50
Taking true branch
382 B = L22;
383 C = L1;
384 } else if (L22 == A) {
385 B = L21;
386 C = L1;
387 }
388
389 unsigned LeftType = getMaskedICmpType(A, B, C, PredL);
51
Passing null pointer value via 1st parameter 'A'
52
Calling 'getMaskedICmpType'
390 unsigned RightType = getMaskedICmpType(A, D, E, PredR);
391 return Optional<std::pair<unsigned, unsigned>>(std::make_pair(LeftType, RightType));
392}
393
394/// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single
395/// (icmp(A & X) ==/!= Y), where the left-hand side is of type Mask_NotAllZeros
396/// and the right hand side is of type BMask_Mixed. For example,
397/// (icmp (A & 12) != 0) & (icmp (A & 15) == 8) -> (icmp (A & 15) == 8).
398static Value *foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
399 ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, Value *A, Value *B, Value *C,
400 Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR,
401 InstCombiner::BuilderTy &Builder) {
402 // We are given the canonical form:
403 // (icmp ne (A & B), 0) & (icmp eq (A & D), E).
404 // where D & E == E.
405 //
406 // If IsAnd is false, we get it in negated form:
407 // (icmp eq (A & B), 0) | (icmp ne (A & D), E) ->
408 // !((icmp ne (A & B), 0) & (icmp eq (A & D), E)).
409 //
410 // We currently handle the case of B, C, D, E are constant.
411 //
412 ConstantInt *BCst, *CCst, *DCst, *ECst;
413 if (!match(B, m_ConstantInt(BCst)) || !match(C, m_ConstantInt(CCst)) ||
414 !match(D, m_ConstantInt(DCst)) || !match(E, m_ConstantInt(ECst)))
415 return nullptr;
416
417 ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
418
419 // Update E to the canonical form when D is a power of two and RHS is
420 // canonicalized as,
421 // (icmp ne (A & D), 0) -> (icmp eq (A & D), D) or
422 // (icmp ne (A & D), D) -> (icmp eq (A & D), 0).
423 if (PredR != NewCC)
424 ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst));
425
426 // If B or D is zero, skip because if LHS or RHS can be trivially folded by
427 // other folding rules and this pattern won't apply any more.
428 if (BCst->getValue() == 0 || DCst->getValue() == 0)
429 return nullptr;
430
431 // If B and D don't intersect, ie. (B & D) == 0, no folding because we can't
432 // deduce anything from it.
433 // For example,
434 // (icmp ne (A & 12), 0) & (icmp eq (A & 3), 1) -> no folding.
435 if ((BCst->getValue() & DCst->getValue()) == 0)
436 return nullptr;
437
438 // If the following two conditions are met:
439 //
440 // 1. mask B covers only a single bit that's not covered by mask D, that is,
441 // (B & (B ^ D)) is a power of 2 (in other words, B minus the intersection of
442 // B and D has only one bit set) and,
443 //
444 // 2. RHS (and E) indicates that the rest of B's bits are zero (in other
445 // words, the intersection of B and D is zero), that is, ((B & D) & E) == 0
446 //
447 // then that single bit in B must be one and thus the whole expression can be
448 // folded to
449 // (A & (B | D)) == (B & (B ^ D)) | E.
450 //
451 // For example,
452 // (icmp ne (A & 12), 0) & (icmp eq (A & 7), 1) -> (icmp eq (A & 15), 9)
453 // (icmp ne (A & 15), 0) & (icmp eq (A & 7), 0) -> (icmp eq (A & 15), 8)
454 if ((((BCst->getValue() & DCst->getValue()) & ECst->getValue()) == 0) &&
455 (BCst->getValue() & (BCst->getValue() ^ DCst->getValue())).isPowerOf2()) {
456 APInt BorD = BCst->getValue() | DCst->getValue();
457 APInt BandBxorDorE = (BCst->getValue() & (BCst->getValue() ^ DCst->getValue())) |
458 ECst->getValue();
459 Value *NewMask = ConstantInt::get(BCst->getType(), BorD);
460 Value *NewMaskedValue = ConstantInt::get(BCst->getType(), BandBxorDorE);
461 Value *NewAnd = Builder.CreateAnd(A, NewMask);
462 return Builder.CreateICmp(NewCC, NewAnd, NewMaskedValue);
463 }
464
465 auto IsSubSetOrEqual = [](ConstantInt *C1, ConstantInt *C2) {
466 return (C1->getValue() & C2->getValue()) == C1->getValue();
467 };
468 auto IsSuperSetOrEqual = [](ConstantInt *C1, ConstantInt *C2) {
469 return (C1->getValue() & C2->getValue()) == C2->getValue();
470 };
471
472 // In the following, we consider only the cases where B is a superset of D, B
473 // is a subset of D, or B == D because otherwise there's at least one bit
474 // covered by B but not D, in which case we can't deduce much from it, so
475 // no folding (aside from the single must-be-one bit case right above.)
476 // For example,
477 // (icmp ne (A & 14), 0) & (icmp eq (A & 3), 1) -> no folding.
478 if (!IsSubSetOrEqual(BCst, DCst) && !IsSuperSetOrEqual(BCst, DCst))
479 return nullptr;
480
481 // At this point, either B is a superset of D, B is a subset of D or B == D.
482
483 // If E is zero, if B is a subset of (or equal to) D, LHS and RHS contradict
484 // and the whole expression becomes false (or true if negated), otherwise, no
485 // folding.
486 // For example,
487 // (icmp ne (A & 3), 0) & (icmp eq (A & 7), 0) -> false.
488 // (icmp ne (A & 15), 0) & (icmp eq (A & 3), 0) -> no folding.
489 if (ECst->isZero()) {
490 if (IsSubSetOrEqual(BCst, DCst))
491 return ConstantInt::get(LHS->getType(), !IsAnd);
492 return nullptr;
493 }
494
495 // At this point, B, D, E aren't zero and (B & D) == B, (B & D) == D or B ==
496 // D. If B is a superset of (or equal to) D, since E is not zero, LHS is
497 // subsumed by RHS (RHS implies LHS.) So the whole expression becomes
498 // RHS. For example,
499 // (icmp ne (A & 255), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
500 // (icmp ne (A & 15), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
501 if (IsSuperSetOrEqual(BCst, DCst))
502 return RHS;
503 // Otherwise, B is a subset of D. If B and E have a common bit set,
504 // ie. (B & E) != 0, then LHS is subsumed by RHS. For example.
505 // (icmp ne (A & 12), 0) & (icmp eq (A & 15), 8) -> (icmp eq (A & 15), 8).
506 assert(IsSubSetOrEqual(BCst, DCst) && "Precondition due to above code")(static_cast <bool> (IsSubSetOrEqual(BCst, DCst) &&
"Precondition due to above code") ? void (0) : __assert_fail
("IsSubSetOrEqual(BCst, DCst) && \"Precondition due to above code\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 506
, __extension__ __PRETTY_FUNCTION__))
;
507 if ((BCst->getValue() & ECst->getValue()) != 0)
508 return RHS;
509 // Otherwise, LHS and RHS contradict and the whole expression becomes false
510 // (or true if negated.) For example,
511 // (icmp ne (A & 7), 0) & (icmp eq (A & 15), 8) -> false.
512 // (icmp ne (A & 6), 0) & (icmp eq (A & 15), 8) -> false.
513 return ConstantInt::get(LHS->getType(), !IsAnd);
514}
515
516/// Try to fold (icmp(A & B) ==/!= 0) &/| (icmp(A & D) ==/!= E) into a single
517/// (icmp(A & X) ==/!= Y), where the left-hand side and the right hand side
518/// aren't of the common mask pattern type.
519static Value *foldLogOpOfMaskedICmpsAsymmetric(
520 ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, Value *A, Value *B, Value *C,
521 Value *D, Value *E, ICmpInst::Predicate PredL, ICmpInst::Predicate PredR,
522 unsigned LHSMask, unsigned RHSMask, InstCombiner::BuilderTy &Builder) {
523 assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&(static_cast <bool> (ICmpInst::isEquality(PredL) &&
ICmpInst::isEquality(PredR) && "Expected equality predicates for masked type of icmps."
) ? void (0) : __assert_fail ("ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && \"Expected equality predicates for masked type of icmps.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 524
, __extension__ __PRETTY_FUNCTION__))
524 "Expected equality predicates for masked type of icmps.")(static_cast <bool> (ICmpInst::isEquality(PredL) &&
ICmpInst::isEquality(PredR) && "Expected equality predicates for masked type of icmps."
) ? void (0) : __assert_fail ("ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && \"Expected equality predicates for masked type of icmps.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 524
, __extension__ __PRETTY_FUNCTION__))
;
525 // Handle Mask_NotAllZeros-BMask_Mixed cases.
526 // (icmp ne/eq (A & B), C) &/| (icmp eq/ne (A & D), E), or
527 // (icmp eq/ne (A & B), C) &/| (icmp ne/eq (A & D), E)
528 // which gets swapped to
529 // (icmp ne/eq (A & D), E) &/| (icmp eq/ne (A & B), C).
530 if (!IsAnd) {
531 LHSMask = conjugateICmpMask(LHSMask);
532 RHSMask = conjugateICmpMask(RHSMask);
533 }
534 if ((LHSMask & Mask_NotAllZeros) && (RHSMask & BMask_Mixed)) {
535 if (Value *V = foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
536 LHS, RHS, IsAnd, A, B, C, D, E,
537 PredL, PredR, Builder)) {
538 return V;
539 }
540 } else if ((LHSMask & BMask_Mixed) && (RHSMask & Mask_NotAllZeros)) {
541 if (Value *V = foldLogOpOfMaskedICmps_NotAllZeros_BMask_Mixed(
542 RHS, LHS, IsAnd, A, D, E, B, C,
543 PredR, PredL, Builder)) {
544 return V;
545 }
546 }
547 return nullptr;
548}
549
550/// Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E)
551/// into a single (icmp(A & X) ==/!= Y).
552static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
553 InstCombiner::BuilderTy &Builder) {
554 Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr, *E = nullptr;
5
'A' initialized to a null pointer value
555 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
556 Optional<std::pair<unsigned, unsigned>> MaskPair =
557 getMaskedTypeForICmpPair(A, B, C, D, E, LHS, RHS, PredL, PredR);
6
Passing value via 1st parameter 'A'
7
Calling 'getMaskedTypeForICmpPair'
558 if (!MaskPair)
559 return nullptr;
560 assert(ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) &&(static_cast <bool> (ICmpInst::isEquality(PredL) &&
ICmpInst::isEquality(PredR) && "Expected equality predicates for masked type of icmps."
) ? void (0) : __assert_fail ("ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && \"Expected equality predicates for masked type of icmps.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 561
, __extension__ __PRETTY_FUNCTION__))
561 "Expected equality predicates for masked type of icmps.")(static_cast <bool> (ICmpInst::isEquality(PredL) &&
ICmpInst::isEquality(PredR) && "Expected equality predicates for masked type of icmps."
) ? void (0) : __assert_fail ("ICmpInst::isEquality(PredL) && ICmpInst::isEquality(PredR) && \"Expected equality predicates for masked type of icmps.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 561
, __extension__ __PRETTY_FUNCTION__))
;
562 unsigned LHSMask = MaskPair->first;
563 unsigned RHSMask = MaskPair->second;
564 unsigned Mask = LHSMask & RHSMask;
565 if (Mask == 0) {
566 // Even if the two sides don't share a common pattern, check if folding can
567 // still happen.
568 if (Value *V = foldLogOpOfMaskedICmpsAsymmetric(
569 LHS, RHS, IsAnd, A, B, C, D, E, PredL, PredR, LHSMask, RHSMask,
570 Builder))
571 return V;
572 return nullptr;
573 }
574
575 // In full generality:
576 // (icmp (A & B) Op C) | (icmp (A & D) Op E)
577 // == ![ (icmp (A & B) !Op C) & (icmp (A & D) !Op E) ]
578 //
579 // If the latter can be converted into (icmp (A & X) Op Y) then the former is
580 // equivalent to (icmp (A & X) !Op Y).
581 //
582 // Therefore, we can pretend for the rest of this function that we're dealing
583 // with the conjunction, provided we flip the sense of any comparisons (both
584 // input and output).
585
586 // In most cases we're going to produce an EQ for the "&&" case.
587 ICmpInst::Predicate NewCC = IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
588 if (!IsAnd) {
589 // Convert the masking analysis into its equivalent with negated
590 // comparisons.
591 Mask = conjugateICmpMask(Mask);
592 }
593
594 if (Mask & Mask_AllZeros) {
595 // (icmp eq (A & B), 0) & (icmp eq (A & D), 0)
596 // -> (icmp eq (A & (B|D)), 0)
597 Value *NewOr = Builder.CreateOr(B, D);
598 Value *NewAnd = Builder.CreateAnd(A, NewOr);
599 // We can't use C as zero because we might actually handle
600 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
601 // with B and D, having a single bit set.
602 Value *Zero = Constant::getNullValue(A->getType());
603 return Builder.CreateICmp(NewCC, NewAnd, Zero);
604 }
605 if (Mask & BMask_AllOnes) {
606 // (icmp eq (A & B), B) & (icmp eq (A & D), D)
607 // -> (icmp eq (A & (B|D)), (B|D))
608 Value *NewOr = Builder.CreateOr(B, D);
609 Value *NewAnd = Builder.CreateAnd(A, NewOr);
610 return Builder.CreateICmp(NewCC, NewAnd, NewOr);
611 }
612 if (Mask & AMask_AllOnes) {
613 // (icmp eq (A & B), A) & (icmp eq (A & D), A)
614 // -> (icmp eq (A & (B&D)), A)
615 Value *NewAnd1 = Builder.CreateAnd(B, D);
616 Value *NewAnd2 = Builder.CreateAnd(A, NewAnd1);
617 return Builder.CreateICmp(NewCC, NewAnd2, A);
618 }
619
620 // Remaining cases assume at least that B and D are constant, and depend on
621 // their actual values. This isn't strictly necessary, just a "handle the
622 // easy cases for now" decision.
623 const APInt *ConstB, *ConstD;
624 if (!match(B, m_APInt(ConstB)) || !match(D, m_APInt(ConstD)))
625 return nullptr;
626
627 if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) {
628 // (icmp ne (A & B), 0) & (icmp ne (A & D), 0) and
629 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
630 // -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
631 // Only valid if one of the masks is a superset of the other (check "B&D" is
632 // the same as either B or D).
633 APInt NewMask = *ConstB & *ConstD;
634 if (NewMask == *ConstB)
635 return LHS;
636 else if (NewMask == *ConstD)
637 return RHS;
638 }
639
640 if (Mask & AMask_NotAllOnes) {
641 // (icmp ne (A & B), B) & (icmp ne (A & D), D)
642 // -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
643 // Only valid if one of the masks is a superset of the other (check "B|D" is
644 // the same as either B or D).
645 APInt NewMask = *ConstB | *ConstD;
646 if (NewMask == *ConstB)
647 return LHS;
648 else if (NewMask == *ConstD)
649 return RHS;
650 }
651
652 if (Mask & BMask_Mixed) {
653 // (icmp eq (A & B), C) & (icmp eq (A & D), E)
654 // We already know that B & C == C && D & E == E.
655 // If we can prove that (B & D) & (C ^ E) == 0, that is, the bits of
656 // C and E, which are shared by both the mask B and the mask D, don't
657 // contradict, then we can transform to
658 // -> (icmp eq (A & (B|D)), (C|E))
659 // Currently, we only handle the case of B, C, D, and E being constant.
660 // We can't simply use C and E because we might actually handle
661 // (icmp ne (A & B), B) & (icmp eq (A & D), D)
662 // with B and D, having a single bit set.
663 const APInt *OldConstC, *OldConstE;
664 if (!match(C, m_APInt(OldConstC)) || !match(E, m_APInt(OldConstE)))
665 return nullptr;
666
667 const APInt ConstC = PredL != NewCC ? *ConstB ^ *OldConstC : *OldConstC;
668 const APInt ConstE = PredR != NewCC ? *ConstD ^ *OldConstE : *OldConstE;
669
670 // If there is a conflict, we should actually return a false for the
671 // whole construct.
672 if (((*ConstB & *ConstD) & (ConstC ^ ConstE)).getBoolValue())
673 return ConstantInt::get(LHS->getType(), !IsAnd);
674
675 Value *NewOr1 = Builder.CreateOr(B, D);
676 Value *NewAnd = Builder.CreateAnd(A, NewOr1);
677 Constant *NewOr2 = ConstantInt::get(A->getType(), ConstC | ConstE);
678 return Builder.CreateICmp(NewCC, NewAnd, NewOr2);
679 }
680
681 return nullptr;
682}
683
684/// Try to fold a signed range checked with lower bound 0 to an unsigned icmp.
685/// Example: (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
686/// If \p Inverted is true then the check is for the inverted range, e.g.
687/// (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
688Value *InstCombinerImpl::simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1,
689 bool Inverted) {
690 // Check the lower range comparison, e.g. x >= 0
691 // InstCombine already ensured that if there is a constant it's on the RHS.
692 ConstantInt *RangeStart = dyn_cast<ConstantInt>(Cmp0->getOperand(1));
693 if (!RangeStart)
694 return nullptr;
695
696 ICmpInst::Predicate Pred0 = (Inverted ? Cmp0->getInversePredicate() :
697 Cmp0->getPredicate());
698
699 // Accept x > -1 or x >= 0 (after potentially inverting the predicate).
700 if (!((Pred0 == ICmpInst::ICMP_SGT && RangeStart->isMinusOne()) ||
701 (Pred0 == ICmpInst::ICMP_SGE && RangeStart->isZero())))
702 return nullptr;
703
704 ICmpInst::Predicate Pred1 = (Inverted ? Cmp1->getInversePredicate() :
705 Cmp1->getPredicate());
706
707 Value *Input = Cmp0->getOperand(0);
708 Value *RangeEnd;
709 if (Cmp1->getOperand(0) == Input) {
710 // For the upper range compare we have: icmp x, n
711 RangeEnd = Cmp1->getOperand(1);
712 } else if (Cmp1->getOperand(1) == Input) {
713 // For the upper range compare we have: icmp n, x
714 RangeEnd = Cmp1->getOperand(0);
715 Pred1 = ICmpInst::getSwappedPredicate(Pred1);
716 } else {
717 return nullptr;
718 }
719
720 // Check the upper range comparison, e.g. x < n
721 ICmpInst::Predicate NewPred;
722 switch (Pred1) {
723 case ICmpInst::ICMP_SLT: NewPred = ICmpInst::ICMP_ULT; break;
724 case ICmpInst::ICMP_SLE: NewPred = ICmpInst::ICMP_ULE; break;
725 default: return nullptr;
726 }
727
728 // This simplification is only valid if the upper range is not negative.
729 KnownBits Known = computeKnownBits(RangeEnd, /*Depth=*/0, Cmp1);
730 if (!Known.isNonNegative())
731 return nullptr;
732
733 if (Inverted)
734 NewPred = ICmpInst::getInversePredicate(NewPred);
735
736 return Builder.CreateICmp(NewPred, Input, RangeEnd);
737}
738
739static Value *
740foldAndOrOfEqualityCmpsWithConstants(ICmpInst *LHS, ICmpInst *RHS,
741 bool JoinedByAnd,
742 InstCombiner::BuilderTy &Builder) {
743 Value *X = LHS->getOperand(0);
744 if (X != RHS->getOperand(0))
745 return nullptr;
746
747 const APInt *C1, *C2;
748 if (!match(LHS->getOperand(1), m_APInt(C1)) ||
749 !match(RHS->getOperand(1), m_APInt(C2)))
750 return nullptr;
751
752 // We only handle (X != C1 && X != C2) and (X == C1 || X == C2).
753 ICmpInst::Predicate Pred = LHS->getPredicate();
754 if (Pred != RHS->getPredicate())
755 return nullptr;
756 if (JoinedByAnd && Pred != ICmpInst::ICMP_NE)
757 return nullptr;
758 if (!JoinedByAnd && Pred != ICmpInst::ICMP_EQ)
759 return nullptr;
760
761 // The larger unsigned constant goes on the right.
762 if (C1->ugt(*C2))
763 std::swap(C1, C2);
764
765 APInt Xor = *C1 ^ *C2;
766 if (Xor.isPowerOf2()) {
767 // If LHSC and RHSC differ by only one bit, then set that bit in X and
768 // compare against the larger constant:
769 // (X == C1 || X == C2) --> (X | (C1 ^ C2)) == C2
770 // (X != C1 && X != C2) --> (X | (C1 ^ C2)) != C2
771 // We choose an 'or' with a Pow2 constant rather than the inverse mask with
772 // 'and' because that may lead to smaller codegen from a smaller constant.
773 Value *Or = Builder.CreateOr(X, ConstantInt::get(X->getType(), Xor));
774 return Builder.CreateICmp(Pred, Or, ConstantInt::get(X->getType(), *C2));
775 }
776
777 return nullptr;
778}
779
780// Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
781// Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2)
782Value *InstCombinerImpl::foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS,
783 ICmpInst *RHS,
784 Instruction *CxtI,
785 bool IsAnd,
786 bool IsLogical) {
787 CmpInst::Predicate Pred = IsAnd ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
788 if (LHS->getPredicate() != Pred || RHS->getPredicate() != Pred)
789 return nullptr;
790
791 if (!match(LHS->getOperand(1), m_Zero()) ||
792 !match(RHS->getOperand(1), m_Zero()))
793 return nullptr;
794
795 Value *L1, *L2, *R1, *R2;
796 if (match(LHS->getOperand(0), m_And(m_Value(L1), m_Value(L2))) &&
797 match(RHS->getOperand(0), m_And(m_Value(R1), m_Value(R2)))) {
798 if (L1 == R2 || L2 == R2)
799 std::swap(R1, R2);
800 if (L2 == R1)
801 std::swap(L1, L2);
802
803 if (L1 == R1 &&
804 isKnownToBeAPowerOfTwo(L2, false, 0, CxtI) &&
805 isKnownToBeAPowerOfTwo(R2, false, 0, CxtI)) {
806 // If this is a logical and/or, then we must prevent propagation of a
807 // poison value from the RHS by inserting freeze.
808 if (IsLogical)
809 R2 = Builder.CreateFreeze(R2);
810 Value *Mask = Builder.CreateOr(L2, R2);
811 Value *Masked = Builder.CreateAnd(L1, Mask);
812 auto NewPred = IsAnd ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
813 return Builder.CreateICmp(NewPred, Masked, Mask);
814 }
815 }
816
817 return nullptr;
818}
819
820/// General pattern:
821/// X & Y
822///
823/// Where Y is checking that all the high bits (covered by a mask 4294967168)
824/// are uniform, i.e. %arg & 4294967168 can be either 4294967168 or 0
825/// Pattern can be one of:
826/// %t = add i32 %arg, 128
827/// %r = icmp ult i32 %t, 256
828/// Or
829/// %t0 = shl i32 %arg, 24
830/// %t1 = ashr i32 %t0, 24
831/// %r = icmp eq i32 %t1, %arg
832/// Or
833/// %t0 = trunc i32 %arg to i8
834/// %t1 = sext i8 %t0 to i32
835/// %r = icmp eq i32 %t1, %arg
836/// This pattern is a signed truncation check.
837///
838/// And X is checking that some bit in that same mask is zero.
839/// I.e. can be one of:
840/// %r = icmp sgt i32 %arg, -1
841/// Or
842/// %t = and i32 %arg, 2147483648
843/// %r = icmp eq i32 %t, 0
844///
845/// Since we are checking that all the bits in that mask are the same,
846/// and a particular bit is zero, what we are really checking is that all the
847/// masked bits are zero.
848/// So this should be transformed to:
849/// %r = icmp ult i32 %arg, 128
850static Value *foldSignedTruncationCheck(ICmpInst *ICmp0, ICmpInst *ICmp1,
851 Instruction &CxtI,
852 InstCombiner::BuilderTy &Builder) {
853 assert(CxtI.getOpcode() == Instruction::And)(static_cast <bool> (CxtI.getOpcode() == Instruction::And
) ? void (0) : __assert_fail ("CxtI.getOpcode() == Instruction::And"
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 853
, __extension__ __PRETTY_FUNCTION__))
;
854
855 // Match icmp ult (add %arg, C01), C1 (C1 == C01 << 1; powers of two)
856 auto tryToMatchSignedTruncationCheck = [](ICmpInst *ICmp, Value *&X,
857 APInt &SignBitMask) -> bool {
858 CmpInst::Predicate Pred;
859 const APInt *I01, *I1; // powers of two; I1 == I01 << 1
860 if (!(match(ICmp,
861 m_ICmp(Pred, m_Add(m_Value(X), m_Power2(I01)), m_Power2(I1))) &&
862 Pred == ICmpInst::ICMP_ULT && I1->ugt(*I01) && I01->shl(1) == *I1))
863 return false;
864 // Which bit is the new sign bit as per the 'signed truncation' pattern?
865 SignBitMask = *I01;
866 return true;
867 };
868
869 // One icmp needs to be 'signed truncation check'.
870 // We need to match this first, else we will mismatch commutative cases.
871 Value *X1;
872 APInt HighestBit;
873 ICmpInst *OtherICmp;
874 if (tryToMatchSignedTruncationCheck(ICmp1, X1, HighestBit))
875 OtherICmp = ICmp0;
876 else if (tryToMatchSignedTruncationCheck(ICmp0, X1, HighestBit))
877 OtherICmp = ICmp1;
878 else
879 return nullptr;
880
881 assert(HighestBit.isPowerOf2() && "expected to be power of two (non-zero)")(static_cast <bool> (HighestBit.isPowerOf2() &&
"expected to be power of two (non-zero)") ? void (0) : __assert_fail
("HighestBit.isPowerOf2() && \"expected to be power of two (non-zero)\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 881
, __extension__ __PRETTY_FUNCTION__))
;
882
883 // Try to match/decompose into: icmp eq (X & Mask), 0
884 auto tryToDecompose = [](ICmpInst *ICmp, Value *&X,
885 APInt &UnsetBitsMask) -> bool {
886 CmpInst::Predicate Pred = ICmp->getPredicate();
887 // Can it be decomposed into icmp eq (X & Mask), 0 ?
888 if (llvm::decomposeBitTestICmp(ICmp->getOperand(0), ICmp->getOperand(1),
889 Pred, X, UnsetBitsMask,
890 /*LookThroughTrunc=*/false) &&
891 Pred == ICmpInst::ICMP_EQ)
892 return true;
893 // Is it icmp eq (X & Mask), 0 already?
894 const APInt *Mask;
895 if (match(ICmp, m_ICmp(Pred, m_And(m_Value(X), m_APInt(Mask)), m_Zero())) &&
896 Pred == ICmpInst::ICMP_EQ) {
897 UnsetBitsMask = *Mask;
898 return true;
899 }
900 return false;
901 };
902
903 // And the other icmp needs to be decomposable into a bit test.
904 Value *X0;
905 APInt UnsetBitsMask;
906 if (!tryToDecompose(OtherICmp, X0, UnsetBitsMask))
907 return nullptr;
908
909 assert(!UnsetBitsMask.isZero() && "empty mask makes no sense.")(static_cast <bool> (!UnsetBitsMask.isZero() &&
"empty mask makes no sense.") ? void (0) : __assert_fail ("!UnsetBitsMask.isZero() && \"empty mask makes no sense.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 909
, __extension__ __PRETTY_FUNCTION__))
;
910
911 // Are they working on the same value?
912 Value *X;
913 if (X1 == X0) {
914 // Ok as is.
915 X = X1;
916 } else if (match(X0, m_Trunc(m_Specific(X1)))) {
917 UnsetBitsMask = UnsetBitsMask.zext(X1->getType()->getScalarSizeInBits());
918 X = X1;
919 } else
920 return nullptr;
921
922 // So which bits should be uniform as per the 'signed truncation check'?
923 // (all the bits starting with (i.e. including) HighestBit)
924 APInt SignBitsMask = ~(HighestBit - 1U);
925
926 // UnsetBitsMask must have some common bits with SignBitsMask,
927 if (!UnsetBitsMask.intersects(SignBitsMask))
928 return nullptr;
929
930 // Does UnsetBitsMask contain any bits outside of SignBitsMask?
931 if (!UnsetBitsMask.isSubsetOf(SignBitsMask)) {
932 APInt OtherHighestBit = (~UnsetBitsMask) + 1U;
933 if (!OtherHighestBit.isPowerOf2())
934 return nullptr;
935 HighestBit = APIntOps::umin(HighestBit, OtherHighestBit);
936 }
937 // Else, if it does not, then all is ok as-is.
938
939 // %r = icmp ult %X, SignBit
940 return Builder.CreateICmpULT(X, ConstantInt::get(X->getType(), HighestBit),
941 CxtI.getName() + ".simplified");
942}
943
944/// Reduce a pair of compares that check if a value has exactly 1 bit set.
945static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
946 InstCombiner::BuilderTy &Builder) {
947 // Handle 'and' / 'or' commutation: make the equality check the first operand.
948 if (JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_NE)
949 std::swap(Cmp0, Cmp1);
950 else if (!JoinedByAnd && Cmp1->getPredicate() == ICmpInst::ICMP_EQ)
951 std::swap(Cmp0, Cmp1);
952
953 // (X != 0) && (ctpop(X) u< 2) --> ctpop(X) == 1
954 CmpInst::Predicate Pred0, Pred1;
955 Value *X;
956 if (JoinedByAnd && match(Cmp0, m_ICmp(Pred0, m_Value(X), m_ZeroInt())) &&
957 match(Cmp1, m_ICmp(Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
958 m_SpecificInt(2))) &&
959 Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT) {
960 Value *CtPop = Cmp1->getOperand(0);
961 return Builder.CreateICmpEQ(CtPop, ConstantInt::get(CtPop->getType(), 1));
962 }
963 // (X == 0) || (ctpop(X) u> 1) --> ctpop(X) != 1
964 if (!JoinedByAnd && match(Cmp0, m_ICmp(Pred0, m_Value(X), m_ZeroInt())) &&
965 match(Cmp1, m_ICmp(Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific(X)),
966 m_SpecificInt(1))) &&
967 Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_UGT) {
968 Value *CtPop = Cmp1->getOperand(0);
969 return Builder.CreateICmpNE(CtPop, ConstantInt::get(CtPop->getType(), 1));
970 }
971 return nullptr;
972}
973
974/// Commuted variants are assumed to be handled by calling this function again
975/// with the parameters swapped.
976static Value *foldUnsignedUnderflowCheck(ICmpInst *ZeroICmp,
977 ICmpInst *UnsignedICmp, bool IsAnd,
978 const SimplifyQuery &Q,
979 InstCombiner::BuilderTy &Builder) {
980 Value *ZeroCmpOp;
981 ICmpInst::Predicate EqPred;
982 if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(ZeroCmpOp), m_Zero())) ||
983 !ICmpInst::isEquality(EqPred))
984 return nullptr;
985
986 auto IsKnownNonZero = [&](Value *V) {
987 return isKnownNonZero(V, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
988 };
989
990 ICmpInst::Predicate UnsignedPred;
991
992 Value *A, *B;
993 if (match(UnsignedICmp,
994 m_c_ICmp(UnsignedPred, m_Specific(ZeroCmpOp), m_Value(A))) &&
995 match(ZeroCmpOp, m_c_Add(m_Specific(A), m_Value(B))) &&
996 (ZeroICmp->hasOneUse() || UnsignedICmp->hasOneUse())) {
997 auto GetKnownNonZeroAndOther = [&](Value *&NonZero, Value *&Other) {
998 if (!IsKnownNonZero(NonZero))
999 std::swap(NonZero, Other);
1000 return IsKnownNonZero(NonZero);
1001 };
1002
1003 // Given ZeroCmpOp = (A + B)
1004 // ZeroCmpOp <= A && ZeroCmpOp != 0 --> (0-B) < A
1005 // ZeroCmpOp > A || ZeroCmpOp == 0 --> (0-B) >= A
1006 //
1007 // ZeroCmpOp < A && ZeroCmpOp != 0 --> (0-X) < Y iff
1008 // ZeroCmpOp >= A || ZeroCmpOp == 0 --> (0-X) >= Y iff
1009 // with X being the value (A/B) that is known to be non-zero,
1010 // and Y being remaining value.
1011 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1012 IsAnd)
1013 return Builder.CreateICmpULT(Builder.CreateNeg(B), A);
1014 if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE &&
1015 IsAnd && GetKnownNonZeroAndOther(B, A))
1016 return Builder.CreateICmpULT(Builder.CreateNeg(B), A);
1017 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1018 !IsAnd)
1019 return Builder.CreateICmpUGE(Builder.CreateNeg(B), A);
1020 if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ &&
1021 !IsAnd && GetKnownNonZeroAndOther(B, A))
1022 return Builder.CreateICmpUGE(Builder.CreateNeg(B), A);
1023 }
1024
1025 Value *Base, *Offset;
1026 if (!match(ZeroCmpOp, m_Sub(m_Value(Base), m_Value(Offset))))
1027 return nullptr;
1028
1029 if (!match(UnsignedICmp,
1030 m_c_ICmp(UnsignedPred, m_Specific(Base), m_Specific(Offset))) ||
1031 !ICmpInst::isUnsigned(UnsignedPred))
1032 return nullptr;
1033
1034 // Base >=/> Offset && (Base - Offset) != 0 <--> Base > Offset
1035 // (no overflow and not null)
1036 if ((UnsignedPred == ICmpInst::ICMP_UGE ||
1037 UnsignedPred == ICmpInst::ICMP_UGT) &&
1038 EqPred == ICmpInst::ICMP_NE && IsAnd)
1039 return Builder.CreateICmpUGT(Base, Offset);
1040
1041 // Base <=/< Offset || (Base - Offset) == 0 <--> Base <= Offset
1042 // (overflow or null)
1043 if ((UnsignedPred == ICmpInst::ICMP_ULE ||
1044 UnsignedPred == ICmpInst::ICMP_ULT) &&
1045 EqPred == ICmpInst::ICMP_EQ && !IsAnd)
1046 return Builder.CreateICmpULE(Base, Offset);
1047
1048 // Base <= Offset && (Base - Offset) != 0 --> Base < Offset
1049 if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
1050 IsAnd)
1051 return Builder.CreateICmpULT(Base, Offset);
1052
1053 // Base > Offset || (Base - Offset) == 0 --> Base >= Offset
1054 if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
1055 !IsAnd)
1056 return Builder.CreateICmpUGE(Base, Offset);
1057
1058 return nullptr;
1059}
1060
1061struct IntPart {
1062 Value *From;
1063 unsigned StartBit;
1064 unsigned NumBits;
1065};
1066
1067/// Match an extraction of bits from an integer.
1068static Optional<IntPart> matchIntPart(Value *V) {
1069 Value *X;
1070 if (!match(V, m_OneUse(m_Trunc(m_Value(X)))))
1071 return None;
1072
1073 unsigned NumOriginalBits = X->getType()->getScalarSizeInBits();
1074 unsigned NumExtractedBits = V->getType()->getScalarSizeInBits();
1075 Value *Y;
1076 const APInt *Shift;
1077 // For a trunc(lshr Y, Shift) pattern, make sure we're only extracting bits
1078 // from Y, not any shifted-in zeroes.
1079 if (match(X, m_OneUse(m_LShr(m_Value(Y), m_APInt(Shift)))) &&
1080 Shift->ule(NumOriginalBits - NumExtractedBits))
1081 return {{Y, (unsigned)Shift->getZExtValue(), NumExtractedBits}};
1082 return {{X, 0, NumExtractedBits}};
1083}
1084
1085/// Materialize an extraction of bits from an integer in IR.
1086static Value *extractIntPart(const IntPart &P, IRBuilderBase &Builder) {
1087 Value *V = P.From;
1088 if (P.StartBit)
1089 V = Builder.CreateLShr(V, P.StartBit);
1090 Type *TruncTy = V->getType()->getWithNewBitWidth(P.NumBits);
1091 if (TruncTy != V->getType())
1092 V = Builder.CreateTrunc(V, TruncTy);
1093 return V;
1094}
1095
1096/// (icmp eq X0, Y0) & (icmp eq X1, Y1) -> icmp eq X01, Y01
1097/// (icmp ne X0, Y0) | (icmp ne X1, Y1) -> icmp ne X01, Y01
1098/// where X0, X1 and Y0, Y1 are adjacent parts extracted from an integer.
1099Value *InstCombinerImpl::foldEqOfParts(ICmpInst *Cmp0, ICmpInst *Cmp1,
1100 bool IsAnd) {
1101 if (!Cmp0->hasOneUse() || !Cmp1->hasOneUse())
1102 return nullptr;
1103
1104 CmpInst::Predicate Pred = IsAnd ? CmpInst::ICMP_EQ : CmpInst::ICMP_NE;
1105 if (Cmp0->getPredicate() != Pred || Cmp1->getPredicate() != Pred)
1106 return nullptr;
1107
1108 Optional<IntPart> L0 = matchIntPart(Cmp0->getOperand(0));
1109 Optional<IntPart> R0 = matchIntPart(Cmp0->getOperand(1));
1110 Optional<IntPart> L1 = matchIntPart(Cmp1->getOperand(0));
1111 Optional<IntPart> R1 = matchIntPart(Cmp1->getOperand(1));
1112 if (!L0 || !R0 || !L1 || !R1)
1113 return nullptr;
1114
1115 // Make sure the LHS/RHS compare a part of the same value, possibly after
1116 // an operand swap.
1117 if (L0->From != L1->From || R0->From != R1->From) {
1118 if (L0->From != R1->From || R0->From != L1->From)
1119 return nullptr;
1120 std::swap(L1, R1);
1121 }
1122
1123 // Make sure the extracted parts are adjacent, canonicalizing to L0/R0 being
1124 // the low part and L1/R1 being the high part.
1125 if (L0->StartBit + L0->NumBits != L1->StartBit ||
1126 R0->StartBit + R0->NumBits != R1->StartBit) {
1127 if (L1->StartBit + L1->NumBits != L0->StartBit ||
1128 R1->StartBit + R1->NumBits != R0->StartBit)
1129 return nullptr;
1130 std::swap(L0, L1);
1131 std::swap(R0, R1);
1132 }
1133
1134 // We can simplify to a comparison of these larger parts of the integers.
1135 IntPart L = {L0->From, L0->StartBit, L0->NumBits + L1->NumBits};
1136 IntPart R = {R0->From, R0->StartBit, R0->NumBits + R1->NumBits};
1137 Value *LValue = extractIntPart(L, Builder);
1138 Value *RValue = extractIntPart(R, Builder);
1139 return Builder.CreateICmp(Pred, LValue, RValue);
1140}
1141
1142/// Reduce logic-of-compares with equality to a constant by substituting a
1143/// common operand with the constant. Callers are expected to call this with
1144/// Cmp0/Cmp1 switched to handle logic op commutativity.
1145static Value *foldAndOrOfICmpsWithConstEq(ICmpInst *Cmp0, ICmpInst *Cmp1,
1146 BinaryOperator &Logic,
1147 InstCombiner::BuilderTy &Builder,
1148 const SimplifyQuery &Q) {
1149 bool IsAnd = Logic.getOpcode() == Instruction::And;
1150 assert((IsAnd || Logic.getOpcode() == Instruction::Or) && "Wrong logic op")(static_cast <bool> ((IsAnd || Logic.getOpcode() == Instruction
::Or) && "Wrong logic op") ? void (0) : __assert_fail
("(IsAnd || Logic.getOpcode() == Instruction::Or) && \"Wrong logic op\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1150
, __extension__ __PRETTY_FUNCTION__))
;
1151
1152 // Match an equality compare with a non-poison constant as Cmp0.
1153 // Also, give up if the compare can be constant-folded to avoid looping.
1154 ICmpInst::Predicate Pred0;
1155 Value *X;
1156 Constant *C;
1157 if (!match(Cmp0, m_ICmp(Pred0, m_Value(X), m_Constant(C))) ||
1158 !isGuaranteedNotToBeUndefOrPoison(C) || isa<Constant>(X))
1159 return nullptr;
1160 if ((IsAnd && Pred0 != ICmpInst::ICMP_EQ) ||
1161 (!IsAnd && Pred0 != ICmpInst::ICMP_NE))
1162 return nullptr;
1163
1164 // The other compare must include a common operand (X). Canonicalize the
1165 // common operand as operand 1 (Pred1 is swapped if the common operand was
1166 // operand 0).
1167 Value *Y;
1168 ICmpInst::Predicate Pred1;
1169 if (!match(Cmp1, m_c_ICmp(Pred1, m_Value(Y), m_Deferred(X))))
1170 return nullptr;
1171
1172 // Replace variable with constant value equivalence to remove a variable use:
1173 // (X == C) && (Y Pred1 X) --> (X == C) && (Y Pred1 C)
1174 // (X != C) || (Y Pred1 X) --> (X != C) || (Y Pred1 C)
1175 // Can think of the 'or' substitution with the 'and' bool equivalent:
1176 // A || B --> A || (!A && B)
1177 Value *SubstituteCmp = SimplifyICmpInst(Pred1, Y, C, Q);
1178 if (!SubstituteCmp) {
1179 // If we need to create a new instruction, require that the old compare can
1180 // be removed.
1181 if (!Cmp1->hasOneUse())
1182 return nullptr;
1183 SubstituteCmp = Builder.CreateICmp(Pred1, Y, C);
1184 }
1185 return Builder.CreateBinOp(Logic.getOpcode(), Cmp0, SubstituteCmp);
1186}
1187
1188/// Fold (icmp Pred1 V1, C1) & (icmp Pred2 V2, C2)
1189/// or (icmp Pred1 V1, C1) | (icmp Pred2 V2, C2)
1190/// into a single comparison using range-based reasoning.
1191static Value *foldAndOrOfICmpsUsingRanges(
1192 ICmpInst::Predicate Pred1, Value *V1, const APInt &C1,
1193 ICmpInst::Predicate Pred2, Value *V2, const APInt &C2,
1194 IRBuilderBase &Builder, bool IsAnd) {
1195 // Look through add of a constant offset on V1, V2, or both operands. This
1196 // allows us to interpret the V + C' < C'' range idiom into a proper range.
1197 const APInt *Offset1 = nullptr, *Offset2 = nullptr;
1198 if (V1 != V2) {
1199 Value *X;
1200 if (match(V1, m_Add(m_Value(X), m_APInt(Offset1))))
1201 V1 = X;
1202 if (match(V2, m_Add(m_Value(X), m_APInt(Offset2))))
1203 V2 = X;
1204 }
1205
1206 if (V1 != V2)
1207 return nullptr;
1208
1209 ConstantRange CR1 = ConstantRange::makeExactICmpRegion(Pred1, C1);
1210 if (Offset1)
1211 CR1 = CR1.subtract(*Offset1);
1212
1213 ConstantRange CR2 = ConstantRange::makeExactICmpRegion(Pred2, C2);
1214 if (Offset2)
1215 CR2 = CR2.subtract(*Offset2);
1216
1217 Optional<ConstantRange> CR =
1218 IsAnd ? CR1.exactIntersectWith(CR2) : CR1.exactUnionWith(CR2);
1219 if (!CR)
1220 return nullptr;
1221
1222 CmpInst::Predicate NewPred;
1223 APInt NewC, Offset;
1224 CR->getEquivalentICmp(NewPred, NewC, Offset);
1225
1226 Type *Ty = V1->getType();
1227 Value *NewV = V1;
1228 if (Offset != 0)
1229 NewV = Builder.CreateAdd(NewV, ConstantInt::get(Ty, Offset));
1230 return Builder.CreateICmp(NewPred, NewV, ConstantInt::get(Ty, NewC));
1231}
1232
1233/// Fold (icmp)&(icmp) if possible.
1234Value *InstCombinerImpl::foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS,
1235 BinaryOperator &And) {
1236 const SimplifyQuery Q = SQ.getWithInstruction(&And);
1237
1238 // Fold (!iszero(A & K1) & !iszero(A & K2)) -> (A & (K1 | K2)) == (K1 | K2)
1239 // if K1 and K2 are a one-bit mask.
1240 if (Value *V
0.1
'V' is null
0.1
'V' is null
0.1
'V' is null
0.1
'V' is null
= foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, &And,
1
Taking false branch
1241 /* IsAnd */ true))
1242 return V;
1243
1244 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1245
1246 // (icmp1 A, B) & (icmp2 A, B) --> (icmp3 A, B)
1247 if (predicatesFoldable(PredL, PredR)) {
2
Assuming the condition is false
3
Taking false branch
1248 if (LHS->getOperand(0) == RHS->getOperand(1) &&
1249 LHS->getOperand(1) == RHS->getOperand(0))
1250 LHS->swapOperands();
1251 if (LHS->getOperand(0) == RHS->getOperand(0) &&
1252 LHS->getOperand(1) == RHS->getOperand(1)) {
1253 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
1254 unsigned Code = getICmpCode(LHS) & getICmpCode(RHS);
1255 bool IsSigned = LHS->isSigned() || RHS->isSigned();
1256 return getNewICmpValue(Code, IsSigned, Op0, Op1, Builder);
1257 }
1258 }
1259
1260 // handle (roughly): (icmp eq (A & B), C) & (icmp eq (A & D), E)
1261 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, true, Builder))
4
Calling 'foldLogOpOfMaskedICmps'
1262 return V;
1263
1264 if (Value *V = foldAndOrOfICmpsWithConstEq(LHS, RHS, And, Builder, Q))
1265 return V;
1266 if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, And, Builder, Q))
1267 return V;
1268
1269 // E.g. (icmp sge x, 0) & (icmp slt x, n) --> icmp ult x, n
1270 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/false))
1271 return V;
1272
1273 // E.g. (icmp slt x, n) & (icmp sge x, 0) --> icmp ult x, n
1274 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/false))
1275 return V;
1276
1277 if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, true, Builder))
1278 return V;
1279
1280 if (Value *V = foldSignedTruncationCheck(LHS, RHS, And, Builder))
1281 return V;
1282
1283 if (Value *V = foldIsPowerOf2(LHS, RHS, true /* JoinedByAnd */, Builder))
1284 return V;
1285
1286 if (Value *X =
1287 foldUnsignedUnderflowCheck(LHS, RHS, /*IsAnd=*/true, Q, Builder))
1288 return X;
1289 if (Value *X =
1290 foldUnsignedUnderflowCheck(RHS, LHS, /*IsAnd=*/true, Q, Builder))
1291 return X;
1292
1293 if (Value *X = foldEqOfParts(LHS, RHS, /*IsAnd=*/true))
1294 return X;
1295
1296 // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2).
1297 Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
1298
1299 // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
1300 // TODO: Remove this when foldLogOpOfMaskedICmps can handle undefs.
1301 if (PredL == ICmpInst::ICMP_EQ && match(LHS->getOperand(1), m_ZeroInt()) &&
1302 PredR == ICmpInst::ICMP_EQ && match(RHS->getOperand(1), m_ZeroInt()) &&
1303 LHS0->getType() == RHS0->getType()) {
1304 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
1305 return Builder.CreateICmp(PredL, NewOr,
1306 Constant::getNullValue(NewOr->getType()));
1307 }
1308
1309 const APInt *LHSC, *RHSC;
1310 if (!match(LHS->getOperand(1), m_APInt(LHSC)) ||
1311 !match(RHS->getOperand(1), m_APInt(RHSC)))
1312 return nullptr;
1313
1314 // (trunc x) == C1 & (and x, CA) == C2 -> (and x, CA|CMAX) == C1|C2
1315 // where CMAX is the all ones value for the truncated type,
1316 // iff the lower bits of C2 and CA are zero.
1317 if (PredL == ICmpInst::ICMP_EQ && PredL == PredR && LHS->hasOneUse() &&
1318 RHS->hasOneUse()) {
1319 Value *V;
1320 const APInt *AndC, *SmallC = nullptr, *BigC = nullptr;
1321
1322 // (trunc x) == C1 & (and x, CA) == C2
1323 // (and x, CA) == C2 & (trunc x) == C1
1324 if (match(RHS0, m_Trunc(m_Value(V))) &&
1325 match(LHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
1326 SmallC = RHSC;
1327 BigC = LHSC;
1328 } else if (match(LHS0, m_Trunc(m_Value(V))) &&
1329 match(RHS0, m_And(m_Specific(V), m_APInt(AndC)))) {
1330 SmallC = LHSC;
1331 BigC = RHSC;
1332 }
1333
1334 if (SmallC && BigC) {
1335 unsigned BigBitSize = BigC->getBitWidth();
1336 unsigned SmallBitSize = SmallC->getBitWidth();
1337
1338 // Check that the low bits are zero.
1339 APInt Low = APInt::getLowBitsSet(BigBitSize, SmallBitSize);
1340 if ((Low & *AndC).isZero() && (Low & *BigC).isZero()) {
1341 Value *NewAnd = Builder.CreateAnd(V, Low | *AndC);
1342 APInt N = SmallC->zext(BigBitSize) | *BigC;
1343 Value *NewVal = ConstantInt::get(NewAnd->getType(), N);
1344 return Builder.CreateICmp(PredL, NewAnd, NewVal);
1345 }
1346 }
1347 }
1348
1349 return foldAndOrOfICmpsUsingRanges(PredL, LHS0, *LHSC, PredR, RHS0, *RHSC,
1350 Builder, /* IsAnd */ true);
1351}
1352
1353Value *InstCombinerImpl::foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS,
1354 bool IsAnd) {
1355 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
1356 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
1357 FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
1358
1359 if (LHS0 == RHS1 && RHS0 == LHS1) {
1360 // Swap RHS operands to match LHS.
1361 PredR = FCmpInst::getSwappedPredicate(PredR);
1362 std::swap(RHS0, RHS1);
1363 }
1364
1365 // Simplify (fcmp cc0 x, y) & (fcmp cc1 x, y).
1366 // Suppose the relation between x and y is R, where R is one of
1367 // U(1000), L(0100), G(0010) or E(0001), and CC0 and CC1 are the bitmasks for
1368 // testing the desired relations.
1369 //
1370 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1371 // bool(R & CC0) && bool(R & CC1)
1372 // = bool((R & CC0) & (R & CC1))
1373 // = bool(R & (CC0 & CC1)) <= by re-association, commutation, and idempotency
1374 //
1375 // Since (R & CC0) and (R & CC1) are either R or 0, we actually have this:
1376 // bool(R & CC0) || bool(R & CC1)
1377 // = bool((R & CC0) | (R & CC1))
1378 // = bool(R & (CC0 | CC1)) <= by reversed distribution (contribution? ;)
1379 if (LHS0 == RHS0 && LHS1 == RHS1) {
1380 unsigned FCmpCodeL = getFCmpCode(PredL);
1381 unsigned FCmpCodeR = getFCmpCode(PredR);
1382 unsigned NewPred = IsAnd ? FCmpCodeL & FCmpCodeR : FCmpCodeL | FCmpCodeR;
1383 return getFCmpValue(NewPred, LHS0, LHS1, Builder);
1384 }
1385
1386 if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
1387 (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) {
1388 if (LHS0->getType() != RHS0->getType())
1389 return nullptr;
1390
1391 // FCmp canonicalization ensures that (fcmp ord/uno X, X) and
1392 // (fcmp ord/uno X, C) will be transformed to (fcmp X, +0.0).
1393 if (match(LHS1, m_PosZeroFP()) && match(RHS1, m_PosZeroFP()))
1394 // Ignore the constants because they are obviously not NANs:
1395 // (fcmp ord x, 0.0) & (fcmp ord y, 0.0) -> (fcmp ord x, y)
1396 // (fcmp uno x, 0.0) | (fcmp uno y, 0.0) -> (fcmp uno x, y)
1397 return Builder.CreateFCmp(PredL, LHS0, RHS0);
1398 }
1399
1400 return nullptr;
1401}
1402
1403/// This a limited reassociation for a special case (see above) where we are
1404/// checking if two values are either both NAN (unordered) or not-NAN (ordered).
1405/// This could be handled more generally in '-reassociation', but it seems like
1406/// an unlikely pattern for a large number of logic ops and fcmps.
1407static Instruction *reassociateFCmps(BinaryOperator &BO,
1408 InstCombiner::BuilderTy &Builder) {
1409 Instruction::BinaryOps Opcode = BO.getOpcode();
1410 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&(static_cast <bool> ((Opcode == Instruction::And || Opcode
== Instruction::Or) && "Expecting and/or op for fcmp transform"
) ? void (0) : __assert_fail ("(Opcode == Instruction::And || Opcode == Instruction::Or) && \"Expecting and/or op for fcmp transform\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1411
, __extension__ __PRETTY_FUNCTION__))
1411 "Expecting and/or op for fcmp transform")(static_cast <bool> ((Opcode == Instruction::And || Opcode
== Instruction::Or) && "Expecting and/or op for fcmp transform"
) ? void (0) : __assert_fail ("(Opcode == Instruction::And || Opcode == Instruction::Or) && \"Expecting and/or op for fcmp transform\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1411
, __extension__ __PRETTY_FUNCTION__))
;
1412
1413 // There are 4 commuted variants of the pattern. Canonicalize operands of this
1414 // logic op so an fcmp is operand 0 and a matching logic op is operand 1.
1415 Value *Op0 = BO.getOperand(0), *Op1 = BO.getOperand(1), *X;
1416 FCmpInst::Predicate Pred;
1417 if (match(Op1, m_FCmp(Pred, m_Value(), m_AnyZeroFP())))
1418 std::swap(Op0, Op1);
1419
1420 // Match inner binop and the predicate for combining 2 NAN checks into 1.
1421 Value *BO10, *BO11;
1422 FCmpInst::Predicate NanPred = Opcode == Instruction::And ? FCmpInst::FCMP_ORD
1423 : FCmpInst::FCMP_UNO;
1424 if (!match(Op0, m_FCmp(Pred, m_Value(X), m_AnyZeroFP())) || Pred != NanPred ||
1425 !match(Op1, m_BinOp(Opcode, m_Value(BO10), m_Value(BO11))))
1426 return nullptr;
1427
1428 // The inner logic op must have a matching fcmp operand.
1429 Value *Y;
1430 if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) ||
1431 Pred != NanPred || X->getType() != Y->getType())
1432 std::swap(BO10, BO11);
1433
1434 if (!match(BO10, m_FCmp(Pred, m_Value(Y), m_AnyZeroFP())) ||
1435 Pred != NanPred || X->getType() != Y->getType())
1436 return nullptr;
1437
1438 // and (fcmp ord X, 0), (and (fcmp ord Y, 0), Z) --> and (fcmp ord X, Y), Z
1439 // or (fcmp uno X, 0), (or (fcmp uno Y, 0), Z) --> or (fcmp uno X, Y), Z
1440 Value *NewFCmp = Builder.CreateFCmp(Pred, X, Y);
1441 if (auto *NewFCmpInst = dyn_cast<FCmpInst>(NewFCmp)) {
1442 // Intersect FMF from the 2 source fcmps.
1443 NewFCmpInst->copyIRFlags(Op0);
1444 NewFCmpInst->andIRFlags(BO10);
1445 }
1446 return BinaryOperator::Create(Opcode, NewFCmp, BO11);
1447}
1448
1449/// Match variations of De Morgan's Laws:
1450/// (~A & ~B) == (~(A | B))
1451/// (~A | ~B) == (~(A & B))
1452static Instruction *matchDeMorgansLaws(BinaryOperator &I,
1453 InstCombiner::BuilderTy &Builder) {
1454 const Instruction::BinaryOps Opcode = I.getOpcode();
1455 assert((Opcode == Instruction::And || Opcode == Instruction::Or) &&(static_cast <bool> ((Opcode == Instruction::And || Opcode
== Instruction::Or) && "Trying to match De Morgan's Laws with something other than and/or"
) ? void (0) : __assert_fail ("(Opcode == Instruction::And || Opcode == Instruction::Or) && \"Trying to match De Morgan's Laws with something other than and/or\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1456
, __extension__ __PRETTY_FUNCTION__))
1456 "Trying to match De Morgan's Laws with something other than and/or")(static_cast <bool> ((Opcode == Instruction::And || Opcode
== Instruction::Or) && "Trying to match De Morgan's Laws with something other than and/or"
) ? void (0) : __assert_fail ("(Opcode == Instruction::And || Opcode == Instruction::Or) && \"Trying to match De Morgan's Laws with something other than and/or\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1456
, __extension__ __PRETTY_FUNCTION__))
;
1457
1458 // Flip the logic operation.
1459 const Instruction::BinaryOps FlippedOpcode =
1460 (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1461
1462 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1463 Value *A, *B;
1464 if (match(Op0, m_OneUse(m_Not(m_Value(A)))) &&
1465 match(Op1, m_OneUse(m_Not(m_Value(B)))) &&
1466 !InstCombiner::isFreeToInvert(A, A->hasOneUse()) &&
1467 !InstCombiner::isFreeToInvert(B, B->hasOneUse())) {
1468 Value *AndOr =
1469 Builder.CreateBinOp(FlippedOpcode, A, B, I.getName() + ".demorgan");
1470 return BinaryOperator::CreateNot(AndOr);
1471 }
1472
1473 // The 'not' ops may require reassociation.
1474 // (A & ~B) & ~C --> A & ~(B | C)
1475 // (~B & A) & ~C --> A & ~(B | C)
1476 // (A | ~B) | ~C --> A | ~(B & C)
1477 // (~B | A) | ~C --> A | ~(B & C)
1478 Value *C;
1479 if (match(Op0, m_OneUse(m_c_BinOp(Opcode, m_Value(A), m_Not(m_Value(B))))) &&
1480 match(Op1, m_Not(m_Value(C)))) {
1481 Value *FlippedBO = Builder.CreateBinOp(FlippedOpcode, B, C);
1482 return BinaryOperator::Create(Opcode, A, Builder.CreateNot(FlippedBO));
1483 }
1484
1485 return nullptr;
1486}
1487
1488bool InstCombinerImpl::shouldOptimizeCast(CastInst *CI) {
1489 Value *CastSrc = CI->getOperand(0);
1490
1491 // Noop casts and casts of constants should be eliminated trivially.
1492 if (CI->getSrcTy() == CI->getDestTy() || isa<Constant>(CastSrc))
1493 return false;
1494
1495 // If this cast is paired with another cast that can be eliminated, we prefer
1496 // to have it eliminated.
1497 if (const auto *PrecedingCI = dyn_cast<CastInst>(CastSrc))
1498 if (isEliminableCastPair(PrecedingCI, CI))
1499 return false;
1500
1501 return true;
1502}
1503
1504/// Fold {and,or,xor} (cast X), C.
1505static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
1506 InstCombiner::BuilderTy &Builder) {
1507 Constant *C = dyn_cast<Constant>(Logic.getOperand(1));
1508 if (!C)
1509 return nullptr;
1510
1511 auto LogicOpc = Logic.getOpcode();
1512 Type *DestTy = Logic.getType();
1513 Type *SrcTy = Cast->getSrcTy();
1514
1515 // Move the logic operation ahead of a zext or sext if the constant is
1516 // unchanged in the smaller source type. Performing the logic in a smaller
1517 // type may provide more information to later folds, and the smaller logic
1518 // instruction may be cheaper (particularly in the case of vectors).
1519 Value *X;
1520 if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
1521 Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1522 Constant *ZextTruncC = ConstantExpr::getZExt(TruncC, DestTy);
1523 if (ZextTruncC == C) {
1524 // LogicOpc (zext X), C --> zext (LogicOpc X, C)
1525 Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC);
1526 return new ZExtInst(NewOp, DestTy);
1527 }
1528 }
1529
1530 if (match(Cast, m_OneUse(m_SExt(m_Value(X))))) {
1531 Constant *TruncC = ConstantExpr::getTrunc(C, SrcTy);
1532 Constant *SextTruncC = ConstantExpr::getSExt(TruncC, DestTy);
1533 if (SextTruncC == C) {
1534 // LogicOpc (sext X), C --> sext (LogicOpc X, C)
1535 Value *NewOp = Builder.CreateBinOp(LogicOpc, X, TruncC);
1536 return new SExtInst(NewOp, DestTy);
1537 }
1538 }
1539
1540 return nullptr;
1541}
1542
1543/// Fold {and,or,xor} (cast X), Y.
1544Instruction *InstCombinerImpl::foldCastedBitwiseLogic(BinaryOperator &I) {
1545 auto LogicOpc = I.getOpcode();
1546 assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding")(static_cast <bool> (I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding"
) ? void (0) : __assert_fail ("I.isBitwiseLogicOp() && \"Unexpected opcode for bitwise logic folding\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1546
, __extension__ __PRETTY_FUNCTION__))
;
1547
1548 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1549 CastInst *Cast0 = dyn_cast<CastInst>(Op0);
1550 if (!Cast0)
1551 return nullptr;
1552
1553 // This must be a cast from an integer or integer vector source type to allow
1554 // transformation of the logic operation to the source type.
1555 Type *DestTy = I.getType();
1556 Type *SrcTy = Cast0->getSrcTy();
1557 if (!SrcTy->isIntOrIntVectorTy())
1558 return nullptr;
1559
1560 if (Instruction *Ret = foldLogicCastConstant(I, Cast0, Builder))
1561 return Ret;
1562
1563 CastInst *Cast1 = dyn_cast<CastInst>(Op1);
1564 if (!Cast1)
1565 return nullptr;
1566
1567 // Both operands of the logic operation are casts. The casts must be of the
1568 // same type for reduction.
1569 auto CastOpcode = Cast0->getOpcode();
1570 if (CastOpcode != Cast1->getOpcode() || SrcTy != Cast1->getSrcTy())
1571 return nullptr;
1572
1573 Value *Cast0Src = Cast0->getOperand(0);
1574 Value *Cast1Src = Cast1->getOperand(0);
1575
1576 // fold logic(cast(A), cast(B)) -> cast(logic(A, B))
1577 if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {
1578 Value *NewOp = Builder.CreateBinOp(LogicOpc, Cast0Src, Cast1Src,
1579 I.getName());
1580 return CastInst::Create(CastOpcode, NewOp, DestTy);
1581 }
1582
1583 // For now, only 'and'/'or' have optimizations after this.
1584 if (LogicOpc == Instruction::Xor)
1585 return nullptr;
1586
1587 // If this is logic(cast(icmp), cast(icmp)), try to fold this even if the
1588 // cast is otherwise not optimizable. This happens for vector sexts.
1589 ICmpInst *ICmp0 = dyn_cast<ICmpInst>(Cast0Src);
1590 ICmpInst *ICmp1 = dyn_cast<ICmpInst>(Cast1Src);
1591 if (ICmp0 && ICmp1) {
1592 Value *Res = LogicOpc == Instruction::And ? foldAndOfICmps(ICmp0, ICmp1, I)
1593 : foldOrOfICmps(ICmp0, ICmp1, I);
1594 if (Res)
1595 return CastInst::Create(CastOpcode, Res, DestTy);
1596 return nullptr;
1597 }
1598
1599 // If this is logic(cast(fcmp), cast(fcmp)), try to fold this even if the
1600 // cast is otherwise not optimizable. This happens for vector sexts.
1601 FCmpInst *FCmp0 = dyn_cast<FCmpInst>(Cast0Src);
1602 FCmpInst *FCmp1 = dyn_cast<FCmpInst>(Cast1Src);
1603 if (FCmp0 && FCmp1)
1604 if (Value *R = foldLogicOfFCmps(FCmp0, FCmp1, LogicOpc == Instruction::And))
1605 return CastInst::Create(CastOpcode, R, DestTy);
1606
1607 return nullptr;
1608}
1609
1610static Instruction *foldAndToXor(BinaryOperator &I,
1611 InstCombiner::BuilderTy &Builder) {
1612 assert(I.getOpcode() == Instruction::And)(static_cast <bool> (I.getOpcode() == Instruction::And)
? void (0) : __assert_fail ("I.getOpcode() == Instruction::And"
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1612
, __extension__ __PRETTY_FUNCTION__))
;
1613 Value *Op0 = I.getOperand(0);
1614 Value *Op1 = I.getOperand(1);
1615 Value *A, *B;
1616
1617 // Operand complexity canonicalization guarantees that the 'or' is Op0.
1618 // (A | B) & ~(A & B) --> A ^ B
1619 // (A | B) & ~(B & A) --> A ^ B
1620 if (match(&I, m_BinOp(m_Or(m_Value(A), m_Value(B)),
1621 m_Not(m_c_And(m_Deferred(A), m_Deferred(B))))))
1622 return BinaryOperator::CreateXor(A, B);
1623
1624 // (A | ~B) & (~A | B) --> ~(A ^ B)
1625 // (A | ~B) & (B | ~A) --> ~(A ^ B)
1626 // (~B | A) & (~A | B) --> ~(A ^ B)
1627 // (~B | A) & (B | ~A) --> ~(A ^ B)
1628 if (Op0->hasOneUse() || Op1->hasOneUse())
1629 if (match(&I, m_BinOp(m_c_Or(m_Value(A), m_Not(m_Value(B))),
1630 m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B)))))
1631 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1632
1633 return nullptr;
1634}
1635
1636static Instruction *foldOrToXor(BinaryOperator &I,
1637 InstCombiner::BuilderTy &Builder) {
1638 assert(I.getOpcode() == Instruction::Or)(static_cast <bool> (I.getOpcode() == Instruction::Or) ?
void (0) : __assert_fail ("I.getOpcode() == Instruction::Or"
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1638
, __extension__ __PRETTY_FUNCTION__))
;
1639 Value *Op0 = I.getOperand(0);
1640 Value *Op1 = I.getOperand(1);
1641 Value *A, *B;
1642
1643 // Operand complexity canonicalization guarantees that the 'and' is Op0.
1644 // (A & B) | ~(A | B) --> ~(A ^ B)
1645 // (A & B) | ~(B | A) --> ~(A ^ B)
1646 if (Op0->hasOneUse() || Op1->hasOneUse())
1647 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1648 match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1649 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1650
1651 // Operand complexity canonicalization guarantees that the 'xor' is Op0.
1652 // (A ^ B) | ~(A | B) --> ~(A & B)
1653 // (A ^ B) | ~(B | A) --> ~(A & B)
1654 if (Op0->hasOneUse() || Op1->hasOneUse())
1655 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
1656 match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1657 return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
1658
1659 // (A & ~B) | (~A & B) --> A ^ B
1660 // (A & ~B) | (B & ~A) --> A ^ B
1661 // (~B & A) | (~A & B) --> A ^ B
1662 // (~B & A) | (B & ~A) --> A ^ B
1663 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
1664 match(Op1, m_c_And(m_Not(m_Specific(A)), m_Specific(B))))
1665 return BinaryOperator::CreateXor(A, B);
1666
1667 return nullptr;
1668}
1669
1670/// Return true if a constant shift amount is always less than the specified
1671/// bit-width. If not, the shift could create poison in the narrower type.
1672static bool canNarrowShiftAmt(Constant *C, unsigned BitWidth) {
1673 APInt Threshold(C->getType()->getScalarSizeInBits(), BitWidth);
1674 return match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
1675}
1676
1677/// Try to use narrower ops (sink zext ops) for an 'and' with binop operand and
1678/// a common zext operand: and (binop (zext X), C), (zext X).
1679Instruction *InstCombinerImpl::narrowMaskedBinOp(BinaryOperator &And) {
1680 // This transform could also apply to {or, and, xor}, but there are better
1681 // folds for those cases, so we don't expect those patterns here. AShr is not
1682 // handled because it should always be transformed to LShr in this sequence.
1683 // The subtract transform is different because it has a constant on the left.
1684 // Add/mul commute the constant to RHS; sub with constant RHS becomes add.
1685 Value *Op0 = And.getOperand(0), *Op1 = And.getOperand(1);
1686 Constant *C;
1687 if (!match(Op0, m_OneUse(m_Add(m_Specific(Op1), m_Constant(C)))) &&
1688 !match(Op0, m_OneUse(m_Mul(m_Specific(Op1), m_Constant(C)))) &&
1689 !match(Op0, m_OneUse(m_LShr(m_Specific(Op1), m_Constant(C)))) &&
1690 !match(Op0, m_OneUse(m_Shl(m_Specific(Op1), m_Constant(C)))) &&
1691 !match(Op0, m_OneUse(m_Sub(m_Constant(C), m_Specific(Op1)))))
1692 return nullptr;
1693
1694 Value *X;
1695 if (!match(Op1, m_ZExt(m_Value(X))) || Op1->hasNUsesOrMore(3))
1696 return nullptr;
1697
1698 Type *Ty = And.getType();
1699 if (!isa<VectorType>(Ty) && !shouldChangeType(Ty, X->getType()))
1700 return nullptr;
1701
1702 // If we're narrowing a shift, the shift amount must be safe (less than the
1703 // width) in the narrower type. If the shift amount is greater, instsimplify
1704 // usually handles that case, but we can't guarantee/assert it.
1705 Instruction::BinaryOps Opc = cast<BinaryOperator>(Op0)->getOpcode();
1706 if (Opc == Instruction::LShr || Opc == Instruction::Shl)
1707 if (!canNarrowShiftAmt(C, X->getType()->getScalarSizeInBits()))
1708 return nullptr;
1709
1710 // and (sub C, (zext X)), (zext X) --> zext (and (sub C', X), X)
1711 // and (binop (zext X), C), (zext X) --> zext (and (binop X, C'), X)
1712 Value *NewC = ConstantExpr::getTrunc(C, X->getType());
1713 Value *NewBO = Opc == Instruction::Sub ? Builder.CreateBinOp(Opc, NewC, X)
1714 : Builder.CreateBinOp(Opc, X, NewC);
1715 return new ZExtInst(Builder.CreateAnd(NewBO, X), Ty);
1716}
1717
1718/// Try folding relatively complex patterns for both And and Or operations
1719/// with all And and Or swapped.
1720static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
1721 InstCombiner::BuilderTy &Builder) {
1722 const Instruction::BinaryOps Opcode = I.getOpcode();
1723 assert(Opcode == Instruction::And || Opcode == Instruction::Or)(static_cast <bool> (Opcode == Instruction::And || Opcode
== Instruction::Or) ? void (0) : __assert_fail ("Opcode == Instruction::And || Opcode == Instruction::Or"
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1723
, __extension__ __PRETTY_FUNCTION__))
;
1724
1725 // Flip the logic operation.
1726 const Instruction::BinaryOps FlippedOpcode =
1727 (Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
1728
1729 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1730 Value *A, *B, *C, *X, *Y, *Dummy;
1731
1732 // Match following expressions:
1733 // (~(A | B) & C)
1734 // (~(A & B) | C)
1735 // Captures X = ~(A | B) or ~(A & B)
1736 const auto matchNotOrAnd =
1737 [Opcode, FlippedOpcode](Value *Op, auto m_A, auto m_B, auto m_C,
1738 Value *&X, bool CountUses = false) -> bool {
1739 if (CountUses && !Op->hasOneUse())
1740 return false;
1741
1742 if (match(Op, m_c_BinOp(FlippedOpcode,
1743 m_CombineAnd(m_Value(X),
1744 m_Not(m_c_BinOp(Opcode, m_A, m_B))),
1745 m_C)))
1746 return !CountUses || X->hasOneUse();
1747
1748 return false;
1749 };
1750
1751 // (~(A | B) & C) | ... --> ...
1752 // (~(A & B) | C) & ... --> ...
1753 // TODO: One use checks are conservative. We just need to check that a total
1754 // number of multiple used values does not exceed reduction
1755 // in operations.
1756 if (matchNotOrAnd(Op0, m_Value(A), m_Value(B), m_Value(C), X)) {
1757 // (~(A | B) & C) | (~(A | C) & B) --> (B ^ C) & ~A
1758 // (~(A & B) | C) & (~(A & C) | B) --> ~((B ^ C) & A)
1759 if (matchNotOrAnd(Op1, m_Specific(A), m_Specific(C), m_Specific(B), Dummy,
1760 true)) {
1761 Value *Xor = Builder.CreateXor(B, C);
1762 return (Opcode == Instruction::Or)
1763 ? BinaryOperator::CreateAnd(Xor, Builder.CreateNot(A))
1764 : BinaryOperator::CreateNot(Builder.CreateAnd(Xor, A));
1765 }
1766
1767 // (~(A | B) & C) | (~(B | C) & A) --> (A ^ C) & ~B
1768 // (~(A & B) | C) & (~(B & C) | A) --> ~((A ^ C) & B)
1769 if (matchNotOrAnd(Op1, m_Specific(B), m_Specific(C), m_Specific(A), Dummy,
1770 true)) {
1771 Value *Xor = Builder.CreateXor(A, C);
1772 return (Opcode == Instruction::Or)
1773 ? BinaryOperator::CreateAnd(Xor, Builder.CreateNot(B))
1774 : BinaryOperator::CreateNot(Builder.CreateAnd(Xor, B));
1775 }
1776
1777 // (~(A | B) & C) | ~(A | C) --> ~((B & C) | A)
1778 // (~(A & B) | C) & ~(A & C) --> ~((B | C) & A)
1779 if (match(Op1, m_OneUse(m_Not(m_OneUse(
1780 m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
1781 return BinaryOperator::CreateNot(Builder.CreateBinOp(
1782 Opcode, Builder.CreateBinOp(FlippedOpcode, B, C), A));
1783
1784 // (~(A | B) & C) | ~(B | C) --> ~((A & C) | B)
1785 // (~(A & B) | C) & ~(B & C) --> ~((A | C) & B)
1786 if (match(Op1, m_OneUse(m_Not(m_OneUse(
1787 m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)))))))
1788 return BinaryOperator::CreateNot(Builder.CreateBinOp(
1789 Opcode, Builder.CreateBinOp(FlippedOpcode, A, C), B));
1790
1791 // (~(A | B) & C) | ~(C | (A ^ B)) --> ~((A | B) & (C | (A ^ B)))
1792 // Note, the pattern with swapped and/or is not handled because the
1793 // result is more undefined than a source:
1794 // (~(A & B) | C) & ~(C & (A ^ B)) --> (A ^ B ^ C) | ~(A | C) is invalid.
1795 if (Opcode == Instruction::Or && Op0->hasOneUse() &&
1796 match(Op1, m_OneUse(m_Not(m_CombineAnd(
1797 m_Value(Y),
1798 m_c_BinOp(Opcode, m_Specific(C),
1799 m_c_Xor(m_Specific(A), m_Specific(B)))))))) {
1800 // X = ~(A | B)
1801 // Y = (C | (A ^ B)
1802 Value *Or = cast<BinaryOperator>(X)->getOperand(0);
1803 return BinaryOperator::CreateNot(Builder.CreateAnd(Or, Y));
1804 }
1805 }
1806
1807 // (~A & B & C) | ... --> ...
1808 // (~A | B | C) | ... --> ...
1809 // TODO: One use checks are conservative. We just need to check that a total
1810 // number of multiple used values does not exceed reduction
1811 // in operations.
1812 if (match(Op0,
1813 m_OneUse(m_c_BinOp(FlippedOpcode,
1814 m_BinOp(FlippedOpcode, m_Value(B), m_Value(C)),
1815 m_CombineAnd(m_Value(X), m_Not(m_Value(A)))))) ||
1816 match(Op0, m_OneUse(m_c_BinOp(
1817 FlippedOpcode,
1818 m_c_BinOp(FlippedOpcode, m_Value(C),
1819 m_CombineAnd(m_Value(X), m_Not(m_Value(A)))),
1820 m_Value(B))))) {
1821 // X = ~A
1822 // (~A & B & C) | ~(A | B | C) --> ~(A | (B ^ C))
1823 // (~A | B | C) & ~(A & B & C) --> (~A | (B ^ C))
1824 if (match(Op1, m_OneUse(m_Not(m_c_BinOp(
1825 Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)),
1826 m_Specific(C))))) ||
1827 match(Op1, m_OneUse(m_Not(m_c_BinOp(
1828 Opcode, m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)),
1829 m_Specific(A))))) ||
1830 match(Op1, m_OneUse(m_Not(m_c_BinOp(
1831 Opcode, m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)),
1832 m_Specific(B)))))) {
1833 Value *Xor = Builder.CreateXor(B, C);
1834 return (Opcode == Instruction::Or)
1835 ? BinaryOperator::CreateNot(Builder.CreateOr(Xor, A))
1836 : BinaryOperator::CreateOr(Xor, X);
1837 }
1838
1839 // (~A & B & C) | ~(A | B) --> (C | ~B) & ~A
1840 // (~A | B | C) & ~(A & B) --> (C & ~B) | ~A
1841 if (match(Op1, m_OneUse(m_Not(m_OneUse(
1842 m_c_BinOp(Opcode, m_Specific(A), m_Specific(B)))))))
1843 return BinaryOperator::Create(
1844 FlippedOpcode, Builder.CreateBinOp(Opcode, C, Builder.CreateNot(B)),
1845 X);
1846
1847 // (~A & B & C) | ~(A | C) --> (B | ~C) & ~A
1848 // (~A | B | C) & ~(A & C) --> (B & ~C) | ~A
1849 if (match(Op1, m_OneUse(m_Not(m_OneUse(
1850 m_c_BinOp(Opcode, m_Specific(A), m_Specific(C)))))))
1851 return BinaryOperator::Create(
1852 FlippedOpcode, Builder.CreateBinOp(Opcode, B, Builder.CreateNot(C)),
1853 X);
1854 }
1855
1856 return nullptr;
1857}
1858
1859// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
1860// here. We should standardize that construct where it is needed or choose some
1861// other way to ensure that commutated variants of patterns are not missed.
1862Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
1863 Type *Ty = I.getType();
1864
1865 if (Value *V = SimplifyAndInst(I.getOperand(0), I.getOperand(1),
1866 SQ.getWithInstruction(&I)))
1867 return replaceInstUsesWith(I, V);
1868
1869 if (SimplifyAssociativeOrCommutative(I))
1870 return &I;
1871
1872 if (Instruction *X = foldVectorBinop(I))
1873 return X;
1874
1875 // See if we can simplify any instructions used by the instruction whose sole
1876 // purpose is to compute bits we don't care about.
1877 if (SimplifyDemandedInstructionBits(I))
1878 return &I;
1879
1880 // Do this before using distributive laws to catch simple and/or/not patterns.
1881 if (Instruction *Xor = foldAndToXor(I, Builder))
1882 return Xor;
1883
1884 if (Instruction *X = foldComplexAndOrPatterns(I, Builder))
1885 return X;
1886
1887 // (A|B)&(A|C) -> A|(B&C) etc
1888 if (Value *V = SimplifyUsingDistributiveLaws(I))
1889 return replaceInstUsesWith(I, V);
1890
1891 if (Value *V = SimplifyBSwap(I, Builder))
1892 return replaceInstUsesWith(I, V);
1893
1894 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1895
1896 Value *X, *Y;
1897 if (match(Op0, m_OneUse(m_LogicalShift(m_One(), m_Value(X)))) &&
1898 match(Op1, m_One())) {
1899 // (1 << X) & 1 --> zext(X == 0)
1900 // (1 >> X) & 1 --> zext(X == 0)
1901 Value *IsZero = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, 0));
1902 return new ZExtInst(IsZero, Ty);
1903 }
1904
1905 const APInt *C;
1906 if (match(Op1, m_APInt(C))) {
1907 const APInt *XorC;
1908 if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_APInt(XorC))))) {
1909 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
1910 Constant *NewC = ConstantInt::get(Ty, *C & *XorC);
1911 Value *And = Builder.CreateAnd(X, Op1);
1912 And->takeName(Op0);
1913 return BinaryOperator::CreateXor(And, NewC);
1914 }
1915
1916 const APInt *OrC;
1917 if (match(Op0, m_OneUse(m_Or(m_Value(X), m_APInt(OrC))))) {
1918 // (X | C1) & C2 --> (X & C2^(C1&C2)) | (C1&C2)
1919 // NOTE: This reduces the number of bits set in the & mask, which
1920 // can expose opportunities for store narrowing for scalars.
1921 // NOTE: SimplifyDemandedBits should have already removed bits from C1
1922 // that aren't set in C2. Meaning we can replace (C1&C2) with C1 in
1923 // above, but this feels safer.
1924 APInt Together = *C & *OrC;
1925 Value *And = Builder.CreateAnd(X, ConstantInt::get(Ty, Together ^ *C));
1926 And->takeName(Op0);
1927 return BinaryOperator::CreateOr(And, ConstantInt::get(Ty, Together));
1928 }
1929
1930 // If the mask is only needed on one incoming arm, push the 'and' op up.
1931 if (match(Op0, m_OneUse(m_Xor(m_Value(X), m_Value(Y)))) ||
1932 match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
1933 APInt NotAndMask(~(*C));
1934 BinaryOperator::BinaryOps BinOp = cast<BinaryOperator>(Op0)->getOpcode();
1935 if (MaskedValueIsZero(X, NotAndMask, 0, &I)) {
1936 // Not masking anything out for the LHS, move mask to RHS.
1937 // and ({x}or X, Y), C --> {x}or X, (and Y, C)
1938 Value *NewRHS = Builder.CreateAnd(Y, Op1, Y->getName() + ".masked");
1939 return BinaryOperator::Create(BinOp, X, NewRHS);
1940 }
1941 if (!isa<Constant>(Y) && MaskedValueIsZero(Y, NotAndMask, 0, &I)) {
1942 // Not masking anything out for the RHS, move mask to LHS.
1943 // and ({x}or X, Y), C --> {x}or (and X, C), Y
1944 Value *NewLHS = Builder.CreateAnd(X, Op1, X->getName() + ".masked");
1945 return BinaryOperator::Create(BinOp, NewLHS, Y);
1946 }
1947 }
1948
1949 unsigned Width = Ty->getScalarSizeInBits();
1950 const APInt *ShiftC;
1951 if (match(Op0, m_OneUse(m_SExt(m_AShr(m_Value(X), m_APInt(ShiftC)))))) {
1952 if (*C == APInt::getLowBitsSet(Width, Width - ShiftC->getZExtValue())) {
1953 // We are clearing high bits that were potentially set by sext+ashr:
1954 // and (sext (ashr X, ShiftC)), C --> lshr (sext X), ShiftC
1955 Value *Sext = Builder.CreateSExt(X, Ty);
1956 Constant *ShAmtC = ConstantInt::get(Ty, ShiftC->zext(Width));
1957 return BinaryOperator::CreateLShr(Sext, ShAmtC);
1958 }
1959 }
1960
1961 const APInt *AddC;
1962 if (match(Op0, m_Add(m_Value(X), m_APInt(AddC)))) {
1963 // If we add zeros to every bit below a mask, the add has no effect:
1964 // (X + AddC) & LowMaskC --> X & LowMaskC
1965 unsigned Ctlz = C->countLeadingZeros();
1966 APInt LowMask(APInt::getLowBitsSet(Width, Width - Ctlz));
1967 if ((*AddC & LowMask).isZero())
1968 return BinaryOperator::CreateAnd(X, Op1);
1969
1970 // If we are masking the result of the add down to exactly one bit and
1971 // the constant we are adding has no bits set below that bit, then the
1972 // add is flipping a single bit. Example:
1973 // (X + 4) & 4 --> (X & 4) ^ 4
1974 if (Op0->hasOneUse() && C->isPowerOf2() && (*AddC & (*C - 1)) == 0) {
1975 assert((*C & *AddC) != 0 && "Expected common bit")(static_cast <bool> ((*C & *AddC) != 0 && "Expected common bit"
) ? void (0) : __assert_fail ("(*C & *AddC) != 0 && \"Expected common bit\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 1975
, __extension__ __PRETTY_FUNCTION__))
;
1976 Value *NewAnd = Builder.CreateAnd(X, Op1);
1977 return BinaryOperator::CreateXor(NewAnd, Op1);
1978 }
1979 }
1980
1981 // ((C1 OP zext(X)) & C2) -> zext((C1 OP X) & C2) if C2 fits in the
1982 // bitwidth of X and OP behaves well when given trunc(C1) and X.
1983 auto isSuitableBinOpcode = [](BinaryOperator *B) {
1984 switch (B->getOpcode()) {
1985 case Instruction::Xor:
1986 case Instruction::Or:
1987 case Instruction::Mul:
1988 case Instruction::Add:
1989 case Instruction::Sub:
1990 return true;
1991 default:
1992 return false;
1993 }
1994 };
1995 BinaryOperator *BO;
1996 if (match(Op0, m_OneUse(m_BinOp(BO))) && isSuitableBinOpcode(BO)) {
1997 Value *X;
1998 const APInt *C1;
1999 // TODO: The one-use restrictions could be relaxed a little if the AND
2000 // is going to be removed.
2001 if (match(BO, m_c_BinOp(m_OneUse(m_ZExt(m_Value(X))), m_APInt(C1))) &&
2002 C->isIntN(X->getType()->getScalarSizeInBits())) {
2003 unsigned XWidth = X->getType()->getScalarSizeInBits();
2004 Constant *TruncC1 = ConstantInt::get(X->getType(), C1->trunc(XWidth));
2005 Value *BinOp = isa<ZExtInst>(BO->getOperand(0))
2006 ? Builder.CreateBinOp(BO->getOpcode(), X, TruncC1)
2007 : Builder.CreateBinOp(BO->getOpcode(), TruncC1, X);
2008 Constant *TruncC = ConstantInt::get(X->getType(), C->trunc(XWidth));
2009 Value *And = Builder.CreateAnd(BinOp, TruncC);
2010 return new ZExtInst(And, Ty);
2011 }
2012 }
2013 }
2014
2015 if (match(&I, m_And(m_OneUse(m_Shl(m_ZExt(m_Value(X)), m_Value(Y))),
2016 m_SignMask())) &&
2017 match(Y, m_SpecificInt_ICMP(
2018 ICmpInst::Predicate::ICMP_EQ,
2019 APInt(Ty->getScalarSizeInBits(),
2020 Ty->getScalarSizeInBits() -
2021 X->getType()->getScalarSizeInBits())))) {
2022 auto *SExt = Builder.CreateSExt(X, Ty, X->getName() + ".signext");
2023 auto *SanitizedSignMask = cast<Constant>(Op1);
2024 // We must be careful with the undef elements of the sign bit mask, however:
2025 // the mask elt can be undef iff the shift amount for that lane was undef,
2026 // otherwise we need to sanitize undef masks to zero.
2027 SanitizedSignMask = Constant::replaceUndefsWith(
2028 SanitizedSignMask, ConstantInt::getNullValue(Ty->getScalarType()));
2029 SanitizedSignMask =
2030 Constant::mergeUndefsWith(SanitizedSignMask, cast<Constant>(Y));
2031 return BinaryOperator::CreateAnd(SExt, SanitizedSignMask);
2032 }
2033
2034 if (Instruction *Z = narrowMaskedBinOp(I))
2035 return Z;
2036
2037 if (I.getType()->isIntOrIntVectorTy(1)) {
2038 if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
2039 if (auto *I =
2040 foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ true))
2041 return I;
2042 }
2043 if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
2044 if (auto *I =
2045 foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ true))
2046 return I;
2047 }
2048 }
2049
2050 if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2051 return FoldedLogic;
2052
2053 if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2054 return DeMorgan;
2055
2056 {
2057 Value *A, *B, *C;
2058 // A & (A ^ B) --> A & ~B
2059 if (match(Op1, m_OneUse(m_c_Xor(m_Specific(Op0), m_Value(B)))))
2060 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(B));
2061 // (A ^ B) & A --> A & ~B
2062 if (match(Op0, m_OneUse(m_c_Xor(m_Specific(Op1), m_Value(B)))))
2063 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(B));
2064
2065 // A & ~(A ^ B) --> A & B
2066 if (match(Op1, m_Not(m_c_Xor(m_Specific(Op0), m_Value(B)))))
2067 return BinaryOperator::CreateAnd(Op0, B);
2068 // ~(A ^ B) & A --> A & B
2069 if (match(Op0, m_Not(m_c_Xor(m_Specific(Op1), m_Value(B)))))
2070 return BinaryOperator::CreateAnd(Op1, B);
2071
2072 // (A ^ B) & ((B ^ C) ^ A) -> (A ^ B) & ~C
2073 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2074 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2075 if (Op1->hasOneUse() || isFreeToInvert(C, C->hasOneUse()))
2076 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(C));
2077
2078 // ((A ^ C) ^ B) & (B ^ A) -> (B ^ A) & ~C
2079 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2080 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2081 if (Op0->hasOneUse() || isFreeToInvert(C, C->hasOneUse()))
2082 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(C));
2083
2084 // (A | B) & (~A ^ B) -> A & B
2085 // (A | B) & (B ^ ~A) -> A & B
2086 // (B | A) & (~A ^ B) -> A & B
2087 // (B | A) & (B ^ ~A) -> A & B
2088 if (match(Op1, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2089 match(Op0, m_c_Or(m_Specific(A), m_Specific(B))))
2090 return BinaryOperator::CreateAnd(A, B);
2091
2092 // (~A ^ B) & (A | B) -> A & B
2093 // (~A ^ B) & (B | A) -> A & B
2094 // (B ^ ~A) & (A | B) -> A & B
2095 // (B ^ ~A) & (B | A) -> A & B
2096 if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
2097 match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
2098 return BinaryOperator::CreateAnd(A, B);
2099
2100 // (~A | B) & (A ^ B) -> ~A & B
2101 // (~A | B) & (B ^ A) -> ~A & B
2102 // (B | ~A) & (A ^ B) -> ~A & B
2103 // (B | ~A) & (B ^ A) -> ~A & B
2104 if (match(Op0, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2105 match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
2106 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2107
2108 // (A ^ B) & (~A | B) -> ~A & B
2109 // (B ^ A) & (~A | B) -> ~A & B
2110 // (A ^ B) & (B | ~A) -> ~A & B
2111 // (B ^ A) & (B | ~A) -> ~A & B
2112 if (match(Op1, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
2113 match(Op0, m_c_Xor(m_Specific(A), m_Specific(B))))
2114 return BinaryOperator::CreateAnd(Builder.CreateNot(A), B);
2115 }
2116
2117 {
2118 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2119 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2120 if (LHS && RHS)
2121 if (Value *Res = foldAndOfICmps(LHS, RHS, I))
2122 return replaceInstUsesWith(I, Res);
2123
2124 // TODO: Make this recursive; it's a little tricky because an arbitrary
2125 // number of 'and' instructions might have to be created.
2126 if (LHS && match(Op1, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
2127 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2128 if (Value *Res = foldAndOfICmps(LHS, Cmp, I))
2129 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
2130 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2131 if (Value *Res = foldAndOfICmps(LHS, Cmp, I))
2132 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
2133 }
2134 if (RHS && match(Op0, m_OneUse(m_And(m_Value(X), m_Value(Y))))) {
2135 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2136 if (Value *Res = foldAndOfICmps(Cmp, RHS, I))
2137 return replaceInstUsesWith(I, Builder.CreateAnd(Res, Y));
2138 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2139 if (Value *Res = foldAndOfICmps(Cmp, RHS, I))
2140 return replaceInstUsesWith(I, Builder.CreateAnd(Res, X));
2141 }
2142 }
2143
2144 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2145 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2146 if (Value *Res = foldLogicOfFCmps(LHS, RHS, true))
2147 return replaceInstUsesWith(I, Res);
2148
2149 if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2150 return FoldedFCmps;
2151
2152 if (Instruction *CastedAnd = foldCastedBitwiseLogic(I))
2153 return CastedAnd;
2154
2155 if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2156 return Sel;
2157
2158 // and(sext(A), B) / and(B, sext(A)) --> A ? B : 0, where A is i1 or <N x i1>.
2159 // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2160 // with binop identity constant. But creating a select with non-constant
2161 // arm may not be reversible due to poison semantics. Is that a good
2162 // canonicalization?
2163 Value *A;
2164 if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2165 A->getType()->isIntOrIntVectorTy(1))
2166 return SelectInst::Create(A, Op1, Constant::getNullValue(Ty));
2167 if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2168 A->getType()->isIntOrIntVectorTy(1))
2169 return SelectInst::Create(A, Op0, Constant::getNullValue(Ty));
2170
2171 // (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0
2172 unsigned FullShift = Ty->getScalarSizeInBits() - 1;
2173 if (match(&I, m_c_And(m_OneUse(m_AShr(m_Value(X), m_SpecificInt(FullShift))),
2174 m_Value(Y)))) {
2175 Constant *Zero = ConstantInt::getNullValue(Ty);
2176 Value *Cmp = Builder.CreateICmpSLT(X, Zero, "isneg");
2177 return SelectInst::Create(Cmp, Y, Zero);
2178 }
2179 // If there's a 'not' of the shifted value, swap the select operands:
2180 // ~(iN X s>> (N-1)) & Y --> (X s< 0) ? 0 : Y
2181 if (match(&I, m_c_And(m_OneUse(m_Not(
2182 m_AShr(m_Value(X), m_SpecificInt(FullShift)))),
2183 m_Value(Y)))) {
2184 Constant *Zero = ConstantInt::getNullValue(Ty);
2185 Value *Cmp = Builder.CreateICmpSLT(X, Zero, "isneg");
2186 return SelectInst::Create(Cmp, Zero, Y);
2187 }
2188
2189 // (~x) & y --> ~(x | (~y)) iff that gets rid of inversions
2190 if (sinkNotIntoOtherHandOfAndOrOr(I))
2191 return &I;
2192
2193 // An and recurrence w/loop invariant step is equivelent to (and start, step)
2194 PHINode *PN = nullptr;
2195 Value *Start = nullptr, *Step = nullptr;
2196 if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
2197 return replaceInstUsesWith(I, Builder.CreateAnd(Start, Step));
2198
2199 return nullptr;
2200}
2201
2202Instruction *InstCombinerImpl::matchBSwapOrBitReverse(Instruction &I,
2203 bool MatchBSwaps,
2204 bool MatchBitReversals) {
2205 SmallVector<Instruction *, 4> Insts;
2206 if (!recognizeBSwapOrBitReverseIdiom(&I, MatchBSwaps, MatchBitReversals,
2207 Insts))
2208 return nullptr;
2209 Instruction *LastInst = Insts.pop_back_val();
2210 LastInst->removeFromParent();
2211
2212 for (auto *Inst : Insts)
2213 Worklist.push(Inst);
2214 return LastInst;
2215}
2216
2217/// Match UB-safe variants of the funnel shift intrinsic.
2218static Instruction *matchFunnelShift(Instruction &Or, InstCombinerImpl &IC) {
2219 // TODO: Can we reduce the code duplication between this and the related
2220 // rotate matching code under visitSelect and visitTrunc?
2221 unsigned Width = Or.getType()->getScalarSizeInBits();
2222
2223 // First, find an or'd pair of opposite shifts:
2224 // or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1)
2225 BinaryOperator *Or0, *Or1;
2226 if (!match(Or.getOperand(0), m_BinOp(Or0)) ||
2227 !match(Or.getOperand(1), m_BinOp(Or1)))
2228 return nullptr;
2229
2230 Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
2231 if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) ||
2232 !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) ||
2233 Or0->getOpcode() == Or1->getOpcode())
2234 return nullptr;
2235
2236 // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)).
2237 if (Or0->getOpcode() == BinaryOperator::LShr) {
2238 std::swap(Or0, Or1);
2239 std::swap(ShVal0, ShVal1);
2240 std::swap(ShAmt0, ShAmt1);
2241 }
2242 assert(Or0->getOpcode() == BinaryOperator::Shl &&(static_cast <bool> (Or0->getOpcode() == BinaryOperator
::Shl && Or1->getOpcode() == BinaryOperator::LShr &&
"Illegal or(shift,shift) pair") ? void (0) : __assert_fail (
"Or0->getOpcode() == BinaryOperator::Shl && Or1->getOpcode() == BinaryOperator::LShr && \"Illegal or(shift,shift) pair\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 2244
, __extension__ __PRETTY_FUNCTION__))
2243 Or1->getOpcode() == BinaryOperator::LShr &&(static_cast <bool> (Or0->getOpcode() == BinaryOperator
::Shl && Or1->getOpcode() == BinaryOperator::LShr &&
"Illegal or(shift,shift) pair") ? void (0) : __assert_fail (
"Or0->getOpcode() == BinaryOperator::Shl && Or1->getOpcode() == BinaryOperator::LShr && \"Illegal or(shift,shift) pair\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 2244
, __extension__ __PRETTY_FUNCTION__))
2244 "Illegal or(shift,shift) pair")(static_cast <bool> (Or0->getOpcode() == BinaryOperator
::Shl && Or1->getOpcode() == BinaryOperator::LShr &&
"Illegal or(shift,shift) pair") ? void (0) : __assert_fail (
"Or0->getOpcode() == BinaryOperator::Shl && Or1->getOpcode() == BinaryOperator::LShr && \"Illegal or(shift,shift) pair\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 2244
, __extension__ __PRETTY_FUNCTION__))
;
2245
2246 // Match the shift amount operands for a funnel shift pattern. This always
2247 // matches a subtraction on the R operand.
2248 auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
2249 // Check for constant shift amounts that sum to the bitwidth.
2250 const APInt *LI, *RI;
2251 if (match(L, m_APIntAllowUndef(LI)) && match(R, m_APIntAllowUndef(RI)))
2252 if (LI->ult(Width) && RI->ult(Width) && (*LI + *RI) == Width)
2253 return ConstantInt::get(L->getType(), *LI);
2254
2255 Constant *LC, *RC;
2256 if (match(L, m_Constant(LC)) && match(R, m_Constant(RC)) &&
2257 match(L, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2258 match(R, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, APInt(Width, Width))) &&
2259 match(ConstantExpr::getAdd(LC, RC), m_SpecificIntAllowUndef(Width)))
2260 return ConstantExpr::mergeUndefsWith(LC, RC);
2261
2262 // (shl ShVal, X) | (lshr ShVal, (Width - x)) iff X < Width.
2263 // We limit this to X < Width in case the backend re-expands the intrinsic,
2264 // and has to reintroduce a shift modulo operation (InstCombine might remove
2265 // it after this fold). This still doesn't guarantee that the final codegen
2266 // will match this original pattern.
2267 if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L))))) {
2268 KnownBits KnownL = IC.computeKnownBits(L, /*Depth*/ 0, &Or);
2269 return KnownL.getMaxValue().ult(Width) ? L : nullptr;
2270 }
2271
2272 // For non-constant cases, the following patterns currently only work for
2273 // rotation patterns.
2274 // TODO: Add general funnel-shift compatible patterns.
2275 if (ShVal0 != ShVal1)
2276 return nullptr;
2277
2278 // For non-constant cases we don't support non-pow2 shift masks.
2279 // TODO: Is it worth matching urem as well?
2280 if (!isPowerOf2_32(Width))
2281 return nullptr;
2282
2283 // The shift amount may be masked with negation:
2284 // (shl ShVal, (X & (Width - 1))) | (lshr ShVal, ((-X) & (Width - 1)))
2285 Value *X;
2286 unsigned Mask = Width - 1;
2287 if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) &&
2288 match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask))))
2289 return X;
2290
2291 // Similar to above, but the shift amount may be extended after masking,
2292 // so return the extended value as the parameter for the intrinsic.
2293 if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2294 match(R, m_And(m_Neg(m_ZExt(m_And(m_Specific(X), m_SpecificInt(Mask)))),
2295 m_SpecificInt(Mask))))
2296 return L;
2297
2298 if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
2299 match(R, m_ZExt(m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask)))))
2300 return L;
2301
2302 return nullptr;
2303 };
2304
2305 Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, Width);
2306 bool IsFshl = true; // Sub on LSHR.
2307 if (!ShAmt) {
2308 ShAmt = matchShiftAmount(ShAmt1, ShAmt0, Width);
2309 IsFshl = false; // Sub on SHL.
2310 }
2311 if (!ShAmt)
2312 return nullptr;
2313
2314 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2315 Function *F = Intrinsic::getDeclaration(Or.getModule(), IID, Or.getType());
2316 return CallInst::Create(F, {ShVal0, ShVal1, ShAmt});
2317}
2318
2319/// Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns.
2320static Instruction *matchOrConcat(Instruction &Or,
2321 InstCombiner::BuilderTy &Builder) {
2322 assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'")(static_cast <bool> (Or.getOpcode() == Instruction::Or &&
"bswap requires an 'or'") ? void (0) : __assert_fail ("Or.getOpcode() == Instruction::Or && \"bswap requires an 'or'\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 2322
, __extension__ __PRETTY_FUNCTION__))
;
2323 Value *Op0 = Or.getOperand(0), *Op1 = Or.getOperand(1);
2324 Type *Ty = Or.getType();
2325
2326 unsigned Width = Ty->getScalarSizeInBits();
2327 if ((Width & 1) != 0)
2328 return nullptr;
2329 unsigned HalfWidth = Width / 2;
2330
2331 // Canonicalize zext (lower half) to LHS.
2332 if (!isa<ZExtInst>(Op0))
2333 std::swap(Op0, Op1);
2334
2335 // Find lower/upper half.
2336 Value *LowerSrc, *ShlVal, *UpperSrc;
2337 const APInt *C;
2338 if (!match(Op0, m_OneUse(m_ZExt(m_Value(LowerSrc)))) ||
2339 !match(Op1, m_OneUse(m_Shl(m_Value(ShlVal), m_APInt(C)))) ||
2340 !match(ShlVal, m_OneUse(m_ZExt(m_Value(UpperSrc)))))
2341 return nullptr;
2342 if (*C != HalfWidth || LowerSrc->getType() != UpperSrc->getType() ||
2343 LowerSrc->getType()->getScalarSizeInBits() != HalfWidth)
2344 return nullptr;
2345
2346 auto ConcatIntrinsicCalls = [&](Intrinsic::ID id, Value *Lo, Value *Hi) {
2347 Value *NewLower = Builder.CreateZExt(Lo, Ty);
2348 Value *NewUpper = Builder.CreateZExt(Hi, Ty);
2349 NewUpper = Builder.CreateShl(NewUpper, HalfWidth);
2350 Value *BinOp = Builder.CreateOr(NewLower, NewUpper);
2351 Function *F = Intrinsic::getDeclaration(Or.getModule(), id, Ty);
2352 return Builder.CreateCall(F, BinOp);
2353 };
2354
2355 // BSWAP: Push the concat down, swapping the lower/upper sources.
2356 // concat(bswap(x),bswap(y)) -> bswap(concat(x,y))
2357 Value *LowerBSwap, *UpperBSwap;
2358 if (match(LowerSrc, m_BSwap(m_Value(LowerBSwap))) &&
2359 match(UpperSrc, m_BSwap(m_Value(UpperBSwap))))
2360 return ConcatIntrinsicCalls(Intrinsic::bswap, UpperBSwap, LowerBSwap);
2361
2362 // BITREVERSE: Push the concat down, swapping the lower/upper sources.
2363 // concat(bitreverse(x),bitreverse(y)) -> bitreverse(concat(x,y))
2364 Value *LowerBRev, *UpperBRev;
2365 if (match(LowerSrc, m_BitReverse(m_Value(LowerBRev))) &&
2366 match(UpperSrc, m_BitReverse(m_Value(UpperBRev))))
2367 return ConcatIntrinsicCalls(Intrinsic::bitreverse, UpperBRev, LowerBRev);
2368
2369 return nullptr;
2370}
2371
2372/// If all elements of two constant vectors are 0/-1 and inverses, return true.
2373static bool areInverseVectorBitmasks(Constant *C1, Constant *C2) {
2374 unsigned NumElts = cast<FixedVectorType>(C1->getType())->getNumElements();
2375 for (unsigned i = 0; i != NumElts; ++i) {
2376 Constant *EltC1 = C1->getAggregateElement(i);
2377 Constant *EltC2 = C2->getAggregateElement(i);
2378 if (!EltC1 || !EltC2)
2379 return false;
2380
2381 // One element must be all ones, and the other must be all zeros.
2382 if (!((match(EltC1, m_Zero()) && match(EltC2, m_AllOnes())) ||
2383 (match(EltC2, m_Zero()) && match(EltC1, m_AllOnes()))))
2384 return false;
2385 }
2386 return true;
2387}
2388
2389/// We have an expression of the form (A & C) | (B & D). If A is a scalar or
2390/// vector composed of all-zeros or all-ones values and is the bitwise 'not' of
2391/// B, it can be used as the condition operand of a select instruction.
2392Value *InstCombinerImpl::getSelectCondition(Value *A, Value *B) {
2393 // We may have peeked through bitcasts in the caller.
2394 // Exit immediately if we don't have (vector) integer types.
2395 Type *Ty = A->getType();
2396 if (!Ty->isIntOrIntVectorTy() || !B->getType()->isIntOrIntVectorTy())
2397 return nullptr;
2398
2399 // If A is the 'not' operand of B and has enough signbits, we have our answer.
2400 if (match(B, m_Not(m_Specific(A)))) {
2401 // If these are scalars or vectors of i1, A can be used directly.
2402 if (Ty->isIntOrIntVectorTy(1))
2403 return A;
2404
2405 // If we look through a vector bitcast, the caller will bitcast the operands
2406 // to match the condition's number of bits (N x i1).
2407 // To make this poison-safe, disallow bitcast from wide element to narrow
2408 // element. That could allow poison in lanes where it was not present in the
2409 // original code.
2410 A = peekThroughBitcast(A);
2411 if (A->getType()->isIntOrIntVectorTy()) {
2412 unsigned NumSignBits = ComputeNumSignBits(A);
2413 if (NumSignBits == A->getType()->getScalarSizeInBits() &&
2414 NumSignBits <= Ty->getScalarSizeInBits())
2415 return Builder.CreateTrunc(A, CmpInst::makeCmpResultType(A->getType()));
2416 }
2417 return nullptr;
2418 }
2419
2420 // If both operands are constants, see if the constants are inverse bitmasks.
2421 Constant *AConst, *BConst;
2422 if (match(A, m_Constant(AConst)) && match(B, m_Constant(BConst)))
2423 if (AConst == ConstantExpr::getNot(BConst) &&
2424 ComputeNumSignBits(A) == Ty->getScalarSizeInBits())
2425 return Builder.CreateZExtOrTrunc(A, CmpInst::makeCmpResultType(Ty));
2426
2427 // Look for more complex patterns. The 'not' op may be hidden behind various
2428 // casts. Look through sexts and bitcasts to find the booleans.
2429 Value *Cond;
2430 Value *NotB;
2431 if (match(A, m_SExt(m_Value(Cond))) &&
2432 Cond->getType()->isIntOrIntVectorTy(1)) {
2433 // A = sext i1 Cond; B = sext (not (i1 Cond))
2434 if (match(B, m_SExt(m_Not(m_Specific(Cond)))))
2435 return Cond;
2436
2437 // A = sext i1 Cond; B = not ({bitcast} (sext (i1 Cond)))
2438 // TODO: The one-use checks are unnecessary or misplaced. If the caller
2439 // checked for uses on logic ops/casts, that should be enough to
2440 // make this transform worthwhile.
2441 if (match(B, m_OneUse(m_Not(m_Value(NotB))))) {
2442 NotB = peekThroughBitcast(NotB, true);
2443 if (match(NotB, m_SExt(m_Specific(Cond))))
2444 return Cond;
2445 }
2446 }
2447
2448 // All scalar (and most vector) possibilities should be handled now.
2449 // Try more matches that only apply to non-splat constant vectors.
2450 if (!Ty->isVectorTy())
2451 return nullptr;
2452
2453 // If both operands are xor'd with constants using the same sexted boolean
2454 // operand, see if the constants are inverse bitmasks.
2455 // TODO: Use ConstantExpr::getNot()?
2456 if (match(A, (m_Xor(m_SExt(m_Value(Cond)), m_Constant(AConst)))) &&
2457 match(B, (m_Xor(m_SExt(m_Specific(Cond)), m_Constant(BConst)))) &&
2458 Cond->getType()->isIntOrIntVectorTy(1) &&
2459 areInverseVectorBitmasks(AConst, BConst)) {
2460 AConst = ConstantExpr::getTrunc(AConst, CmpInst::makeCmpResultType(Ty));
2461 return Builder.CreateXor(Cond, AConst);
2462 }
2463 return nullptr;
2464}
2465
2466/// We have an expression of the form (A & C) | (B & D). Try to simplify this
2467/// to "A' ? C : D", where A' is a boolean or vector of booleans.
2468Value *InstCombinerImpl::matchSelectFromAndOr(Value *A, Value *C, Value *B,
2469 Value *D) {
2470 // The potential condition of the select may be bitcasted. In that case, look
2471 // through its bitcast and the corresponding bitcast of the 'not' condition.
2472 Type *OrigType = A->getType();
2473 A = peekThroughBitcast(A, true);
2474 B = peekThroughBitcast(B, true);
2475 if (Value *Cond = getSelectCondition(A, B)) {
2476 // ((bc Cond) & C) | ((bc ~Cond) & D) --> bc (select Cond, (bc C), (bc D))
2477 // If this is a vector, we may need to cast to match the condition's length.
2478 // The bitcasts will either all exist or all not exist. The builder will
2479 // not create unnecessary casts if the types already match.
2480 Type *SelTy = A->getType();
2481 if (auto *VecTy = dyn_cast<VectorType>(Cond->getType())) {
2482 unsigned Elts = VecTy->getElementCount().getKnownMinValue();
2483 Type *EltTy = Builder.getIntNTy(SelTy->getPrimitiveSizeInBits() / Elts);
2484 SelTy = VectorType::get(EltTy, VecTy->getElementCount());
2485 }
2486 Value *BitcastC = Builder.CreateBitCast(C, SelTy);
2487 Value *BitcastD = Builder.CreateBitCast(D, SelTy);
2488 Value *Select = Builder.CreateSelect(Cond, BitcastC, BitcastD);
2489 return Builder.CreateBitCast(Select, OrigType);
2490 }
2491
2492 return nullptr;
2493}
2494
2495/// Fold (icmp)|(icmp) if possible.
2496Value *InstCombinerImpl::foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
2497 BinaryOperator &Or) {
2498 const SimplifyQuery Q = SQ.getWithInstruction(&Or);
2499
2500 // Fold (iszero(A & K1) | iszero(A & K2)) -> (A & (K1 | K2)) != (K1 | K2)
2501 // if K1 and K2 are a one-bit mask.
2502 if (Value *V = foldAndOrOfICmpsOfAndWithPow2(LHS, RHS, &Or,
2503 /* IsAnd */ false))
2504 return V;
2505
2506 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
2507 Value *LHS0 = LHS->getOperand(0), *RHS0 = RHS->getOperand(0);
2508 Value *LHS1 = LHS->getOperand(1), *RHS1 = RHS->getOperand(1);
2509 const APInt *LHSC = nullptr, *RHSC = nullptr;
2510 match(LHS1, m_APInt(LHSC));
2511 match(RHS1, m_APInt(RHSC));
2512
2513 // Fold (icmp ult/ule (A + C1), C3) | (icmp ult/ule (A + C2), C3)
2514 // --> (icmp ult/ule ((A & ~(C1 ^ C2)) + max(C1, C2)), C3)
2515 // The original condition actually refers to the following two ranges:
2516 // [MAX_UINT-C1+1, MAX_UINT-C1+1+C3] and [MAX_UINT-C2+1, MAX_UINT-C2+1+C3]
2517 // We can fold these two ranges if:
2518 // 1) C1 and C2 is unsigned greater than C3.
2519 // 2) The two ranges are separated.
2520 // 3) C1 ^ C2 is one-bit mask.
2521 // 4) LowRange1 ^ LowRange2 and HighRange1 ^ HighRange2 are one-bit mask.
2522 // This implies all values in the two ranges differ by exactly one bit.
2523 if ((PredL == ICmpInst::ICMP_ULT || PredL == ICmpInst::ICMP_ULE) &&
2524 PredL == PredR && LHSC && RHSC && LHS->hasOneUse() && RHS->hasOneUse() &&
2525 LHSC->getBitWidth() == RHSC->getBitWidth() && *LHSC == *RHSC) {
2526
2527 Value *AddOpnd;
2528 const APInt *LAddC, *RAddC;
2529 if (match(LHS0, m_Add(m_Value(AddOpnd), m_APInt(LAddC))) &&
2530 match(RHS0, m_Add(m_Specific(AddOpnd), m_APInt(RAddC))) &&
2531 LAddC->ugt(*LHSC) && RAddC->ugt(*LHSC)) {
2532
2533 APInt DiffC = *LAddC ^ *RAddC;
2534 if (DiffC.isPowerOf2()) {
2535 const APInt *MaxAddC = nullptr;
2536 if (LAddC->ult(*RAddC))
2537 MaxAddC = RAddC;
2538 else
2539 MaxAddC = LAddC;
2540
2541 APInt RRangeLow = -*RAddC;
2542 APInt RRangeHigh = RRangeLow + *LHSC;
2543 APInt LRangeLow = -*LAddC;
2544 APInt LRangeHigh = LRangeLow + *LHSC;
2545 APInt LowRangeDiff = RRangeLow ^ LRangeLow;
2546 APInt HighRangeDiff = RRangeHigh ^ LRangeHigh;
2547 APInt RangeDiff = LRangeLow.sgt(RRangeLow) ? LRangeLow - RRangeLow
2548 : RRangeLow - LRangeLow;
2549
2550 if (LowRangeDiff.isPowerOf2() && LowRangeDiff == HighRangeDiff &&
2551 RangeDiff.ugt(*LHSC)) {
2552 Type *Ty = AddOpnd->getType();
2553 Value *MaskC = ConstantInt::get(Ty, ~DiffC);
2554
2555 Value *NewAnd = Builder.CreateAnd(AddOpnd, MaskC);
2556 Value *NewAdd = Builder.CreateAdd(NewAnd,
2557 ConstantInt::get(Ty, *MaxAddC));
2558 return Builder.CreateICmp(LHS->getPredicate(), NewAdd,
2559 ConstantInt::get(Ty, *LHSC));
2560 }
2561 }
2562 }
2563 }
2564
2565 // (icmp1 A, B) | (icmp2 A, B) --> (icmp3 A, B)
2566 if (predicatesFoldable(PredL, PredR)) {
2567 if (LHS0 == RHS1 && LHS1 == RHS0)
2568 LHS->swapOperands();
2569 if (LHS0 == RHS0 && LHS1 == RHS1) {
2570 unsigned Code = getICmpCode(LHS) | getICmpCode(RHS);
2571 bool IsSigned = LHS->isSigned() || RHS->isSigned();
2572 return getNewICmpValue(Code, IsSigned, LHS0, LHS1, Builder);
2573 }
2574 }
2575
2576 // handle (roughly):
2577 // (icmp ne (A & B), C) | (icmp ne (A & D), E)
2578 if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, false, Builder))
2579 return V;
2580
2581 if (LHS->hasOneUse() || RHS->hasOneUse()) {
2582 // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
2583 // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
2584 Value *A = nullptr, *B = nullptr;
2585 if (PredL == ICmpInst::ICMP_EQ && match(LHS1, m_Zero())) {
2586 B = LHS0;
2587 if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS1)
2588 A = RHS0;
2589 else if (PredR == ICmpInst::ICMP_UGT && LHS0 == RHS0)
2590 A = RHS1;
2591 }
2592 // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
2593 // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
2594 else if (PredR == ICmpInst::ICMP_EQ && match(RHS1, m_Zero())) {
2595 B = RHS0;
2596 if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS1)
2597 A = LHS0;
2598 else if (PredL == ICmpInst::ICMP_UGT && RHS0 == LHS0)
2599 A = LHS1;
2600 }
2601 if (A && B && B->getType()->isIntOrIntVectorTy())
2602 return Builder.CreateICmp(
2603 ICmpInst::ICMP_UGE,
2604 Builder.CreateAdd(B, Constant::getAllOnesValue(B->getType())), A);
2605 }
2606
2607 if (Value *V = foldAndOrOfICmpsWithConstEq(LHS, RHS, Or, Builder, Q))
2608 return V;
2609 if (Value *V = foldAndOrOfICmpsWithConstEq(RHS, LHS, Or, Builder, Q))
2610 return V;
2611
2612 // E.g. (icmp slt x, 0) | (icmp sgt x, n) --> icmp ugt x, n
2613 if (Value *V = simplifyRangeCheck(LHS, RHS, /*Inverted=*/true))
2614 return V;
2615
2616 // E.g. (icmp sgt x, n) | (icmp slt x, 0) --> icmp ugt x, n
2617 if (Value *V = simplifyRangeCheck(RHS, LHS, /*Inverted=*/true))
2618 return V;
2619
2620 if (Value *V = foldAndOrOfEqualityCmpsWithConstants(LHS, RHS, false, Builder))
2621 return V;
2622
2623 if (Value *V = foldIsPowerOf2(LHS, RHS, false /* JoinedByAnd */, Builder))
2624 return V;
2625
2626 if (Value *X =
2627 foldUnsignedUnderflowCheck(LHS, RHS, /*IsAnd=*/false, Q, Builder))
2628 return X;
2629 if (Value *X =
2630 foldUnsignedUnderflowCheck(RHS, LHS, /*IsAnd=*/false, Q, Builder))
2631 return X;
2632
2633 if (Value *X = foldEqOfParts(LHS, RHS, /*IsAnd=*/false))
2634 return X;
2635
2636 // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
2637 // TODO: Remove this when foldLogOpOfMaskedICmps can handle undefs.
2638 if (PredL == ICmpInst::ICMP_NE && match(LHS1, m_ZeroInt()) &&
2639 PredR == ICmpInst::ICMP_NE && match(RHS1, m_ZeroInt()) &&
2640 LHS0->getType() == RHS0->getType()) {
2641 Value *NewOr = Builder.CreateOr(LHS0, RHS0);
2642 return Builder.CreateICmp(PredL, NewOr,
2643 Constant::getNullValue(NewOr->getType()));
2644 }
2645
2646 // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2).
2647 if (!LHSC || !RHSC)
2648 return nullptr;
2649
2650 return foldAndOrOfICmpsUsingRanges(PredL, LHS0, *LHSC, PredR, RHS0, *RHSC,
2651 Builder, /* IsAnd */ false);
2652}
2653
2654// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
2655// here. We should standardize that construct where it is needed or choose some
2656// other way to ensure that commutated variants of patterns are not missed.
2657Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
2658 if (Value *V = SimplifyOrInst(I.getOperand(0), I.getOperand(1),
2659 SQ.getWithInstruction(&I)))
2660 return replaceInstUsesWith(I, V);
2661
2662 if (SimplifyAssociativeOrCommutative(I))
2663 return &I;
2664
2665 if (Instruction *X = foldVectorBinop(I))
2666 return X;
2667
2668 // See if we can simplify any instructions used by the instruction whose sole
2669 // purpose is to compute bits we don't care about.
2670 if (SimplifyDemandedInstructionBits(I))
2671 return &I;
2672
2673 // Do this before using distributive laws to catch simple and/or/not patterns.
2674 if (Instruction *Xor = foldOrToXor(I, Builder))
2675 return Xor;
2676
2677 if (Instruction *X = foldComplexAndOrPatterns(I, Builder))
2678 return X;
2679
2680 // (A&B)|(A&C) -> A&(B|C) etc
2681 if (Value *V = SimplifyUsingDistributiveLaws(I))
2682 return replaceInstUsesWith(I, V);
2683
2684 if (Value *V = SimplifyBSwap(I, Builder))
2685 return replaceInstUsesWith(I, V);
2686
2687 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
2688 Type *Ty = I.getType();
2689 if (Ty->isIntOrIntVectorTy(1)) {
2690 if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
2691 if (auto *I =
2692 foldAndOrOfSelectUsingImpliedCond(Op1, *SI0, /* IsAnd */ false))
2693 return I;
2694 }
2695 if (auto *SI1 = dyn_cast<SelectInst>(Op1)) {
2696 if (auto *I =
2697 foldAndOrOfSelectUsingImpliedCond(Op0, *SI1, /* IsAnd */ false))
2698 return I;
2699 }
2700 }
2701
2702 if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
2703 return FoldedLogic;
2704
2705 if (Instruction *BitOp = matchBSwapOrBitReverse(I, /*MatchBSwaps*/ true,
2706 /*MatchBitReversals*/ true))
2707 return BitOp;
2708
2709 if (Instruction *Funnel = matchFunnelShift(I, *this))
2710 return Funnel;
2711
2712 if (Instruction *Concat = matchOrConcat(I, Builder))
2713 return replaceInstUsesWith(I, Concat);
2714
2715 Value *X, *Y;
2716 const APInt *CV;
2717 if (match(&I, m_c_Or(m_OneUse(m_Xor(m_Value(X), m_APInt(CV))), m_Value(Y))) &&
2718 !CV->isAllOnes() && MaskedValueIsZero(Y, *CV, 0, &I)) {
2719 // (X ^ C) | Y -> (X | Y) ^ C iff Y & C == 0
2720 // The check for a 'not' op is for efficiency (if Y is known zero --> ~X).
2721 Value *Or = Builder.CreateOr(X, Y);
2722 return BinaryOperator::CreateXor(Or, ConstantInt::get(Ty, *CV));
2723 }
2724
2725 // If the operands have no common bits set:
2726 // or (mul X, Y), X --> add (mul X, Y), X --> mul X, (Y + 1)
2727 if (match(&I,
2728 m_c_Or(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), m_Deferred(X))) &&
2729 haveNoCommonBitsSet(Op0, Op1, DL)) {
2730 Value *IncrementY = Builder.CreateAdd(Y, ConstantInt::get(Ty, 1));
2731 return BinaryOperator::CreateMul(X, IncrementY);
2732 }
2733
2734 // (A & C) | (B & D)
2735 Value *A, *B, *C, *D;
2736 if (match(Op0, m_And(m_Value(A), m_Value(C))) &&
2737 match(Op1, m_And(m_Value(B), m_Value(D)))) {
2738
2739 // (A & C0) | (B & C1)
2740 const APInt *C0, *C1;
2741 if (match(C, m_APInt(C0)) && match(D, m_APInt(C1))) {
2742 Value *X;
2743 if (*C0 == ~*C1) {
2744 // ((X | B) & MaskC) | (B & ~MaskC) -> (X & MaskC) | B
2745 if (match(A, m_c_Or(m_Value(X), m_Specific(B))))
2746 return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C0), B);
2747 // (A & MaskC) | ((X | A) & ~MaskC) -> (X & ~MaskC) | A
2748 if (match(B, m_c_Or(m_Specific(A), m_Value(X))))
2749 return BinaryOperator::CreateOr(Builder.CreateAnd(X, *C1), A);
2750
2751 // ((X ^ B) & MaskC) | (B & ~MaskC) -> (X & MaskC) ^ B
2752 if (match(A, m_c_Xor(m_Value(X), m_Specific(B))))
2753 return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C0), B);
2754 // (A & MaskC) | ((X ^ A) & ~MaskC) -> (X & ~MaskC) ^ A
2755 if (match(B, m_c_Xor(m_Specific(A), m_Value(X))))
2756 return BinaryOperator::CreateXor(Builder.CreateAnd(X, *C1), A);
2757 }
2758
2759 if ((*C0 & *C1).isZero()) {
2760 // ((X | B) & C0) | (B & C1) --> (X | B) & (C0 | C1)
2761 // iff (C0 & C1) == 0 and (X & ~C0) == 0
2762 if (match(A, m_c_Or(m_Value(X), m_Specific(B))) &&
2763 MaskedValueIsZero(X, ~*C0, 0, &I)) {
2764 Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2765 return BinaryOperator::CreateAnd(A, C01);
2766 }
2767 // (A & C0) | ((X | A) & C1) --> (X | A) & (C0 | C1)
2768 // iff (C0 & C1) == 0 and (X & ~C1) == 0
2769 if (match(B, m_c_Or(m_Value(X), m_Specific(A))) &&
2770 MaskedValueIsZero(X, ~*C1, 0, &I)) {
2771 Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2772 return BinaryOperator::CreateAnd(B, C01);
2773 }
2774 // ((X | C2) & C0) | ((X | C3) & C1) --> (X | C2 | C3) & (C0 | C1)
2775 // iff (C0 & C1) == 0 and (C2 & ~C0) == 0 and (C3 & ~C1) == 0.
2776 const APInt *C2, *C3;
2777 if (match(A, m_Or(m_Value(X), m_APInt(C2))) &&
2778 match(B, m_Or(m_Specific(X), m_APInt(C3))) &&
2779 (*C2 & ~*C0).isZero() && (*C3 & ~*C1).isZero()) {
2780 Value *Or = Builder.CreateOr(X, *C2 | *C3, "bitfield");
2781 Constant *C01 = ConstantInt::get(Ty, *C0 | *C1);
2782 return BinaryOperator::CreateAnd(Or, C01);
2783 }
2784 }
2785 }
2786
2787 // Don't try to form a select if it's unlikely that we'll get rid of at
2788 // least one of the operands. A select is generally more expensive than the
2789 // 'or' that it is replacing.
2790 if (Op0->hasOneUse() || Op1->hasOneUse()) {
2791 // (Cond & C) | (~Cond & D) -> Cond ? C : D, and commuted variants.
2792 if (Value *V = matchSelectFromAndOr(A, C, B, D))
2793 return replaceInstUsesWith(I, V);
2794 if (Value *V = matchSelectFromAndOr(A, C, D, B))
2795 return replaceInstUsesWith(I, V);
2796 if (Value *V = matchSelectFromAndOr(C, A, B, D))
2797 return replaceInstUsesWith(I, V);
2798 if (Value *V = matchSelectFromAndOr(C, A, D, B))
2799 return replaceInstUsesWith(I, V);
2800 if (Value *V = matchSelectFromAndOr(B, D, A, C))
2801 return replaceInstUsesWith(I, V);
2802 if (Value *V = matchSelectFromAndOr(B, D, C, A))
2803 return replaceInstUsesWith(I, V);
2804 if (Value *V = matchSelectFromAndOr(D, B, A, C))
2805 return replaceInstUsesWith(I, V);
2806 if (Value *V = matchSelectFromAndOr(D, B, C, A))
2807 return replaceInstUsesWith(I, V);
2808 }
2809 }
2810
2811 // (A ^ B) | ((B ^ C) ^ A) -> (A ^ B) | C
2812 if (match(Op0, m_Xor(m_Value(A), m_Value(B))))
2813 if (match(Op1, m_Xor(m_Xor(m_Specific(B), m_Value(C)), m_Specific(A))))
2814 return BinaryOperator::CreateOr(Op0, C);
2815
2816 // ((A ^ C) ^ B) | (B ^ A) -> (B ^ A) | C
2817 if (match(Op0, m_Xor(m_Xor(m_Value(A), m_Value(C)), m_Value(B))))
2818 if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
2819 return BinaryOperator::CreateOr(Op1, C);
2820
2821 // ((B | C) & A) | B -> B | (A & C)
2822 if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
2823 return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
2824
2825 if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
2826 return DeMorgan;
2827
2828 // Canonicalize xor to the RHS.
2829 bool SwappedForXor = false;
2830 if (match(Op0, m_Xor(m_Value(), m_Value()))) {
2831 std::swap(Op0, Op1);
2832 SwappedForXor = true;
2833 }
2834
2835 // A | ( A ^ B) -> A | B
2836 // A | (~A ^ B) -> A | ~B
2837 // (A & B) | (A ^ B)
2838 // ~A | (A ^ B) -> ~(A & B)
2839 // The swap above should always make Op0 the 'not' for the last case.
2840 if (match(Op1, m_Xor(m_Value(A), m_Value(B)))) {
2841 if (Op0 == A || Op0 == B)
2842 return BinaryOperator::CreateOr(A, B);
2843
2844 if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||
2845 match(Op0, m_And(m_Specific(B), m_Specific(A))))
2846 return BinaryOperator::CreateOr(A, B);
2847
2848 if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
2849 (match(Op0, m_Not(m_Specific(A))) || match(Op0, m_Not(m_Specific(B)))))
2850 return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
2851
2852 if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
2853 Value *Not = Builder.CreateNot(B, B->getName() + ".not");
2854 return BinaryOperator::CreateOr(Not, Op0);
2855 }
2856 if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
2857 Value *Not = Builder.CreateNot(A, A->getName() + ".not");
2858 return BinaryOperator::CreateOr(Not, Op0);
2859 }
2860 }
2861
2862 // A | ~(A | B) -> A | ~B
2863 // A | ~(A ^ B) -> A | ~B
2864 if (match(Op1, m_Not(m_Value(A))))
2865 if (BinaryOperator *B = dyn_cast<BinaryOperator>(A))
2866 if ((Op0 == B->getOperand(0) || Op0 == B->getOperand(1)) &&
2867 Op1->hasOneUse() && (B->getOpcode() == Instruction::Or ||
2868 B->getOpcode() == Instruction::Xor)) {
2869 Value *NotOp = Op0 == B->getOperand(0) ? B->getOperand(1) :
2870 B->getOperand(0);
2871 Value *Not = Builder.CreateNot(NotOp, NotOp->getName() + ".not");
2872 return BinaryOperator::CreateOr(Not, Op0);
2873 }
2874
2875 if (SwappedForXor)
2876 std::swap(Op0, Op1);
2877
2878 {
2879 ICmpInst *LHS = dyn_cast<ICmpInst>(Op0);
2880 ICmpInst *RHS = dyn_cast<ICmpInst>(Op1);
2881 if (LHS && RHS)
2882 if (Value *Res = foldOrOfICmps(LHS, RHS, I))
2883 return replaceInstUsesWith(I, Res);
2884
2885 // TODO: Make this recursive; it's a little tricky because an arbitrary
2886 // number of 'or' instructions might have to be created.
2887 Value *X, *Y;
2888 if (LHS && match(Op1, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2889 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2890 if (Value *Res = foldOrOfICmps(LHS, Cmp, I))
2891 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2892 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2893 if (Value *Res = foldOrOfICmps(LHS, Cmp, I))
2894 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2895 }
2896 if (RHS && match(Op0, m_OneUse(m_Or(m_Value(X), m_Value(Y))))) {
2897 if (auto *Cmp = dyn_cast<ICmpInst>(X))
2898 if (Value *Res = foldOrOfICmps(Cmp, RHS, I))
2899 return replaceInstUsesWith(I, Builder.CreateOr(Res, Y));
2900 if (auto *Cmp = dyn_cast<ICmpInst>(Y))
2901 if (Value *Res = foldOrOfICmps(Cmp, RHS, I))
2902 return replaceInstUsesWith(I, Builder.CreateOr(Res, X));
2903 }
2904 }
2905
2906 if (FCmpInst *LHS = dyn_cast<FCmpInst>(I.getOperand(0)))
2907 if (FCmpInst *RHS = dyn_cast<FCmpInst>(I.getOperand(1)))
2908 if (Value *Res = foldLogicOfFCmps(LHS, RHS, false))
2909 return replaceInstUsesWith(I, Res);
2910
2911 if (Instruction *FoldedFCmps = reassociateFCmps(I, Builder))
2912 return FoldedFCmps;
2913
2914 if (Instruction *CastedOr = foldCastedBitwiseLogic(I))
2915 return CastedOr;
2916
2917 if (Instruction *Sel = foldBinopOfSextBoolToSelect(I))
2918 return Sel;
2919
2920 // or(sext(A), B) / or(B, sext(A)) --> A ? -1 : B, where A is i1 or <N x i1>.
2921 // TODO: Move this into foldBinopOfSextBoolToSelect as a more generalized fold
2922 // with binop identity constant. But creating a select with non-constant
2923 // arm may not be reversible due to poison semantics. Is that a good
2924 // canonicalization?
2925 if (match(Op0, m_OneUse(m_SExt(m_Value(A)))) &&
2926 A->getType()->isIntOrIntVectorTy(1))
2927 return SelectInst::Create(A, ConstantInt::getAllOnesValue(Ty), Op1);
2928 if (match(Op1, m_OneUse(m_SExt(m_Value(A)))) &&
2929 A->getType()->isIntOrIntVectorTy(1))
2930 return SelectInst::Create(A, ConstantInt::getAllOnesValue(Ty), Op0);
2931
2932 // Note: If we've gotten to the point of visiting the outer OR, then the
2933 // inner one couldn't be simplified. If it was a constant, then it won't
2934 // be simplified by a later pass either, so we try swapping the inner/outer
2935 // ORs in the hopes that we'll be able to simplify it this way.
2936 // (X|C) | V --> (X|V) | C
2937 ConstantInt *CI;
2938 if (Op0->hasOneUse() && !match(Op1, m_ConstantInt()) &&
2939 match(Op0, m_Or(m_Value(A), m_ConstantInt(CI)))) {
2940 Value *Inner = Builder.CreateOr(A, Op1);
2941 Inner->takeName(Op0);
2942 return BinaryOperator::CreateOr(Inner, CI);
2943 }
2944
2945 // Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
2946 // Since this OR statement hasn't been optimized further yet, we hope
2947 // that this transformation will allow the new ORs to be optimized.
2948 {
2949 Value *X = nullptr, *Y = nullptr;
2950 if (Op0->hasOneUse() && Op1->hasOneUse() &&
2951 match(Op0, m_Select(m_Value(X), m_Value(A), m_Value(B))) &&
2952 match(Op1, m_Select(m_Value(Y), m_Value(C), m_Value(D))) && X == Y) {
2953 Value *orTrue = Builder.CreateOr(A, C);
2954 Value *orFalse = Builder.CreateOr(B, D);
2955 return SelectInst::Create(X, orTrue, orFalse);
2956 }
2957 }
2958
2959 // or(ashr(subNSW(Y, X), ScalarSizeInBits(Y) - 1), X) --> X s> Y ? -1 : X.
2960 {
2961 Value *X, *Y;
2962 if (match(&I, m_c_Or(m_OneUse(m_AShr(
2963 m_NSWSub(m_Value(Y), m_Value(X)),
2964 m_SpecificInt(Ty->getScalarSizeInBits() - 1))),
2965 m_Deferred(X)))) {
2966 Value *NewICmpInst = Builder.CreateICmpSGT(X, Y);
2967 Value *AllOnes = ConstantInt::getAllOnesValue(Ty);
2968 return SelectInst::Create(NewICmpInst, AllOnes, X);
2969 }
2970 }
2971
2972 if (Instruction *V =
2973 canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(I))
2974 return V;
2975
2976 CmpInst::Predicate Pred;
2977 Value *Mul, *Ov, *MulIsNotZero, *UMulWithOv;
2978 // Check if the OR weakens the overflow condition for umul.with.overflow by
2979 // treating any non-zero result as overflow. In that case, we overflow if both
2980 // umul.with.overflow operands are != 0, as in that case the result can only
2981 // be 0, iff the multiplication overflows.
2982 if (match(&I,
2983 m_c_Or(m_CombineAnd(m_ExtractValue<1>(m_Value(UMulWithOv)),
2984 m_Value(Ov)),
2985 m_CombineAnd(m_ICmp(Pred,
2986 m_CombineAnd(m_ExtractValue<0>(
2987 m_Deferred(UMulWithOv)),
2988 m_Value(Mul)),
2989 m_ZeroInt()),
2990 m_Value(MulIsNotZero)))) &&
2991 (Ov->hasOneUse() || (MulIsNotZero->hasOneUse() && Mul->hasOneUse())) &&
2992 Pred == CmpInst::ICMP_NE) {
2993 Value *A, *B;
2994 if (match(UMulWithOv, m_Intrinsic<Intrinsic::umul_with_overflow>(
2995 m_Value(A), m_Value(B)))) {
2996 Value *NotNullA = Builder.CreateIsNotNull(A);
2997 Value *NotNullB = Builder.CreateIsNotNull(B);
2998 return BinaryOperator::CreateAnd(NotNullA, NotNullB);
2999 }
3000 }
3001
3002 // (~x) | y --> ~(x & (~y)) iff that gets rid of inversions
3003 if (sinkNotIntoOtherHandOfAndOrOr(I))
3004 return &I;
3005
3006 // Improve "get low bit mask up to and including bit X" pattern:
3007 // (1 << X) | ((1 << X) + -1) --> -1 l>> (bitwidth(x) - 1 - X)
3008 if (match(&I, m_c_Or(m_Add(m_Shl(m_One(), m_Value(X)), m_AllOnes()),
3009 m_Shl(m_One(), m_Deferred(X)))) &&
3010 match(&I, m_c_Or(m_OneUse(m_Value()), m_Value()))) {
3011 Value *Sub = Builder.CreateSub(
3012 ConstantInt::get(Ty, Ty->getScalarSizeInBits() - 1), X);
3013 return BinaryOperator::CreateLShr(Constant::getAllOnesValue(Ty), Sub);
3014 }
3015
3016 // An or recurrence w/loop invariant step is equivelent to (or start, step)
3017 PHINode *PN = nullptr;
3018 Value *Start = nullptr, *Step = nullptr;
3019 if (matchSimpleRecurrence(&I, PN, Start, Step) && DT.dominates(Step, PN))
3020 return replaceInstUsesWith(I, Builder.CreateOr(Start, Step));
3021
3022 return nullptr;
3023}
3024
3025/// A ^ B can be specified using other logic ops in a variety of patterns. We
3026/// can fold these early and efficiently by morphing an existing instruction.
3027static Instruction *foldXorToXor(BinaryOperator &I,
3028 InstCombiner::BuilderTy &Builder) {
3029 assert(I.getOpcode() == Instruction::Xor)(static_cast <bool> (I.getOpcode() == Instruction::Xor)
? void (0) : __assert_fail ("I.getOpcode() == Instruction::Xor"
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 3029
, __extension__ __PRETTY_FUNCTION__))
;
3030 Value *Op0 = I.getOperand(0);
3031 Value *Op1 = I.getOperand(1);
3032 Value *A, *B;
3033
3034 // There are 4 commuted variants for each of the basic patterns.
3035
3036 // (A & B) ^ (A | B) -> A ^ B
3037 // (A & B) ^ (B | A) -> A ^ B
3038 // (A | B) ^ (A & B) -> A ^ B
3039 // (A | B) ^ (B & A) -> A ^ B
3040 if (match(&I, m_c_Xor(m_And(m_Value(A), m_Value(B)),
3041 m_c_Or(m_Deferred(A), m_Deferred(B)))))
3042 return BinaryOperator::CreateXor(A, B);
3043
3044 // (A | ~B) ^ (~A | B) -> A ^ B
3045 // (~B | A) ^ (~A | B) -> A ^ B
3046 // (~A | B) ^ (A | ~B) -> A ^ B
3047 // (B | ~A) ^ (A | ~B) -> A ^ B
3048 if (match(&I, m_Xor(m_c_Or(m_Value(A), m_Not(m_Value(B))),
3049 m_c_Or(m_Not(m_Deferred(A)), m_Deferred(B)))))
3050 return BinaryOperator::CreateXor(A, B);
3051
3052 // (A & ~B) ^ (~A & B) -> A ^ B
3053 // (~B & A) ^ (~A & B) -> A ^ B
3054 // (~A & B) ^ (A & ~B) -> A ^ B
3055 // (B & ~A) ^ (A & ~B) -> A ^ B
3056 if (match(&I, m_Xor(m_c_And(m_Value(A), m_Not(m_Value(B))),
3057 m_c_And(m_Not(m_Deferred(A)), m_Deferred(B)))))
3058 return BinaryOperator::CreateXor(A, B);
3059
3060 // For the remaining cases we need to get rid of one of the operands.
3061 if (!Op0->hasOneUse() && !Op1->hasOneUse())
3062 return nullptr;
3063
3064 // (A | B) ^ ~(A & B) -> ~(A ^ B)
3065 // (A | B) ^ ~(B & A) -> ~(A ^ B)
3066 // (A & B) ^ ~(A | B) -> ~(A ^ B)
3067 // (A & B) ^ ~(B | A) -> ~(A ^ B)
3068 // Complexity sorting ensures the not will be on the right side.
3069 if ((match(Op0, m_Or(m_Value(A), m_Value(B))) &&
3070 match(Op1, m_Not(m_c_And(m_Specific(A), m_Specific(B))))) ||
3071 (match(Op0, m_And(m_Value(A), m_Value(B))) &&
3072 match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B))))))
3073 return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
3074
3075 return nullptr;
3076}
3077
3078Value *InstCombinerImpl::foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS,
3079 BinaryOperator &I) {
3080 assert(I.getOpcode() == Instruction::Xor && I.getOperand(0) == LHS &&(static_cast <bool> (I.getOpcode() == Instruction::Xor &&
I.getOperand(0) == LHS && I.getOperand(1) == RHS &&
"Should be 'xor' with these operands") ? void (0) : __assert_fail
("I.getOpcode() == Instruction::Xor && I.getOperand(0) == LHS && I.getOperand(1) == RHS && \"Should be 'xor' with these operands\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 3081
, __extension__ __PRETTY_FUNCTION__))
3081 I.getOperand(1) == RHS && "Should be 'xor' with these operands")(static_cast <bool> (I.getOpcode() == Instruction::Xor &&
I.getOperand(0) == LHS && I.getOperand(1) == RHS &&
"Should be 'xor' with these operands") ? void (0) : __assert_fail
("I.getOpcode() == Instruction::Xor && I.getOperand(0) == LHS && I.getOperand(1) == RHS && \"Should be 'xor' with these operands\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 3081
, __extension__ __PRETTY_FUNCTION__))
;
3082
3083 if (predicatesFoldable(LHS->getPredicate(), RHS->getPredicate())) {
3084 if (LHS->getOperand(0) == RHS->getOperand(1) &&
3085 LHS->getOperand(1) == RHS->getOperand(0))
3086 LHS->swapOperands();
3087 if (LHS->getOperand(0) == RHS->getOperand(0) &&
3088 LHS->getOperand(1) == RHS->getOperand(1)) {
3089 // (icmp1 A, B) ^ (icmp2 A, B) --> (icmp3 A, B)
3090 Value *Op0 = LHS->getOperand(0), *Op1 = LHS->getOperand(1);
3091 unsigned Code = getICmpCode(LHS) ^ getICmpCode(RHS);
3092 bool IsSigned = LHS->isSigned() || RHS->isSigned();
3093 return getNewICmpValue(Code, IsSigned, Op0, Op1, Builder);
3094 }
3095 }
3096
3097 // TODO: This can be generalized to compares of non-signbits using
3098 // decomposeBitTestICmp(). It could be enhanced more by using (something like)
3099 // foldLogOpOfMaskedICmps().
3100 ICmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
3101 Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
3102 Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
3103 if ((LHS->hasOneUse() || RHS->hasOneUse()) &&
3104 LHS0->getType() == RHS0->getType() &&
3105 LHS0->getType()->isIntOrIntVectorTy()) {
3106 // (X > -1) ^ (Y > -1) --> (X ^ Y) < 0
3107 // (X < 0) ^ (Y < 0) --> (X ^ Y) < 0
3108 if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) &&
3109 PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes())) ||
3110 (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) &&
3111 PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero()))) {
3112 Value *Zero = ConstantInt::getNullValue(LHS0->getType());
3113 return Builder.CreateICmpSLT(Builder.CreateXor(LHS0, RHS0), Zero);
3114 }
3115 // (X > -1) ^ (Y < 0) --> (X ^ Y) > -1
3116 // (X < 0) ^ (Y > -1) --> (X ^ Y) > -1
3117 if ((PredL == CmpInst::ICMP_SGT && match(LHS1, m_AllOnes()) &&
3118 PredR == CmpInst::ICMP_SLT && match(RHS1, m_Zero())) ||
3119 (PredL == CmpInst::ICMP_SLT && match(LHS1, m_Zero()) &&
3120 PredR == CmpInst::ICMP_SGT && match(RHS1, m_AllOnes()))) {
3121 Value *MinusOne = ConstantInt::getAllOnesValue(LHS0->getType());
3122 return Builder.CreateICmpSGT(Builder.CreateXor(LHS0, RHS0), MinusOne);
3123 }
3124 }
3125
3126 // Instead of trying to imitate the folds for and/or, decompose this 'xor'
3127 // into those logic ops. That is, try to turn this into an and-of-icmps
3128 // because we have many folds for that pattern.
3129 //
3130 // This is based on a truth table definition of xor:
3131 // X ^ Y --> (X | Y) & !(X & Y)
3132 if (Value *OrICmp = SimplifyBinOp(Instruction::Or, LHS, RHS, SQ)) {
3133 // TODO: If OrICmp is true, then the definition of xor simplifies to !(X&Y).
3134 // TODO: If OrICmp is false, the whole thing is false (InstSimplify?).
3135 if (Value *AndICmp = SimplifyBinOp(Instruction::And, LHS, RHS, SQ)) {
3136 // TODO: Independently handle cases where the 'and' side is a constant.
3137 ICmpInst *X = nullptr, *Y = nullptr;
3138 if (OrICmp == LHS && AndICmp == RHS) {
3139 // (LHS | RHS) & !(LHS & RHS) --> LHS & !RHS --> X & !Y
3140 X = LHS;
3141 Y = RHS;
3142 }
3143 if (OrICmp == RHS && AndICmp == LHS) {
3144 // !(LHS & RHS) & (LHS | RHS) --> !LHS & RHS --> !Y & X
3145 X = RHS;
3146 Y = LHS;
3147 }
3148 if (X && Y && (Y->hasOneUse() || canFreelyInvertAllUsersOf(Y, &I))) {
3149 // Invert the predicate of 'Y', thus inverting its output.
3150 Y->setPredicate(Y->getInversePredicate());
3151 // So, are there other uses of Y?
3152 if (!Y->hasOneUse()) {
3153 // We need to adapt other uses of Y though. Get a value that matches
3154 // the original value of Y before inversion. While this increases
3155 // immediate instruction count, we have just ensured that all the
3156 // users are freely-invertible, so that 'not' *will* get folded away.
3157 BuilderTy::InsertPointGuard Guard(Builder);
3158 // Set insertion point to right after the Y.
3159 Builder.SetInsertPoint(Y->getParent(), ++(Y->getIterator()));
3160 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3161 // Replace all uses of Y (excluding the one in NotY!) with NotY.
3162 Worklist.pushUsersToWorkList(*Y);
3163 Y->replaceUsesWithIf(NotY,
3164 [NotY](Use &U) { return U.getUser() != NotY; });
3165 }
3166 // All done.
3167 return Builder.CreateAnd(LHS, RHS);
3168 }
3169 }
3170 }
3171
3172 return nullptr;
3173}
3174
3175/// If we have a masked merge, in the canonical form of:
3176/// (assuming that A only has one use.)
3177/// | A | |B|
3178/// ((x ^ y) & M) ^ y
3179/// | D |
3180/// * If M is inverted:
3181/// | D |
3182/// ((x ^ y) & ~M) ^ y
3183/// We can canonicalize by swapping the final xor operand
3184/// to eliminate the 'not' of the mask.
3185/// ((x ^ y) & M) ^ x
3186/// * If M is a constant, and D has one use, we transform to 'and' / 'or' ops
3187/// because that shortens the dependency chain and improves analysis:
3188/// (x & M) | (y & ~M)
3189static Instruction *visitMaskedMerge(BinaryOperator &I,
3190 InstCombiner::BuilderTy &Builder) {
3191 Value *B, *X, *D;
3192 Value *M;
3193 if (!match(&I, m_c_Xor(m_Value(B),
3194 m_OneUse(m_c_And(
3195 m_CombineAnd(m_c_Xor(m_Deferred(B), m_Value(X)),
3196 m_Value(D)),
3197 m_Value(M))))))
3198 return nullptr;
3199
3200 Value *NotM;
3201 if (match(M, m_Not(m_Value(NotM)))) {
3202 // De-invert the mask and swap the value in B part.
3203 Value *NewA = Builder.CreateAnd(D, NotM);
3204 return BinaryOperator::CreateXor(NewA, X);
3205 }
3206
3207 Constant *C;
3208 if (D->hasOneUse() && match(M, m_Constant(C))) {
3209 // Propagating undef is unsafe. Clamp undef elements to -1.
3210 Type *EltTy = C->getType()->getScalarType();
3211 C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
3212 // Unfold.
3213 Value *LHS = Builder.CreateAnd(X, C);
3214 Value *NotC = Builder.CreateNot(C);
3215 Value *RHS = Builder.CreateAnd(B, NotC);
3216 return BinaryOperator::CreateOr(LHS, RHS);
3217 }
3218
3219 return nullptr;
3220}
3221
3222// Transform
3223// ~(x ^ y)
3224// into:
3225// (~x) ^ y
3226// or into
3227// x ^ (~y)
3228static Instruction *sinkNotIntoXor(BinaryOperator &I,
3229 InstCombiner::BuilderTy &Builder) {
3230 Value *X, *Y;
3231 // FIXME: one-use check is not needed in general, but currently we are unable
3232 // to fold 'not' into 'icmp', if that 'icmp' has multiple uses. (D35182)
3233 if (!match(&I, m_Not(m_OneUse(m_Xor(m_Value(X), m_Value(Y))))))
3234 return nullptr;
3235
3236 // We only want to do the transform if it is free to do.
3237 if (InstCombiner::isFreeToInvert(X, X->hasOneUse())) {
3238 // Ok, good.
3239 } else if (InstCombiner::isFreeToInvert(Y, Y->hasOneUse())) {
3240 std::swap(X, Y);
3241 } else
3242 return nullptr;
3243
3244 Value *NotX = Builder.CreateNot(X, X->getName() + ".not");
3245 return BinaryOperator::CreateXor(NotX, Y, I.getName() + ".demorgan");
3246}
3247
3248/// Canonicalize a shifty way to code absolute value to the more common pattern
3249/// that uses negation and select.
3250static Instruction *canonicalizeAbs(BinaryOperator &Xor,
3251 InstCombiner::BuilderTy &Builder) {
3252 assert(Xor.getOpcode() == Instruction::Xor && "Expected an xor instruction.")(static_cast <bool> (Xor.getOpcode() == Instruction::Xor
&& "Expected an xor instruction.") ? void (0) : __assert_fail
("Xor.getOpcode() == Instruction::Xor && \"Expected an xor instruction.\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 3252
, __extension__ __PRETTY_FUNCTION__))
;
3253
3254 // There are 4 potential commuted variants. Move the 'ashr' candidate to Op1.
3255 // We're relying on the fact that we only do this transform when the shift has
3256 // exactly 2 uses and the add has exactly 1 use (otherwise, we might increase
3257 // instructions).
3258 Value *Op0 = Xor.getOperand(0), *Op1 = Xor.getOperand(1);
3259 if (Op0->hasNUses(2))
3260 std::swap(Op0, Op1);
3261
3262 Type *Ty = Xor.getType();
3263 Value *A;
3264 const APInt *ShAmt;
3265 if (match(Op1, m_AShr(m_Value(A), m_APInt(ShAmt))) &&
3266 Op1->hasNUses(2) && *ShAmt == Ty->getScalarSizeInBits() - 1 &&
3267 match(Op0, m_OneUse(m_c_Add(m_Specific(A), m_Specific(Op1))))) {
3268 // Op1 = ashr i32 A, 31 ; smear the sign bit
3269 // xor (add A, Op1), Op1 ; add -1 and flip bits if negative
3270 // --> (A < 0) ? -A : A
3271 Value *Cmp = Builder.CreateICmpSLT(A, ConstantInt::getNullValue(Ty));
3272 // Copy the nuw/nsw flags from the add to the negate.
3273 auto *Add = cast<BinaryOperator>(Op0);
3274 Value *Neg = Builder.CreateNeg(A, "", Add->hasNoUnsignedWrap(),
3275 Add->hasNoSignedWrap());
3276 return SelectInst::Create(Cmp, Neg, A);
3277 }
3278 return nullptr;
3279}
3280
3281// Transform
3282// z = (~x) &/| y
3283// into:
3284// z = ~(x |/& (~y))
3285// iff y is free to invert and all uses of z can be freely updated.
3286bool InstCombinerImpl::sinkNotIntoOtherHandOfAndOrOr(BinaryOperator &I) {
3287 Instruction::BinaryOps NewOpc;
3288 switch (I.getOpcode()) {
3289 case Instruction::And:
3290 NewOpc = Instruction::Or;
3291 break;
3292 case Instruction::Or:
3293 NewOpc = Instruction::And;
3294 break;
3295 default:
3296 return false;
3297 };
3298
3299 Value *X, *Y;
3300 if (!match(&I, m_c_BinOp(m_Not(m_Value(X)), m_Value(Y))))
3301 return false;
3302
3303 // Will we be able to fold the `not` into Y eventually?
3304 if (!InstCombiner::isFreeToInvert(Y, Y->hasOneUse()))
3305 return false;
3306
3307 // And can our users be adapted?
3308 if (!InstCombiner::canFreelyInvertAllUsersOf(&I, /*IgnoredUser=*/nullptr))
3309 return false;
3310
3311 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3312 Value *NewBinOp =
3313 BinaryOperator::Create(NewOpc, X, NotY, I.getName() + ".not");
3314 Builder.Insert(NewBinOp);
3315 replaceInstUsesWith(I, NewBinOp);
3316 // We can not just create an outer `not`, it will most likely be immediately
3317 // folded back, reconstructing our initial pattern, and causing an
3318 // infinite combine loop, so immediately manually fold it away.
3319 freelyInvertAllUsersOf(NewBinOp);
3320 return true;
3321}
3322
3323Instruction *InstCombinerImpl::foldNot(BinaryOperator &I) {
3324 Value *NotOp;
3325 if (!match(&I, m_Not(m_Value(NotOp))))
3326 return nullptr;
3327
3328 // Apply DeMorgan's Law for 'nand' / 'nor' logic with an inverted operand.
3329 // We must eliminate the and/or (one-use) for these transforms to not increase
3330 // the instruction count.
3331 //
3332 // ~(~X & Y) --> (X | ~Y)
3333 // ~(Y & ~X) --> (X | ~Y)
3334 //
3335 // Note: The logical matches do not check for the commuted patterns because
3336 // those are handled via SimplifySelectsFeedingBinaryOp().
3337 Type *Ty = I.getType();
3338 Value *X, *Y;
3339 if (match(NotOp, m_OneUse(m_c_And(m_Not(m_Value(X)), m_Value(Y))))) {
3340 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3341 return BinaryOperator::CreateOr(X, NotY);
3342 }
3343 if (match(NotOp, m_OneUse(m_LogicalAnd(m_Not(m_Value(X)), m_Value(Y))))) {
3344 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3345 return SelectInst::Create(X, ConstantInt::getTrue(Ty), NotY);
3346 }
3347
3348 // ~(~X | Y) --> (X & ~Y)
3349 // ~(Y | ~X) --> (X & ~Y)
3350 if (match(NotOp, m_OneUse(m_c_Or(m_Not(m_Value(X)), m_Value(Y))))) {
3351 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3352 return BinaryOperator::CreateAnd(X, NotY);
3353 }
3354 if (match(NotOp, m_OneUse(m_LogicalOr(m_Not(m_Value(X)), m_Value(Y))))) {
3355 Value *NotY = Builder.CreateNot(Y, Y->getName() + ".not");
3356 return SelectInst::Create(X, NotY, ConstantInt::getFalse(Ty));
3357 }
3358
3359 // Is this a 'not' (~) fed by a binary operator?
3360 BinaryOperator *NotVal;
3361 if (match(NotOp, m_BinOp(NotVal))) {
3362 if (NotVal->getOpcode() == Instruction::And ||
3363 NotVal->getOpcode() == Instruction::Or) {
3364 // Apply DeMorgan's Law when inverts are free:
3365 // ~(X & Y) --> (~X | ~Y)
3366 // ~(X | Y) --> (~X & ~Y)
3367 if (isFreeToInvert(NotVal->getOperand(0),
3368 NotVal->getOperand(0)->hasOneUse()) &&
3369 isFreeToInvert(NotVal->getOperand(1),
3370 NotVal->getOperand(1)->hasOneUse())) {
3371 Value *NotX = Builder.CreateNot(NotVal->getOperand(0), "notlhs");
3372 Value *NotY = Builder.CreateNot(NotVal->getOperand(1), "notrhs");
3373 if (NotVal->getOpcode() == Instruction::And)
3374 return BinaryOperator::CreateOr(NotX, NotY);
3375 return BinaryOperator::CreateAnd(NotX, NotY);
3376 }
3377 }
3378
3379 // ~((-X) | Y) --> (X - 1) & (~Y)
3380 if (match(NotVal,
3381 m_OneUse(m_c_Or(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))) {
3382 Value *DecX = Builder.CreateAdd(X, ConstantInt::getAllOnesValue(Ty));
3383 Value *NotY = Builder.CreateNot(Y);
3384 return BinaryOperator::CreateAnd(DecX, NotY);
3385 }
3386
3387 // ~(~X >>s Y) --> (X >>s Y)
3388 if (match(NotVal, m_AShr(m_Not(m_Value(X)), m_Value(Y))))
3389 return BinaryOperator::CreateAShr(X, Y);
3390
3391 // If we are inverting a right-shifted constant, we may be able to eliminate
3392 // the 'not' by inverting the constant and using the opposite shift type.
3393 // Canonicalization rules ensure that only a negative constant uses 'ashr',
3394 // but we must check that in case that transform has not fired yet.
3395
3396 // ~(C >>s Y) --> ~C >>u Y (when inverting the replicated sign bits)
3397 Constant *C;
3398 if (match(NotVal, m_AShr(m_Constant(C), m_Value(Y))) &&
3399 match(C, m_Negative())) {
3400 // We matched a negative constant, so propagating undef is unsafe.
3401 // Clamp undef elements to -1.
3402 Type *EltTy = Ty->getScalarType();
3403 C = Constant::replaceUndefsWith(C, ConstantInt::getAllOnesValue(EltTy));
3404 return BinaryOperator::CreateLShr(ConstantExpr::getNot(C), Y);
3405 }
3406
3407 // ~(C >>u Y) --> ~C >>s Y (when inverting the replicated sign bits)
3408 if (match(NotVal, m_LShr(m_Constant(C), m_Value(Y))) &&
3409 match(C, m_NonNegative())) {
3410 // We matched a non-negative constant, so propagating undef is unsafe.
3411 // Clamp undef elements to 0.
3412 Type *EltTy = Ty->getScalarType();
3413 C = Constant::replaceUndefsWith(C, ConstantInt::getNullValue(EltTy));
3414 return BinaryOperator::CreateAShr(ConstantExpr::getNot(C), Y);
3415 }
3416
3417 // ~(X + C) --> ~C - X
3418 if (match(NotVal, m_c_Add(m_Value(X), m_ImmConstant(C))))
3419 return BinaryOperator::CreateSub(ConstantExpr::getNot(C), X);
3420
3421 // ~(X - Y) --> ~X + Y
3422 // FIXME: is it really beneficial to sink the `not` here?
3423 if (match(NotVal, m_Sub(m_Value(X), m_Value(Y))))
3424 if (isa<Constant>(X) || NotVal->hasOneUse())
3425 return BinaryOperator::CreateAdd(Builder.CreateNot(X), Y);
3426
3427 // ~(~X + Y) --> X - Y
3428 if (match(NotVal, m_c_Add(m_Not(m_Value(X)), m_Value(Y))))
3429 return BinaryOperator::CreateWithCopiedFlags(Instruction::Sub, X, Y,
3430 NotVal);
3431 }
3432
3433 // not (cmp A, B) = !cmp A, B
3434 CmpInst::Predicate Pred;
3435 if (match(NotOp, m_OneUse(m_Cmp(Pred, m_Value(), m_Value())))) {
3436 cast<CmpInst>(NotOp)->setPredicate(CmpInst::getInversePredicate(Pred));
3437 return replaceInstUsesWith(I, NotOp);
3438 }
3439
3440 // Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max:
3441 // ~min(~X, ~Y) --> max(X, Y)
3442 // ~max(~X, Y) --> min(X, ~Y)
3443 auto *II = dyn_cast<IntrinsicInst>(NotOp);
3444 if (II && II->hasOneUse()) {
3445 if (match(NotOp, m_MaxOrMin(m_Value(X), m_Value(Y))) &&
3446 isFreeToInvert(X, X->hasOneUse()) &&
3447 isFreeToInvert(Y, Y->hasOneUse())) {
3448 Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3449 Value *NotX = Builder.CreateNot(X);
3450 Value *NotY = Builder.CreateNot(Y);
3451 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, NotX, NotY);
3452 return replaceInstUsesWith(I, InvMaxMin);
3453 }
3454 if (match(NotOp, m_c_MaxOrMin(m_Not(m_Value(X)), m_Value(Y)))) {
3455 Intrinsic::ID InvID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3456 Value *NotY = Builder.CreateNot(Y);
3457 Value *InvMaxMin = Builder.CreateBinaryIntrinsic(InvID, X, NotY);
3458 return replaceInstUsesWith(I, InvMaxMin);
3459 }
3460 }
3461
3462 // TODO: Remove folds if we canonicalize to intrinsics (see above).
3463 // Eliminate a bitwise 'not' op of 'not' min/max by inverting the min/max:
3464 //
3465 // %notx = xor i32 %x, -1
3466 // %cmp1 = icmp sgt i32 %notx, %y
3467 // %smax = select i1 %cmp1, i32 %notx, i32 %y
3468 // %res = xor i32 %smax, -1
3469 // =>
3470 // %noty = xor i32 %y, -1
3471 // %cmp2 = icmp slt %x, %noty
3472 // %res = select i1 %cmp2, i32 %x, i32 %noty
3473 //
3474 // Same is applicable for smin/umax/umin.
3475 if (NotOp->hasOneUse()) {
3476 Value *LHS, *RHS;
3477 SelectPatternFlavor SPF = matchSelectPattern(NotOp, LHS, RHS).Flavor;
3478 if (SelectPatternResult::isMinOrMax(SPF)) {
3479 // It's possible we get here before the not has been simplified, so make
3480 // sure the input to the not isn't freely invertible.
3481 if (match(LHS, m_Not(m_Value(X))) && !isFreeToInvert(X, X->hasOneUse())) {
3482 Value *NotY = Builder.CreateNot(RHS);
3483 return SelectInst::Create(
3484 Builder.CreateICmp(getInverseMinMaxPred(SPF), X, NotY), X, NotY);
3485 }
3486
3487 // It's possible we get here before the not has been simplified, so make
3488 // sure the input to the not isn't freely invertible.
3489 if (match(RHS, m_Not(m_Value(Y))) && !isFreeToInvert(Y, Y->hasOneUse())) {
3490 Value *NotX = Builder.CreateNot(LHS);
3491 return SelectInst::Create(
3492 Builder.CreateICmp(getInverseMinMaxPred(SPF), NotX, Y), NotX, Y);
3493 }
3494
3495 // If both sides are freely invertible, then we can get rid of the xor
3496 // completely.
3497 if (isFreeToInvert(LHS, !LHS->hasNUsesOrMore(3)) &&
3498 isFreeToInvert(RHS, !RHS->hasNUsesOrMore(3))) {
3499 Value *NotLHS = Builder.CreateNot(LHS);
3500 Value *NotRHS = Builder.CreateNot(RHS);
3501 return SelectInst::Create(
3502 Builder.CreateICmp(getInverseMinMaxPred(SPF), NotLHS, NotRHS),
3503 NotLHS, NotRHS);
3504 }
3505 }
3506
3507 // Pull 'not' into operands of select if both operands are one-use compares
3508 // or one is one-use compare and the other one is a constant.
3509 // Inverting the predicates eliminates the 'not' operation.
3510 // Example:
3511 // not (select ?, (cmp TPred, ?, ?), (cmp FPred, ?, ?) -->
3512 // select ?, (cmp InvTPred, ?, ?), (cmp InvFPred, ?, ?)
3513 // not (select ?, (cmp TPred, ?, ?), true -->
3514 // select ?, (cmp InvTPred, ?, ?), false
3515 if (auto *Sel = dyn_cast<SelectInst>(NotOp)) {
3516 Value *TV = Sel->getTrueValue();
3517 Value *FV = Sel->getFalseValue();
3518 auto *CmpT = dyn_cast<CmpInst>(TV);
3519 auto *CmpF = dyn_cast<CmpInst>(FV);
3520 bool InvertibleT = (CmpT && CmpT->hasOneUse()) || isa<Constant>(TV);
3521 bool InvertibleF = (CmpF && CmpF->hasOneUse()) || isa<Constant>(FV);
3522 if (InvertibleT && InvertibleF) {
3523 if (CmpT)
3524 CmpT->setPredicate(CmpT->getInversePredicate());
3525 else
3526 Sel->setTrueValue(ConstantExpr::getNot(cast<Constant>(TV)));
3527 if (CmpF)
3528 CmpF->setPredicate(CmpF->getInversePredicate());
3529 else
3530 Sel->setFalseValue(ConstantExpr::getNot(cast<Constant>(FV)));
3531 return replaceInstUsesWith(I, Sel);
3532 }
3533 }
3534 }
3535
3536 if (Instruction *NewXor = sinkNotIntoXor(I, Builder))
3537 return NewXor;
3538
3539 return nullptr;
3540}
3541
3542// FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
3543// here. We should standardize that construct where it is needed or choose some
3544// other way to ensure that commutated variants of patterns are not missed.
3545Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
3546 if (Value *V = SimplifyXorInst(I.getOperand(0), I.getOperand(1),
3547 SQ.getWithInstruction(&I)))
3548 return replaceInstUsesWith(I, V);
3549
3550 if (SimplifyAssociativeOrCommutative(I))
3551 return &I;
3552
3553 if (Instruction *X = foldVectorBinop(I))
3554 return X;
3555
3556 if (Instruction *NewXor = foldXorToXor(I, Builder))
3557 return NewXor;
3558
3559 // (A&B)^(A&C) -> A&(B^C) etc
3560 if (Value *V = SimplifyUsingDistributiveLaws(I))
3561 return replaceInstUsesWith(I, V);
3562
3563 // See if we can simplify any instructions used by the instruction whose sole
3564 // purpose is to compute bits we don't care about.
3565 if (SimplifyDemandedInstructionBits(I))
3566 return &I;
3567
3568 if (Value *V = SimplifyBSwap(I, Builder))
3569 return replaceInstUsesWith(I, V);
3570
3571 if (Instruction *R = foldNot(I))
3572 return R;
3573
3574 // Fold (X & M) ^ (Y & ~M) -> (X & M) | (Y & ~M)
3575 // This it a special case in haveNoCommonBitsSet, but the computeKnownBits
3576 // calls in there are unnecessary as SimplifyDemandedInstructionBits should
3577 // have already taken care of those cases.
3578 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
3579 Value *M;
3580 if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(M)), m_Value()),
3581 m_c_And(m_Deferred(M), m_Value()))))
3582 return BinaryOperator::CreateOr(Op0, Op1);
3583
3584 if (Instruction *Xor = visitMaskedMerge(I, Builder))
3585 return Xor;
3586
3587 Value *X, *Y;
3588 Constant *C1;
3589 if (match(Op1, m_Constant(C1))) {
3590 // Use DeMorgan and reassociation to eliminate a 'not' op.
3591 Constant *C2;
3592 if (match(Op0, m_OneUse(m_Or(m_Not(m_Value(X)), m_Constant(C2))))) {
3593 // (~X | C2) ^ C1 --> ((X & ~C2) ^ -1) ^ C1 --> (X & ~C2) ^ ~C1
3594 Value *And = Builder.CreateAnd(X, ConstantExpr::getNot(C2));
3595 return BinaryOperator::CreateXor(And, ConstantExpr::getNot(C1));
3596 }
3597 if (match(Op0, m_OneUse(m_And(m_Not(m_Value(X)), m_Constant(C2))))) {
3598 // (~X & C2) ^ C1 --> ((X | ~C2) ^ -1) ^ C1 --> (X | ~C2) ^ ~C1
3599 Value *Or = Builder.CreateOr(X, ConstantExpr::getNot(C2));
3600 return BinaryOperator::CreateXor(Or, ConstantExpr::getNot(C1));
3601 }
3602
3603 // Convert xor ([trunc] (ashr X, BW-1)), C =>
3604 // select(X >s -1, C, ~C)
3605 // The ashr creates "AllZeroOrAllOne's", which then optionally inverses the
3606 // constant depending on whether this input is less than 0.
3607 const APInt *CA;
3608 if (match(Op0, m_OneUse(m_TruncOrSelf(
3609 m_AShr(m_Value(X), m_APIntAllowUndef(CA))))) &&
3610 *CA == X->getType()->getScalarSizeInBits() - 1 &&
3611 !match(C1, m_AllOnes())) {
3612 assert(!C1->isZeroValue() && "Unexpected xor with 0")(static_cast <bool> (!C1->isZeroValue() && "Unexpected xor with 0"
) ? void (0) : __assert_fail ("!C1->isZeroValue() && \"Unexpected xor with 0\""
, "llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp", 3612
, __extension__ __PRETTY_FUNCTION__))
;
3613 Value *ICmp =
3614 Builder.CreateICmpSGT(X, Constant::getAllOnesValue(X->getType()));
3615 return SelectInst::Create(ICmp, Op1, Builder.CreateNot(Op1));
3616 }
3617 }
3618
3619 Type *Ty = I.getType();
3620 {
3621 const APInt *RHSC;
3622 if (match(Op1, m_APInt(RHSC))) {
3623 Value *X;
3624 const APInt *C;
3625 // (C - X) ^ signmaskC --> (C + signmaskC) - X
3626 if (RHSC->isSignMask() && match(Op0, m_Sub(m_APInt(C), m_Value(X))))
3627 return BinaryOperator::CreateSub(ConstantInt::get(Ty, *C + *RHSC), X);
3628
3629 // (X + C) ^ signmaskC --> X + (C + signmaskC)
3630 if (RHSC->isSignMask() && match(Op0, m_Add(m_Value(X), m_APInt(C))))
3631 return BinaryOperator::CreateAdd(X, ConstantInt::get(Ty, *C + *RHSC));
3632
3633 // (X | C) ^ RHSC --> X ^ (C ^ RHSC) iff X & C == 0
3634 if (match(Op0, m_Or(m_Value(X), m_APInt(C))) &&
3635 MaskedValueIsZero(X, *C, 0, &I))
3636 return BinaryOperator::CreateXor(X, ConstantInt::get(Ty, *C ^ *RHSC));
3637
3638 // If RHSC is inverting the remaining bits of shifted X,
3639 // canonicalize to a 'not' before the shift to help SCEV and codegen:
3640 // (X << C) ^ RHSC --> ~X << C
3641 if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_APInt(C)))) &&
3642 *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).shl(*C)) {
3643 Value *NotX = Builder.CreateNot(X);
3644 return BinaryOperator::CreateShl(NotX, ConstantInt::get(Ty, *C));
3645 }
3646 // (X >>u C) ^ RHSC --> ~X >>u C
3647 if (match(Op0, m_OneUse(m_LShr(m_Value(X), m_APInt(C)))) &&
3648 *RHSC == APInt::getAllOnes(Ty->getScalarSizeInBits()).lshr(*C)) {
3649 Value *NotX = Builder.CreateNot(X);
3650 return BinaryOperator::CreateLShr(NotX, ConstantInt::get(Ty, *C));
3651 }
3652 // TODO: We could handle 'ashr' here as well. That would be matching
3653 // a 'not' op and moving it before the shift. Doing that requires
3654 // preventing the inverse fold in canShiftBinOpWithConstantRHS().
3655 }
3656 }
3657
3658 // FIXME: This should not be limited to scalar (pull into APInt match above).
3659 {
3660 Value *X;
3661 ConstantInt *C1, *C2, *C3;
3662 // ((X^C1) >> C2) ^ C3 -> (X>>C2) ^ ((C1>>C2)^C3)
3663 if (match(Op1, m_ConstantInt(C3)) &&
3664 match(Op0, m_LShr(m_Xor(m_Value(X), m_ConstantInt(C1)),
3665 m_ConstantInt(C2))) &&
3666 Op0->hasOneUse()) {
3667 // fold (C1 >> C2) ^ C3
3668 APInt FoldConst = C1->getValue().lshr(C2->getValue());
3669 FoldConst ^= C3->getValue();
3670 // Prepare the two operands.
3671 auto *Opnd0 = cast<Instruction>(Builder.CreateLShr(X, C2));
3672 Opnd0->takeName(cast<Instruction>(Op0));
3673 Opnd0->setDebugLoc(I.getDebugLoc());
3674 return BinaryOperator::CreateXor(Opnd0, ConstantInt::get(Ty, FoldConst));
3675 }
3676 }
3677
3678 if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
3679 return FoldedLogic;
3680
3681 // Y ^ (X | Y) --> X & ~Y
3682 // Y ^ (Y | X) --> X & ~Y
3683 if (match(Op1, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op0)))))
3684 return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op0));
3685 // (X | Y) ^ Y --> X & ~Y
3686 // (Y | X) ^ Y --> X & ~Y
3687 if (match(Op0, m_OneUse(m_c_Or(m_Value(X), m_Specific(Op1)))))
3688 return BinaryOperator::CreateAnd(X, Builder.CreateNot(Op1));
3689
3690 // Y ^ (X & Y) --> ~X & Y
3691 // Y ^ (Y & X) --> ~X & Y
3692 if (match(Op1, m_OneUse(m_c_And(m_Value(X), m_Specific(Op0)))))
3693 return BinaryOperator::CreateAnd(Op0, Builder.CreateNot(X));
3694 // (X & Y) ^ Y --> ~X & Y
3695 // (Y & X) ^ Y --> ~X & Y
3696 // Canonical form is (X & C) ^ C; don't touch that.
3697 // TODO: A 'not' op is better for analysis and codegen, but demanded bits must
3698 // be fixed to prefer that (otherwise we get infinite looping).
3699 if (!match(Op1, m_Constant()) &&
3700 match(Op0, m_OneUse(m_c_And(m_Value(X), m_Specific(Op1)))))
3701 return BinaryOperator::CreateAnd(Op1, Builder.CreateNot(X));
3702
3703 Value *A, *B, *C;
3704 // (A ^ B) ^ (A | C) --> (~A & C) ^ B -- There are 4 commuted variants.
3705 if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))),
3706 m_OneUse(m_c_Or(m_Deferred(A), m_Value(C))))))
3707 return BinaryOperator::CreateXor(
3708 Builder.CreateAnd(Builder.CreateNot(A), C), B);
3709
3710 // (A ^ B) ^ (B | C) --> (~B & C) ^ A -- There are 4 commuted variants.
3711 if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_Value(A), m_Value(B))),
3712 m_OneUse(m_c_Or(m_Deferred(B), m_Value(C))))))
3713 return BinaryOperator::CreateXor(
3714 Builder.CreateAnd(Builder.CreateNot(B), C), A);
3715
3716 // (A & B) ^ (A ^ B) -> (A | B)
3717 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
3718 match(Op1, m_c_Xor(m_Specific(A), m_Specific(B))))
3719 return BinaryOperator::CreateOr(A, B);
3720 // (A ^ B) ^ (A & B) -> (A | B)
3721 if (match(Op0, m_Xor(m_Value(A), m_Value(B))) &&
3722 match(Op1, m_c_And(m_Specific(A), m_Specific(B))))
3723 return BinaryOperator::CreateOr(A, B);
3724
3725 // (A & ~B) ^ ~A -> ~(A & B)
3726 // (~B & A) ^ ~A -> ~(A & B)
3727 if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
3728 match(Op1, m_Not(m_Specific(A))))
3729 return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
3730
3731 // (~A & B) ^ A --> A | B -- There are 4 commuted variants.
3732 if (match(&I, m_c_Xor(m_c_And(m_Not(m_Value(A)), m_Value(B)), m_Deferred(A))))
3733 return BinaryOperator::CreateOr(A, B);
3734
3735 // (~A | B) ^ A --> ~(A & B)
3736 if (match(Op0, m_OneUse(m_c_Or(m_Not(m_Specific(Op1)), m_Value(B)))))
3737 return BinaryOperator::CreateNot(Builder.CreateAnd(Op1, B));
3738
3739 // A ^ (~A | B) --> ~(A & B)
3740 if (match(Op1, m_OneUse(m_c_Or(m_Not(m_Specific(Op0)), m_Value(B)))))
3741 return BinaryOperator::CreateNot(Builder.CreateAnd(Op0, B));
3742
3743 // (A | B) ^ (A | C) --> (B ^ C) & ~A -- There are 4 commuted variants.
3744 // TODO: Loosen one-use restriction if common operand is a constant.
3745 Value *D;
3746 if (match(Op0, m_OneUse(m_Or(m_Value(A), m_Value(B)))) &&
3747 match(Op1, m_OneUse(m_Or(m_Value(C), m_Value(D))))) {
3748 if (B == C || B == D)
3749 std::swap(A, B);
3750 if (A == C)
3751 std::swap(C, D);
3752 if (A == D) {
3753 Value *NotA = Builder.CreateNot(A);
3754 return BinaryOperator::CreateAnd(Builder.CreateXor(B, C), NotA);
3755 }
3756 }
3757
3758 if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand(0)))
3759 if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand(1)))
3760 if (Value *V = foldXorOfICmps(LHS, RHS, I))
3761 return replaceInstUsesWith(I, V);
3762
3763 if (Instruction *CastedXor = foldCastedBitwiseLogic(I))
3764 return CastedXor;
3765
3766 if (Instruction *Abs = canonicalizeAbs(I, Builder))
3767 return Abs;
3768
3769 // Otherwise, if all else failed, try to hoist the xor-by-constant:
3770 // (X ^ C) ^ Y --> (X ^ Y) ^ C
3771 // Just like we do in other places, we completely avoid the fold
3772 // for constantexprs, at least to avoid endless combine loop.
3773 if (match(&I, m_c_Xor(m_OneUse(m_Xor(m_CombineAnd(m_Value(X),
3774 m_Unless(m_ConstantExpr())),
3775 m_ImmConstant(C1))),
3776 m_Value(Y))))
3777 return BinaryOperator::CreateXor(Builder.CreateXor(X, Y), C1);
3778
3779 return nullptr;
3780}

/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/include/llvm/IR/Type.h

1//===- llvm/Type.h - Classes for handling data types ------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the declaration of the Type class. For more "Type"
10// stuff, look in DerivedTypes.h.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_TYPE_H
15#define LLVM_IR_TYPE_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/Support/CBindingWrapping.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/TypeSize.h"
24#include <cassert>
25#include <cstdint>
26#include <iterator>
27
28namespace llvm {
29
30class IntegerType;
31struct fltSemantics;
32class LLVMContext;
33class PointerType;
34class raw_ostream;
35class StringRef;
36
37/// The instances of the Type class are immutable: once they are created,
38/// they are never changed. Also note that only one instance of a particular
39/// type is ever created. Thus seeing if two types are equal is a matter of
40/// doing a trivial pointer comparison. To enforce that no two equal instances
41/// are created, Type instances can only be created via static factory methods
42/// in class Type and in derived classes. Once allocated, Types are never
43/// free'd.
44///
45class Type {
46public:
47 //===--------------------------------------------------------------------===//
48 /// Definitions of all of the base types for the Type system. Based on this
49 /// value, you can cast to a class defined in DerivedTypes.h.
50 /// Note: If you add an element to this, you need to add an element to the
51 /// Type::getPrimitiveType function, or else things will break!
52 /// Also update LLVMTypeKind and LLVMGetTypeKind () in the C binding.
53 ///
54 enum TypeID {
55 // PrimitiveTypes
56 HalfTyID = 0, ///< 16-bit floating point type
57 BFloatTyID, ///< 16-bit floating point type (7-bit significand)
58 FloatTyID, ///< 32-bit floating point type
59 DoubleTyID, ///< 64-bit floating point type
60 X86_FP80TyID, ///< 80-bit floating point type (X87)
61 FP128TyID, ///< 128-bit floating point type (112-bit significand)
62 PPC_FP128TyID, ///< 128-bit floating point type (two 64-bits, PowerPC)
63 VoidTyID, ///< type with no size
64 LabelTyID, ///< Labels
65 MetadataTyID, ///< Metadata
66 X86_MMXTyID, ///< MMX vectors (64 bits, X86 specific)
67 X86_AMXTyID, ///< AMX vectors (8192 bits, X86 specific)
68 TokenTyID, ///< Tokens
69
70 // Derived types... see DerivedTypes.h file.
71 IntegerTyID, ///< Arbitrary bit width integers
72 FunctionTyID, ///< Functions
73 PointerTyID, ///< Pointers
74 StructTyID, ///< Structures
75 ArrayTyID, ///< Arrays
76 FixedVectorTyID, ///< Fixed width SIMD vector type
77 ScalableVectorTyID ///< Scalable SIMD vector type
78 };
79
80private:
81 /// This refers to the LLVMContext in which this type was uniqued.
82 LLVMContext &Context;
83
84 TypeID ID : 8; // The current base type of this type.
85 unsigned SubclassData : 24; // Space for subclasses to store data.
86 // Note that this should be synchronized with
87 // MAX_INT_BITS value in IntegerType class.
88
89protected:
90 friend class LLVMContextImpl;
91
92 explicit Type(LLVMContext &C, TypeID tid)
93 : Context(C), ID(tid), SubclassData(0) {}
94 ~Type() = default;
95
96 unsigned getSubclassData() const { return SubclassData; }
97
98 void setSubclassData(unsigned val) {
99 SubclassData = val;
100 // Ensure we don't have any accidental truncation.
101 assert(getSubclassData() == val && "Subclass data too large for field")(static_cast <bool> (getSubclassData() == val &&
"Subclass data too large for field") ? void (0) : __assert_fail
("getSubclassData() == val && \"Subclass data too large for field\""
, "llvm/include/llvm/IR/Type.h", 101, __extension__ __PRETTY_FUNCTION__
))
;
102 }
103
104 /// Keeps track of how many Type*'s there are in the ContainedTys list.
105 unsigned NumContainedTys = 0;
106
107 /// A pointer to the array of Types contained by this Type. For example, this
108 /// includes the arguments of a function type, the elements of a structure,
109 /// the pointee of a pointer, the element type of an array, etc. This pointer
110 /// may be 0 for types that don't contain other types (Integer, Double,
111 /// Float).
112 Type * const *ContainedTys = nullptr;
113
114public:
115 /// Print the current type.
116 /// Omit the type details if \p NoDetails == true.
117 /// E.g., let %st = type { i32, i16 }
118 /// When \p NoDetails is true, we only print %st.
119 /// Put differently, \p NoDetails prints the type as if
120 /// inlined with the operands when printing an instruction.
121 void print(raw_ostream &O, bool IsForDebug = false,
122 bool NoDetails = false) const;
123
124 void dump() const;
125
126 /// Return the LLVMContext in which this type was uniqued.
127 LLVMContext &getContext() const { return Context; }
128
129 //===--------------------------------------------------------------------===//
130 // Accessors for working with types.
131 //
132
133 /// Return the type id for the type. This will return one of the TypeID enum
134 /// elements defined above.
135 TypeID getTypeID() const { return ID; }
136
137 /// Return true if this is 'void'.
138 bool isVoidTy() const { return getTypeID() == VoidTyID; }
139
140 /// Return true if this is 'half', a 16-bit IEEE fp type.
141 bool isHalfTy() const { return getTypeID() == HalfTyID; }
142
143 /// Return true if this is 'bfloat', a 16-bit bfloat type.
144 bool isBFloatTy() const { return getTypeID() == BFloatTyID; }
145
146 /// Return true if this is 'float', a 32-bit IEEE fp type.
147 bool isFloatTy() const { return getTypeID() == FloatTyID; }
148
149 /// Return true if this is 'double', a 64-bit IEEE fp type.
150 bool isDoubleTy() const { return getTypeID() == DoubleTyID; }
151
152 /// Return true if this is x86 long double.
153 bool isX86_FP80Ty() const { return getTypeID() == X86_FP80TyID; }
154
155 /// Return true if this is 'fp128'.
156 bool isFP128Ty() const { return getTypeID() == FP128TyID; }
157
158 /// Return true if this is powerpc long double.
159 bool isPPC_FP128Ty() const { return getTypeID() == PPC_FP128TyID; }
160
161 /// Return true if this is one of the six floating-point types
162 bool isFloatingPointTy() const {
163 return getTypeID() == HalfTyID || getTypeID() == BFloatTyID ||
164 getTypeID() == FloatTyID || getTypeID() == DoubleTyID ||
165 getTypeID() == X86_FP80TyID || getTypeID() == FP128TyID ||
166 getTypeID() == PPC_FP128TyID;
167 }
168
169 const fltSemantics &getFltSemantics() const;
170
171 /// Return true if this is X86 MMX.
172 bool isX86_MMXTy() const { return getTypeID() == X86_MMXTyID; }
173
174 /// Return true if this is X86 AMX.
175 bool isX86_AMXTy() const { return getTypeID() == X86_AMXTyID; }
176
177 /// Return true if this is a FP type or a vector of FP.
178 bool isFPOrFPVectorTy() const { return getScalarType()->isFloatingPointTy(); }
179
180 /// Return true if this is 'label'.
181 bool isLabelTy() const { return getTypeID() == LabelTyID; }
182
183 /// Return true if this is 'metadata'.
184 bool isMetadataTy() const { return getTypeID() == MetadataTyID; }
185
186 /// Return true if this is 'token'.
187 bool isTokenTy() const { return getTypeID() == TokenTyID; }
188
189 /// True if this is an instance of IntegerType.
190 bool isIntegerTy() const { return getTypeID() == IntegerTyID; }
10
Assuming the condition is true
11
Returning the value 1, which participates in a condition later
17
Assuming the condition is true
18
Returning the value 1, which participates in a condition later
191
192 /// Return true if this is an IntegerType of the given width.
193 bool isIntegerTy(unsigned Bitwidth) const;
194
195 /// Return true if this is an integer type or a vector of integer types.
196 bool isIntOrIntVectorTy() const { return getScalarType()->isIntegerTy(); }
9
Calling 'Type::isIntegerTy'
12
Returning from 'Type::isIntegerTy'
13
Returning the value 1, which participates in a condition later
16
Calling 'Type::isIntegerTy'
19
Returning from 'Type::isIntegerTy'
20
Returning the value 1, which participates in a condition later
197
198 /// Return true if this is an integer type or a vector of integer types of
199 /// the given width.
200 bool isIntOrIntVectorTy(unsigned BitWidth) const {
201 return getScalarType()->isIntegerTy(BitWidth);
202 }
203
204 /// Return true if this is an integer type or a pointer type.
205 bool isIntOrPtrTy() const { return isIntegerTy() || isPointerTy(); }
206
207 /// True if this is an instance of FunctionType.
208 bool isFunctionTy() const { return getTypeID() == FunctionTyID; }
209
210 /// True if this is an instance of StructType.
211 bool isStructTy() const { return getTypeID() == StructTyID; }
212
213 /// True if this is an instance of ArrayType.
214 bool isArrayTy() const { return getTypeID() == ArrayTyID; }
215
216 /// True if this is an instance of PointerType.
217 bool isPointerTy() const { return getTypeID() == PointerTyID; }
218
219 /// True if this is an instance of an opaque PointerType.
220 bool isOpaquePointerTy() const;
221
222 /// Return true if this is a pointer type or a vector of pointer types.
223 bool isPtrOrPtrVectorTy() const { return getScalarType()->isPointerTy(); }
224
225 /// True if this is an instance of VectorType.
226 inline bool isVectorTy() const {
227 return getTypeID() == ScalableVectorTyID || getTypeID() == FixedVectorTyID;
228 }
229
230 /// Return true if this type could be converted with a lossless BitCast to
231 /// type 'Ty'. For example, i8* to i32*. BitCasts are valid for types of the
232 /// same size only where no re-interpretation of the bits is done.
233 /// Determine if this type could be losslessly bitcast to Ty
234 bool canLosslesslyBitCastTo(Type *Ty) const;
235
236 /// Return true if this type is empty, that is, it has no elements or all of
237 /// its elements are empty.
238 bool isEmptyTy() const;
239
240 /// Return true if the type is "first class", meaning it is a valid type for a
241 /// Value.
242 bool isFirstClassType() const {
243 return getTypeID() != FunctionTyID && getTypeID() != VoidTyID;
244 }
245
246 /// Return true if the type is a valid type for a register in codegen. This
247 /// includes all first-class types except struct and array types.
248 bool isSingleValueType() const {
249 return isFloatingPointTy() || isX86_MMXTy() || isIntegerTy() ||
250 isPointerTy() || isVectorTy() || isX86_AMXTy();
251 }
252
253 /// Return true if the type is an aggregate type. This means it is valid as
254 /// the first operand of an insertvalue or extractvalue instruction. This
255 /// includes struct and array types, but does not include vector types.
256 bool isAggregateType() const {
257 return getTypeID() == StructTyID || getTypeID() == ArrayTyID;
258 }
259
260 /// Return true if it makes sense to take the size of this type. To get the
261 /// actual size for a particular target, it is reasonable to use the
262 /// DataLayout subsystem to do this.
263 bool isSized(SmallPtrSetImpl<Type*> *Visited = nullptr) const {
264 // If it's a primitive, it is always sized.
265 if (getTypeID() == IntegerTyID || isFloatingPointTy() ||
266 getTypeID() == PointerTyID || getTypeID() == X86_MMXTyID ||
267 getTypeID() == X86_AMXTyID)
268 return true;
269 // If it is not something that can have a size (e.g. a function or label),
270 // it doesn't have a size.
271 if (getTypeID() != StructTyID && getTypeID() != ArrayTyID && !isVectorTy())
272 return false;
273 // Otherwise we have to try harder to decide.
274 return isSizedDerivedType(Visited);
275 }
276
277 /// Return the basic size of this type if it is a primitive type. These are
278 /// fixed by LLVM and are not target-dependent.
279 /// This will return zero if the type does not have a size or is not a
280 /// primitive type.
281 ///
282 /// If this is a scalable vector type, the scalable property will be set and
283 /// the runtime size will be a positive integer multiple of the base size.
284 ///
285 /// Note that this may not reflect the size of memory allocated for an
286 /// instance of the type or the number of bytes that are written when an
287 /// instance of the type is stored to memory. The DataLayout class provides
288 /// additional query functions to provide this information.
289 ///
290 TypeSize getPrimitiveSizeInBits() const LLVM_READONLY__attribute__((__pure__));
291
292 /// If this is a vector type, return the getPrimitiveSizeInBits value for the
293 /// element type. Otherwise return the getPrimitiveSizeInBits value for this
294 /// type.
295 unsigned getScalarSizeInBits() const LLVM_READONLY__attribute__((__pure__));
296
297 /// Return the width of the mantissa of this type. This is only valid on
298 /// floating-point types. If the FP type does not have a stable mantissa (e.g.
299 /// ppc long double), this method returns -1.
300 int getFPMantissaWidth() const;
301
302 /// Return whether the type is IEEE compatible, as defined by the eponymous
303 /// method in APFloat.
304 bool isIEEE() const;
305
306 /// If this is a vector type, return the element type, otherwise return
307 /// 'this'.
308 inline Type *getScalarType() const {
309 if (isVectorTy())
310 return getContainedType(0);
311 return const_cast<Type *>(this);
312 }
313
314 //===--------------------------------------------------------------------===//
315 // Type Iteration support.
316 //
317 using subtype_iterator = Type * const *;
318
319 subtype_iterator subtype_begin() const { return ContainedTys; }
320 subtype_iterator subtype_end() const { return &ContainedTys[NumContainedTys];}
321 ArrayRef<Type*> subtypes() const {
322 return makeArrayRef(subtype_begin(), subtype_end());
323 }
324
325 using subtype_reverse_iterator = std::reverse_iterator<subtype_iterator>;
326
327 subtype_reverse_iterator subtype_rbegin() const {
328 return subtype_reverse_iterator(subtype_end());
329 }
330 subtype_reverse_iterator subtype_rend() const {
331 return subtype_reverse_iterator(subtype_begin());
332 }
333
334 /// This method is used to implement the type iterator (defined at the end of
335 /// the file). For derived types, this returns the types 'contained' in the
336 /// derived type.
337 Type *getContainedType(unsigned i) const {
338 assert(i < NumContainedTys && "Index out of range!")(static_cast <bool> (i < NumContainedTys && "Index out of range!"
) ? void (0) : __assert_fail ("i < NumContainedTys && \"Index out of range!\""
, "llvm/include/llvm/IR/Type.h", 338, __extension__ __PRETTY_FUNCTION__
))
;
339 return ContainedTys[i];
340 }
341
342 /// Return the number of types in the derived type.
343 unsigned getNumContainedTypes() const { return NumContainedTys; }
344
345 //===--------------------------------------------------------------------===//
346 // Helper methods corresponding to subclass methods. This forces a cast to
347 // the specified subclass and calls its accessor. "getArrayNumElements" (for
348 // example) is shorthand for cast<ArrayType>(Ty)->getNumElements(). This is
349 // only intended to cover the core methods that are frequently used, helper
350 // methods should not be added here.
351
352 inline unsigned getIntegerBitWidth() const;
353
354 inline Type *getFunctionParamType(unsigned i) const;
355 inline unsigned getFunctionNumParams() const;
356 inline bool isFunctionVarArg() const;
357
358 inline StringRef getStructName() const;
359 inline unsigned getStructNumElements() const;
360 inline Type *getStructElementType(unsigned N) const;
361
362 inline uint64_t getArrayNumElements() const;
363
364 Type *getArrayElementType() const {
365 assert(getTypeID() == ArrayTyID)(static_cast <bool> (getTypeID() == ArrayTyID) ? void (
0) : __assert_fail ("getTypeID() == ArrayTyID", "llvm/include/llvm/IR/Type.h"
, 365, __extension__ __PRETTY_FUNCTION__))
;
366 return ContainedTys[0];
367 }
368
369 Type *getPointerElementType() const {
370 assert(getTypeID() == PointerTyID)(static_cast <bool> (getTypeID() == PointerTyID) ? void
(0) : __assert_fail ("getTypeID() == PointerTyID", "llvm/include/llvm/IR/Type.h"
, 370, __extension__ __PRETTY_FUNCTION__))
;
371 assert(NumContainedTys &&(static_cast <bool> (NumContainedTys && "Attempting to get element type of opaque pointer"
) ? void (0) : __assert_fail ("NumContainedTys && \"Attempting to get element type of opaque pointer\""
, "llvm/include/llvm/IR/Type.h", 372, __extension__ __PRETTY_FUNCTION__
))
372 "Attempting to get element type of opaque pointer")(static_cast <bool> (NumContainedTys && "Attempting to get element type of opaque pointer"
) ? void (0) : __assert_fail ("NumContainedTys && \"Attempting to get element type of opaque pointer\""
, "llvm/include/llvm/IR/Type.h", 372, __extension__ __PRETTY_FUNCTION__
))
;
373 return ContainedTys[0];
374 }
375
376 /// Given vector type, change the element type,
377 /// whilst keeping the old number of elements.
378 /// For non-vectors simply returns \p EltTy.
379 inline Type *getWithNewType(Type *EltTy) const;
380
381 /// Given an integer or vector type, change the lane bitwidth to NewBitwidth,
382 /// whilst keeping the old number of lanes.
383 inline Type *getWithNewBitWidth(unsigned NewBitWidth) const;
384
385 /// Given scalar/vector integer type, returns a type with elements twice as
386 /// wide as in the original type. For vectors, preserves element count.
387 inline Type *getExtendedType() const;
388
389 /// Get the address space of this pointer or pointer vector type.
390 inline unsigned getPointerAddressSpace() const;
391
392 //===--------------------------------------------------------------------===//
393 // Static members exported by the Type class itself. Useful for getting
394 // instances of Type.
395 //
396
397 /// Return a type based on an identifier.
398 static Type *getPrimitiveType(LLVMContext &C, TypeID IDNumber);
399
400 //===--------------------------------------------------------------------===//
401 // These are the builtin types that are always available.
402 //
403 static Type *getVoidTy(LLVMContext &C);
404 static Type *getLabelTy(LLVMContext &C);
405 static Type *getHalfTy(LLVMContext &C);
406 static Type *getBFloatTy(LLVMContext &C);
407 static Type *getFloatTy(LLVMContext &C);
408 static Type *getDoubleTy(LLVMContext &C);
409 static Type *getMetadataTy(LLVMContext &C);
410 static Type *getX86_FP80Ty(LLVMContext &C);
411 static Type *getFP128Ty(LLVMContext &C);
412 static Type *getPPC_FP128Ty(LLVMContext &C);
413 static Type *getX86_MMXTy(LLVMContext &C);
414 static Type *getX86_AMXTy(LLVMContext &C);
415 static Type *getTokenTy(LLVMContext &C);
416 static IntegerType *getIntNTy(LLVMContext &C, unsigned N);
417 static IntegerType *getInt1Ty(LLVMContext &C);
418 static IntegerType *getInt8Ty(LLVMContext &C);
419 static IntegerType *getInt16Ty(LLVMContext &C);
420 static IntegerType *getInt32Ty(LLVMContext &C);
421 static IntegerType *getInt64Ty(LLVMContext &C);
422 static IntegerType *getInt128Ty(LLVMContext &C);
423 template <typename ScalarTy> static Type *getScalarTy(LLVMContext &C) {
424 int noOfBits = sizeof(ScalarTy) * CHAR_BIT8;
425 if (std::is_integral<ScalarTy>::value) {
426 return (Type*) Type::getIntNTy(C, noOfBits);
427 } else if (std::is_floating_point<ScalarTy>::value) {
428 switch (noOfBits) {
429 case 32:
430 return Type::getFloatTy(C);
431 case 64:
432 return Type::getDoubleTy(C);
433 }
434 }
435 llvm_unreachable("Unsupported type in Type::getScalarTy")::llvm::llvm_unreachable_internal("Unsupported type in Type::getScalarTy"
, "llvm/include/llvm/IR/Type.h", 435)
;
436 }
437 static Type *getFloatingPointTy(LLVMContext &C, const fltSemantics &S);
438
439 //===--------------------------------------------------------------------===//
440 // Convenience methods for getting pointer types with one of the above builtin
441 // types as pointee.
442 //
443 static PointerType *getHalfPtrTy(LLVMContext &C, unsigned AS = 0);
444 static PointerType *getBFloatPtrTy(LLVMContext &C, unsigned AS = 0);
445 static PointerType *getFloatPtrTy(LLVMContext &C, unsigned AS = 0);
446 static PointerType *getDoublePtrTy(LLVMContext &C, unsigned AS = 0);
447 static PointerType *getX86_FP80PtrTy(LLVMContext &C, unsigned AS = 0);
448 static PointerType *getFP128PtrTy(LLVMContext &C, unsigned AS = 0);
449 static PointerType *getPPC_FP128PtrTy(LLVMContext &C, unsigned AS = 0);
450 static PointerType *getX86_MMXPtrTy(LLVMContext &C, unsigned AS = 0);
451 static PointerType *getX86_AMXPtrTy(LLVMContext &C, unsigned AS = 0);
452 static PointerType *getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS = 0);
453 static PointerType *getInt1PtrTy(LLVMContext &C, unsigned AS = 0);
454 static PointerType *getInt8PtrTy(LLVMContext &C, unsigned AS = 0);
455 static PointerType *getInt16PtrTy(LLVMContext &C, unsigned AS = 0);
456 static PointerType *getInt32PtrTy(LLVMContext &C, unsigned AS = 0);
457 static PointerType *getInt64PtrTy(LLVMContext &C, unsigned AS = 0);
458
459 /// Return a pointer to the current type. This is equivalent to
460 /// PointerType::get(Foo, AddrSpace).
461 /// TODO: Remove this after opaque pointer transition is complete.
462 PointerType *getPointerTo(unsigned AddrSpace = 0) const;
463
464private:
465 /// Derived types like structures and arrays are sized iff all of the members
466 /// of the type are sized as well. Since asking for their size is relatively
467 /// uncommon, move this operation out-of-line.
468 bool isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited = nullptr) const;
469};
470
471// Printing of types.
472inline raw_ostream &operator<<(raw_ostream &OS, const Type &T) {
473 T.print(OS);
474 return OS;
475}
476
477// allow isa<PointerType>(x) to work without DerivedTypes.h included.
478template <> struct isa_impl<PointerType, Type> {
479 static inline bool doit(const Type &Ty) {
480 return Ty.getTypeID() == Type::PointerTyID;
481 }
482};
483
484// Create wrappers for C Binding types (see CBindingWrapping.h).
485DEFINE_ISA_CONVERSION_FUNCTIONS(Type, LLVMTypeRef)inline Type *unwrap(LLVMTypeRef P) { return reinterpret_cast<
Type*>(P); } inline LLVMTypeRef wrap(const Type *P) { return
reinterpret_cast<LLVMTypeRef>(const_cast<Type*>(
P)); } template<typename T> inline T *unwrap(LLVMTypeRef
P) { return cast<T>(unwrap(P)); }
486
487/* Specialized opaque type conversions.
488 */
489inline Type **unwrap(LLVMTypeRef* Tys) {
490 return reinterpret_cast<Type**>(Tys);
491}
492
493inline LLVMTypeRef *wrap(Type **Tys) {
494 return reinterpret_cast<LLVMTypeRef*>(const_cast<Type**>(Tys));
495}
496
497} // end namespace llvm
498
499#endif // LLVM_IR_TYPE_H

/build/llvm-toolchain-snapshot-14~++20220116100644+5f782d25a742/llvm/include/llvm/IR/Instructions.h

1//===- llvm/Instructions.h - Instruction subclass definitions ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file exposes the class definitions of all of the subclasses of the
10// Instruction class. This is meant to be an easy way to get access to all
11// instruction subclasses.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_INSTRUCTIONS_H
16#define LLVM_IR_INSTRUCTIONS_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/Bitfields.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/None.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/IR/Attributes.h"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/CFG.h"
32#include "llvm/IR/Constant.h"
33#include "llvm/IR/DerivedTypes.h"
34#include "llvm/IR/Function.h"
35#include "llvm/IR/InstrTypes.h"
36#include "llvm/IR/Instruction.h"
37#include "llvm/IR/OperandTraits.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Use.h"
40#include "llvm/IR/User.h"
41#include "llvm/IR/Value.h"
42#include "llvm/Support/AtomicOrdering.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/ErrorHandling.h"
45#include <cassert>
46#include <cstddef>
47#include <cstdint>
48#include <iterator>
49
50namespace llvm {
51
52class APInt;
53class ConstantInt;
54class DataLayout;
55class LLVMContext;
56
57//===----------------------------------------------------------------------===//
58// AllocaInst Class
59//===----------------------------------------------------------------------===//
60
61/// an instruction to allocate memory on the stack
62class AllocaInst : public UnaryInstruction {
63 Type *AllocatedType;
64
65 using AlignmentField = AlignmentBitfieldElementT<0>;
66 using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>;
67 using SwiftErrorField = BoolBitfieldElementT<UsedWithInAllocaField::NextBit>;
68 static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField,
69 SwiftErrorField>(),
70 "Bitfields must be contiguous");
71
72protected:
73 // Note: Instruction needs to be a friend here to call cloneImpl.
74 friend class Instruction;
75
76 AllocaInst *cloneImpl() const;
77
78public:
79 explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
80 const Twine &Name, Instruction *InsertBefore);
81 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
82 const Twine &Name, BasicBlock *InsertAtEnd);
83
84 AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
85 Instruction *InsertBefore);
86 AllocaInst(Type *Ty, unsigned AddrSpace,
87 const Twine &Name, BasicBlock *InsertAtEnd);
88
89 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
90 const Twine &Name = "", Instruction *InsertBefore = nullptr);
91 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
92 const Twine &Name, BasicBlock *InsertAtEnd);
93
94 /// Return true if there is an allocation size parameter to the allocation
95 /// instruction that is not 1.
96 bool isArrayAllocation() const;
97
98 /// Get the number of elements allocated. For a simple allocation of a single
99 /// element, this will return a constant 1 value.
100 const Value *getArraySize() const { return getOperand(0); }
101 Value *getArraySize() { return getOperand(0); }
102
103 /// Overload to return most specific pointer type.
104 PointerType *getType() const {
105 return cast<PointerType>(Instruction::getType());
106 }
107
108 /// Return the address space for the allocation.
109 unsigned getAddressSpace() const {
110 return getType()->getAddressSpace();
111 }
112
113 /// Get allocation size in bits. Returns None if size can't be determined,
114 /// e.g. in case of a VLA.
115 Optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const;
116
117 /// Return the type that is being allocated by the instruction.
118 Type *getAllocatedType() const { return AllocatedType; }
119 /// for use only in special circumstances that need to generically
120 /// transform a whole instruction (eg: IR linking and vectorization).
121 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
122
123 /// Return the alignment of the memory that is being allocated by the
124 /// instruction.
125 Align getAlign() const {
126 return Align(1ULL << getSubclassData<AlignmentField>());
127 }
128
129 void setAlignment(Align Align) {
130 setSubclassData<AlignmentField>(Log2(Align));
131 }
132
133 // FIXME: Remove this one transition to Align is over.
134 uint64_t getAlignment() const { return getAlign().value(); }
135
136 /// Return true if this alloca is in the entry block of the function and is a
137 /// constant size. If so, the code generator will fold it into the
138 /// prolog/epilog code, so it is basically free.
139 bool isStaticAlloca() const;
140
141 /// Return true if this alloca is used as an inalloca argument to a call. Such
142 /// allocas are never considered static even if they are in the entry block.
143 bool isUsedWithInAlloca() const {
144 return getSubclassData<UsedWithInAllocaField>();
145 }
146
147 /// Specify whether this alloca is used to represent the arguments to a call.
148 void setUsedWithInAlloca(bool V) {
149 setSubclassData<UsedWithInAllocaField>(V);
150 }
151
152 /// Return true if this alloca is used as a swifterror argument to a call.
153 bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); }
154 /// Specify whether this alloca is used to represent a swifterror.
155 void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); }
156
157 // Methods for support type inquiry through isa, cast, and dyn_cast:
158 static bool classof(const Instruction *I) {
159 return (I->getOpcode() == Instruction::Alloca);
160 }
161 static bool classof(const Value *V) {
162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
163 }
164
165private:
166 // Shadow Instruction::setInstructionSubclassData with a private forwarding
167 // method so that subclasses cannot accidentally use it.
168 template <typename Bitfield>
169 void setSubclassData(typename Bitfield::Type Value) {
170 Instruction::setSubclassData<Bitfield>(Value);
171 }
172};
173
174//===----------------------------------------------------------------------===//
175// LoadInst Class
176//===----------------------------------------------------------------------===//
177
178/// An instruction for reading from memory. This uses the SubclassData field in
179/// Value to store whether or not the load is volatile.
180class LoadInst : public UnaryInstruction {
181 using VolatileField = BoolBitfieldElementT<0>;
182 using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>;
183 using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>;
184 static_assert(
185 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
186 "Bitfields must be contiguous");
187
188 void AssertOK();
189
190protected:
191 // Note: Instruction needs to be a friend here to call cloneImpl.
192 friend class Instruction;
193
194 LoadInst *cloneImpl() const;
195
196public:
197 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr,
198 Instruction *InsertBefore);
199 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
200 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
201 Instruction *InsertBefore);
202 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
203 BasicBlock *InsertAtEnd);
204 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
205 Align Align, Instruction *InsertBefore = nullptr);
206 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
207 Align Align, BasicBlock *InsertAtEnd);
208 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
209 Align Align, AtomicOrdering Order,
210 SyncScope::ID SSID = SyncScope::System,
211 Instruction *InsertBefore = nullptr);
212 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
213 Align Align, AtomicOrdering Order, SyncScope::ID SSID,
214 BasicBlock *InsertAtEnd);
215
216 /// Return true if this is a load from a volatile memory location.
217 bool isVolatile() const { return getSubclassData<VolatileField>(); }
218
219 /// Specify whether this is a volatile load or not.
220 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
221
222 /// Return the alignment of the access that is being performed.
223 /// FIXME: Remove this function once transition to Align is over.
224 /// Use getAlign() instead.
225 uint64_t getAlignment() const { return getAlign().value(); }
226
227 /// Return the alignment of the access that is being performed.
228 Align getAlign() const {
229 return Align(1ULL << (getSubclassData<AlignmentField>()));
230 }
231
232 void setAlignment(Align Align) {
233 setSubclassData<AlignmentField>(Log2(Align));
234 }
235
236 /// Returns the ordering constraint of this load instruction.
237 AtomicOrdering getOrdering() const {
238 return getSubclassData<OrderingField>();
239 }
240 /// Sets the ordering constraint of this load instruction. May not be Release
241 /// or AcquireRelease.
242 void setOrdering(AtomicOrdering Ordering) {
243 setSubclassData<OrderingField>(Ordering);
244 }
245
246 /// Returns the synchronization scope ID of this load instruction.
247 SyncScope::ID getSyncScopeID() const {
248 return SSID;
249 }
250
251 /// Sets the synchronization scope ID of this load instruction.
252 void setSyncScopeID(SyncScope::ID SSID) {
253 this->SSID = SSID;
254 }
255
256 /// Sets the ordering constraint and the synchronization scope ID of this load
257 /// instruction.
258 void setAtomic(AtomicOrdering Ordering,
259 SyncScope::ID SSID = SyncScope::System) {
260 setOrdering(Ordering);
261 setSyncScopeID(SSID);
262 }
263
264 bool isSimple() const { return !isAtomic() && !isVolatile(); }
265
266 bool isUnordered() const {
267 return (getOrdering() == AtomicOrdering::NotAtomic ||
268 getOrdering() == AtomicOrdering::Unordered) &&
269 !isVolatile();
270 }
271
272 Value *getPointerOperand() { return getOperand(0); }
273 const Value *getPointerOperand() const { return getOperand(0); }
274 static unsigned getPointerOperandIndex() { return 0U; }
275 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
276
277 /// Returns the address space of the pointer operand.
278 unsigned getPointerAddressSpace() const {
279 return getPointerOperandType()->getPointerAddressSpace();
280 }
281
282 // Methods for support type inquiry through isa, cast, and dyn_cast:
283 static bool classof(const Instruction *I) {
284 return I->getOpcode() == Instruction::Load;
285 }
286 static bool classof(const Value *V) {
287 return isa<Instruction>(V) && classof(cast<Instruction>(V));
288 }
289
290private:
291 // Shadow Instruction::setInstructionSubclassData with a private forwarding
292 // method so that subclasses cannot accidentally use it.
293 template <typename Bitfield>
294 void setSubclassData(typename Bitfield::Type Value) {
295 Instruction::setSubclassData<Bitfield>(Value);
296 }
297
298 /// The synchronization scope ID of this load instruction. Not quite enough
299 /// room in SubClassData for everything, so synchronization scope ID gets its
300 /// own field.
301 SyncScope::ID SSID;
302};
303
304//===----------------------------------------------------------------------===//
305// StoreInst Class
306//===----------------------------------------------------------------------===//
307
308/// An instruction for storing to memory.
309class StoreInst : public Instruction {
310 using VolatileField = BoolBitfieldElementT<0>;
311 using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>;
312 using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>;
313 static_assert(
314 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
315 "Bitfields must be contiguous");
316
317 void AssertOK();
318
319protected:
320 // Note: Instruction needs to be a friend here to call cloneImpl.
321 friend class Instruction;
322
323 StoreInst *cloneImpl() const;
324
325public:
326 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
327 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
328 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Instruction *InsertBefore);
329 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
330 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
331 Instruction *InsertBefore = nullptr);
332 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
333 BasicBlock *InsertAtEnd);
334 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
335 AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System,
336 Instruction *InsertBefore = nullptr);
337 StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align,
338 AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd);
339
340 // allocate space for exactly two operands
341 void *operator new(size_t S) { return User::operator new(S, 2); }
342 void operator delete(void *Ptr) { User::operator delete(Ptr); }
343
344 /// Return true if this is a store to a volatile memory location.
345 bool isVolatile() const { return getSubclassData<VolatileField>(); }
346
347 /// Specify whether this is a volatile store or not.
348 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
349
350 /// Transparently provide more efficient getOperand methods.
351 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
352
353 /// Return the alignment of the access that is being performed
354 /// FIXME: Remove this function once transition to Align is over.
355 /// Use getAlign() instead.
356 uint64_t getAlignment() const { return getAlign().value(); }
357
358 Align getAlign() const {
359 return Align(1ULL << (getSubclassData<AlignmentField>()));
360 }
361
362 void setAlignment(Align Align) {
363 setSubclassData<AlignmentField>(Log2(Align));
364 }
365
366 /// Returns the ordering constraint of this store instruction.
367 AtomicOrdering getOrdering() const {
368 return getSubclassData<OrderingField>();
369 }
370
371 /// Sets the ordering constraint of this store instruction. May not be
372 /// Acquire or AcquireRelease.
373 void setOrdering(AtomicOrdering Ordering) {
374 setSubclassData<OrderingField>(Ordering);
375 }
376
377 /// Returns the synchronization scope ID of this store instruction.
378 SyncScope::ID getSyncScopeID() const {
379 return SSID;
380 }
381
382 /// Sets the synchronization scope ID of this store instruction.
383 void setSyncScopeID(SyncScope::ID SSID) {
384 this->SSID = SSID;
385 }
386
387 /// Sets the ordering constraint and the synchronization scope ID of this
388 /// store instruction.
389 void setAtomic(AtomicOrdering Ordering,
390 SyncScope::ID SSID = SyncScope::System) {
391 setOrdering(Ordering);
392 setSyncScopeID(SSID);
393 }
394
395 bool isSimple() const { return !isAtomic() && !isVolatile(); }
396
397 bool isUnordered() const {
398 return (getOrdering() == AtomicOrdering::NotAtomic ||
399 getOrdering() == AtomicOrdering::Unordered) &&
400 !isVolatile();
401 }
402
403 Value *getValueOperand() { return getOperand(0); }
404 const Value *getValueOperand() const { return getOperand(0); }
405
406 Value *getPointerOperand() { return getOperand(1); }
407 const Value *getPointerOperand() const { return getOperand(1); }
408 static unsigned getPointerOperandIndex() { return 1U; }
409 Type *getPointerOperandType() const { return getPointerOperand()->getType(); }
410
411 /// Returns the address space of the pointer operand.
412 unsigned getPointerAddressSpace() const {
413 return getPointerOperandType()->getPointerAddressSpace();
414 }
415
416 // Methods for support type inquiry through isa, cast, and dyn_cast:
417 static bool classof(const Instruction *I) {
418 return I->getOpcode() == Instruction::Store;
419 }
420 static bool classof(const Value *V) {
421 return isa<Instruction>(V) && classof(cast<Instruction>(V));
422 }
423
424private:
425 // Shadow Instruction::setInstructionSubclassData with a private forwarding
426 // method so that subclasses cannot accidentally use it.
427 template <typename Bitfield>
428 void setSubclassData(typename Bitfield::Type Value) {
429 Instruction::setSubclassData<Bitfield>(Value);
430 }
431
432 /// The synchronization scope ID of this store instruction. Not quite enough
433 /// room in SubClassData for everything, so synchronization scope ID gets its
434 /// own field.
435 SyncScope::ID SSID;
436};
437
438template <>
439struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
440};
441
442DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)StoreInst::op_iterator StoreInst::op_begin() { return OperandTraits
<StoreInst>::op_begin(this); } StoreInst::const_op_iterator
StoreInst::op_begin() const { return OperandTraits<StoreInst
>::op_begin(const_cast<StoreInst*>(this)); } StoreInst
::op_iterator StoreInst::op_end() { return OperandTraits<StoreInst
>::op_end(this); } StoreInst::const_op_iterator StoreInst::
op_end() const { return OperandTraits<StoreInst>::op_end
(const_cast<StoreInst*>(this)); } Value *StoreInst::getOperand
(unsigned i_nocapture) const { (static_cast <bool> (i_nocapture
< OperandTraits<StoreInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 442, __extension__ __PRETTY_FUNCTION__
)); return cast_or_null<Value>( OperandTraits<StoreInst
>::op_begin(const_cast<StoreInst*>(this))[i_nocapture
].get()); } void StoreInst::setOperand(unsigned i_nocapture, Value
*Val_nocapture) { (static_cast <bool> (i_nocapture <
OperandTraits<StoreInst>::operands(this) && "setOperand() out of range!"
) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 442, __extension__ __PRETTY_FUNCTION__
)); OperandTraits<StoreInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned StoreInst::getNumOperands() const
{ return OperandTraits<StoreInst>::operands(this); } template
<int Idx_nocapture> Use &StoreInst::Op() { return this
->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture
> const Use &StoreInst::Op() const { return this->OpFrom
<Idx_nocapture>(this); }
443
444//===----------------------------------------------------------------------===//
445// FenceInst Class
446//===----------------------------------------------------------------------===//
447
448/// An instruction for ordering other memory operations.
449class FenceInst : public Instruction {
450 using OrderingField = AtomicOrderingBitfieldElementT<0>;
451
452 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
453
454protected:
455 // Note: Instruction needs to be a friend here to call cloneImpl.
456 friend class Instruction;
457
458 FenceInst *cloneImpl() const;
459
460public:
461 // Ordering may only be Acquire, Release, AcquireRelease, or
462 // SequentiallyConsistent.
463 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
464 SyncScope::ID SSID = SyncScope::System,
465 Instruction *InsertBefore = nullptr);
466 FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID,
467 BasicBlock *InsertAtEnd);
468
469 // allocate space for exactly zero operands
470 void *operator new(size_t S) { return User::operator new(S, 0); }
471 void operator delete(void *Ptr) { User::operator delete(Ptr); }
472
473 /// Returns the ordering constraint of this fence instruction.
474 AtomicOrdering getOrdering() const {
475 return getSubclassData<OrderingField>();
476 }
477
478 /// Sets the ordering constraint of this fence instruction. May only be
479 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
480 void setOrdering(AtomicOrdering Ordering) {
481 setSubclassData<OrderingField>(Ordering);
482 }
483
484 /// Returns the synchronization scope ID of this fence instruction.
485 SyncScope::ID getSyncScopeID() const {
486 return SSID;
487 }
488
489 /// Sets the synchronization scope ID of this fence instruction.
490 void setSyncScopeID(SyncScope::ID SSID) {
491 this->SSID = SSID;
492 }
493
494 // Methods for support type inquiry through isa, cast, and dyn_cast:
495 static bool classof(const Instruction *I) {
496 return I->getOpcode() == Instruction::Fence;
497 }
498 static bool classof(const Value *V) {
499 return isa<Instruction>(V) && classof(cast<Instruction>(V));
500 }
501
502private:
503 // Shadow Instruction::setInstructionSubclassData with a private forwarding
504 // method so that subclasses cannot accidentally use it.
505 template <typename Bitfield>
506 void setSubclassData(typename Bitfield::Type Value) {
507 Instruction::setSubclassData<Bitfield>(Value);
508 }
509
510 /// The synchronization scope ID of this fence instruction. Not quite enough
511 /// room in SubClassData for everything, so synchronization scope ID gets its
512 /// own field.
513 SyncScope::ID SSID;
514};
515
516//===----------------------------------------------------------------------===//
517// AtomicCmpXchgInst Class
518//===----------------------------------------------------------------------===//
519
520/// An instruction that atomically checks whether a
521/// specified value is in a memory location, and, if it is, stores a new value
522/// there. The value returned by this instruction is a pair containing the
523/// original value as first element, and an i1 indicating success (true) or
524/// failure (false) as second element.
525///
526class AtomicCmpXchgInst : public Instruction {
527 void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align,
528 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
529 SyncScope::ID SSID);
530
531 template <unsigned Offset>
532 using AtomicOrderingBitfieldElement =
533 typename Bitfield::Element<AtomicOrdering, Offset, 3,
534 AtomicOrdering::LAST>;
535
536protected:
537 // Note: Instruction needs to be a friend here to call cloneImpl.
538 friend class Instruction;
539
540 AtomicCmpXchgInst *cloneImpl() const;
541
542public:
543 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
544 AtomicOrdering SuccessOrdering,
545 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
546 Instruction *InsertBefore = nullptr);
547 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
548 AtomicOrdering SuccessOrdering,
549 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
550 BasicBlock *InsertAtEnd);
551
552 // allocate space for exactly three operands
553 void *operator new(size_t S) { return User::operator new(S, 3); }
554 void operator delete(void *Ptr) { User::operator delete(Ptr); }
555
556 using VolatileField = BoolBitfieldElementT<0>;
557 using WeakField = BoolBitfieldElementT<VolatileField::NextBit>;
558 using SuccessOrderingField =
559 AtomicOrderingBitfieldElementT<WeakField::NextBit>;
560 using FailureOrderingField =
561 AtomicOrderingBitfieldElementT<SuccessOrderingField::NextBit>;
562 using AlignmentField =
563 AlignmentBitfieldElementT<FailureOrderingField::NextBit>;
564 static_assert(
565 Bitfield::areContiguous<VolatileField, WeakField, SuccessOrderingField,
566 FailureOrderingField, AlignmentField>(),
567 "Bitfields must be contiguous");
568
569 /// Return the alignment of the memory that is being allocated by the
570 /// instruction.
571 Align getAlign() const {
572 return Align(1ULL << getSubclassData<AlignmentField>());
573 }
574
575 void setAlignment(Align Align) {
576 setSubclassData<AlignmentField>(Log2(Align));
577 }
578
579 /// Return true if this is a cmpxchg from a volatile memory
580 /// location.
581 ///
582 bool isVolatile() const { return getSubclassData<VolatileField>(); }
583
584 /// Specify whether this is a volatile cmpxchg.
585 ///
586 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
587
588 /// Return true if this cmpxchg may spuriously fail.
589 bool isWeak() const { return getSubclassData<WeakField>(); }
590
591 void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); }
592
593 /// Transparently provide more efficient getOperand methods.
594 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
595
596 static bool isValidSuccessOrdering(AtomicOrdering Ordering) {
597 return Ordering != AtomicOrdering::NotAtomic &&
598 Ordering != AtomicOrdering::Unordered;
599 }
600
601 static bool isValidFailureOrdering(AtomicOrdering Ordering) {
602 return Ordering != AtomicOrdering::NotAtomic &&
603 Ordering != AtomicOrdering::Unordered &&
604 Ordering != AtomicOrdering::AcquireRelease &&
605 Ordering != AtomicOrdering::Release;
606 }
607
608 /// Returns the success ordering constraint of this cmpxchg instruction.
609 AtomicOrdering getSuccessOrdering() const {
610 return getSubclassData<SuccessOrderingField>();
611 }
612
613 /// Sets the success ordering constraint of this cmpxchg instruction.
614 void setSuccessOrdering(AtomicOrdering Ordering) {
615 assert(isValidSuccessOrdering(Ordering) &&(static_cast <bool> (isValidSuccessOrdering(Ordering) &&
"invalid CmpXchg success ordering") ? void (0) : __assert_fail
("isValidSuccessOrdering(Ordering) && \"invalid CmpXchg success ordering\""
, "llvm/include/llvm/IR/Instructions.h", 616, __extension__ __PRETTY_FUNCTION__
))
616 "invalid CmpXchg success ordering")(static_cast <bool> (isValidSuccessOrdering(Ordering) &&
"invalid CmpXchg success ordering") ? void (0) : __assert_fail
("isValidSuccessOrdering(Ordering) && \"invalid CmpXchg success ordering\""
, "llvm/include/llvm/IR/Instructions.h", 616, __extension__ __PRETTY_FUNCTION__
))
;
617 setSubclassData<SuccessOrderingField>(Ordering);
618 }
619
620 /// Returns the failure ordering constraint of this cmpxchg instruction.
621 AtomicOrdering getFailureOrdering() const {
622 return getSubclassData<FailureOrderingField>();
623 }
624
625 /// Sets the failure ordering constraint of this cmpxchg instruction.
626 void setFailureOrdering(AtomicOrdering Ordering) {
627 assert(isValidFailureOrdering(Ordering) &&(static_cast <bool> (isValidFailureOrdering(Ordering) &&
"invalid CmpXchg failure ordering") ? void (0) : __assert_fail
("isValidFailureOrdering(Ordering) && \"invalid CmpXchg failure ordering\""
, "llvm/include/llvm/IR/Instructions.h", 628, __extension__ __PRETTY_FUNCTION__
))
628 "invalid CmpXchg failure ordering")(static_cast <bool> (isValidFailureOrdering(Ordering) &&
"invalid CmpXchg failure ordering") ? void (0) : __assert_fail
("isValidFailureOrdering(Ordering) && \"invalid CmpXchg failure ordering\""
, "llvm/include/llvm/IR/Instructions.h", 628, __extension__ __PRETTY_FUNCTION__
))
;
629 setSubclassData<FailureOrderingField>(Ordering);
630 }
631
632 /// Returns a single ordering which is at least as strong as both the
633 /// success and failure orderings for this cmpxchg.
634 AtomicOrdering getMergedOrdering() const {
635 if (getFailureOrdering() == AtomicOrdering::SequentiallyConsistent)
636 return AtomicOrdering::SequentiallyConsistent;
637 if (getFailureOrdering() == AtomicOrdering::Acquire) {
638 if (getSuccessOrdering() == AtomicOrdering::Monotonic)
639 return AtomicOrdering::Acquire;
640 if (getSuccessOrdering() == AtomicOrdering::Release)
641 return AtomicOrdering::AcquireRelease;
642 }
643 return getSuccessOrdering();
644 }
645
646 /// Returns the synchronization scope ID of this cmpxchg instruction.
647 SyncScope::ID getSyncScopeID() const {
648 return SSID;
649 }
650
651 /// Sets the synchronization scope ID of this cmpxchg instruction.
652 void setSyncScopeID(SyncScope::ID SSID) {
653 this->SSID = SSID;
654 }
655
656 Value *getPointerOperand() { return getOperand(0); }
657 const Value *getPointerOperand() const { return getOperand(0); }
658 static unsigned getPointerOperandIndex() { return 0U; }
659
660 Value *getCompareOperand() { return getOperand(1); }
661 const Value *getCompareOperand() const { return getOperand(1); }
662
663 Value *getNewValOperand() { return getOperand(2); }
664 const Value *getNewValOperand() const { return getOperand(2); }
665
666 /// Returns the address space of the pointer operand.
667 unsigned getPointerAddressSpace() const {
668 return getPointerOperand()->getType()->getPointerAddressSpace();
669 }
670
671 /// Returns the strongest permitted ordering on failure, given the
672 /// desired ordering on success.
673 ///
674 /// If the comparison in a cmpxchg operation fails, there is no atomic store
675 /// so release semantics cannot be provided. So this function drops explicit
676 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
677 /// operation would remain SequentiallyConsistent.
678 static AtomicOrdering
679 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
680 switch (SuccessOrdering) {
681 default:
682 llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering"
, "llvm/include/llvm/IR/Instructions.h", 682)
;
683 case AtomicOrdering::Release:
684 case AtomicOrdering::Monotonic:
685 return AtomicOrdering::Monotonic;
686 case AtomicOrdering::AcquireRelease:
687 case AtomicOrdering::Acquire:
688 return AtomicOrdering::Acquire;
689 case AtomicOrdering::SequentiallyConsistent:
690 return AtomicOrdering::SequentiallyConsistent;
691 }
692 }
693
694 // Methods for support type inquiry through isa, cast, and dyn_cast:
695 static bool classof(const Instruction *I) {
696 return I->getOpcode() == Instruction::AtomicCmpXchg;
697 }
698 static bool classof(const Value *V) {
699 return isa<Instruction>(V) && classof(cast<Instruction>(V));
700 }
701
702private:
703 // Shadow Instruction::setInstructionSubclassData with a private forwarding
704 // method so that subclasses cannot accidentally use it.
705 template <typename Bitfield>
706 void setSubclassData(typename Bitfield::Type Value) {
707 Instruction::setSubclassData<Bitfield>(Value);
708 }
709
710 /// The synchronization scope ID of this cmpxchg instruction. Not quite
711 /// enough room in SubClassData for everything, so synchronization scope ID
712 /// gets its own field.
713 SyncScope::ID SSID;
714};
715
716template <>
717struct OperandTraits<AtomicCmpXchgInst> :
718 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
719};
720
721DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)AtomicCmpXchgInst::op_iterator AtomicCmpXchgInst::op_begin() {
return OperandTraits<AtomicCmpXchgInst>::op_begin(this
); } AtomicCmpXchgInst::const_op_iterator AtomicCmpXchgInst::
op_begin() const { return OperandTraits<AtomicCmpXchgInst>
::op_begin(const_cast<AtomicCmpXchgInst*>(this)); } AtomicCmpXchgInst
::op_iterator AtomicCmpXchgInst::op_end() { return OperandTraits
<AtomicCmpXchgInst>::op_end(this); } AtomicCmpXchgInst::
const_op_iterator AtomicCmpXchgInst::op_end() const { return OperandTraits
<AtomicCmpXchgInst>::op_end(const_cast<AtomicCmpXchgInst
*>(this)); } Value *AtomicCmpXchgInst::getOperand(unsigned
i_nocapture) const { (static_cast <bool> (i_nocapture <
OperandTraits<AtomicCmpXchgInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 721, __extension__ __PRETTY_FUNCTION__
)); return cast_or_null<Value>( OperandTraits<AtomicCmpXchgInst
>::op_begin(const_cast<AtomicCmpXchgInst*>(this))[i_nocapture
].get()); } void AtomicCmpXchgInst::setOperand(unsigned i_nocapture
, Value *Val_nocapture) { (static_cast <bool> (i_nocapture
< OperandTraits<AtomicCmpXchgInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 721, __extension__ __PRETTY_FUNCTION__
)); OperandTraits<AtomicCmpXchgInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned AtomicCmpXchgInst::getNumOperands
() const { return OperandTraits<AtomicCmpXchgInst>::operands
(this); } template <int Idx_nocapture> Use &AtomicCmpXchgInst
::Op() { return this->OpFrom<Idx_nocapture>(this); }
template <int Idx_nocapture> const Use &AtomicCmpXchgInst
::Op() const { return this->OpFrom<Idx_nocapture>(this
); }
722
723//===----------------------------------------------------------------------===//
724// AtomicRMWInst Class
725//===----------------------------------------------------------------------===//
726
727/// an instruction that atomically reads a memory location,
728/// combines it with another value, and then stores the result back. Returns
729/// the old value.
730///
731class AtomicRMWInst : public Instruction {
732protected:
733 // Note: Instruction needs to be a friend here to call cloneImpl.
734 friend class Instruction;
735
736 AtomicRMWInst *cloneImpl() const;
737
738public:
739 /// This enumeration lists the possible modifications atomicrmw can make. In
740 /// the descriptions, 'p' is the pointer to the instruction's memory location,
741 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
742 /// instruction. These instructions always return 'old'.
743 enum BinOp : unsigned {
744 /// *p = v
745 Xchg,
746 /// *p = old + v
747 Add,
748 /// *p = old - v
749 Sub,
750 /// *p = old & v
751 And,
752 /// *p = ~(old & v)
753 Nand,
754 /// *p = old | v
755 Or,
756 /// *p = old ^ v
757 Xor,
758 /// *p = old >signed v ? old : v
759 Max,
760 /// *p = old <signed v ? old : v
761 Min,
762 /// *p = old >unsigned v ? old : v
763 UMax,
764 /// *p = old <unsigned v ? old : v
765 UMin,
766
767 /// *p = old + v
768 FAdd,
769
770 /// *p = old - v
771 FSub,
772
773 FIRST_BINOP = Xchg,
774 LAST_BINOP = FSub,
775 BAD_BINOP
776 };
777
778private:
779 template <unsigned Offset>
780 using AtomicOrderingBitfieldElement =
781 typename Bitfield::Element<AtomicOrdering, Offset, 3,
782 AtomicOrdering::LAST>;
783
784 template <unsigned Offset>
785 using BinOpBitfieldElement =
786 typename Bitfield::Element<BinOp, Offset, 4, BinOp::LAST_BINOP>;
787
788public:
789 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
790 AtomicOrdering Ordering, SyncScope::ID SSID,
791 Instruction *InsertBefore = nullptr);
792 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
793 AtomicOrdering Ordering, SyncScope::ID SSID,
794 BasicBlock *InsertAtEnd);
795
796 // allocate space for exactly two operands
797 void *operator new(size_t S) { return User::operator new(S, 2); }
798 void operator delete(void *Ptr) { User::operator delete(Ptr); }
799
800 using VolatileField = BoolBitfieldElementT<0>;
801 using AtomicOrderingField =
802 AtomicOrderingBitfieldElementT<VolatileField::NextBit>;
803 using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>;
804 using AlignmentField = AlignmentBitfieldElementT<OperationField::NextBit>;
805 static_assert(Bitfield::areContiguous<VolatileField, AtomicOrderingField,
806 OperationField, AlignmentField>(),
807 "Bitfields must be contiguous");
808
809 BinOp getOperation() const { return getSubclassData<OperationField>(); }
810
811 static StringRef getOperationName(BinOp Op);
812
813 static bool isFPOperation(BinOp Op) {
814 switch (Op) {
815 case AtomicRMWInst::FAdd:
816 case AtomicRMWInst::FSub:
817 return true;
818 default:
819 return false;
820 }
821 }
822
823 void setOperation(BinOp Operation) {
824 setSubclassData<OperationField>(Operation);
825 }
826
827 /// Return the alignment of the memory that is being allocated by the
828 /// instruction.
829 Align getAlign() const {
830 return Align(1ULL << getSubclassData<AlignmentField>());
831 }
832
833 void setAlignment(Align Align) {
834 setSubclassData<AlignmentField>(Log2(Align));
835 }
836
837 /// Return true if this is a RMW on a volatile memory location.
838 ///
839 bool isVolatile() const { return getSubclassData<VolatileField>(); }
840
841 /// Specify whether this is a volatile RMW or not.
842 ///
843 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
844
845 /// Transparently provide more efficient getOperand methods.
846 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
847
848 /// Returns the ordering constraint of this rmw instruction.
849 AtomicOrdering getOrdering() const {
850 return getSubclassData<AtomicOrderingField>();
851 }
852
853 /// Sets the ordering constraint of this rmw instruction.
854 void setOrdering(AtomicOrdering Ordering) {
855 assert(Ordering != AtomicOrdering::NotAtomic &&(static_cast <bool> (Ordering != AtomicOrdering::NotAtomic
&& "atomicrmw instructions can only be atomic.") ? void
(0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\""
, "llvm/include/llvm/IR/Instructions.h", 856, __extension__ __PRETTY_FUNCTION__
))
856 "atomicrmw instructions can only be atomic.")(static_cast <bool> (Ordering != AtomicOrdering::NotAtomic
&& "atomicrmw instructions can only be atomic.") ? void
(0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\""
, "llvm/include/llvm/IR/Instructions.h", 856, __extension__ __PRETTY_FUNCTION__
))
;
857 setSubclassData<AtomicOrderingField>(Ordering);
858 }
859
860 /// Returns the synchronization scope ID of this rmw instruction.
861 SyncScope::ID getSyncScopeID() const {
862 return SSID;
863 }
864
865 /// Sets the synchronization scope ID of this rmw instruction.
866 void setSyncScopeID(SyncScope::ID SSID) {
867 this->SSID = SSID;
868 }
869
870 Value *getPointerOperand() { return getOperand(0); }
871 const Value *getPointerOperand() const { return getOperand(0); }
872 static unsigned getPointerOperandIndex() { return 0U; }
873
874 Value *getValOperand() { return getOperand(1); }
875 const Value *getValOperand() const { return getOperand(1); }
876
877 /// Returns the address space of the pointer operand.
878 unsigned getPointerAddressSpace() const {
879 return getPointerOperand()->getType()->getPointerAddressSpace();
880 }
881
882 bool isFloatingPointOperation() const {
883 return isFPOperation(getOperation());
884 }
885
886 // Methods for support type inquiry through isa, cast, and dyn_cast:
887 static bool classof(const Instruction *I) {
888 return I->getOpcode() == Instruction::AtomicRMW;
889 }
890 static bool classof(const Value *V) {
891 return isa<Instruction>(V) && classof(cast<Instruction>(V));
892 }
893
894private:
895 void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align,
896 AtomicOrdering Ordering, SyncScope::ID SSID);
897
898 // Shadow Instruction::setInstructionSubclassData with a private forwarding
899 // method so that subclasses cannot accidentally use it.
900 template <typename Bitfield>
901 void setSubclassData(typename Bitfield::Type Value) {
902 Instruction::setSubclassData<Bitfield>(Value);
903 }
904
905 /// The synchronization scope ID of this rmw instruction. Not quite enough
906 /// room in SubClassData for everything, so synchronization scope ID gets its
907 /// own field.
908 SyncScope::ID SSID;
909};
910
911template <>
912struct OperandTraits<AtomicRMWInst>
913 : public FixedNumOperandTraits<AtomicRMWInst,2> {
914};
915
916DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)AtomicRMWInst::op_iterator AtomicRMWInst::op_begin() { return
OperandTraits<AtomicRMWInst>::op_begin(this); } AtomicRMWInst
::const_op_iterator AtomicRMWInst::op_begin() const { return OperandTraits
<AtomicRMWInst>::op_begin(const_cast<AtomicRMWInst*>
(this)); } AtomicRMWInst::op_iterator AtomicRMWInst::op_end()
{ return OperandTraits<AtomicRMWInst>::op_end(this); }
AtomicRMWInst::const_op_iterator AtomicRMWInst::op_end() const
{ return OperandTraits<AtomicRMWInst>::op_end(const_cast
<AtomicRMWInst*>(this)); } Value *AtomicRMWInst::getOperand
(unsigned i_nocapture) const { (static_cast <bool> (i_nocapture
< OperandTraits<AtomicRMWInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 916, __extension__ __PRETTY_FUNCTION__
)); return cast_or_null<Value>( OperandTraits<AtomicRMWInst
>::op_begin(const_cast<AtomicRMWInst*>(this))[i_nocapture
].get()); } void AtomicRMWInst::setOperand(unsigned i_nocapture
, Value *Val_nocapture) { (static_cast <bool> (i_nocapture
< OperandTraits<AtomicRMWInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 916, __extension__ __PRETTY_FUNCTION__
)); OperandTraits<AtomicRMWInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned AtomicRMWInst::getNumOperands()
const { return OperandTraits<AtomicRMWInst>::operands(
this); } template <int Idx_nocapture> Use &AtomicRMWInst
::Op() { return this->OpFrom<Idx_nocapture>(this); }
template <int Idx_nocapture> const Use &AtomicRMWInst
::Op() const { return this->OpFrom<Idx_nocapture>(this
); }
917
918//===----------------------------------------------------------------------===//
919// GetElementPtrInst Class
920//===----------------------------------------------------------------------===//
921
922// checkGEPType - Simple wrapper function to give a better assertion failure
923// message on bad indexes for a gep instruction.
924//
925inline Type *checkGEPType(Type *Ty) {
926 assert(Ty && "Invalid GetElementPtrInst indices for type!")(static_cast <bool> (Ty && "Invalid GetElementPtrInst indices for type!"
) ? void (0) : __assert_fail ("Ty && \"Invalid GetElementPtrInst indices for type!\""
, "llvm/include/llvm/IR/Instructions.h", 926, __extension__ __PRETTY_FUNCTION__
))
;
927 return Ty;
928}
929
930/// an instruction for type-safe pointer arithmetic to
931/// access elements of arrays and structs
932///
933class GetElementPtrInst : public Instruction {
934 Type *SourceElementType;
935 Type *ResultElementType;
936
937 GetElementPtrInst(const GetElementPtrInst &GEPI);
938
939 /// Constructors - Create a getelementptr instruction with a base pointer an
940 /// list of indices. The first ctor can optionally insert before an existing
941 /// instruction, the second appends the new instruction to the specified
942 /// BasicBlock.
943 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
944 ArrayRef<Value *> IdxList, unsigned Values,
945 const Twine &NameStr, Instruction *InsertBefore);
946 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
947 ArrayRef<Value *> IdxList, unsigned Values,
948 const Twine &NameStr, BasicBlock *InsertAtEnd);
949
950 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
951
952protected:
953 // Note: Instruction needs to be a friend here to call cloneImpl.
954 friend class Instruction;
955
956 GetElementPtrInst *cloneImpl() const;
957
958public:
959 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
960 ArrayRef<Value *> IdxList,
961 const Twine &NameStr = "",
962 Instruction *InsertBefore = nullptr) {
963 unsigned Values = 1 + unsigned(IdxList.size());
964 assert(PointeeType && "Must specify element type")(static_cast <bool> (PointeeType && "Must specify element type"
) ? void (0) : __assert_fail ("PointeeType && \"Must specify element type\""
, "llvm/include/llvm/IR/Instructions.h", 964, __extension__ __PRETTY_FUNCTION__
))
;
965 assert(cast<PointerType>(Ptr->getType()->getScalarType())(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 966, __extension__ __PRETTY_FUNCTION__
))
966 ->isOpaqueOrPointeeTypeMatches(PointeeType))(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 966, __extension__ __PRETTY_FUNCTION__
))
;
967 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
968 NameStr, InsertBefore);
969 }
970
971 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
972 ArrayRef<Value *> IdxList,
973 const Twine &NameStr,
974 BasicBlock *InsertAtEnd) {
975 unsigned Values = 1 + unsigned(IdxList.size());
976 assert(PointeeType && "Must specify element type")(static_cast <bool> (PointeeType && "Must specify element type"
) ? void (0) : __assert_fail ("PointeeType && \"Must specify element type\""
, "llvm/include/llvm/IR/Instructions.h", 976, __extension__ __PRETTY_FUNCTION__
))
;
977 assert(cast<PointerType>(Ptr->getType()->getScalarType())(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 978, __extension__ __PRETTY_FUNCTION__
))
978 ->isOpaqueOrPointeeTypeMatches(PointeeType))(static_cast <bool> (cast<PointerType>(Ptr->getType
()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType
)) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)"
, "llvm/include/llvm/IR/Instructions.h", 978, __extension__ __PRETTY_FUNCTION__
))
;
979 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
980 NameStr, InsertAtEnd);
981 }
982
983 /// Create an "inbounds" getelementptr. See the documentation for the
984 /// "inbounds" flag in LangRef.html for details.
985 static GetElementPtrInst *
986 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
987 const Twine &NameStr = "",
988 Instruction *InsertBefore = nullptr) {
989 GetElementPtrInst *GEP =
990 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
991 GEP->setIsInBounds(true);
992 return GEP;
993 }
994
995 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
996 ArrayRef<Value *> IdxList,
997 const Twine &NameStr,
998 BasicBlock *InsertAtEnd) {
999 GetElementPtrInst *GEP =
1000 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
1001 GEP->setIsInBounds(true);
1002 return GEP;
1003 }
1004
1005 /// Transparently provide more efficient getOperand methods.
1006 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void
setOperand(unsigned, Value*); inline op_iterator op_begin();
inline const_op_iterator op_begin() const; inline op_iterator
op_end(); inline const_op_iterator op_end() const; protected
: template <int> inline Use &Op(); template <int
> inline const Use &Op() const; public: inline unsigned
getNumOperands() const
;
1007
1008 Type *getSourceElementType() const { return SourceElementType; }
1009
1010 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
1011 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
1012
1013 Type *getResultElementType() const {
1014 assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1015, __extension__ __PRETTY_FUNCTION__
))
1015 ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1015, __extension__ __PRETTY_FUNCTION__
))
;
1016 return ResultElementType;
1017 }
1018
1019 /// Returns the address space of this instruction's pointer type.
1020 unsigned getAddressSpace() const {
1021 // Note that this is always the same as the pointer operand's address space
1022 // and that is cheaper to compute, so cheat here.
1023 return getPointerAddressSpace();
1024 }
1025
1026 /// Returns the result type of a getelementptr with the given source
1027 /// element type and indexes.
1028 ///
1029 /// Null is returned if the indices are invalid for the specified
1030 /// source element type.
1031 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
1032 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
1033 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
1034
1035 /// Return the type of the element at the given index of an indexable
1036 /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})".
1037 ///
1038 /// Returns null if the type can't be indexed, or the given index is not
1039 /// legal for the given type.
1040 static Type *getTypeAtIndex(Type *Ty, Value *Idx);
1041 static Type *getTypeAtIndex(Type *Ty, uint64_t Idx);
1042
1043 inline op_iterator idx_begin() { return op_begin()+1; }
1044 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1045 inline op_iterator idx_end() { return op_end(); }
1046 inline const_op_iterator idx_end() const { return op_end(); }
1047
1048 inline iterator_range<op_iterator> indices() {
1049 return make_range(idx_begin(), idx_end());
1050 }
1051
1052 inline iterator_range<const_op_iterator> indices() const {
1053 return make_range(idx_begin(), idx_end());
1054 }
1055
1056 Value *getPointerOperand() {
1057 return getOperand(0);
1058 }
1059 const Value *getPointerOperand() const {
1060 return getOperand(0);
1061 }
1062 static unsigned getPointerOperandIndex() {
1063 return 0U; // get index for modifying correct operand.
1064 }
1065
1066 /// Method to return the pointer operand as a
1067 /// PointerType.
1068 Type *getPointerOperandType() const {
1069 return getPointerOperand()->getType();
1070 }
1071
1072 /// Returns the address space of the pointer operand.
1073 unsigned getPointerAddressSpace() const {
1074 return getPointerOperandType()->getPointerAddressSpace();
1075 }
1076
1077 /// Returns the pointer type returned by the GEP
1078 /// instruction, which may be a vector of pointers.
1079 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
1080 ArrayRef<Value *> IdxList) {
1081 PointerType *OrigPtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
1082 unsigned AddrSpace = OrigPtrTy->getAddressSpace();
1083 Type *ResultElemTy = checkGEPType(getIndexedType(ElTy, IdxList));
1084 Type *PtrTy = OrigPtrTy->isOpaque()
1085 ? PointerType::get(OrigPtrTy->getContext(), AddrSpace)
1086 : PointerType::get(ResultElemTy, AddrSpace);
1087 // Vector GEP
1088 if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) {
1089 ElementCount EltCount = PtrVTy->getElementCount();
1090 return VectorType::get(PtrTy, EltCount);
1091 }
1092 for (Value *Index : IdxList)
1093 if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) {
1094 ElementCount EltCount = IndexVTy->getElementCount();
1095 return VectorType::get(PtrTy, EltCount);
1096 }
1097 // Scalar GEP
1098 return PtrTy;
1099 }
1100
1101 unsigned getNumIndices() const { // Note: always non-negative
1102 return getNumOperands() - 1;
1103 }
1104
1105 bool hasIndices() const {
1106 return getNumOperands() > 1;
1107 }
1108
1109 /// Return true if all of the indices of this GEP are
1110 /// zeros. If so, the result pointer and the first operand have the same
1111 /// value, just potentially different types.
1112 bool hasAllZeroIndices() const;
1113
1114 /// Return true if all of the indices of this GEP are
1115 /// constant integers. If so, the result pointer and the first operand have
1116 /// a constant offset between them.
1117 bool hasAllConstantIndices() const;
1118
1119 /// Set or clear the inbounds flag on this GEP instruction.
1120 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1121 void setIsInBounds(bool b = true);
1122
1123 /// Determine whether the GEP has the inbounds flag.
1124 bool isInBounds() const;
1125
1126 /// Accumulate the constant address offset of this GEP if possible.
1127 ///
1128 /// This routine accepts an APInt into which it will accumulate the constant
1129 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1130 /// all-constant, it returns false and the value of the offset APInt is
1131 /// undefined (it is *not* preserved!). The APInt passed into this routine
1132 /// must be at least as wide as the IntPtr type for the address space of
1133 /// the base GEP pointer.
1134 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1135 bool collectOffset(const DataLayout &DL, unsigned BitWidth,
1136 MapVector<Value *, APInt> &VariableOffsets,
1137 APInt &ConstantOffset) const;
1138 // Methods for support type inquiry through isa, cast, and dyn_cast:
1139 static bool classof(const Instruction *I) {
1140 return (I->getOpcode() == Instruction::GetElementPtr);
1141 }
1142 static bool classof(const Value *V) {
1143 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1144 }
1145};
1146
1147template <>
1148struct OperandTraits<GetElementPtrInst> :
1149 public VariadicOperandTraits<GetElementPtrInst, 1> {
1150};
1151
1152GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1153 ArrayRef<Value *> IdxList, unsigned Values,
1154 const Twine &NameStr,
1155 Instruction *InsertBefore)
1156 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1157 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1158 Values, InsertBefore),
1159 SourceElementType(PointeeType),
1160 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1161 assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1162, __extension__ __PRETTY_FUNCTION__
))
1162 ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1162, __extension__ __PRETTY_FUNCTION__
))
;
1163 init(Ptr, IdxList, NameStr);
1164}
1165
1166GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1167 ArrayRef<Value *> IdxList, unsigned Values,
1168 const Twine &NameStr,
1169 BasicBlock *InsertAtEnd)
1170 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1171 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1172 Values, InsertAtEnd),
1173 SourceElementType(PointeeType),
1174 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1175 assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1176, __extension__ __PRETTY_FUNCTION__
))
1176 ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()->
getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType
)) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)"
, "llvm/include/llvm/IR/Instructions.h", 1176, __extension__ __PRETTY_FUNCTION__
))
;
1177 init(Ptr, IdxList, NameStr);
1178}
1179
1180DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)GetElementPtrInst::op_iterator GetElementPtrInst::op_begin() {
return OperandTraits<GetElementPtrInst>::op_begin(this
); } GetElementPtrInst::const_op_iterator GetElementPtrInst::
op_begin() const { return OperandTraits<GetElementPtrInst>
::op_begin(const_cast<GetElementPtrInst*>(this)); } GetElementPtrInst
::op_iterator GetElementPtrInst::op_end() { return OperandTraits
<GetElementPtrInst>::op_end(this); } GetElementPtrInst::
const_op_iterator GetElementPtrInst::op_end() const { return OperandTraits
<GetElementPtrInst>::op_end(const_cast<GetElementPtrInst
*>(this)); } Value *GetElementPtrInst::getOperand(unsigned
i_nocapture) const { (static_cast <bool> (i_nocapture <
OperandTraits<GetElementPtrInst>::operands(this) &&
"getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"getOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1180, __extension__ __PRETTY_FUNCTION__
)); return cast_or_null<Value>( OperandTraits<GetElementPtrInst
>::op_begin(const_cast<GetElementPtrInst*>(this))[i_nocapture
].get()); } void GetElementPtrInst::setOperand(unsigned i_nocapture
, Value *Val_nocapture) { (static_cast <bool> (i_nocapture
< OperandTraits<GetElementPtrInst>::operands(this) &&
"setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"setOperand() out of range!\""
, "llvm/include/llvm/IR/Instructions.h", 1180, __extension__ __PRETTY_FUNCTION__
)); OperandTraits<GetElementPtrInst>::op_begin(this)[i_nocapture
] = Val_nocapture; } unsigned GetElementPtrInst::getNumOperands
() const { return OperandTraits<GetElementPtrInst>::operands
(this); } template <int Idx_nocapture> Use &GetElementPtrInst
::Op() { return this->OpFrom<Idx_nocapture>(this); }
template <int Idx_nocapture> const Use &GetElementPtrInst
::Op() const { return this->OpFrom<Idx_nocapture>(this
); }
1181
1182//===----------------------------------------------------------------------===//
1183// ICmpInst Class
1184//===----------------------------------------------------------------------===//
1185
1186/// This instruction compares its operands according to the predicate given
1187/// to the constructor. It only operates on integers or pointers. The operands
1188/// must be identical types.
1189/// Represent an integer comparison operator.
1190class ICmpInst: public CmpInst {
1191 void AssertOK() {
1192 assert(isIntPredicate() &&(static_cast <bool> (isIntPredicate() && "Invalid ICmp predicate value"
) ? void (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\""
, "llvm/include/llvm/IR/Instructions.h", 1193, __extension__ __PRETTY_FUNCTION__
))
1193 "Invalid ICmp predicate value")(static_cast <bool> (isIntPredicate() && "Invalid ICmp predicate value"
) ? void (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\""
, "llvm/include/llvm/IR/Instructions.h", 1193, __extension__ __PRETTY_FUNCTION__
))
;
1194 assert(getOperand(0)->getType() == getOperand(1)->getType() &&(static_cast <bool> (getOperand(0)->getType() == getOperand
(1)->getType() && "Both operands to ICmp instruction are not of the same type!"
) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\""
, "llvm/include/llvm/IR/Instructions.h", 1195, __extension__ __PRETTY_FUNCTION__
))
1195 "Both operands to ICmp instruction are not of the same type!")(static_cast <bool> (getOperand(0)->getType() == getOperand
(1)->getType() && "Both operands to ICmp instruction are not of the same type!"
) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\""
, "llvm/include/llvm/IR/Instructions.h", 1195, __extension__ __PRETTY_FUNCTION__
))
;
1196 // Check that the operands are the right type
1197 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy
() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
"Invalid operand types for ICmp instruction") ? void (0) : __assert_fail
("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1199, __extension__ __PRETTY_FUNCTION__
))
1198 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy
() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
"Invalid operand types for ICmp instruction") ? void (0) : __assert_fail
("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1199, __extension__ __PRETTY_FUNCTION__
))
1199 "Invalid operand types for ICmp instruction")(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy
() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
"Invalid operand types for ICmp instruction") ? void (0) : __assert_fail
("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\""
, "llvm/include/llvm/IR/Instructions.h", 1199, __extension__ __PRETTY_FUNCTION__
))
;
1200 }
1201
1202protected:
1203 // Note: Instruction needs to be a friend here to call cloneImpl.
1204 friend class Instruction;
1205
1206 /// Clone an identical ICmpInst
1207 ICmpInst *cloneImpl() const;
1208
1209public:
1210 /// Constructor with insert-before-instruction semantics.
1211 ICmpInst(
1212 Instruction *InsertBefore, ///< Where to insert
1213 Predicate pred, ///< The predicate to use for the comparison
1214 Value *LHS, ///< The left-hand-side of the expression
1215 Value *RHS, ///< The right-hand-side of the expression
1216 const Twine &NameStr = "" ///< Name of the instruction
1217 ) : CmpInst(makeCmpResultType(LHS->getType()),
1218 Instruction::ICmp, pred, LHS, RHS, NameStr,
1219 InsertBefore) {
1220#ifndef NDEBUG
1221 AssertOK();
1222#endif
1223 }
1224
1225 /// Constructor with insert-at-end semantics.
1226 ICmpInst(
1227 BasicBlock &InsertAtEnd, ///< Block to insert into.
1228 Predicate pred, ///< The predicate to use for the comparison
1229 Value *LHS, ///< The left-hand-side of the expression
1230 Value *RHS, ///< The right-hand-side of the expression
1231 const Twine &NameStr = "" ///< Name of the instruction
1232 ) : CmpInst(makeCmpResultType(LHS->getType()),
1233 Instruction::ICmp, pred, LHS, RHS, NameStr,
1234 &InsertAtEnd) {
1235#ifndef NDEBUG
1236 AssertOK();
1237#endif
1238 }
1239
1240 /// Constructor with no-insertion semantics
1241 ICmpInst(
1242 Predicate pred, ///< The predicate to use for the comparison
1243 Value *LHS, ///< The left-hand-side of the expression
1244 Value *RHS, ///< The right-hand-side of the expression
1245 const Twine &NameStr = "" ///< Name of the instruction
1246 ) : CmpInst(makeCmpResultType(LHS->getType()),
1247 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1248#ifndef NDEBUG
1249 AssertOK();
1250#endif
1251 }
1252
1253 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1254