diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -429,18 +429,16 @@ return true; if (auto *CI = dyn_cast(C)) { - if (CI->getBitWidth() > 64 || - (CI->getBitWidth() & 7) != 0) + if ((CI->getBitWidth() & 7) != 0) return false; - - uint64_t Val = CI->getZExtValue(); + const APInt &Val = CI->getValue(); unsigned IntBytes = unsigned(CI->getBitWidth()/8); for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) { - int n = ByteOffset; + unsigned n = ByteOffset; if (!DL.isLittleEndian()) n = IntBytes - n - 1; - CurPtr[i] = (unsigned char)(Val >> (n * 8)); + CurPtr[i] = Val.extractBits(8, n * 8).getZExtValue(); ++ByteOffset; } return true; diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll b/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll --- a/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll +++ b/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll @@ -412,3 +412,12 @@ %v = load i8, ptr @g_i1 ret i8 %v } + +@global128 = internal constant i128 1125899906842625 +define i128 @load-128bit(){ +; CHECK-LABEL: @load-128bit( +; CHECK-NEXT: ret i128 1125899906842625 +; + %1 = load i128, ptr @global128, align 4 + ret i128 %1 +}