blob: 230b55365710bedcecb33e5301fd4bc930ca9b8f [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
Chris Lattner3f5b8772002-05-06 16:14:14 +00005// make instructions dead, so a subsequent DIE pass is useful. This pass is
6// where algebraic simplification happens.
Chris Lattner8a2a3112001-12-14 16:52:21 +00007//
8// This pass combines things like:
9// %Y = add int 1, %X
10// %Z = add int 1, %Y
11// into:
12// %Z = add int 2, %X
13//
14// This is a simple worklist driven algorithm.
15//
16//===----------------------------------------------------------------------===//
17
Chris Lattner022103b2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattner968ddc92002-04-08 20:18:09 +000020#include "llvm/ConstantHandling.h"
Chris Lattner8a2a3112001-12-14 16:52:21 +000021#include "llvm/iMemory.h"
Chris Lattner8d70cd92002-04-15 19:45:29 +000022#include "llvm/iOther.h"
Chris Lattner473945d2002-05-06 18:06:38 +000023#include "llvm/iPHINode.h"
Chris Lattnerdd841ae2002-04-18 17:39:14 +000024#include "llvm/iOperators.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000025#include "llvm/Pass.h"
Chris Lattner221d6882002-02-12 21:07:25 +000026#include "llvm/Support/InstIterator.h"
Chris Lattnerdd841ae2002-04-18 17:39:14 +000027#include "llvm/Support/InstVisitor.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000028#include "Support/StatisticReporter.h"
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +000029#include <algorithm>
Chris Lattner8a2a3112001-12-14 16:52:21 +000030
Chris Lattner3dec1f22002-05-10 15:38:35 +000031static Statistic<> NumCombined("instcombine\t- Number of insts combined");
Chris Lattner8a2a3112001-12-14 16:52:21 +000032
Chris Lattnerdd841ae2002-04-18 17:39:14 +000033namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000034 class InstCombiner : public FunctionPass,
Chris Lattnerdd841ae2002-04-18 17:39:14 +000035 public InstVisitor<InstCombiner, Instruction*> {
36 // Worklist of all of the instructions that need to be simplified.
37 std::vector<Instruction*> WorkList;
38
Chris Lattner7e708292002-06-25 16:13:24 +000039 void AddUsesToWorkList(Instruction &I) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +000040 // The instruction was simplified, add all users of the instruction to
41 // the work lists because they might get more simplified now...
42 //
Chris Lattner7e708292002-06-25 16:13:24 +000043 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerdd841ae2002-04-18 17:39:14 +000044 UI != UE; ++UI)
45 WorkList.push_back(cast<Instruction>(*UI));
46 }
47
48 public:
Chris Lattner7e708292002-06-25 16:13:24 +000049 virtual bool runOnFunction(Function &F);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000050
Chris Lattner97e52e42002-04-28 21:27:06 +000051 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.preservesCFG();
53 }
54
Chris Lattnerdd841ae2002-04-18 17:39:14 +000055 // Visitation implementation - Implement instruction combining for different
56 // instruction types. The semantics are as follows:
57 // Return Value:
58 // null - No change was made
59 // I - Change was made, I is still valid
60 // otherwise - Change was made, replace I with returned instruction
61 //
Chris Lattner7e708292002-06-25 16:13:24 +000062 Instruction *visitNot(UnaryOperator &I);
63 Instruction *visitAdd(BinaryOperator &I);
64 Instruction *visitSub(BinaryOperator &I);
65 Instruction *visitMul(BinaryOperator &I);
66 Instruction *visitDiv(BinaryOperator &I);
67 Instruction *visitRem(BinaryOperator &I);
68 Instruction *visitAnd(BinaryOperator &I);
69 Instruction *visitOr (BinaryOperator &I);
70 Instruction *visitXor(BinaryOperator &I);
71 Instruction *visitSetCondInst(BinaryOperator &I);
72 Instruction *visitShiftInst(Instruction &I);
73 Instruction *visitCastInst(CastInst &CI);
74 Instruction *visitPHINode(PHINode &PN);
75 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
76 Instruction *visitMemAccessInst(MemAccessInst &MAI);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000077
78 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner7e708292002-06-25 16:13:24 +000079 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattnerdd841ae2002-04-18 17:39:14 +000080 };
Chris Lattnerf6293092002-07-23 18:06:35 +000081
Chris Lattnera6275cc2002-07-26 21:12:46 +000082 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattnerdd841ae2002-04-18 17:39:14 +000083}
84
85
Chris Lattner7e708292002-06-25 16:13:24 +000086Instruction *InstCombiner::visitNot(UnaryOperator &I) {
87 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattnered3a5502002-05-06 17:03:21 +000088
89 // not (not X) = X
Chris Lattner7e708292002-06-25 16:13:24 +000090 if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(0)))
Chris Lattnered3a5502002-05-06 17:03:21 +000091 if (Op->getOpcode() == Instruction::Not) {
92 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +000093 I.replaceAllUsesWith(Op->getOperand(0));
94 return &I;
Chris Lattnered3a5502002-05-06 17:03:21 +000095 }
96 return 0;
97}
98
Chris Lattnerdd841ae2002-04-18 17:39:14 +000099
100// Make sure that this instruction has a constant on the right hand side if it
101// has any constant arguments. If not, fix it an return true.
102//
Chris Lattner7e708292002-06-25 16:13:24 +0000103static bool SimplifyBinOp(BinaryOperator &I) {
104 if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
105 return !I.swapOperands();
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000106 return false;
107}
Chris Lattner8a2a3112001-12-14 16:52:21 +0000108
Chris Lattnerb35dde12002-05-06 16:49:18 +0000109// dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
110// instruction if the LHS is a constant zero (which is the 'negate' form).
111//
112static inline Value *dyn_castNegInst(Value *V) {
113 Instruction *I = dyn_cast<Instruction>(V);
114 if (!I || I->getOpcode() != Instruction::Sub) return 0;
115
116 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
117 return I->getOperand(1);
118 return 0;
119}
120
Chris Lattner7e708292002-06-25 16:13:24 +0000121Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
122 if (I.use_empty()) return 0; // Don't fix dead add instructions...
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000123 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000124 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000125
126 // Eliminate 'add int %X, 0'
Chris Lattner7e708292002-06-25 16:13:24 +0000127 if (RHS == Constant::getNullValue(I.getType())) {
Chris Lattnerb35dde12002-05-06 16:49:18 +0000128 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000129 I.replaceAllUsesWith(LHS);
130 return &I;
Chris Lattnerb35dde12002-05-06 16:49:18 +0000131 }
132
Chris Lattner5c4afb92002-05-08 22:46:53 +0000133 // -A + B --> B - A
Chris Lattnerb35dde12002-05-06 16:49:18 +0000134 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner5c4afb92002-05-08 22:46:53 +0000135 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000136
137 // A + -B --> A - B
138 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner5c4afb92002-05-08 22:46:53 +0000139 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000140
141 // Simplify add instructions with a constant RHS...
Chris Lattnerb35dde12002-05-06 16:49:18 +0000142 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
143 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
144 if (ILHS->getOpcode() == Instruction::Add &&
145 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000146 // Fold:
147 // %Y = add int %X, 1
148 // %Z = add int %Y, 1
149 // into:
150 // %Z = add int %X, 2
151 //
Chris Lattnerb35dde12002-05-06 16:49:18 +0000152 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
Chris Lattner7e708292002-06-25 16:13:24 +0000153 I.setOperand(0, ILHS->getOperand(0));
154 I.setOperand(1, Val);
155 return &I;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000156 }
Chris Lattner8a2a3112001-12-14 16:52:21 +0000157 }
158 }
Chris Lattner8a2a3112001-12-14 16:52:21 +0000159 }
160
Chris Lattner7e708292002-06-25 16:13:24 +0000161 return Changed ? &I : 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000162}
163
Chris Lattner7e708292002-06-25 16:13:24 +0000164Instruction *InstCombiner::visitSub(BinaryOperator &I) {
165 if (I.use_empty()) return 0; // Don't fix dead add instructions...
166 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000167
168 if (Op0 == Op1) { // sub X, X -> 0
169 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000170 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
171 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000172 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000173
174 // If this is a subtract instruction with a constant RHS, convert it to an add
175 // instruction of a negative constant
176 //
Chris Lattner3f5b8772002-05-06 16:14:14 +0000177 if (Constant *Op2 = dyn_cast<Constant>(Op1))
Chris Lattner7e708292002-06-25 16:13:24 +0000178 if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
179 return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000180
Chris Lattner5c4afb92002-05-08 22:46:53 +0000181 // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
182 if (Value *V = dyn_castNegInst(Op1))
183 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000184
Chris Lattner40371712002-05-09 01:29:19 +0000185 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
186 // not used by anyone else...
187 //
188 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
Chris Lattner86c25fd2002-05-14 16:44:07 +0000189 if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
Chris Lattner40371712002-05-09 01:29:19 +0000190 // Swap the two operands of the subexpr...
191 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
192 Op1I->setOperand(0, IIOp1);
193 Op1I->setOperand(1, IIOp0);
194
195 // Create the new top level add instruction...
196 return BinaryOperator::create(Instruction::Add, Op0, Op1);
197 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000198 return 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000199}
200
Chris Lattner7e708292002-06-25 16:13:24 +0000201Instruction *InstCombiner::visitMul(BinaryOperator &I) {
202 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000203 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000204 Value *Op1 = I.getOperand(0);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000205
206 // Simplify add instructions with a constant RHS...
Chris Lattner7e708292002-06-25 16:13:24 +0000207 if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
208 if (I.getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000209 // Eliminate 'mul int %X, 1'
210 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000211 I.replaceAllUsesWith(Op1);
212 return &I;
Chris Lattner6c1ce212002-04-29 22:24:47 +0000213
Chris Lattner7e708292002-06-25 16:13:24 +0000214 } else if (I.getType()->isIntegral() &&
Chris Lattner6c1ce212002-04-29 22:24:47 +0000215 cast<ConstantInt>(Op2)->equalsInt(2)) {
216 // Convert 'mul int %X, 2' to 'add int %X, %X'
Chris Lattner7e708292002-06-25 16:13:24 +0000217 return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
Chris Lattner6c1ce212002-04-29 22:24:47 +0000218
219 } else if (Op2->isNullValue()) {
220 // Eliminate 'mul int %X, 0'
Chris Lattner7e708292002-06-25 16:13:24 +0000221 AddUsesToWorkList(I); // Add all modified instrs to worklist
222 I.replaceAllUsesWith(Op2); // Set this value to zero directly
223 return &I;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000224 }
225 }
226
Chris Lattner7e708292002-06-25 16:13:24 +0000227 return Changed ? &I : 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000228}
229
230
Chris Lattner7e708292002-06-25 16:13:24 +0000231Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
232 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000233
234 // div X, 1 == X
Chris Lattner7e708292002-06-25 16:13:24 +0000235 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
Chris Lattner3f5b8772002-05-06 16:14:14 +0000236 if (RHS->equalsInt(1)) {
237 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000238 I.replaceAllUsesWith(I.getOperand(0));
239 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000240 }
241 return 0;
242}
243
244
Chris Lattner7e708292002-06-25 16:13:24 +0000245Instruction *InstCombiner::visitRem(BinaryOperator &I) {
246 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000247
248 // rem X, 1 == 0
Chris Lattner7e708292002-06-25 16:13:24 +0000249 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
Chris Lattner3f5b8772002-05-06 16:14:14 +0000250 if (RHS->equalsInt(1)) {
Chris Lattner7e708292002-06-25 16:13:24 +0000251 AddUsesToWorkList(I); // Add all modified instrs to worklist
252 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
253 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000254 }
255 return 0;
256}
257
258static Constant *getMaxValue(const Type *Ty) {
259 assert(Ty == Type::BoolTy || Ty->isIntegral());
260 if (Ty == Type::BoolTy)
261 return ConstantBool::True;
262
Chris Lattner3f5b8772002-05-06 16:14:14 +0000263 if (Ty->isSigned())
Chris Lattnerd6619372002-05-06 18:54:59 +0000264 return ConstantSInt::get(Ty, -1);
265 else if (Ty->isUnsigned()) {
266 // Calculate -1 casted to the right type...
267 unsigned TypeBits = Ty->getPrimitiveSize()*8;
268 uint64_t Val = (uint64_t)-1LL; // All ones
269 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000270 return ConstantUInt::get(Ty, Val);
Chris Lattnerd6619372002-05-06 18:54:59 +0000271 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000272 return 0;
273}
274
275
Chris Lattner7e708292002-06-25 16:13:24 +0000276Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
277 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000278 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000279 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000280
281 // and X, X = X and X, 0 == 0
Chris Lattner7e708292002-06-25 16:13:24 +0000282 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
283 AddUsesToWorkList(I); // Add all modified instrs to worklist
284 I.replaceAllUsesWith(Op1);
285 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000286 }
287
288 // and X, -1 == X
289 if (Constant *RHS = dyn_cast<Constant>(Op1))
Chris Lattner7e708292002-06-25 16:13:24 +0000290 if (RHS == getMaxValue(I.getType())) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000291 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000292 I.replaceAllUsesWith(Op0);
293 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000294 }
295
Chris Lattner7e708292002-06-25 16:13:24 +0000296 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000297}
298
299
300
Chris Lattner7e708292002-06-25 16:13:24 +0000301Instruction *InstCombiner::visitOr(BinaryOperator &I) {
302 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000303 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000304 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000305
306 // or X, X = X or X, 0 == X
Chris Lattner7e708292002-06-25 16:13:24 +0000307 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000308 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000309 I.replaceAllUsesWith(Op0);
310 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000311 }
312
313 // or X, -1 == -1
314 if (Constant *RHS = dyn_cast<Constant>(Op1))
Chris Lattner7e708292002-06-25 16:13:24 +0000315 if (RHS == getMaxValue(I.getType())) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000316 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000317 I.replaceAllUsesWith(Op1);
318 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000319 }
320
Chris Lattner7e708292002-06-25 16:13:24 +0000321 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000322}
323
324
325
Chris Lattner7e708292002-06-25 16:13:24 +0000326Instruction *InstCombiner::visitXor(BinaryOperator &I) {
327 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000328 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000329 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000330
331 // xor X, X = 0
332 if (Op0 == Op1) {
333 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000334 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
335 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000336 }
337
338 // xor X, 0 == X
Chris Lattner7e708292002-06-25 16:13:24 +0000339 if (Op1 == Constant::getNullValue(I.getType())) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000340 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000341 I.replaceAllUsesWith(Op0);
342 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000343 }
344
Chris Lattner7e708292002-06-25 16:13:24 +0000345 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000346}
347
Chris Lattner53a5b572002-05-09 20:11:54 +0000348// isTrueWhenEqual - Return true if the specified setcondinst instruction is
349// true when both operands are equal...
350//
Chris Lattner7e708292002-06-25 16:13:24 +0000351static bool isTrueWhenEqual(Instruction &I) {
352 return I.getOpcode() == Instruction::SetEQ ||
353 I.getOpcode() == Instruction::SetGE ||
354 I.getOpcode() == Instruction::SetLE;
Chris Lattner53a5b572002-05-09 20:11:54 +0000355}
356
Chris Lattner7e708292002-06-25 16:13:24 +0000357Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
358 if (I.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner3f5b8772002-05-06 16:14:14 +0000359 bool Changed = SimplifyBinOp(I);
360
361 // setcc X, X
Chris Lattner7e708292002-06-25 16:13:24 +0000362 if (I.getOperand(0) == I.getOperand(1)) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000363 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000364 I.replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I)));
365 return &I;
Chris Lattner53a5b572002-05-09 20:11:54 +0000366 }
367
368 // setcc <global*>, 0 - Global value addresses are never null!
Chris Lattner7e708292002-06-25 16:13:24 +0000369 if (isa<GlobalValue>(I.getOperand(0)) &&
370 isa<ConstantPointerNull>(I.getOperand(1))) {
Chris Lattner53a5b572002-05-09 20:11:54 +0000371 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000372 I.replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I)));
373 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000374 }
375
Chris Lattner7e708292002-06-25 16:13:24 +0000376 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000377}
378
379
380
Chris Lattner7e708292002-06-25 16:13:24 +0000381Instruction *InstCombiner::visitShiftInst(Instruction &I) {
382 if (I.use_empty()) return 0; // Don't fix dead instructions...
383 assert(I.getOperand(1)->getType() == Type::UByteTy);
384 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000385
386 // shl X, 0 == X and shr X, 0 == X
387 // shl 0, X == 0 and shr 0, X == 0
388 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
389 Op0 == Constant::getNullValue(Op0->getType())) {
390 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000391 I.replaceAllUsesWith(Op0);
392 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000393 }
394
395 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
396 // a signed value.
397 //
398 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
399 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
400 if (CUI->getValue() >= TypeBits &&
Chris Lattner7e708292002-06-25 16:13:24 +0000401 !(Op0->getType()->isSigned() && I.getOpcode() == Instruction::Shr)) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000402 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000403 I.replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
404 return &I;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000405 }
406 }
407 return 0;
408}
409
410
Chris Lattnera1be5662002-05-02 17:06:02 +0000411// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
412// instruction.
413//
Chris Lattner7e708292002-06-25 16:13:24 +0000414static inline bool isEliminableCastOfCast(const CastInst &CI,
Chris Lattnera1be5662002-05-02 17:06:02 +0000415 const CastInst *CSrc) {
Chris Lattner7e708292002-06-25 16:13:24 +0000416 assert(CI.getOperand(0) == CSrc);
Chris Lattnera1be5662002-05-02 17:06:02 +0000417 const Type *SrcTy = CSrc->getOperand(0)->getType();
418 const Type *MidTy = CSrc->getType();
Chris Lattner7e708292002-06-25 16:13:24 +0000419 const Type *DstTy = CI.getType();
Chris Lattnera1be5662002-05-02 17:06:02 +0000420
421 // It is legal to eliminate the instruction if casting A->B->A
422 if (SrcTy == DstTy) return true;
423
424 // Allow free casting and conversion of sizes as long as the sign doesn't
425 // change...
426 if (SrcTy->isSigned() == MidTy->isSigned() &&
427 MidTy->isSigned() == DstTy->isSigned())
428 return true;
429
430 // Otherwise, we cannot succeed. Specifically we do not want to allow things
431 // like: short -> ushort -> uint, because this can create wrong results if
432 // the input short is negative!
433 //
434 return false;
435}
436
437
438// CastInst simplification
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000439//
Chris Lattner7e708292002-06-25 16:13:24 +0000440Instruction *InstCombiner::visitCastInst(CastInst &CI) {
441 if (CI.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner473945d2002-05-06 18:06:38 +0000442
Chris Lattnera1be5662002-05-02 17:06:02 +0000443 // If the user is casting a value to the same type, eliminate this cast
444 // instruction...
Chris Lattner7e708292002-06-25 16:13:24 +0000445 if (CI.getType() == CI.getOperand(0)->getType() && !CI.use_empty()) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000446 AddUsesToWorkList(CI); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000447 CI.replaceAllUsesWith(CI.getOperand(0));
448 return &CI;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000449 }
Chris Lattnera1be5662002-05-02 17:06:02 +0000450
451
452 // If casting the result of another cast instruction, try to eliminate this
453 // one!
454 //
Chris Lattner7e708292002-06-25 16:13:24 +0000455 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0)))
Chris Lattnera1be5662002-05-02 17:06:02 +0000456 if (isEliminableCastOfCast(CI, CSrc)) {
457 // This instruction now refers directly to the cast's src operand. This
458 // has a good chance of making CSrc dead.
Chris Lattner7e708292002-06-25 16:13:24 +0000459 CI.setOperand(0, CSrc->getOperand(0));
460 return &CI;
Chris Lattnera1be5662002-05-02 17:06:02 +0000461 }
462
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000463 return 0;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000464}
465
Chris Lattnera1be5662002-05-02 17:06:02 +0000466
Chris Lattner473945d2002-05-06 18:06:38 +0000467// PHINode simplification
468//
Chris Lattner7e708292002-06-25 16:13:24 +0000469Instruction *InstCombiner::visitPHINode(PHINode &PN) {
470 if (PN.use_empty()) return 0; // Don't fix dead instructions...
Chris Lattner473945d2002-05-06 18:06:38 +0000471
472 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattner7e708292002-06-25 16:13:24 +0000473 if (PN.getNumIncomingValues() == 1) {
Chris Lattner473945d2002-05-06 18:06:38 +0000474 AddUsesToWorkList(PN); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000475 PN.replaceAllUsesWith(PN.getIncomingValue(0));
476 return &PN;
Chris Lattner473945d2002-05-06 18:06:38 +0000477 }
478
479 return 0;
480}
481
Chris Lattnera1be5662002-05-02 17:06:02 +0000482
Chris Lattner7e708292002-06-25 16:13:24 +0000483Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnera1be5662002-05-02 17:06:02 +0000484 // Is it getelementptr %P, uint 0
Chris Lattner7e708292002-06-25 16:13:24 +0000485 // If so, eliminate the noop.
486 if (GEP.getNumOperands() == 2 && !GEP.use_empty() &&
487 GEP.getOperand(1) == Constant::getNullValue(Type::UIntTy)) {
Chris Lattnera1be5662002-05-02 17:06:02 +0000488 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
Chris Lattner7e708292002-06-25 16:13:24 +0000489 GEP.replaceAllUsesWith(GEP.getOperand(0));
490 return &GEP;
Chris Lattnera1be5662002-05-02 17:06:02 +0000491 }
492
493 return visitMemAccessInst(GEP);
494}
495
496
Chris Lattner8a2a3112001-12-14 16:52:21 +0000497// Combine Indices - If the source pointer to this mem access instruction is a
498// getelementptr instruction, combine the indices of the GEP into this
499// instruction
500//
Chris Lattner7e708292002-06-25 16:13:24 +0000501Instruction *InstCombiner::visitMemAccessInst(MemAccessInst &MAI) {
Chris Lattner43199a02002-07-10 22:37:17 +0000502 return 0; // DISABLE FOLDING. GEP is now the only MAI!
503
Chris Lattner8a2a3112001-12-14 16:52:21 +0000504 GetElementPtrInst *Src =
Chris Lattner7e708292002-06-25 16:13:24 +0000505 dyn_cast<GetElementPtrInst>(MAI.getPointerOperand());
Chris Lattner8a2a3112001-12-14 16:52:21 +0000506 if (!Src) return 0;
507
Chris Lattner697954c2002-01-20 22:54:45 +0000508 std::vector<Value *> Indices;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000509
510 // Only special case we have to watch out for is pointer arithmetic on the
511 // 0th index of MAI.
Chris Lattner7e708292002-06-25 16:13:24 +0000512 unsigned FirstIdx = MAI.getFirstIndexOperandNumber();
513 if (FirstIdx == MAI.getNumOperands() ||
514 (FirstIdx == MAI.getNumOperands()-1 &&
515 MAI.getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) {
Chris Lattner8a2a3112001-12-14 16:52:21 +0000516 // Replace the index list on this MAI with the index on the getelementptr
517 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
Chris Lattner7e708292002-06-25 16:13:24 +0000518 } else if (*MAI.idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
Chris Lattner8a2a3112001-12-14 16:52:21 +0000519 // Otherwise we can do the fold if the first index of the GEP is a zero
520 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
Chris Lattner7e708292002-06-25 16:13:24 +0000521 Indices.insert(Indices.end(), MAI.idx_begin()+1, MAI.idx_end());
Chris Lattner8a2a3112001-12-14 16:52:21 +0000522 }
523
524 if (Indices.empty()) return 0; // Can't do the fold?
525
Chris Lattner7e708292002-06-25 16:13:24 +0000526 switch (MAI.getOpcode()) {
Chris Lattner8a2a3112001-12-14 16:52:21 +0000527 case Instruction::GetElementPtr:
Chris Lattner7e708292002-06-25 16:13:24 +0000528 return new GetElementPtrInst(Src->getOperand(0), Indices, MAI.getName());
Chris Lattner8a2a3112001-12-14 16:52:21 +0000529 case Instruction::Load:
Chris Lattner7e708292002-06-25 16:13:24 +0000530 return new LoadInst(Src->getOperand(0), Indices, MAI.getName());
Chris Lattner8a2a3112001-12-14 16:52:21 +0000531 case Instruction::Store:
Chris Lattner7e708292002-06-25 16:13:24 +0000532 return new StoreInst(MAI.getOperand(0), Src->getOperand(0), Indices);
Chris Lattner8a2a3112001-12-14 16:52:21 +0000533 default:
534 assert(0 && "Unknown memaccessinst!");
535 break;
536 }
537 abort();
538 return 0;
539}
540
Chris Lattner8a2a3112001-12-14 16:52:21 +0000541
Chris Lattner7e708292002-06-25 16:13:24 +0000542bool InstCombiner::runOnFunction(Function &F) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000543 bool Changed = false;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000544
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000545 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattner8a2a3112001-12-14 16:52:21 +0000546
547 while (!WorkList.empty()) {
548 Instruction *I = WorkList.back(); // Get an instruction from the worklist
549 WorkList.pop_back();
550
551 // Now that we have an instruction, try combining it to simplify it...
Chris Lattner7e708292002-06-25 16:13:24 +0000552 Instruction *Result = visit(*I);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000553 if (Result) {
Chris Lattner3dec1f22002-05-10 15:38:35 +0000554 ++NumCombined;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000555 // Should we replace the old instruction with a new one?
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +0000556 if (Result != I) {
557 // Instructions can end up on the worklist more than once. Make sure
558 // we do not process an instruction that has been deleted.
559 std::vector<Instruction*>::iterator It = std::find(WorkList.begin(),
560 WorkList.end(), I);
561 while (It != WorkList.end()) {
562 It = WorkList.erase(It);
563 It = std::find(It, WorkList.end(), I);
564 }
565
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000566 ReplaceInstWithInst(I, Result);
Chris Lattner7e708292002-06-25 16:13:24 +0000567 } else {
568 // FIXME:
569 // FIXME:
570 // FIXME: This should DCE the instruction to simplify the cases above.
571 // FIXME:
572 // FIXME:
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +0000573 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000574
575 WorkList.push_back(Result);
Chris Lattner7e708292002-06-25 16:13:24 +0000576 AddUsesToWorkList(*Result);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000577 Changed = true;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000578 }
579 }
580
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000581 return Changed;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000582}
583
584Pass *createInstructionCombiningPass() {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000585 return new InstCombiner();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000586}