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