LLVM 22.0.0git
InstCombineCasts.cpp
Go to the documentation of this file.
1//===- InstCombineCasts.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 visit functions for cast operations.
10//
11//===----------------------------------------------------------------------===//
12
13#include "InstCombineInternal.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/SetVector.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DebugInfo.h"
20#include "llvm/IR/Value.h"
23#include <optional>
24
25using namespace llvm;
26using namespace PatternMatch;
27
28#define DEBUG_TYPE "instcombine"
29
30/// Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns
31/// true for, actually insert the code to evaluate the expression.
33 bool isSigned) {
36
37 // Otherwise, it must be an instruction.
39 Instruction *Res = nullptr;
40 unsigned Opc = I->getOpcode();
41 switch (Opc) {
42 case Instruction::Add:
43 case Instruction::Sub:
44 case Instruction::Mul:
45 case Instruction::And:
46 case Instruction::Or:
47 case Instruction::Xor:
48 case Instruction::AShr:
49 case Instruction::LShr:
50 case Instruction::Shl:
51 case Instruction::UDiv:
52 case Instruction::URem: {
53 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
54 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
56 if (Opc == Instruction::LShr || Opc == Instruction::AShr)
57 Res->setIsExact(I->isExact());
58 break;
59 }
60 case Instruction::Trunc:
61 case Instruction::ZExt:
62 case Instruction::SExt:
63 // If the source type of the cast is the type we're trying for then we can
64 // just return the source. There's no need to insert it because it is not
65 // new.
66 if (I->getOperand(0)->getType() == Ty)
67 return I->getOperand(0);
68
69 // Otherwise, must be the same type of cast, so just reinsert a new one.
70 // This also handles the case of zext(trunc(x)) -> zext(x).
71 Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty,
72 Opc == Instruction::SExt);
73 break;
74 case Instruction::Select: {
75 Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
76 Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned);
77 Res = SelectInst::Create(I->getOperand(0), True, False);
78 break;
79 }
80 case Instruction::PHI: {
81 PHINode *OPN = cast<PHINode>(I);
83 for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) {
84 Value *V =
86 NPN->addIncoming(V, OPN->getIncomingBlock(i));
87 }
88 Res = NPN;
89 break;
90 }
91 case Instruction::FPToUI:
92 case Instruction::FPToSI:
93 Res = CastInst::Create(
94 static_cast<Instruction::CastOps>(Opc), I->getOperand(0), Ty);
95 break;
96 case Instruction::Call:
98 switch (II->getIntrinsicID()) {
99 default:
100 llvm_unreachable("Unsupported call!");
101 case Intrinsic::vscale: {
103 I->getModule(), Intrinsic::vscale, {Ty});
104 Res = CallInst::Create(Fn->getFunctionType(), Fn);
105 break;
106 }
107 }
108 }
109 break;
110 case Instruction::ShuffleVector: {
111 auto *ScalarTy = cast<VectorType>(Ty)->getElementType();
112 auto *VTy = cast<VectorType>(I->getOperand(0)->getType());
113 auto *FixedTy = VectorType::get(ScalarTy, VTy->getElementCount());
114 Value *Op0 = EvaluateInDifferentType(I->getOperand(0), FixedTy, isSigned);
115 Value *Op1 = EvaluateInDifferentType(I->getOperand(1), FixedTy, isSigned);
116 Res = new ShuffleVectorInst(Op0, Op1,
117 cast<ShuffleVectorInst>(I)->getShuffleMask());
118 break;
119 }
120 default:
121 // TODO: Can handle more cases here.
122 llvm_unreachable("Unreachable!");
123 }
124
125 Res->takeName(I);
126 return InsertNewInstWith(Res, I->getIterator());
127}
128
130InstCombinerImpl::isEliminableCastPair(const CastInst *CI1,
131 const CastInst *CI2) {
132 Type *SrcTy = CI1->getSrcTy();
133 Type *MidTy = CI1->getDestTy();
134 Type *DstTy = CI2->getDestTy();
135
136 Instruction::CastOps firstOp = CI1->getOpcode();
137 Instruction::CastOps secondOp = CI2->getOpcode();
138 Type *SrcIntPtrTy =
139 SrcTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(SrcTy) : nullptr;
140 Type *DstIntPtrTy =
141 DstTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(DstTy) : nullptr;
142 unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy,
143 DstTy, &DL);
144
145 // We don't want to form an inttoptr or ptrtoint that converts to an integer
146 // type that differs from the pointer size.
147 if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) ||
148 (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy))
149 Res = 0;
150
151 return Instruction::CastOps(Res);
152}
153
154/// Implement the transforms common to all CastInst visitors.
156 Value *Src = CI.getOperand(0);
157 Type *Ty = CI.getType();
158
159 if (Value *Res =
160 simplifyCastInst(CI.getOpcode(), Src, Ty, SQ.getWithInstruction(&CI)))
161 return replaceInstUsesWith(CI, Res);
162
163 // Try to eliminate a cast of a cast.
164 if (auto *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast
165 if (Instruction::CastOps NewOpc = isEliminableCastPair(CSrc, &CI)) {
166 // The first cast (CSrc) is eliminable so we need to fix up or replace
167 // the second cast (CI). CSrc will then have a good chance of being dead.
168 auto *Res = CastInst::Create(NewOpc, CSrc->getOperand(0), Ty);
169 // Point debug users of the dying cast to the new one.
170 if (CSrc->hasOneUse())
171 replaceAllDbgUsesWith(*CSrc, *Res, CI, DT);
172 return Res;
173 }
174 }
175
176 if (auto *Sel = dyn_cast<SelectInst>(Src)) {
177 // We are casting a select. Try to fold the cast into the select if the
178 // select does not have a compare instruction with matching operand types
179 // or the select is likely better done in a narrow type.
180 // Creating a select with operands that are different sizes than its
181 // condition may inhibit other folds and lead to worse codegen.
182 auto *Cmp = dyn_cast<CmpInst>(Sel->getCondition());
183 if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType() ||
184 (CI.getOpcode() == Instruction::Trunc &&
185 shouldChangeType(CI.getSrcTy(), CI.getType()))) {
186
187 // If it's a bitcast involving vectors, make sure it has the same number
188 // of elements on both sides.
189 if (CI.getOpcode() != Instruction::BitCast ||
191 if (Instruction *NV = FoldOpIntoSelect(CI, Sel)) {
192 replaceAllDbgUsesWith(*Sel, *NV, CI, DT);
193 return NV;
194 }
195 }
196 }
197 }
198
199 // If we are casting a PHI, then fold the cast into the PHI.
200 if (auto *PN = dyn_cast<PHINode>(Src)) {
201 // Don't do this if it would create a PHI node with an illegal type from a
202 // legal type.
203 if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() ||
204 shouldChangeType(CI.getSrcTy(), CI.getType()))
205 if (Instruction *NV = foldOpIntoPhi(CI, PN))
206 return NV;
207 }
208
209 // Canonicalize a unary shuffle after the cast if neither operation changes
210 // the size or element size of the input vector.
211 // TODO: We could allow size-changing ops if that doesn't harm codegen.
212 // cast (shuffle X, Mask) --> shuffle (cast X), Mask
213 Value *X;
214 ArrayRef<int> Mask;
215 if (match(Src, m_OneUse(m_Shuffle(m_Value(X), m_Undef(), m_Mask(Mask))))) {
216 // TODO: Allow scalable vectors?
217 auto *SrcTy = dyn_cast<FixedVectorType>(X->getType());
218 auto *DestTy = dyn_cast<FixedVectorType>(Ty);
219 if (SrcTy && DestTy &&
220 SrcTy->getNumElements() == DestTy->getNumElements() &&
221 SrcTy->getPrimitiveSizeInBits() == DestTy->getPrimitiveSizeInBits()) {
222 Value *CastX = Builder.CreateCast(CI.getOpcode(), X, DestTy);
223 return new ShuffleVectorInst(CastX, Mask);
224 }
225 }
226
227 return nullptr;
228}
229
230/// Constants and extensions/truncates from the destination type are always
231/// free to be evaluated in that type. This is a helper for canEvaluate*.
232static bool canAlwaysEvaluateInType(Value *V, Type *Ty) {
233 if (isa<Constant>(V))
234 return match(V, m_ImmConstant());
235
236 Value *X;
237 if ((match(V, m_ZExtOrSExt(m_Value(X))) || match(V, m_Trunc(m_Value(X)))) &&
238 X->getType() == Ty)
239 return true;
240
241 return false;
242}
243
244/// Filter out values that we can not evaluate in the destination type for free.
245/// This is a helper for canEvaluate*.
246static bool canNotEvaluateInType(Value *V, Type *Ty) {
247 if (!isa<Instruction>(V))
248 return true;
249 // We don't extend or shrink something that has multiple uses -- doing so
250 // would require duplicating the instruction which isn't profitable.
251 if (!V->hasOneUse())
252 return true;
253
254 return false;
255}
256
257/// Return true if we can evaluate the specified expression tree as type Ty
258/// instead of its larger type, and arrive with the same value.
259/// This is used by code that tries to eliminate truncates.
260///
261/// Ty will always be a type smaller than V. We should return true if trunc(V)
262/// can be computed by computing V in the smaller type. If V is an instruction,
263/// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only
264/// makes sense if x and y can be efficiently truncated.
265///
266/// This function works on both vectors and scalars.
267///
269 Instruction *CxtI) {
270 if (canAlwaysEvaluateInType(V, Ty))
271 return true;
272 if (canNotEvaluateInType(V, Ty))
273 return false;
274
275 auto *I = cast<Instruction>(V);
276 Type *OrigTy = V->getType();
277 switch (I->getOpcode()) {
278 case Instruction::Add:
279 case Instruction::Sub:
280 case Instruction::Mul:
281 case Instruction::And:
282 case Instruction::Or:
283 case Instruction::Xor:
284 // These operators can all arbitrarily be extended or truncated.
285 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
286 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
287
288 case Instruction::UDiv:
289 case Instruction::URem: {
290 // UDiv and URem can be truncated if all the truncated bits are zero.
291 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
292 uint32_t BitWidth = Ty->getScalarSizeInBits();
293 assert(BitWidth < OrigBitWidth && "Unexpected bitwidths!");
294 APInt Mask = APInt::getBitsSetFrom(OrigBitWidth, BitWidth);
295 // Do not preserve the original context instruction. Simplifying div/rem
296 // based on later context may introduce a trap.
297 if (IC.MaskedValueIsZero(I->getOperand(0), Mask, I) &&
298 IC.MaskedValueIsZero(I->getOperand(1), Mask, I)) {
299 return canEvaluateTruncated(I->getOperand(0), Ty, IC, I) &&
300 canEvaluateTruncated(I->getOperand(1), Ty, IC, I);
301 }
302 break;
303 }
304 case Instruction::Shl: {
305 // If we are truncating the result of this SHL, and if it's a shift of an
306 // inrange amount, we can always perform a SHL in a smaller type.
307 uint32_t BitWidth = Ty->getScalarSizeInBits();
308 KnownBits AmtKnownBits =
309 llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout());
310 if (AmtKnownBits.getMaxValue().ult(BitWidth))
311 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
312 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
313 break;
314 }
315 case Instruction::LShr: {
316 // If this is a truncate of a logical shr, we can truncate it to a smaller
317 // lshr iff we know that the bits we would otherwise be shifting in are
318 // already zeros.
319 // TODO: It is enough to check that the bits we would be shifting in are
320 // zero - use AmtKnownBits.getMaxValue().
321 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
322 uint32_t BitWidth = Ty->getScalarSizeInBits();
323 KnownBits AmtKnownBits = IC.computeKnownBits(I->getOperand(1), CxtI);
324 APInt MaxShiftAmt = AmtKnownBits.getMaxValue();
325 APInt ShiftedBits = APInt::getBitsSetFrom(OrigBitWidth, BitWidth);
326 if (MaxShiftAmt.ult(BitWidth)) {
327 // If the only user is a trunc then we can narrow the shift if any new
328 // MSBs are not going to be used.
329 if (auto *Trunc = dyn_cast<TruncInst>(V->user_back())) {
330 auto DemandedBits = Trunc->getType()->getScalarSizeInBits();
331 if ((MaxShiftAmt + DemandedBits).ule(BitWidth))
332 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
333 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
334 }
335 if (IC.MaskedValueIsZero(I->getOperand(0), ShiftedBits, CxtI))
336 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
337 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
338 }
339 break;
340 }
341 case Instruction::AShr: {
342 // If this is a truncate of an arithmetic shr, we can truncate it to a
343 // smaller ashr iff we know that all the bits from the sign bit of the
344 // original type and the sign bit of the truncate type are similar.
345 // TODO: It is enough to check that the bits we would be shifting in are
346 // similar to sign bit of the truncate type.
347 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits();
348 uint32_t BitWidth = Ty->getScalarSizeInBits();
349 KnownBits AmtKnownBits =
350 llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout());
351 unsigned ShiftedBits = OrigBitWidth - BitWidth;
352 if (AmtKnownBits.getMaxValue().ult(BitWidth) &&
353 ShiftedBits < IC.ComputeNumSignBits(I->getOperand(0), CxtI))
354 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
355 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
356 break;
357 }
358 case Instruction::Trunc:
359 // trunc(trunc(x)) -> trunc(x)
360 return true;
361 case Instruction::ZExt:
362 case Instruction::SExt:
363 // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest
364 // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest
365 return true;
366 case Instruction::Select: {
368 return canEvaluateTruncated(SI->getTrueValue(), Ty, IC, CxtI) &&
369 canEvaluateTruncated(SI->getFalseValue(), Ty, IC, CxtI);
370 }
371 case Instruction::PHI: {
372 // We can change a phi if we can change all operands. Note that we never
373 // get into trouble with cyclic PHIs here because we only consider
374 // instructions with a single use.
375 PHINode *PN = cast<PHINode>(I);
376 for (Value *IncValue : PN->incoming_values())
377 if (!canEvaluateTruncated(IncValue, Ty, IC, CxtI))
378 return false;
379 return true;
380 }
381 case Instruction::FPToUI:
382 case Instruction::FPToSI: {
383 // If the integer type can hold the max FP value, it is safe to cast
384 // directly to that type. Otherwise, we may create poison via overflow
385 // that did not exist in the original code.
386 Type *InputTy = I->getOperand(0)->getType()->getScalarType();
387 const fltSemantics &Semantics = InputTy->getFltSemantics();
388 uint32_t MinBitWidth =
390 I->getOpcode() == Instruction::FPToSI);
391 return Ty->getScalarSizeInBits() >= MinBitWidth;
392 }
393 case Instruction::ShuffleVector:
394 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) &&
395 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI);
396 default:
397 // TODO: Can handle more cases here.
398 break;
399 }
400
401 return false;
402}
403
404/// Given a vector that is bitcast to an integer, optionally logically
405/// right-shifted, and truncated, convert it to an extractelement.
406/// Example (big endian):
407/// trunc (lshr (bitcast <4 x i32> %X to i128), 32) to i32
408/// --->
409/// extractelement <4 x i32> %X, 1
411 InstCombinerImpl &IC) {
412 Value *TruncOp = Trunc.getOperand(0);
413 Type *DestType = Trunc.getType();
414 if (!TruncOp->hasOneUse() || !isa<IntegerType>(DestType))
415 return nullptr;
416
417 Value *VecInput = nullptr;
418 ConstantInt *ShiftVal = nullptr;
419 if (!match(TruncOp, m_CombineOr(m_BitCast(m_Value(VecInput)),
420 m_LShr(m_BitCast(m_Value(VecInput)),
421 m_ConstantInt(ShiftVal)))) ||
422 !isa<VectorType>(VecInput->getType()))
423 return nullptr;
424
425 VectorType *VecType = cast<VectorType>(VecInput->getType());
426 unsigned VecWidth = VecType->getPrimitiveSizeInBits();
427 unsigned DestWidth = DestType->getPrimitiveSizeInBits();
428 unsigned ShiftAmount = ShiftVal ? ShiftVal->getZExtValue() : 0;
429
430 if ((VecWidth % DestWidth != 0) || (ShiftAmount % DestWidth != 0))
431 return nullptr;
432
433 // If the element type of the vector doesn't match the result type,
434 // bitcast it to a vector type that we can extract from.
435 unsigned NumVecElts = VecWidth / DestWidth;
436 if (VecType->getElementType() != DestType) {
437 VecType = FixedVectorType::get(DestType, NumVecElts);
438 VecInput = IC.Builder.CreateBitCast(VecInput, VecType, "bc");
439 }
440
441 unsigned Elt = ShiftAmount / DestWidth;
442 if (IC.getDataLayout().isBigEndian())
443 Elt = NumVecElts - 1 - Elt;
444
445 return ExtractElementInst::Create(VecInput, IC.Builder.getInt32(Elt));
446}
447
448/// Whenever an element is extracted from a vector, optionally shifted down, and
449/// then truncated, canonicalize by converting it to a bitcast followed by an
450/// extractelement.
451///
452/// Examples (little endian):
453/// trunc (extractelement <4 x i64> %X, 0) to i32
454/// --->
455/// extractelement <8 x i32> (bitcast <4 x i64> %X to <8 x i32>), i32 0
456///
457/// trunc (lshr (extractelement <4 x i32> %X, 0), 8) to i8
458/// --->
459/// extractelement <16 x i8> (bitcast <4 x i32> %X to <16 x i8>), i32 1
461 InstCombinerImpl &IC) {
462 Value *Src = Trunc.getOperand(0);
463 Type *SrcType = Src->getType();
464 Type *DstType = Trunc.getType();
465
466 // Only attempt this if we have simple aliasing of the vector elements.
467 // A badly fit destination size would result in an invalid cast.
468 unsigned SrcBits = SrcType->getScalarSizeInBits();
469 unsigned DstBits = DstType->getScalarSizeInBits();
470 unsigned TruncRatio = SrcBits / DstBits;
471 if ((SrcBits % DstBits) != 0)
472 return nullptr;
473
474 Value *VecOp;
475 ConstantInt *Cst;
476 const APInt *ShiftAmount = nullptr;
477 if (!match(Src, m_OneUse(m_ExtractElt(m_Value(VecOp), m_ConstantInt(Cst)))) &&
478 !match(Src,
480 m_APInt(ShiftAmount)))))
481 return nullptr;
482
483 auto *VecOpTy = cast<VectorType>(VecOp->getType());
484 auto VecElts = VecOpTy->getElementCount();
485
486 uint64_t BitCastNumElts = VecElts.getKnownMinValue() * TruncRatio;
487 uint64_t VecOpIdx = Cst->getZExtValue();
488 uint64_t NewIdx = IC.getDataLayout().isBigEndian()
489 ? (VecOpIdx + 1) * TruncRatio - 1
490 : VecOpIdx * TruncRatio;
491
492 // Adjust index by the whole number of truncated elements.
493 if (ShiftAmount) {
494 // Check shift amount is in range and shifts a whole number of truncated
495 // elements.
496 if (ShiftAmount->uge(SrcBits) || ShiftAmount->urem(DstBits) != 0)
497 return nullptr;
498
499 uint64_t IdxOfs = ShiftAmount->udiv(DstBits).getZExtValue();
500 NewIdx = IC.getDataLayout().isBigEndian() ? (NewIdx - IdxOfs)
501 : (NewIdx + IdxOfs);
502 }
503
504 assert(BitCastNumElts <= std::numeric_limits<uint32_t>::max() &&
505 NewIdx <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
506
507 auto *BitCastTo =
508 VectorType::get(DstType, BitCastNumElts, VecElts.isScalable());
509 Value *BitCast = IC.Builder.CreateBitCast(VecOp, BitCastTo);
510 return ExtractElementInst::Create(BitCast, IC.Builder.getInt32(NewIdx));
511}
512
513/// Funnel/Rotate left/right may occur in a wider type than necessary because of
514/// type promotion rules. Try to narrow the inputs and convert to funnel shift.
515Instruction *InstCombinerImpl::narrowFunnelShift(TruncInst &Trunc) {
516 assert((isa<VectorType>(Trunc.getSrcTy()) ||
517 shouldChangeType(Trunc.getSrcTy(), Trunc.getType())) &&
518 "Don't narrow to an illegal scalar type");
519
520 // Bail out on strange types. It is possible to handle some of these patterns
521 // even with non-power-of-2 sizes, but it is not a likely scenario.
522 Type *DestTy = Trunc.getType();
523 unsigned NarrowWidth = DestTy->getScalarSizeInBits();
524 unsigned WideWidth = Trunc.getSrcTy()->getScalarSizeInBits();
525 if (!isPowerOf2_32(NarrowWidth))
526 return nullptr;
527
528 // First, find an or'd pair of opposite shifts:
529 // trunc (or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1))
530 BinaryOperator *Or0, *Or1;
531 if (!match(Trunc.getOperand(0), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1)))))
532 return nullptr;
533
534 Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1;
535 if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) ||
536 !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) ||
537 Or0->getOpcode() == Or1->getOpcode())
538 return nullptr;
539
540 // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)).
541 if (Or0->getOpcode() == BinaryOperator::LShr) {
542 std::swap(Or0, Or1);
543 std::swap(ShVal0, ShVal1);
544 std::swap(ShAmt0, ShAmt1);
545 }
546 assert(Or0->getOpcode() == BinaryOperator::Shl &&
547 Or1->getOpcode() == BinaryOperator::LShr &&
548 "Illegal or(shift,shift) pair");
549
550 // Match the shift amount operands for a funnel/rotate pattern. This always
551 // matches a subtraction on the R operand.
552 auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * {
553 // The shift amounts may add up to the narrow bit width:
554 // (shl ShVal0, L) | (lshr ShVal1, Width - L)
555 // If this is a funnel shift (different operands are shifted), then the
556 // shift amount can not over-shift (create poison) in the narrow type.
557 unsigned MaxShiftAmountWidth = Log2_32(NarrowWidth);
558 APInt HiBitMask = ~APInt::getLowBitsSet(WideWidth, MaxShiftAmountWidth);
559 if (ShVal0 == ShVal1 || MaskedValueIsZero(L, HiBitMask))
560 if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L)))))
561 return L;
562
563 // The following patterns currently only work for rotation patterns.
564 // TODO: Add more general funnel-shift compatible patterns.
565 if (ShVal0 != ShVal1)
566 return nullptr;
567
568 // The shift amount may be masked with negation:
569 // (shl ShVal0, (X & (Width - 1))) | (lshr ShVal1, ((-X) & (Width - 1)))
570 Value *X;
571 unsigned Mask = Width - 1;
572 if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) &&
574 return X;
575
576 // Same as above, but the shift amount may be extended after masking:
577 if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) &&
579 return X;
580
581 return nullptr;
582 };
583
584 Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, NarrowWidth);
585 bool IsFshl = true; // Sub on LSHR.
586 if (!ShAmt) {
587 ShAmt = matchShiftAmount(ShAmt1, ShAmt0, NarrowWidth);
588 IsFshl = false; // Sub on SHL.
589 }
590 if (!ShAmt)
591 return nullptr;
592
593 // The right-shifted value must have high zeros in the wide type (for example
594 // from 'zext', 'and' or 'shift'). High bits of the left-shifted value are
595 // truncated, so those do not matter.
596 APInt HiBitMask = APInt::getHighBitsSet(WideWidth, WideWidth - NarrowWidth);
597 if (!MaskedValueIsZero(ShVal1, HiBitMask, &Trunc))
598 return nullptr;
599
600 // Adjust the width of ShAmt for narrowed funnel shift operation:
601 // - Zero-extend if ShAmt is narrower than the destination type.
602 // - Truncate if ShAmt is wider, discarding non-significant high-order bits.
603 // This prepares ShAmt for llvm.fshl.i8(trunc(ShVal), trunc(ShVal),
604 // zext/trunc(ShAmt)).
605 Value *NarrowShAmt = Builder.CreateZExtOrTrunc(ShAmt, DestTy);
606
607 Value *X, *Y;
608 X = Y = Builder.CreateTrunc(ShVal0, DestTy);
609 if (ShVal0 != ShVal1)
610 Y = Builder.CreateTrunc(ShVal1, DestTy);
611 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
612 Function *F =
613 Intrinsic::getOrInsertDeclaration(Trunc.getModule(), IID, DestTy);
614 return CallInst::Create(F, {X, Y, NarrowShAmt});
615}
616
617/// Try to narrow the width of math or bitwise logic instructions by pulling a
618/// truncate ahead of binary operators.
619Instruction *InstCombinerImpl::narrowBinOp(TruncInst &Trunc) {
620 Type *SrcTy = Trunc.getSrcTy();
621 Type *DestTy = Trunc.getType();
622 unsigned SrcWidth = SrcTy->getScalarSizeInBits();
623 unsigned DestWidth = DestTy->getScalarSizeInBits();
624
625 if (!isa<VectorType>(SrcTy) && !shouldChangeType(SrcTy, DestTy))
626 return nullptr;
627
628 BinaryOperator *BinOp;
629 if (!match(Trunc.getOperand(0), m_OneUse(m_BinOp(BinOp))))
630 return nullptr;
631
632 Value *BinOp0 = BinOp->getOperand(0);
633 Value *BinOp1 = BinOp->getOperand(1);
634 switch (BinOp->getOpcode()) {
635 case Instruction::And:
636 case Instruction::Or:
637 case Instruction::Xor:
638 case Instruction::Add:
639 case Instruction::Sub:
640 case Instruction::Mul: {
641 Constant *C;
642 if (match(BinOp0, m_Constant(C))) {
643 // trunc (binop C, X) --> binop (trunc C', X)
644 Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy);
645 Value *TruncX = Builder.CreateTrunc(BinOp1, DestTy);
646 return BinaryOperator::Create(BinOp->getOpcode(), NarrowC, TruncX);
647 }
648 if (match(BinOp1, m_Constant(C))) {
649 // trunc (binop X, C) --> binop (trunc X, C')
650 Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy);
651 Value *TruncX = Builder.CreateTrunc(BinOp0, DestTy);
652 return BinaryOperator::Create(BinOp->getOpcode(), TruncX, NarrowC);
653 }
654 Value *X;
655 if (match(BinOp0, m_ZExtOrSExt(m_Value(X))) && X->getType() == DestTy) {
656 // trunc (binop (ext X), Y) --> binop X, (trunc Y)
657 Value *NarrowOp1 = Builder.CreateTrunc(BinOp1, DestTy);
658 return BinaryOperator::Create(BinOp->getOpcode(), X, NarrowOp1);
659 }
660 if (match(BinOp1, m_ZExtOrSExt(m_Value(X))) && X->getType() == DestTy) {
661 // trunc (binop Y, (ext X)) --> binop (trunc Y), X
662 Value *NarrowOp0 = Builder.CreateTrunc(BinOp0, DestTy);
663 return BinaryOperator::Create(BinOp->getOpcode(), NarrowOp0, X);
664 }
665 break;
666 }
667 case Instruction::LShr:
668 case Instruction::AShr: {
669 // trunc (*shr (trunc A), C) --> trunc(*shr A, C)
670 Value *A;
671 Constant *C;
672 if (match(BinOp0, m_Trunc(m_Value(A))) && match(BinOp1, m_Constant(C))) {
673 unsigned MaxShiftAmt = SrcWidth - DestWidth;
674 // If the shift is small enough, all zero/sign bits created by the shift
675 // are removed by the trunc.
677 APInt(SrcWidth, MaxShiftAmt)))) {
678 auto *OldShift = cast<Instruction>(Trunc.getOperand(0));
679 bool IsExact = OldShift->isExact();
680 if (Constant *ShAmt = ConstantFoldIntegerCast(C, A->getType(),
681 /*IsSigned*/ true, DL)) {
682 ShAmt = Constant::mergeUndefsWith(ShAmt, C);
683 Value *Shift =
684 OldShift->getOpcode() == Instruction::AShr
685 ? Builder.CreateAShr(A, ShAmt, OldShift->getName(), IsExact)
686 : Builder.CreateLShr(A, ShAmt, OldShift->getName(), IsExact);
687 return CastInst::CreateTruncOrBitCast(Shift, DestTy);
688 }
689 }
690 }
691 break;
692 }
693 default: break;
694 }
695
696 if (Instruction *NarrowOr = narrowFunnelShift(Trunc))
697 return NarrowOr;
698
699 return nullptr;
700}
701
702/// Try to narrow the width of a splat shuffle. This could be generalized to any
703/// shuffle with a constant operand, but we limit the transform to avoid
704/// creating a shuffle type that targets may not be able to lower effectively.
706 InstCombiner::BuilderTy &Builder) {
707 auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0));
708 if (Shuf && Shuf->hasOneUse() && match(Shuf->getOperand(1), m_Undef()) &&
709 all_equal(Shuf->getShuffleMask()) &&
710 ElementCount::isKnownGE(Shuf->getType()->getElementCount(),
711 cast<VectorType>(Shuf->getOperand(0)->getType())
712 ->getElementCount())) {
713 // trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask
714 // trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask
715 Type *NewTruncTy = Shuf->getOperand(0)->getType()->getWithNewType(
716 Trunc.getType()->getScalarType());
717 Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), NewTruncTy);
718 return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask());
719 }
720
721 return nullptr;
722}
723
724/// Try to narrow the width of an insert element. This could be generalized for
725/// any vector constant, but we limit the transform to insertion into undef to
726/// avoid potential backend problems from unsupported insertion widths. This
727/// could also be extended to handle the case of inserting a scalar constant
728/// into a vector variable.
730 InstCombiner::BuilderTy &Builder) {
731 Instruction::CastOps Opcode = Trunc.getOpcode();
732 assert((Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) &&
733 "Unexpected instruction for shrinking");
734
735 auto *InsElt = dyn_cast<InsertElementInst>(Trunc.getOperand(0));
736 if (!InsElt || !InsElt->hasOneUse())
737 return nullptr;
738
739 Type *DestTy = Trunc.getType();
740 Type *DestScalarTy = DestTy->getScalarType();
741 Value *VecOp = InsElt->getOperand(0);
742 Value *ScalarOp = InsElt->getOperand(1);
743 Value *Index = InsElt->getOperand(2);
744
745 if (match(VecOp, m_Undef())) {
746 // trunc (inselt undef, X, Index) --> inselt undef, (trunc X), Index
747 // fptrunc (inselt undef, X, Index) --> inselt undef, (fptrunc X), Index
748 UndefValue *NarrowUndef = UndefValue::get(DestTy);
749 Value *NarrowOp = Builder.CreateCast(Opcode, ScalarOp, DestScalarTy);
750 return InsertElementInst::Create(NarrowUndef, NarrowOp, Index);
751 }
752
753 return nullptr;
754}
755
757 if (Instruction *Result = commonCastTransforms(Trunc))
758 return Result;
759
760 Value *Src = Trunc.getOperand(0);
761 Type *DestTy = Trunc.getType(), *SrcTy = Src->getType();
762 unsigned DestWidth = DestTy->getScalarSizeInBits();
763 unsigned SrcWidth = SrcTy->getScalarSizeInBits();
764
765 // Attempt to truncate the entire input expression tree to the destination
766 // type. Only do this if the dest type is a simple type, don't convert the
767 // expression tree to something weird like i93 unless the source is also
768 // strange.
769 if ((DestTy->isVectorTy() || shouldChangeType(SrcTy, DestTy)) &&
770 canEvaluateTruncated(Src, DestTy, *this, &Trunc)) {
771
772 // If this cast is a truncate, evaluting in a different type always
773 // eliminates the cast, so it is always a win.
775 dbgs() << "ICE: EvaluateInDifferentType converting expression type"
776 " to avoid cast: "
777 << Trunc << '\n');
778 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
779 assert(Res->getType() == DestTy);
780 return replaceInstUsesWith(Trunc, Res);
781 }
782
783 // For integer types, check if we can shorten the entire input expression to
784 // DestWidth * 2, which won't allow removing the truncate, but reducing the
785 // width may enable further optimizations, e.g. allowing for larger
786 // vectorization factors.
787 if (auto *DestITy = dyn_cast<IntegerType>(DestTy)) {
788 if (DestWidth * 2 < SrcWidth) {
789 auto *NewDestTy = DestITy->getExtendedType();
790 if (shouldChangeType(SrcTy, NewDestTy) &&
791 canEvaluateTruncated(Src, NewDestTy, *this, &Trunc)) {
793 dbgs() << "ICE: EvaluateInDifferentType converting expression type"
794 " to reduce the width of operand of"
795 << Trunc << '\n');
796 Value *Res = EvaluateInDifferentType(Src, NewDestTy, false);
797 return new TruncInst(Res, DestTy);
798 }
799 }
800 }
801
802 // See if we can simplify any instructions used by the input whose sole
803 // purpose is to compute bits we don't care about.
805 return &Trunc;
806
807 if (DestWidth == 1) {
808 Value *Zero = Constant::getNullValue(SrcTy);
809
810 Value *X;
811 const APInt *C1;
812 Constant *C2;
813 if (match(Src, m_OneUse(m_Shr(m_Shl(m_Power2(C1), m_Value(X)),
814 m_ImmConstant(C2))))) {
815 // trunc ((C1 << X) >> C2) to i1 --> X == (C2-cttz(C1)), where C1 is pow2
816 Constant *Log2C1 = ConstantInt::get(SrcTy, C1->exactLogBase2());
817 Constant *CmpC = ConstantExpr::getSub(C2, Log2C1);
818 return new ICmpInst(ICmpInst::ICMP_EQ, X, CmpC);
819 }
820
821 if (match(Src, m_Shr(m_Value(X), m_SpecificInt(SrcWidth - 1)))) {
822 // trunc (ashr X, BW-1) to i1 --> icmp slt X, 0
823 // trunc (lshr X, BW-1) to i1 --> icmp slt X, 0
824 return new ICmpInst(ICmpInst::ICMP_SLT, X, Zero);
825 }
826
827 Constant *C;
828 if (match(Src, m_OneUse(m_LShr(m_Value(X), m_ImmConstant(C))))) {
829 // trunc (lshr X, C) to i1 --> icmp ne (and X, C'), 0
830 Constant *One = ConstantInt::get(SrcTy, APInt(SrcWidth, 1));
831 Value *MaskC = Builder.CreateShl(One, C);
832 Value *And = Builder.CreateAnd(X, MaskC);
833 return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
834 }
836 m_Deferred(X))))) {
837 // trunc (or (lshr X, C), X) to i1 --> icmp ne (and X, C'), 0
838 Constant *One = ConstantInt::get(SrcTy, APInt(SrcWidth, 1));
839 Value *MaskC = Builder.CreateShl(One, C);
840 Value *And = Builder.CreateAnd(X, Builder.CreateOr(MaskC, One));
841 return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
842 }
843
844 {
845 const APInt *C;
846 if (match(Src, m_Shl(m_APInt(C), m_Value(X))) && (*C)[0] == 1) {
847 // trunc (C << X) to i1 --> X == 0, where C is odd
848 return new ICmpInst(ICmpInst::Predicate::ICMP_EQ, X, Zero);
849 }
850 }
851
852 if (Trunc.hasNoUnsignedWrap() || Trunc.hasNoSignedWrap()) {
853 Value *X, *Y;
854 if (match(Src, m_Xor(m_Value(X), m_Value(Y))))
855 return new ICmpInst(ICmpInst::ICMP_NE, X, Y);
856 }
857 }
858
859 Value *A, *B;
860 Constant *C;
861 if (match(Src, m_LShr(m_SExt(m_Value(A)), m_Constant(C)))) {
862 unsigned AWidth = A->getType()->getScalarSizeInBits();
863 unsigned MaxShiftAmt = SrcWidth - std::max(DestWidth, AWidth);
864 auto *OldSh = cast<Instruction>(Src);
865 bool IsExact = OldSh->isExact();
866
867 // If the shift is small enough, all zero bits created by the shift are
868 // removed by the trunc.
870 APInt(SrcWidth, MaxShiftAmt)))) {
871 auto GetNewShAmt = [&](unsigned Width) {
872 Constant *MaxAmt = ConstantInt::get(SrcTy, Width - 1, false);
873 Constant *Cmp =
875 Constant *ShAmt = ConstantFoldSelectInstruction(Cmp, C, MaxAmt);
876 return ConstantFoldCastOperand(Instruction::Trunc, ShAmt, A->getType(),
877 DL);
878 };
879
880 // trunc (lshr (sext A), C) --> ashr A, C
881 if (A->getType() == DestTy) {
882 Constant *ShAmt = GetNewShAmt(DestWidth);
883 ShAmt = Constant::mergeUndefsWith(ShAmt, C);
884 return IsExact ? BinaryOperator::CreateExactAShr(A, ShAmt)
885 : BinaryOperator::CreateAShr(A, ShAmt);
886 }
887 // The types are mismatched, so create a cast after shifting:
888 // trunc (lshr (sext A), C) --> sext/trunc (ashr A, C)
889 if (Src->hasOneUse()) {
890 Constant *ShAmt = GetNewShAmt(AWidth);
891 Value *Shift = Builder.CreateAShr(A, ShAmt, "", IsExact);
892 return CastInst::CreateIntegerCast(Shift, DestTy, true);
893 }
894 }
895 // TODO: Mask high bits with 'and'.
896 }
897
898 if (Instruction *I = narrowBinOp(Trunc))
899 return I;
900
902 return I;
903
904 if (Instruction *I = shrinkInsertElt(Trunc, Builder))
905 return I;
906
907 if (Src->hasOneUse() &&
908 (isa<VectorType>(SrcTy) || shouldChangeType(SrcTy, DestTy))) {
909 // Transform "trunc (shl X, cst)" -> "shl (trunc X), cst" so long as the
910 // dest type is native and cst < dest size.
911 if (match(Src, m_Shl(m_Value(A), m_Constant(C))) &&
912 !match(A, m_Shr(m_Value(), m_Constant()))) {
913 // Skip shifts of shift by constants. It undoes a combine in
914 // FoldShiftByConstant and is the extend in reg pattern.
915 APInt Threshold = APInt(C->getType()->getScalarSizeInBits(), DestWidth);
916 if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold))) {
917 Value *NewTrunc = Builder.CreateTrunc(A, DestTy, A->getName() + ".tr");
918 return BinaryOperator::Create(Instruction::Shl, NewTrunc,
919 ConstantExpr::getTrunc(C, DestTy));
920 }
921 }
922 }
923
924 if (Instruction *I = foldVecTruncToExtElt(Trunc, *this))
925 return I;
926
927 if (Instruction *I = foldVecExtTruncToExtElt(Trunc, *this))
928 return I;
929
930 // trunc (ctlz_i32(zext(A), B) --> add(ctlz_i16(A, B), C)
932 m_Value(B))))) {
933 unsigned AWidth = A->getType()->getScalarSizeInBits();
934 if (AWidth == DestWidth && AWidth > Log2_32(SrcWidth)) {
935 Value *WidthDiff = ConstantInt::get(A->getType(), SrcWidth - AWidth);
936 Value *NarrowCtlz =
937 Builder.CreateIntrinsic(Intrinsic::ctlz, {Trunc.getType()}, {A, B});
938 return BinaryOperator::CreateAdd(NarrowCtlz, WidthDiff);
939 }
940 }
941
942 if (match(Src, m_VScale())) {
943 if (Trunc.getFunction() &&
944 Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
945 Attribute Attr =
946 Trunc.getFunction()->getFnAttribute(Attribute::VScaleRange);
947 if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax())
948 if (Log2_32(*MaxVScale) < DestWidth)
949 return replaceInstUsesWith(Trunc, Builder.CreateVScale(DestTy));
950 }
951 }
952
953 if (DestWidth == 1 &&
954 (Trunc.hasNoUnsignedWrap() || Trunc.hasNoSignedWrap()) &&
955 isKnownNonZero(Src, SQ.getWithInstruction(&Trunc)))
956 return replaceInstUsesWith(Trunc, ConstantInt::getTrue(DestTy));
957
958 bool Changed = false;
959 if (!Trunc.hasNoSignedWrap() &&
960 ComputeMaxSignificantBits(Src, &Trunc) <= DestWidth) {
961 Trunc.setHasNoSignedWrap(true);
962 Changed = true;
963 }
964 if (!Trunc.hasNoUnsignedWrap() &&
965 MaskedValueIsZero(Src, APInt::getBitsSetFrom(SrcWidth, DestWidth),
966 &Trunc)) {
967 Trunc.setHasNoUnsignedWrap(true);
968 Changed = true;
969 }
970
971 const APInt *C1;
972 Value *V1;
973 // OP = { lshr, ashr }
974 // trunc ( OP i8 C1, V1) to i1 -> icmp eq V1, log_2(C1) iff C1 is power of 2
975 if (DestWidth == 1 && match(Src, m_Shr(m_Power2(C1), m_Value(V1)))) {
976 Value *Right = ConstantInt::get(V1->getType(), C1->countr_zero());
977 return new ICmpInst(ICmpInst::ICMP_EQ, V1, Right);
978 }
979
980 // OP = { lshr, ashr }
981 // trunc ( OP i8 C1, V1) to i1 -> icmp ult V1, log_2(C1 + 1) iff (C1 + 1) is
982 // power of 2
983 if (DestWidth == 1 && match(Src, m_Shr(m_LowBitMask(C1), m_Value(V1)))) {
984 Value *Right = ConstantInt::get(V1->getType(), C1->countr_one());
985 return new ICmpInst(ICmpInst::ICMP_ULT, V1, Right);
986 }
987
988 // OP = { lshr, ashr }
989 // trunc ( OP i8 C1, V1) to i1 -> icmp ugt V1, cttz(C1) - 1 iff (C1) is
990 // negative power of 2
991 if (DestWidth == 1 && match(Src, m_Shr(m_NegatedPower2(C1), m_Value(V1)))) {
992 Value *Right = ConstantInt::get(V1->getType(), C1->countr_zero());
993 return new ICmpInst(ICmpInst::ICMP_UGE, V1, Right);
994 }
995
996 return Changed ? &Trunc : nullptr;
997}
998
999Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp,
1000 ZExtInst &Zext) {
1001 // If we are just checking for a icmp eq of a single bit and zext'ing it
1002 // to an integer, then shift the bit to the appropriate place and then
1003 // cast to integer to avoid the comparison.
1004
1005 // FIXME: This set of transforms does not check for extra uses and/or creates
1006 // an extra instruction (an optional final cast is not included
1007 // in the transform comments). We may also want to favor icmp over
1008 // shifts in cases of equal instructions because icmp has better
1009 // analysis in general (invert the transform).
1010
1011 const APInt *Op1CV;
1012 if (match(Cmp->getOperand(1), m_APInt(Op1CV))) {
1013
1014 // zext (x <s 0) to i32 --> x>>u31 true if signbit set.
1015 if (Cmp->getPredicate() == ICmpInst::ICMP_SLT && Op1CV->isZero()) {
1016 Value *In = Cmp->getOperand(0);
1017 Value *Sh = ConstantInt::get(In->getType(),
1018 In->getType()->getScalarSizeInBits() - 1);
1019 In = Builder.CreateLShr(In, Sh, In->getName() + ".lobit");
1020 if (In->getType() != Zext.getType())
1021 In = Builder.CreateIntCast(In, Zext.getType(), false /*ZExt*/);
1022
1023 return replaceInstUsesWith(Zext, In);
1024 }
1025
1026 // zext (X == 0) to i32 --> X^1 iff X has only the low bit set.
1027 // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set.
1028 // zext (X != 0) to i32 --> X iff X has only the low bit set.
1029 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set.
1030
1031 if (Op1CV->isZero() && Cmp->isEquality()) {
1032 // Exactly 1 possible 1? But not the high-bit because that is
1033 // canonicalized to this form.
1034 KnownBits Known = computeKnownBits(Cmp->getOperand(0), &Zext);
1035 APInt KnownZeroMask(~Known.Zero);
1036 uint32_t ShAmt = KnownZeroMask.logBase2();
1037 bool IsExpectShAmt = KnownZeroMask.isPowerOf2() &&
1038 (Zext.getType()->getScalarSizeInBits() != ShAmt + 1);
1039 if (IsExpectShAmt &&
1040 (Cmp->getOperand(0)->getType() == Zext.getType() ||
1041 Cmp->getPredicate() == ICmpInst::ICMP_NE || ShAmt == 0)) {
1042 Value *In = Cmp->getOperand(0);
1043 if (ShAmt) {
1044 // Perform a logical shr by shiftamt.
1045 // Insert the shift to put the result in the low bit.
1046 In = Builder.CreateLShr(In, ConstantInt::get(In->getType(), ShAmt),
1047 In->getName() + ".lobit");
1048 }
1049
1050 // Toggle the low bit for "X == 0".
1051 if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
1052 In = Builder.CreateXor(In, ConstantInt::get(In->getType(), 1));
1053
1054 if (Zext.getType() == In->getType())
1055 return replaceInstUsesWith(Zext, In);
1056
1057 Value *IntCast = Builder.CreateIntCast(In, Zext.getType(), false);
1058 return replaceInstUsesWith(Zext, IntCast);
1059 }
1060 }
1061 }
1062
1063 if (Cmp->isEquality()) {
1064 // Test if a bit is clear/set using a shifted-one mask:
1065 // zext (icmp eq (and X, (1 << ShAmt)), 0) --> and (lshr (not X), ShAmt), 1
1066 // zext (icmp ne (and X, (1 << ShAmt)), 0) --> and (lshr X, ShAmt), 1
1067 Value *X, *ShAmt;
1068 if (Cmp->hasOneUse() && match(Cmp->getOperand(1), m_ZeroInt()) &&
1069 match(Cmp->getOperand(0),
1070 m_OneUse(m_c_And(m_Shl(m_One(), m_Value(ShAmt)), m_Value(X))))) {
1071 auto *And = cast<BinaryOperator>(Cmp->getOperand(0));
1072 Value *Shift = And->getOperand(X == And->getOperand(0) ? 1 : 0);
1073 if (Zext.getType() == And->getType() ||
1074 Cmp->getPredicate() != ICmpInst::ICMP_EQ || Shift->hasOneUse()) {
1075 if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
1076 X = Builder.CreateNot(X);
1077 Value *Lshr = Builder.CreateLShr(X, ShAmt);
1078 Value *And1 =
1079 Builder.CreateAnd(Lshr, ConstantInt::get(X->getType(), 1));
1080 return replaceInstUsesWith(
1081 Zext, Builder.CreateZExtOrTrunc(And1, Zext.getType()));
1082 }
1083 }
1084 }
1085
1086 return nullptr;
1087}
1088
1089/// Determine if the specified value can be computed in the specified wider type
1090/// and produce the same low bits. If not, return false.
1091///
1092/// If this function returns true, it can also return a non-zero number of bits
1093/// (in BitsToClear) which indicates that the value it computes is correct for
1094/// the zero extend, but that the additional BitsToClear bits need to be zero'd
1095/// out. For example, to promote something like:
1096///
1097/// %B = trunc i64 %A to i32
1098/// %C = lshr i32 %B, 8
1099/// %E = zext i32 %C to i64
1100///
1101/// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be
1102/// set to 8 to indicate that the promoted value needs to have bits 24-31
1103/// cleared in addition to bits 32-63. Since an 'and' will be generated to
1104/// clear the top bits anyway, doing this has no extra cost.
1105///
1106/// This function works on both vectors and scalars.
1107static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear,
1108 InstCombinerImpl &IC, Instruction *CxtI) {
1109 BitsToClear = 0;
1110 if (canAlwaysEvaluateInType(V, Ty))
1111 return true;
1112 if (canNotEvaluateInType(V, Ty))
1113 return false;
1114
1115 auto *I = cast<Instruction>(V);
1116 unsigned Tmp;
1117 switch (I->getOpcode()) {
1118 case Instruction::ZExt: // zext(zext(x)) -> zext(x).
1119 case Instruction::SExt: // zext(sext(x)) -> sext(x).
1120 case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x)
1121 return true;
1122 case Instruction::And:
1123 case Instruction::Or:
1124 case Instruction::Xor:
1125 case Instruction::Add:
1126 case Instruction::Sub:
1127 case Instruction::Mul:
1128 if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI) ||
1129 !canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI))
1130 return false;
1131 // These can all be promoted if neither operand has 'bits to clear'.
1132 if (BitsToClear == 0 && Tmp == 0)
1133 return true;
1134
1135 // If the operation is an AND/OR/XOR and the bits to clear are zero in the
1136 // other side, BitsToClear is ok.
1137 if (Tmp == 0 && I->isBitwiseLogicOp()) {
1138 // We use MaskedValueIsZero here for generality, but the case we care
1139 // about the most is constant RHS.
1140 unsigned VSize = V->getType()->getScalarSizeInBits();
1141 if (IC.MaskedValueIsZero(I->getOperand(1),
1142 APInt::getHighBitsSet(VSize, BitsToClear),
1143 CxtI)) {
1144 // If this is an And instruction and all of the BitsToClear are
1145 // known to be zero we can reset BitsToClear.
1146 if (I->getOpcode() == Instruction::And)
1147 BitsToClear = 0;
1148 return true;
1149 }
1150 }
1151
1152 // Otherwise, we don't know how to analyze this BitsToClear case yet.
1153 return false;
1154
1155 case Instruction::Shl: {
1156 // We can promote shl(x, cst) if we can promote x. Since shl overwrites the
1157 // upper bits we can reduce BitsToClear by the shift amount.
1158 uint64_t ShiftAmt;
1159 if (match(I->getOperand(1), m_ConstantInt(ShiftAmt))) {
1160 if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI))
1161 return false;
1162 BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0;
1163 return true;
1164 }
1165 return false;
1166 }
1167 case Instruction::LShr: {
1168 // We can promote lshr(x, cst) if we can promote x. This requires the
1169 // ultimate 'and' to clear out the high zero bits we're clearing out though.
1170 uint64_t ShiftAmt;
1171 if (match(I->getOperand(1), m_ConstantInt(ShiftAmt))) {
1172 if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI))
1173 return false;
1174 BitsToClear += ShiftAmt;
1175 if (BitsToClear > V->getType()->getScalarSizeInBits())
1176 BitsToClear = V->getType()->getScalarSizeInBits();
1177 return true;
1178 }
1179 // Cannot promote variable LSHR.
1180 return false;
1181 }
1182 case Instruction::Select:
1183 if (!canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI) ||
1184 !canEvaluateZExtd(I->getOperand(2), Ty, BitsToClear, IC, CxtI) ||
1185 // TODO: If important, we could handle the case when the BitsToClear are
1186 // known zero in the disagreeing side.
1187 Tmp != BitsToClear)
1188 return false;
1189 return true;
1190
1191 case Instruction::PHI: {
1192 // We can change a phi if we can change all operands. Note that we never
1193 // get into trouble with cyclic PHIs here because we only consider
1194 // instructions with a single use.
1195 PHINode *PN = cast<PHINode>(I);
1196 if (!canEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear, IC, CxtI))
1197 return false;
1198 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i)
1199 if (!canEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp, IC, CxtI) ||
1200 // TODO: If important, we could handle the case when the BitsToClear
1201 // are known zero in the disagreeing input.
1202 Tmp != BitsToClear)
1203 return false;
1204 return true;
1205 }
1206 case Instruction::Call:
1207 // llvm.vscale() can always be executed in larger type, because the
1208 // value is automatically zero-extended.
1210 if (II->getIntrinsicID() == Intrinsic::vscale)
1211 return true;
1212 return false;
1213 default:
1214 // TODO: Can handle more cases here.
1215 return false;
1216 }
1217}
1218
1220 // If this zero extend is only used by a truncate, let the truncate be
1221 // eliminated before we try to optimize this zext.
1222 if (Zext.hasOneUse() && isa<TruncInst>(Zext.user_back()) &&
1223 !isa<Constant>(Zext.getOperand(0)))
1224 return nullptr;
1225
1226 // If one of the common conversion will work, do it.
1227 if (Instruction *Result = commonCastTransforms(Zext))
1228 return Result;
1229
1230 Value *Src = Zext.getOperand(0);
1231 Type *SrcTy = Src->getType(), *DestTy = Zext.getType();
1232
1233 // zext nneg bool x -> 0
1234 if (SrcTy->isIntOrIntVectorTy(1) && Zext.hasNonNeg())
1236
1237 // Try to extend the entire expression tree to the wide destination type.
1238 unsigned BitsToClear;
1239 if (shouldChangeType(SrcTy, DestTy) &&
1240 canEvaluateZExtd(Src, DestTy, BitsToClear, *this, &Zext)) {
1241 assert(BitsToClear <= SrcTy->getScalarSizeInBits() &&
1242 "Can't clear more bits than in SrcTy");
1243
1244 // Okay, we can transform this! Insert the new expression now.
1245 LLVM_DEBUG(
1246 dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1247 " to avoid zero extend: "
1248 << Zext << '\n');
1249 Value *Res = EvaluateInDifferentType(Src, DestTy, false);
1250 assert(Res->getType() == DestTy);
1251
1252 // Preserve debug values referring to Src if the zext is its last use.
1253 if (auto *SrcOp = dyn_cast<Instruction>(Src))
1254 if (SrcOp->hasOneUse())
1255 replaceAllDbgUsesWith(*SrcOp, *Res, Zext, DT);
1256
1257 uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits() - BitsToClear;
1258 uint32_t DestBitSize = DestTy->getScalarSizeInBits();
1259
1260 // If the high bits are already filled with zeros, just replace this
1261 // cast with the result.
1263 Res, APInt::getHighBitsSet(DestBitSize, DestBitSize - SrcBitsKept),
1264 &Zext))
1265 return replaceInstUsesWith(Zext, Res);
1266
1267 // We need to emit an AND to clear the high bits.
1268 Constant *C = ConstantInt::get(Res->getType(),
1269 APInt::getLowBitsSet(DestBitSize, SrcBitsKept));
1270 return BinaryOperator::CreateAnd(Res, C);
1271 }
1272
1273 // If this is a TRUNC followed by a ZEXT then we are dealing with integral
1274 // types and if the sizes are just right we can convert this into a logical
1275 // 'and' which will be much cheaper than the pair of casts.
1276 if (auto *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast
1277 // TODO: Subsume this into EvaluateInDifferentType.
1278
1279 // Get the sizes of the types involved. We know that the intermediate type
1280 // will be smaller than A or C, but don't know the relation between A and C.
1281 Value *A = CSrc->getOperand(0);
1282 unsigned SrcSize = A->getType()->getScalarSizeInBits();
1283 unsigned MidSize = CSrc->getType()->getScalarSizeInBits();
1284 unsigned DstSize = DestTy->getScalarSizeInBits();
1285 // If we're actually extending zero bits, then if
1286 // SrcSize < DstSize: zext(a & mask)
1287 // SrcSize == DstSize: a & mask
1288 // SrcSize > DstSize: trunc(a) & mask
1289 if (SrcSize < DstSize) {
1290 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
1291 Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
1292 Value *And = Builder.CreateAnd(A, AndConst, CSrc->getName() + ".mask");
1293 return new ZExtInst(And, DestTy);
1294 }
1295
1296 if (SrcSize == DstSize) {
1297 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
1298 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
1299 AndValue));
1300 }
1301 if (SrcSize > DstSize) {
1302 Value *Trunc = Builder.CreateTrunc(A, DestTy);
1303 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
1304 return BinaryOperator::CreateAnd(Trunc,
1305 ConstantInt::get(Trunc->getType(),
1306 AndValue));
1307 }
1308 }
1309
1310 if (auto *Cmp = dyn_cast<ICmpInst>(Src))
1311 return transformZExtICmp(Cmp, Zext);
1312
1313 // zext(trunc(X) & C) -> (X & zext(C)).
1314 Constant *C;
1315 Value *X;
1316 if (match(Src, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Constant(C)))) &&
1317 X->getType() == DestTy)
1318 return BinaryOperator::CreateAnd(X, Builder.CreateZExt(C, DestTy));
1319
1320 // zext((trunc(X) & C) ^ C) -> ((X & zext(C)) ^ zext(C)).
1321 Value *And;
1322 if (match(Src, m_OneUse(m_Xor(m_Value(And), m_Constant(C)))) &&
1324 X->getType() == DestTy) {
1325 Value *ZC = Builder.CreateZExt(C, DestTy);
1326 return BinaryOperator::CreateXor(Builder.CreateAnd(X, ZC), ZC);
1327 }
1328
1329 // If we are truncating, masking, and then zexting back to the original type,
1330 // that's just a mask. This is not handled by canEvaluateZextd if the
1331 // intermediate values have extra uses. This could be generalized further for
1332 // a non-constant mask operand.
1333 // zext (and (trunc X), C) --> and X, (zext C)
1334 if (match(Src, m_And(m_Trunc(m_Value(X)), m_Constant(C))) &&
1335 X->getType() == DestTy) {
1336 Value *ZextC = Builder.CreateZExt(C, DestTy);
1337 return BinaryOperator::CreateAnd(X, ZextC);
1338 }
1339
1340 if (match(Src, m_VScale())) {
1341 if (Zext.getFunction() &&
1342 Zext.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
1343 Attribute Attr =
1344 Zext.getFunction()->getFnAttribute(Attribute::VScaleRange);
1345 if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) {
1346 unsigned TypeWidth = Src->getType()->getScalarSizeInBits();
1347 if (Log2_32(*MaxVScale) < TypeWidth)
1348 return replaceInstUsesWith(Zext, Builder.CreateVScale(DestTy));
1349 }
1350 }
1351 }
1352
1353 if (!Zext.hasNonNeg()) {
1354 // If this zero extend is only used by a shift, add nneg flag.
1355 if (Zext.hasOneUse() &&
1356 SrcTy->getScalarSizeInBits() >
1357 Log2_64_Ceil(DestTy->getScalarSizeInBits()) &&
1358 match(Zext.user_back(), m_Shift(m_Value(), m_Specific(&Zext)))) {
1359 Zext.setNonNeg();
1360 return &Zext;
1361 }
1362
1363 if (isKnownNonNegative(Src, SQ.getWithInstruction(&Zext))) {
1364 Zext.setNonNeg();
1365 return &Zext;
1366 }
1367 }
1368
1369 return nullptr;
1370}
1371
1372/// Transform (sext icmp) to bitwise / integer operations to eliminate the icmp.
1373Instruction *InstCombinerImpl::transformSExtICmp(ICmpInst *Cmp,
1374 SExtInst &Sext) {
1375 Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
1376 ICmpInst::Predicate Pred = Cmp->getPredicate();
1377
1378 // Don't bother if Op1 isn't of vector or integer type.
1379 if (!Op1->getType()->isIntOrIntVectorTy())
1380 return nullptr;
1381
1382 if (Pred == ICmpInst::ICMP_SLT && match(Op1, m_ZeroInt())) {
1383 // sext (x <s 0) --> ashr x, 31 (all ones if negative)
1384 Value *Sh = ConstantInt::get(Op0->getType(),
1385 Op0->getType()->getScalarSizeInBits() - 1);
1386 Value *In = Builder.CreateAShr(Op0, Sh, Op0->getName() + ".lobit");
1387 if (In->getType() != Sext.getType())
1388 In = Builder.CreateIntCast(In, Sext.getType(), true /*SExt*/);
1389
1390 return replaceInstUsesWith(Sext, In);
1391 }
1392
1393 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
1394 // If we know that only one bit of the LHS of the icmp can be set and we
1395 // have an equality comparison with zero or a power of 2, we can transform
1396 // the icmp and sext into bitwise/integer operations.
1397 if (Cmp->hasOneUse() &&
1398 Cmp->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){
1399 KnownBits Known = computeKnownBits(Op0, &Sext);
1400
1401 APInt KnownZeroMask(~Known.Zero);
1402 if (KnownZeroMask.isPowerOf2()) {
1403 Value *In = Cmp->getOperand(0);
1404
1405 // If the icmp tests for a known zero bit we can constant fold it.
1406 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) {
1407 Value *V = Pred == ICmpInst::ICMP_NE ?
1409 ConstantInt::getNullValue(Sext.getType());
1410 return replaceInstUsesWith(Sext, V);
1411 }
1412
1413 if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) {
1414 // sext ((x & 2^n) == 0) -> (x >> n) - 1
1415 // sext ((x & 2^n) != 2^n) -> (x >> n) - 1
1416 unsigned ShiftAmt = KnownZeroMask.countr_zero();
1417 // Perform a right shift to place the desired bit in the LSB.
1418 if (ShiftAmt)
1419 In = Builder.CreateLShr(In,
1420 ConstantInt::get(In->getType(), ShiftAmt));
1421
1422 // At this point "In" is either 1 or 0. Subtract 1 to turn
1423 // {1, 0} -> {0, -1}.
1424 In = Builder.CreateAdd(In,
1425 ConstantInt::getAllOnesValue(In->getType()),
1426 "sext");
1427 } else {
1428 // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1
1429 // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1
1430 unsigned ShiftAmt = KnownZeroMask.countl_zero();
1431 // Perform a left shift to place the desired bit in the MSB.
1432 if (ShiftAmt)
1433 In = Builder.CreateShl(In,
1434 ConstantInt::get(In->getType(), ShiftAmt));
1435
1436 // Distribute the bit over the whole bit width.
1437 In = Builder.CreateAShr(In, ConstantInt::get(In->getType(),
1438 KnownZeroMask.getBitWidth() - 1), "sext");
1439 }
1440
1441 if (Sext.getType() == In->getType())
1442 return replaceInstUsesWith(Sext, In);
1443 return CastInst::CreateIntegerCast(In, Sext.getType(), true/*SExt*/);
1444 }
1445 }
1446 }
1447
1448 return nullptr;
1449}
1450
1451/// Return true if we can take the specified value and return it as type Ty
1452/// without inserting any new casts and without changing the value of the common
1453/// low bits. This is used by code that tries to promote integer operations to
1454/// a wider types will allow us to eliminate the extension.
1455///
1456/// This function works on both vectors and scalars.
1457///
1458static bool canEvaluateSExtd(Value *V, Type *Ty) {
1459 assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() &&
1460 "Can't sign extend type to a smaller type");
1461 if (canAlwaysEvaluateInType(V, Ty))
1462 return true;
1463 if (canNotEvaluateInType(V, Ty))
1464 return false;
1465
1466 auto *I = cast<Instruction>(V);
1467 switch (I->getOpcode()) {
1468 case Instruction::SExt: // sext(sext(x)) -> sext(x)
1469 case Instruction::ZExt: // sext(zext(x)) -> zext(x)
1470 case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x)
1471 return true;
1472 case Instruction::And:
1473 case Instruction::Or:
1474 case Instruction::Xor:
1475 case Instruction::Add:
1476 case Instruction::Sub:
1477 case Instruction::Mul:
1478 // These operators can all arbitrarily be extended if their inputs can.
1479 return canEvaluateSExtd(I->getOperand(0), Ty) &&
1480 canEvaluateSExtd(I->getOperand(1), Ty);
1481
1482 //case Instruction::Shl: TODO
1483 //case Instruction::LShr: TODO
1484
1485 case Instruction::Select:
1486 return canEvaluateSExtd(I->getOperand(1), Ty) &&
1487 canEvaluateSExtd(I->getOperand(2), Ty);
1488
1489 case Instruction::PHI: {
1490 // We can change a phi if we can change all operands. Note that we never
1491 // get into trouble with cyclic PHIs here because we only consider
1492 // instructions with a single use.
1493 PHINode *PN = cast<PHINode>(I);
1494 for (Value *IncValue : PN->incoming_values())
1495 if (!canEvaluateSExtd(IncValue, Ty)) return false;
1496 return true;
1497 }
1498 default:
1499 // TODO: Can handle more cases here.
1500 break;
1501 }
1502
1503 return false;
1504}
1505
1507 // If this sign extend is only used by a truncate, let the truncate be
1508 // eliminated before we try to optimize this sext.
1509 if (Sext.hasOneUse() && isa<TruncInst>(Sext.user_back()))
1510 return nullptr;
1511
1512 if (Instruction *I = commonCastTransforms(Sext))
1513 return I;
1514
1515 Value *Src = Sext.getOperand(0);
1516 Type *SrcTy = Src->getType(), *DestTy = Sext.getType();
1517 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1518 unsigned DestBitSize = DestTy->getScalarSizeInBits();
1519
1520 // If the value being extended is zero or positive, use a zext instead.
1521 if (isKnownNonNegative(Src, SQ.getWithInstruction(&Sext))) {
1522 auto CI = CastInst::Create(Instruction::ZExt, Src, DestTy);
1523 CI->setNonNeg(true);
1524 return CI;
1525 }
1526
1527 // Try to extend the entire expression tree to the wide destination type.
1528 if (shouldChangeType(SrcTy, DestTy) && canEvaluateSExtd(Src, DestTy)) {
1529 // Okay, we can transform this! Insert the new expression now.
1530 LLVM_DEBUG(
1531 dbgs() << "ICE: EvaluateInDifferentType converting expression type"
1532 " to avoid sign extend: "
1533 << Sext << '\n');
1534 Value *Res = EvaluateInDifferentType(Src, DestTy, true);
1535 assert(Res->getType() == DestTy);
1536
1537 // If the high bits are already filled with sign bit, just replace this
1538 // cast with the result.
1539 if (ComputeNumSignBits(Res, &Sext) > DestBitSize - SrcBitSize)
1540 return replaceInstUsesWith(Sext, Res);
1541
1542 // We need to emit a shl + ashr to do the sign extend.
1543 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize);
1544 return BinaryOperator::CreateAShr(Builder.CreateShl(Res, ShAmt, "sext"),
1545 ShAmt);
1546 }
1547
1548 Value *X;
1549 if (match(Src, m_Trunc(m_Value(X)))) {
1550 // If the input has more sign bits than bits truncated, then convert
1551 // directly to final type.
1552 unsigned XBitSize = X->getType()->getScalarSizeInBits();
1553 if (ComputeNumSignBits(X, &Sext) > XBitSize - SrcBitSize)
1554 return CastInst::CreateIntegerCast(X, DestTy, /* isSigned */ true);
1555
1556 // If input is a trunc from the destination type, then convert into shifts.
1557 if (Src->hasOneUse() && X->getType() == DestTy) {
1558 // sext (trunc X) --> ashr (shl X, C), C
1559 Constant *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize);
1560 return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShAmt), ShAmt);
1561 }
1562
1563 // If we are replacing shifted-in high zero bits with sign bits, convert
1564 // the logic shift to arithmetic shift and eliminate the cast to
1565 // intermediate type:
1566 // sext (trunc (lshr Y, C)) --> sext/trunc (ashr Y, C)
1567 Value *Y;
1568 if (Src->hasOneUse() &&
1570 m_SpecificIntAllowPoison(XBitSize - SrcBitSize)))) {
1571 Value *Ashr = Builder.CreateAShr(Y, XBitSize - SrcBitSize);
1572 return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true);
1573 }
1574 }
1575
1576 if (auto *Cmp = dyn_cast<ICmpInst>(Src))
1577 return transformSExtICmp(Cmp, Sext);
1578
1579 // If the input is a shl/ashr pair of a same constant, then this is a sign
1580 // extension from a smaller value. If we could trust arbitrary bitwidth
1581 // integers, we could turn this into a truncate to the smaller bit and then
1582 // use a sext for the whole extension. Since we don't, look deeper and check
1583 // for a truncate. If the source and dest are the same type, eliminate the
1584 // trunc and extend and just do shifts. For example, turn:
1585 // %a = trunc i32 %i to i8
1586 // %b = shl i8 %a, C
1587 // %c = ashr i8 %b, C
1588 // %d = sext i8 %c to i32
1589 // into:
1590 // %a = shl i32 %i, 32-(8-C)
1591 // %d = ashr i32 %a, 32-(8-C)
1592 Value *A = nullptr;
1593 // TODO: Eventually this could be subsumed by EvaluateInDifferentType.
1594 Constant *BA = nullptr, *CA = nullptr;
1595 if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_Constant(BA)),
1596 m_ImmConstant(CA))) &&
1597 BA->isElementWiseEqual(CA) && A->getType() == DestTy) {
1598 Constant *WideCurrShAmt =
1599 ConstantFoldCastOperand(Instruction::SExt, CA, DestTy, DL);
1600 assert(WideCurrShAmt && "Constant folding of ImmConstant cannot fail");
1601 Constant *NumLowbitsLeft = ConstantExpr::getSub(
1602 ConstantInt::get(DestTy, SrcTy->getScalarSizeInBits()), WideCurrShAmt);
1603 Constant *NewShAmt = ConstantExpr::getSub(
1604 ConstantInt::get(DestTy, DestTy->getScalarSizeInBits()),
1605 NumLowbitsLeft);
1606 NewShAmt =
1608 A = Builder.CreateShl(A, NewShAmt, Sext.getName());
1609 return BinaryOperator::CreateAShr(A, NewShAmt);
1610 }
1611
1612 // Splatting a bit of constant-index across a value:
1613 // sext (ashr (trunc iN X to iM), M-1) to iN --> ashr (shl X, N-M), N-1
1614 // If the dest type is different, use a cast (adjust use check).
1615 if (match(Src, m_OneUse(m_AShr(m_Trunc(m_Value(X)),
1616 m_SpecificInt(SrcBitSize - 1))))) {
1617 Type *XTy = X->getType();
1618 unsigned XBitSize = XTy->getScalarSizeInBits();
1619 Constant *ShlAmtC = ConstantInt::get(XTy, XBitSize - SrcBitSize);
1620 Constant *AshrAmtC = ConstantInt::get(XTy, XBitSize - 1);
1621 if (XTy == DestTy)
1622 return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShlAmtC),
1623 AshrAmtC);
1624 if (cast<BinaryOperator>(Src)->getOperand(0)->hasOneUse()) {
1625 Value *Ashr = Builder.CreateAShr(Builder.CreateShl(X, ShlAmtC), AshrAmtC);
1626 return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true);
1627 }
1628 }
1629
1630 if (match(Src, m_VScale())) {
1631 if (Sext.getFunction() &&
1632 Sext.getFunction()->hasFnAttribute(Attribute::VScaleRange)) {
1633 Attribute Attr =
1634 Sext.getFunction()->getFnAttribute(Attribute::VScaleRange);
1635 if (std::optional<unsigned> MaxVScale = Attr.getVScaleRangeMax())
1636 if (Log2_32(*MaxVScale) < (SrcBitSize - 1))
1637 return replaceInstUsesWith(Sext, Builder.CreateVScale(DestTy));
1638 }
1639 }
1640
1641 return nullptr;
1642}
1643
1644/// Return a Constant* for the specified floating-point constant if it fits
1645/// in the specified FP type without changing its value.
1646static bool fitsInFPType(APFloat F, const fltSemantics &Sem) {
1647 bool losesInfo;
1648 (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
1649 return !losesInfo;
1650}
1651
1653 bool PreferBFloat) {
1654 // See if the value can be truncated to bfloat and then reextended.
1655 if (PreferBFloat && fitsInFPType(F, APFloat::BFloat()))
1656 return Type::getBFloatTy(Ctx);
1657 // See if the value can be truncated to half and then reextended.
1658 if (!PreferBFloat && fitsInFPType(F, APFloat::IEEEhalf()))
1659 return Type::getHalfTy(Ctx);
1660 // See if the value can be truncated to float and then reextended.
1662 return Type::getFloatTy(Ctx);
1663 if (&F.getSemantics() == &APFloat::IEEEdouble())
1664 return nullptr; // Won't shrink.
1665 // See if the value can be truncated to double and then reextended.
1667 return Type::getDoubleTy(Ctx);
1668 // Don't try to shrink to various long double types.
1669 return nullptr;
1670}
1671
1672static Type *shrinkFPConstant(ConstantFP *CFP, bool PreferBFloat) {
1673 Type *Ty = CFP->getType();
1674 if (Ty->getScalarType()->isPPC_FP128Ty())
1675 return nullptr; // No constant folding of this.
1676
1677 Type *ShrinkTy =
1678 shrinkFPConstant(CFP->getContext(), CFP->getValueAPF(), PreferBFloat);
1679 if (ShrinkTy)
1680 if (auto *VecTy = dyn_cast<VectorType>(Ty))
1681 ShrinkTy = VectorType::get(ShrinkTy, VecTy);
1682
1683 return ShrinkTy;
1684}
1685
1686// Determine if this is a vector of ConstantFPs and if so, return the minimal
1687// type we can safely truncate all elements to.
1688static Type *shrinkFPConstantVector(Value *V, bool PreferBFloat) {
1689 auto *CV = dyn_cast<Constant>(V);
1690 auto *CVVTy = dyn_cast<FixedVectorType>(V->getType());
1691 if (!CV || !CVVTy)
1692 return nullptr;
1693
1694 Type *MinType = nullptr;
1695
1696 unsigned NumElts = CVVTy->getNumElements();
1697
1698 // For fixed-width vectors we find the minimal type by looking
1699 // through the constant values of the vector.
1700 for (unsigned i = 0; i != NumElts; ++i) {
1701 if (isa<UndefValue>(CV->getAggregateElement(i)))
1702 continue;
1703
1704 auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
1705 if (!CFP)
1706 return nullptr;
1707
1708 Type *T = shrinkFPConstant(CFP, PreferBFloat);
1709 if (!T)
1710 return nullptr;
1711
1712 // If we haven't found a type yet or this type has a larger mantissa than
1713 // our previous type, this is our new minimal type.
1714 if (!MinType || T->getFPMantissaWidth() > MinType->getFPMantissaWidth())
1715 MinType = T;
1716 }
1717
1718 // Make a vector type from the minimal type.
1719 return MinType ? FixedVectorType::get(MinType, NumElts) : nullptr;
1720}
1721
1722/// Find the minimum FP type we can safely truncate to.
1723static Type *getMinimumFPType(Value *V, bool PreferBFloat) {
1724 if (auto *FPExt = dyn_cast<FPExtInst>(V))
1725 return FPExt->getOperand(0)->getType();
1726
1727 // If this value is a constant, return the constant in the smallest FP type
1728 // that can accurately represent it. This allows us to turn
1729 // (float)((double)X+2.0) into x+2.0f.
1730 if (auto *CFP = dyn_cast<ConstantFP>(V))
1731 if (Type *T = shrinkFPConstant(CFP, PreferBFloat))
1732 return T;
1733
1734 // Try to shrink scalable and fixed splat vectors.
1735 if (auto *FPC = dyn_cast<Constant>(V))
1736 if (auto *VTy = dyn_cast<VectorType>(V->getType()))
1737 if (auto *Splat = dyn_cast_or_null<ConstantFP>(FPC->getSplatValue()))
1738 if (Type *T = shrinkFPConstant(Splat, PreferBFloat))
1739 return VectorType::get(T, VTy);
1740
1741 // Try to shrink a vector of FP constants. This returns nullptr on scalable
1742 // vectors
1743 if (Type *T = shrinkFPConstantVector(V, PreferBFloat))
1744 return T;
1745
1746 return V->getType();
1747}
1748
1749/// Return true if the cast from integer to FP can be proven to be exact for all
1750/// possible inputs (the conversion does not lose any precision).
1752 CastInst::CastOps Opcode = I.getOpcode();
1753 assert((Opcode == CastInst::SIToFP || Opcode == CastInst::UIToFP) &&
1754 "Unexpected cast");
1755 Value *Src = I.getOperand(0);
1756 Type *SrcTy = Src->getType();
1757 Type *FPTy = I.getType();
1758 bool IsSigned = Opcode == Instruction::SIToFP;
1759 int SrcSize = (int)SrcTy->getScalarSizeInBits() - IsSigned;
1760
1761 // Easy case - if the source integer type has less bits than the FP mantissa,
1762 // then the cast must be exact.
1763 int DestNumSigBits = FPTy->getFPMantissaWidth();
1764 if (SrcSize <= DestNumSigBits)
1765 return true;
1766
1767 // Cast from FP to integer and back to FP is independent of the intermediate
1768 // integer width because of poison on overflow.
1769 Value *F;
1770 if (match(Src, m_FPToSI(m_Value(F))) || match(Src, m_FPToUI(m_Value(F)))) {
1771 // If this is uitofp (fptosi F), the source needs an extra bit to avoid
1772 // potential rounding of negative FP input values.
1773 int SrcNumSigBits = F->getType()->getFPMantissaWidth();
1774 if (!IsSigned && match(Src, m_FPToSI(m_Value())))
1775 SrcNumSigBits++;
1776
1777 // [su]itofp (fpto[su]i F) --> exact if the source type has less or equal
1778 // significant bits than the destination (and make sure neither type is
1779 // weird -- ppc_fp128).
1780 if (SrcNumSigBits > 0 && DestNumSigBits > 0 &&
1781 SrcNumSigBits <= DestNumSigBits)
1782 return true;
1783 }
1784
1785 // TODO:
1786 // Try harder to find if the source integer type has less significant bits.
1787 // For example, compute number of sign bits.
1788 KnownBits SrcKnown = IC.computeKnownBits(Src, &I);
1789 int SigBits = (int)SrcTy->getScalarSizeInBits() -
1790 SrcKnown.countMinLeadingZeros() -
1791 SrcKnown.countMinTrailingZeros();
1792 if (SigBits <= DestNumSigBits)
1793 return true;
1794
1795 return false;
1796}
1797
1800 return I;
1801
1802 // If we have fptrunc(OpI (fpextend x), (fpextend y)), we would like to
1803 // simplify this expression to avoid one or more of the trunc/extend
1804 // operations if we can do so without changing the numerical results.
1805 //
1806 // The exact manner in which the widths of the operands interact to limit
1807 // what we can and cannot do safely varies from operation to operation, and
1808 // is explained below in the various case statements.
1809 Type *Ty = FPT.getType();
1810 auto *BO = dyn_cast<BinaryOperator>(FPT.getOperand(0));
1811 if (BO && BO->hasOneUse()) {
1812 bool PreferBFloat = Ty->getScalarType()->isBFloatTy();
1813 Type *LHSMinType = getMinimumFPType(BO->getOperand(0), PreferBFloat);
1814 Type *RHSMinType = getMinimumFPType(BO->getOperand(1), PreferBFloat);
1815 unsigned OpWidth = BO->getType()->getFPMantissaWidth();
1816 unsigned LHSWidth = LHSMinType->getFPMantissaWidth();
1817 unsigned RHSWidth = RHSMinType->getFPMantissaWidth();
1818 unsigned SrcWidth = std::max(LHSWidth, RHSWidth);
1819 unsigned DstWidth = Ty->getFPMantissaWidth();
1820 switch (BO->getOpcode()) {
1821 default: break;
1822 case Instruction::FAdd:
1823 case Instruction::FSub:
1824 // For addition and subtraction, the infinitely precise result can
1825 // essentially be arbitrarily wide; proving that double rounding
1826 // will not occur because the result of OpI is exact (as we will for
1827 // FMul, for example) is hopeless. However, we *can* nonetheless
1828 // frequently know that double rounding cannot occur (or that it is
1829 // innocuous) by taking advantage of the specific structure of
1830 // infinitely-precise results that admit double rounding.
1831 //
1832 // Specifically, if OpWidth >= 2*DstWdith+1 and DstWidth is sufficient
1833 // to represent both sources, we can guarantee that the double
1834 // rounding is innocuous (See p50 of Figueroa's 2000 PhD thesis,
1835 // "A Rigorous Framework for Fully Supporting the IEEE Standard ..."
1836 // for proof of this fact).
1837 //
1838 // Note: Figueroa does not consider the case where DstFormat !=
1839 // SrcFormat. It's possible (likely even!) that this analysis
1840 // could be tightened for those cases, but they are rare (the main
1841 // case of interest here is (float)((double)float + float)).
1842 if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) {
1843 Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty);
1844 Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty);
1845 Instruction *RI = BinaryOperator::Create(BO->getOpcode(), LHS, RHS);
1846 RI->copyFastMathFlags(BO);
1847 return RI;
1848 }
1849 break;
1850 case Instruction::FMul:
1851 // For multiplication, the infinitely precise result has at most
1852 // LHSWidth + RHSWidth significant bits; if OpWidth is sufficient
1853 // that such a value can be exactly represented, then no double
1854 // rounding can possibly occur; we can safely perform the operation
1855 // in the destination format if it can represent both sources.
1856 if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) {
1857 Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty);
1858 Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty);
1859 return BinaryOperator::CreateFMulFMF(LHS, RHS, BO);
1860 }
1861 break;
1862 case Instruction::FDiv:
1863 // For division, we use again use the bound from Figueroa's
1864 // dissertation. I am entirely certain that this bound can be
1865 // tightened in the unbalanced operand case by an analysis based on
1866 // the diophantine rational approximation bound, but the well-known
1867 // condition used here is a good conservative first pass.
1868 // TODO: Tighten bound via rigorous analysis of the unbalanced case.
1869 if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) {
1870 Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty);
1871 Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty);
1872 return BinaryOperator::CreateFDivFMF(LHS, RHS, BO);
1873 }
1874 break;
1875 case Instruction::FRem: {
1876 // Remainder is straightforward. Remainder is always exact, so the
1877 // type of OpI doesn't enter into things at all. We simply evaluate
1878 // in whichever source type is larger, then convert to the
1879 // destination type.
1880 if (SrcWidth == OpWidth)
1881 break;
1882 Value *LHS, *RHS;
1883 if (LHSWidth == SrcWidth) {
1884 LHS = Builder.CreateFPTrunc(BO->getOperand(0), LHSMinType);
1885 RHS = Builder.CreateFPTrunc(BO->getOperand(1), LHSMinType);
1886 } else {
1887 LHS = Builder.CreateFPTrunc(BO->getOperand(0), RHSMinType);
1888 RHS = Builder.CreateFPTrunc(BO->getOperand(1), RHSMinType);
1889 }
1890
1891 Value *ExactResult = Builder.CreateFRemFMF(LHS, RHS, BO);
1892 return CastInst::CreateFPCast(ExactResult, Ty);
1893 }
1894 }
1895 }
1896
1897 // (fptrunc (fneg x)) -> (fneg (fptrunc x))
1898 Value *X;
1900 if (Op && Op->hasOneUse()) {
1901 FastMathFlags FMF = FPT.getFastMathFlags();
1902 if (auto *FPMO = dyn_cast<FPMathOperator>(Op))
1903 FMF &= FPMO->getFastMathFlags();
1904
1905 if (match(Op, m_FNeg(m_Value(X)))) {
1906 Value *InnerTrunc = Builder.CreateFPTruncFMF(X, Ty, FMF);
1907 Value *Neg = Builder.CreateFNegFMF(InnerTrunc, FMF);
1908 return replaceInstUsesWith(FPT, Neg);
1909 }
1910
1911 // If we are truncating a select that has an extended operand, we can
1912 // narrow the other operand and do the select as a narrow op.
1913 Value *Cond, *X, *Y;
1915 X->getType() == Ty) {
1916 // fptrunc (select Cond, (fpext X), Y --> select Cond, X, (fptrunc Y)
1917 Value *NarrowY = Builder.CreateFPTruncFMF(Y, Ty, FMF);
1918 Value *Sel =
1919 Builder.CreateSelectFMF(Cond, X, NarrowY, FMF, "narrow.sel", Op);
1920 return replaceInstUsesWith(FPT, Sel);
1921 }
1923 X->getType() == Ty) {
1924 // fptrunc (select Cond, Y, (fpext X) --> select Cond, (fptrunc Y), X
1925 Value *NarrowY = Builder.CreateFPTruncFMF(Y, Ty, FMF);
1926 Value *Sel =
1927 Builder.CreateSelectFMF(Cond, NarrowY, X, FMF, "narrow.sel", Op);
1928 return replaceInstUsesWith(FPT, Sel);
1929 }
1930 }
1931
1932 if (auto *II = dyn_cast<IntrinsicInst>(FPT.getOperand(0))) {
1933 switch (II->getIntrinsicID()) {
1934 default: break;
1935 case Intrinsic::ceil:
1936 case Intrinsic::fabs:
1937 case Intrinsic::floor:
1938 case Intrinsic::nearbyint:
1939 case Intrinsic::rint:
1940 case Intrinsic::round:
1941 case Intrinsic::roundeven:
1942 case Intrinsic::trunc: {
1943 Value *Src = II->getArgOperand(0);
1944 if (!Src->hasOneUse())
1945 break;
1946
1947 // Except for fabs, this transformation requires the input of the unary FP
1948 // operation to be itself an fpext from the type to which we're
1949 // truncating.
1950 if (II->getIntrinsicID() != Intrinsic::fabs) {
1951 FPExtInst *FPExtSrc = dyn_cast<FPExtInst>(Src);
1952 if (!FPExtSrc || FPExtSrc->getSrcTy() != Ty)
1953 break;
1954 }
1955
1956 // Do unary FP operation on smaller type.
1957 // (fptrunc (fabs x)) -> (fabs (fptrunc x))
1958 Value *InnerTrunc = Builder.CreateFPTrunc(Src, Ty);
1960 FPT.getModule(), II->getIntrinsicID(), Ty);
1962 II->getOperandBundlesAsDefs(OpBundles);
1963 CallInst *NewCI =
1964 CallInst::Create(Overload, {InnerTrunc}, OpBundles, II->getName());
1965 // A normal value may be converted to an infinity. It means that we cannot
1966 // propagate ninf from the intrinsic. So we propagate FMF from fptrunc.
1967 NewCI->copyFastMathFlags(&FPT);
1968 return NewCI;
1969 }
1970 }
1971 }
1972
1973 if (Instruction *I = shrinkInsertElt(FPT, Builder))
1974 return I;
1975
1976 Value *Src = FPT.getOperand(0);
1977 if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
1978 auto *FPCast = cast<CastInst>(Src);
1979 if (isKnownExactCastIntToFP(*FPCast, *this))
1980 return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty);
1981 }
1982
1983 return nullptr;
1984}
1985
1987 // If the source operand is a cast from integer to FP and known exact, then
1988 // cast the integer operand directly to the destination type.
1989 Type *Ty = FPExt.getType();
1990 Value *Src = FPExt.getOperand(0);
1991 if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) {
1992 auto *FPCast = cast<CastInst>(Src);
1993 if (isKnownExactCastIntToFP(*FPCast, *this))
1994 return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty);
1995 }
1996
1997 return commonCastTransforms(FPExt);
1998}
1999
2000/// fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X)
2001/// This is safe if the intermediate type has enough bits in its mantissa to
2002/// accurately represent all values of X. For example, this won't work with
2003/// i64 -> float -> i64.
2006 return nullptr;
2007
2008 auto *OpI = cast<CastInst>(FI.getOperand(0));
2009 Value *X = OpI->getOperand(0);
2010 Type *XType = X->getType();
2011 Type *DestType = FI.getType();
2012 bool IsOutputSigned = isa<FPToSIInst>(FI);
2013
2014 // Since we can assume the conversion won't overflow, our decision as to
2015 // whether the input will fit in the float should depend on the minimum
2016 // of the input range and output range.
2017
2018 // This means this is also safe for a signed input and unsigned output, since
2019 // a negative input would lead to undefined behavior.
2020 if (!isKnownExactCastIntToFP(*OpI, *this)) {
2021 // The first cast may not round exactly based on the source integer width
2022 // and FP width, but the overflow UB rules can still allow this to fold.
2023 // If the destination type is narrow, that means the intermediate FP value
2024 // must be large enough to hold the source value exactly.
2025 // For example, (uint8_t)((float)(uint32_t 16777217) is undefined behavior.
2026 int OutputSize = (int)DestType->getScalarSizeInBits();
2027 if (OutputSize > OpI->getType()->getFPMantissaWidth())
2028 return nullptr;
2029 }
2030
2031 if (DestType->getScalarSizeInBits() > XType->getScalarSizeInBits()) {
2032 bool IsInputSigned = isa<SIToFPInst>(OpI);
2033 if (IsInputSigned && IsOutputSigned)
2034 return new SExtInst(X, DestType);
2035 return new ZExtInst(X, DestType);
2036 }
2037 if (DestType->getScalarSizeInBits() < XType->getScalarSizeInBits())
2038 return new TruncInst(X, DestType);
2039
2040 assert(XType == DestType && "Unexpected types for int to FP to int casts");
2041 return replaceInstUsesWith(FI, X);
2042}
2043
2045 // fpto{u/s}i non-norm --> 0
2046 FPClassTest Mask =
2047 FI.getOpcode() == Instruction::FPToUI ? fcPosNormal : fcNormal;
2049 FI.getOperand(0), Mask, IC.getSimplifyQuery().getWithInstruction(&FI));
2050 if (FPClass.isKnownNever(Mask))
2052
2053 return nullptr;
2054}
2055
2057 if (Instruction *I = foldItoFPtoI(FI))
2058 return I;
2059
2060 if (Instruction *I = foldFPtoI(FI, *this))
2061 return I;
2062
2063 return commonCastTransforms(FI);
2064}
2065
2067 if (Instruction *I = foldItoFPtoI(FI))
2068 return I;
2069
2070 if (Instruction *I = foldFPtoI(FI, *this))
2071 return I;
2072
2073 return commonCastTransforms(FI);
2074}
2075
2077 if (Instruction *R = commonCastTransforms(CI))
2078 return R;
2079 if (!CI.hasNonNeg() && isKnownNonNegative(CI.getOperand(0), SQ)) {
2080 CI.setNonNeg();
2081 return &CI;
2082 }
2083 return nullptr;
2084}
2085
2087 if (Instruction *R = commonCastTransforms(CI))
2088 return R;
2089 if (isKnownNonNegative(CI.getOperand(0), SQ)) {
2090 auto *UI =
2091 CastInst::Create(Instruction::UIToFP, CI.getOperand(0), CI.getType());
2092 UI->setNonNeg(true);
2093 return UI;
2094 }
2095 return nullptr;
2096}
2097
2099 // If the source integer type is not the intptr_t type for this target, do a
2100 // trunc or zext to the intptr_t type, then inttoptr of it. This allows the
2101 // cast to be exposed to other transforms.
2102 unsigned AS = CI.getAddressSpace();
2103 if (CI.getOperand(0)->getType()->getScalarSizeInBits() !=
2104 DL.getPointerSizeInBits(AS)) {
2105 Type *Ty = CI.getOperand(0)->getType()->getWithNewType(
2106 DL.getIntPtrType(CI.getContext(), AS));
2107 Value *P = Builder.CreateZExtOrTrunc(CI.getOperand(0), Ty);
2108 return new IntToPtrInst(P, CI.getType());
2109 }
2110
2111 // Replace (inttoptr (add (ptrtoint %Base), %Offset)) with
2112 // (getelementptr i8, %Base, %Offset) if the pointer is only used as integer
2113 // value.
2114 Value *Base;
2115 Value *Offset;
2116 auto UsesPointerAsInt = [](User *U) {
2118 return true;
2119 if (auto *P = dyn_cast<PHINode>(U))
2120 return P->hasOneUse() && isa<ICmpInst, PtrToIntInst>(*P->user_begin());
2121 return false;
2122 };
2123 if (match(CI.getOperand(0),
2125 m_Value(Offset)))) &&
2127 Base->getType()->getPointerAddressSpace() &&
2128 all_of(CI.users(), UsesPointerAsInt)) {
2129 return GetElementPtrInst::Create(Builder.getInt8Ty(), Base, Offset);
2130 }
2131
2133 return I;
2134
2135 return nullptr;
2136}
2137
2139 // Look through chain of one-use GEPs.
2140 Type *PtrTy = Ptr->getType();
2142 while (true) {
2143 auto *GEP = dyn_cast<GEPOperator>(Ptr);
2144 if (!GEP || !GEP->hasOneUse())
2145 break;
2146 GEPs.push_back(GEP);
2147 Ptr = GEP->getPointerOperand();
2148 }
2149
2150 // Don't handle case where GEP converts from pointer to vector.
2151 if (GEPs.empty() || PtrTy != Ptr->getType())
2152 return nullptr;
2153
2154 // Check whether we know the integer value of the base pointer.
2155 Value *Res;
2156 Type *IdxTy = DL.getIndexType(PtrTy);
2157 if (match(Ptr, m_OneUse(m_IntToPtr(m_Value(Res)))) &&
2158 Res->getType() == IntTy && IntTy == IdxTy) {
2159 // pass
2160 } else if (isa<ConstantPointerNull>(Ptr)) {
2161 Res = Constant::getNullValue(IdxTy);
2162 } else {
2163 return nullptr;
2164 }
2165
2166 // Perform the entire operation on integers instead.
2167 for (GEPOperator *GEP : reverse(GEPs)) {
2168 Value *Offset = EmitGEPOffset(GEP);
2169 Res = Builder.CreateAdd(Res, Offset, "", GEP->hasNoUnsignedWrap());
2170 }
2171 return Builder.CreateZExtOrTrunc(Res, IntTy);
2172}
2173
2175 // If the destination integer type is not the intptr_t type for this target,
2176 // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast
2177 // to be exposed to other transforms.
2179 Type *SrcTy = SrcOp->getType();
2180 Type *Ty = CI.getType();
2181 unsigned AS = CI.getPointerAddressSpace();
2182 unsigned TySize = Ty->getScalarSizeInBits();
2183 unsigned PtrSize = DL.getPointerSizeInBits(AS);
2184 if (TySize != PtrSize) {
2185 Type *IntPtrTy =
2186 SrcTy->getWithNewType(DL.getIntPtrType(CI.getContext(), AS));
2187 Value *P = Builder.CreatePtrToInt(SrcOp, IntPtrTy);
2188 return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
2189 }
2190
2191 // (ptrtoint (ptrmask P, M))
2192 // -> (and (ptrtoint P), M)
2193 // This is generally beneficial as `and` is better supported than `ptrmask`.
2194 Value *Ptr, *Mask;
2196 m_Value(Mask)))) &&
2197 Mask->getType() == Ty)
2198 return BinaryOperator::CreateAnd(Builder.CreatePtrToInt(Ptr, Ty), Mask);
2199
2200 if (Value *V = foldPtrToIntOfGEP(Ty, SrcOp))
2201 return replaceInstUsesWith(CI, V);
2202
2203 Value *Vec, *Scalar, *Index;
2205 m_Value(Scalar), m_Value(Index)))) &&
2206 Vec->getType() == Ty) {
2207 assert(Vec->getType()->getScalarSizeInBits() == PtrSize && "Wrong type");
2208 // Convert the scalar to int followed by insert to eliminate one cast:
2209 // p2i (ins (i2p Vec), Scalar, Index --> ins Vec, (p2i Scalar), Index
2210 Value *NewCast = Builder.CreatePtrToInt(Scalar, Ty->getScalarType());
2211 return InsertElementInst::Create(Vec, NewCast, Index);
2212 }
2213
2214 return commonCastTransforms(CI);
2215}
2216
2218 // FIXME: Implement variants of ptrtoint folds.
2219 return commonCastTransforms(CI);
2220}
2221
2222/// This input value (which is known to have vector type) is being zero extended
2223/// or truncated to the specified vector type. Since the zext/trunc is done
2224/// using an integer type, we have a (bitcast(cast(bitcast))) pattern,
2225/// endianness will impact which end of the vector that is extended or
2226/// truncated.
2227///
2228/// A vector is always stored with index 0 at the lowest address, which
2229/// corresponds to the most significant bits for a big endian stored integer and
2230/// the least significant bits for little endian. A trunc/zext of an integer
2231/// impacts the big end of the integer. Thus, we need to add/remove elements at
2232/// the front of the vector for big endian targets, and the back of the vector
2233/// for little endian targets.
2234///
2235/// Try to replace it with a shuffle (and vector/vector bitcast) if possible.
2236///
2237/// The source and destination vector types may have different element types.
2238static Instruction *
2240 InstCombinerImpl &IC) {
2241 // We can only do this optimization if the output is a multiple of the input
2242 // element size, or the input is a multiple of the output element size.
2243 // Convert the input type to have the same element type as the output.
2244 VectorType *SrcTy = cast<VectorType>(InVal->getType());
2245
2246 if (SrcTy->getElementType() != DestTy->getElementType()) {
2247 // The input types don't need to be identical, but for now they must be the
2248 // same size. There is no specific reason we couldn't handle things like
2249 // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten
2250 // there yet.
2251 if (SrcTy->getElementType()->getPrimitiveSizeInBits() !=
2252 DestTy->getElementType()->getPrimitiveSizeInBits())
2253 return nullptr;
2254
2255 SrcTy =
2256 FixedVectorType::get(DestTy->getElementType(),
2257 cast<FixedVectorType>(SrcTy)->getNumElements());
2258 InVal = IC.Builder.CreateBitCast(InVal, SrcTy);
2259 }
2260
2261 bool IsBigEndian = IC.getDataLayout().isBigEndian();
2262 unsigned SrcElts = cast<FixedVectorType>(SrcTy)->getNumElements();
2263 unsigned DestElts = cast<FixedVectorType>(DestTy)->getNumElements();
2264
2265 assert(SrcElts != DestElts && "Element counts should be different.");
2266
2267 // Now that the element types match, get the shuffle mask and RHS of the
2268 // shuffle to use, which depends on whether we're increasing or decreasing the
2269 // size of the input.
2270 auto ShuffleMaskStorage = llvm::to_vector<16>(llvm::seq<int>(0, SrcElts));
2271 ArrayRef<int> ShuffleMask;
2272 Value *V2;
2273
2274 if (SrcElts > DestElts) {
2275 // If we're shrinking the number of elements (rewriting an integer
2276 // truncate), just shuffle in the elements corresponding to the least
2277 // significant bits from the input and use poison as the second shuffle
2278 // input.
2279 V2 = PoisonValue::get(SrcTy);
2280 // Make sure the shuffle mask selects the "least significant bits" by
2281 // keeping elements from back of the src vector for big endian, and from the
2282 // front for little endian.
2283 ShuffleMask = ShuffleMaskStorage;
2284 if (IsBigEndian)
2285 ShuffleMask = ShuffleMask.take_back(DestElts);
2286 else
2287 ShuffleMask = ShuffleMask.take_front(DestElts);
2288 } else {
2289 // If we're increasing the number of elements (rewriting an integer zext),
2290 // shuffle in all of the elements from InVal. Fill the rest of the result
2291 // elements with zeros from a constant zero.
2292 V2 = Constant::getNullValue(SrcTy);
2293 // Use first elt from V2 when indicating zero in the shuffle mask.
2294 uint32_t NullElt = SrcElts;
2295 // Extend with null values in the "most significant bits" by adding elements
2296 // in front of the src vector for big endian, and at the back for little
2297 // endian.
2298 unsigned DeltaElts = DestElts - SrcElts;
2299 if (IsBigEndian)
2300 ShuffleMaskStorage.insert(ShuffleMaskStorage.begin(), DeltaElts, NullElt);
2301 else
2302 ShuffleMaskStorage.append(DeltaElts, NullElt);
2303 ShuffleMask = ShuffleMaskStorage;
2304 }
2305
2306 return new ShuffleVectorInst(InVal, V2, ShuffleMask);
2307}
2308
2309static bool isMultipleOfTypeSize(unsigned Value, Type *Ty) {
2310 return Value % Ty->getPrimitiveSizeInBits() == 0;
2311}
2312
2313static unsigned getTypeSizeIndex(unsigned Value, Type *Ty) {
2314 return Value / Ty->getPrimitiveSizeInBits();
2315}
2316
2317/// V is a value which is inserted into a vector of VecEltTy.
2318/// Look through the value to see if we can decompose it into
2319/// insertions into the vector. See the example in the comment for
2320/// OptimizeIntegerToVectorInsertions for the pattern this handles.
2321/// The type of V is always a non-zero multiple of VecEltTy's size.
2322/// Shift is the number of bits between the lsb of V and the lsb of
2323/// the vector.
2324///
2325/// This returns false if the pattern can't be matched or true if it can,
2326/// filling in Elements with the elements found here.
2327static bool collectInsertionElements(Value *V, unsigned Shift,
2328 SmallVectorImpl<Value *> &Elements,
2329 Type *VecEltTy, bool isBigEndian) {
2330 assert(isMultipleOfTypeSize(Shift, VecEltTy) &&
2331 "Shift should be a multiple of the element type size");
2332
2333 // Undef values never contribute useful bits to the result.
2334 if (isa<UndefValue>(V)) return true;
2335
2336 // If we got down to a value of the right type, we win, try inserting into the
2337 // right element.
2338 if (V->getType() == VecEltTy) {
2339 // Inserting null doesn't actually insert any elements.
2340 if (Constant *C = dyn_cast<Constant>(V))
2341 if (C->isNullValue())
2342 return true;
2343
2344 unsigned ElementIndex = getTypeSizeIndex(Shift, VecEltTy);
2345 if (isBigEndian)
2346 ElementIndex = Elements.size() - ElementIndex - 1;
2347
2348 // Fail if multiple elements are inserted into this slot.
2349 if (Elements[ElementIndex])
2350 return false;
2351
2352 Elements[ElementIndex] = V;
2353 return true;
2354 }
2355
2356 if (Constant *C = dyn_cast<Constant>(V)) {
2357 // Figure out the # elements this provides, and bitcast it or slice it up
2358 // as required.
2359 unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(),
2360 VecEltTy);
2361 // If the constant is the size of a vector element, we just need to bitcast
2362 // it to the right type so it gets properly inserted.
2363 if (NumElts == 1)
2365 Shift, Elements, VecEltTy, isBigEndian);
2366
2367 // Okay, this is a constant that covers multiple elements. Slice it up into
2368 // pieces and insert each element-sized piece into the vector.
2369 if (!isa<IntegerType>(C->getType()))
2370 C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(),
2371 C->getType()->getPrimitiveSizeInBits()));
2372 unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits();
2373 Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
2374
2375 for (unsigned i = 0; i != NumElts; ++i) {
2376 unsigned ShiftI = i * ElementSize;
2378 Instruction::LShr, C, ConstantInt::get(C->getType(), ShiftI));
2379 if (!Piece)
2380 return false;
2381
2382 Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
2383 if (!collectInsertionElements(Piece, ShiftI + Shift, Elements, VecEltTy,
2384 isBigEndian))
2385 return false;
2386 }
2387 return true;
2388 }
2389
2390 if (!V->hasOneUse()) return false;
2391
2393 if (!I) return false;
2394 switch (I->getOpcode()) {
2395 default: return false; // Unhandled case.
2396 case Instruction::BitCast:
2397 if (I->getOperand(0)->getType()->isVectorTy())
2398 return false;
2399 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2400 isBigEndian);
2401 case Instruction::ZExt:
2403 I->getOperand(0)->getType()->getPrimitiveSizeInBits(),
2404 VecEltTy))
2405 return false;
2406 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2407 isBigEndian);
2408 case Instruction::Or:
2409 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2410 isBigEndian) &&
2411 collectInsertionElements(I->getOperand(1), Shift, Elements, VecEltTy,
2412 isBigEndian);
2413 case Instruction::Shl: {
2414 // Must be shifting by a constant that is a multiple of the element size.
2415 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1));
2416 if (!CI) return false;
2417 Shift += CI->getZExtValue();
2418 if (!isMultipleOfTypeSize(Shift, VecEltTy)) return false;
2419 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy,
2420 isBigEndian);
2421 }
2422
2423 }
2424}
2425
2426
2427/// If the input is an 'or' instruction, we may be doing shifts and ors to
2428/// assemble the elements of the vector manually.
2429/// Try to rip the code out and replace it with insertelements. This is to
2430/// optimize code like this:
2431///
2432/// %tmp37 = bitcast float %inc to i32
2433/// %tmp38 = zext i32 %tmp37 to i64
2434/// %tmp31 = bitcast float %inc5 to i32
2435/// %tmp32 = zext i32 %tmp31 to i64
2436/// %tmp33 = shl i64 %tmp32, 32
2437/// %ins35 = or i64 %tmp33, %tmp38
2438/// %tmp43 = bitcast i64 %ins35 to <2 x float>
2439///
2440/// Into two insertelements that do "buildvector{%inc, %inc5}".
2442 InstCombinerImpl &IC) {
2443 auto *DestVecTy = cast<FixedVectorType>(CI.getType());
2444 Value *IntInput = CI.getOperand(0);
2445
2446 // if the int input is just an undef value do not try to optimize to vector
2447 // insertions as it will prevent undef propagation
2448 if (isa<UndefValue>(IntInput))
2449 return nullptr;
2450
2451 SmallVector<Value*, 8> Elements(DestVecTy->getNumElements());
2452 if (!collectInsertionElements(IntInput, 0, Elements,
2453 DestVecTy->getElementType(),
2454 IC.getDataLayout().isBigEndian()))
2455 return nullptr;
2456
2457 // If we succeeded, we know that all of the element are specified by Elements
2458 // or are zero if Elements has a null entry. Recast this as a set of
2459 // insertions.
2460 Value *Result = Constant::getNullValue(CI.getType());
2461 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
2462 if (!Elements[i]) continue; // Unset element.
2463
2464 Result = IC.Builder.CreateInsertElement(Result, Elements[i],
2465 IC.Builder.getInt32(i));
2466 }
2467
2468 return Result;
2469}
2470
2471/// Canonicalize scalar bitcasts of extracted elements into a bitcast of the
2472/// vector followed by extract element. The backend tends to handle bitcasts of
2473/// vectors better than bitcasts of scalars because vector registers are
2474/// usually not type-specific like scalar integer or scalar floating-point.
2476 InstCombinerImpl &IC) {
2477 Value *VecOp, *Index;
2478 if (!match(BitCast.getOperand(0),
2479 m_OneUse(m_ExtractElt(m_Value(VecOp), m_Value(Index)))))
2480 return nullptr;
2481
2482 // The bitcast must be to a vectorizable type, otherwise we can't make a new
2483 // type to extract from.
2484 Type *DestType = BitCast.getType();
2485 VectorType *VecType = cast<VectorType>(VecOp->getType());
2486 if (VectorType::isValidElementType(DestType)) {
2487 auto *NewVecType = VectorType::get(DestType, VecType);
2488 auto *NewBC = IC.Builder.CreateBitCast(VecOp, NewVecType, "bc");
2489 return ExtractElementInst::Create(NewBC, Index);
2490 }
2491
2492 // Only solve DestType is vector to avoid inverse transform in visitBitCast.
2493 // bitcast (extractelement <1 x elt>, dest) -> bitcast(<1 x elt>, dest)
2494 auto *FixedVType = dyn_cast<FixedVectorType>(VecType);
2495 if (DestType->isVectorTy() && FixedVType && FixedVType->getNumElements() == 1)
2496 return CastInst::Create(Instruction::BitCast, VecOp, DestType);
2497
2498 return nullptr;
2499}
2500
2501/// Change the type of a bitwise logic operation if we can eliminate a bitcast.
2503 InstCombiner::BuilderTy &Builder) {
2504 Type *DestTy = BitCast.getType();
2505 BinaryOperator *BO;
2506
2507 if (!match(BitCast.getOperand(0), m_OneUse(m_BinOp(BO))) ||
2508 !BO->isBitwiseLogicOp())
2509 return nullptr;
2510
2511 // FIXME: This transform is restricted to vector types to avoid backend
2512 // problems caused by creating potentially illegal operations. If a fix-up is
2513 // added to handle that situation, we can remove this check.
2514 if (!DestTy->isVectorTy() || !BO->getType()->isVectorTy())
2515 return nullptr;
2516
2517 if (DestTy->isFPOrFPVectorTy()) {
2518 Value *X, *Y;
2519 // bitcast(logic(bitcast(X), bitcast(Y))) -> bitcast'(logic(bitcast'(X), Y))
2520 if (match(BO->getOperand(0), m_OneUse(m_BitCast(m_Value(X)))) &&
2522 if (X->getType()->isFPOrFPVectorTy() &&
2523 Y->getType()->isIntOrIntVectorTy()) {
2524 Value *CastedOp =
2525 Builder.CreateBitCast(BO->getOperand(0), Y->getType());
2526 Value *NewBO = Builder.CreateBinOp(BO->getOpcode(), CastedOp, Y);
2527 return CastInst::CreateBitOrPointerCast(NewBO, DestTy);
2528 }
2529 if (X->getType()->isIntOrIntVectorTy() &&
2530 Y->getType()->isFPOrFPVectorTy()) {
2531 Value *CastedOp =
2532 Builder.CreateBitCast(BO->getOperand(1), X->getType());
2533 Value *NewBO = Builder.CreateBinOp(BO->getOpcode(), CastedOp, X);
2534 return CastInst::CreateBitOrPointerCast(NewBO, DestTy);
2535 }
2536 }
2537 return nullptr;
2538 }
2539
2540 if (!DestTy->isIntOrIntVectorTy())
2541 return nullptr;
2542
2543 Value *X;
2544 if (match(BO->getOperand(0), m_OneUse(m_BitCast(m_Value(X)))) &&
2545 X->getType() == DestTy && !isa<Constant>(X)) {
2546 // bitcast(logic(bitcast(X), Y)) --> logic'(X, bitcast(Y))
2547 Value *CastedOp1 = Builder.CreateBitCast(BO->getOperand(1), DestTy);
2548 return BinaryOperator::Create(BO->getOpcode(), X, CastedOp1);
2549 }
2550
2551 if (match(BO->getOperand(1), m_OneUse(m_BitCast(m_Value(X)))) &&
2552 X->getType() == DestTy && !isa<Constant>(X)) {
2553 // bitcast(logic(Y, bitcast(X))) --> logic'(bitcast(Y), X)
2554 Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy);
2555 return BinaryOperator::Create(BO->getOpcode(), CastedOp0, X);
2556 }
2557
2558 // Canonicalize vector bitcasts to come before vector bitwise logic with a
2559 // constant. This eases recognition of special constants for later ops.
2560 // Example:
2561 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b
2562 Constant *C;
2563 if (match(BO->getOperand(1), m_Constant(C))) {
2564 // bitcast (logic X, C) --> logic (bitcast X, C')
2565 Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy);
2566 Value *CastedC = Builder.CreateBitCast(C, DestTy);
2567 return BinaryOperator::Create(BO->getOpcode(), CastedOp0, CastedC);
2568 }
2569
2570 return nullptr;
2571}
2572
2573/// Change the type of a select if we can eliminate a bitcast.
2575 InstCombiner::BuilderTy &Builder) {
2576 Value *Cond, *TVal, *FVal;
2577 if (!match(BitCast.getOperand(0),
2578 m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))))
2579 return nullptr;
2580
2581 // A vector select must maintain the same number of elements in its operands.
2582 Type *CondTy = Cond->getType();
2583 Type *DestTy = BitCast.getType();
2584 if (auto *CondVTy = dyn_cast<VectorType>(CondTy))
2585 if (!DestTy->isVectorTy() ||
2586 CondVTy->getElementCount() !=
2587 cast<VectorType>(DestTy)->getElementCount())
2588 return nullptr;
2589
2590 // FIXME: This transform is restricted from changing the select between
2591 // scalars and vectors to avoid backend problems caused by creating
2592 // potentially illegal operations. If a fix-up is added to handle that
2593 // situation, we can remove this check.
2594 if (DestTy->isVectorTy() != TVal->getType()->isVectorTy())
2595 return nullptr;
2596
2597 auto *Sel = cast<Instruction>(BitCast.getOperand(0));
2598 Value *X;
2599 if (match(TVal, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestTy &&
2600 !isa<Constant>(X)) {
2601 // bitcast(select(Cond, bitcast(X), Y)) --> select'(Cond, X, bitcast(Y))
2602 Value *CastedVal = Builder.CreateBitCast(FVal, DestTy);
2603 return SelectInst::Create(Cond, X, CastedVal, "", nullptr, Sel);
2604 }
2605
2606 if (match(FVal, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestTy &&
2607 !isa<Constant>(X)) {
2608 // bitcast(select(Cond, Y, bitcast(X))) --> select'(Cond, bitcast(Y), X)
2609 Value *CastedVal = Builder.CreateBitCast(TVal, DestTy);
2610 return SelectInst::Create(Cond, CastedVal, X, "", nullptr, Sel);
2611 }
2612
2613 return nullptr;
2614}
2615
2616/// Check if all users of CI are StoreInsts.
2617static bool hasStoreUsersOnly(CastInst &CI) {
2618 for (User *U : CI.users()) {
2619 if (!isa<StoreInst>(U))
2620 return false;
2621 }
2622 return true;
2623}
2624
2625/// This function handles following case
2626///
2627/// A -> B cast
2628/// PHI
2629/// B -> A cast
2630///
2631/// All the related PHI nodes can be replaced by new PHI nodes with type A.
2632/// The uses of \p CI can be changed to the new PHI node corresponding to \p PN.
2633Instruction *InstCombinerImpl::optimizeBitCastFromPhi(CastInst &CI,
2634 PHINode *PN) {
2635 // BitCast used by Store can be handled in InstCombineLoadStoreAlloca.cpp.
2636 if (hasStoreUsersOnly(CI))
2637 return nullptr;
2638
2639 Value *Src = CI.getOperand(0);
2640 Type *SrcTy = Src->getType(); // Type B
2641 Type *DestTy = CI.getType(); // Type A
2642
2643 SmallVector<PHINode *, 4> PhiWorklist;
2644 SmallSetVector<PHINode *, 4> OldPhiNodes;
2645
2646 // Find all of the A->B casts and PHI nodes.
2647 // We need to inspect all related PHI nodes, but PHIs can be cyclic, so
2648 // OldPhiNodes is used to track all known PHI nodes, before adding a new
2649 // PHI to PhiWorklist, it is checked against and added to OldPhiNodes first.
2650 PhiWorklist.push_back(PN);
2651 OldPhiNodes.insert(PN);
2652 while (!PhiWorklist.empty()) {
2653 auto *OldPN = PhiWorklist.pop_back_val();
2654 for (Value *IncValue : OldPN->incoming_values()) {
2655 if (isa<Constant>(IncValue))
2656 continue;
2657
2658 if (auto *LI = dyn_cast<LoadInst>(IncValue)) {
2659 // If there is a sequence of one or more load instructions, each loaded
2660 // value is used as address of later load instruction, bitcast is
2661 // necessary to change the value type, don't optimize it. For
2662 // simplicity we give up if the load address comes from another load.
2663 Value *Addr = LI->getOperand(0);
2664 if (Addr == &CI || isa<LoadInst>(Addr))
2665 return nullptr;
2666 // Don't tranform "load <256 x i32>, <256 x i32>*" to
2667 // "load x86_amx, x86_amx*", because x86_amx* is invalid.
2668 // TODO: Remove this check when bitcast between vector and x86_amx
2669 // is replaced with a specific intrinsic.
2670 if (DestTy->isX86_AMXTy())
2671 return nullptr;
2672 if (LI->hasOneUse() && LI->isSimple())
2673 continue;
2674 // If a LoadInst has more than one use, changing the type of loaded
2675 // value may create another bitcast.
2676 return nullptr;
2677 }
2678
2679 if (auto *PNode = dyn_cast<PHINode>(IncValue)) {
2680 if (OldPhiNodes.insert(PNode))
2681 PhiWorklist.push_back(PNode);
2682 continue;
2683 }
2684
2685 auto *BCI = dyn_cast<BitCastInst>(IncValue);
2686 // We can't handle other instructions.
2687 if (!BCI)
2688 return nullptr;
2689
2690 // Verify it's a A->B cast.
2691 Type *TyA = BCI->getOperand(0)->getType();
2692 Type *TyB = BCI->getType();
2693 if (TyA != DestTy || TyB != SrcTy)
2694 return nullptr;
2695 }
2696 }
2697
2698 // Check that each user of each old PHI node is something that we can
2699 // rewrite, so that all of the old PHI nodes can be cleaned up afterwards.
2700 for (auto *OldPN : OldPhiNodes) {
2701 for (User *V : OldPN->users()) {
2702 if (auto *SI = dyn_cast<StoreInst>(V)) {
2703 if (!SI->isSimple() || SI->getOperand(0) != OldPN)
2704 return nullptr;
2705 } else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
2706 // Verify it's a B->A cast.
2707 Type *TyB = BCI->getOperand(0)->getType();
2708 Type *TyA = BCI->getType();
2709 if (TyA != DestTy || TyB != SrcTy)
2710 return nullptr;
2711 } else if (auto *PHI = dyn_cast<PHINode>(V)) {
2712 // As long as the user is another old PHI node, then even if we don't
2713 // rewrite it, the PHI web we're considering won't have any users
2714 // outside itself, so it'll be dead.
2715 if (!OldPhiNodes.contains(PHI))
2716 return nullptr;
2717 } else {
2718 return nullptr;
2719 }
2720 }
2721 }
2722
2723 // For each old PHI node, create a corresponding new PHI node with a type A.
2724 SmallDenseMap<PHINode *, PHINode *> NewPNodes;
2725 for (auto *OldPN : OldPhiNodes) {
2726 Builder.SetInsertPoint(OldPN);
2727 PHINode *NewPN = Builder.CreatePHI(DestTy, OldPN->getNumOperands());
2728 NewPNodes[OldPN] = NewPN;
2729 }
2730
2731 // Fill in the operands of new PHI nodes.
2732 for (auto *OldPN : OldPhiNodes) {
2733 PHINode *NewPN = NewPNodes[OldPN];
2734 for (unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) {
2735 Value *V = OldPN->getOperand(j);
2736 Value *NewV = nullptr;
2737 if (auto *C = dyn_cast<Constant>(V)) {
2738 NewV = ConstantExpr::getBitCast(C, DestTy);
2739 } else if (auto *LI = dyn_cast<LoadInst>(V)) {
2740 // Explicitly perform load combine to make sure no opposing transform
2741 // can remove the bitcast in the meantime and trigger an infinite loop.
2742 Builder.SetInsertPoint(LI);
2743 NewV = combineLoadToNewType(*LI, DestTy);
2744 // Remove the old load and its use in the old phi, which itself becomes
2745 // dead once the whole transform finishes.
2746 replaceInstUsesWith(*LI, PoisonValue::get(LI->getType()));
2748 } else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
2749 NewV = BCI->getOperand(0);
2750 } else if (auto *PrevPN = dyn_cast<PHINode>(V)) {
2751 NewV = NewPNodes[PrevPN];
2752 }
2753 assert(NewV);
2754 NewPN->addIncoming(NewV, OldPN->getIncomingBlock(j));
2755 }
2756 }
2757
2758 // Traverse all accumulated PHI nodes and process its users,
2759 // which are Stores and BitcCasts. Without this processing
2760 // NewPHI nodes could be replicated and could lead to extra
2761 // moves generated after DeSSA.
2762 // If there is a store with type B, change it to type A.
2763
2764
2765 // Replace users of BitCast B->A with NewPHI. These will help
2766 // later to get rid off a closure formed by OldPHI nodes.
2767 Instruction *RetVal = nullptr;
2768 for (auto *OldPN : OldPhiNodes) {
2769 PHINode *NewPN = NewPNodes[OldPN];
2770 for (User *V : make_early_inc_range(OldPN->users())) {
2771 if (auto *SI = dyn_cast<StoreInst>(V)) {
2772 assert(SI->isSimple() && SI->getOperand(0) == OldPN);
2773 Builder.SetInsertPoint(SI);
2774 auto *NewBC =
2775 cast<BitCastInst>(Builder.CreateBitCast(NewPN, SrcTy));
2776 SI->setOperand(0, NewBC);
2777 Worklist.push(SI);
2778 assert(hasStoreUsersOnly(*NewBC));
2779 }
2780 else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
2781 Type *TyB = BCI->getOperand(0)->getType();
2782 Type *TyA = BCI->getType();
2783 assert(TyA == DestTy && TyB == SrcTy);
2784 (void) TyA;
2785 (void) TyB;
2786 Instruction *I = replaceInstUsesWith(*BCI, NewPN);
2787 if (BCI == &CI)
2788 RetVal = I;
2789 } else if (auto *PHI = dyn_cast<PHINode>(V)) {
2790 assert(OldPhiNodes.contains(PHI));
2791 (void) PHI;
2792 } else {
2793 llvm_unreachable("all uses should be handled");
2794 }
2795 }
2796 }
2797
2798 return RetVal;
2799}
2800
2801/// Fold (bitcast (or (and (bitcast X to int), signmask), nneg Y) to fp) to
2802/// copysign((bitcast Y to fp), X)
2804 InstCombiner::BuilderTy &Builder,
2805 const SimplifyQuery &SQ) {
2806 Value *X, *Y;
2807 Type *FTy = CI.getType();
2808 if (!FTy->isFPOrFPVectorTy())
2809 return nullptr;
2812 m_Value(Y)))))
2813 return nullptr;
2814 if (X->getType() != FTy)
2815 return nullptr;
2816 if (!isKnownNonNegative(Y, SQ))
2817 return nullptr;
2818
2819 return Builder.CreateCopySign(Builder.CreateBitCast(Y, FTy), X);
2820}
2821
2823 // If the operands are integer typed then apply the integer transforms,
2824 // otherwise just apply the common ones.
2825 Value *Src = CI.getOperand(0);
2826 Type *SrcTy = Src->getType();
2827 Type *DestTy = CI.getType();
2828
2829 // Get rid of casts from one type to the same type. These are useless and can
2830 // be replaced by the operand.
2831 if (DestTy == Src->getType())
2832 return replaceInstUsesWith(CI, Src);
2833
2834 if (isa<FixedVectorType>(DestTy)) {
2835 if (isa<IntegerType>(SrcTy)) {
2836 // If this is a cast from an integer to vector, check to see if the input
2837 // is a trunc or zext of a bitcast from vector. If so, we can replace all
2838 // the casts with a shuffle and (potentially) a bitcast.
2839 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) {
2840 CastInst *SrcCast = cast<CastInst>(Src);
2841 if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0)))
2842 if (isa<VectorType>(BCIn->getOperand(0)->getType()))
2844 BCIn->getOperand(0), cast<VectorType>(DestTy), *this))
2845 return I;
2846 }
2847
2848 // If the input is an 'or' instruction, we may be doing shifts and ors to
2849 // assemble the elements of the vector manually. Try to rip the code out
2850 // and replace it with insertelements.
2851 if (Value *V = optimizeIntegerToVectorInsertions(CI, *this))
2852 return replaceInstUsesWith(CI, V);
2853 }
2854 }
2855
2856 if (FixedVectorType *SrcVTy = dyn_cast<FixedVectorType>(SrcTy)) {
2857 if (SrcVTy->getNumElements() == 1) {
2858 // If our destination is not a vector, then make this a straight
2859 // scalar-scalar cast.
2860 if (!DestTy->isVectorTy()) {
2861 Value *Elem =
2862 Builder.CreateExtractElement(Src,
2864 return CastInst::Create(Instruction::BitCast, Elem, DestTy);
2865 }
2866
2867 // Otherwise, see if our source is an insert. If so, then use the scalar
2868 // component directly:
2869 // bitcast (inselt <1 x elt> V, X, 0) to <n x m> --> bitcast X to <n x m>
2870 if (auto *InsElt = dyn_cast<InsertElementInst>(Src))
2871 return new BitCastInst(InsElt->getOperand(1), DestTy);
2872 }
2873
2874 // Convert an artificial vector insert into more analyzable bitwise logic.
2875 unsigned BitWidth = DestTy->getScalarSizeInBits();
2876 Value *X, *Y;
2877 uint64_t IndexC;
2879 m_Value(Y), m_ConstantInt(IndexC)))) &&
2880 DestTy->isIntegerTy() && X->getType() == DestTy &&
2881 Y->getType()->isIntegerTy() && isDesirableIntType(BitWidth)) {
2882 // Adjust for big endian - the LSBs are at the high index.
2883 if (DL.isBigEndian())
2884 IndexC = SrcVTy->getNumElements() - 1 - IndexC;
2885
2886 // We only handle (endian-normalized) insert to index 0. Any other insert
2887 // would require a left-shift, so that is an extra instruction.
2888 if (IndexC == 0) {
2889 // bitcast (inselt (bitcast X), Y, 0) --> or (and X, MaskC), (zext Y)
2890 unsigned EltWidth = Y->getType()->getScalarSizeInBits();
2891 APInt MaskC = APInt::getHighBitsSet(BitWidth, BitWidth - EltWidth);
2892 Value *AndX = Builder.CreateAnd(X, MaskC);
2893 Value *ZextY = Builder.CreateZExt(Y, DestTy);
2894 return BinaryOperator::CreateOr(AndX, ZextY);
2895 }
2896 }
2897 }
2898
2899 if (auto *Shuf = dyn_cast<ShuffleVectorInst>(Src)) {
2900 // Okay, we have (bitcast (shuffle ..)). Check to see if this is
2901 // a bitcast to a vector with the same # elts.
2902 Value *ShufOp0 = Shuf->getOperand(0);
2903 Value *ShufOp1 = Shuf->getOperand(1);
2904 auto ShufElts = cast<VectorType>(Shuf->getType())->getElementCount();
2905 auto SrcVecElts = cast<VectorType>(ShufOp0->getType())->getElementCount();
2906 if (Shuf->hasOneUse() && DestTy->isVectorTy() &&
2907 cast<VectorType>(DestTy)->getElementCount() == ShufElts &&
2908 ShufElts == SrcVecElts) {
2909 BitCastInst *Tmp;
2910 // If either of the operands is a cast from CI.getType(), then
2911 // evaluating the shuffle in the casted destination's type will allow
2912 // us to eliminate at least one cast.
2913 if (((Tmp = dyn_cast<BitCastInst>(ShufOp0)) &&
2914 Tmp->getOperand(0)->getType() == DestTy) ||
2915 ((Tmp = dyn_cast<BitCastInst>(ShufOp1)) &&
2916 Tmp->getOperand(0)->getType() == DestTy)) {
2917 Value *LHS = Builder.CreateBitCast(ShufOp0, DestTy);
2918 Value *RHS = Builder.CreateBitCast(ShufOp1, DestTy);
2919 // Return a new shuffle vector. Use the same element ID's, as we
2920 // know the vector types match #elts.
2921 return new ShuffleVectorInst(LHS, RHS, Shuf->getShuffleMask());
2922 }
2923 }
2924
2925 // A bitcasted-to-scalar and byte/bit reversing shuffle is better recognized
2926 // as a byte/bit swap:
2927 // bitcast <N x i8> (shuf X, undef, <N, N-1,...0>) -> bswap (bitcast X)
2928 // bitcast <N x i1> (shuf X, undef, <N, N-1,...0>) -> bitreverse (bitcast X)
2929 if (DestTy->isIntegerTy() && ShufElts.getKnownMinValue() % 2 == 0 &&
2930 Shuf->hasOneUse() && Shuf->isReverse()) {
2931 unsigned IntrinsicNum = 0;
2932 if (DL.isLegalInteger(DestTy->getScalarSizeInBits()) &&
2933 SrcTy->getScalarSizeInBits() == 8) {
2934 IntrinsicNum = Intrinsic::bswap;
2935 } else if (SrcTy->getScalarSizeInBits() == 1) {
2936 IntrinsicNum = Intrinsic::bitreverse;
2937 }
2938 if (IntrinsicNum != 0) {
2939 assert(ShufOp0->getType() == SrcTy && "Unexpected shuffle mask");
2940 assert(match(ShufOp1, m_Undef()) && "Unexpected shuffle op");
2941 Function *BswapOrBitreverse = Intrinsic::getOrInsertDeclaration(
2942 CI.getModule(), IntrinsicNum, DestTy);
2943 Value *ScalarX = Builder.CreateBitCast(ShufOp0, DestTy);
2944 return CallInst::Create(BswapOrBitreverse, {ScalarX});
2945 }
2946 }
2947 }
2948
2949 // Handle the A->B->A cast, and there is an intervening PHI node.
2950 if (PHINode *PN = dyn_cast<PHINode>(Src))
2951 if (Instruction *I = optimizeBitCastFromPhi(CI, PN))
2952 return I;
2953
2954 if (Instruction *I = canonicalizeBitCastExtElt(CI, *this))
2955 return I;
2956
2958 return I;
2959
2961 return I;
2962
2963 if (Value *V = foldCopySignIdioms(CI, Builder, SQ.getWithInstruction(&CI)))
2964 return replaceInstUsesWith(CI, V);
2965
2966 return commonCastTransforms(CI);
2967}
2968
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
static bool isSigned(unsigned int Opcode)
Hexagon Common GEP
static bool collectInsertionElements(Value *V, unsigned Shift, SmallVectorImpl< Value * > &Elements, Type *VecEltTy, bool isBigEndian)
V is a value which is inserted into a vector of VecEltTy.
static bool canEvaluateSExtd(Value *V, Type *Ty)
Return true if we can take the specified value and return it as type Ty without inserting any new cas...
static bool hasStoreUsersOnly(CastInst &CI)
Check if all users of CI are StoreInsts.
static Value * foldCopySignIdioms(BitCastInst &CI, InstCombiner::BuilderTy &Builder, const SimplifyQuery &SQ)
Fold (bitcast (or (and (bitcast X to int), signmask), nneg Y) to fp) to copysign((bitcast Y to fp),...
static Type * shrinkFPConstantVector(Value *V, bool PreferBFloat)
static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear, InstCombinerImpl &IC, Instruction *CxtI)
Determine if the specified value can be computed in the specified wider type and produce the same low...
static Instruction * canonicalizeBitCastExtElt(BitCastInst &BitCast, InstCombinerImpl &IC)
Canonicalize scalar bitcasts of extracted elements into a bitcast of the vector followed by extract e...
static Instruction * shrinkSplatShuffle(TruncInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of a splat shuffle.
static Instruction * foldFPtoI(Instruction &FI, InstCombiner &IC)
static Instruction * foldBitCastSelect(BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a select if we can eliminate a bitcast.
static Instruction * foldBitCastBitwiseLogic(BitCastInst &BitCast, InstCombiner::BuilderTy &Builder)
Change the type of a bitwise logic operation if we can eliminate a bitcast.
static bool fitsInFPType(APFloat F, const fltSemantics &Sem)
Return a Constant* for the specified floating-point constant if it fits in the specified FP type with...
static Instruction * optimizeVectorResizeWithIntegerBitCasts(Value *InVal, VectorType *DestTy, InstCombinerImpl &IC)
This input value (which is known to have vector type) is being zero extended or truncated to the spec...
static Instruction * shrinkInsertElt(CastInst &Trunc, InstCombiner::BuilderTy &Builder)
Try to narrow the width of an insert element.
static Type * getMinimumFPType(Value *V, bool PreferBFloat)
Find the minimum FP type we can safely truncate to.
static bool isMultipleOfTypeSize(unsigned Value, Type *Ty)
static Value * optimizeIntegerToVectorInsertions(BitCastInst &CI, InstCombinerImpl &IC)
If the input is an 'or' instruction, we may be doing shifts and ors to assemble the elements of the v...
static bool canAlwaysEvaluateInType(Value *V, Type *Ty)
Constants and extensions/truncates from the destination type are always free to be evaluated in that ...
static Type * shrinkFPConstant(LLVMContext &Ctx, const APFloat &F, bool PreferBFloat)
static Instruction * foldVecExtTruncToExtElt(TruncInst &Trunc, InstCombinerImpl &IC)
Whenever an element is extracted from a vector, optionally shifted down, and then truncated,...
static bool canNotEvaluateInType(Value *V, Type *Ty)
Filter out values that we can not evaluate in the destination type for free.
static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC)
Return true if the cast from integer to FP can be proven to be exact for all possible inputs (the con...
static unsigned getTypeSizeIndex(unsigned Value, Type *Ty)
static Instruction * foldVecTruncToExtElt(TruncInst &Trunc, InstCombinerImpl &IC)
Given a vector that is bitcast to an integer, optionally logically right-shifted, and truncated,...
static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombinerImpl &IC, Instruction *CxtI)
Return true if we can evaluate the specified expression tree as type Ty instead of its larger type,...
This file provides internal interfaces used to implement the InstCombine.
This file provides the interface for the instcombine pass implementation.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define T
uint64_t IntrinsicInst * II
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file implements a set that has insertion order iteration characteristics.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static unsigned getScalarSizeInBits(Type *Ty)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
static const fltSemantics & BFloat()
Definition APFloat.h:295
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
static LLVM_ABI unsigned int semanticsIntSizeInBits(const fltSemantics &, bool)
Definition APFloat.cpp:310
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
int32_t exactLogBase2() const
Definition APInt.h:1783
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1639
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:286
unsigned countr_one() const
Count the number of trailing one bits.
Definition APInt.h:1656
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
This class represents a conversion between pointers from one address space to another.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static BinaryOperator * CreateFMulFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition InstrTypes.h:244
static BinaryOperator * CreateFDivFMF(Value *V1, Value *V2, FastMathFlags FMF, const Twine &Name="")
Definition InstrTypes.h:248
This class represents a no-op cast from one type to another.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
Type * getSrcTy() const
Return the source type, as a convenience.
Definition InstrTypes.h:615
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Definition InstrTypes.h:610
static LLVM_ABI unsigned isEliminableCastPair(Instruction::CastOps firstOpcode, Instruction::CastOps secondOpcode, Type *SrcTy, Type *MidTy, Type *DstTy, const DataLayout *DL)
Determine how a pair of casts can be eliminated, if they can be at all.
static LLVM_ABI CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
static LLVM_ABI CastInst * CreateFPCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create an FPExt, BitCast, or FPTrunc for fp -> fp casts.
static LLVM_ABI CastInst * CreateTruncOrBitCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a Trunc or BitCast cast instruction.
static LLVM_ABI CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
Type * getDestTy() const
Return the destination type, as a convenience.
Definition InstrTypes.h:617
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
static LLVM_ABI Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
const APFloat & getValueAPF() const
Definition Constants.h:320
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * mergeUndefsWith(Constant *C, Constant *Other)
Merges undefs of a Constant with another Constant, along with the undefs already present.
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 bool isElementWiseEqual(Value *Y) const
Return true if this constant and a constant 'Y' are element-wise equal.
bool isBigEndian() const
Definition DataLayout.h:208
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This class represents an extension of floating point types.
This class represents a cast from floating point to signed integer.
This class represents a cast from floating point to unsigned integer.
This class represents a truncation of floating point types.
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
Class to represent fixed width SIMD vectors.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:803
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:762
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This instruction compares its operands according to the predicate given to the constructor.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition IRBuilder.h:2579
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition IRBuilder.h:522
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition IRBuilder.h:2207
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Instruction * visitZExt(ZExtInst &Zext)
Instruction * visitAddrSpaceCast(AddrSpaceCastInst &CI)
Instruction * visitSExt(SExtInst &Sext)
Instruction * foldOpIntoPhi(Instruction &I, PHINode *PN, bool AllowMultipleUses=false)
Given a binary operator, cast instruction, or select which has a PHI node as operand #0,...
Instruction * visitFPToSI(FPToSIInst &FI)
Instruction * visitTrunc(TruncInst &CI)
Instruction * visitUIToFP(CastInst &CI)
Instruction * visitPtrToInt(PtrToIntInst &CI)
Instruction * FoldOpIntoSelect(Instruction &Op, SelectInst *SI, bool FoldWithMultiUse=false, bool SimplifyBothArms=false)
Given an instruction with a select as one operand and a constant as the other operand,...
Instruction * visitSIToFP(CastInst &CI)
Value * foldPtrToIntOfGEP(Type *IntTy, Value *Ptr)
Instruction * commonCastTransforms(CastInst &CI)
Implement the transforms common to all CastInst visitors.
Instruction * eraseInstFromFunction(Instruction &I) override
Combiner aware instruction erasure.
Instruction * foldItoFPtoI(CastInst &FI)
fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X) This is safe if the intermediate ty...
Instruction * visitFPTrunc(FPTruncInst &CI)
Instruction * visitBitCast(BitCastInst &CI)
Instruction * visitIntToPtr(IntToPtrInst &CI)
Instruction * visitFPToUI(FPToUIInst &FI)
Instruction * visitPtrToAddr(PtrToAddrInst &CI)
Value * EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned)
Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns true for,...
bool SimplifyDemandedInstructionBits(Instruction &Inst)
Tries to simplify operands to an integer instruction based on its demanded bits.
Instruction * visitFPExt(CastInst &CI)
LoadInst * combineLoadToNewType(LoadInst &LI, Type *NewTy, const Twine &Suffix="")
Helper to combine a load to a new type.
The core instruction combiner logic.
SimplifyQuery SQ
const DataLayout & getDataLayout() const
unsigned ComputeMaxSignificantBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
IRBuilder< TargetFolder, IRBuilderCallbackInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
unsigned ComputeNumSignBits(const Value *Op, const Instruction *CxtI=nullptr, unsigned Depth=0) const
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
InstructionWorklist & Worklist
A worklist of the instructions that need to be simplified.
Instruction * InsertNewInstWith(Instruction *New, BasicBlock::iterator Old)
Same as InsertNewInstBefore, but also sets the debug loc.
const DataLayout & DL
void computeKnownBits(const Value *V, KnownBits &Known, const Instruction *CxtI, unsigned Depth=0) const
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const Instruction *CxtI=nullptr, unsigned Depth=0) const
DominatorTree & DT
BuilderTy & Builder
const SimplifyQuery & getSimplifyQuery() const
LLVM_ABI void copyFastMathFlags(FastMathFlags FMF)
Convenience function for transferring all fast-math flag values to this instruction,...
static bool isBitwiseLogicOp(unsigned Opcode)
Determine if the Opcode is and/or/xor.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI void setNonNeg(bool b=true)
Set or clear the nneg flag on this instruction, which must be a zext instruction.
LLVM_ABI bool hasNonNeg() const LLVM_READONLY
Determine whether the the nneg flag is set.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
LLVM_ABI void setIsExact(bool b=true)
Set or clear the exact flag on this instruction, which must be an operator which supports this flag.
This class represents a cast from an integer to a pointer.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:319
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
This class represents a cast from a pointer to an address (non-capturing ptrtoint).
This class represents a cast from a pointer to an integer.
Value * getPointerOperand()
Gets the pointer operand.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:150
This instruction constructs a fixed permutation of two input vectors.
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.
This class represents a truncation of integer types.
void setHasNoSignedWrap(bool B)
void setHasNoUnsignedWrap(bool B)
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:198
LLVM_ABI Type * getWithNewType(Type *EltTy) const
Given vector type, change the element type, whilst keeping the old number of elements.
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
bool isX86_AMXTy() const
Return true if this is X86 AMX.
Definition Type.h:200
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:286
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:285
LLVM_ABI int getFPMantissaWidth() const
Return the width of the mantissa of this type.
Definition Type.cpp:236
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:107
static LLVM_ABI Type * getBFloatTy(LLVMContext &C)
Definition Type.cpp:284
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:283
'undef' values are things that do not have specified contents.
Definition Constants.h:1420
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
This class represents zero extension of integer types.
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:238
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
specific_intval< true > m_SpecificIntAllowPoison(const APInt &V)
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)
Matches logical shift operations.
CastInst_match< OpTy, FPToUIInst > m_FPToUI(const OpTy &Op)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
CastInst_match< OpTy, FPExtInst > m_FPExt(const OpTy &Op)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
cst_pred_ty< is_negated_power2 > m_NegatedPower2()
Match a integer or vector negated power-of-2.
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
CastInst_match< OpTy, FPToSIInst > m_FPToSI(const OpTy &Op)
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
auto m_Undef()
Match an arbitrary undef constant.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::IntToPtr > m_IntToPtr(const OpTy &Op)
Matches IntToPtr.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
LLVM_ABI Constant * ConstantFoldSelectInstruction(Constant *Cond, Constant *V1, Constant *V2)
Attempt to constant fold a select instruction with the specified operands.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
unsigned Log2_64_Ceil(uint64_t Value)
Return the ceil log base 2 of the specified value, 64 if the value is zero.
Definition MathExtras.h:350
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
LLVM_ABI Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)
Point debug users of From to To or salvage them.
Definition Local.cpp:2414
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
@ And
Bitwise or logical AND of integers.
DWARFExpression::Operation Op
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 all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2108
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
Matching combinators.
SimplifyQuery getWithInstruction(const Instruction *I) const