[cfe-commits] r42648 - in /cfe/trunk: CodeGen/CGStmt.cpp test/CodeGen/switch.c
Devang Patel
dpatel at apple.com
Fri Oct 5 13:54:08 PDT 2007
Author: dpatel
Date: Fri Oct 5 15:54:07 2007
New Revision: 42648
URL: http://llvm.org/viewvc/llvm-project?rev=42648&view=rev
Log:
Support case statement ranges.
Modified:
cfe/trunk/CodeGen/CGStmt.cpp
cfe/trunk/test/CodeGen/switch.c
Modified: cfe/trunk/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGStmt.cpp?rev=42648&r1=42647&r2=42648&view=diff
==============================================================================
--- cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/trunk/CodeGen/CGStmt.cpp Fri Oct 5 15:54:07 2007
@@ -356,9 +356,21 @@
void CodeGenFunction::EmitCaseStmt(const CaseStmt &S) {
StartBlock("sw.bb");
- llvm::Value *V = EmitScalarExpr(S.getLHS());
- SwitchInsn->addCase(cast<llvm::ConstantInt>(V), Builder.GetInsertBlock());
- assert (!S.getRHS() && "Case statement range is not yet supported");
+ llvm::BasicBlock *CaseDest = Builder.GetInsertBlock();
+ llvm::ConstantInt *LV = cast<llvm::ConstantInt>(EmitScalarExpr(S.getLHS()));
+ SwitchInsn->addCase(LV, CaseDest);
+ if (const Expr *R = S.getRHS()) {
+ llvm::ConstantInt *RV = cast<llvm::ConstantInt>(EmitScalarExpr(R));
+ llvm::APInt LHS = LV->getValue();
+ llvm::APInt RHS = RV->getValue();
+ LHS++;
+ while (LHS != RHS) {
+ SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
+ LHS++;
+ }
+ SwitchInsn->addCase(llvm::ConstantInt::get(LHS), CaseDest);
+ }
+
EmitStmt(S.getSubStmt());
}
Modified: cfe/trunk/test/CodeGen/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/switch.c?rev=42648&r1=42647&r2=42648&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/switch.c (original)
+++ cfe/trunk/test/CodeGen/switch.c Fri Oct 5 15:54:07 2007
@@ -15,3 +15,18 @@
}
+int foo2(int i) {
+ int j = 0;
+ switch (i) {
+ case 1 :
+ j = 2; break;
+ case 2 ... 10:
+ j = 3; break;
+ default:
+ j = 42; break;
+ }
+ j = j + 1;
+ return j;
+}
+
+
More information about the cfe-commits
mailing list