blob: 494666129887508d84f73a390e14fb5fa484d849 [file] [log] [blame]
Chris Lattnerca081252001-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 Lattnerf4cdbf32002-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 Lattnerca081252001-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 Lattnerb4cfa7f2002-05-07 20:03:00 +000018#include "llvm/Transforms/Scalar.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000019#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerae7a0d32002-08-02 19:29:35 +000020#include "llvm/Transforms/Utils/Local.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000021#include "llvm/ConstantHandling.h"
Chris Lattnerca081252001-12-14 16:52:21 +000022#include "llvm/iMemory.h"
Chris Lattner3a60d042002-04-15 19:45:29 +000023#include "llvm/iOther.h"
Chris Lattnerbbbdd852002-05-06 18:06:38 +000024#include "llvm/iPHINode.h"
Chris Lattner260ab202002-04-18 17:39:14 +000025#include "llvm/iOperators.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000026#include "llvm/Pass.h"
Chris Lattner60a65912002-02-12 21:07:25 +000027#include "llvm/Support/InstIterator.h"
Chris Lattner260ab202002-04-18 17:39:14 +000028#include "llvm/Support/InstVisitor.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000029#include "Support/StatisticReporter.h"
Chris Lattner053c0932002-05-14 15:24:07 +000030#include <algorithm>
Chris Lattnerca081252001-12-14 16:52:21 +000031
Chris Lattner0b18c1d2002-05-10 15:38:35 +000032static Statistic<> NumCombined("instcombine\t- Number of insts combined");
Chris Lattnerca081252001-12-14 16:52:21 +000033
Chris Lattner260ab202002-04-18 17:39:14 +000034namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +000035 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000036 public InstVisitor<InstCombiner, Instruction*> {
37 // Worklist of all of the instructions that need to be simplified.
38 std::vector<Instruction*> WorkList;
39
Chris Lattner113f4f42002-06-25 16:13:24 +000040 void AddUsesToWorkList(Instruction &I) {
Chris Lattner260ab202002-04-18 17:39:14 +000041 // The instruction was simplified, add all users of the instruction to
42 // the work lists because they might get more simplified now...
43 //
Chris Lattner113f4f42002-06-25 16:13:24 +000044 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner260ab202002-04-18 17:39:14 +000045 UI != UE; ++UI)
46 WorkList.push_back(cast<Instruction>(*UI));
47 }
48
49 public:
Chris Lattner113f4f42002-06-25 16:13:24 +000050 virtual bool runOnFunction(Function &F);
Chris Lattner260ab202002-04-18 17:39:14 +000051
Chris Lattnerf12cc842002-04-28 21:27:06 +000052 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.preservesCFG();
54 }
55
Chris Lattner260ab202002-04-18 17:39:14 +000056 // Visitation implementation - Implement instruction combining for different
57 // instruction types. The semantics are as follows:
58 // Return Value:
59 // null - No change was made
60 // I - Change was made, I is still valid
61 // otherwise - Change was made, replace I with returned instruction
62 //
Chris Lattner113f4f42002-06-25 16:13:24 +000063 Instruction *visitNot(UnaryOperator &I);
64 Instruction *visitAdd(BinaryOperator &I);
65 Instruction *visitSub(BinaryOperator &I);
66 Instruction *visitMul(BinaryOperator &I);
67 Instruction *visitDiv(BinaryOperator &I);
68 Instruction *visitRem(BinaryOperator &I);
69 Instruction *visitAnd(BinaryOperator &I);
70 Instruction *visitOr (BinaryOperator &I);
71 Instruction *visitXor(BinaryOperator &I);
72 Instruction *visitSetCondInst(BinaryOperator &I);
73 Instruction *visitShiftInst(Instruction &I);
74 Instruction *visitCastInst(CastInst &CI);
75 Instruction *visitPHINode(PHINode &PN);
76 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
Chris Lattner260ab202002-04-18 17:39:14 +000077
78 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +000079 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner260ab202002-04-18 17:39:14 +000080 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000081
Chris Lattnerc8b70922002-07-26 21:12:46 +000082 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattner260ab202002-04-18 17:39:14 +000083}
84
85
Chris Lattner113f4f42002-06-25 16:13:24 +000086Instruction *InstCombiner::visitNot(UnaryOperator &I) {
Chris Lattner5d6bec52002-05-06 17:03:21 +000087 // not (not X) = X
Chris Lattner113f4f42002-06-25 16:13:24 +000088 if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(0)))
Chris Lattner5d6bec52002-05-06 17:03:21 +000089 if (Op->getOpcode() == Instruction::Not) {
90 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +000091 I.replaceAllUsesWith(Op->getOperand(0));
92 return &I;
Chris Lattner5d6bec52002-05-06 17:03:21 +000093 }
94 return 0;
95}
96
Chris Lattner260ab202002-04-18 17:39:14 +000097
98// Make sure that this instruction has a constant on the right hand side if it
99// has any constant arguments. If not, fix it an return true.
100//
Chris Lattner113f4f42002-06-25 16:13:24 +0000101static bool SimplifyBinOp(BinaryOperator &I) {
102 if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
103 return !I.swapOperands();
Chris Lattner260ab202002-04-18 17:39:14 +0000104 return false;
105}
Chris Lattnerca081252001-12-14 16:52:21 +0000106
Chris Lattner9fa53de2002-05-06 16:49:18 +0000107// dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
108// instruction if the LHS is a constant zero (which is the 'negate' form).
109//
110static inline Value *dyn_castNegInst(Value *V) {
111 Instruction *I = dyn_cast<Instruction>(V);
112 if (!I || I->getOpcode() != Instruction::Sub) return 0;
113
114 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
115 return I->getOperand(1);
116 return 0;
117}
118
Chris Lattner113f4f42002-06-25 16:13:24 +0000119Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattner260ab202002-04-18 17:39:14 +0000120 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000121 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000122
123 // Eliminate 'add int %X, 0'
Chris Lattner113f4f42002-06-25 16:13:24 +0000124 if (RHS == Constant::getNullValue(I.getType())) {
Chris Lattner9fa53de2002-05-06 16:49:18 +0000125 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000126 I.replaceAllUsesWith(LHS);
127 return &I;
Chris Lattner9fa53de2002-05-06 16:49:18 +0000128 }
129
Chris Lattner147e9752002-05-08 22:46:53 +0000130 // -A + B --> B - A
Chris Lattner9fa53de2002-05-06 16:49:18 +0000131 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000132 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000133
134 // A + -B --> A - B
135 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000136 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000137
138 // Simplify add instructions with a constant RHS...
Chris Lattner9fa53de2002-05-06 16:49:18 +0000139 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
140 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
141 if (ILHS->getOpcode() == Instruction::Add &&
142 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattner260ab202002-04-18 17:39:14 +0000143 // Fold:
144 // %Y = add int %X, 1
145 // %Z = add int %Y, 1
146 // into:
147 // %Z = add int %X, 2
148 //
Chris Lattner9fa53de2002-05-06 16:49:18 +0000149 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000150 I.setOperand(0, ILHS->getOperand(0));
151 I.setOperand(1, Val);
152 return &I;
Chris Lattner04805fa2002-02-26 21:46:54 +0000153 }
Chris Lattnerca081252001-12-14 16:52:21 +0000154 }
155 }
Chris Lattnerca081252001-12-14 16:52:21 +0000156 }
157
Chris Lattner113f4f42002-06-25 16:13:24 +0000158 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000159}
160
Chris Lattner113f4f42002-06-25 16:13:24 +0000161Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000162 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000163
164 if (Op0 == Op1) { // sub X, X -> 0
165 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000166 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
167 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000168 }
Chris Lattner260ab202002-04-18 17:39:14 +0000169
170 // If this is a subtract instruction with a constant RHS, convert it to an add
171 // instruction of a negative constant
172 //
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000173 if (Constant *Op2 = dyn_cast<Constant>(Op1))
Chris Lattner113f4f42002-06-25 16:13:24 +0000174 if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
175 return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
Chris Lattner260ab202002-04-18 17:39:14 +0000176
Chris Lattner147e9752002-05-08 22:46:53 +0000177 // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
178 if (Value *V = dyn_castNegInst(Op1))
179 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000180
Chris Lattnerad3c4952002-05-09 01:29:19 +0000181 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
182 // not used by anyone else...
183 //
184 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
Chris Lattner170ed7b2002-05-14 16:44:07 +0000185 if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
Chris Lattnerad3c4952002-05-09 01:29:19 +0000186 // Swap the two operands of the subexpr...
187 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
188 Op1I->setOperand(0, IIOp1);
189 Op1I->setOperand(1, IIOp0);
190
191 // Create the new top level add instruction...
192 return BinaryOperator::create(Instruction::Add, Op0, Op1);
193 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000194 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000195}
196
Chris Lattner113f4f42002-06-25 16:13:24 +0000197Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattner260ab202002-04-18 17:39:14 +0000198 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000199 Value *Op1 = I.getOperand(0);
Chris Lattner260ab202002-04-18 17:39:14 +0000200
201 // Simplify add instructions with a constant RHS...
Chris Lattner113f4f42002-06-25 16:13:24 +0000202 if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
203 if (I.getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
Chris Lattner260ab202002-04-18 17:39:14 +0000204 // Eliminate 'mul int %X, 1'
205 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000206 I.replaceAllUsesWith(Op1);
207 return &I;
Chris Lattner31ba1292002-04-29 22:24:47 +0000208
Chris Lattner113f4f42002-06-25 16:13:24 +0000209 } else if (I.getType()->isIntegral() &&
Chris Lattner31ba1292002-04-29 22:24:47 +0000210 cast<ConstantInt>(Op2)->equalsInt(2)) {
211 // Convert 'mul int %X, 2' to 'add int %X, %X'
Chris Lattner113f4f42002-06-25 16:13:24 +0000212 return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
Chris Lattner31ba1292002-04-29 22:24:47 +0000213
214 } else if (Op2->isNullValue()) {
215 // Eliminate 'mul int %X, 0'
Chris Lattner113f4f42002-06-25 16:13:24 +0000216 AddUsesToWorkList(I); // Add all modified instrs to worklist
217 I.replaceAllUsesWith(Op2); // Set this value to zero directly
218 return &I;
Chris Lattner260ab202002-04-18 17:39:14 +0000219 }
220 }
221
Chris Lattner113f4f42002-06-25 16:13:24 +0000222 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000223}
224
225
Chris Lattner113f4f42002-06-25 16:13:24 +0000226Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000227 // div X, 1 == X
Chris Lattner113f4f42002-06-25 16:13:24 +0000228 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000229 if (RHS->equalsInt(1)) {
230 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000231 I.replaceAllUsesWith(I.getOperand(0));
232 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000233 }
234 return 0;
235}
236
237
Chris Lattner113f4f42002-06-25 16:13:24 +0000238Instruction *InstCombiner::visitRem(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000239 // rem X, 1 == 0
Chris Lattner113f4f42002-06-25 16:13:24 +0000240 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000241 if (RHS->equalsInt(1)) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000242 AddUsesToWorkList(I); // Add all modified instrs to worklist
243 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
244 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000245 }
246 return 0;
247}
248
249static Constant *getMaxValue(const Type *Ty) {
250 assert(Ty == Type::BoolTy || Ty->isIntegral());
251 if (Ty == Type::BoolTy)
252 return ConstantBool::True;
253
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000254 if (Ty->isSigned())
Chris Lattnera6e047a2002-05-06 18:54:59 +0000255 return ConstantSInt::get(Ty, -1);
256 else if (Ty->isUnsigned()) {
257 // Calculate -1 casted to the right type...
258 unsigned TypeBits = Ty->getPrimitiveSize()*8;
259 uint64_t Val = (uint64_t)-1LL; // All ones
260 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000261 return ConstantUInt::get(Ty, Val);
Chris Lattnera6e047a2002-05-06 18:54:59 +0000262 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000263 return 0;
264}
265
266
Chris Lattner113f4f42002-06-25 16:13:24 +0000267Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000268 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000269 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000270
271 // and X, X = X and X, 0 == 0
Chris Lattner113f4f42002-06-25 16:13:24 +0000272 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
273 AddUsesToWorkList(I); // Add all modified instrs to worklist
274 I.replaceAllUsesWith(Op1);
275 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000276 }
277
278 // and X, -1 == X
279 if (Constant *RHS = dyn_cast<Constant>(Op1))
Chris Lattner113f4f42002-06-25 16:13:24 +0000280 if (RHS == getMaxValue(I.getType())) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000281 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000282 I.replaceAllUsesWith(Op0);
283 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000284 }
285
Chris Lattner113f4f42002-06-25 16:13:24 +0000286 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000287}
288
289
290
Chris Lattner113f4f42002-06-25 16:13:24 +0000291Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000292 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000293 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000294
295 // or X, X = X or X, 0 == X
Chris Lattner113f4f42002-06-25 16:13:24 +0000296 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000297 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000298 I.replaceAllUsesWith(Op0);
299 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000300 }
301
302 // or X, -1 == -1
303 if (Constant *RHS = dyn_cast<Constant>(Op1))
Chris Lattner113f4f42002-06-25 16:13:24 +0000304 if (RHS == getMaxValue(I.getType())) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000305 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000306 I.replaceAllUsesWith(Op1);
307 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000308 }
309
Chris Lattner113f4f42002-06-25 16:13:24 +0000310 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000311}
312
313
314
Chris Lattner113f4f42002-06-25 16:13:24 +0000315Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000316 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000317 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000318
319 // xor X, X = 0
320 if (Op0 == Op1) {
321 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000322 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
323 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000324 }
325
326 // xor X, 0 == X
Chris Lattner113f4f42002-06-25 16:13:24 +0000327 if (Op1 == Constant::getNullValue(I.getType())) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000328 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000329 I.replaceAllUsesWith(Op0);
330 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000331 }
332
Chris Lattner113f4f42002-06-25 16:13:24 +0000333 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000334}
335
Chris Lattner1fc23f32002-05-09 20:11:54 +0000336// isTrueWhenEqual - Return true if the specified setcondinst instruction is
337// true when both operands are equal...
338//
Chris Lattner113f4f42002-06-25 16:13:24 +0000339static bool isTrueWhenEqual(Instruction &I) {
340 return I.getOpcode() == Instruction::SetEQ ||
341 I.getOpcode() == Instruction::SetGE ||
342 I.getOpcode() == Instruction::SetLE;
Chris Lattner1fc23f32002-05-09 20:11:54 +0000343}
344
Chris Lattner113f4f42002-06-25 16:13:24 +0000345Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000346 bool Changed = SimplifyBinOp(I);
347
348 // setcc X, X
Chris Lattner113f4f42002-06-25 16:13:24 +0000349 if (I.getOperand(0) == I.getOperand(1)) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000350 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000351 I.replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I)));
352 return &I;
Chris Lattner1fc23f32002-05-09 20:11:54 +0000353 }
354
355 // setcc <global*>, 0 - Global value addresses are never null!
Chris Lattner113f4f42002-06-25 16:13:24 +0000356 if (isa<GlobalValue>(I.getOperand(0)) &&
357 isa<ConstantPointerNull>(I.getOperand(1))) {
Chris Lattner1fc23f32002-05-09 20:11:54 +0000358 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000359 I.replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I)));
360 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000361 }
362
Chris Lattner113f4f42002-06-25 16:13:24 +0000363 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000364}
365
366
367
Chris Lattner113f4f42002-06-25 16:13:24 +0000368Instruction *InstCombiner::visitShiftInst(Instruction &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000369 assert(I.getOperand(1)->getType() == Type::UByteTy);
370 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000371
372 // shl X, 0 == X and shr X, 0 == X
373 // shl 0, X == 0 and shr 0, X == 0
374 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
375 Op0 == Constant::getNullValue(Op0->getType())) {
376 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000377 I.replaceAllUsesWith(Op0);
378 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000379 }
380
381 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
382 // a signed value.
383 //
384 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
385 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
386 if (CUI->getValue() >= TypeBits &&
Chris Lattner113f4f42002-06-25 16:13:24 +0000387 !(Op0->getType()->isSigned() && I.getOpcode() == Instruction::Shr)) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000388 AddUsesToWorkList(I); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000389 I.replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
390 return &I;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000391 }
392 }
393 return 0;
394}
395
396
Chris Lattner48a44f72002-05-02 17:06:02 +0000397// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
398// instruction.
399//
Chris Lattner113f4f42002-06-25 16:13:24 +0000400static inline bool isEliminableCastOfCast(const CastInst &CI,
Chris Lattner48a44f72002-05-02 17:06:02 +0000401 const CastInst *CSrc) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000402 assert(CI.getOperand(0) == CSrc);
Chris Lattner48a44f72002-05-02 17:06:02 +0000403 const Type *SrcTy = CSrc->getOperand(0)->getType();
404 const Type *MidTy = CSrc->getType();
Chris Lattner113f4f42002-06-25 16:13:24 +0000405 const Type *DstTy = CI.getType();
Chris Lattner48a44f72002-05-02 17:06:02 +0000406
Chris Lattner650b6da2002-08-02 20:00:25 +0000407 // It is legal to eliminate the instruction if casting A->B->A if the sizes
408 // are identical and the bits don't get reinterpreted (for example
409 // int->float->int)
410 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
411 return true;
Chris Lattner48a44f72002-05-02 17:06:02 +0000412
413 // Allow free casting and conversion of sizes as long as the sign doesn't
414 // change...
Chris Lattner650b6da2002-08-02 20:00:25 +0000415 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral() &&
416 SrcTy->isSigned() == MidTy->isSigned() &&
417 MidTy->isSigned() == DstTy->isSigned()) {
418 // Only accept cases where we are either monotonically increasing the type
419 // size, or monotonically decreasing it.
420 //
421 unsigned SrcSize = SrcTy->getPrimitiveSize();
422 unsigned MidSize = MidTy->getPrimitiveSize();
423 unsigned DstSize = DstTy->getPrimitiveSize();
424 if (SrcSize < MidSize && MidSize < DstSize)
425 return true;
426
427 if (SrcSize > MidSize && MidSize > DstSize)
428 return true;
429 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000430
431 // Otherwise, we cannot succeed. Specifically we do not want to allow things
432 // like: short -> ushort -> uint, because this can create wrong results if
433 // the input short is negative!
434 //
435 return false;
436}
437
438
439// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +0000440//
Chris Lattner113f4f42002-06-25 16:13:24 +0000441Instruction *InstCombiner::visitCastInst(CastInst &CI) {
Chris Lattner48a44f72002-05-02 17:06:02 +0000442 // If the user is casting a value to the same type, eliminate this cast
443 // instruction...
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000444 if (CI.getType() == CI.getOperand(0)->getType()) {
Chris Lattner260ab202002-04-18 17:39:14 +0000445 AddUsesToWorkList(CI); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000446 CI.replaceAllUsesWith(CI.getOperand(0));
447 return &CI;
Chris Lattner260ab202002-04-18 17:39:14 +0000448 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000449
Chris Lattner48a44f72002-05-02 17:06:02 +0000450 // If casting the result of another cast instruction, try to eliminate this
451 // one!
452 //
Chris Lattner650b6da2002-08-02 20:00:25 +0000453 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
Chris Lattner48a44f72002-05-02 17:06:02 +0000454 if (isEliminableCastOfCast(CI, CSrc)) {
455 // This instruction now refers directly to the cast's src operand. This
456 // has a good chance of making CSrc dead.
Chris Lattner113f4f42002-06-25 16:13:24 +0000457 CI.setOperand(0, CSrc->getOperand(0));
458 return &CI;
Chris Lattner48a44f72002-05-02 17:06:02 +0000459 }
460
Chris Lattner650b6da2002-08-02 20:00:25 +0000461 // If this is an A->B->A cast, and we are dealing with integral types, try
462 // to convert this into a logical 'and' instruction.
463 //
464 if (CSrc->getOperand(0)->getType() == CI.getType() &&
465 CI.getType()->isIntegral() && CSrc->getType()->isIntegral() &&
466 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
467 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
468 assert(CSrc->getType() != Type::ULongTy &&
469 "Cannot have type bigger than ulong!");
470 unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
471 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
472 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
473 AndOp);
474 }
475 }
476
Chris Lattner260ab202002-04-18 17:39:14 +0000477 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +0000478}
479
Chris Lattner48a44f72002-05-02 17:06:02 +0000480
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000481// PHINode simplification
482//
Chris Lattner113f4f42002-06-25 16:13:24 +0000483Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000484 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattner113f4f42002-06-25 16:13:24 +0000485 if (PN.getNumIncomingValues() == 1) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000486 AddUsesToWorkList(PN); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000487 PN.replaceAllUsesWith(PN.getIncomingValue(0));
488 return &PN;
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000489 }
490
491 return 0;
492}
493
Chris Lattner48a44f72002-05-02 17:06:02 +0000494
Chris Lattner113f4f42002-06-25 16:13:24 +0000495Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000496 // Is it 'getelementptr %P, uint 0' or 'getelementptr %P'
Chris Lattner113f4f42002-06-25 16:13:24 +0000497 // If so, eliminate the noop.
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000498 if ((GEP.getNumOperands() == 2 &&
499 GEP.getOperand(1) == Constant::getNullValue(Type::UIntTy)) ||
500 GEP.getNumOperands() == 1) {
Chris Lattner48a44f72002-05-02 17:06:02 +0000501 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
Chris Lattner113f4f42002-06-25 16:13:24 +0000502 GEP.replaceAllUsesWith(GEP.getOperand(0));
503 return &GEP;
Chris Lattner48a44f72002-05-02 17:06:02 +0000504 }
505
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000506 // Combine Indices - If the source pointer to this getelementptr instruction
507 // is a getelementptr instruction, combine the indices of the two
508 // getelementptr instructions into a single instruction.
509 //
510 if (GetElementPtrInst *Src =
511 dyn_cast<GetElementPtrInst>(GEP.getPointerOperand())) {
512 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +0000513
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000514 // Can we combine the two pointer arithmetics offsets?
515 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
516 isa<Constant>(GEP.getOperand(1))) {
517 // Replace the index list on this GEP with the index on the getelementptr
518 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
519 Indices[0] = *cast<Constant>(Src->getOperand(1)) +
520 *cast<Constant>(GEP.getOperand(1));
521 assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
522
523 } else if (*GEP.idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
524 // Otherwise we can do the fold if the first index of the GEP is a zero
525 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
526 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
527 }
528
529 if (!Indices.empty())
530 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
Chris Lattnerca081252001-12-14 16:52:21 +0000531 }
532
Chris Lattnerca081252001-12-14 16:52:21 +0000533 return 0;
534}
535
Chris Lattnerca081252001-12-14 16:52:21 +0000536
Chris Lattner113f4f42002-06-25 16:13:24 +0000537bool InstCombiner::runOnFunction(Function &F) {
Chris Lattner260ab202002-04-18 17:39:14 +0000538 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +0000539
Chris Lattner260ab202002-04-18 17:39:14 +0000540 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +0000541
542 while (!WorkList.empty()) {
543 Instruction *I = WorkList.back(); // Get an instruction from the worklist
544 WorkList.pop_back();
545
546 // Now that we have an instruction, try combining it to simplify it...
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000547 if (Instruction *Result = visit(*I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000548 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +0000549 // Should we replace the old instruction with a new one?
Chris Lattner053c0932002-05-14 15:24:07 +0000550 if (Result != I) {
551 // Instructions can end up on the worklist more than once. Make sure
552 // we do not process an instruction that has been deleted.
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000553 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
554 WorkList.end());
Chris Lattner053c0932002-05-14 15:24:07 +0000555
Chris Lattner260ab202002-04-18 17:39:14 +0000556 ReplaceInstWithInst(I, Result);
Chris Lattner113f4f42002-06-25 16:13:24 +0000557 } else {
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000558 BasicBlock::iterator II = I;
559
560 // If the instruction was modified, it's possible that it is now dead.
561 // if so, remove it.
562 if (dceInstruction(II)) {
563 // Instructions may end up in the worklist more than once. Erase them
564 // all.
565 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
566 WorkList.end());
567 Result = 0;
568 }
Chris Lattner053c0932002-05-14 15:24:07 +0000569 }
Chris Lattner260ab202002-04-18 17:39:14 +0000570
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000571 if (Result) {
572 WorkList.push_back(Result);
573 AddUsesToWorkList(*Result);
574 }
Chris Lattner260ab202002-04-18 17:39:14 +0000575 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +0000576 }
577 }
578
Chris Lattner260ab202002-04-18 17:39:14 +0000579 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +0000580}
581
582Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +0000583 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +0000584}