blob: b64f3b90e0b8fbb3dca4bd86aa10119fb3b968d7 [file] [log] [blame]
Dan Gohman83e3c4f2009-09-10 23:07:18 +00001//===-- ConstantFolding.cpp - Fold instructions into constants ------------===//
John Criswellbd9d3702005-10-27 16:00:10 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
John Criswellbd9d3702005-10-27 16:00:10 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohman83e3c4f2009-09-10 23:07:18 +000010// This file defines routines for folding instructions into constants.
11//
12// Also, to supplement the basic VMCore ConstantExpr simplifications,
13// this file defines some additional folding routines that can make use of
14// TargetData information. These functions cannot go in VMCore due to library
15// dependency issues.
John Criswellbd9d3702005-10-27 16:00:10 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Analysis/ConstantFolding.h"
20#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattner55207322007-01-30 23:45:45 +000022#include "llvm/Function.h"
Dan Gohman9a38e3e2009-05-07 19:46:24 +000023#include "llvm/GlobalVariable.h"
John Criswellbd9d3702005-10-27 16:00:10 +000024#include "llvm/Instructions.h"
25#include "llvm/Intrinsics.h"
Jay Foad562b84b2011-04-11 09:35:34 +000026#include "llvm/Operator.h"
Chris Lattner62d327e2009-10-22 06:38:35 +000027#include "llvm/Analysis/ValueTracking.h"
28#include "llvm/Target/TargetData.h"
Chris Lattner55207322007-01-30 23:45:45 +000029#include "llvm/ADT/SmallVector.h"
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +000030#include "llvm/ADT/StringMap.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
John Criswellbd9d3702005-10-27 16:00:10 +000032#include "llvm/Support/GetElementPtrTypeIterator.h"
33#include "llvm/Support/MathExtras.h"
34#include <cerrno>
Jeff Cohen97af7512006-12-02 02:22:01 +000035#include <cmath>
Dylan Noblesmithe8e00f52011-06-23 13:56:01 +000036
37#include "FEnv.h"
John Criswellbd9d3702005-10-27 16:00:10 +000038using namespace llvm;
39
Chris Lattner03dd25c2007-01-31 00:51:48 +000040//===----------------------------------------------------------------------===//
41// Constant Folding internal helper functions
42//===----------------------------------------------------------------------===//
43
Chris Lattner6333c392009-10-25 06:08:26 +000044/// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
45/// TargetData. This always returns a non-null constant, but it may be a
46/// ConstantExpr if unfoldable.
47static Constant *FoldBitCast(Constant *C, const Type *DestTy,
48 const TargetData &TD) {
Chris Lattner93798da2009-10-25 06:15:37 +000049
50 // This only handles casts to vectors currently.
51 const VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
52 if (DestVTy == 0)
53 return ConstantExpr::getBitCast(C, DestTy);
54
55 // If this is a scalar -> vector cast, convert the input into a <1 x scalar>
56 // vector so the code below can handle it uniformly.
57 if (isa<ConstantFP>(C) || isa<ConstantInt>(C)) {
58 Constant *Ops = C; // don't take the address of C!
Chris Lattner2ca5c862011-02-15 00:14:00 +000059 return FoldBitCast(ConstantVector::get(Ops), DestTy, TD);
Chris Lattner93798da2009-10-25 06:15:37 +000060 }
61
Chris Lattner6333c392009-10-25 06:08:26 +000062 // If this is a bitcast from constant vector -> vector, fold it.
63 ConstantVector *CV = dyn_cast<ConstantVector>(C);
64 if (CV == 0)
65 return ConstantExpr::getBitCast(C, DestTy);
66
Chris Lattner6333c392009-10-25 06:08:26 +000067 // If the element types match, VMCore can fold it.
68 unsigned NumDstElt = DestVTy->getNumElements();
69 unsigned NumSrcElt = CV->getNumOperands();
70 if (NumDstElt == NumSrcElt)
71 return ConstantExpr::getBitCast(C, DestTy);
72
73 const Type *SrcEltTy = CV->getType()->getElementType();
74 const Type *DstEltTy = DestVTy->getElementType();
75
76 // Otherwise, we're changing the number of elements in a vector, which
77 // requires endianness information to do the right thing. For example,
78 // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
79 // folds to (little endian):
80 // <4 x i32> <i32 0, i32 0, i32 1, i32 0>
81 // and to (big endian):
82 // <4 x i32> <i32 0, i32 0, i32 0, i32 1>
83
84 // First thing is first. We only want to think about integer here, so if
85 // we have something in FP form, recast it as integer.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000086 if (DstEltTy->isFloatingPointTy()) {
Chris Lattner6333c392009-10-25 06:08:26 +000087 // Fold to an vector of integers with same size as our FP type.
88 unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits();
89 const Type *DestIVTy =
90 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt);
91 // Recursively handle this integer conversion, if possible.
92 C = FoldBitCast(C, DestIVTy, TD);
93 if (!C) return ConstantExpr::getBitCast(C, DestTy);
94
95 // Finally, VMCore can handle this now that #elts line up.
96 return ConstantExpr::getBitCast(C, DestTy);
97 }
98
99 // Okay, we know the destination is integer, if the input is FP, convert
100 // it to integer first.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000101 if (SrcEltTy->isFloatingPointTy()) {
Chris Lattner6333c392009-10-25 06:08:26 +0000102 unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits();
103 const Type *SrcIVTy =
104 VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt);
105 // Ask VMCore to do the conversion now that #elts line up.
106 C = ConstantExpr::getBitCast(C, SrcIVTy);
107 CV = dyn_cast<ConstantVector>(C);
108 if (!CV) // If VMCore wasn't able to fold it, bail out.
109 return C;
110 }
111
112 // Now we know that the input and output vectors are both integer vectors
113 // of the same size, and that their #elements is not the same. Do the
114 // conversion here, which depends on whether the input or output has
115 // more elements.
116 bool isLittleEndian = TD.isLittleEndian();
117
118 SmallVector<Constant*, 32> Result;
119 if (NumDstElt < NumSrcElt) {
120 // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>)
121 Constant *Zero = Constant::getNullValue(DstEltTy);
122 unsigned Ratio = NumSrcElt/NumDstElt;
123 unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits();
124 unsigned SrcElt = 0;
125 for (unsigned i = 0; i != NumDstElt; ++i) {
126 // Build each element of the result.
127 Constant *Elt = Zero;
128 unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
129 for (unsigned j = 0; j != Ratio; ++j) {
130 Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++));
131 if (!Src) // Reject constantexpr elements.
132 return ConstantExpr::getBitCast(C, DestTy);
133
134 // Zero extend the element to the right size.
135 Src = ConstantExpr::getZExt(Src, Elt->getType());
136
137 // Shift it to the right place, depending on endianness.
138 Src = ConstantExpr::getShl(Src,
139 ConstantInt::get(Src->getType(), ShiftAmt));
140 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
141
142 // Mix it in.
143 Elt = ConstantExpr::getOr(Elt, Src);
144 }
145 Result.push_back(Elt);
146 }
147 } else {
148 // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>)
149 unsigned Ratio = NumDstElt/NumSrcElt;
150 unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits();
151
152 // Loop over each source value, expanding into multiple results.
153 for (unsigned i = 0; i != NumSrcElt; ++i) {
154 Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i));
155 if (!Src) // Reject constantexpr elements.
156 return ConstantExpr::getBitCast(C, DestTy);
157
158 unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
159 for (unsigned j = 0; j != Ratio; ++j) {
160 // Shift the piece of the value into the right place, depending on
161 // endianness.
162 Constant *Elt = ConstantExpr::getLShr(Src,
163 ConstantInt::get(Src->getType(), ShiftAmt));
164 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
165
166 // Truncate and remember this piece.
167 Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy));
168 }
169 }
170 }
171
Chris Lattner2ca5c862011-02-15 00:14:00 +0000172 return ConstantVector::get(Result);
Chris Lattner6333c392009-10-25 06:08:26 +0000173}
174
175
Chris Lattner03dd25c2007-01-31 00:51:48 +0000176/// IsConstantOffsetFromGlobal - If this constant is actually a constant offset
177/// from a global, return the global and the constant. Because of
178/// constantexprs, this function is recursive.
179static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
180 int64_t &Offset, const TargetData &TD) {
181 // Trivial case, constant is the global.
182 if ((GV = dyn_cast<GlobalValue>(C))) {
183 Offset = 0;
184 return true;
185 }
186
187 // Otherwise, if this isn't a constant expr, bail out.
188 ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
189 if (!CE) return false;
190
191 // Look through ptr->int and ptr->ptr casts.
192 if (CE->getOpcode() == Instruction::PtrToInt ||
193 CE->getOpcode() == Instruction::BitCast)
194 return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD);
195
196 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
197 if (CE->getOpcode() == Instruction::GetElementPtr) {
198 // Cannot compute this if the element type of the pointer is missing size
199 // info.
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000200 if (!cast<PointerType>(CE->getOperand(0)->getType())
201 ->getElementType()->isSized())
Chris Lattner03dd25c2007-01-31 00:51:48 +0000202 return false;
203
204 // If the base isn't a global+constant, we aren't either.
205 if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD))
206 return false;
207
208 // Otherwise, add any offset that our operands provide.
209 gep_type_iterator GTI = gep_type_begin(CE);
Gabor Greifde2d74b2008-05-22 06:43:33 +0000210 for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end();
Gabor Greif785c6af2008-05-22 19:24:54 +0000211 i != e; ++i, ++GTI) {
Gabor Greifde2d74b2008-05-22 06:43:33 +0000212 ConstantInt *CI = dyn_cast<ConstantInt>(*i);
Chris Lattner03dd25c2007-01-31 00:51:48 +0000213 if (!CI) return false; // Index isn't a simple constant?
Dan Gohmane368b462010-06-18 14:22:04 +0000214 if (CI->isZero()) continue; // Not adding anything.
Chris Lattner03dd25c2007-01-31 00:51:48 +0000215
216 if (const StructType *ST = dyn_cast<StructType>(*GTI)) {
217 // N = N + Offset
Chris Lattnerb1919e22007-02-10 19:55:17 +0000218 Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
Chris Lattner03dd25c2007-01-31 00:51:48 +0000219 } else {
Jeff Cohenca5183d2007-03-05 00:00:42 +0000220 const SequentialType *SQT = cast<SequentialType>(*GTI);
Duncan Sands777d2302009-05-09 07:06:46 +0000221 Offset += TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue();
Chris Lattner03dd25c2007-01-31 00:51:48 +0000222 }
223 }
224 return true;
225 }
226
227 return false;
228}
229
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000230/// ReadDataFromGlobal - Recursive helper to read bits out of global. C is the
231/// constant being copied out of. ByteOffset is an offset into C. CurPtr is the
232/// pointer to copy results into and BytesLeft is the number of bytes left in
233/// the CurPtr buffer. TD is the target data.
234static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset,
235 unsigned char *CurPtr, unsigned BytesLeft,
236 const TargetData &TD) {
237 assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) &&
238 "Out of range access");
239
Chris Lattnerc7b13822009-10-24 05:27:19 +0000240 // If this element is zero or undefined, we can just return since *CurPtr is
241 // zero initialized.
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000242 if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
243 return true;
244
245 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
246 if (CI->getBitWidth() > 64 ||
247 (CI->getBitWidth() & 7) != 0)
248 return false;
249
250 uint64_t Val = CI->getZExtValue();
251 unsigned IntBytes = unsigned(CI->getBitWidth()/8);
252
253 for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) {
Chris Lattnerc7b13822009-10-24 05:27:19 +0000254 CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8));
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000255 ++ByteOffset;
256 }
257 return true;
258 }
259
260 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
261 if (CFP->getType()->isDoubleTy()) {
Chris Lattner6333c392009-10-25 06:08:26 +0000262 C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000263 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
264 }
265 if (CFP->getType()->isFloatTy()){
Chris Lattner6333c392009-10-25 06:08:26 +0000266 C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000267 return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD);
268 }
Chris Lattnerc7b13822009-10-24 05:27:19 +0000269 return false;
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000270 }
271
272 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
273 const StructLayout *SL = TD.getStructLayout(CS->getType());
274 unsigned Index = SL->getElementContainingOffset(ByteOffset);
275 uint64_t CurEltOffset = SL->getElementOffset(Index);
276 ByteOffset -= CurEltOffset;
277
278 while (1) {
279 // If the element access is to the element itself and not to tail padding,
280 // read the bytes from the element.
281 uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType());
282
283 if (ByteOffset < EltSize &&
284 !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr,
285 BytesLeft, TD))
286 return false;
287
288 ++Index;
289
290 // Check to see if we read from the last struct element, if so we're done.
291 if (Index == CS->getType()->getNumElements())
292 return true;
293
294 // If we read all of the bytes we needed from this element we're done.
295 uint64_t NextEltOffset = SL->getElementOffset(Index);
296
297 if (BytesLeft <= NextEltOffset-CurEltOffset-ByteOffset)
298 return true;
299
300 // Move to the next element of the struct.
Chris Lattnerc5af6492009-10-24 05:22:15 +0000301 CurPtr += NextEltOffset-CurEltOffset-ByteOffset;
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000302 BytesLeft -= NextEltOffset-CurEltOffset-ByteOffset;
303 ByteOffset = 0;
304 CurEltOffset = NextEltOffset;
305 }
306 // not reached.
307 }
308
309 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
310 uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType());
311 uint64_t Index = ByteOffset / EltSize;
312 uint64_t Offset = ByteOffset - Index * EltSize;
313 for (; Index != CA->getType()->getNumElements(); ++Index) {
314 if (!ReadDataFromGlobal(CA->getOperand(Index), Offset, CurPtr,
315 BytesLeft, TD))
316 return false;
317 if (EltSize >= BytesLeft)
318 return true;
319
320 Offset = 0;
321 BytesLeft -= EltSize;
322 CurPtr += EltSize;
323 }
324 return true;
325 }
326
327 if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
328 uint64_t EltSize = TD.getTypeAllocSize(CV->getType()->getElementType());
329 uint64_t Index = ByteOffset / EltSize;
330 uint64_t Offset = ByteOffset - Index * EltSize;
331 for (; Index != CV->getType()->getNumElements(); ++Index) {
332 if (!ReadDataFromGlobal(CV->getOperand(Index), Offset, CurPtr,
333 BytesLeft, TD))
334 return false;
335 if (EltSize >= BytesLeft)
336 return true;
337
338 Offset = 0;
339 BytesLeft -= EltSize;
340 CurPtr += EltSize;
341 }
342 return true;
343 }
344
Anders Carlsson6475d942011-02-06 20:11:56 +0000345 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Anders Carlsson5d43ff42011-02-06 20:22:49 +0000346 if (CE->getOpcode() == Instruction::IntToPtr &&
347 CE->getOperand(0)->getType() == TD.getIntPtrType(CE->getContext()))
Anders Carlsson6475d942011-02-06 20:11:56 +0000348 return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
349 BytesLeft, TD);
Anders Carlsson6475d942011-02-06 20:11:56 +0000350 }
351
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000352 // Otherwise, unknown initializer type.
353 return false;
354}
355
356static Constant *FoldReinterpretLoadFromConstPtr(Constant *C,
357 const TargetData &TD) {
Chris Lattner17f0cd32009-10-23 06:57:37 +0000358 const Type *LoadTy = cast<PointerType>(C->getType())->getElementType();
359 const IntegerType *IntType = dyn_cast<IntegerType>(LoadTy);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000360
361 // If this isn't an integer load we can't fold it directly.
362 if (!IntType) {
363 // If this is a float/double load, we can try folding it as an int32/64 load
Chris Lattner17f0cd32009-10-23 06:57:37 +0000364 // and then bitcast the result. This can be useful for union cases. Note
365 // that address spaces don't matter here since we're not going to result in
366 // an actual new load.
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000367 const Type *MapTy;
Chris Lattner17f0cd32009-10-23 06:57:37 +0000368 if (LoadTy->isFloatTy())
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000369 MapTy = Type::getInt32PtrTy(C->getContext());
Chris Lattner17f0cd32009-10-23 06:57:37 +0000370 else if (LoadTy->isDoubleTy())
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000371 MapTy = Type::getInt64PtrTy(C->getContext());
Duncan Sands1df98592010-02-16 11:11:14 +0000372 else if (LoadTy->isVectorTy()) {
Chris Lattner17f0cd32009-10-23 06:57:37 +0000373 MapTy = IntegerType::get(C->getContext(),
374 TD.getTypeAllocSizeInBits(LoadTy));
375 MapTy = PointerType::getUnqual(MapTy);
376 } else
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000377 return 0;
378
Chris Lattner6333c392009-10-25 06:08:26 +0000379 C = FoldBitCast(C, MapTy, TD);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000380 if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD))
Chris Lattner6333c392009-10-25 06:08:26 +0000381 return FoldBitCast(Res, LoadTy, TD);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000382 return 0;
383 }
384
385 unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8;
Chris Lattner739208a2009-10-23 06:50:36 +0000386 if (BytesLoaded > 32 || BytesLoaded == 0) return 0;
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000387
388 GlobalValue *GVal;
389 int64_t Offset;
390 if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD))
391 return 0;
392
393 GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal);
Chris Lattnerc7b13822009-10-24 05:27:19 +0000394 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000395 !GV->getInitializer()->getType()->isSized())
396 return 0;
397
398 // If we're loading off the beginning of the global, some bytes may be valid,
399 // but we don't try to handle this.
400 if (Offset < 0) return 0;
401
402 // If we're not accessing anything in this constant, the result is undefined.
403 if (uint64_t(Offset) >= TD.getTypeAllocSize(GV->getInitializer()->getType()))
404 return UndefValue::get(IntType);
405
Chris Lattner739208a2009-10-23 06:50:36 +0000406 unsigned char RawBytes[32] = {0};
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000407 if (!ReadDataFromGlobal(GV->getInitializer(), Offset, RawBytes,
408 BytesLoaded, TD))
409 return 0;
410
Chris Lattnerb31189f2010-01-08 19:02:23 +0000411 APInt ResultVal = APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1]);
412 for (unsigned i = 1; i != BytesLoaded; ++i) {
Chris Lattner739208a2009-10-23 06:50:36 +0000413 ResultVal <<= 8;
Dan Gohmance800512010-04-12 02:22:30 +0000414 ResultVal |= RawBytes[BytesLoaded-1-i];
Chris Lattner739208a2009-10-23 06:50:36 +0000415 }
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000416
Chris Lattner739208a2009-10-23 06:50:36 +0000417 return ConstantInt::get(IntType->getContext(), ResultVal);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000418}
419
Chris Lattner878e4942009-10-22 06:25:11 +0000420/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would
421/// produce if it is constant and determinable. If this is not determinable,
422/// return null.
423Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C,
424 const TargetData *TD) {
425 // First, try the easy cases:
426 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
427 if (GV->isConstant() && GV->hasDefinitiveInitializer())
428 return GV->getInitializer();
429
Chris Lattnere00c43f2009-10-22 06:44:07 +0000430 // If the loaded value isn't a constant expr, we can't handle it.
431 ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
432 if (!CE) return 0;
433
434 if (CE->getOpcode() == Instruction::GetElementPtr) {
435 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
436 if (GV->isConstant() && GV->hasDefinitiveInitializer())
437 if (Constant *V =
438 ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
439 return V;
440 }
441
442 // Instead of loading constant c string, use corresponding integer value
443 // directly if string length is small enough.
444 std::string Str;
Chris Lattner44a7a382009-12-04 06:29:29 +0000445 if (TD && GetConstantStringInfo(CE, Str) && !Str.empty()) {
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000446 unsigned StrLen = Str.length();
Chris Lattnere00c43f2009-10-22 06:44:07 +0000447 const Type *Ty = cast<PointerType>(CE->getType())->getElementType();
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000448 unsigned NumBits = Ty->getPrimitiveSizeInBits();
Chris Lattnereae28952010-07-12 00:22:51 +0000449 // Replace load with immediate integer if the result is an integer or fp
450 // value.
451 if ((NumBits >> 3) == StrLen + 1 && (NumBits & 7) == 0 &&
Chandler Carruth490b8f52010-07-12 06:47:05 +0000452 (isa<IntegerType>(Ty) || Ty->isFloatingPointTy())) {
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000453 APInt StrVal(NumBits, 0);
454 APInt SingleChar(NumBits, 0);
Chris Lattnere00c43f2009-10-22 06:44:07 +0000455 if (TD->isLittleEndian()) {
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000456 for (signed i = StrLen-1; i >= 0; i--) {
Chris Lattnere00c43f2009-10-22 06:44:07 +0000457 SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
Chris Lattner62d327e2009-10-22 06:38:35 +0000458 StrVal = (StrVal << 8) | SingleChar;
459 }
Chris Lattnere00c43f2009-10-22 06:44:07 +0000460 } else {
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000461 for (unsigned i = 0; i < StrLen; i++) {
Chris Lattnere00c43f2009-10-22 06:44:07 +0000462 SingleChar = (uint64_t) Str[i] & UCHAR_MAX;
463 StrVal = (StrVal << 8) | SingleChar;
464 }
465 // Append NULL at the end.
466 SingleChar = 0;
467 StrVal = (StrVal << 8) | SingleChar;
Chris Lattner62d327e2009-10-22 06:38:35 +0000468 }
Chris Lattnereae28952010-07-12 00:22:51 +0000469
470 Constant *Res = ConstantInt::get(CE->getContext(), StrVal);
471 if (Ty->isFloatingPointTy())
472 Res = ConstantExpr::getBitCast(Res, Ty);
473 return Res;
Chris Lattner62d327e2009-10-22 06:38:35 +0000474 }
Chris Lattner878e4942009-10-22 06:25:11 +0000475 }
Chris Lattnere00c43f2009-10-22 06:44:07 +0000476
477 // If this load comes from anywhere in a constant global, and if the global
478 // is all undef or zero, we know what it loads.
Dan Gohmanbd1801b2011-01-24 18:53:32 +0000479 if (GlobalVariable *GV =
480 dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, TD))) {
Chris Lattnere00c43f2009-10-22 06:44:07 +0000481 if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
482 const Type *ResTy = cast<PointerType>(C->getType())->getElementType();
483 if (GV->getInitializer()->isNullValue())
484 return Constant::getNullValue(ResTy);
485 if (isa<UndefValue>(GV->getInitializer()))
486 return UndefValue::get(ResTy);
487 }
488 }
489
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000490 // Try hard to fold loads from bitcasted strange and non-type-safe things. We
491 // currently don't do any of this for big endian systems. It can be
492 // generalized in the future if someone is interested.
493 if (TD && TD->isLittleEndian())
494 return FoldReinterpretLoadFromConstPtr(CE, *TD);
Chris Lattner878e4942009-10-22 06:25:11 +0000495 return 0;
496}
497
498static Constant *ConstantFoldLoadInst(const LoadInst *LI, const TargetData *TD){
499 if (LI->isVolatile()) return 0;
500
501 if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
502 return ConstantFoldLoadFromConstPtr(C, TD);
Chris Lattnerfe8c7c82009-10-23 06:23:49 +0000503
Chris Lattner878e4942009-10-22 06:25:11 +0000504 return 0;
505}
Chris Lattner03dd25c2007-01-31 00:51:48 +0000506
507/// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
Nick Lewycky67e35662008-12-15 01:35:36 +0000508/// Attempt to symbolically evaluate the result of a binary operator merging
Chris Lattner03dd25c2007-01-31 00:51:48 +0000509/// these together. If target data info is available, it is provided as TD,
510/// otherwise TD is null.
511static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0,
Chris Lattner7b550cc2009-11-06 04:27:31 +0000512 Constant *Op1, const TargetData *TD){
Chris Lattner03dd25c2007-01-31 00:51:48 +0000513 // SROA
514
515 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
516 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
517 // bits.
518
519
520 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
521 // constant. This happens frequently when iterating over a global array.
522 if (Opc == Instruction::Sub && TD) {
523 GlobalValue *GV1, *GV2;
524 int64_t Offs1, Offs2;
525
526 if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD))
527 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
528 GV1 == GV2) {
529 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
Owen Andersoneed707b2009-07-24 23:12:02 +0000530 return ConstantInt::get(Op0->getType(), Offs1-Offs2);
Chris Lattner03dd25c2007-01-31 00:51:48 +0000531 }
532 }
533
Chris Lattner03dd25c2007-01-31 00:51:48 +0000534 return 0;
535}
536
Dan Gohman4f8eea82010-02-01 18:27:38 +0000537/// CastGEPIndices - If array indices are not pointer-sized integers,
538/// explicitly cast them so that they aren't implicitly casted by the
539/// getelementptr.
540static Constant *CastGEPIndices(Constant *const *Ops, unsigned NumOps,
541 const Type *ResultTy,
542 const TargetData *TD) {
543 if (!TD) return 0;
544 const Type *IntPtrTy = TD->getIntPtrType(ResultTy->getContext());
545
546 bool Any = false;
547 SmallVector<Constant*, 32> NewIdxs;
548 for (unsigned i = 1; i != NumOps; ++i) {
549 if ((i == 1 ||
550 !isa<StructType>(GetElementPtrInst::getIndexedType(Ops[0]->getType(),
Chris Lattnerc1da2042010-11-21 08:39:01 +0000551 reinterpret_cast<Value *const *>(Ops+1),
Dan Gohman4f8eea82010-02-01 18:27:38 +0000552 i-1))) &&
553 Ops[i]->getType() != IntPtrTy) {
554 Any = true;
555 NewIdxs.push_back(ConstantExpr::getCast(CastInst::getCastOpcode(Ops[i],
556 true,
557 IntPtrTy,
558 true),
559 Ops[i], IntPtrTy));
560 } else
561 NewIdxs.push_back(Ops[i]);
562 }
563 if (!Any) return 0;
564
565 Constant *C =
566 ConstantExpr::getGetElementPtr(Ops[0], &NewIdxs[0], NewIdxs.size());
567 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
568 if (Constant *Folded = ConstantFoldConstantExpression(CE, TD))
569 C = Folded;
570 return C;
571}
572
Chris Lattner03dd25c2007-01-31 00:51:48 +0000573/// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP
574/// constant expression, do so.
Chris Lattner7b550cc2009-11-06 04:27:31 +0000575static Constant *SymbolicallyEvaluateGEP(Constant *const *Ops, unsigned NumOps,
Chris Lattner03dd25c2007-01-31 00:51:48 +0000576 const Type *ResultTy,
577 const TargetData *TD) {
578 Constant *Ptr = Ops[0];
Chris Lattner268e7d72008-05-08 04:54:43 +0000579 if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized())
Chris Lattner03dd25c2007-01-31 00:51:48 +0000580 return 0;
Chris Lattnerf1cadf22011-01-06 22:24:29 +0000581
582 const Type *IntPtrTy = TD->getIntPtrType(Ptr->getContext());
Chris Lattner268e7d72008-05-08 04:54:43 +0000583
584 // If this is a constant expr gep that is effectively computing an
585 // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
586 for (unsigned i = 1; i != NumOps; ++i)
Chris Lattner8cd4efb2011-01-06 06:19:46 +0000587 if (!isa<ConstantInt>(Ops[i])) {
588
589 // If this is "gep i8* Ptr, (sub 0, V)", fold this as:
590 // "inttoptr (sub (ptrtoint Ptr), V)"
591 if (NumOps == 2 &&
592 cast<PointerType>(ResultTy)->getElementType()->isIntegerTy(8)) {
593 ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
Chris Lattner156eb0a2011-01-16 03:43:53 +0000594 assert((CE == 0 || CE->getType() == IntPtrTy) &&
Chris Lattnerf1cadf22011-01-06 22:24:29 +0000595 "CastGEPIndices didn't canonicalize index types!");
Chris Lattner8cd4efb2011-01-06 06:19:46 +0000596 if (CE && CE->getOpcode() == Instruction::Sub &&
Chris Lattnerf1cadf22011-01-06 22:24:29 +0000597 CE->getOperand(0)->isNullValue()) {
Chris Lattner8cd4efb2011-01-06 06:19:46 +0000598 Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
599 Res = ConstantExpr::getSub(Res, CE->getOperand(1));
600 Res = ConstantExpr::getIntToPtr(Res, ResultTy);
601 if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
602 Res = ConstantFoldConstantExpression(ResCE, TD);
603 return Res;
604 }
605 }
Dan Gohmande0e5872009-08-19 18:18:36 +0000606 return 0;
Chris Lattner8cd4efb2011-01-06 06:19:46 +0000607 }
Chris Lattner268e7d72008-05-08 04:54:43 +0000608
Chris Lattnerf1cadf22011-01-06 22:24:29 +0000609 unsigned BitWidth = TD->getTypeSizeInBits(IntPtrTy);
Dan Gohmancda97062009-08-21 16:52:54 +0000610 APInt Offset = APInt(BitWidth,
611 TD->getIndexedOffset(Ptr->getType(),
612 (Value**)Ops+1, NumOps-1));
Duncan Sands890edda2010-03-12 17:55:20 +0000613 Ptr = cast<Constant>(Ptr->stripPointerCasts());
Dan Gohman0891d752010-03-10 19:31:51 +0000614
615 // If this is a GEP of a GEP, fold it all into a single GEP.
616 while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
617 SmallVector<Value *, 4> NestedOps(GEP->op_begin()+1, GEP->op_end());
Duncan Sands890edda2010-03-12 17:55:20 +0000618
619 // Do not try the incorporate the sub-GEP if some index is not a number.
620 bool AllConstantInt = true;
621 for (unsigned i = 0, e = NestedOps.size(); i != e; ++i)
622 if (!isa<ConstantInt>(NestedOps[i])) {
623 AllConstantInt = false;
624 break;
625 }
626 if (!AllConstantInt)
627 break;
628
Dan Gohman0891d752010-03-10 19:31:51 +0000629 Ptr = cast<Constant>(GEP->getOperand(0));
630 Offset += APInt(BitWidth,
631 TD->getIndexedOffset(Ptr->getType(),
632 (Value**)NestedOps.data(),
633 NestedOps.size()));
Duncan Sands890edda2010-03-12 17:55:20 +0000634 Ptr = cast<Constant>(Ptr->stripPointerCasts());
Dan Gohman0891d752010-03-10 19:31:51 +0000635 }
636
Dan Gohmande0e5872009-08-19 18:18:36 +0000637 // If the base value for this address is a literal integer value, fold the
638 // getelementptr to the resulting integer value casted to the pointer type.
Dan Gohman40b037e2010-03-18 19:34:33 +0000639 APInt BasePtr(BitWidth, 0);
640 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
641 if (CE->getOpcode() == Instruction::IntToPtr)
Jay Foad40f8f622010-12-07 08:25:19 +0000642 if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
643 BasePtr = Base->getValue().zextOrTrunc(BitWidth);
Dan Gohman40b037e2010-03-18 19:34:33 +0000644 if (Ptr->isNullValue() || BasePtr != 0) {
Chris Lattner7b550cc2009-11-06 04:27:31 +0000645 Constant *C = ConstantInt::get(Ptr->getContext(), Offset+BasePtr);
Dan Gohmande0e5872009-08-19 18:18:36 +0000646 return ConstantExpr::getIntToPtr(C, ResultTy);
647 }
648
649 // Otherwise form a regular getelementptr. Recompute the indices so that
650 // we eliminate over-indexing of the notional static type array bounds.
651 // This makes it easy to determine if the getelementptr is "inbounds".
652 // Also, this helps GlobalOpt do SROA on GlobalVariables.
653 const Type *Ty = Ptr->getType();
654 SmallVector<Constant*, 32> NewIdxs;
Dan Gohman3d013342009-08-19 22:46:59 +0000655 do {
Dan Gohmande0e5872009-08-19 18:18:36 +0000656 if (const SequentialType *ATy = dyn_cast<SequentialType>(Ty)) {
Duncan Sands1df98592010-02-16 11:11:14 +0000657 if (ATy->isPointerTy()) {
Chris Lattnere568fa22009-12-03 01:05:45 +0000658 // The only pointer indexing we'll do is on the first index of the GEP.
659 if (!NewIdxs.empty())
660 break;
661
662 // Only handle pointers to sized types, not pointers to functions.
663 if (!ATy->getElementType()->isSized())
664 return 0;
665 }
666
Dan Gohmande0e5872009-08-19 18:18:36 +0000667 // Determine which element of the array the offset points into.
Dan Gohmancda97062009-08-21 16:52:54 +0000668 APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType()));
Chris Lattnerc1da2042010-11-21 08:39:01 +0000669 const IntegerType *IntPtrTy = TD->getIntPtrType(Ty->getContext());
Dan Gohmande0e5872009-08-19 18:18:36 +0000670 if (ElemSize == 0)
Chris Lattnerc1da2042010-11-21 08:39:01 +0000671 // The element size is 0. This may be [0 x Ty]*, so just use a zero
Duncan Sandse5839102010-11-21 12:43:13 +0000672 // index for this level and proceed to the next level to see if it can
673 // accommodate the offset.
Chris Lattnerc1da2042010-11-21 08:39:01 +0000674 NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
675 else {
676 // The element size is non-zero divide the offset by the element
677 // size (rounding down), to compute the index at this level.
678 APInt NewIdx = Offset.udiv(ElemSize);
679 Offset -= NewIdx * ElemSize;
680 NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
681 }
Dan Gohmande0e5872009-08-19 18:18:36 +0000682 Ty = ATy->getElementType();
683 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Dan Gohmancda97062009-08-21 16:52:54 +0000684 // Determine which field of the struct the offset points into. The
685 // getZExtValue is at least as safe as the StructLayout API because we
686 // know the offset is within the struct at this point.
Dan Gohmande0e5872009-08-19 18:18:36 +0000687 const StructLayout &SL = *TD->getStructLayout(STy);
Dan Gohmancda97062009-08-21 16:52:54 +0000688 unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue());
Chris Lattner7b550cc2009-11-06 04:27:31 +0000689 NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
690 ElIdx));
Dan Gohmancda97062009-08-21 16:52:54 +0000691 Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx));
Dan Gohmande0e5872009-08-19 18:18:36 +0000692 Ty = STy->getTypeAtIndex(ElIdx);
693 } else {
Dan Gohman3d013342009-08-19 22:46:59 +0000694 // We've reached some non-indexable type.
695 break;
Dan Gohmande0e5872009-08-19 18:18:36 +0000696 }
Dan Gohman3d013342009-08-19 22:46:59 +0000697 } while (Ty != cast<PointerType>(ResultTy)->getElementType());
698
699 // If we haven't used up the entire offset by descending the static
700 // type, then the offset is pointing into the middle of an indivisible
701 // member, so we can't simplify it.
702 if (Offset != 0)
703 return 0;
Dan Gohmande0e5872009-08-19 18:18:36 +0000704
Dan Gohman3bfbc452009-09-11 00:04:14 +0000705 // Create a GEP.
706 Constant *C =
Dan Gohman6e7ad952009-09-03 23:34:49 +0000707 ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size());
708 assert(cast<PointerType>(C->getType())->getElementType() == Ty &&
709 "Computed GetElementPtr has unexpected type!");
Dan Gohmande0e5872009-08-19 18:18:36 +0000710
Dan Gohman3d013342009-08-19 22:46:59 +0000711 // If we ended up indexing a member with a type that doesn't match
Dan Gohman4c0d5d52009-08-20 16:42:55 +0000712 // the type of what the original indices indexed, add a cast.
Dan Gohman3d013342009-08-19 22:46:59 +0000713 if (Ty != cast<PointerType>(ResultTy)->getElementType())
Chris Lattner6333c392009-10-25 06:08:26 +0000714 C = FoldBitCast(C, ResultTy, *TD);
Dan Gohman3d013342009-08-19 22:46:59 +0000715
716 return C;
Chris Lattner03dd25c2007-01-31 00:51:48 +0000717}
718
Chris Lattner1afab9c2007-12-11 07:29:44 +0000719
Chris Lattner03dd25c2007-01-31 00:51:48 +0000720
721//===----------------------------------------------------------------------===//
722// Constant Folding public APIs
723//===----------------------------------------------------------------------===//
724
Duncan Sandsb9b369f2010-11-23 10:16:18 +0000725/// ConstantFoldInstruction - Try to constant fold the specified instruction.
726/// If successful, the constant result is returned, if not, null is returned.
727/// Note that this fails if not all of the operands are constant. Otherwise,
728/// this function can only fail when attempting to fold instructions like loads
729/// and stores, which have no constant expression form.
Chris Lattner7b550cc2009-11-06 04:27:31 +0000730Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
Duncan Sandsb9b369f2010-11-23 10:16:18 +0000731 // Handle PHI nodes quickly here...
Chris Lattner55207322007-01-30 23:45:45 +0000732 if (PHINode *PN = dyn_cast<PHINode>(I)) {
Duncan Sandsc0362d52010-11-14 12:53:18 +0000733 Constant *CommonValue = 0;
John Criswellbd9d3702005-10-27 16:00:10 +0000734
Duncan Sandsc0362d52010-11-14 12:53:18 +0000735 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
736 Value *Incoming = PN->getIncomingValue(i);
Duncan Sandsb9b369f2010-11-23 10:16:18 +0000737 // If the incoming value is undef then skip it. Note that while we could
738 // skip the value if it is equal to the phi node itself we choose not to
739 // because that would break the rule that constant folding only applies if
740 // all operands are constants.
741 if (isa<UndefValue>(Incoming))
Duncan Sandsc0362d52010-11-14 12:53:18 +0000742 continue;
743 // If the incoming value is not a constant, or is a different constant to
744 // the one we saw previously, then give up.
745 Constant *C = dyn_cast<Constant>(Incoming);
746 if (!C || (CommonValue && C != CommonValue))
747 return 0;
748 CommonValue = C;
749 }
Chris Lattner55207322007-01-30 23:45:45 +0000750
Duncan Sandsc0362d52010-11-14 12:53:18 +0000751 // If we reach here, all incoming values are the same constant or undef.
752 return CommonValue ? CommonValue : UndefValue::get(PN->getType());
Chris Lattner55207322007-01-30 23:45:45 +0000753 }
754
755 // Scan the operand list, checking to see if they are all constants, if so,
756 // hand off to ConstantFoldInstOperands.
757 SmallVector<Constant*, 8> Ops;
Gabor Greifde2d74b2008-05-22 06:43:33 +0000758 for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
759 if (Constant *Op = dyn_cast<Constant>(*i))
Chris Lattner55207322007-01-30 23:45:45 +0000760 Ops.push_back(Op);
761 else
762 return 0; // All operands not constant!
763
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000764 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
Chris Lattner8f73dea2009-11-09 23:06:58 +0000765 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
766 TD);
Chris Lattner58665d42009-09-16 00:08:07 +0000767
Chris Lattner878e4942009-10-22 06:25:11 +0000768 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
769 return ConstantFoldLoadInst(LI, TD);
Frits van Bommel3ee0af32010-11-29 20:36:52 +0000770
771 if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I))
772 return ConstantExpr::getInsertValue(
773 cast<Constant>(IVI->getAggregateOperand()),
774 cast<Constant>(IVI->getInsertedValueOperand()),
775 IVI->idx_begin(), IVI->getNumIndices());
776
777 if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I))
778 return ConstantExpr::getExtractValue(
779 cast<Constant>(EVI->getAggregateOperand()),
780 EVI->idx_begin(), EVI->getNumIndices());
781
Chris Lattner58665d42009-09-16 00:08:07 +0000782 return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
Chris Lattner7b550cc2009-11-06 04:27:31 +0000783 Ops.data(), Ops.size(), TD);
Chris Lattner55207322007-01-30 23:45:45 +0000784}
785
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000786/// ConstantFoldConstantExpression - Attempt to fold the constant expression
787/// using the specified TargetData. If successful, the constant result is
788/// result is returned, if not, null is returned.
Dan Gohmanbaf0c672010-02-08 22:00:06 +0000789Constant *llvm::ConstantFoldConstantExpression(const ConstantExpr *CE,
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000790 const TargetData *TD) {
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000791 SmallVector<Constant*, 8> Ops;
Chris Lattnerc1da2042010-11-21 08:39:01 +0000792 for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end();
793 i != e; ++i) {
Dan Gohman01b97dd2009-11-23 16:22:21 +0000794 Constant *NewC = cast<Constant>(*i);
795 // Recursively fold the ConstantExpr's operands.
796 if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC))
797 NewC = ConstantFoldConstantExpression(NewCE, TD);
798 Ops.push_back(NewC);
799 }
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000800
801 if (CE->isCompare())
Chris Lattner8f73dea2009-11-09 23:06:58 +0000802 return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
803 TD);
Chris Lattner58665d42009-09-16 00:08:07 +0000804 return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(),
Chris Lattner7b550cc2009-11-06 04:27:31 +0000805 Ops.data(), Ops.size(), TD);
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000806}
807
Chris Lattner55207322007-01-30 23:45:45 +0000808/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
809/// specified opcode and operands. If successful, the constant result is
810/// returned, if not, null is returned. Note that this function can fail when
811/// attempting to fold instructions like loads and stores, which have no
812/// constant expression form.
813///
Dan Gohman01b97dd2009-11-23 16:22:21 +0000814/// TODO: This function neither utilizes nor preserves nsw/nuw/inbounds/etc
815/// information, due to only being passed an opcode and operands. Constant
816/// folding using this function strips this information.
817///
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000818Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
819 Constant* const* Ops, unsigned NumOps,
Chris Lattner55207322007-01-30 23:45:45 +0000820 const TargetData *TD) {
Chris Lattner03dd25c2007-01-31 00:51:48 +0000821 // Handle easy binops first.
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000822 if (Instruction::isBinaryOp(Opcode)) {
Chris Lattner03dd25c2007-01-31 00:51:48 +0000823 if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1]))
Chris Lattner7b550cc2009-11-06 04:27:31 +0000824 if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD))
Chris Lattner03dd25c2007-01-31 00:51:48 +0000825 return C;
826
Owen Andersonbaf3c402009-07-29 18:55:55 +0000827 return ConstantExpr::get(Opcode, Ops[0], Ops[1]);
Chris Lattner03dd25c2007-01-31 00:51:48 +0000828 }
Chris Lattner55207322007-01-30 23:45:45 +0000829
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000830 switch (Opcode) {
Chris Lattner55207322007-01-30 23:45:45 +0000831 default: return 0;
Chris Lattner79fa3cf2010-01-02 01:22:23 +0000832 case Instruction::ICmp:
833 case Instruction::FCmp: assert(0 && "Invalid for compares");
Chris Lattner55207322007-01-30 23:45:45 +0000834 case Instruction::Call:
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000835 if (Function *F = dyn_cast<Function>(Ops[NumOps - 1]))
Chris Lattner55207322007-01-30 23:45:45 +0000836 if (canConstantFoldCallTo(F))
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000837 return ConstantFoldCall(F, Ops, NumOps - 1);
Chris Lattner55207322007-01-30 23:45:45 +0000838 return 0;
Chris Lattner001f7532007-08-11 23:49:01 +0000839 case Instruction::PtrToInt:
840 // If the input is a inttoptr, eliminate the pair. This requires knowing
841 // the width of a pointer, so it can't be done in ConstantExpr::getCast.
842 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) {
843 if (TD && CE->getOpcode() == Instruction::IntToPtr) {
844 Constant *Input = CE->getOperand(0);
Dan Gohman6de29f82009-06-15 22:12:54 +0000845 unsigned InWidth = Input->getType()->getScalarSizeInBits();
Nick Lewycky04aa2c32008-10-24 06:14:27 +0000846 if (TD->getPointerSizeInBits() < InWidth) {
847 Constant *Mask =
Chris Lattner7b550cc2009-11-06 04:27:31 +0000848 ConstantInt::get(CE->getContext(), APInt::getLowBitsSet(InWidth,
Nick Lewycky04aa2c32008-10-24 06:14:27 +0000849 TD->getPointerSizeInBits()));
Owen Andersonbaf3c402009-07-29 18:55:55 +0000850 Input = ConstantExpr::getAnd(Input, Mask);
Nick Lewycky04aa2c32008-10-24 06:14:27 +0000851 }
Chris Lattner001f7532007-08-11 23:49:01 +0000852 // Do a zext or trunc to get to the dest size.
Owen Andersonbaf3c402009-07-29 18:55:55 +0000853 return ConstantExpr::getIntegerCast(Input, DestTy, false);
Chris Lattner001f7532007-08-11 23:49:01 +0000854 }
855 }
Owen Andersonbaf3c402009-07-29 18:55:55 +0000856 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
Chris Lattner001f7532007-08-11 23:49:01 +0000857 case Instruction::IntToPtr:
Duncan Sands81b06be2008-08-13 20:20:35 +0000858 // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if
859 // the int size is >= the ptr size. This requires knowing the width of a
860 // pointer, so it can't be done in ConstantExpr::getCast.
Dan Gohmanb80a2a62010-02-23 16:35:41 +0000861 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))
Dan Gohman9a38e3e2009-05-07 19:46:24 +0000862 if (TD &&
Dan Gohmanb80a2a62010-02-23 16:35:41 +0000863 TD->getPointerSizeInBits() <= CE->getType()->getScalarSizeInBits() &&
864 CE->getOpcode() == Instruction::PtrToInt)
865 return FoldBitCast(CE->getOperand(0), DestTy, *TD);
866
Owen Andersonbaf3c402009-07-29 18:55:55 +0000867 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
Chris Lattner55207322007-01-30 23:45:45 +0000868 case Instruction::Trunc:
869 case Instruction::ZExt:
870 case Instruction::SExt:
871 case Instruction::FPTrunc:
872 case Instruction::FPExt:
873 case Instruction::UIToFP:
874 case Instruction::SIToFP:
875 case Instruction::FPToUI:
876 case Instruction::FPToSI:
Owen Andersonbaf3c402009-07-29 18:55:55 +0000877 return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
Chris Lattner55207322007-01-30 23:45:45 +0000878 case Instruction::BitCast:
Chris Lattner1afab9c2007-12-11 07:29:44 +0000879 if (TD)
Chris Lattner6333c392009-10-25 06:08:26 +0000880 return FoldBitCast(Ops[0], DestTy, *TD);
Owen Andersonbaf3c402009-07-29 18:55:55 +0000881 return ConstantExpr::getBitCast(Ops[0], DestTy);
Chris Lattner55207322007-01-30 23:45:45 +0000882 case Instruction::Select:
Owen Andersonbaf3c402009-07-29 18:55:55 +0000883 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
Chris Lattner55207322007-01-30 23:45:45 +0000884 case Instruction::ExtractElement:
Owen Andersonbaf3c402009-07-29 18:55:55 +0000885 return ConstantExpr::getExtractElement(Ops[0], Ops[1]);
Chris Lattner55207322007-01-30 23:45:45 +0000886 case Instruction::InsertElement:
Owen Andersonbaf3c402009-07-29 18:55:55 +0000887 return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
Chris Lattner55207322007-01-30 23:45:45 +0000888 case Instruction::ShuffleVector:
Owen Andersonbaf3c402009-07-29 18:55:55 +0000889 return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
Chris Lattner55207322007-01-30 23:45:45 +0000890 case Instruction::GetElementPtr:
Dan Gohman4f8eea82010-02-01 18:27:38 +0000891 if (Constant *C = CastGEPIndices(Ops, NumOps, DestTy, TD))
892 return C;
Chris Lattner7b550cc2009-11-06 04:27:31 +0000893 if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, TD))
Chris Lattner03dd25c2007-01-31 00:51:48 +0000894 return C;
895
Owen Andersonbaf3c402009-07-29 18:55:55 +0000896 return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1);
Chris Lattner55207322007-01-30 23:45:45 +0000897 }
898}
899
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000900/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare
901/// instruction (icmp/fcmp) with the specified operands. If it fails, it
902/// returns a constant expression of the specified operands.
903///
904Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
Chris Lattner8f73dea2009-11-09 23:06:58 +0000905 Constant *Ops0, Constant *Ops1,
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000906 const TargetData *TD) {
907 // fold: icmp (inttoptr x), null -> icmp x, 0
908 // fold: icmp (ptrtoint x), 0 -> icmp x, null
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000909 // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000910 // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y
911 //
912 // ConstantExpr::getCompare cannot do this, because it doesn't have TD
913 // around to know if bit truncation is happening.
Chris Lattner8f73dea2009-11-09 23:06:58 +0000914 if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
915 if (TD && Ops1->isNullValue()) {
Chris Lattner7b550cc2009-11-06 04:27:31 +0000916 const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000917 if (CE0->getOpcode() == Instruction::IntToPtr) {
918 // Convert the integer value to the right size to ensure we get the
919 // proper extension or truncation.
Owen Andersonbaf3c402009-07-29 18:55:55 +0000920 Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000921 IntPtrTy, false);
Chris Lattner8f73dea2009-11-09 23:06:58 +0000922 Constant *Null = Constant::getNullValue(C->getType());
923 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD);
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000924 }
925
926 // Only do this transformation if the int is intptrty in size, otherwise
927 // there is a truncation or extension that we aren't modeling.
928 if (CE0->getOpcode() == Instruction::PtrToInt &&
929 CE0->getType() == IntPtrTy) {
930 Constant *C = CE0->getOperand(0);
Chris Lattner8f73dea2009-11-09 23:06:58 +0000931 Constant *Null = Constant::getNullValue(C->getType());
932 return ConstantFoldCompareInstOperands(Predicate, C, Null, TD);
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000933 }
934 }
935
Chris Lattner8f73dea2009-11-09 23:06:58 +0000936 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000937 if (TD && CE0->getOpcode() == CE1->getOpcode()) {
Chris Lattner7b550cc2009-11-06 04:27:31 +0000938 const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000939
940 if (CE0->getOpcode() == Instruction::IntToPtr) {
941 // Convert the integer value to the right size to ensure we get the
942 // proper extension or truncation.
Owen Andersonbaf3c402009-07-29 18:55:55 +0000943 Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0),
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000944 IntPtrTy, false);
Owen Andersonbaf3c402009-07-29 18:55:55 +0000945 Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000946 IntPtrTy, false);
Chris Lattner8f73dea2009-11-09 23:06:58 +0000947 return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD);
Nick Lewycky3dfd7bf2008-05-25 20:56:15 +0000948 }
949
950 // Only do this transformation if the int is intptrty in size, otherwise
951 // there is a truncation or extension that we aren't modeling.
952 if ((CE0->getOpcode() == Instruction::PtrToInt &&
953 CE0->getType() == IntPtrTy &&
Chris Lattner8f73dea2009-11-09 23:06:58 +0000954 CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()))
955 return ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0),
956 CE1->getOperand(0), TD);
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000957 }
958 }
Chris Lattner79fa3cf2010-01-02 01:22:23 +0000959
960 // icmp eq (or x, y), 0 -> (icmp eq x, 0) & (icmp eq y, 0)
961 // icmp ne (or x, y), 0 -> (icmp ne x, 0) | (icmp ne y, 0)
962 if ((Predicate == ICmpInst::ICMP_EQ || Predicate == ICmpInst::ICMP_NE) &&
963 CE0->getOpcode() == Instruction::Or && Ops1->isNullValue()) {
964 Constant *LHS =
965 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0), Ops1,TD);
966 Constant *RHS =
967 ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(1), Ops1,TD);
968 unsigned OpC =
969 Predicate == ICmpInst::ICMP_EQ ? Instruction::And : Instruction::Or;
970 Constant *Ops[] = { LHS, RHS };
971 return ConstantFoldInstOperands(OpC, LHS->getType(), Ops, 2, TD);
972 }
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000973 }
Chris Lattner8f73dea2009-11-09 23:06:58 +0000974
975 return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
Chris Lattnerf286f6f2007-12-10 22:53:04 +0000976}
977
978
Chris Lattner55207322007-01-30 23:45:45 +0000979/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
980/// getelementptr constantexpr, return the constant value being addressed by the
981/// constant expression, or null if something is funny and we can't decide.
982Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C,
Dan Gohmanc6f69e92009-10-05 16:36:26 +0000983 ConstantExpr *CE) {
Owen Andersona7235ea2009-07-31 20:28:14 +0000984 if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType()))
Chris Lattner55207322007-01-30 23:45:45 +0000985 return 0; // Do not allow stepping over the value!
986
987 // Loop over all of the operands, tracking down which value we are
988 // addressing...
989 gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
990 for (++I; I != E; ++I)
991 if (const StructType *STy = dyn_cast<StructType>(*I)) {
992 ConstantInt *CU = cast<ConstantInt>(I.getOperand());
993 assert(CU->getZExtValue() < STy->getNumElements() &&
994 "Struct index out of range!");
995 unsigned El = (unsigned)CU->getZExtValue();
996 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
997 C = CS->getOperand(El);
998 } else if (isa<ConstantAggregateZero>(C)) {
Owen Andersona7235ea2009-07-31 20:28:14 +0000999 C = Constant::getNullValue(STy->getElementType(El));
Chris Lattner55207322007-01-30 23:45:45 +00001000 } else if (isa<UndefValue>(C)) {
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001001 C = UndefValue::get(STy->getElementType(El));
Chris Lattner55207322007-01-30 23:45:45 +00001002 } else {
1003 return 0;
1004 }
1005 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) {
1006 if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) {
1007 if (CI->getZExtValue() >= ATy->getNumElements())
1008 return 0;
1009 if (ConstantArray *CA = dyn_cast<ConstantArray>(C))
1010 C = CA->getOperand(CI->getZExtValue());
1011 else if (isa<ConstantAggregateZero>(C))
Owen Andersona7235ea2009-07-31 20:28:14 +00001012 C = Constant::getNullValue(ATy->getElementType());
Chris Lattner55207322007-01-30 23:45:45 +00001013 else if (isa<UndefValue>(C))
Owen Anderson9e9a0d52009-07-30 23:03:37 +00001014 C = UndefValue::get(ATy->getElementType());
Chris Lattner55207322007-01-30 23:45:45 +00001015 else
1016 return 0;
Chris Lattner62d327e2009-10-22 06:38:35 +00001017 } else if (const VectorType *VTy = dyn_cast<VectorType>(*I)) {
1018 if (CI->getZExtValue() >= VTy->getNumElements())
Chris Lattner55207322007-01-30 23:45:45 +00001019 return 0;
Reid Spencer9d6565a2007-02-15 02:26:10 +00001020 if (ConstantVector *CP = dyn_cast<ConstantVector>(C))
Chris Lattner55207322007-01-30 23:45:45 +00001021 C = CP->getOperand(CI->getZExtValue());
1022 else if (isa<ConstantAggregateZero>(C))
Chris Lattner62d327e2009-10-22 06:38:35 +00001023 C = Constant::getNullValue(VTy->getElementType());
Chris Lattner55207322007-01-30 23:45:45 +00001024 else if (isa<UndefValue>(C))
Chris Lattner62d327e2009-10-22 06:38:35 +00001025 C = UndefValue::get(VTy->getElementType());
Chris Lattner55207322007-01-30 23:45:45 +00001026 else
1027 return 0;
1028 } else {
1029 return 0;
1030 }
1031 } else {
1032 return 0;
1033 }
1034 return C;
1035}
1036
1037
1038//===----------------------------------------------------------------------===//
1039// Constant Folding for Calls
1040//
John Criswellbd9d3702005-10-27 16:00:10 +00001041
1042/// canConstantFoldCallTo - Return true if its even possible to fold a call to
1043/// the specified function.
1044bool
Dan Gohmanfa9b80e2008-01-31 01:05:10 +00001045llvm::canConstantFoldCallTo(const Function *F) {
John Criswellbd9d3702005-10-27 16:00:10 +00001046 switch (F->getIntrinsicID()) {
Dale Johannesen9ab7fb32007-10-02 17:43:59 +00001047 case Intrinsic::sqrt:
1048 case Intrinsic::powi:
Reid Spencere9391fd2007-04-01 07:35:23 +00001049 case Intrinsic::bswap:
1050 case Intrinsic::ctpop:
1051 case Intrinsic::ctlz:
1052 case Intrinsic::cttz:
Evan Phoenix1614e502009-10-05 22:53:52 +00001053 case Intrinsic::sadd_with_overflow:
Frits van Bommel62086102011-03-27 14:26:13 +00001054 case Intrinsic::uadd_with_overflow:
Evan Phoenix1614e502009-10-05 22:53:52 +00001055 case Intrinsic::ssub_with_overflow:
Frits van Bommel62086102011-03-27 14:26:13 +00001056 case Intrinsic::usub_with_overflow:
Chris Lattnereafc5cb2010-10-14 00:05:07 +00001057 case Intrinsic::smul_with_overflow:
Frits van Bommel62086102011-03-27 14:26:13 +00001058 case Intrinsic::umul_with_overflow:
Anton Korobeynikovfaa75f6e2010-03-19 00:36:35 +00001059 case Intrinsic::convert_from_fp16:
1060 case Intrinsic::convert_to_fp16:
Chandler Carruth15ed90c2011-01-11 01:07:24 +00001061 case Intrinsic::x86_sse_cvtss2si:
1062 case Intrinsic::x86_sse_cvtss2si64:
1063 case Intrinsic::x86_sse_cvttss2si:
1064 case Intrinsic::x86_sse_cvttss2si64:
1065 case Intrinsic::x86_sse2_cvtsd2si:
1066 case Intrinsic::x86_sse2_cvtsd2si64:
1067 case Intrinsic::x86_sse2_cvttsd2si:
1068 case Intrinsic::x86_sse2_cvttsd2si64:
John Criswellbd9d3702005-10-27 16:00:10 +00001069 return true;
Chris Lattner68a06032009-10-05 05:00:35 +00001070 default:
1071 return false;
1072 case 0: break;
John Criswellbd9d3702005-10-27 16:00:10 +00001073 }
1074
Chris Lattner6f532a92009-04-03 00:02:39 +00001075 if (!F->hasName()) return false;
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001076 StringRef Name = F->getName();
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001077
1078 // In these cases, the check of the length is required. We don't want to
1079 // return true for a name like "cos\0blah" which strcmp would return equal to
1080 // "cos", but has length 8.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001081 switch (Name[0]) {
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001082 default: return false;
1083 case 'a':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001084 return Name == "acos" || Name == "asin" ||
1085 Name == "atan" || Name == "atan2";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001086 case 'c':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001087 return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001088 case 'e':
Chris Lattner805fa972011-05-22 22:22:35 +00001089 return Name == "exp" || Name == "exp2";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001090 case 'f':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001091 return Name == "fabs" || Name == "fmod" || Name == "floor";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001092 case 'l':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001093 return Name == "log" || Name == "log10";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001094 case 'p':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001095 return Name == "pow";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001096 case 's':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001097 return Name == "sin" || Name == "sinh" || Name == "sqrt" ||
1098 Name == "sinf" || Name == "sqrtf";
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001099 case 't':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001100 return Name == "tan" || Name == "tanh";
John Criswellbd9d3702005-10-27 16:00:10 +00001101 }
1102}
1103
Chris Lattner72d88ae2007-01-30 23:15:43 +00001104static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
Chris Lattner7b550cc2009-11-06 04:27:31 +00001105 const Type *Ty) {
Dan Gohman3f2f21e2010-09-17 20:06:27 +00001106 sys::llvm_fenv_clearexcept();
John Criswellbd9d3702005-10-27 16:00:10 +00001107 V = NativeFP(V);
Dan Gohman3f2f21e2010-09-17 20:06:27 +00001108 if (sys::llvm_fenv_testexcept()) {
1109 sys::llvm_fenv_clearexcept();
Chris Lattnerf19f58a2008-03-30 18:02:00 +00001110 return 0;
Dale Johannesen43421b32007-09-06 18:13:44 +00001111 }
Chris Lattnerf19f58a2008-03-30 18:02:00 +00001112
Chris Lattnerd0806a12009-10-05 05:06:24 +00001113 if (Ty->isFloatTy())
Chris Lattner7b550cc2009-11-06 04:27:31 +00001114 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
Chris Lattnerd0806a12009-10-05 05:06:24 +00001115 if (Ty->isDoubleTy())
Chris Lattner7b550cc2009-11-06 04:27:31 +00001116 return ConstantFP::get(Ty->getContext(), APFloat(V));
Torok Edwinc23197a2009-07-14 16:55:14 +00001117 llvm_unreachable("Can only constant fold float/double");
Gabor Greif33e456d2008-05-21 14:07:30 +00001118 return 0; // dummy return to suppress warning
John Criswellbd9d3702005-10-27 16:00:10 +00001119}
1120
Dan Gohman38415242007-07-16 15:26:22 +00001121static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
Chris Lattner7b550cc2009-11-06 04:27:31 +00001122 double V, double W, const Type *Ty) {
Dan Gohman3f2f21e2010-09-17 20:06:27 +00001123 sys::llvm_fenv_clearexcept();
Dan Gohman38415242007-07-16 15:26:22 +00001124 V = NativeFP(V, W);
Dan Gohman3f2f21e2010-09-17 20:06:27 +00001125 if (sys::llvm_fenv_testexcept()) {
1126 sys::llvm_fenv_clearexcept();
Chris Lattnerf19f58a2008-03-30 18:02:00 +00001127 return 0;
Dale Johannesen43421b32007-09-06 18:13:44 +00001128 }
Chris Lattnerf19f58a2008-03-30 18:02:00 +00001129
Chris Lattnerd0806a12009-10-05 05:06:24 +00001130 if (Ty->isFloatTy())
Chris Lattner7b550cc2009-11-06 04:27:31 +00001131 return ConstantFP::get(Ty->getContext(), APFloat((float)V));
Chris Lattnerd0806a12009-10-05 05:06:24 +00001132 if (Ty->isDoubleTy())
Chris Lattner7b550cc2009-11-06 04:27:31 +00001133 return ConstantFP::get(Ty->getContext(), APFloat(V));
Torok Edwinc23197a2009-07-14 16:55:14 +00001134 llvm_unreachable("Can only constant fold float/double");
Gabor Greif33e456d2008-05-21 14:07:30 +00001135 return 0; // dummy return to suppress warning
Dan Gohman38415242007-07-16 15:26:22 +00001136}
1137
Chandler Carruth15ed90c2011-01-11 01:07:24 +00001138/// ConstantFoldConvertToInt - Attempt to an SSE floating point to integer
1139/// conversion of a constant floating point. If roundTowardZero is false, the
1140/// default IEEE rounding is used (toward nearest, ties to even). This matches
1141/// the behavior of the non-truncating SSE instructions in the default rounding
1142/// mode. The desired integer type Ty is used to select how many bits are
1143/// available for the result. Returns null if the conversion cannot be
1144/// performed, otherwise returns the Constant value resulting from the
1145/// conversion.
1146static Constant *ConstantFoldConvertToInt(ConstantFP *Op, bool roundTowardZero,
1147 const Type *Ty) {
1148 assert(Op && "Called with NULL operand");
1149 APFloat Val(Op->getValueAPF());
1150
1151 // All of these conversion intrinsics form an integer of at most 64bits.
1152 unsigned ResultWidth = cast<IntegerType>(Ty)->getBitWidth();
1153 assert(ResultWidth <= 64 &&
1154 "Can only constant fold conversions to 64 and 32 bit ints");
1155
1156 uint64_t UIntVal;
1157 bool isExact = false;
1158 APFloat::roundingMode mode = roundTowardZero? APFloat::rmTowardZero
1159 : APFloat::rmNearestTiesToEven;
1160 APFloat::opStatus status = Val.convertToInteger(&UIntVal, ResultWidth,
1161 /*isSigned=*/true, mode,
1162 &isExact);
1163 if (status != APFloat::opOK && status != APFloat::opInexact)
1164 return 0;
1165 return ConstantInt::get(Ty, UIntVal, /*isSigned=*/true);
1166}
1167
John Criswellbd9d3702005-10-27 16:00:10 +00001168/// ConstantFoldCall - Attempt to constant fold a call to the specified function
1169/// with the specified arguments, returning null if unsuccessful.
1170Constant *
Chris Lattnerf286f6f2007-12-10 22:53:04 +00001171llvm::ConstantFoldCall(Function *F,
Chris Lattner68a06032009-10-05 05:00:35 +00001172 Constant *const *Operands, unsigned NumOperands) {
Chris Lattner6f532a92009-04-03 00:02:39 +00001173 if (!F->hasName()) return 0;
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001174 StringRef Name = F->getName();
Chris Lattnere65cd402009-10-05 05:26:04 +00001175
John Criswellbd9d3702005-10-27 16:00:10 +00001176 const Type *Ty = F->getReturnType();
Chris Lattner72d88ae2007-01-30 23:15:43 +00001177 if (NumOperands == 1) {
John Criswellbd9d3702005-10-27 16:00:10 +00001178 if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
Chandler Carruthf4db8772011-01-10 09:02:58 +00001179 if (F->getIntrinsicID() == Intrinsic::convert_to_fp16) {
Anton Korobeynikovfaa75f6e2010-03-19 00:36:35 +00001180 APFloat Val(Op->getValueAPF());
1181
1182 bool lost = false;
1183 Val.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven, &lost);
1184
1185 return ConstantInt::get(F->getContext(), Val.bitcastToAPInt());
1186 }
1187
Chris Lattnerd0806a12009-10-05 05:06:24 +00001188 if (!Ty->isFloatTy() && !Ty->isDoubleTy())
Dale Johannesen43421b32007-09-06 18:13:44 +00001189 return 0;
Jakob Stoklund Olesen1f386c42010-09-27 21:29:20 +00001190
1191 /// We only fold functions with finite arguments. Folding NaN and inf is
1192 /// likely to be aborted with an exception anyway, and some host libms
1193 /// have known errors raising exceptions.
1194 if (Op->getValueAPF().isNaN() || Op->getValueAPF().isInfinity())
1195 return 0;
1196
Dale Johannesen43421b32007-09-06 18:13:44 +00001197 /// Currently APFloat versions of these functions do not exist, so we use
1198 /// the host native double versions. Float versions are not called
1199 /// directly but for all these it is true (float)(f((double)arg)) ==
1200 /// f(arg). Long double not supported yet.
Chris Lattnerd0806a12009-10-05 05:06:24 +00001201 double V = Ty->isFloatTy() ? (double)Op->getValueAPF().convertToFloat() :
Dale Johannesen43421b32007-09-06 18:13:44 +00001202 Op->getValueAPF().convertToDouble();
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001203 switch (Name[0]) {
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001204 case 'a':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001205 if (Name == "acos")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001206 return ConstantFoldFP(acos, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001207 else if (Name == "asin")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001208 return ConstantFoldFP(asin, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001209 else if (Name == "atan")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001210 return ConstantFoldFP(atan, V, Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001211 break;
1212 case 'c':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001213 if (Name == "ceil")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001214 return ConstantFoldFP(ceil, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001215 else if (Name == "cos")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001216 return ConstantFoldFP(cos, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001217 else if (Name == "cosh")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001218 return ConstantFoldFP(cosh, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001219 else if (Name == "cosf")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001220 return ConstantFoldFP(cos, V, Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001221 break;
1222 case 'e':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001223 if (Name == "exp")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001224 return ConstantFoldFP(exp, V, Ty);
Chris Lattner805fa972011-05-22 22:22:35 +00001225
1226 if (Name == "exp2") {
1227 // Constant fold exp2(x) as pow(2,x) in case the host doesn't have a
1228 // C99 library.
1229 return ConstantFoldBinaryFP(pow, 2.0, V, Ty);
1230 }
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001231 break;
1232 case 'f':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001233 if (Name == "fabs")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001234 return ConstantFoldFP(fabs, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001235 else if (Name == "floor")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001236 return ConstantFoldFP(floor, V, Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001237 break;
1238 case 'l':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001239 if (Name == "log" && V > 0)
Chris Lattner7b550cc2009-11-06 04:27:31 +00001240 return ConstantFoldFP(log, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001241 else if (Name == "log10" && V > 0)
Chris Lattner7b550cc2009-11-06 04:27:31 +00001242 return ConstantFoldFP(log10, V, Ty);
Chandler Carruthf4db8772011-01-10 09:02:58 +00001243 else if (F->getIntrinsicID() == Intrinsic::sqrt &&
1244 (Ty->isFloatTy() || Ty->isDoubleTy())) {
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001245 if (V >= -0.0)
Chris Lattner7b550cc2009-11-06 04:27:31 +00001246 return ConstantFoldFP(sqrt, V, Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001247 else // Undefined
Owen Andersona7235ea2009-07-31 20:28:14 +00001248 return Constant::getNullValue(Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001249 }
1250 break;
1251 case 's':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001252 if (Name == "sin")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001253 return ConstantFoldFP(sin, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001254 else if (Name == "sinh")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001255 return ConstantFoldFP(sinh, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001256 else if (Name == "sqrt" && V >= 0)
Chris Lattner7b550cc2009-11-06 04:27:31 +00001257 return ConstantFoldFP(sqrt, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001258 else if (Name == "sqrtf" && V >= 0)
Chris Lattner7b550cc2009-11-06 04:27:31 +00001259 return ConstantFoldFP(sqrt, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001260 else if (Name == "sinf")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001261 return ConstantFoldFP(sin, V, Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001262 break;
1263 case 't':
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001264 if (Name == "tan")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001265 return ConstantFoldFP(tan, V, Ty);
Daniel Dunbarf0443c12009-07-26 08:34:35 +00001266 else if (Name == "tanh")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001267 return ConstantFoldFP(tanh, V, Ty);
Chris Lattnerc5f6a1f2007-08-08 06:55:43 +00001268 break;
1269 default:
1270 break;
John Criswellbd9d3702005-10-27 16:00:10 +00001271 }
Chris Lattner68a06032009-10-05 05:00:35 +00001272 return 0;
1273 }
Chandler Carruthf4db8772011-01-10 09:02:58 +00001274
Chris Lattner68a06032009-10-05 05:00:35 +00001275 if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
Chandler Carruthf4db8772011-01-10 09:02:58 +00001276 switch (F->getIntrinsicID()) {
1277 case Intrinsic::bswap:
Chris Lattner7b550cc2009-11-06 04:27:31 +00001278 return ConstantInt::get(F->getContext(), Op->getValue().byteSwap());
Chandler Carruthf4db8772011-01-10 09:02:58 +00001279 case Intrinsic::ctpop:
Owen Andersoneed707b2009-07-24 23:12:02 +00001280 return ConstantInt::get(Ty, Op->getValue().countPopulation());
Chandler Carruthf4db8772011-01-10 09:02:58 +00001281 case Intrinsic::cttz:
Owen Andersoneed707b2009-07-24 23:12:02 +00001282 return ConstantInt::get(Ty, Op->getValue().countTrailingZeros());
Chandler Carruthf4db8772011-01-10 09:02:58 +00001283 case Intrinsic::ctlz:
Owen Andersoneed707b2009-07-24 23:12:02 +00001284 return ConstantInt::get(Ty, Op->getValue().countLeadingZeros());
Chandler Carruthf4db8772011-01-10 09:02:58 +00001285 case Intrinsic::convert_from_fp16: {
Anton Korobeynikovfaa75f6e2010-03-19 00:36:35 +00001286 APFloat Val(Op->getValue());
1287
1288 bool lost = false;
1289 APFloat::opStatus status =
1290 Val.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven, &lost);
1291
1292 // Conversion is always precise.
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +00001293 (void)status;
Anton Korobeynikovfaa75f6e2010-03-19 00:36:35 +00001294 assert(status == APFloat::opOK && !lost &&
1295 "Precision lost during fp16 constfolding");
1296
1297 return ConstantFP::get(F->getContext(), Val);
1298 }
Chandler Carruthf4db8772011-01-10 09:02:58 +00001299 default:
1300 return 0;
1301 }
John Criswellbd9d3702005-10-27 16:00:10 +00001302 }
Chandler Carruthf4db8772011-01-10 09:02:58 +00001303
Chandler Carruth15ed90c2011-01-11 01:07:24 +00001304 if (ConstantVector *Op = dyn_cast<ConstantVector>(Operands[0])) {
1305 switch (F->getIntrinsicID()) {
1306 default: break;
1307 case Intrinsic::x86_sse_cvtss2si:
1308 case Intrinsic::x86_sse_cvtss2si64:
1309 case Intrinsic::x86_sse2_cvtsd2si:
1310 case Intrinsic::x86_sse2_cvtsd2si64:
1311 if (ConstantFP *FPOp = dyn_cast<ConstantFP>(Op->getOperand(0)))
1312 return ConstantFoldConvertToInt(FPOp, /*roundTowardZero=*/false, Ty);
1313 case Intrinsic::x86_sse_cvttss2si:
1314 case Intrinsic::x86_sse_cvttss2si64:
1315 case Intrinsic::x86_sse2_cvttsd2si:
1316 case Intrinsic::x86_sse2_cvttsd2si64:
1317 if (ConstantFP *FPOp = dyn_cast<ConstantFP>(Op->getOperand(0)))
1318 return ConstantFoldConvertToInt(FPOp, /*roundTowardZero=*/true, Ty);
1319 }
1320 }
1321
Dan Gohman9ee71232010-02-17 00:54:58 +00001322 if (isa<UndefValue>(Operands[0])) {
Chandler Carruthf4db8772011-01-10 09:02:58 +00001323 if (F->getIntrinsicID() == Intrinsic::bswap)
Dan Gohman9ee71232010-02-17 00:54:58 +00001324 return Operands[0];
1325 return 0;
1326 }
1327
Chris Lattner68a06032009-10-05 05:00:35 +00001328 return 0;
1329 }
Chandler Carruthf4db8772011-01-10 09:02:58 +00001330
Chris Lattner68a06032009-10-05 05:00:35 +00001331 if (NumOperands == 2) {
John Criswellbd9d3702005-10-27 16:00:10 +00001332 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
Chris Lattnerd0806a12009-10-05 05:06:24 +00001333 if (!Ty->isFloatTy() && !Ty->isDoubleTy())
Dale Johannesen9ab7fb32007-10-02 17:43:59 +00001334 return 0;
Chris Lattnerd0806a12009-10-05 05:06:24 +00001335 double Op1V = Ty->isFloatTy() ?
1336 (double)Op1->getValueAPF().convertToFloat() :
Dale Johannesen43421b32007-09-06 18:13:44 +00001337 Op1->getValueAPF().convertToDouble();
John Criswellbd9d3702005-10-27 16:00:10 +00001338 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
Chris Lattnerd0806a12009-10-05 05:06:24 +00001339 if (Op2->getType() != Op1->getType())
1340 return 0;
1341
1342 double Op2V = Ty->isFloatTy() ?
Dale Johannesen43421b32007-09-06 18:13:44 +00001343 (double)Op2->getValueAPF().convertToFloat():
1344 Op2->getValueAPF().convertToDouble();
John Criswellbd9d3702005-10-27 16:00:10 +00001345
Chris Lattner68a06032009-10-05 05:00:35 +00001346 if (Name == "pow")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001347 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty);
Chris Lattner68a06032009-10-05 05:00:35 +00001348 if (Name == "fmod")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001349 return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty);
Chris Lattner68a06032009-10-05 05:00:35 +00001350 if (Name == "atan2")
Chris Lattner7b550cc2009-11-06 04:27:31 +00001351 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
Chris Lattnerb5282dc2007-01-15 06:27:37 +00001352 } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
Chandler Carruthf4db8772011-01-10 09:02:58 +00001353 if (F->getIntrinsicID() == Intrinsic::powi && Ty->isFloatTy())
Chris Lattner7b550cc2009-11-06 04:27:31 +00001354 return ConstantFP::get(F->getContext(),
1355 APFloat((float)std::pow((float)Op1V,
Chris Lattner02a260a2008-04-20 00:41:09 +00001356 (int)Op2C->getZExtValue())));
Chandler Carruthf4db8772011-01-10 09:02:58 +00001357 if (F->getIntrinsicID() == Intrinsic::powi && Ty->isDoubleTy())
Chris Lattner7b550cc2009-11-06 04:27:31 +00001358 return ConstantFP::get(F->getContext(),
1359 APFloat((double)std::pow((double)Op1V,
1360 (int)Op2C->getZExtValue())));
John Criswellbd9d3702005-10-27 16:00:10 +00001361 }
Chris Lattner68a06032009-10-05 05:00:35 +00001362 return 0;
John Criswellbd9d3702005-10-27 16:00:10 +00001363 }
Chris Lattnere65cd402009-10-05 05:26:04 +00001364
1365
1366 if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
1367 if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
1368 switch (F->getIntrinsicID()) {
1369 default: break;
Chris Lattnereafc5cb2010-10-14 00:05:07 +00001370 case Intrinsic::sadd_with_overflow:
1371 case Intrinsic::uadd_with_overflow:
1372 case Intrinsic::ssub_with_overflow:
1373 case Intrinsic::usub_with_overflow:
Frits van Bommel62086102011-03-27 14:26:13 +00001374 case Intrinsic::smul_with_overflow:
1375 case Intrinsic::umul_with_overflow: {
Chris Lattnereafc5cb2010-10-14 00:05:07 +00001376 APInt Res;
1377 bool Overflow;
1378 switch (F->getIntrinsicID()) {
1379 default: assert(0 && "Invalid case");
1380 case Intrinsic::sadd_with_overflow:
1381 Res = Op1->getValue().sadd_ov(Op2->getValue(), Overflow);
1382 break;
1383 case Intrinsic::uadd_with_overflow:
1384 Res = Op1->getValue().uadd_ov(Op2->getValue(), Overflow);
1385 break;
1386 case Intrinsic::ssub_with_overflow:
1387 Res = Op1->getValue().ssub_ov(Op2->getValue(), Overflow);
1388 break;
1389 case Intrinsic::usub_with_overflow:
1390 Res = Op1->getValue().usub_ov(Op2->getValue(), Overflow);
1391 break;
1392 case Intrinsic::smul_with_overflow:
1393 Res = Op1->getValue().smul_ov(Op2->getValue(), Overflow);
1394 break;
Frits van Bommel62086102011-03-27 14:26:13 +00001395 case Intrinsic::umul_with_overflow:
1396 Res = Op1->getValue().umul_ov(Op2->getValue(), Overflow);
1397 break;
Chris Lattnereafc5cb2010-10-14 00:05:07 +00001398 }
Chris Lattnere65cd402009-10-05 05:26:04 +00001399 Constant *Ops[] = {
Chris Lattnereafc5cb2010-10-14 00:05:07 +00001400 ConstantInt::get(F->getContext(), Res),
1401 ConstantInt::get(Type::getInt1Ty(F->getContext()), Overflow)
Chris Lattnere65cd402009-10-05 05:26:04 +00001402 };
Chris Lattnerb065b062011-06-20 04:01:31 +00001403 return ConstantStruct::get(cast<StructType>(F->getReturnType()), Ops);
Chris Lattnere65cd402009-10-05 05:26:04 +00001404 }
Chris Lattnere65cd402009-10-05 05:26:04 +00001405 }
1406 }
1407
1408 return 0;
1409 }
1410 return 0;
John Criswellbd9d3702005-10-27 16:00:10 +00001411 }
1412 return 0;
1413}