Lines Matching full:constant
1 //===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
13 // The current constant folding implementation is implemented in two pieces: the
40 /// This function determines which opcode to use to fold two constant cast
46 unsigned opc, ///< opcode of the second cast constant expression in foldConstantCastPair()
47 ConstantExpr *Op, ///< the first cast constant expression in foldConstantCastPair()
54 // The types and opcodes for the two Cast constant expressions in foldConstantCastPair()
70 static Constant *FoldBitCast(Constant *V, Type *DestTy) { in FoldBitCast()
75 // Handle casts from one vector constant to another. We know that the src in FoldBitCast()
79 return Constant::getAllOnesValue(DestTy); in FoldBitCast()
89 // Handle integral constant input. in FoldBitCast()
112 // Make sure dest type is compatible with the folded integer constant. in FoldBitCast()
123 static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V, in foldMaybeUndesirableCast()
130 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, in ConstantFoldCastInstruction()
141 return Constant::getNullValue(DestTy); in ConstantFoldCastInstruction()
147 return Constant::getNullValue(DestTy); in ConstantFoldCastInstruction()
149 // If the cast operand is a constant expression, there's a few things we can in ConstantFoldCastInstruction()
159 // If the cast operand is a constant vector, perform the cast by in ConstantFoldCastInstruction()
169 if (Constant *Splat = V->getSplatValue()) { in ConstantFoldCastInstruction()
170 Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy); in ConstantFoldCastInstruction()
176 SmallVector<Constant *, 16> res; in ConstantFoldCastInstruction()
181 Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); in ConstantFoldCastInstruction()
182 Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy); in ConstantFoldCastInstruction()
194 llvm_unreachable("Failed to cast constant expression"); in ConstantFoldCastInstruction()
215 // the input constant. in ConstantFoldCastInstruction()
267 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, in ConstantFoldSelectInstruction()
268 Constant *V1, Constant *V2) { in ConstantFoldSelectInstruction()
273 // If the condition is a vector constant, fold the result elementwise. in ConstantFoldSelectInstruction()
276 SmallVector<Constant*, 16> Result; in ConstantFoldSelectInstruction()
279 Constant *V; in ConstantFoldSelectInstruction()
280 Constant *V1Element = ConstantExpr::getExtractElement(V1, in ConstantFoldSelectInstruction()
282 Constant *V2Element = ConstantExpr::getExtractElement(V2, in ConstantFoldSelectInstruction()
284 auto *Cond = cast<Constant>(CondV->getOperand(i)); in ConstantFoldSelectInstruction()
320 auto NotPoison = [](Constant *C) { in ConstantFoldSelectInstruction()
345 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, in ConstantFoldExtractElementInstruction()
346 Constant *Idx) { in ConstantFoldExtractElementInstruction()
371 SmallVector<Constant *, 8> Ops; in ConstantFoldExtractElementInstruction()
374 Constant *Op = CE->getOperand(i); in ConstantFoldExtractElementInstruction()
376 Constant *ScalarOp = ConstantExpr::getExtractElement(Op, Idx); in ConstantFoldExtractElementInstruction()
397 if (Constant *C = Val->getAggregateElement(CIdx)) in ConstantFoldExtractElementInstruction()
402 if (Constant *SplatVal = Val->getSplatValue()) in ConstantFoldExtractElementInstruction()
409 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, in ConstantFoldInsertElementInstruction()
410 Constant *Elt, in ConstantFoldInsertElementInstruction()
411 Constant *Idx) { in ConstantFoldInsertElementInstruction()
434 SmallVector<Constant*, 16> Result; in ConstantFoldInsertElementInstruction()
444 Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i)); in ConstantFoldInsertElementInstruction()
451 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, in ConstantFoldShuffleVectorInstruction()
468 Constant *Elt = in ConstantFoldShuffleVectorInstruction()
486 SmallVector<Constant*, 32> Result; in ConstantFoldShuffleVectorInstruction()
493 Constant *InElt; in ConstantFoldShuffleVectorInstruction()
511 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg, in ConstantFoldExtractValueInstruction()
517 if (Constant *C = Agg->getAggregateElement(Idxs[0])) in ConstantFoldExtractValueInstruction()
523 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, in ConstantFoldInsertValueInstruction()
524 Constant *Val, in ConstantFoldInsertValueInstruction()
536 SmallVector<Constant*, 32> Result; in ConstantFoldInsertValueInstruction()
538 Constant *C = Agg->getAggregateElement(i); in ConstantFoldInsertValueInstruction()
552 Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { in ConstantFoldUnaryInstruction()
570 // Constant should not be UndefValue, unless these are vector constants. in ConstantFoldUnaryInstruction()
587 if (Constant *Splat = C->getSplatValue()) in ConstantFoldUnaryInstruction()
588 if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat)) in ConstantFoldUnaryInstruction()
591 // Fold each element and create a vector constant from those constants. in ConstantFoldUnaryInstruction()
592 SmallVector<Constant *, 16> Result; in ConstantFoldUnaryInstruction()
594 Constant *ExtractIdx = ConstantInt::get(Ty, i); in ConstantFoldUnaryInstruction()
595 Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx); in ConstantFoldUnaryInstruction()
596 Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt); in ConstantFoldUnaryInstruction()
609 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, in ConstantFoldBinaryInstruction()
610 Constant *C2) { in ConstantFoldBinaryInstruction()
615 if (Constant *Identity = ConstantExpr::getBinOpIdentity( in ConstantFoldBinaryInstruction()
621 } else if (Constant *Identity = ConstantExpr::getBinOpIdentity( in ConstantFoldBinaryInstruction()
643 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
651 return Constant::getNullValue(C1->getType()); // undef & X -> 0 in ConstantFoldBinaryInstruction()
663 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
672 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
680 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
684 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 in ConstantFoldBinaryInstruction()
690 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
697 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
703 return Constant::getNullValue(C1->getType()); in ConstantFoldBinaryInstruction()
729 // Neither constant should be UndefValue, unless these are vector constants. in ConstantFoldBinaryInstruction()
732 // Handle simplifications when the RHS is a constant int. in ConstantFoldBinaryInstruction()
747 return Constant::getNullValue(CI2->getType()); // X % 1 == 0 in ConstantFoldBinaryInstruction()
756 // If and'ing the address of a global with a constant, fold it. in ConstantFoldBinaryInstruction()
789 return Constant::getNullValue(CI2->getType()); in ConstantFoldBinaryInstruction()
897 if (Constant *C2Splat = C2->getSplatValue()) { in ConstantFoldBinaryInstruction()
900 if (Constant *C1Splat = C1->getSplatValue()) { in ConstantFoldBinaryInstruction()
901 Constant *Res = in ConstantFoldBinaryInstruction()
912 // Fold each element and create a vector constant from those constants. in ConstantFoldBinaryInstruction()
913 SmallVector<Constant*, 16> Result; in ConstantFoldBinaryInstruction()
916 Constant *ExtractIdx = ConstantInt::get(Ty, i); in ConstantFoldBinaryInstruction()
917 Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx); in ConstantFoldBinaryInstruction()
918 Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx); in ConstantFoldBinaryInstruction()
924 Constant *Res = ConstantExpr::isDesirableBinOp(Opcode) in ConstantFoldBinaryInstruction()
944 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2); in ConstantFoldBinaryInstruction()
949 // If C2 is a constant expr and C1 isn't, flop them around and fold the in ConstantFoldBinaryInstruction()
1016 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) { in evaluateICmpRelation()
1029 auto GetComplexity = [](Constant *V) { in evaluateICmpRelation()
1046 // Now we know that the RHS is a BlockAddress or simple constant. in evaluateICmpRelation()
1058 // constant. in evaluateICmpRelation()
1066 // NOTE: We should not be doing this constant folding if null pointer in evaluateICmpRelation()
1068 // query it from the Constant type. in evaluateICmpRelation()
1076 // constantexpr, a global, block address, or a simple constant. in evaluateICmpRelation()
1077 Constant *CE1Op0 = CE1->getOperand(0); in evaluateICmpRelation()
1082 // Ok, since this is a getelementptr, we know that the constant has a in evaluateICmpRelation()
1104 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand()); in evaluateICmpRelation()
1125 Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, in ConstantFoldCompareInstruction()
1126 Constant *C1, Constant *C2) { in ConstantFoldCompareInstruction()
1136 return Constant::getNullValue(ResultTy); in ConstantFoldCompareInstruction()
1139 return Constant::getAllOnesValue(ResultTy); in ConstantFoldCompareInstruction()
1164 // The caller is expected to commute the operands if the constant expression in ConstantFoldCompareInstruction()
1168 return Constant::getAllOnesValue(ResultTy); in ConstantFoldCompareInstruction()
1171 return Constant::getNullValue(ResultTy); in ConstantFoldCompareInstruction()
1199 if (Constant *C1Splat = C1->getSplatValue()) in ConstantFoldCompareInstruction()
1200 if (Constant *C2Splat = C2->getSplatValue()) in ConstantFoldCompareInstruction()
1201 if (Constant *Elt = in ConstantFoldCompareInstruction()
1210 // If we can constant fold the comparison of each element, constant fold in ConstantFoldCompareInstruction()
1212 SmallVector<Constant*, 4> ResElts; in ConstantFoldCompareInstruction()
1214 // Compare the elements, producing an i1 result or constant expr. in ConstantFoldCompareInstruction()
1217 Constant *C1E = in ConstantFoldCompareInstruction()
1219 Constant *C2E = in ConstantFoldCompareInstruction()
1221 Constant *Elt = ConstantFoldCompareInstruction(Predicate, C1E, C2E); in ConstantFoldCompareInstruction()
1329 // If C2 is a constant expr and C1 isn't, flip them around and fold the in ConstantFoldCompareInstruction()
1339 Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, in ConstantFoldGetElementPtr()
1359 Constant *IdxC = cast<Constant>(Idx); in ConstantFoldGetElementPtr()