[llvm] [TypePromotion] Support positive addition amounts in isSafeWrap. (PR #81690)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 29 20:11:19 PST 2024


================
@@ -356,15 +351,23 @@ bool TypePromotionImpl::isSafeWrap(Instruction *I) {
   APInt OverflowConst = cast<ConstantInt>(I->getOperand(1))->getValue();
   if (Opc == Instruction::Sub)
     OverflowConst = -OverflowConst;
-  if (!OverflowConst.isNonPositive())
-    return false;
+
+  // If the constant is positive, we will end up filling the promoted bits with
+  // all 1s. Make sure that results in a cheap add constant.
+  if (!OverflowConst.isNonPositive()) {
+    // We don't have the true promoted width, just use 64 so we can create an
+    // int64_t for the isLegalAddImmediate call.
+    if (OverflowConst.getBitWidth() >= 64)
+      return false;
+
+    APInt NewConst = -((-OverflowConst).zext(64));
+    if (!TLI->isLegalAddImmediate(NewConst.getSExtValue()))
----------------
AtariDreams wrote:

I think we should do this if the immediate is cheaper, not just legal. 

https://github.com/llvm/llvm-project/pull/81690


More information about the llvm-commits mailing list