From 46238a6150d68bc54b41efe8d00a71690ab33c4d Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 3 Jul 2004 00:26:11 +0000 Subject: [PATCH] Implement add.ll:test22, a common case in MSIL files git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14587 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/InstructionCombining.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index bf571194864..c0d0591c9b3 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -122,6 +122,7 @@ namespace { Instruction *visitFreeInst(FreeInst &FI); Instruction *visitLoadInst(LoadInst &LI); Instruction *visitBranchInst(BranchInst &BI); + Instruction *visitSwitchInst(SwitchInst &SI); // visitInstruction - Specify what to return for unhandled instructions... Instruction *visitInstruction(Instruction &I) { return 0; } @@ -3004,6 +3005,23 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { return 0; } +Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { + Value *Cond = SI.getCondition(); + if (Instruction *I = dyn_cast(Cond)) { + if (I->getOpcode() == Instruction::Add) + if (ConstantInt *AddRHS = dyn_cast(I->getOperand(1))) { + // change 'switch (X+4) case 1:' into 'switch (X) case -3' + for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) + SI.setOperand(i, ConstantExpr::getSub(cast(SI.getOperand(i)), + AddRHS)); + SI.setOperand(0, I->getOperand(0)); + WorkList.push_back(I); + return &SI; + } + } + return 0; +} + void InstCombiner::removeFromWorkList(Instruction *I) { WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I), -- 2.34.1