summaryrefslogtreecommitdiff
path: root/lib/Transforms/Scalar/Reassociate.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2005-05-08 00:19:31 +0000
committerChris Lattner <sabre@nondot.org>2005-05-08 00:19:31 +0000
commit469001000620df176decd093a300db84a06cc78b (patch)
tree46afe383815efda1cd4f97f4524b8d010f98b39f /lib/Transforms/Scalar/Reassociate.cpp
parentcc8a2b98f28c10d93f45489b8c6f0c8b8205bb3b (diff)
downloadllvm-469001000620df176decd093a300db84a06cc78b.tar.gz
llvm-469001000620df176decd093a300db84a06cc78b.tar.bz2
llvm-469001000620df176decd093a300db84a06cc78b.tar.xz
Refactor some code
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21772 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar/Reassociate.cpp')
-rw-r--r--lib/Transforms/Scalar/Reassociate.cpp100
1 files changed, 55 insertions, 45 deletions
diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp
index 0e55ec1ddf..970072db5a 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<ValueEntry> &Ops);
+ void OptimizeExpression(unsigned Opcode, std::vector<ValueEntry> &Ops);
void LinearizeExprTree(BinaryOperator *I, std::vector<ValueEntry> &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<ValueEntry> &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<Constant>(Ops[Ops.size()-2].Op))
+ if (Constant *V2 = dyn_cast<Constant>(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<ConstantIntegral>(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<ConstantInt>(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<Constant>(Ops[Ops.size()-2].Op))
- if (Constant *V2 = dyn_cast<Constant>(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<ConstantIntegral>(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<ConstantInt>(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,