Skip to content

Commit 53fb222

Browse files
SC llvm teamSC llvm team
authored andcommitted
Merged main:a533b76468ac into amd-gfx:b70a41a9b44c
Local branch amd-gfx b70a41a Merged main:c2205ab30917 into amd-gfx:cfb0aab9fdbf Remote branch main a533b76 [clang-format][NFC] Simplify parseBracedList() (llvm#72010)
2 parents b70a41a + a533b76 commit 53fb222

File tree

6 files changed

+118
-124
lines changed

6 files changed

+118
-124
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,8 +2017,7 @@ void UnwrappedLineParser::parseStructuralElement(
20172017
} else if (Style.Language == FormatStyle::LK_Proto &&
20182018
FormatTok->is(tok::less)) {
20192019
nextToken();
2020-
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2021-
/*ClosingBraceKind=*/tok::greater);
2020+
parseBracedList(/*IsAngleBracket=*/true);
20222021
}
20232022
break;
20242023
case tok::l_square:
@@ -2379,9 +2378,7 @@ bool UnwrappedLineParser::tryToParseChildBlock() {
23792378
return true;
23802379
}
23812380

2382-
bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
2383-
bool IsEnum,
2384-
tok::TokenKind ClosingBraceKind) {
2381+
bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
23852382
bool HasError = false;
23862383

23872384
// FIXME: Once we have an expression parser in the UnwrappedLineParser,
@@ -2403,7 +2400,7 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
24032400
parseChildBlock();
24042401
}
24052402
}
2406-
if (FormatTok->Tok.getKind() == ClosingBraceKind) {
2403+
if (FormatTok->is(IsAngleBracket ? tok::greater : tok::r_brace)) {
24072404
if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
24082405
addUnwrappedLine();
24092406
nextToken();
@@ -2434,14 +2431,9 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
24342431
parseBracedList();
24352432
break;
24362433
case tok::less:
2437-
if (Style.Language == FormatStyle::LK_Proto ||
2438-
ClosingBraceKind == tok::greater) {
2439-
nextToken();
2440-
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
2441-
/*ClosingBraceKind=*/tok::greater);
2442-
} else {
2443-
nextToken();
2444-
}
2434+
nextToken();
2435+
if (IsAngleBracket)
2436+
parseBracedList(/*IsAngleBracket=*/true);
24452437
break;
24462438
case tok::semi:
24472439
// JavaScript (or more precisely TypeScript) can have semicolons in braced
@@ -2453,8 +2445,8 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
24532445
break;
24542446
}
24552447
HasError = true;
2456-
if (!ContinueOnSemicolons)
2457-
return !HasError;
2448+
if (!IsEnum)
2449+
return false;
24582450
nextToken();
24592451
break;
24602452
case tok::comma:
@@ -3618,8 +3610,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
36183610
return;
36193611

36203612
nextToken();
3621-
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
3622-
/*ClosingBraceKind=*/tok::greater);
3613+
parseBracedList(/*IsAngleBracket=*/true);
36233614
break;
36243615

36253616
default:
@@ -3650,8 +3641,7 @@ void UnwrappedLineParser::parseConstraintExpression() {
36503641
nextToken();
36513642
if (FormatTok->is(tok::less)) {
36523643
nextToken();
3653-
parseBracedList(/*ContinueOnSemicolons=*/false, /*IsEnum=*/false,
3654-
/*ClosingBraceKind=*/tok::greater);
3644+
parseBracedList(/*IsAngleBracket=*/true);
36553645
}
36563646
TopLevelParensAllowed = false;
36573647
break;
@@ -3732,8 +3722,7 @@ bool UnwrappedLineParser::parseEnum() {
37323722
addUnwrappedLine();
37333723
Line->Level += 1;
37343724
}
3735-
bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true,
3736-
/*IsEnum=*/true);
3725+
bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
37373726
if (!Style.AllowShortEnumsOnASingleLine)
37383727
Line->Level -= 1;
37393728
if (HasError) {

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ class UnwrappedLineParser {
150150
bool *HasDoWhile = nullptr,
151151
bool *HasLabel = nullptr);
152152
bool tryToParseBracedList();
153-
bool parseBracedList(bool ContinueOnSemicolons = false, bool IsEnum = false,
154-
tok::TokenKind ClosingBraceKind = tok::r_brace);
153+
bool parseBracedList(bool IsAngleBracket = false, bool IsEnum = false);
155154
bool parseParens(TokenType AmpAmpTokenType = TT_Unknown);
156155
void parseSquare(bool LambdaIntroducer = false);
157156
void keepAncestorBraces();

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Indicate that this is LLVM compiled from the amd-gfx branch. */
1818
#define LLVM_HAVE_BRANCH_AMD_GFX
19-
#define LLVM_MAIN_REVISION 480536
19+
#define LLVM_MAIN_REVISION 480538
2020

2121
/* Define if LLVM_ENABLE_DUMP is enabled */
2222
#cmakedefine LLVM_ENABLE_DUMP

llvm/lib/Target/RISCV/GISel/RISCVInstructionSelector.cpp

Lines changed: 93 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,92 @@ RISCVInstructionSelector::selectAddrRegImm(MachineOperand &Root) const {
296296
[=](MachineInstrBuilder &MIB) { MIB.addImm(0); }}};
297297
}
298298

