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