Bug Summary

File:llvm/include/llvm/IR/Instructions.h
Warning:line 1724, column 19
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name InstCombineSelect.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 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Transforms/InstCombine -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

1//===- InstCombineSelect.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 visitSelect function.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/Optional.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/Analysis/AssumptionCache.h"
19#include "llvm/Analysis/CmpInstAnalysis.h"
20#include "llvm/Analysis/InstructionSimplify.h"
21#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Constant.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/IRBuilder.h"
27#include "llvm/IR/InstrTypes.h"
28#include "llvm/IR/Instruction.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/Intrinsics.h"
32#include "llvm/IR/Operator.h"
33#include "llvm/IR/PatternMatch.h"
34#include "llvm/IR/Type.h"
35#include "llvm/IR/User.h"
36#include "llvm/IR/Value.h"
37#include "llvm/Support/Casting.h"
38#include "llvm/Support/ErrorHandling.h"
39#include "llvm/Support/KnownBits.h"
40#include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
41#include <cassert>
42#include <utility>
43
44using namespace llvm;
45using namespace PatternMatch;
46
47#define DEBUG_TYPE"instcombine" "instcombine"
48
49static Value *createMinMax(InstCombiner::BuilderTy &Builder,
50 SelectPatternFlavor SPF, Value *A, Value *B) {
51 CmpInst::Predicate Pred = getMinMaxPred(SPF);
52 assert(CmpInst::isIntPredicate(Pred) && "Expected integer predicate")((CmpInst::isIntPredicate(Pred) && "Expected integer predicate"
) ? static_cast<void> (0) : __assert_fail ("CmpInst::isIntPredicate(Pred) && \"Expected integer predicate\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 52, __PRETTY_FUNCTION__))
;
53 return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B);
54}
55
56/// Replace a select operand based on an equality comparison with the identity
57/// constant of a binop.
58static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
59 const TargetLibraryInfo &TLI) {
60 // The select condition must be an equality compare with a constant operand.
61 Value *X;
62 Constant *C;
63 CmpInst::Predicate Pred;
64 if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C))))
65 return nullptr;
66
67 bool IsEq;
68 if (ICmpInst::isEquality(Pred))
69 IsEq = Pred == ICmpInst::ICMP_EQ;
70 else if (Pred == FCmpInst::FCMP_OEQ)
71 IsEq = true;
72 else if (Pred == FCmpInst::FCMP_UNE)
73 IsEq = false;
74 else
75 return nullptr;
76
77 // A select operand must be a binop.
78 BinaryOperator *BO;
79 if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)))
80 return nullptr;
81
82 // The compare constant must be the identity constant for that binop.
83 // If this a floating-point compare with 0.0, any zero constant will do.
84 Type *Ty = BO->getType();
85 Constant *IdC = ConstantExpr::getBinOpIdentity(BO->getOpcode(), Ty, true);
86 if (IdC != C) {
87 if (!IdC || !CmpInst::isFPPredicate(Pred))
88 return nullptr;
89 if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP()))
90 return nullptr;
91 }
92
93 // Last, match the compare variable operand with a binop operand.
94 Value *Y;
95 if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X))))
96 return nullptr;
97 if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X))))
98 return nullptr;
99
100 // +0.0 compares equal to -0.0, and so it does not behave as required for this
101 // transform. Bail out if we can not exclude that possibility.
102 if (isa<FPMathOperator>(BO))
103 if (!BO->hasNoSignedZeros() && !CannotBeNegativeZero(Y, &TLI))
104 return nullptr;
105
106 // BO = binop Y, X
107 // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO }
108 // =>
109 // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y }
110 Sel.setOperand(IsEq ? 1 : 2, Y);
111 return &Sel;
112}
113
114/// This folds:
115/// select (icmp eq (and X, C1)), TC, FC
116/// iff C1 is a power 2 and the difference between TC and FC is a power-of-2.
117/// To something like:
118/// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC
119/// Or:
120/// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC
121/// With some variations depending if FC is larger than TC, or the shift
122/// isn't needed, or the bit widths don't match.
123static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
124 InstCombiner::BuilderTy &Builder) {
125 const APInt *SelTC, *SelFC;
126 if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
127 !match(Sel.getFalseValue(), m_APInt(SelFC)))
128 return nullptr;
129
130 // If this is a vector select, we need a vector compare.
131 Type *SelType = Sel.getType();
132 if (SelType->isVectorTy() != Cmp->getType()->isVectorTy())
133 return nullptr;
134
135 Value *V;
136 APInt AndMask;
137 bool CreateAnd = false;
138 ICmpInst::Predicate Pred = Cmp->getPredicate();
139 if (ICmpInst::isEquality(Pred)) {
140 if (!match(Cmp->getOperand(1), m_Zero()))
141 return nullptr;
142
143 V = Cmp->getOperand(0);
144 const APInt *AndRHS;
145 if (!match(V, m_And(m_Value(), m_Power2(AndRHS))))
146 return nullptr;
147
148 AndMask = *AndRHS;
149 } else if (decomposeBitTestICmp(Cmp->getOperand(0), Cmp->getOperand(1),
150 Pred, V, AndMask)) {
151 assert(ICmpInst::isEquality(Pred) && "Not equality test?")((ICmpInst::isEquality(Pred) && "Not equality test?")
? static_cast<void> (0) : __assert_fail ("ICmpInst::isEquality(Pred) && \"Not equality test?\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 151, __PRETTY_FUNCTION__))
;
152 if (!AndMask.isPowerOf2())
153 return nullptr;
154
155 CreateAnd = true;
156 } else {
157 return nullptr;
158 }
159
160 // In general, when both constants are non-zero, we would need an offset to
161 // replace the select. This would require more instructions than we started
162 // with. But there's one special-case that we handle here because it can
163 // simplify/reduce the instructions.
164 APInt TC = *SelTC;
165 APInt FC = *SelFC;
166 if (!TC.isNullValue() && !FC.isNullValue()) {
167 // If the select constants differ by exactly one bit and that's the same
168 // bit that is masked and checked by the select condition, the select can
169 // be replaced by bitwise logic to set/clear one bit of the constant result.
170 if (TC.getBitWidth() != AndMask.getBitWidth() || (TC ^ FC) != AndMask)
171 return nullptr;
172 if (CreateAnd) {
173 // If we have to create an 'and', then we must kill the cmp to not
174 // increase the instruction count.
175 if (!Cmp->hasOneUse())
176 return nullptr;
177 V = Builder.CreateAnd(V, ConstantInt::get(SelType, AndMask));
178 }
179 bool ExtraBitInTC = TC.ugt(FC);
180 if (Pred == ICmpInst::ICMP_EQ) {
181 // If the masked bit in V is clear, clear or set the bit in the result:
182 // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) ^ TC
183 // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) | TC
184 Constant *C = ConstantInt::get(SelType, TC);
185 return ExtraBitInTC ? Builder.CreateXor(V, C) : Builder.CreateOr(V, C);
186 }
187 if (Pred == ICmpInst::ICMP_NE) {
188 // If the masked bit in V is set, set or clear the bit in the result:
189 // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) | FC
190 // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) ^ FC
191 Constant *C = ConstantInt::get(SelType, FC);
192 return ExtraBitInTC ? Builder.CreateOr(V, C) : Builder.CreateXor(V, C);
193 }
194 llvm_unreachable("Only expecting equality predicates")::llvm::llvm_unreachable_internal("Only expecting equality predicates"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 194)
;
195 }
196
197 // Make sure one of the select arms is a power-of-2.
198 if (!TC.isPowerOf2() && !FC.isPowerOf2())
199 return nullptr;
200
201 // Determine which shift is needed to transform result of the 'and' into the
202 // desired result.
203 const APInt &ValC = !TC.isNullValue() ? TC : FC;
204 unsigned ValZeros = ValC.logBase2();
205 unsigned AndZeros = AndMask.logBase2();
206
207 // Insert the 'and' instruction on the input to the truncate.
208 if (CreateAnd)
209 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask));
210
211 // If types don't match, we can still convert the select by introducing a zext
212 // or a trunc of the 'and'.
213 if (ValZeros > AndZeros) {
214 V = Builder.CreateZExtOrTrunc(V, SelType);
215 V = Builder.CreateShl(V, ValZeros - AndZeros);
216 } else if (ValZeros < AndZeros) {
217 V = Builder.CreateLShr(V, AndZeros - ValZeros);
218 V = Builder.CreateZExtOrTrunc(V, SelType);
219 } else {
220 V = Builder.CreateZExtOrTrunc(V, SelType);
221 }
222
223 // Okay, now we know that everything is set up, we just don't know whether we
224 // have a icmp_ne or icmp_eq and whether the true or false val is the zero.
225 bool ShouldNotVal = !TC.isNullValue();
226 ShouldNotVal ^= Pred == ICmpInst::ICMP_NE;
227 if (ShouldNotVal)
228 V = Builder.CreateXor(V, ValC);
229
230 return V;
231}
232
233/// We want to turn code that looks like this:
234/// %C = or %A, %B
235/// %D = select %cond, %C, %A
236/// into:
237/// %C = select %cond, %B, 0
238/// %D = or %A, %C
239///
240/// Assuming that the specified instruction is an operand to the select, return
241/// a bitmask indicating which operands of this instruction are foldable if they
242/// equal the other incoming value of the select.
243static unsigned getSelectFoldableOperands(BinaryOperator *I) {
244 switch (I->getOpcode()) {
245 case Instruction::Add:
246 case Instruction::Mul:
247 case Instruction::And:
248 case Instruction::Or:
249 case Instruction::Xor:
250 return 3; // Can fold through either operand.
251 case Instruction::Sub: // Can only fold on the amount subtracted.
252 case Instruction::Shl: // Can only fold on the shift amount.
253 case Instruction::LShr:
254 case Instruction::AShr:
255 return 1;
256 default:
257 return 0; // Cannot fold
258 }
259}
260
261/// For the same transformation as the previous function, return the identity
262/// constant that goes into the select.
263static APInt getSelectFoldableConstant(BinaryOperator *I) {
264 switch (I->getOpcode()) {
265 default: llvm_unreachable("This cannot happen!")::llvm::llvm_unreachable_internal("This cannot happen!", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 265)
;
266 case Instruction::Add:
267 case Instruction::Sub:
268 case Instruction::Or:
269 case Instruction::Xor:
270 case Instruction::Shl:
271 case Instruction::LShr:
272 case Instruction::AShr:
273 return APInt::getNullValue(I->getType()->getScalarSizeInBits());
274 case Instruction::And:
275 return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits());
276 case Instruction::Mul:
277 return APInt(I->getType()->getScalarSizeInBits(), 1);
278 }
279}
280
281/// We have (select c, TI, FI), and we know that TI and FI have the same opcode.
282Instruction *InstCombiner::foldSelectOpOp(SelectInst &SI, Instruction *TI,
283 Instruction *FI) {
284 // Don't break up min/max patterns. The hasOneUse checks below prevent that
285 // for most cases, but vector min/max with bitcasts can be transformed. If the
286 // one-use restrictions are eased for other patterns, we still don't want to
287 // obfuscate min/max.
288 if ((match(&SI, m_SMin(m_Value(), m_Value())) ||
289 match(&SI, m_SMax(m_Value(), m_Value())) ||
290 match(&SI, m_UMin(m_Value(), m_Value())) ||
291 match(&SI, m_UMax(m_Value(), m_Value()))))
292 return nullptr;
293
294 // If this is a cast from the same type, merge.
295 Value *Cond = SI.getCondition();
296 Type *CondTy = Cond->getType();
297 if (TI->getNumOperands() == 1 && TI->isCast()) {
298 Type *FIOpndTy = FI->getOperand(0)->getType();
299 if (TI->getOperand(0)->getType() != FIOpndTy)
300 return nullptr;
301
302 // The select condition may be a vector. We may only change the operand
303 // type if the vector width remains the same (and matches the condition).
304 if (CondTy->isVectorTy()) {
305 if (!FIOpndTy->isVectorTy())
306 return nullptr;
307 if (CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements())
308 return nullptr;
309
310 // TODO: If the backend knew how to deal with casts better, we could
311 // remove this limitation. For now, there's too much potential to create
312 // worse codegen by promoting the select ahead of size-altering casts
313 // (PR28160).
314 //
315 // Note that ValueTracking's matchSelectPattern() looks through casts
316 // without checking 'hasOneUse' when it matches min/max patterns, so this
317 // transform may end up happening anyway.
318 if (TI->getOpcode() != Instruction::BitCast &&
319 (!TI->hasOneUse() || !FI->hasOneUse()))
320 return nullptr;
321 } else if (!TI->hasOneUse() || !FI->hasOneUse()) {
322 // TODO: The one-use restrictions for a scalar select could be eased if
323 // the fold of a select in visitLoadInst() was enhanced to match a pattern
324 // that includes a cast.
325 return nullptr;
326 }
327
328 // Fold this by inserting a select from the input values.
329 Value *NewSI =
330 Builder.CreateSelect(Cond, TI->getOperand(0), FI->getOperand(0),
331 SI.getName() + ".v", &SI);
332 return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
333 TI->getType());
334 }
335
336 // Cond ? -X : -Y --> -(Cond ? X : Y)
337 Value *X, *Y;
338 if (match(TI, m_FNeg(m_Value(X))) && match(FI, m_FNeg(m_Value(Y))) &&
339 (TI->hasOneUse() || FI->hasOneUse())) {
340 Value *NewSel = Builder.CreateSelect(Cond, X, Y, SI.getName() + ".v", &SI);
341 // TODO: Remove the hack for the binop form when the unary op is optimized
342 // properly with all IR passes.
343 if (TI->getOpcode() != Instruction::FNeg)
344 return BinaryOperator::CreateFNegFMF(NewSel, cast<BinaryOperator>(TI));
345 return UnaryOperator::CreateFNeg(NewSel);
346 }
347
348 // Only handle binary operators (including two-operand getelementptr) with
349 // one-use here. As with the cast case above, it may be possible to relax the
350 // one-use constraint, but that needs be examined carefully since it may not
351 // reduce the total number of instructions.
352 if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 ||
353 (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) ||
354 !TI->hasOneUse() || !FI->hasOneUse())
355 return nullptr;
356
357 // Figure out if the operations have any operands in common.
358 Value *MatchOp, *OtherOpT, *OtherOpF;
359 bool MatchIsOpZero;
360 if (TI->getOperand(0) == FI->getOperand(0)) {
361 MatchOp = TI->getOperand(0);
362 OtherOpT = TI->getOperand(1);
363 OtherOpF = FI->getOperand(1);
364 MatchIsOpZero = true;
365 } else if (TI->getOperand(1) == FI->getOperand(1)) {
366 MatchOp = TI->getOperand(1);
367 OtherOpT = TI->getOperand(0);
368 OtherOpF = FI->getOperand(0);
369 MatchIsOpZero = false;
370 } else if (!TI->isCommutative()) {
371 return nullptr;
372 } else if (TI->getOperand(0) == FI->getOperand(1)) {
373 MatchOp = TI->getOperand(0);
374 OtherOpT = TI->getOperand(1);
375 OtherOpF = FI->getOperand(0);
376 MatchIsOpZero = true;
377 } else if (TI->getOperand(1) == FI->getOperand(0)) {
378 MatchOp = TI->getOperand(1);
379 OtherOpT = TI->getOperand(0);
380 OtherOpF = FI->getOperand(1);
381 MatchIsOpZero = true;
382 } else {
383 return nullptr;
384 }
385
386 // If the select condition is a vector, the operands of the original select's
387 // operands also must be vectors. This may not be the case for getelementptr
388 // for example.
389 if (CondTy->isVectorTy() && (!OtherOpT->getType()->isVectorTy() ||
390 !OtherOpF->getType()->isVectorTy()))
391 return nullptr;
392
393 // If we reach here, they do have operations in common.
394 Value *NewSI = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
395 SI.getName() + ".v", &SI);
396 Value *Op0 = MatchIsOpZero ? MatchOp : NewSI;
397 Value *Op1 = MatchIsOpZero ? NewSI : MatchOp;
398 if (auto *BO = dyn_cast<BinaryOperator>(TI)) {
399 BinaryOperator *NewBO = BinaryOperator::Create(BO->getOpcode(), Op0, Op1);
400 NewBO->copyIRFlags(TI);
401 NewBO->andIRFlags(FI);
402 return NewBO;
403 }
404 if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
405 auto *FGEP = cast<GetElementPtrInst>(FI);
406 Type *ElementType = TGEP->getResultElementType();
407 return TGEP->isInBounds() && FGEP->isInBounds()
408 ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1})
409 : GetElementPtrInst::Create(ElementType, Op0, {Op1});
410 }
411 llvm_unreachable("Expected BinaryOperator or GEP")::llvm::llvm_unreachable_internal("Expected BinaryOperator or GEP"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 411)
;
412 return nullptr;
413}
414
415static bool isSelect01(const APInt &C1I, const APInt &C2I) {
416 if (!C1I.isNullValue() && !C2I.isNullValue()) // One side must be zero.
417 return false;
418 return C1I.isOneValue() || C1I.isAllOnesValue() ||
419 C2I.isOneValue() || C2I.isAllOnesValue();
420}
421
422/// Try to fold the select into one of the operands to allow further
423/// optimization.
424Instruction *InstCombiner::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
425 Value *FalseVal) {
426 // See the comment above GetSelectFoldableOperands for a description of the
427 // transformation we are doing here.
428 if (auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) {
429 if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) {
430 if (unsigned SFO = getSelectFoldableOperands(TVI)) {
431 unsigned OpToFold = 0;
432 if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
433 OpToFold = 1;
434 } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
435 OpToFold = 2;
436 }
437
438 if (OpToFold) {
439 APInt CI = getSelectFoldableConstant(TVI);
440 Value *OOp = TVI->getOperand(2-OpToFold);
441 // Avoid creating select between 2 constants unless it's selecting
442 // between 0, 1 and -1.
443 const APInt *OOpC;
444 bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
445 if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) {
446 Value *C = ConstantInt::get(OOp->getType(), CI);
447 Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C);
448 NewSel->takeName(TVI);
449 BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(),
450 FalseVal, NewSel);
451 BO->copyIRFlags(TVI);
452 return BO;
453 }
454 }
455 }
456 }
457 }
458
459 if (auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) {
460 if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) {
461 if (unsigned SFO = getSelectFoldableOperands(FVI)) {
462 unsigned OpToFold = 0;
463 if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
464 OpToFold = 1;
465 } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
466 OpToFold = 2;
467 }
468
469 if (OpToFold) {
470 APInt CI = getSelectFoldableConstant(FVI);
471 Value *OOp = FVI->getOperand(2-OpToFold);
472 // Avoid creating select between 2 constants unless it's selecting
473 // between 0, 1 and -1.
474 const APInt *OOpC;
475 bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
476 if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) {
477 Value *C = ConstantInt::get(OOp->getType(), CI);
478 Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp);
479 NewSel->takeName(FVI);
480 BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(),
481 TrueVal, NewSel);
482 BO->copyIRFlags(FVI);
483 return BO;
484 }
485 }
486 }
487 }
488 }
489
490 return nullptr;
491}
492
493/// We want to turn:
494/// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1)
495/// into:
496/// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0)
497/// Note:
498/// Z may be 0 if lshr is missing.
499/// Worst-case scenario is that we will replace 5 instructions with 5 different
500/// instructions, but we got rid of select.
501static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp,
502 Value *TVal, Value *FVal,
503 InstCombiner::BuilderTy &Builder) {
504 if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() &&
505 Cmp->getPredicate() == ICmpInst::ICMP_EQ &&
506 match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One())))
507 return nullptr;
508
509 // The TrueVal has general form of: and %B, 1
510 Value *B;
511 if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One()))))
512 return nullptr;
513
514 // Where %B may be optionally shifted: lshr %X, %Z.
515 Value *X, *Z;
516 const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z))));
517 if (!HasShift)
518 X = B;
519
520 Value *Y;
521 if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y))))
522 return nullptr;
523
524 // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0
525 // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0
526 Constant *One = ConstantInt::get(SelType, 1);
527 Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One;
528 Value *FullMask = Builder.CreateOr(Y, MaskB);
529 Value *MaskedX = Builder.CreateAnd(X, FullMask);
530 Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX);
531 return new ZExtInst(ICmpNeZero, SelType);
532}
533
534/// We want to turn:
535/// (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1
536/// (select (icmp slt x, C), ashr (X, Y), lshr (X, Y)); iff C s>= 0
537/// into:
538/// ashr (X, Y)
539static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
540 Value *FalseVal,
541 InstCombiner::BuilderTy &Builder) {
542 ICmpInst::Predicate Pred = IC->getPredicate();
543 Value *CmpLHS = IC->getOperand(0);
544 Value *CmpRHS = IC->getOperand(1);
545 if (!CmpRHS->getType()->isIntOrIntVectorTy())
546 return nullptr;
547
548 Value *X, *Y;
549 unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits();
550 if ((Pred != ICmpInst::ICMP_SGT ||
551 !match(CmpRHS,
552 m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) &&
553 (Pred != ICmpInst::ICMP_SLT ||
554 !match(CmpRHS,
555 m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, 0)))))
556 return nullptr;
557
558 // Canonicalize so that ashr is in FalseVal.
559 if (Pred == ICmpInst::ICMP_SLT)
560 std::swap(TrueVal, FalseVal);
561
562 if (match(TrueVal, m_LShr(m_Value(X), m_Value(Y))) &&
563 match(FalseVal, m_AShr(m_Specific(X), m_Specific(Y))) &&
564 match(CmpLHS, m_Specific(X))) {
565 const auto *Ashr = cast<Instruction>(FalseVal);
566 // if lshr is not exact and ashr is, this new ashr must not be exact.
567 bool IsExact = Ashr->isExact() && cast<Instruction>(TrueVal)->isExact();
568 return Builder.CreateAShr(X, Y, IC->getName(), IsExact);
569 }
570
571 return nullptr;
572}
573
574/// We want to turn:
575/// (select (icmp eq (and X, C1), 0), Y, (or Y, C2))
576/// into:
577/// (or (shl (and X, C1), C3), Y)
578/// iff:
579/// C1 and C2 are both powers of 2
580/// where:
581/// C3 = Log(C2) - Log(C1)
582///
583/// This transform handles cases where:
584/// 1. The icmp predicate is inverted
585/// 2. The select operands are reversed
586/// 3. The magnitude of C2 and C1 are flipped
587static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
588 Value *FalseVal,
589 InstCombiner::BuilderTy &Builder) {
590 // Only handle integer compares. Also, if this is a vector select, we need a
591 // vector compare.
592 if (!TrueVal->getType()->isIntOrIntVectorTy() ||
593 TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy())
594 return nullptr;
595
596 Value *CmpLHS = IC->getOperand(0);
597 Value *CmpRHS = IC->getOperand(1);
598
599 Value *V;
600 unsigned C1Log;
601 bool IsEqualZero;
602 bool NeedAnd = false;
603 if (IC->isEquality()) {
604 if (!match(CmpRHS, m_Zero()))
605 return nullptr;
606
607 const APInt *C1;
608 if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1))))
609 return nullptr;
610
611 V = CmpLHS;
612 C1Log = C1->logBase2();
613 IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ;
614 } else if (IC->getPredicate() == ICmpInst::ICMP_SLT ||
615 IC->getPredicate() == ICmpInst::ICMP_SGT) {
616 // We also need to recognize (icmp slt (trunc (X)), 0) and
617 // (icmp sgt (trunc (X)), -1).
618 IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_SGT;
619 if ((IsEqualZero && !match(CmpRHS, m_AllOnes())) ||
620 (!IsEqualZero && !match(CmpRHS, m_Zero())))
621 return nullptr;
622
623 if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V)))))
624 return nullptr;
625
626 C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1;
627 NeedAnd = true;
628 } else {
629 return nullptr;
630 }
631
632 const APInt *C2;
633 bool OrOnTrueVal = false;
634 bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)));
635 if (!OrOnFalseVal)
636 OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)));
637
638 if (!OrOnFalseVal && !OrOnTrueVal)
639 return nullptr;
640
641 Value *Y = OrOnFalseVal ? TrueVal : FalseVal;
642
643 unsigned C2Log = C2->logBase2();
644
645 bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal);
646 bool NeedShift = C1Log != C2Log;
647 bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() !=
648 V->getType()->getScalarSizeInBits();
649
650 // Make sure we don't create more instructions than we save.
651 Value *Or = OrOnFalseVal ? FalseVal : TrueVal;
652 if ((NeedShift + NeedXor + NeedZExtTrunc) >
653 (IC->hasOneUse() + Or->hasOneUse()))
654 return nullptr;
655
656 if (NeedAnd) {
657 // Insert the AND instruction on the input to the truncate.
658 APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log);
659 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1));
660 }
661
662 if (C2Log > C1Log) {
663 V = Builder.CreateZExtOrTrunc(V, Y->getType());
664 V = Builder.CreateShl(V, C2Log - C1Log);
665 } else if (C1Log > C2Log) {
666 V = Builder.CreateLShr(V, C1Log - C2Log);
667 V = Builder.CreateZExtOrTrunc(V, Y->getType());
668 } else
669 V = Builder.CreateZExtOrTrunc(V, Y->getType());
670
671 if (NeedXor)
672 V = Builder.CreateXor(V, *C2);
673
674 return Builder.CreateOr(V, Y);
675}
676
677/// Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b).
678/// There are 8 commuted/swapped variants of this pattern.
679/// TODO: Also support a - UMIN(a,b) patterns.
680static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI,
681 const Value *TrueVal,
682 const Value *FalseVal,
683 InstCombiner::BuilderTy &Builder) {
684 ICmpInst::Predicate Pred = ICI->getPredicate();
685 if (!ICmpInst::isUnsigned(Pred))
686 return nullptr;
687
688 // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0
689 if (match(TrueVal, m_Zero())) {
690 Pred = ICmpInst::getInversePredicate(Pred);
691 std::swap(TrueVal, FalseVal);
692 }
693 if (!match(FalseVal, m_Zero()))
694 return nullptr;
695
696 Value *A = ICI->getOperand(0);
697 Value *B = ICI->getOperand(1);
698 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) {
699 // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0
700 std::swap(A, B);
701 Pred = ICmpInst::getSwappedPredicate(Pred);
702 }
703
704 assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&(((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
"Unexpected isUnsigned predicate!") ? static_cast<void>
(0) : __assert_fail ("(Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && \"Unexpected isUnsigned predicate!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 705, __PRETTY_FUNCTION__))
705 "Unexpected isUnsigned predicate!")(((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
"Unexpected isUnsigned predicate!") ? static_cast<void>
(0) : __assert_fail ("(Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && \"Unexpected isUnsigned predicate!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 705, __PRETTY_FUNCTION__))
;
706
707 // Ensure the sub is of the form:
708 // (a > b) ? a - b : 0 -> usub.sat(a, b)
709 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
710 // Checking for both a-b and a+(-b) as a constant.
711 bool IsNegative = false;
712 const APInt *C;
713 if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A))) ||
714 (match(A, m_APInt(C)) &&
715 match(TrueVal, m_Add(m_Specific(B), m_SpecificInt(-*C)))))
716 IsNegative = true;
717 else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B))) &&
718 !(match(B, m_APInt(C)) &&
719 match(TrueVal, m_Add(m_Specific(A), m_SpecificInt(-*C)))))
720 return nullptr;
721
722 // If we are adding a negate and the sub and icmp are used anywhere else, we
723 // would end up with more instructions.
724 if (IsNegative && !TrueVal->hasOneUse() && !ICI->hasOneUse())
725 return nullptr;
726
727 // (a > b) ? a - b : 0 -> usub.sat(a, b)
728 // (a > b) ? b - a : 0 -> -usub.sat(a, b)
729 Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, B);
730 if (IsNegative)
731 Result = Builder.CreateNeg(Result);
732 return Result;
733}
734
735static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
736 InstCombiner::BuilderTy &Builder) {
737 if (!Cmp->hasOneUse())
738 return nullptr;
739
740 // Match unsigned saturated add with constant.
741 Value *Cmp0 = Cmp->getOperand(0);
742 Value *Cmp1 = Cmp->getOperand(1);
743 ICmpInst::Predicate Pred = Cmp->getPredicate();
744 Value *X;
745 const APInt *C, *CmpC;
746 if (Pred == ICmpInst::ICMP_ULT &&
747 match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
748 match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
749 // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
750 return Builder.CreateBinaryIntrinsic(
751 Intrinsic::uadd_sat, X, ConstantInt::get(X->getType(), *C));
752 }
753
754 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
755 // There are 8 commuted variants.
756 // Canonicalize -1 (saturated result) to true value of the select. Just
757 // swapping the compare operands is legal, because the selected value is the
758 // same in case of equality, so we can interchange u< and u<=.
759 if (match(FVal, m_AllOnes())) {
760 std::swap(TVal, FVal);
761 std::swap(Cmp0, Cmp1);
762 }
763 if (!match(TVal, m_AllOnes()))
764 return nullptr;
765
766 // Canonicalize predicate to 'ULT'.
767 if (Pred == ICmpInst::ICMP_UGT) {
768 Pred = ICmpInst::ICMP_ULT;
769 std::swap(Cmp0, Cmp1);
770 }
771 if (Pred != ICmpInst::ICMP_ULT)
772 return nullptr;
773
774 // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
775 Value *Y;
776 if (match(Cmp0, m_Not(m_Value(X))) &&
777 match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) {
778 // (~X u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
779 // (~X u< Y) ? -1 : (Y + X) --> uadd.sat(X, Y)
780 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y);
781 }
782 // The 'not' op may be included in the sum but not the compare.
783 X = Cmp0;
784 Y = Cmp1;
785 if (match(FVal, m_c_Add(m_Not(m_Specific(X)), m_Specific(Y)))) {
786 // (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
787 // (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
788 BinaryOperator *BO = cast<BinaryOperator>(FVal);
789 return Builder.CreateBinaryIntrinsic(
790 Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
791 }
792 // The overflow may be detected via the add wrapping round.
793 if (match(Cmp0, m_c_Add(m_Specific(Cmp1), m_Value(Y))) &&
794 match(FVal, m_c_Add(m_Specific(Cmp1), m_Specific(Y)))) {
795 // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y)
796 // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
797 return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp1, Y);
798 }
799
800 return nullptr;
801}
802
803/// Fold the following code sequence:
804/// \code
805/// int a = ctlz(x & -x);
806// x ? 31 - a : a;
807/// \code
808///
809/// into:
810/// cttz(x)
811static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
812 Value *FalseVal,
813 InstCombiner::BuilderTy &Builder) {
814 unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits();
815 if (!ICI->isEquality() || !match(ICI->getOperand(1), m_Zero()))
816 return nullptr;
817
818 if (ICI->getPredicate() == ICmpInst::ICMP_NE)
819 std::swap(TrueVal, FalseVal);
820
821 if (!match(FalseVal,
822 m_Xor(m_Deferred(TrueVal), m_SpecificInt(BitWidth - 1))))
823 return nullptr;
824
825 if (!match(TrueVal, m_Intrinsic<Intrinsic::ctlz>()))
826 return nullptr;
827
828 Value *X = ICI->getOperand(0);
829 auto *II = cast<IntrinsicInst>(TrueVal);
830 if (!match(II->getOperand(0), m_c_And(m_Specific(X), m_Neg(m_Specific(X)))))
831 return nullptr;
832
833 Function *F = Intrinsic::getDeclaration(II->getModule(), Intrinsic::cttz,
834 II->getType());
835 return CallInst::Create(F, {X, II->getArgOperand(1)});
836}
837
838/// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single
839/// call to cttz/ctlz with flag 'is_zero_undef' cleared.
840///
841/// For example, we can fold the following code sequence:
842/// \code
843/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true)
844/// %1 = icmp ne i32 %x, 0
845/// %2 = select i1 %1, i32 %0, i32 32
846/// \code
847///
848/// into:
849/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
850static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
851 InstCombiner::BuilderTy &Builder) {
852 ICmpInst::Predicate Pred = ICI->getPredicate();
853 Value *CmpLHS = ICI->getOperand(0);
854 Value *CmpRHS = ICI->getOperand(1);
855
856 // Check if the condition value compares a value for equality against zero.
857 if (!ICI->isEquality() || !match(CmpRHS, m_Zero()))
858 return nullptr;
859
860 Value *Count = FalseVal;
861 Value *ValueOnZero = TrueVal;
862 if (Pred == ICmpInst::ICMP_NE)
863 std::swap(Count, ValueOnZero);
864
865 // Skip zero extend/truncate.
866 Value *V = nullptr;
867 if (match(Count, m_ZExt(m_Value(V))) ||
868 match(Count, m_Trunc(m_Value(V))))
869 Count = V;
870
871 // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the
872 // input to the cttz/ctlz is used as LHS for the compare instruction.
873 if (!match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) &&
874 !match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS))))
875 return nullptr;
876
877 IntrinsicInst *II = cast<IntrinsicInst>(Count);
878
879 // Check if the value propagated on zero is a constant number equal to the
880 // sizeof in bits of 'Count'.
881 unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits();
882 if (match(ValueOnZero, m_SpecificInt(SizeOfInBits))) {
883 // Explicitly clear the 'undef_on_zero' flag.
884 IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone());
885 NewI->setArgOperand(1, ConstantInt::getFalse(NewI->getContext()));
886 Builder.Insert(NewI);
887 return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType());
888 }
889
890 // If the ValueOnZero is not the bitwidth, we can at least make use of the
891 // fact that the cttz/ctlz result will not be used if the input is zero, so
892 // it's okay to relax it to undef for that case.
893 if (II->hasOneUse() && !match(II->getArgOperand(1), m_One()))
894 II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
895
896 return nullptr;
897}
898
899/// Return true if we find and adjust an icmp+select pattern where the compare
900/// is with a constant that can be incremented or decremented to match the
901/// minimum or maximum idiom.
902static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) {
903 ICmpInst::Predicate Pred = Cmp.getPredicate();
904 Value *CmpLHS = Cmp.getOperand(0);
905 Value *CmpRHS = Cmp.getOperand(1);
906 Value *TrueVal = Sel.getTrueValue();
907 Value *FalseVal = Sel.getFalseValue();
908
909 // We may move or edit the compare, so make sure the select is the only user.
910 const APInt *CmpC;
911 if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC)))
912 return false;
913
914 // These transforms only work for selects of integers or vector selects of
915 // integer vectors.
916 Type *SelTy = Sel.getType();
917 auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType());
918 if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy())
919 return false;
920
921 Constant *AdjustedRHS;
922 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
923 AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1);
924 else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT)
925 AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1);
926 else
927 return false;
928
929 // X > C ? X : C+1 --> X < C+1 ? C+1 : X
930 // X < C ? X : C-1 --> X > C-1 ? C-1 : X
931 if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
932 (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
933 ; // Nothing to do here. Values match without any sign/zero extension.
934 }
935 // Types do not match. Instead of calculating this with mixed types, promote
936 // all to the larger type. This enables scalar evolution to analyze this
937 // expression.
938 else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) {
939 Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy);
940
941 // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
942 // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
943 // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X
944 // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X
945 if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) {
946 CmpLHS = TrueVal;
947 AdjustedRHS = SextRHS;
948 } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) &&
949 SextRHS == TrueVal) {
950 CmpLHS = FalseVal;
951 AdjustedRHS = SextRHS;
952 } else if (Cmp.isUnsigned()) {
953 Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy);
954 // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
955 // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
956 // zext + signed compare cannot be changed:
957 // 0xff <s 0x00, but 0x00ff >s 0x0000
958 if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) {
959 CmpLHS = TrueVal;
960 AdjustedRHS = ZextRHS;
961 } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) &&
962 ZextRHS == TrueVal) {
963 CmpLHS = FalseVal;
964 AdjustedRHS = ZextRHS;
965 } else {
966 return false;
967 }
968 } else {
969 return false;
970 }
971 } else {
972 return false;
973 }
974
975 Pred = ICmpInst::getSwappedPredicate(Pred);
976 CmpRHS = AdjustedRHS;
977 std::swap(FalseVal, TrueVal);
978 Cmp.setPredicate(Pred);
979 Cmp.setOperand(0, CmpLHS);
980 Cmp.setOperand(1, CmpRHS);
981 Sel.setOperand(1, TrueVal);
982 Sel.setOperand(2, FalseVal);
983 Sel.swapProfMetadata();
984
985 // Move the compare instruction right before the select instruction. Otherwise
986 // the sext/zext value may be defined after the compare instruction uses it.
987 Cmp.moveBefore(&Sel);
988
989 return true;
990}
991
992/// If this is an integer min/max (icmp + select) with a constant operand,
993/// create the canonical icmp for the min/max operation and canonicalize the
994/// constant to the 'false' operand of the select:
995/// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2
996/// Note: if C1 != C2, this will change the icmp constant to the existing
997/// constant operand of the select.
998static Instruction *
999canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp,
1000 InstCombiner::BuilderTy &Builder) {
1001 if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1)))
1002 return nullptr;
1003
1004 // Canonicalize the compare predicate based on whether we have min or max.
1005 Value *LHS, *RHS;
1006 SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS);
1007 if (!SelectPatternResult::isMinOrMax(SPR.Flavor))
1008 return nullptr;
1009
1010 // Is this already canonical?
1011 ICmpInst::Predicate CanonicalPred = getMinMaxPred(SPR.Flavor);
1012 if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS &&
1013 Cmp.getPredicate() == CanonicalPred)
1014 return nullptr;
1015
1016 // Create the canonical compare and plug it into the select.
1017 Sel.setCondition(Builder.CreateICmp(CanonicalPred, LHS, RHS));
1018
1019 // If the select operands did not change, we're done.
1020 if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS)
1021 return &Sel;
1022
1023 // If we are swapping the select operands, swap the metadata too.
1024 assert(Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS &&((Sel.getTrueValue() == RHS && Sel.getFalseValue() ==
LHS && "Unexpected results from matchSelectPattern")
? static_cast<void> (0) : __assert_fail ("Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && \"Unexpected results from matchSelectPattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1025, __PRETTY_FUNCTION__))
1025 "Unexpected results from matchSelectPattern")((Sel.getTrueValue() == RHS && Sel.getFalseValue() ==
LHS && "Unexpected results from matchSelectPattern")
? static_cast<void> (0) : __assert_fail ("Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && \"Unexpected results from matchSelectPattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1025, __PRETTY_FUNCTION__))
;
1026 Sel.swapValues();
1027 Sel.swapProfMetadata();
1028 return &Sel;
1029}
1030
1031/// There are many select variants for each of ABS/NABS.
1032/// In matchSelectPattern(), there are different compare constants, compare
1033/// predicates/operands and select operands.
1034/// In isKnownNegation(), there are different formats of negated operands.
1035/// Canonicalize all these variants to 1 pattern.
1036/// This makes CSE more likely.
1037static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp,
1038 InstCombiner::BuilderTy &Builder) {
1039 if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1)))
1040 return nullptr;
1041
1042 // Choose a sign-bit check for the compare (likely simpler for codegen).
1043 // ABS: (X <s 0) ? -X : X
1044 // NABS: (X <s 0) ? X : -X
1045 Value *LHS, *RHS;
1046 SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor;
1047 if (SPF != SelectPatternFlavor::SPF_ABS &&
1048 SPF != SelectPatternFlavor::SPF_NABS)
1049 return nullptr;
1050
1051 Value *TVal = Sel.getTrueValue();
1052 Value *FVal = Sel.getFalseValue();
1053 assert(isKnownNegation(TVal, FVal) &&((isKnownNegation(TVal, FVal) && "Unexpected result from matchSelectPattern"
) ? static_cast<void> (0) : __assert_fail ("isKnownNegation(TVal, FVal) && \"Unexpected result from matchSelectPattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1054, __PRETTY_FUNCTION__))
1054 "Unexpected result from matchSelectPattern")((isKnownNegation(TVal, FVal) && "Unexpected result from matchSelectPattern"
) ? static_cast<void> (0) : __assert_fail ("isKnownNegation(TVal, FVal) && \"Unexpected result from matchSelectPattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1054, __PRETTY_FUNCTION__))
;
1055
1056 // The compare may use the negated abs()/nabs() operand, or it may use
1057 // negation in non-canonical form such as: sub A, B.
1058 bool CmpUsesNegatedOp = match(Cmp.getOperand(0), m_Neg(m_Specific(TVal))) ||
1059 match(Cmp.getOperand(0), m_Neg(m_Specific(FVal)));
1060
1061 bool CmpCanonicalized = !CmpUsesNegatedOp &&
1062 match(Cmp.getOperand(1), m_ZeroInt()) &&
1063 Cmp.getPredicate() == ICmpInst::ICMP_SLT;
1064 bool RHSCanonicalized = match(RHS, m_Neg(m_Specific(LHS)));
1065
1066 // Is this already canonical?
1067 if (CmpCanonicalized && RHSCanonicalized)
1068 return nullptr;
1069
1070 // If RHS is used by other instructions except compare and select, don't
1071 // canonicalize it to not increase the instruction count.
1072 if (!(RHS->hasOneUse() || (RHS->hasNUses(2) && CmpUsesNegatedOp)))
1073 return nullptr;
1074
1075 // Create the canonical compare: icmp slt LHS 0.
1076 if (!CmpCanonicalized) {
1077 Cmp.setPredicate(ICmpInst::ICMP_SLT);
1078 Cmp.setOperand(1, ConstantInt::getNullValue(Cmp.getOperand(0)->getType()));
1079 if (CmpUsesNegatedOp)
1080 Cmp.setOperand(0, LHS);
1081 }
1082
1083 // Create the canonical RHS: RHS = sub (0, LHS).
1084 if (!RHSCanonicalized) {
1085 assert(RHS->hasOneUse() && "RHS use number is not right")((RHS->hasOneUse() && "RHS use number is not right"
) ? static_cast<void> (0) : __assert_fail ("RHS->hasOneUse() && \"RHS use number is not right\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1085, __PRETTY_FUNCTION__))
;
1086 RHS = Builder.CreateNeg(LHS);
1087 if (TVal == LHS) {
1088 Sel.setFalseValue(RHS);
1089 FVal = RHS;
1090 } else {
1091 Sel.setTrueValue(RHS);
1092 TVal = RHS;
1093 }
1094 }
1095
1096 // If the select operands do not change, we're done.
1097 if (SPF == SelectPatternFlavor::SPF_NABS) {
1098 if (TVal == LHS)
1099 return &Sel;
1100 assert(FVal == LHS && "Unexpected results from matchSelectPattern")((FVal == LHS && "Unexpected results from matchSelectPattern"
) ? static_cast<void> (0) : __assert_fail ("FVal == LHS && \"Unexpected results from matchSelectPattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1100, __PRETTY_FUNCTION__))
;
1101 } else {
1102 if (FVal == LHS)
1103 return &Sel;
1104 assert(TVal == LHS && "Unexpected results from matchSelectPattern")((TVal == LHS && "Unexpected results from matchSelectPattern"
) ? static_cast<void> (0) : __assert_fail ("TVal == LHS && \"Unexpected results from matchSelectPattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 1104, __PRETTY_FUNCTION__))
;
1105 }
1106
1107 // We are swapping the select operands, so swap the metadata too.
1108 Sel.swapValues();
1109 Sel.swapProfMetadata();
1110 return &Sel;
1111}
1112
1113static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *ReplaceOp,
1114 const SimplifyQuery &Q) {
1115 // If this is a binary operator, try to simplify it with the replaced op
1116 // because we know Op and ReplaceOp are equivalant.
1117 // For example: V = X + 1, Op = X, ReplaceOp = 42
1118 // Simplifies as: add(42, 1) --> 43
1119 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
1120 if (BO->getOperand(0) == Op)
1121 return SimplifyBinOp(BO->getOpcode(), ReplaceOp, BO->getOperand(1), Q);
1122 if (BO->getOperand(1) == Op)
1123 return SimplifyBinOp(BO->getOpcode(), BO->getOperand(0), ReplaceOp, Q);
1124 }
1125
1126 return nullptr;
1127}
1128
1129/// If we have a select with an equality comparison, then we know the value in
1130/// one of the arms of the select. See if substituting this value into an arm
1131/// and simplifying the result yields the same value as the other arm.
1132///
1133/// To make this transform safe, we must drop poison-generating flags
1134/// (nsw, etc) if we simplified to a binop because the select may be guarding
1135/// that poison from propagating. If the existing binop already had no
1136/// poison-generating flags, then this transform can be done by instsimplify.
1137///
1138/// Consider:
1139/// %cmp = icmp eq i32 %x, 2147483647
1140/// %add = add nsw i32 %x, 1
1141/// %sel = select i1 %cmp, i32 -2147483648, i32 %add
1142///
1143/// We can't replace %sel with %add unless we strip away the flags.
1144/// TODO: Wrapping flags could be preserved in some cases with better analysis.
1145static Value *foldSelectValueEquivalence(SelectInst &Sel, ICmpInst &Cmp,
1146 const SimplifyQuery &Q) {
1147 if (!Cmp.isEquality())
1148 return nullptr;
1149
1150 // Canonicalize the pattern to ICMP_EQ by swapping the select operands.
1151 Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
1152 if (Cmp.getPredicate() == ICmpInst::ICMP_NE)
1153 std::swap(TrueVal, FalseVal);
1154
1155 // Try each equivalence substitution possibility.
1156 // We have an 'EQ' comparison, so the select's false value will propagate.
1157 // Example:
1158 // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
1159 // (X == 42) ? (X + 1) : 43 --> (X == 42) ? (42 + 1) : 43 --> 43
1160 Value *CmpLHS = Cmp.getOperand(0), *CmpRHS = Cmp.getOperand(1);
1161 if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q) == TrueVal ||
1162 simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q) == TrueVal ||
1163 simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q) == FalseVal ||
1164 simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q) == FalseVal) {
1165 if (auto *FalseInst = dyn_cast<Instruction>(FalseVal))
1166 FalseInst->dropPoisonGeneratingFlags();
1167 return FalseVal;
1168 }
1169 return nullptr;
1170}
1171
1172// See if this is a pattern like:
1173// %old_cmp1 = icmp slt i32 %x, C2
1174// %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
1175// %old_x_offseted = add i32 %x, C1
1176// %old_cmp0 = icmp ult i32 %old_x_offseted, C0
1177// %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement
1178// This can be rewritten as more canonical pattern:
1179// %new_cmp1 = icmp slt i32 %x, -C1
1180// %new_cmp2 = icmp sge i32 %x, C0-C1
1181// %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x
1182// %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low
1183// Iff -C1 s<= C2 s<= C0-C1
1184// Also ULT predicate can also be UGT iff C0 != -1 (+invert result)
1185// SLT predicate can also be SGT iff C2 != INT_MAX (+invert res.)
1186static Instruction *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
1187 InstCombiner::BuilderTy &Builder) {
1188 Value *X = Sel0.getTrueValue();
1189 Value *Sel1 = Sel0.getFalseValue();
1190
1191 // First match the condition of the outermost select.
1192 // Said condition must be one-use.
1193 if (!Cmp0.hasOneUse())
1194 return nullptr;
1195 Value *Cmp00 = Cmp0.getOperand(0);
1196 Constant *C0;
1197 if (!match(Cmp0.getOperand(1),
1198 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0))))
1199 return nullptr;
1200 // Canonicalize Cmp0 into the form we expect.
1201 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1202 switch (Cmp0.getPredicate()) {
1203 case ICmpInst::Predicate::ICMP_ULT:
1204 break; // Great!
1205 case ICmpInst::Predicate::ICMP_ULE:
1206 // We'd have to increment C0 by one, and for that it must not have all-ones
1207 // element, but then it would have been canonicalized to 'ult' before
1208 // we get here. So we can't do anything useful with 'ule'.
1209 return nullptr;
1210 case ICmpInst::Predicate::ICMP_UGT:
1211 // We want to canonicalize it to 'ult', so we'll need to increment C0,
1212 // which again means it must not have any all-ones elements.
1213 if (!match(C0,
1214 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
1215 APInt::getAllOnesValue(
1216 C0->getType()->getScalarSizeInBits()))))
1217 return nullptr; // Can't do, have all-ones element[s].
1218 C0 = AddOne(C0);
1219 std::swap(X, Sel1);
1220 break;
1221 case ICmpInst::Predicate::ICMP_UGE:
1222 // The only way we'd get this predicate if this `icmp` has extra uses,
1223 // but then we won't be able to do this fold.
1224 return nullptr;
1225 default:
1226 return nullptr; // Unknown predicate.
1227 }
1228
1229 // Now that we've canonicalized the ICmp, we know the X we expect;
1230 // the select in other hand should be one-use.
1231 if (!Sel1->hasOneUse())
1232 return nullptr;
1233
1234 // We now can finish matching the condition of the outermost select:
1235 // it should either be the X itself, or an addition of some constant to X.
1236 Constant *C1;
1237 if (Cmp00 == X)
1238 C1 = ConstantInt::getNullValue(Sel0.getType());
1239 else if (!match(Cmp00,
1240 m_Add(m_Specific(X),
1241 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C1)))))
1242 return nullptr;
1243
1244 Value *Cmp1;
1245 ICmpInst::Predicate Pred1;
1246 Constant *C2;
1247 Value *ReplacementLow, *ReplacementHigh;
1248 if (!match(Sel1, m_Select(m_Value(Cmp1), m_Value(ReplacementLow),
1249 m_Value(ReplacementHigh))) ||
1250 !match(Cmp1,
1251 m_ICmp(Pred1, m_Specific(X),
1252 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C2)))))
1253 return nullptr;
1254
1255 if (!Cmp1->hasOneUse() && (Cmp00 == X || !Cmp00->hasOneUse()))
1256 return nullptr; // Not enough one-use instructions for the fold.
1257 // FIXME: this restriction could be relaxed if Cmp1 can be reused as one of
1258 // two comparisons we'll need to build.
1259
1260 // Canonicalize Cmp1 into the form we expect.
1261 // FIXME: we shouldn't care about lanes that are 'undef' in the end?
1262 switch (Pred1) {
1263 case ICmpInst::Predicate::ICMP_SLT:
1264 break;
1265 case ICmpInst::Predicate::ICMP_SLE:
1266 // We'd have to increment C2 by one, and for that it must not have signed
1267 // max element, but then it would have been canonicalized to 'slt' before
1268 // we get here. So we can't do anything useful with 'sle'.
1269 return nullptr;
1270 case ICmpInst::Predicate::ICMP_SGT:
1271 // We want to canonicalize it to 'slt', so we'll need to increment C2,
1272 // which again means it must not have any signed max elements.
1273 if (!match(C2,
1274 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
1275 APInt::getSignedMaxValue(
1276 C2->getType()->getScalarSizeInBits()))))
1277 return nullptr; // Can't do, have signed max element[s].
1278 C2 = AddOne(C2);
1279 LLVM_FALLTHROUGH[[gnu::fallthrough]];
1280 case ICmpInst::Predicate::ICMP_SGE:
1281 // Also non-canonical, but here we don't need to change C2,
1282 // so we don't have any restrictions on C2, so we can just handle it.
1283 std::swap(ReplacementLow, ReplacementHigh);
1284 break;
1285 default:
1286 return nullptr; // Unknown predicate.
1287 }
1288
1289 // The thresholds of this clamp-like pattern.
1290 auto *ThresholdLowIncl = ConstantExpr::getNeg(C1);
1291 auto *ThresholdHighExcl = ConstantExpr::getSub(C0, C1);
1292
1293 // The fold has a precondition 1: C2 s>= ThresholdLow
1294 auto *Precond1 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SGE, C2,
1295 ThresholdLowIncl);
1296 if (!match(Precond1, m_One()))
1297 return nullptr;
1298 // The fold has a precondition 2: C2 s<= ThresholdHigh
1299 auto *Precond2 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SLE, C2,
1300 ThresholdHighExcl);
1301 if (!match(Precond2, m_One()))
1302 return nullptr;
1303
1304 // All good, finally emit the new pattern.
1305 Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl);
1306 Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl);
1307 Value *MaybeReplacedLow =
1308 Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X);
1309 Instruction *MaybeReplacedHigh =
1310 SelectInst::Create(ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow);
1311
1312 return MaybeReplacedHigh;
1313}
1314
1315// If we have
1316// %cmp = icmp [canonical predicate] i32 %x, C0
1317// %r = select i1 %cmp, i32 %y, i32 C1
1318// Where C0 != C1 and %x may be different from %y, see if the constant that we
1319// will have if we flip the strictness of the predicate (i.e. without changing
1320// the result) is identical to the C1 in select. If it matches we can change
1321// original comparison to one with swapped predicate, reuse the constant,
1322// and swap the hands of select.
1323static Instruction *
1324tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
1325 InstCombiner::BuilderTy &Builder) {
1326 ICmpInst::Predicate Pred;
1327 Value *X;
1328 Constant *C0;
1329 if (!match(&Cmp, m_OneUse(m_ICmp(
1330 Pred, m_Value(X),
1331 m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0))))))
1332 return nullptr;
1333
1334 // If comparison predicate is non-relational, we won't be able to do anything.
1335 if (ICmpInst::isEquality(Pred))
1336 return nullptr;
1337
1338 // If comparison predicate is non-canonical, then we certainly won't be able
1339 // to make it canonical; canonicalizeCmpWithConstant() already tried.
1340 if (!isCanonicalPredicate(Pred))
1341 return nullptr;
1342
1343 // If the [input] type of comparison and select type are different, lets abort
1344 // for now. We could try to compare constants with trunc/[zs]ext though.
1345 if (C0->getType() != Sel.getType())
1346 return nullptr;
1347
1348 // FIXME: are there any magic icmp predicate+constant pairs we must not touch?
1349
1350 Value *SelVal0, *SelVal1; // We do not care which one is from where.
1351 match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
1352 // At least one of these values we are selecting between must be a constant
1353 // else we'll never succeed.
1354 if (!match(SelVal0, m_AnyIntegralConstant()) &&
1355 !match(SelVal1, m_AnyIntegralConstant()))
1356 return nullptr;
1357
1358 // Does this constant C match any of the `select` values?
1359 auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
1360 return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
1361 };
1362
1363 // If C0 *already* matches true/false value of select, we are done.
1364 if (MatchesSelectValue(C0))
1365 return nullptr;
1366
1367 // Check the constant we'd have with flipped-strictness predicate.
1368 auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant(Pred, C0);
1369 if (!FlippedStrictness)
1370 return nullptr;
1371
1372 // If said constant doesn't match either, then there is no hope,
1373 if (!MatchesSelectValue(FlippedStrictness->second))
1374 return nullptr;
1375
1376 // It matched! Lets insert the new comparison just before select.
1377 InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);
1378 Builder.SetInsertPoint(&Sel);
1379
1380 Pred = ICmpInst::getSwappedPredicate(Pred); // Yes, swapped.
1381 Value *NewCmp = Builder.CreateICmp(Pred, X, FlippedStrictness->second,
1382 Cmp.getName() + ".inv");
1383 Sel.setCondition(NewCmp);
1384 Sel.swapValues();
1385 Sel.swapProfMetadata();
1386
1387 return &Sel;
1388}
1389
1390/// Visit a SelectInst that has an ICmpInst as its first operand.
1391Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI,
1392 ICmpInst *ICI) {
1393 if (Value *V = foldSelectValueEquivalence(SI, *ICI, SQ))
1394 return replaceInstUsesWith(SI, V);
1395
1396 if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder))
1397 return NewSel;
1398
1399 if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, Builder))
1400 return NewAbs;
1401
1402 if (Instruction *NewAbs = canonicalizeClampLike(SI, *ICI, Builder))
1403 return NewAbs;
1404
1405 if (Instruction *NewSel =
1406 tryToReuseConstantFromSelectInComparison(SI, *ICI, Builder))
1407 return NewSel;
1408
1409 bool Changed = adjustMinMax(SI, *ICI);
1410
1411 if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
1412 return replaceInstUsesWith(SI, V);
1413
1414 // NOTE: if we wanted to, this is where to detect integer MIN/MAX
1415 Value *TrueVal = SI.getTrueValue();
1416 Value *FalseVal = SI.getFalseValue();
1417 ICmpInst::Predicate Pred = ICI->getPredicate();
1418 Value *CmpLHS = ICI->getOperand(0);
1419 Value *CmpRHS = ICI->getOperand(1);
1420 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
1421 if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
1422 // Transform (X == C) ? X : Y -> (X == C) ? C : Y
1423 SI.setOperand(1, CmpRHS);
1424 Changed = true;
1425 } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
1426 // Transform (X != C) ? Y : X -> (X != C) ? Y : C
1427 SI.setOperand(2, CmpRHS);
1428 Changed = true;
1429 }
1430 }
1431
1432 // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring
1433 // decomposeBitTestICmp() might help.
1434 {
1435 unsigned BitWidth =
1436 DL.getTypeSizeInBits(TrueVal->getType()->getScalarType());
1437 APInt MinSignedValue = APInt::getSignedMinValue(BitWidth);
1438 Value *X;
1439 const APInt *Y, *C;
1440 bool TrueWhenUnset;
1441 bool IsBitTest = false;
1442 if (ICmpInst::isEquality(Pred) &&
1443 match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) &&
1444 match(CmpRHS, m_Zero())) {
1445 IsBitTest = true;
1446 TrueWhenUnset = Pred == ICmpInst::ICMP_EQ;
1447 } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) {
1448 X = CmpLHS;
1449 Y = &MinSignedValue;
1450 IsBitTest = true;
1451 TrueWhenUnset = false;
1452 } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) {
1453 X = CmpLHS;
1454 Y = &MinSignedValue;
1455 IsBitTest = true;
1456 TrueWhenUnset = true;
1457 }
1458 if (IsBitTest) {
1459 Value *V = nullptr;
1460 // (X & Y) == 0 ? X : X ^ Y --> X & ~Y
1461 if (TrueWhenUnset && TrueVal == X &&
1462 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1463 V = Builder.CreateAnd(X, ~(*Y));
1464 // (X & Y) != 0 ? X ^ Y : X --> X & ~Y
1465 else if (!TrueWhenUnset && FalseVal == X &&
1466 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1467 V = Builder.CreateAnd(X, ~(*Y));
1468 // (X & Y) == 0 ? X ^ Y : X --> X | Y
1469 else if (TrueWhenUnset && FalseVal == X &&
1470 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1471 V = Builder.CreateOr(X, *Y);
1472 // (X & Y) != 0 ? X : X ^ Y --> X | Y
1473 else if (!TrueWhenUnset && TrueVal == X &&
1474 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
1475 V = Builder.CreateOr(X, *Y);
1476
1477 if (V)
1478 return replaceInstUsesWith(SI, V);
1479 }
1480 }
1481
1482 if (Instruction *V =
1483 foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder))
1484 return V;
1485
1486 if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
1487 return V;
1488
1489 if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder))
1490 return replaceInstUsesWith(SI, V);
1491
1492 if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))
1493 return replaceInstUsesWith(SI, V);
1494
1495 if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
1496 return replaceInstUsesWith(SI, V);
1497
1498 if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder))
1499 return replaceInstUsesWith(SI, V);
1500
1501 if (Value *V = canonicalizeSaturatedAdd(ICI, TrueVal, FalseVal, Builder))
1502 return replaceInstUsesWith(SI, V);
1503
1504 return Changed ? &SI : nullptr;
1505}
1506
1507/// SI is a select whose condition is a PHI node (but the two may be in
1508/// different blocks). See if the true/false values (V) are live in all of the
1509/// predecessor blocks of the PHI. For example, cases like this can't be mapped:
1510///
1511/// X = phi [ C1, BB1], [C2, BB2]
1512/// Y = add
1513/// Z = select X, Y, 0
1514///
1515/// because Y is not live in BB1/BB2.
1516static bool canSelectOperandBeMappingIntoPredBlock(const Value *V,
1517 const SelectInst &SI) {
1518 // If the value is a non-instruction value like a constant or argument, it
1519 // can always be mapped.
1520 const Instruction *I = dyn_cast<Instruction>(V);
1521 if (!I) return true;
1522
1523 // If V is a PHI node defined in the same block as the condition PHI, we can
1524 // map the arguments.
1525 const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
1526
1527 if (const PHINode *VP = dyn_cast<PHINode>(I))
1528 if (VP->getParent() == CondPHI->getParent())
1529 return true;
1530
1531 // Otherwise, if the PHI and select are defined in the same block and if V is
1532 // defined in a different block, then we can transform it.
1533 if (SI.getParent() == CondPHI->getParent() &&
1534 I->getParent() != CondPHI->getParent())
1535 return true;
1536
1537 // Otherwise we have a 'hard' case and we can't tell without doing more
1538 // detailed dominator based analysis, punt.
1539 return false;
1540}
1541
1542/// We have an SPF (e.g. a min or max) of an SPF of the form:
1543/// SPF2(SPF1(A, B), C)
1544Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner,
1545 SelectPatternFlavor SPF1,
1546 Value *A, Value *B,
1547 Instruction &Outer,
1548 SelectPatternFlavor SPF2, Value *C) {
1549 if (Outer.getType() != Inner->getType())
1550 return nullptr;
1551
1552 if (C == A || C == B) {
1553 // MAX(MAX(A, B), B) -> MAX(A, B)
1554 // MIN(MIN(a, b), a) -> MIN(a, b)
1555 // TODO: This could be done in instsimplify.
1556 if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1))
1557 return replaceInstUsesWith(Outer, Inner);
1558
1559 // MAX(MIN(a, b), a) -> a
1560 // MIN(MAX(a, b), a) -> a
1561 // TODO: This could be done in instsimplify.
1562 if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
1563 (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
1564 (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
1565 (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
1566 return replaceInstUsesWith(Outer, C);
1567 }
1568
1569 if (SPF1 == SPF2) {
1570 const APInt *CB, *CC;
1571 if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) {
1572 // MIN(MIN(A, 23), 97) -> MIN(A, 23)
1573 // MAX(MAX(A, 97), 23) -> MAX(A, 97)
1574 // TODO: This could be done in instsimplify.
1575 if ((SPF1 == SPF_UMIN && CB->ule(*CC)) ||
1576 (SPF1 == SPF_SMIN && CB->sle(*CC)) ||
1577 (SPF1 == SPF_UMAX && CB->uge(*CC)) ||
1578 (SPF1 == SPF_SMAX && CB->sge(*CC)))
1579 return replaceInstUsesWith(Outer, Inner);
1580
1581 // MIN(MIN(A, 97), 23) -> MIN(A, 23)
1582 // MAX(MAX(A, 23), 97) -> MAX(A, 97)
1583 if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) ||
1584 (SPF1 == SPF_SMIN && CB->sgt(*CC)) ||
1585 (SPF1 == SPF_UMAX && CB->ult(*CC)) ||
1586 (SPF1 == SPF_SMAX && CB->slt(*CC))) {
1587 Outer.replaceUsesOfWith(Inner, A);
1588 return &Outer;
1589 }
1590 }
1591 }
1592
1593 // max(max(A, B), min(A, B)) --> max(A, B)
1594 // min(min(A, B), max(A, B)) --> min(A, B)
1595 // TODO: This could be done in instsimplify.
1596 if (SPF1 == SPF2 &&
1597 ((SPF1 == SPF_UMIN && match(C, m_c_UMax(m_Specific(A), m_Specific(B)))) ||
1598 (SPF1 == SPF_SMIN && match(C, m_c_SMax(m_Specific(A), m_Specific(B)))) ||
1599 (SPF1 == SPF_UMAX && match(C, m_c_UMin(m_Specific(A), m_Specific(B)))) ||
1600 (SPF1 == SPF_SMAX && match(C, m_c_SMin(m_Specific(A), m_Specific(B))))))
1601 return replaceInstUsesWith(Outer, Inner);
1602
1603 // ABS(ABS(X)) -> ABS(X)
1604 // NABS(NABS(X)) -> NABS(X)
1605 // TODO: This could be done in instsimplify.
1606 if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) {
1607 return replaceInstUsesWith(Outer, Inner);
1608 }
1609
1610 // ABS(NABS(X)) -> ABS(X)
1611 // NABS(ABS(X)) -> NABS(X)
1612 if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) ||
1613 (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) {
1614 SelectInst *SI = cast<SelectInst>(Inner);
1615 Value *NewSI =
1616 Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(),
1617 SI->getTrueValue(), SI->getName(), SI);
1618 return replaceInstUsesWith(Outer, NewSI);
1619 }
1620
1621 auto IsFreeOrProfitableToInvert =
1622 [&](Value *V, Value *&NotV, bool &ElidesXor) {
1623 if (match(V, m_Not(m_Value(NotV)))) {
1624 // If V has at most 2 uses then we can get rid of the xor operation
1625 // entirely.
1626 ElidesXor |= !V->hasNUsesOrMore(3);
1627 return true;
1628 }
1629
1630 if (isFreeToInvert(V, !V->hasNUsesOrMore(3))) {
1631 NotV = nullptr;
1632 return true;
1633 }
1634
1635 return false;
1636 };
1637
1638 Value *NotA, *NotB, *NotC;
1639 bool ElidesXor = false;
1640
1641 // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C)
1642 // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C)
1643 // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C)
1644 // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C)
1645 //
1646 // This transform is performance neutral if we can elide at least one xor from
1647 // the set of three operands, since we'll be tacking on an xor at the very
1648 // end.
1649 if (SelectPatternResult::isMinOrMax(SPF1) &&
1650 SelectPatternResult::isMinOrMax(SPF2) &&
1651 IsFreeOrProfitableToInvert(A, NotA, ElidesXor) &&
1652 IsFreeOrProfitableToInvert(B, NotB, ElidesXor) &&
1653 IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) {
1654 if (!NotA)
1655 NotA = Builder.CreateNot(A);
1656 if (!NotB)
1657 NotB = Builder.CreateNot(B);
1658 if (!NotC)
1659 NotC = Builder.CreateNot(C);
1660
1661 Value *NewInner = createMinMax(Builder, getInverseMinMaxFlavor(SPF1), NotA,
1662 NotB);
1663 Value *NewOuter = Builder.CreateNot(
1664 createMinMax(Builder, getInverseMinMaxFlavor(SPF2), NewInner, NotC));
1665 return replaceInstUsesWith(Outer, NewOuter);
1666 }
1667
1668 return nullptr;
1669}
1670
1671/// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))).
1672/// This is even legal for FP.
1673static Instruction *foldAddSubSelect(SelectInst &SI,
1674 InstCombiner::BuilderTy &Builder) {
1675 Value *CondVal = SI.getCondition();
1676 Value *TrueVal = SI.getTrueValue();
1677 Value *FalseVal = SI.getFalseValue();
1678 auto *TI = dyn_cast<Instruction>(TrueVal);
1679 auto *FI = dyn_cast<Instruction>(FalseVal);
1680 if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse())
1681 return nullptr;
1682
1683 Instruction *AddOp = nullptr, *SubOp = nullptr;
1684 if ((TI->getOpcode() == Instruction::Sub &&
1685 FI->getOpcode() == Instruction::Add) ||
1686 (TI->getOpcode() == Instruction::FSub &&
1687 FI->getOpcode() == Instruction::FAdd)) {
1688 AddOp = FI;
1689 SubOp = TI;
1690 } else if ((FI->getOpcode() == Instruction::Sub &&
1691 TI->getOpcode() == Instruction::Add) ||
1692 (FI->getOpcode() == Instruction::FSub &&
1693 TI->getOpcode() == Instruction::FAdd)) {
1694 AddOp = TI;
1695 SubOp = FI;
1696 }
1697
1698 if (AddOp) {
1699 Value *OtherAddOp = nullptr;
1700 if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
1701 OtherAddOp = AddOp->getOperand(1);
1702 } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
1703 OtherAddOp = AddOp->getOperand(0);
1704 }
1705
1706 if (OtherAddOp) {
1707 // So at this point we know we have (Y -> OtherAddOp):
1708 // select C, (add X, Y), (sub X, Z)
1709 Value *NegVal; // Compute -Z
1710 if (SI.getType()->isFPOrFPVectorTy()) {
1711 NegVal = Builder.CreateFNeg(SubOp->getOperand(1));
1712 if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
1713 FastMathFlags Flags = AddOp->getFastMathFlags();
1714 Flags &= SubOp->getFastMathFlags();
1715 NegInst->setFastMathFlags(Flags);
1716 }
1717 } else {
1718 NegVal = Builder.CreateNeg(SubOp->getOperand(1));
1719 }
1720
1721 Value *NewTrueOp = OtherAddOp;
1722 Value *NewFalseOp = NegVal;
1723 if (AddOp != TI)
1724 std::swap(NewTrueOp, NewFalseOp);
1725 Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp,
1726 SI.getName() + ".p", &SI);
1727
1728 if (SI.getType()->isFPOrFPVectorTy()) {
1729 Instruction *RI =
1730 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
1731
1732 FastMathFlags Flags = AddOp->getFastMathFlags();
1733 Flags &= SubOp->getFastMathFlags();
1734 RI->setFastMathFlags(Flags);
1735 return RI;
1736 } else
1737 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
1738 }
1739 }
1740 return nullptr;
1741}
1742
1743/// Turn X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
1744/// And X - Y overflows ? 0 : X - Y -> usub_sat X, Y
1745/// Along with a number of patterns similar to:
1746/// X + Y overflows ? (X < 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1747/// X - Y overflows ? (X > 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1748static Instruction *
1749foldOverflowingAddSubSelect(SelectInst &SI, InstCombiner::BuilderTy &Builder) {
1750 Value *CondVal = SI.getCondition();
1751 Value *TrueVal = SI.getTrueValue();
1752 Value *FalseVal = SI.getFalseValue();
1753
1754 WithOverflowInst *II;
1755 if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))) ||
1756 !match(FalseVal, m_ExtractValue<0>(m_Specific(II))))
1757 return nullptr;
1758
1759 Value *X = II->getLHS();
1760 Value *Y = II->getRHS();
1761
1762 auto IsSignedSaturateLimit = [&](Value *Limit, bool IsAdd) {
1763 Type *Ty = Limit->getType();
1764
1765 ICmpInst::Predicate Pred;
1766 Value *TrueVal, *FalseVal, *Op;
1767 const APInt *C;
1768 if (!match(Limit, m_Select(m_ICmp(Pred, m_Value(Op), m_APInt(C)),
1769 m_Value(TrueVal), m_Value(FalseVal))))
1770 return false;
1771
1772 auto IsZeroOrOne = [](const APInt &C) {
1773 return C.isNullValue() || C.isOneValue();
1774 };
1775 auto IsMinMax = [&](Value *Min, Value *Max) {
1776 APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
1777 APInt MaxVal = APInt::getSignedMaxValue(Ty->getScalarSizeInBits());
1778 return match(Min, m_SpecificInt(MinVal)) &&
1779 match(Max, m_SpecificInt(MaxVal));
1780 };
1781
1782 if (Op != X && Op != Y)
1783 return false;
1784
1785 if (IsAdd) {
1786 // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1787 // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1788 // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1789 // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1790 if (Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
1791 IsMinMax(TrueVal, FalseVal))
1792 return true;
1793 // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1794 // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1795 // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1796 // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1797 if (Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
1798 IsMinMax(FalseVal, TrueVal))
1799 return true;
1800 } else {
1801 // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1802 // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1803 if (Op == X && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C + 1) &&
1804 IsMinMax(TrueVal, FalseVal))
1805 return true;
1806 // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1807 // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1808 if (Op == X && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 2) &&
1809 IsMinMax(FalseVal, TrueVal))
1810 return true;
1811 // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1812 // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1813 if (Op == Y && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
1814 IsMinMax(FalseVal, TrueVal))
1815 return true;
1816 // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1817 // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1818 if (Op == Y && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
1819 IsMinMax(TrueVal, FalseVal))
1820 return true;
1821 }
1822
1823 return false;
1824 };
1825
1826 Intrinsic::ID NewIntrinsicID;
1827 if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow &&
1828 match(TrueVal, m_AllOnes()))
1829 // X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
1830 NewIntrinsicID = Intrinsic::uadd_sat;
1831 else if (II->getIntrinsicID() == Intrinsic::usub_with_overflow &&
1832 match(TrueVal, m_Zero()))
1833 // X - Y overflows ? 0 : X - Y -> usub_sat X, Y
1834 NewIntrinsicID = Intrinsic::usub_sat;
1835 else if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow &&
1836 IsSignedSaturateLimit(TrueVal, /*IsAdd=*/true))
1837 // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1838 // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1839 // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1840 // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1841 // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1842 // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
1843 // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1844 // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
1845 NewIntrinsicID = Intrinsic::sadd_sat;
1846 else if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow &&
1847 IsSignedSaturateLimit(TrueVal, /*IsAdd=*/false))
1848 // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1849 // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1850 // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1851 // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1852 // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1853 // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
1854 // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1855 // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
1856 NewIntrinsicID = Intrinsic::ssub_sat;
1857 else
1858 return nullptr;
1859
1860 Function *F =
1861 Intrinsic::getDeclaration(SI.getModule(), NewIntrinsicID, SI.getType());
1862 return CallInst::Create(F, {X, Y});
1863}
1864
1865Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) {
1866 Constant *C;
1867 if (!match(Sel.getTrueValue(), m_Constant(C)) &&
1868 !match(Sel.getFalseValue(), m_Constant(C)))
1869 return nullptr;
1870
1871 Instruction *ExtInst;
1872 if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) &&
1873 !match(Sel.getFalseValue(), m_Instruction(ExtInst)))
1874 return nullptr;
1875
1876 auto ExtOpcode = ExtInst->getOpcode();
1877 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
1878 return nullptr;
1879
1880 // If we are extending from a boolean type or if we can create a select that
1881 // has the same size operands as its condition, try to narrow the select.
1882 Value *X = ExtInst->getOperand(0);
1883 Type *SmallType = X->getType();
1884 Value *Cond = Sel.getCondition();
1885 auto *Cmp = dyn_cast<CmpInst>(Cond);
1886 if (!SmallType->isIntOrIntVectorTy(1) &&
1887 (!Cmp || Cmp->getOperand(0)->getType() != SmallType))
1888 return nullptr;
1889
1890 // If the constant is the same after truncation to the smaller type and
1891 // extension to the original type, we can narrow the select.
1892 Type *SelType = Sel.getType();
1893 Constant *TruncC = ConstantExpr::getTrunc(C, SmallType);
1894 Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType);
1895 if (ExtC == C) {
1896 Value *TruncCVal = cast<Value>(TruncC);
1897 if (ExtInst == Sel.getFalseValue())
1898 std::swap(X, TruncCVal);
1899
1900 // select Cond, (ext X), C --> ext(select Cond, X, C')
1901 // select Cond, C, (ext X) --> ext(select Cond, C', X)
1902 Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel);
1903 return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType);
1904 }
1905
1906 // If one arm of the select is the extend of the condition, replace that arm
1907 // with the extension of the appropriate known bool value.
1908 if (Cond == X) {
1909 if (ExtInst == Sel.getTrueValue()) {
1910 // select X, (sext X), C --> select X, -1, C
1911 // select X, (zext X), C --> select X, 1, C
1912 Constant *One = ConstantInt::getTrue(SmallType);
1913 Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType);
1914 return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel);
1915 } else {
1916 // select X, C, (sext X) --> select X, C, 0
1917 // select X, C, (zext X) --> select X, C, 0
1918 Constant *Zero = ConstantInt::getNullValue(SelType);
1919 return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel);
1920 }
1921 }
1922
1923 return nullptr;
1924}
1925
1926/// Try to transform a vector select with a constant condition vector into a
1927/// shuffle for easier combining with other shuffles and insert/extract.
1928static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) {
1929 Value *CondVal = SI.getCondition();
1930 Constant *CondC;
1931 if (!CondVal->getType()->isVectorTy() || !match(CondVal, m_Constant(CondC)))
8
Taking true branch
1932 return nullptr;
9
Returning null pointer, which participates in a condition later
1933
1934 unsigned NumElts = CondVal->getType()->getVectorNumElements();
1935 SmallVector<Constant *, 16> Mask;
1936 Mask.reserve(NumElts);
1937 Type *Int32Ty = Type::getInt32Ty(CondVal->getContext());
1938 for (unsigned i = 0; i != NumElts; ++i) {
1939 Constant *Elt = CondC->getAggregateElement(i);
1940 if (!Elt)
1941 return nullptr;
1942
1943 if (Elt->isOneValue()) {
1944 // If the select condition element is true, choose from the 1st vector.
1945 Mask.push_back(ConstantInt::get(Int32Ty, i));
1946 } else if (Elt->isNullValue()) {
1947 // If the select condition element is false, choose from the 2nd vector.
1948 Mask.push_back(ConstantInt::get(Int32Ty, i + NumElts));
1949 } else if (isa<UndefValue>(Elt)) {
1950 // Undef in a select condition (choose one of the operands) does not mean
1951 // the same thing as undef in a shuffle mask (any value is acceptable), so
1952 // give up.
1953 return nullptr;
1954 } else {
1955 // Bail out on a constant expression.
1956 return nullptr;
1957 }
1958 }
1959
1960 return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(),
1961 ConstantVector::get(Mask));
1962}
1963
1964/// If we have a select of vectors with a scalar condition, try to convert that
1965/// to a vector select by splatting the condition. A splat may get folded with
1966/// other operations in IR and having all operands of a select be vector types
1967/// is likely better for vector codegen.
1968static Instruction *canonicalizeScalarSelectOfVecs(
1969 SelectInst &Sel, InstCombiner::BuilderTy &Builder) {
1970 Type *Ty = Sel.getType();
1971 if (!Ty->isVectorTy())
13
Calling 'Type::isVectorTy'
16
Returning from 'Type::isVectorTy'
17
Taking true branch
1972 return nullptr;
18
Returning null pointer, which participates in a condition later
1973
1974 // We can replace a single-use extract with constant index.
1975 Value *Cond = Sel.getCondition();
1976 if (!match(Cond, m_OneUse(m_ExtractElement(m_Value(), m_ConstantInt()))))
1977 return nullptr;
1978
1979 // select (extelt V, Index), T, F --> select (splat V, Index), T, F
1980 // Splatting the extracted condition reduces code (we could directly create a
1981 // splat shuffle of the source vector to eliminate the intermediate step).
1982 unsigned NumElts = Ty->getVectorNumElements();
1983 Value *SplatCond = Builder.CreateVectorSplat(NumElts, Cond);
1984 Sel.setCondition(SplatCond);
1985 return &Sel;
1986}
1987
1988/// Reuse bitcasted operands between a compare and select:
1989/// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
1990/// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D))
1991static Instruction *foldSelectCmpBitcasts(SelectInst &Sel,
1992 InstCombiner::BuilderTy &Builder) {
1993 Value *Cond = Sel.getCondition();
1994 Value *TVal = Sel.getTrueValue();
1995 Value *FVal = Sel.getFalseValue();
1996
1997 CmpInst::Predicate Pred;
1998 Value *A, *B;
1999 if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B))))
2000 return nullptr;
2001
2002 // The select condition is a compare instruction. If the select's true/false
2003 // values are already the same as the compare operands, there's nothing to do.
2004 if (TVal == A || TVal == B || FVal == A || FVal == B)
2005 return nullptr;
2006
2007 Value *C, *D;
2008 if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D))))
2009 return nullptr;
2010
2011 // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc)
2012 Value *TSrc, *FSrc;
2013 if (!match(TVal, m_BitCast(m_Value(TSrc))) ||
2014 !match(FVal, m_BitCast(m_Value(FSrc))))
2015 return nullptr;
2016
2017 // If the select true/false values are *different bitcasts* of the same source
2018 // operands, make the select operands the same as the compare operands and
2019 // cast the result. This is the canonical select form for min/max.
2020 Value *NewSel;
2021 if (TSrc == C && FSrc == D) {
2022 // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
2023 // bitcast (select (cmp A, B), A, B)
2024 NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel);
2025 } else if (TSrc == D && FSrc == C) {
2026 // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) -->
2027 // bitcast (select (cmp A, B), B, A)
2028 NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel);
2029 } else {
2030 return nullptr;
2031 }
2032 return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType());
2033}
2034
2035/// Try to eliminate select instructions that test the returned flag of cmpxchg
2036/// instructions.
2037///
2038/// If a select instruction tests the returned flag of a cmpxchg instruction and
2039/// selects between the returned value of the cmpxchg instruction its compare
2040/// operand, the result of the select will always be equal to its false value.
2041/// For example:
2042///
2043/// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
2044/// %1 = extractvalue { i64, i1 } %0, 1
2045/// %2 = extractvalue { i64, i1 } %0, 0
2046/// %3 = select i1 %1, i64 %compare, i64 %2
2047/// ret i64 %3
2048///
2049/// The returned value of the cmpxchg instruction (%2) is the original value
2050/// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2
2051/// must have been equal to %compare. Thus, the result of the select is always
2052/// equal to %2, and the code can be simplified to:
2053///
2054/// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
2055/// %1 = extractvalue { i64, i1 } %0, 0
2056/// ret i64 %1
2057///
2058static Instruction *foldSelectCmpXchg(SelectInst &SI) {
2059 // A helper that determines if V is an extractvalue instruction whose
2060 // aggregate operand is a cmpxchg instruction and whose single index is equal
2061 // to I. If such conditions are true, the helper returns the cmpxchg
2062 // instruction; otherwise, a nullptr is returned.
2063 auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * {
2064 auto *Extract = dyn_cast<ExtractValueInst>(V);
2065 if (!Extract)
2066 return nullptr;
2067 if (Extract->getIndices()[0] != I)
2068 return nullptr;
2069 return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand());
2070 };
2071
2072 // If the select has a single user, and this user is a select instruction that
2073 // we can simplify, skip the cmpxchg simplification for now.
2074 if (SI.hasOneUse())
2075 if (auto *Select = dyn_cast<SelectInst>(SI.user_back()))
2076 if (Select->getCondition() == SI.getCondition())
2077 if (Select->getFalseValue() == SI.getTrueValue() ||
2078 Select->getTrueValue() == SI.getFalseValue())
2079 return nullptr;
2080
2081 // Ensure the select condition is the returned flag of a cmpxchg instruction.
2082 auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1);
2083 if (!CmpXchg)
2084 return nullptr;
2085
2086 // Check the true value case: The true value of the select is the returned
2087 // value of the same cmpxchg used by the condition, and the false value is the
2088 // cmpxchg instruction's compare operand.
2089 if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0))
2090 if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue()) {
2091 SI.setTrueValue(SI.getFalseValue());
2092 return &SI;
2093 }
2094
2095 // Check the false value case: The false value of the select is the returned
2096 // value of the same cmpxchg used by the condition, and the true value is the
2097 // cmpxchg instruction's compare operand.
2098 if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0))
2099 if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue()) {
2100 SI.setTrueValue(SI.getFalseValue());
2101 return &SI;
2102 }
2103
2104 return nullptr;
2105}
2106
2107static Instruction *moveAddAfterMinMax(SelectPatternFlavor SPF, Value *X,
2108 Value *Y,
2109 InstCombiner::BuilderTy &Builder) {
2110 assert(SelectPatternResult::isMinOrMax(SPF) && "Expected min/max pattern")((SelectPatternResult::isMinOrMax(SPF) && "Expected min/max pattern"
) ? static_cast<void> (0) : __assert_fail ("SelectPatternResult::isMinOrMax(SPF) && \"Expected min/max pattern\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 2110, __PRETTY_FUNCTION__))
;
2111 bool IsUnsigned = SPF == SelectPatternFlavor::SPF_UMIN ||
2112 SPF == SelectPatternFlavor::SPF_UMAX;
2113 // TODO: If InstSimplify could fold all cases where C2 <= C1, we could change
2114 // the constant value check to an assert.
2115 Value *A;
2116 const APInt *C1, *C2;
2117 if (IsUnsigned && match(X, m_NUWAdd(m_Value(A), m_APInt(C1))) &&
2118 match(Y, m_APInt(C2)) && C2->uge(*C1) && X->hasNUses(2)) {
2119 // umin (add nuw A, C1), C2 --> add nuw (umin A, C2 - C1), C1
2120 // umax (add nuw A, C1), C2 --> add nuw (umax A, C2 - C1), C1
2121 Value *NewMinMax = createMinMax(Builder, SPF, A,
2122 ConstantInt::get(X->getType(), *C2 - *C1));
2123 return BinaryOperator::CreateNUW(BinaryOperator::Add, NewMinMax,
2124 ConstantInt::get(X->getType(), *C1));
2125 }
2126
2127 if (!IsUnsigned && match(X, m_NSWAdd(m_Value(A), m_APInt(C1))) &&
2128 match(Y, m_APInt(C2)) && X->hasNUses(2)) {
2129 bool Overflow;
2130 APInt Diff = C2->ssub_ov(*C1, Overflow);
2131 if (!Overflow) {
2132 // smin (add nsw A, C1), C2 --> add nsw (smin A, C2 - C1), C1
2133 // smax (add nsw A, C1), C2 --> add nsw (smax A, C2 - C1), C1
2134 Value *NewMinMax = createMinMax(Builder, SPF, A,
2135 ConstantInt::get(X->getType(), Diff));
2136 return BinaryOperator::CreateNSW(BinaryOperator::Add, NewMinMax,
2137 ConstantInt::get(X->getType(), *C1));
2138 }
2139 }
2140
2141 return nullptr;
2142}
2143
2144/// Match a sadd_sat or ssub_sat which is using min/max to clamp the value.
2145Instruction *InstCombiner::matchSAddSubSat(SelectInst &MinMax1) {
2146 Type *Ty = MinMax1.getType();
2147
2148 // We are looking for a tree of:
2149 // max(INT_MIN, min(INT_MAX, add(sext(A), sext(B))))
2150 // Where the min and max could be reversed
2151 Instruction *MinMax2;
2152 BinaryOperator *AddSub;
2153 const APInt *MinValue, *MaxValue;
2154 if (match(&MinMax1, m_SMin(m_Instruction(MinMax2), m_APInt(MaxValue)))) {
2155 if (!match(MinMax2, m_SMax(m_BinOp(AddSub), m_APInt(MinValue))))
2156 return nullptr;
2157 } else if (match(&MinMax1,
2158 m_SMax(m_Instruction(MinMax2), m_APInt(MinValue)))) {
2159 if (!match(MinMax2, m_SMin(m_BinOp(AddSub), m_APInt(MaxValue))))
2160 return nullptr;
2161 } else
2162 return nullptr;
2163
2164 // Check that the constants clamp a saturate, and that the new type would be
2165 // sensible to convert to.
2166 if (!(*MaxValue + 1).isPowerOf2() || -*MinValue != *MaxValue + 1)
2167 return nullptr;
2168 // In what bitwidth can this be treated as saturating arithmetics?
2169 unsigned NewBitWidth = (*MaxValue + 1).logBase2() + 1;
2170 // FIXME: This isn't quite right for vectors, but using the scalar type is a
2171 // good first approximation for what should be done there.
2172 if (!shouldChangeType(Ty->getScalarType()->getIntegerBitWidth(), NewBitWidth))
2173 return nullptr;
2174
2175 // Also make sure that the number of uses is as expected. The "3"s are for the
2176 // the two items of min/max (the compare and the select).
2177 if (MinMax2->hasNUsesOrMore(3) || AddSub->hasNUsesOrMore(3))
2178 return nullptr;
2179
2180 // Create the new type (which can be a vector type)
2181 Type *NewTy = Ty->getWithNewBitWidth(NewBitWidth);
2182 // Match the two extends from the add/sub
2183 Value *A, *B;
2184 if(!match(AddSub, m_BinOp(m_SExt(m_Value(A)), m_SExt(m_Value(B)))))
2185 return nullptr;
2186 // And check the incoming values are of a type smaller than or equal to the
2187 // size of the saturation. Otherwise the higher bits can cause different
2188 // results.
2189 if (A->getType()->getScalarSizeInBits() > NewBitWidth ||
2190 B->getType()->getScalarSizeInBits() > NewBitWidth)
2191 return nullptr;
2192
2193 Intrinsic::ID IntrinsicID;
2194 if (AddSub->getOpcode() == Instruction::Add)
2195 IntrinsicID = Intrinsic::sadd_sat;
2196 else if (AddSub->getOpcode() == Instruction::Sub)
2197 IntrinsicID = Intrinsic::ssub_sat;
2198 else
2199 return nullptr;
2200
2201 // Finally create and return the sat intrinsic, truncated to the new type
2202 Function *F = Intrinsic::getDeclaration(MinMax1.getModule(), IntrinsicID, NewTy);
2203 Value *AT = Builder.CreateSExt(A, NewTy);
2204 Value *BT = Builder.CreateSExt(B, NewTy);
2205 Value *Sat = Builder.CreateCall(F, {AT, BT});
2206 return CastInst::Create(Instruction::SExt, Sat, Ty);
2207}
2208
2209/// Reduce a sequence of min/max with a common operand.
2210static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS,
2211 Value *RHS,
2212 InstCombiner::BuilderTy &Builder) {
2213 assert(SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max")((SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max"
) ? static_cast<void> (0) : __assert_fail ("SelectPatternResult::isMinOrMax(SPF) && \"Expected a min/max\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp"
, 2213, __PRETTY_FUNCTION__))
;
2214 // TODO: Allow FP min/max with nnan/nsz.
2215 if (!LHS->getType()->isIntOrIntVectorTy())
2216 return nullptr;
2217
2218 // Match 3 of the same min/max ops. Example: umin(umin(), umin()).
2219 Value *A, *B, *C, *D;
2220 SelectPatternResult L = matchSelectPattern(LHS, A, B);
2221 SelectPatternResult R = matchSelectPattern(RHS, C, D);
2222 if (SPF != L.Flavor || L.Flavor != R.Flavor)
2223 return nullptr;
2224
2225 // Look for a common operand. The use checks are different than usual because
2226 // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by
2227 // the select.
2228 Value *MinMaxOp = nullptr;
2229 Value *ThirdOp = nullptr;
2230 if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) {
2231 // If the LHS is only used in this chain and the RHS is used outside of it,
2232 // reuse the RHS min/max because that will eliminate the LHS.
2233 if (D == A || C == A) {
2234 // min(min(a, b), min(c, a)) --> min(min(c, a), b)
2235 // min(min(a, b), min(a, d)) --> min(min(a, d), b)
2236 MinMaxOp = RHS;
2237 ThirdOp = B;
2238 } else if (D == B || C == B) {
2239 // min(min(a, b), min(c, b)) --> min(min(c, b), a)
2240 // min(min(a, b), min(b, d)) --> min(min(b, d), a)
2241 MinMaxOp = RHS;
2242 ThirdOp = A;
2243 }
2244 } else if (!RHS->hasNUsesOrMore(3)) {
2245 // Reuse the LHS. This will eliminate the RHS.
2246 if (D == A || D == B) {
2247 // min(min(a, b), min(c, a)) --> min(min(a, b), c)
2248 // min(min(a, b), min(c, b)) --> min(min(a, b), c)
2249 MinMaxOp = LHS;
2250 ThirdOp = C;
2251 } else if (C == A || C == B) {
2252 // min(min(a, b), min(b, d)) --> min(min(a, b), d)
2253 // min(min(a, b), min(c, b)) --> min(min(a, b), d)
2254 MinMaxOp = LHS;
2255 ThirdOp = D;
2256 }
2257 }
2258 if (!MinMaxOp || !ThirdOp)
2259 return nullptr;
2260
2261 CmpInst::Predicate P = getMinMaxPred(SPF);
2262 Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp);
2263 return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp);
2264}
2265
2266/// Try to reduce a rotate pattern that includes a compare and select into a
2267/// funnel shift intrinsic. Example:
2268/// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b)))
2269/// --> call llvm.fshl.i32(a, a, b)
2270static Instruction *foldSelectRotate(SelectInst &Sel) {
2271 // The false value of the select must be a rotate of the true value.
2272 Value *Or0, *Or1;
2273 if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_Value(Or0), m_Value(Or1)))))
2274 return nullptr;
2275
2276 Value *TVal = Sel.getTrueValue();
2277 Value *SA0, *SA1;
2278 if (!match(Or0, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA0)))) ||
2279 !match(Or1, m_OneUse(m_LogicalShift(m_Specific(TVal), m_Value(SA1)))))
2280 return nullptr;
2281
2282 auto ShiftOpcode0 = cast<BinaryOperator>(Or0)->getOpcode();
2283 auto ShiftOpcode1 = cast<BinaryOperator>(Or1)->getOpcode();
2284 if (ShiftOpcode0 == ShiftOpcode1)
2285 return nullptr;
2286
2287 // We have one of these patterns so far:
2288 // select ?, TVal, (or (lshr TVal, SA0), (shl TVal, SA1))
2289 // select ?, TVal, (or (shl TVal, SA0), (lshr TVal, SA1))
2290 // This must be a power-of-2 rotate for a bitmasking transform to be valid.
2291 unsigned Width = Sel.getType()->getScalarSizeInBits();
2292 if (!isPowerOf2_32(Width))
2293 return nullptr;
2294
2295 // Check the shift amounts to see if they are an opposite pair.
2296 Value *ShAmt;
2297 if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0)))))
2298 ShAmt = SA0;
2299 else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1)))))
2300 ShAmt = SA1;
2301 else
2302 return nullptr;
2303
2304 // Finally, see if the select is filtering out a shift-by-zero.
2305 Value *Cond = Sel.getCondition();
2306 ICmpInst::Predicate Pred;
2307 if (!match(Cond, m_OneUse(m_ICmp(Pred, m_Specific(ShAmt), m_ZeroInt()))) ||
2308 Pred != ICmpInst::ICMP_EQ)
2309 return nullptr;
2310
2311 // This is a rotate that avoids shift-by-bitwidth UB in a suboptimal way.
2312 // Convert to funnel shift intrinsic.
2313 bool IsFshl = (ShAmt == SA0 && ShiftOpcode0 == BinaryOperator::Shl) ||
2314 (ShAmt == SA1 && ShiftOpcode1 == BinaryOperator::Shl);
2315 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
2316 Function *F = Intrinsic::getDeclaration(Sel.getModule(), IID, Sel.getType());
2317 return IntrinsicInst::Create(F, { TVal, TVal, ShAmt });
2318}
2319
2320Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
2321 Value *CondVal = SI.getCondition();
2322 Value *TrueVal = SI.getTrueValue();
2323 Value *FalseVal = SI.getFalseValue();
1
'FalseVal' initialized here
2324 Type *SelType = SI.getType();
2325
2326 // FIXME: Remove this workaround when freeze related patches are done.
2327 // For select with undef operand which feeds into an equality comparison,
2328 // don't simplify it so loop unswitch can know the equality comparison
2329 // may have an undef operand. This is a workaround for PR31652 caused by
2330 // descrepancy about branch on undef between LoopUnswitch and GVN.
2331 if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) {
2
Assuming 'TrueVal' is not a 'UndefValue'
3
Assuming 'FalseVal' is not a 'UndefValue'
4
Taking false branch
2332 if (llvm::any_of(SI.users(), [&](User *U) {
2333 ICmpInst *CI = dyn_cast<ICmpInst>(U);
2334 if (CI && CI->isEquality())
2335 return true;
2336 return false;
2337 })) {
2338 return nullptr;
2339 }
2340 }
2341
2342 if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal,
5
Assuming 'V' is null
6
Taking false branch
2343 SQ.getWithInstruction(&SI)))
2344 return replaceInstUsesWith(SI, V);
2345
2346 if (Instruction *I
10.1
'I' is null
10.1
'I' is null
10.1
'I' is null
10.1
'I' is null
10.1
'I' is null
= canonicalizeSelectToShuffle(SI))
7
Calling 'canonicalizeSelectToShuffle'
10
Returning from 'canonicalizeSelectToShuffle'
11
Taking false branch
2347 return I;
2348
2349 if (Instruction *I
19.1
'I' is null
19.1
'I' is null
19.1
'I' is null
19.1
'I' is null
19.1
'I' is null
= canonicalizeScalarSelectOfVecs(SI, Builder))
12
Calling 'canonicalizeScalarSelectOfVecs'
19
Returning from 'canonicalizeScalarSelectOfVecs'
20
Taking false branch
2350 return I;
2351
2352 // Canonicalize a one-use integer compare with a non-canonical predicate by
2353 // inverting the predicate and swapping the select operands. This matches a
2354 // compare canonicalization for conditional branches.
2355 // TODO: Should we do the same for FP compares?
2356 CmpInst::Predicate Pred;
2357 if (match(CondVal, m_OneUse(m_ICmp(Pred, m_Value(), m_Value()))) &&
21
Taking false branch
2358 !isCanonicalPredicate(Pred)) {
2359 // Swap true/false values and condition.
2360 CmpInst *Cond = cast<CmpInst>(CondVal);
2361 Cond->setPredicate(CmpInst::getInversePredicate(Pred));
2362 SI.setOperand(1, FalseVal);
2363 SI.setOperand(2, TrueVal);
2364 SI.swapProfMetadata();
2365 Worklist.Add(Cond);
2366 return &SI;
2367 }
2368
2369 if (SelType->isIntOrIntVectorTy(1) &&
22
Assuming the condition is false
2370 TrueVal->getType() == CondVal->getType()) {
2371 if (match(TrueVal, m_One())) {
2372 // Change: A = select B, true, C --> A = or B, C
2373 return BinaryOperator::CreateOr(CondVal, FalseVal);
2374 }
2375 if (match(TrueVal, m_Zero())) {
2376 // Change: A = select B, false, C --> A = and !B, C
2377 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2378 return BinaryOperator::CreateAnd(NotCond, FalseVal);
2379 }
2380 if (match(FalseVal, m_Zero())) {
2381 // Change: A = select B, C, false --> A = and B, C
2382 return BinaryOperator::CreateAnd(CondVal, TrueVal);
2383 }
2384 if (match(FalseVal, m_One())) {
2385 // Change: A = select B, C, true --> A = or !B, C
2386 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2387 return BinaryOperator::CreateOr(NotCond, TrueVal);
2388 }
2389
2390 // select a, a, b -> a | b
2391 // select a, b, a -> a & b
2392 if (CondVal == TrueVal)
2393 return BinaryOperator::CreateOr(CondVal, FalseVal);
2394 if (CondVal == FalseVal)
2395 return BinaryOperator::CreateAnd(CondVal, TrueVal);
2396
2397 // select a, ~a, b -> (~a) & b
2398 // select a, b, ~a -> (~a) | b
2399 if (match(TrueVal, m_Not(m_Specific(CondVal))))
2400 return BinaryOperator::CreateAnd(TrueVal, FalseVal);
2401 if (match(FalseVal, m_Not(m_Specific(CondVal))))
2402 return BinaryOperator::CreateOr(TrueVal, FalseVal);
2403 }
2404
2405 // Selecting between two integer or vector splat integer constants?
2406 //
2407 // Note that we don't handle a scalar select of vectors:
2408 // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0>
2409 // because that may need 3 instructions to splat the condition value:
2410 // extend, insertelement, shufflevector.
2411 if (SelType->isIntOrIntVectorTy() &&
23
Calling 'Type::isIntOrIntVectorTy'
29
Returning from 'Type::isIntOrIntVectorTy'
2412 CondVal->getType()->isVectorTy() == SelType->isVectorTy()) {
2413 // select C, 1, 0 -> zext C to int
2414 if (match(TrueVal, m_One()) && match(FalseVal, m_Zero()))
2415 return new ZExtInst(CondVal, SelType);
2416
2417 // select C, -1, 0 -> sext C to int
2418 if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero()))
2419 return new SExtInst(CondVal, SelType);
2420
2421 // select C, 0, 1 -> zext !C to int
2422 if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) {
2423 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2424 return new ZExtInst(NotCond, SelType);
2425 }
2426
2427 // select C, 0, -1 -> sext !C to int
2428 if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) {
2429 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
2430 return new SExtInst(NotCond, SelType);
2431 }
2432 }
2433
2434 // See if we are selecting two values based on a comparison of the two values.
2435 if (FCmpInst *FCI
30.1
'FCI' is non-null
30.1
'FCI' is non-null
30.1
'FCI' is non-null
30.1
'FCI' is non-null
30.1
'FCI' is non-null
= dyn_cast<FCmpInst>(CondVal)) {
30
Assuming 'CondVal' is a 'FCmpInst'
31
Taking true branch
2436 Value *Cmp0 = FCI->getOperand(0), *Cmp1 = FCI->getOperand(1);
2437 if ((Cmp0 == TrueVal && Cmp1 == FalseVal) ||
32
Assuming 'Cmp0' is equal to 'TrueVal'
33
Assuming 'Cmp1' is equal to 'FalseVal'
2438 (Cmp0 == FalseVal && Cmp1 == TrueVal)) {
2439 // Canonicalize to use ordered comparisons by swapping the select
2440 // operands.
2441 //
2442 // e.g.
2443 // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X
2444 if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) {
34
Calling 'Value::hasOneUse'
38
Returning from 'Value::hasOneUse'
39
Assuming the condition is true
40
Assuming the condition is true
41
Taking true branch
2445 FCmpInst::Predicate InvPred = FCI->getInversePredicate();
2446 IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2447 // FIXME: The FMF should propagate from the select, not the fcmp.
2448 Builder.setFastMathFlags(FCI->getFastMathFlags());
2449 Value *NewCond = Builder.CreateFCmp(InvPred, Cmp0, Cmp1,
2450 FCI->getName() + ".inv");
2451 Value *NewSel = Builder.CreateSelect(NewCond, FalseVal, TrueVal);
42
Passing null pointer value via 2nd parameter 'True'
43
Calling 'IRBuilder::CreateSelect'
2452 return replaceInstUsesWith(SI, NewSel);
2453 }
2454
2455 // NOTE: if we wanted to, this is where to detect MIN/MAX
2456 }
2457 }
2458
2459 // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
2460 // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work. We
2461 // also require nnan because we do not want to unintentionally change the
2462 // sign of a NaN value.
2463 // FIXME: These folds should test/propagate FMF from the select, not the
2464 // fsub or fneg.
2465 // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
2466 Instruction *FSub;
2467 if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
2468 match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
2469 match(TrueVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
2470 (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
2471 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FSub);
2472 return replaceInstUsesWith(SI, Fabs);
2473 }
2474 // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
2475 if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
2476 match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
2477 match(FalseVal, m_Instruction(FSub)) && FSub->hasNoNaNs() &&
2478 (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
2479 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FSub);
2480 return replaceInstUsesWith(SI, Fabs);
2481 }
2482 // With nnan and nsz:
2483 // (X < +/-0.0) ? -X : X --> fabs(X)
2484 // (X <= +/-0.0) ? -X : X --> fabs(X)
2485 Instruction *FNeg;
2486 if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
2487 match(TrueVal, m_FNeg(m_Specific(FalseVal))) &&
2488 match(TrueVal, m_Instruction(FNeg)) &&
2489 FNeg->hasNoNaNs() && FNeg->hasNoSignedZeros() &&
2490 (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
2491 Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
2492 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, FNeg);
2493 return replaceInstUsesWith(SI, Fabs);
2494 }
2495 // With nnan and nsz:
2496 // (X > +/-0.0) ? X : -X --> fabs(X)
2497 // (X >= +/-0.0) ? X : -X --> fabs(X)
2498 if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
2499 match(FalseVal, m_FNeg(m_Specific(TrueVal))) &&
2500 match(FalseVal, m_Instruction(FNeg)) &&
2501 FNeg->hasNoNaNs() && FNeg->hasNoSignedZeros() &&
2502 (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
2503 Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
2504 Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, FNeg);
2505 return replaceInstUsesWith(SI, Fabs);
2506 }
2507
2508 // See if we are selecting two values based on a comparison of the two values.
2509 if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
2510 if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
2511 return Result;
2512
2513 if (Instruction *Add = foldAddSubSelect(SI, Builder))
2514 return Add;
2515 if (Instruction *Add = foldOverflowingAddSubSelect(SI, Builder))
2516 return Add;
2517
2518 // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
2519 auto *TI = dyn_cast<Instruction>(TrueVal);
2520 auto *FI = dyn_cast<Instruction>(FalseVal);
2521 if (TI && FI && TI->getOpcode() == FI->getOpcode())
2522 if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
2523 return IV;
2524
2525 if (Instruction *I = foldSelectExtConst(SI))
2526 return I;
2527
2528 // See if we can fold the select into one of our operands.
2529 if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
2530 if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
2531 return FoldI;
2532
2533 Value *LHS, *RHS;
2534 Instruction::CastOps CastOp;
2535 SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp);
2536 auto SPF = SPR.Flavor;
2537 if (SPF) {
2538 Value *LHS2, *RHS2;
2539 if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor)
2540 if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2,
2541 RHS2, SI, SPF, RHS))
2542 return R;
2543 if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor)
2544 if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2,
2545 RHS2, SI, SPF, LHS))
2546 return R;
2547 // TODO.
2548 // ABS(-X) -> ABS(X)
2549 }
2550
2551 if (SelectPatternResult::isMinOrMax(SPF)) {
2552 // Canonicalize so that
2553 // - type casts are outside select patterns.
2554 // - float clamp is transformed to min/max pattern
2555
2556 bool IsCastNeeded = LHS->getType() != SelType;
2557 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
2558 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
2559 if (IsCastNeeded ||
2560 (LHS->getType()->isFPOrFPVectorTy() &&
2561 ((CmpLHS != LHS && CmpLHS != RHS) ||
2562 (CmpRHS != LHS && CmpRHS != RHS)))) {
2563 CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered);
2564
2565 Value *Cmp;
2566 if (CmpInst::isIntPredicate(MinMaxPred)) {
2567 Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
2568 } else {
2569 IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2570 auto FMF =
2571 cast<FPMathOperator>(SI.getCondition())->getFastMathFlags();
2572 Builder.setFastMathFlags(FMF);
2573 Cmp = Builder.CreateFCmp(MinMaxPred, LHS, RHS);
2574 }
2575
2576 Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
2577 if (!IsCastNeeded)
2578 return replaceInstUsesWith(SI, NewSI);
2579
2580 Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType);
2581 return replaceInstUsesWith(SI, NewCast);
2582 }
2583
2584 // MAX(~a, ~b) -> ~MIN(a, b)
2585 // MAX(~a, C) -> ~MIN(a, ~C)
2586 // MIN(~a, ~b) -> ~MAX(a, b)
2587 // MIN(~a, C) -> ~MAX(a, ~C)
2588 auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * {
2589 Value *A;
2590 if (match(X, m_Not(m_Value(A))) && !X->hasNUsesOrMore(3) &&
2591 !isFreeToInvert(A, A->hasOneUse()) &&
2592 // Passing false to only consider m_Not and constants.
2593 isFreeToInvert(Y, false)) {
2594 Value *B = Builder.CreateNot(Y);
2595 Value *NewMinMax = createMinMax(Builder, getInverseMinMaxFlavor(SPF),
2596 A, B);
2597 // Copy the profile metadata.
2598 if (MDNode *MD = SI.getMetadata(LLVMContext::MD_prof)) {
2599 cast<SelectInst>(NewMinMax)->setMetadata(LLVMContext::MD_prof, MD);
2600 // Swap the metadata if the operands are swapped.
2601 if (X == SI.getFalseValue() && Y == SI.getTrueValue())
2602 cast<SelectInst>(NewMinMax)->swapProfMetadata();
2603 }
2604
2605 return BinaryOperator::CreateNot(NewMinMax);
2606 }
2607
2608 return nullptr;
2609 };
2610
2611 if (Instruction *I = moveNotAfterMinMax(LHS, RHS))
2612 return I;
2613 if (Instruction *I = moveNotAfterMinMax(RHS, LHS))
2614 return I;
2615
2616 if (Instruction *I = moveAddAfterMinMax(SPF, LHS, RHS, Builder))
2617 return I;
2618
2619 if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder))
2620 return I;
2621 if (Instruction *I = matchSAddSubSat(SI))
2622 return I;
2623 }
2624 }
2625
2626 // Canonicalize select of FP values where NaN and -0.0 are not valid as
2627 // minnum/maxnum intrinsics.
2628 if (isa<FPMathOperator>(SI) && SI.hasNoNaNs() && SI.hasNoSignedZeros()) {
2629 Value *X, *Y;
2630 if (match(&SI, m_OrdFMax(m_Value(X), m_Value(Y))))
2631 return replaceInstUsesWith(
2632 SI, Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI));
2633
2634 if (match(&SI, m_OrdFMin(m_Value(X), m_Value(Y))))
2635 return replaceInstUsesWith(
2636 SI, Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI));
2637 }
2638
2639 // See if we can fold the select into a phi node if the condition is a select.
2640 if (auto *PN = dyn_cast<PHINode>(SI.getCondition()))
2641 // The true/false values have to be live in the PHI predecessor's blocks.
2642 if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
2643 canSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
2644 if (Instruction *NV = foldOpIntoPhi(SI, PN))
2645 return NV;
2646
2647 if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
2648 if (TrueSI->getCondition()->getType() == CondVal->getType()) {
2649 // select(C, select(C, a, b), c) -> select(C, a, c)
2650 if (TrueSI->getCondition() == CondVal) {
2651 if (SI.getTrueValue() == TrueSI->getTrueValue())
2652 return nullptr;
2653 SI.setOperand(1, TrueSI->getTrueValue());
2654 return &SI;
2655 }
2656 // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b)
2657 // We choose this as normal form to enable folding on the And and shortening
2658 // paths for the values (this helps GetUnderlyingObjects() for example).
2659 if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) {
2660 Value *And = Builder.CreateAnd(CondVal, TrueSI->getCondition());
2661 SI.setOperand(0, And);
2662 SI.setOperand(1, TrueSI->getTrueValue());
2663 return &SI;
2664 }
2665 }
2666 }
2667 if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
2668 if (FalseSI->getCondition()->getType() == CondVal->getType()) {
2669 // select(C, a, select(C, b, c)) -> select(C, a, c)
2670 if (FalseSI->getCondition() == CondVal) {
2671 if (SI.getFalseValue() == FalseSI->getFalseValue())
2672 return nullptr;
2673 SI.setOperand(2, FalseSI->getFalseValue());
2674 return &SI;
2675 }
2676 // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b)
2677 if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) {
2678 Value *Or = Builder.CreateOr(CondVal, FalseSI->getCondition());
2679 SI.setOperand(0, Or);
2680 SI.setOperand(2, FalseSI->getFalseValue());
2681 return &SI;
2682 }
2683 }
2684 }
2685
2686 auto canMergeSelectThroughBinop = [](BinaryOperator *BO) {
2687 // The select might be preventing a division by 0.
2688 switch (BO->getOpcode()) {
2689 default:
2690 return true;
2691 case Instruction::SRem:
2692 case Instruction::URem:
2693 case Instruction::SDiv:
2694 case Instruction::UDiv:
2695 return false;
2696 }
2697 };
2698
2699 // Try to simplify a binop sandwiched between 2 selects with the same
2700 // condition.
2701 // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
2702 BinaryOperator *TrueBO;
2703 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) &&
2704 canMergeSelectThroughBinop(TrueBO)) {
2705 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) {
2706 if (TrueBOSI->getCondition() == CondVal) {
2707 TrueBO->setOperand(0, TrueBOSI->getTrueValue());
2708 Worklist.Add(TrueBO);
2709 return &SI;
2710 }
2711 }
2712 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) {
2713 if (TrueBOSI->getCondition() == CondVal) {
2714 TrueBO->setOperand(1, TrueBOSI->getTrueValue());
2715 Worklist.Add(TrueBO);
2716 return &SI;
2717 }
2718 }
2719 }
2720
2721 // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W))
2722 BinaryOperator *FalseBO;
2723 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) &&
2724 canMergeSelectThroughBinop(FalseBO)) {
2725 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) {
2726 if (FalseBOSI->getCondition() == CondVal) {
2727 FalseBO->setOperand(0, FalseBOSI->getFalseValue());
2728 Worklist.Add(FalseBO);
2729 return &SI;
2730 }
2731 }
2732 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) {
2733 if (FalseBOSI->getCondition() == CondVal) {
2734 FalseBO->setOperand(1, FalseBOSI->getFalseValue());
2735 Worklist.Add(FalseBO);
2736 return &SI;
2737 }
2738 }
2739 }
2740
2741 Value *NotCond;
2742 if (match(CondVal, m_Not(m_Value(NotCond)))) {
2743 SI.setOperand(0, NotCond);
2744 SI.setOperand(1, FalseVal);
2745 SI.setOperand(2, TrueVal);
2746 SI.swapProfMetadata();
2747 return &SI;
2748 }
2749
2750 if (VectorType *VecTy = dyn_cast<VectorType>(SelType)) {
2751 unsigned VWidth = VecTy->getNumElements();
2752 APInt UndefElts(VWidth, 0);
2753 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
2754 if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
2755 if (V != &SI)
2756 return replaceInstUsesWith(SI, V);
2757 return &SI;
2758 }
2759 }
2760
2761 // If we can compute the condition, there's no need for a select.
2762 // Like the above fold, we are attempting to reduce compile-time cost by
2763 // putting this fold here with limitations rather than in InstSimplify.
2764 // The motivation for this call into value tracking is to take advantage of
2765 // the assumption cache, so make sure that is populated.
2766 if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) {
2767 KnownBits Known(1);
2768 computeKnownBits(CondVal, Known, 0, &SI);
2769 if (Known.One.isOneValue())
2770 return replaceInstUsesWith(SI, TrueVal);
2771 if (Known.Zero.isOneValue())
2772 return replaceInstUsesWith(SI, FalseVal);
2773 }
2774
2775 if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
2776 return BitCastSel;
2777
2778 // Simplify selects that test the returned flag of cmpxchg instructions.
2779 if (Instruction *Select = foldSelectCmpXchg(SI))
2780 return Select;
2781
2782 if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI))
2783 return Select;
2784
2785 if (Instruction *Rot = foldSelectRotate(SI))
2786 return Rot;
2787
2788 return nullptr;
2789}

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

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h

