[PATCH] D127972: [AsmPrinter] Further restrict expressions supported in global initializers

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 16 07:51:12 PDT 2022


nikic created this revision.
Herald added subscribers: jsji, pengfei, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

`lowerConstant()` currently accepts a number of constant expressions which have corresponding MC expressions, but which cannot be evaluated as a relocatable expression (unless the operands are constant, in which case we'll just fold the expression to a constant).

The motivation here is to clarify which constant expressions are really needed for https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179, and in particular clarify that we do not need to support any division expressions, which are particularly problematic.


https://reviews.llvm.org/D127972

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/test/CodeGen/X86/nonconst-static-div.ll
  llvm/test/CodeGen/X86/ptrtoint-constexpr.ll


Index: llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
===================================================================
--- llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
+++ llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
@@ -9,6 +9,6 @@
 
 ; CHECK:	.globl x
 ; CHECK: x:
-; CHECK: .quad	((0+1)&4294967295)*3
+; CHECK: .quad	3
 
 @x = global i64 mul (i64 3, i64 ptrtoint (i2* getelementptr (i2, i2* null, i64 1) to i64))
Index: llvm/test/CodeGen/X86/nonconst-static-div.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/nonconst-static-div.ll
@@ -0,0 +1,7 @@
+; RUN: not --crash llc -mtriple=i686-linux-gnu < %s 2>&1 | FileCheck %s
+
+ at g = external global i32
+ at g2 = global i64 sdiv (i64 3, i64 ptrtoint (ptr @g to i64))
+
+; CHECK: Unsupported expression in static initializer: sdiv
+
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2813,34 +2813,17 @@
         return RelocExpr;
       }
     }
+
+    const MCExpr *LHS = lowerConstant(CE->getOperand(0));
+    const MCExpr *RHS = lowerConstant(CE->getOperand(1));
+    return MCBinaryExpr::createSub(LHS, RHS, Ctx);
+    break;
   }
-  // else fallthrough
-  LLVM_FALLTHROUGH;
-
-  // The MC library also has a right-shift operator, but it isn't consistently
-  // signed or unsigned between different targets.
-  case Instruction::Add:
-  case Instruction::Mul:
-  case Instruction::SDiv:
-  case Instruction::SRem:
-  case Instruction::Shl:
-  case Instruction::And:
-  case Instruction::Or:
-  case Instruction::Xor: {
+
+  case Instruction::Add: {
     const MCExpr *LHS = lowerConstant(CE->getOperand(0));
     const MCExpr *RHS = lowerConstant(CE->getOperand(1));
-    switch (CE->getOpcode()) {
-    default: llvm_unreachable("Unknown binary operator constant cast expr");
-    case Instruction::Add: return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
-    case Instruction::Sub: return MCBinaryExpr::createSub(LHS, RHS, Ctx);
-    case Instruction::Mul: return MCBinaryExpr::createMul(LHS, RHS, Ctx);
-    case Instruction::SDiv: return MCBinaryExpr::createDiv(LHS, RHS, Ctx);
-    case Instruction::SRem: return MCBinaryExpr::createMod(LHS, RHS, Ctx);
-    case Instruction::Shl: return MCBinaryExpr::createShl(LHS, RHS, Ctx);
-    case Instruction::And: return MCBinaryExpr::createAnd(LHS, RHS, Ctx);
-    case Instruction::Or:  return MCBinaryExpr::createOr (LHS, RHS, Ctx);
-    case Instruction::Xor: return MCBinaryExpr::createXor(LHS, RHS, Ctx);
-    }
+    return MCBinaryExpr::createAdd(LHS, RHS, Ctx);
   }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127972.437540.patch
Type: text/x-patch
Size: 2704 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220616/ff585130/attachment.bin>


More information about the llvm-commits mailing list