[llvm] 348ea34 - [AsmPrinter] Further restrict expressions supported in global initializers
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 29 01:02:16 PDT 2022
Author: Nikita Popov
Date: 2022-06-29T10:02:07+02:00
New Revision: 348ea34bcdbd4c62dd9b41cb05c7081f530f39cc
URL: https://github.com/llvm/llvm-project/commit/348ea34bcdbd4c62dd9b41cb05c7081f530f39cc
DIFF: https://github.com/llvm/llvm-project/commit/348ea34bcdbd4c62dd9b41cb05c7081f530f39cc.diff
LOG: [AsmPrinter] Further restrict expressions supported in global initializers
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.
Differential Revision: https://reviews.llvm.org/D127972
Added:
llvm/test/CodeGen/X86/nonconst-static-div.ll
Modified:
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index c7fef2f8162be..4a31bf85446be 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2722,6 +2722,9 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
llvm_unreachable("Unknown constant value to lower!");
}
+ // The constant expression opcodes are limited to those that are necessary
+ // to represent relocations on supported targets. Expressions involving only
+ // constant addresses are constant folded instead.
switch (CE->getOpcode()) {
case Instruction::AddrSpaceCast: {
const Constant *Op = CE->getOperand(0);
@@ -2839,34 +2842,17 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
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
diff erent 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);
}
}
}
diff --git a/llvm/test/CodeGen/X86/nonconst-static-div.ll b/llvm/test/CodeGen/X86/nonconst-static-div.ll
new file mode 100644
index 0000000000000..ff4b6fed8ed58
--- /dev/null
+++ b/llvm/test/CodeGen/X86/nonconst-static-div.ll
@@ -0,0 +1,12 @@
+; RUN: not --crash llc -mtriple=i686-linux-gnu < %s 2>&1 | FileCheck %s
+
+; Targets only support a limited set of relocations. Make sure that unusual
+; constant expressions (and in particular potentially trapping ones involving
+; division) are already rejected when lowering the Constant to the MC layer,
+; rather than only when trying to emit a relocation. This makes sure that an
+; error is reported when targeting assembly (without -filetype=obj).
+
+ 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
diff --git a/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll b/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
index 134ce3a41a797..15ad13022235a 100644
--- a/llvm/test/CodeGen/X86/ptrtoint-constexpr.ll
+++ b/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 (ptr getelementptr (i2, ptr null, i64 1) to i64))
More information about the llvm-commits
mailing list