1//===- llvm/Value.h - Definition of the Value class -------------*- 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 declares the Value class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_IR_VALUE_H
14#define LLVM_IR_VALUE_H
15
16#include "llvm-c/Types.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/iterator_range.h"
19#include "llvm/IR/Use.h"
20#include "llvm/Support/Alignment.h"
21#include "llvm/Support/CBindingWrapping.h"
22#include "llvm/Support/Casting.h"
23#include <cassert>
24#include <iterator>
25#include <memory>
26
27namespace llvm {
28
29class APInt;
30class Argument;
31class BasicBlock;
32class Constant;
33class ConstantData;
34class ConstantAggregate;
35class DataLayout;
36class Function;
37class GlobalAlias;
38class GlobalIFunc;
39class GlobalIndirectSymbol;
40class GlobalObject;
41class GlobalValue;
42class GlobalVariable;
43class InlineAsm;
44class Instruction;
45class LLVMContext;
46class Module;
47class ModuleSlotTracker;
48class raw_ostream;
49template<typename ValueTy> class StringMapEntry;
50class StringRef;
51class Twine;
52class Type;
53class User;
54
55using ValueName = StringMapEntry<Value *>;
56
57//===----------------------------------------------------------------------===//
58// Value Class
59//===----------------------------------------------------------------------===//
60
61/// LLVM Value Representation
62///
63/// This is a very important LLVM class. It is the base class of all values
64/// computed by a program that may be used as operands to other values. Value is
65/// the super class of other important classes such as Instruction and Function.
66/// All Values have a Type. Type is not a subclass of Value. Some values can
67/// have a name and they belong to some Module. Setting the name on the Value
68/// automatically updates the module's symbol table.
69///
70/// Every value has a "use list" that keeps track of which other Values are
71/// using this Value. A Value can also have an arbitrary number of ValueHandle
72/// objects that watch it and listen to RAUW and Destroy events. See
73/// llvm/IR/ValueHandle.h for details.
74class Value {
75 // The least-significant bit of the first word of Value *must* be zero:
76 // http://www.llvm.org/docs/ProgrammersManual.html#the-waymarking-algorithm
77 Type *VTy;
78 Use *UseList;
79
80 friend class ValueAsMetadata; // Allow access to IsUsedByMD.
81 friend class ValueHandleBase;
82
83 const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
84 unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
85
86protected:
87 /// Hold subclass data that can be dropped.
88 ///
89 /// This member is similar to SubclassData, however it is for holding
90 /// information which may be used to aid optimization, but which may be
91 /// cleared to zero without affecting conservative interpretation.
92 unsigned char SubclassOptionalData : 7;
93
94private:
95 /// Hold arbitrary subclass data.
96 ///
97 /// This member is defined by this class, but is not used for anything.
98 /// Subclasses can use it to hold whatever state they find useful. This
99 /// field is initialized to zero by the ctor.
100 unsigned short SubclassData;
101
102protected:
103 /// The number of operands in the subclass.
104 ///
105 /// This member is defined by this class, but not used for anything.
106 /// Subclasses can use it to store their number of operands, if they have
107 /// any.
108 ///
109 /// This is stored here to save space in User on 64-bit hosts. Since most
110 /// instances of Value have operands, 32-bit hosts aren't significantly
111 /// affected.
112 ///
113 /// Note, this should *NOT* be used directly by any class other than User.
114 /// User uses this value to find the Use list.
115 enum : unsigned { NumUserOperandsBits = 28 };
116 unsigned NumUserOperands : NumUserOperandsBits;
117
118 // Use the same type as the bitfield above so that MSVC will pack them.
119 unsigned IsUsedByMD : 1;
120 unsigned HasName : 1;
121 unsigned HasHungOffUses : 1;
122 unsigned HasDescriptor : 1;
123
124private:
125 template <typename UseT> // UseT == 'Use' or 'const Use'
126 class use_iterator_impl
127 : public std::iterator<std::forward_iterator_tag, UseT *> {
128 friend class Value;
129
130 UseT *U;
131
132 explicit use_iterator_impl(UseT *u) : U(u) {}
133
134 public:
135 use_iterator_impl() : U() {}
136
137 bool operator==(const use_iterator_impl &x) const { return U == x.U; }
138 bool operator!=(const use_iterator_impl &x) const { return !operator==(x); }
139
140 use_iterator_impl &operator++() { // Preincrement
141 assert(U && "Cannot increment end iterator!")((U && "Cannot increment end iterator!") ? static_cast
<void> (0) : __assert_fail ("U && \"Cannot increment end iterator!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 141, __PRETTY_FUNCTION__))
;
142 U = U->getNext();
143 return *this;
144 }
145
146 use_iterator_impl operator++(int) { // Postincrement
147 auto tmp = *this;
148 ++*this;
149 return tmp;
150 }
151
152 UseT &operator*() const {
153 assert(U && "Cannot dereference end iterator!")((U && "Cannot dereference end iterator!") ? static_cast
<void> (0) : __assert_fail ("U && \"Cannot dereference end iterator!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 153, __PRETTY_FUNCTION__))
;
154 return *U;
155 }
156
157 UseT *operator->() const { return &operator*(); }
158
159 operator use_iterator_impl<const UseT>() const {
160 return use_iterator_impl<const UseT>(U);
161 }
162 };
163
164 template <typename UserTy> // UserTy == 'User' or 'const User'
165 class user_iterator_impl
166 : public std::iterator<std::forward_iterator_tag, UserTy *> {
167 use_iterator_impl<Use> UI;
168 explicit user_iterator_impl(Use *U) : UI(U) {}
169 friend class Value;
170
171 public:
172 user_iterator_impl() = default;
173
174 bool operator==(const user_iterator_impl &x) const { return UI == x.UI; }
175 bool operator!=(const user_iterator_impl &x) const { return !operator==(x); }
176
177 /// Returns true if this iterator is equal to user_end() on the value.
178 bool atEnd() const { return *this == user_iterator_impl(); }
179
180 user_iterator_impl &operator++() { // Preincrement
181 ++UI;
182 return *this;
183 }
184
185 user_iterator_impl operator++(int) { // Postincrement
186 auto tmp = *this;
187 ++*this;
188 return tmp;
189 }
190
191 // Retrieve a pointer to the current User.
192 UserTy *operator*() const {
193 return UI->getUser();
194 }
195
196 UserTy *operator->() const { return operator*(); }
197
198 operator user_iterator_impl<const UserTy>() const {
199 return user_iterator_impl<const UserTy>(*UI);
200 }
201
202 Use &getUse() const { return *UI; }
203 };
204
205protected:
206 Value(Type *Ty, unsigned scid);
207
208 /// Value's destructor should be virtual by design, but that would require
209 /// that Value and all of its subclasses have a vtable that effectively
210 /// duplicates the information in the value ID. As a size optimization, the
211 /// destructor has been protected, and the caller should manually call
212 /// deleteValue.
213 ~Value(); // Use deleteValue() to delete a generic Value.
214
215public:
216 Value(const Value &) = delete;
217 Value &operator=(const Value &) = delete;
218
219 /// Delete a pointer to a generic Value.
220 void deleteValue();
221
222 /// Support for debugging, callable in GDB: V->dump()
223 void dump() const;
224
225 /// Implement operator<< on Value.
226 /// @{
227 void print(raw_ostream &O, bool IsForDebug = false) const;
228 void print(raw_ostream &O, ModuleSlotTracker &MST,
229 bool IsForDebug = false) const;
230 /// @}
231
232 /// Print the name of this Value out to the specified raw_ostream.
233 ///
234 /// This is useful when you just want to print 'int %reg126', not the
235 /// instruction that generated it. If you specify a Module for context, then
236 /// even constanst get pretty-printed; for example, the type of a null
237 /// pointer is printed symbolically.
238 /// @{
239 void printAsOperand(raw_ostream &O, bool PrintType = true,
240 const Module *M = nullptr) const;
241 void printAsOperand(raw_ostream &O, bool PrintType,
242 ModuleSlotTracker &MST) const;
243 /// @}
244
245 /// All values are typed, get the type of this value.
246 Type *getType() const { return VTy; }
247
248 /// All values hold a context through their type.
249 LLVMContext &getContext() const;
250
251 // All values can potentially be named.
252 bool hasName() const { return HasName; }
253 ValueName *getValueName() const;
254 void setValueName(ValueName *VN);
255
256private:
257 void destroyValueName();
258 enum class ReplaceMetadataUses { No, Yes };
259 void doRAUW(Value *New, ReplaceMetadataUses);
260 void setNameImpl(const Twine &Name);
261
262public:
263 /// Return a constant reference to the value's name.
264 ///
265 /// This guaranteed to return the same reference as long as the value is not
266 /// modified. If the value has a name, this does a hashtable lookup, so it's
267 /// not free.
268 StringRef getName() const;
269
270 /// Change the name of the value.
271 ///
272 /// Choose a new unique name if the provided name is taken.
273 ///
274 /// \param Name The new name; or "" if the value's name should be removed.
275 void setName(const Twine &Name);
276
277 /// Transfer the name from V to this value.
278 ///
279 /// After taking V's name, sets V's name to empty.
280 ///
281 /// \note It is an error to call V->takeName(V).
282 void takeName(Value *V);
283
284 /// Change all uses of this to point to a new Value.
285 ///
286 /// Go through the uses list for this definition and make each use point to
287 /// "V" instead of "this". After this completes, 'this's use list is
288 /// guaranteed to be empty.
289 void replaceAllUsesWith(Value *V);
290
291 /// Change non-metadata uses of this to point to a new Value.
292 ///
293 /// Go through the uses list for this definition and make each use point to
294 /// "V" instead of "this". This function skips metadata entries in the list.
295 void replaceNonMetadataUsesWith(Value *V);
296
297 /// Go through the uses list for this definition and make each use point
298 /// to "V" if the callback ShouldReplace returns true for the given Use.
299 /// Unlike replaceAllUsesWith() this function does not support basic block
300 /// values or constant users.
301 void replaceUsesWithIf(Value *New,
302 llvm::function_ref<bool(Use &U)> ShouldReplace) {
303 assert(New && "Value::replaceUsesWithIf(<null>) is invalid!")((New && "Value::replaceUsesWithIf(<null>) is invalid!"
) ? static_cast<void> (0) : __assert_fail ("New && \"Value::replaceUsesWithIf(<null>) is invalid!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 303, __PRETTY_FUNCTION__))
;
304 assert(New->getType() == getType() &&((New->getType() == getType() && "replaceUses of value with new value of different type!"
) ? static_cast<void> (0) : __assert_fail ("New->getType() == getType() && \"replaceUses of value with new value of different type!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 305, __PRETTY_FUNCTION__))
305 "replaceUses of value with new value of different type!")((New->getType() == getType() && "replaceUses of value with new value of different type!"
) ? static_cast<void> (0) : __assert_fail ("New->getType() == getType() && \"replaceUses of value with new value of different type!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 305, __PRETTY_FUNCTION__))
;
306
307 for (use_iterator UI = use_begin(), E = use_end(); UI != E;) {
308 Use &U = *UI;
309 ++UI;
310 if (!ShouldReplace(U))
311 continue;
312 U.set(New);
313 }
314 }
315
316 /// replaceUsesOutsideBlock - Go through the uses list for this definition and
317 /// make each use point to "V" instead of "this" when the use is outside the
318 /// block. 'This's use list is expected to have at least one element.
319 /// Unlike replaceAllUsesWith() this function does not support basic block
320 /// values or constant users.
321 void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
322
323 //----------------------------------------------------------------------
324 // Methods for handling the chain of uses of this Value.
325 //
326 // Materializing a function can introduce new uses, so these methods come in
327 // two variants:
328 // The methods that start with materialized_ check the uses that are
329 // currently known given which functions are materialized. Be very careful
330 // when using them since you might not get all uses.
331 // The methods that don't start with materialized_ assert that modules is
332 // fully materialized.
333 void assertModuleIsMaterializedImpl() const;
334 // This indirection exists so we can keep assertModuleIsMaterializedImpl()
335 // around in release builds of Value.cpp to be linked with other code built
336 // in debug mode. But this avoids calling it in any of the release built code.
337 void assertModuleIsMaterialized() const {
338#ifndef NDEBUG
339 assertModuleIsMaterializedImpl();
340#endif
341 }
342
343 bool use_empty() const {
344 assertModuleIsMaterialized();
345 return UseList == nullptr;
346 }
347
348 bool materialized_use_empty() const {
349 return UseList == nullptr;
350 }
351
352 using use_iterator = use_iterator_impl<Use>;
353 using const_use_iterator = use_iterator_impl<const Use>;
354
355 use_iterator materialized_use_begin() { return use_iterator(UseList); }
356 const_use_iterator materialized_use_begin() const {
357 return const_use_iterator(UseList);
358 }
359 use_iterator use_begin() {
360 assertModuleIsMaterialized();
361 return materialized_use_begin();
362 }
363 const_use_iterator use_begin() const {
364 assertModuleIsMaterialized();
365 return materialized_use_begin();
366 }
367 use_iterator use_end() { return use_iterator(); }
368 const_use_iterator use_end() const { return const_use_iterator(); }
369 iterator_range<use_iterator> materialized_uses() {
370 return make_range(materialized_use_begin(), use_end());
371 }
372 iterator_range<const_use_iterator> materialized_uses() const {
373 return make_range(materialized_use_begin(), use_end());
374 }
375 iterator_range<use_iterator> uses() {
376 assertModuleIsMaterialized();
377 return materialized_uses();
378 }
379 iterator_range<const_use_iterator> uses() const {
380 assertModuleIsMaterialized();
381 return materialized_uses();
382 }
383
384 bool user_empty() const {
385 assertModuleIsMaterialized();
386 return UseList == nullptr;
387 }
388
389 using user_iterator = user_iterator_impl<User>;
390 using const_user_iterator = user_iterator_impl<const User>;
391
392 user_iterator materialized_user_begin() { return user_iterator(UseList); }
393 const_user_iterator materialized_user_begin() const {
394 return const_user_iterator(UseList);
395 }
396 user_iterator user_begin() {
397 assertModuleIsMaterialized();
398 return materialized_user_begin();
399 }
400 const_user_iterator user_begin() const {
401 assertModuleIsMaterialized();
402 return materialized_user_begin();
403 }
404 user_iterator user_end() { return user_iterator(); }
405 const_user_iterator user_end() const { return const_user_iterator(); }
406 User *user_back() {
407 assertModuleIsMaterialized();
408 return *materialized_user_begin();
409 }
410 const User *user_back() const {
411 assertModuleIsMaterialized();
412 return *materialized_user_begin();
413 }
414 iterator_range<user_iterator> materialized_users() {
415 return make_range(materialized_user_begin(), user_end());
416 }
417 iterator_range<const_user_iterator> materialized_users() const {
418 return make_range(materialized_user_begin(), user_end());
419 }
420 iterator_range<user_iterator> users() {
421 assertModuleIsMaterialized();
422 return materialized_users();
423 }
424 iterator_range<const_user_iterator> users() const {
425 assertModuleIsMaterialized();
426 return materialized_users();
427 }
428
429 /// Return true if there is exactly one user of this value.
430 ///
431 /// This is specialized because it is a common request and does not require
432 /// traversing the whole use list.
433 bool hasOneUse() const {
434 const_use_iterator I = use_begin(), E = use_end();
435 if (I == E) return false;
35
Assuming the condition is false
36
Taking false branch
436 return ++I == E;
37
Returning value, which participates in a condition later
437 }
438
439 /// Return true if this Value has exactly N users.
440 bool hasNUses(unsigned N) const;
441
442 /// Return true if this value has N users or more.
443 ///
444 /// This is logically equivalent to getNumUses() >= N.
445 bool hasNUsesOrMore(unsigned N) const;
446
447 /// Check if this value is used in the specified basic block.
448 bool isUsedInBasicBlock(const BasicBlock *BB) const;
449
450 /// This method computes the number of uses of this Value.
451 ///
452 /// This is a linear time operation. Use hasOneUse, hasNUses, or
453 /// hasNUsesOrMore to check for specific values.
454 unsigned getNumUses() const;
455
456 /// This method should only be used by the Use class.
457 void addUse(Use &U) { U.addToList(&UseList); }
458
459 /// Concrete subclass of this.
460 ///
461 /// An enumeration for keeping track of the concrete subclass of Value that
462 /// is actually instantiated. Values of this enumeration are kept in the
463 /// Value classes SubclassID field. They are used for concrete type
464 /// identification.
465 enum ValueTy {
466#define HANDLE_VALUE(Name) Name##Val,
467#include "llvm/IR/Value.def"
468
469 // Markers:
470#define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val,
471#include "llvm/IR/Value.def"
472 };
473
474 /// Return an ID for the concrete type of this object.
475 ///
476 /// This is used to implement the classof checks. This should not be used
477 /// for any other purpose, as the values may change as LLVM evolves. Also,
478 /// note that for instructions, the Instruction's opcode is added to
479 /// InstructionVal. So this means three things:
480 /// # there is no value with code InstructionVal (no opcode==0).
481 /// # there are more possible values for the value type than in ValueTy enum.
482 /// # the InstructionVal enumerator must be the highest valued enumerator in
483 /// the ValueTy enum.
484 unsigned getValueID() const {
485 return SubclassID;
486 }
487
488 /// Return the raw optional flags value contained in this value.
489 ///
490 /// This should only be used when testing two Values for equivalence.
491 unsigned getRawSubclassOptionalData() const {
492 return SubclassOptionalData;
493 }
494
495 /// Clear the optional flags contained in this value.
496 void clearSubclassOptionalData() {
497 SubclassOptionalData = 0;
498 }
499
500 /// Check the optional flags for equality.
501 bool hasSameSubclassOptionalData(const Value *V) const {
502 return SubclassOptionalData == V->SubclassOptionalData;
503 }
504
505 /// Return true if there is a value handle associated with this value.
506 bool hasValueHandle() const { return HasValueHandle; }
507
508 /// Return true if there is metadata referencing this value.
509 bool isUsedByMetadata() const { return IsUsedByMD; }
510
511 /// Return true if this value is a swifterror value.
512 ///
513 /// swifterror values can be either a function argument or an alloca with a
514 /// swifterror attribute.
515 bool isSwiftError() const;
516
517 /// Strip off pointer casts, all-zero GEPs and address space casts.
518 ///
519 /// Returns the original uncasted value. If this is called on a non-pointer
520 /// value, it returns 'this'.
521 const Value *stripPointerCasts() const;
522 Value *stripPointerCasts() {
523 return const_cast<Value *>(
524 static_cast<const Value *>(this)->stripPointerCasts());
525 }
526
527 /// Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
528 ///
529 /// Returns the original uncasted value. If this is called on a non-pointer
530 /// value, it returns 'this'.
531 const Value *stripPointerCastsAndAliases() const;
532 Value *stripPointerCastsAndAliases() {
533 return const_cast<Value *>(
534 static_cast<const Value *>(this)->stripPointerCastsAndAliases());
535 }
536
537 /// Strip off pointer casts, all-zero GEPs and address space casts
538 /// but ensures the representation of the result stays the same.
539 ///
540 /// Returns the original uncasted value with the same representation. If this
541 /// is called on a non-pointer value, it returns 'this'.
542 const Value *stripPointerCastsSameRepresentation() const;
543 Value *stripPointerCastsSameRepresentation() {
544 return const_cast<Value *>(static_cast<const Value *>(this)
545 ->stripPointerCastsSameRepresentation());
546 }
547
548 /// Strip off pointer casts, all-zero GEPs and invariant group info.
549 ///
550 /// Returns the original uncasted value. If this is called on a non-pointer
551 /// value, it returns 'this'. This function should be used only in
552 /// Alias analysis.
553 const Value *stripPointerCastsAndInvariantGroups() const;
554 Value *stripPointerCastsAndInvariantGroups() {
555 return const_cast<Value *>(static_cast<const Value *>(this)
556 ->stripPointerCastsAndInvariantGroups());
557 }
558
559 /// Strip off pointer casts and all-constant inbounds GEPs.
560 ///
561 /// Returns the original pointer value. If this is called on a non-pointer
562 /// value, it returns 'this'.
563 const Value *stripInBoundsConstantOffsets() const;
564 Value *stripInBoundsConstantOffsets() {
565 return const_cast<Value *>(
566 static_cast<const Value *>(this)->stripInBoundsConstantOffsets());
567 }
568
569 /// Accumulate the constant offset this value has compared to a base pointer.
570 /// Only 'getelementptr' instructions (GEPs) with constant indices are
571 /// accumulated but other instructions, e.g., casts, are stripped away as
572 /// well. The accumulated constant offset is added to \p Offset and the base
573 /// pointer is returned.
574 ///
575 /// The APInt \p Offset has to have a bit-width equal to the IntPtr type for
576 /// the address space of 'this' pointer value, e.g., use
577 /// DataLayout::getIndexTypeSizeInBits(Ty).
578 ///
579 /// If \p AllowNonInbounds is true, constant offsets in GEPs are stripped and
580 /// accumulated even if the GEP is not "inbounds".
581 ///
582 /// If this is called on a non-pointer value, it returns 'this' and the
583 /// \p Offset is not modified.
584 ///
585 /// Note that this function will never return a nullptr. It will also never
586 /// manipulate the \p Offset in a way that would not match the difference
587 /// between the underlying value and the returned one. Thus, if no constant
588 /// offset was found, the returned value is the underlying one and \p Offset
589 /// is unchanged.
590 const Value *stripAndAccumulateConstantOffsets(const DataLayout &DL,
591 APInt &Offset,
592 bool AllowNonInbounds) const;
593 Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset,
594 bool AllowNonInbounds) {
595 return const_cast<Value *>(
596 static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets(
597 DL, Offset, AllowNonInbounds));
598 }
599
600 /// This is a wrapper around stripAndAccumulateConstantOffsets with the
601 /// in-bounds requirement set to false.
602 const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
603 APInt &Offset) const {
604 return stripAndAccumulateConstantOffsets(DL, Offset,
605 /* AllowNonInbounds */ false);
606 }
607 Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
608 APInt &Offset) {
609 return stripAndAccumulateConstantOffsets(DL, Offset,
610 /* AllowNonInbounds */ false);
611 }
612
613 /// Strip off pointer casts and inbounds GEPs.
614 ///
615 /// Returns the original pointer value. If this is called on a non-pointer
616 /// value, it returns 'this'.
617 const Value *stripInBoundsOffsets() const;
618 Value *stripInBoundsOffsets() {
619 return const_cast<Value *>(
620 static_cast<const Value *>(this)->stripInBoundsOffsets());
621 }
622
623 /// Returns the number of bytes known to be dereferenceable for the
624 /// pointer value.
625 ///
626 /// If CanBeNull is set by this function the pointer can either be null or be
627 /// dereferenceable up to the returned number of bytes.
628 uint64_t getPointerDereferenceableBytes(const DataLayout &DL,
629 bool &CanBeNull) const;
630
631 /// Returns an alignment of the pointer value.
632 ///
633 /// Returns an alignment which is either specified explicitly, e.g. via
634 /// align attribute of a function argument, or guaranteed by DataLayout.
635 MaybeAlign getPointerAlignment(const DataLayout &DL) const;
636
637 /// Translate PHI node to its predecessor from the given basic block.
638 ///
639 /// If this value is a PHI node with CurBB as its parent, return the value in
640 /// the PHI node corresponding to PredBB. If not, return ourself. This is
641 /// useful if you want to know the value something has in a predecessor
642 /// block.
643 const Value *DoPHITranslation(const BasicBlock *CurBB,
644 const BasicBlock *PredBB) const;
645 Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) {
646 return const_cast<Value *>(
647 static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB));
648 }
649
650 /// The maximum alignment for instructions.
651 ///
652 /// This is the greatest alignment value supported by load, store, and alloca
653 /// instructions, and global values.
654 static const unsigned MaxAlignmentExponent = 29;
655 static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
656
657 /// Mutate the type of this Value to be of the specified type.
658 ///
659 /// Note that this is an extremely dangerous operation which can create
660 /// completely invalid IR very easily. It is strongly recommended that you
661 /// recreate IR objects with the right types instead of mutating them in
662 /// place.
663 void mutateType(Type *Ty) {
664 VTy = Ty;
665 }
666
667 /// Sort the use-list.
668 ///
669 /// Sorts the Value's use-list by Cmp using a stable mergesort. Cmp is
670 /// expected to compare two \a Use references.
671 template <class Compare> void sortUseList(Compare Cmp);
672
673 /// Reverse the use-list.
674 void reverseUseList();
675
676private:
677 /// Merge two lists together.
678 ///
679 /// Merges \c L and \c R using \c Cmp. To enable stable sorts, always pushes
680 /// "equal" items from L before items from R.
681 ///
682 /// \return the first element in the list.
683 ///
684 /// \note Completely ignores \a Use::Prev (doesn't read, doesn't update).
685 template <class Compare>
686 static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) {
687 Use *Merged;
688 Use **Next = &Merged;
689
690 while (true) {
691 if (!L) {
692 *Next = R;
693 break;
694 }
695 if (!R) {
696 *Next = L;
697 break;
698 }
699 if (Cmp(*R, *L)) {
700 *Next = R;
701 Next = &R->Next;
702 R = R->Next;
703 } else {
704 *Next = L;
705 Next = &L->Next;
706 L = L->Next;
707 }
708 }
709
710 return Merged;
711 }
712
713protected:
714 unsigned short getSubclassDataFromValue() const { return SubclassData; }
715 void setValueSubclassData(unsigned short D) { SubclassData = D; }
716};
717
718struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } };
719
720/// Use this instead of std::unique_ptr<Value> or std::unique_ptr<Instruction>.
721/// Those don't work because Value and Instruction's destructors are protected,
722/// aren't virtual, and won't destroy the complete object.
723using unique_value = std::unique_ptr<Value, ValueDeleter>;
724
725inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
726 V.print(OS);
727 return OS;
728}
729
730void Use::set(Value *V) {
731 if (Val) removeFromList();
732 Val = V;
733 if (V) V->addUse(*this);
734}
735
736Value *Use::operator=(Value *RHS) {
737 set(RHS);
738 return RHS;
739}
740
741const Use &Use::operator=(const Use &RHS) {
742 set(RHS.Val);
743 return *this;
744}
745
746template <class Compare> void Value::sortUseList(Compare Cmp) {
747 if (!UseList || !UseList->Next)
748 // No need to sort 0 or 1 uses.
749 return;
750
751 // Note: this function completely ignores Prev pointers until the end when
752 // they're fixed en masse.
753
754 // Create a binomial vector of sorted lists, visiting uses one at a time and
755 // merging lists as necessary.
756 const unsigned MaxSlots = 32;
757 Use *Slots[MaxSlots];
758
759 // Collect the first use, turning it into a single-item list.
760 Use *Next = UseList->Next;
761 UseList->Next = nullptr;
762 unsigned NumSlots = 1;
763 Slots[0] = UseList;
764
765 // Collect all but the last use.
766 while (Next->Next) {
767 Use *Current = Next;
768 Next = Current->Next;
769
770 // Turn Current into a single-item list.
771 Current->Next = nullptr;
772
773 // Save Current in the first available slot, merging on collisions.
774 unsigned I;
775 for (I = 0; I < NumSlots; ++I) {
776 if (!Slots[I])
777 break;
778
779 // Merge two lists, doubling the size of Current and emptying slot I.
780 //
781 // Since the uses in Slots[I] originally preceded those in Current, send
782 // Slots[I] in as the left parameter to maintain a stable sort.
783 Current = mergeUseLists(Slots[I], Current, Cmp);
784 Slots[I] = nullptr;
785 }
786 // Check if this is a new slot.
787 if (I == NumSlots) {
788 ++NumSlots;
789 assert(NumSlots <= MaxSlots && "Use list bigger than 2^32")((NumSlots <= MaxSlots && "Use list bigger than 2^32"
) ? static_cast<void> (0) : __assert_fail ("NumSlots <= MaxSlots && \"Use list bigger than 2^32\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 789, __PRETTY_FUNCTION__))
;
790 }
791
792 // Found an open slot.
793 Slots[I] = Current;
794 }
795
796 // Merge all the lists together.
797 assert(Next && "Expected one more Use")((Next && "Expected one more Use") ? static_cast<void
> (0) : __assert_fail ("Next && \"Expected one more Use\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 797, __PRETTY_FUNCTION__))
;
798 assert(!Next->Next && "Expected only one Use")((!Next->Next && "Expected only one Use") ? static_cast
<void> (0) : __assert_fail ("!Next->Next && \"Expected only one Use\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/Value.h"
, 798, __PRETTY_FUNCTION__))
;
799 UseList = Next;
800 for (unsigned I = 0; I < NumSlots; ++I)
801 if (Slots[I])
802 // Since the uses in Slots[I] originally preceded those in UseList, send
803 // Slots[I] in as the left parameter to maintain a stable sort.
804 UseList = mergeUseLists(Slots[I], UseList, Cmp);
805
806 // Fix the Prev pointers.
807 for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) {
808 I->setPrev(Prev);
809 Prev = &I->Next;
810 }
811}
812
813// isa - Provide some specializations of isa so that we don't have to include
814// the subtype header files to test to see if the value is a subclass...
815//
816template <> struct isa_impl<Constant, Value> {
817 static inline bool doit(const Value &Val) {
818 static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal");
819 return Val.getValueID() <= Value::ConstantLastVal;
820 }
821};
822
823template <> struct isa_impl<ConstantData, Value> {
824 static inline bool doit(const Value &Val) {
825 return Val.getValueID() >= Value::ConstantDataFirstVal &&
826 Val.getValueID() <= Value::ConstantDataLastVal;
827 }
828};
829
830template <> struct isa_impl<ConstantAggregate, Value> {
831 static inline bool doit(const Value &Val) {
832 return Val.getValueID() >= Value::ConstantAggregateFirstVal &&
833 Val.getValueID() <= Value::ConstantAggregateLastVal;
834 }
835};
836
837template <> struct isa_impl<Argument, Value> {
838 static inline bool doit (const Value &Val) {
839 return Val.getValueID() == Value::ArgumentVal;
840 }
841};
842
843template <> struct isa_impl<InlineAsm, Value> {
844 static inline bool doit(const Value &Val) {
845 return Val.getValueID() == Value::InlineAsmVal;
846 }
847};
848
849template <> struct isa_impl<Instruction, Value> {
850 static inline bool doit(const Value &Val) {
851 return Val.getValueID() >= Value::InstructionVal;
852 }
853};
854
855template <> struct isa_impl<BasicBlock, Value> {
856 static inline bool doit(const Value &Val) {
857 return Val.getValueID() == Value::BasicBlockVal;
858 }
859};
860
861template <> struct isa_impl<Function, Value> {
862 static inline bool doit(const Value &Val) {
863 return Val.getValueID() == Value::FunctionVal;
864 }
865};
866
867template <> struct isa_impl<GlobalVariable, Value> {
868 static inline bool doit(const Value &Val) {
869 return Val.getValueID() == Value::GlobalVariableVal;
870 }
871};
872
873template <> struct isa_impl<GlobalAlias, Value> {
874 static inline bool doit(const Value &Val) {
875 return Val.getValueID() == Value::GlobalAliasVal;
876 }
877};
878
879template <> struct isa_impl<GlobalIFunc, Value> {
880 static inline bool doit(const Value &Val) {
881 return Val.getValueID() == Value::GlobalIFuncVal;
882 }
883};
884
885template <> struct isa_impl<GlobalIndirectSymbol, Value> {
886 static inline bool doit(const Value &Val) {
887 return isa<GlobalAlias>(Val) || isa<GlobalIFunc>(Val);
888 }
889};
890
891template <> struct isa_impl<GlobalValue, Value> {
892 static inline bool doit(const Value &Val) {
893 return isa<GlobalObject>(Val) || isa<GlobalIndirectSymbol>(Val);
894 }
895};
896
897template <> struct isa_impl<GlobalObject, Value> {
898 static inline bool doit(const Value &Val) {
899 return isa<GlobalVariable>(Val) || isa<Function>(Val);
900 }
901};
902
903// Create wrappers for C Binding types (see CBindingWrapping.h).
904DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef)inline Value *unwrap(LLVMValueRef P) { return reinterpret_cast
<Value*>(P); } inline LLVMValueRef wrap(const Value *P)
{ return reinterpret_cast<LLVMValueRef>(const_cast<
Value*>(P)); } template<typename T> inline T *unwrap
(LLVMValueRef P) { return cast<T>(unwrap(P)); }
905
906// Specialized opaque value conversions.
907inline Value **unwrap(LLVMValueRef *Vals) {
908 return reinterpret_cast<Value**>(Vals);
909}
910
911template<typename T>
912inline T **unwrap(LLVMValueRef *Vals, unsigned Length) {
913#ifndef NDEBUG
914 for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I)
915 unwrap<T>(*I); // For side effect of calling assert on invalid usage.
916#endif
917 (void)Length;
918 return reinterpret_cast<T**>(Vals);
919}
920
921inline LLVMValueRef *wrap(const Value **Vals) {
922 return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals));
923}
924
925} // end namespace llvm
926
927#endif // LLVM_IR_VALUE_H

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h

1//===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- 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 defines the IRBuilder class, which is used as a convenient way
10// to create LLVM instructions with a consistent and simplified interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_IRBUILDER_H
15#define LLVM_IR_IRBUILDER_H
16
17#include "llvm-c/Types.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Constant.h"
24#include "llvm/IR/ConstantFolder.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DebugLoc.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Value.h"
40#include "llvm/IR/ValueHandle.h"
41#include "llvm/Support/AtomicOrdering.h"
42#include "llvm/Support/CBindingWrapping.h"
43#include "llvm/Support/Casting.h"
44#include <cassert>
45#include <cstddef>
46#include <cstdint>
47#include <functional>
48#include <utility>
49
50namespace llvm {
51
52class APInt;
53class MDNode;
54class Use;
55
56/// This provides the default implementation of the IRBuilder
57/// 'InsertHelper' method that is called whenever an instruction is created by
58/// IRBuilder and needs to be inserted.
59///
60/// By default, this inserts the instruction at the insertion point.
61class IRBuilderDefaultInserter {
62protected:
63 void InsertHelper(Instruction *I, const Twine &Name,
64 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
65 if (BB) BB->getInstList().insert(InsertPt, I);
66 I->setName(Name);
67 }
68};
69
70/// Provides an 'InsertHelper' that calls a user-provided callback after
71/// performing the default insertion.
72class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
73 std::function<void(Instruction *)> Callback;
74
75public:
76 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
77 : Callback(std::move(Callback)) {}
78
79protected:
80 void InsertHelper(Instruction *I, const Twine &Name,
81 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
82 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
83 Callback(I);
84 }
85};
86
87/// Common base class shared among various IRBuilders.
88class IRBuilderBase {
89 DebugLoc CurDbgLocation;
90
91protected:
92 BasicBlock *BB;
93 BasicBlock::iterator InsertPt;
94 LLVMContext &Context;
95
96 MDNode *DefaultFPMathTag;
97 FastMathFlags FMF;
98
99 bool IsFPConstrained;
100 fp::ExceptionBehavior DefaultConstrainedExcept;
101 fp::RoundingMode DefaultConstrainedRounding;
102
103 ArrayRef<OperandBundleDef> DefaultOperandBundles;
104
105public:
106 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
107 ArrayRef<OperandBundleDef> OpBundles = None)
108 : Context(context), DefaultFPMathTag(FPMathTag), IsFPConstrained(false),
109 DefaultConstrainedExcept(fp::ebStrict),
110 DefaultConstrainedRounding(fp::rmDynamic),
111 DefaultOperandBundles(OpBundles) {
112 ClearInsertionPoint();
113 }
114
115 //===--------------------------------------------------------------------===//
116 // Builder configuration methods
117 //===--------------------------------------------------------------------===//
118
119 /// Clear the insertion point: created instructions will not be
120 /// inserted into a block.
121 void ClearInsertionPoint() {
122 BB = nullptr;
123 InsertPt = BasicBlock::iterator();
124 }
125
126 BasicBlock *GetInsertBlock() const { return BB; }
127 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
128 LLVMContext &getContext() const { return Context; }
129
130 /// This specifies that created instructions should be appended to the
131 /// end of the specified block.
132 void SetInsertPoint(BasicBlock *TheBB) {
133 BB = TheBB;
134 InsertPt = BB->end();
135 }
136
137 /// This specifies that created instructions should be inserted before
138 /// the specified instruction.
139 void SetInsertPoint(Instruction *I) {
140 BB = I->getParent();
141 InsertPt = I->getIterator();
142 assert(InsertPt != BB->end() && "Can't read debug loc from end()")((InsertPt != BB->end() && "Can't read debug loc from end()"
) ? static_cast<void> (0) : __assert_fail ("InsertPt != BB->end() && \"Can't read debug loc from end()\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 142, __PRETTY_FUNCTION__))
;
143 SetCurrentDebugLocation(I->getDebugLoc());
144 }
145
146 /// This specifies that created instructions should be inserted at the
147 /// specified point.
148 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
149 BB = TheBB;
150 InsertPt = IP;
151 if (IP != TheBB->end())
152 SetCurrentDebugLocation(IP->getDebugLoc());
153 }
154
155 /// Set location information used by debugging information.
156 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
157
158 /// Get location information used by debugging information.
159 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
160
161 /// If this builder has a current debug location, set it on the
162 /// specified instruction.
163 void SetInstDebugLocation(Instruction *I) const {
164 if (CurDbgLocation)
165 I->setDebugLoc(CurDbgLocation);
166 }
167
168 /// Get the return type of the current function that we're emitting
169 /// into.
170 Type *getCurrentFunctionReturnType() const;
171
172 /// InsertPoint - A saved insertion point.
173 class InsertPoint {
174 BasicBlock *Block = nullptr;
175 BasicBlock::iterator Point;
176
177 public:
178 /// Creates a new insertion point which doesn't point to anything.
179 InsertPoint() = default;
180
181 /// Creates a new insertion point at the given location.
182 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
183 : Block(InsertBlock), Point(InsertPoint) {}
184
185 /// Returns true if this insert point is set.
186 bool isSet() const { return (Block != nullptr); }
187
188 BasicBlock *getBlock() const { return Block; }
189 BasicBlock::iterator getPoint() const { return Point; }
190 };
191
192 /// Returns the current insert point.
193 InsertPoint saveIP() const {
194 return InsertPoint(GetInsertBlock(), GetInsertPoint());
195 }
196
197 /// Returns the current insert point, clearing it in the process.
198 InsertPoint saveAndClearIP() {
199 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
200 ClearInsertionPoint();
201 return IP;
202 }
203
204 /// Sets the current insert point to a previously-saved location.
205 void restoreIP(InsertPoint IP) {
206 if (IP.isSet())
207 SetInsertPoint(IP.getBlock(), IP.getPoint());
208 else
209 ClearInsertionPoint();
210 }
211
212 /// Get the floating point math metadata being used.
213 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
214
215 /// Get the flags to be applied to created floating point ops
216 FastMathFlags getFastMathFlags() const { return FMF; }
217
218 /// Clear the fast-math flags.
219 void clearFastMathFlags() { FMF.clear(); }
220
221 /// Set the floating point math metadata to be used.
222 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
223
224 /// Set the fast-math flags to be used with generated fp-math operators
225 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
226
227 /// Enable/Disable use of constrained floating point math. When
228 /// enabled the CreateF<op>() calls instead create constrained
229 /// floating point intrinsic calls. Fast math flags are unaffected
230 /// by this setting.
231 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
232
233 /// Query for the use of constrained floating point math
234 bool getIsFPConstrained() { return IsFPConstrained; }
235
236 /// Set the exception handling to be used with constrained floating point
237 void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
238 DefaultConstrainedExcept = NewExcept;
239 }
240
241 /// Set the rounding mode handling to be used with constrained floating point
242 void setDefaultConstrainedRounding(fp::RoundingMode NewRounding) {
243 DefaultConstrainedRounding = NewRounding;
244 }
245
246 /// Get the exception handling used with constrained floating point
247 fp::ExceptionBehavior getDefaultConstrainedExcept() {
248 return DefaultConstrainedExcept;
249 }
250
251 /// Get the rounding mode handling used with constrained floating point
252 fp::RoundingMode getDefaultConstrainedRounding() {
253 return DefaultConstrainedRounding;
254 }
255
256 void setConstrainedFPFunctionAttr() {
257 assert(BB && "Must have a basic block to set any function attributes!")((BB && "Must have a basic block to set any function attributes!"
) ? static_cast<void> (0) : __assert_fail ("BB && \"Must have a basic block to set any function attributes!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 257, __PRETTY_FUNCTION__))
;
258
259 Function *F = BB->getParent();
260 if (!F->hasFnAttribute(Attribute::StrictFP)) {
261 F->addFnAttr(Attribute::StrictFP);
262 }
263 }
264
265 void setConstrainedFPCallAttr(CallInst *I) {
266 if (!I->hasFnAttr(Attribute::StrictFP))
267 I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP);
268 }
269
270 //===--------------------------------------------------------------------===//
271 // RAII helpers.
272 //===--------------------------------------------------------------------===//
273
274 // RAII object that stores the current insertion point and restores it
275 // when the object is destroyed. This includes the debug location.
276 class InsertPointGuard {
277 IRBuilderBase &Builder;
278 AssertingVH<BasicBlock> Block;
279 BasicBlock::iterator Point;
280 DebugLoc DbgLoc;
281
282 public:
283 InsertPointGuard(IRBuilderBase &B)
284 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
285 DbgLoc(B.getCurrentDebugLocation()) {}
286
287 InsertPointGuard(const InsertPointGuard &) = delete;
288 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
289
290 ~InsertPointGuard() {
291 Builder.restoreIP(InsertPoint(Block, Point));
292 Builder.SetCurrentDebugLocation(DbgLoc);
293 }
294 };
295
296 // RAII object that stores the current fast math settings and restores
297 // them when the object is destroyed.
298 class FastMathFlagGuard {
299 IRBuilderBase &Builder;
300 FastMathFlags FMF;
301 MDNode *FPMathTag;
302
303 public:
304 FastMathFlagGuard(IRBuilderBase &B)
305 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
306
307 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
308 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
309
310 ~FastMathFlagGuard() {
311 Builder.FMF = FMF;
312 Builder.DefaultFPMathTag = FPMathTag;
313 }
314 };
315
316 //===--------------------------------------------------------------------===//
317 // Miscellaneous creation methods.
318 //===--------------------------------------------------------------------===//
319
320 /// Make a new global variable with initializer type i8*
321 ///
322 /// Make a new global variable with an initializer that has array of i8 type
323 /// filled in with the null terminated string value specified. The new global
324 /// variable will be marked mergable with any others of the same contents. If
325 /// Name is specified, it is the name of the global variable created.
326 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
327 unsigned AddressSpace = 0);
328
329 /// Get a constant value representing either true or false.
330 ConstantInt *getInt1(bool V) {
331 return ConstantInt::get(getInt1Ty(), V);
332 }
333
334 /// Get the constant value for i1 true.
335 ConstantInt *getTrue() {
336 return ConstantInt::getTrue(Context);
337 }
338
339 /// Get the constant value for i1 false.
340 ConstantInt *getFalse() {
341 return ConstantInt::getFalse(Context);
342 }
343
344 /// Get a constant 8-bit value.
345 ConstantInt *getInt8(uint8_t C) {
346 return ConstantInt::get(getInt8Ty(), C);
347 }
348
349 /// Get a constant 16-bit value.
350 ConstantInt *getInt16(uint16_t C) {
351 return ConstantInt::get(getInt16Ty(), C);
352 }
353
354 /// Get a constant 32-bit value.
355 ConstantInt *getInt32(uint32_t C) {
356 return ConstantInt::get(getInt32Ty(), C);
357 }
358
359 /// Get a constant 64-bit value.
360 ConstantInt *getInt64(uint64_t C) {
361 return ConstantInt::get(getInt64Ty(), C);
362 }
363
364 /// Get a constant N-bit value, zero extended or truncated from
365 /// a 64-bit value.
366 ConstantInt *getIntN(unsigned N, uint64_t C) {
367 return ConstantInt::get(getIntNTy(N), C);
368 }
369
370 /// Get a constant integer value.
371 ConstantInt *getInt(const APInt &AI) {
372 return ConstantInt::get(Context, AI);
373 }
374
375 //===--------------------------------------------------------------------===//
376 // Type creation methods
377 //===--------------------------------------------------------------------===//
378
379 /// Fetch the type representing a single bit
380 IntegerType *getInt1Ty() {
381 return Type::getInt1Ty(Context);
382 }
383
384 /// Fetch the type representing an 8-bit integer.
385 IntegerType *getInt8Ty() {
386 return Type::getInt8Ty(Context);
387 }
388
389 /// Fetch the type representing a 16-bit integer.
390 IntegerType *getInt16Ty() {
391 return Type::getInt16Ty(Context);
392 }
393
394 /// Fetch the type representing a 32-bit integer.
395 IntegerType *getInt32Ty() {
396 return Type::getInt32Ty(Context);
397 }
398
399 /// Fetch the type representing a 64-bit integer.
400 IntegerType *getInt64Ty() {
401 return Type::getInt64Ty(Context);
402 }
403
404 /// Fetch the type representing a 128-bit integer.
405 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
406
407 /// Fetch the type representing an N-bit integer.
408 IntegerType *getIntNTy(unsigned N) {
409 return Type::getIntNTy(Context, N);
410 }
411
412 /// Fetch the type representing a 16-bit floating point value.
413 Type *getHalfTy() {
414 return Type::getHalfTy(Context);
415 }
416
417 /// Fetch the type representing a 32-bit floating point value.
418 Type *getFloatTy() {
419 return Type::getFloatTy(Context);
420 }
421
422 /// Fetch the type representing a 64-bit floating point value.
423 Type *getDoubleTy() {
424 return Type::getDoubleTy(Context);
425 }
426
427 /// Fetch the type representing void.
428 Type *getVoidTy() {
429 return Type::getVoidTy(Context);
430 }
431
432 /// Fetch the type representing a pointer to an 8-bit integer value.
433 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
434 return Type::getInt8PtrTy(Context, AddrSpace);
435 }
436
437 /// Fetch the type representing a pointer to an integer value.
438 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
439 return DL.getIntPtrType(Context, AddrSpace);
440 }
441
442 //===--------------------------------------------------------------------===//
443 // Intrinsic creation methods
444 //===--------------------------------------------------------------------===//
445
446 /// Create and insert a memset to the specified pointer and the
447 /// specified value.
448 ///
449 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
450 /// specified, it will be added to the instruction. Likewise with alias.scope
451 /// and noalias tags.
452 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
453 MaybeAlign Align, bool isVolatile = false,
454 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
455 MDNode *NoAliasTag = nullptr) {
456 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
457 TBAATag, ScopeTag, NoAliasTag);
458 }
459
460 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
461 bool isVolatile = false, MDNode *TBAATag = nullptr,
462 MDNode *ScopeTag = nullptr,
463 MDNode *NoAliasTag = nullptr);
464
465 /// Create and insert an element unordered-atomic memset of the region of
466 /// memory starting at the given pointer to the given value.
467 ///
468 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
469 /// specified, it will be added to the instruction. Likewise with alias.scope
470 /// and noalias tags.
471 /// FIXME: Remove this function once transition to Align is over.
472 /// Use the version that takes Align instead of this one.
473 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
474 CallInst *CreateElementUnorderedAtomicMemSet(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
475 Value *Ptr, Value *Val, uint64_t Size, unsigned Alignment,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
476 uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
477 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
478 "Use the version that takes Align instead of this one")CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, uint64_t Size, unsigned Alignment, uint32_t ElementSize
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
{
479 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
480 Align(Alignment), ElementSize,
481 TBAATag, ScopeTag, NoAliasTag);
482 }
483
484 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
485 uint64_t Size, Align Alignment,
486 uint32_t ElementSize,
487 MDNode *TBAATag = nullptr,
488 MDNode *ScopeTag = nullptr,
489 MDNode *NoAliasTag = nullptr) {
490 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
491 Align(Alignment), ElementSize,
492 TBAATag, ScopeTag, NoAliasTag);
493 }
494
495 /// FIXME: Remove this function once transition to Align is over.
496 /// Use the version that takes Align instead of this one.
497 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, Value *Size, unsigned Alignment, uint32_t ElementSize,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
498 CallInst *CreateElementUnorderedAtomicMemSet(CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, Value *Size, unsigned Alignment, uint32_t ElementSize,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
499 Value *Ptr, Value *Val, Value *Size, unsigned Alignment,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, Value *Size, unsigned Alignment, uint32_t ElementSize,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
500 uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, Value *Size, unsigned Alignment, uint32_t ElementSize,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
501 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, Value *Size, unsigned Alignment, uint32_t ElementSize,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
502 "Use the version that takes Align instead of this one")CallInst *CreateElementUnorderedAtomicMemSet( Value *Ptr, Value
*Val, Value *Size, unsigned Alignment, uint32_t ElementSize,
MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes Align instead of this one"
)))
{
503 return CreateElementUnorderedAtomicMemSet(Ptr, Val, Size, Align(Alignment),
504 ElementSize, TBAATag, ScopeTag,
505 NoAliasTag);
506 }
507
508 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
509 Value *Size, Align Alignment,
510 uint32_t ElementSize,
511 MDNode *TBAATag = nullptr,
512 MDNode *ScopeTag = nullptr,
513 MDNode *NoAliasTag = nullptr);
514
515 /// Create and insert a memcpy between the specified pointers.
516 ///
517 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
518 /// specified, it will be added to the instruction. Likewise with alias.scope
519 /// and noalias tags.
520 /// FIXME: Remove this function once transition to Align is over.
521 /// Use the version that takes MaybeAlign instead of this one.
522 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
523 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
524 unsigned SrcAlign, uint64_t Size,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
525 bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
526 MDNode *TBAAStructTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
527 MDNode *ScopeTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
528 MDNode *NoAliasTag = nullptr),CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
529 "Use the version that takes MaybeAlign instead")CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
{
530 return CreateMemCpy(Dst, MaybeAlign(DstAlign), Src, MaybeAlign(SrcAlign),
531 getInt64(Size), isVolatile, TBAATag, TBAAStructTag,
532 ScopeTag, NoAliasTag);
533 }
534
535 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
536 MaybeAlign SrcAlign, uint64_t Size,
537 bool isVolatile = false, MDNode *TBAATag = nullptr,
538 MDNode *TBAAStructTag = nullptr,
539 MDNode *ScopeTag = nullptr,
540 MDNode *NoAliasTag = nullptr) {
541 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
542 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
543 NoAliasTag);
544 }
545
546 /// FIXME: Remove this function once transition to Align is over.
547 /// Use the version that takes MaybeAlign instead of this one.
548 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
549 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
550 unsigned SrcAlign, Value *Size,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
551 bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
552 MDNode *TBAAStructTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
553 MDNode *ScopeTag = nullptr,CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
554 MDNode *NoAliasTag = nullptr),CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
555 "Use the version that takes MaybeAlign instead")CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *
Src, unsigned SrcAlign, Value *Size, bool isVolatile = false,
MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode
*ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) __attribute__
((deprecated("Use the version that takes MaybeAlign instead")
))
;
556 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
557 MaybeAlign SrcAlign, Value *Size,
558 bool isVolatile = false, MDNode *TBAATag = nullptr,
559 MDNode *TBAAStructTag = nullptr,
560 MDNode *ScopeTag = nullptr,
561 MDNode *NoAliasTag = nullptr);
562
563 /// Create and insert an element unordered-atomic memcpy between the
564 /// specified pointers.
565 ///
566 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
567 ///
568 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
569 /// specified, it will be added to the instruction. Likewise with alias.scope
570 /// and noalias tags.
571 CallInst *CreateElementUnorderedAtomicMemCpy(
572 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
573 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
574 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
575 MDNode *NoAliasTag = nullptr) {
576 return CreateElementUnorderedAtomicMemCpy(
577 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
578 TBAAStructTag, ScopeTag, NoAliasTag);
579 }
580
581 CallInst *CreateElementUnorderedAtomicMemCpy(
582 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
583 uint32_t ElementSize, MDNode *TBAATag = nullptr,
584 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
585 MDNode *NoAliasTag = nullptr);
586
587 /// Create and insert a memmove between the specified
588 /// pointers.
589 ///
590 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
591 /// specified, it will be added to the instruction. Likewise with alias.scope
592 /// and noalias tags.
593 /// FIXME: Remove this function once transition to Align is over.
594 /// Use the version that takes MaybeAlign instead of this one.
595 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
596 CallInst *CreateMemMove(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
597 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
598 uint64_t Size, bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
599 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
600 "Use the version that takes MaybeAlign")CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, uint64_t Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
{
601 return CreateMemMove(Dst, MaybeAlign(DstAlign), Src, MaybeAlign(SrcAlign),
602 getInt64(Size), isVolatile, TBAATag, ScopeTag,
603 NoAliasTag);
604 }
605 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
606 MaybeAlign SrcAlign, uint64_t Size,
607 bool isVolatile = false, MDNode *TBAATag = nullptr,
608 MDNode *ScopeTag = nullptr,
609 MDNode *NoAliasTag = nullptr) {
610 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
611 isVolatile, TBAATag, ScopeTag, NoAliasTag);
612 }
613 /// FIXME: Remove this function once transition to Align is over.
614 /// Use the version that takes MaybeAlign instead of this one.
615 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, Value *Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
616 CallInst *CreateMemMove(CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, Value *Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
617 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, Value *Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
618 Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, Value *Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
619 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr),CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, Value *Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
620 "Use the version that takes MaybeAlign")CallInst *CreateMemMove( Value *Dst, unsigned DstAlign, Value
*Src, unsigned SrcAlign, Value *Size, bool isVolatile = false
, MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, MDNode
*NoAliasTag = nullptr) __attribute__((deprecated("Use the version that takes MaybeAlign"
)))
{
621 return CreateMemMove(Dst, MaybeAlign(DstAlign), Src, MaybeAlign(SrcAlign),
622 Size, isVolatile, TBAATag, ScopeTag, NoAliasTag);
623 }
624 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
625 MaybeAlign SrcAlign, Value *Size,
626 bool isVolatile = false, MDNode *TBAATag = nullptr,
627 MDNode *ScopeTag = nullptr,
628 MDNode *NoAliasTag = nullptr);
629
630 /// \brief Create and insert an element unordered-atomic memmove between the
631 /// specified pointers.
632 ///
633 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
634 /// respectively.
635 ///
636 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
637 /// specified, it will be added to the instruction. Likewise with alias.scope
638 /// and noalias tags.
639 CallInst *CreateElementUnorderedAtomicMemMove(
640 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
641 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
642 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
643 MDNode *NoAliasTag = nullptr) {
644 return CreateElementUnorderedAtomicMemMove(
645 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
646 TBAAStructTag, ScopeTag, NoAliasTag);
647 }
648
649 CallInst *CreateElementUnorderedAtomicMemMove(
650 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
651 uint32_t ElementSize, MDNode *TBAATag = nullptr,
652 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
653 MDNode *NoAliasTag = nullptr);
654
655 /// Create a vector fadd reduction intrinsic of the source vector.
656 /// The first parameter is a scalar accumulator value for ordered reductions.
657 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
658
659 /// Create a vector fmul reduction intrinsic of the source vector.
660 /// The first parameter is a scalar accumulator value for ordered reductions.
661 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
662
663 /// Create a vector int add reduction intrinsic of the source vector.
664 CallInst *CreateAddReduce(Value *Src);
665
666 /// Create a vector int mul reduction intrinsic of the source vector.
667 CallInst *CreateMulReduce(Value *Src);
668
669 /// Create a vector int AND reduction intrinsic of the source vector.
670 CallInst *CreateAndReduce(Value *Src);
671
672 /// Create a vector int OR reduction intrinsic of the source vector.
673 CallInst *CreateOrReduce(Value *Src);
674
675 /// Create a vector int XOR reduction intrinsic of the source vector.
676 CallInst *CreateXorReduce(Value *Src);
677
678 /// Create a vector integer max reduction intrinsic of the source
679 /// vector.
680 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
681
682 /// Create a vector integer min reduction intrinsic of the source
683 /// vector.
684 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
685
686 /// Create a vector float max reduction intrinsic of the source
687 /// vector.
688 CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
689
690 /// Create a vector float min reduction intrinsic of the source
691 /// vector.
692 CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
693
694 /// Create a lifetime.start intrinsic.
695 ///
696 /// If the pointer isn't i8* it will be converted.
697 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
698
699 /// Create a lifetime.end intrinsic.
700 ///
701 /// If the pointer isn't i8* it will be converted.
702 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
703
704 /// Create a call to invariant.start intrinsic.
705 ///
706 /// If the pointer isn't i8* it will be converted.
707 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
708
709 /// Create a call to Masked Load intrinsic
710 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
711 Value *PassThru = nullptr, const Twine &Name = "");
712
713 /// Create a call to Masked Store intrinsic
714 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
715 Value *Mask);
716
717 /// Create a call to Masked Gather intrinsic
718 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
719 Value *Mask = nullptr,
720 Value *PassThru = nullptr,
721 const Twine& Name = "");
722
723 /// Create a call to Masked Scatter intrinsic
724 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
725 Value *Mask = nullptr);
726
727 /// Create an assume intrinsic call that allows the optimizer to
728 /// assume that the provided condition will be true.
729 CallInst *CreateAssumption(Value *Cond);
730
731 /// Create a call to the experimental.gc.statepoint intrinsic to
732 /// start a new statepoint sequence.
733 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
734 Value *ActualCallee,
735 ArrayRef<Value *> CallArgs,
736 ArrayRef<Value *> DeoptArgs,
737 ArrayRef<Value *> GCArgs,
738 const Twine &Name = "");
739
740 /// Create a call to the experimental.gc.statepoint intrinsic to
741 /// start a new statepoint sequence.
742 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
743 Value *ActualCallee, uint32_t Flags,
744 ArrayRef<Use> CallArgs,
745 ArrayRef<Use> TransitionArgs,
746 ArrayRef<Use> DeoptArgs,
747 ArrayRef<Value *> GCArgs,
748 const Twine &Name = "");
749
750 /// Conveninence function for the common case when CallArgs are filled
751 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
752 /// .get()'ed to get the Value pointer.
753 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
754 Value *ActualCallee, ArrayRef<Use> CallArgs,
755 ArrayRef<Value *> DeoptArgs,
756 ArrayRef<Value *> GCArgs,
757 const Twine &Name = "");
758
759 /// Create an invoke to the experimental.gc.statepoint intrinsic to
760 /// start a new statepoint sequence.
761 InvokeInst *
762 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
763 Value *ActualInvokee, BasicBlock *NormalDest,
764 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
765 ArrayRef<Value *> DeoptArgs,
766 ArrayRef<Value *> GCArgs, const Twine &Name = "");
767
768 /// Create an invoke to the experimental.gc.statepoint intrinsic to
769 /// start a new statepoint sequence.
770 InvokeInst *CreateGCStatepointInvoke(
771 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
772 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
773 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
774 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
775 const Twine &Name = "");
776
777 // Convenience function for the common case when CallArgs are filled in using
778 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
779 // get the Value *.
780 InvokeInst *
781 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
782 Value *ActualInvokee, BasicBlock *NormalDest,
783 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
784 ArrayRef<Value *> DeoptArgs,
785 ArrayRef<Value *> GCArgs, const Twine &Name = "");
786
787 /// Create a call to the experimental.gc.result intrinsic to extract
788 /// the result from a call wrapped in a statepoint.
789 CallInst *CreateGCResult(Instruction *Statepoint,
790 Type *ResultType,
791 const Twine &Name = "");
792
793 /// Create a call to the experimental.gc.relocate intrinsics to
794 /// project the relocated value of one pointer from the statepoint.
795 CallInst *CreateGCRelocate(Instruction *Statepoint,
796 int BaseOffset,
797 int DerivedOffset,
798 Type *ResultType,
799 const Twine &Name = "");
800
801 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
802 /// type.
803 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
804 Instruction *FMFSource = nullptr,
805 const Twine &Name = "");
806
807 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
808 /// first type.
809 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
810 Instruction *FMFSource = nullptr,
811 const Twine &Name = "");
812
813 /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
814 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
815 /// the intrinsic.
816 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
817 ArrayRef<Value *> Args,
818 Instruction *FMFSource = nullptr,
819 const Twine &Name = "");
820
821 /// Create call to the minnum intrinsic.
822 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
823 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
824 }
825
826 /// Create call to the maxnum intrinsic.
827 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
828 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
829 }
830
831 /// Create call to the minimum intrinsic.
832 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
833 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
834 }
835
836 /// Create call to the maximum intrinsic.
837 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
838 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
839 }
840
841private:
842 /// Create a call to a masked intrinsic with given Id.
843 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
844 ArrayRef<Type *> OverloadedTypes,
845 const Twine &Name = "");
846
847 Value *getCastedInt8PtrValue(Value *Ptr);
848};
849
850/// This provides a uniform API for creating instructions and inserting
851/// them into a basic block: either at the end of a BasicBlock, or at a specific
852/// iterator location in a block.
853///
854/// Note that the builder does not expose the full generality of LLVM
855/// instructions. For access to extra instruction properties, use the mutators
856/// (e.g. setVolatile) on the instructions after they have been
857/// created. Convenience state exists to specify fast-math flags and fp-math
858/// tags.
859///
860/// The first template argument specifies a class to use for creating constants.
861/// This defaults to creating minimally folded constants. The second template
862/// argument allows clients to specify custom insertion hooks that are called on
863/// every newly created insertion.
864template <typename T = ConstantFolder,
865 typename Inserter = IRBuilderDefaultInserter>
866class IRBuilder : public IRBuilderBase, public Inserter {
867 T Folder;
868
869public:
870 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
871 MDNode *FPMathTag = nullptr,
872 ArrayRef<OperandBundleDef> OpBundles = None)
873 : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
874 Folder(F) {}
875
876 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
877 ArrayRef<OperandBundleDef> OpBundles = None)
878 : IRBuilderBase(C, FPMathTag, OpBundles) {}
879
880 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
881 ArrayRef<OperandBundleDef> OpBundles = None)
882 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
883 SetInsertPoint(TheBB);
884 }
885
886 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
887 ArrayRef<OperandBundleDef> OpBundles = None)
888 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
889 SetInsertPoint(TheBB);
890 }
891
892 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
893 ArrayRef<OperandBundleDef> OpBundles = None)
894 : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles) {
895 SetInsertPoint(IP);
896 }
897
898 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
899 MDNode *FPMathTag = nullptr,
900 ArrayRef<OperandBundleDef> OpBundles = None)
901 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
902 SetInsertPoint(TheBB, IP);
903 }
904
905 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
906 MDNode *FPMathTag = nullptr,
907 ArrayRef<OperandBundleDef> OpBundles = None)
908 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
909 SetInsertPoint(TheBB, IP);
910 }
911
912 /// Get the constant folder being used.
913 const T &getFolder() { return Folder; }
914
915 /// Insert and return the specified instruction.
916 template<typename InstTy>
917 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
918 this->InsertHelper(I, Name, BB, InsertPt);
919 this->SetInstDebugLocation(I);
920 return I;
921 }
922
923 /// No-op overload to handle constants.
924 Constant *Insert(Constant *C, const Twine& = "") const {
925 return C;
926 }
927
928 //===--------------------------------------------------------------------===//
929 // Instruction creation methods: Terminators
930 //===--------------------------------------------------------------------===//
931
932private:
933 /// Helper to add branch weight and unpredictable metadata onto an
934 /// instruction.
935 /// \returns The annotated instruction.
936 template <typename InstTy>
937 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
938 if (Weights)
939 I->setMetadata(LLVMContext::MD_prof, Weights);
940 if (Unpredictable)
941 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
942 return I;
943 }
944
945public:
946 /// Create a 'ret void' instruction.
947 ReturnInst *CreateRetVoid() {
948 return Insert(ReturnInst::Create(Context));
949 }
950
951 /// Create a 'ret <val>' instruction.
952 ReturnInst *CreateRet(Value *V) {
953 return Insert(ReturnInst::Create(Context, V));
954 }
955
956 /// Create a sequence of N insertvalue instructions,
957 /// with one Value from the retVals array each, that build a aggregate
958 /// return value one value at a time, and a ret instruction to return
959 /// the resulting aggregate value.
960 ///
961 /// This is a convenience function for code that uses aggregate return values
962 /// as a vehicle for having multiple return values.
963 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
964 Value *V = UndefValue::get(getCurrentFunctionReturnType());
965 for (unsigned i = 0; i != N; ++i)
966 V = CreateInsertValue(V, retVals[i], i, "mrv");
967 return Insert(ReturnInst::Create(Context, V));
968 }
969
970 /// Create an unconditional 'br label X' instruction.
971 BranchInst *CreateBr(BasicBlock *Dest) {
972 return Insert(BranchInst::Create(Dest));
973 }
974
975 /// Create a conditional 'br Cond, TrueDest, FalseDest'
976 /// instruction.
977 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
978 MDNode *BranchWeights = nullptr,
979 MDNode *Unpredictable = nullptr) {
980 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
981 BranchWeights, Unpredictable));
982 }
983
984 /// Create a conditional 'br Cond, TrueDest, FalseDest'
985 /// instruction. Copy branch meta data if available.
986 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
987 Instruction *MDSrc) {
988 BranchInst *Br = BranchInst::Create(True, False, Cond);
989 if (MDSrc) {
990 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
991 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
992 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
993 }
994 return Insert(Br);
995 }
996
997 /// Create a switch instruction with the specified value, default dest,
998 /// and with a hint for the number of cases that will be added (for efficient
999 /// allocation).
1000 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1001 MDNode *BranchWeights = nullptr,
1002 MDNode *Unpredictable = nullptr) {
1003 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1004 BranchWeights, Unpredictable));
1005 }
1006
1007 /// Create an indirect branch instruction with the specified address
1008 /// operand, with an optional hint for the number of destinations that will be
1009 /// added (for efficient allocation).
1010 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1011 return Insert(IndirectBrInst::Create(Addr, NumDests));
1012 }
1013
1014 /// Create an invoke instruction.
1015 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1016 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1017 ArrayRef<Value *> Args,
1018 ArrayRef<OperandBundleDef> OpBundles,
1019 const Twine &Name = "") {
1020 return Insert(
1021 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles),
1022 Name);
1023 }
1024 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1025 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1026 ArrayRef<Value *> Args = None,
1027 const Twine &Name = "") {
1028 return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args),
1029 Name);
1030 }
1031
1032 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1033 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1034 ArrayRef<OperandBundleDef> OpBundles,
1035 const Twine &Name = "") {
1036 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1037 NormalDest, UnwindDest, Args, OpBundles, Name);
1038 }
1039
1040 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1041 BasicBlock *UnwindDest,
1042 ArrayRef<Value *> Args = None,
1043 const Twine &Name = "") {
1044 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1045 NormalDest, UnwindDest, Args, Name);
1046 }
1047
1048 // Deprecated [opaque pointer types]
1049 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
1050 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1051 ArrayRef<OperandBundleDef> OpBundles,
1052 const Twine &Name = "") {
1053 return CreateInvoke(
1054 cast<FunctionType>(
1055 cast<PointerType>(Callee->getType())->getElementType()),
1056 Callee, NormalDest, UnwindDest, Args, OpBundles, Name);
1057 }
1058
1059 // Deprecated [opaque pointer types]
1060 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
1061 BasicBlock *UnwindDest,
1062 ArrayRef<Value *> Args = None,
1063 const Twine &Name = "") {
1064 return CreateInvoke(
1065 cast<FunctionType>(
1066 cast<PointerType>(Callee->getType())->getElementType()),
1067 Callee, NormalDest, UnwindDest, Args, Name);
1068 }
1069
1070 /// \brief Create a callbr instruction.
1071 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1072 BasicBlock *DefaultDest,
1073 ArrayRef<BasicBlock *> IndirectDests,
1074 ArrayRef<Value *> Args = None,
1075 const Twine &Name = "") {
1076 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1077 Args), Name);
1078 }
1079 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1080 BasicBlock *DefaultDest,
1081 ArrayRef<BasicBlock *> IndirectDests,
1082 ArrayRef<Value *> Args,
1083 ArrayRef<OperandBundleDef> OpBundles,
1084 const Twine &Name = "") {
1085 return Insert(
1086 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1087 OpBundles), Name);
1088 }
1089
1090 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1091 ArrayRef<BasicBlock *> IndirectDests,
1092 ArrayRef<Value *> Args = None,
1093 const Twine &Name = "") {
1094 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1095 DefaultDest, IndirectDests, Args, Name);
1096 }
1097 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1098 ArrayRef<BasicBlock *> IndirectDests,
1099 ArrayRef<Value *> Args,
1100 ArrayRef<OperandBundleDef> OpBundles,
1101 const Twine &Name = "") {
1102 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1103 DefaultDest, IndirectDests, Args, Name);
1104 }
1105
1106 ResumeInst *CreateResume(Value *Exn) {
1107 return Insert(ResumeInst::Create(Exn));
1108 }
1109
1110 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1111 BasicBlock *UnwindBB = nullptr) {
1112 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1113 }
1114
1115 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1116 unsigned NumHandlers,
1117 const Twine &Name = "") {
1118 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1119 Name);
1120 }
1121
1122 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1123 const Twine &Name = "") {
1124 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1125 }
1126
1127 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1128 ArrayRef<Value *> Args = None,
1129 const Twine &Name = "") {
1130 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1131 }
1132
1133 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1134 return Insert(CatchReturnInst::Create(CatchPad, BB));
1135 }
1136
1137 UnreachableInst *CreateUnreachable() {
1138 return Insert(new UnreachableInst(Context));
1139 }
1140
1141 //===--------------------------------------------------------------------===//
1142 // Instruction creation methods: Binary Operators
1143 //===--------------------------------------------------------------------===//
1144private:
1145 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1146 Value *LHS, Value *RHS,
1147 const Twine &Name,
1148 bool HasNUW, bool HasNSW) {
1149 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1150 if (HasNUW) BO->setHasNoUnsignedWrap();
1151 if (HasNSW) BO->setHasNoSignedWrap();
1152 return BO;
1153 }
1154
1155 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1156 FastMathFlags FMF) const {
1157 if (!FPMD)
1158 FPMD = DefaultFPMathTag;
1159 if (FPMD)
1160 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1161 I->setFastMathFlags(FMF);
1162 return I;
1163 }
1164
1165 Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
1166 Value *R, const Twine &Name) const {
1167 auto *LC = dyn_cast<Constant>(L);
1168 auto *RC = dyn_cast<Constant>(R);
1169 return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
1170 }
1171
1172 Value *getConstrainedFPRounding(Optional<fp::RoundingMode> Rounding) {
1173 fp::RoundingMode UseRounding = DefaultConstrainedRounding;
1174
1175 if (Rounding.hasValue())
1176 UseRounding = Rounding.getValue();
1177
1178 Optional<StringRef> RoundingStr = RoundingModeToStr(UseRounding);
1179 assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")((RoundingStr.hasValue() && "Garbage strict rounding mode!"
) ? static_cast<void> (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1179, __PRETTY_FUNCTION__))
;
1180 auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue());
1181
1182 return MetadataAsValue::get(Context, RoundingMDS);
1183 }
1184
1185 Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) {
1186 fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept;
1187
1188 if (Except.hasValue())
1189 UseExcept = Except.getValue();
1190
1191 Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(UseExcept);
1192 assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")((ExceptStr.hasValue() && "Garbage strict exception behavior!"
) ? static_cast<void> (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1192, __PRETTY_FUNCTION__))
;
1193 auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue());
1194
1195 return MetadataAsValue::get(Context, ExceptMDS);
1196 }
1197
1198 Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1199 assert(CmpInst::isFPPredicate(Predicate) &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1202, __PRETTY_FUNCTION__))
1200 Predicate != CmpInst::FCMP_FALSE &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1202, __PRETTY_FUNCTION__))
1201 Predicate != CmpInst::FCMP_TRUE &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1202, __PRETTY_FUNCTION__))
1202 "Invalid constrained FP comparison predicate!")((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1202, __PRETTY_FUNCTION__))
;
1203
1204 StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1205 auto *PredicateMDS = MDString::get(Context, PredicateStr);
1206
1207 return MetadataAsValue::get(Context, PredicateMDS);
1208 }
1209
1210public:
1211 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1212 bool HasNUW = false, bool HasNSW = false) {
1213 if (auto *LC = dyn_cast<Constant>(LHS))
1214 if (auto *RC = dyn_cast<Constant>(RHS))
1215 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
1216 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
1217 HasNUW, HasNSW);
1218 }
1219
1220 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1221 return CreateAdd(LHS, RHS, Name, false, true);
1222 }
1223
1224 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1225 return CreateAdd(LHS, RHS, Name, true, false);
1226 }
1227
1228 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1229 bool HasNUW = false, bool HasNSW = false) {
1230 if (auto *LC = dyn_cast<Constant>(LHS))
1231 if (auto *RC = dyn_cast<Constant>(RHS))
1232 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
1233 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
1234 HasNUW, HasNSW);
1235 }
1236
1237 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1238 return CreateSub(LHS, RHS, Name, false, true);
1239 }
1240
1241 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1242 return CreateSub(LHS, RHS, Name, true, false);
1243 }
1244
1245 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1246 bool HasNUW = false, bool HasNSW = false) {
1247 if (auto *LC = dyn_cast<Constant>(LHS))
1248 if (auto *RC = dyn_cast<Constant>(RHS))
1249 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1250 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1251 HasNUW, HasNSW);
1252 }
1253
1254 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1255 return CreateMul(LHS, RHS, Name, false, true);
1256 }
1257
1258 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1259 return CreateMul(LHS, RHS, Name, true, false);
1260 }
1261
1262 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1263 bool isExact = false) {
1264 if (auto *LC = dyn_cast<Constant>(LHS))
1265 if (auto *RC = dyn_cast<Constant>(RHS))
1266 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1267 if (!isExact)
1268 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1269 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1270 }
1271
1272 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1273 return CreateUDiv(LHS, RHS, Name, true);
1274 }
1275
1276 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1277 bool isExact = false) {
1278 if (auto *LC = dyn_cast<Constant>(LHS))
1279 if (auto *RC = dyn_cast<Constant>(RHS))
1280 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1281 if (!isExact)
1282 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1283 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1284 }
1285
1286 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1287 return CreateSDiv(LHS, RHS, Name, true);
1288 }
1289
1290 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1291 if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1292 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1293 }
1294
1295 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1296 if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1297 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1298 }
1299
1300 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1301 bool HasNUW = false, bool HasNSW = false) {
1302 if (auto *LC = dyn_cast<Constant>(LHS))
1303 if (auto *RC = dyn_cast<Constant>(RHS))
1304 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1305 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1306 HasNUW, HasNSW);
1307 }
1308
1309 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1310 bool HasNUW = false, bool HasNSW = false) {
1311 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1312 HasNUW, HasNSW);
1313 }
1314
1315 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1316 bool HasNUW = false, bool HasNSW = false) {
1317 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1318 HasNUW, HasNSW);
1319 }
1320
1321 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1322 bool isExact = false) {
1323 if (auto *LC = dyn_cast<Constant>(LHS))
1324 if (auto *RC = dyn_cast<Constant>(RHS))
1325 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1326 if (!isExact)
1327 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1328 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1329 }
1330
1331 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1332 bool isExact = false) {
1333 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1334 }
1335
1336 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1337 bool isExact = false) {
1338 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1339 }
1340
1341 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1342 bool isExact = false) {
1343 if (auto *LC = dyn_cast<Constant>(LHS))
1344 if (auto *RC = dyn_cast<Constant>(RHS))
1345 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1346 if (!isExact)
1347 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1348 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1349 }
1350
1351 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1352 bool isExact = false) {
1353 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1354 }
1355
1356 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1357 bool isExact = false) {
1358 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1359 }
1360
1361 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1362 if (auto *RC = dyn_cast<Constant>(RHS)) {
1363 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1364 return LHS; // LHS & -1 -> LHS
1365 if (auto *LC = dyn_cast<Constant>(LHS))
1366 return Insert(Folder.CreateAnd(LC, RC), Name);
1367 }
1368 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1369 }
1370
1371 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1372 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1373 }
1374
1375 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1376 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1377 }
1378
1379 Value *CreateAnd(ArrayRef<Value*> Ops) {
1380 assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail
("!Ops.empty()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1380, __PRETTY_FUNCTION__))
;
1381 Value *Accum = Ops[0];
1382 for (unsigned i = 1; i < Ops.size(); i++)
1383 Accum = CreateAnd(Accum, Ops[i]);
1384 return Accum;
1385 }
1386
1387 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1388 if (auto *RC = dyn_cast<Constant>(RHS)) {
1389 if (RC->isNullValue())
1390 return LHS; // LHS | 0 -> LHS
1391 if (auto *LC = dyn_cast<Constant>(LHS))
1392 return Insert(Folder.CreateOr(LC, RC), Name);
1393 }
1394 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1395 }
1396
1397 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1398 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1399 }
1400
1401 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1402 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1403 }
1404
1405 Value *CreateOr(ArrayRef<Value*> Ops) {
1406 assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail
("!Ops.empty()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/IRBuilder.h"
, 1406, __PRETTY_FUNCTION__))
;
1407 Value *Accum = Ops[0];
1408 for (unsigned i = 1; i < Ops.size(); i++)
1409 Accum = CreateOr(Accum, Ops[i]);
1410 return Accum;
1411 }
1412
1413 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1414 if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1415 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1416 }
1417
1418 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1419 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1420 }
1421
1422 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1423 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1424 }
1425
1426 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1427 MDNode *FPMD = nullptr) {
1428 if (IsFPConstrained)
1429 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1430 L, R, nullptr, Name, FPMD);
1431
1432 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1433 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1434 return Insert(I, Name);
1435 }
1436
1437 /// Copy fast-math-flags from an instruction rather than using the builder's
1438 /// default FMF.
1439 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1440 const Twine &Name = "") {
1441 if (IsFPConstrained)
1442 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1443 L, R, FMFSource, Name);
1444
1445 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1446 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1447 FMFSource->getFastMathFlags());
1448 return Insert(I, Name);
1449 }
1450
1451 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1452 MDNode *FPMD = nullptr) {
1453 if (IsFPConstrained)
1454 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1455 L, R, nullptr, Name, FPMD);
1456
1457 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1458 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1459 return Insert(I, Name);
1460 }
1461
1462 /// Copy fast-math-flags from an instruction rather than using the builder's
1463 /// default FMF.
1464 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1465 const Twine &Name = "") {
1466 if (IsFPConstrained)
1467 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1468 L, R, FMFSource, Name);
1469
1470 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1471 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1472 FMFSource->getFastMathFlags());
1473 return Insert(I, Name);
1474 }
1475
1476 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1477 MDNode *FPMD = nullptr) {
1478 if (IsFPConstrained)
1479 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1480 L, R, nullptr, Name, FPMD);
1481
1482 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1483 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1484 return Insert(I, Name);
1485 }
1486
1487 /// Copy fast-math-flags from an instruction rather than using the builder's
1488 /// default FMF.
1489 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1490 const Twine &Name = "") {
1491 if (IsFPConstrained)
1492 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1493 L, R, FMFSource, Name);
1494
1495 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1496 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1497 FMFSource->getFastMathFlags());
1498 return Insert(I, Name);
1499 }
1500
1501 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1502 MDNode *FPMD = nullptr) {
1503 if (IsFPConstrained)
1504 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1505 L, R, nullptr, Name, FPMD);
1506
1507 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1508 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1509 return Insert(I, Name);
1510 }
1511
1512 /// Copy fast-math-flags from an instruction rather than using the builder's
1513 /// default FMF.
1514 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1515 const Twine &Name = "") {
1516 if (IsFPConstrained)
1517 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1518 L, R, FMFSource, Name);
1519
1520 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1521 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1522 FMFSource->getFastMathFlags());
1523 return Insert(I, Name);
1524 }
1525
1526 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1527 MDNode *FPMD = nullptr) {
1528 if (IsFPConstrained)
1529 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1530 L, R, nullptr, Name, FPMD);
1531
1532 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1533 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1534 return Insert(I, Name);
1535 }
1536
1537 /// Copy fast-math-flags from an instruction rather than using the builder's
1538 /// default FMF.
1539 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1540 const Twine &Name = "") {
1541 if (IsFPConstrained)
1542 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1543 L, R, FMFSource, Name);
1544
1545 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1546 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1547 FMFSource->getFastMathFlags());
1548 return Insert(I, Name);
1549 }
1550
1551 Value *CreateBinOp(Instruction::BinaryOps Opc,
1552 Value *LHS, Value *RHS, const Twine &Name = "",
1553 MDNode *FPMathTag = nullptr) {
1554 if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1555 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1556 if (isa<FPMathOperator>(BinOp))
1557 setFPAttrs(BinOp, FPMathTag, FMF);
1558 return Insert(BinOp, Name);
1559 }
1560
1561 CallInst *CreateConstrainedFPBinOp(
1562 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1563 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1564 Optional<fp::RoundingMode> Rounding = None,
1565 Optional<fp::ExceptionBehavior> Except = None) {
1566 Value *RoundingV = getConstrainedFPRounding(Rounding);
1567 Value *ExceptV = getConstrainedFPExcept(Except);
1568
1569 FastMathFlags UseFMF = FMF;
1570 if (FMFSource)
1571 UseFMF = FMFSource->getFastMathFlags();
1572
1573 CallInst *C = CreateIntrinsic(ID, {L->getType()},
1574 {L, R, RoundingV, ExceptV}, nullptr, Name);
1575 setConstrainedFPCallAttr(C);
1576 setFPAttrs(C, FPMathTag, UseFMF);
1577 return C;
1578 }
1579
1580 Value *CreateNeg(Value *V, const Twine &Name = "",
1581 bool HasNUW = false,