299+
/// Returns the RISCVCC::CondCode that corresponds to the CmpInst::Predicate CC.
300+
/// CC Must be an ICMP Predicate.
301+
static RISCVCC::CondCode getRISCVCCFromICmp(CmpInst::Predicate CC) {
302+
switch (CC) {
303+
default:
304+
llvm_unreachable("Expected ICMP CmpInst::Predicate.");
305+
case CmpInst::Predicate::ICMP_EQ:
306+
return RISCVCC::COND_EQ;
307+
case CmpInst::Predicate::ICMP_NE:
308+
return RISCVCC::COND_NE;
309+
case CmpInst::Predicate::ICMP_ULT:
310+
return RISCVCC::COND_LTU;
311+
case CmpInst::Predicate::ICMP_SLT:
312+
return RISCVCC::COND_LT;
313+
case CmpInst::Predicate::ICMP_UGE:
314+
return RISCVCC::COND_GEU;
315+
case CmpInst::Predicate::ICMP_SGE:
316+
return RISCVCC::COND_GE;
317+
}
318+
}
319+
320+
static void getOperandsForBranch(Register CondReg, MachineRegisterInfo &MRI,
321+
RISCVCC::CondCode &CC, Register &LHS,
322+
Register &RHS) {
323+
// Try to fold an ICmp. If that fails, use a NE compare with X0.
324+
CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
325+
if (!mi_match(CondReg, MRI, m_GICmp(m_Pred(Pred), m_Reg(LHS), m_Reg(RHS)))) {
326+
LHS = CondReg;
327+
RHS = RISCV::X0;
328+
CC = RISCVCC::COND_NE;
329+
return;
330+
}
331+
332+
// We found an ICmp, do some canonicalizations.
333+
334+
// Adjust comparisons to use comparison with 0 if possible.
335+
if (auto Constant = getIConstantVRegSExtVal(RHS, MRI)) {
336+
switch (Pred) {
337+
case CmpInst::Predicate::ICMP_SGT:
338+
// Convert X > -1 to X >= 0
339+
if (*Constant == -1) {
340+
CC = RISCVCC::COND_GE;
341+
RHS = RISCV::X0;
342+
return;
343+
}
344+
break;
345+
case CmpInst::Predicate::ICMP_SLT:
346+
// Convert X < 1 to 0 >= X
347+
if (*Constant == 1) {
348+
CC = RISCVCC::COND_GE;
349+
RHS = LHS;
350+
LHS = RISCV::X0;
351+
return;
352+
}
353+
break;
354+
default:
355+
break;
356+
}
357+
}
358+
359+
switch (Pred) {
360+
default:
361+
llvm_unreachable("Expected ICMP CmpInst::Predicate.");
362+
case CmpInst::Predicate::ICMP_EQ:
363+
case CmpInst::Predicate::ICMP_NE:
364+
case CmpInst::Predicate::ICMP_ULT:
365+
case CmpInst::Predicate::ICMP_SLT:
366+
case CmpInst::Predicate::ICMP_UGE:
367+
case CmpInst::Predicate::ICMP_SGE:
368+
// These CCs are supported directly by RISC-V branches.
369+
break;
370+
case CmpInst::Predicate::ICMP_SGT:
371+
case CmpInst::Predicate::ICMP_SLE:
372+
case CmpInst::Predicate::ICMP_UGT:
373+
case CmpInst::Predicate::ICMP_ULE:
374+
// These CCs are not supported directly by RISC-V branches, but changing the
375+
// direction of the CC and swapping LHS and RHS are.
376+
Pred = CmpInst::getSwappedPredicate(Pred);
377+
std::swap(LHS, RHS);
378+
break;
379+
}
380+
381+
CC = getRISCVCCFromICmp(Pred);
382+
return;
383+
}
384+
299385
bool RISCVInstructionSelector::select(MachineInstr &MI) {
300386
MachineBasicBlock &MBB = *MI.getParent();
301387
MachineFunction &MF = *MBB.getParent();
@@ -398,10 +484,12 @@ bool RISCVInstructionSelector::select(MachineInstr &MI) {
398484
case TargetOpcode::G_GLOBAL_VALUE:
399485
return selectGlobalValue(MI, MIB, MRI);
400486
case TargetOpcode::G_BRCOND: {
401-
// TODO: Fold with G_ICMP.
402-
auto Bcc =
403-
MIB.buildInstr(RISCV::BNE, {}, {MI.getOperand(0), Register(RISCV::X0)})
404-
.addMBB(MI.getOperand(1).getMBB());
487+
Register LHS, RHS;
488+
RISCVCC::CondCode CC;
489+
getOperandsForBranch(MI.getOperand(0).getReg(), MRI, CC, LHS, RHS);
490+
491+
auto Bcc = MIB.buildInstr(RISCVCC::getBrCond(CC), {}, {LHS, RHS})
492+
.addMBB(MI.getOperand(1).getMBB());
405493
MI.eraseFromParent();
406494
return constrainSelectedInstRegOperands(*Bcc, TII, TRI, RBI);
407495
}
@@ -719,101 +807,14 @@ bool RISCVInstructionSelector::selectSExtInreg(MachineInstr &MI,
719807
return true;
720808
}
721809

722-
/// Returns the RISCVCC::CondCode that corresponds to the CmpInst::Predicate CC.
723-
/// CC Must be an ICMP Predicate.
724-
static RISCVCC::CondCode getRISCVCCFromICMP(CmpInst::Predicate CC) {
725-
switch (CC) {
726-
default:
727-
llvm_unreachable("Expected ICMP CmpInst::Predicate.");
728-
case CmpInst::Predicate::ICMP_EQ:
729-
return RISCVCC::COND_EQ;
730-
case CmpInst::Predicate::ICMP_NE:
731-
return RISCVCC::COND_NE;
732-
case CmpInst::Predicate::ICMP_ULT:
733-
return RISCVCC::COND_LTU;
734-
case CmpInst::Predicate::ICMP_SLT:
735-
return RISCVCC::COND_LT;
736-
case CmpInst::Predicate::ICMP_UGE:
737-
return RISCVCC::COND_GEU;
738-
case CmpInst::Predicate::ICMP_SGE:
739-
return RISCVCC::COND_GE;
740-
}
741-
}
742-
743-
static void getOperandsForBranch(Register CondReg, MachineIRBuilder &MIB,
744-
MachineRegisterInfo &MRI,
745-
RISCVCC::CondCode &CC, Register &LHS,
746-
Register &RHS) {
747-
// Try to fold an ICmp. If that fails, use a NE compare with X0.
748-
CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE;
749-
if (!mi_match(CondReg, MRI, m_GICmp(m_Pred(Pred), m_Reg(LHS), m_Reg(RHS)))) {
750-
LHS = CondReg;
751-
RHS = RISCV::X0;
752-
CC = RISCVCC::COND_NE;
753-
return;
754-
}
755-
756-
// We found an ICmp, do some canonicalizations.
757-
758-
// Adjust comparisons to use comparison with 0 if possible.
759-
if (auto Constant = getIConstantVRegSExtVal(RHS, MRI)) {
760-
switch (Pred) {
761-
case CmpInst::Predicate::ICMP_SGT:
762-
// Convert X > -1 to X >= 0
763-
if (*Constant == -1) {
764-
CC = RISCVCC::COND_GE;
765-
RHS = RISCV::X0;
766-
return;
767-
}
768-
break;
769-
case CmpInst::Predicate::ICMP_SLT:
770-
// Convert X < 1 to 0 >= X
771-
if (*Constant == 1) {
772-
CC = RISCVCC::COND_GE;
773-
RHS = LHS;
774-
LHS = RISCV::X0;
775-
return;
776-
}
777-
break;
778-
default:
779-
break;
780-
}
781-
}
782-
783-
switch (Pred) {
784-
default:
785-
llvm_unreachable("Expected ICMP CmpInst::Predicate.");
786-
case CmpInst::Predicate::ICMP_EQ:
787-
case CmpInst::Predicate::ICMP_NE:
788-
case CmpInst::Predicate::ICMP_ULT:
789-
case CmpInst::Predicate::ICMP_SLT:
790-
case CmpInst::Predicate::ICMP_UGE:
791-
case CmpInst::Predicate::ICMP_SGE:
792-
// These CCs are supported directly by RISC-V branches.
793-
break;
794-
case CmpInst::Predicate::ICMP_SGT:
795-
case CmpInst::Predicate::ICMP_SLE:
796-
case CmpInst::Predicate::ICMP_UGT:
797-
case CmpInst::Predicate::ICMP_ULE:
798-
// These CCs are not supported directly by RISC-V branches, but changing the
799-
// direction of the CC and swapping LHS and RHS are.
800-
Pred = CmpInst::getSwappedPredicate(Pred);
801-
std::swap(LHS, RHS);
802-
break;
803-
}
804-
805-
CC = getRISCVCCFromICMP(Pred);
806-
return;
807-
}
808-
809810
bool RISCVInstructionSelector::selectSelect(MachineInstr &MI,
810811
MachineIRBuilder &MIB,
811812
MachineRegisterInfo &MRI) const {
812813
auto &SelectMI = cast<GSelect>(MI);
813814

814815
Register LHS, RHS;
815816
RISCVCC::CondCode CC;
816-
getOperandsForBranch(SelectMI.getCondReg(), MIB, MRI, CC, LHS, RHS);
817+
getOperandsForBranch(SelectMI.getCondReg(), MRI, CC, LHS, RHS);
817818

818819
MachineInstr *Result = MIB.buildInstr(RISCV::Select_GPR_Using_CC_GPR)
819820
.addDef(SelectMI.getReg(0))

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -904,25 +904,29 @@ static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
904904
Cond.push_back(LastInst.getOperand(1));
905905
}
906906

907-
const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
907+
unsigned RISCVCC::getBrCond(RISCVCC::CondCode CC) {
908908
switch (CC) {
909909
default:
910910
llvm_unreachable("Unknown condition code!");
911911
case RISCVCC::COND_EQ:
912-
return get(RISCV::BEQ);
912+
return RISCV::BEQ;
913913
case RISCVCC::COND_NE:
914-
return get(RISCV::BNE);
914+
return RISCV::BNE;
915915
case RISCVCC::COND_LT:
916-
return get(RISCV::BLT);
916+
return RISCV::BLT;
917917
case RISCVCC::COND_GE:
918-
return get(RISCV::BGE);
918+
return RISCV::BGE;
919919
case RISCVCC::COND_LTU:
920-
return get(RISCV::BLTU);
920+
return RISCV::BLTU;
921921
case RISCVCC::COND_GEU:
922-
return get(RISCV::BGEU);
922+
return RISCV::BGEU;
923923
}
924924
}
925925

926+
const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
927+
return get(RISCVCC::getBrCond(CC));
928+
}
929+
926930
RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
927931
switch (CC) {
928932
default:

llvm/lib/Target/RISCV/RISCVInstrInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum CondCode {
4343
};
4444

4545
CondCode getOppositeBranchCondition(CondCode);
46+
unsigned getBrCond(CondCode CC);
4647

4748
} // end of namespace RISCVCC
4849

0 commit comments

Comments
 (0)