/data/zyw/opt-ci/actions-runner/_work/llvm-opt-benchmark/llvm-opt-benchmark/llvm/llvm-project/llvm/lib/IR/ConstantFold.cpp
Line | Count | Source |
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" |
32 | | using namespace llvm; |
33 | | using namespace llvm::PatternMatch; |
34 | | |
35 | | //===----------------------------------------------------------------------===// |
36 | | // ConstantFold*Instruction Implementations |
37 | | //===----------------------------------------------------------------------===// |
38 | | |
39 | | /// This function determines which opcode to use to fold two constant cast |
40 | | /// expressions together. It uses CastInst::isEliminableCastPair to determine |
41 | | /// the opcode. Consequently its just a wrapper around that function. |
42 | | /// Determine if it is valid to fold a cast of a cast |
43 | | static unsigned |
44 | | foldConstantCastPair( |
45 | | unsigned opc, ///< opcode of the second cast constant expression |
46 | | ConstantExpr *Op, ///< the first cast constant expression |
47 | | Type *DstTy ///< destination type of the first cast |
48 | 1.71M | ) { |
49 | 1.71M | assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); |
50 | 1.71M | assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); |
51 | 1.71M | assert(CastInst::isCast(opc) && "Invalid cast opcode"); |
52 | | |
53 | | // The types and opcodes for the two Cast constant expressions |
54 | 1.71M | Type *SrcTy = Op->getOperand(0)->getType(); |
55 | 1.71M | Type *MidTy = Op->getType(); |
56 | 1.71M | Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); |
57 | 1.71M | Instruction::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. |
62 | 1.71M | IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext()); |
63 | | |
64 | | // Let CastInst::isEliminableCastPair do the heavy lifting. |
65 | 1.71M | return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, |
66 | 1.71M | nullptr, FakeIntPtrTy, nullptr); |
67 | 1.71M | } |
68 | | |
69 | 450k | static Constant *FoldBitCast(Constant *V, Type *DestTy) { |
70 | 450k | Type *SrcTy = V->getType(); |
71 | 450k | if (SrcTy == DestTy) |
72 | 0 | return V; // no-op cast |
73 | | |
74 | 450k | if (V->isAllOnesValue()) |
75 | 4.62k | return Constant::getAllOnesValue(DestTy); |
76 | | |
77 | | // Handle ConstantInt -> ConstantFP |
78 | 445k | if (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). |
82 | 23.0k | if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy)20 ) |
83 | 20 | return ConstantExpr::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. |
87 | 23.0k | if (!DestTy->isFPOrFPVectorTy() || DestTy->isPPC_FP128Ty() || |
88 | 23.0k | DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits()) |
89 | 0 | return nullptr; |
90 | | |
91 | 23.0k | return ConstantFP::get( |
92 | 23.0k | DestTy, |
93 | 23.0k | APFloat(DestTy->getScalarType()->getFltSemantics(), CI->getValue())); |
94 | 23.0k | } |
95 | | |
96 | | // Handle ConstantFP -> ConstantInt |
97 | 422k | if (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). |
101 | 421k | if (isa<VectorType>(DestTy) && !isa<VectorType>(SrcTy)0 ) |
102 | 0 | return ConstantExpr::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 |
110 | 421k | if (SrcTy->isPPC_FP128Ty()) |
111 | 0 | return nullptr; |
112 | | |
113 | | // Make sure dest type is compatible with the folded integer constant. |
114 | 421k | if (!DestTy->isIntOrIntVectorTy() || |
115 | 421k | DestTy->getScalarSizeInBits() != SrcTy->getScalarSizeInBits()) |
116 | 0 | return nullptr; |
117 | | |
118 | 421k | return ConstantInt::get(DestTy, FP->getValueAPF().bitcastToAPInt()); |
119 | 421k | } |
120 | | |
121 | 1.73k | return nullptr; |
122 | 422k | } |
123 | | |
124 | | static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V, |
125 | 1.73M | Type *DestTy) { |
126 | 1.73M | return ConstantExpr::isDesirableCastOp(opc) |
127 | 1.73M | ? ConstantExpr::getCast(opc, V, DestTy)1.72M |
128 | 1.73M | : ConstantFoldCastInstruction(opc, V, DestTy)14.9k ; |
129 | 1.73M | } |
130 | | |
131 | | Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, |
132 | 64.6M | Type *DestTy) { |
133 | 64.6M | if (isa<PoisonValue>(V)) |
134 | 112k | return PoisonValue::get(DestTy); |
135 | | |
136 | 64.5M | if (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. |
140 | 184k | if (opc == Instruction::ZExt || opc == Instruction::SExt88.7k || |
141 | 184k | opc == Instruction::UIToFP88.0k || opc == Instruction::SIToFP87.9k ) |
142 | 97.0k | return Constant::getNullValue(DestTy); |
143 | 87.7k | return UndefValue::get(DestTy); |
144 | 184k | } |
145 | | |
146 | 64.3M | if (V->isNullValue() && !DestTy->isX86_AMXTy()26.4M && |
147 | 64.3M | opc != Instruction::AddrSpaceCast26.4M ) |
148 | 26.4M | return Constant::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. |
152 | 37.9M | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
153 | 7.22M | if (CE->isCast()) { |
154 | | // Try hard to fold cast of cast because they are often eliminable. |
155 | 1.71M | if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) |
156 | 1.69M | return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy); |
157 | 1.71M | } |
158 | 7.22M | } |
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. |
163 | 36.2M | if (DestTy->isVectorTy() && V->getType()->isVectorTy()43.0k && |
164 | 36.2M | cast<VectorType>(DestTy)->getElementCount() == |
165 | 43.0k | cast<VectorType>(V->getType())->getElementCount()) { |
166 | 37.2k | VectorType *DestVecTy = cast<VectorType>(DestTy); |
167 | 37.2k | Type *DstEltTy = DestVecTy->getElementType(); |
168 | | // Fast path for splatted constants. |
169 | 37.2k | if (Constant *Splat = V->getSplatValue()) { |
170 | 36.3k | Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy); |
171 | 36.3k | if (!Res) |
172 | 0 | return nullptr; |
173 | 36.3k | return ConstantVector::getSplat( |
174 | 36.3k | cast<VectorType>(DestTy)->getElementCount(), Res); |
175 | 36.3k | } |
176 | 949 | if (isa<ScalableVectorType>(DestTy)) |
177 | 0 | return nullptr; |
178 | 949 | SmallVector<Constant *, 16> res; |
179 | 949 | Type *Ty = IntegerType::get(V->getContext(), 32); |
180 | 949 | for (unsigned i = 0, |
181 | 949 | e = cast<FixedVectorType>(V->getType())->getNumElements(); |
182 | 3.77k | i != e; ++i2.82k ) { |
183 | 2.82k | Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); |
184 | 2.82k | Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy); |
185 | 2.82k | if (!Casted) |
186 | 0 | return nullptr; |
187 | 2.82k | res.push_back(Casted); |
188 | 2.82k | } |
189 | 949 | return ConstantVector::get(res); |
190 | 949 | } |
191 | | |
192 | | // We actually have to do a cast now. Perform the cast according to the |
193 | | // opcode specified. |
194 | 36.1M | switch (opc) { |
195 | 0 | default: |
196 | 0 | llvm_unreachable("Failed to cast constant expression"); |
197 | 11.3k | case Instruction::FPTrunc: |
198 | 17.1k | case Instruction::FPExt: |
199 | 17.1k | if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
200 | 17.1k | bool ignored; |
201 | 17.1k | APFloat Val = FPC->getValueAPF(); |
202 | 17.1k | Val.convert(DestTy->getScalarType()->getFltSemantics(), |
203 | 17.1k | APFloat::rmNearestTiesToEven, &ignored); |
204 | 17.1k | return ConstantFP::get(DestTy, Val); |
205 | 17.1k | } |
206 | 0 | return nullptr; // Can't fold. |
207 | 148k | case Instruction::FPToUI: |
208 | 300k | case Instruction::FPToSI: |
209 | 300k | if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { |
210 | 300k | const APFloat &V = FPC->getValueAPF(); |
211 | 300k | bool ignored; |
212 | 300k | APSInt IntVal(DestTy->getScalarSizeInBits(), opc == Instruction::FPToUI); |
213 | 300k | if (APFloat::opInvalidOp == |
214 | 300k | V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) { |
215 | | // Undefined behavior invoked - the destination type can't represent |
216 | | // the input constant. |
217 | 15.0k | return PoisonValue::get(DestTy); |
218 | 15.0k | } |
219 | 285k | return ConstantInt::get(DestTy, IntVal); |
220 | 300k | } |
221 | 0 | return nullptr; // Can't fold. |
222 | 62.5k | case Instruction::UIToFP: |
223 | 138k | case Instruction::SIToFP: |
224 | 138k | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
225 | 138k | const APInt &api = CI->getValue(); |
226 | 138k | APFloat apf(DestTy->getScalarType()->getFltSemantics(), |
227 | 138k | APInt::getZero(DestTy->getScalarSizeInBits())); |
228 | 138k | apf.convertFromAPInt(api, opc==Instruction::SIToFP, |
229 | 138k | APFloat::rmNearestTiesToEven); |
230 | 138k | return ConstantFP::get(DestTy, apf); |
231 | 138k | } |
232 | 0 | return nullptr; |
233 | 7.51M | case Instruction::ZExt: |
234 | 7.51M | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
235 | 7.50M | uint32_t BitWidth = DestTy->getScalarSizeInBits(); |
236 | 7.50M | return ConstantInt::get(DestTy, CI->getValue().zext(BitWidth)); |
237 | 7.50M | } |
238 | 13.6k | return nullptr; |
239 | 2.04M | case Instruction::SExt: |
240 | 2.04M | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
241 | 2.04M | uint32_t BitWidth = DestTy->getScalarSizeInBits(); |
242 | 2.04M | return ConstantInt::get(DestTy, CI->getValue().sext(BitWidth)); |
243 | 2.04M | } |
244 | 2.36k | return nullptr; |
245 | 16.8M | case Instruction::Trunc: { |
246 | 16.8M | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
247 | 16.8M | uint32_t BitWidth = DestTy->getScalarSizeInBits(); |
248 | 16.8M | return ConstantInt::get(DestTy, CI->getValue().trunc(BitWidth)); |
249 | 16.8M | } |
250 | | |
251 | 295 | return nullptr; |
252 | 16.8M | } |
253 | 450k | case Instruction::BitCast: |
254 | 450k | return FoldBitCast(V, DestTy); |
255 | 0 | case Instruction::AddrSpaceCast: |
256 | 3.00M | case Instruction::IntToPtr: |
257 | 8.87M | case Instruction::PtrToInt: |
258 | 8.87M | return nullptr; |
259 | 36.1M | } |
260 | 36.1M | } |
261 | | |
262 | | Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, |
263 | 504k | Constant *V1, Constant *V2) { |
264 | | // Check for i1 and vector true/false conditions. |
265 | 504k | if (Cond->isNullValue()) return V2274k ; |
266 | 230k | if (Cond->isAllOnesValue()) return V1230k ; |
267 | | |
268 | | // If the condition is a vector constant, fold the result elementwise. |
269 | 399 | if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { |
270 | 10 | auto *V1VTy = CondV->getType(); |
271 | 10 | SmallVector<Constant*, 16> Result; |
272 | 10 | Type *Ty = IntegerType::get(CondV->getContext(), 32); |
273 | 238 | for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i228 ) { |
274 | 228 | Constant *V; |
275 | 228 | Constant *V1Element = ConstantExpr::getExtractElement(V1, |
276 | 228 | ConstantInt::get(Ty, i)); |
277 | 228 | Constant *V2Element = ConstantExpr::getExtractElement(V2, |
278 | 228 | ConstantInt::get(Ty, i)); |
279 | 228 | auto *Cond = cast<Constant>(CondV->getOperand(i)); |
280 | 228 | if (isa<PoisonValue>(Cond)) { |
281 | 0 | V = PoisonValue::get(V1Element->getType()); |
282 | 228 | } else if (V1Element == V2Element) { |
283 | 157 | V = V1Element; |
284 | 157 | } else if (71 isa<UndefValue>(Cond)71 ) { |
285 | 0 | V = isa<UndefValue>(V1Element) ? V1Element : V2Element; |
286 | 71 | } else { |
287 | 71 | if (!isa<ConstantInt>(Cond)) break0 ; |
288 | 71 | V = Cond->isNullValue() ? V2Element12 : V1Element59 ; |
289 | 71 | } |
290 | 228 | Result.push_back(V); |
291 | 228 | } |
292 | | |
293 | | // If we were able to build the vector, return it. |
294 | 10 | if (Result.size() == V1VTy->getNumElements()) |
295 | 10 | return ConstantVector::get(Result); |
296 | 10 | } |
297 | | |
298 | 389 | if (isa<PoisonValue>(Cond)) |
299 | 130 | return PoisonValue::get(V1->getType()); |
300 | | |
301 | 259 | if (isa<UndefValue>(Cond)) { |
302 | 259 | if (isa<UndefValue>(V1)) return V1175 ; |
303 | 84 | return V2; |
304 | 259 | } |
305 | | |
306 | 0 | if (V1 == V2) return V1; |
307 | | |
308 | 0 | if (isa<PoisonValue>(V1)) |
309 | 0 | return V2; |
310 | 0 | if (isa<PoisonValue>(V2)) |
311 | 0 | return V1; |
312 | | |
313 | | // If the true or false value is undef, we can fold to the other value as |
314 | | // long as the other value isn't poison. |
315 | 0 | auto NotPoison = [](Constant *C) { |
316 | 0 | if (isa<PoisonValue>(C)) |
317 | 0 | return false; |
318 | | |
319 | | // TODO: We can analyze ConstExpr by opcode to determine if there is any |
320 | | // possibility of poison. |
321 | 0 | if (isa<ConstantExpr>(C)) |
322 | 0 | return false; |
323 | | |
324 | 0 | if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) || |
325 | 0 | isa<ConstantPointerNull>(C) || isa<Function>(C)) |
326 | 0 | return true; |
327 | | |
328 | 0 | if (C->getType()->isVectorTy()) |
329 | 0 | return !C->containsPoisonElement() && !C->containsConstantExpression(); |
330 | | |
331 | | // TODO: Recursively analyze aggregates or other constants. |
332 | 0 | return false; |
333 | 0 | }; |
334 | 0 | if (isa<UndefValue>(V1) && NotPoison(V2)) return V2; |
335 | 0 | if (isa<UndefValue>(V2) && NotPoison(V1)) return V1; |
336 | | |
337 | 0 | return nullptr; |
338 | 0 | } |
339 | | |
340 | | Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, |
341 | 1.98M | Constant *Idx) { |
342 | 1.98M | auto *ValVTy = cast<VectorType>(Val->getType()); |
343 | | |
344 | | // extractelt poison, C -> poison |
345 | | // extractelt C, undef -> poison |
346 | 1.98M | if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx)1.72M ) |
347 | 259k | return PoisonValue::get(ValVTy->getElementType()); |
348 | | |
349 | | // extractelt undef, C -> undef |
350 | 1.72M | if (isa<UndefValue>(Val)) |
351 | 30.7k | return UndefValue::get(ValVTy->getElementType()); |
352 | | |
353 | 1.69M | auto *CIdx = dyn_cast<ConstantInt>(Idx); |
354 | 1.69M | if (!CIdx) |
355 | 0 | return nullptr; |
356 | | |
357 | 1.69M | if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) { |
358 | | // ee({w,x,y,z}, wrong_value) -> poison |
359 | 1.69M | if (CIdx->uge(ValFVTy->getNumElements())) |
360 | 0 | return PoisonValue::get(ValFVTy->getElementType()); |
361 | 1.69M | } |
362 | | |
363 | | // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...) |
364 | 1.69M | if (auto *CE = dyn_cast<ConstantExpr>(Val)) { |
365 | 84 | if (auto *GEP = dyn_cast<GEPOperator>(CE)) { |
366 | 0 | SmallVector<Constant *, 8> Ops; |
367 | 0 | Ops.reserve(CE->getNumOperands()); |
368 | 0 | for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { |
369 | 0 | Constant *Op = CE->getOperand(i); |
370 | 0 | if (Op->getType()->isVectorTy()) { |
371 | 0 | Constant *ScalarOp = ConstantExpr::getExtractElement(Op, Idx); |
372 | 0 | if (!ScalarOp) |
373 | 0 | return nullptr; |
374 | 0 | Ops.push_back(ScalarOp); |
375 | 0 | } else |
376 | 0 | Ops.push_back(Op); |
377 | 0 | } |
378 | 0 | return CE->getWithOperands(Ops, ValVTy->getElementType(), false, |
379 | 0 | GEP->getSourceElementType()); |
380 | 84 | } else if (CE->getOpcode() == Instruction::InsertElement) { |
381 | 0 | if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) { |
382 | 0 | if (APSInt::isSameValue(APSInt(IEIdx->getValue()), |
383 | 0 | APSInt(CIdx->getValue()))) { |
384 | 0 | return CE->getOperand(1); |
385 | 0 | } else { |
386 | 0 | return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx); |
387 | 0 | } |
388 | 0 | } |
389 | 0 | } |
390 | 84 | } |
391 | | |
392 | 1.69M | if (Constant *C = Val->getAggregateElement(CIdx)) |
393 | 1.69M | return C; |
394 | | |
395 | | // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x |
396 | 84 | if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) { |
397 | 84 | if (Constant *SplatVal = Val->getSplatValue()) |
398 | 0 | return SplatVal; |
399 | 84 | } |
400 | | |
401 | 84 | return nullptr; |
402 | 84 | } |
403 | | |
404 | | Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, |
405 | | Constant *Elt, |
406 | 154k | Constant *Idx) { |
407 | 154k | if (isa<UndefValue>(Idx)) |
408 | 0 | return PoisonValue::get(Val->getType()); |
409 | | |
410 | | // Inserting null into all zeros is still all zeros. |
411 | | // TODO: This is true for undef and poison splats too. |
412 | 154k | if (isa<ConstantAggregateZero>(Val) && Elt->isNullValue()188 ) |
413 | 141 | return Val; |
414 | | |
415 | 154k | ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); |
416 | 154k | if (!CIdx) return nullptr0 ; |
417 | | |
418 | | // Do not iterate on scalable vector. The num of elements is unknown at |
419 | | // compile-time. |
420 | 154k | if (isa<ScalableVectorType>(Val->getType())) |
421 | 0 | return nullptr; |
422 | | |
423 | 154k | auto *ValTy = cast<FixedVectorType>(Val->getType()); |
424 | | |
425 | 154k | unsigned NumElts = ValTy->getNumElements(); |
426 | 154k | if (CIdx->uge(NumElts)) |
427 | 0 | return PoisonValue::get(Val->getType()); |
428 | | |
429 | 154k | SmallVector<Constant*, 16> Result; |
430 | 154k | Result.reserve(NumElts); |
431 | 154k | auto *Ty = Type::getInt32Ty(Val->getContext()); |
432 | 154k | uint64_t IdxVal = CIdx->getZExtValue(); |
433 | 2.18M | for (unsigned i = 0; i != NumElts; ++i2.02M ) { |
434 | 2.02M | if (i == IdxVal) { |
435 | 154k | Result.push_back(Elt); |
436 | 154k | continue; |
437 | 154k | } |
438 | | |
439 | 1.87M | Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i)); |
440 | 1.87M | Result.push_back(C); |
441 | 1.87M | } |
442 | | |
443 | 154k | return ConstantVector::get(Result); |
444 | 154k | } |
445 | | |
446 | | Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, |
447 | 38.7k | ArrayRef<int> Mask) { |
448 | 38.7k | auto *V1VTy = cast<VectorType>(V1->getType()); |
449 | 38.7k | unsigned MaskNumElts = Mask.size(); |
450 | 38.7k | auto MaskEltCount = |
451 | 38.7k | ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy)); |
452 | 38.7k | Type *EltTy = V1VTy->getElementType(); |
453 | | |
454 | | // Poison shuffle mask -> poison value. |
455 | 38.9k | if (all_of(Mask, [](int Elt) 38.7k { return Elt == PoisonMaskElem; })) { |
456 | 0 | return PoisonValue::get(VectorType::get(EltTy, MaskEltCount)); |
457 | 0 | } |
458 | | |
459 | | // If the mask is all zeros this is a splat, no need to go through all |
460 | | // elements. |
461 | 230k | if (38.7k all_of(Mask, [](int Elt) 38.7k { return Elt == 0; })) { |
462 | 26.8k | Type *Ty = IntegerType::get(V1->getContext(), 32); |
463 | 26.8k | Constant *Elt = |
464 | 26.8k | ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0)); |
465 | | |
466 | | // For scalable vectors, make sure this doesn't fold back into a |
467 | | // shufflevector. |
468 | 26.8k | if (!MaskEltCount.isScalable() || Elt->isNullValue()0 || isa<UndefValue>(Elt)0 ) |
469 | 26.8k | return ConstantVector::getSplat(MaskEltCount, Elt); |
470 | 26.8k | } |
471 | | |
472 | | // Do not iterate on scalable vector. The num of elements is unknown at |
473 | | // compile-time. |
474 | 11.9k | if (isa<ScalableVectorType>(V1VTy)) |
475 | 0 | return nullptr; |
476 | | |
477 | 11.9k | unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue(); |
478 | | |
479 | | // Loop over the shuffle mask, evaluating each element. |
480 | 11.9k | SmallVector<Constant*, 32> Result; |
481 | 57.4k | for (unsigned i = 0; i != MaskNumElts; ++i45.5k ) { |
482 | 45.5k | int Elt = Mask[i]; |
483 | 45.5k | if (Elt == -1) { |
484 | 7.53k | Result.push_back(UndefValue::get(EltTy)); |
485 | 7.53k | continue; |
486 | 7.53k | } |
487 | 38.0k | Constant *InElt; |
488 | 38.0k | if (unsigned(Elt) >= SrcNumElts*2) |
489 | 0 | InElt = UndefValue::get(EltTy); |
490 | 38.0k | else if (unsigned(Elt) >= SrcNumElts) { |
491 | 1.59k | Type *Ty = IntegerType::get(V2->getContext(), 32); |
492 | 1.59k | InElt = |
493 | 1.59k | ConstantExpr::getExtractElement(V2, |
494 | 1.59k | ConstantInt::get(Ty, Elt - SrcNumElts)); |
495 | 36.4k | } else { |
496 | 36.4k | Type *Ty = IntegerType::get(V1->getContext(), 32); |
497 | 36.4k | InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt)); |
498 | 36.4k | } |
499 | 38.0k | Result.push_back(InElt); |
500 | 38.0k | } |
501 | | |
502 | 11.9k | return ConstantVector::get(Result); |
503 | 11.9k | } |
504 | | |
505 | | Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg, |
506 | 338k | ArrayRef<unsigned> Idxs) { |
507 | | // Base case: no indices, so return the entire value. |
508 | 338k | if (Idxs.empty()) |
509 | 169k | return Agg; |
510 | | |
511 | 169k | if (Constant *C = Agg->getAggregateElement(Idxs[0])) |
512 | 169k | return ConstantFoldExtractValueInstruction(C, Idxs.slice(1)); |
513 | | |
514 | 0 | return nullptr; |
515 | 169k | } |
516 | | |
517 | | Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, |
518 | | Constant *Val, |
519 | 523k | ArrayRef<unsigned> Idxs) { |
520 | | // Base case: no indices, so replace the entire value. |
521 | 523k | if (Idxs.empty()) |
522 | 261k | return Val; |
523 | | |
524 | 261k | unsigned NumElts; |
525 | 261k | if (StructType *ST = dyn_cast<StructType>(Agg->getType())) |
526 | 261k | NumElts = ST->getNumElements(); |
527 | 0 | else |
528 | 0 | NumElts = cast<ArrayType>(Agg->getType())->getNumElements(); |
529 | | |
530 | 261k | SmallVector<Constant*, 32> Result; |
531 | 785k | for (unsigned i = 0; i != NumElts; ++i523k ) { |
532 | 523k | Constant *C = Agg->getAggregateElement(i); |
533 | 523k | if (!C) return nullptr0 ; |
534 | | |
535 | 523k | if (Idxs[0] == i) |
536 | 261k | C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1)); |
537 | | |
538 | 523k | Result.push_back(C); |
539 | 523k | } |
540 | | |
541 | 261k | if (StructType *ST = dyn_cast<StructType>(Agg->getType())) |
542 | 261k | return ConstantStruct::get(ST, Result); |
543 | 0 | return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result); |
544 | 261k | } |
545 | | |
546 | 28.3k | Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { |
547 | 28.3k | assert(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. |
551 | 28.3k | bool IsScalableVector = isa<ScalableVectorType>(C->getType()); |
552 | 28.3k | bool HasScalarUndefOrScalableVectorUndef = |
553 | 28.3k | (!C->getType()->isVectorTy() || IsScalableVector2.76k ) && isa<UndefValue>(C)25.6k ; |
554 | | |
555 | 28.3k | if (HasScalarUndefOrScalableVectorUndef) { |
556 | 55 | switch (static_cast<Instruction::UnaryOps>(Opcode)) { |
557 | 55 | case Instruction::FNeg: |
558 | 55 | return C; // -undef -> undef |
559 | 0 | case Instruction::UnaryOpsEnd: |
560 | 0 | llvm_unreachable("Invalid UnaryOp"); |
561 | 55 | } |
562 | 55 | } |
563 | | |
564 | | // Constant should not be UndefValue, unless these are vector constants. |
565 | 28.3k | assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue"); |
566 | | // We only have FP UnaryOps right now. |
567 | 28.3k | assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp"); |
568 | | |
569 | 28.3k | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
570 | 25.5k | const APFloat &CV = CFP->getValueAPF(); |
571 | 25.5k | switch (Opcode) { |
572 | 0 | default: |
573 | 0 | break; |
574 | 25.5k | case Instruction::FNeg: |
575 | 25.5k | return ConstantFP::get(C->getType(), neg(CV)); |
576 | 25.5k | } |
577 | 25.5k | } else if (auto *2.76k VTy2.76k = dyn_cast<VectorType>(C->getType())) { |
578 | | // Fast path for splatted constants. |
579 | 2.76k | if (Constant *Splat = C->getSplatValue()) |
580 | 2.72k | if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat)) |
581 | 2.72k | return ConstantVector::getSplat(VTy->getElementCount(), Elt); |
582 | | |
583 | 44 | if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) { |
584 | | // Fold each element and create a vector constant from those constants. |
585 | 44 | Type *Ty = IntegerType::get(FVTy->getContext(), 32); |
586 | 44 | SmallVector<Constant *, 16> Result; |
587 | 206 | for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i162 ) { |
588 | 162 | Constant *ExtractIdx = ConstantInt::get(Ty, i); |
589 | 162 | Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx); |
590 | 162 | Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt); |
591 | 162 | if (!Res) |
592 | 0 | return nullptr; |
593 | 162 | Result.push_back(Res); |
594 | 162 | } |
595 | | |
596 | 44 | return ConstantVector::get(Result); |
597 | 44 | } |
598 | 44 | } |
599 | | |
600 | | // We don't know how to fold this. |
601 | 0 | return nullptr; |
602 | 28.3k | } |
603 | | |
604 | | Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, |
605 | 88.7M | Constant *C2) { |
606 | 88.7M | assert(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. |
610 | 88.7M | if (Constant *Identity = ConstantExpr::getBinOpIdentity( |
611 | 88.7M | Opcode, C1->getType(), /*AllowRHSIdentity*/ false)) { |
612 | 57.2M | if (C1 == Identity) |
613 | 8.28M | return C2; |
614 | 48.9M | if (C2 == Identity) |
615 | 506k | return C1; |
616 | 48.9M | } else if (Constant *31.5M Identity31.5M = ConstantExpr::getBinOpIdentity( |
617 | 31.5M | Opcode, C1->getType(), /*AllowRHSIdentity*/ true)) { |
618 | 30.5M | if (C2 == Identity) |
619 | 6.39M | return C1; |
620 | 30.5M | } |
621 | | |
622 | | // Binary operations propagate poison. |
623 | 73.6M | if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2)73.5M ) |
624 | 26.6k | return PoisonValue::get(C1->getType()); |
625 | | |
626 | | // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length |
627 | | // vectors are always evaluated per element. |
628 | 73.5M | bool IsScalableVector = isa<ScalableVectorType>(C1->getType()); |
629 | 73.5M | bool HasScalarUndefOrScalableVectorUndef = |
630 | 73.5M | (!C1->getType()->isVectorTy() || IsScalableVector76.4k ) && |
631 | 73.5M | (73.5M isa<UndefValue>(C1)73.5M || isa<UndefValue>(C2)73.2M ); |
632 | 73.5M | if (HasScalarUndefOrScalableVectorUndef) { |
633 | 299k | switch (static_cast<Instruction::BinaryOps>(Opcode)) { |
634 | 25.3k | case Instruction::Xor: |
635 | 25.3k | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) |
636 | | // Handle undef ^ undef -> 0 special case. This is a common |
637 | | // idiom (misuse). |
638 | 36 | return Constant::getNullValue(C1->getType()); |
639 | 25.3k | [[fallthrough]]; |
640 | 25.8k | case Instruction::Add: |
641 | 30.7k | case Instruction::Sub: |
642 | 30.7k | return UndefValue::get(C1->getType()); |
643 | 243k | case Instruction::And: |
644 | 243k | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)243k ) // undef & undef -> undef |
645 | 0 | return C1; |
646 | 243k | return Constant::getNullValue(C1->getType()); // undef & X -> 0 |
647 | 2.80k | case Instruction::Mul: { |
648 | | // undef * undef -> undef |
649 | 2.80k | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) |
650 | 0 | return C1; |
651 | 2.80k | const APInt *CV; |
652 | | // X * undef -> undef if X is odd |
653 | 2.80k | if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV))) |
654 | 2.80k | if ((*CV)[0]) |
655 | 692 | return UndefValue::get(C1->getType()); |
656 | | |
657 | | // X * undef -> 0 otherwise |
658 | 2.11k | return Constant::getNullValue(C1->getType()); |
659 | 2.80k | } |
660 | 237 | case Instruction::SDiv: |
661 | 578 | case Instruction::UDiv: |
662 | | // X / undef -> poison |
663 | | // X / 0 -> poison |
664 | 578 | if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) |
665 | 40 | return PoisonValue::get(C2->getType()); |
666 | | // undef / X -> 0 otherwise |
667 | 538 | return Constant::getNullValue(C1->getType()); |
668 | 215 | case Instruction::URem: |
669 | 243 | case Instruction::SRem: |
670 | | // X % undef -> poison |
671 | | // X % 0 -> poison |
672 | 243 | if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) |
673 | 0 | return PoisonValue::get(C2->getType()); |
674 | | // undef % X -> 0 otherwise |
675 | 243 | return Constant::getNullValue(C1->getType()); |
676 | 11.4k | case Instruction::Or: // X | undef -> -1 |
677 | 11.4k | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)11.3k ) // undef | undef -> undef |
678 | 1 | return C1; |
679 | 11.4k | return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 |
680 | 6.61k | case Instruction::LShr: |
681 | | // X >>l undef -> poison |
682 | 6.61k | if (isa<UndefValue>(C2)) |
683 | 1 | return PoisonValue::get(C2->getType()); |
684 | | // undef >>l X -> 0 |
685 | 6.61k | return Constant::getNullValue(C1->getType()); |
686 | 482 | case Instruction::AShr: |
687 | | // X >>a undef -> poison |
688 | 482 | if (isa<UndefValue>(C2)) |
689 | 15 | return PoisonValue::get(C2->getType()); |
690 | | // TODO: undef >>a X -> poison if the shift is exact |
691 | | // undef >>a X -> 0 |
692 | 467 | return Constant::getNullValue(C1->getType()); |
693 | 3.64k | case Instruction::Shl: |
694 | | // X << undef -> undef |
695 | 3.64k | if (isa<UndefValue>(C2)) |
696 | 33 | return PoisonValue::get(C2->getType()); |
697 | | // undef << X -> 0 |
698 | 3.61k | return Constant::getNullValue(C1->getType()); |
699 | 0 | case Instruction::FSub: |
700 | | // -0.0 - undef --> undef (consistent with "fneg undef") |
701 | 0 | if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2)) |
702 | 0 | return C2; |
703 | 0 | [[fallthrough]]; |
704 | 8 | case Instruction::FAdd: |
705 | 16 | case Instruction::FMul: |
706 | 17 | case Instruction::FDiv: |
707 | 17 | case Instruction::FRem: |
708 | | // [any flop] undef, undef -> undef |
709 | 17 | if (isa<UndefValue>(C1) && isa<UndefValue>(C2)9 ) |
710 | 2 | return 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. |
718 | 15 | return ConstantFP::getNaN(C1->getType()); |
719 | 0 | case Instruction::BinaryOpsEnd: |
720 | 0 | llvm_unreachable("Invalid BinaryOp"); |
721 | 299k | } |
722 | 299k | } |
723 | | |
724 | | // Neither constant should be UndefValue, unless these are vector constants. |
725 | 73.5M | assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue"); |
726 | | |
727 | | // Handle simplifications when the RHS is a constant int. |
728 | 73.2M | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
729 | 73.0M | if (C2 == ConstantExpr::getBinOpAbsorber(Opcode, C2->getType(), |
730 | 73.0M | /*AllowLHSConstant*/ false)) |
731 | 35.1k | return C2; |
732 | | |
733 | 72.9M | switch (Opcode) { |
734 | 354k | case Instruction::UDiv: |
735 | 402k | case Instruction::SDiv: |
736 | 402k | if (CI2->isZero()) |
737 | 970 | return PoisonValue::get(CI2->getType()); // X / 0 == poison |
738 | 401k | break; |
739 | 923k | case Instruction::URem: |
740 | 937k | case Instruction::SRem: |
741 | 937k | if (CI2->isOne()) |
742 | 123 | return Constant::getNullValue(CI2->getType()); // X % 1 == 0 |
743 | 937k | if (CI2->isZero()) |
744 | 46 | return PoisonValue::get(CI2->getType()); // X % 0 == poison |
745 | 937k | break; |
746 | 5.74M | case Instruction::And: |
747 | 5.74M | assert(!CI2->isZero() && "And zero handled above"); |
748 | 5.74M | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { |
749 | | // If and'ing the address of a global with a constant, fold it. |
750 | 17.7k | if (CE1->getOpcode() == Instruction::PtrToInt && |
751 | 17.7k | isa<GlobalValue>(CE1->getOperand(0))17.4k ) { |
752 | 17.4k | GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); |
753 | | |
754 | 17.4k | Align GVAlign; // defaults to 1 |
755 | | |
756 | 17.4k | if (Module *TheModule = GV->getParent()) { |
757 | 17.4k | const DataLayout &DL = TheModule->getDataLayout(); |
758 | 17.4k | 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 |
769 | 17.4k | if (isa<Function>(GV) && !DL.getFunctionPtrAlign()2.05k ) |
770 | 2.05k | GVAlign = Align(4); |
771 | 17.4k | } else if (0 isa<GlobalVariable>(GV)0 ) { |
772 | 0 | GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne(); |
773 | 0 | } |
774 | | |
775 | 17.4k | if (GVAlign > 1) { |
776 | 2.54k | unsigned DstWidth = CI2->getBitWidth(); |
777 | 2.54k | unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign)); |
778 | 2.54k | APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth)); |
779 | | |
780 | | // If checking bits we know are clear, return zero. |
781 | 2.54k | if ((CI2->getValue() & BitsNotSet) == CI2->getValue()) |
782 | 2.05k | return Constant::getNullValue(CI2->getType()); |
783 | 2.54k | } |
784 | 17.4k | } |
785 | 17.7k | } |
786 | 5.73M | break; |
787 | 72.9M | } |
788 | 72.9M | } else if (251k isa<ConstantInt>(C1)251k ) { |
789 | | // If C1 is a ConstantInt and C2 is not, swap the operands. |
790 | 67.1k | if (Instruction::isCommutative(Opcode)) |
791 | 65.9k | return ConstantExpr::isDesirableBinOp(Opcode) |
792 | 65.9k | ? ConstantExpr::get(Opcode, C2, C1)4.08k |
793 | 65.9k | : ConstantFoldBinaryInstruction(Opcode, C2, C1)61.8k ; |
794 | 67.1k | } |
795 | | |
796 | 73.1M | if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { |
797 | 72.7M | if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { |
798 | 72.7M | const APInt &C1V = CI1->getValue(); |
799 | 72.7M | const APInt &C2V = CI2->getValue(); |
800 | 72.7M | switch (Opcode) { |
801 | 0 | default: |
802 | 0 | break; |
803 | 7.90M | case Instruction::Add: |
804 | 7.90M | return ConstantInt::get(C1->getType(), C1V + C2V); |
805 | 14.8M | case Instruction::Sub: |
806 | 14.8M | return ConstantInt::get(C1->getType(), C1V - C2V); |
807 | 1.87M | case Instruction::Mul: |
808 | 1.87M | return ConstantInt::get(C1->getType(), C1V * C2V); |
809 | 354k | case Instruction::UDiv: |
810 | 354k | assert(!CI2->isZero() && "Div by zero handled above"); |
811 | 354k | return ConstantInt::get(CI1->getType(), C1V.udiv(C2V)); |
812 | 46.4k | case Instruction::SDiv: |
813 | 46.4k | assert(!CI2->isZero() && "Div by zero handled above"); |
814 | 46.4k | if (C2V.isAllOnes() && C1V.isMinSignedValue()37 ) |
815 | 0 | return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison |
816 | 46.4k | return ConstantInt::get(CI1->getType(), C1V.sdiv(C2V)); |
817 | 922k | case Instruction::URem: |
818 | 922k | assert(!CI2->isZero() && "Div by zero handled above"); |
819 | 922k | return ConstantInt::get(C1->getType(), C1V.urem(C2V)); |
820 | 14.5k | case Instruction::SRem: |
821 | 14.5k | assert(!CI2->isZero() && "Div by zero handled above"); |
822 | 14.5k | if (C2V.isAllOnes() && C1V.isMinSignedValue()11 ) |
823 | 0 | return PoisonValue::get(C1->getType()); // MIN_INT % -1 -> poison |
824 | 14.5k | return ConstantInt::get(C1->getType(), C1V.srem(C2V)); |
825 | 5.72M | case Instruction::And: |
826 | 5.72M | return ConstantInt::get(C1->getType(), C1V & C2V); |
827 | 3.33M | case Instruction::Or: |
828 | 3.33M | return ConstantInt::get(C1->getType(), C1V | C2V); |
829 | 28.9M | case Instruction::Xor: |
830 | 28.9M | return ConstantInt::get(C1->getType(), C1V ^ C2V); |
831 | 7.61M | case Instruction::Shl: |
832 | 7.61M | if (C2V.ult(C1V.getBitWidth())) |
833 | 7.61M | return ConstantInt::get(C1->getType(), C1V.shl(C2V)); |
834 | 1.82k | return PoisonValue::get(C1->getType()); // too big shift is poison |
835 | 1.05M | case Instruction::LShr: |
836 | 1.05M | if (C2V.ult(C1V.getBitWidth())) |
837 | 1.05M | return ConstantInt::get(C1->getType(), C1V.lshr(C2V)); |
838 | 152 | return PoisonValue::get(C1->getType()); // too big shift is poison |
839 | 113k | case Instruction::AShr: |
840 | 113k | if (C2V.ult(C1V.getBitWidth())) |
841 | 113k | return ConstantInt::get(C1->getType(), C1V.ashr(C2V)); |
842 | 2 | return PoisonValue::get(C1->getType()); // too big shift is poison |
843 | 72.7M | } |
844 | 72.7M | } |
845 | | |
846 | 1.23k | if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(), |
847 | 1.23k | /*AllowLHSConstant*/ true)) |
848 | 54 | return C1; |
849 | 375k | } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { |
850 | 106k | if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { |
851 | 106k | const APFloat &C1V = CFP1->getValueAPF(); |
852 | 106k | const APFloat &C2V = CFP2->getValueAPF(); |
853 | 106k | APFloat C3V = C1V; // copy for modification |
854 | 106k | switch (Opcode) { |
855 | 0 | default: |
856 | 0 | break; |
857 | 19.2k | case Instruction::FAdd: |
858 | 19.2k | (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); |
859 | 19.2k | return ConstantFP::get(C1->getType(), C3V); |
860 | 5.18k | case Instruction::FSub: |
861 | 5.18k | (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); |
862 | 5.18k | return ConstantFP::get(C1->getType(), C3V); |
863 | 42.4k | case Instruction::FMul: |
864 | 42.4k | (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); |
865 | 42.4k | return ConstantFP::get(C1->getType(), C3V); |
866 | 39.5k | case Instruction::FDiv: |
867 | 39.5k | (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); |
868 | 39.5k | return ConstantFP::get(C1->getType(), C3V); |
869 | 29 | case Instruction::FRem: |
870 | 29 | (void)C3V.mod(C2V); |
871 | 29 | return ConstantFP::get(C1->getType(), C3V); |
872 | 106k | } |
873 | 106k | } |
874 | 106k | } |
875 | | |
876 | 270k | if (auto *VTy = dyn_cast<VectorType>(C1->getType())) { |
877 | | // Fast path for splatted constants. |
878 | 76.4k | if (Constant *C2Splat = C2->getSplatValue()) { |
879 | 75.2k | if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue()6.27k ) |
880 | 0 | return PoisonValue::get(VTy); |
881 | 75.2k | if (Constant *C1Splat = C1->getSplatValue()) { |
882 | 74.5k | Constant *Res = |
883 | 74.5k | ConstantExpr::isDesirableBinOp(Opcode) |
884 | 74.5k | ? ConstantExpr::get(Opcode, C1Splat, C2Splat)12.2k |
885 | 74.5k | : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat)62.3k ; |
886 | 74.5k | if (!Res) |
887 | 0 | return nullptr; |
888 | 74.5k | return ConstantVector::getSplat(VTy->getElementCount(), Res); |
889 | 74.5k | } |
890 | 75.2k | } |
891 | | |
892 | 1.80k | if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) { |
893 | | // Fold each element and create a vector constant from those constants. |
894 | 1.80k | SmallVector<Constant*, 16> Result; |
895 | 1.80k | Type *Ty = IntegerType::get(FVTy->getContext(), 32); |
896 | 20.4k | for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i18.6k ) { |
897 | 18.6k | Constant *ExtractIdx = ConstantInt::get(Ty, i); |
898 | 18.6k | Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx); |
899 | 18.6k | Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx); |
900 | 18.6k | Constant *Res = ConstantExpr::isDesirableBinOp(Opcode) |
901 | 18.6k | ? ConstantExpr::get(Opcode, LHS, RHS)11.1k |
902 | 18.6k | : ConstantFoldBinaryInstruction(Opcode, LHS, RHS)7.44k ; |
903 | 18.6k | if (!Res) |
904 | 0 | return nullptr; |
905 | 18.6k | Result.push_back(Res); |
906 | 18.6k | } |
907 | | |
908 | 1.80k | return ConstantVector::get(Result); |
909 | 1.80k | } |
910 | 1.80k | } |
911 | | |
912 | 193k | if (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)). |
919 | 192k | if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode145k ) { |
920 | 25 | Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2); |
921 | 25 | if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode0 ) |
922 | 25 | return ConstantExpr::get(Opcode, CE1->getOperand(0), T); |
923 | 25 | } |
924 | 192k | } else if (1.18k isa<ConstantExpr>(C2)1.18k ) { |
925 | | // If C2 is a constant expr and C1 isn't, flop them around and fold the |
926 | | // other way if possible. |
927 | 1.18k | if (Instruction::isCommutative(Opcode)) |
928 | 0 | return ConstantFoldBinaryInstruction(Opcode, C2, C1); |
929 | 1.18k | } |
930 | | |
931 | | // i1 can be simplified in many cases. |
932 | 193k | if (C1->getType()->isIntegerTy(1)) { |
933 | 0 | switch (Opcode) { |
934 | 0 | case Instruction::Add: |
935 | 0 | case Instruction::Sub: |
936 | 0 | return ConstantExpr::getXor(C1, C2); |
937 | 0 | case Instruction::Shl: |
938 | 0 | case Instruction::LShr: |
939 | 0 | case 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. |
942 | 0 | return C1; |
943 | 0 | case Instruction::SDiv: |
944 | 0 | case Instruction::UDiv: |
945 | | // We can assume that C2 == 1. If it were zero the result would be |
946 | | // undefined through division by zero. |
947 | 0 | return C1; |
948 | 0 | case Instruction::URem: |
949 | 0 | case Instruction::SRem: |
950 | | // We can assume that C2 == 1. If it were zero the result would be |
951 | | // undefined through division by zero. |
952 | 0 | return ConstantInt::getFalse(C1->getContext()); |
953 | 0 | default: |
954 | 0 | break; |
955 | 0 | } |
956 | 0 | } |
957 | | |
958 | | // We don't know how to fold this. |
959 | 193k | return nullptr; |
960 | 193k | } |
961 | | |
962 | | static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, |
963 | 226k | const GlobalValue *GV2) { |
964 | 450k | auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) { |
965 | 450k | if (GV->isInterposable() || GV->hasGlobalUnnamedAddr()) |
966 | 1.22k | return true; |
967 | 448k | if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) { |
968 | 418k | Type *Ty = GVar->getValueType(); |
969 | | // A global with opaque type might end up being zero sized. |
970 | 418k | if (!Ty->isSized()) |
971 | 0 | return true; |
972 | | // A global with an empty type might lie at the address of any other |
973 | | // global. |
974 | 418k | if (Ty->isEmptyTy()) |
975 | 2.14k | return true; |
976 | 418k | } |
977 | 446k | return false; |
978 | 448k | }; |
979 | | // Don't try to decide equality of aliases. |
980 | 226k | if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2)) |
981 | 226k | if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2)223k ) |
982 | 223k | return ICmpInst::ICMP_NE; |
983 | 3.36k | return ICmpInst::BAD_ICMP_PREDICATE; |
984 | 226k | } |
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. |
992 | 10.5M | static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) { |
993 | 10.5M | assert(V1->getType() == V2->getType() && |
994 | 10.5M | "Cannot compare different types of values!"); |
995 | 10.5M | if (V1 == V2) return ICmpInst::ICMP_EQ7.82M ; |
996 | | |
997 | | // The following folds only apply to pointers. |
998 | 2.71M | if (!V1->getType()->isPointerTy()) |
999 | 2.21k | return 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). |
1005 | 5.41M | auto GetComplexity = [](Constant *V) 2.70M { |
1006 | 5.41M | if (isa<ConstantExpr>(V)) |
1007 | 79.1k | return 3; |
1008 | 5.34M | if (isa<GlobalValue>(V)) |
1009 | 2.86M | return 2; |
1010 | 2.47M | if (isa<BlockAddress>(V)) |
1011 | 510 | return 1; |
1012 | 2.47M | return 0; |
1013 | 2.47M | }; |
1014 | 2.70M | if (GetComplexity(V1) < GetComplexity(V2)) { |
1015 | 224k | ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1); |
1016 | 224k | if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) |
1017 | 224k | return ICmpInst::getSwappedPredicate(SwappedRelation); |
1018 | 0 | return ICmpInst::BAD_ICMP_PREDICATE; |
1019 | 224k | } |
1020 | | |
1021 | 2.48M | if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) { |
1022 | | // Now we know that the RHS is a BlockAddress or simple constant. |
1023 | 255 | if (const BlockAddress *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. |
1027 | 255 | if (BA2->getFunction() != BA->getFunction()) |
1028 | 0 | return ICmpInst::ICMP_NE; |
1029 | 255 | } else if (0 isa<ConstantPointerNull>(V2)0 ) { |
1030 | 0 | return ICmpInst::ICMP_NE; |
1031 | 0 | } |
1032 | 2.48M | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) { |
1033 | | // Now we know that the RHS is a GlobalValue, BlockAddress or simple |
1034 | | // constant. |
1035 | 2.40M | if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { |
1036 | 226k | return areGlobalsPotentiallyEqual(GV, GV2); |
1037 | 2.18M | } else if (isa<BlockAddress>(V2)) { |
1038 | 0 | return ICmpInst::ICMP_NE; // Globals never equal labels. |
1039 | 2.18M | } else if (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. |
1045 | 2.18M | if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV)1.53M && |
1046 | 2.18M | !NullPointerIsDefined(nullptr /* F */, |
1047 | 1.53M | GV->getType()->getAddressSpace())) |
1048 | 1.53M | return ICmpInst::ICMP_UGT; |
1049 | 2.18M | } |
1050 | 2.40M | } else if (auto *78.4k CE178.4k = 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. |
1053 | 78.4k | Constant *CE1Op0 = CE1->getOperand(0); |
1054 | | |
1055 | 78.4k | switch (CE1->getOpcode()) { |
1056 | 74.6k | case Instruction::GetElementPtr: { |
1057 | 74.6k | GEPOperator *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. |
1060 | 74.6k | if (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. |
1063 | 73.4k | if (const GlobalValue *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 |
1066 | 73.4k | if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds()) |
1067 | 73.4k | return ICmpInst::ICMP_UGT; |
1068 | 73.4k | } |
1069 | 73.4k | } else if (const GlobalValue *1.19k GV21.19k = dyn_cast<GlobalValue>(V2)) { |
1070 | 486 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { |
1071 | 486 | if (GV != GV2) { |
1072 | 476 | if (CE1GEP->hasAllZeroIndices()) |
1073 | 0 | return areGlobalsPotentiallyEqual(GV, GV2); |
1074 | 476 | return ICmpInst::BAD_ICMP_PREDICATE; |
1075 | 476 | } |
1076 | 486 | } |
1077 | 713 | } else if (const auto *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. |
1080 | 600 | const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand()); |
1081 | 600 | if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { |
1082 | | // Don't know relative ordering, but check for inequality. |
1083 | 600 | if (CE1Op0 != CE2Op0) { |
1084 | 597 | if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices()0 ) |
1085 | 0 | return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0), |
1086 | 0 | cast<GlobalValue>(CE2Op0)); |
1087 | 597 | return ICmpInst::BAD_ICMP_PREDICATE; |
1088 | 597 | } |
1089 | 600 | } |
1090 | 600 | } |
1091 | 165 | break; |
1092 | 74.6k | } |
1093 | 3.76k | default: |
1094 | 3.76k | break; |
1095 | 78.4k | } |
1096 | 78.4k | } |
1097 | | |
1098 | 648k | return ICmpInst::BAD_ICMP_PREDICATE; |
1099 | 2.48M | } |
1100 | | |
1101 | | Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, |
1102 | 39.2M | Constant *C1, Constant *C2) { |
1103 | 39.2M | Type *ResultTy; |
1104 | 39.2M | if (VectorType *VT = dyn_cast<VectorType>(C1->getType())) |
1105 | 22.1k | ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()), |
1106 | 22.1k | VT->getElementCount()); |
1107 | 39.2M | else |
1108 | 39.2M | ResultTy = Type::getInt1Ty(C1->getContext()); |
1109 | | |
1110 | | // Fold FCMP_FALSE/FCMP_TRUE unconditionally. |
1111 | 39.2M | if (Predicate == FCmpInst::FCMP_FALSE) |
1112 | 0 | return Constant::getNullValue(ResultTy); |
1113 | | |
1114 | 39.2M | if (Predicate == FCmpInst::FCMP_TRUE) |
1115 | 0 | return Constant::getAllOnesValue(ResultTy); |
1116 | | |
1117 | | // Handle some degenerate cases first |
1118 | 39.2M | if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2)39.2M ) |
1119 | 62.9k | return PoisonValue::get(ResultTy); |
1120 | | |
1121 | 39.2M | if (isa<UndefValue>(C1) || isa<UndefValue>(C2)38.7M ) { |
1122 | 443k | bool 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. |
1126 | 443k | if (ICmpInst::isEquality(Predicate) || (19.8k isIntegerPredicate19.8k && C1 == C217.9k )) |
1127 | 423k | return UndefValue::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. |
1131 | 19.5k | if (isIntegerPredicate) |
1132 | 17.7k | return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate)); |
1133 | | |
1134 | | // Choosing NaN for the undef will always make unordered comparison succeed |
1135 | | // and ordered comparison fails. |
1136 | 1.83k | return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate)); |
1137 | 19.5k | } |
1138 | | |
1139 | 38.7M | if (C2->isNullValue()) { |
1140 | | // The caller is expected to commute the operands if the constant expression |
1141 | | // is C2. |
1142 | | // C1 >= 0 --> true |
1143 | 23.6M | if (Predicate == ICmpInst::ICMP_UGE) |
1144 | 511 | return Constant::getAllOnesValue(ResultTy); |
1145 | | // C1 < 0 --> false |
1146 | 23.6M | if (Predicate == ICmpInst::ICMP_ULT) |
1147 | 25.9k | return Constant::getNullValue(ResultTy); |
1148 | 23.6M | } |
1149 | | |
1150 | | // If the comparison is a comparison between two i1's, simplify it. |
1151 | 38.7M | if (C1->getType()->isIntOrIntVectorTy(1)) { |
1152 | 253k | switch (Predicate) { |
1153 | 8.38k | case ICmpInst::ICMP_EQ: |
1154 | 8.38k | if (isa<ConstantExpr>(C1)) |
1155 | 0 | return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2)); |
1156 | 8.38k | return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2); |
1157 | 36 | case ICmpInst::ICMP_NE: |
1158 | 36 | return ConstantExpr::getXor(C1, C2); |
1159 | 244k | default: |
1160 | 244k | break; |
1161 | 253k | } |
1162 | 253k | } |
1163 | | |
1164 | 38.7M | if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)28.2M ) { |
1165 | 28.2M | const APInt &V1 = cast<ConstantInt>(C1)->getValue(); |
1166 | 28.2M | const APInt &V2 = cast<ConstantInt>(C2)->getValue(); |
1167 | 28.2M | return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate)); |
1168 | 28.2M | } else if (10.4M isa<ConstantFP>(C1)10.4M && isa<ConstantFP>(C2)147k ) { |
1169 | 147k | const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF(); |
1170 | 147k | const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF(); |
1171 | 147k | return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate)); |
1172 | 10.3M | } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) { |
1173 | | |
1174 | | // Fast path for splatted constants. |
1175 | 22.1k | if (Constant *C1Splat = C1->getSplatValue()) |
1176 | 22.1k | if (Constant *C2Splat = C2->getSplatValue()) |
1177 | 22.0k | if (Constant *Elt = |
1178 | 22.0k | ConstantFoldCompareInstruction(Predicate, C1Splat, C2Splat)) |
1179 | 22.0k | return ConstantVector::getSplat(C1VTy->getElementCount(), Elt); |
1180 | | |
1181 | | // Do not iterate on scalable vector. The number of elements is unknown at |
1182 | | // compile-time. |
1183 | 62 | if (isa<ScalableVectorType>(C1VTy)) |
1184 | 0 | return nullptr; |
1185 | | |
1186 | | // If we can constant fold the comparison of each element, constant fold |
1187 | | // the whole vector comparison. |
1188 | 62 | SmallVector<Constant*, 4> ResElts; |
1189 | 62 | Type *Ty = IntegerType::get(C1->getContext(), 32); |
1190 | | // Compare the elements, producing an i1 result or constant expr. |
1191 | 62 | for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue(); |
1192 | 616 | I != E; ++I554 ) { |
1193 | 554 | Constant *C1E = |
1194 | 554 | ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I)); |
1195 | 554 | Constant *C2E = |
1196 | 554 | ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I)); |
1197 | 554 | Constant *Elt = ConstantFoldCompareInstruction(Predicate, C1E, C2E); |
1198 | 554 | if (!Elt) |
1199 | 0 | return nullptr; |
1200 | | |
1201 | 554 | ResElts.push_back(Elt); |
1202 | 554 | } |
1203 | | |
1204 | 62 | return ConstantVector::get(ResElts); |
1205 | 62 | } |
1206 | | |
1207 | 10.3M | if (C1->getType()->isFPOrFPVectorTy()) { |
1208 | 0 | if (C1 == C2) { |
1209 | | // We know that C1 == C2 || isUnordered(C1, C2). |
1210 | 0 | if (Predicate == FCmpInst::FCMP_ONE) |
1211 | 0 | return ConstantInt::getFalse(ResultTy); |
1212 | 0 | else if (Predicate == FCmpInst::FCMP_UEQ) |
1213 | 0 | return ConstantInt::getTrue(ResultTy); |
1214 | 0 | } |
1215 | 10.3M | } else { |
1216 | | // Evaluate the relation between the two constants, per the predicate. |
1217 | 10.3M | int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. |
1218 | 10.3M | switch (evaluateICmpRelation(C1, C2)) { |
1219 | 0 | default: llvm_unreachable("Unknown relational!"); |
1220 | 655k | case ICmpInst::BAD_ICMP_PREDICATE: |
1221 | 655k | break; // Couldn't determine anything about these constants. |
1222 | 7.82M | case 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 | 7.82M | Result = ICmpInst::isTrueWhenEqual(Predicate); |
1226 | 7.82M | break; |
1227 | 224k | case ICmpInst::ICMP_ULT: |
1228 | 224k | switch (Predicate) { |
1229 | 203k | case ICmpInst::ICMP_ULT: 3 case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE: |
1230 | 203k | Result = 1; break; |
1231 | 20.6k | case ICmpInst::ICMP_UGT: 44 case ICmpInst::ICMP_EQ: 20.6k case ICmpInst::ICMP_UGE: |
1232 | 20.6k | Result = 0; break; |
1233 | 0 | default: |
1234 | 0 | break; |
1235 | 224k | } |
1236 | 224k | break; |
1237 | 224k | case ICmpInst::ICMP_SLT: |
1238 | 0 | switch (Predicate) { |
1239 | 0 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE: |
1240 | 0 | Result = 1; break; |
1241 | 0 | case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE: |
1242 | 0 | Result = 0; break; |
1243 | 0 | default: |
1244 | 0 | break; |
1245 | 0 | } |
1246 | 0 | break; |
1247 | 1.38M | case ICmpInst::ICMP_UGT: |
1248 | 1.38M | switch (Predicate) { |
1249 | 116k | case ICmpInst::ICMP_UGT: 12 case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE: |
1250 | 116k | Result = 1; break; |
1251 | 1.26M | case ICmpInst::ICMP_ULT: 0 case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE: |
1252 | 1.26M | Result = 0; break; |
1253 | 0 | default: |
1254 | 0 | break; |
1255 | 1.38M | } |
1256 | 1.38M | break; |
1257 | 1.38M | case ICmpInst::ICMP_SGT: |
1258 | 0 | switch (Predicate) { |
1259 | 0 | case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE: |
1260 | 0 | Result = 1; break; |
1261 | 0 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE: |
1262 | 0 | Result = 0; break; |
1263 | 0 | default: |
1264 | 0 | break; |
1265 | 0 | } |
1266 | 0 | break; |
1267 | 0 | case ICmpInst::ICMP_ULE: |
1268 | 0 | if (Predicate == ICmpInst::ICMP_UGT) |
1269 | 0 | Result = 0; |
1270 | 0 | if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE) |
1271 | 0 | Result = 1; |
1272 | 0 | break; |
1273 | 0 | case ICmpInst::ICMP_SLE: |
1274 | 0 | if (Predicate == ICmpInst::ICMP_SGT) |
1275 | 0 | Result = 0; |
1276 | 0 | if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE) |
1277 | 0 | Result = 1; |
1278 | 0 | break; |
1279 | 0 | case ICmpInst::ICMP_UGE: |
1280 | 0 | if (Predicate == ICmpInst::ICMP_ULT) |
1281 | 0 | Result = 0; |
1282 | 0 | if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE) |
1283 | 0 | Result = 1; |
1284 | 0 | break; |
1285 | 0 | case ICmpInst::ICMP_SGE: |
1286 | 0 | if (Predicate == ICmpInst::ICMP_SLT) |
1287 | 0 | Result = 0; |
1288 | 0 | if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE) |
1289 | 0 | Result = 1; |
1290 | 0 | break; |
1291 | 223k | case ICmpInst::ICMP_NE: |
1292 | 223k | if (Predicate == ICmpInst::ICMP_EQ) |
1293 | 198k | Result = 0; |
1294 | 223k | if (Predicate == ICmpInst::ICMP_NE) |
1295 | 24.4k | Result = 1; |
1296 | 223k | break; |
1297 | 10.3M | } |
1298 | | |
1299 | | // If we evaluated the result, return it now. |
1300 | 10.3M | if (Result != -1) |
1301 | 9.65M | return ConstantInt::get(ResultTy, Result); |
1302 | | |
1303 | 655k | if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)648k ) || |
1304 | 655k | (C1->isNullValue() && !C2->isNullValue()0 )) { |
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 | 0 | Predicate = ICmpInst::getSwappedPredicate(Predicate); |
1309 | 0 | return ConstantFoldCompareInstruction(Predicate, C2, C1); |
1310 | 0 | } |
1311 | 655k | } |
1312 | 655k | return nullptr; |
1313 | 10.3M | } |
1314 | | |
1315 | | Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, |
1316 | | std::optional<ConstantRange> InRange, |
1317 | 13.8M | ArrayRef<Value *> Idxs) { |
1318 | 13.8M | if (Idxs.empty()) return C0 ; |
1319 | | |
1320 | 13.8M | Type *GEPTy = GetElementPtrInst::getGEPReturnType( |
1321 | 13.8M | C, ArrayRef((Value *const *)Idxs.data(), Idxs.size())); |
1322 | | |
1323 | 13.8M | if (isa<PoisonValue>(C)) |
1324 | 72 | return PoisonValue::get(GEPTy); |
1325 | | |
1326 | 13.8M | if (isa<UndefValue>(C)) |
1327 | 24 | return UndefValue::get(GEPTy); |
1328 | | |
1329 | 13.8M | auto IsNoOp = [&]() { |
1330 | | // Avoid losing inrange information. |
1331 | 13.8M | if (InRange) |
1332 | 3.34M | return false; |
1333 | | |
1334 | 11.1M | return all_of(Idxs, [](Value *Idx) 10.4M { |
1335 | 11.1M | Constant *IdxC = cast<Constant>(Idx); |
1336 | 11.1M | return IdxC->isNullValue() || isa<UndefValue>(IdxC)10.3M ; |
1337 | 11.1M | }); |
1338 | 13.8M | }; |
1339 | 13.8M | if (IsNoOp()) |
1340 | 82.1k | return GEPTy->isVectorTy() && !C->getType()->isVectorTy()0 |
1341 | 82.1k | ? ConstantVector::getSplat( |
1342 | 0 | cast<VectorType>(GEPTy)->getElementCount(), C) |
1343 | 82.1k | : C; |
1344 | | |
1345 | 13.7M | return nullptr; |
1346 | 13.8M | } |