LLVM 24.0.0git
ExpandIRInsts.cpp
Go to the documentation of this file.
1//===--- ExpandIRInsts.cpp - Expand IR instructions -----------------------===//
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// This pass expands certain instructions at the IR level.
9//
10// The following expansions are implemented:
11// - Expansion of ‘fptoui .. to’, ‘fptosi .. to’, ‘uitofp .. to’, ‘sitofp
12// .. to’ instructions with a bitwidth above a threshold. This is
13// useful for targets like x86_64 that cannot lower fp convertions
14// with more than 128 bits.
15//
16// - Expansion of ‘frem‘ for types MVT::f16, MVT::f32, and MVT::f64 for
17// targets which use "Expand" as the legalization action for the
18// corresponding type.
19//
20// - Expansion of ‘udiv‘, ‘sdiv‘, ‘urem‘, and ‘srem‘ instructions with
21// a bitwidth above a threshold into a call to auto-generated
22// functions. This is useful for targets like x86_64 that cannot
23// lower divisions with more than 128 bits or targets like x86_32 that
24// cannot lower divisions with more than 64 bits.
25//
26// Instructions with vector types are scalarized first if their scalar
27// types can be expanded. Scalable vector types are not supported.
28//===----------------------------------------------------------------------===//
29
37#include "llvm/CodeGen/Passes.h"
42#include "llvm/IR/IRBuilder.h"
44#include "llvm/IR/Instruction.h"
47#include "llvm/IR/MDBuilder.h"
48#include "llvm/IR/Module.h"
49#include "llvm/IR/PassManager.h"
52#include "llvm/Pass.h"
59#include <optional>
60
61#define DEBUG_TYPE "expand-ir-insts"
62
63using namespace llvm;
64
65namespace llvm {
67}
68
70 ExpandFpConvertBits("expand-fp-convert-bits", cl::Hidden,
72 cl::desc("fp convert instructions on integers with "
73 "more than <N> bits are expanded."));
74
76 ExpandDivRemBits("expand-div-rem-bits", cl::Hidden,
78 cl::desc("div and rem instructions on integers with "
79 "more than <N> bits are expanded."));
80
81static bool isConstantPowerOfTwo(Value *V, bool SignedOp) {
82 auto *C = dyn_cast<ConstantInt>(V);
83 if (!C)
84 return false;
85
86 APInt Val = C->getValue();
87 if (SignedOp && Val.isNegative())
88 Val = -Val;
89 return Val.isPowerOf2();
90}
91
92static bool isSigned(unsigned Opcode) {
93 return Opcode == Instruction::SDiv || Opcode == Instruction::SRem;
94}
95
96/// For signed div/rem by a power of 2, compute the bias-adjusted dividend:
97/// Sign = ashr X, (BitWidth - 1) -- 0 or -1
98/// Bias = lshr Sign, (BitWidth - ShiftAmt) -- 0 or 2^ShiftAmt - 1
99/// Adjusted = add X, Bias
100/// The bias adds (2^ShiftAmt - 1) for negative X, correcting rounding towards
101/// zero (instead of towards -inf that a plain ashr would give).
102/// The lshr form is used instead of 'and' to avoid large immediate constants.
103static Value *addSignedBias(IRBuilder<> &Builder, Value *X, unsigned BitWidth,
104 unsigned ShiftAmt) {
105 assert(ShiftAmt > 0 && ShiftAmt < BitWidth &&
106 "ShiftAmt out of range; callers should handle ShiftAmt == 0");
107 Value *Sign = Builder.CreateAShr(X, BitWidth - 1, "sign");
108 Value *Bias = Builder.CreateLShr(Sign, BitWidth - ShiftAmt, "bias");
109 return Builder.CreateAdd(X, Bias, "adjusted");
110}
111
112/// Expand division or remainder by a power-of-2 constant.
113/// Division (let C = log2(|divisor|)):
114/// udiv X, 2^C -> lshr X, C
115/// sdiv X, 2^C -> ashr (add X, Bias), C (Bias corrects rounding)
116/// sdiv exact X, 2^C -> ashr exact X, C (no bias needed)
117/// For negative power-of-2 divisors, the division result is negated.
118/// Remainder (let C = log2(|divisor|)):
119/// urem X, 2^C -> and X, (2^C - 1)
120/// srem X, 2^C -> sub X, (shl (ashr (add X, Bias), C), C)
122 LLVM_DEBUG(dbgs() << "Expanding instruction: " << *BO << '\n');
123
124 unsigned Opcode = BO->getOpcode();
125 bool IsDiv = (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv);
126 bool IsSigned = isSigned(Opcode);
127 // isExact() is only valid for div.
128 bool IsExact = IsDiv && BO->isExact();
129
130 assert(isConstantPowerOfTwo(BO->getOperand(1), IsSigned) &&
131 "Expected power-of-2 constant divisor");
132
133 Value *X = BO->getOperand(0);
134 auto *C = cast<ConstantInt>(BO->getOperand(1));
135 Type *Ty = BO->getType();
136 unsigned BitWidth = Ty->getIntegerBitWidth();
137
138 APInt DivisorVal = C->getValue();
139 bool IsNegativeDivisor = IsSigned && DivisorVal.isNegative();
140 // Use countr_zero() to get the shift amount directly from the bit pattern.
141 // This works correctly for both positive and negative powers of 2, including
142 // INT_MIN, without needing to negate the value first.
143 unsigned ShiftAmt = DivisorVal.countr_zero();
144
145 IRBuilder<> Builder(BO);
146 Value *Result;
147
148 if (ShiftAmt == 0) {
149 // Div by 1/-1: X / 1 = X, X / -1 = -X.
150 // Rem by 1/-1: always 0.
151 if (IsDiv)
152 Result = IsNegativeDivisor ? Builder.CreateNeg(X) : X;
153 else
154 Result = ConstantInt::get(Ty, 0);
155 } else if (IsSigned) {
156 // The signed expansion uses X multiple times (bias computation, shift,
157 // and sub for remainder). Freeze X to ensure consistent behavior if it is
158 // undef/poison. For exact division, no bias is needed and X is used only
159 // once, so freeze is unnecessary.
160 if (!IsExact && !isGuaranteedNotToBeUndefOrPoison(X))
161 X = Builder.CreateFreeze(X, X->getName() + ".fr");
162 // For exact division, no bias is needed since there's no rounding.
163 Value *Dividend =
164 IsExact ? X : addSignedBias(Builder, X, BitWidth, ShiftAmt);
165 Value *Quotient = Builder.CreateAShr(
166 Dividend, ShiftAmt, IsDiv && IsNegativeDivisor ? "pre.neg" : "shifted",
167 IsExact);
168 if (IsDiv) {
169 Result = IsNegativeDivisor ? Builder.CreateNeg(Quotient) : Quotient;
170 } else {
171 // Rem = X - (Quotient << ShiftAmt):
172 // clear lower ShiftAmt bits via round-trip shift, then subtract.
173 Value *Truncated = Builder.CreateShl(Quotient, ShiftAmt, "truncated");
174 Result = Builder.CreateSub(X, Truncated);
175 }
176 } else {
177 if (IsDiv) {
178 Result = Builder.CreateLShr(X, ShiftAmt, "", IsExact);
179 } else {
180 APInt Mask = APInt::getLowBitsSet(BitWidth, ShiftAmt);
181 Result = Builder.CreateAnd(X, ConstantInt::get(Ty, Mask));
182 }
183 }
184
185 BO->replaceAllUsesWith(Result);
186 if (Result != X)
187 if (auto *RI = dyn_cast<Instruction>(Result))
188 RI->takeName(BO);
189 BO->dropAllReferences();
190 BO->eraseFromParent();
191}
192
193/// This class implements a precise expansion of the frem instruction.
194/// The generated code is based on the fmod implementation in the AMD device
195/// libs.
196namespace {
197class FRemExpander {
198 /// The IRBuilder to use for the expansion.
199 IRBuilder<> &B;
200
201 /// Floating point type of the return value and the arguments of the FRem
202 /// instructions that should be expanded.
203 Type *FremTy;
204
205 /// Floating point type to use for the computation. This may be
206 /// wider than the \p FremTy.
207 Type *ComputeFpTy;
208
209 /// Integer type used to hold the exponents returned by frexp.
210 Type *ExTy;
211
212 /// How many bits of the quotient to compute per iteration of the
213 /// algorithm, stored as a value of type \p ExTy.
214 Value *Bits;
215
216 /// Constant 1 of type \p ExTy.
217 Value *One;
218
219 /// The frem argument/return types that can be expanded by this class.
220 // TODO: The expansion could work for other floating point types
221 // as well, but this would require additional testing.
222 static constexpr std::array<MVT, 3> ExpandableTypes{MVT::f16, MVT::f32,
223 MVT::f64};
224
225public:
226 static bool canExpandType(Type *Ty) {
227 EVT VT = EVT::getEVT(Ty);
228 assert(VT.isSimple() && "Can expand only simple types");
229
230 return is_contained(ExpandableTypes, VT.getSimpleVT());
231 }
232
233 static bool shouldExpandFremType(const TargetLowering &TLI,
234 const LibcallLoweringInfo &Libcalls,
235 EVT VT) {
236 assert(!VT.isVector() && "Cannot handle vector type; must scalarize first");
237 switch (TLI.getOperationAction(ISD::FREM, VT)) {
238 case TargetLowering::LegalizeAction::Expand:
239 return true;
240 case TargetLowering::LegalizeAction::LibCall:
241 // The target expects a libcall, but expand inline for the supported
242 // types when that libcall is unavailable.
243 return VT.isSimple() && is_contained(ExpandableTypes, VT.getSimpleVT()) &&
244 Libcalls.getLibcallImpl(RTLIB::getREM(VT)) == RTLIB::Unsupported;
245 default:
246 return false;
247 }
248 }
249
250 static bool shouldExpandFremType(const TargetLowering &TLI,
251 const LibcallLoweringInfo &Libcalls,
252 Type *Ty) {
253 // Consider scalar type for simplicity. It seems unlikely that a
254 // vector type can be legalized without expansion if the scalar
255 // type cannot.
256 return shouldExpandFremType(TLI, Libcalls,
258 }
259
260 /// Return true if the pass should expand frem instructions of any type
261 /// for the target represented by \p TLI.
262 static bool shouldExpandAnyFremType(const TargetLowering &TLI,
263 const LibcallLoweringInfo &Libcalls) {
264 return any_of(ExpandableTypes, [&](MVT V) {
265 return shouldExpandFremType(TLI, Libcalls, EVT(V));
266 });
267 }
268
269 static FRemExpander create(IRBuilder<> &B, Type *Ty) {
270 assert(canExpandType(Ty) && "Expected supported floating point type");
271
272 // The type to use for the computation of the remainder. This may be
273 // wider than the input/result type which affects the ...
274 Type *ComputeTy = Ty;
275 // ... maximum number of iterations of the remainder computation loop
276 // to use. This value is for the case in which the computation
277 // uses the same input/result type.
278 unsigned MaxIter = 2;
279
280 if (Ty->isHalfTy()) {
281 // Use the wider type and less iterations.
282 ComputeTy = B.getFloatTy();
283 MaxIter = 1;
284 }
285
286 unsigned Precision = APFloat::semanticsPrecision(Ty->getFltSemantics());
287 return FRemExpander{B, Ty, Precision / MaxIter, ComputeTy};
288 }
289
290 /// Build the FRem expansion for the numerator \p X and the
291 /// denumerator \p Y. The type of X and Y must match \p FremTy. The
292 /// code will be generated at the insertion point of \p B and the
293 /// insertion point will be reset at exit.
294 Value *buildFRem(Value *X, Value *Y, std::optional<SimplifyQuery> &SQ) const;
295
296 /// Build an approximate FRem expansion for the numerator \p X and
297 /// the denumerator \p Y at the insertion point of builder \p B.
298 /// The type of X and Y must match \p FremTy.
299 Value *buildApproxFRem(Value *X, Value *Y) const;
300
301private:
302 FRemExpander(IRBuilder<> &B, Type *FremTy, unsigned Bits, Type *ComputeFpTy)
303 : B(B), FremTy(FremTy), ComputeFpTy(ComputeFpTy), ExTy(B.getInt32Ty()),
304 Bits(ConstantInt::get(ExTy, Bits)), One(ConstantInt::get(ExTy, 1)) {}
305
306 Value *createRcp(Value *V, const Twine &Name) const {
307 // Leave it to later optimizations to turn this into an rcp
308 // instruction if available.
309 return B.CreateFDiv(ConstantFP::get(ComputeFpTy, 1.0), V, Name);
310 }
311
312 // Helper function to build the UPDATE_AX code which is common to the
313 // loop body and the "final iteration".
314 Value *buildUpdateAx(Value *Ax, Value *Ay, Value *Ayinv) const {
315 // Build:
316 // float q = rint(ax * ayinv);
317 // ax = fma(-q, ay, ax);
318 // int clt = ax < 0.0f;
319 // float axp = ax + ay;
320 // ax = clt ? axp : ax;
321 Value *Q = B.CreateUnaryIntrinsic(Intrinsic::rint, B.CreateFMul(Ax, Ayinv),
322 {}, "q");
323 Value *AxUpdate = B.CreateFMA(B.CreateFNeg(Q), Ay, Ax, {}, "ax");
324 Value *Clt = B.CreateFCmp(CmpInst::FCMP_OLT, AxUpdate,
325 ConstantFP::getZero(ComputeFpTy), "clt");
326 Value *Axp = B.CreateFAdd(AxUpdate, Ay, "axp");
327 return B.CreateSelect(Clt, Axp, AxUpdate, "ax");
328 }
329
330 /// Build code to extract the exponent and mantissa of \p Src.
331 /// Return the exponent minus one for use as a loop bound and
332 /// the mantissa taken to the given \p NewExp power.
333 std::pair<Value *, Value *> buildExpAndPower(Value *Src, Value *NewExp,
334 const Twine &ExName,
335 const Twine &PowName) const {
336 // Build:
337 // ExName = frexp_exp(Src) - 1;
338 // PowName = fldexp(frexp_mant(ExName), NewExp);
339 Type *Ty = Src->getType();
340 Type *ExTy = B.getInt32Ty();
341 Value *Frexp = B.CreateIntrinsic(Intrinsic::frexp, {Ty, ExTy}, Src);
342 Value *Mant = B.CreateExtractValue(Frexp, {0});
343 Value *Exp = B.CreateExtractValue(Frexp, {1});
344
345 Exp = B.CreateSub(Exp, One, ExName);
346 Value *Pow = B.CreateLdexp(Mant, NewExp, {}, PowName);
347
348 return {Pow, Exp};
349 }
350
351 /// Build the main computation of the remainder for the case in which
352 /// Ax > Ay, where Ax = |X|, Ay = |Y|, and X is the numerator and Y the
353 /// denumerator. Add the incoming edge from the computation result
354 /// to \p RetPhi.
355 void buildRemainderComputation(Value *AxInitial, Value *AyInitial, Value *X,
356 PHINode *RetPhi, FastMathFlags FMF) const {
357 IRBuilder<>::FastMathFlagGuard Guard(B);
358 B.setFastMathFlags(FMF);
359
360 // Build:
361 // ex = frexp_exp(ax) - 1;
362 // ax = fldexp(frexp_mant(ax), bits);
363 // ey = frexp_exp(ay) - 1;
364 // ay = fledxp(frexp_mant(ay), 1);
365 auto [Ax, Ex] = buildExpAndPower(AxInitial, Bits, "ex", "ax");
366 auto [Ay, Ey] = buildExpAndPower(AyInitial, One, "ey", "ay");
367
368 // Build:
369 // int nb = ex - ey;
370 // float ayinv = 1.0/ay;
371 Value *Nb = B.CreateSub(Ex, Ey, "nb");
372 Value *Ayinv = createRcp(Ay, "ayinv");
373
374 // Build: while (nb > bits)
375 BasicBlock *PreheaderBB = B.GetInsertBlock();
376 Function *Fun = PreheaderBB->getParent();
377 auto *LoopBB = BasicBlock::Create(B.getContext(), "frem.loop_body", Fun);
378 auto *ExitBB = BasicBlock::Create(B.getContext(), "frem.loop_exit", Fun);
379
380 B.CreateCondBr(B.CreateICmp(CmpInst::ICMP_SGT, Nb, Bits), LoopBB, ExitBB);
381
382 // Build loop body:
383 // UPDATE_AX
384 // ax = fldexp(ax, bits);
385 // nb -= bits;
386 // One iteration of the loop is factored out. The code shared by
387 // the loop and this "iteration" is denoted by UPDATE_AX.
388 B.SetInsertPoint(LoopBB);
389 PHINode *NbIv = B.CreatePHI(Nb->getType(), 2, "nb_iv");
390 NbIv->addIncoming(Nb, PreheaderBB);
391
392 auto *AxPhi = B.CreatePHI(ComputeFpTy, 2, "ax_loop_phi");
393 AxPhi->addIncoming(Ax, PreheaderBB);
394
395 Value *AxPhiUpdate = buildUpdateAx(AxPhi, Ay, Ayinv);
396 AxPhiUpdate = B.CreateLdexp(AxPhiUpdate, Bits, {}, "ax_update");
397 AxPhi->addIncoming(AxPhiUpdate, LoopBB);
398 NbIv->addIncoming(B.CreateSub(NbIv, Bits, "nb_update"), LoopBB);
399
400 B.CreateCondBr(B.CreateICmp(CmpInst::ICMP_SGT, NbIv, Bits), LoopBB, ExitBB);
401
402 // Build final iteration
403 // ax = fldexp(ax, nb - bits + 1);
404 // UPDATE_AX
405 B.SetInsertPoint(ExitBB);
406
407 auto *AxPhiExit = B.CreatePHI(ComputeFpTy, 2, "ax_exit_phi");
408 AxPhiExit->addIncoming(Ax, PreheaderBB);
409 AxPhiExit->addIncoming(AxPhi, LoopBB);
410 auto *NbExitPhi = B.CreatePHI(Nb->getType(), 2, "nb_exit_phi");
411 NbExitPhi->addIncoming(NbIv, LoopBB);
412 NbExitPhi->addIncoming(Nb, PreheaderBB);
413
414 Value *AxFinal = B.CreateLdexp(
415 AxPhiExit, B.CreateAdd(B.CreateSub(NbExitPhi, Bits), One), {}, "ax");
416 AxFinal = buildUpdateAx(AxFinal, Ay, Ayinv);
417
418 // Build:
419 // ax = fldexp(ax, ey);
420 // ret = copysign(ax,x);
421 AxFinal = B.CreateLdexp(AxFinal, Ey, {}, "ax");
422 if (ComputeFpTy != FremTy)
423 AxFinal = B.CreateFPTrunc(AxFinal, FremTy);
424 Value *Ret = B.CreateCopySign(AxFinal, X);
425
426 RetPhi->addIncoming(Ret, ExitBB);
427 }
428
429 /// Build the else-branch of the conditional in the FRem
430 /// expansion, i.e. the case in wich Ax <= Ay, where Ax = |X|, Ay
431 /// = |Y|, and X is the numerator and Y the denumerator. Add the
432 /// incoming edge from the result to \p RetPhi.
433 void buildElseBranch(Value *Ax, Value *Ay, Value *X, PHINode *RetPhi) const {
434 // Build:
435 // ret = ax == ay ? copysign(0.0f, x) : x;
436 Value *ZeroWithXSign = B.CreateCopySign(ConstantFP::getZero(FremTy), X);
437 Value *Ret = B.CreateSelect(B.CreateFCmpOEQ(Ax, Ay), ZeroWithXSign, X);
438
439 RetPhi->addIncoming(Ret, B.GetInsertBlock());
440 }
441
442 /// Return a value that is NaN if one of the corner cases concerning
443 /// the inputs \p X and \p Y is detected, and \p Ret otherwise.
444 Value *handleInputCornerCases(Value *Ret, Value *X, Value *Y,
445 std::optional<SimplifyQuery> &SQ,
446 bool NoInfs) const {
447 // Build:
448 // ret = (y == 0.0f || isnan(y)) ? QNAN : ret;
449 // ret = isfinite(x) ? ret : QNAN;
450 Value *Nan = ConstantFP::getQNaN(FremTy);
451 Ret = B.CreateSelect(B.CreateFCmpUEQ(Y, ConstantFP::getZero(FremTy)), Nan,
452 Ret);
453 Value *XFinite =
454 NoInfs || (SQ && isKnownNeverInfinity(X, *SQ))
455 ? B.getTrue()
456 : B.CreateFCmpULT(B.CreateFAbs(X), ConstantFP::getInfinity(FremTy));
457 Ret = B.CreateSelect(XFinite, Ret, Nan);
458
459 return Ret;
460 }
461};
462} // namespace
463
464Value *FRemExpander::buildApproxFRem(Value *X, Value *Y) const {
465 IRBuilder<>::FastMathFlagGuard Guard(B);
466 // Propagating the approximate functions flag to the
467 // division leads to an unacceptable drop in precision
468 // on AMDGPU.
469 // TODO Find out if any flags might be worth propagating.
470 B.clearFastMathFlags();
471
472 Value *Quot = B.CreateFDiv(X, Y);
473 Value *Trunc = B.CreateUnaryIntrinsic(Intrinsic::trunc, Quot, {});
474 Value *Neg = B.CreateFNeg(Trunc);
475
476 return B.CreateFMA(Neg, Y, X);
477}
478
479Value *FRemExpander::buildFRem(Value *X, Value *Y,
480 std::optional<SimplifyQuery> &SQ) const {
481 assert(X->getType() == FremTy && Y->getType() == FremTy);
482
483 FastMathFlags FMF = B.getFastMathFlags();
484
485 // This function generates the following code structure:
486 // if (abs(x) > abs(y))
487 // { ret = compute remainder }
488 // else
489 // { ret = x or 0 with sign of x }
490 // Adjust ret to NaN/inf in input
491 // return ret
492 Value *Ax = B.CreateFAbs(X, {}, "ax");
493 Value *Ay = B.CreateFAbs(Y, {}, "ay");
494 if (ComputeFpTy != X->getType()) {
495 Ax = B.CreateFPExt(Ax, ComputeFpTy, "ax");
496 Ay = B.CreateFPExt(Ay, ComputeFpTy, "ay");
497 }
498 Value *AxAyCmp = B.CreateFCmpOGT(Ax, Ay);
499
500 PHINode *RetPhi = B.CreatePHI(FremTy, 2, "ret");
501 Value *Ret = RetPhi;
502
503 // We would return NaN in all corner cases handled here.
504 // Hence, if NaNs are excluded, keep the result as it is.
505 if (!FMF.noNaNs())
506 Ret = handleInputCornerCases(Ret, X, Y, SQ, FMF.noInfs());
507
508 Function *Fun = B.GetInsertBlock()->getParent();
509 auto *ThenBB = BasicBlock::Create(B.getContext(), "frem.compute", Fun);
510 auto *ElseBB = BasicBlock::Create(B.getContext(), "frem.else", Fun);
511 SplitBlockAndInsertIfThenElse(AxAyCmp, RetPhi, &ThenBB, &ElseBB);
512
513 auto SavedInsertPt = B.GetInsertPoint();
514
515 // Build remainder computation for "then" branch
516 //
517 // The ordered comparison ensures that ax and ay are not NaNs
518 // in the then-branch. Furthermore, y cannot be an infinity and the
519 // check at the end of the function ensures that the result will not
520 // be used if x is an infinity.
521 FastMathFlags ComputeFMF = FMF;
522 ComputeFMF.setNoInfs();
523 ComputeFMF.setNoNaNs();
524
525 B.SetInsertPoint(ThenBB);
526 buildRemainderComputation(Ax, Ay, X, RetPhi, FMF);
527 B.CreateBr(RetPhi->getParent());
528
529 // Build "else"-branch
530 B.SetInsertPoint(ElseBB);
531 buildElseBranch(Ax, Ay, X, RetPhi);
532 B.CreateBr(RetPhi->getParent());
533
534 B.SetInsertPoint(SavedInsertPt);
535
536 return Ret;
537}
538
539static bool expandFRem(BinaryOperator &I, std::optional<SimplifyQuery> &SQ) {
540 LLVM_DEBUG(dbgs() << "Expanding instruction: " << I << '\n');
541
542 Type *Ty = I.getType();
543 assert(FRemExpander::canExpandType(Ty) &&
544 "Expected supported floating point type");
545
546 FastMathFlags FMF = I.getFastMathFlags();
547 // TODO Make use of those flags for optimization?
548 FMF.setAllowReciprocal(false);
549 FMF.setAllowContract(false);
550
551 IRBuilder<> B(&I);
552 B.setFastMathFlags(FMF);
553 B.SetCurrentDebugLocation(I.getDebugLoc());
554
555 const FRemExpander Expander = FRemExpander::create(B, Ty);
556 Value *Ret = FMF.approxFunc()
557 ? Expander.buildApproxFRem(I.getOperand(0), I.getOperand(1))
558 : Expander.buildFRem(I.getOperand(0), I.getOperand(1), SQ);
559
560 I.replaceAllUsesWith(Ret);
561 Ret->takeName(&I);
562 I.eraseFromParent();
563
564 return true;
565}
566// clang-format off: preserve formatting of the following example
567
568/// Generate code to convert a fp number to integer, replacing FPToS(U)I with
569/// the generated code. This currently generates code similarly to compiler-rt's
570/// implementations.
571///
572/// An example IR generated from compiler-rt/fixsfdi.c looks like below:
573/// define dso_local i64 @foo(float noundef %a) local_unnamed_addr #0 {
574/// entry:
575/// %0 = bitcast float %a to i32
576/// %conv.i = zext i32 %0 to i64
577/// %tobool.not = icmp sgt i32 %0, -1
578/// %conv = select i1 %tobool.not, i64 1, i64 -1
579/// %and = lshr i64 %conv.i, 23
580/// %shr = and i64 %and, 255
581/// %and2 = and i64 %conv.i, 8388607
582/// %or = or i64 %and2, 8388608
583/// %cmp = icmp ult i64 %shr, 127
584/// br i1 %cmp, label %cleanup, label %if.end
585///
586/// if.end: ; preds = %entry
587/// %sub = add nuw nsw i64 %shr, 4294967169
588/// %conv5 = and i64 %sub, 4294967232
589/// %cmp6.not = icmp eq i64 %conv5, 0
590/// br i1 %cmp6.not, label %if.end12, label %if.then8
591///
592/// if.then8: ; preds = %if.end
593/// %cond11 = select i1 %tobool.not, i64 9223372036854775807, i64
594/// -9223372036854775808 br label %cleanup
595///
596/// if.end12: ; preds = %if.end
597/// %cmp13 = icmp ult i64 %shr, 150
598/// br i1 %cmp13, label %if.then15, label %if.else
599///
600/// if.then15: ; preds = %if.end12
601/// %sub16 = sub nuw nsw i64 150, %shr
602/// %shr17 = lshr i64 %or, %sub16
603/// %mul = mul nsw i64 %shr17, %conv
604/// br label %cleanup
605///
606/// if.else: ; preds = %if.end12
607/// %sub18 = add nsw i64 %shr, -150
608/// %shl = shl i64 %or, %sub18
609/// %mul19 = mul nsw i64 %shl, %conv
610/// br label %cleanup
611///
612/// cleanup: ; preds = %entry,
613/// %if.else, %if.then15, %if.then8
614/// %retval.0 = phi i64 [ %cond11, %if.then8 ], [ %mul, %if.then15 ], [
615/// %mul19, %if.else ], [ 0, %entry ] ret i64 %retval.0
616/// }
617///
618/// Replace fp to integer with generated code.
619static void expandFPToI(Instruction *FPToI, bool IsSaturating, bool IsSigned) {
620 // clang-format on
621 IRBuilder<> Builder(FPToI);
622 auto *FloatVal = FPToI->getOperand(0);
623 IntegerType *IntTy = cast<IntegerType>(FPToI->getType());
624
625 unsigned BitWidth = FPToI->getType()->getIntegerBitWidth();
626 unsigned FPMantissaWidth = FloatVal->getType()->getFPMantissaWidth() - 1;
627
628 // FIXME: fp16's range is covered by i32. So `fptoi half` can convert
629 // to i32 first following a sext/zext to target integer type.
630 Value *A1 = nullptr;
631 if (FloatVal->getType()->isHalfTy() && BitWidth >= 32) {
632 if (FPToI->getOpcode() == Instruction::FPToUI) {
633 Value *A0 = Builder.CreateFPToUI(FloatVal, Builder.getInt32Ty());
634 A1 = Builder.CreateZExt(A0, IntTy);
635 } else { // FPToSI
636 Value *A0 = Builder.CreateFPToSI(FloatVal, Builder.getInt32Ty());
637 A1 = Builder.CreateSExt(A0, IntTy);
638 }
639 FPToI->replaceAllUsesWith(A1);
640 FPToI->dropAllReferences();
641 FPToI->eraseFromParent();
642 return;
643 }
644
645 // fp80 conversion is implemented by fpext to fp128 first then do the
646 // conversion.
647 FPMantissaWidth = FPMantissaWidth == 63 ? 112 : FPMantissaWidth;
648 unsigned FloatWidth =
649 PowerOf2Ceil(FloatVal->getType()->getScalarSizeInBits());
650 unsigned ExponentWidth = FloatWidth - FPMantissaWidth - 1;
651 unsigned ExponentBias = (1 << (ExponentWidth - 1)) - 1;
652 IntegerType *FloatIntTy = Builder.getIntNTy(FloatWidth);
653 Value *ImplicitBit = ConstantInt::get(
654 FloatIntTy, APInt::getOneBitSet(FloatWidth, FPMantissaWidth));
655 Value *SignificandMask = ConstantInt::get(
656 FloatIntTy, APInt::getLowBitsSet(FloatWidth, FPMantissaWidth));
657
658 BasicBlock *Entry = Builder.GetInsertBlock();
659 Function *F = Entry->getParent();
660 Entry->setName(Twine(Entry->getName(), "fp-to-i-entry"));
661 BasicBlock *CheckSaturateBB, *SaturateBB;
662 BasicBlock *End =
663 Entry->splitBasicBlock(Builder.GetInsertPoint(), "fp-to-i-cleanup");
664 if (IsSaturating) {
665 CheckSaturateBB = BasicBlock::Create(Builder.getContext(),
666 "fp-to-i-if-check.saturate", F, End);
667 SaturateBB =
668 BasicBlock::Create(Builder.getContext(), "fp-to-i-if-saturate", F, End);
669 }
670 BasicBlock *CheckExpSizeBB = BasicBlock::Create(
671 Builder.getContext(), "fp-to-i-if-check.exp.size", F, End);
672 BasicBlock *ExpSmallBB =
673 BasicBlock::Create(Builder.getContext(), "fp-to-i-if-exp.small", F, End);
674 BasicBlock *ExpLargeBB =
675 BasicBlock::Create(Builder.getContext(), "fp-to-i-if-exp.large", F, End);
676
677 Entry->getTerminator()->eraseFromParent();
678
679 // entry:
680 Builder.SetInsertPoint(Entry);
681 // We're going to introduce branches on the value, so freeze it.
683 FloatVal = Builder.CreateFreeze(FloatVal);
684 // fp80 conversion is implemented by fpext to fp128 first then do the
685 // conversion.
686 if (FloatVal->getType()->isX86_FP80Ty())
687 FloatVal =
688 Builder.CreateFPExt(FloatVal, Type::getFP128Ty(Builder.getContext()));
689 Value *ARep = Builder.CreateBitCast(FloatVal, FloatIntTy);
690 Value *PosOrNeg, *Sign;
691 if (IsSigned) {
692 PosOrNeg =
693 Builder.CreateICmpSGT(ARep, ConstantInt::getSigned(FloatIntTy, -1));
694 Sign = Builder.CreateSelectWithUnknownProfile(
695 PosOrNeg, ConstantInt::getSigned(IntTy, 1),
696 ConstantInt::getSigned(IntTy, -1), "sign");
697 }
698 Value *And =
699 Builder.CreateLShr(ARep, Builder.getIntN(FloatWidth, FPMantissaWidth));
700 Value *BiasedExp = Builder.CreateAnd(
701 And, Builder.getIntN(FloatWidth, (1 << ExponentWidth) - 1), "biased.exp");
702 Value *Abs = Builder.CreateAnd(ARep, SignificandMask);
703 Value *Significand = Builder.CreateOr(Abs, ImplicitBit, "significand");
704 Value *ZeroResultCond = Builder.CreateICmpULT(
705 BiasedExp, Builder.getIntN(FloatWidth, ExponentBias), "exp.is.negative");
706 if (IsSaturating) {
707 Value *IsNaN = Builder.CreateFCmpUNO(FloatVal, FloatVal, "is.nan");
708 ZeroResultCond = Builder.CreateOr(ZeroResultCond, IsNaN);
709 if (!IsSigned) {
710 Value *IsNeg = Builder.CreateIsNeg(ARep);
711 ZeroResultCond = Builder.CreateOr(ZeroResultCond, IsNeg);
712 }
713 }
714 Instruction *CondBr = Builder.CreateCondBr(
715 ZeroResultCond, End, IsSaturating ? CheckSaturateBB : CheckExpSizeBB);
716 // We do not have any information on the value of the exponent, so mark the
717 // branch weights as unkown.
719
720 Value *Saturated;
721 if (IsSaturating) {
722 // check.saturate:
723 Builder.SetInsertPoint(CheckSaturateBB);
724 uint64_t SaturatingBiasedExp =
725 static_cast<uint64_t>(ExponentBias) + BitWidth - IsSigned;
726 // Clamp to the all-ones (inf/NaN) exponent. Without this, when the integer
727 // is wide enough to hold every finite float the threshold exceeds any
728 // possible biased exponent, so +/-inf would never saturate.
729 uint64_t MaxBiasedExp = (1ULL << ExponentWidth) - 1;
730 if (SaturatingBiasedExp > MaxBiasedExp)
731 SaturatingBiasedExp = MaxBiasedExp;
732 Value *Cmp3 = Builder.CreateICmpUGE(
733 BiasedExp, ConstantInt::get(FloatIntTy, SaturatingBiasedExp));
734 Value *CondBrSat = Builder.CreateCondBr(Cmp3, SaturateBB, CheckExpSizeBB);
735 // Saturation is considered an unlikely event.
736 applyProfMetadataIfEnabled(CondBrSat, [&](Instruction *Inst) {
737 Inst->setMetadata(
738 LLVMContext::MD_prof,
740 });
741
742 // saturate:
743 Builder.SetInsertPoint(SaturateBB);
744 if (IsSigned) {
745 Value *SignedMax =
746 ConstantInt::get(IntTy, APInt::getSignedMaxValue(BitWidth));
747 Value *SignedMin =
748 ConstantInt::get(IntTy, APInt::getSignedMinValue(BitWidth));
749 // Select between the signed max and min values for saturation.
750 Saturated = Builder.CreateSelectWithUnknownProfile(
751 PosOrNeg, SignedMax, SignedMin, "saturated");
752 } else {
753 Saturated = ConstantInt::getAllOnesValue(IntTy);
754 }
755 Builder.CreateBr(End);
756 }
757
758 // if.end9:
759 Builder.SetInsertPoint(CheckExpSizeBB);
760 Value *ExpSmallerMantissaWidth = Builder.CreateICmpULT(
761 BiasedExp, Builder.getIntN(FloatWidth, ExponentBias + FPMantissaWidth),
762 "exp.smaller.mantissa.width");
763 // We cannot determine whether this is a left shift or a right shift,
764 // so we mark the branch weights as unknown.
765 Value *CondBr2 =
766 Builder.CreateCondBr(ExpSmallerMantissaWidth, ExpSmallBB, ExpLargeBB);
767 applyProfMetadataIfEnabled(CondBr2, [&](Instruction *Inst) {
769 });
770
771 // exp.small:
772 Builder.SetInsertPoint(ExpSmallBB);
773 Value *Sub13 = Builder.CreateSub(
774 Builder.getIntN(FloatWidth, ExponentBias + FPMantissaWidth), BiasedExp);
775 Value *ExpSmallRes =
776 Builder.CreateZExtOrTrunc(Builder.CreateLShr(Significand, Sub13), IntTy);
777 if (IsSigned)
778 ExpSmallRes = Builder.CreateMul(ExpSmallRes, Sign);
779 Builder.CreateBr(End);
780
781 // exp.large:
782 Builder.SetInsertPoint(ExpLargeBB);
783 Value *Sub15 = Builder.CreateAdd(
784 BiasedExp,
786 FloatIntTy, -static_cast<int64_t>(ExponentBias + FPMantissaWidth)));
787 Value *SignificandCast = Builder.CreateZExtOrTrunc(Significand, IntTy);
788 Value *ExpLargeRes = Builder.CreateShl(
789 SignificandCast, Builder.CreateZExtOrTrunc(Sub15, IntTy));
790 if (IsSigned)
791 ExpLargeRes = Builder.CreateMul(ExpLargeRes, Sign);
792 Builder.CreateBr(End);
793
794 // cleanup:
795 Builder.SetInsertPoint(End, End->begin());
796 PHINode *Retval0 = Builder.CreatePHI(FPToI->getType(), 3 + IsSaturating);
797
798 if (IsSaturating)
799 Retval0->addIncoming(Saturated, SaturateBB);
800 Retval0->addIncoming(ExpSmallRes, ExpSmallBB);
801 Retval0->addIncoming(ExpLargeRes, ExpLargeBB);
802 Retval0->addIncoming(Builder.getIntN(BitWidth, 0), Entry);
803
804 FPToI->replaceAllUsesWith(Retval0);
805 FPToI->dropAllReferences();
806 FPToI->eraseFromParent();
807}
808
809// clang-format off: preserve formatting of the following example
810
811/// Generate code to convert a fp number to integer, replacing S(U)IToFP with
812/// the generated code. This currently generates code similarly to compiler-rt's
813/// implementations. This implementation has an implicit assumption that integer
814/// width is larger than fp.
815///
816/// An example IR generated from compiler-rt/floatdisf.c looks like below:
817/// define dso_local float @__floatdisf(i64 noundef %a) local_unnamed_addr #0 {
818/// entry:
819/// %cmp = icmp eq i64 %a, 0
820/// br i1 %cmp, label %return, label %if.end
821///
822/// if.end: ; preds = %entry
823/// %shr = ashr i64 %a, 63
824/// %xor = xor i64 %shr, %a
825/// %sub = sub nsw i64 %xor, %shr
826/// %0 = tail call i64 @llvm.ctlz.i64(i64 %sub, i1 true), !range !5
827/// %cast = trunc i64 %0 to i32
828/// %sub1 = sub nuw nsw i32 64, %cast
829/// %sub2 = xor i32 %cast, 63
830/// %cmp3 = icmp ult i32 %cast, 40
831/// br i1 %cmp3, label %if.then4, label %if.else
832///
833/// if.then4: ; preds = %if.end
834/// switch i32 %sub1, label %sw.default [
835/// i32 25, label %sw.bb
836/// i32 26, label %sw.epilog
837/// ]
838///
839/// sw.bb: ; preds = %if.then4
840/// %shl = shl i64 %sub, 1
841/// br label %sw.epilog
842///
843/// sw.default: ; preds = %if.then4
844/// %sub5 = sub nsw i64 38, %0
845/// %sh_prom = and i64 %sub5, 4294967295
846/// %shr6 = lshr i64 %sub, %sh_prom
847/// %shr9 = lshr i64 274877906943, %0
848/// %and = and i64 %shr9, %sub
849/// %cmp10 = icmp ne i64 %and, 0
850/// %conv11 = zext i1 %cmp10 to i64
851/// %or = or i64 %shr6, %conv11
852/// br label %sw.epilog
853///
854/// sw.epilog: ; preds = %sw.default,
855/// %if.then4, %sw.bb
856/// %a.addr.0 = phi i64 [ %or, %sw.default ], [ %sub, %if.then4 ], [ %shl,
857/// %sw.bb ] %1 = lshr i64 %a.addr.0, 2 %2 = and i64 %1, 1 %or16 = or i64 %2,
858/// %a.addr.0 %inc = add nsw i64 %or16, 1 %3 = and i64 %inc, 67108864
859/// %tobool.not = icmp eq i64 %3, 0
860/// %spec.select.v = select i1 %tobool.not, i64 2, i64 3
861/// %spec.select = ashr i64 %inc, %spec.select.v
862/// %spec.select56 = select i1 %tobool.not, i32 %sub2, i32 %sub1
863/// br label %if.end26
864///
865/// if.else: ; preds = %if.end
866/// %sub23 = add nuw nsw i64 %0, 4294967256
867/// %sh_prom24 = and i64 %sub23, 4294967295
868/// %shl25 = shl i64 %sub, %sh_prom24
869/// br label %if.end26
870///
871/// if.end26: ; preds = %sw.epilog,
872/// %if.else
873/// %a.addr.1 = phi i64 [ %shl25, %if.else ], [ %spec.select, %sw.epilog ]
874/// %e.0 = phi i32 [ %sub2, %if.else ], [ %spec.select56, %sw.epilog ]
875/// %conv27 = trunc i64 %shr to i32
876/// %and28 = and i32 %conv27, -2147483648
877/// %add = shl nuw nsw i32 %e.0, 23
878/// %shl29 = add nuw nsw i32 %add, 1065353216
879/// %conv31 = trunc i64 %a.addr.1 to i32
880/// %and32 = and i32 %conv31, 8388607
881/// %or30 = or i32 %and32, %and28
882/// %or33 = or i32 %or30, %shl29
883/// %4 = bitcast i32 %or33 to float
884/// br label %return
885///
886/// return: ; preds = %entry,
887/// %if.end26
888/// %retval.0 = phi float [ %4, %if.end26 ], [ 0.000000e+00, %entry ]
889/// ret float %retval.0
890/// }
891///
892/// Replace integer to fp with generated code.
893static void expandIToFP(Instruction *IToFP) {
894 // clang-format on
895 IRBuilder<> Builder(IToFP);
896 auto *IntVal = IToFP->getOperand(0);
897 IntegerType *IntTy = cast<IntegerType>(IntVal->getType());
898
899 unsigned BitWidth = IntVal->getType()->getIntegerBitWidth();
900 unsigned FPMantissaWidth = IToFP->getType()->getFPMantissaWidth() - 1;
901 // fp80 conversion is implemented by conversion tp fp128 first following
902 // a fptrunc to fp80.
903 FPMantissaWidth = FPMantissaWidth == 63 ? 112 : FPMantissaWidth;
904 // FIXME: As there is no related builtins added in compliler-rt,
905 // here currently utilized the fp32 <-> fp16 lib calls to implement.
906 FPMantissaWidth = FPMantissaWidth == 10 ? 23 : FPMantissaWidth;
907 FPMantissaWidth = FPMantissaWidth == 7 ? 23 : FPMantissaWidth;
908 unsigned FloatWidth = PowerOf2Ceil(FPMantissaWidth);
909 bool IsSigned = IToFP->getOpcode() == Instruction::SIToFP;
910
911 // We're going to introduce branches on the value, so freeze it.
913 IntVal = Builder.CreateFreeze(IntVal);
914
915 // The expansion below assumes that int width >= float width. Zero or sign
916 // extend the integer accordingly.
917 if (BitWidth < FloatWidth) {
918 BitWidth = FloatWidth;
919 IntTy = Builder.getIntNTy(BitWidth);
920 IntVal = Builder.CreateIntCast(IntVal, IntTy, IsSigned);
921 }
922
923 Value *Temp1 =
924 Builder.CreateShl(Builder.getIntN(BitWidth, 1),
925 Builder.getIntN(BitWidth, FPMantissaWidth + 3));
926
927 BasicBlock *Entry = Builder.GetInsertBlock();
928 Function *F = Entry->getParent();
929 Entry->setName(Twine(Entry->getName(), "itofp-entry"));
930 BasicBlock *End =
931 Entry->splitBasicBlock(Builder.GetInsertPoint(), "itofp-return");
932 BasicBlock *IfEnd =
933 BasicBlock::Create(Builder.getContext(), "itofp-if-end", F, End);
934 BasicBlock *IfThen4 =
935 BasicBlock::Create(Builder.getContext(), "itofp-if-then4", F, End);
936 BasicBlock *SwBB =
937 BasicBlock::Create(Builder.getContext(), "itofp-sw-bb", F, End);
938 BasicBlock *SwDefault =
939 BasicBlock::Create(Builder.getContext(), "itofp-sw-default", F, End);
940 BasicBlock *SwEpilog =
941 BasicBlock::Create(Builder.getContext(), "itofp-sw-epilog", F, End);
942 BasicBlock *IfThen20 =
943 BasicBlock::Create(Builder.getContext(), "itofp-if-then20", F, End);
944 BasicBlock *IfElse =
945 BasicBlock::Create(Builder.getContext(), "itofp-if-else", F, End);
946 BasicBlock *IfEnd26 =
947 BasicBlock::Create(Builder.getContext(), "itofp-if-end26", F, End);
948
949 Entry->getTerminator()->eraseFromParent();
950
951 Function *CTLZ =
952 Intrinsic::getOrInsertDeclaration(F->getParent(), Intrinsic::ctlz, IntTy);
953 ConstantInt *True = Builder.getTrue();
954
955 // entry:
956 Builder.SetInsertPoint(Entry);
957 // We assume that the zero is an unlikely input case, so the branch to 'End'
958 // is the unlikely path.
959 Value *Cmp = Builder.CreateICmpEQ(IntVal, ConstantInt::getSigned(IntTy, 0));
960 Value *CondBrEntry = Builder.CreateCondBr(Cmp, End, IfEnd);
961 applyProfMetadataIfEnabled(CondBrEntry, [&](Instruction *Inst) {
962 Inst->setMetadata(
963 LLVMContext::MD_prof,
965 });
966
967 // if.end:
968 Builder.SetInsertPoint(IfEnd);
969 Value *Shr =
970 Builder.CreateAShr(IntVal, Builder.getIntN(BitWidth, BitWidth - 1));
971 Value *Xor = Builder.CreateXor(Shr, IntVal);
972 Value *Sub = Builder.CreateSub(Xor, Shr);
973 Value *Call = Builder.CreateCall(CTLZ, {IsSigned ? Sub : IntVal, True});
974 Value *Cast = Builder.CreateTrunc(Call, Builder.getInt32Ty());
975 int BitWidthNew = FloatWidth == 128 ? BitWidth : 32;
976 Value *Sub1 = Builder.CreateSub(Builder.getIntN(BitWidthNew, BitWidth),
977 FloatWidth == 128 ? Call : Cast);
978 Value *Sub2 = Builder.CreateSub(Builder.getIntN(BitWidthNew, BitWidth - 1),
979 FloatWidth == 128 ? Call : Cast);
980 Value *Cmp3 = Builder.CreateICmpSGT(
981 Sub1, Builder.getIntN(BitWidthNew, FPMantissaWidth + 1));
982 // This branch handles the rare case where rounding the mantissa causes a
983 // carry-out at the most significant bit, necessitating an increment of the
984 // exponent. This is rare case, so the True path is mared as likely.
985 Value *CondBrIfEnd = Builder.CreateCondBr(Cmp3, IfThen4, IfElse);
986 applyProfMetadataIfEnabled(CondBrIfEnd, [&](Instruction *Inst) {
987 Inst->setMetadata(
988 LLVMContext::MD_prof,
990 });
991
992 // if.then4:
993 Builder.SetInsertPoint(IfThen4);
994 SwitchInst *SI = Builder.CreateSwitch(Sub1, SwDefault);
995 SI->addCase(Builder.getIntN(BitWidthNew, FPMantissaWidth + 2), SwBB);
996 SI->addCase(Builder.getIntN(BitWidthNew, FPMantissaWidth + 3), SwEpilog);
997 // Add branch weights to the SwitchInst. The weights are provided for the
998 // default case first (SwDefault), followed by each explicit case in the
999 // order they were added (SwBB, then SwEpilog). Because the following cases
1000 // are rare, the defalut case is given a likely weight.
1002 SI->setMetadata(
1003 LLVMContext::MD_prof,
1004 MDBuilder(SI->getContext())
1005 .createBranchWeights({llvm::MDBuilder::kLikelyBranchWeight,
1006 llvm::MDBuilder::kUnlikelyBranchWeight,
1007 llvm::MDBuilder::kUnlikelyBranchWeight}));
1008 }
1009
1010 // sw.bb:
1011 Builder.SetInsertPoint(SwBB);
1012 Value *Shl =
1013 Builder.CreateShl(IsSigned ? Sub : IntVal, Builder.getIntN(BitWidth, 1));
1014 Builder.CreateBr(SwEpilog);
1015
1016 // sw.default:
1017 Builder.SetInsertPoint(SwDefault);
1018 Value *Sub5 = Builder.CreateSub(
1019 Builder.getIntN(BitWidthNew, BitWidth - FPMantissaWidth - 3),
1020 FloatWidth == 128 ? Call : Cast);
1021 Value *ShProm = Builder.CreateZExt(Sub5, IntTy);
1022 Value *Shr6 = Builder.CreateLShr(IsSigned ? Sub : IntVal,
1023 FloatWidth == 128 ? Sub5 : ShProm);
1024 Value *Sub8 =
1025 Builder.CreateAdd(FloatWidth == 128 ? Call : Cast,
1026 Builder.getIntN(BitWidthNew, FPMantissaWidth + 3));
1027 Value *ShProm9 = Builder.CreateZExt(Sub8, IntTy);
1028 Value *Shr9 = Builder.CreateLShr(ConstantInt::getSigned(IntTy, -1),
1029 FloatWidth == 128 ? Sub8 : ShProm9);
1030 Value *And = Builder.CreateAnd(Shr9, IsSigned ? Sub : IntVal);
1031 Value *Cmp10 = Builder.CreateICmpNE(And, Builder.getIntN(BitWidth, 0));
1032 Value *Conv11 = Builder.CreateZExt(Cmp10, IntTy);
1033 Value *Or = Builder.CreateOr(Shr6, Conv11);
1034 Builder.CreateBr(SwEpilog);
1035
1036 // sw.epilog:
1037 Builder.SetInsertPoint(SwEpilog);
1038 PHINode *AAddr0 = Builder.CreatePHI(IntTy, 3);
1039 AAddr0->addIncoming(Or, SwDefault);
1040 AAddr0->addIncoming(IsSigned ? Sub : IntVal, IfThen4);
1041 AAddr0->addIncoming(Shl, SwBB);
1042 Value *A0 = Builder.CreateTrunc(AAddr0, Builder.getInt32Ty());
1043 Value *A1 = Builder.CreateLShr(A0, Builder.getInt32(2));
1044 Value *A2 = Builder.CreateAnd(A1, Builder.getInt32(1));
1045 Value *Conv16 = Builder.CreateZExt(A2, IntTy);
1046 Value *Or17 = Builder.CreateOr(AAddr0, Conv16);
1047 Value *Inc = Builder.CreateAdd(Or17, Builder.getIntN(BitWidth, 1));
1048 Value *Shr18 = nullptr;
1049 if (IsSigned)
1050 Shr18 = Builder.CreateAShr(Inc, Builder.getIntN(BitWidth, 2));
1051 else
1052 Shr18 = Builder.CreateLShr(Inc, Builder.getIntN(BitWidth, 2));
1053 Value *A3 = Builder.CreateAnd(Inc, Temp1, "a3");
1054 Value *PosOrNeg = Builder.CreateICmpEQ(A3, Builder.getIntN(BitWidth, 0));
1055 Value *ExtractT60 = Builder.CreateTrunc(Shr18, Builder.getIntNTy(FloatWidth));
1056 Value *Extract63 = Builder.CreateLShr(Shr18, Builder.getIntN(BitWidth, 32));
1057 Value *ExtractT64 = nullptr;
1058 if (FloatWidth > 80)
1059 ExtractT64 = Builder.CreateTrunc(Sub2, Builder.getInt64Ty());
1060 else
1061 ExtractT64 = Builder.CreateTrunc(Extract63, Builder.getInt32Ty());
1062 // Rounding usually keeps the exponent within its current magnitude and
1063 // overflow is rare. The False path is unlikely to be taken.
1064 Value *CondBrSwEpilog = Builder.CreateCondBr(PosOrNeg, IfEnd26, IfThen20);
1065 applyProfMetadataIfEnabled(CondBrSwEpilog, [&](Instruction *Inst) {
1066 Inst->setMetadata(
1067 LLVMContext::MD_prof,
1069 });
1070
1071 // if.then20
1072 Builder.SetInsertPoint(IfThen20);
1073 Value *Shr21 = nullptr;
1074 if (IsSigned)
1075 Shr21 = Builder.CreateAShr(Inc, Builder.getIntN(BitWidth, 3));
1076 else
1077 Shr21 = Builder.CreateLShr(Inc, Builder.getIntN(BitWidth, 3));
1078 Value *ExtractT = Builder.CreateTrunc(Shr21, Builder.getIntNTy(FloatWidth));
1079 Value *Extract = Builder.CreateLShr(Shr21, Builder.getIntN(BitWidth, 32));
1080 Value *ExtractT62 = nullptr;
1081 if (FloatWidth > 80)
1082 ExtractT62 = Builder.CreateTrunc(Sub1, Builder.getInt64Ty());
1083 else
1084 ExtractT62 = Builder.CreateTrunc(Extract, Builder.getInt32Ty());
1085 Builder.CreateBr(IfEnd26);
1086
1087 // if.else:
1088 Builder.SetInsertPoint(IfElse);
1089 Value *Sub24 = Builder.CreateAdd(
1090 FloatWidth == 128 ? Call : Cast,
1091 ConstantInt::getSigned(Builder.getIntNTy(BitWidthNew),
1092 -(int)(BitWidth - FPMantissaWidth - 1)));
1093 Value *ShProm25 = Builder.CreateZExt(Sub24, IntTy);
1094 Value *Shl26 = Builder.CreateShl(IsSigned ? Sub : IntVal,
1095 FloatWidth == 128 ? Sub24 : ShProm25);
1096 Value *ExtractT61 = Builder.CreateTrunc(Shl26, Builder.getIntNTy(FloatWidth));
1097 Value *Extract65 = Builder.CreateLShr(Shl26, Builder.getIntN(BitWidth, 32));
1098 Value *ExtractT66 = nullptr;
1099 if (FloatWidth > 80)
1100 ExtractT66 = Builder.CreateTrunc(Sub2, Builder.getInt64Ty());
1101 else
1102 ExtractT66 = Builder.CreateTrunc(Extract65, Builder.getInt32Ty());
1103 Builder.CreateBr(IfEnd26);
1104
1105 // if.end26:
1106 Builder.SetInsertPoint(IfEnd26);
1107 PHINode *AAddr1Off0 = Builder.CreatePHI(Builder.getIntNTy(FloatWidth), 3);
1108 AAddr1Off0->addIncoming(ExtractT, IfThen20);
1109 AAddr1Off0->addIncoming(ExtractT60, SwEpilog);
1110 AAddr1Off0->addIncoming(ExtractT61, IfElse);
1111 PHINode *AAddr1Off32 = nullptr;
1112 if (FloatWidth > 32) {
1113 AAddr1Off32 =
1114 Builder.CreatePHI(Builder.getIntNTy(FloatWidth > 80 ? 64 : 32), 3);
1115 AAddr1Off32->addIncoming(ExtractT62, IfThen20);
1116 AAddr1Off32->addIncoming(ExtractT64, SwEpilog);
1117 AAddr1Off32->addIncoming(ExtractT66, IfElse);
1118 }
1119 PHINode *E0 = nullptr;
1120 if (FloatWidth <= 80) {
1121 E0 = Builder.CreatePHI(Builder.getIntNTy(BitWidthNew), 3);
1122 E0->addIncoming(Sub1, IfThen20);
1123 E0->addIncoming(Sub2, SwEpilog);
1124 E0->addIncoming(Sub2, IfElse);
1125 }
1126 Value *And29 = nullptr;
1127 if (FloatWidth > 80) {
1128 Value *Temp2 = Builder.CreateShl(Builder.getIntN(BitWidth, 1),
1129 Builder.getIntN(BitWidth, 63));
1130 And29 = Builder.CreateAnd(Shr, Temp2, "and29");
1131 } else {
1132 Value *Conv28 = Builder.CreateTrunc(Shr, Builder.getInt32Ty());
1133 And29 = Builder.CreateAnd(
1134 Conv28, ConstantInt::get(Builder.getContext(), APInt::getSignMask(32)));
1135 }
1136 unsigned TempMod = FPMantissaWidth % 32;
1137 Value *And34 = nullptr;
1138 Value *Shl30 = nullptr;
1139 if (FloatWidth > 80) {
1140 TempMod += 32;
1141 Value *Add = Builder.CreateShl(AAddr1Off32, Builder.getInt64(TempMod));
1142 Shl30 = Builder.CreateAdd(
1143 Add, Builder.getInt64(((1ull << (62ull - TempMod)) - 1ull) << TempMod));
1144 And34 = Builder.CreateZExt(Shl30, Builder.getInt128Ty());
1145 } else {
1146 Value *Add = Builder.CreateShl(E0, Builder.getInt32(TempMod));
1147 Shl30 = Builder.CreateAdd(
1148 Add, Builder.getInt32(((1 << (30 - TempMod)) - 1) << TempMod));
1149 And34 = Builder.CreateAnd(FloatWidth > 32 ? AAddr1Off32 : AAddr1Off0,
1150 Builder.getInt32((1 << TempMod) - 1));
1151 }
1152 Value *Or35 = nullptr;
1153 if (FloatWidth > 80) {
1154 Value *And29Trunc = Builder.CreateTrunc(And29, Builder.getInt128Ty());
1155 Value *Or31 = Builder.CreateOr(And29Trunc, And34);
1156 Value *Or34 = Builder.CreateShl(Or31, Builder.getIntN(128, 64));
1157 Value *Temp3 = Builder.CreateShl(Builder.getIntN(128, 1),
1158 Builder.getIntN(128, FPMantissaWidth));
1159 Value *Temp4 = Builder.CreateSub(Temp3, Builder.getIntN(128, 1));
1160 Value *A6 = Builder.CreateAnd(AAddr1Off0, Temp4);
1161 Or35 = Builder.CreateOr(Or34, A6);
1162 } else {
1163 Value *Or31 = Builder.CreateOr(And34, And29);
1164 Or35 = Builder.CreateOr(IsSigned ? Or31 : And34, Shl30);
1165 }
1166 Value *A4 = nullptr;
1167 if (IToFP->getType()->isDoubleTy()) {
1168 Value *ZExt1 = Builder.CreateZExt(Or35, Builder.getIntNTy(FloatWidth));
1169 Value *Shl1 = Builder.CreateShl(ZExt1, Builder.getIntN(FloatWidth, 32));
1170 Value *And1 =
1171 Builder.CreateAnd(AAddr1Off0, Builder.getIntN(FloatWidth, 0xFFFFFFFF));
1172 Value *Or1 = Builder.CreateOr(Shl1, And1);
1173 A4 = Builder.CreateBitCast(Or1, IToFP->getType());
1174 } else if (IToFP->getType()->isX86_FP80Ty()) {
1175 Value *A40 =
1176 Builder.CreateBitCast(Or35, Type::getFP128Ty(Builder.getContext()));
1177 A4 = Builder.CreateFPTrunc(A40, IToFP->getType());
1178 } else if (IToFP->getType()->isHalfTy() || IToFP->getType()->isBFloatTy()) {
1179 // Deal with "half" situation. This is a workaround since we don't have
1180 // floattihf.c currently as referring.
1181 Value *A40 =
1182 Builder.CreateBitCast(Or35, Type::getFloatTy(Builder.getContext()));
1183 A4 = Builder.CreateFPTrunc(A40, IToFP->getType());
1184 } else // float type
1185 A4 = Builder.CreateBitCast(Or35, IToFP->getType());
1186
1187 // Sub2 is the unbiased exponent (the index of the top set bit in the input).
1188 // The exponent arithmetic above wraps to garbage instead of inf once it
1189 // overflows the exponent field, so saturate to a correctly-signed infinity
1190 // when Sub2 reaches 1 << (ExponentWidth - 1). Sub2 is at most BitWidth - 1,
1191 // so skip the check entirely when even that can't reach the threshold.
1192 // (Values that round *up* into inf, e.g. 2^n - 1, keep Sub2 = BitWidth - 1;
1193 // these are handled by the conversion's own rounding, not by this
1194 // saturation.)
1195 unsigned ExponentWidth = FloatWidth - FPMantissaWidth - 1;
1196 uint64_t MinInfExp = 1ULL << (ExponentWidth - 1);
1197 if (BitWidth - 1 >= MinInfExp) {
1198 Value *MinInfExpVal = Builder.getIntN(BitWidthNew, MinInfExp);
1199 Value *Overflow = Builder.CreateICmpUGE(Sub2, MinInfExpVal);
1200 Value *Inf = ConstantFP::getInfinity(IToFP->getType(), /*Negative=*/false);
1201 if (IsSigned) {
1202 Value *NegInf =
1203 ConstantFP::getInfinity(IToFP->getType(), /*Negative=*/true);
1204 Value *IsNeg =
1205 Builder.CreateICmpSLT(IntVal, ConstantInt::getNullValue(IntTy));
1206 Inf = Builder.CreateSelectWithUnknownProfile(IsNeg, NegInf, Inf,
1207 DEBUG_TYPE);
1208 }
1209 A4 = Builder.CreateSelect(Overflow, Inf, A4);
1210 // We consider overflow to be an unlikely case.
1212 Inst->setMetadata(
1213 LLVMContext::MD_prof,
1215 });
1216 }
1217 Builder.CreateBr(End);
1218
1219 // return:
1220 Builder.SetInsertPoint(End, End->begin());
1221 PHINode *Retval0 = Builder.CreatePHI(IToFP->getType(), 2);
1222 Retval0->addIncoming(A4, IfEnd26);
1223 Retval0->addIncoming(ConstantFP::getZero(IToFP->getType(), false), Entry);
1224
1225 IToFP->replaceAllUsesWith(Retval0);
1226 IToFP->dropAllReferences();
1227 IToFP->eraseFromParent();
1228}
1229
1232 VectorType *VTy = cast<FixedVectorType>(I->getType());
1233
1234 IRBuilder<> Builder(I);
1235
1236 unsigned NumElements = VTy->getElementCount().getFixedValue();
1237 Value *Result = PoisonValue::get(VTy);
1238 for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
1239 Value *Ext = Builder.CreateExtractElement(I->getOperand(0), Idx);
1240
1241 Value *NewOp = nullptr;
1242 if (auto *BinOp = dyn_cast<BinaryOperator>(I))
1243 NewOp = Builder.CreateBinOp(
1244 BinOp->getOpcode(), Ext,
1245 Builder.CreateExtractElement(I->getOperand(1), Idx));
1246 else if (auto *CastI = dyn_cast<CastInst>(I))
1247 NewOp = Builder.CreateCast(CastI->getOpcode(), Ext,
1248 I->getType()->getScalarType());
1249 else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1250 assert(II->getIntrinsicID() == Intrinsic::fptoui_sat ||
1251 II->getIntrinsicID() == Intrinsic::fptosi_sat);
1252 NewOp = Builder.CreateIntrinsic(I->getType()->getScalarType(),
1253 II->getIntrinsicID(), {Ext});
1254 } else
1255 llvm_unreachable("Unsupported instruction type");
1256
1257 Result = Builder.CreateInsertElement(Result, NewOp, Idx);
1258 if (auto *ScalarizedI = dyn_cast<Instruction>(NewOp)) {
1259 ScalarizedI->copyIRFlags(I, true);
1260 Worklist.push_back(ScalarizedI);
1261 }
1262 }
1263
1264 I->replaceAllUsesWith(Result);
1265 I->dropAllReferences();
1266 I->eraseFromParent();
1267}
1268
1271 if (I.getOperand(0)->getType()->isVectorTy())
1272 scalarize(&I, Worklist);
1273 else
1274 Worklist.push_back(&I);
1275}
1276
1277static bool runImpl(Function &F, const TargetLowering &TLI,
1278 const LibcallLoweringInfo &Libcalls, AssumptionCache *AC) {
1280
1281 unsigned MaxLegalFpConvertBitWidth =
1284 MaxLegalFpConvertBitWidth = ExpandFpConvertBits;
1285
1286 unsigned MaxLegalDivRemBitWidth = TLI.getMaxDivRemBitWidthSupported();
1288 MaxLegalDivRemBitWidth = ExpandDivRemBits;
1289
1290 bool DisableExpandLargeFp =
1291 MaxLegalFpConvertBitWidth >= IntegerType::MAX_INT_BITS;
1292 bool DisableExpandLargeDivRem =
1293 MaxLegalDivRemBitWidth >= IntegerType::MAX_INT_BITS;
1294 bool DisableFrem = !FRemExpander::shouldExpandAnyFremType(TLI, Libcalls);
1295
1296 if (DisableExpandLargeFp && DisableFrem && DisableExpandLargeDivRem)
1297 return false;
1298
1299 auto ShouldHandleInst = [&](Instruction &I) {
1300 Type *Ty = I.getType();
1301 // TODO: This pass doesn't handle scalable vectors.
1302 if (Ty->isScalableTy())
1303 return false;
1304
1305 switch (I.getOpcode()) {
1306 case Instruction::FRem:
1307 return !DisableFrem &&
1308 FRemExpander::shouldExpandFremType(TLI, Libcalls, Ty);
1309 case Instruction::FPToUI:
1310 case Instruction::FPToSI:
1311 return !DisableExpandLargeFp &&
1312 cast<IntegerType>(Ty->getScalarType())->getIntegerBitWidth() >
1313 MaxLegalFpConvertBitWidth;
1314 case Instruction::UIToFP:
1315 case Instruction::SIToFP:
1316 return !DisableExpandLargeFp &&
1317 cast<IntegerType>(I.getOperand(0)->getType()->getScalarType())
1318 ->getIntegerBitWidth() > MaxLegalFpConvertBitWidth;
1319 case Instruction::UDiv:
1320 case Instruction::SDiv:
1321 case Instruction::URem:
1322 case Instruction::SRem:
1323 // Power-of-2 divisors are handled inside the expansion (via efficient
1324 // shift/mask sequences) rather than being excluded here, so that
1325 // backends that cannot lower wide div/rem even for powers of two
1326 // (e.g. when DAGCombiner is disabled) still get valid lowered code.
1327 return !DisableExpandLargeDivRem &&
1328 cast<IntegerType>(Ty->getScalarType())->getIntegerBitWidth() >
1329 MaxLegalDivRemBitWidth;
1330 case Instruction::Call: {
1331 auto *II = dyn_cast<IntrinsicInst>(&I);
1332 if (II && (II->getIntrinsicID() == Intrinsic::fptoui_sat ||
1333 II->getIntrinsicID() == Intrinsic::fptosi_sat)) {
1334 return !DisableExpandLargeFp &&
1335 cast<IntegerType>(Ty->getScalarType())->getIntegerBitWidth() >
1336 MaxLegalFpConvertBitWidth;
1337 }
1338 return false;
1339 }
1340 }
1341
1342 return false;
1343 };
1344
1345 bool Modified = false;
1346 for (auto It = inst_begin(&F), End = inst_end(F); It != End;) {
1347 Instruction &I = *It++;
1348 if (!ShouldHandleInst(I))
1349 continue;
1350
1351 addToWorklist(I, Worklist);
1352 Modified = true;
1353 }
1354
1355 while (!Worklist.empty()) {
1356 Instruction *I = Worklist.pop_back_val();
1357
1358 switch (I->getOpcode()) {
1359 case Instruction::FRem: {
1360 auto SQ = [&]() -> std::optional<SimplifyQuery> {
1361 if (AC) {
1362 auto Res = std::make_optional<SimplifyQuery>(
1363 I->getModule()->getDataLayout(), I);
1364 Res->AC = AC;
1365 return Res;
1366 }
1367 return {};
1368 }();
1369
1371 break;
1372 }
1373
1374 case Instruction::FPToUI:
1375 expandFPToI(I, /*IsSaturating=*/false, /*IsSigned=*/false);
1376 break;
1377 case Instruction::FPToSI:
1378 expandFPToI(I, /*IsSaturating=*/false, /*IsSigned=*/true);
1379 break;
1380
1381 case Instruction::UIToFP:
1382 case Instruction::SIToFP:
1383 expandIToFP(I);
1384 break;
1385
1386 case Instruction::UDiv:
1387 case Instruction::SDiv:
1388 case Instruction::URem:
1389 case Instruction::SRem: {
1390 auto *BO = cast<BinaryOperator>(I);
1391 // TODO: isConstantPowerOfTwo does not handle vector constants, so
1392 // vector div/rem by a power-of-2 splat goes through the generic path.
1393 if (isConstantPowerOfTwo(BO->getOperand(1), isSigned(BO->getOpcode()))) {
1394 expandPow2DivRem(BO);
1395 } else {
1396 unsigned Opc = BO->getOpcode();
1397 if (Opc == Instruction::UDiv || Opc == Instruction::SDiv)
1398 expandDivision(BO);
1399 else
1400 expandRemainder(BO);
1401 }
1402 break;
1403 }
1404 case Instruction::Call: {
1405 auto *II = cast<IntrinsicInst>(I);
1406 assert(II->getIntrinsicID() == Intrinsic::fptoui_sat ||
1407 II->getIntrinsicID() == Intrinsic::fptosi_sat);
1408 expandFPToI(I, /*IsSaturating=*/true,
1409 /*IsSigned=*/II->getIntrinsicID() == Intrinsic::fptosi_sat);
1410 break;
1411 }
1412 }
1413 }
1414
1415 return Modified;
1416}
1417
1418namespace {
1419class ExpandIRInstsLegacyPass : public FunctionPass {
1420 CodeGenOptLevel OptLevel;
1421
1422public:
1423 static char ID;
1424
1425 ExpandIRInstsLegacyPass(CodeGenOptLevel OptLevel)
1426 : FunctionPass(ID), OptLevel(OptLevel) {}
1427
1428 ExpandIRInstsLegacyPass() : ExpandIRInstsLegacyPass(CodeGenOptLevel::None) {}
1429
1430 bool runOnFunction(Function &F) override {
1431 auto *TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
1432 const TargetSubtargetInfo *Subtarget = TM->getSubtargetImpl(F);
1433 auto *TLI = Subtarget->getTargetLowering();
1434 AssumptionCache *AC = nullptr;
1435
1436 const LibcallLoweringInfo &Libcalls =
1437 getAnalysis<LibcallLoweringInfoWrapper>().getLibcallLowering(
1438 *F.getParent(), *Subtarget);
1439
1440 if (OptLevel != CodeGenOptLevel::None && !F.hasOptNone())
1441 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1442 return runImpl(F, *TLI, Libcalls, AC);
1443 }
1444
1445 void getAnalysisUsage(AnalysisUsage &AU) const override {
1446 AU.addRequired<LibcallLoweringInfoWrapper>();
1447 AU.addRequired<TargetPassConfig>();
1448 if (OptLevel != CodeGenOptLevel::None)
1449 AU.addRequired<AssumptionCacheTracker>();
1450 AU.addPreserved<AAResultsWrapperPass>();
1451 AU.addPreserved<GlobalsAAWrapperPass>();
1452 AU.addRequired<LibcallLoweringInfoWrapper>();
1453 }
1454};
1455} // namespace
1456
1458 CodeGenOptLevel OptLevel)
1459 : TM(&TM), OptLevel(OptLevel) {}
1460
1462 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
1463 static_cast<PassInfoMixin<ExpandIRInstsPass> *>(this)->printPipeline(
1464 OS, MapClassName2PassName);
1465 OS << '<';
1466 OS << "O" << (int)OptLevel;
1467 OS << '>';
1468}
1469
1472 const TargetSubtargetInfo *STI = TM->getSubtargetImpl(F);
1473 auto &TLI = *STI->getTargetLowering();
1474 AssumptionCache *AC = nullptr;
1475 if (OptLevel != CodeGenOptLevel::None)
1476 AC = &FAM.getResult<AssumptionAnalysis>(F);
1477
1478 auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
1479
1480 const ModuleLibcallLoweringInfo *LibcallLowering =
1481 MAMProxy.getCachedResult<LibcallLoweringModuleAnalysis>(*F.getParent());
1482
1483 if (!LibcallLowering) {
1484 F.getContext().emitError("'" + LibcallLoweringModuleAnalysis::name() +
1485 "' analysis required");
1486 return PreservedAnalyses::all();
1487 }
1488
1489 const LibcallLoweringInfo &Libcalls =
1490 getLibcallLowering(*LibcallLowering, *STI);
1491
1492 return runImpl(F, TLI, Libcalls, AC) ? PreservedAnalyses::none()
1494}
1495
1496char ExpandIRInstsLegacyPass::ID = 0;
1497INITIALIZE_PASS_BEGIN(ExpandIRInstsLegacyPass, "expand-ir-insts",
1498 "Expand certain fp instructions", false, false)
1500INITIALIZE_PASS_END(ExpandIRInstsLegacyPass, "expand-ir-insts",
1501 "Expand IR instructions", false, false)
1502
1504 return new ExpandIRInstsLegacyPass(OptLevel);
1505}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
static bool runOnFunction(Function &F, bool PostInlining)
static bool expandFRem(BinaryOperator &I, std::optional< SimplifyQuery > &SQ)
static void expandIToFP(Instruction *IToFP)
Generate code to convert a fp number to integer, replacing S(U)IToFP with the generated code.
static cl::opt< unsigned > ExpandDivRemBits("expand-div-rem-bits", cl::Hidden, cl::init(IntegerType::MAX_INT_BITS), cl::desc("div and rem instructions on integers with " "more than <N> bits are expanded."))
static void expandPow2DivRem(BinaryOperator *BO)
Expand division or remainder by a power-of-2 constant.
static bool isSigned(unsigned Opcode)
static void addToWorklist(Instruction &I, SmallVector< Instruction *, 4 > &Worklist)
static Value * addSignedBias(IRBuilder<> &Builder, Value *X, unsigned BitWidth, unsigned ShiftAmt)
For signed div/rem by a power of 2, compute the bias-adjusted dividend: Sign = ashr X,...
static cl::opt< unsigned > ExpandFpConvertBits("expand-fp-convert-bits", cl::Hidden, cl::init(IntegerType::MAX_INT_BITS), cl::desc("fp convert instructions on integers with " "more than <N> bits are expanded."))
static void expandFPToI(Instruction *FPToI, bool IsSaturating, bool IsSigned)
Generate code to convert a fp number to integer, replacing FPToS(U)I with the generated code.
static bool isConstantPowerOfTwo(Value *V, bool SignedOp)
static void scalarize(Instruction *I, SmallVectorImpl< Instruction * > &Worklist)
#define DEBUG_TYPE
This is the interface for a simple mod/ref and alias analysis over globals.
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
Function * Fun
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file contains the declarations for profiling metadata utility functions.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
Target-Independent Code Generator Pass Configuration Options pass.
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:226
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:206
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:326
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1660
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:216
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:437
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:303
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:236
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:446
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
BinaryOps getOpcode() const
Definition InstrTypes.h:409
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
static LLVM_ABI ConstantFP * getZero(Type *Ty, bool Negative=false)
static LLVM_ABI ConstantFP * getQNaN(Type *Ty, bool Negative=false, APInt *Payload=nullptr)
static LLVM_ABI ConstantFP * getInfinity(Type *Ty, bool Negative=false)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:135
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI ExpandIRInstsPass(const TargetMachine &TM, CodeGenOptLevel OptLevel)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
void setAllowContract(bool B=true)
Definition FMF.h:90
bool noInfs() const
Definition FMF.h:66
void setAllowReciprocal(bool B=true)
Definition FMF.h:87
bool approxFunc() const
Definition FMF.h:70
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
void setNoInfs(bool B=true)
Definition FMF.h:81
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Module * getParent()
Get the module that this global value is contained inside of...
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2893
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Class to represent integer types.
@ MAX_INT_BITS
Maximum number of bits that can be specified.
Tracks which library functions to use for a particular subtarget or function.
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition MDBuilder.cpp:38
LLVM_ABI MDNode * createLikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards true destination.
Definition MDBuilder.cpp:43
LLVM_ABI MDNode * createUnlikelyBranchWeights()
Return metadata containing two branch weights, with significant bias towards false destination.
Definition MDBuilder.cpp:48
Records a mapping from an opaque lowering context to its LibcallLoweringInfo.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Multiway switch.
unsigned getMaxDivRemBitWidthSupported() const
Returns the size in bits of the maximum div/rem the backend supports.
unsigned getMaxLargeFPConvertBitWidthSupported() const
Returns the size in bits of the maximum fp to/from int conversion the backend supports.
LegalizeAction getOperationAction(unsigned Op, EVT VT) const
Return how this operation should be treated: either it is legal, needs to be promoted to a larger siz...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
Primary interface to the complete machine description for the target machine.
TargetSubtargetInfo - Generic base class for all target subtargets.
virtual const TargetLowering * getTargetLowering() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
bool isX86_FP80Ty() const
Return true if this is x86 long double.
Definition Type.h:161
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition Type.h:147
static LLVM_ABI Type * getFP128Ty(LLVMContext &C)
Definition Type.cpp:291
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:144
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition Type.h:158
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:286
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Definition Type.cpp:237
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
void dropAllReferences()
Drop all references to operands.
Definition User.h:324
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool expandDivision(BinaryOperator *Div)
Generate code to divide two integers, replacing Div with the generated code.
LLVM_ABI cl::opt< bool > ProfcheckDisableMetadataFixes
Definition LoopInfo.cpp:60
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void setExplicitlyUnknownBranchWeightsIfProfiled(Instruction &I, StringRef PassName, const Function *F=nullptr)
Like setExplicitlyUnknownBranchWeights(...), but only sets unknown branch weights in the new instruct...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
LLVM_ABI void applyProfMetadataIfEnabled(Value *V, llvm::function_ref< void(Instruction *)> setMetadataCallback)
inst_iterator inst_begin(Function *F)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition MathExtras.h:386
LLVM_ABI FunctionPass * createExpandIRInstsPass(CodeGenOptLevel)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
LLVM_ABI const LibcallLoweringInfo & getLibcallLowering(const ModuleLibcallLoweringInfo &ModuleInfo, const TargetSubtargetInfo &Subtarget)
Resolve the LibcallLoweringInfo for Subtarget from the module-level ModuleInfo, applying the subtarge...
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore, Instruction **ThenTerm, Instruction **ElseTerm, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr)
SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, but also creates the ElseBlock...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:149
inst_iterator inst_end(Function *F)
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ Xor
Bitwise or logical XOR of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
LLVM_ABI bool expandRemainder(BinaryOperator *Rem)
Generate code to calculate the remainder of two integers, replacing Rem with the generated code.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
Matching combinators.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:89