Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Thu Jul 17 2025 12:26:55 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp