blob: e69db1d463da5c934b933d56d1f15d5fe5127806 [file] [log] [blame]
Chris Lattnere6794492002-08-12 21:17:25 +00001//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
Chris Lattnerca081252001-12-14 16:52:21 +00002//
3// InstructionCombining - Combine instructions to form fewer, simple
Chris Lattner99f48c62002-09-02 04:59:56 +00004// instructions. This pass does not modify the CFG This pass is where algebraic
5// simplification happens.
Chris Lattnerca081252001-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//
Chris Lattner216c7b82003-09-10 05:29:43 +000015// This pass guarantees that the following canonicalizations are performed on
Chris Lattnerbfb1d032003-07-23 21:41:57 +000016// the program:
17// 1. If a binary operator has a constant operand, it is moved to the RHS
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +000018// 2. Bitwise operators with constant operands are always grouped so that
19// shifts are performed first, then or's, then and's, then xor's.
Chris Lattnerbfb1d032003-07-23 21:41:57 +000020// 3. SetCC instructions are converted from <,>,<=,>= to ==,!= if possible
21// 4. All SetCC instructions on boolean values are replaced with logical ops
Chris Lattnerede3fe02003-08-13 04:18:28 +000022// 5. add X, X is represented as (X*2) => (X << 1)
23// 6. Multiplies with a power-of-two constant argument are transformed into
24// shifts.
Chris Lattnerbfb1d032003-07-23 21:41:57 +000025// N. This list is incomplete
26//
Chris Lattnerca081252001-12-14 16:52:21 +000027//===----------------------------------------------------------------------===//
28
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000029#include "llvm/Transforms/Scalar.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000030#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerae7a0d32002-08-02 19:29:35 +000031#include "llvm/Transforms/Utils/Local.h"
Chris Lattner471bd762003-05-22 19:07:21 +000032#include "llvm/Instructions.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000033#include "llvm/Pass.h"
Chris Lattner34428442003-05-27 16:40:51 +000034#include "llvm/Constants.h"
35#include "llvm/ConstantHandling.h"
Chris Lattner1085bdf2002-11-04 16:18:53 +000036#include "llvm/DerivedTypes.h"
Chris Lattner0f1d8a32003-06-26 05:06:25 +000037#include "llvm/GlobalVariable.h"
Chris Lattner60a65912002-02-12 21:07:25 +000038#include "llvm/Support/InstIterator.h"
Chris Lattner260ab202002-04-18 17:39:14 +000039#include "llvm/Support/InstVisitor.h"
Chris Lattner970c33a2003-06-19 17:00:31 +000040#include "llvm/Support/CallSite.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000041#include "Support/Statistic.h"
Chris Lattner053c0932002-05-14 15:24:07 +000042#include <algorithm>
Chris Lattnerca081252001-12-14 16:52:21 +000043
Chris Lattner260ab202002-04-18 17:39:14 +000044namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000045 Statistic<> NumCombined ("instcombine", "Number of insts combined");
46 Statistic<> NumConstProp("instcombine", "Number of constant folds");
47 Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
48
Chris Lattnerc8e66542002-04-27 06:56:12 +000049 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000050 public InstVisitor<InstCombiner, Instruction*> {
51 // Worklist of all of the instructions that need to be simplified.
52 std::vector<Instruction*> WorkList;
53
Chris Lattner113f4f42002-06-25 16:13:24 +000054 void AddUsesToWorkList(Instruction &I) {
Chris Lattner260ab202002-04-18 17:39:14 +000055 // The instruction was simplified, add all users of the instruction to
56 // the work lists because they might get more simplified now...
57 //
Chris Lattner113f4f42002-06-25 16:13:24 +000058 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner260ab202002-04-18 17:39:14 +000059 UI != UE; ++UI)
60 WorkList.push_back(cast<Instruction>(*UI));
61 }
62
Chris Lattner99f48c62002-09-02 04:59:56 +000063 // removeFromWorkList - remove all instances of I from the worklist.
64 void removeFromWorkList(Instruction *I);
Chris Lattner260ab202002-04-18 17:39:14 +000065 public:
Chris Lattner113f4f42002-06-25 16:13:24 +000066 virtual bool runOnFunction(Function &F);
Chris Lattner260ab202002-04-18 17:39:14 +000067
Chris Lattnerf12cc842002-04-28 21:27:06 +000068 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000069 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000070 }
71
Chris Lattner260ab202002-04-18 17:39:14 +000072 // Visitation implementation - Implement instruction combining for different
73 // instruction types. The semantics are as follows:
74 // Return Value:
75 // null - No change was made
Chris Lattnere6794492002-08-12 21:17:25 +000076 // I - Change was made, I is still valid, I may be dead though
Chris Lattner260ab202002-04-18 17:39:14 +000077 // otherwise - Change was made, replace I with returned instruction
78 //
Chris Lattner113f4f42002-06-25 16:13:24 +000079 Instruction *visitAdd(BinaryOperator &I);
80 Instruction *visitSub(BinaryOperator &I);
81 Instruction *visitMul(BinaryOperator &I);
82 Instruction *visitDiv(BinaryOperator &I);
83 Instruction *visitRem(BinaryOperator &I);
84 Instruction *visitAnd(BinaryOperator &I);
85 Instruction *visitOr (BinaryOperator &I);
86 Instruction *visitXor(BinaryOperator &I);
87 Instruction *visitSetCondInst(BinaryOperator &I);
Chris Lattnere8d6c602003-03-10 19:16:08 +000088 Instruction *visitShiftInst(ShiftInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +000089 Instruction *visitCastInst(CastInst &CI);
Chris Lattner970c33a2003-06-19 17:00:31 +000090 Instruction *visitCallInst(CallInst &CI);
91 Instruction *visitInvokeInst(InvokeInst &II);
Chris Lattner113f4f42002-06-25 16:13:24 +000092 Instruction *visitPHINode(PHINode &PN);
93 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
Chris Lattner1085bdf2002-11-04 16:18:53 +000094 Instruction *visitAllocationInst(AllocationInst &AI);
Chris Lattner0f1d8a32003-06-26 05:06:25 +000095 Instruction *visitLoadInst(LoadInst &LI);
Chris Lattner9eef8a72003-06-04 04:46:00 +000096 Instruction *visitBranchInst(BranchInst &BI);
Chris Lattner260ab202002-04-18 17:39:14 +000097
98 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +000099 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000100
Chris Lattner970c33a2003-06-19 17:00:31 +0000101 private:
102 bool transformConstExprCastCall(CallSite CS);
103
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000104 // InsertNewInstBefore - insert an instruction New before instruction Old
105 // in the program. Add the new instruction to the worklist.
106 //
107 void InsertNewInstBefore(Instruction *New, Instruction &Old) {
Chris Lattner65217ff2002-08-23 18:32:43 +0000108 assert(New && New->getParent() == 0 &&
109 "New instruction already inserted into a basic block!");
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000110 BasicBlock *BB = Old.getParent();
111 BB->getInstList().insert(&Old, New); // Insert inst
112 WorkList.push_back(New); // Add to worklist
113 }
114
Chris Lattner3ac7c262003-08-13 20:16:26 +0000115 public:
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000116 // ReplaceInstUsesWith - This method is to be used when an instruction is
117 // found to be dead, replacable with another preexisting expression. Here
118 // we add all uses of I to the worklist, replace all uses of I with the new
119 // value, then return I, so that the inst combiner will know that I was
120 // modified.
121 //
122 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
123 AddUsesToWorkList(I); // Add all modified instrs to worklist
124 I.replaceAllUsesWith(V);
125 return &I;
126 }
Chris Lattner3ac7c262003-08-13 20:16:26 +0000127 private:
Chris Lattnerdfae8be2003-07-24 17:35:25 +0000128 /// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
129 /// InsertBefore instruction. This is specialized a bit to avoid inserting
130 /// casts that are known to not do anything...
131 ///
132 Value *InsertOperandCastBefore(Value *V, const Type *DestTy,
133 Instruction *InsertBefore);
134
Chris Lattner7fb29e12003-03-11 00:12:48 +0000135 // SimplifyCommutative - This performs a few simplifications for commutative
136 // operators...
137 bool SimplifyCommutative(BinaryOperator &I);
Chris Lattnerba1cb382003-09-19 17:17:26 +0000138
139 Instruction *OptAndOp(Instruction *Op, ConstantIntegral *OpRHS,
140 ConstantIntegral *AndRHS, BinaryOperator &TheAnd);
Chris Lattner260ab202002-04-18 17:39:14 +0000141 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000142
Chris Lattnerc8b70922002-07-26 21:12:46 +0000143 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattner260ab202002-04-18 17:39:14 +0000144}
145
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000146// getComplexity: Assign a complexity or rank value to LLVM Values...
147// 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst
148static unsigned getComplexity(Value *V) {
149 if (isa<Instruction>(V)) {
150 if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
151 return 2;
152 return 3;
153 }
154 if (isa<Argument>(V)) return 2;
155 return isa<Constant>(V) ? 0 : 1;
156}
Chris Lattner260ab202002-04-18 17:39:14 +0000157
Chris Lattner7fb29e12003-03-11 00:12:48 +0000158// isOnlyUse - Return true if this instruction will be deleted if we stop using
159// it.
160static bool isOnlyUse(Value *V) {
161 return V->use_size() == 1 || isa<Constant>(V);
162}
163
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000164// SimplifyCommutative - This performs a few simplifications for commutative
165// operators:
Chris Lattner260ab202002-04-18 17:39:14 +0000166//
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000167// 1. Order operands such that they are listed from right (least complex) to
168// left (most complex). This puts constants before unary operators before
169// binary operators.
170//
Chris Lattner7fb29e12003-03-11 00:12:48 +0000171// 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
172// 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000173//
Chris Lattner7fb29e12003-03-11 00:12:48 +0000174bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000175 bool Changed = false;
176 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
177 Changed = !I.swapOperands();
178
179 if (!I.isAssociative()) return Changed;
180 Instruction::BinaryOps Opcode = I.getOpcode();
Chris Lattner7fb29e12003-03-11 00:12:48 +0000181 if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
182 if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
183 if (isa<Constant>(I.getOperand(1))) {
Chris Lattner34428442003-05-27 16:40:51 +0000184 Constant *Folded = ConstantExpr::get(I.getOpcode(),
185 cast<Constant>(I.getOperand(1)),
186 cast<Constant>(Op->getOperand(1)));
Chris Lattner7fb29e12003-03-11 00:12:48 +0000187 I.setOperand(0, Op->getOperand(0));
188 I.setOperand(1, Folded);
189 return true;
190 } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
191 if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
192 isOnlyUse(Op) && isOnlyUse(Op1)) {
193 Constant *C1 = cast<Constant>(Op->getOperand(1));
194 Constant *C2 = cast<Constant>(Op1->getOperand(1));
195
196 // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattner34428442003-05-27 16:40:51 +0000197 Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
Chris Lattner7fb29e12003-03-11 00:12:48 +0000198 Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
199 Op1->getOperand(0),
200 Op1->getName(), &I);
201 WorkList.push_back(New);
202 I.setOperand(0, New);
203 I.setOperand(1, Folded);
204 return true;
205 }
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000206 }
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000207 return Changed;
Chris Lattner260ab202002-04-18 17:39:14 +0000208}
Chris Lattnerca081252001-12-14 16:52:21 +0000209
Chris Lattnerbb74e222003-03-10 23:06:50 +0000210// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
211// if the LHS is a constant zero (which is the 'negate' form).
Chris Lattner9fa53de2002-05-06 16:49:18 +0000212//
Chris Lattnerbb74e222003-03-10 23:06:50 +0000213static inline Value *dyn_castNegVal(Value *V) {
214 if (BinaryOperator::isNeg(V))
215 return BinaryOperator::getNegArgument(cast<BinaryOperator>(V));
216
Chris Lattner9244df62003-04-30 22:19:10 +0000217 // Constants can be considered to be negated values if they can be folded...
218 if (Constant *C = dyn_cast<Constant>(V))
Chris Lattner34428442003-05-27 16:40:51 +0000219 return ConstantExpr::get(Instruction::Sub,
220 Constant::getNullValue(V->getType()), C);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000221 return 0;
Chris Lattner9fa53de2002-05-06 16:49:18 +0000222}
223
Chris Lattnerbb74e222003-03-10 23:06:50 +0000224static inline Value *dyn_castNotVal(Value *V) {
225 if (BinaryOperator::isNot(V))
226 return BinaryOperator::getNotArgument(cast<BinaryOperator>(V));
227
228 // Constants can be considered to be not'ed values...
Chris Lattnerdd65d862003-04-30 22:34:06 +0000229 if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V))
Chris Lattner34428442003-05-27 16:40:51 +0000230 return ConstantExpr::get(Instruction::Xor,
231 ConstantIntegral::getAllOnesValue(C->getType()),C);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000232 return 0;
233}
234
Chris Lattner7fb29e12003-03-11 00:12:48 +0000235// dyn_castFoldableMul - If this value is a multiply that can be folded into
236// other computations (because it has a constant operand), return the
237// non-constant operand of the multiply.
238//
239static inline Value *dyn_castFoldableMul(Value *V) {
240 if (V->use_size() == 1 && V->getType()->isInteger())
241 if (Instruction *I = dyn_cast<Instruction>(V))
242 if (I->getOpcode() == Instruction::Mul)
243 if (isa<Constant>(I->getOperand(1)))
244 return I->getOperand(0);
245 return 0;
Chris Lattner3082c5a2003-02-18 19:28:33 +0000246}
Chris Lattner31ae8632002-08-14 17:51:49 +0000247
Chris Lattner7fb29e12003-03-11 00:12:48 +0000248// dyn_castMaskingAnd - If this value is an And instruction masking a value with
249// a constant, return the constant being anded with.
250//
Chris Lattner01d56392003-08-12 19:17:27 +0000251template<class ValueType>
252static inline Constant *dyn_castMaskingAnd(ValueType *V) {
Chris Lattner7fb29e12003-03-11 00:12:48 +0000253 if (Instruction *I = dyn_cast<Instruction>(V))
254 if (I->getOpcode() == Instruction::And)
255 return dyn_cast<Constant>(I->getOperand(1));
256
257 // If this is a constant, it acts just like we were masking with it.
258 return dyn_cast<Constant>(V);
259}
Chris Lattner3082c5a2003-02-18 19:28:33 +0000260
261// Log2 - Calculate the log base 2 for the specified value if it is exactly a
262// power of 2.
263static unsigned Log2(uint64_t Val) {
264 assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
265 unsigned Count = 0;
266 while (Val != 1) {
267 if (Val & 1) return 0; // Multiple bits set?
268 Val >>= 1;
269 ++Count;
270 }
271 return Count;
Chris Lattner31ae8632002-08-14 17:51:49 +0000272}
273
Chris Lattnerb8b97502003-08-13 19:01:45 +0000274
275/// AssociativeOpt - Perform an optimization on an associative operator. This
276/// function is designed to check a chain of associative operators for a
277/// potential to apply a certain optimization. Since the optimization may be
278/// applicable if the expression was reassociated, this checks the chain, then
279/// reassociates the expression as necessary to expose the optimization
280/// opportunity. This makes use of a special Functor, which must define
281/// 'shouldApply' and 'apply' methods.
282///
283template<typename Functor>
284Instruction *AssociativeOpt(BinaryOperator &Root, const Functor &F) {
285 unsigned Opcode = Root.getOpcode();
286 Value *LHS = Root.getOperand(0);
287
288 // Quick check, see if the immediate LHS matches...
289 if (F.shouldApply(LHS))
290 return F.apply(Root);
291
292 // Otherwise, if the LHS is not of the same opcode as the root, return.
293 Instruction *LHSI = dyn_cast<Instruction>(LHS);
294 while (LHSI && LHSI->getOpcode() == Opcode && LHSI->use_size() == 1) {
295 // Should we apply this transform to the RHS?
296 bool ShouldApply = F.shouldApply(LHSI->getOperand(1));
297
298 // If not to the RHS, check to see if we should apply to the LHS...
299 if (!ShouldApply && F.shouldApply(LHSI->getOperand(0))) {
300 cast<BinaryOperator>(LHSI)->swapOperands(); // Make the LHS the RHS
301 ShouldApply = true;
302 }
303
304 // If the functor wants to apply the optimization to the RHS of LHSI,
305 // reassociate the expression from ((? op A) op B) to (? op (A op B))
306 if (ShouldApply) {
307 BasicBlock *BB = Root.getParent();
308 // All of the instructions have a single use and have no side-effects,
309 // because of this, we can pull them all into the current basic block.
310 if (LHSI->getParent() != BB) {
311 // Move all of the instructions from root to LHSI into the current
312 // block.
313 Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
314 Instruction *LastUse = &Root;
315 while (TmpLHSI->getParent() == BB) {
316 LastUse = TmpLHSI;
317 TmpLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
318 }
319
320 // Loop over all of the instructions in other blocks, moving them into
321 // the current one.
322 Value *TmpLHS = TmpLHSI;
323 do {
324 TmpLHSI = cast<Instruction>(TmpLHS);
325 // Remove from current block...
326 TmpLHSI->getParent()->getInstList().remove(TmpLHSI);
327 // Insert before the last instruction...
328 BB->getInstList().insert(LastUse, TmpLHSI);
329 TmpLHS = TmpLHSI->getOperand(0);
330 } while (TmpLHSI != LHSI);
331 }
332
333 // Now all of the instructions are in the current basic block, go ahead
334 // and perform the reassociation.
335 Instruction *TmpLHSI = cast<Instruction>(Root.getOperand(0));
336
337 // First move the selected RHS to the LHS of the root...
338 Root.setOperand(0, LHSI->getOperand(1));
339
340 // Make what used to be the LHS of the root be the user of the root...
341 Value *ExtraOperand = TmpLHSI->getOperand(1);
342 Root.replaceAllUsesWith(TmpLHSI); // Users now use TmpLHSI
343 TmpLHSI->setOperand(1, &Root); // TmpLHSI now uses the root
344 BB->getInstList().remove(&Root); // Remove root from the BB
345 BB->getInstList().insert(TmpLHSI, &Root); // Insert root before TmpLHSI
346
347 // Now propagate the ExtraOperand down the chain of instructions until we
348 // get to LHSI.
349 while (TmpLHSI != LHSI) {
350 Instruction *NextLHSI = cast<Instruction>(TmpLHSI->getOperand(0));
351 Value *NextOp = NextLHSI->getOperand(1);
352 NextLHSI->setOperand(1, ExtraOperand);
353 TmpLHSI = NextLHSI;
354 ExtraOperand = NextOp;
355 }
356
357 // Now that the instructions are reassociated, have the functor perform
358 // the transformation...
359 return F.apply(Root);
360 }
361
362 LHSI = dyn_cast<Instruction>(LHSI->getOperand(0));
363 }
364 return 0;
365}
366
367
368// AddRHS - Implements: X + X --> X << 1
369struct AddRHS {
370 Value *RHS;
371 AddRHS(Value *rhs) : RHS(rhs) {}
372 bool shouldApply(Value *LHS) const { return LHS == RHS; }
373 Instruction *apply(BinaryOperator &Add) const {
374 return new ShiftInst(Instruction::Shl, Add.getOperand(0),
375 ConstantInt::get(Type::UByteTy, 1));
376 }
377};
378
379// AddMaskingAnd - Implements (A & C1)+(B & C2) --> (A & C1)|(B & C2)
380// iff C1&C2 == 0
381struct AddMaskingAnd {
382 Constant *C2;
383 AddMaskingAnd(Constant *c) : C2(c) {}
384 bool shouldApply(Value *LHS) const {
385 if (Constant *C1 = dyn_castMaskingAnd(LHS))
386 return ConstantExpr::get(Instruction::And, C1, C2)->isNullValue();
387 return false;
388 }
389 Instruction *apply(BinaryOperator &Add) const {
390 return BinaryOperator::create(Instruction::Or, Add.getOperand(0),
391 Add.getOperand(1));
392 }
393};
394
395
396
Chris Lattner113f4f42002-06-25 16:13:24 +0000397Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000398 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000399 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000400
Chris Lattnerb8b97502003-08-13 19:01:45 +0000401 // X + 0 --> X
Chris Lattnere6794492002-08-12 21:17:25 +0000402 if (RHS == Constant::getNullValue(I.getType()))
403 return ReplaceInstUsesWith(I, LHS);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000404
Chris Lattnerb8b97502003-08-13 19:01:45 +0000405 // X + X --> X << 1
406 if (I.getType()->isInteger())
407 if (Instruction *Result = AssociativeOpt(I, AddRHS(RHS))) return Result;
Chris Lattnerede3fe02003-08-13 04:18:28 +0000408
Chris Lattner147e9752002-05-08 22:46:53 +0000409 // -A + B --> B - A
Chris Lattnerbb74e222003-03-10 23:06:50 +0000410 if (Value *V = dyn_castNegVal(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000411 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000412
413 // A + -B --> A - B
Chris Lattnerbb74e222003-03-10 23:06:50 +0000414 if (!isa<Constant>(RHS))
415 if (Value *V = dyn_castNegVal(RHS))
416 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000417
Chris Lattner57c8d992003-02-18 19:57:07 +0000418 // X*C + X --> X * (C+1)
419 if (dyn_castFoldableMul(LHS) == RHS) {
Chris Lattner34428442003-05-27 16:40:51 +0000420 Constant *CP1 =
421 ConstantExpr::get(Instruction::Add,
422 cast<Constant>(cast<Instruction>(LHS)->getOperand(1)),
423 ConstantInt::get(I.getType(), 1));
Chris Lattner57c8d992003-02-18 19:57:07 +0000424 return BinaryOperator::create(Instruction::Mul, RHS, CP1);
425 }
426
427 // X + X*C --> X * (C+1)
428 if (dyn_castFoldableMul(RHS) == LHS) {
Chris Lattner34428442003-05-27 16:40:51 +0000429 Constant *CP1 =
430 ConstantExpr::get(Instruction::Add,
431 cast<Constant>(cast<Instruction>(RHS)->getOperand(1)),
432 ConstantInt::get(I.getType(), 1));
Chris Lattner57c8d992003-02-18 19:57:07 +0000433 return BinaryOperator::create(Instruction::Mul, LHS, CP1);
434 }
435
Chris Lattnerb8b97502003-08-13 19:01:45 +0000436 // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0
437 if (Constant *C2 = dyn_castMaskingAnd(RHS))
438 if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R;
Chris Lattner7fb29e12003-03-11 00:12:48 +0000439
Chris Lattner113f4f42002-06-25 16:13:24 +0000440 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000441}
442
Chris Lattnerbdb0ce02003-07-22 21:46:59 +0000443// isSignBit - Return true if the value represented by the constant only has the
444// highest order bit set.
445static bool isSignBit(ConstantInt *CI) {
446 unsigned NumBits = CI->getType()->getPrimitiveSize()*8;
447 return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1));
448}
449
Chris Lattnerdfae8be2003-07-24 17:35:25 +0000450static unsigned getTypeSizeInBits(const Type *Ty) {
451 return Ty == Type::BoolTy ? 1 : Ty->getPrimitiveSize()*8;
452}
453
Chris Lattner113f4f42002-06-25 16:13:24 +0000454Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000455 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000456
Chris Lattnere6794492002-08-12 21:17:25 +0000457 if (Op0 == Op1) // sub X, X -> 0
458 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner260ab202002-04-18 17:39:14 +0000459
Chris Lattnere6794492002-08-12 21:17:25 +0000460 // If this is a 'B = x-(-A)', change to B = x+A...
Chris Lattnerbb74e222003-03-10 23:06:50 +0000461 if (Value *V = dyn_castNegVal(Op1))
Chris Lattner147e9752002-05-08 22:46:53 +0000462 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000463
Chris Lattner3082c5a2003-02-18 19:28:33 +0000464 // Replace (-1 - A) with (~A)...
465 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0))
466 if (C->isAllOnesValue())
467 return BinaryOperator::createNot(Op1);
Chris Lattnerad3c4952002-05-09 01:29:19 +0000468
Chris Lattner3082c5a2003-02-18 19:28:33 +0000469 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
470 if (Op1I->use_size() == 1) {
471 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
472 // is not used by anyone else...
473 //
474 if (Op1I->getOpcode() == Instruction::Sub) {
475 // Swap the two operands of the subexpr...
476 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
477 Op1I->setOperand(0, IIOp1);
478 Op1I->setOperand(1, IIOp0);
479
480 // Create the new top level add instruction...
481 return BinaryOperator::create(Instruction::Add, Op0, Op1);
482 }
483
484 // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
485 //
486 if (Op1I->getOpcode() == Instruction::And &&
487 (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
488 Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
489
490 Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I);
491 return BinaryOperator::create(Instruction::And, Op0, NewNot);
492 }
Chris Lattner57c8d992003-02-18 19:57:07 +0000493
494 // X - X*C --> X * (1-C)
495 if (dyn_castFoldableMul(Op1I) == Op0) {
Chris Lattner34428442003-05-27 16:40:51 +0000496 Constant *CP1 =
497 ConstantExpr::get(Instruction::Sub,
498 ConstantInt::get(I.getType(), 1),
499 cast<Constant>(cast<Instruction>(Op1)->getOperand(1)));
Chris Lattner57c8d992003-02-18 19:57:07 +0000500 assert(CP1 && "Couldn't constant fold 1-C?");
501 return BinaryOperator::create(Instruction::Mul, Op0, CP1);
502 }
Chris Lattnerad3c4952002-05-09 01:29:19 +0000503 }
Chris Lattner3082c5a2003-02-18 19:28:33 +0000504
Chris Lattner57c8d992003-02-18 19:57:07 +0000505 // X*C - X --> X * (C-1)
506 if (dyn_castFoldableMul(Op0) == Op1) {
Chris Lattner34428442003-05-27 16:40:51 +0000507 Constant *CP1 =
508 ConstantExpr::get(Instruction::Sub,
509 cast<Constant>(cast<Instruction>(Op0)->getOperand(1)),
510 ConstantInt::get(I.getType(), 1));
Chris Lattner57c8d992003-02-18 19:57:07 +0000511 assert(CP1 && "Couldn't constant fold C - 1?");
512 return BinaryOperator::create(Instruction::Mul, Op1, CP1);
513 }
514
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000515 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000516}
517
Chris Lattner113f4f42002-06-25 16:13:24 +0000518Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000519 bool Changed = SimplifyCommutative(I);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000520 Value *Op0 = I.getOperand(0);
Chris Lattner260ab202002-04-18 17:39:14 +0000521
Chris Lattnere6794492002-08-12 21:17:25 +0000522 // Simplify mul instructions with a constant RHS...
Chris Lattner3082c5a2003-02-18 19:28:33 +0000523 if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
524 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerede3fe02003-08-13 04:18:28 +0000525
526 // ((X << C1)*C2) == (X * (C2 << C1))
527 if (ShiftInst *SI = dyn_cast<ShiftInst>(Op0))
528 if (SI->getOpcode() == Instruction::Shl)
529 if (Constant *ShOp = dyn_cast<Constant>(SI->getOperand(1)))
530 return BinaryOperator::create(Instruction::Mul, SI->getOperand(0),
531 *CI << *ShOp);
532
Chris Lattnercce81be2003-09-11 22:24:54 +0000533 if (CI->isNullValue())
534 return ReplaceInstUsesWith(I, Op1); // X * 0 == 0
535 if (CI->equalsInt(1)) // X * 1 == X
536 return ReplaceInstUsesWith(I, Op0);
537 if (CI->isAllOnesValue()) // X * -1 == 0 - X
Chris Lattner35236d82003-06-25 17:09:20 +0000538 return BinaryOperator::createNeg(Op0, I.getName());
Chris Lattner31ba1292002-04-29 22:24:47 +0000539
Chris Lattnercce81be2003-09-11 22:24:54 +0000540 int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue();
Chris Lattner3082c5a2003-02-18 19:28:33 +0000541 if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C
542 return new ShiftInst(Instruction::Shl, Op0,
543 ConstantUInt::get(Type::UByteTy, C));
544 } else {
545 ConstantFP *Op1F = cast<ConstantFP>(Op1);
546 if (Op1F->isNullValue())
547 return ReplaceInstUsesWith(I, Op1);
Chris Lattner31ba1292002-04-29 22:24:47 +0000548
Chris Lattner3082c5a2003-02-18 19:28:33 +0000549 // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
550 // ANSI says we can drop signals, so we can do this anyway." (from GCC)
551 if (Op1F->getValue() == 1.0)
552 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
553 }
Chris Lattner260ab202002-04-18 17:39:14 +0000554 }
555
Chris Lattner934a64cf2003-03-10 23:23:04 +0000556 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
557 if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
558 return BinaryOperator::create(Instruction::Mul, Op0v, Op1v);
559
Chris Lattner113f4f42002-06-25 16:13:24 +0000560 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000561}
562
Chris Lattner113f4f42002-06-25 16:13:24 +0000563Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000564 // div X, 1 == X
Chris Lattner3082c5a2003-02-18 19:28:33 +0000565 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
Chris Lattnere6794492002-08-12 21:17:25 +0000566 if (RHS->equalsInt(1))
567 return ReplaceInstUsesWith(I, I.getOperand(0));
Chris Lattner3082c5a2003-02-18 19:28:33 +0000568
569 // Check to see if this is an unsigned division with an exact power of 2,
570 // if so, convert to a right shift.
571 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
572 if (uint64_t Val = C->getValue()) // Don't break X / 0
573 if (uint64_t C = Log2(Val))
574 return new ShiftInst(Instruction::Shr, I.getOperand(0),
575 ConstantUInt::get(Type::UByteTy, C));
576 }
577
578 // 0 / X == 0, we don't need to preserve faults!
579 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
580 if (LHS->equalsInt(0))
581 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
582
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000583 return 0;
584}
585
586
Chris Lattner113f4f42002-06-25 16:13:24 +0000587Instruction *InstCombiner::visitRem(BinaryOperator &I) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000588 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
589 if (RHS->equalsInt(1)) // X % 1 == 0
590 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
591
592 // Check to see if this is an unsigned remainder with an exact power of 2,
593 // if so, convert to a bitwise and.
594 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
595 if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero)
596 if (Log2(Val))
597 return BinaryOperator::create(Instruction::And, I.getOperand(0),
598 ConstantUInt::get(I.getType(), Val-1));
599 }
600
601 // 0 % X == 0, we don't need to preserve faults!
602 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
603 if (LHS->equalsInt(0))
Chris Lattnere6794492002-08-12 21:17:25 +0000604 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
605
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000606 return 0;
607}
608
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000609// isMaxValueMinusOne - return true if this is Max-1
Chris Lattnere6794492002-08-12 21:17:25 +0000610static bool isMaxValueMinusOne(const ConstantInt *C) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000611 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
612 // Calculate -1 casted to the right type...
613 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
614 uint64_t Val = ~0ULL; // All ones
615 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
616 return CU->getValue() == Val-1;
617 }
618
619 const ConstantSInt *CS = cast<ConstantSInt>(C);
620
621 // Calculate 0111111111..11111
622 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
623 int64_t Val = INT64_MAX; // All ones
624 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
625 return CS->getValue() == Val-1;
626}
627
628// isMinValuePlusOne - return true if this is Min+1
Chris Lattnere6794492002-08-12 21:17:25 +0000629static bool isMinValuePlusOne(const ConstantInt *C) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000630 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
631 return CU->getValue() == 1;
632
633 const ConstantSInt *CS = cast<ConstantSInt>(C);
634
635 // Calculate 1111111111000000000000
636 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
637 int64_t Val = -1; // All ones
638 Val <<= TypeBits-1; // Shift over to the right spot
639 return CS->getValue() == Val+1;
640}
641
Chris Lattner3ac7c262003-08-13 20:16:26 +0000642/// getSetCondCode - Encode a setcc opcode into a three bit mask. These bits
643/// are carefully arranged to allow folding of expressions such as:
644///
645/// (A < B) | (A > B) --> (A != B)
646///
647/// Bit value '4' represents that the comparison is true if A > B, bit value '2'
648/// represents that the comparison is true if A == B, and bit value '1' is true
649/// if A < B.
650///
651static unsigned getSetCondCode(const SetCondInst *SCI) {
652 switch (SCI->getOpcode()) {
653 // False -> 0
654 case Instruction::SetGT: return 1;
655 case Instruction::SetEQ: return 2;
656 case Instruction::SetGE: return 3;
657 case Instruction::SetLT: return 4;
658 case Instruction::SetNE: return 5;
659 case Instruction::SetLE: return 6;
660 // True -> 7
661 default:
662 assert(0 && "Invalid SetCC opcode!");
663 return 0;
664 }
665}
666
667/// getSetCCValue - This is the complement of getSetCondCode, which turns an
668/// opcode and two operands into either a constant true or false, or a brand new
669/// SetCC instruction.
670static Value *getSetCCValue(unsigned Opcode, Value *LHS, Value *RHS) {
671 switch (Opcode) {
672 case 0: return ConstantBool::False;
673 case 1: return new SetCondInst(Instruction::SetGT, LHS, RHS);
674 case 2: return new SetCondInst(Instruction::SetEQ, LHS, RHS);
675 case 3: return new SetCondInst(Instruction::SetGE, LHS, RHS);
676 case 4: return new SetCondInst(Instruction::SetLT, LHS, RHS);
677 case 5: return new SetCondInst(Instruction::SetNE, LHS, RHS);
678 case 6: return new SetCondInst(Instruction::SetLE, LHS, RHS);
679 case 7: return ConstantBool::True;
680 default: assert(0 && "Illegal SetCCCode!"); return 0;
681 }
682}
683
684// FoldSetCCLogical - Implements (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B)
685struct FoldSetCCLogical {
686 InstCombiner &IC;
687 Value *LHS, *RHS;
688 FoldSetCCLogical(InstCombiner &ic, SetCondInst *SCI)
689 : IC(ic), LHS(SCI->getOperand(0)), RHS(SCI->getOperand(1)) {}
690 bool shouldApply(Value *V) const {
691 if (SetCondInst *SCI = dyn_cast<SetCondInst>(V))
692 return (SCI->getOperand(0) == LHS && SCI->getOperand(1) == RHS ||
693 SCI->getOperand(0) == RHS && SCI->getOperand(1) == LHS);
694 return false;
695 }
696 Instruction *apply(BinaryOperator &Log) const {
697 SetCondInst *SCI = cast<SetCondInst>(Log.getOperand(0));
698 if (SCI->getOperand(0) != LHS) {
699 assert(SCI->getOperand(1) == LHS);
700 SCI->swapOperands(); // Swap the LHS and RHS of the SetCC
701 }
702
703 unsigned LHSCode = getSetCondCode(SCI);
704 unsigned RHSCode = getSetCondCode(cast<SetCondInst>(Log.getOperand(1)));
705 unsigned Code;
706 switch (Log.getOpcode()) {
707 case Instruction::And: Code = LHSCode & RHSCode; break;
708 case Instruction::Or: Code = LHSCode | RHSCode; break;
709 case Instruction::Xor: Code = LHSCode ^ RHSCode; break;
Chris Lattner2caaaba2003-09-22 20:33:34 +0000710 default: assert(0 && "Illegal logical opcode!"); return 0;
Chris Lattner3ac7c262003-08-13 20:16:26 +0000711 }
712
713 Value *RV = getSetCCValue(Code, LHS, RHS);
714 if (Instruction *I = dyn_cast<Instruction>(RV))
715 return I;
716 // Otherwise, it's a constant boolean value...
717 return IC.ReplaceInstUsesWith(Log, RV);
718 }
719};
720
721
Chris Lattnerba1cb382003-09-19 17:17:26 +0000722// OptAndOp - This handles expressions of the form ((val OP C1) & C2). Where
723// the Op parameter is 'OP', OpRHS is 'C1', and AndRHS is 'C2'. Op is
724// guaranteed to be either a shift instruction or a binary operator.
725Instruction *InstCombiner::OptAndOp(Instruction *Op,
726 ConstantIntegral *OpRHS,
727 ConstantIntegral *AndRHS,
728 BinaryOperator &TheAnd) {
729 Value *X = Op->getOperand(0);
730 switch (Op->getOpcode()) {
731 case Instruction::Xor:
732 if ((*AndRHS & *OpRHS)->isNullValue()) {
733 // (X ^ C1) & C2 --> (X & C2) iff (C1&C2) == 0
734 return BinaryOperator::create(Instruction::And, X, AndRHS);
735 } else if (Op->use_size() == 1) {
736 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
737 std::string OpName = Op->getName(); Op->setName("");
738 Instruction *And = BinaryOperator::create(Instruction::And,
739 X, AndRHS, OpName);
740 InsertNewInstBefore(And, TheAnd);
741 return BinaryOperator::create(Instruction::Xor, And, *AndRHS & *OpRHS);
742 }
743 break;
744 case Instruction::Or:
745 // (X | C1) & C2 --> X & C2 iff C1 & C1 == 0
746 if ((*AndRHS & *OpRHS)->isNullValue())
747 return BinaryOperator::create(Instruction::And, X, AndRHS);
748 else {
749 Constant *Together = *AndRHS & *OpRHS;
750 if (Together == AndRHS) // (X | C) & C --> C
751 return ReplaceInstUsesWith(TheAnd, AndRHS);
752
753 if (Op->use_size() == 1 && Together != OpRHS) {
754 // (X | C1) & C2 --> (X | (C1&C2)) & C2
755 std::string Op0Name = Op->getName(); Op->setName("");
756 Instruction *Or = BinaryOperator::create(Instruction::Or, X,
757 Together, Op0Name);
758 InsertNewInstBefore(Or, TheAnd);
759 return BinaryOperator::create(Instruction::And, Or, AndRHS);
760 }
761 }
762 break;
763 case Instruction::Add:
764 if (Op->use_size() == 1) {
765 // Adding a one to a single bit bit-field should be turned into an XOR
766 // of the bit. First thing to check is to see if this AND is with a
767 // single bit constant.
768 unsigned long long AndRHSV = cast<ConstantInt>(AndRHS)->getRawValue();
769
770 // Clear bits that are not part of the constant.
771 AndRHSV &= (1ULL << AndRHS->getType()->getPrimitiveSize()*8)-1;
772
773 // If there is only one bit set...
774 if ((AndRHSV & (AndRHSV-1)) == 0) {
775 // Ok, at this point, we know that we are masking the result of the
776 // ADD down to exactly one bit. If the constant we are adding has
777 // no bits set below this bit, then we can eliminate the ADD.
778 unsigned long long AddRHS = cast<ConstantInt>(OpRHS)->getRawValue();
779
780 // Check to see if any bits below the one bit set in AndRHSV are set.
781 if ((AddRHS & (AndRHSV-1)) == 0) {
782 // If not, the only thing that can effect the output of the AND is
783 // the bit specified by AndRHSV. If that bit is set, the effect of
784 // the XOR is to toggle the bit. If it is clear, then the ADD has
785 // no effect.
786 if ((AddRHS & AndRHSV) == 0) { // Bit is not set, noop
787 TheAnd.setOperand(0, X);
788 return &TheAnd;
789 } else {
790 std::string Name = Op->getName(); Op->setName("");
791 // Pull the XOR out of the AND.
792 Instruction *NewAnd =
793 BinaryOperator::create(Instruction::And, X, AndRHS, Name);
794 InsertNewInstBefore(NewAnd, TheAnd);
795 return BinaryOperator::create(Instruction::Xor, NewAnd, AndRHS);
796 }
797 }
798 }
799 }
800 break;
Chris Lattner2da29172003-09-19 19:05:02 +0000801
802 case Instruction::Shl: {
803 // We know that the AND will not produce any of the bits shifted in, so if
804 // the anded constant includes them, clear them now!
805 //
806 Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
807 Constant *CI = *AndRHS & *(*AllOne << *OpRHS);
808 if (CI != AndRHS) {
809 TheAnd.setOperand(1, CI);
810 return &TheAnd;
811 }
812 break;
813 }
814 case Instruction::Shr:
815 // We know that the AND will not produce any of the bits shifted in, so if
816 // the anded constant includes them, clear them now! This only applies to
817 // unsigned shifts, because a signed shr may bring in set bits!
818 //
819 if (AndRHS->getType()->isUnsigned()) {
820 Constant *AllOne = ConstantIntegral::getAllOnesValue(AndRHS->getType());
821 Constant *CI = *AndRHS & *(*AllOne >> *OpRHS);
822 if (CI != AndRHS) {
823 TheAnd.setOperand(1, CI);
824 return &TheAnd;
825 }
826 }
827 break;
Chris Lattnerba1cb382003-09-19 17:17:26 +0000828 }
829 return 0;
830}
831
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000832
Chris Lattner113f4f42002-06-25 16:13:24 +0000833Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000834 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000835 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000836
837 // and X, X = X and X, 0 == 0
Chris Lattnere6794492002-08-12 21:17:25 +0000838 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
839 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000840
841 // and X, -1 == X
Chris Lattner49b47ae2003-07-23 17:57:01 +0000842 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattnere6794492002-08-12 21:17:25 +0000843 if (RHS->isAllOnesValue())
844 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000845
Chris Lattnerba1cb382003-09-19 17:17:26 +0000846 // Optimize a variety of ((val OP C1) & C2) combinations...
847 if (isa<BinaryOperator>(Op0) || isa<ShiftInst>(Op0)) {
848 Instruction *Op0I = cast<Instruction>(Op0);
Chris Lattner33217db2003-07-23 19:36:21 +0000849 Value *X = Op0I->getOperand(0);
Chris Lattner16464b32003-07-23 19:25:52 +0000850 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
Chris Lattnerba1cb382003-09-19 17:17:26 +0000851 if (Instruction *Res = OptAndOp(Op0I, Op0CI, RHS, I))
852 return Res;
Chris Lattner33217db2003-07-23 19:36:21 +0000853 }
Chris Lattner49b47ae2003-07-23 17:57:01 +0000854 }
855
Chris Lattnerbb74e222003-03-10 23:06:50 +0000856 Value *Op0NotVal = dyn_castNotVal(Op0);
857 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000858
859 // (~A & ~B) == (~(A | B)) - Demorgan's Law
Chris Lattnerbb74e222003-03-10 23:06:50 +0000860 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000861 Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal,
Chris Lattner49b47ae2003-07-23 17:57:01 +0000862 Op1NotVal,I.getName()+".demorgan");
863 InsertNewInstBefore(Or, I);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000864 return BinaryOperator::createNot(Or);
865 }
866
867 if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
868 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner65217ff2002-08-23 18:32:43 +0000869
Chris Lattner3ac7c262003-08-13 20:16:26 +0000870 // (setcc1 A, B) & (setcc2 A, B) --> (setcc3 A, B)
871 if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
872 if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
873 return R;
874
Chris Lattner113f4f42002-06-25 16:13:24 +0000875 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000876}
877
878
879
Chris Lattner113f4f42002-06-25 16:13:24 +0000880Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000881 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000882 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000883
884 // or X, X = X or X, 0 == X
Chris Lattnere6794492002-08-12 21:17:25 +0000885 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
886 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000887
888 // or X, -1 == -1
Chris Lattner8f0d1562003-07-23 18:29:44 +0000889 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattnere6794492002-08-12 21:17:25 +0000890 if (RHS->isAllOnesValue())
891 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000892
Chris Lattner8f0d1562003-07-23 18:29:44 +0000893 if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
894 // (X & C1) | C2 --> (X | C2) & (C1|C2)
895 if (Op0I->getOpcode() == Instruction::And && isOnlyUse(Op0))
896 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
897 std::string Op0Name = Op0I->getName(); Op0I->setName("");
898 Instruction *Or = BinaryOperator::create(Instruction::Or,
899 Op0I->getOperand(0), RHS,
900 Op0Name);
901 InsertNewInstBefore(Or, I);
902 return BinaryOperator::create(Instruction::And, Or, *RHS | *Op0CI);
903 }
904
905 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
906 if (Op0I->getOpcode() == Instruction::Xor && isOnlyUse(Op0))
907 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1))) {
908 std::string Op0Name = Op0I->getName(); Op0I->setName("");
909 Instruction *Or = BinaryOperator::create(Instruction::Or,
910 Op0I->getOperand(0), RHS,
911 Op0Name);
912 InsertNewInstBefore(Or, I);
913 return BinaryOperator::create(Instruction::Xor, Or, *Op0CI & *~*RHS);
914 }
915 }
916 }
917
Chris Lattner812aab72003-08-12 19:11:07 +0000918 // (A & C1)|(A & C2) == A & (C1|C2)
Chris Lattner01d56392003-08-12 19:17:27 +0000919 if (Instruction *LHS = dyn_cast<BinaryOperator>(Op0))
920 if (Instruction *RHS = dyn_cast<BinaryOperator>(Op1))
921 if (LHS->getOperand(0) == RHS->getOperand(0))
922 if (Constant *C0 = dyn_castMaskingAnd(LHS))
923 if (Constant *C1 = dyn_castMaskingAnd(RHS))
924 return BinaryOperator::create(Instruction::And, LHS->getOperand(0),
Chris Lattner812aab72003-08-12 19:11:07 +0000925 *C0 | *C1);
926
Chris Lattner3e327a42003-03-10 23:13:59 +0000927 Value *Op0NotVal = dyn_castNotVal(Op0);
928 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000929
Chris Lattner3e327a42003-03-10 23:13:59 +0000930 if (Op1 == Op0NotVal) // ~A | A == -1
931 return ReplaceInstUsesWith(I,
932 ConstantIntegral::getAllOnesValue(I.getType()));
933
934 if (Op0 == Op1NotVal) // A | ~A == -1
935 return ReplaceInstUsesWith(I,
936 ConstantIntegral::getAllOnesValue(I.getType()));
937
938 // (~A | ~B) == (~(A & B)) - Demorgan's Law
939 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
940 Instruction *And = BinaryOperator::create(Instruction::And, Op0NotVal,
941 Op1NotVal,I.getName()+".demorgan",
942 &I);
943 WorkList.push_back(And);
944 return BinaryOperator::createNot(And);
945 }
Chris Lattner3082c5a2003-02-18 19:28:33 +0000946
Chris Lattner3ac7c262003-08-13 20:16:26 +0000947 // (setcc1 A, B) | (setcc2 A, B) --> (setcc3 A, B)
948 if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
949 if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
950 return R;
951
Chris Lattner113f4f42002-06-25 16:13:24 +0000952 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000953}
954
955
956
Chris Lattner113f4f42002-06-25 16:13:24 +0000957Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000958 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000959 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000960
961 // xor X, X = 0
Chris Lattnere6794492002-08-12 21:17:25 +0000962 if (Op0 == Op1)
963 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000964
Chris Lattner97638592003-07-23 21:37:07 +0000965 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000966 // xor X, 0 == X
Chris Lattner97638592003-07-23 21:37:07 +0000967 if (RHS->isNullValue())
Chris Lattnere6794492002-08-12 21:17:25 +0000968 return ReplaceInstUsesWith(I, Op0);
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000969
Chris Lattner97638592003-07-23 21:37:07 +0000970 if (BinaryOperator *Op0I = dyn_cast<BinaryOperator>(Op0)) {
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000971 // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
Chris Lattner97638592003-07-23 21:37:07 +0000972 if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0I))
973 if (RHS == ConstantBool::True && SCI->use_size() == 1)
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000974 return new SetCondInst(SCI->getInverseCondition(),
975 SCI->getOperand(0), SCI->getOperand(1));
Chris Lattner97638592003-07-23 21:37:07 +0000976
977 if (ConstantInt *Op0CI = dyn_cast<ConstantInt>(Op0I->getOperand(1)))
978 if (Op0I->getOpcode() == Instruction::And) {
979 // (X & C1) ^ C2 --> (X & C1) | C2 iff (C1&C2) == 0
980 if ((*RHS & *Op0CI)->isNullValue())
981 return BinaryOperator::create(Instruction::Or, Op0, RHS);
982 } else if (Op0I->getOpcode() == Instruction::Or) {
983 // (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2
984 if ((*RHS & *Op0CI) == RHS)
985 return BinaryOperator::create(Instruction::And, Op0, ~*RHS);
986 }
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000987 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000988 }
989
Chris Lattnerbb74e222003-03-10 23:06:50 +0000990 if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
Chris Lattner3082c5a2003-02-18 19:28:33 +0000991 if (X == Op1)
992 return ReplaceInstUsesWith(I,
993 ConstantIntegral::getAllOnesValue(I.getType()));
994
Chris Lattnerbb74e222003-03-10 23:06:50 +0000995 if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
Chris Lattner3082c5a2003-02-18 19:28:33 +0000996 if (X == Op0)
997 return ReplaceInstUsesWith(I,
998 ConstantIntegral::getAllOnesValue(I.getType()));
999
Chris Lattner1bbb7b62003-03-10 18:24:17 +00001000 if (Instruction *Op1I = dyn_cast<Instruction>(Op1))
1001 if (Op1I->getOpcode() == Instruction::Or)
1002 if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B
1003 cast<BinaryOperator>(Op1I)->swapOperands();
1004 I.swapOperands();
1005 std::swap(Op0, Op1);
1006 } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B
1007 I.swapOperands();
1008 std::swap(Op0, Op1);
1009 }
1010
1011 if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
1012 if (Op0I->getOpcode() == Instruction::Or && Op0I->use_size() == 1) {
1013 if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B
1014 cast<BinaryOperator>(Op0I)->swapOperands();
Chris Lattnerdcf240a2003-03-10 21:43:22 +00001015 if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B
Chris Lattner1bbb7b62003-03-10 18:24:17 +00001016 Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I);
1017 WorkList.push_back(cast<Instruction>(NotB));
Chris Lattnerdcf240a2003-03-10 21:43:22 +00001018 return BinaryOperator::create(Instruction::And, Op0I->getOperand(0),
1019 NotB);
Chris Lattner1bbb7b62003-03-10 18:24:17 +00001020 }
1021 }
1022
Chris Lattner7fb29e12003-03-11 00:12:48 +00001023 // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1^C2 == 0
1024 if (Constant *C1 = dyn_castMaskingAnd(Op0))
1025 if (Constant *C2 = dyn_castMaskingAnd(Op1))
Chris Lattner34428442003-05-27 16:40:51 +00001026 if (ConstantExpr::get(Instruction::And, C1, C2)->isNullValue())
Chris Lattner7fb29e12003-03-11 00:12:48 +00001027 return BinaryOperator::create(Instruction::Or, Op0, Op1);
1028
Chris Lattner3ac7c262003-08-13 20:16:26 +00001029 // (setcc1 A, B) ^ (setcc2 A, B) --> (setcc3 A, B)
1030 if (SetCondInst *RHS = dyn_cast<SetCondInst>(I.getOperand(1)))
1031 if (Instruction *R = AssociativeOpt(I, FoldSetCCLogical(*this, RHS)))
1032 return R;
1033
Chris Lattner113f4f42002-06-25 16:13:24 +00001034 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001035}
1036
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001037// AddOne, SubOne - Add or subtract a constant one from an integer constant...
1038static Constant *AddOne(ConstantInt *C) {
Chris Lattner34428442003-05-27 16:40:51 +00001039 Constant *Result = ConstantExpr::get(Instruction::Add, C,
1040 ConstantInt::get(C->getType(), 1));
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001041 assert(Result && "Constant folding integer addition failed!");
1042 return Result;
1043}
1044static Constant *SubOne(ConstantInt *C) {
Chris Lattner34428442003-05-27 16:40:51 +00001045 Constant *Result = ConstantExpr::get(Instruction::Sub, C,
1046 ConstantInt::get(C->getType(), 1));
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001047 assert(Result && "Constant folding integer addition failed!");
1048 return Result;
1049}
1050
Chris Lattner1fc23f32002-05-09 20:11:54 +00001051// isTrueWhenEqual - Return true if the specified setcondinst instruction is
1052// true when both operands are equal...
1053//
Chris Lattner113f4f42002-06-25 16:13:24 +00001054static bool isTrueWhenEqual(Instruction &I) {
1055 return I.getOpcode() == Instruction::SetEQ ||
1056 I.getOpcode() == Instruction::SetGE ||
1057 I.getOpcode() == Instruction::SetLE;
Chris Lattner1fc23f32002-05-09 20:11:54 +00001058}
1059
Chris Lattner113f4f42002-06-25 16:13:24 +00001060Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +00001061 bool Changed = SimplifyCommutative(I);
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001062 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1063 const Type *Ty = Op0->getType();
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001064
1065 // setcc X, X
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001066 if (Op0 == Op1)
1067 return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
Chris Lattner1fc23f32002-05-09 20:11:54 +00001068
Chris Lattnerd07283a2003-08-13 05:38:46 +00001069 // setcc <global/alloca*>, 0 - Global/Stack value addresses are never null!
1070 if (isa<ConstantPointerNull>(Op1) &&
1071 (isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0)))
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001072 return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
1073
Chris Lattnerd07283a2003-08-13 05:38:46 +00001074
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001075 // setcc's with boolean values can always be turned into bitwise operations
1076 if (Ty == Type::BoolTy) {
1077 // If this is <, >, or !=, we can change this into a simple xor instruction
1078 if (!isTrueWhenEqual(I))
1079 return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
1080
1081 // Otherwise we need to make a temporary intermediate instruction and insert
1082 // it into the instruction stream. This is what we are after:
1083 //
1084 // seteq bool %A, %B -> ~(A^B)
1085 // setle bool %A, %B -> ~A | B
1086 // setge bool %A, %B -> A | ~B
1087 //
1088 if (I.getOpcode() == Instruction::SetEQ) { // seteq case
1089 Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
1090 I.getName()+"tmp");
1091 InsertNewInstBefore(Xor, I);
Chris Lattner31ae8632002-08-14 17:51:49 +00001092 return BinaryOperator::createNot(Xor, I.getName());
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001093 }
1094
1095 // Handle the setXe cases...
1096 assert(I.getOpcode() == Instruction::SetGE ||
1097 I.getOpcode() == Instruction::SetLE);
1098
1099 if (I.getOpcode() == Instruction::SetGE)
1100 std::swap(Op0, Op1); // Change setge -> setle
1101
1102 // Now we just have the SetLE case.
Chris Lattner31ae8632002-08-14 17:51:49 +00001103 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001104 InsertNewInstBefore(Not, I);
1105 return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
1106 }
1107
1108 // Check to see if we are doing one of many comparisons against constant
1109 // integers at the end of their ranges...
1110 //
1111 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnerd492a0b2003-07-23 17:02:11 +00001112 // Simplify seteq and setne instructions...
1113 if (I.getOpcode() == Instruction::SetEQ ||
1114 I.getOpcode() == Instruction::SetNE) {
1115 bool isSetNE = I.getOpcode() == Instruction::SetNE;
1116
Chris Lattnercfbce7c2003-07-23 17:26:36 +00001117 // If the first operand is (and|or|xor) with a constant, and the second
Chris Lattnerd492a0b2003-07-23 17:02:11 +00001118 // operand is a constant, simplify a bit.
Chris Lattnerc992add2003-08-13 05:33:12 +00001119 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) {
1120 switch (BO->getOpcode()) {
1121 case Instruction::Add:
1122 if (CI->isNullValue()) {
1123 // Replace ((add A, B) != 0) with (A != -B) if A or B is
1124 // efficiently invertible, or if the add has just this one use.
1125 Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
1126 if (Value *NegVal = dyn_castNegVal(BOp1))
1127 return new SetCondInst(I.getOpcode(), BOp0, NegVal);
1128 else if (Value *NegVal = dyn_castNegVal(BOp0))
1129 return new SetCondInst(I.getOpcode(), NegVal, BOp1);
1130 else if (BO->use_size() == 1) {
1131 Instruction *Neg = BinaryOperator::createNeg(BOp1, BO->getName());
1132 BO->setName("");
1133 InsertNewInstBefore(Neg, I);
1134 return new SetCondInst(I.getOpcode(), BOp0, Neg);
1135 }
1136 }
1137 break;
1138 case Instruction::Xor:
1139 // For the xor case, we can xor two constants together, eliminating
1140 // the explicit xor.
1141 if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
1142 return BinaryOperator::create(I.getOpcode(), BO->getOperand(0),
1143 *CI ^ *BOC);
1144
1145 // FALLTHROUGH
1146 case Instruction::Sub:
1147 // Replace (([sub|xor] A, B) != 0) with (A != B)
1148 if (CI->isNullValue())
1149 return new SetCondInst(I.getOpcode(), BO->getOperand(0),
1150 BO->getOperand(1));
1151 break;
1152
1153 case Instruction::Or:
1154 // If bits are being or'd in that are not present in the constant we
1155 // are comparing against, then the comparison could never succeed!
1156 if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1)))
Chris Lattnerd492a0b2003-07-23 17:02:11 +00001157 if (!(*BOC & *~*CI)->isNullValue())
1158 return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE));
Chris Lattnerc992add2003-08-13 05:33:12 +00001159 break;
1160
1161 case Instruction::And:
1162 if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
Chris Lattnerd492a0b2003-07-23 17:02:11 +00001163 // If bits are being compared against that are and'd out, then the
1164 // comparison can never succeed!
1165 if (!(*CI & *~*BOC)->isNullValue())
1166 return ReplaceInstUsesWith(I, ConstantBool::get(isSetNE));
Chris Lattnerc992add2003-08-13 05:33:12 +00001167
1168 // Replace (and X, (1 << size(X)-1) != 0) with x < 0, converting X
1169 // to be a signed value as appropriate.
1170 if (isSignBit(BOC)) {
1171 Value *X = BO->getOperand(0);
1172 // If 'X' is not signed, insert a cast now...
1173 if (!BOC->getType()->isSigned()) {
1174 const Type *DestTy;
1175 switch (BOC->getType()->getPrimitiveID()) {
1176 case Type::UByteTyID: DestTy = Type::SByteTy; break;
1177 case Type::UShortTyID: DestTy = Type::ShortTy; break;
1178 case Type::UIntTyID: DestTy = Type::IntTy; break;
1179 case Type::ULongTyID: DestTy = Type::LongTy; break;
1180 default: assert(0 && "Invalid unsigned integer type!"); abort();
1181 }
1182 CastInst *NewCI = new CastInst(X,DestTy,X->getName()+".signed");
1183 InsertNewInstBefore(NewCI, I);
1184 X = NewCI;
1185 }
1186 return new SetCondInst(isSetNE ? Instruction::SetLT :
1187 Instruction::SetGE, X,
1188 Constant::getNullValue(X->getType()));
1189 }
Chris Lattnerd492a0b2003-07-23 17:02:11 +00001190 }
Chris Lattnerc992add2003-08-13 05:33:12 +00001191 default: break;
1192 }
1193 }
Chris Lattnere967b342003-06-04 05:10:11 +00001194 }
Chris Lattner791ac1a2003-06-01 03:35:25 +00001195
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001196 // Check to see if we are comparing against the minimum or maximum value...
Chris Lattnere6794492002-08-12 21:17:25 +00001197 if (CI->isMinValue()) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001198 if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE
1199 return ReplaceInstUsesWith(I, ConstantBool::False);
1200 if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE
1201 return ReplaceInstUsesWith(I, ConstantBool::True);
1202 if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN
1203 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
1204 if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN
1205 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
1206
Chris Lattnere6794492002-08-12 21:17:25 +00001207 } else if (CI->isMaxValue()) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +00001208 if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE
1209 return ReplaceInstUsesWith(I, ConstantBool::False);
1210 if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE
1211 return ReplaceInstUsesWith(I, ConstantBool::True);
1212 if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX
1213 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
1214 if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX
1215 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
1216
1217 // Comparing against a value really close to min or max?
1218 } else if (isMinValuePlusOne(CI)) {
1219 if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN
1220 return BinaryOperator::create(Instruction::SetEQ, Op0,
1221 SubOne(CI), I.getName());
1222 if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN
1223 return BinaryOperator::create(Instruction::SetNE, Op0,
1224 SubOne(CI), I.getName());
1225
1226 } else if (isMaxValueMinusOne(CI)) {
1227 if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX
1228 return BinaryOperator::create(Instruction::SetEQ, Op0,
1229 AddOne(CI), I.getName());
1230 if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX
1231 return BinaryOperator::create(Instruction::SetNE, Op0,
1232 AddOne(CI), I.getName());
1233 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001234 }
1235
Chris Lattner113f4f42002-06-25 16:13:24 +00001236 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001237}
1238
1239
1240
Chris Lattnere8d6c602003-03-10 19:16:08 +00001241Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +00001242 assert(I.getOperand(1)->getType() == Type::UByteTy);
1243 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001244 bool isLeftShift = I.getOpcode() == Instruction::Shl;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001245
1246 // shl X, 0 == X and shr X, 0 == X
1247 // shl 0, X == 0 and shr 0, X == 0
1248 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
Chris Lattnere6794492002-08-12 21:17:25 +00001249 Op0 == Constant::getNullValue(Op0->getType()))
1250 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001251
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001252 // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
1253 if (!isLeftShift)
1254 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
1255 if (CSI->isAllOnesValue())
1256 return ReplaceInstUsesWith(I, CSI);
1257
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001258 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
Chris Lattner3204d4e2003-07-24 17:52:58 +00001259 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
1260 // of a signed value.
1261 //
Chris Lattnere8d6c602003-03-10 19:16:08 +00001262 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
1263 if (CUI->getValue() >= TypeBits &&
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001264 (!Op0->getType()->isSigned() || isLeftShift))
Chris Lattnere8d6c602003-03-10 19:16:08 +00001265 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
Chris Lattner55f3d942002-09-10 23:04:09 +00001266
Chris Lattnerede3fe02003-08-13 04:18:28 +00001267 // ((X*C1) << C2) == (X * (C1 << C2))
1268 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0))
1269 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
1270 if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1)))
1271 return BinaryOperator::create(Instruction::Mul, BO->getOperand(0),
1272 *BOOp << *CUI);
1273
1274
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001275 // If the operand is an bitwise operator with a constant RHS, and the
1276 // shift is the only use, we can pull it out of the shift.
1277 if (Op0->use_size() == 1)
1278 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0))
1279 if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) {
1280 bool isValid = true; // Valid only for And, Or, Xor
1281 bool highBitSet = false; // Transform if high bit of constant set?
1282
1283 switch (Op0BO->getOpcode()) {
1284 default: isValid = false; break; // Do not perform transform!
1285 case Instruction::Or:
1286 case Instruction::Xor:
1287 highBitSet = false;
1288 break;
1289 case Instruction::And:
1290 highBitSet = true;
1291 break;
1292 }
1293
1294 // If this is a signed shift right, and the high bit is modified
1295 // by the logical operation, do not perform the transformation.
1296 // The highBitSet boolean indicates the value of the high bit of
1297 // the constant which would cause it to be modified for this
1298 // operation.
1299 //
1300 if (isValid && !isLeftShift && !I.getType()->isUnsigned()) {
1301 uint64_t Val = Op0C->getRawValue();
1302 isValid = ((Val & (1 << (TypeBits-1))) != 0) == highBitSet;
1303 }
1304
1305 if (isValid) {
1306 Constant *NewRHS =
1307 ConstantFoldShiftInstruction(I.getOpcode(), Op0C, CUI);
1308
1309 Instruction *NewShift =
1310 new ShiftInst(I.getOpcode(), Op0BO->getOperand(0), CUI,
1311 Op0BO->getName());
1312 Op0BO->setName("");
1313 InsertNewInstBefore(NewShift, I);
1314
1315 return BinaryOperator::create(Op0BO->getOpcode(), NewShift,
1316 NewRHS);
1317 }
1318 }
1319
Chris Lattner3204d4e2003-07-24 17:52:58 +00001320 // If this is a shift of a shift, see if we can fold the two together...
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001321 if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0))
Chris Lattnerab780df2003-07-24 18:38:56 +00001322 if (ConstantUInt *ShiftAmt1C =
1323 dyn_cast<ConstantUInt>(Op0SI->getOperand(1))) {
Chris Lattner3204d4e2003-07-24 17:52:58 +00001324 unsigned ShiftAmt1 = ShiftAmt1C->getValue();
1325 unsigned ShiftAmt2 = CUI->getValue();
1326
1327 // Check for (A << c1) << c2 and (A >> c1) >> c2
1328 if (I.getOpcode() == Op0SI->getOpcode()) {
1329 unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift...
1330 return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0),
1331 ConstantUInt::get(Type::UByteTy, Amt));
1332 }
1333
Chris Lattnerab780df2003-07-24 18:38:56 +00001334 // Check for (A << c1) >> c2 or visaversa. If we are dealing with
1335 // signed types, we can only support the (A >> c1) << c2 configuration,
1336 // because it can not turn an arbitrary bit of A into a sign bit.
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001337 if (I.getType()->isUnsigned() || isLeftShift) {
Chris Lattner3204d4e2003-07-24 17:52:58 +00001338 // Calculate bitmask for what gets shifted off the edge...
1339 Constant *C = ConstantIntegral::getAllOnesValue(I.getType());
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001340 if (isLeftShift)
Chris Lattner3204d4e2003-07-24 17:52:58 +00001341 C = ConstantExpr::getShift(Instruction::Shl, C, ShiftAmt1C);
Chris Lattnerdeaa0dd2003-08-12 21:53:41 +00001342 else
1343 C = ConstantExpr::getShift(Instruction::Shr, C, ShiftAmt1C);
Chris Lattner3204d4e2003-07-24 17:52:58 +00001344
1345 Instruction *Mask =
1346 BinaryOperator::create(Instruction::And, Op0SI->getOperand(0),
1347 C, Op0SI->getOperand(0)->getName()+".mask");
1348 InsertNewInstBefore(Mask, I);
1349
1350 // Figure out what flavor of shift we should use...
1351 if (ShiftAmt1 == ShiftAmt2)
1352 return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2
1353 else if (ShiftAmt1 < ShiftAmt2) {
1354 return new ShiftInst(I.getOpcode(), Mask,
1355 ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1));
1356 } else {
1357 return new ShiftInst(Op0SI->getOpcode(), Mask,
1358 ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
1359 }
1360 }
1361 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001362 }
Chris Lattner2e0fb392002-10-08 16:16:40 +00001363
Chris Lattnerf4cdbf32002-05-06 16:14:14 +00001364 return 0;
1365}
1366
1367
Chris Lattner48a44f72002-05-02 17:06:02 +00001368// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
1369// instruction.
1370//
Chris Lattnerdfae8be2003-07-24 17:35:25 +00001371static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy,
1372 const Type *DstTy) {
Chris Lattner48a44f72002-05-02 17:06:02 +00001373
Chris Lattner650b6da2002-08-02 20:00:25 +00001374 // It is legal to eliminate the instruction if casting A->B->A if the sizes
1375 // are identical and the bits don't get reinterpreted (for example
Chris Lattner0bb75912002-08-14 23:21:10 +00001376 // int->float->int would not be allowed)
Misha Brukmane5838c42003-05-20 18:45:36 +00001377 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
Chris Lattner650b6da2002-08-02 20:00:25 +00001378 return true;
Chris Lattner48a44f72002-05-02 17:06:02 +00001379
1380 // Allow free casting and conversion of sizes as long as the sign doesn't
1381 // change...
Chris Lattnerb0b412e2002-09-03 01:08:28 +00001382 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
Chris Lattner650b6da2002-08-02 20:00:25 +00001383 unsigned SrcSize = SrcTy->getPrimitiveSize();
1384 unsigned MidSize = MidTy->getPrimitiveSize();
1385 unsigned DstSize = DstTy->getPrimitiveSize();
Chris Lattner650b6da2002-08-02 20:00:25 +00001386
Chris Lattner3732aca2002-08-15 16:15:25 +00001387 // Cases where we are monotonically decreasing the size of the type are
1388 // always ok, regardless of what sign changes are going on.
1389 //
Chris Lattner0bb75912002-08-14 23:21:10 +00001390 if (SrcSize >= MidSize && MidSize >= DstSize)
Chris Lattner650b6da2002-08-02 20:00:25 +00001391 return true;
Chris Lattner3732aca2002-08-15 16:15:25 +00001392
Chris Lattner555518c2002-09-23 23:39:43 +00001393 // Cases where the source and destination type are the same, but the middle
1394 // type is bigger are noops.
1395 //
1396 if (SrcSize == DstSize && MidSize > SrcSize)
1397 return true;
1398
Chris Lattner3732aca2002-08-15 16:15:25 +00001399 // If we are monotonically growing, things are more complex.
1400 //
1401 if (SrcSize <= MidSize && MidSize <= DstSize) {
1402 // We have eight combinations of signedness to worry about. Here's the
1403 // table:
1404 static const int SignTable[8] = {
1405 // CODE, SrcSigned, MidSigned, DstSigned, Comment
1406 1, // U U U Always ok
1407 1, // U U S Always ok
1408 3, // U S U Ok iff SrcSize != MidSize
1409 3, // U S S Ok iff SrcSize != MidSize
1410 0, // S U U Never ok
1411 2, // S U S Ok iff MidSize == DstSize
1412 1, // S S U Always ok
1413 1, // S S S Always ok
1414 };
1415
1416 // Choose an action based on the current entry of the signtable that this
1417 // cast of cast refers to...
1418 unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
1419 switch (SignTable[Row]) {
1420 case 0: return false; // Never ok
1421 case 1: return true; // Always ok
1422 case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
1423 case 3: // Ok iff SrcSize != MidSize
1424 return SrcSize != MidSize || SrcTy == Type::BoolTy;
1425 default: assert(0 && "Bad entry in sign table!");
1426 }
Chris Lattner3732aca2002-08-15 16:15:25 +00001427 }
Chris Lattner650b6da2002-08-02 20:00:25 +00001428 }
Chris Lattner48a44f72002-05-02 17:06:02 +00001429
1430 // Otherwise, we cannot succeed. Specifically we do not want to allow things
1431 // like: short -> ushort -> uint, because this can create wrong results if
1432 // the input short is negative!
1433 //
1434 return false;
1435}
1436
Chris Lattnerdfae8be2003-07-24 17:35:25 +00001437static bool ValueRequiresCast(const Value *V, const Type *Ty) {
1438 if (V->getType() == Ty || isa<Constant>(V)) return false;
1439 if (const CastInst *CI = dyn_cast<CastInst>(V))
1440 if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty))
1441 return false;
1442 return true;
1443}
1444
1445/// InsertOperandCastBefore - This inserts a cast of V to DestTy before the
1446/// InsertBefore instruction. This is specialized a bit to avoid inserting
1447/// casts that are known to not do anything...
1448///
1449Value *InstCombiner::InsertOperandCastBefore(Value *V, const Type *DestTy,
1450 Instruction *InsertBefore) {
1451 if (V->getType() == DestTy) return V;
1452 if (Constant *C = dyn_cast<Constant>(V))
1453 return ConstantExpr::getCast(C, DestTy);
1454
1455 CastInst *CI = new CastInst(V, DestTy, V->getName());
1456 InsertNewInstBefore(CI, *InsertBefore);
1457 return CI;
1458}
Chris Lattner48a44f72002-05-02 17:06:02 +00001459
1460// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +00001461//
Chris Lattner113f4f42002-06-25 16:13:24 +00001462Instruction *InstCombiner::visitCastInst(CastInst &CI) {
Chris Lattner55d4bda2003-06-23 21:59:52 +00001463 Value *Src = CI.getOperand(0);
1464
Chris Lattner48a44f72002-05-02 17:06:02 +00001465 // If the user is casting a value to the same type, eliminate this cast
1466 // instruction...
Chris Lattner55d4bda2003-06-23 21:59:52 +00001467 if (CI.getType() == Src->getType())
1468 return ReplaceInstUsesWith(CI, Src);
Chris Lattner48a44f72002-05-02 17:06:02 +00001469
Chris Lattner48a44f72002-05-02 17:06:02 +00001470 // If casting the result of another cast instruction, try to eliminate this
1471 // one!
1472 //
Chris Lattner55d4bda2003-06-23 21:59:52 +00001473 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {
Chris Lattnerdfae8be2003-07-24 17:35:25 +00001474 if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(),
1475 CSrc->getType(), CI.getType())) {
Chris Lattner48a44f72002-05-02 17:06:02 +00001476 // This instruction now refers directly to the cast's src operand. This
1477 // has a good chance of making CSrc dead.
Chris Lattner113f4f42002-06-25 16:13:24 +00001478 CI.setOperand(0, CSrc->getOperand(0));
1479 return &CI;
Chris Lattner48a44f72002-05-02 17:06:02 +00001480 }
1481
Chris Lattner650b6da2002-08-02 20:00:25 +00001482 // If this is an A->B->A cast, and we are dealing with integral types, try
1483 // to convert this into a logical 'and' instruction.
1484 //
1485 if (CSrc->getOperand(0)->getType() == CI.getType() &&
Chris Lattnerb0b412e2002-09-03 01:08:28 +00001486 CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
Chris Lattner650b6da2002-08-02 20:00:25 +00001487 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
1488 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
1489 assert(CSrc->getType() != Type::ULongTy &&
1490 "Cannot have type bigger than ulong!");
Chris Lattner196897c2003-05-26 23:41:32 +00001491 uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1;
Chris Lattner650b6da2002-08-02 20:00:25 +00001492 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
1493 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
1494 AndOp);
1495 }
1496 }
1497
Chris Lattnerd0d51602003-06-21 23:12:02 +00001498 // If casting the result of a getelementptr instruction with no offset, turn
1499 // this into a cast of the original pointer!
1500 //
Chris Lattner55d4bda2003-06-23 21:59:52 +00001501 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
Chris Lattnerd0d51602003-06-21 23:12:02 +00001502 bool AllZeroOperands = true;
1503 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
1504 if (!isa<Constant>(GEP->getOperand(i)) ||
1505 !cast<Constant>(GEP->getOperand(i))->isNullValue()) {
1506 AllZeroOperands = false;
1507 break;
1508 }
1509 if (AllZeroOperands) {
1510 CI.setOperand(0, GEP->getOperand(0));
1511 return &CI;
1512 }
1513 }
1514
Chris Lattnerdfae8be2003-07-24 17:35:25 +00001515 // If the source value is an instruction with only this use, we can attempt to
1516 // propagate the cast into the instruction. Also, only handle integral types
1517 // for now.
1518 if (Instruction *SrcI = dyn_cast<Instruction>(Src))
1519 if (SrcI->use_size() == 1 && Src->getType()->isIntegral() &&
1520 CI.getType()->isInteger()) { // Don't mess with casts to bool here
1521 const Type *DestTy = CI.getType();
1522 unsigned SrcBitSize = getTypeSizeInBits(Src->getType());
1523 unsigned DestBitSize = getTypeSizeInBits(DestTy);
1524
1525 Value *Op0 = SrcI->getNumOperands() > 0 ? SrcI->getOperand(0) : 0;
1526 Value *Op1 = SrcI->getNumOperands() > 1 ? SrcI->getOperand(1) : 0;
1527
1528 switch (SrcI->getOpcode()) {
1529 case Instruction::Add:
1530 case Instruction::Mul:
1531 case Instruction::And:
1532 case Instruction::Or:
1533 case Instruction::Xor:
1534 // If we are discarding information, or just changing the sign, rewrite.
1535 if (DestBitSize <= SrcBitSize && DestBitSize != 1) {
1536 // Don't insert two casts if they cannot be eliminated. We allow two
1537 // casts to be inserted if the sizes are the same. This could only be
1538 // converting signedness, which is a noop.
1539 if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy) ||
1540 !ValueRequiresCast(Op0, DestTy)) {
1541 Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
1542 Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI);
1543 return BinaryOperator::create(cast<BinaryOperator>(SrcI)
1544 ->getOpcode(), Op0c, Op1c);
1545 }
1546 }
1547 break;
1548 case Instruction::Shl:
1549 // Allow changing the sign of the source operand. Do not allow changing
1550 // the size of the shift, UNLESS the shift amount is a constant. We
1551 // mush not change variable sized shifts to a smaller size, because it
1552 // is undefined to shift more bits out than exist in the value.
1553 if (DestBitSize == SrcBitSize ||
1554 (DestBitSize < SrcBitSize && isa<Constant>(Op1))) {
1555 Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
1556 return new ShiftInst(Instruction::Shl, Op0c, Op1);
1557 }
1558 break;
1559 }
1560 }
1561
Chris Lattner260ab202002-04-18 17:39:14 +00001562 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +00001563}
1564
Chris Lattner970c33a2003-06-19 17:00:31 +00001565// CallInst simplification
1566//
1567Instruction *InstCombiner::visitCallInst(CallInst &CI) {
1568 if (transformConstExprCastCall(&CI)) return 0;
1569 return 0;
1570}
1571
1572// InvokeInst simplification
1573//
1574Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
1575 if (transformConstExprCastCall(&II)) return 0;
1576 return 0;
1577}
1578
1579// getPromotedType - Return the specified type promoted as it would be to pass
1580// though a va_arg area...
1581static const Type *getPromotedType(const Type *Ty) {
1582 switch (Ty->getPrimitiveID()) {
1583 case Type::SByteTyID:
1584 case Type::ShortTyID: return Type::IntTy;
1585 case Type::UByteTyID:
1586 case Type::UShortTyID: return Type::UIntTy;
1587 case Type::FloatTyID: return Type::DoubleTy;
1588 default: return Ty;
1589 }
1590}
1591
1592// transformConstExprCastCall - If the callee is a constexpr cast of a function,
1593// attempt to move the cast to the arguments of the call/invoke.
1594//
1595bool InstCombiner::transformConstExprCastCall(CallSite CS) {
1596 if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
1597 ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
1598 if (CE->getOpcode() != Instruction::Cast ||
1599 !isa<ConstantPointerRef>(CE->getOperand(0)))
1600 return false;
1601 ConstantPointerRef *CPR = cast<ConstantPointerRef>(CE->getOperand(0));
1602 if (!isa<Function>(CPR->getValue())) return false;
1603 Function *Callee = cast<Function>(CPR->getValue());
1604 Instruction *Caller = CS.getInstruction();
1605
1606 // Okay, this is a cast from a function to a different type. Unless doing so
1607 // would cause a type conversion of one of our arguments, change this call to
1608 // be a direct call with arguments casted to the appropriate types.
1609 //
1610 const FunctionType *FT = Callee->getFunctionType();
1611 const Type *OldRetTy = Caller->getType();
1612
1613 if (Callee->isExternal() &&
1614 !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()))
1615 return false; // Cannot transform this return value...
1616
1617 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
1618 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
1619
1620 CallSite::arg_iterator AI = CS.arg_begin();
1621 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
1622 const Type *ParamTy = FT->getParamType(i);
1623 bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy);
1624 if (Callee->isExternal() && !isConvertible) return false;
1625 }
1626
1627 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
1628 Callee->isExternal())
1629 return false; // Do not delete arguments unless we have a function body...
1630
1631 // Okay, we decided that this is a safe thing to do: go ahead and start
1632 // inserting cast instructions as necessary...
1633 std::vector<Value*> Args;
1634 Args.reserve(NumActualArgs);
1635
1636 AI = CS.arg_begin();
1637 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
1638 const Type *ParamTy = FT->getParamType(i);
1639 if ((*AI)->getType() == ParamTy) {
1640 Args.push_back(*AI);
1641 } else {
1642 Instruction *Cast = new CastInst(*AI, ParamTy, "tmp");
1643 InsertNewInstBefore(Cast, *Caller);
1644 Args.push_back(Cast);
1645 }
1646 }
1647
1648 // If the function takes more arguments than the call was taking, add them
1649 // now...
1650 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
1651 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1652
1653 // If we are removing arguments to the function, emit an obnoxious warning...
1654 if (FT->getNumParams() < NumActualArgs)
1655 if (!FT->isVarArg()) {
1656 std::cerr << "WARNING: While resolving call to function '"
1657 << Callee->getName() << "' arguments were dropped!\n";
1658 } else {
1659 // Add all of the arguments in their promoted form to the arg list...
1660 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
1661 const Type *PTy = getPromotedType((*AI)->getType());
1662 if (PTy != (*AI)->getType()) {
1663 // Must promote to pass through va_arg area!
1664 Instruction *Cast = new CastInst(*AI, PTy, "tmp");
1665 InsertNewInstBefore(Cast, *Caller);
1666 Args.push_back(Cast);
1667 } else {
1668 Args.push_back(*AI);
1669 }
1670 }
1671 }
1672
1673 if (FT->getReturnType() == Type::VoidTy)
1674 Caller->setName(""); // Void type should not have a name...
1675
1676 Instruction *NC;
1677 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1678 NC = new InvokeInst(Callee, II->getNormalDest(), II->getExceptionalDest(),
1679 Args, Caller->getName(), Caller);
1680 } else {
1681 NC = new CallInst(Callee, Args, Caller->getName(), Caller);
1682 }
1683
1684 // Insert a cast of the return type as necessary...
1685 Value *NV = NC;
1686 if (Caller->getType() != NV->getType() && !Caller->use_empty()) {
1687 if (NV->getType() != Type::VoidTy) {
1688 NV = NC = new CastInst(NC, Caller->getType(), "tmp");
1689 InsertNewInstBefore(NC, *Caller);
1690 AddUsesToWorkList(*Caller);
1691 } else {
1692 NV = Constant::getNullValue(Caller->getType());
1693 }
1694 }
1695
1696 if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
1697 Caller->replaceAllUsesWith(NV);
1698 Caller->getParent()->getInstList().erase(Caller);
1699 removeFromWorkList(Caller);
1700 return true;
1701}
1702
1703
Chris Lattner48a44f72002-05-02 17:06:02 +00001704
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001705// PHINode simplification
1706//
Chris Lattner113f4f42002-06-25 16:13:24 +00001707Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001708 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattnere6794492002-08-12 21:17:25 +00001709 if (PN.getNumIncomingValues() == 1)
1710 return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
Chris Lattner9cd1e662002-08-20 15:35:35 +00001711
1712 // Otherwise if all of the incoming values are the same for the PHI, replace
1713 // the PHI node with the incoming value.
1714 //
Chris Lattnerf6c0efa2002-08-22 20:22:01 +00001715 Value *InVal = 0;
1716 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1717 if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself...
1718 if (InVal && PN.getIncomingValue(i) != InVal)
1719 return 0; // Not the same, bail out.
1720 else
1721 InVal = PN.getIncomingValue(i);
1722
1723 // The only case that could cause InVal to be null is if we have a PHI node
1724 // that only has entries for itself. In this case, there is no entry into the
1725 // loop, so kill the PHI.
1726 //
1727 if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001728
Chris Lattner9cd1e662002-08-20 15:35:35 +00001729 // All of the incoming values are the same, replace the PHI node now.
1730 return ReplaceInstUsesWith(PN, InVal);
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001731}
1732
Chris Lattner48a44f72002-05-02 17:06:02 +00001733
Chris Lattner113f4f42002-06-25 16:13:24 +00001734Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner471bd762003-05-22 19:07:21 +00001735 // Is it 'getelementptr %P, long 0' or 'getelementptr %P'
Chris Lattner113f4f42002-06-25 16:13:24 +00001736 // If so, eliminate the noop.
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001737 if ((GEP.getNumOperands() == 2 &&
Chris Lattner136dab72002-09-11 01:21:33 +00001738 GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
Chris Lattnere6794492002-08-12 21:17:25 +00001739 GEP.getNumOperands() == 1)
1740 return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
Chris Lattner48a44f72002-05-02 17:06:02 +00001741
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001742 // Combine Indices - If the source pointer to this getelementptr instruction
1743 // is a getelementptr instruction, combine the indices of the two
1744 // getelementptr instructions into a single instruction.
1745 //
Chris Lattnerc59af1d2002-08-17 22:21:59 +00001746 if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001747 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +00001748
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001749 // Can we combine the two pointer arithmetics offsets?
Chris Lattner471bd762003-05-22 19:07:21 +00001750 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
1751 isa<Constant>(GEP.getOperand(1))) {
Chris Lattner235af562003-03-05 22:33:14 +00001752 // Replace: gep (gep %P, long C1), long C2, ...
1753 // With: gep %P, long (C1+C2), ...
Chris Lattner34428442003-05-27 16:40:51 +00001754 Value *Sum = ConstantExpr::get(Instruction::Add,
1755 cast<Constant>(Src->getOperand(1)),
1756 cast<Constant>(GEP.getOperand(1)));
Chris Lattner235af562003-03-05 22:33:14 +00001757 assert(Sum && "Constant folding of longs failed!?");
1758 GEP.setOperand(0, Src->getOperand(0));
1759 GEP.setOperand(1, Sum);
1760 AddUsesToWorkList(*Src); // Reduce use count of Src
1761 return &GEP;
Chris Lattner471bd762003-05-22 19:07:21 +00001762 } else if (Src->getNumOperands() == 2) {
Chris Lattner235af562003-03-05 22:33:14 +00001763 // Replace: gep (gep %P, long B), long A, ...
1764 // With: T = long A+B; gep %P, T, ...
1765 //
1766 Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1),
1767 GEP.getOperand(1),
1768 Src->getName()+".sum", &GEP);
1769 GEP.setOperand(0, Src->getOperand(0));
1770 GEP.setOperand(1, Sum);
1771 WorkList.push_back(cast<Instruction>(Sum));
1772 return &GEP;
Chris Lattner5d606a02002-11-04 16:43:32 +00001773 } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
Chris Lattnera8339e32002-09-17 21:05:42 +00001774 Src->getNumOperands() != 1) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001775 // Otherwise we can do the fold if the first index of the GEP is a zero
1776 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
1777 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
Chris Lattner5d606a02002-11-04 16:43:32 +00001778 } else if (Src->getOperand(Src->getNumOperands()-1) ==
1779 Constant::getNullValue(Type::LongTy)) {
1780 // If the src gep ends with a constant array index, merge this get into
1781 // it, even if we have a non-zero array index.
1782 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1);
1783 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001784 }
1785
1786 if (!Indices.empty())
1787 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
Chris Lattnerc59af1d2002-08-17 22:21:59 +00001788
1789 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
1790 // GEP of global variable. If all of the indices for this GEP are
1791 // constants, we can promote this to a constexpr instead of an instruction.
1792
1793 // Scan for nonconstants...
1794 std::vector<Constant*> Indices;
1795 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
1796 for (; I != E && isa<Constant>(*I); ++I)
1797 Indices.push_back(cast<Constant>(*I));
1798
1799 if (I == E) { // If they are all constants...
Chris Lattner46b3d302003-04-16 22:40:51 +00001800 Constant *CE =
Chris Lattnerc59af1d2002-08-17 22:21:59 +00001801 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
1802
1803 // Replace all uses of the GEP with the new constexpr...
1804 return ReplaceInstUsesWith(GEP, CE);
1805 }
Chris Lattnerca081252001-12-14 16:52:21 +00001806 }
1807
Chris Lattnerca081252001-12-14 16:52:21 +00001808 return 0;
1809}
1810
Chris Lattner1085bdf2002-11-04 16:18:53 +00001811Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
1812 // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
1813 if (AI.isArrayAllocation()) // Check C != 1
1814 if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
1815 const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
Chris Lattnera2620ac2002-11-09 00:49:43 +00001816 AllocationInst *New = 0;
Chris Lattner1085bdf2002-11-04 16:18:53 +00001817
1818 // Create and insert the replacement instruction...
1819 if (isa<MallocInst>(AI))
1820 New = new MallocInst(NewTy, 0, AI.getName(), &AI);
Chris Lattnera2620ac2002-11-09 00:49:43 +00001821 else {
1822 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
Chris Lattner1085bdf2002-11-04 16:18:53 +00001823 New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
Chris Lattnera2620ac2002-11-09 00:49:43 +00001824 }
Chris Lattner1085bdf2002-11-04 16:18:53 +00001825
1826 // Scan to the end of the allocation instructions, to skip over a block of
1827 // allocas if possible...
1828 //
1829 BasicBlock::iterator It = New;
1830 while (isa<AllocationInst>(*It)) ++It;
1831
1832 // Now that I is pointing to the first non-allocation-inst in the block,
1833 // insert our getelementptr instruction...
1834 //
1835 std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
1836 Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
1837
1838 // Now make everything use the getelementptr instead of the original
1839 // allocation.
1840 ReplaceInstUsesWith(AI, V);
1841 return &AI;
1842 }
1843 return 0;
1844}
1845
Chris Lattner0f1d8a32003-06-26 05:06:25 +00001846/// GetGEPGlobalInitializer - Given a constant, and a getelementptr
1847/// constantexpr, return the constant value being addressed by the constant
1848/// expression, or null if something is funny.
1849///
1850static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) {
1851 if (CE->getOperand(1) != Constant::getNullValue(Type::LongTy))
1852 return 0; // Do not allow stepping over the value!
1853
1854 // Loop over all of the operands, tracking down which value we are
1855 // addressing...
1856 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i)
1857 if (ConstantUInt *CU = dyn_cast<ConstantUInt>(CE->getOperand(i))) {
1858 ConstantStruct *CS = cast<ConstantStruct>(C);
1859 if (CU->getValue() >= CS->getValues().size()) return 0;
1860 C = cast<Constant>(CS->getValues()[CU->getValue()]);
1861 } else if (ConstantSInt *CS = dyn_cast<ConstantSInt>(CE->getOperand(i))) {
1862 ConstantArray *CA = cast<ConstantArray>(C);
1863 if ((uint64_t)CS->getValue() >= CA->getValues().size()) return 0;
1864 C = cast<Constant>(CA->getValues()[CS->getValue()]);
1865 } else
1866 return 0;
1867 return C;
1868}
1869
1870Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
1871 Value *Op = LI.getOperand(0);
1872 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Op))
1873 Op = CPR->getValue();
1874
1875 // Instcombine load (constant global) into the value loaded...
1876 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
Chris Lattnerbdb0ce02003-07-22 21:46:59 +00001877 if (GV->isConstant() && !GV->isExternal())
Chris Lattner0f1d8a32003-06-26 05:06:25 +00001878 return ReplaceInstUsesWith(LI, GV->getInitializer());
1879
1880 // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded...
1881 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
1882 if (CE->getOpcode() == Instruction::GetElementPtr)
1883 if (ConstantPointerRef *G=dyn_cast<ConstantPointerRef>(CE->getOperand(0)))
1884 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getValue()))
Chris Lattnerbdb0ce02003-07-22 21:46:59 +00001885 if (GV->isConstant() && !GV->isExternal())
Chris Lattner0f1d8a32003-06-26 05:06:25 +00001886 if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE))
1887 return ReplaceInstUsesWith(LI, V);
1888 return 0;
1889}
1890
1891
Chris Lattner9eef8a72003-06-04 04:46:00 +00001892Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
1893 // Change br (not X), label True, label False to: br X, label False, True
Chris Lattner45789ac2003-06-05 20:12:51 +00001894 if (BI.isConditional() && !isa<Constant>(BI.getCondition()))
Chris Lattnere967b342003-06-04 05:10:11 +00001895 if (Value *V = dyn_castNotVal(BI.getCondition())) {
1896 BasicBlock *TrueDest = BI.getSuccessor(0);
1897 BasicBlock *FalseDest = BI.getSuccessor(1);
1898 // Swap Destinations and condition...
1899 BI.setCondition(V);
1900 BI.setSuccessor(0, FalseDest);
1901 BI.setSuccessor(1, TrueDest);
1902 return &BI;
1903 }
Chris Lattner9eef8a72003-06-04 04:46:00 +00001904 return 0;
1905}
Chris Lattner1085bdf2002-11-04 16:18:53 +00001906
Chris Lattnerca081252001-12-14 16:52:21 +00001907
Chris Lattner99f48c62002-09-02 04:59:56 +00001908void InstCombiner::removeFromWorkList(Instruction *I) {
1909 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
1910 WorkList.end());
1911}
1912
Chris Lattner113f4f42002-06-25 16:13:24 +00001913bool InstCombiner::runOnFunction(Function &F) {
Chris Lattner260ab202002-04-18 17:39:14 +00001914 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +00001915
Chris Lattner260ab202002-04-18 17:39:14 +00001916 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +00001917
1918 while (!WorkList.empty()) {
1919 Instruction *I = WorkList.back(); // Get an instruction from the worklist
1920 WorkList.pop_back();
1921
Misha Brukman632df282002-10-29 23:06:16 +00001922 // Check to see if we can DCE or ConstantPropagate the instruction...
Chris Lattner99f48c62002-09-02 04:59:56 +00001923 // Check to see if we can DIE the instruction...
1924 if (isInstructionTriviallyDead(I)) {
1925 // Add operands to the worklist...
1926 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1927 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1928 WorkList.push_back(Op);
1929
1930 ++NumDeadInst;
1931 BasicBlock::iterator BBI = I;
1932 if (dceInstruction(BBI)) {
1933 removeFromWorkList(I);
1934 continue;
1935 }
1936 }
1937
Misha Brukman632df282002-10-29 23:06:16 +00001938 // Instruction isn't dead, see if we can constant propagate it...
Chris Lattner99f48c62002-09-02 04:59:56 +00001939 if (Constant *C = ConstantFoldInstruction(I)) {
1940 // Add operands to the worklist...
1941 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1942 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1943 WorkList.push_back(Op);
Chris Lattnerc6509f42002-12-05 22:41:53 +00001944 ReplaceInstUsesWith(*I, C);
1945
Chris Lattner99f48c62002-09-02 04:59:56 +00001946 ++NumConstProp;
1947 BasicBlock::iterator BBI = I;
1948 if (dceInstruction(BBI)) {
1949 removeFromWorkList(I);
1950 continue;
1951 }
1952 }
1953
Chris Lattnerca081252001-12-14 16:52:21 +00001954 // Now that we have an instruction, try combining it to simplify it...
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001955 if (Instruction *Result = visit(*I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +00001956 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +00001957 // Should we replace the old instruction with a new one?
Chris Lattner053c0932002-05-14 15:24:07 +00001958 if (Result != I) {
1959 // Instructions can end up on the worklist more than once. Make sure
1960 // we do not process an instruction that has been deleted.
Chris Lattner99f48c62002-09-02 04:59:56 +00001961 removeFromWorkList(I);
Chris Lattner260ab202002-04-18 17:39:14 +00001962 ReplaceInstWithInst(I, Result);
Chris Lattner113f4f42002-06-25 16:13:24 +00001963 } else {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001964 BasicBlock::iterator II = I;
1965
1966 // If the instruction was modified, it's possible that it is now dead.
1967 // if so, remove it.
1968 if (dceInstruction(II)) {
1969 // Instructions may end up in the worklist more than once. Erase them
1970 // all.
Chris Lattner99f48c62002-09-02 04:59:56 +00001971 removeFromWorkList(I);
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001972 Result = 0;
1973 }
Chris Lattner053c0932002-05-14 15:24:07 +00001974 }
Chris Lattner260ab202002-04-18 17:39:14 +00001975
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001976 if (Result) {
1977 WorkList.push_back(Result);
1978 AddUsesToWorkList(*Result);
1979 }
Chris Lattner260ab202002-04-18 17:39:14 +00001980 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +00001981 }
1982 }
1983
Chris Lattner260ab202002-04-18 17:39:14 +00001984 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +00001985}
1986
1987Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +00001988 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +00001989}