LLVM 22.0.0git
ConstantFold.cpp
Go to the documentation of this file.
1//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
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 folding of constants for LLVM. This implements the
10// (internal) ConstantFold.h interface, which is used by the
11// ConstantExpr::get* methods to automatically fold constants when possible.
12//
13// The current constant folding implementation is implemented in two pieces: the
14// pieces that don't need DataLayout, and the pieces that do. This is to avoid
15// a dependence in IR on Target.
16//
17//===----------------------------------------------------------------------===//
18
20#include "llvm/ADT/APSInt.h"
22#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
25#include "llvm/IR/GlobalAlias.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/Operator.h"
32using namespace llvm;
33using namespace llvm::PatternMatch;
34
35//===----------------------------------------------------------------------===//
36// ConstantFold*Instruction Implementations
37//===----------------------------------------------------------------------===//
38
39/// This function determines which opcode to use to fold two constant cast
40/// expressions together. It uses CastInst::isEliminableCastPair to determine
41/// the opcode. Consequently its just a wrapper around that function.
42/// Determine if it is valid to fold a cast of a cast
43static unsigned
45 unsigned opc, ///< opcode of the second cast constant expression
46 ConstantExpr *Op, ///< the first cast constant expression
47 Type *DstTy ///< destination type of the first cast
48) {
49 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
50 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
51 assert(CastInst::isCast(opc) && "Invalid cast opcode");
52
53 // The types and opcodes for the two Cast constant expressions
54 Type *SrcTy = Op->getOperand(0)->getType();
55 Type *MidTy = Op->getType();
56 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
58 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
59 /*DL=*/nullptr);
60}
61
62static Constant *FoldBitCast(Constant *V, Type *DestTy) {
63 Type *SrcTy = V->getType();
64 if (SrcTy == DestTy)
65 return V; // no-op cast
66
67 if (V->isAllOnesValue())
68 return Constant::getAllOnesValue(DestTy);
69
70 // Handle ConstantInt -> ConstantFP
72 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
73 // This allows for other simplifications (although some of them
74 // can only be handled by Analysis/ConstantFolding.cpp).
75 if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
77
78 // Make sure dest type is compatible with the folded fp constant.
79 // See note below regarding the PPC_FP128 restriction.
80 if (!DestTy->isFPOrFPVectorTy() || DestTy->isPPC_FP128Ty() ||
81 DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits())
82 return nullptr;
83
84 return ConstantFP::get(
85 DestTy,
86 APFloat(DestTy->getScalarType()->getFltSemantics(), CI->getValue()));
87 }
88
89 // Handle ConstantFP -> ConstantInt
91 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
92 // This allows for other simplifications (although some of them
93 // can only be handled by Analysis/ConstantFolding.cpp).
94 if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy))
96
97 // PPC_FP128 is really the sum of two consecutive doubles, where the first
98 // double is always stored first in memory, regardless of the target
99 // endianness. The memory layout of i128, however, depends on the target
100 // endianness, and so we can't fold this without target endianness
101 // information. This should instead be handled by
102 // Analysis/ConstantFolding.cpp
103 if (SrcTy->isPPC_FP128Ty())
104 return nullptr;
105
106 // Make sure dest type is compatible with the folded integer constant.
107 if (!DestTy->isIntOrIntVectorTy() ||
108 DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits())
109 return nullptr;
110
111 return ConstantInt::get(DestTy, FP->getValueAPF().bitcastToAPInt());
112 }
113
114 return nullptr;
115}
116
118 Type *DestTy) {
120 ? ConstantExpr::getCast(opc, V, DestTy)
121 : ConstantFoldCastInstruction(opc, V, DestTy);
122}
123
125 Type *DestTy) {
126 if (isa<PoisonValue>(V))
127 return PoisonValue::get(DestTy);
128
129 if (isa<UndefValue>(V)) {
130 // zext(undef) = 0, because the top bits will be zero.
131 // sext(undef) = 0, because the top bits will all be the same.
132 // [us]itofp(undef) = 0, because the result value is bounded.
133 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
134 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
135 return Constant::getNullValue(DestTy);
136 return UndefValue::get(DestTy);
137 }
138
139 if (V->isNullValue() && !DestTy->isX86_AMXTy() &&
140 opc != Instruction::AddrSpaceCast)
141 return Constant::getNullValue(DestTy);
142
143 // If the cast operand is a constant expression, there's a few things we can
144 // do to try to simplify it.
146 if (CE->isCast()) {
147 // Try hard to fold cast of cast because they are often eliminable.
148 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
149 return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy);
150 }
151 }
152
153 // If the cast operand is a constant vector, perform the cast by
154 // operating on each element. In the cast of bitcasts, the element
155 // count may be mismatched; don't attempt to handle that here.
156 if (DestTy->isVectorTy() && V->getType()->isVectorTy() &&
157 cast<VectorType>(DestTy)->getElementCount() ==
158 cast<VectorType>(V->getType())->getElementCount()) {
159 VectorType *DestVecTy = cast<VectorType>(DestTy);
160 Type *DstEltTy = DestVecTy->getElementType();
161 // Fast path for splatted constants.
162 if (Constant *Splat = V->getSplatValue()) {
163 Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy);
164 if (!Res)
165 return nullptr;
167 cast<VectorType>(DestTy)->getElementCount(), Res);
168 }
169 if (isa<ScalableVectorType>(DestTy))
170 return nullptr;
172 Type *Ty = IntegerType::get(V->getContext(), 32);
173 for (unsigned i = 0,
174 e = cast<FixedVectorType>(V->getType())->getNumElements();
175 i != e; ++i) {
176 Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
177 Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy);
178 if (!Casted)
179 return nullptr;
180 res.push_back(Casted);
181 }
182 return ConstantVector::get(res);
183 }
184
185 // We actually have to do a cast now. Perform the cast according to the
186 // opcode specified.
187 switch (opc) {
188 default:
189 llvm_unreachable("Failed to cast constant expression");
190 case Instruction::FPTrunc:
191 case Instruction::FPExt:
192 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
193 bool ignored;
194 APFloat Val = FPC->getValueAPF();
195 Val.convert(DestTy->getScalarType()->getFltSemantics(),
197 return ConstantFP::get(DestTy, Val);
198 }
199 return nullptr; // Can't fold.
200 case Instruction::FPToUI:
201 case Instruction::FPToSI:
202 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
203 const APFloat &V = FPC->getValueAPF();
204 bool ignored;
205 APSInt IntVal(DestTy->getScalarSizeInBits(), opc == Instruction::FPToUI);
207 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) {
208 // Undefined behavior invoked - the destination type can't represent
209 // the input constant.
210 return PoisonValue::get(DestTy);
211 }
212 return ConstantInt::get(DestTy, IntVal);
213 }
214 return nullptr; // Can't fold.
215 case Instruction::UIToFP:
216 case Instruction::SIToFP:
217 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
218 const APInt &api = CI->getValue();
219 APFloat apf(DestTy->getScalarType()->getFltSemantics(),
221 apf.convertFromAPInt(api, opc==Instruction::SIToFP,
223 return ConstantFP::get(DestTy, apf);
224 }
225 return nullptr;
226 case Instruction::ZExt:
227 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
229 return ConstantInt::get(DestTy, CI->getValue().zext(BitWidth));
230 }
231 return nullptr;
232 case Instruction::SExt:
233 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
235 return ConstantInt::get(DestTy, CI->getValue().sext(BitWidth));
236 }
237 return nullptr;
238 case Instruction::Trunc: {
239 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
241 return ConstantInt::get(DestTy, CI->getValue().trunc(BitWidth));
242 }
243
244 return nullptr;
245 }
246 case Instruction::BitCast:
247 return FoldBitCast(V, DestTy);
248 case Instruction::AddrSpaceCast:
249 case Instruction::IntToPtr:
250 case Instruction::PtrToAddr:
251 case Instruction::PtrToInt:
252 return nullptr;
253 }
254}
255
257 Constant *V1, Constant *V2) {
258 // Check for i1 and vector true/false conditions.
259 if (Cond->isNullValue()) return V2;
260 if (Cond->isAllOnesValue()) return V1;
261
262 // If the condition is a vector constant, fold the result elementwise.
264 auto *V1VTy = CondV->getType();
266 Type *Ty = IntegerType::get(CondV->getContext(), 32);
267 for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) {
268 Constant *V;
270 ConstantInt::get(Ty, i));
272 ConstantInt::get(Ty, i));
273 auto *Cond = cast<Constant>(CondV->getOperand(i));
274 if (isa<PoisonValue>(Cond)) {
275 V = PoisonValue::get(V1Element->getType());
276 } else if (V1Element == V2Element) {
277 V = V1Element;
278 } else if (isa<UndefValue>(Cond)) {
279 V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
280 } else {
281 if (!isa<ConstantInt>(Cond)) break;
282 V = Cond->isNullValue() ? V2Element : V1Element;
283 }
284 Result.push_back(V);
285 }
286
287 // If we were able to build the vector, return it.
288 if (Result.size() == V1VTy->getNumElements())
289 return ConstantVector::get(Result);
290 }
291
293 return PoisonValue::get(V1->getType());
294
295 if (isa<UndefValue>(Cond)) {
296 if (isa<UndefValue>(V1)) return V1;
297 return V2;
298 }
299
300 if (V1 == V2) return V1;
301
302 if (isa<PoisonValue>(V1))
303 return V2;
304 if (isa<PoisonValue>(V2))
305 return V1;
306
307 // If the true or false value is undef, we can fold to the other value as
308 // long as the other value isn't poison.
309 auto NotPoison = [](Constant *C) {
310 if (isa<PoisonValue>(C))
311 return false;
312
313 // TODO: We can analyze ConstExpr by opcode to determine if there is any
314 // possibility of poison.
315 if (isa<ConstantExpr>(C))
316 return false;
317
320 return true;
321
322 if (C->getType()->isVectorTy())
323 return !C->containsPoisonElement() && !C->containsConstantExpression();
324
325 // TODO: Recursively analyze aggregates or other constants.
326 return false;
327 };
328 if (isa<UndefValue>(V1) && NotPoison(V2)) return V2;
329 if (isa<UndefValue>(V2) && NotPoison(V1)) return V1;
330
331 return nullptr;
332}
333
335 Constant *Idx) {
336 auto *ValVTy = cast<VectorType>(Val->getType());
337
338 // extractelt poison, C -> poison
339 // extractelt C, undef -> poison
340 if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx))
341 return PoisonValue::get(ValVTy->getElementType());
342
343 // extractelt undef, C -> undef
344 if (isa<UndefValue>(Val))
345 return UndefValue::get(ValVTy->getElementType());
346
347 auto *CIdx = dyn_cast<ConstantInt>(Idx);
348 if (!CIdx)
349 return nullptr;
350
351 if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) {
352 // ee({w,x,y,z}, wrong_value) -> poison
353 if (CIdx->uge(ValFVTy->getNumElements()))
354 return PoisonValue::get(ValFVTy->getElementType());
355 }
356
357 // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...)
358 if (auto *CE = dyn_cast<ConstantExpr>(Val)) {
359 if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
361 Ops.reserve(CE->getNumOperands());
362 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
363 Constant *Op = CE->getOperand(i);
364 if (Op->getType()->isVectorTy()) {
366 if (!ScalarOp)
367 return nullptr;
368 Ops.push_back(ScalarOp);
369 } else
370 Ops.push_back(Op);
371 }
372 return CE->getWithOperands(Ops, ValVTy->getElementType(), false,
373 GEP->getSourceElementType());
374 } else if (CE->getOpcode() == Instruction::InsertElement) {
375 if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) {
376 if (APSInt::isSameValue(APSInt(IEIdx->getValue()),
377 APSInt(CIdx->getValue()))) {
378 return CE->getOperand(1);
379 } else {
380 return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx);
381 }
382 }
383 }
384 }
385
386 if (Constant *C = Val->getAggregateElement(CIdx))
387 return C;
388
389 // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x
390 if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) {
391 if (Constant *SplatVal = Val->getSplatValue())
392 return SplatVal;
393 }
394
395 return nullptr;
396}
397
399 Constant *Elt,
400 Constant *Idx) {
401 if (isa<UndefValue>(Idx))
402 return PoisonValue::get(Val->getType());
403
404 // Inserting null into all zeros is still all zeros.
405 // TODO: This is true for undef and poison splats too.
406 if (isa<ConstantAggregateZero>(Val) && Elt->isNullValue())
407 return Val;
408
410 if (!CIdx) return nullptr;
411
412 // Do not iterate on scalable vector. The num of elements is unknown at
413 // compile-time.
415 return nullptr;
416
417 auto *ValTy = cast<FixedVectorType>(Val->getType());
418
419 unsigned NumElts = ValTy->getNumElements();
420 if (CIdx->uge(NumElts))
421 return PoisonValue::get(Val->getType());
422
424 Result.reserve(NumElts);
425 auto *Ty = Type::getInt32Ty(Val->getContext());
426 uint64_t IdxVal = CIdx->getZExtValue();
427 for (unsigned i = 0; i != NumElts; ++i) {
428 if (i == IdxVal) {
429 Result.push_back(Elt);
430 continue;
431 }
432
433 Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
434 Result.push_back(C);
435 }
436
437 return ConstantVector::get(Result);
438}
439
441 ArrayRef<int> Mask) {
442 auto *V1VTy = cast<VectorType>(V1->getType());
443 unsigned MaskNumElts = Mask.size();
444 auto MaskEltCount =
445 ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy));
446 Type *EltTy = V1VTy->getElementType();
447
448 // Poison shuffle mask -> poison value.
449 if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
450 return PoisonValue::get(VectorType::get(EltTy, MaskEltCount));
451 }
452
453 // If the mask is all zeros this is a splat, no need to go through all
454 // elements.
455 if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
456 Type *Ty = IntegerType::get(V1->getContext(), 32);
457 Constant *Elt =
458 ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0));
459
460 // For scalable vectors, make sure this doesn't fold back into a
461 // shufflevector.
462 if (!MaskEltCount.isScalable() || Elt->isNullValue() || isa<UndefValue>(Elt))
463 return ConstantVector::getSplat(MaskEltCount, Elt);
464 }
465
466 // Do not iterate on scalable vector. The num of elements is unknown at
467 // compile-time.
468 if (isa<ScalableVectorType>(V1VTy))
469 return nullptr;
470
471 unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue();
472
473 // Loop over the shuffle mask, evaluating each element.
475 for (unsigned i = 0; i != MaskNumElts; ++i) {
476 int Elt = Mask[i];
477 if (Elt == -1) {
478 Result.push_back(UndefValue::get(EltTy));
479 continue;
480 }
481 Constant *InElt;
482 if (unsigned(Elt) >= SrcNumElts*2)
483 InElt = UndefValue::get(EltTy);
484 else if (unsigned(Elt) >= SrcNumElts) {
485 Type *Ty = IntegerType::get(V2->getContext(), 32);
486 InElt =
488 ConstantInt::get(Ty, Elt - SrcNumElts));
489 } else {
490 Type *Ty = IntegerType::get(V1->getContext(), 32);
491 InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
492 }
493 Result.push_back(InElt);
494 }
495
496 return ConstantVector::get(Result);
497}
498
500 ArrayRef<unsigned> Idxs) {
501 // Base case: no indices, so return the entire value.
502 if (Idxs.empty())
503 return Agg;
504
505 if (Constant *C = Agg->getAggregateElement(Idxs[0]))
507
508 return nullptr;
509}
510
512 Constant *Val,
513 ArrayRef<unsigned> Idxs) {
514 // Base case: no indices, so replace the entire value.
515 if (Idxs.empty())
516 return Val;
517
518 unsigned NumElts;
519 if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
520 NumElts = ST->getNumElements();
521 else
522 NumElts = cast<ArrayType>(Agg->getType())->getNumElements();
523
525 for (unsigned i = 0; i != NumElts; ++i) {
526 Constant *C = Agg->getAggregateElement(i);
527 if (!C) return nullptr;
528
529 if (Idxs[0] == i)
531
532 Result.push_back(C);
533 }
534
535 if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
536 return ConstantStruct::get(ST, Result);
537 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result);
538}
539
541 assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");
542
543 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
544 // vectors are always evaluated per element.
545 bool IsScalableVector = isa<ScalableVectorType>(C->getType());
546 bool HasScalarUndefOrScalableVectorUndef =
547 (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C);
548
549 if (HasScalarUndefOrScalableVectorUndef) {
550 switch (static_cast<Instruction::UnaryOps>(Opcode)) {
551 case Instruction::FNeg:
552 return C; // -undef -> undef
553 case Instruction::UnaryOpsEnd:
554 llvm_unreachable("Invalid UnaryOp");
555 }
556 }
557
558 // Constant should not be UndefValue, unless these are vector constants.
559 assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue");
560 // We only have FP UnaryOps right now.
561 assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");
562
563 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
564 const APFloat &CV = CFP->getValueAPF();
565 switch (Opcode) {
566 default:
567 break;
568 case Instruction::FNeg:
569 return ConstantFP::get(C->getType(), neg(CV));
570 }
571 } else if (auto *VTy = dyn_cast<VectorType>(C->getType())) {
572 // Fast path for splatted constants.
573 if (Constant *Splat = C->getSplatValue())
574 if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat))
575 return ConstantVector::getSplat(VTy->getElementCount(), Elt);
576
577 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
578 // Fold each element and create a vector constant from those constants.
579 Type *Ty = IntegerType::get(FVTy->getContext(), 32);
581 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
582 Constant *ExtractIdx = ConstantInt::get(Ty, i);
583 Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx);
584 Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt);
585 if (!Res)
586 return nullptr;
587 Result.push_back(Res);
588 }
589
590 return ConstantVector::get(Result);
591 }
592 }
593
594 // We don't know how to fold this.
595 return nullptr;
596}
597
599 Constant *C2) {
600 assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected");
601
602 // Simplify BinOps with their identity values first. They are no-ops and we
603 // can always return the other value, including undef or poison values.
605 Opcode, C1->getType(), /*AllowRHSIdentity*/ false)) {
606 if (C1 == Identity)
607 return C2;
608 if (C2 == Identity)
609 return C1;
610 } else if (Constant *Identity = ConstantExpr::getBinOpIdentity(
611 Opcode, C1->getType(), /*AllowRHSIdentity*/ true)) {
612 if (C2 == Identity)
613 return C1;
614 }
615
616 // Binary operations propagate poison.
617 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
618 return PoisonValue::get(C1->getType());
619
620 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length
621 // vectors are always evaluated per element.
622 bool IsScalableVector = isa<ScalableVectorType>(C1->getType());
623 bool HasScalarUndefOrScalableVectorUndef =
624 (!C1->getType()->isVectorTy() || IsScalableVector) &&
625 (isa<UndefValue>(C1) || isa<UndefValue>(C2));
626 if (HasScalarUndefOrScalableVectorUndef) {
627 switch (static_cast<Instruction::BinaryOps>(Opcode)) {
628 case Instruction::Xor:
629 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
630 // Handle undef ^ undef -> 0 special case. This is a common
631 // idiom (misuse).
632 return Constant::getNullValue(C1->getType());
633 [[fallthrough]];
634 case Instruction::Add:
635 case Instruction::Sub:
636 return UndefValue::get(C1->getType());
637 case Instruction::And:
638 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
639 return C1;
640 return Constant::getNullValue(C1->getType()); // undef & X -> 0
641 case Instruction::Mul: {
642 // undef * undef -> undef
643 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
644 return C1;
645 const APInt *CV;
646 // X * undef -> undef if X is odd
647 if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
648 if ((*CV)[0])
649 return UndefValue::get(C1->getType());
650
651 // X * undef -> 0 otherwise
652 return Constant::getNullValue(C1->getType());
653 }
654 case Instruction::SDiv:
655 case Instruction::UDiv:
656 // X / undef -> poison
657 // X / 0 -> poison
658 if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
659 return PoisonValue::get(C2->getType());
660 // undef / X -> 0 otherwise
661 return Constant::getNullValue(C1->getType());
662 case Instruction::URem:
663 case Instruction::SRem:
664 // X % undef -> poison
665 // X % 0 -> poison
666 if (match(C2, m_CombineOr(m_Undef(), m_Zero())))
667 return PoisonValue::get(C2->getType());
668 // undef % X -> 0 otherwise
669 return Constant::getNullValue(C1->getType());
670 case Instruction::Or: // X | undef -> -1
671 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
672 return C1;
673 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
674 case Instruction::LShr:
675 // X >>l undef -> poison
676 if (isa<UndefValue>(C2))
677 return PoisonValue::get(C2->getType());
678 // undef >>l X -> 0
679 return Constant::getNullValue(C1->getType());
680 case Instruction::AShr:
681 // X >>a undef -> poison
682 if (isa<UndefValue>(C2))
683 return PoisonValue::get(C2->getType());
684 // TODO: undef >>a X -> poison if the shift is exact
685 // undef >>a X -> 0
686 return Constant::getNullValue(C1->getType());
687 case Instruction::Shl:
688 // X << undef -> undef
689 if (isa<UndefValue>(C2))
690 return PoisonValue::get(C2->getType());
691 // undef << X -> 0
692 return Constant::getNullValue(C1->getType());
693 case Instruction::FSub:
694 // -0.0 - undef --> undef (consistent with "fneg undef")
695 if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2))
696 return C2;
697 [[fallthrough]];
698 case Instruction::FAdd:
699 case Instruction::FMul:
700 case Instruction::FDiv:
701 case Instruction::FRem:
702 // [any flop] undef, undef -> undef
703 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
704 return C1;
705 // [any flop] C, undef -> NaN
706 // [any flop] undef, C -> NaN
707 // We could potentially specialize NaN/Inf constants vs. 'normal'
708 // constants (possibly differently depending on opcode and operand). This
709 // would allow returning undef sometimes. But it is always safe to fold to
710 // NaN because we can choose the undef operand as NaN, and any FP opcode
711 // with a NaN operand will propagate NaN.
712 return ConstantFP::getNaN(C1->getType());
713 case Instruction::BinaryOpsEnd:
714 llvm_unreachable("Invalid BinaryOp");
715 }
716 }
717
718 // Neither constant should be UndefValue, unless these are vector constants.
719 assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue");
720
721 // Handle simplifications when the RHS is a constant int.
722 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
723 if (C2 == ConstantExpr::getBinOpAbsorber(Opcode, C2->getType(),
724 /*AllowLHSConstant*/ false))
725 return C2;
726
727 switch (Opcode) {
728 case Instruction::UDiv:
729 case Instruction::SDiv:
730 if (CI2->isZero())
731 return PoisonValue::get(CI2->getType()); // X / 0 == poison
732 break;
733 case Instruction::URem:
734 case Instruction::SRem:
735 if (CI2->isOne())
736 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
737 if (CI2->isZero())
738 return PoisonValue::get(CI2->getType()); // X % 0 == poison
739 break;
740 case Instruction::And:
741 assert(!CI2->isZero() && "And zero handled above");
742 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
743 // If and'ing the address of a global with a constant, fold it.
744 if ((CE1->getOpcode() == Instruction::PtrToInt ||
745 CE1->getOpcode() == Instruction::PtrToAddr) &&
746 isa<GlobalValue>(CE1->getOperand(0))) {
747 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
748
749 Align GVAlign; // defaults to 1
750
751 if (Module *TheModule = GV->getParent()) {
752 const DataLayout &DL = TheModule->getDataLayout();
753 GVAlign = GV->getPointerAlignment(DL);
754
755 // If the function alignment is not specified then assume that it
756 // is 4.
757 // This is dangerous; on x86, the alignment of the pointer
758 // corresponds to the alignment of the function, but might be less
759 // than 4 if it isn't explicitly specified.
760 // However, a fix for this behaviour was reverted because it
761 // increased code size (see https://reviews.llvm.org/D55115)
762 // FIXME: This code should be deleted once existing targets have
763 // appropriate defaults
764 if (isa<Function>(GV) && !DL.getFunctionPtrAlign())
765 GVAlign = Align(4);
766 } else if (isa<GlobalVariable>(GV)) {
767 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne();
768 }
769
770 if (GVAlign > 1) {
771 unsigned DstWidth = CI2->getBitWidth();
772 unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign));
773 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
774
775 // If checking bits we know are clear, return zero.
776 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
777 return Constant::getNullValue(CI2->getType());
778 }
779 }
780 }
781 break;
782 }
783 } else if (isa<ConstantInt>(C1)) {
784 // If C1 is a ConstantInt and C2 is not, swap the operands.
785 if (Instruction::isCommutative(Opcode))
786 return ConstantExpr::isDesirableBinOp(Opcode)
787 ? ConstantExpr::get(Opcode, C2, C1)
788 : ConstantFoldBinaryInstruction(Opcode, C2, C1);
789 }
790
791 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
792 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
793 const APInt &C1V = CI1->getValue();
794 const APInt &C2V = CI2->getValue();
795 switch (Opcode) {
796 default:
797 break;
798 case Instruction::Add:
799 return ConstantInt::get(C1->getType(), C1V + C2V);
800 case Instruction::Sub:
801 return ConstantInt::get(C1->getType(), C1V - C2V);
802 case Instruction::Mul:
803 return ConstantInt::get(C1->getType(), C1V * C2V);
804 case Instruction::UDiv:
805 assert(!CI2->isZero() && "Div by zero handled above");
806 return ConstantInt::get(CI1->getType(), C1V.udiv(C2V));
807 case Instruction::SDiv:
808 assert(!CI2->isZero() && "Div by zero handled above");
809 if (C2V.isAllOnes() && C1V.isMinSignedValue())
810 return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison
811 return ConstantInt::get(CI1->getType(), C1V.sdiv(C2V));
812 case Instruction::URem:
813 assert(!CI2->isZero() && "Div by zero handled above");
814 return ConstantInt::get(C1->getType(), C1V.urem(C2V));
815 case Instruction::SRem:
816 assert(!CI2->isZero() && "Div by zero handled above");
817 if (C2V.isAllOnes() && C1V.isMinSignedValue())
818 return PoisonValue::get(C1->getType()); // MIN_INT % -1 -> poison
819 return ConstantInt::get(C1->getType(), C1V.srem(C2V));
820 case Instruction::And:
821 return ConstantInt::get(C1->getType(), C1V & C2V);
822 case Instruction::Or:
823 return ConstantInt::get(C1->getType(), C1V | C2V);
824 case Instruction::Xor:
825 return ConstantInt::get(C1->getType(), C1V ^ C2V);
826 case Instruction::Shl:
827 if (C2V.ult(C1V.getBitWidth()))
828 return ConstantInt::get(C1->getType(), C1V.shl(C2V));
829 return PoisonValue::get(C1->getType()); // too big shift is poison
830 case Instruction::LShr:
831 if (C2V.ult(C1V.getBitWidth()))
832 return ConstantInt::get(C1->getType(), C1V.lshr(C2V));
833 return PoisonValue::get(C1->getType()); // too big shift is poison
834 case Instruction::AShr:
835 if (C2V.ult(C1V.getBitWidth()))
836 return ConstantInt::get(C1->getType(), C1V.ashr(C2V));
837 return PoisonValue::get(C1->getType()); // too big shift is poison
838 }
839 }
840
841 if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(),
842 /*AllowLHSConstant*/ true))
843 return C1;
844 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
845 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
846 const APFloat &C1V = CFP1->getValueAPF();
847 const APFloat &C2V = CFP2->getValueAPF();
848 APFloat C3V = C1V; // copy for modification
849 switch (Opcode) {
850 default:
851 break;
852 case Instruction::FAdd:
853 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
854 return ConstantFP::get(C1->getType(), C3V);
855 case Instruction::FSub:
857 return ConstantFP::get(C1->getType(), C3V);
858 case Instruction::FMul:
860 return ConstantFP::get(C1->getType(), C3V);
861 case Instruction::FDiv:
862 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
863 return ConstantFP::get(C1->getType(), C3V);
864 case Instruction::FRem:
865 (void)C3V.mod(C2V);
866 return ConstantFP::get(C1->getType(), C3V);
867 }
868 }
869 }
870
871 if (auto *VTy = dyn_cast<VectorType>(C1->getType())) {
872 // Fast path for splatted constants.
873 if (Constant *C2Splat = C2->getSplatValue()) {
874 if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue())
875 return PoisonValue::get(VTy);
876 if (Constant *C1Splat = C1->getSplatValue()) {
877 Constant *Res =
879 ? ConstantExpr::get(Opcode, C1Splat, C2Splat)
880 : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat);
881 if (!Res)
882 return nullptr;
883 return ConstantVector::getSplat(VTy->getElementCount(), Res);
884 }
885 }
886
887 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) {
888 // Fold each element and create a vector constant from those constants.
890 Type *Ty = IntegerType::get(FVTy->getContext(), 32);
891 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) {
892 Constant *ExtractIdx = ConstantInt::get(Ty, i);
893 Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx);
894 Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx);
896 ? ConstantExpr::get(Opcode, LHS, RHS)
897 : ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
898 if (!Res)
899 return nullptr;
900 Result.push_back(Res);
901 }
902
903 return ConstantVector::get(Result);
904 }
905 }
906
907 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
908 // There are many possible foldings we could do here. We should probably
909 // at least fold add of a pointer with an integer into the appropriate
910 // getelementptr. This will improve alias analysis a bit.
911
912 // Given ((a + b) + c), if (b + c) folds to something interesting, return
913 // (a + (b + c)).
914 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
915 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
916 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
917 return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
918 }
919 } else if (isa<ConstantExpr>(C2)) {
920 // If C2 is a constant expr and C1 isn't, flop them around and fold the
921 // other way if possible.
922 if (Instruction::isCommutative(Opcode))
923 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
924 }
925
926 // i1 can be simplified in many cases.
927 if (C1->getType()->isIntegerTy(1)) {
928 switch (Opcode) {
929 case Instruction::Add:
930 case Instruction::Sub:
931 return ConstantExpr::getXor(C1, C2);
932 case Instruction::Shl:
933 case Instruction::LShr:
934 case Instruction::AShr:
935 // We can assume that C2 == 0. If it were one the result would be
936 // undefined because the shift value is as large as the bitwidth.
937 return C1;
938 case Instruction::SDiv:
939 case Instruction::UDiv:
940 // We can assume that C2 == 1. If it were zero the result would be
941 // undefined through division by zero.
942 return C1;
943 case Instruction::URem:
944 case Instruction::SRem:
945 // We can assume that C2 == 1. If it were zero the result would be
946 // undefined through division by zero.
947 return ConstantInt::getFalse(C1->getContext());
948 default:
949 break;
950 }
951 }
952
953 // We don't know how to fold this.
954 return nullptr;
955}
956
958 const GlobalValue *GV2) {
959 auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
960 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
961 return true;
962 if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
963 Type *Ty = GVar->getValueType();
964 // A global with opaque type might end up being zero sized.
965 if (!Ty->isSized())
966 return true;
967 // A global with an empty type might lie at the address of any other
968 // global.
969 if (Ty->isEmptyTy())
970 return true;
971 }
972 return false;
973 };
974 // Don't try to decide equality of aliases.
975 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
976 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
977 return ICmpInst::ICMP_NE;
979}
980
981/// This function determines if there is anything we can decide about the two
982/// constants provided. This doesn't need to handle simple things like integer
983/// comparisons, but should instead handle ConstantExprs and GlobalValues.
984/// If we can determine that the two constants have a particular relation to
985/// each other, we should return the corresponding ICmp predicate, otherwise
986/// return ICmpInst::BAD_ICMP_PREDICATE.
988 assert(V1->getType() == V2->getType() &&
989 "Cannot compare different types of values!");
990 if (V1 == V2) return ICmpInst::ICMP_EQ;
991
992 // The following folds only apply to pointers.
993 if (!V1->getType()->isPointerTy())
995
996 // To simplify this code we canonicalize the relation so that the first
997 // operand is always the most "complex" of the two. We consider simple
998 // constants (like ConstantPointerNull) to be the simplest, followed by
999 // BlockAddress, GlobalValues, and ConstantExpr's (the most complex).
1000 auto GetComplexity = [](Constant *V) {
1001 if (isa<ConstantExpr>(V))
1002 return 3;
1003 if (isa<GlobalValue>(V))
1004 return 2;
1005 if (isa<BlockAddress>(V))
1006 return 1;
1007 return 0;
1008 };
1009 if (GetComplexity(V1) < GetComplexity(V2)) {
1010 ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1);
1011 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1012 return ICmpInst::getSwappedPredicate(SwappedRelation);
1014 }
1015
1016 if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1017 // Now we know that the RHS is a BlockAddress or simple constant.
1018 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1019 // Block address in another function can't equal this one, but block
1020 // addresses in the current function might be the same if blocks are
1021 // empty.
1022 if (BA2->getFunction() != BA->getFunction())
1023 return ICmpInst::ICMP_NE;
1024 } else if (isa<ConstantPointerNull>(V2)) {
1025 return ICmpInst::ICMP_NE;
1026 }
1027 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1028 // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1029 // constant.
1030 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1031 return areGlobalsPotentiallyEqual(GV, GV2);
1032 } else if (isa<BlockAddress>(V2)) {
1033 return ICmpInst::ICMP_NE; // Globals never equal labels.
1034 } else if (isa<ConstantPointerNull>(V2)) {
1035 // GlobalVals can never be null unless they have external weak linkage.
1036 // We don't try to evaluate aliases here.
1037 // NOTE: We should not be doing this constant folding if null pointer
1038 // is considered valid for the function. But currently there is no way to
1039 // query it from the Constant type.
1040 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) &&
1041 !NullPointerIsDefined(nullptr /* F */,
1042 GV->getType()->getAddressSpace()))
1043 return ICmpInst::ICMP_UGT;
1044 }
1045 } else if (auto *CE1 = dyn_cast<ConstantExpr>(V1)) {
1046 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1047 // constantexpr, a global, block address, or a simple constant.
1048 Constant *CE1Op0 = CE1->getOperand(0);
1049
1050 switch (CE1->getOpcode()) {
1051 case Instruction::GetElementPtr: {
1052 GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
1053 // Ok, since this is a getelementptr, we know that the constant has a
1054 // pointer type. Check the various cases.
1055 if (isa<ConstantPointerNull>(V2)) {
1056 // If we are comparing a GEP to a null pointer, check to see if the base
1057 // of the GEP equals the null pointer.
1058 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1059 // If its not weak linkage, the GVal must have a non-zero address
1060 // so the result is greater-than
1061 if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds())
1062 return ICmpInst::ICMP_UGT;
1063 }
1064 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1065 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1066 if (GV != GV2) {
1067 if (CE1GEP->hasAllZeroIndices())
1068 return areGlobalsPotentiallyEqual(GV, GV2);
1070 }
1071 }
1072 } else if (const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) {
1073 // By far the most common case to handle is when the base pointers are
1074 // obviously to the same global.
1075 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand());
1076 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1077 // Don't know relative ordering, but check for inequality.
1078 if (CE1Op0 != CE2Op0) {
1079 if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
1081 cast<GlobalValue>(CE2Op0));
1083 }
1084 }
1085 }
1086 break;
1087 }
1088 default:
1089 break;
1090 }
1091 }
1092
1094}
1095
1097 Constant *C1, Constant *C2) {
1098 Type *ResultTy;
1099 if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1100 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1101 VT->getElementCount());
1102 else
1103 ResultTy = Type::getInt1Ty(C1->getContext());
1104
1105 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1106 if (Predicate == FCmpInst::FCMP_FALSE)
1107 return Constant::getNullValue(ResultTy);
1108
1109 if (Predicate == FCmpInst::FCMP_TRUE)
1110 return Constant::getAllOnesValue(ResultTy);
1111
1112 // Handle some degenerate cases first
1113 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))
1114 return PoisonValue::get(ResultTy);
1115
1116 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1117 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
1118 // For EQ and NE, we can always pick a value for the undef to make the
1119 // predicate pass or fail, so we can return undef.
1120 // Also, if both operands are undef, we can return undef for int comparison.
1121 if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
1122 return UndefValue::get(ResultTy);
1123
1124 // Otherwise, for integer compare, pick the same value as the non-undef
1125 // operand, and fold it to true or false.
1126 if (isIntegerPredicate)
1127 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate));
1128
1129 // Choosing NaN for the undef will always make unordered comparison succeed
1130 // and ordered comparison fails.
1131 return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
1132 }
1133
1134 if (C2->isNullValue()) {
1135 // The caller is expected to commute the operands if the constant expression
1136 // is C2.
1137 // C1 >= 0 --> true
1138 if (Predicate == ICmpInst::ICMP_UGE)
1139 return Constant::getAllOnesValue(ResultTy);
1140 // C1 < 0 --> false
1141 if (Predicate == ICmpInst::ICMP_ULT)
1142 return Constant::getNullValue(ResultTy);
1143 }
1144
1145 // If the comparison is a comparison between two i1's, simplify it.
1146 if (C1->getType()->isIntOrIntVectorTy(1)) {
1147 switch (Predicate) {
1148 case ICmpInst::ICMP_EQ:
1149 if (isa<ConstantExpr>(C1))
1152 case ICmpInst::ICMP_NE:
1153 return ConstantExpr::getXor(C1, C2);
1154 default:
1155 break;
1156 }
1157 }
1158
1159 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1160 const APInt &V1 = cast<ConstantInt>(C1)->getValue();
1161 const APInt &V2 = cast<ConstantInt>(C2)->getValue();
1162 return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate));
1163 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1164 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF();
1165 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF();
1166 return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate));
1167 } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) {
1168
1169 // Fast path for splatted constants.
1170 if (Constant *C1Splat = C1->getSplatValue())
1171 if (Constant *C2Splat = C2->getSplatValue())
1172 if (Constant *Elt =
1173 ConstantFoldCompareInstruction(Predicate, C1Splat, C2Splat))
1174 return ConstantVector::getSplat(C1VTy->getElementCount(), Elt);
1175
1176 // Do not iterate on scalable vector. The number of elements is unknown at
1177 // compile-time.
1178 if (isa<ScalableVectorType>(C1VTy))
1179 return nullptr;
1180
1181 // If we can constant fold the comparison of each element, constant fold
1182 // the whole vector comparison.
1184 Type *Ty = IntegerType::get(C1->getContext(), 32);
1185 // Compare the elements, producing an i1 result or constant expr.
1186 for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue();
1187 I != E; ++I) {
1188 Constant *C1E =
1189 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I));
1190 Constant *C2E =
1191 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I));
1192 Constant *Elt = ConstantFoldCompareInstruction(Predicate, C1E, C2E);
1193 if (!Elt)
1194 return nullptr;
1195
1196 ResElts.push_back(Elt);
1197 }
1198
1199 return ConstantVector::get(ResElts);
1200 }
1201
1202 if (C1->getType()->isFPOrFPVectorTy()) {
1203 if (C1 == C2) {
1204 // We know that C1 == C2 || isUnordered(C1, C2).
1205 if (Predicate == FCmpInst::FCMP_ONE)
1206 return ConstantInt::getFalse(ResultTy);
1207 else if (Predicate == FCmpInst::FCMP_UEQ)
1208 return ConstantInt::getTrue(ResultTy);
1209 }
1210 } else {
1211 // Evaluate the relation between the two constants, per the predicate.
1212 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
1213 switch (evaluateICmpRelation(C1, C2)) {
1214 default: llvm_unreachable("Unknown relational!");
1216 break; // Couldn't determine anything about these constants.
1217 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1218 // If we know the constants are equal, we can decide the result of this
1219 // computation precisely.
1220 Result = ICmpInst::isTrueWhenEqual(Predicate);
1221 break;
1222 case ICmpInst::ICMP_ULT:
1223 switch (Predicate) {
1225 Result = 1; break;
1227 Result = 0; break;
1228 default:
1229 break;
1230 }
1231 break;
1232 case ICmpInst::ICMP_SLT:
1233 switch (Predicate) {
1235 Result = 1; break;
1237 Result = 0; break;
1238 default:
1239 break;
1240 }
1241 break;
1242 case ICmpInst::ICMP_UGT:
1243 switch (Predicate) {
1245 Result = 1; break;
1247 Result = 0; break;
1248 default:
1249 break;
1250 }
1251 break;
1252 case ICmpInst::ICMP_SGT:
1253 switch (Predicate) {
1255 Result = 1; break;
1257 Result = 0; break;
1258 default:
1259 break;
1260 }
1261 break;
1262 case ICmpInst::ICMP_ULE:
1263 if (Predicate == ICmpInst::ICMP_UGT)
1264 Result = 0;
1265 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE)
1266 Result = 1;
1267 break;
1268 case ICmpInst::ICMP_SLE:
1269 if (Predicate == ICmpInst::ICMP_SGT)
1270 Result = 0;
1271 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE)
1272 Result = 1;
1273 break;
1274 case ICmpInst::ICMP_UGE:
1275 if (Predicate == ICmpInst::ICMP_ULT)
1276 Result = 0;
1277 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE)
1278 Result = 1;
1279 break;
1280 case ICmpInst::ICMP_SGE:
1281 if (Predicate == ICmpInst::ICMP_SLT)
1282 Result = 0;
1283 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE)
1284 Result = 1;
1285 break;
1286 case ICmpInst::ICMP_NE:
1287 if (Predicate == ICmpInst::ICMP_EQ)
1288 Result = 0;
1289 if (Predicate == ICmpInst::ICMP_NE)
1290 Result = 1;
1291 break;
1292 }
1293
1294 // If we evaluated the result, return it now.
1295 if (Result != -1)
1296 return ConstantInt::get(ResultTy, Result);
1297
1298 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
1299 (C1->isNullValue() && !C2->isNullValue())) {
1300 // If C2 is a constant expr and C1 isn't, flip them around and fold the
1301 // other way if possible.
1302 // Also, if C1 is null and C2 isn't, flip them around.
1303 Predicate = ICmpInst::getSwappedPredicate(Predicate);
1304 return ConstantFoldCompareInstruction(Predicate, C2, C1);
1305 }
1306 }
1307 return nullptr;
1308}
1309
1311 std::optional<ConstantRange> InRange,
1312 ArrayRef<Value *> Idxs) {
1313 if (Idxs.empty()) return C;
1314
1316 C, ArrayRef((Value *const *)Idxs.data(), Idxs.size()));
1317
1318 if (isa<PoisonValue>(C))
1319 return PoisonValue::get(GEPTy);
1320
1321 if (isa<UndefValue>(C))
1322 return UndefValue::get(GEPTy);
1323
1324 auto IsNoOp = [&]() {
1325 // Avoid losing inrange information.
1326 if (InRange)
1327 return false;
1328
1329 return all_of(Idxs, [](Value *Idx) {
1330 Constant *IdxC = cast<Constant>(Idx);
1331 return IdxC->isNullValue() || isa<UndefValue>(IdxC);
1332 });
1333 };
1334 if (IsNoOp())
1335 return GEPTy->isVectorTy() && !C->getType()->isVectorTy()
1337 cast<VectorType>(GEPTy)->getElementCount(), C)
1338 : C;
1339
1340 return nullptr;
1341}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static unsigned foldConstantCastPair(unsigned opc, ConstantExpr *Op, Type *DstTy)
This function determines which opcode to use to fold two constant cast expressions together.
static Constant * foldMaybeUndesirableCast(unsigned opc, Constant *V, Type *DestTy)
static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, const GlobalValue *GV2)
static Constant * FoldBitCast(Constant *V, Type *DestTy)
static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2)
This function determines if there is anything we can decide about the two constants provided.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define I(x, y, z)
Definition MD5.cpp:58
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
#define T
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6060
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1172
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1163
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1329
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1208
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:423
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:827
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:306
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
static bool isSameValue(const APSInt &I1, const APSInt &I2)
Determine if two APSInts have the same value, zero- or sign-extending as needed.
Definition APSInt.h:320
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:144
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
The address of a basic block.
Definition Constants.h:899
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.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static LLVM_ABI bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
A constant value that is initialized with an expression using other constant values.
Definition Constants.h:1120
static LLVM_ABI Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI bool isDesirableCastOp(unsigned Opcode)
Whether creating a constant expression for this cast is desirable.
static LLVM_ABI Constant * getBinOpAbsorber(unsigned Opcode, Type *Ty, bool AllowLHSConstant=false)
Return the absorbing element for the given binary operation, i.e.
static LLVM_ABI Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static LLVM_ABI Constant * getNot(Constant *C)
static LLVM_ABI Constant * getXor(Constant *C1, Constant *C2)
static LLVM_ABI Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static LLVM_ABI bool isDesirableBinOp(unsigned Opcode)
Whether creating a constant expression for this binary operator is desirable.
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false, bool NSZ=false)
Return the identity constant for a binary opcode.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
static LLVM_ABI Constant * getNaN(Type *Ty, bool Negative=false, uint64_t Payload=0)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(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
bool uge(uint64_t Num) const
This function will return true iff this constant represents a value with active bits bigger than 64 b...
Definition Constants.h:257
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
Constant Vector Declarations.
Definition Constants.h:517
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
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 Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:316
static LLVM_ABI bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
bool isInBounds() const
Test whether this is an inbounds GEP, as defined by LangRef.html.
Definition Operator.h:430
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
Definition Operator.h:491
static Type * getGEPReturnType(Value *Ptr, ArrayRef< Value * > IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers.
Module * getParent()
Get the module that this global value is contained inside of...
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isCast() const
LLVM_ABI bool isAssociative() const LLVM_READONLY
Return true if the instruction is associative:
LLVM_ABI bool isCommutative() const LLVM_READONLY
Return true if the instruction is commutative:
bool isBinaryOp() const
bool isUnaryOp() const
bool isIntDivRem() const
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 Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Class to represent struct types.
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
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
Definition Type.h:165
LLVM_ABI bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition Type.cpp:250
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
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
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:294
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
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:107
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
LLVM_ABI Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition Value.cpp:956
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
Base class of all SIMD vector types.
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Type * getElementType() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
cstfp_pred_ty< is_neg_zero_fp > m_NegZeroFP()
Match a floating-point negative zero.
auto m_Undef()
Match an arbitrary undef constant.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
This is an optimization pass for GlobalISel generic memory operations.
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
LLVM_ABI Constant * ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2)
LLVM_ABI Constant * ConstantFoldUnaryInstruction(unsigned Opcode, Constant *V)
LLVM_ABI Constant * ConstantFoldGetElementPtr(Type *Ty, Constant *C, std::optional< ConstantRange > InRange, ArrayRef< Value * > Idxs)
LLVM_ABI Constant * ConstantFoldExtractValueInstruction(Constant *Agg, ArrayRef< unsigned > Idxs)
Attempt to constant fold an extractvalue instruction with the specified operands and indices.
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI Constant * ConstantFoldInsertElementInstruction(Constant *Val, Constant *Elt, Constant *Idx)
Attempt to constant fold an insertelement instruction with the specified operands and indices.
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
constexpr int PoisonMaskElem
LLVM_ABI Constant * ConstantFoldExtractElementInstruction(Constant *Val, Constant *Idx)
Attempt to constant fold an extractelement instruction with the specified operands and indices.
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
APFloat neg(APFloat X)
Returns the negated value of the argument.
Definition APFloat.h:1551
LLVM_ABI Constant * ConstantFoldCastInstruction(unsigned opcode, Constant *V, Type *DestTy)
LLVM_ABI Constant * ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val, ArrayRef< unsigned > Idxs)
ConstantFoldInsertValueInstruction - Attempt to constant fold an insertvalue instruction with the spe...
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI Constant * ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, ArrayRef< int > Mask)
Attempt to constant fold a shufflevector instruction with the specified operands and mask.
LLVM_ABI Constant * ConstantFoldBinaryInstruction(unsigned Opcode, Constant *V1, Constant *V2)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39