blob: 0ecec730d90c9c0ab3f7fae5ec27929a852a8d74 [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//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000018#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerae7a0d32002-08-02 19:29:35 +000019#include "llvm/Transforms/Utils/Local.h"
Chris Lattner471bd762003-05-22 19:07:21 +000020#include "llvm/Instructions.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner34428442003-05-27 16:40:51 +000022#include "llvm/Constants.h"
23#include "llvm/ConstantHandling.h"
Chris Lattner1085bdf2002-11-04 16:18:53 +000024#include "llvm/DerivedTypes.h"
Chris Lattner0f1d8a32003-06-26 05:06:25 +000025#include "llvm/GlobalVariable.h"
Chris Lattner60a65912002-02-12 21:07:25 +000026#include "llvm/Support/InstIterator.h"
Chris Lattner260ab202002-04-18 17:39:14 +000027#include "llvm/Support/InstVisitor.h"
Chris Lattner970c33a2003-06-19 17:00:31 +000028#include "llvm/Support/CallSite.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000029#include "Support/Statistic.h"
Chris Lattner053c0932002-05-14 15:24:07 +000030#include <algorithm>
Chris Lattnerca081252001-12-14 16:52:21 +000031
Chris Lattner260ab202002-04-18 17:39:14 +000032namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 Statistic<> NumCombined ("instcombine", "Number of insts combined");
34 Statistic<> NumConstProp("instcombine", "Number of constant folds");
35 Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
36
Chris Lattnerc8e66542002-04-27 06:56:12 +000037 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000038 public InstVisitor<InstCombiner, Instruction*> {
39 // Worklist of all of the instructions that need to be simplified.
40 std::vector<Instruction*> WorkList;
41
Chris Lattner113f4f42002-06-25 16:13:24 +000042 void AddUsesToWorkList(Instruction &I) {
Chris Lattner260ab202002-04-18 17:39:14 +000043 // The instruction was simplified, add all users of the instruction to
44 // the work lists because they might get more simplified now...
45 //
Chris Lattner113f4f42002-06-25 16:13:24 +000046 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner260ab202002-04-18 17:39:14 +000047 UI != UE; ++UI)
48 WorkList.push_back(cast<Instruction>(*UI));
49 }
50
Chris Lattner99f48c62002-09-02 04:59:56 +000051 // removeFromWorkList - remove all instances of I from the worklist.
52 void removeFromWorkList(Instruction *I);
Chris Lattner260ab202002-04-18 17:39:14 +000053 public:
Chris Lattner113f4f42002-06-25 16:13:24 +000054 virtual bool runOnFunction(Function &F);
Chris Lattner260ab202002-04-18 17:39:14 +000055
Chris Lattnerf12cc842002-04-28 21:27:06 +000056 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000057 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000058 }
59
Chris Lattner260ab202002-04-18 17:39:14 +000060 // Visitation implementation - Implement instruction combining for different
61 // instruction types. The semantics are as follows:
62 // Return Value:
63 // null - No change was made
Chris Lattnere6794492002-08-12 21:17:25 +000064 // I - Change was made, I is still valid, I may be dead though
Chris Lattner260ab202002-04-18 17:39:14 +000065 // otherwise - Change was made, replace I with returned instruction
66 //
Chris Lattner113f4f42002-06-25 16:13:24 +000067 Instruction *visitAdd(BinaryOperator &I);
68 Instruction *visitSub(BinaryOperator &I);
69 Instruction *visitMul(BinaryOperator &I);
70 Instruction *visitDiv(BinaryOperator &I);
71 Instruction *visitRem(BinaryOperator &I);
72 Instruction *visitAnd(BinaryOperator &I);
73 Instruction *visitOr (BinaryOperator &I);
74 Instruction *visitXor(BinaryOperator &I);
75 Instruction *visitSetCondInst(BinaryOperator &I);
Chris Lattnere8d6c602003-03-10 19:16:08 +000076 Instruction *visitShiftInst(ShiftInst &I);
Chris Lattner113f4f42002-06-25 16:13:24 +000077 Instruction *visitCastInst(CastInst &CI);
Chris Lattner970c33a2003-06-19 17:00:31 +000078 Instruction *visitCallInst(CallInst &CI);
79 Instruction *visitInvokeInst(InvokeInst &II);
Chris Lattner113f4f42002-06-25 16:13:24 +000080 Instruction *visitPHINode(PHINode &PN);
81 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
Chris Lattner1085bdf2002-11-04 16:18:53 +000082 Instruction *visitAllocationInst(AllocationInst &AI);
Chris Lattner0f1d8a32003-06-26 05:06:25 +000083 Instruction *visitLoadInst(LoadInst &LI);
Chris Lattner9eef8a72003-06-04 04:46:00 +000084 Instruction *visitBranchInst(BranchInst &BI);
Chris Lattner260ab202002-04-18 17:39:14 +000085
86 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +000087 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner6d14f2a2002-08-09 23:47:40 +000088
Chris Lattner970c33a2003-06-19 17:00:31 +000089 private:
90 bool transformConstExprCastCall(CallSite CS);
91
Chris Lattner6d14f2a2002-08-09 23:47:40 +000092 // InsertNewInstBefore - insert an instruction New before instruction Old
93 // in the program. Add the new instruction to the worklist.
94 //
95 void InsertNewInstBefore(Instruction *New, Instruction &Old) {
Chris Lattner65217ff2002-08-23 18:32:43 +000096 assert(New && New->getParent() == 0 &&
97 "New instruction already inserted into a basic block!");
Chris Lattner6d14f2a2002-08-09 23:47:40 +000098 BasicBlock *BB = Old.getParent();
99 BB->getInstList().insert(&Old, New); // Insert inst
100 WorkList.push_back(New); // Add to worklist
101 }
102
103 // ReplaceInstUsesWith - This method is to be used when an instruction is
104 // found to be dead, replacable with another preexisting expression. Here
105 // we add all uses of I to the worklist, replace all uses of I with the new
106 // value, then return I, so that the inst combiner will know that I was
107 // modified.
108 //
109 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
110 AddUsesToWorkList(I); // Add all modified instrs to worklist
111 I.replaceAllUsesWith(V);
112 return &I;
113 }
Chris Lattner7fb29e12003-03-11 00:12:48 +0000114
115 // SimplifyCommutative - This performs a few simplifications for commutative
116 // operators...
117 bool SimplifyCommutative(BinaryOperator &I);
Chris Lattner260ab202002-04-18 17:39:14 +0000118 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000119
Chris Lattnerc8b70922002-07-26 21:12:46 +0000120 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattner260ab202002-04-18 17:39:14 +0000121}
122
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000123// getComplexity: Assign a complexity or rank value to LLVM Values...
124// 0 -> Constant, 1 -> Other, 2 -> Argument, 2 -> Unary, 3 -> OtherInst
125static unsigned getComplexity(Value *V) {
126 if (isa<Instruction>(V)) {
127 if (BinaryOperator::isNeg(V) || BinaryOperator::isNot(V))
128 return 2;
129 return 3;
130 }
131 if (isa<Argument>(V)) return 2;
132 return isa<Constant>(V) ? 0 : 1;
133}
Chris Lattner260ab202002-04-18 17:39:14 +0000134
Chris Lattner7fb29e12003-03-11 00:12:48 +0000135// isOnlyUse - Return true if this instruction will be deleted if we stop using
136// it.
137static bool isOnlyUse(Value *V) {
138 return V->use_size() == 1 || isa<Constant>(V);
139}
140
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000141// SimplifyCommutative - This performs a few simplifications for commutative
142// operators:
Chris Lattner260ab202002-04-18 17:39:14 +0000143//
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000144// 1. Order operands such that they are listed from right (least complex) to
145// left (most complex). This puts constants before unary operators before
146// binary operators.
147//
Chris Lattner7fb29e12003-03-11 00:12:48 +0000148// 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
149// 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000150//
Chris Lattner7fb29e12003-03-11 00:12:48 +0000151bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000152 bool Changed = false;
153 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
154 Changed = !I.swapOperands();
155
156 if (!I.isAssociative()) return Changed;
157 Instruction::BinaryOps Opcode = I.getOpcode();
Chris Lattner7fb29e12003-03-11 00:12:48 +0000158 if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
159 if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
160 if (isa<Constant>(I.getOperand(1))) {
Chris Lattner34428442003-05-27 16:40:51 +0000161 Constant *Folded = ConstantExpr::get(I.getOpcode(),
162 cast<Constant>(I.getOperand(1)),
163 cast<Constant>(Op->getOperand(1)));
Chris Lattner7fb29e12003-03-11 00:12:48 +0000164 I.setOperand(0, Op->getOperand(0));
165 I.setOperand(1, Folded);
166 return true;
167 } else if (BinaryOperator *Op1=dyn_cast<BinaryOperator>(I.getOperand(1)))
168 if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
169 isOnlyUse(Op) && isOnlyUse(Op1)) {
170 Constant *C1 = cast<Constant>(Op->getOperand(1));
171 Constant *C2 = cast<Constant>(Op1->getOperand(1));
172
173 // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
Chris Lattner34428442003-05-27 16:40:51 +0000174 Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
Chris Lattner7fb29e12003-03-11 00:12:48 +0000175 Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
176 Op1->getOperand(0),
177 Op1->getName(), &I);
178 WorkList.push_back(New);
179 I.setOperand(0, New);
180 I.setOperand(1, Folded);
181 return true;
182 }
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000183 }
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000184 return Changed;
Chris Lattner260ab202002-04-18 17:39:14 +0000185}
Chris Lattnerca081252001-12-14 16:52:21 +0000186
Chris Lattnerbb74e222003-03-10 23:06:50 +0000187// dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
188// if the LHS is a constant zero (which is the 'negate' form).
Chris Lattner9fa53de2002-05-06 16:49:18 +0000189//
Chris Lattnerbb74e222003-03-10 23:06:50 +0000190static inline Value *dyn_castNegVal(Value *V) {
191 if (BinaryOperator::isNeg(V))
192 return BinaryOperator::getNegArgument(cast<BinaryOperator>(V));
193
Chris Lattner9244df62003-04-30 22:19:10 +0000194 // Constants can be considered to be negated values if they can be folded...
195 if (Constant *C = dyn_cast<Constant>(V))
Chris Lattner34428442003-05-27 16:40:51 +0000196 return ConstantExpr::get(Instruction::Sub,
197 Constant::getNullValue(V->getType()), C);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000198 return 0;
Chris Lattner9fa53de2002-05-06 16:49:18 +0000199}
200
Chris Lattnerbb74e222003-03-10 23:06:50 +0000201static inline Value *dyn_castNotVal(Value *V) {
202 if (BinaryOperator::isNot(V))
203 return BinaryOperator::getNotArgument(cast<BinaryOperator>(V));
204
205 // Constants can be considered to be not'ed values...
Chris Lattnerdd65d862003-04-30 22:34:06 +0000206 if (ConstantIntegral *C = dyn_cast<ConstantIntegral>(V))
Chris Lattner34428442003-05-27 16:40:51 +0000207 return ConstantExpr::get(Instruction::Xor,
208 ConstantIntegral::getAllOnesValue(C->getType()),C);
Chris Lattnerbb74e222003-03-10 23:06:50 +0000209 return 0;
210}
211
Chris Lattner7fb29e12003-03-11 00:12:48 +0000212// dyn_castFoldableMul - If this value is a multiply that can be folded into
213// other computations (because it has a constant operand), return the
214// non-constant operand of the multiply.
215//
216static inline Value *dyn_castFoldableMul(Value *V) {
217 if (V->use_size() == 1 && V->getType()->isInteger())
218 if (Instruction *I = dyn_cast<Instruction>(V))
219 if (I->getOpcode() == Instruction::Mul)
220 if (isa<Constant>(I->getOperand(1)))
221 return I->getOperand(0);
222 return 0;
Chris Lattner3082c5a2003-02-18 19:28:33 +0000223}
Chris Lattner31ae8632002-08-14 17:51:49 +0000224
Chris Lattner7fb29e12003-03-11 00:12:48 +0000225// dyn_castMaskingAnd - If this value is an And instruction masking a value with
226// a constant, return the constant being anded with.
227//
228static inline Constant *dyn_castMaskingAnd(Value *V) {
229 if (Instruction *I = dyn_cast<Instruction>(V))
230 if (I->getOpcode() == Instruction::And)
231 return dyn_cast<Constant>(I->getOperand(1));
232
233 // If this is a constant, it acts just like we were masking with it.
234 return dyn_cast<Constant>(V);
235}
Chris Lattner3082c5a2003-02-18 19:28:33 +0000236
237// Log2 - Calculate the log base 2 for the specified value if it is exactly a
238// power of 2.
239static unsigned Log2(uint64_t Val) {
240 assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
241 unsigned Count = 0;
242 while (Val != 1) {
243 if (Val & 1) return 0; // Multiple bits set?
244 Val >>= 1;
245 ++Count;
246 }
247 return Count;
Chris Lattner31ae8632002-08-14 17:51:49 +0000248}
249
Chris Lattner113f4f42002-06-25 16:13:24 +0000250Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000251 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000252 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000253
254 // Eliminate 'add int %X, 0'
Chris Lattnere6794492002-08-12 21:17:25 +0000255 if (RHS == Constant::getNullValue(I.getType()))
256 return ReplaceInstUsesWith(I, LHS);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000257
Chris Lattner147e9752002-05-08 22:46:53 +0000258 // -A + B --> B - A
Chris Lattnerbb74e222003-03-10 23:06:50 +0000259 if (Value *V = dyn_castNegVal(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000260 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000261
262 // A + -B --> A - B
Chris Lattnerbb74e222003-03-10 23:06:50 +0000263 if (!isa<Constant>(RHS))
264 if (Value *V = dyn_castNegVal(RHS))
265 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000266
Chris Lattner57c8d992003-02-18 19:57:07 +0000267 // X*C + X --> X * (C+1)
268 if (dyn_castFoldableMul(LHS) == RHS) {
Chris Lattner34428442003-05-27 16:40:51 +0000269 Constant *CP1 =
270 ConstantExpr::get(Instruction::Add,
271 cast<Constant>(cast<Instruction>(LHS)->getOperand(1)),
272 ConstantInt::get(I.getType(), 1));
Chris Lattner57c8d992003-02-18 19:57:07 +0000273 return BinaryOperator::create(Instruction::Mul, RHS, CP1);
274 }
275
276 // X + X*C --> X * (C+1)
277 if (dyn_castFoldableMul(RHS) == LHS) {
Chris Lattner34428442003-05-27 16:40:51 +0000278 Constant *CP1 =
279 ConstantExpr::get(Instruction::Add,
280 cast<Constant>(cast<Instruction>(RHS)->getOperand(1)),
281 ConstantInt::get(I.getType(), 1));
Chris Lattner57c8d992003-02-18 19:57:07 +0000282 return BinaryOperator::create(Instruction::Mul, LHS, CP1);
283 }
284
Chris Lattner7fb29e12003-03-11 00:12:48 +0000285 // (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
286 if (Constant *C1 = dyn_castMaskingAnd(LHS))
287 if (Constant *C2 = dyn_castMaskingAnd(RHS))
Chris Lattner34428442003-05-27 16:40:51 +0000288 if (ConstantExpr::get(Instruction::And, C1, C2)->isNullValue())
Chris Lattner7fb29e12003-03-11 00:12:48 +0000289 return BinaryOperator::create(Instruction::Or, LHS, RHS);
290
Chris Lattner113f4f42002-06-25 16:13:24 +0000291 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000292}
293
Chris Lattnerbdb0ce02003-07-22 21:46:59 +0000294// isSignBit - Return true if the value represented by the constant only has the
295// highest order bit set.
296static bool isSignBit(ConstantInt *CI) {
297 unsigned NumBits = CI->getType()->getPrimitiveSize()*8;
298 return (CI->getRawValue() & ~(-1LL << NumBits)) == (1ULL << (NumBits-1));
299}
300
Chris Lattner113f4f42002-06-25 16:13:24 +0000301Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000302 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000303
Chris Lattnere6794492002-08-12 21:17:25 +0000304 if (Op0 == Op1) // sub X, X -> 0
305 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner260ab202002-04-18 17:39:14 +0000306
Chris Lattnere6794492002-08-12 21:17:25 +0000307 // If this is a 'B = x-(-A)', change to B = x+A...
Chris Lattnerbb74e222003-03-10 23:06:50 +0000308 if (Value *V = dyn_castNegVal(Op1))
Chris Lattner147e9752002-05-08 22:46:53 +0000309 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000310
Chris Lattner3082c5a2003-02-18 19:28:33 +0000311 // Replace (-1 - A) with (~A)...
312 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0))
313 if (C->isAllOnesValue())
314 return BinaryOperator::createNot(Op1);
Chris Lattnerad3c4952002-05-09 01:29:19 +0000315
Chris Lattner3082c5a2003-02-18 19:28:33 +0000316 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
317 if (Op1I->use_size() == 1) {
318 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
319 // is not used by anyone else...
320 //
321 if (Op1I->getOpcode() == Instruction::Sub) {
322 // Swap the two operands of the subexpr...
323 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
324 Op1I->setOperand(0, IIOp1);
325 Op1I->setOperand(1, IIOp0);
326
327 // Create the new top level add instruction...
328 return BinaryOperator::create(Instruction::Add, Op0, Op1);
329 }
330
331 // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
332 //
333 if (Op1I->getOpcode() == Instruction::And &&
334 (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
335 Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
336
337 Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I);
338 return BinaryOperator::create(Instruction::And, Op0, NewNot);
339 }
Chris Lattner57c8d992003-02-18 19:57:07 +0000340
341 // X - X*C --> X * (1-C)
342 if (dyn_castFoldableMul(Op1I) == Op0) {
Chris Lattner34428442003-05-27 16:40:51 +0000343 Constant *CP1 =
344 ConstantExpr::get(Instruction::Sub,
345 ConstantInt::get(I.getType(), 1),
346 cast<Constant>(cast<Instruction>(Op1)->getOperand(1)));
Chris Lattner57c8d992003-02-18 19:57:07 +0000347 assert(CP1 && "Couldn't constant fold 1-C?");
348 return BinaryOperator::create(Instruction::Mul, Op0, CP1);
349 }
Chris Lattnerad3c4952002-05-09 01:29:19 +0000350 }
Chris Lattner3082c5a2003-02-18 19:28:33 +0000351
Chris Lattner57c8d992003-02-18 19:57:07 +0000352 // X*C - X --> X * (C-1)
353 if (dyn_castFoldableMul(Op0) == Op1) {
Chris Lattner34428442003-05-27 16:40:51 +0000354 Constant *CP1 =
355 ConstantExpr::get(Instruction::Sub,
356 cast<Constant>(cast<Instruction>(Op0)->getOperand(1)),
357 ConstantInt::get(I.getType(), 1));
Chris Lattner57c8d992003-02-18 19:57:07 +0000358 assert(CP1 && "Couldn't constant fold C - 1?");
359 return BinaryOperator::create(Instruction::Mul, Op1, CP1);
360 }
361
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000362 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000363}
364
Chris Lattner113f4f42002-06-25 16:13:24 +0000365Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000366 bool Changed = SimplifyCommutative(I);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000367 Value *Op0 = I.getOperand(0);
Chris Lattner260ab202002-04-18 17:39:14 +0000368
Chris Lattnere6794492002-08-12 21:17:25 +0000369 // Simplify mul instructions with a constant RHS...
Chris Lattner3082c5a2003-02-18 19:28:33 +0000370 if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
371 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
372 const Type *Ty = CI->getType();
Chris Lattner35236d82003-06-25 17:09:20 +0000373 int64_t Val = Ty->isSigned() ? cast<ConstantSInt>(CI)->getValue() :
374 (int64_t)cast<ConstantUInt>(CI)->getValue();
Chris Lattner3082c5a2003-02-18 19:28:33 +0000375 switch (Val) {
Chris Lattner35236d82003-06-25 17:09:20 +0000376 case -1: // X * -1 -> -X
377 return BinaryOperator::createNeg(Op0, I.getName());
Chris Lattner3082c5a2003-02-18 19:28:33 +0000378 case 0:
379 return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul double %X, 0'
380 case 1:
381 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul int %X, 1'
382 case 2: // Convert 'mul int %X, 2' to 'add int %X, %X'
383 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
384 }
Chris Lattner31ba1292002-04-29 22:24:47 +0000385
Chris Lattner3082c5a2003-02-18 19:28:33 +0000386 if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C
387 return new ShiftInst(Instruction::Shl, Op0,
388 ConstantUInt::get(Type::UByteTy, C));
389 } else {
390 ConstantFP *Op1F = cast<ConstantFP>(Op1);
391 if (Op1F->isNullValue())
392 return ReplaceInstUsesWith(I, Op1);
Chris Lattner31ba1292002-04-29 22:24:47 +0000393
Chris Lattner3082c5a2003-02-18 19:28:33 +0000394 // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
395 // ANSI says we can drop signals, so we can do this anyway." (from GCC)
396 if (Op1F->getValue() == 1.0)
397 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
398 }
Chris Lattner260ab202002-04-18 17:39:14 +0000399 }
400
Chris Lattner934a64cf2003-03-10 23:23:04 +0000401 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
402 if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
403 return BinaryOperator::create(Instruction::Mul, Op0v, Op1v);
404
Chris Lattner113f4f42002-06-25 16:13:24 +0000405 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000406}
407
Chris Lattner113f4f42002-06-25 16:13:24 +0000408Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000409 // div X, 1 == X
Chris Lattner3082c5a2003-02-18 19:28:33 +0000410 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
Chris Lattnere6794492002-08-12 21:17:25 +0000411 if (RHS->equalsInt(1))
412 return ReplaceInstUsesWith(I, I.getOperand(0));
Chris Lattner3082c5a2003-02-18 19:28:33 +0000413
414 // Check to see if this is an unsigned division with an exact power of 2,
415 // if so, convert to a right shift.
416 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
417 if (uint64_t Val = C->getValue()) // Don't break X / 0
418 if (uint64_t C = Log2(Val))
419 return new ShiftInst(Instruction::Shr, I.getOperand(0),
420 ConstantUInt::get(Type::UByteTy, C));
421 }
422
423 // 0 / X == 0, we don't need to preserve faults!
424 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
425 if (LHS->equalsInt(0))
426 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
427
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000428 return 0;
429}
430
431
Chris Lattner113f4f42002-06-25 16:13:24 +0000432Instruction *InstCombiner::visitRem(BinaryOperator &I) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000433 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
434 if (RHS->equalsInt(1)) // X % 1 == 0
435 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
436
437 // Check to see if this is an unsigned remainder with an exact power of 2,
438 // if so, convert to a bitwise and.
439 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
440 if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero)
441 if (Log2(Val))
442 return BinaryOperator::create(Instruction::And, I.getOperand(0),
443 ConstantUInt::get(I.getType(), Val-1));
444 }
445
446 // 0 % X == 0, we don't need to preserve faults!
447 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
448 if (LHS->equalsInt(0))
Chris Lattnere6794492002-08-12 21:17:25 +0000449 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
450
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000451 return 0;
452}
453
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000454// isMaxValueMinusOne - return true if this is Max-1
Chris Lattnere6794492002-08-12 21:17:25 +0000455static bool isMaxValueMinusOne(const ConstantInt *C) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000456 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
457 // Calculate -1 casted to the right type...
458 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
459 uint64_t Val = ~0ULL; // All ones
460 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
461 return CU->getValue() == Val-1;
462 }
463
464 const ConstantSInt *CS = cast<ConstantSInt>(C);
465
466 // Calculate 0111111111..11111
467 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
468 int64_t Val = INT64_MAX; // All ones
469 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
470 return CS->getValue() == Val-1;
471}
472
473// isMinValuePlusOne - return true if this is Min+1
Chris Lattnere6794492002-08-12 21:17:25 +0000474static bool isMinValuePlusOne(const ConstantInt *C) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000475 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
476 return CU->getValue() == 1;
477
478 const ConstantSInt *CS = cast<ConstantSInt>(C);
479
480 // Calculate 1111111111000000000000
481 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
482 int64_t Val = -1; // All ones
483 Val <<= TypeBits-1; // Shift over to the right spot
484 return CS->getValue() == Val+1;
485}
486
487
Chris Lattner113f4f42002-06-25 16:13:24 +0000488Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000489 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000490 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000491
492 // and X, X = X and X, 0 == 0
Chris Lattnere6794492002-08-12 21:17:25 +0000493 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
494 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000495
496 // and X, -1 == X
Chris Lattner83282632002-08-13 17:50:24 +0000497 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattnere6794492002-08-12 21:17:25 +0000498 if (RHS->isAllOnesValue())
499 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000500
Chris Lattnerbb74e222003-03-10 23:06:50 +0000501 Value *Op0NotVal = dyn_castNotVal(Op0);
502 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000503
504 // (~A & ~B) == (~(A | B)) - Demorgan's Law
Chris Lattnerbb74e222003-03-10 23:06:50 +0000505 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000506 Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal,
507 Op1NotVal,I.getName()+".demorgan",
508 &I);
Chris Lattner3e327a42003-03-10 23:13:59 +0000509 WorkList.push_back(Or);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000510 return BinaryOperator::createNot(Or);
511 }
512
513 if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
514 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner65217ff2002-08-23 18:32:43 +0000515
Chris Lattner113f4f42002-06-25 16:13:24 +0000516 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000517}
518
519
520
Chris Lattner113f4f42002-06-25 16:13:24 +0000521Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000522 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000523 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000524
525 // or X, X = X or X, 0 == X
Chris Lattnere6794492002-08-12 21:17:25 +0000526 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
527 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000528
529 // or X, -1 == -1
Chris Lattner83282632002-08-13 17:50:24 +0000530 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattnere6794492002-08-12 21:17:25 +0000531 if (RHS->isAllOnesValue())
532 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000533
Chris Lattner3e327a42003-03-10 23:13:59 +0000534 Value *Op0NotVal = dyn_castNotVal(Op0);
535 Value *Op1NotVal = dyn_castNotVal(Op1);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000536
Chris Lattner3e327a42003-03-10 23:13:59 +0000537 if (Op1 == Op0NotVal) // ~A | A == -1
538 return ReplaceInstUsesWith(I,
539 ConstantIntegral::getAllOnesValue(I.getType()));
540
541 if (Op0 == Op1NotVal) // A | ~A == -1
542 return ReplaceInstUsesWith(I,
543 ConstantIntegral::getAllOnesValue(I.getType()));
544
545 // (~A | ~B) == (~(A & B)) - Demorgan's Law
546 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
547 Instruction *And = BinaryOperator::create(Instruction::And, Op0NotVal,
548 Op1NotVal,I.getName()+".demorgan",
549 &I);
550 WorkList.push_back(And);
551 return BinaryOperator::createNot(And);
552 }
Chris Lattner3082c5a2003-02-18 19:28:33 +0000553
Chris Lattner113f4f42002-06-25 16:13:24 +0000554 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000555}
556
557
558
Chris Lattner113f4f42002-06-25 16:13:24 +0000559Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000560 bool Changed = SimplifyCommutative(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000561 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000562
563 // xor X, X = 0
Chris Lattnere6794492002-08-12 21:17:25 +0000564 if (Op0 == Op1)
565 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000566
Chris Lattner83282632002-08-13 17:50:24 +0000567 if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000568 // xor X, 0 == X
Chris Lattnere6794492002-08-12 21:17:25 +0000569 if (Op1C->isNullValue())
570 return ReplaceInstUsesWith(I, Op0);
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000571
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000572 // Is this a "NOT" instruction?
573 if (Op1C->isAllOnesValue()) {
574 // xor (xor X, -1), -1 = not (not X) = X
Chris Lattnerbb74e222003-03-10 23:06:50 +0000575 if (Value *X = dyn_castNotVal(Op0))
Chris Lattner31ae8632002-08-14 17:51:49 +0000576 return ReplaceInstUsesWith(I, X);
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000577
578 // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
579 if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
580 if (SCI->use_size() == 1)
581 return new SetCondInst(SCI->getInverseCondition(),
582 SCI->getOperand(0), SCI->getOperand(1));
583 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000584 }
585
Chris Lattnerbb74e222003-03-10 23:06:50 +0000586 if (Value *X = dyn_castNotVal(Op0)) // ~A ^ A == -1
Chris Lattner3082c5a2003-02-18 19:28:33 +0000587 if (X == Op1)
588 return ReplaceInstUsesWith(I,
589 ConstantIntegral::getAllOnesValue(I.getType()));
590
Chris Lattnerbb74e222003-03-10 23:06:50 +0000591 if (Value *X = dyn_castNotVal(Op1)) // A ^ ~A == -1
Chris Lattner3082c5a2003-02-18 19:28:33 +0000592 if (X == Op0)
593 return ReplaceInstUsesWith(I,
594 ConstantIntegral::getAllOnesValue(I.getType()));
595
Chris Lattner1bbb7b62003-03-10 18:24:17 +0000596 if (Instruction *Op1I = dyn_cast<Instruction>(Op1))
597 if (Op1I->getOpcode() == Instruction::Or)
598 if (Op1I->getOperand(0) == Op0) { // B^(B|A) == (A|B)^B
599 cast<BinaryOperator>(Op1I)->swapOperands();
600 I.swapOperands();
601 std::swap(Op0, Op1);
602 } else if (Op1I->getOperand(1) == Op0) { // B^(A|B) == (A|B)^B
603 I.swapOperands();
604 std::swap(Op0, Op1);
605 }
606
607 if (Instruction *Op0I = dyn_cast<Instruction>(Op0))
608 if (Op0I->getOpcode() == Instruction::Or && Op0I->use_size() == 1) {
609 if (Op0I->getOperand(0) == Op1) // (B|A)^B == (A|B)^B
610 cast<BinaryOperator>(Op0I)->swapOperands();
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000611 if (Op0I->getOperand(1) == Op1) { // (A|B)^B == A & ~B
Chris Lattner1bbb7b62003-03-10 18:24:17 +0000612 Value *NotB = BinaryOperator::createNot(Op1, Op1->getName()+".not", &I);
613 WorkList.push_back(cast<Instruction>(NotB));
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000614 return BinaryOperator::create(Instruction::And, Op0I->getOperand(0),
615 NotB);
Chris Lattner1bbb7b62003-03-10 18:24:17 +0000616 }
617 }
618
Chris Lattner7fb29e12003-03-11 00:12:48 +0000619 // (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1^C2 == 0
620 if (Constant *C1 = dyn_castMaskingAnd(Op0))
621 if (Constant *C2 = dyn_castMaskingAnd(Op1))
Chris Lattner34428442003-05-27 16:40:51 +0000622 if (ConstantExpr::get(Instruction::And, C1, C2)->isNullValue())
Chris Lattner7fb29e12003-03-11 00:12:48 +0000623 return BinaryOperator::create(Instruction::Or, Op0, Op1);
624
Chris Lattner113f4f42002-06-25 16:13:24 +0000625 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000626}
627
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000628// AddOne, SubOne - Add or subtract a constant one from an integer constant...
629static Constant *AddOne(ConstantInt *C) {
Chris Lattner34428442003-05-27 16:40:51 +0000630 Constant *Result = ConstantExpr::get(Instruction::Add, C,
631 ConstantInt::get(C->getType(), 1));
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000632 assert(Result && "Constant folding integer addition failed!");
633 return Result;
634}
635static Constant *SubOne(ConstantInt *C) {
Chris Lattner34428442003-05-27 16:40:51 +0000636 Constant *Result = ConstantExpr::get(Instruction::Sub, C,
637 ConstantInt::get(C->getType(), 1));
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000638 assert(Result && "Constant folding integer addition failed!");
639 return Result;
640}
641
Chris Lattner1fc23f32002-05-09 20:11:54 +0000642// isTrueWhenEqual - Return true if the specified setcondinst instruction is
643// true when both operands are equal...
644//
Chris Lattner113f4f42002-06-25 16:13:24 +0000645static bool isTrueWhenEqual(Instruction &I) {
646 return I.getOpcode() == Instruction::SetEQ ||
647 I.getOpcode() == Instruction::SetGE ||
648 I.getOpcode() == Instruction::SetLE;
Chris Lattner1fc23f32002-05-09 20:11:54 +0000649}
650
Chris Lattner113f4f42002-06-25 16:13:24 +0000651Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
Chris Lattnerdcf240a2003-03-10 21:43:22 +0000652 bool Changed = SimplifyCommutative(I);
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000653 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
654 const Type *Ty = Op0->getType();
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000655
656 // setcc X, X
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000657 if (Op0 == Op1)
658 return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
Chris Lattner1fc23f32002-05-09 20:11:54 +0000659
660 // setcc <global*>, 0 - Global value addresses are never null!
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000661 if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
662 return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
663
664 // setcc's with boolean values can always be turned into bitwise operations
665 if (Ty == Type::BoolTy) {
666 // If this is <, >, or !=, we can change this into a simple xor instruction
667 if (!isTrueWhenEqual(I))
668 return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
669
670 // Otherwise we need to make a temporary intermediate instruction and insert
671 // it into the instruction stream. This is what we are after:
672 //
673 // seteq bool %A, %B -> ~(A^B)
674 // setle bool %A, %B -> ~A | B
675 // setge bool %A, %B -> A | ~B
676 //
677 if (I.getOpcode() == Instruction::SetEQ) { // seteq case
678 Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
679 I.getName()+"tmp");
680 InsertNewInstBefore(Xor, I);
Chris Lattner31ae8632002-08-14 17:51:49 +0000681 return BinaryOperator::createNot(Xor, I.getName());
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000682 }
683
684 // Handle the setXe cases...
685 assert(I.getOpcode() == Instruction::SetGE ||
686 I.getOpcode() == Instruction::SetLE);
687
688 if (I.getOpcode() == Instruction::SetGE)
689 std::swap(Op0, Op1); // Change setge -> setle
690
691 // Now we just have the SetLE case.
Chris Lattner31ae8632002-08-14 17:51:49 +0000692 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000693 InsertNewInstBefore(Not, I);
694 return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
695 }
696
697 // Check to see if we are doing one of many comparisons against constant
698 // integers at the end of their ranges...
699 //
700 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
Chris Lattnere967b342003-06-04 05:10:11 +0000701 if (CI->isNullValue()) {
702 if (I.getOpcode() == Instruction::SetNE)
703 return new CastInst(Op0, Type::BoolTy, I.getName());
704 else if (I.getOpcode() == Instruction::SetEQ) {
705 // seteq X, 0 -> not (cast X to bool)
706 Instruction *Val = new CastInst(Op0, Type::BoolTy, I.getName()+".not");
707 InsertNewInstBefore(Val, I);
708 return BinaryOperator::createNot(Val, I.getName());
709 }
710 }
Chris Lattner791ac1a2003-06-01 03:35:25 +0000711
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000712 // Check to see if we are comparing against the minimum or maximum value...
Chris Lattnere6794492002-08-12 21:17:25 +0000713 if (CI->isMinValue()) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000714 if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE
715 return ReplaceInstUsesWith(I, ConstantBool::False);
716 if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE
717 return ReplaceInstUsesWith(I, ConstantBool::True);
718 if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN
719 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
720 if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN
721 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
722
Chris Lattnere6794492002-08-12 21:17:25 +0000723 } else if (CI->isMaxValue()) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000724 if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE
725 return ReplaceInstUsesWith(I, ConstantBool::False);
726 if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE
727 return ReplaceInstUsesWith(I, ConstantBool::True);
728 if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX
729 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
730 if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX
731 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
732
733 // Comparing against a value really close to min or max?
734 } else if (isMinValuePlusOne(CI)) {
735 if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN
736 return BinaryOperator::create(Instruction::SetEQ, Op0,
737 SubOne(CI), I.getName());
738 if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN
739 return BinaryOperator::create(Instruction::SetNE, Op0,
740 SubOne(CI), I.getName());
741
742 } else if (isMaxValueMinusOne(CI)) {
743 if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX
744 return BinaryOperator::create(Instruction::SetEQ, Op0,
745 AddOne(CI), I.getName());
746 if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX
747 return BinaryOperator::create(Instruction::SetNE, Op0,
748 AddOne(CI), I.getName());
749 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000750 }
751
Chris Lattner113f4f42002-06-25 16:13:24 +0000752 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000753}
754
755
756
Chris Lattnere8d6c602003-03-10 19:16:08 +0000757Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000758 assert(I.getOperand(1)->getType() == Type::UByteTy);
759 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000760
761 // shl X, 0 == X and shr X, 0 == X
762 // shl 0, X == 0 and shr 0, X == 0
763 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
Chris Lattnere6794492002-08-12 21:17:25 +0000764 Op0 == Constant::getNullValue(Op0->getType()))
765 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000766
Chris Lattnere8d6c602003-03-10 19:16:08 +0000767 // If this is a shift of a shift, see if we can fold the two together...
768 if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) {
769 if (isa<Constant>(Op1) && isa<Constant>(Op0SI->getOperand(1))) {
770 ConstantUInt *ShiftAmt1C = cast<ConstantUInt>(Op0SI->getOperand(1));
771 unsigned ShiftAmt1 = ShiftAmt1C->getValue();
772 unsigned ShiftAmt2 = cast<ConstantUInt>(Op1)->getValue();
773
774 // Check for (A << c1) << c2 and (A >> c1) >> c2
775 if (I.getOpcode() == Op0SI->getOpcode()) {
776 unsigned Amt = ShiftAmt1+ShiftAmt2; // Fold into one big shift...
777 return new ShiftInst(I.getOpcode(), Op0SI->getOperand(0),
778 ConstantUInt::get(Type::UByteTy, Amt));
779 }
780
781 if (I.getType()->isUnsigned()) { // Check for (A << c1) >> c2 or visaversa
782 // Calculate bitmask for what gets shifted off the edge...
783 Constant *C = ConstantIntegral::getAllOnesValue(I.getType());
784 if (I.getOpcode() == Instruction::Shr)
Chris Lattner34428442003-05-27 16:40:51 +0000785 C = ConstantExpr::getShift(Instruction::Shr, C, ShiftAmt1C);
Chris Lattnere8d6c602003-03-10 19:16:08 +0000786 else
Chris Lattner34428442003-05-27 16:40:51 +0000787 C = ConstantExpr::getShift(Instruction::Shl, C, ShiftAmt1C);
Chris Lattnere8d6c602003-03-10 19:16:08 +0000788
789 Instruction *Mask =
790 BinaryOperator::create(Instruction::And, Op0SI->getOperand(0),
791 C, Op0SI->getOperand(0)->getName()+".mask",&I);
792 WorkList.push_back(Mask);
793
794 // Figure out what flavor of shift we should use...
795 if (ShiftAmt1 == ShiftAmt2)
796 return ReplaceInstUsesWith(I, Mask); // (A << c) >> c === A & c2
797 else if (ShiftAmt1 < ShiftAmt2) {
798 return new ShiftInst(I.getOpcode(), Mask,
799 ConstantUInt::get(Type::UByteTy, ShiftAmt2-ShiftAmt1));
800 } else {
801 return new ShiftInst(Op0SI->getOpcode(), Mask,
802 ConstantUInt::get(Type::UByteTy, ShiftAmt1-ShiftAmt2));
803 }
804 }
805 }
806 }
807
Chris Lattnere6794492002-08-12 21:17:25 +0000808 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000809 // a signed value.
810 //
811 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
Chris Lattnere8d6c602003-03-10 19:16:08 +0000812 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
813 if (CUI->getValue() >= TypeBits &&
814 (!Op0->getType()->isSigned() || I.getOpcode() == Instruction::Shl))
815 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
Chris Lattner55f3d942002-09-10 23:04:09 +0000816
817 // Check to see if we are shifting left by 1. If so, turn it into an add
818 // instruction.
819 if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
Chris Lattner3082c5a2003-02-18 19:28:33 +0000820 // Convert 'shl int %X, 1' to 'add int %X, %X'
Chris Lattner55f3d942002-09-10 23:04:09 +0000821 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
822
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000823 }
Chris Lattner2e0fb392002-10-08 16:16:40 +0000824
825 // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
826 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
827 if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue())
828 return ReplaceInstUsesWith(I, CSI);
829
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000830 return 0;
831}
832
833
Chris Lattner48a44f72002-05-02 17:06:02 +0000834// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
835// instruction.
836//
Chris Lattner113f4f42002-06-25 16:13:24 +0000837static inline bool isEliminableCastOfCast(const CastInst &CI,
Chris Lattner48a44f72002-05-02 17:06:02 +0000838 const CastInst *CSrc) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000839 assert(CI.getOperand(0) == CSrc);
Chris Lattner48a44f72002-05-02 17:06:02 +0000840 const Type *SrcTy = CSrc->getOperand(0)->getType();
841 const Type *MidTy = CSrc->getType();
Chris Lattner113f4f42002-06-25 16:13:24 +0000842 const Type *DstTy = CI.getType();
Chris Lattner48a44f72002-05-02 17:06:02 +0000843
Chris Lattner650b6da2002-08-02 20:00:25 +0000844 // It is legal to eliminate the instruction if casting A->B->A if the sizes
845 // are identical and the bits don't get reinterpreted (for example
Chris Lattner0bb75912002-08-14 23:21:10 +0000846 // int->float->int would not be allowed)
Misha Brukmane5838c42003-05-20 18:45:36 +0000847 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
Chris Lattner650b6da2002-08-02 20:00:25 +0000848 return true;
Chris Lattner48a44f72002-05-02 17:06:02 +0000849
850 // Allow free casting and conversion of sizes as long as the sign doesn't
851 // change...
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000852 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
Chris Lattner650b6da2002-08-02 20:00:25 +0000853 unsigned SrcSize = SrcTy->getPrimitiveSize();
854 unsigned MidSize = MidTy->getPrimitiveSize();
855 unsigned DstSize = DstTy->getPrimitiveSize();
Chris Lattner650b6da2002-08-02 20:00:25 +0000856
Chris Lattner3732aca2002-08-15 16:15:25 +0000857 // Cases where we are monotonically decreasing the size of the type are
858 // always ok, regardless of what sign changes are going on.
859 //
Chris Lattner0bb75912002-08-14 23:21:10 +0000860 if (SrcSize >= MidSize && MidSize >= DstSize)
Chris Lattner650b6da2002-08-02 20:00:25 +0000861 return true;
Chris Lattner3732aca2002-08-15 16:15:25 +0000862
Chris Lattner555518c2002-09-23 23:39:43 +0000863 // Cases where the source and destination type are the same, but the middle
864 // type is bigger are noops.
865 //
866 if (SrcSize == DstSize && MidSize > SrcSize)
867 return true;
868
Chris Lattner3732aca2002-08-15 16:15:25 +0000869 // If we are monotonically growing, things are more complex.
870 //
871 if (SrcSize <= MidSize && MidSize <= DstSize) {
872 // We have eight combinations of signedness to worry about. Here's the
873 // table:
874 static const int SignTable[8] = {
875 // CODE, SrcSigned, MidSigned, DstSigned, Comment
876 1, // U U U Always ok
877 1, // U U S Always ok
878 3, // U S U Ok iff SrcSize != MidSize
879 3, // U S S Ok iff SrcSize != MidSize
880 0, // S U U Never ok
881 2, // S U S Ok iff MidSize == DstSize
882 1, // S S U Always ok
883 1, // S S S Always ok
884 };
885
886 // Choose an action based on the current entry of the signtable that this
887 // cast of cast refers to...
888 unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
889 switch (SignTable[Row]) {
890 case 0: return false; // Never ok
891 case 1: return true; // Always ok
892 case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
893 case 3: // Ok iff SrcSize != MidSize
894 return SrcSize != MidSize || SrcTy == Type::BoolTy;
895 default: assert(0 && "Bad entry in sign table!");
896 }
Chris Lattner3732aca2002-08-15 16:15:25 +0000897 }
Chris Lattner650b6da2002-08-02 20:00:25 +0000898 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000899
900 // Otherwise, we cannot succeed. Specifically we do not want to allow things
901 // like: short -> ushort -> uint, because this can create wrong results if
902 // the input short is negative!
903 //
904 return false;
905}
906
907
908// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +0000909//
Chris Lattner113f4f42002-06-25 16:13:24 +0000910Instruction *InstCombiner::visitCastInst(CastInst &CI) {
Chris Lattner55d4bda2003-06-23 21:59:52 +0000911 Value *Src = CI.getOperand(0);
912
Chris Lattner48a44f72002-05-02 17:06:02 +0000913 // If the user is casting a value to the same type, eliminate this cast
914 // instruction...
Chris Lattner55d4bda2003-06-23 21:59:52 +0000915 if (CI.getType() == Src->getType())
916 return ReplaceInstUsesWith(CI, Src);
Chris Lattner48a44f72002-05-02 17:06:02 +0000917
Chris Lattner48a44f72002-05-02 17:06:02 +0000918 // If casting the result of another cast instruction, try to eliminate this
919 // one!
920 //
Chris Lattner55d4bda2003-06-23 21:59:52 +0000921 if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {
Chris Lattner48a44f72002-05-02 17:06:02 +0000922 if (isEliminableCastOfCast(CI, CSrc)) {
923 // This instruction now refers directly to the cast's src operand. This
924 // has a good chance of making CSrc dead.
Chris Lattner113f4f42002-06-25 16:13:24 +0000925 CI.setOperand(0, CSrc->getOperand(0));
926 return &CI;
Chris Lattner48a44f72002-05-02 17:06:02 +0000927 }
928
Chris Lattner650b6da2002-08-02 20:00:25 +0000929 // If this is an A->B->A cast, and we are dealing with integral types, try
930 // to convert this into a logical 'and' instruction.
931 //
932 if (CSrc->getOperand(0)->getType() == CI.getType() &&
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000933 CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
Chris Lattner650b6da2002-08-02 20:00:25 +0000934 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
935 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
936 assert(CSrc->getType() != Type::ULongTy &&
937 "Cannot have type bigger than ulong!");
Chris Lattner196897c2003-05-26 23:41:32 +0000938 uint64_t AndValue = (1ULL << CSrc->getType()->getPrimitiveSize()*8)-1;
Chris Lattner650b6da2002-08-02 20:00:25 +0000939 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
940 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
941 AndOp);
942 }
943 }
944
Chris Lattnerd0d51602003-06-21 23:12:02 +0000945 // If casting the result of a getelementptr instruction with no offset, turn
946 // this into a cast of the original pointer!
947 //
Chris Lattner55d4bda2003-06-23 21:59:52 +0000948 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) {
Chris Lattnerd0d51602003-06-21 23:12:02 +0000949 bool AllZeroOperands = true;
950 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
951 if (!isa<Constant>(GEP->getOperand(i)) ||
952 !cast<Constant>(GEP->getOperand(i))->isNullValue()) {
953 AllZeroOperands = false;
954 break;
955 }
956 if (AllZeroOperands) {
957 CI.setOperand(0, GEP->getOperand(0));
958 return &CI;
959 }
960 }
961
Chris Lattner55d4bda2003-06-23 21:59:52 +0000962 // If this is a cast to bool (which is effectively a "!=0" test), then we can
963 // perform a few optimizations...
964 //
965 if (CI.getType() == Type::BoolTy) {
966 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Src)) {
967 Value *Op0 = BO->getOperand(0), *Op1 = BO->getOperand(1);
968
Chris Lattnerbdb0ce02003-07-22 21:46:59 +0000969 switch (BO->getOpcode()) {
970 case Instruction::Sub:
971 case Instruction::Xor:
972 // Replace (cast ([sub|xor] A, B) to bool) with (setne A, B)
Chris Lattner55d4bda2003-06-23 21:59:52 +0000973 return new SetCondInst(Instruction::SetNE, Op0, Op1);
974
975 // Replace (cast (add A, B) to bool) with (setne A, -B) if B is
976 // efficiently invertible, or if the add has just this one use.
Chris Lattnerbdb0ce02003-07-22 21:46:59 +0000977 case Instruction::Add:
Chris Lattner55d4bda2003-06-23 21:59:52 +0000978 if (Value *NegVal = dyn_castNegVal(Op1))
979 return new SetCondInst(Instruction::SetNE, Op0, NegVal);
980 else if (Value *NegVal = dyn_castNegVal(Op0))
981 return new SetCondInst(Instruction::SetNE, NegVal, Op1);
982 else if (BO->use_size() == 1) {
983 Instruction *Neg = BinaryOperator::createNeg(Op1, BO->getName());
984 BO->setName("");
985 InsertNewInstBefore(Neg, CI);
986 return new SetCondInst(Instruction::SetNE, Op0, Neg);
987 }
Chris Lattnerbdb0ce02003-07-22 21:46:59 +0000988 break;
989
990 case Instruction::And:
991 // Replace (cast (and X, (1 << size(X)-1)) to bool) with x < 0,
992 // converting X to be a signed value as appropriate. Don't worry about
993 // bool values, as they will be optimized other ways if they occur in
994 // this configuration.
995 if (ConstantInt *CInt = dyn_cast<ConstantInt>(Op1))
996 if (isSignBit(CInt)) {
997 // If 'X' is not signed, insert a cast now...
998 if (!CInt->getType()->isSigned()) {
999 const Type *DestTy;
1000 switch (CInt->getType()->getPrimitiveID()) {
1001 case Type::UByteTyID: DestTy = Type::SByteTy; break;
1002 case Type::UShortTyID: DestTy = Type::ShortTy; break;
1003 case Type::UIntTyID: DestTy = Type::IntTy; break;
1004 case Type::ULongTyID: DestTy = Type::LongTy; break;
1005 default: assert(0 && "Invalid unsigned integer type!"); abort();
1006 }
1007 CastInst *NewCI = new CastInst(Op0, DestTy,
1008 Op0->getName()+".signed");
1009 InsertNewInstBefore(NewCI, CI);
1010 Op0 = NewCI;
1011 }
1012 return new SetCondInst(Instruction::SetLT, Op0,
1013 Constant::getNullValue(Op0->getType()));
1014 }
1015 break;
1016 default: break;
1017 }
Chris Lattner55d4bda2003-06-23 21:59:52 +00001018 }
1019 }
1020
Chris Lattner260ab202002-04-18 17:39:14 +00001021 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +00001022}
1023
Chris Lattner970c33a2003-06-19 17:00:31 +00001024// CallInst simplification
1025//
1026Instruction *InstCombiner::visitCallInst(CallInst &CI) {
1027 if (transformConstExprCastCall(&CI)) return 0;
1028 return 0;
1029}
1030
1031// InvokeInst simplification
1032//
1033Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) {
1034 if (transformConstExprCastCall(&II)) return 0;
1035 return 0;
1036}
1037
1038// getPromotedType - Return the specified type promoted as it would be to pass
1039// though a va_arg area...
1040static const Type *getPromotedType(const Type *Ty) {
1041 switch (Ty->getPrimitiveID()) {
1042 case Type::SByteTyID:
1043 case Type::ShortTyID: return Type::IntTy;
1044 case Type::UByteTyID:
1045 case Type::UShortTyID: return Type::UIntTy;
1046 case Type::FloatTyID: return Type::DoubleTy;
1047 default: return Ty;
1048 }
1049}
1050
1051// transformConstExprCastCall - If the callee is a constexpr cast of a function,
1052// attempt to move the cast to the arguments of the call/invoke.
1053//
1054bool InstCombiner::transformConstExprCastCall(CallSite CS) {
1055 if (!isa<ConstantExpr>(CS.getCalledValue())) return false;
1056 ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue());
1057 if (CE->getOpcode() != Instruction::Cast ||
1058 !isa<ConstantPointerRef>(CE->getOperand(0)))
1059 return false;
1060 ConstantPointerRef *CPR = cast<ConstantPointerRef>(CE->getOperand(0));
1061 if (!isa<Function>(CPR->getValue())) return false;
1062 Function *Callee = cast<Function>(CPR->getValue());
1063 Instruction *Caller = CS.getInstruction();
1064
1065 // Okay, this is a cast from a function to a different type. Unless doing so
1066 // would cause a type conversion of one of our arguments, change this call to
1067 // be a direct call with arguments casted to the appropriate types.
1068 //
1069 const FunctionType *FT = Callee->getFunctionType();
1070 const Type *OldRetTy = Caller->getType();
1071
1072 if (Callee->isExternal() &&
1073 !OldRetTy->isLosslesslyConvertibleTo(FT->getReturnType()))
1074 return false; // Cannot transform this return value...
1075
1076 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
1077 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs);
1078
1079 CallSite::arg_iterator AI = CS.arg_begin();
1080 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) {
1081 const Type *ParamTy = FT->getParamType(i);
1082 bool isConvertible = (*AI)->getType()->isLosslesslyConvertibleTo(ParamTy);
1083 if (Callee->isExternal() && !isConvertible) return false;
1084 }
1085
1086 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() &&
1087 Callee->isExternal())
1088 return false; // Do not delete arguments unless we have a function body...
1089
1090 // Okay, we decided that this is a safe thing to do: go ahead and start
1091 // inserting cast instructions as necessary...
1092 std::vector<Value*> Args;
1093 Args.reserve(NumActualArgs);
1094
1095 AI = CS.arg_begin();
1096 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) {
1097 const Type *ParamTy = FT->getParamType(i);
1098 if ((*AI)->getType() == ParamTy) {
1099 Args.push_back(*AI);
1100 } else {
1101 Instruction *Cast = new CastInst(*AI, ParamTy, "tmp");
1102 InsertNewInstBefore(Cast, *Caller);
1103 Args.push_back(Cast);
1104 }
1105 }
1106
1107 // If the function takes more arguments than the call was taking, add them
1108 // now...
1109 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i)
1110 Args.push_back(Constant::getNullValue(FT->getParamType(i)));
1111
1112 // If we are removing arguments to the function, emit an obnoxious warning...
1113 if (FT->getNumParams() < NumActualArgs)
1114 if (!FT->isVarArg()) {
1115 std::cerr << "WARNING: While resolving call to function '"
1116 << Callee->getName() << "' arguments were dropped!\n";
1117 } else {
1118 // Add all of the arguments in their promoted form to the arg list...
1119 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) {
1120 const Type *PTy = getPromotedType((*AI)->getType());
1121 if (PTy != (*AI)->getType()) {
1122 // Must promote to pass through va_arg area!
1123 Instruction *Cast = new CastInst(*AI, PTy, "tmp");
1124 InsertNewInstBefore(Cast, *Caller);
1125 Args.push_back(Cast);
1126 } else {
1127 Args.push_back(*AI);
1128 }
1129 }
1130 }
1131
1132 if (FT->getReturnType() == Type::VoidTy)
1133 Caller->setName(""); // Void type should not have a name...
1134
1135 Instruction *NC;
1136 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
1137 NC = new InvokeInst(Callee, II->getNormalDest(), II->getExceptionalDest(),
1138 Args, Caller->getName(), Caller);
1139 } else {
1140 NC = new CallInst(Callee, Args, Caller->getName(), Caller);
1141 }
1142
1143 // Insert a cast of the return type as necessary...
1144 Value *NV = NC;
1145 if (Caller->getType() != NV->getType() && !Caller->use_empty()) {
1146 if (NV->getType() != Type::VoidTy) {
1147 NV = NC = new CastInst(NC, Caller->getType(), "tmp");
1148 InsertNewInstBefore(NC, *Caller);
1149 AddUsesToWorkList(*Caller);
1150 } else {
1151 NV = Constant::getNullValue(Caller->getType());
1152 }
1153 }
1154
1155 if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
1156 Caller->replaceAllUsesWith(NV);
1157 Caller->getParent()->getInstList().erase(Caller);
1158 removeFromWorkList(Caller);
1159 return true;
1160}
1161
1162
Chris Lattner48a44f72002-05-02 17:06:02 +00001163
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001164// PHINode simplification
1165//
Chris Lattner113f4f42002-06-25 16:13:24 +00001166Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001167 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattnere6794492002-08-12 21:17:25 +00001168 if (PN.getNumIncomingValues() == 1)
1169 return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
Chris Lattner9cd1e662002-08-20 15:35:35 +00001170
1171 // Otherwise if all of the incoming values are the same for the PHI, replace
1172 // the PHI node with the incoming value.
1173 //
Chris Lattnerf6c0efa2002-08-22 20:22:01 +00001174 Value *InVal = 0;
1175 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1176 if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself...
1177 if (InVal && PN.getIncomingValue(i) != InVal)
1178 return 0; // Not the same, bail out.
1179 else
1180 InVal = PN.getIncomingValue(i);
1181
1182 // The only case that could cause InVal to be null is if we have a PHI node
1183 // that only has entries for itself. In this case, there is no entry into the
1184 // loop, so kill the PHI.
1185 //
1186 if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001187
Chris Lattner9cd1e662002-08-20 15:35:35 +00001188 // All of the incoming values are the same, replace the PHI node now.
1189 return ReplaceInstUsesWith(PN, InVal);
Chris Lattnerbbbdd852002-05-06 18:06:38 +00001190}
1191
Chris Lattner48a44f72002-05-02 17:06:02 +00001192
Chris Lattner113f4f42002-06-25 16:13:24 +00001193Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner471bd762003-05-22 19:07:21 +00001194 // Is it 'getelementptr %P, long 0' or 'getelementptr %P'
Chris Lattner113f4f42002-06-25 16:13:24 +00001195 // If so, eliminate the noop.
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001196 if ((GEP.getNumOperands() == 2 &&
Chris Lattner136dab72002-09-11 01:21:33 +00001197 GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
Chris Lattnere6794492002-08-12 21:17:25 +00001198 GEP.getNumOperands() == 1)
1199 return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
Chris Lattner48a44f72002-05-02 17:06:02 +00001200
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001201 // Combine Indices - If the source pointer to this getelementptr instruction
1202 // is a getelementptr instruction, combine the indices of the two
1203 // getelementptr instructions into a single instruction.
1204 //
Chris Lattnerc59af1d2002-08-17 22:21:59 +00001205 if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001206 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +00001207
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001208 // Can we combine the two pointer arithmetics offsets?
Chris Lattner471bd762003-05-22 19:07:21 +00001209 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
1210 isa<Constant>(GEP.getOperand(1))) {
Chris Lattner235af562003-03-05 22:33:14 +00001211 // Replace: gep (gep %P, long C1), long C2, ...
1212 // With: gep %P, long (C1+C2), ...
Chris Lattner34428442003-05-27 16:40:51 +00001213 Value *Sum = ConstantExpr::get(Instruction::Add,
1214 cast<Constant>(Src->getOperand(1)),
1215 cast<Constant>(GEP.getOperand(1)));
Chris Lattner235af562003-03-05 22:33:14 +00001216 assert(Sum && "Constant folding of longs failed!?");
1217 GEP.setOperand(0, Src->getOperand(0));
1218 GEP.setOperand(1, Sum);
1219 AddUsesToWorkList(*Src); // Reduce use count of Src
1220 return &GEP;
Chris Lattner471bd762003-05-22 19:07:21 +00001221 } else if (Src->getNumOperands() == 2) {
Chris Lattner235af562003-03-05 22:33:14 +00001222 // Replace: gep (gep %P, long B), long A, ...
1223 // With: T = long A+B; gep %P, T, ...
1224 //
1225 Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1),
1226 GEP.getOperand(1),
1227 Src->getName()+".sum", &GEP);
1228 GEP.setOperand(0, Src->getOperand(0));
1229 GEP.setOperand(1, Sum);
1230 WorkList.push_back(cast<Instruction>(Sum));
1231 return &GEP;
Chris Lattner5d606a02002-11-04 16:43:32 +00001232 } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
Chris Lattnera8339e32002-09-17 21:05:42 +00001233 Src->getNumOperands() != 1) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001234 // Otherwise we can do the fold if the first index of the GEP is a zero
1235 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
1236 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
Chris Lattner5d606a02002-11-04 16:43:32 +00001237 } else if (Src->getOperand(Src->getNumOperands()-1) ==
1238 Constant::getNullValue(Type::LongTy)) {
1239 // If the src gep ends with a constant array index, merge this get into
1240 // it, even if we have a non-zero array index.
1241 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1);
1242 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001243 }
1244
1245 if (!Indices.empty())
1246 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
Chris Lattnerc59af1d2002-08-17 22:21:59 +00001247
1248 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
1249 // GEP of global variable. If all of the indices for this GEP are
1250 // constants, we can promote this to a constexpr instead of an instruction.
1251
1252 // Scan for nonconstants...
1253 std::vector<Constant*> Indices;
1254 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
1255 for (; I != E && isa<Constant>(*I); ++I)
1256 Indices.push_back(cast<Constant>(*I));
1257
1258 if (I == E) { // If they are all constants...
Chris Lattner46b3d302003-04-16 22:40:51 +00001259 Constant *CE =
Chris Lattnerc59af1d2002-08-17 22:21:59 +00001260 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
1261
1262 // Replace all uses of the GEP with the new constexpr...
1263 return ReplaceInstUsesWith(GEP, CE);
1264 }
Chris Lattnerca081252001-12-14 16:52:21 +00001265 }
1266
Chris Lattnerca081252001-12-14 16:52:21 +00001267 return 0;
1268}
1269
Chris Lattner1085bdf2002-11-04 16:18:53 +00001270Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
1271 // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
1272 if (AI.isArrayAllocation()) // Check C != 1
1273 if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
1274 const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
Chris Lattnera2620ac2002-11-09 00:49:43 +00001275 AllocationInst *New = 0;
Chris Lattner1085bdf2002-11-04 16:18:53 +00001276
1277 // Create and insert the replacement instruction...
1278 if (isa<MallocInst>(AI))
1279 New = new MallocInst(NewTy, 0, AI.getName(), &AI);
Chris Lattnera2620ac2002-11-09 00:49:43 +00001280 else {
1281 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
Chris Lattner1085bdf2002-11-04 16:18:53 +00001282 New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
Chris Lattnera2620ac2002-11-09 00:49:43 +00001283 }
Chris Lattner1085bdf2002-11-04 16:18:53 +00001284
1285 // Scan to the end of the allocation instructions, to skip over a block of
1286 // allocas if possible...
1287 //
1288 BasicBlock::iterator It = New;
1289 while (isa<AllocationInst>(*It)) ++It;
1290
1291 // Now that I is pointing to the first non-allocation-inst in the block,
1292 // insert our getelementptr instruction...
1293 //
1294 std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
1295 Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
1296
1297 // Now make everything use the getelementptr instead of the original
1298 // allocation.
1299 ReplaceInstUsesWith(AI, V);
1300 return &AI;
1301 }
1302 return 0;
1303}
1304
Chris Lattner0f1d8a32003-06-26 05:06:25 +00001305/// GetGEPGlobalInitializer - Given a constant, and a getelementptr
1306/// constantexpr, return the constant value being addressed by the constant
1307/// expression, or null if something is funny.
1308///
1309static Constant *GetGEPGlobalInitializer(Constant *C, ConstantExpr *CE) {
1310 if (CE->getOperand(1) != Constant::getNullValue(Type::LongTy))
1311 return 0; // Do not allow stepping over the value!
1312
1313 // Loop over all of the operands, tracking down which value we are
1314 // addressing...
1315 for (unsigned i = 2, e = CE->getNumOperands(); i != e; ++i)
1316 if (ConstantUInt *CU = dyn_cast<ConstantUInt>(CE->getOperand(i))) {
1317 ConstantStruct *CS = cast<ConstantStruct>(C);
1318 if (CU->getValue() >= CS->getValues().size()) return 0;
1319 C = cast<Constant>(CS->getValues()[CU->getValue()]);
1320 } else if (ConstantSInt *CS = dyn_cast<ConstantSInt>(CE->getOperand(i))) {
1321 ConstantArray *CA = cast<ConstantArray>(C);
1322 if ((uint64_t)CS->getValue() >= CA->getValues().size()) return 0;
1323 C = cast<Constant>(CA->getValues()[CS->getValue()]);
1324 } else
1325 return 0;
1326 return C;
1327}
1328
1329Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
1330 Value *Op = LI.getOperand(0);
1331 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Op))
1332 Op = CPR->getValue();
1333
1334 // Instcombine load (constant global) into the value loaded...
1335 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
Chris Lattnerbdb0ce02003-07-22 21:46:59 +00001336 if (GV->isConstant() && !GV->isExternal())
Chris Lattner0f1d8a32003-06-26 05:06:25 +00001337 return ReplaceInstUsesWith(LI, GV->getInitializer());
1338
1339 // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded...
1340 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op))
1341 if (CE->getOpcode() == Instruction::GetElementPtr)
1342 if (ConstantPointerRef *G=dyn_cast<ConstantPointerRef>(CE->getOperand(0)))
1343 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G->getValue()))
Chris Lattnerbdb0ce02003-07-22 21:46:59 +00001344 if (GV->isConstant() && !GV->isExternal())
Chris Lattner0f1d8a32003-06-26 05:06:25 +00001345 if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE))
1346 return ReplaceInstUsesWith(LI, V);
1347 return 0;
1348}
1349
1350
Chris Lattner9eef8a72003-06-04 04:46:00 +00001351Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
1352 // Change br (not X), label True, label False to: br X, label False, True
Chris Lattner45789ac2003-06-05 20:12:51 +00001353 if (BI.isConditional() && !isa<Constant>(BI.getCondition()))
Chris Lattnere967b342003-06-04 05:10:11 +00001354 if (Value *V = dyn_castNotVal(BI.getCondition())) {
1355 BasicBlock *TrueDest = BI.getSuccessor(0);
1356 BasicBlock *FalseDest = BI.getSuccessor(1);
1357 // Swap Destinations and condition...
1358 BI.setCondition(V);
1359 BI.setSuccessor(0, FalseDest);
1360 BI.setSuccessor(1, TrueDest);
1361 return &BI;
1362 }
Chris Lattner9eef8a72003-06-04 04:46:00 +00001363 return 0;
1364}
Chris Lattner1085bdf2002-11-04 16:18:53 +00001365
Chris Lattnerca081252001-12-14 16:52:21 +00001366
Chris Lattner99f48c62002-09-02 04:59:56 +00001367void InstCombiner::removeFromWorkList(Instruction *I) {
1368 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
1369 WorkList.end());
1370}
1371
Chris Lattner113f4f42002-06-25 16:13:24 +00001372bool InstCombiner::runOnFunction(Function &F) {
Chris Lattner260ab202002-04-18 17:39:14 +00001373 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +00001374
Chris Lattner260ab202002-04-18 17:39:14 +00001375 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +00001376
1377 while (!WorkList.empty()) {
1378 Instruction *I = WorkList.back(); // Get an instruction from the worklist
1379 WorkList.pop_back();
1380
Misha Brukman632df282002-10-29 23:06:16 +00001381 // Check to see if we can DCE or ConstantPropagate the instruction...
Chris Lattner99f48c62002-09-02 04:59:56 +00001382 // Check to see if we can DIE the instruction...
1383 if (isInstructionTriviallyDead(I)) {
1384 // Add operands to the worklist...
1385 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1386 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1387 WorkList.push_back(Op);
1388
1389 ++NumDeadInst;
1390 BasicBlock::iterator BBI = I;
1391 if (dceInstruction(BBI)) {
1392 removeFromWorkList(I);
1393 continue;
1394 }
1395 }
1396
Misha Brukman632df282002-10-29 23:06:16 +00001397 // Instruction isn't dead, see if we can constant propagate it...
Chris Lattner99f48c62002-09-02 04:59:56 +00001398 if (Constant *C = ConstantFoldInstruction(I)) {
1399 // Add operands to the worklist...
1400 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1401 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
1402 WorkList.push_back(Op);
Chris Lattnerc6509f42002-12-05 22:41:53 +00001403 ReplaceInstUsesWith(*I, C);
1404
Chris Lattner99f48c62002-09-02 04:59:56 +00001405 ++NumConstProp;
1406 BasicBlock::iterator BBI = I;
1407 if (dceInstruction(BBI)) {
1408 removeFromWorkList(I);
1409 continue;
1410 }
1411 }
1412
Chris Lattnerca081252001-12-14 16:52:21 +00001413 // Now that we have an instruction, try combining it to simplify it...
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001414 if (Instruction *Result = visit(*I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +00001415 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +00001416 // Should we replace the old instruction with a new one?
Chris Lattner053c0932002-05-14 15:24:07 +00001417 if (Result != I) {
1418 // Instructions can end up on the worklist more than once. Make sure
1419 // we do not process an instruction that has been deleted.
Chris Lattner99f48c62002-09-02 04:59:56 +00001420 removeFromWorkList(I);
Chris Lattner260ab202002-04-18 17:39:14 +00001421 ReplaceInstWithInst(I, Result);
Chris Lattner113f4f42002-06-25 16:13:24 +00001422 } else {
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001423 BasicBlock::iterator II = I;
1424
1425 // If the instruction was modified, it's possible that it is now dead.
1426 // if so, remove it.
1427 if (dceInstruction(II)) {
1428 // Instructions may end up in the worklist more than once. Erase them
1429 // all.
Chris Lattner99f48c62002-09-02 04:59:56 +00001430 removeFromWorkList(I);
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001431 Result = 0;
1432 }
Chris Lattner053c0932002-05-14 15:24:07 +00001433 }
Chris Lattner260ab202002-04-18 17:39:14 +00001434
Chris Lattnerae7a0d32002-08-02 19:29:35 +00001435 if (Result) {
1436 WorkList.push_back(Result);
1437 AddUsesToWorkList(*Result);
1438 }
Chris Lattner260ab202002-04-18 17:39:14 +00001439 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +00001440 }
1441 }
1442
Chris Lattner260ab202002-04-18 17:39:14 +00001443 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +00001444}
1445
1446Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +00001447 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +00001448}