1 //===-- ConstantFolding.cpp - Analyze constant folding possibilities ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions determines the possibility of performing constant
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/ConstantFolding.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Support/GetElementPtrTypeIterator.h"
24 #include "llvm/Support/MathExtras.h"
29 //===----------------------------------------------------------------------===//
30 // Constant Folding internal helper functions
31 //===----------------------------------------------------------------------===//
33 /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset
34 /// from a global, return the global and the constant. Because of
35 /// constantexprs, this function is recursive.
36 static bool IsConstantOffsetFromGlobal(Constant
*C
, GlobalValue
*&GV
,
37 int64_t &Offset
, const TargetData
&TD
) {
38 // Trivial case, constant is the global.
39 if ((GV
= dyn_cast
<GlobalValue
>(C
))) {
44 // Otherwise, if this isn't a constant expr, bail out.
45 ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
);
46 if (!CE
) return false;
48 // Look through ptr->int and ptr->ptr casts.
49 if (CE
->getOpcode() == Instruction::PtrToInt
||
50 CE
->getOpcode() == Instruction::BitCast
)
51 return IsConstantOffsetFromGlobal(CE
->getOperand(0), GV
, Offset
, TD
);
53 // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
54 if (CE
->getOpcode() == Instruction::GetElementPtr
) {
55 // Cannot compute this if the element type of the pointer is missing size
57 if (!cast
<PointerType
>(CE
->getOperand(0)->getType())->getElementType()->isSized())
60 // If the base isn't a global+constant, we aren't either.
61 if (!IsConstantOffsetFromGlobal(CE
->getOperand(0), GV
, Offset
, TD
))
64 // Otherwise, add any offset that our operands provide.
65 gep_type_iterator GTI
= gep_type_begin(CE
);
66 for (unsigned i
= 1, e
= CE
->getNumOperands(); i
!= e
; ++i
, ++GTI
) {
67 ConstantInt
*CI
= dyn_cast
<ConstantInt
>(CE
->getOperand(i
));
68 if (!CI
) return false; // Index isn't a simple constant?
69 if (CI
->getZExtValue() == 0) continue; // Not adding anything.
71 if (const StructType
*ST
= dyn_cast
<StructType
>(*GTI
)) {
73 Offset
+= TD
.getStructLayout(ST
)->getElementOffset(CI
->getZExtValue());
75 const SequentialType
*SQT
= cast
<SequentialType
>(*GTI
);
76 Offset
+= TD
.getTypeSize(SQT
->getElementType())*CI
->getSExtValue();
86 /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression.
87 /// Attempt to symbolically evaluate the result of a binary operator merging
88 /// these together. If target data info is available, it is provided as TD,
89 /// otherwise TD is null.
90 static Constant
*SymbolicallyEvaluateBinop(unsigned Opc
, Constant
*Op0
,
91 Constant
*Op1
, const TargetData
*TD
){
94 // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl.
95 // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute
99 // If the constant expr is something like &A[123] - &A[4].f, fold this into a
100 // constant. This happens frequently when iterating over a global array.
101 if (Opc
== Instruction::Sub
&& TD
) {
102 GlobalValue
*GV1
, *GV2
;
103 int64_t Offs1
, Offs2
;
105 if (IsConstantOffsetFromGlobal(Op0
, GV1
, Offs1
, *TD
))
106 if (IsConstantOffsetFromGlobal(Op1
, GV2
, Offs2
, *TD
) &&
108 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
109 return ConstantInt::get(Op0
->getType(), Offs1
-Offs2
);
113 // TODO: Fold icmp setne/seteq as well.
117 /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP
118 /// constant expression, do so.
119 static Constant
*SymbolicallyEvaluateGEP(Constant
** Ops
, unsigned NumOps
,
120 const Type
*ResultTy
,
121 const TargetData
*TD
) {
122 Constant
*Ptr
= Ops
[0];
123 if (!cast
<PointerType
>(Ptr
->getType())->getElementType()->isSized())
126 if (TD
&& Ptr
->isNullValue()) {
127 // If this is a constant expr gep that is effectively computing an
128 // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12'
129 bool isFoldableGEP
= true;
130 for (unsigned i
= 1; i
!= NumOps
; ++i
)
131 if (!isa
<ConstantInt
>(Ops
[i
])) {
132 isFoldableGEP
= false;
136 uint64_t Offset
= TD
->getIndexedOffset(Ptr
->getType(),
137 (Value
**)Ops
+1, NumOps
-1);
138 Constant
*C
= ConstantInt::get(TD
->getIntPtrType(), Offset
);
139 return ConstantExpr::getIntToPtr(C
, ResultTy
);
147 //===----------------------------------------------------------------------===//
148 // Constant Folding public APIs
149 //===----------------------------------------------------------------------===//
152 /// ConstantFoldInstruction - Attempt to constant fold the specified
153 /// instruction. If successful, the constant result is returned, if not, null
154 /// is returned. Note that this function can only fail when attempting to fold
155 /// instructions like loads and stores, which have no constant expression form.
157 Constant
*llvm::ConstantFoldInstruction(Instruction
*I
, const TargetData
*TD
) {
158 if (PHINode
*PN
= dyn_cast
<PHINode
>(I
)) {
159 if (PN
->getNumIncomingValues() == 0)
160 return Constant::getNullValue(PN
->getType());
162 Constant
*Result
= dyn_cast
<Constant
>(PN
->getIncomingValue(0));
163 if (Result
== 0) return 0;
165 // Handle PHI nodes specially here...
166 for (unsigned i
= 1, e
= PN
->getNumIncomingValues(); i
!= e
; ++i
)
167 if (PN
->getIncomingValue(i
) != Result
&& PN
->getIncomingValue(i
) != PN
)
168 return 0; // Not all the same incoming constants...
170 // If we reach here, all incoming values are the same constant.
174 // Scan the operand list, checking to see if they are all constants, if so,
175 // hand off to ConstantFoldInstOperands.
176 SmallVector
<Constant
*, 8> Ops
;
177 for (unsigned i
= 0, e
= I
->getNumOperands(); i
!= e
; ++i
)
178 if (Constant
*Op
= dyn_cast
<Constant
>(I
->getOperand(i
)))
181 return 0; // All operands not constant!
183 return ConstantFoldInstOperands(I
, &Ops
[0], Ops
.size(), TD
);
186 /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
187 /// specified opcode and operands. If successful, the constant result is
188 /// returned, if not, null is returned. Note that this function can fail when
189 /// attempting to fold instructions like loads and stores, which have no
190 /// constant expression form.
192 Constant
*llvm::ConstantFoldInstOperands(const Instruction
* I
,
193 Constant
** Ops
, unsigned NumOps
,
194 const TargetData
*TD
) {
195 unsigned Opc
= I
->getOpcode();
196 const Type
*DestTy
= I
->getType();
198 // Handle easy binops first.
199 if (isa
<BinaryOperator
>(I
)) {
200 if (isa
<ConstantExpr
>(Ops
[0]) || isa
<ConstantExpr
>(Ops
[1]))
201 if (Constant
*C
= SymbolicallyEvaluateBinop(I
->getOpcode(), Ops
[0],
205 return ConstantExpr::get(Opc
, Ops
[0], Ops
[1]);
210 case Instruction::Call
:
211 if (Function
*F
= dyn_cast
<Function
>(Ops
[0]))
212 if (canConstantFoldCallTo(F
))
213 return ConstantFoldCall(F
, Ops
+1, NumOps
-1);
215 case Instruction::ICmp
:
216 case Instruction::FCmp
:
217 return ConstantExpr::getCompare(cast
<CmpInst
>(I
)->getPredicate(), Ops
[0],
219 case Instruction::Trunc
:
220 case Instruction::ZExt
:
221 case Instruction::SExt
:
222 case Instruction::FPTrunc
:
223 case Instruction::FPExt
:
224 case Instruction::UIToFP
:
225 case Instruction::SIToFP
:
226 case Instruction::FPToUI
:
227 case Instruction::FPToSI
:
228 case Instruction::PtrToInt
:
229 case Instruction::IntToPtr
:
230 case Instruction::BitCast
:
231 return ConstantExpr::getCast(Opc
, Ops
[0], DestTy
);
232 case Instruction::Select
:
233 return ConstantExpr::getSelect(Ops
[0], Ops
[1], Ops
[2]);
234 case Instruction::ExtractElement
:
235 return ConstantExpr::getExtractElement(Ops
[0], Ops
[1]);
236 case Instruction::InsertElement
:
237 return ConstantExpr::getInsertElement(Ops
[0], Ops
[1], Ops
[2]);
238 case Instruction::ShuffleVector
:
239 return ConstantExpr::getShuffleVector(Ops
[0], Ops
[1], Ops
[2]);
240 case Instruction::GetElementPtr
:
241 if (Constant
*C
= SymbolicallyEvaluateGEP(Ops
, NumOps
, I
->getType(), TD
))
244 return ConstantExpr::getGetElementPtr(Ops
[0], Ops
+1, NumOps
-1);
248 /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a
249 /// getelementptr constantexpr, return the constant value being addressed by the
250 /// constant expression, or null if something is funny and we can't decide.
251 Constant
*llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant
*C
,
253 if (CE
->getOperand(1) != Constant::getNullValue(CE
->getOperand(1)->getType()))
254 return 0; // Do not allow stepping over the value!
256 // Loop over all of the operands, tracking down which value we are
258 gep_type_iterator I
= gep_type_begin(CE
), E
= gep_type_end(CE
);
259 for (++I
; I
!= E
; ++I
)
260 if (const StructType
*STy
= dyn_cast
<StructType
>(*I
)) {
261 ConstantInt
*CU
= cast
<ConstantInt
>(I
.getOperand());
262 assert(CU
->getZExtValue() < STy
->getNumElements() &&
263 "Struct index out of range!");
264 unsigned El
= (unsigned)CU
->getZExtValue();
265 if (ConstantStruct
*CS
= dyn_cast
<ConstantStruct
>(C
)) {
266 C
= CS
->getOperand(El
);
267 } else if (isa
<ConstantAggregateZero
>(C
)) {
268 C
= Constant::getNullValue(STy
->getElementType(El
));
269 } else if (isa
<UndefValue
>(C
)) {
270 C
= UndefValue::get(STy
->getElementType(El
));
274 } else if (ConstantInt
*CI
= dyn_cast
<ConstantInt
>(I
.getOperand())) {
275 if (const ArrayType
*ATy
= dyn_cast
<ArrayType
>(*I
)) {
276 if (CI
->getZExtValue() >= ATy
->getNumElements())
278 if (ConstantArray
*CA
= dyn_cast
<ConstantArray
>(C
))
279 C
= CA
->getOperand(CI
->getZExtValue());
280 else if (isa
<ConstantAggregateZero
>(C
))
281 C
= Constant::getNullValue(ATy
->getElementType());
282 else if (isa
<UndefValue
>(C
))
283 C
= UndefValue::get(ATy
->getElementType());
286 } else if (const VectorType
*PTy
= dyn_cast
<VectorType
>(*I
)) {
287 if (CI
->getZExtValue() >= PTy
->getNumElements())
289 if (ConstantVector
*CP
= dyn_cast
<ConstantVector
>(C
))
290 C
= CP
->getOperand(CI
->getZExtValue());
291 else if (isa
<ConstantAggregateZero
>(C
))
292 C
= Constant::getNullValue(PTy
->getElementType());
293 else if (isa
<UndefValue
>(C
))
294 C
= UndefValue::get(PTy
->getElementType());
307 //===----------------------------------------------------------------------===//
308 // Constant Folding for Calls
311 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
312 /// the specified function.
314 llvm::canConstantFoldCallTo(Function
*F
) {
315 const std::string
&Name
= F
->getName();
317 switch (F
->getIntrinsicID()) {
318 case Intrinsic::sqrt_f32
:
319 case Intrinsic::sqrt_f64
:
320 case Intrinsic::powi_f32
:
321 case Intrinsic::powi_f64
:
322 case Intrinsic::bswap
:
323 case Intrinsic::ctpop
:
324 case Intrinsic::ctlz
:
325 case Intrinsic::cttz
:
333 return Name
== "acos" || Name
== "asin" || Name
== "atan" ||
336 return Name
== "ceil" || Name
== "cos" || Name
== "cosf" ||
339 return Name
== "exp";
341 return Name
== "fabs" || Name
== "fmod" || Name
== "floor";
343 return Name
== "log" || Name
== "log10";
345 return Name
== "pow";
347 return Name
== "sin" || Name
== "sinh" ||
348 Name
== "sqrt" || Name
== "sqrtf";
350 return Name
== "tan" || Name
== "tanh";
356 static Constant
*ConstantFoldFP(double (*NativeFP
)(double), double V
,
361 return ConstantFP::get(Ty
, V
);
366 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
367 /// with the specified arguments, returning null if unsuccessful.
369 llvm::ConstantFoldCall(Function
*F
, Constant
** Operands
, unsigned NumOperands
) {
370 const std::string
&Name
= F
->getName();
371 const Type
*Ty
= F
->getReturnType();
373 if (NumOperands
== 1) {
374 if (ConstantFP
*Op
= dyn_cast
<ConstantFP
>(Operands
[0])) {
375 double V
= Op
->getValue();
380 return ConstantFoldFP(acos
, V
, Ty
);
381 else if (Name
== "asin")
382 return ConstantFoldFP(asin
, V
, Ty
);
383 else if (Name
== "atan")
384 return ConstantFP::get(Ty
, atan(V
));
388 return ConstantFoldFP(ceil
, V
, Ty
);
389 else if (Name
== "cos")
390 return ConstantFP::get(Ty
, cos(V
));
391 else if (Name
== "cosh")
392 return ConstantFP::get(Ty
, cosh(V
));
396 return ConstantFP::get(Ty
, exp(V
));
400 return ConstantFP::get(Ty
, fabs(V
));
401 else if (Name
== "floor")
402 return ConstantFoldFP(floor
, V
, Ty
);
405 if (Name
== "log" && V
> 0)
406 return ConstantFP::get(Ty
, log(V
));
407 else if (Name
== "log10" && V
> 0)
408 return ConstantFoldFP(log10
, V
, Ty
);
409 else if (Name
== "llvm.sqrt.f32" || Name
== "llvm.sqrt.f64") {
411 return ConstantFP::get(Ty
, sqrt(V
));
413 return ConstantFP::get(Ty
, 0.0);
418 return ConstantFP::get(Ty
, sin(V
));
419 else if (Name
== "sinh")
420 return ConstantFP::get(Ty
, sinh(V
));
421 else if (Name
== "sqrt" && V
>= 0)
422 return ConstantFP::get(Ty
, sqrt(V
));
423 else if (Name
== "sqrtf" && V
>= 0)
424 return ConstantFP::get(Ty
, sqrt((float)V
));
428 return ConstantFP::get(Ty
, tan(V
));
429 else if (Name
== "tanh")
430 return ConstantFP::get(Ty
, tanh(V
));
435 } else if (ConstantInt
*Op
= dyn_cast
<ConstantInt
>(Operands
[0])) {
436 if (Name
.size() > 11 && !memcmp(&Name
[0], "llvm.bswap", 10)) {
437 return ConstantInt::get(Op
->getValue().byteSwap());
438 } else if (Name
.size() > 11 && !memcmp(&Name
[0],"llvm.ctpop",10)) {
439 uint64_t ctpop
= Op
->getValue().countPopulation();
440 return ConstantInt::get(Type::Int32Ty
, ctpop
);
441 } else if (Name
.size() > 10 && !memcmp(&Name
[0], "llvm.cttz", 9)) {
442 uint64_t cttz
= Op
->getValue().countTrailingZeros();
443 return ConstantInt::get(Type::Int32Ty
, cttz
);
444 } else if (Name
.size() > 10 && !memcmp(&Name
[0], "llvm.ctlz", 9)) {
445 uint64_t ctlz
= Op
->getValue().countLeadingZeros();
446 return ConstantInt::get(Type::Int32Ty
, ctlz
);
449 } else if (NumOperands
== 2) {
450 if (ConstantFP
*Op1
= dyn_cast
<ConstantFP
>(Operands
[0])) {
451 double Op1V
= Op1
->getValue();
452 if (ConstantFP
*Op2
= dyn_cast
<ConstantFP
>(Operands
[1])) {
453 double Op2V
= Op2
->getValue();
457 double V
= pow(Op1V
, Op2V
);
459 return ConstantFP::get(Ty
, V
);
460 } else if (Name
== "fmod") {
462 double V
= fmod(Op1V
, Op2V
);
464 return ConstantFP::get(Ty
, V
);
465 } else if (Name
== "atan2") {
466 return ConstantFP::get(Ty
, atan2(Op1V
,Op2V
));
468 } else if (ConstantInt
*Op2C
= dyn_cast
<ConstantInt
>(Operands
[1])) {
469 if (Name
== "llvm.powi.f32") {
470 return ConstantFP::get(Ty
, std::pow((float)Op1V
,
471 (int)Op2C
->getZExtValue()));
472 } else if (Name
== "llvm.powi.f64") {
473 return ConstantFP::get(Ty
, std::pow((double)Op1V
,
474 (int)Op2C
->getZExtValue()));