-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Add support for flag output operand "=@cc" for SystemZ. #125970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 9 commits
8a07b3d
d3f3c03
062e03a
5aef564
f10e46b
2ab7a75
132cf31
fccc70e
7ea0c5f
d787c8b
82a26f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1114,10 +1114,12 @@ class TargetInfo : public TransferrableTargetInfo, | |
|
||
std::string ConstraintStr; // constraint: "=rm" | ||
std::string Name; // Operand name: [foo] with no []'s. | ||
unsigned FlagOutputCCUpperBound; | ||
|
||
public: | ||
ConstraintInfo(StringRef ConstraintStr, StringRef Name) | ||
: Flags(0), TiedOperand(-1), ConstraintStr(ConstraintStr.str()), | ||
Name(Name.str()) { | ||
Name(Name.str()), FlagOutputCCUpperBound(0) { | ||
ImmRange.Min = ImmRange.Max = 0; | ||
ImmRange.isConstrained = false; | ||
} | ||
|
@@ -1188,6 +1190,14 @@ class TargetInfo : public TransferrableTargetInfo, | |
TiedOperand = N; | ||
// Don't copy Name or constraint string. | ||
} | ||
|
||
// CC range can be set by target. SystemZ sets it to 4. It is 2 by default. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment is wrong now (2 is no longer default). Also, it's probably not necessary to specifically call out SystemZ here. |
||
void setFlagOutputCCUpperBound(unsigned CCBound) { | ||
FlagOutputCCUpperBound = CCBound; | ||
} | ||
unsigned getFlagOutputCCUpperBound() const { | ||
return FlagOutputCCUpperBound; | ||
} | ||
}; | ||
|
||
/// Validate register name used for global register variables. | ||
|
@@ -1228,6 +1238,7 @@ class TargetInfo : public TransferrableTargetInfo, | |
std::string &/*SuggestedModifier*/) const { | ||
return true; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be here. |
||
virtual bool | ||
validateAsmConstraint(const char *&Name, | ||
TargetInfo::ConstraintInfo &info) const = 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,15 @@ bool SystemZTargetInfo::validateAsmConstraint( | |
case 'T': // Likewise, plus an index | ||
Info.setAllowsMemory(); | ||
return true; | ||
case '@': | ||
// CC condition changes. | ||
if (StringRef(Name) == "@cc") { | ||
Name += 2; | ||
Info.setAllowsRegister(); | ||
Info.setFlagOutputCCUpperBound(4); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we should have the platform-specific comment explaining why this is 4. |
||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
|
@@ -160,6 +169,9 @@ unsigned SystemZTargetInfo::getMinGlobalAlign(uint64_t Size, | |
|
||
void SystemZTargetInfo::getTargetDefines(const LangOptions &Opts, | ||
MacroBuilder &Builder) const { | ||
// Inline assembly supports SystemZ flag outputs. | ||
Builder.defineMacro("__GCC_ASM_FLAG_OUTPUTS__"); | ||
|
||
Builder.defineMacro("__s390__"); | ||
Builder.defineMacro("__s390x__"); | ||
Builder.defineMacro("__zarch__"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,6 +119,10 @@ class LLVM_LIBRARY_VISIBILITY SystemZTargetInfo : public TargetInfo { | |
TargetInfo::ConstraintInfo &info) const override; | ||
|
||
std::string convertConstraint(const char *&Constraint) const override { | ||
if (llvm::StringRef(Constraint) == "@cc") { | ||
uweigand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Constraint += 2; | ||
return std::string("{@cc}"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we also should have a |
||
switch (Constraint[0]) { | ||
case 'p': // Keep 'p' constraint. | ||
return std::string("p"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2601,7 +2601,7 @@ EmitAsmStores(CodeGenFunction &CGF, const AsmStmt &S, | |
const llvm::ArrayRef<LValue> ResultRegDests, | ||
const llvm::ArrayRef<QualType> ResultRegQualTys, | ||
const llvm::BitVector &ResultTypeRequiresCast, | ||
const llvm::BitVector &ResultRegIsFlagReg) { | ||
const std::vector<unsigned> &ResultRegIsFlagReg) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument name should also be updated now. |
||
CGBuilderTy &Builder = CGF.Builder; | ||
CodeGenModule &CGM = CGF.CGM; | ||
llvm::LLVMContext &CTX = CGF.getLLVMContext(); | ||
|
@@ -2621,9 +2621,18 @@ EmitAsmStores(CodeGenFunction &CGF, const AsmStmt &S, | |
if ((i < ResultRegIsFlagReg.size()) && ResultRegIsFlagReg[i]) { | ||
// Target must guarantee the Value `Tmp` here is lowered to a boolean | ||
// value. | ||
llvm::Constant *Two = llvm::ConstantInt::get(Tmp->getType(), 2); | ||
// Lowering 'Tmp' as - 'icmp ult %Tmp , CCUpperBound'. On some targets | ||
// CCUpperBound is not binary. CCUpperBound is 4 for SystemZ, | ||
// interval [0, 4). With this range known, llvm.assume intrinsic guides | ||
// optimizer to generate more optimized IR in most of the cases as | ||
// observed for select_cc on SystemZ unit tests for flag output operands. | ||
// For some cases for br_cc, generated IR was weird. e.g. switch table | ||
// for simple simple comparison terms for br_cc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to explain that wrong code will result from an incorrect assertion. |
||
unsigned CCUpperBound = ResultRegIsFlagReg[i]; | ||
llvm::Constant *CCUpperBoundConst = | ||
llvm::ConstantInt::get(Tmp->getType(), CCUpperBound); | ||
llvm::Value *IsBooleanValue = | ||
Builder.CreateCmp(llvm::CmpInst::ICMP_ULT, Tmp, Two); | ||
Builder.CreateCmp(llvm::CmpInst::ICMP_ULT, Tmp, CCUpperBoundConst); | ||
llvm::Function *FnAssume = CGM.getIntrinsic(llvm::Intrinsic::assume); | ||
Builder.CreateCall(FnAssume, IsBooleanValue); | ||
} | ||
|
@@ -2750,7 +2759,7 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { | |
std::vector<llvm::Type *> ArgElemTypes; | ||
std::vector<llvm::Value*> Args; | ||
llvm::BitVector ResultTypeRequiresCast; | ||
llvm::BitVector ResultRegIsFlagReg; | ||
std::vector<unsigned> ResultRegIsFlagReg; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again the name. |
||
|
||
// Keep track of inout constraints. | ||
std::string InOutConstraints; | ||
|
@@ -2808,8 +2817,7 @@ void CodeGenFunction::EmitAsmStmt(const AsmStmt &S) { | |
ResultRegQualTys.push_back(QTy); | ||
ResultRegDests.push_back(Dest); | ||
|
||
bool IsFlagReg = llvm::StringRef(OutputConstraint).starts_with("{@cc"); | ||
ResultRegIsFlagReg.push_back(IsFlagReg); | ||
ResultRegIsFlagReg.push_back(Info.getFlagOutputCCUpperBound()); | ||
|
||
llvm::Type *Ty = ConvertTypeForMem(QTy); | ||
const bool RequiresCast = Info.allowsRegister() && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: %clang_cc1 -O2 -triple s390x-linux -emit-llvm -o - %s | FileCheck %s | ||
|
||
int foo_012(int x) { | ||
// CHECK-LABEL: @foo_012 | ||
// CHECK: = tail call { i32, i32 } asm "ahi $0,42\0A", "=d,={@cc},0"(i32 %x) | ||
int cc; | ||
asm ("ahi %[x],42\n" : [x] "+d"(x), "=@cc" (cc)); | ||
return cc == 0 || cc == 1 || cc == 2 ? 42 : 0; | ||
} | ||
|
||
int foo_013(int x) { | ||
// CHECK-LABEL: @foo_013 | ||
// CHECK: = tail call { i32, i32 } asm "ahi $0,42\0A", "=d,={@cc},0"(i32 %x) | ||
int cc; | ||
asm ("ahi %[x],42\n" : [x] "+d"(x), "=@cc" (cc)); | ||
return cc == 0 || cc == 1 || cc == 3 ? 42 : 0; | ||
} | ||
|
||
int foo_023(int x) { | ||
// CHECK-LABEL: @foo_023 | ||
// CHECK: = tail call { i32, i32 } asm "ahi $0,42\0A", "=d,={@cc},0"(i32 %x) | ||
int cc; | ||
asm ("ahi %[x],42\n" : [x] "+d"(x), "=@cc" (cc)); | ||
return cc == 0 || cc == 2 || cc == 3 ? 42 : 0; | ||
} | ||
|
||
int foo_123(int x) { | ||
// CHECK-LABEL: @foo_123 | ||
// CHECK: = tail call { i32, i32 } asm "ahi $0,42\0A", "=d,={@cc},0"(i32 %x) | ||
int cc; | ||
asm ("ahi %[x],42\n" : [x] "+d"(x), "=@cc" (cc)); | ||
return cc == 1 || cc == 2 || cc == 3 ? 42 : 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// RUN: %clang -target systemz-unknown-unknown -x c -E -dM -o - %s | FileCheck -match-full-lines %s | ||
// RUN: %clang -target s390x-unknown-unknown -x c -E -dM -o - %s | FileCheck -match-full-lines %s | ||
|
||
// CHECK: #define __GCC_ASM_FLAG_OUTPUTS__ 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2837,7 +2837,6 @@ void SelectionDAGBuilder::visitBr(const BranchInst &I) { | |
Opcode = Instruction::And; | ||
else if (match(BOp, m_LogicalOr(m_Value(BOp0), m_Value(BOp1)))) | ||
Opcode = Instruction::Or; | ||
|
||
if (Opcode && | ||
!(match(BOp0, m_ExtractElt(m_Value(Vec), m_Value())) && | ||
match(BOp1, m_ExtractElt(m_Specific(Vec), m_Value()))) && | ||
|
@@ -12118,12 +12117,13 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond, | |
SDValue CondLHS = getValue(Cond); | ||
EVT VT = CondLHS.getValueType(); | ||
SDLoc DL = getCurSDLoc(); | ||
SDValue Cond; | ||
|
||
SDValue Or = DAG.getNode(ISD::OR, DL, VT, CondLHS, | ||
DAG.getConstant(CommonBit, DL, VT)); | ||
SDValue Cond = DAG.getSetCC( | ||
DL, MVT::i1, Or, DAG.getConstant(BigValue | SmallValue, DL, VT), | ||
ISD::SETEQ); | ||
Cond = DAG.getSetCC(DL, MVT::i1, Or, | ||
DAG.getConstant(BigValue | SmallValue, DL, VT), | ||
ISD::SETEQ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All changes to this file should not be here. |
||
|
||
// Update successor info. | ||
// Both Small and Big will jump to Small.BB, so we sum up the | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering if we can re-use the existing range fields
ImmRange
here. Those are currently only used for input operands and require that only immediates within this range are used as input. For an output operand, this isn't useful - but instead we could take it to mean that the output is logically constrained to fall within that range, so we can add appropriate assertions.