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