blob: 179f3b296ab080650b3f8584a6335f572ad8b296 [file] [log] [blame]
Chris Lattner233f7dc2002-08-12 21:17:25 +00001//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
Chris Lattner8a2a3112001-12-14 16:52:21 +00002//
3// InstructionCombining - Combine instructions to form fewer, simple
Chris Lattner62b14df2002-09-02 04:59:56 +00004// instructions. This pass does not modify the CFG This pass is where algebraic
5// simplification happens.
Chris Lattner8a2a3112001-12-14 16:52:21 +00006//
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
Chris Lattner022103b2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner497c60c2002-05-07 18:12:18 +000018#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattner90ac28c2002-08-02 19:29:35 +000019#include "llvm/Transforms/Utils/Local.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 Lattner62b14df2002-09-02 04:59:56 +000031static Statistic<> NumCombined ("instcombine\t- Number of insts combined");
32static Statistic<> NumConstProp("instcombine\t- Number of constant folds");
33static Statistic<> NumDeadInst ("instcombine\t- Number of dead inst eliminate");
Chris Lattner8a2a3112001-12-14 16:52:21 +000034
Chris Lattnerdd841ae2002-04-18 17:39:14 +000035namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000036 class InstCombiner : public FunctionPass,
Chris Lattnerdd841ae2002-04-18 17:39:14 +000037 public InstVisitor<InstCombiner, Instruction*> {
38 // Worklist of all of the instructions that need to be simplified.
39 std::vector<Instruction*> WorkList;
40
Chris Lattner7e708292002-06-25 16:13:24 +000041 void AddUsesToWorkList(Instruction &I) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +000042 // The instruction was simplified, add all users of the instruction to
43 // the work lists because they might get more simplified now...
44 //
Chris Lattner7e708292002-06-25 16:13:24 +000045 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerdd841ae2002-04-18 17:39:14 +000046 UI != UE; ++UI)
47 WorkList.push_back(cast<Instruction>(*UI));
48 }
49
Chris Lattner62b14df2002-09-02 04:59:56 +000050 // removeFromWorkList - remove all instances of I from the worklist.
51 void removeFromWorkList(Instruction *I);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000052 public:
Chris Lattner7e708292002-06-25 16:13:24 +000053 virtual bool runOnFunction(Function &F);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000054
Chris Lattner97e52e42002-04-28 21:27:06 +000055 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.preservesCFG();
57 }
58
Chris Lattnerdd841ae2002-04-18 17:39:14 +000059 // Visitation implementation - Implement instruction combining for different
60 // instruction types. The semantics are as follows:
61 // Return Value:
62 // null - No change was made
Chris Lattner233f7dc2002-08-12 21:17:25 +000063 // I - Change was made, I is still valid, I may be dead though
Chris Lattnerdd841ae2002-04-18 17:39:14 +000064 // otherwise - Change was made, replace I with returned instruction
65 //
Chris Lattner7e708292002-06-25 16:13:24 +000066 Instruction *visitAdd(BinaryOperator &I);
67 Instruction *visitSub(BinaryOperator &I);
68 Instruction *visitMul(BinaryOperator &I);
69 Instruction *visitDiv(BinaryOperator &I);
70 Instruction *visitRem(BinaryOperator &I);
71 Instruction *visitAnd(BinaryOperator &I);
72 Instruction *visitOr (BinaryOperator &I);
73 Instruction *visitXor(BinaryOperator &I);
74 Instruction *visitSetCondInst(BinaryOperator &I);
75 Instruction *visitShiftInst(Instruction &I);
76 Instruction *visitCastInst(CastInst &CI);
77 Instruction *visitPHINode(PHINode &PN);
78 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000079
80 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner7e708292002-06-25 16:13:24 +000081 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner8b170942002-08-09 23:47:40 +000082
83 // InsertNewInstBefore - insert an instruction New before instruction Old
84 // in the program. Add the new instruction to the worklist.
85 //
86 void InsertNewInstBefore(Instruction *New, Instruction &Old) {
Chris Lattnere6f9a912002-08-23 18:32:43 +000087 assert(New && New->getParent() == 0 &&
88 "New instruction already inserted into a basic block!");
Chris Lattner8b170942002-08-09 23:47:40 +000089 BasicBlock *BB = Old.getParent();
90 BB->getInstList().insert(&Old, New); // Insert inst
91 WorkList.push_back(New); // Add to worklist
92 }
93
94 // ReplaceInstUsesWith - This method is to be used when an instruction is
95 // found to be dead, replacable with another preexisting expression. Here
96 // we add all uses of I to the worklist, replace all uses of I with the new
97 // value, then return I, so that the inst combiner will know that I was
98 // modified.
99 //
100 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
101 AddUsesToWorkList(I); // Add all modified instrs to worklist
102 I.replaceAllUsesWith(V);
103 return &I;
104 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000105 };
Chris Lattnerf6293092002-07-23 18:06:35 +0000106
Chris Lattnera6275cc2002-07-26 21:12:46 +0000107 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000108}
109
110
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000111// Make sure that this instruction has a constant on the right hand side if it
112// has any constant arguments. If not, fix it an return true.
113//
Chris Lattner7e708292002-06-25 16:13:24 +0000114static bool SimplifyBinOp(BinaryOperator &I) {
115 if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
116 return !I.swapOperands();
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000117 return false;
118}
Chris Lattner8a2a3112001-12-14 16:52:21 +0000119
Chris Lattnerb35dde12002-05-06 16:49:18 +0000120// dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
121// instruction if the LHS is a constant zero (which is the 'negate' form).
122//
123static inline Value *dyn_castNegInst(Value *V) {
124 Instruction *I = dyn_cast<Instruction>(V);
125 if (!I || I->getOpcode() != Instruction::Sub) return 0;
126
127 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
128 return I->getOperand(1);
129 return 0;
130}
131
Chris Lattneraf2930e2002-08-14 17:51:49 +0000132static inline Value *dyn_castNotInst(Value *V) {
133 Instruction *I = dyn_cast<Instruction>(V);
134 if (!I || I->getOpcode() != Instruction::Xor) return 0;
135
136 if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1)))
137 if (CI->isAllOnesValue())
138 return I->getOperand(0);
139 return 0;
140}
141
Chris Lattner7e708292002-06-25 16:13:24 +0000142Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000143 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000144 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000145
146 // Eliminate 'add int %X, 0'
Chris Lattner233f7dc2002-08-12 21:17:25 +0000147 if (RHS == Constant::getNullValue(I.getType()))
148 return ReplaceInstUsesWith(I, LHS);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000149
Chris Lattner5c4afb92002-05-08 22:46:53 +0000150 // -A + B --> B - A
Chris Lattnerb35dde12002-05-06 16:49:18 +0000151 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner5c4afb92002-05-08 22:46:53 +0000152 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000153
154 // A + -B --> A - B
155 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner5c4afb92002-05-08 22:46:53 +0000156 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000157
158 // Simplify add instructions with a constant RHS...
Chris Lattnerb35dde12002-05-06 16:49:18 +0000159 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
160 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
161 if (ILHS->getOpcode() == Instruction::Add &&
162 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000163 // Fold:
164 // %Y = add int %X, 1
165 // %Z = add int %Y, 1
166 // into:
167 // %Z = add int %X, 2
168 //
Chris Lattnerb35dde12002-05-06 16:49:18 +0000169 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
Chris Lattner7e708292002-06-25 16:13:24 +0000170 I.setOperand(0, ILHS->getOperand(0));
171 I.setOperand(1, Val);
172 return &I;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000173 }
Chris Lattner8a2a3112001-12-14 16:52:21 +0000174 }
175 }
Chris Lattner8a2a3112001-12-14 16:52:21 +0000176 }
177
Chris Lattner7e708292002-06-25 16:13:24 +0000178 return Changed ? &I : 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000179}
180
Chris Lattner7e708292002-06-25 16:13:24 +0000181Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000182 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000183
Chris Lattner233f7dc2002-08-12 21:17:25 +0000184 if (Op0 == Op1) // sub X, X -> 0
185 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000186
187 // If this is a subtract instruction with a constant RHS, convert it to an add
188 // instruction of a negative constant
189 //
Chris Lattner3f5b8772002-05-06 16:14:14 +0000190 if (Constant *Op2 = dyn_cast<Constant>(Op1))
Chris Lattner7e708292002-06-25 16:13:24 +0000191 if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
192 return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000193
Chris Lattner233f7dc2002-08-12 21:17:25 +0000194 // If this is a 'B = x-(-A)', change to B = x+A...
Chris Lattner5c4afb92002-05-08 22:46:53 +0000195 if (Value *V = dyn_castNegInst(Op1))
196 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000197
Chris Lattner40371712002-05-09 01:29:19 +0000198 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
199 // not used by anyone else...
200 //
201 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
Chris Lattner86c25fd2002-05-14 16:44:07 +0000202 if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
Chris Lattner40371712002-05-09 01:29:19 +0000203 // Swap the two operands of the subexpr...
204 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
205 Op1I->setOperand(0, IIOp1);
206 Op1I->setOperand(1, IIOp0);
207
208 // Create the new top level add instruction...
209 return BinaryOperator::create(Instruction::Add, Op0, Op1);
210 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000211 return 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000212}
213
Chris Lattner7e708292002-06-25 16:13:24 +0000214Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000215 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000216 Value *Op1 = I.getOperand(0);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000217
Chris Lattner233f7dc2002-08-12 21:17:25 +0000218 // Simplify mul instructions with a constant RHS...
Chris Lattner7e708292002-06-25 16:13:24 +0000219 if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
Chris Lattner0c4e8862002-09-03 01:08:28 +0000220 if (I.getType()->isInteger() && cast<ConstantInt>(Op2)->equalsInt(1))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000221 return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul int %X, 1'
Chris Lattner6c1ce212002-04-29 22:24:47 +0000222
Chris Lattner0c4e8862002-09-03 01:08:28 +0000223 if (I.getType()->isInteger() && cast<ConstantInt>(Op2)->equalsInt(2))
Chris Lattner6c1ce212002-04-29 22:24:47 +0000224 // Convert 'mul int %X, 2' to 'add int %X, %X'
Chris Lattner7e708292002-06-25 16:13:24 +0000225 return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
Chris Lattner6c1ce212002-04-29 22:24:47 +0000226
Chris Lattner233f7dc2002-08-12 21:17:25 +0000227 if (Op2->isNullValue())
228 return ReplaceInstUsesWith(I, Op2); // Eliminate 'mul int %X, 0'
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000229 }
230
Chris Lattner7e708292002-06-25 16:13:24 +0000231 return Changed ? &I : 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000232}
233
234
Chris Lattner7e708292002-06-25 16:13:24 +0000235Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000236 // div X, 1 == X
Chris Lattner7e708292002-06-25 16:13:24 +0000237 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000238 if (RHS->equalsInt(1))
239 return ReplaceInstUsesWith(I, I.getOperand(0));
Chris Lattner3f5b8772002-05-06 16:14:14 +0000240 return 0;
241}
242
243
Chris Lattner7e708292002-06-25 16:13:24 +0000244Instruction *InstCombiner::visitRem(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000245 // rem X, 1 == 0
Chris Lattner7e708292002-06-25 16:13:24 +0000246 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000247 if (RHS->equalsInt(1))
248 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
249
Chris Lattner3f5b8772002-05-06 16:14:14 +0000250 return 0;
251}
252
Chris Lattner8b170942002-08-09 23:47:40 +0000253// isMaxValueMinusOne - return true if this is Max-1
Chris Lattner233f7dc2002-08-12 21:17:25 +0000254static bool isMaxValueMinusOne(const ConstantInt *C) {
Chris Lattner8b170942002-08-09 23:47:40 +0000255 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
256 // Calculate -1 casted to the right type...
257 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
258 uint64_t Val = ~0ULL; // All ones
259 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
260 return CU->getValue() == Val-1;
261 }
262
263 const ConstantSInt *CS = cast<ConstantSInt>(C);
264
265 // Calculate 0111111111..11111
266 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
267 int64_t Val = INT64_MAX; // All ones
268 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
269 return CS->getValue() == Val-1;
270}
271
272// isMinValuePlusOne - return true if this is Min+1
Chris Lattner233f7dc2002-08-12 21:17:25 +0000273static bool isMinValuePlusOne(const ConstantInt *C) {
Chris Lattner8b170942002-08-09 23:47:40 +0000274 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
275 return CU->getValue() == 1;
276
277 const ConstantSInt *CS = cast<ConstantSInt>(C);
278
279 // Calculate 1111111111000000000000
280 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
281 int64_t Val = -1; // All ones
282 Val <<= TypeBits-1; // Shift over to the right spot
283 return CS->getValue() == Val+1;
284}
285
286
Chris Lattner7e708292002-06-25 16:13:24 +0000287Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000288 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000289 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000290
291 // and X, X = X and X, 0 == 0
Chris Lattner233f7dc2002-08-12 21:17:25 +0000292 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
293 return ReplaceInstUsesWith(I, Op1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000294
295 // and X, -1 == X
Chris Lattner572f4a02002-08-13 17:50:24 +0000296 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000297 if (RHS->isAllOnesValue())
298 return ReplaceInstUsesWith(I, Op0);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000299
Chris Lattnere6f9a912002-08-23 18:32:43 +0000300 // and (not A), (not B) == not (or A, B)
301 if (Op0->use_size() == 1 && Op1->use_size() == 1)
302 if (Value *A = dyn_castNotInst(Op0))
303 if (Value *B = dyn_castNotInst(Op1)) {
304 Instruction *Or = BinaryOperator::create(Instruction::Or, A, B,
305 I.getName()+".demorgan");
306 InsertNewInstBefore(Or, I);
307 return BinaryOperator::createNot(Or, I.getName());
308 }
309
Chris Lattner7e708292002-06-25 16:13:24 +0000310 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000311}
312
313
314
Chris Lattner7e708292002-06-25 16:13:24 +0000315Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000316 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000317 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000318
319 // or X, X = X or X, 0 == X
Chris Lattner233f7dc2002-08-12 21:17:25 +0000320 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
321 return ReplaceInstUsesWith(I, Op0);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000322
323 // or X, -1 == -1
Chris Lattner572f4a02002-08-13 17:50:24 +0000324 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000325 if (RHS->isAllOnesValue())
326 return ReplaceInstUsesWith(I, Op1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000327
Chris Lattner7e708292002-06-25 16:13:24 +0000328 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000329}
330
331
332
Chris Lattner7e708292002-06-25 16:13:24 +0000333Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000334 bool Changed = SimplifyBinOp(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000335 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000336
337 // xor X, X = 0
Chris Lattner233f7dc2002-08-12 21:17:25 +0000338 if (Op0 == Op1)
339 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner3f5b8772002-05-06 16:14:14 +0000340
Chris Lattner572f4a02002-08-13 17:50:24 +0000341 if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattner8b170942002-08-09 23:47:40 +0000342 // xor X, 0 == X
Chris Lattner233f7dc2002-08-12 21:17:25 +0000343 if (Op1C->isNullValue())
344 return ReplaceInstUsesWith(I, Op0);
Chris Lattner8b170942002-08-09 23:47:40 +0000345
Chris Lattner05bd1b22002-08-20 18:24:26 +0000346 // Is this a "NOT" instruction?
347 if (Op1C->isAllOnesValue()) {
348 // xor (xor X, -1), -1 = not (not X) = X
Chris Lattneraf2930e2002-08-14 17:51:49 +0000349 if (Value *X = dyn_castNotInst(Op0))
350 return ReplaceInstUsesWith(I, X);
Chris Lattner05bd1b22002-08-20 18:24:26 +0000351
352 // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
353 if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
354 if (SCI->use_size() == 1)
355 return new SetCondInst(SCI->getInverseCondition(),
356 SCI->getOperand(0), SCI->getOperand(1));
357 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000358 }
359
Chris Lattner7e708292002-06-25 16:13:24 +0000360 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000361}
362
Chris Lattner8b170942002-08-09 23:47:40 +0000363// AddOne, SubOne - Add or subtract a constant one from an integer constant...
364static Constant *AddOne(ConstantInt *C) {
365 Constant *Result = *C + *ConstantInt::get(C->getType(), 1);
366 assert(Result && "Constant folding integer addition failed!");
367 return Result;
368}
369static Constant *SubOne(ConstantInt *C) {
370 Constant *Result = *C - *ConstantInt::get(C->getType(), 1);
371 assert(Result && "Constant folding integer addition failed!");
372 return Result;
373}
374
Chris Lattner53a5b572002-05-09 20:11:54 +0000375// isTrueWhenEqual - Return true if the specified setcondinst instruction is
376// true when both operands are equal...
377//
Chris Lattner7e708292002-06-25 16:13:24 +0000378static bool isTrueWhenEqual(Instruction &I) {
379 return I.getOpcode() == Instruction::SetEQ ||
380 I.getOpcode() == Instruction::SetGE ||
381 I.getOpcode() == Instruction::SetLE;
Chris Lattner53a5b572002-05-09 20:11:54 +0000382}
383
Chris Lattner7e708292002-06-25 16:13:24 +0000384Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000385 bool Changed = SimplifyBinOp(I);
Chris Lattner8b170942002-08-09 23:47:40 +0000386 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
387 const Type *Ty = Op0->getType();
Chris Lattner3f5b8772002-05-06 16:14:14 +0000388
389 // setcc X, X
Chris Lattner8b170942002-08-09 23:47:40 +0000390 if (Op0 == Op1)
391 return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
Chris Lattner53a5b572002-05-09 20:11:54 +0000392
393 // setcc <global*>, 0 - Global value addresses are never null!
Chris Lattner8b170942002-08-09 23:47:40 +0000394 if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
395 return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
396
397 // setcc's with boolean values can always be turned into bitwise operations
398 if (Ty == Type::BoolTy) {
399 // If this is <, >, or !=, we can change this into a simple xor instruction
400 if (!isTrueWhenEqual(I))
401 return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
402
403 // Otherwise we need to make a temporary intermediate instruction and insert
404 // it into the instruction stream. This is what we are after:
405 //
406 // seteq bool %A, %B -> ~(A^B)
407 // setle bool %A, %B -> ~A | B
408 // setge bool %A, %B -> A | ~B
409 //
410 if (I.getOpcode() == Instruction::SetEQ) { // seteq case
411 Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
412 I.getName()+"tmp");
413 InsertNewInstBefore(Xor, I);
Chris Lattneraf2930e2002-08-14 17:51:49 +0000414 return BinaryOperator::createNot(Xor, I.getName());
Chris Lattner8b170942002-08-09 23:47:40 +0000415 }
416
417 // Handle the setXe cases...
418 assert(I.getOpcode() == Instruction::SetGE ||
419 I.getOpcode() == Instruction::SetLE);
420
421 if (I.getOpcode() == Instruction::SetGE)
422 std::swap(Op0, Op1); // Change setge -> setle
423
424 // Now we just have the SetLE case.
Chris Lattneraf2930e2002-08-14 17:51:49 +0000425 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
Chris Lattner8b170942002-08-09 23:47:40 +0000426 InsertNewInstBefore(Not, I);
427 return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
428 }
429
430 // Check to see if we are doing one of many comparisons against constant
431 // integers at the end of their ranges...
432 //
433 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
434 // Check to see if we are comparing against the minimum or maximum value...
Chris Lattner233f7dc2002-08-12 21:17:25 +0000435 if (CI->isMinValue()) {
Chris Lattner8b170942002-08-09 23:47:40 +0000436 if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE
437 return ReplaceInstUsesWith(I, ConstantBool::False);
438 if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE
439 return ReplaceInstUsesWith(I, ConstantBool::True);
440 if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN
441 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
442 if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN
443 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
444
Chris Lattner233f7dc2002-08-12 21:17:25 +0000445 } else if (CI->isMaxValue()) {
Chris Lattner8b170942002-08-09 23:47:40 +0000446 if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE
447 return ReplaceInstUsesWith(I, ConstantBool::False);
448 if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE
449 return ReplaceInstUsesWith(I, ConstantBool::True);
450 if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX
451 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
452 if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX
453 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
454
455 // Comparing against a value really close to min or max?
456 } else if (isMinValuePlusOne(CI)) {
457 if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN
458 return BinaryOperator::create(Instruction::SetEQ, Op0,
459 SubOne(CI), I.getName());
460 if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN
461 return BinaryOperator::create(Instruction::SetNE, Op0,
462 SubOne(CI), I.getName());
463
464 } else if (isMaxValueMinusOne(CI)) {
465 if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX
466 return BinaryOperator::create(Instruction::SetEQ, Op0,
467 AddOne(CI), I.getName());
468 if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX
469 return BinaryOperator::create(Instruction::SetNE, Op0,
470 AddOne(CI), I.getName());
471 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000472 }
473
Chris Lattner7e708292002-06-25 16:13:24 +0000474 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000475}
476
477
478
Chris Lattner7e708292002-06-25 16:13:24 +0000479Instruction *InstCombiner::visitShiftInst(Instruction &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000480 assert(I.getOperand(1)->getType() == Type::UByteTy);
481 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000482
483 // shl X, 0 == X and shr X, 0 == X
484 // shl 0, X == 0 and shr 0, X == 0
485 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
Chris Lattner233f7dc2002-08-12 21:17:25 +0000486 Op0 == Constant::getNullValue(Op0->getType()))
487 return ReplaceInstUsesWith(I, Op0);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000488
Chris Lattner233f7dc2002-08-12 21:17:25 +0000489 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
Chris Lattner3f5b8772002-05-06 16:14:14 +0000490 // a signed value.
491 //
492 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
Chris Lattnerf2836082002-09-10 23:04:09 +0000493 if (I.getOpcode() == Instruction::Shr) {
494 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
495 if (CUI->getValue() >= TypeBits && !(Op0->getType()->isSigned()))
496 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
497 }
498
499 // Check to see if we are shifting left by 1. If so, turn it into an add
500 // instruction.
501 if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
502 // Convert 'shl int %X, 2' to 'add int %X, %X'
503 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
504
Chris Lattner3f5b8772002-05-06 16:14:14 +0000505 }
506 return 0;
507}
508
509
Chris Lattnera1be5662002-05-02 17:06:02 +0000510// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
511// instruction.
512//
Chris Lattner7e708292002-06-25 16:13:24 +0000513static inline bool isEliminableCastOfCast(const CastInst &CI,
Chris Lattnera1be5662002-05-02 17:06:02 +0000514 const CastInst *CSrc) {
Chris Lattner7e708292002-06-25 16:13:24 +0000515 assert(CI.getOperand(0) == CSrc);
Chris Lattnera1be5662002-05-02 17:06:02 +0000516 const Type *SrcTy = CSrc->getOperand(0)->getType();
517 const Type *MidTy = CSrc->getType();
Chris Lattner7e708292002-06-25 16:13:24 +0000518 const Type *DstTy = CI.getType();
Chris Lattnera1be5662002-05-02 17:06:02 +0000519
Chris Lattner8fd217c2002-08-02 20:00:25 +0000520 // It is legal to eliminate the instruction if casting A->B->A if the sizes
521 // are identical and the bits don't get reinterpreted (for example
Chris Lattner5cf6f112002-08-14 23:21:10 +0000522 // int->float->int would not be allowed)
Chris Lattner8fd217c2002-08-02 20:00:25 +0000523 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
524 return true;
Chris Lattnera1be5662002-05-02 17:06:02 +0000525
526 // Allow free casting and conversion of sizes as long as the sign doesn't
527 // change...
Chris Lattner0c4e8862002-09-03 01:08:28 +0000528 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
Chris Lattner8fd217c2002-08-02 20:00:25 +0000529 unsigned SrcSize = SrcTy->getPrimitiveSize();
530 unsigned MidSize = MidTy->getPrimitiveSize();
531 unsigned DstSize = DstTy->getPrimitiveSize();
Chris Lattner8fd217c2002-08-02 20:00:25 +0000532
Chris Lattner3ecce662002-08-15 16:15:25 +0000533 // Cases where we are monotonically decreasing the size of the type are
534 // always ok, regardless of what sign changes are going on.
535 //
Chris Lattner5cf6f112002-08-14 23:21:10 +0000536 if (SrcSize >= MidSize && MidSize >= DstSize)
Chris Lattner8fd217c2002-08-02 20:00:25 +0000537 return true;
Chris Lattner3ecce662002-08-15 16:15:25 +0000538
539 // If we are monotonically growing, things are more complex.
540 //
541 if (SrcSize <= MidSize && MidSize <= DstSize) {
542 // We have eight combinations of signedness to worry about. Here's the
543 // table:
544 static const int SignTable[8] = {
545 // CODE, SrcSigned, MidSigned, DstSigned, Comment
546 1, // U U U Always ok
547 1, // U U S Always ok
548 3, // U S U Ok iff SrcSize != MidSize
549 3, // U S S Ok iff SrcSize != MidSize
550 0, // S U U Never ok
551 2, // S U S Ok iff MidSize == DstSize
552 1, // S S U Always ok
553 1, // S S S Always ok
554 };
555
556 // Choose an action based on the current entry of the signtable that this
557 // cast of cast refers to...
558 unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
559 switch (SignTable[Row]) {
560 case 0: return false; // Never ok
561 case 1: return true; // Always ok
562 case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
563 case 3: // Ok iff SrcSize != MidSize
564 return SrcSize != MidSize || SrcTy == Type::BoolTy;
565 default: assert(0 && "Bad entry in sign table!");
566 }
Chris Lattner3ecce662002-08-15 16:15:25 +0000567 }
Chris Lattner8fd217c2002-08-02 20:00:25 +0000568 }
Chris Lattnera1be5662002-05-02 17:06:02 +0000569
570 // Otherwise, we cannot succeed. Specifically we do not want to allow things
571 // like: short -> ushort -> uint, because this can create wrong results if
572 // the input short is negative!
573 //
574 return false;
575}
576
577
578// CastInst simplification
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000579//
Chris Lattner7e708292002-06-25 16:13:24 +0000580Instruction *InstCombiner::visitCastInst(CastInst &CI) {
Chris Lattnera1be5662002-05-02 17:06:02 +0000581 // If the user is casting a value to the same type, eliminate this cast
582 // instruction...
Chris Lattner233f7dc2002-08-12 21:17:25 +0000583 if (CI.getType() == CI.getOperand(0)->getType())
584 return ReplaceInstUsesWith(CI, CI.getOperand(0));
Chris Lattnera1be5662002-05-02 17:06:02 +0000585
Chris Lattnera1be5662002-05-02 17:06:02 +0000586 // If casting the result of another cast instruction, try to eliminate this
587 // one!
588 //
Chris Lattner8fd217c2002-08-02 20:00:25 +0000589 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
Chris Lattnera1be5662002-05-02 17:06:02 +0000590 if (isEliminableCastOfCast(CI, CSrc)) {
591 // This instruction now refers directly to the cast's src operand. This
592 // has a good chance of making CSrc dead.
Chris Lattner7e708292002-06-25 16:13:24 +0000593 CI.setOperand(0, CSrc->getOperand(0));
594 return &CI;
Chris Lattnera1be5662002-05-02 17:06:02 +0000595 }
596
Chris Lattner8fd217c2002-08-02 20:00:25 +0000597 // If this is an A->B->A cast, and we are dealing with integral types, try
598 // to convert this into a logical 'and' instruction.
599 //
600 if (CSrc->getOperand(0)->getType() == CI.getType() &&
Chris Lattner0c4e8862002-09-03 01:08:28 +0000601 CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
Chris Lattner8fd217c2002-08-02 20:00:25 +0000602 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
603 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
604 assert(CSrc->getType() != Type::ULongTy &&
605 "Cannot have type bigger than ulong!");
606 unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
607 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
608 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
609 AndOp);
610 }
611 }
612
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000613 return 0;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000614}
615
Chris Lattnera1be5662002-05-02 17:06:02 +0000616
Chris Lattner473945d2002-05-06 18:06:38 +0000617// PHINode simplification
618//
Chris Lattner7e708292002-06-25 16:13:24 +0000619Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Chris Lattner473945d2002-05-06 18:06:38 +0000620 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattnerf02c4682002-08-20 15:35:35 +0000621 if (PN.getNumIncomingValues() == 0)
622 return ReplaceInstUsesWith(PN, Constant::getNullValue(PN.getType()));
Chris Lattner233f7dc2002-08-12 21:17:25 +0000623 if (PN.getNumIncomingValues() == 1)
624 return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
Chris Lattnerf02c4682002-08-20 15:35:35 +0000625
626 // Otherwise if all of the incoming values are the same for the PHI, replace
627 // the PHI node with the incoming value.
628 //
Chris Lattnerc20e2452002-08-22 20:22:01 +0000629 Value *InVal = 0;
630 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
631 if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself...
632 if (InVal && PN.getIncomingValue(i) != InVal)
633 return 0; // Not the same, bail out.
634 else
635 InVal = PN.getIncomingValue(i);
636
637 // The only case that could cause InVal to be null is if we have a PHI node
638 // that only has entries for itself. In this case, there is no entry into the
639 // loop, so kill the PHI.
640 //
641 if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
Chris Lattner473945d2002-05-06 18:06:38 +0000642
Chris Lattnerf02c4682002-08-20 15:35:35 +0000643 // All of the incoming values are the same, replace the PHI node now.
644 return ReplaceInstUsesWith(PN, InVal);
Chris Lattner473945d2002-05-06 18:06:38 +0000645}
646
Chris Lattnera1be5662002-05-02 17:06:02 +0000647
Chris Lattner7e708292002-06-25 16:13:24 +0000648Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000649 // Is it 'getelementptr %P, uint 0' or 'getelementptr %P'
Chris Lattner7e708292002-06-25 16:13:24 +0000650 // If so, eliminate the noop.
Chris Lattner90ac28c2002-08-02 19:29:35 +0000651 if ((GEP.getNumOperands() == 2 &&
Chris Lattner3cac88a2002-09-11 01:21:33 +0000652 GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
Chris Lattner233f7dc2002-08-12 21:17:25 +0000653 GEP.getNumOperands() == 1)
654 return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
Chris Lattnera1be5662002-05-02 17:06:02 +0000655
Chris Lattner90ac28c2002-08-02 19:29:35 +0000656 // Combine Indices - If the source pointer to this getelementptr instruction
657 // is a getelementptr instruction, combine the indices of the two
658 // getelementptr instructions into a single instruction.
659 //
Chris Lattner9b761232002-08-17 22:21:59 +0000660 if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000661 std::vector<Value *> Indices;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000662
Chris Lattner90ac28c2002-08-02 19:29:35 +0000663 // Can we combine the two pointer arithmetics offsets?
664 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
665 isa<Constant>(GEP.getOperand(1))) {
666 // Replace the index list on this GEP with the index on the getelementptr
667 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
668 Indices[0] = *cast<Constant>(Src->getOperand(1)) +
669 *cast<Constant>(GEP.getOperand(1));
670 assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
671
Chris Lattner3cac88a2002-09-11 01:21:33 +0000672 } else if (*GEP.idx_begin() == ConstantUInt::getNullValue(Type::LongTy)) {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000673 // Otherwise we can do the fold if the first index of the GEP is a zero
674 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
675 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
676 }
677
678 if (!Indices.empty())
679 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
Chris Lattner9b761232002-08-17 22:21:59 +0000680
681 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
682 // GEP of global variable. If all of the indices for this GEP are
683 // constants, we can promote this to a constexpr instead of an instruction.
684
685 // Scan for nonconstants...
686 std::vector<Constant*> Indices;
687 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
688 for (; I != E && isa<Constant>(*I); ++I)
689 Indices.push_back(cast<Constant>(*I));
690
691 if (I == E) { // If they are all constants...
692 ConstantExpr *CE =
693 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
694
695 // Replace all uses of the GEP with the new constexpr...
696 return ReplaceInstUsesWith(GEP, CE);
697 }
Chris Lattner8a2a3112001-12-14 16:52:21 +0000698 }
699
Chris Lattner8a2a3112001-12-14 16:52:21 +0000700 return 0;
701}
702
Chris Lattner8a2a3112001-12-14 16:52:21 +0000703
Chris Lattner62b14df2002-09-02 04:59:56 +0000704void InstCombiner::removeFromWorkList(Instruction *I) {
705 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
706 WorkList.end());
707}
708
Chris Lattner7e708292002-06-25 16:13:24 +0000709bool InstCombiner::runOnFunction(Function &F) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000710 bool Changed = false;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000711
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000712 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattner8a2a3112001-12-14 16:52:21 +0000713
714 while (!WorkList.empty()) {
715 Instruction *I = WorkList.back(); // Get an instruction from the worklist
716 WorkList.pop_back();
717
Chris Lattner62b14df2002-09-02 04:59:56 +0000718 // Check to see if we can DCE or ConstantPropogate the instruction...
719 // Check to see if we can DIE the instruction...
720 if (isInstructionTriviallyDead(I)) {
721 // Add operands to the worklist...
722 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
723 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
724 WorkList.push_back(Op);
725
726 ++NumDeadInst;
727 BasicBlock::iterator BBI = I;
728 if (dceInstruction(BBI)) {
729 removeFromWorkList(I);
730 continue;
731 }
732 }
733
734 // Instruction isn't dead, see if we can constant propogate it...
735 if (Constant *C = ConstantFoldInstruction(I)) {
736 // Add operands to the worklist...
737 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
738 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
739 WorkList.push_back(Op);
740 I->replaceAllUsesWith(C);
741 ++NumConstProp;
742 BasicBlock::iterator BBI = I;
743 if (dceInstruction(BBI)) {
744 removeFromWorkList(I);
745 continue;
746 }
747 }
748
Chris Lattner8a2a3112001-12-14 16:52:21 +0000749 // Now that we have an instruction, try combining it to simplify it...
Chris Lattner90ac28c2002-08-02 19:29:35 +0000750 if (Instruction *Result = visit(*I)) {
Chris Lattner3dec1f22002-05-10 15:38:35 +0000751 ++NumCombined;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000752 // Should we replace the old instruction with a new one?
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +0000753 if (Result != I) {
754 // Instructions can end up on the worklist more than once. Make sure
755 // we do not process an instruction that has been deleted.
Chris Lattner62b14df2002-09-02 04:59:56 +0000756 removeFromWorkList(I);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000757 ReplaceInstWithInst(I, Result);
Chris Lattner7e708292002-06-25 16:13:24 +0000758 } else {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000759 BasicBlock::iterator II = I;
760
761 // If the instruction was modified, it's possible that it is now dead.
762 // if so, remove it.
763 if (dceInstruction(II)) {
764 // Instructions may end up in the worklist more than once. Erase them
765 // all.
Chris Lattner62b14df2002-09-02 04:59:56 +0000766 removeFromWorkList(I);
Chris Lattner90ac28c2002-08-02 19:29:35 +0000767 Result = 0;
768 }
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +0000769 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000770
Chris Lattner90ac28c2002-08-02 19:29:35 +0000771 if (Result) {
772 WorkList.push_back(Result);
773 AddUsesToWorkList(*Result);
774 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000775 Changed = true;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000776 }
777 }
778
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000779 return Changed;
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000780}
781
782Pass *createInstructionCombiningPass() {
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000783 return new InstCombiner();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000784}