blob: 7c77a666d6ef0935413f076fceabded2e44f66e8 [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 Lattnerc54e2b82003-05-22 19:07:21 +000021#include "llvm/Instructions.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000022#include "llvm/Pass.h"
Chris Lattner0864acf2002-11-04 16:18:53 +000023#include "llvm/DerivedTypes.h"
Chris Lattner221d6882002-02-12 21:07:25 +000024#include "llvm/Support/InstIterator.h"
Chris Lattnerdd841ae2002-04-18 17:39:14 +000025#include "llvm/Support/InstVisitor.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000026#include "Support/Statistic.h"
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +000027#include <algorithm>
Chris Lattner8a2a3112001-12-14 16:52:21 +000028
Chris Lattnerdd841ae2002-04-18 17:39:14 +000029namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000030 Statistic<> NumCombined ("instcombine", "Number of insts combined");
31 Statistic<> NumConstProp("instcombine", "Number of constant folds");
32 Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
33
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
Chris Lattner62b14df2002-09-02 04:59:56 +000048 // removeFromWorkList - remove all instances of I from the worklist.
49 void removeFromWorkList(Instruction *I);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000050 public:
Chris Lattner7e708292002-06-25 16:13:24 +000051 virtual bool runOnFunction(Function &F);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000052
Chris Lattner97e52e42002-04-28 21:27:06 +000053 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000054 AU.setPreservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000055 }
56
Chris Lattnerdd841ae2002-04-18 17:39:14 +000057 // Visitation implementation - Implement instruction combining for different
58 // instruction types. The semantics are as follows:
59 // Return Value:
60 // null - No change was made
Chris Lattner233f7dc2002-08-12 21:17:25 +000061 // I - Change was made, I is still valid, I may be dead though
Chris Lattnerdd841ae2002-04-18 17:39:14 +000062 // otherwise - Change was made, replace I with returned instruction
63 //
Chris Lattner7e708292002-06-25 16:13:24 +000064 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);
Chris Lattnerea340052003-03-10 19:16:08 +000073 Instruction *visitShiftInst(ShiftInst &I);
Chris Lattner7e708292002-06-25 16:13:24 +000074 Instruction *visitCastInst(CastInst &CI);
75 Instruction *visitPHINode(PHINode &PN);
76 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
Chris Lattner0864acf2002-11-04 16:18:53 +000077 Instruction *visitAllocationInst(AllocationInst &AI);
Chris Lattnerdd841ae2002-04-18 17:39:14 +000078
79 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner7e708292002-06-25 16:13:24 +000080 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner8b170942002-08-09 23:47:40 +000081
82 // InsertNewInstBefore - insert an instruction New before instruction Old
83 // in the program. Add the new instruction to the worklist.
84 //
85 void InsertNewInstBefore(Instruction *New, Instruction &Old) {
Chris Lattnere6f9a912002-08-23 18:32:43 +000086 assert(New && New->getParent() == 0 &&
87 "New instruction already inserted into a basic block!");
Chris Lattner8b170942002-08-09 23:47:40 +000088 BasicBlock *BB = Old.getParent();
89 BB->getInstList().insert(&Old, New); // Insert inst
90 WorkList.push_back(New); // Add to worklist
91 }
92
93 // ReplaceInstUsesWith - This method is to be used when an instruction is
94 // found to be dead, replacable with another preexisting expression. Here
95 // we add all uses of I to the worklist, replace all uses of I with the new
96 // value, then return I, so that the inst combiner will know that I was
97 // modified.
98 //
99 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
100 AddUsesToWorkList(I); // Add all modified instrs to worklist
101 I.replaceAllUsesWith(V);
102 return &I;
103 }
Chris Lattnerc8802d22003-03-11 00:12:48 +0000104
105 // SimplifyCommutative - This performs a few simplifications for commutative
106 // operators...
107 bool SimplifyCommutative(BinaryOperator &I);
108
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000109 };
Chris Lattnerf6293092002-07-23 18:06:35 +0000110
Chris Lattnera6275cc2002-07-26 21:12:46 +0000111 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000112}
113
Chris Lattner4f98c562003-03-10 21:43:22 +0000114// getComplexity: Assign a complexity or rank value to LLVM Values...
115// 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst
116static unsigned getComplexity(Value *V) {
117 if (isa<Instruction>(V)) {
118 if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
119 return 2;
120 return 3;
121 }
122 if (isa<Argument>(V)) return 2;
123 return isa<Constant>(V) ? 0 : 1;
124}
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000125
Chris Lattnerc8802d22003-03-11 00:12:48 +0000126// isOnlyUse - Return true if this instruction will be deleted if we stop using
127// it.
128static bool isOnlyUse(Value *V) {
129 return V->use_size() == 1 || isa<Constant>(V);
130}
131
Chris Lattner4f98c562003-03-10 21:43:22 +0000132// SimplifyCommutative - This performs a few simplifications for commutative
133// operators:
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000134//
Chris Lattner4f98c562003-03-10 21:43:22 +0000135// 1. Order operands such that they are listed from right (least complex) to
136// left (most complex). This puts constants before unary operators before
137// binary operators.
138//
Chris Lattnerc8802d22003-03-11 00:12:48 +0000139// 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
140// 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattner4f98c562003-03-10 21:43:22 +0000141//
Chris Lattnerc8802d22003-03-11 00:12:48 +0000142bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000143 bool Changed = false;
144 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
145 Changed = !I.swapOperands();
146
147 if (!I.isAssociative()) return Changed;
148 Instruction::BinaryOps Opcode = I.getOpcode();
Chris Lattnerc8802d22003-03-11 00:12:48 +0000149 if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
150 if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
151 if (isa<Constant>(I.getOperand(1))) {
152 Constant *Folded = ConstantFoldBinaryInstruction(I.getOpcode(),
153 cast<Constant>(I.getOperand(1)), cast<Constant>(Op->getOperand(1)));
154 assert(Folded && "Couldn't constant fold commutative operand?");
155 I.setOperand(0, Op->getOperand(0));
156 I.setOperand(1, Folded);
157 return true;
158 } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
159 if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
160 isOnlyUse(Op) && isOnlyUse(Op1)) {
161 Constant *C1 = cast<Constant>(Op->getOperand(1));
162 Constant *C2 = cast<Constant>(Op1->getOperand(1));
163
164 // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
165 Constant *Folded = ConstantFoldBinaryInstruction(I.getOpcode(),C1,C2);
166 assert(Folded && "Couldn't constant fold commutative operand?");
167 Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
168 Op1->getOperand(0),
169 Op1->getName(), &I);
170 WorkList.push_back(New);
171 I.setOperand(0, New);
172 I.setOperand(1, Folded);
173 return true;
174 }
Chris Lattner4f98c562003-03-10 21:43:22 +0000175 }
Chris Lattner4f98c562003-03-10 21:43:22 +0000176 return Changed;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000177}
Chris Lattner8a2a3112001-12-14 16:52:21 +0000178
Chris Lattner8d969642003-03-10 23:06:50 +0000179// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
180// if the LHS is a constant zero (which is the 'negate' form).
Chris Lattnerb35dde12002-05-06 16:49:18 +0000181//
Chris Lattner8d969642003-03-10 23:06:50 +0000182static inline Value *dyn_castNegVal(Value *V) {
183 if (BinaryOperator::isNeg(V))
184 return BinaryOperator::getNegArgument(cast<BinaryOperator>(V));
185
Chris Lattnerfe32e0c2003-04-30 22:19:10 +0000186 // Constants can be considered to be negated values if they can be folded...
187 if (Constant *C = dyn_cast<Constant>(V))
188 return *Constant::getNullValue(V->getType()) - *C;
Chris Lattner8d969642003-03-10 23:06:50 +0000189 return 0;
Chris Lattnerb35dde12002-05-06 16:49:18 +0000190}
191
Chris Lattner8d969642003-03-10 23:06:50 +0000192static inline Value *dyn_castNotVal(Value *V) {
193 if (BinaryOperator::isNot(V))
194 return BinaryOperator::getNotArgument(cast<BinaryOperator>(V));
195
196 // Constants can be considered to be not'ed values...
Chris Lattner3f2ec392003-04-30 22:34:06 +0000197 if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V))
198 return *ConstantIntegral::getAllOnesValue(C->getType()) ^ *C;
Chris Lattner8d969642003-03-10 23:06:50 +0000199 return 0;
200}
201
Chris Lattnerc8802d22003-03-11 00:12:48 +0000202// dyn_castFoldableMul - If this value is a multiply that can be folded into
203// other computations (because it has a constant operand), return the
204// non-constant operand of the multiply.
205//
206static inline Value *dyn_castFoldableMul(Value *V) {
207 if (V->use_size() == 1 && V->getType()->isInteger())
208 if (Instruction *I = dyn_cast<Instruction>(V))
209 if (I->getOpcode() == Instruction::Mul)
210 if (isa<Constant>(I->getOperand(1)))
211 return I->getOperand(0);
212 return 0;
Chris Lattnera2881962003-02-18 19:28:33 +0000213}
Chris Lattneraf2930e2002-08-14 17:51:49 +0000214
Chris Lattnerc8802d22003-03-11 00:12:48 +0000215// dyn_castMaskingAnd - If this value is an And instruction masking a value with
216// a constant, return the constant being anded with.
217//
218static inline Constant *dyn_castMaskingAnd(Value *V) {
219 if (Instruction *I = dyn_cast<Instruction>(V))
220 if (I->getOpcode() == Instruction::And)
221 return dyn_cast<Constant>(I->getOperand(1));
222
223 // If this is a constant, it acts just like we were masking with it.
224 return dyn_cast<Constant>(V);
225}
Chris Lattnera2881962003-02-18 19:28:33 +0000226
227// Log2 - Calculate the log base 2 for the specified value if it is exactly a
228// power of 2.
229static unsigned Log2(uint64_t Val) {
230 assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
231 unsigned Count = 0;
232 while (Val != 1) {
233 if (Val & 1) return 0; // Multiple bits set?
234 Val >>= 1;
235 ++Count;
236 }
237 return Count;
Chris Lattneraf2930e2002-08-14 17:51:49 +0000238}
239
Chris Lattner7e708292002-06-25 16:13:24 +0000240Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000241 bool Changed = SimplifyCommutative(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000242 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000243
244 // Eliminate 'add int %X, 0'
Chris Lattner233f7dc2002-08-12 21:17:25 +0000245 if (RHS == Constant::getNullValue(I.getType()))
246 return ReplaceInstUsesWith(I, LHS);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000247
Chris Lattner5c4afb92002-05-08 22:46:53 +0000248 // -A + B --> B - A
Chris Lattner8d969642003-03-10 23:06:50 +0000249 if (Value *V = dyn_castNegVal(LHS))
Chris Lattner5c4afb92002-05-08 22:46:53 +0000250 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000251
252 // A + -B --> A - B
Chris Lattner8d969642003-03-10 23:06:50 +0000253 if (!isa<Constant>(RHS))
254 if (Value *V = dyn_castNegVal(RHS))
255 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000256
Chris Lattnerad3448c2003-02-18 19:57:07 +0000257 // X*C + X --> X * (C+1)
258 if (dyn_castFoldableMul(LHS) == RHS) {
259 Constant *CP1 = *cast<Constant>(cast<Instruction>(LHS)->getOperand(1)) +
260 *ConstantInt::get(I.getType(), 1);
261 assert(CP1 && "Couldn't constant fold C + 1?");
262 return BinaryOperator::create(Instruction::Mul, RHS, CP1);
263 }
264
265 // X + X*C --> X * (C+1)
266 if (dyn_castFoldableMul(RHS) == LHS) {
267 Constant *CP1 = *cast<Constant>(cast<Instruction>(RHS)->getOperand(1)) +
268 *ConstantInt::get(I.getType(), 1);
269 assert(CP1 && "Couldn't constant fold C + 1?");
270 return BinaryOperator::create(Instruction::Mul, LHS, CP1);
271 }
272
Chris Lattnerc8802d22003-03-11 00:12:48 +0000273 // (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
274 if (Constant *C1 = dyn_castMaskingAnd(LHS))
275 if (Constant *C2 = dyn_castMaskingAnd(RHS))
276 if ((*C1 & *C2)->isNullValue())
277 return BinaryOperator::create(Instruction::Or, LHS, RHS);
278
Chris Lattner7e708292002-06-25 16:13:24 +0000279 return Changed ? &I : 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000280}
281
Chris Lattner7e708292002-06-25 16:13:24 +0000282Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000283 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000284
Chris Lattner233f7dc2002-08-12 21:17:25 +0000285 if (Op0 == Op1) // sub X, X -> 0
286 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000287
Chris Lattner233f7dc2002-08-12 21:17:25 +0000288 // If this is a 'B = x-(-A)', change to B = x+A...
Chris Lattner8d969642003-03-10 23:06:50 +0000289 if (Value *V = dyn_castNegVal(Op1))
Chris Lattner5c4afb92002-05-08 22:46:53 +0000290 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattnerb35dde12002-05-06 16:49:18 +0000291
Chris Lattnera2881962003-02-18 19:28:33 +0000292 // Replace (-1 - A) with (~A)...
293 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0))
294 if (C->isAllOnesValue())
295 return BinaryOperator::createNot(Op1);
Chris Lattner40371712002-05-09 01:29:19 +0000296
Chris Lattnera2881962003-02-18 19:28:33 +0000297 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
298 if (Op1I->use_size() == 1) {
299 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
300 // is not used by anyone else...
301 //
302 if (Op1I->getOpcode() == Instruction::Sub) {
303 // Swap the two operands of the subexpr...
304 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
305 Op1I->setOperand(0, IIOp1);
306 Op1I->setOperand(1, IIOp0);
307
308 // Create the new top level add instruction...
309 return BinaryOperator::create(Instruction::Add, Op0, Op1);
310 }
311
312 // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
313 //
314 if (Op1I->getOpcode() == Instruction::And &&
315 (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
316 Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
317
318 Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I);
319 return BinaryOperator::create(Instruction::And, Op0, NewNot);
320 }
Chris Lattnerad3448c2003-02-18 19:57:07 +0000321
322 // X - X*C --> X * (1-C)
323 if (dyn_castFoldableMul(Op1I) == Op0) {
324 Constant *CP1 = *ConstantInt::get(I.getType(), 1) -
325 *cast<Constant>(cast<Instruction>(Op1)->getOperand(1));
326 assert(CP1 && "Couldn't constant fold 1-C?");
327 return BinaryOperator::create(Instruction::Mul, Op0, CP1);
328 }
Chris Lattner40371712002-05-09 01:29:19 +0000329 }
Chris Lattnera2881962003-02-18 19:28:33 +0000330
Chris Lattnerad3448c2003-02-18 19:57:07 +0000331 // X*C - X --> X * (C-1)
332 if (dyn_castFoldableMul(Op0) == Op1) {
333 Constant *CP1 = *cast<Constant>(cast<Instruction>(Op0)->getOperand(1)) -
334 *ConstantInt::get(I.getType(), 1);
335 assert(CP1 && "Couldn't constant fold C - 1?");
336 return BinaryOperator::create(Instruction::Mul, Op1, CP1);
337 }
338
Chris Lattner3f5b8772002-05-06 16:14:14 +0000339 return 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000340}
341
Chris Lattner7e708292002-06-25 16:13:24 +0000342Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000343 bool Changed = SimplifyCommutative(I);
Chris Lattnera2881962003-02-18 19:28:33 +0000344 Value *Op0 = I.getOperand(0);
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000345
Chris Lattner233f7dc2002-08-12 21:17:25 +0000346 // Simplify mul instructions with a constant RHS...
Chris Lattnera2881962003-02-18 19:28:33 +0000347 if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
348 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
349 const Type *Ty = CI->getType();
350 uint64_t Val = Ty->isSigned() ?
351 (uint64_t)cast<ConstantSInt>(CI)->getValue() :
352 cast<ConstantUInt>(CI)->getValue();
353 switch (Val) {
354 case 0:
355 return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul double %X, 0'
356 case 1:
357 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul int %X, 1'
358 case 2: // Convert 'mul int %X, 2' to 'add int %X, %X'
359 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
360 }
Chris Lattner6c1ce212002-04-29 22:24:47 +0000361
Chris Lattnera2881962003-02-18 19:28:33 +0000362 if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C
363 return new ShiftInst(Instruction::Shl, Op0,
364 ConstantUInt::get(Type::UByteTy, C));
365 } else {
366 ConstantFP *Op1F = cast<ConstantFP>(Op1);
367 if (Op1F->isNullValue())
368 return ReplaceInstUsesWith(I, Op1);
Chris Lattner6c1ce212002-04-29 22:24:47 +0000369
Chris Lattnera2881962003-02-18 19:28:33 +0000370 // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
371 // ANSI says we can drop signals, so we can do this anyway." (from GCC)
372 if (Op1F->getValue() == 1.0)
373 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
374 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000375 }
376
Chris Lattnera4f445b2003-03-10 23:23:04 +0000377 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
378 if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
379 return BinaryOperator::create(Instruction::Mul, Op0v, Op1v);
380
Chris Lattner7e708292002-06-25 16:13:24 +0000381 return Changed ? &I : 0;
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000382}
383
Chris Lattner7e708292002-06-25 16:13:24 +0000384Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
Chris Lattner3f5b8772002-05-06 16:14:14 +0000385 // div X, 1 == X
Chris Lattnera2881962003-02-18 19:28:33 +0000386 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
Chris Lattner233f7dc2002-08-12 21:17:25 +0000387 if (RHS->equalsInt(1))
388 return ReplaceInstUsesWith(I, I.getOperand(0));
Chris Lattnera2881962003-02-18 19:28:33 +0000389
390 // Check to see if this is an unsigned division with an exact power of 2,
391 // if so, convert to a right shift.
392 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
393 if (uint64_t Val = C->getValue()) // Don't break X / 0
394 if (uint64_t C = Log2(Val))
395 return new ShiftInst(Instruction::Shr, I.getOperand(0),
396 ConstantUInt::get(Type::UByteTy, C));
397 }
398
399 // 0 / X == 0, we don't need to preserve faults!
400 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
401 if (LHS->equalsInt(0))
402 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
403
Chris Lattner3f5b8772002-05-06 16:14:14 +0000404 return 0;
405}
406
407
Chris Lattner7e708292002-06-25 16:13:24 +0000408Instruction *InstCombiner::visitRem(BinaryOperator &I) {
Chris Lattnera2881962003-02-18 19:28:33 +0000409 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
410 if (RHS->equalsInt(1)) // X % 1 == 0
411 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
412
413 // Check to see if this is an unsigned remainder with an exact power of 2,
414 // if so, convert to a bitwise and.
415 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
416 if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero)
417 if (Log2(Val))
418 return BinaryOperator::create(Instruction::And, I.getOperand(0),
419 ConstantUInt::get(I.getType(), Val-1));
420 }
421
422 // 0 % X == 0, we don't need to preserve faults!
423 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
424 if (LHS->equalsInt(0))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000425 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
426
Chris Lattner3f5b8772002-05-06 16:14:14 +0000427 return 0;
428}
429
Chris Lattner8b170942002-08-09 23:47:40 +0000430// isMaxValueMinusOne - return true if this is Max-1
Chris Lattner233f7dc2002-08-12 21:17:25 +0000431static bool isMaxValueMinusOne(const ConstantInt *C) {
Chris Lattner8b170942002-08-09 23:47:40 +0000432 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
433 // Calculate -1 casted to the right type...
434 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
435 uint64_t Val = ~0ULL; // All ones
436 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
437 return CU->getValue() == Val-1;
438 }
439
440 const ConstantSInt *CS = cast<ConstantSInt>(C);
441
442 // Calculate 0111111111..11111
443 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
444 int64_t Val = INT64_MAX; // All ones
445 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
446 return CS->getValue() == Val-1;
447}
448
449// isMinValuePlusOne - return true if this is Min+1
Chris Lattner233f7dc2002-08-12 21:17:25 +0000450static bool isMinValuePlusOne(const ConstantInt *C) {
Chris Lattner8b170942002-08-09 23:47:40 +0000451 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
452 return CU->getValue() == 1;
453
454 const ConstantSInt *CS = cast<ConstantSInt>(C);
455
456 // Calculate 1111111111000000000000
457 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
458 int64_t Val = -1; // All ones
459 Val <<= TypeBits-1; // Shift over to the right spot
460 return CS->getValue() == Val+1;
461}
462
463
Chris Lattner7e708292002-06-25 16:13:24 +0000464Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000465 bool Changed = SimplifyCommutative(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000466 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000467
468 // and X, X = X and X, 0 == 0
Chris Lattner233f7dc2002-08-12 21:17:25 +0000469 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
470 return ReplaceInstUsesWith(I, Op1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000471
472 // and X, -1 == X
Chris Lattner572f4a02002-08-13 17:50:24 +0000473 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000474 if (RHS->isAllOnesValue())
475 return ReplaceInstUsesWith(I, Op0);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000476
Chris Lattner8d969642003-03-10 23:06:50 +0000477 Value *Op0NotVal = dyn_castNotVal(Op0);
478 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattnera2881962003-02-18 19:28:33 +0000479
480 // (~A & ~B) == (~(A | B)) - Demorgan's Law
Chris Lattner8d969642003-03-10 23:06:50 +0000481 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
Chris Lattnera2881962003-02-18 19:28:33 +0000482 Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal,
483 Op1NotVal,I.getName()+".demorgan",
484 &I);
Chris Lattnera27231a2003-03-10 23:13:59 +0000485 WorkList.push_back(Or);
Chris Lattnera2881962003-02-18 19:28:33 +0000486 return BinaryOperator::createNot(Or);
487 }
488
489 if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
490 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnere6f9a912002-08-23 18:32:43 +0000491
Chris Lattner7e708292002-06-25 16:13:24 +0000492 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000493}
494
495
496
Chris Lattner7e708292002-06-25 16:13:24 +0000497Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000498 bool Changed = SimplifyCommutative(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000499 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000500
501 // or X, X = X or X, 0 == X
Chris Lattner233f7dc2002-08-12 21:17:25 +0000502 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
503 return ReplaceInstUsesWith(I, Op0);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000504
505 // or X, -1 == -1
Chris Lattner572f4a02002-08-13 17:50:24 +0000506 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattner233f7dc2002-08-12 21:17:25 +0000507 if (RHS->isAllOnesValue())
508 return ReplaceInstUsesWith(I, Op1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000509
Chris Lattnera27231a2003-03-10 23:13:59 +0000510 Value *Op0NotVal = dyn_castNotVal(Op0);
511 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattnera2881962003-02-18 19:28:33 +0000512
Chris Lattnera27231a2003-03-10 23:13:59 +0000513 if (Op1 == Op0NotVal) // ~A | A == -1
514 return ReplaceInstUsesWith(I,
515 ConstantIntegral::getAllOnesValue(I.getType()));
516
517 if (Op0 == Op1NotVal) // A | ~A == -1
518 return ReplaceInstUsesWith(I,
519 ConstantIntegral::getAllOnesValue(I.getType()));
520
521 // (~A | ~B) == (~(A & B)) - Demorgan's Law
522 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
523 Instruction *And = BinaryOperator::create(Instruction::And, Op0NotVal,
524 Op1NotVal,I.getName()+".demorgan",
525 &I);
526 WorkList.push_back(And);
527 return BinaryOperator::createNot(And);
528 }
Chris Lattnera2881962003-02-18 19:28:33 +0000529
Chris Lattner7e708292002-06-25 16:13:24 +0000530 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000531}
532
533
534
Chris Lattner7e708292002-06-25 16:13:24 +0000535Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000536 bool Changed = SimplifyCommutative(I);
Chris Lattner7e708292002-06-25 16:13:24 +0000537 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000538
539 // xor X, X = 0
Chris Lattner233f7dc2002-08-12 21:17:25 +0000540 if (Op0 == Op1)
541 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner3f5b8772002-05-06 16:14:14 +0000542
Chris Lattner572f4a02002-08-13 17:50:24 +0000543 if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattner8b170942002-08-09 23:47:40 +0000544 // xor X, 0 == X
Chris Lattner233f7dc2002-08-12 21:17:25 +0000545 if (Op1C->isNullValue())
546 return ReplaceInstUsesWith(I, Op0);
Chris Lattner8b170942002-08-09 23:47:40 +0000547
Chris Lattner05bd1b22002-08-20 18:24:26 +0000548 // Is this a "NOT" instruction?
549 if (Op1C->isAllOnesValue()) {
550 // xor (xor X, -1), -1 = not (not X) = X
Chris Lattner8d969642003-03-10 23:06:50 +0000551 if (Value *X = dyn_castNotVal(Op0))
Chris Lattneraf2930e2002-08-14 17:51:49 +0000552 return ReplaceInstUsesWith(I, X);
Chris Lattner05bd1b22002-08-20 18:24:26 +0000553
554 // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
555 if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
556 if (SCI->use_size() == 1)
557 return new SetCondInst(SCI->getInverseCondition(),
558 SCI->getOperand(0), SCI->getOperand(1));
559 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000560 }
561
Chris Lattner8d969642003-03-10 23:06:50 +0000562 if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
Chris Lattnera2881962003-02-18 19:28:33 +0000563 if (X == Op1)
564 return ReplaceInstUsesWith(I,
565 ConstantIntegral::getAllOnesValue(I.getType()));
566
Chris Lattner8d969642003-03-10 23:06:50 +0000567 if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
Chris Lattnera2881962003-02-18 19:28:33 +0000568 if (X == Op0)
569 return ReplaceInstUsesWith(I,
570 ConstantIntegral::getAllOnesValue(I.getType()));
571
Chris Lattnercb40a372003-03-10 18:24:17 +0000572 if (Instruction *Op1I = dyn_cast<Instruction>(Op1))
573 if (Op1I->getOpcode() == Instruction::Or)
574 if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B
575 cast<BinaryOperator>(Op1I)->swapOperands();
576 I.swapOperands();
577 std::swap(Op0, Op1);
578 } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B
579 I.swapOperands();
580 std::swap(Op0, Op1);
581 }
582
583 if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
584 if (Op0I->getOpcode() == Instruction::Or && Op0I->use_size() == 1) {
585 if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B
586 cast<BinaryOperator>(Op0I)->swapOperands();
Chris Lattner4f98c562003-03-10 21:43:22 +0000587 if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B
Chris Lattnercb40a372003-03-10 18:24:17 +0000588 Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I);
589 WorkList.push_back(cast<Instruction>(NotB));
Chris Lattner4f98c562003-03-10 21:43:22 +0000590 return BinaryOperator::create(Instruction::And, Op0I->getOperand(0),
591 NotB);
Chris Lattnercb40a372003-03-10 18:24:17 +0000592 }
593 }
594
Chris Lattnerc8802d22003-03-11 00:12:48 +0000595 // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1^C2 == 0
596 if (Constant *C1 = dyn_castMaskingAnd(Op0))
597 if (Constant *C2 = dyn_castMaskingAnd(Op1))
598 if ((*C1 & *C2)->isNullValue())
599 return BinaryOperator::create(Instruction::Or, Op0, Op1);
600
Chris Lattner7e708292002-06-25 16:13:24 +0000601 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000602}
603
Chris Lattner8b170942002-08-09 23:47:40 +0000604// AddOne, SubOne - Add or subtract a constant one from an integer constant...
605static Constant *AddOne(ConstantInt *C) {
606 Constant *Result = *C + *ConstantInt::get(C->getType(), 1);
607 assert(Result && "Constant folding integer addition failed!");
608 return Result;
609}
610static Constant *SubOne(ConstantInt *C) {
611 Constant *Result = *C - *ConstantInt::get(C->getType(), 1);
612 assert(Result && "Constant folding integer addition failed!");
613 return Result;
614}
615
Chris Lattner53a5b572002-05-09 20:11:54 +0000616// isTrueWhenEqual - Return true if the specified setcondinst instruction is
617// true when both operands are equal...
618//
Chris Lattner7e708292002-06-25 16:13:24 +0000619static bool isTrueWhenEqual(Instruction &I) {
620 return I.getOpcode() == Instruction::SetEQ ||
621 I.getOpcode() == Instruction::SetGE ||
622 I.getOpcode() == Instruction::SetLE;
Chris Lattner53a5b572002-05-09 20:11:54 +0000623}
624
Chris Lattner7e708292002-06-25 16:13:24 +0000625Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
Chris Lattner4f98c562003-03-10 21:43:22 +0000626 bool Changed = SimplifyCommutative(I);
Chris Lattner8b170942002-08-09 23:47:40 +0000627 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
628 const Type *Ty = Op0->getType();
Chris Lattner3f5b8772002-05-06 16:14:14 +0000629
630 // setcc X, X
Chris Lattner8b170942002-08-09 23:47:40 +0000631 if (Op0 == Op1)
632 return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
Chris Lattner53a5b572002-05-09 20:11:54 +0000633
634 // setcc <global*>, 0 - Global value addresses are never null!
Chris Lattner8b170942002-08-09 23:47:40 +0000635 if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
636 return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
637
638 // setcc's with boolean values can always be turned into bitwise operations
639 if (Ty == Type::BoolTy) {
640 // If this is <, >, or !=, we can change this into a simple xor instruction
641 if (!isTrueWhenEqual(I))
642 return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
643
644 // Otherwise we need to make a temporary intermediate instruction and insert
645 // it into the instruction stream. This is what we are after:
646 //
647 // seteq bool %A, %B -> ~(A^B)
648 // setle bool %A, %B -> ~A | B
649 // setge bool %A, %B -> A | ~B
650 //
651 if (I.getOpcode() == Instruction::SetEQ) { // seteq case
652 Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
653 I.getName()+"tmp");
654 InsertNewInstBefore(Xor, I);
Chris Lattneraf2930e2002-08-14 17:51:49 +0000655 return BinaryOperator::createNot(Xor, I.getName());
Chris Lattner8b170942002-08-09 23:47:40 +0000656 }
657
658 // Handle the setXe cases...
659 assert(I.getOpcode() == Instruction::SetGE ||
660 I.getOpcode() == Instruction::SetLE);
661
662 if (I.getOpcode() == Instruction::SetGE)
663 std::swap(Op0, Op1); // Change setge -> setle
664
665 // Now we just have the SetLE case.
Chris Lattneraf2930e2002-08-14 17:51:49 +0000666 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
Chris Lattner8b170942002-08-09 23:47:40 +0000667 InsertNewInstBefore(Not, I);
668 return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
669 }
670
671 // Check to see if we are doing one of many comparisons against constant
672 // integers at the end of their ranges...
673 //
674 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
675 // Check to see if we are comparing against the minimum or maximum value...
Chris Lattner233f7dc2002-08-12 21:17:25 +0000676 if (CI->isMinValue()) {
Chris Lattner8b170942002-08-09 23:47:40 +0000677 if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE
678 return ReplaceInstUsesWith(I, ConstantBool::False);
679 if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE
680 return ReplaceInstUsesWith(I, ConstantBool::True);
681 if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN
682 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
683 if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN
684 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
685
Chris Lattner233f7dc2002-08-12 21:17:25 +0000686 } else if (CI->isMaxValue()) {
Chris Lattner8b170942002-08-09 23:47:40 +0000687 if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE
688 return ReplaceInstUsesWith(I, ConstantBool::False);
689 if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE
690 return ReplaceInstUsesWith(I, ConstantBool::True);
691 if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX
692 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
693 if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX
694 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
695
696 // Comparing against a value really close to min or max?
697 } else if (isMinValuePlusOne(CI)) {
698 if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN
699 return BinaryOperator::create(Instruction::SetEQ, Op0,
700 SubOne(CI), I.getName());
701 if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN
702 return BinaryOperator::create(Instruction::SetNE, Op0,
703 SubOne(CI), I.getName());
704
705 } else if (isMaxValueMinusOne(CI)) {
706 if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX
707 return BinaryOperator::create(Instruction::SetEQ, Op0,
708 AddOne(CI), I.getName());
709 if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX
710 return BinaryOperator::create(Instruction::SetNE, Op0,
711 AddOne(CI), I.getName());
712 }
Chris Lattner3f5b8772002-05-06 16:14:14 +0000713 }
714
Chris Lattner7e708292002-06-25 16:13:24 +0000715 return Changed ? &I : 0;
Chris Lattner3f5b8772002-05-06 16:14:14 +0000716}
717
718
719
Chris Lattnerea340052003-03-10 19:16:08 +0000720Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
Chris Lattner7e708292002-06-25 16:13:24 +0000721 assert(I.getOperand(1)->getType() == Type::UByteTy);
722 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000723
724 // shl X, 0 == X and shr X, 0 == X
725 // shl 0, X == 0 and shr 0, X == 0
726 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
Chris Lattner233f7dc2002-08-12 21:17:25 +0000727 Op0 == Constant::getNullValue(Op0->getType()))
728 return ReplaceInstUsesWith(I, Op0);
Chris Lattner3f5b8772002-05-06 16:14:14 +0000729
Chris Lattnerea340052003-03-10 19:16:08 +0000730 // If this is a shift of a shift, see if we can fold the two together...
731 if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) {
732 if (isa<Constant>(Op1) && isa<Constant>(Op0SI->getOperand(1))) {
733 ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(Op0SI->getOperand(1));
734 unsigned ShiftAmt1 = ShiftAmt1C->getValue();
735 unsigned ShiftAmt2 = cast<ConstantUInt>(Op1)->getValue();
736
737 // Check for (A << c1) << c2 and (A >> c1) >> c2
738 if (I.getOpcode() == Op0SI->getOpcode()) {
739 unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift...
740 return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0),
741 ConstantUInt::get(Type::UByteTy, Amt));
742 }
743
744 if (I.getType()->isUnsigned()) { // Check for (A << c1) >> c2 or visaversa
745 // Calculate bitmask for what gets shifted off the edge...
746 Constant *C = ConstantIntegral::getAllOnesValue(I.getType());
747 if (I.getOpcode() == Instruction::Shr)
748 C = *C >> *ShiftAmt1C;
749 else
750 C = *C << *ShiftAmt1C;
751 assert(C && "Couldn't constant fold shift expression?");
752
753 Instruction *Mask =
754 BinaryOperator::create(Instruction::And, Op0SI->getOperand(0),
755 C, Op0SI->getOperand(0)->getName()+".mask",&I);
756 WorkList.push_back(Mask);
757
758 // Figure out what flavor of shift we should use...
759 if (ShiftAmt1 == ShiftAmt2)
760 return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2
761 else if (ShiftAmt1 < ShiftAmt2) {
762 return new ShiftInst(I.getOpcode(), Mask,
763 ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1));
764 } else {
765 return new ShiftInst(Op0SI->getOpcode(), Mask,
766 ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
767 }
768 }
769 }
770 }
771
Chris Lattner233f7dc2002-08-12 21:17:25 +0000772 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
Chris Lattner3f5b8772002-05-06 16:14:14 +0000773 // a signed value.
774 //
775 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
Chris Lattnerea340052003-03-10 19:16:08 +0000776 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
777 if (CUI->getValue() >= TypeBits &&
778 (!Op0->getType()->isSigned() || I.getOpcode() == Instruction::Shl))
779 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
Chris Lattnerf2836082002-09-10 23:04:09 +0000780
781 // Check to see if we are shifting left by 1. If so, turn it into an add
782 // instruction.
783 if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
Chris Lattnera2881962003-02-18 19:28:33 +0000784 // Convert 'shl int %X, 1' to 'add int %X, %X'
Chris Lattnerf2836082002-09-10 23:04:09 +0000785 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
786
Chris Lattner3f5b8772002-05-06 16:14:14 +0000787 }
Chris Lattner6eaeb572002-10-08 16:16:40 +0000788
789 // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
790 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
791 if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue())
792 return ReplaceInstUsesWith(I, CSI);
793
Chris Lattner3f5b8772002-05-06 16:14:14 +0000794 return 0;
795}
796
797
Chris Lattnera1be5662002-05-02 17:06:02 +0000798// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
799// instruction.
800//
Chris Lattner7e708292002-06-25 16:13:24 +0000801static inline bool isEliminableCastOfCast(const CastInst &CI,
Chris Lattnera1be5662002-05-02 17:06:02 +0000802 const CastInst *CSrc) {
Chris Lattner7e708292002-06-25 16:13:24 +0000803 assert(CI.getOperand(0) == CSrc);
Chris Lattnera1be5662002-05-02 17:06:02 +0000804 const Type *SrcTy = CSrc->getOperand(0)->getType();
805 const Type *MidTy = CSrc->getType();
Chris Lattner7e708292002-06-25 16:13:24 +0000806 const Type *DstTy = CI.getType();
Chris Lattnera1be5662002-05-02 17:06:02 +0000807
Chris Lattner8fd217c2002-08-02 20:00:25 +0000808 // It is legal to eliminate the instruction if casting A->B->A if the sizes
809 // are identical and the bits don't get reinterpreted (for example
Chris Lattner5cf6f112002-08-14 23:21:10 +0000810 // int->float->int would not be allowed)
Misha Brukmanf117cc92003-05-20 18:45:36 +0000811 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
Chris Lattner8fd217c2002-08-02 20:00:25 +0000812 return true;
Chris Lattnera1be5662002-05-02 17:06:02 +0000813
814 // Allow free casting and conversion of sizes as long as the sign doesn't
815 // change...
Chris Lattner0c4e8862002-09-03 01:08:28 +0000816 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
Chris Lattner8fd217c2002-08-02 20:00:25 +0000817 unsigned SrcSize = SrcTy->getPrimitiveSize();
818 unsigned MidSize = MidTy->getPrimitiveSize();
819 unsigned DstSize = DstTy->getPrimitiveSize();
Chris Lattner8fd217c2002-08-02 20:00:25 +0000820
Chris Lattner3ecce662002-08-15 16:15:25 +0000821 // Cases where we are monotonically decreasing the size of the type are
822 // always ok, regardless of what sign changes are going on.
823 //
Chris Lattner5cf6f112002-08-14 23:21:10 +0000824 if (SrcSize >= MidSize && MidSize >= DstSize)
Chris Lattner8fd217c2002-08-02 20:00:25 +0000825 return true;
Chris Lattner3ecce662002-08-15 16:15:25 +0000826
Chris Lattnerd06451f2002-09-23 23:39:43 +0000827 // Cases where the source and destination type are the same, but the middle
828 // type is bigger are noops.
829 //
830 if (SrcSize == DstSize && MidSize > SrcSize)
831 return true;
832
Chris Lattner3ecce662002-08-15 16:15:25 +0000833 // If we are monotonically growing, things are more complex.
834 //
835 if (SrcSize <= MidSize && MidSize <= DstSize) {
836 // We have eight combinations of signedness to worry about. Here's the
837 // table:
838 static const int SignTable[8] = {
839 // CODE, SrcSigned, MidSigned, DstSigned, Comment
840 1, // U U U Always ok
841 1, // U U S Always ok
842 3, // U S U Ok iff SrcSize != MidSize
843 3, // U S S Ok iff SrcSize != MidSize
844 0, // S U U Never ok
845 2, // S U S Ok iff MidSize == DstSize
846 1, // S S U Always ok
847 1, // S S S Always ok
848 };
849
850 // Choose an action based on the current entry of the signtable that this
851 // cast of cast refers to...
852 unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
853 switch (SignTable[Row]) {
854 case 0: return false; // Never ok
855 case 1: return true; // Always ok
856 case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
857 case 3: // Ok iff SrcSize != MidSize
858 return SrcSize != MidSize || SrcTy == Type::BoolTy;
859 default: assert(0 && "Bad entry in sign table!");
860 }
Chris Lattner3ecce662002-08-15 16:15:25 +0000861 }
Chris Lattner8fd217c2002-08-02 20:00:25 +0000862 }
Chris Lattnera1be5662002-05-02 17:06:02 +0000863
864 // Otherwise, we cannot succeed. Specifically we do not want to allow things
865 // like: short -> ushort -> uint, because this can create wrong results if
866 // the input short is negative!
867 //
868 return false;
869}
870
871
872// CastInst simplification
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000873//
Chris Lattner7e708292002-06-25 16:13:24 +0000874Instruction *InstCombiner::visitCastInst(CastInst &CI) {
Chris Lattnera1be5662002-05-02 17:06:02 +0000875 // If the user is casting a value to the same type, eliminate this cast
876 // instruction...
Chris Lattner233f7dc2002-08-12 21:17:25 +0000877 if (CI.getType() == CI.getOperand(0)->getType())
878 return ReplaceInstUsesWith(CI, CI.getOperand(0));
Chris Lattnera1be5662002-05-02 17:06:02 +0000879
Chris Lattnera1be5662002-05-02 17:06:02 +0000880 // If casting the result of another cast instruction, try to eliminate this
881 // one!
882 //
Chris Lattner8fd217c2002-08-02 20:00:25 +0000883 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
Chris Lattnera1be5662002-05-02 17:06:02 +0000884 if (isEliminableCastOfCast(CI, CSrc)) {
885 // This instruction now refers directly to the cast's src operand. This
886 // has a good chance of making CSrc dead.
Chris Lattner7e708292002-06-25 16:13:24 +0000887 CI.setOperand(0, CSrc->getOperand(0));
888 return &CI;
Chris Lattnera1be5662002-05-02 17:06:02 +0000889 }
890
Chris Lattner8fd217c2002-08-02 20:00:25 +0000891 // If this is an A->B->A cast, and we are dealing with integral types, try
892 // to convert this into a logical 'and' instruction.
893 //
894 if (CSrc->getOperand(0)->getType() == CI.getType() &&
Chris Lattner0c4e8862002-09-03 01:08:28 +0000895 CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
Chris Lattner8fd217c2002-08-02 20:00:25 +0000896 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
897 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
898 assert(CSrc->getType() != Type::ULongTy &&
899 "Cannot have type bigger than ulong!");
900 unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
901 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
902 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
903 AndOp);
904 }
905 }
906
Chris Lattnerdd841ae2002-04-18 17:39:14 +0000907 return 0;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000908}
909
Chris Lattnera1be5662002-05-02 17:06:02 +0000910
Chris Lattner473945d2002-05-06 18:06:38 +0000911// PHINode simplification
912//
Chris Lattner7e708292002-06-25 16:13:24 +0000913Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Chris Lattner473945d2002-05-06 18:06:38 +0000914 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattner233f7dc2002-08-12 21:17:25 +0000915 if (PN.getNumIncomingValues() == 1)
916 return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
Chris Lattnerf02c4682002-08-20 15:35:35 +0000917
918 // Otherwise if all of the incoming values are the same for the PHI, replace
919 // the PHI node with the incoming value.
920 //
Chris Lattnerc20e2452002-08-22 20:22:01 +0000921 Value *InVal = 0;
922 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
923 if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself...
924 if (InVal && PN.getIncomingValue(i) != InVal)
925 return 0; // Not the same, bail out.
926 else
927 InVal = PN.getIncomingValue(i);
928
929 // The only case that could cause InVal to be null is if we have a PHI node
930 // that only has entries for itself. In this case, there is no entry into the
931 // loop, so kill the PHI.
932 //
933 if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
Chris Lattner473945d2002-05-06 18:06:38 +0000934
Chris Lattnerf02c4682002-08-20 15:35:35 +0000935 // All of the incoming values are the same, replace the PHI node now.
936 return ReplaceInstUsesWith(PN, InVal);
Chris Lattner473945d2002-05-06 18:06:38 +0000937}
938
Chris Lattnera1be5662002-05-02 17:06:02 +0000939
Chris Lattner7e708292002-06-25 16:13:24 +0000940Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerc54e2b82003-05-22 19:07:21 +0000941 // Is it 'getelementptr %P, long 0' or 'getelementptr %P'
Chris Lattner7e708292002-06-25 16:13:24 +0000942 // If so, eliminate the noop.
Chris Lattner90ac28c2002-08-02 19:29:35 +0000943 if ((GEP.getNumOperands() == 2 &&
Chris Lattner3cac88a2002-09-11 01:21:33 +0000944 GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
Chris Lattner233f7dc2002-08-12 21:17:25 +0000945 GEP.getNumOperands() == 1)
946 return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
Chris Lattnera1be5662002-05-02 17:06:02 +0000947
Chris Lattner90ac28c2002-08-02 19:29:35 +0000948 // Combine Indices - If the source pointer to this getelementptr instruction
949 // is a getelementptr instruction, combine the indices of the two
950 // getelementptr instructions into a single instruction.
951 //
Chris Lattner9b761232002-08-17 22:21:59 +0000952 if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000953 std::vector<Value *> Indices;
Chris Lattner8a2a3112001-12-14 16:52:21 +0000954
Chris Lattner90ac28c2002-08-02 19:29:35 +0000955 // Can we combine the two pointer arithmetics offsets?
Chris Lattnerc54e2b82003-05-22 19:07:21 +0000956 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
957 isa<Constant>(GEP.getOperand(1))) {
Chris Lattnerdecd0812003-03-05 22:33:14 +0000958 // Replace: gep (gep %P, long C1), long C2, ...
959 // With: gep %P, long (C1+C2), ...
960 Value *Sum = *cast<Constant>(Src->getOperand(1)) +
Chris Lattner90ac28c2002-08-02 19:29:35 +0000961 *cast<Constant>(GEP.getOperand(1));
Chris Lattnerdecd0812003-03-05 22:33:14 +0000962 assert(Sum && "Constant folding of longs failed!?");
963 GEP.setOperand(0, Src->getOperand(0));
964 GEP.setOperand(1, Sum);
965 AddUsesToWorkList(*Src); // Reduce use count of Src
966 return &GEP;
Chris Lattnerc54e2b82003-05-22 19:07:21 +0000967 } else if (Src->getNumOperands() == 2) {
Chris Lattnerdecd0812003-03-05 22:33:14 +0000968 // Replace: gep (gep %P, long B), long A, ...
969 // With: T = long A+B; gep %P, T, ...
970 //
971 Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1),
972 GEP.getOperand(1),
973 Src->getName()+".sum", &GEP);
974 GEP.setOperand(0, Src->getOperand(0));
975 GEP.setOperand(1, Sum);
976 WorkList.push_back(cast<Instruction>(Sum));
977 return &GEP;
Chris Lattner01885342002-11-04 16:43:32 +0000978 } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
Chris Lattnerdfcbf012002-09-17 21:05:42 +0000979 Src->getNumOperands() != 1) {
Chris Lattner90ac28c2002-08-02 19:29:35 +0000980 // Otherwise we can do the fold if the first index of the GEP is a zero
981 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
982 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
Chris Lattner01885342002-11-04 16:43:32 +0000983 } else if (Src->getOperand(Src->getNumOperands()-1) ==
984 Constant::getNullValue(Type::LongTy)) {
985 // If the src gep ends with a constant array index, merge this get into
986 // it, even if we have a non-zero array index.
987 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1);
988 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
Chris Lattner90ac28c2002-08-02 19:29:35 +0000989 }
990
991 if (!Indices.empty())
992 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
Chris Lattner9b761232002-08-17 22:21:59 +0000993
994 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
995 // GEP of global variable. If all of the indices for this GEP are
996 // constants, we can promote this to a constexpr instead of an instruction.
997
998 // Scan for nonconstants...
999 std::vector<Constant*> Indices;
1000 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
1001 for (; I != E && isa<Constant>(*I); ++I)
1002 Indices.push_back(cast<Constant>(*I));
1003
1004 if (I == E) { // If they are all constants...
Chris Lattnerfb242b62003-04-16 22:40:51 +00001005 Constant *CE =
Chris Lattner9b761232002-08-17 22:21:59 +00001006 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
1007
1008 // Replace all uses of the GEP with the new constexpr...
1009 return ReplaceInstUsesWith(GEP, CE);
1010 }
Chris Lattner8a2a3112001-12-14 16:52:21 +00001011 }
1012
Chris Lattner8a2a3112001-12-14 16:52:21 +00001013 return 0;
1014}
1015
Chris Lattner0864acf2002-11-04 16:18:53 +00001016Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
1017 // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
1018 if (AI.isArrayAllocation()) // Check C != 1
1019 if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
1020 const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
Chris Lattner0006bd72002-11-09 00:49:43 +00001021 AllocationInst *New = 0;
Chris Lattner0864acf2002-11-04 16:18:53 +00001022
1023 // Create and insert the replacement instruction...
1024 if (isa<MallocInst>(AI))
1025 New = new MallocInst(NewTy, 0, AI.getName(), &AI);
Chris Lattner0006bd72002-11-09 00:49:43 +00001026 else {
1027 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
Chris Lattner0864acf2002-11-04 16:18:53 +00001028 New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
Chris Lattner0006bd72002-11-09 00:49:43 +00001029 }
Chris Lattner0864acf2002-11-04 16:18:53 +00001030
1031 // Scan to the end of the allocation instructions, to skip over a block of
1032 // allocas if possible...
1033 //
1034 BasicBlock::iterator It = New;
1035 while (isa<AllocationInst>(*It)) ++It;
1036
1037 // Now that I is pointing to the first non-allocation-inst in the block,
1038 // insert our getelementptr instruction...
1039 //
1040 std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
1041 Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
1042
1043 // Now make everything use the getelementptr instead of the original
1044 // allocation.
1045 ReplaceInstUsesWith(AI, V);
1046 return &AI;
1047 }
1048 return 0;
1049}
1050
1051
Chris Lattner8a2a3112001-12-14 16:52:21 +00001052
Chris Lattner62b14df2002-09-02 04:59:56 +00001053void InstCombiner::removeFromWorkList(Instruction *I) {
1054 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
1055 WorkList.end());
1056}
1057
Chris Lattner7e708292002-06-25 16:13:24 +00001058bool InstCombiner::runOnFunction(Function &F) {
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001059 bool Changed = false;
Chris Lattner8a2a3112001-12-14 16:52:21 +00001060
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001061 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattner8a2a3112001-12-14 16:52:21 +00001062
1063 while (!WorkList.empty()) {
1064 Instruction *I = WorkList.back(); // Get an instruction from the worklist
1065 WorkList.pop_back();
1066
Misha Brukmana3bbcb52002-10-29 23:06:16 +00001067 // Check to see if we can DCE or ConstantPropagate the instruction...
Chris Lattner62b14df2002-09-02 04:59:56 +00001068 // Check to see if we can DIE the instruction...
1069 if (isInstructionTriviallyDead(I)) {
1070 // Add operands to the worklist...
1071 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1072 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1073 WorkList.push_back(Op);
1074
1075 ++NumDeadInst;
1076 BasicBlock::iterator BBI = I;
1077 if (dceInstruction(BBI)) {
1078 removeFromWorkList(I);
1079 continue;
1080 }
1081 }
1082
Misha Brukmana3bbcb52002-10-29 23:06:16 +00001083 // Instruction isn't dead, see if we can constant propagate it...
Chris Lattner62b14df2002-09-02 04:59:56 +00001084 if (Constant *C = ConstantFoldInstruction(I)) {
1085 // Add operands to the worklist...
1086 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1087 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1088 WorkList.push_back(Op);
Chris Lattnerc736d562002-12-05 22:41:53 +00001089 ReplaceInstUsesWith(*I, C);
1090
Chris Lattner62b14df2002-09-02 04:59:56 +00001091 ++NumConstProp;
1092 BasicBlock::iterator BBI = I;
1093 if (dceInstruction(BBI)) {
1094 removeFromWorkList(I);
1095 continue;
1096 }
1097 }
1098
Chris Lattner8a2a3112001-12-14 16:52:21 +00001099 // Now that we have an instruction, try combining it to simplify it...
Chris Lattner90ac28c2002-08-02 19:29:35 +00001100 if (Instruction *Result = visit(*I)) {
Chris Lattner3dec1f22002-05-10 15:38:35 +00001101 ++NumCombined;
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001102 // Should we replace the old instruction with a new one?
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +00001103 if (Result != I) {
1104 // Instructions can end up on the worklist more than once. Make sure
1105 // we do not process an instruction that has been deleted.
Chris Lattner62b14df2002-09-02 04:59:56 +00001106 removeFromWorkList(I);
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001107 ReplaceInstWithInst(I, Result);
Chris Lattner7e708292002-06-25 16:13:24 +00001108 } else {
Chris Lattner90ac28c2002-08-02 19:29:35 +00001109 BasicBlock::iterator II = I;
1110
1111 // If the instruction was modified, it's possible that it is now dead.
1112 // if so, remove it.
1113 if (dceInstruction(II)) {
1114 // Instructions may end up in the worklist more than once. Erase them
1115 // all.
Chris Lattner62b14df2002-09-02 04:59:56 +00001116 removeFromWorkList(I);
Chris Lattner90ac28c2002-08-02 19:29:35 +00001117 Result = 0;
1118 }
Chris Lattnerb3bc8fa2002-05-14 15:24:07 +00001119 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001120
Chris Lattner90ac28c2002-08-02 19:29:35 +00001121 if (Result) {
1122 WorkList.push_back(Result);
1123 AddUsesToWorkList(*Result);
1124 }
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001125 Changed = true;
Chris Lattner8a2a3112001-12-14 16:52:21 +00001126 }
1127 }
1128
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001129 return Changed;
Chris Lattnerbd0ef772002-02-26 21:46:54 +00001130}
1131
1132Pass *createInstructionCombiningPass() {
Chris Lattnerdd841ae2002-04-18 17:39:14 +00001133 return new InstCombiner();
Chris Lattnerbd0ef772002-02-26 21:46:54 +00001134}