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