From 469001000620df176decd093a300db84a06cc78b Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 8 May 2005 00:19:31 +0000 Subject: [PATCH] Refactor some code git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21772 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/Reassociate.cpp | 100 ++++++++++++++------------ 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index 0e55ec1ddf4..970072db5a2 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -63,6 +63,7 @@ namespace { unsigned getRank(Value *V); void RewriteExprTree(BinaryOperator *I, unsigned Idx, std::vector &Ops); + void OptimizeExpression(unsigned Opcode, std::vector &Ops); void LinearizeExprTree(BinaryOperator *I, std::vector &Ops); void LinearizeExpr(BinaryOperator *I); void ReassociateBB(BasicBlock *BB); @@ -343,6 +344,57 @@ static Instruction *ConvertShiftToMul(Instruction *Shl) { return Mul; } +void Reassociate::OptimizeExpression(unsigned Opcode, + std::vector &Ops) { + // Now that we have the linearized expression tree, try to optimize it. + // Start by folding any constants that we found. + FoldConstants: + if (Ops.size() == 1) return; + + if (Constant *V1 = dyn_cast(Ops[Ops.size()-2].Op)) + if (Constant *V2 = dyn_cast(Ops.back().Op)) { + Ops.pop_back(); + Ops.back().Op = ConstantExpr::get(Opcode, V1, V2); + goto FoldConstants; + } + + // Check for destructive annihilation due to a constant being used. + if (ConstantIntegral *CstVal = dyn_cast(Ops.back().Op)) + switch (Opcode) { + default: break; + case Instruction::And: + if (CstVal->isNullValue()) { // ... & 0 -> 0 + Ops[0].Op = CstVal; + Ops.erase(Ops.begin()+1, Ops.end()); + } else if (CstVal->isAllOnesValue()) { // ... & -1 -> ... + Ops.pop_back(); + } + break; + case Instruction::Mul: + if (CstVal->isNullValue()) { // ... * 0 -> 0 + Ops[0].Op = CstVal; + Ops.erase(Ops.begin()+1, Ops.end()); + } else if (cast(CstVal)->getRawValue() == 1) { + Ops.pop_back(); // ... * 1 -> ... + } + break; + case Instruction::Or: + if (CstVal->isAllOnesValue()) { // ... | -1 -> -1 + Ops[0].Op = CstVal; + Ops.erase(Ops.begin()+1, Ops.end()); + } + // FALLTHROUGH! + case Instruction::Add: + case Instruction::Xor: + if (CstVal->isNullValue()) // ... [|^+] 0 -> ... + Ops.pop_back(); + break; + } + + // Handle destructive annihilation do to identities between elements in the + // argument list here. +} + /// ReassociateBB - Inspect all of the instructions in this basic block, /// reassociating them as we go. @@ -383,51 +435,9 @@ void Reassociate::ReassociateBB(BasicBlock *BB) { // the vector. std::stable_sort(Ops.begin(), Ops.end()); - // Now that we have the linearized expression tree, try to optimize it. - // Start by folding any constants that we found. - FoldConstants: - if (Ops.size() > 1) - if (Constant *V1 = dyn_cast(Ops[Ops.size()-2].Op)) - if (Constant *V2 = dyn_cast(Ops.back().Op)) { - Ops.pop_back(); - Ops.back().Op = ConstantExpr::get(I->getOpcode(), V1, V2); - goto FoldConstants; - } - - // Check for destructive annihilation due to a constant being used. - if (Ops.size() != 1) { // Nothing to annihilate? - if (ConstantIntegral *CstVal = dyn_cast(Ops.back().Op)) - switch (I->getOpcode()) { - default: break; - case Instruction::And: - if (CstVal->isNullValue()) { // ... & 0 -> 0 - Ops[0].Op = CstVal; - Ops.erase(Ops.begin()+1, Ops.end()); - } else if (CstVal->isAllOnesValue()) { // ... & -1 -> ... - Ops.pop_back(); - } - break; - case Instruction::Mul: - if (CstVal->isNullValue()) { // ... * 0 -> 0 - Ops[0].Op = CstVal; - Ops.erase(Ops.begin()+1, Ops.end()); - } else if (cast(CstVal)->getRawValue() == 1) { - Ops.pop_back(); // ... * 1 -> ... - } - break; - case Instruction::Or: - if (CstVal->isAllOnesValue()) { // ... | -1 -> -1 - Ops[0].Op = CstVal; - Ops.erase(Ops.begin()+1, Ops.end()); - } - // FALLTHROUGH! - case Instruction::Add: - case Instruction::Xor: - if (CstVal->isNullValue()) // ... [|^+] 0 -> ... - Ops.pop_back(); - break; - } - } + // OptimizeExpression - Now that we have the expression tree in a convenient + // sorted form, optimize it globally if possible. + OptimizeExpression(I->getOpcode(), Ops); if (Ops.size() == 1) { // This expression tree simplified to something that isn't a tree,