Method.h no longer includes BasicBlock.h
[oota-llvm.git] / lib / Transforms / Scalar / InstructionCombining.cpp
1 //===- InstructionCombining.cpp - Combine multiple instructions -------------=//
2 //
3 // InstructionCombining - Combine instructions to form fewer, simple
4 //   instructions.  This pass does not modify the CFG, and has a tendancy to
5 //   make instructions dead, so a subsequent DCE pass is useful.
6 //
7 // This pass combines things like:
8 //    %Y = add int 1, %X
9 //    %Z = add int 1, %Y
10 // into:
11 //    %Z = add int 2, %X
12 //
13 // This is a simple worklist driven algorithm.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Scalar/InstructionCombining.h"
18 #include "llvm/Transforms/Scalar/ConstantHandling.h"
19 #include "llvm/Method.h"
20 #include "llvm/iMemory.h"
21 #include "llvm/Support/InstIterator.h"
22 #include "../TransformInternals.h"
23
24 static Instruction *CombineBinOp(BinaryOperator *I) {
25   bool Changed = false;
26
27   // First thing we do is make sure that this instruction has a constant on the
28   // right hand side if it has any constant arguments.
29   //
30   if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1)))
31     if (!I->swapOperands())
32       Changed = true;
33
34   bool LocalChange = true;
35   while (LocalChange) {
36     LocalChange = false;
37     Value *Op1 = I->getOperand(0);
38     if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) {
39       if (I->getOpcode() == Instruction::Add) {
40         if (Instruction *IOp1 = dyn_cast<Instruction>(Op1)) {
41           if (IOp1->getOpcode() == Instruction::Add &&
42               isa<Constant>(IOp1->getOperand(1))) {
43             // Fold:
44             //    %Y = add int %X, 1
45             //    %Z = add int %Y, 1
46             // into:
47             //    %Z = add int %X, 2
48             //   
49             // Constant fold both constants...
50             Constant *Val = *Op2 + *cast<Constant>(IOp1->getOperand(1));
51             
52             if (Val) {
53               I->setOperand(0, IOp1->getOperand(0));
54               I->setOperand(1, Val);
55               LocalChange = true;
56             }
57           }
58           
59         }
60       }
61     }
62     Changed |= LocalChange;
63   }
64
65   if (!Changed) return 0;
66   return I;
67 }
68
69 // Combine Indices - If the source pointer to this mem access instruction is a
70 // getelementptr instruction, combine the indices of the GEP into this
71 // instruction
72 //
73 static Instruction *CombineIndicies(MemAccessInst *MAI) {
74   GetElementPtrInst *Src =
75     dyn_cast<GetElementPtrInst>(MAI->getPointerOperand());
76   if (!Src) return 0;
77
78   std::vector<Value *> Indices;
79   
80   // Only special case we have to watch out for is pointer arithmetic on the
81   // 0th index of MAI. 
82   unsigned FirstIdx = MAI->getFirstIndexOperandNumber();
83   if (FirstIdx == MAI->getNumOperands() || 
84       (FirstIdx == MAI->getNumOperands()-1 &&
85        MAI->getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) { 
86     // Replace the index list on this MAI with the index on the getelementptr
87     Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
88   } else if (*MAI->idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) { 
89     // Otherwise we can do the fold if the first index of the GEP is a zero
90     Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
91     Indices.insert(Indices.end(), MAI->idx_begin()+1, MAI->idx_end());
92   }
93
94   if (Indices.empty()) return 0;  // Can't do the fold?
95
96   switch (MAI->getOpcode()) {
97   case Instruction::GetElementPtr:
98     return new GetElementPtrInst(Src->getOperand(0), Indices, MAI->getName());
99   case Instruction::Load:
100     return new LoadInst(Src->getOperand(0), Indices, MAI->getName());
101   case Instruction::Store:
102     return new StoreInst(MAI->getOperand(0), Src->getOperand(0),
103                          Indices, MAI->getName());
104   default:
105     assert(0 && "Unknown memaccessinst!");
106     break;
107   }
108   abort();
109   return 0;
110 }
111
112 bool InstructionCombining::CombineInstruction(Instruction *I) {
113   Instruction *Result = 0;
114   if (BinaryOperator *BOP = dyn_cast<BinaryOperator>(I))
115     Result = CombineBinOp(BOP);
116   else if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(I))
117     Result = CombineIndicies(MAI);
118
119   if (!Result) return false;
120   if (Result == I) return true;
121
122   // If we get to here, we are to replace I with Result.
123   ReplaceInstWithInst(I, Result);
124   return true;
125 }
126
127
128 bool InstructionCombining::doit(Method *M) {
129   // Start the worklist out with all of the instructions in the method in it.
130   std::vector<Instruction*> WorkList(inst_begin(M), inst_end(M));
131
132   while (!WorkList.empty()) {
133     Instruction *I = WorkList.back();  // Get an instruction from the worklist
134     WorkList.pop_back();
135
136     // Now that we have an instruction, try combining it to simplify it...
137     if (CombineInstruction(I)) {
138       // The instruction was simplified, add all users of the instruction to
139       // the work lists because they might get more simplified now...
140       //
141       for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
142            UI != UE; ++UI)
143         if (Instruction *User = dyn_cast<Instruction>(*UI))
144           WorkList.push_back(User);
145     }
146   }
147
148   return false;
149 }