@@ -231,26 +231,20 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
231
231
return nullptr ;
232
232
case Instruction::ZExt:
233
233
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
234
- uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth ();
235
- return ConstantInt::get (V->getContext (),
236
- CI->getValue ().zext (BitWidth));
234
+ uint32_t BitWidth = DestTy->getScalarSizeInBits ();
235
+ return ConstantInt::get (DestTy, CI->getValue ().zext (BitWidth));
237
236
}
238
237
return nullptr ;
239
238
case Instruction::SExt:
240
239
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
241
- uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth ();
242
- return ConstantInt::get (V->getContext (),
243
- CI->getValue ().sext (BitWidth));
240
+ uint32_t BitWidth = DestTy->getScalarSizeInBits ();
241
+ return ConstantInt::get (DestTy, CI->getValue ().sext (BitWidth));
244
242
}
245
243
return nullptr ;
246
244
case Instruction::Trunc: {
247
- if (V->getType ()->isVectorTy ())
248
- return nullptr ;
249
-
250
- uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth ();
251
245
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
252
- return ConstantInt::get (V-> getContext (),
253
- CI->getValue ().trunc (DestBitWidth ));
246
+ uint32_t BitWidth = DestTy-> getScalarSizeInBits ();
247
+ return ConstantInt::get (DestTy, CI->getValue ().trunc (BitWidth ));
254
248
}
255
249
256
250
return nullptr ;
@@ -807,44 +801,44 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
807
801
default :
808
802
break ;
809
803
case Instruction::Add:
810
- return ConstantInt::get (CI1-> getContext (), C1V + C2V);
804
+ return ConstantInt::get (C1-> getType (), C1V + C2V);
811
805
case Instruction::Sub:
812
- return ConstantInt::get (CI1-> getContext (), C1V - C2V);
806
+ return ConstantInt::get (C1-> getType (), C1V - C2V);
813
807
case Instruction::Mul:
814
- return ConstantInt::get (CI1-> getContext (), C1V * C2V);
808
+ return ConstantInt::get (C1-> getType (), C1V * C2V);
815
809
case Instruction::UDiv:
816
810
assert (!CI2->isZero () && " Div by zero handled above" );
817
- return ConstantInt::get (CI1->getContext (), C1V.udiv (C2V));
811
+ return ConstantInt::get (CI1->getType (), C1V.udiv (C2V));
818
812
case Instruction::SDiv:
819
813
assert (!CI2->isZero () && " Div by zero handled above" );
820
814
if (C2V.isAllOnes () && C1V.isMinSignedValue ())
821
815
return PoisonValue::get (CI1->getType ()); // MIN_INT / -1 -> poison
822
- return ConstantInt::get (CI1->getContext (), C1V.sdiv (C2V));
816
+ return ConstantInt::get (CI1->getType (), C1V.sdiv (C2V));
823
817
case Instruction::URem:
824
818
assert (!CI2->isZero () && " Div by zero handled above" );
825
- return ConstantInt::get (CI1-> getContext (), C1V.urem (C2V));
819
+ return ConstantInt::get (C1-> getType (), C1V.urem (C2V));
826
820
case Instruction::SRem:
827
821
assert (!CI2->isZero () && " Div by zero handled above" );
828
822
if (C2V.isAllOnes () && C1V.isMinSignedValue ())
829
- return PoisonValue::get (CI1 ->getType ()); // MIN_INT % -1 -> poison
830
- return ConstantInt::get (CI1-> getContext (), C1V.srem (C2V));
823
+ return PoisonValue::get (C1 ->getType ()); // MIN_INT % -1 -> poison
824
+ return ConstantInt::get (C1-> getType (), C1V.srem (C2V));
831
825
case Instruction::And:
832
- return ConstantInt::get (CI1-> getContext (), C1V & C2V);
826
+ return ConstantInt::get (C1-> getType (), C1V & C2V);
833
827
case Instruction::Or:
834
- return ConstantInt::get (CI1-> getContext (), C1V | C2V);
828
+ return ConstantInt::get (C1-> getType (), C1V | C2V);
835
829
case Instruction::Xor:
836
- return ConstantInt::get (CI1-> getContext (), C1V ^ C2V);
830
+ return ConstantInt::get (C1-> getType (), C1V ^ C2V);
837
831
case Instruction::Shl:
838
832
if (C2V.ult (C1V.getBitWidth ()))
839
- return ConstantInt::get (CI1-> getContext (), C1V.shl (C2V));
833
+ return ConstantInt::get (C1-> getType (), C1V.shl (C2V));
840
834
return PoisonValue::get (C1->getType ()); // too big shift is poison
841
835
case Instruction::LShr:
842
836
if (C2V.ult (C1V.getBitWidth ()))
843
- return ConstantInt::get (CI1-> getContext (), C1V.lshr (C2V));
837
+ return ConstantInt::get (C1-> getType (), C1V.lshr (C2V));
844
838
return PoisonValue::get (C1->getType ()); // too big shift is poison
845
839
case Instruction::AShr:
846
840
if (C2V.ult (C1V.getBitWidth ()))
847
- return ConstantInt::get (CI1-> getContext (), C1V.ashr (C2V));
841
+ return ConstantInt::get (C1-> getType (), C1V.ashr (C2V));
848
842
return PoisonValue::get (C1->getType ()); // too big shift is poison
849
843
}
850
844
}
@@ -877,7 +871,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
877
871
return ConstantFP::get (C1->getContext (), C3V);
878
872
}
879
873
}
880
- } else if (auto *VTy = dyn_cast<VectorType>(C1->getType ())) {
874
+ }
875
+
876
+ if (auto *VTy = dyn_cast<VectorType>(C1->getType ())) {
881
877
// Fast path for splatted constants.
882
878
if (Constant *C2Splat = C2->getSplatValue ()) {
883
879
if (Instruction::isIntDivRem (Opcode) && C2Splat->isNullValue ())
0 commit comments