llvm.org GIT mirror llvm / db2b285
ConstantFolding.cpp: Whitespace. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@167377 91177308-0d34-0410-b5e6-96231b3b80d8 NAKAMURA Takumi 8 years ago
1 changed file(s) with 100 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
4040 // Constant Folding internal helper functions
4141 //===----------------------------------------------------------------------===//
4242
43 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
43 /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
4444 /// DataLayout. This always returns a non-null constant, but it may be a
4545 /// ConstantExpr if unfoldable.
4646 static Constant *FoldBitCast(Constant *C, Type *DestTy,
5858 return ConstantExpr::getBitCast(C, DestTy);
5959
6060 unsigned NumSrcElts = CDV->getType()->getNumElements();
61
61
6262 Type *SrcEltTy = CDV->getType()->getElementType();
63
63
6464 // If the vector is a vector of floating point, convert it to vector of int
6565 // to simplify things.
6666 if (SrcEltTy->isFloatingPointTy()) {
7171 C = ConstantExpr::getBitCast(C, SrcIVTy);
7272 CDV = cast(C);
7373 }
74
74
7575 // Now that we know that the input value is a vector of integers, just shift
7676 // and insert them into our result.
7777 unsigned BitShift = TD.getTypeAllocSizeInBits(SrcEltTy);
8383 else
8484 Result |= CDV->getElementAsInteger(i);
8585 }
86
86
8787 return ConstantInt::get(IT, Result);
8888 }
89
89
9090 // The code below only handles casts to vectors currently.
9191 VectorType *DestVTy = dyn_cast(DestTy);
9292 if (DestVTy == 0)
9393 return ConstantExpr::getBitCast(C, DestTy);
94
94
9595 // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
9696 // vector so the code below can handle it uniformly.
9797 if (isa(C) || isa(C)) {
9898 Constant *Ops = C; // don't take the address of C!
9999 return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
100100 }
101
101
102102 // If this is a bitcast from constant vector -> vector, fold it.
103103 if (!isa(C) && !isa(C))
104104 return ConstantExpr::getBitCast(C, DestTy);
105
105
106106 // If the element types match, VMCore can fold it.
107107 unsigned NumDstElt = DestVTy->getNumElements();
108108 unsigned NumSrcElt = C->getType()->getVectorNumElements();
109109 if (NumDstElt == NumSrcElt)
110110 return ConstantExpr::getBitCast(C, DestTy);
111
111
112112 Type *SrcEltTy = C->getType()->getVectorElementType();
113113 Type *DstEltTy = DestVTy->getElementType();
114
115 // Otherwise, we're changing the number of elements in a vector, which
114
115 // Otherwise, we're changing the number of elements in a vector, which
116116 // requires endianness information to do the right thing. For example,
117117 // bitcast (<2 x i64> to <4 x i32>)
118118 // folds to (little endian):
119119 // <4 x i32>
120120 // and to (big endian):
121121 // <4 x i32>
122
122
123123 // First thing is first. We only want to think about integer here, so if
124124 // we have something in FP form, recast it as integer.
125125 if (DstEltTy->isFloatingPointTy()) {
129129 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
130130 // Recursively handle this integer conversion, if possible.
131131 C = FoldBitCast(C, DestIVTy, TD);
132
132
133133 // Finally, VMCore can handle this now that #elts line up.
134134 return ConstantExpr::getBitCast(C, DestTy);
135135 }
136
136
137137 // Okay, we know the destination is integer, if the input is FP, convert
138138 // it to integer first.
139139 if (SrcEltTy->isFloatingPointTy()) {
147147 !isa(C))
148148 return C;
149149 }
150
150
151151 // Now we know that the input and output vectors are both integer vectors
152152 // of the same size, and that their #elements is not the same. Do the
153153 // conversion here, which depends on whether the input or output has
154154 // more elements.
155155 bool isLittleEndian = TD.isLittleEndian();
156
156
157157 SmallVector Result;
158158 if (NumDstElt < NumSrcElt) {
159159 // Handle: bitcast (<4 x i32> to <2 x i64>)
169169 Constant *Src =dyn_cast(C->getAggregateElement(SrcElt++));
170170 if (!Src) // Reject constantexpr elements.
171171 return ConstantExpr::getBitCast(C, DestTy);
172
172
173173 // Zero extend the element to the right size.
174174 Src = ConstantExpr::getZExt(Src, Elt->getType());
175
175
176176 // Shift it to the right place, depending on endianness.
177 Src = ConstantExpr::getShl(Src,
177 Src = ConstantExpr::getShl(Src,
178178 ConstantInt::get(Src->getType(), ShiftAmt));
179179 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
180
180
181181 // Mix it in.
182182 Elt = ConstantExpr::getOr(Elt, Src);
183183 }
185185 }
186186 return ConstantVector::get(Result);
187187 }
188
188
189189 // Handle: bitcast (<2 x i64> to <4 x i32>)
190190 unsigned Ratio = NumDstElt/NumSrcElt;
191191 unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
192
192
193193 // Loop over each source value, expanding into multiple results.
194194 for (unsigned i = 0; i != NumSrcElt; ++i) {
195195 Constant *Src = dyn_cast(C->getAggregateElement(i));
196196 if (!Src) // Reject constantexpr elements.
197197 return ConstantExpr::getBitCast(C, DestTy);
198
198
199199 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
200200 for (unsigned j = 0; j != Ratio; ++j) {
201201 // Shift the piece of the value into the right place, depending on
202202 // endianness.
203 Constant *Elt = ConstantExpr::getLShr(Src,
203 Constant *Elt = ConstantExpr::getLShr(Src,
204204 ConstantInt::get(Src->getType(), ShiftAmt));
205205 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
206
206
207207 // Truncate and remember this piece.
208208 Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
209209 }
210210 }
211
211
212212 return ConstantVector::get(Result);
213213 }
214214
223223 Offset = 0;
224224 return true;
225225 }
226
226
227227 // Otherwise, if this isn't a constant expr, bail out.
228228 ConstantExpr *CE = dyn_cast(C);
229229 if (!CE) return false;
230
230
231231 // Look through ptr->int and ptr->ptr casts.
232232 if (CE->getOpcode() == Instruction::PtrToInt ||
233233 CE->getOpcode() == Instruction::BitCast)
234234 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
235
236 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
235
236 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
237237 if (CE->getOpcode() == Instruction::GetElementPtr) {
238238 // Cannot compute this if the element type of the pointer is missing size
239239 // info.
240240 if (!cast(CE->getOperand(0)->getType())
241241 ->getElementType()->isSized())
242242 return false;
243
243
244244 // If the base isn't a global+constant, we aren't either.
245245 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD))
246246 return false;
247
247
248248 // Otherwise, add any offset that our operands provide.
249249 gep_type_iterator GTI = gep_type_begin(CE);
250250 for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end();
252252 ConstantInt *CI = dyn_cast(*i);
253253 if (!CI) return false; // Index isn't a simple constant?
254254 if (CI->isZero()) continue; // Not adding anything.
255
255
256256 if (StructType *ST = dyn_cast(*GTI)) {
257257 // N = N + Offset
258258 Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
263263 }
264264 return true;
265265 }
266
266
267267 return false;
268268 }
269269
276276 const DataLayout &TD) {
277277 assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
278278 "Out of range access");
279
279
280280 // If this element is zero or undefined, we can just return since *CurPtr is
281281 // zero initialized.
282282 if (isa(C) || isa(C))
283283 return true;
284
284
285285 if (ConstantInt *CI = dyn_cast(C)) {
286286 if (CI->getBitWidth() > 64 ||
287287 (CI->getBitWidth() & 7) != 0)
288288 return false;
289
289
290290 uint64_t Val = CI->getZExtValue();
291291 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
292
292
293293 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
294294 CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8));
295295 ++ByteOffset;
296296 }
297297 return true;
298298 }
299
299
300300 if (ConstantFP *CFP = dyn_cast(C)) {
301301 if (CFP->getType()->isDoubleTy()) {
302302 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
308308 }
309309 return false;
310310 }
311
311
312312 if (ConstantStruct *CS = dyn_cast(C)) {
313313 const StructLayout *SL = TD.getStructLayout(CS->getType());
314314 unsigned Index = SL->getElementContainingOffset(ByteOffset);
315315 uint64_t CurEltOffset = SL->getElementOffset(Index);
316316 ByteOffset -= CurEltOffset;
317
317
318318 while (1) {
319319 // If the element access is to the element itself and not to tail padding,
320320 // read the bytes from the element.
324324 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
325325 BytesLeft, TD))
326326 return false;
327
327
328328 ++Index;
329
329
330330 // Check to see if we read from the last struct element, if so we're done.
331331 if (Index == CS->getType()->getNumElements())
332332 return true;
374374 }
375375 return true;
376376 }
377
377
378378 if (ConstantExpr *CE = dyn_cast(C)) {
379379 if (CE->getOpcode() == Instruction::IntToPtr &&
380 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
381 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
380 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
381 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
382382 BytesLeft, TD);
383383 }
384384
390390 const DataLayout &TD) {
391391 Type *LoadTy = cast(C->getType())->getElementType();
392392 IntegerType *IntType = dyn_cast(LoadTy);
393
393
394394 // If this isn't an integer load we can't fold it directly.
395395 if (!IntType) {
396396 // If this is a float/double load, we can try folding it as an int32/64 load
414414 return FoldBitCast(Res, LoadTy, TD);
415415 return 0;
416416 }
417
417
418418 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
419419 if (BytesLoaded > 32 || BytesLoaded == 0) return 0;
420
420
421421 GlobalValue *GVal;
422422 int64_t Offset;
423423 if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
424424 return 0;
425
425
426426 GlobalVariable *GV = dyn_cast(GVal);
427427 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
428428 !GV->getInitializer()->getType()->isSized())
431431 // If we're loading off the beginning of the global, some bytes may be valid,
432432 // but we don't try to handle this.
433433 if (Offset < 0) return 0;
434
434
435435 // If we're not accessing anything in this constant, the result is undefined.
436436 if (uint64_t(Offset) >= TD.getTypeAllocSize(GV->getInitializer()->getType()))
437437 return UndefValue::get(IntType);
438
438
439439 unsigned char RawBytes[32] = {0};
440440 if (!ReadDataFromGlobal(GV->getInitializer(), Offset, RawBytes,
441441 BytesLoaded, TD))
463463 // If the loaded value isn't a constant expr, we can't handle it.
464464 ConstantExpr *CE = dyn_cast(C);
465465 if (!CE) return 0;
466
466
467467 if (CE->getOpcode() == Instruction::GetElementPtr) {
468468 if (GlobalVariable *GV = dyn_cast(CE->getOperand(0)))
469469 if (GV->isConstant() && GV->hasDefinitiveInitializer())
470 if (Constant *V =
470 if (Constant *V =
471471 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
472472 return V;
473473 }
474
474
475475 // Instead of loading constant c string, use corresponding integer value
476476 // directly if string length is small enough.
477477 StringRef Str;
499499 SingleChar = 0;
500500 StrVal = (StrVal << 8) | SingleChar;
501501 }
502
502
503503 Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
504504 if (Ty->isFloatingPointTy())
505505 Res = ConstantExpr::getBitCast(Res, Ty);
506506 return Res;
507507 }
508508 }
509
509
510510 // If this load comes from anywhere in a constant global, and if the global
511511 // is all undef or zero, we know what it loads.
512512 if (GlobalVariable *GV =
519519 return UndefValue::get(ResTy);
520520 }
521521 }
522
522
523523 // Try hard to fold loads from bitcasted strange and non-type-safe things. We
524524 // currently don't do any of this for big endian systems. It can be
525525 // generalized in the future if someone is interested.
530530
531531 static Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout *TD){
532532 if (LI->isVolatile()) return 0;
533
533
534534 if (Constant *C = dyn_cast(LI->getOperand(0)))
535535 return ConstantFoldLoadFromConstPtr(C, TD);
536536
539539
540540 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
541541 /// Attempt to symbolically evaluate the result of a binary operator merging
542 /// these together. If target data info is available, it is provided as TD,
542 /// these together. If target data info is available, it is provided as TD,
543543 /// otherwise TD is null.
544544 static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
545545 Constant *Op1, const DataLayout *TD){
546546 // SROA
547
547
548548 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
549549 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
550550 // bits.
551
552
551
552
553553 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
554554 // constant. This happens frequently when iterating over a global array.
555555 if (Opc == Instruction::Sub && TD) {
556556 GlobalValue *GV1, *GV2;
557557 int64_t Offs1, Offs2;
558
558
559559 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD))
560560 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
561561 GV1 == GV2) {
563563 return ConstantInt::get(Op0->getType(), Offs1-Offs2);
564564 }
565565 }
566
566
567567 return 0;
568568 }
569569
627627 if (!TD || !cast(Ptr->getType())->getElementType()->isSized() ||
628628 !Ptr->getType()->isPointerTy())
629629 return 0;
630
630
631631 Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext());
632632
633633 // If this is a constant expr gep that is effectively computing an
634634 // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
635635 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
636636 if (!isa(Ops[i])) {
637
637
638638 // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
639639 // "inttoptr (sub (ptrtoint Ptr), V)"
640640 if (Ops.size() == 2 &&
708708 // The only pointer indexing we'll do is on the first index of the GEP.
709709 if (!NewIdxs.empty())
710710 break;
711
711
712712 // Only handle pointers to sized types, not pointers to functions.
713713 if (!ATy->getElementType()->isSized())
714714 return 0;
715715 }
716
716
717717 // Determine which element of the array the offset points into.
718718 APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
719719 IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext());
836836 if (const CmpInst *CI = dyn_cast(I))
837837 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
838838 TD, TLI);
839
839
840840 if (const LoadInst *LI = dyn_cast(I))
841841 return ConstantFoldLoadInst(LI, TD);
842842
886886 /// information, due to only being passed an opcode and operands. Constant
887887 /// folding using this function strips this information.
888888 ///
889 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
889 Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, Type *DestTy,
890890 ArrayRef Ops,
891891 const DataLayout *TD,
892 const TargetLibraryInfo *TLI) {
892 const TargetLibraryInfo *TLI) {
893893 // Handle easy binops first.
894894 if (Instruction::isBinaryOp(Opcode)) {
895895 if (isa(Ops[0]) || isa(Ops[1]))
896896 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
897897 return C;
898
898
899899 return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
900900 }
901
901
902902 switch (Opcode) {
903903 default: return 0;
904904 case Instruction::ICmp:
916916 Constant *Input = CE->getOperand(0);
917917 unsigned InWidth = Input->getType()->getScalarSizeInBits();
918918 if (TD->getPointerSizeInBits() < InWidth) {
919 Constant *Mask =
919 Constant *Mask =
920920 ConstantInt::get(CE->getContext(), APInt::getLowBitsSet(InWidth,
921921 TD->getPointerSizeInBits()));
922922 Input = ConstantExpr::getAnd(Input, Mask);
964964 return C;
965965 if (Constant *C = SymbolicallyEvaluateGEP(Ops, DestTy, TD, TLI))
966966 return C;
967
967
968968 return ConstantExpr::getGetElementPtr(Ops[0], Ops.slice(1));
969969 }
970970 }
974974 /// returns a constant expression of the specified operands.
975975 ///
976976 Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
977 Constant *Ops0, Constant *Ops1,
977 Constant *Ops0, Constant *Ops1,
978978 const DataLayout *TD,
979979 const TargetLibraryInfo *TLI) {
980980 // fold: icmp (inttoptr x), null -> icmp x, 0
995995 Constant *Null = Constant::getNullValue(C->getType());
996996 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
997997 }
998
998
999999 // Only do this transformation if the int is intptrty in size, otherwise
10001000 // there is a truncation or extension that we aren't modeling.
1001 if (CE0->getOpcode() == Instruction::PtrToInt &&
1001 if (CE0->getOpcode() == Instruction::PtrToInt &&
10021002 CE0->getType() == IntPtrTy) {
10031003 Constant *C = CE0->getOperand(0);
10041004 Constant *Null = Constant::getNullValue(C->getType());
10051005 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD, TLI);
10061006 }
10071007 }
1008
1008
10091009 if (ConstantExpr *CE1 = dyn_cast(Ops1)) {
10101010 if (TD && CE0->getOpcode() == CE1->getOpcode()) {
10111011 Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
10291029 CE1->getOperand(0), TD, TLI);
10301030 }
10311031 }
1032
1032
10331033 // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
10341034 // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
10351035 if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
10361036 CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
1037 Constant *LHS =
1037 Constant *LHS =
10381038 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,
10391039 TD, TLI);
1040 Constant *RHS =
1040 Constant *RHS =
10411041 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,
10421042 TD, TLI);
1043 unsigned OpC =
1043 unsigned OpC =
10441044 Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
10451045 Constant *Ops[] = { LHS, RHS };
10461046 return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, TD, TLI);
10471047 }
10481048 }
1049
1049
10501050 return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
10511051 }
10521052
10541054 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
10551055 /// getelementptr constantexpr, return the constant value being addressed by the
10561056 /// constant expression, or null if something is funny and we can't decide.
1057 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
1057 Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
10581058 ConstantExpr *CE) {
10591059 if (!CE->getOperand(1)->isNullValue())
10601060 return 0; // Do not allow stepping over the value!
11241124
11251125 if (!F->hasName()) return false;
11261126 StringRef Name = F->getName();
1127
1127
11281128 // In these cases, the check of the length is required. We don't want to
11291129 // return true for a name like "cos\0blah" which strcmp would return equal to
11301130 // "cos", but has length 8.
11311131 switch (Name[0]) {
11321132 default: return false;
11331133 case 'a':
1134 return Name == "acos" || Name == "asin" ||
1134 return Name == "acos" || Name == "asin" ||
11351135 Name == "atan" || Name == "atan2";
11361136 case 'c':
11371137 return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
11511151 }
11521152 }
11531153
1154 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
1154 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
11551155 Type *Ty) {
11561156 sys::llvm_fenv_clearexcept();
11571157 V = NativeFP(V);
11591159 sys::llvm_fenv_clearexcept();
11601160 return 0;
11611161 }
1162
1162
11631163 if (Ty->isFloatTy())
11641164 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
11651165 if (Ty->isDoubleTy())
11751175 sys::llvm_fenv_clearexcept();
11761176 return 0;
11771177 }
1178
1178
11791179 if (Ty->isFloatTy())
11801180 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
11811181 if (Ty->isDoubleTy())
12691269 case 'e':
12701270 if (Name == "exp" && TLI->has(LibFunc::exp))
12711271 return ConstantFoldFP(exp, V, Ty);
1272
1272
12731273 if (Name == "exp2" && TLI->has(LibFunc::exp2)) {
12741274 // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
12751275 // C99 library.
13451345 }
13461346
13471347 // Support ConstantVector in case we have an Undef in the top.
1348 if (isa(Operands[0]) ||
1348 if (isa(Operands[0]) ||
13491349 isa(Operands[0])) {
13501350 Constant *Op = cast(Operands[0]);
13511351 switch (F->getIntrinsicID()) {
13641364 case Intrinsic::x86_sse2_cvttsd2si64:
13651365 if (ConstantFP *FPOp =
13661366 dyn_cast_or_null(Op->getAggregateElement(0U)))
1367 return ConstantFoldConvertToInt(FPOp->getValueAPF(),
1367 return ConstantFoldConvertToInt(FPOp->getValueAPF(),
13681368 /*roundTowardZero=*/true, Ty);
13691369 }
13701370 }
1371
1371
13721372 if (isa(Operands[0])) {
13731373 if (F->getIntrinsicID() == Intrinsic::bswap)
13741374 return Operands[0];
13821382 if (ConstantFP *Op1 = dyn_cast(Operands[0])) {
13831383 if (!Ty->isFloatTy() && !Ty->isDoubleTy())
13841384 return 0;
1385 double Op1V = Ty->isFloatTy() ?
1385 double Op1V = Ty->isFloatTy() ?
13861386 (double)Op1->getValueAPF().convertToFloat() :
13871387 Op1->getValueAPF().convertToDouble();
13881388 if (ConstantFP *Op2 = dyn_cast(Operands[1])) {
13891389 if (Op2->getType() != Op1->getType())
13901390 return 0;
13911391
1392 double Op2V = Ty->isFloatTy() ?
1392 double Op2V = Ty->isFloatTy() ?
13931393 (double)Op2->getValueAPF().convertToFloat():
13941394 Op2->getValueAPF().convertToDouble();
13951395
14161416 }
14171417 return 0;
14181418 }
1419
1419
14201420 if (ConstantInt *Op1 = dyn_cast(Operands[0])) {
14211421 if (ConstantInt *Op2 = dyn_cast(Operands[1])) {
14221422 switch (F->getIntrinsicID()) {
14661466 return ConstantInt::get(Ty, Op1->getValue().countLeadingZeros());
14671467 }
14681468 }
1469
1469
14701470 return 0;
14711471 }
14721472 return 0;