1 //===- InstructionCombining.cpp - Combine multiple instructions -------------=//
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.
7 // This pass combines things like:
13 // This is a simple worklist driven algorithm.
15 //===----------------------------------------------------------------------===//
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"
24 static Instruction *CombineBinOp(BinaryOperator *I) {
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.
30 if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1)))
31 if (!I->swapOperands())
34 bool LocalChange = true;
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))) {
49 // Constant fold both constants...
50 Constant *Val = *Op2 + *cast<Constant>(IOp1->getOperand(1));
53 I->setOperand(0, IOp1->getOperand(0));
54 I->setOperand(1, Val);
62 Changed |= LocalChange;
65 if (!Changed) return 0;
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
73 static Instruction *CombineIndicies(MemAccessInst *MAI) {
74 GetElementPtrInst *Src =
75 dyn_cast<GetElementPtrInst>(MAI->getPointerOperand());
78 std::vector<Value *> Indices;
80 // Only special case we have to watch out for is pointer arithmetic on the
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());
94 if (Indices.empty()) return 0; // Can't do the fold?
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());
105 assert(0 && "Unknown memaccessinst!");
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);
119 if (!Result) return false;
120 if (Result == I) return true;
122 // If we get to here, we are to replace I with Result.
123 ReplaceInstWithInst(I, Result);
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));
132 while (!WorkList.empty()) {
133 Instruction *I = WorkList.back(); // Get an instruction from the worklist
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...
141 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
143 if (Instruction *User = dyn_cast<Instruction>(*UI))
144 WorkList.push_back(User);