[llvm] [GlobalISel] Constant-fold G_PTR_ADD with different type sizes (PR #81473)
Pierre van Houtryve via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 12 05:09:27 PST 2024
================
@@ -660,8 +660,17 @@ std::optional<APInt> llvm::ConstantFoldBinOp(unsigned Opcode,
default:
break;
case TargetOpcode::G_ADD:
- case TargetOpcode::G_PTR_ADD:
return C1 + C2;
+ case TargetOpcode::G_PTR_ADD: {
+ // Types can be of different width here.
+ if (C1.getBitWidth() > C2.getBitWidth())
+ return C2.zext(C1.getBitWidth()) + C1;
+ // We always need to return something the same size as C1, so
+ // truncate in this case.
+ if (C1.getBitWidth() < C2.getBitWidth())
+ return C1 + C2.trunc(C1.getBitWidth());
+ return C1 + C2;
----------------
Pierre-vh wrote:
I think sext is probably more correct as offsets can be negative, right?
https://github.com/llvm/llvm-project/pull/81473
More information about the llvm-commits
mailing list